GammaLib  1.7.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-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 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 "GBase.hpp"
33 #include "GEvents.hpp"
34 #include "GTime.hpp"
35 #include "GFunction.hpp"
36 
37 /* __ Forward declarations _______________________________________________ */
38 class GVector;
39 class GMatrixSparse;
40 class GModel;
41 class GModels;
42 class GModelPar;
43 class GResponse;
44 class GXmlElement;
45 
46 
47 /***********************************************************************//**
48  * @class GObservation
49  *
50  * @brief Abstract observation base class
51  *
52  * This class provides an abstract interface for an observation. The
53  * observation collects information about the instrument, holds the measured
54  * events, and provides information about the analysis definition.
55  *
56  * The response() method returns a pointer to the response function. The
57  * derived classes have to make sure that this method never returns NULL.
58  *
59  * The method model() returns the probability for an event to be measured
60  * with a given instrument direction, a given energy and at a given time,
61  * given a source model and an instrument pointing direction.
62  * The method npred() returns the total number of expected events within the
63  * analysis region for a given source model and a given instrument pointing
64  * direction.
65  * The methods are defined as virtual and can be overloaded by derived classes
66  * that implement instrument specific observations in order to optimize the
67  * execution speed for data analysis.
68  ***************************************************************************/
69 class GObservation : public GBase {
70 
71 public:
72  // Constructors and destructors
73  GObservation(void);
74  GObservation(const GObservation& obs);
75  virtual ~GObservation(void);
76 
77  // Operators
78  virtual GObservation& operator=(const GObservation& obs);
79 
80  // Pure virtual methods
81  virtual void clear(void) = 0;
82  virtual GObservation* clone(void) const = 0;
83  virtual std::string classname(void) const = 0;
84  virtual void response(const GResponse& rsp) = 0;
85  virtual const GResponse* response(void) const = 0;
86  virtual std::string instrument(void) const = 0;
87  virtual double ontime(void) const = 0;
88  virtual double livetime(void) const = 0;
89  virtual double deadc(const GTime& time = GTime()) const = 0;
90  virtual void read(const GXmlElement& xml) = 0;
91  virtual void write(GXmlElement& xml) const = 0;
92  virtual std::string print(const GChatter& chatter = NORMAL) const = 0;
93 
94  // Virtual methods
95  virtual GEvents* events(void);
96  virtual const GEvents* events(void) const;
97  virtual void events(const GEvents& events);
98  virtual double likelihood(const GModels& models,
99  GVector* gradient,
100  GMatrixSparse* curvature,
101  double* npred) const;
102  virtual double model(const GModels& models,
103  const GEvent& event,
104  GVector* gradient = NULL) const;
105  virtual int nobserved(void) const;
106  virtual double npred(const GModels& models,
107  GVector* gradient = NULL) const;
108  virtual double npred(const GModel& model) const;
109  virtual double model_grad(const GModel& model,
110  const GModelPar& par,
111  const GEvent& event) const;
112  virtual double npred_grad(const GModel& model,
113  const GModelPar& par) const;
114  virtual void remove_response_cache(const std::string& name);
115 
116  // Implemented methods
117  bool has_events(void) const;
118  void name(const std::string& name);
119  void id(const std::string& id);
120  void statistic(const std::string& statistic);
121  const std::string& name(void) const;
122  const std::string& id(void) const;
123  const std::string& statistic(void) const;
124 
125 protected:
126  // Protected methods
127  void init_members(void);
128  void copy_members(const GObservation& obs);
129  void free_members(void);
130 
131  // Likelihood methods
132  virtual double likelihood_poisson_unbinned(const GModels& models,
133  GVector* gradient,
134  GMatrixSparse* curvature,
135  double* npred) const;
136  virtual double likelihood_poisson_binned(const GModels& models,
137  GVector* gradient,
138  GMatrixSparse* curvature,
139  double* npred) const;
140  virtual double likelihood_gaussian_binned(const GModels& models,
141  GVector* gradient,
142  GMatrixSparse* curvature,
143  double* npred) const;
144 
145  // Model gradient kernel classes
146  class model_func : public GFunction {
147  public:
148  model_func(const GObservation* parent,
149  const GModel* model,
150  GModelPar* par,
151  const GEvent* event) :
152  m_parent(parent),
153  m_model(model),
154  m_par(par),
155  m_event(event) { }
156  double eval(const double& x);
157  protected:
158  const GObservation* m_parent; //!< Observation
159  const GModel* m_model; //!< Model
160  GModelPar* m_par; //!< Model parameter
161  const GEvent* m_event; //!< Event
162  };
163 
164  // Npred methods
165  virtual double npred_spec(const GModel& model, const GTime& obsTime) const;
166 
167  // Npred kernel classes
168  class npred_kern : public GFunction {
169  public:
170  npred_kern(const GObservation* parent,
171  const GModel* model) :
172  m_parent(parent),
173  m_model(model) { }
174  double eval(const double& x);
175  protected:
176  const GObservation* m_parent; //!< Observation
177  const GModel* m_model; //!< Model
178  };
179 
180  class npred_spec_kern : public GFunction {
181  public:
183  const GModel* model,
184  const GTime* obsTime) :
185  m_parent(parent),
186  m_model(model),
187  m_time(obsTime) { }
188  double eval(const double& x);
189  protected:
190  const GObservation* m_parent; //!< Observation
191  const GModel* m_model; //!< Model
192  const GTime* m_time; //!< Time
193  };
194 
195  // Npred gradient kernel classes
196  class npred_func : public GFunction {
197  public:
198  npred_func(const GObservation* parent,
199  const GModel* model,
200  GModelPar* par) :
201  m_parent(parent),
202  m_model(model),
203  m_par(par) { }
204  double eval(const double& x);
205  protected:
206  const GObservation* m_parent; //!< Observation
207  const GModel* m_model; //!< Model
208  GModelPar* m_par; //!< Model parameter
209  };
210 
211  // Protected data area
212  std::string m_name; //!< Observation name
213  std::string m_id; //!< Observation identifier
214  std::string m_statistic; //!< Optimizer statistic
215  GEvents* m_events; //!< Pointer to event container
216 };
217 
218 
219 /***********************************************************************//**
220  * @brief Signal if observation has events
221  *
222  * @return True if observation contains events.
223  ***************************************************************************/
224 inline
225 bool GObservation::has_events(void) const
226 {
227  return (m_events != NULL);
228 }
229 
230 
231 /***********************************************************************//**
232  * @brief Set observation name
233  *
234  * @param[in] name Observation name.
235  *
236  * Set name of the observation.
237  ***************************************************************************/
238 inline
239 void GObservation::name(const std::string& name)
240 {
241  m_name = name;
242  return;
243 }
244 
245 
246 /***********************************************************************//**
247  * @brief Set observation identifier
248  *
249  * @param[in] id Observation identifier.
250  *
251  * Set identifier of the observation.
252  ***************************************************************************/
253 inline
254 void GObservation::id(const std::string& id)
255 {
256  m_id = id;
257  return;
258 }
259 
260 
261 /***********************************************************************//**
262  * @brief Set optimizer statistic
263  *
264  * @param[in] statistic Optimizer statistic.
265  *
266  * Set optimizer statistic for the observation.
267  ***************************************************************************/
268 inline
269 void GObservation::statistic(const std::string& statistic)
270 {
272  return;
273 }
274 
275 
276 /***********************************************************************//**
277  * @brief Return observation name
278  *
279  * @return Observation name.
280  ***************************************************************************/
281 inline
282 const std::string& GObservation::name(void) const
283 {
284  return (m_name);
285 }
286 
287 
288 /***********************************************************************//**
289  * @brief Return observation identifier
290  *
291  * @return Observation identifier.
292  ***************************************************************************/
293 inline
294 const std::string& GObservation::id(void) const
295 {
296  return (m_id);
297 }
298 
299 
300 /***********************************************************************//**
301  * @brief Return optimizer statistic
302  *
303  * @return Optimizer statistic.
304  ***************************************************************************/
305 inline
306 const std::string& GObservation::statistic(void) const
307 {
308  return (m_statistic);
309 }
310 
311 #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:97
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...
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:47
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.
Definition: GModelPar.hpp:87
Model container class.
Definition: GModels.hpp:150
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.
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:67
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)