GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GCTAResponse.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GCTAResponse.cpp - CTA Response class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2019 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GCTAResponse.cpp
23  * @brief CTA response class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GEvent.hpp"
32 #include "GSource.hpp"
33 #include "GObservations.hpp"
34 #include "GModelSpatial.hpp"
36 #include "GCTAResponse.hpp"
37 #include "GCTASupport.hpp"
38 
39 /* __ Method name definitions ____________________________________________ */
40 #define G_IRF "GCTAResponse::irf(GEvent&, GSource&, GObservation&)"
41 
42 /* __ Macros _____________________________________________________________ */
43 
44 /* __ Coding definitions _________________________________________________ */
45 
46 /* __ Debug definitions __________________________________________________ */
47 
48 /* __ Constants __________________________________________________________ */
49 
50 
51 /*==========================================================================
52  = =
53  = Constructors/destructors =
54  = =
55  ==========================================================================*/
56 
57 /***********************************************************************//**
58  * @brief Void constructor
59  *
60  * Constructs void CTA response.
61  ***************************************************************************/
63 {
64  // Initialise members
65  init_members();
66 
67  // Return
68  return;
69 }
70 
71 
72 /***********************************************************************//**
73  * @brief Copy constructor
74  *
75  * @param[in] rsp CTA response.
76  *
77  * Constructs CTA response by making a deep copy of an existing object.
78  **************************************************************************/
80 {
81  // Initialise members
82  init_members();
83 
84  // Copy members
85  copy_members(rsp);
86 
87  // Return
88  return;
89 }
90 
91 
92 /***********************************************************************//**
93  * @brief Destructor
94  *
95  * Destroys instance of CTA response object.
96  ***************************************************************************/
98 {
99  // Free members
100  free_members();
101 
102  // Return
103  return;
104 }
105 
106 
107 /*==========================================================================
108  = =
109  = Operators =
110  = =
111  ==========================================================================*/
112 
113 /***********************************************************************//**
114  * @brief Assignment operator
115  *
116  * @param[in] rsp CTA response.
117  * @return CTA response.
118  *
119  * Assigns CTA response object to another CTA response object. The assignment
120  * performs a deep copy of all information, hence the original object from
121  * which the assignment has been performed can be destroyed after this
122  * operation without any loss of information.
123  ***************************************************************************/
125 {
126  // Execute only if object is not identical
127  if (this != &rsp) {
128 
129  // Copy base class members
130  this->GResponse::operator=(rsp);
131 
132  // Free members
133  free_members();
134 
135  // Initialise members
136  init_members();
137 
138  // Copy members
139  copy_members(rsp);
140 
141  } // endif: object was not identical
142 
143  // Return this object
144  return *this;
145 }
146 
147 
148 /*==========================================================================
149  = =
150  = Public methods =
151  = =
152  ==========================================================================*/
153 
154 /***********************************************************************//**
155  * @brief Return instrument response
156  *
157  * @param[in] event Event.
158  * @param[in] source Source.
159  * @param[in] obs Observation.
160  * @return Instrument response.
161  *
162  * Returns the instrument response for a given event, source and observation.
163  ***************************************************************************/
164 double GCTAResponse::irf(const GEvent& event,
165  const GSource& source,
166  const GObservation& obs) const
167 {
168  // Initialise IRF value
169  double irf = 0.0;
170 
171  // Set IRF value attributes
172  std::string name = obs.id() + "::" + source.name();
173  const GCTAInstDir& dir = gammalib::cta_dir(G_IRF, event);
174  const GEnergy& ereco = event.energy();
175  const GEnergy& etrue = source.energy();
176 
177  // Signal if spatial model has free parameters
178  bool has_free_pars = source.model()->has_free_pars();
179 
180  // If the spatial model component has free parameters, or the response
181  // cache should not be used, or the cache does not contain the requested
182  // IRF value then compute the IRF value for the spatial model.
183  if (has_free_pars ||
184  !m_use_irf_cache ||
185  !m_irf_cache.contains(name, dir, ereco, etrue, &irf)) {
186 
187  // Compute IRF for spatial model
188  switch (source.model()->code()) {
190  irf = irf_ptsrc(event, source, obs);
191  break;
193  irf = irf_radial(event, source, obs);
194  break;
196  irf = irf_elliptical(event, source, obs);
197  break;
199  irf = irf_diffuse(event, source, obs);
200  break;
202  irf = irf_composite(event, source, obs);
203  break;
204  default:
205  break;
206  }
207 
208  } // endif: computed spatial model
209 
210  // If the spatial model has no free parameters and the response cache
211  // should be used then put the IRF value in the response cache.
212  if (!has_free_pars && m_use_irf_cache) {
213  m_irf_cache.set(name, dir, ereco, etrue, irf);
214  }
215 
216  // Return IRF value
217  return irf;
218 }
219 
220 
221 /***********************************************************************//**
222  * @brief Remove response cache for model
223  *
224  * @param[in] name Model name.
225  *
226  * Remove response cache for model @p name from response cache.
227  ***************************************************************************/
228 void GCTAResponse::remove_response_cache(const std::string& name)
229 {
230  // Remove model from response caches
231  m_irf_cache.remove(name);
232  m_nroi_cache.remove(name);
233 
234  // Return
235  return;
236 }
237 
238 
239 /*==========================================================================
240  = =
241  = Private methods =
242  = =
243  ==========================================================================*/
244 
245 /***********************************************************************//**
246  * @brief Initialise class members
247  ***************************************************************************/
249 {
250  // Initialize members
251  m_use_irf_cache = true; //!< Switched on by default
252  m_use_nroi_cache = true; //!< Switched on by default
253  m_irf_cache.clear();
255 
256  // Return
257  return;
258 }
259 
260 
261 /***********************************************************************//**
262  * @brief Copy class members
263  *
264  * @param[in] rsp Response to be copied
265  ***************************************************************************/
267 {
268  // Copy members
271  m_irf_cache = rsp.m_irf_cache;
273 
274  // Return
275  return;
276 }
277 
278 
279 /***********************************************************************//**
280  * @brief Delete class members
281  ***************************************************************************/
283 {
284  // Return
285  return;
286 }
287 
288 
289 /***********************************************************************//**
290  * @brief Return instrument response to composite source
291  *
292  * @param[in] event Observed event.
293  * @param[in] source Source.
294  * @param[in] obs Observation.
295  * @return Instrument response to composite source.
296  *
297  * Returns the instrument response to a specified composite source.
298  ***************************************************************************/
300  const GSource& source,
301  const GObservation& obs) const
302 {
303  // Initialise IRF
304  double irf = 0.0;
305 
306  // Get pointer to composite model
307  const GModelSpatialComposite* model =
308  dynamic_cast<const GModelSpatialComposite*>(source.model());
309 
310  // Loop over model components
311  for (int i = 0; i < model->components(); ++i) {
312 
313  // Get pointer to spatial component
314  GModelSpatial* spat = const_cast<GModelSpatial*>(model->component(i));
315 
316  // Create new GSource object
317  GSource src(source.name(), spat, source.energy(), source.time());
318 
319  // Compute irf value
320  irf += this->irf(event, src, obs) * model->scale(i);
321 
322  }
323 
324  // Divide by number of model components
325  double sum = model->sum_of_scales();
326  if (sum > 0.0) {
327  irf /= sum;
328  }
329 
330  // Return IRF value
331  return irf;
332 }
virtual void remove_response_cache(const std::string &name)
Remove response cache for model.
const GCTAInstDir & cta_dir(const std::string &origin, const GEvent &event)
Retrieve CTA instrument direction from generic event.
GCTAResponseCache m_irf_cache
bool has_free_pars(void) const
Checks if the spatial model has free parameters.
const GModelSpatial * component(const int &index) const
Returns pointer to spatial component element.
GCTAResponseCache m_nroi_cache
Abstract interface for the event classes.
Definition: GEvent.hpp:71
double sum(const GVector &vector)
Computes vector sum.
Definition: GVector.cpp:901
Abstract spatial model base class interface definition.
void copy_members(const GCTAResponse &rsp)
Copy class members.
virtual double irf_radial(const GEvent &event, const GSource &source, const GObservation &obs) const =0
const std::string & name(void) const
Return model name.
Definition: GSource.hpp:114
Source class definition.
virtual GClassCode code(void) const =0
void init_members(void)
Initialise class members.
bool m_use_nroi_cache
Control usage of nroi cache.
void id(const std::string &id)
Set observation identifier.
double sum_of_scales(void) const
Returns sum of all model scales.
virtual GCTAResponse & operator=(const GCTAResponse &rsp)
Assignment operator.
Definition of support function used by CTA classes.
bool contains(const std::string &name, const GEnergy &ereco, const GEnergy &etrue, double *value=NULL) const
Check if cache contains a value for specific parameters.
virtual ~GCTAResponse(void)
Destructor.
void clear(void)
Clear CTA response cache.
Abstract event base class definition.
void free_members(void)
Delete class members.
const GTime & time(void) const
Return photon arrival time.
Definition: GSource.hpp:156
#define G_IRF
void remove(const std::string &name)
Remove cache for source.
virtual double irf_ptsrc(const GEvent &event, const GSource &source, const GObservation &obs) const =0
Spatial composite model class interface definition.
const GModelSpatial * model(void) const
Return spatial model component.
Definition: GSource.hpp:128
Abstract observation base class.
const GEnergy & energy(void) const
Return photon energy.
Definition: GSource.hpp:142
Spatial composite model.
CTA response abstract base class definition.
CTA instrument response function class.
GCTAResponse(void)
Void constructor.
virtual GResponse & operator=(const GResponse &rsp)
Assignment operator.
Definition: GResponse.cpp:125
virtual double irf_composite(const GEvent &event, const GSource &source, const GObservation &obs) const
Return instrument response to composite source.
int components(void) const
Return number of model components.
bool m_use_irf_cache
Control usage of irf cache.
CTA instrument direction class.
Definition: GCTAInstDir.hpp:59
Abstract spatial model base class.
virtual double irf(const GEvent &event, const GPhoton &photon, const GObservation &obs) const =0
void set(const std::string &name, const GEnergy &ereco, const GEnergy &etrue, const double &value)
Set cache value.
virtual double irf_elliptical(const GEvent &event, const GSource &source, const GObservation &obs) const =0
Abstract instrument response base class.
Definition: GResponse.hpp:67
Class that handles gamma-ray sources.
Definition: GSource.hpp:53
virtual double irf_diffuse(const GEvent &event, const GSource &source, const GObservation &obs) const =0
Observations container class interface definition.
double scale(const int &index) const
Returns scale of spatial component.
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48