GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GCTAOnOffObservation.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GCTAOnOffObservation.hpp - CTA On/Off observation class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2013-2021 by Chia-Chun Lu & Christoph Deil *
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 GCTAOnOffObservation.hpp
23  * @brief CTA On/Off observation class definition
24  * @author Chia-Chun Lu & Christoph Deil & Pierrick Martin
25  */
26 
27 #ifndef GCTAONOFFOBSERVATION_HPP
28 #define GCTAONOFFOBSERVATION_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include <vector>
33 #include "GPha.hpp"
34 #include "GArf.hpp"
35 #include "GRmf.hpp"
36 #include "GFunction.hpp"
37 #include "GObservation.hpp"
38 #include "GNodeArray.hpp"
39 
40 /* __ Forward declarations _______________________________________________ */
41 class GModels;
42 class GModelSpatial;
43 class GOptimizerPars;
44 class GObservations;
45 class GSkyRegions;
46 class GCTAObservation;
47 class GCTAResponse;
48 class GCTAResponseIrf;
49 
50 
51 /***********************************************************************//**
52  * @class GCTAOnOffObservation
53  *
54  * @brief CTA On/Off observation class
55  *
56  * This class defines a CTA On/Off observation. An On/Off observation is
57  * defined by two spectra, one for an On region including the source of
58  * interest, and one for an Off region including only background. The
59  * response of an On/Off observation is given by the Auxiliary Response File
60  * (ARF) and the Redistribution Matrix File (RMF).
61  *
62  * The class uses GPha objects to store the On and Off spectra, and GArf and
63  * GRmf objects to store the response information. On and Off regions are
64  * defined via GSkyRegionMap objects which are essentially finely-pixellized
65  * skymaps that allow handling arbitrarily complex shapes
66  ***************************************************************************/
68 
69 public:
70  // Constructors and destructors
72  GCTAOnOffObservation(const bool& dummy, const std::string& instrument);
74  GCTAOnOffObservation(const GPha& pha_on,
75  const GPha& pha_off,
76  const GArf& arf,
77  const GRmf& rmf);
79  const GModels& models,
80  const std::string& srcname,
81  const GEbounds& etrue,
82  const GEbounds& ereco,
83  const GSkyRegions& on,
84  const GSkyRegions& off,
85  const bool& use_model_bkg = true);
87  const GCTAObservation& obs_off,
88  const GModels& models,
89  const std::string& srcname,
90  const GEbounds& etrue,
91  const GEbounds& ereco,
92  const GSkyRegions& on,
93  const GSkyRegions& off,
94  const bool& use_model_bkg = true);
96  virtual ~GCTAOnOffObservation(void);
97 
98  // Operators
100 
101  // Implemented pure virtual methods
102  virtual void clear(void);
103  virtual GCTAOnOffObservation* clone(void) const;
104  virtual std::string classname(void) const;
105  virtual void response(const GResponse& rsp);
106  virtual const GCTAResponse* response(void) const;
107  virtual std::string instrument(void) const;
108  virtual double ontime(void) const;
109  virtual double livetime(void) const;
110  virtual double deadc(const GTime& time = GTime()) const;
111  virtual void read(const GXmlElement& xml);
112  virtual void write(GXmlElement& xml) const;
113  virtual std::string print(const GChatter& chatter = NORMAL) const;
114 
115  // Overloaded virtual methods
116  virtual double likelihood(const GModels& models,
117  GVector* gradient,
118  GMatrixSparse* curvature,
119  double* npred) const;
120  virtual int nobserved(void) const;
121 
122  // Other methods
123  void instrument(const std::string& instrument);
124  bool has_response(void) const;
125  const GPha& on_spec(void) const;
126  const GPha& off_spec(void) const;
127  const GArf& arf(void) const;
128  const GRmf& rmf(void) const;
129  GPha model_gamma(const GModels& models) const;
130  GPha model_background(const GModels& models) const;
131 
132 protected:
133  // Protected methods
134  void init_members(void);
135  void copy_members(const GCTAOnOffObservation& obs);
136  void free_members(void);
137  void set_exposure(void);
138  void check_consistency(const std::string& method) const;
139  GArf arf_stacked(const GArf& arf, const GEnergy& emin, const GEnergy& emax) const;
140  GRmf rmf_stacked(const GRmf& rmf, const GEnergy& emin, const GEnergy& emax) const;
141  void set(const GCTAObservation& obs_on,
142  const GCTAObservation& obs_off,
143  const GModels& models,
144  const std::string& srcname,
145  const GSkyRegions& on,
146  const GSkyRegions& off,
147  const bool& use_model_bkg);
148  void compute_arf(const GCTAObservation& obs,
149  const GModelSpatial& spatial,
150  const GSkyRegions& on);
151  void compute_arf_cut(const GCTAObservation& obs,
152  const GModelSpatial& spatial,
153  const GSkyRegions& on);
154  void compute_bgd(const GCTAObservation& obs,
155  const GSkyRegions& off,
156  const GModels& models,
157  const bool& use_model_bkg);
158  void compute_alpha(const GCTAObservation& obs_on,
159  const GCTAObservation& obs_off,
160  const GSkyRegions& on,
161  const GSkyRegions& off,
162  const GModels& models,
163  const bool& use_model_bkg);
164  void compute_rmf(const GCTAObservation& obs,
165  const GSkyRegions& on);
166  void apply_ebounds(const GCTAObservation& obs);
167  double arf_rad_max(const GCTAObservation& obs, const GSkyRegions& on) const;
168  double N_gamma(const GModels& models, const int& ibin, GVector* grad) const;
169  double N_bgd(const GModels& models, const int& ibin, GVector* grad) const;
170 
171  // Likelihood methods
172  virtual double likelihood_cstat(const GModels& models,
173  GVector* gradient,
174  GMatrixSparse* curvature,
175  double* npred) const;
176  virtual double likelihood_wstat(const GModels& models,
177  GVector* gradient,
178  GMatrixSparse* curvature,
179  double* npred) const;
180  virtual double wstat_value(const double& non,
181  const double& noff,
182  const double& alpha,
183  const double& ngam,
184  double& nonpred,
185  double& nbgd,
186  double& dlogLdsky,
187  double& d2logLdsky2) const;
188 
189  // Protected data members
190  std::string m_instrument; //!< Instrument name
191  GCTAResponse* m_response; //!< Pointer to IRFs
192  double m_ontime; //!< Ontime of On observation (seconds)
193  double m_livetime; //!< Livetime of On observation (seconds)
194  double m_deadc; //!< Deadtime correction (livetime/ontime)
195  GPha m_on_spec; //!< On counts spectrum
196  GPha m_off_spec; //!< Off counts spectrum
197  GArf m_arf; //!< Auxiliary Response Function vector
198  GRmf m_rmf; //!< Redistribution matrix
199 };
200 
201 
202 /***********************************************************************//**
203  * @brief Return class name
204  *
205  * @return String containing the class name ("GCTAOnOffObservation").
206  ***************************************************************************/
207 inline
208 std::string GCTAOnOffObservation::classname(void) const
209 {
210  return ("GCTAOnOffObservation");
211 }
212 
213 
214 /***********************************************************************//**
215  * @brief Return instrument
216  *
217  * @return Instrument.
218  ***************************************************************************/
219 inline
220 std::string GCTAOnOffObservation::instrument(void) const
221 {
222  return (m_instrument);
223 }
224 
225 
226 /***********************************************************************//**
227  * @brief Return ontime
228  *
229  * @return Ontime in seconds.
230  ***************************************************************************/
231 inline
233 {
234  return (m_ontime);
235 }
236 
237 
238 /***********************************************************************//**
239  * @brief Return livetime
240  *
241  * @return Livetime in seconds.
242  ***************************************************************************/
243 inline
245 {
246  return (m_livetime);
247 }
248 
249 
250 /***********************************************************************//**
251  * @brief Return deadtime correction factor
252  *
253  * @param[in] time Time.
254  * @return Deadtime correction factor.
255  *
256  * Returns the deadtime correction factor. Optionally, this method takes a
257  * @p time argument that takes provision for returning the deadtime
258  * correction factor as function of time.
259  *
260  * The deadtime correction factor is defined as the livetime divided by the
261  * ontime.
262  ***************************************************************************/
263 inline
264 double GCTAOnOffObservation::deadc(const GTime& time) const
265 {
266  return (m_deadc);
267 }
268 
269 
270 /***********************************************************************//**
271  * @brief Signal if observation contains response information
272  *
273  * @return True if observation contains response information.
274  ***************************************************************************/
275 inline
277 {
278  return (m_response != NULL);
279 }
280 
281 
282 /***********************************************************************//**
283  * @brief Set instrument
284  *
285  * @param[in] instrument Instrument.
286  ***************************************************************************/
287 inline
288 void GCTAOnOffObservation::instrument(const std::string& instrument)
289 {
291  return;
292 }
293 
294 
295 /***********************************************************************//**
296  * @brief Return On spectrum
297  *
298  * @return On spectrum.
299  ***************************************************************************/
300 inline
302 {
303  return (m_on_spec);
304 }
305 
306 
307 /***********************************************************************//**
308  * @brief Return Off spectrum
309  *
310  * @return Off spectrum.
311  ***************************************************************************/
312 inline
314 {
315  return (m_off_spec);
316 }
317 
318 
319 /***********************************************************************//**
320  * @brief Return Auxiliary Response File
321  *
322  * @return Auxiliary Response File.
323  ***************************************************************************/
324 inline
325 const GArf& GCTAOnOffObservation::arf(void) const
326 {
327  return (m_arf);
328 }
329 
330 
331 /***********************************************************************//**
332  * @brief Return Redistribution Matrix File
333  *
334  * @return Redistribution Matrix File.
335  ***************************************************************************/
336 inline
337 const GRmf& GCTAOnOffObservation::rmf(void) const
338 {
339  return (m_rmf);
340 }
341 
342 
343 /***********************************************************************//**
344  * @brief Return number of observed events
345  *
346  * @return Number of observed events.
347  ***************************************************************************/
348 inline
350 {
351  return (int(m_on_spec.counts()+0.5));
352 }
353 
354 #endif /* GCTAONOFFOBSERVATION_HPP */
double m_livetime
Livetime of On observation (seconds)
virtual double likelihood_wstat(const GModels &models, GVector *gradient, GMatrixSparse *curvature, double *npred) const
Evaluate log-likelihood function for On/Off analysis in the case of Poisson signal with measured Pois...
std::string m_instrument
Instrument name.
void apply_ebounds(const GCTAObservation &obs)
Apply CTA observation energy boundaries to On/Off observation.
void init_members(void)
Initialise class members.
virtual const GCTAResponse * response(void) const
Return pointer to CTA response function.
virtual ~GCTAOnOffObservation(void)
Destructor.
virtual GCTAOnOffObservation * clone(void) const
Clone instance.
Sparse matrix class interface definition.
virtual void clear(void)
Clear instance.
virtual double livetime(void) const
Return livetime.
virtual double npred(const GModels &models, GVector *gradients=NULL) const
Return total number (and optionally gradients) of predicted counts for all models.
double counts(void) const
Number of counts in spectrum.
Definition: GPha.cpp:703
Optimizer parameter container class.
void compute_bgd(const GCTAObservation &obs, const GSkyRegions &off, const GModels &models, const bool &use_model_bkg)
Compute background rate in Off regions.
XML element node class.
Definition: GXmlElement.hpp:48
Redistribution Matrix File class.
Definition: GRmf.hpp:55
GPha model_gamma(const GModels &models) const
Time class.
Definition: GTime.hpp:55
const GPha & off_spec(void) const
Return Off spectrum.
virtual double wstat_value(const double &non, const double &noff, const double &alpha, const double &ngam, double &nonpred, double &nbgd, double &dlogLdsky, double &d2logLdsky2) const
Evaluate log-likelihood value in energy bin for On/Off analysis by profiling over the number of backg...
const GArf & arf(void) const
Return Auxiliary Response File.
GCTAResponse * m_response
Pointer to IRFs.
Auxiliary Response File class.
Definition: GArf.hpp:54
GArf m_arf
Auxiliary Response Function vector.
void compute_alpha(const GCTAObservation &obs_on, const GCTAObservation &obs_off, const GSkyRegions &on, const GSkyRegions &off, const GModels &models, const bool &use_model_bkg)
Compute vector of alpha parameters.
double N_gamma(const GModels &models, const int &ibin, GVector *grad) const
XSPEC Pulse Height Analyzer class definition.
const GRmf & rmf(void) const
Return Redistribution Matrix File.
void set(const GCTAObservation &obs_on, const GCTAObservation &obs_off, const GModels &models, const std::string &srcname, const GSkyRegions &on, const GSkyRegions &off, const bool &use_model_bkg)
Set On/Off observation from a CTA observation.
Node array class interface definition.
Model container class.
Definition: GModels.hpp:152
virtual void read(const GXmlElement &xml)
Read On/Off observation from an XML element.
GCTAOnOffObservation & operator=(const GCTAOnOffObservation &obs)
Assignment operator.
XSPEC Redistribution Matrix File class definition.
void set_exposure(void)
Set ontime, livetime and deadtime correction factor.
Energy boundaries container class.
Definition: GEbounds.hpp:60
Single parameter function abstract base class definition.
void copy_members(const GCTAOnOffObservation &obs)
Copy class members.
virtual void write(GXmlElement &xml) const
write observation to an xml element
XSPEC Auxiliary Response File class definition.
bool has_response(void) const
Signal if observation contains response information.
virtual double likelihood_cstat(const GModels &models, GVector *gradient, GMatrixSparse *curvature, double *npred) const
Evaluate log-likelihood function for On/Off analysis in the case of Poisson signal with modeled Poiss...
virtual std::string instrument(void) const
Return instrument.
GChatter
Definition: GTypemaps.hpp:33
const GPha & on_spec(void) const
Return On spectrum.
virtual double deadc(const GTime &time=GTime()) const
Return deadtime correction factor.
Abstract observation base class.
GRmf m_rmf
Redistribution matrix.
Abstract observation base class interface definition.
GPha model_background(const GModels &models) const
Observation container class.
CTA instrument response function class.
double m_deadc
Deadtime correction (livetime/ontime)
CTA instrument response function class.
Sky region container class.
Definition: GSkyRegions.hpp:56
void compute_arf_cut(const GCTAObservation &obs, const GModelSpatial &spatial, const GSkyRegions &on)
Compute ARF of On/Off observation for a IRF with radius cut.
void compute_arf(const GCTAObservation &obs, const GModelSpatial &spatial, const GSkyRegions &on)
Compute ARF of On/Off observation.
void free_members(void)
Delete class members.
double N_bgd(const GModels &models, const int &ibin, GVector *grad) const
virtual std::string print(const GChatter &chatter=NORMAL) const
Print On/Off observation information.
GCTAOnOffObservation(void)
Void constructor.
virtual double ontime(void) const
Return ontime.
GPha m_off_spec
Off counts spectrum.
void check_consistency(const std::string &method) const
Check consistency of data members.
virtual int nobserved(void) const
Return number of observed events.
virtual double likelihood(const GModels &models, GVector *gradient, GMatrixSparse *curvature, double *npred) const
Evaluate log-likelihood function for On/Off analysis.
GRmf rmf_stacked(const GRmf &rmf, const GEnergy &emin, const GEnergy &emax) const
Return RMF for stacking.
void compute_rmf(const GCTAObservation &obs, const GSkyRegions &on)
Compute RMF of On/Off observation.
GPha m_on_spec
On counts spectrum.
Abstract spatial model base class.
double m_ontime
Ontime of On observation (seconds)
Vector class.
Definition: GVector.hpp:46
virtual std::string classname(void) const
Return class name.
CTA observation class.
Abstract instrument response base class.
Definition: GResponse.hpp:77
GArf arf_stacked(const GArf &arf, const GEnergy &emin, const GEnergy &emax) const
Return ARF for stacking.
Pulse Height Analyzer class.
Definition: GPha.hpp:64
double arf_rad_max(const GCTAObservation &obs, const GSkyRegions &on) const
Check if effective area IRF has a radius cut.
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48
CTA On/Off observation class.