GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GModel.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GModel.hpp - Abstract virtual model base class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2009-2025 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 GModel.hpp
23 * @brief Abstract model base class interface definition
24 * @author Juergen Knoedlseder
25 */
26
27#ifndef GMODEL_HPP
28#define GMODEL_HPP
29
30/* __ Includes ___________________________________________________________ */
31#include <vector>
32#include <string>
33#include "GBase.hpp"
34#include "GModelPar.hpp"
36#include "GXmlElement.hpp"
37#include "GEnergy.hpp"
38#include "GTime.hpp"
39
40/* __ Forward declarations _______________________________________________ */
41class GVector;
42class GMatrixSparse;
43class GEvent;
44class GObservation;
45
46
47/***********************************************************************//**
48 * @class GModel
49 *
50 * @brief Abstract model class
51 *
52 * This class implements a parametric model. The eval() method evaluates the
53 * model for a given event and observation. If the gradients parameter is set
54 * to true, the eval() method also computes analytical parameter gradients if
55 * they exist.
56 *
57 * A model has the following attributes:
58 * - @p name
59 * - @p type
60 * - @p instruments
61 * - @p ids
62 * - @p scales
63 *
64 * The model @p name is a text string that names the model. The name()
65 * methods allow setting and retrieving the model name. The model handling
66 * does not actual depend on the value of this text string.
67 *
68 * The model @p type is a text string that specifies the kind of the model.
69 * Examples are @p PointSource, @p ExtendedSource or @p DiffuseSource for
70 * sky models. The type() method allows retrieving the model type. The model
71 * handling does not actual depend on the value of this text string.
72 *
73 * The model @p instruments is a list of text strings that specifies the
74 * instruments to which the model applies. The is_valid() method will check
75 * a given instrument name against that list to verify if the model applies
76 * to an instrument. The instruments() method returns a comma separated
77 * list of all instruments. Another instance of this method allows setting
78 * the instrument list from a comma separated list. If the @p instruments
79 * list is empty, the model applies to all instruments.
80 *
81 * The model @p ids is a list of text strings that specifies the observation
82 * identifiers to which the model applies. This allows specifying of models
83 * for a specific list of observations. The is_valid() method will check
84 * a given observation identifier against that list to verify if the model
85 * applies. The ids() method returns a comma separated list of all
86 * observation identifiers. Another instance of this method allows setting
87 * the observation identifiers from a comma separated list. If the @p ids
88 * list is empty, the model applies to all observation identifiers.
89 *
90 * The model @p scales is a list of model parameters that specify an
91 * instrument dependent scaling factor. This allows to prescale a model for
92 * a given instrument. The scale() methods allow to set and to retrieve the
93 * scale factor for a specific instrument.
94 *
95 * As the class holds simply a collection of model parameters, it should
96 * neither deal with allocation and deallocation, nor with cloning of
97 * model parameters. This will be done by the classes that actually
98 * implement the model parameters.
99 ***************************************************************************/
100class GModel : public GBase {
101
102public:
103 // Constructors and destructors
104 GModel(void);
105 explicit GModel(const GXmlElement& xml);
106 GModel(const GModel& model);
107 virtual ~GModel(void);
108
109 // Operators
110 virtual GModel& operator=(const GModel& model);
111 virtual GModelPar& operator[](const int& index);
112 virtual const GModelPar& operator[](const int& index) const;
113 virtual GModelPar& operator[](const std::string& name);
114 virtual const GModelPar& operator[](const std::string& name) const;
115
116 // Pure virtual methods
117 virtual void clear(void) = 0;
118 virtual GModel* clone(void) const = 0;
119 virtual std::string classname(void) const = 0;
120 virtual std::string type(void) const = 0;
121 virtual bool is_constant(void) const = 0;
122 virtual double eval(const GEvent& event,
123 const GObservation& obs,
124 const bool& gradients = false) const = 0;
125 virtual GVector eval(const GObservation& obs,
126 GMatrixSparse* gradients = NULL,
127 const int& offset = 0) const = 0;
128 virtual double npred(const GEnergy& obsEng, const GTime& obsTime,
129 const GObservation& obs) const = 0;
130 virtual void read(const GXmlElement& xml) = 0;
131 virtual void write(GXmlElement& xml) const = 0;
132 virtual std::string print(const GChatter& chatter = NORMAL) const = 0;
133
134 // Virtual methods (to be eventually overloaded by derived classes)
135 virtual void setup(const GObservation& obs) const;
136
137 // Implemented methods
138 int size(void) const;
139 int scales(void) const;
140 GModelPar& at(const int& index);
141 const GModelPar& at(const int& index) const;
142 bool has_par(const std::string& name) const;
143 bool has_scales(void) const;
144 const std::string& name(void) const;
145 void name(const std::string& name);
146 const double& ts(void) const;
147 void ts(const double& ts);
148 const bool& tscalc(void) const;
149 void tscalc(const bool& tscalc);
150 const bool& has_ts(void) const;
151 std::string instruments(void) const;
152 void instruments(const std::string& instruments);
153 GModelPar& scale(const int& index);
154 const GModelPar& scale(const int& index) const;
155 GModelPar scale(const std::string& instrument) const;
156 void scale(const GModelPar& par);
157 std::string ids(void) const;
158 void ids(const std::string& ids);
159 bool is_valid(const std::string& instruments,
160 const std::string& ids) const;
161 const GModelAssociations& associations(void) const;
163 const bool& has_eval_indices(void) const;
164 const std::vector<int>& eval_indices(void) const;
165
166protected:
167 // Protected methods
168 void init_members(void);
169 void copy_members(const GModel& model);
170 void free_members(void);
171 void read_attributes(const GXmlElement& xml);
172 void write_attributes(GXmlElement& xml) const;
173 std::string print_attributes(void) const;
174 void read_scales(const GXmlElement& xml);
175 void write_scales(GXmlElement& xml) const;
176
177 // Protected members
178 std::string m_name; //!< Model name
179 std::vector<std::string> m_instruments; //!< Instruments to which model applies
180 std::vector<GModelPar> m_scales; //!< Model instrument scale factors
181 std::vector<std::string> m_ids; //!< Identifiers to which model applies
182 std::vector<GModelPar*> m_pars; //!< Pointers to all model parameters
183 GModelAssociations m_associations; //!< Model associations
184 bool m_has_ts; //!< Signals if TS is available
185 bool m_has_tscalc; //!< Signals if tscalc attribute is available
186 bool m_tscalc; //!< Signals if TS should be computed
187 double m_ts; //!< Test Statistic of the model
188
189 // Protected members for efficient gradient access
190 mutable bool m_has_eval_inx; //!< Signal that parameter indices
191 //!< updated by eval() method exist
192 mutable std::vector<int> m_eval_inx; //!< List of parameter indices updated
193 //!< by eval() method
194};
195
196
197/***********************************************************************//**
198 * @brief Returns reference to model parameter by index
199 *
200 * @param[in] index Parameter index [0,...,size()-1].
201 * @return Reference to model parameter.
202 *
203 * Returns a reference to the model parameter of the specified @p index.
204 ***************************************************************************/
205inline
207{
208 // Return reference
209 return *(m_pars[index]);
210}
211
212
213/***********************************************************************//**
214 * @brief Returns reference to model parameter by index (const version)
215 *
216 * @param[in] index Parameter index [0,...,size()-1].
217 * @return Const reference to model parameter.
218 *
219 * Returns a const reference to the model parameter of the specified
220 ***************************************************************************/
221inline
222const GModelPar& GModel::operator[](const int& index) const
223{
224 // Return reference
225 return *(m_pars[index]);
226}
227
228
229/***********************************************************************//**
230 * @brief Return number of parameters in model
231 *
232 * @return Number of parameters in model.
233 *
234 * Returns the number of parameters in the model.
235 ***************************************************************************/
236inline
237int GModel::size(void) const
238{
239 return (int)m_pars.size();
240}
241
242
243/***********************************************************************//**
244 * @brief Return number of scale parameters in model
245 *
246 * @return Number of scale parameters in model.
247 *
248 * Returns the number of scale parameters in the model.
249 ***************************************************************************/
250inline
251int GModel::scales(void) const
252{
253 return (int)m_scales.size();
254}
255
256
257/***********************************************************************//**
258 * @brief Return parameter name
259 *
260 * @return Parameter name.
261 *
262 * Returns the parameter name.
263 ***************************************************************************/
264inline
265const std::string& GModel::name(void) const
266{
267 return m_name;
268}
269
270
271/***********************************************************************//**
272 * @brief Set parameter name
273 *
274 * @param[in] name Parameter name.
275 *
276 * Set the parameter name.
277 ***************************************************************************/
278inline
279void GModel::name(const std::string& name)
280{
281 m_name = name;
282 return;
283}
284
285
286/***********************************************************************//**
287 * @brief Return Test Statistic value
288 *
289 * @return Test Statistic value.
290 *
291 * Returns the Test Statistic value. The Test Statistic value is twice the
292 * difference between the log-likelihood of the null hypothesis and the
293 * alternative hypothesis.
294 ***************************************************************************/
295inline
296const double& GModel::ts(void) const
297{
298 return m_ts;
299}
300
301
302/***********************************************************************//**
303 * @brief Set Test Statistic value
304 *
305 * @param[in] ts Test Statistic value.
306 *
307 * Set the Test Statistic value. The Test Statistic value is twice the
308 * difference between the log-likelihood of the null hypothesis and the
309 * alternative hypothesis.
310 ***************************************************************************/
311inline
312void GModel::ts(const double& ts)
313{
314 m_ts = ts;
315 m_has_ts = true; //!< Signals that TS is now available
316 return;
317}
318
319
320/***********************************************************************//**
321 * @brief Return Test Statistic computation flag
322 *
323 * @return Test Statistic computation flag; true is Test Statistic should be
324 * computed.
325 *
326 * Returns the flag that signals whether Test Statistic values should be
327 * computed.
328 ***************************************************************************/
329inline
330const bool& GModel::tscalc(void) const
331{
332 return m_tscalc;
333}
334
335
336/***********************************************************************//**
337 * @brief Set Test Statistic computation flag
338 *
339 * @param[in] tscalc Test Statistic computation flag.
340 *
341 * Set the flag that signals whether Test Statistic values should be
342 * computed.
343 ***************************************************************************/
344inline
345void GModel::tscalc(const bool& tscalc)
346{
348 m_has_tscalc = true; //!< Signals that tscalc is now available
349 return;
350}
351
352
353/***********************************************************************//**
354 * @brief Signals that model has scales
355 *
356 * @return True if model has scale factors.
357 ***************************************************************************/
358inline
359bool GModel::has_scales(void) const
360{
361 return (!m_scales.empty());
362}
363
364
365/***********************************************************************//**
366 * @brief Signals that model has Test Statistics value
367 *
368 * @return True if model has TS value.
369 ***************************************************************************/
370inline
371const bool& GModel::has_ts(void) const
372{
373 return m_has_ts;
374}
375
376
377/***********************************************************************//**
378 * @brief Return model associations
379 *
380 * @return Model associations.
381 *
382 * Returns model associations.
383 ***************************************************************************/
384inline
386{
387 return m_associations;
388}
389
390
391/***********************************************************************//**
392 * @brief Set model associations
393 *
394 * @param[in] associations Model associations.
395 *
396 * Set the model associations.
397 ***************************************************************************/
398inline
400{
402 return;
403}
404
405
406/***********************************************************************//**
407 * @brief Signals that parameter indices updated by eval() method were set
408 *
409 * @return True if parameter indices updated by eval() method were set.
410 ***************************************************************************/
411inline
412const bool& GModel::has_eval_indices(void) const
413{
414 return m_has_eval_inx;
415}
416
417
418/***********************************************************************//**
419 * @brief Return vector of parameter indices updated by eval() method
420 *
421 * @return Vector of parameter indices updated by eval() method.
422 ***************************************************************************/
423inline
424const std::vector<int>& GModel::eval_indices(void) const
425{
426 return m_eval_inx;
427}
428
429#endif /* GMODEL_HPP */
Definition of interface for all GammaLib classes.
Energy value class definition.
Model associations container class definition.
Model parameter class interface definition.
Time class interface definition.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
XML element node class interface definition.
Interface class for all GammaLib classes.
Definition GBase.hpp:52
Class that handles energies in a unit independent way.
Definition GEnergy.hpp:48
Abstract interface for the event classes.
Definition GEvent.hpp:71
Sparse matrix class interface definition.
Model associations container class.
Model parameter class.
Definition GModelPar.hpp:87
Abstract model class.
Definition GModel.hpp:100
const GModelAssociations & associations(void) const
Return model associations.
Definition GModel.hpp:385
const bool & has_ts(void) const
Signals that model has Test Statistics value.
Definition GModel.hpp:371
void read_scales(const GXmlElement &xml)
Read instrument scales from XML element.
Definition GModel.cpp:864
virtual ~GModel(void)
Destructor.
Definition GModel.cpp:110
virtual std::string classname(void) const =0
Return class name.
const bool & tscalc(void) const
Return Test Statistic computation flag.
Definition GModel.hpp:330
GModelPar & scale(const int &index)
Returns reference to scale parameter by index.
Definition GModel.cpp:384
virtual void read(const GXmlElement &xml)=0
std::vector< std::string > m_instruments
Instruments to which model applies.
Definition GModel.hpp:179
std::vector< GModelPar * > m_pars
Pointers to all model parameters.
Definition GModel.hpp:182
void write_scales(GXmlElement &xml) const
Write instrument scales into XML element.
Definition GModel.cpp:912
virtual std::string type(void) const =0
virtual bool is_constant(void) const =0
const double & ts(void) const
Return Test Statistic value.
Definition GModel.hpp:296
bool has_scales(void) const
Signals that model has scales.
Definition GModel.hpp:359
const bool & has_eval_indices(void) const
Signals that parameter indices updated by eval() method were set.
Definition GModel.hpp:412
void write_attributes(GXmlElement &xml) const
Write model attributes.
Definition GModel.cpp:738
virtual GVector eval(const GObservation &obs, GMatrixSparse *gradients=NULL, const int &offset=0) const =0
std::string m_name
Model name.
Definition GModel.hpp:178
bool is_valid(const std::string &instruments, const std::string &ids) const
Verifies if model is valid for a given instrument and identifier.
Definition GModel.cpp:585
virtual std::string print(const GChatter &chatter=NORMAL) const =0
Print content of object.
void free_members(void)
Delete class members.
Definition GModel.cpp:687
void init_members(void)
Initialise class members.
Definition GModel.cpp:637
void copy_members(const GModel &model)
Copy class members.
Definition GModel.cpp:663
std::string print_attributes(void) const
Print model attributes.
Definition GModel.cpp:785
std::vector< int > m_eval_inx
Definition GModel.hpp:192
int size(void) const
Return number of parameters in model.
Definition GModel.hpp:237
bool m_tscalc
Signals if TS should be computed.
Definition GModel.hpp:186
bool m_has_tscalc
Signals if tscalc attribute is available.
Definition GModel.hpp:185
bool has_par(const std::string &name) const
Checks if parameter name exists.
Definition GModel.cpp:299
GModel(void)
Void constructor.
Definition GModel.cpp:57
virtual GModelPar & operator[](const int &index)
Returns reference to model parameter by index.
Definition GModel.hpp:206
std::string instruments(void) const
Returns instruments to which model applies.
Definition GModel.cpp:325
std::string ids(void) const
Returns observation identifiers to which model applies.
Definition GModel.cpp:517
virtual double npred(const GEnergy &obsEng, const GTime &obsTime, const GObservation &obs) const =0
bool m_has_eval_inx
Definition GModel.hpp:190
GModelPar & at(const int &index)
Returns reference to model parameter by index.
Definition GModel.cpp:254
virtual GModel & operator=(const GModel &model)
Assignment operator.
Definition GModel.cpp:132
bool m_has_ts
Signals if TS is available.
Definition GModel.hpp:184
std::vector< std::string > m_ids
Identifiers to which model applies.
Definition GModel.hpp:181
void read_attributes(const GXmlElement &xml)
Read model attributes.
Definition GModel.cpp:699
virtual double eval(const GEvent &event, const GObservation &obs, const bool &gradients=false) const =0
virtual void write(GXmlElement &xml) const =0
virtual void clear(void)=0
Clear object.
double m_ts
Test Statistic of the model.
Definition GModel.hpp:187
virtual GModel * clone(void) const =0
Clones object.
GModelAssociations m_associations
Model associations.
Definition GModel.hpp:183
const std::string & name(void) const
Return parameter name.
Definition GModel.hpp:265
int scales(void) const
Return number of scale parameters in model.
Definition GModel.hpp:251
std::vector< GModelPar > m_scales
Model instrument scale factors.
Definition GModel.hpp:180
virtual void setup(const GObservation &obs) const
Setup model for a given observation.
Definition GModel.cpp:235
const std::vector< int > & eval_indices(void) const
Return vector of parameter indices updated by eval() method.
Definition GModel.hpp:424
Abstract observation base class.
Time class.
Definition GTime.hpp:55
Vector class.
Definition GVector.hpp:46
XML element node class.