GammaLib  2.0.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GObservation.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GObservation.hpp - Abstract observation base class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2008-2020 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 GObservation.hpp
23  * @brief Abstract observation base class interface definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GOBSERVATION_HPP
28 #define GOBSERVATION_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include <vector>
33 #include "GBase.hpp"
34 #include "GEvents.hpp"
35 #include "GTime.hpp"
36 #include "GFunction.hpp"
37 #include "GModelPar.hpp"
38 
39 /* __ Forward declarations _______________________________________________ */
40 class GVector;
41 class GMatrixSparse;
42 class GModel;
43 class GModels;
44 class GResponse;
45 class GXmlElement;
46 
47 
48 /***********************************************************************//**
49  * @class GObservation
50  *
51  * @brief Abstract observation base class
52  *
53  * This class provides an abstract interface for an observation. The
54  * observation collects information about the instrument, holds the measured
55  * events, and provides information about the analysis definition.
56  *
57  * The response() method returns a pointer to the response function. The
58  * derived classes have to make sure that this method never returns NULL.
59  *
60  * The method model() returns the probability for an event to be measured
61  * with a given instrument direction, a given energy and at a given time,
62  * given a source model and an instrument pointing direction.
63  * The method npred() returns the total number of expected events within the
64  * analysis region for a given source model and a given instrument pointing
65  * direction.
66  * The methods are defined as virtual and can be overloaded by derived classes
67  * that implement instrument specific observations in order to optimize the
68  * execution speed for data analysis.
69  ***************************************************************************/
70 class GObservation : public GBase {
71 
72 public:
73  // Constructors and destructors
74  GObservation(void);
75  GObservation(const GObservation& obs);
76  virtual ~GObservation(void);
77 
78  // Operators
79  virtual GObservation& operator=(const GObservation& obs);
80 
81  // Pure virtual methods
82  virtual void clear(void) = 0;
83  virtual GObservation* clone(void) const = 0;
84  virtual std::string classname(void) const = 0;
85  virtual void response(const GResponse& rsp) = 0;
86  virtual const GResponse* response(void) const = 0;
87  virtual std::string instrument(void) const = 0;
88  virtual double ontime(void) const = 0;
89  virtual double livetime(void) const = 0;
90  virtual double deadc(const GTime& time = GTime()) const = 0;
91  virtual void read(const GXmlElement& xml) = 0;
92  virtual void write(GXmlElement& xml) const = 0;
93  virtual std::string print(const GChatter& chatter = NORMAL) const = 0;
94 
95  // Virtual methods
96  virtual GEvents* events(void);
97  virtual const GEvents* events(void) const;
98  virtual void events(const GEvents& events);
99  virtual double likelihood(const GModels& models,
100  GVector* gradient,
101  GMatrixSparse* curvature,
102  double* npred) const;
103  virtual double model(const GModels& models,
104  const GEvent& event,
105  GVector* gradient = NULL) const;
106  virtual GVector model(const GModels& models,
107  GMatrixSparse* gradient = NULL) const;
108  virtual int nobserved(void) const;
109  virtual double npred(const GModels& models,
110  GVector* gradient = NULL) const;
111  virtual double npred(const GModel& model) const;
112  virtual double model_grad(const GModel& model,
113  const GModelPar& par,
114  const GEvent& event) const;
115  virtual GVector model_grad(const GModel& model,
116  const GModelPar& par) const;
117  virtual double npred_grad(const GModel& model,
118  const GModelPar& par) const;
119  virtual void remove_response_cache(const std::string& name);
120 
121  // Implemented methods
122  bool has_events(void) const;
123  bool has_gradient(const GModel& model,
124  const GModelPar& par) const;
125  void name(const std::string& name);
126  void id(const std::string& id);
127  void statistic(const std::string& statistic);
128  const std::string& name(void) const;
129  const std::string& id(void) const;
130  const std::string& statistic(void) const;
131  void computed_gradient(const GModel& model,
132  const GModelPar& par) const;
133 
134 protected:
135  // Protected methods
136  void init_members(void);
137  void copy_members(const GObservation& obs);
138  void free_members(void);
139 
140  // Likelihood methods
141  virtual double likelihood_poisson_unbinned(const GModels& models,
142  GVector* gradient,
143  GMatrixSparse* curvature,
144  double* npred) const;
145  virtual double likelihood_poisson_binned(const GModels& models,
146  GVector* gradient,
147  GMatrixSparse* curvature,
148  double* npred) const;
149  virtual double likelihood_gaussian_binned(const GModels& models,
150  GVector* gradient,
151  GMatrixSparse* curvature,
152  double* npred) const;
153 
154  // Model gradient kernel classes
155  class model_func : public GFunction {
156  public:
157  model_func(const GObservation* parent,
158  const GModel* model,
159  GModelPar* par,
160  const GEvent* event) :
161  m_parent(parent),
162  m_model(model),
163  m_par(par),
164  m_event(event) { }
165  double eval(const double& x);
166  protected:
167  const GObservation* m_parent; //!< Observation
168  const GModel* m_model; //!< Model
169  GModelPar* m_par; //!< Model parameter
170  const GEvent* m_event; //!< Event
171  };
172 
173  // Npred methods
174  virtual double npred_spec(const GModel& model, const GTime& obsTime) const;
175 
176  // Npred kernel classes
177  class npred_kern : public GFunction {
178  public:
179  npred_kern(const GObservation* parent,
180  const GModel* model) :
181  m_parent(parent),
182  m_model(model) { }
183  double eval(const double& x);
184  protected:
185  const GObservation* m_parent; //!< Observation
186  const GModel* m_model; //!< Model
187  };
188 
189  class npred_spec_kern : public GFunction {
190  public:
192  const GModel* model,
193  const GTime* obsTime) :
194  m_parent(parent),
195  m_model(model),
196  m_time(obsTime) { }
197  double eval(const double& x);
198  protected:
199  const GObservation* m_parent; //!< Observation
200  const GModel* m_model; //!< Model
201  const GTime* m_time; //!< Time
202  };
203 
204  // Npred gradient kernel classes
205  class npred_func : public GFunction {
206  public:
207  npred_func(const GObservation* parent,
208  const GModel* model,
209  GModelPar* par) :
210  m_parent(parent),
211  m_model(model),
212  m_par(par) { }
213  double eval(const double& x);
214  protected:
215  const GObservation* m_parent; //!< Observation
216  const GModel* m_model; //!< Model
217  GModelPar* m_par; //!< Model parameter
218  };
219 
220  // Protected data area
221  std::string m_name; //!< Observation name
222  std::string m_id; //!< Observation identifier
223  std::string m_statistic; //!< Optimizer statistic
224  GEvents* m_events; //!< Pointer to event container
225 
226  // Stack of identifiers of parameters with gradients
227  mutable std::vector<std::string> m_pars_with_gradients;
228 };
229 
230 
231 /***********************************************************************//**
232  * @brief Signal if observation has events
233  *
234  * @return True if observation contains events.
235  ***************************************************************************/
236 inline
237 bool GObservation::has_events(void) const
238 {
239  return (m_events != NULL);
240 }
241 
242 
243 /***********************************************************************//**
244  * @brief Set observation name
245  *
246  * @param[in] name Observation name.
247  *
248  * Set name of the observation.
249  ***************************************************************************/
250 inline
251 void GObservation::name(const std::string& name)
252 {
253  m_name = name;
254  return;
255 }
256 
257 
258 /***********************************************************************//**
259  * @brief Set observation identifier
260  *
261  * @param[in] id Observation identifier.
262  *
263  * Set identifier of the observation.
264  ***************************************************************************/
265 inline
266 void GObservation::id(const std::string& id)
267 {
268  m_id = id;
269  return;
270 }
271 
272 
273 /***********************************************************************//**
274  * @brief Set optimizer statistic
275  *
276  * @param[in] statistic Optimizer statistic.
277  *
278  * Set optimizer statistic for the observation.
279  ***************************************************************************/
280 inline
281 void GObservation::statistic(const std::string& statistic)
282 {
284  return;
285 }
286 
287 
288 /***********************************************************************//**
289  * @brief Return observation name
290  *
291  * @return Observation name.
292  ***************************************************************************/
293 inline
294 const std::string& GObservation::name(void) const
295 {
296  return (m_name);
297 }
298 
299 
300 /***********************************************************************//**
301  * @brief Return observation identifier
302  *
303  * @return Observation identifier.
304  ***************************************************************************/
305 inline
306 const std::string& GObservation::id(void) const
307 {
308  return (m_id);
309 }
310 
311 
312 /***********************************************************************//**
313  * @brief Return optimizer statistic
314  *
315  * @return Optimizer statistic.
316  ***************************************************************************/
317 inline
318 const std::string& GObservation::statistic(void) const
319 {
320  return (m_statistic);
321 }
322 
323 #endif /* GOBSERVATION_HPP */
const std::string & statistic(void) const
Return optimizer statistic.
virtual double livetime(void) const =0
virtual std::string classname(void) const =0
Return class name.
Abstract model class.
Definition: GModel.hpp:100
double eval(const double &x)
Integration kernel for npred_spec() method.
virtual double likelihood_gaussian_binned(const GModels &models, GVector *gradient, GMatrixSparse *curvature, double *npred) const
Evaluate log-likelihood function for Gaussian statistic and binned analysis (version with working arr...
const GEvent * m_event
Event.
virtual double likelihood_poisson_unbinned(const GModels &models, GVector *gradient, GMatrixSparse *curvature, double *npred) const
Evaluate log-likelihood function for Poisson statistic and unbinned analysis (version with working ar...
void computed_gradient(const GModel &model, const GModelPar &par) const
Signals that an analytical gradient was computed for a model parameter.
const GObservation * m_parent
Observation.
double eval(const double &x)
Integration kernel for npred() method.
virtual void read(const GXmlElement &xml)=0
virtual double ontime(void) const =0
std::string m_name
Observation name.
Sparse matrix class interface definition.
virtual std::string instrument(void) const =0
virtual double npred(const GModels &models, GVector *gradient=NULL) const
Return total number (and optionally gradient) of predicted counts for all models. ...
virtual double likelihood(const GModels &models, GVector *gradient, GMatrixSparse *curvature, double *npred) const
Compute likelihood function.
model_func(const GObservation *parent, const GModel *model, GModelPar *par, const GEvent *event)
virtual double npred_grad(const GModel &model, const GModelPar &par) const
Returns parameter gradient of Npred.
GEvents * m_events
Pointer to event container.
const GObservation * m_parent
Observation.
Abstract interface for the event classes.
Definition: GEvent.hpp:71
Definition of interface for all GammaLib classes.
XML element node class.
Definition: GXmlElement.hpp:48
const GModel * m_model
Model.
std::string m_id
Observation identifier.
virtual double model_grad(const GModel &model, const GModelPar &par, const GEvent &event) const
Returns parameter gradient of model for a given event.
Time class.
Definition: GTime.hpp:54
virtual void remove_response_cache(const std::string &name)
Response cache removal hook.
virtual double npred_spec(const GModel &model, const GTime &obsTime) const
Integrates spatially integrated Npred kernel spectrally.
virtual std::string print(const GChatter &chatter=NORMAL) const =0
Print content of object.
virtual int nobserved(void) const
Return total number of observed events.
void free_members(void)
Delete class members.
virtual ~GObservation(void)
Destructor.
Model parameter class interface definition.
Model parameter class.
Definition: GModelPar.hpp:87
Model container class.
Definition: GModels.hpp:152
virtual double model(const GModels &models, const GEvent &event, GVector *gradient=NULL) const
Return model value and (optionally) gradient.
virtual double likelihood_poisson_binned(const GModels &models, GVector *gradient, GMatrixSparse *curvature, double *npred) const
Evaluate log-likelihood function for Poisson statistic and binned analysis (version with working arra...
const std::string & id(void) const
Return observation identifier.
Single parameter function abstract base class definition.
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
virtual void clear(void)=0
Clear object.
GObservation(void)
Void constructor.
const std::string & name(void) const
Return observation name.
double eval(const double &x)
Model function evaluation for gradient computation.
const GModel * m_model
Model.
const GObservation * m_parent
Observation.
virtual double deadc(const GTime &time=GTime()) const =0
virtual GObservation * clone(void) const =0
Clones object.
GChatter
Definition: GTypemaps.hpp:33
npred_spec_kern(const GObservation *parent, const GModel *model, const GTime *obsTime)
GModelPar * m_par
Model parameter.
GModelPar * m_par
Model parameter.
void init_members(void)
Initialise class members.
Abstract observation base class.
virtual void write(GXmlElement &xml) const =0
const GModel * m_model
Model.
Abstract event container class.
Definition: GEvents.hpp:66
const GTime * m_time
Time.
Single parameter function abstract base class.
Definition: GFunction.hpp:44
std::string m_statistic
Optimizer statistic.
virtual GObservation & operator=(const GObservation &obs)
Assignment operator.
virtual GEvents * events(void)
Return events.
const GModel * m_model
Model.
std::vector< std::string > m_pars_with_gradients
bool has_events(void) const
Signal if observation has events.
npred_func(const GObservation *parent, const GModel *model, GModelPar *par)
double eval(const double &x)
Npred function evaluation for gradient computation.
Vector class.
Definition: GVector.hpp:46
void copy_members(const GObservation &obs)
Copy class members.
Abstract instrument response base class.
Definition: GResponse.hpp:77
bool has_gradient(const GModel &model, const GModelPar &par) const
Check whether a model parameter has an analytical gradient.
virtual const GResponse * response(void) const =0
Abstract event container class interface definition.
Time class interface definition.
const GObservation * m_parent
Observation.
npred_kern(const GObservation *parent, const GModel *model)