GammaLib 2.0.0
Loading...
Searching...
No Matches
GModels.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GModels.hpp - Model container class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2009-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 GModels.hpp
23 * @brief Model container class definition
24 * @author Juergen Knoedlseder
25 */
26
27#ifndef GMODELS_HPP
28#define GMODELS_HPP
29
30/* __ Includes ___________________________________________________________ */
31#include <string>
32#include "GContainer.hpp"
33
34/* __ Forward declarations _______________________________________________ */
35class GEvent;
36class GObservation;
37class GModel;
38class GFilename;
39class GVector;
40class GMatrixSparse;
41class GOptimizerPars;
42class GXml;
43
44
45/***********************************************************************//**
46 * @class GModels
47 *
48 * @brief Model container class
49 *
50 * This container class collects models of type GModel that are used to
51 * describe the data. The names of all models in the container have to be
52 * unique, i.e. every name can occur only once. This allows for accessing the
53 * models by name and by index.
54 *
55 * The GModels class provides methods to manage and to access the models
56 * in the container. The number of models in the container is retrieved
57 * using the size() method. The is_empty() method can be used to check
58 * whether the container is empty or whether it contains models:
59 *
60 * GModels models; // Allocate container
61 * int n = models.size(); // Number of models in container
62 * if (models.is_empty()) // Check for emptiness
63 * std::cout << "Empty container";
64 *
65 * Access operators exist for accessing of models by index or by name:
66 *
67 * GModel* mptr = models[i]; // Get i'th model
68 * GModel* mptr = models["Crab"]; // Get a model named "Crab"
69 *
70 * The index access operator does not check the validity of the provided
71 * index. For index validation, use the at() method:
72 *
73 * GModel* mptr = models.at(i); // Get i'th model with index check
74 *
75 * The append() method add a model to the container:
76 *
77 * models.append(model); // Append model
78 *
79 * The append() method clones the model that is passed as argument. The
80 * method returns a pointer to the cloned model so that the attributes of
81 * the cloned model can be manipulated:
82 *
83 * GModel* mptr = models.append(model);
84 *
85 * The insert() methods insert a model before a given index or before
86 * a model with a given name (the methods also return a pointer to the
87 * cloned model):
88 *
89 * models.insert(i, model); // Insert before i'th model
90 * models.insert("Crab", model); // Insert before "Crab" model
91 *
92 * The set() methods replace an existing model by index or by name (also
93 * these methods return a pointer to the cloned model):
94 *
95 * models.set(i, model); // Replace i'th model
96 * models.set("Crab", model); // Replace "Crab" model
97 *
98 * The remove() methods remove an existing model by index or by name:
99 *
100 * models.remove(i); // Remove i'th model
101 * models.remove("Crab"); // Remove "Crab" model
102 *
103 * The existence of a model with a given name can be checked using
104 *
105 * if (models.contains("Crab"))
106 * std::cout << "We have the Crab!";
107 *
108 * The extend() method extends a container by all models found in another
109 * container:
110 *
111 * models.extend(other_models); // Extend container
112 *
113 * For repeated container manipulations, a given @p number of model slots can
114 * be reserved using
115 *
116 * models.reserve(number); // Reserves model slots
117 *
118 * which will speed up the memory allocations for new models.
119 *
120 * Models can be saved into or loaded from an XML file using
121 *
122 * models.save("mymodels.xml"); // Save models in XML file
123 * models.load("mymodels.xml"); // Load models from XML file
124 *
125 * The models can also be loaded upon construction from an XML file:
126 *
127 * GModels models("mymodels.xml"); // Construct by loading models from XML file
128 *
129 * The class can also directly operate on a GXml object using the read() and
130 * write() methods:
131 *
132 * GXml xml; // Allocate GXml object
133 * models.write(xml); // Write into GXml object
134 * models.read(xml); // Read models from GXml object
135 *
136 * The sum of all models in the container are evaluated for a given @p event
137 * and @p observation using the eval() method:
138 *
139 * double value = models.eval(event, observation);
140 *
141 * If the eval() method is called with the optional gradients parameter set
142 * to true, i.e.
143 *
144 * double value = models.eval(event, observation, true);
145 *
146 * the method computes also the parameter gradients for all free model
147 * parameters that have an analytical parameter gradient.
148 *
149 * The only member of GModels is a list of model pointers. The class handles
150 * the proper allocation and deallocation of the model memory.
151 ***************************************************************************/
152class GModels : public GContainer {
153
154public:
155 // Constructors and destructors
156 GModels(void);
157 GModels(const GModels& models);
158 explicit GModels(const GFilename& filename);
159 virtual ~GModels(void);
160
161 // Operators
162 GModels& operator=(const GModels& models);
163 GModel* operator[](const int& index);
164 const GModel* operator[](const int& index) const;
165 GModel* operator[](const std::string& name);
166 const GModel* operator[](const std::string& name) const;
167
168 // Methods
169 void clear(void);
170 GModels* clone(void) const;
171 std::string classname(void) const;
172 GModel* at(const int& index);
173 const GModel* at(const int& index) const;
174 int size(void) const;
175 bool is_empty(void) const;
176 GModel* set(const int& index, const GModel& model);
177 GModel* set(const std::string& name, const GModel& model);
178 GModel* append(const GModel& model);
179 GModel* insert(const int& index, const GModel& model);
180 GModel* insert(const std::string& name, const GModel& model);
181 void remove(const int& index);
182 void remove(const std::string& name);
183 void reserve(const int& num);
184 void extend(const GModels& models);
185 bool contains(const std::string& name) const;
186 void load(const GFilename& filename);
187 void save(const GFilename& filename) const;
188 void read(const GXml& xml);
189 void write(GXml& xml) const;
190 int npars(void) const;
191 GOptimizerPars pars(void) const;
192 double eval(const GEvent& event,
193 const GObservation& obs,
194 const bool& gradients = false) const;
195 GVector eval(const GObservation& obs,
196 GMatrixSparse* gradients = NULL) const;
197 std::string print(const GChatter& chatter = NORMAL) const;
198
199protected:
200 // Protected methods
201 void init_members(void);
202 void copy_members(const GModels& models);
203 void free_members(void);
204 int get_index(const std::string& name) const;
205
206 // Proteced members
207 std::vector<GModel*> m_models; //!< List of models
208};
209
210
211/***********************************************************************//**
212 * @brief Return class name
213 *
214 * @return String containing the class name ("GModels").
215 ***************************************************************************/
216inline
217std::string GModels::classname(void) const
218{
219 return ("GModels");
220}
221
222
223/***********************************************************************//**
224 * @brief Return pointer to model
225 *
226 * @param[in] index Model index [0,...,size()-1].
227 *
228 * Returns a pointer to the model with the specified @p index.
229 ***************************************************************************/
230inline
231GModel* GModels::operator[](const int& index)
232{
233 return (m_models[index]);
234}
235
236
237/***********************************************************************//**
238 * @brief Return pointer to model (const version)
239 *
240 * @param[in] index Model index [0,...,size()-1].
241 *
242 * Returns a const pointer to the model with the specified @p index.
243 ***************************************************************************/
244inline
245const GModel* GModels::operator[](const int& index) const
246{
247 return (m_models[index]);
248}
249
250
251/***********************************************************************//**
252 * @brief Return number of models in container
253 *
254 * @return Number of models in container.
255 *
256 * Returns the number of models in the model container.
257 ***************************************************************************/
258inline
259int GModels::size(void) const
260{
261 return (int)m_models.size();
262}
263
264
265/***********************************************************************//**
266 * @brief Signals if there are no models in container
267 *
268 * @return True if container is empty, false otherwise.
269 *
270 * Signals if the model container does not contain any model.
271 ***************************************************************************/
272inline
273bool GModels::is_empty(void) const
274{
275 return (m_models.empty());
276}
277
278
279/***********************************************************************//**
280 * @brief Reserves space for models in container
281 *
282 * @param[in] num Number of models
283 *
284 * Reserves space for @p num models in the container.
285 ***************************************************************************/
286inline
287void GModels::reserve(const int& num)
288{
289 m_models.reserve(num);
290 return;
291}
292
293#endif /* GMODELS_HPP */
Definition of interface for container classes.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
Interface class for container classes.
Abstract interface for the event classes.
Definition GEvent.hpp:71
Filename class.
Definition GFilename.hpp:62
Sparse matrix class interface definition.
Abstract model class.
Definition GModel.hpp:100
Model container class.
Definition GModels.hpp:152
void save(const GFilename &filename) const
Save models into XML file.
Definition GModels.cpp:711
GModel * insert(const int &index, const GModel &model)
Insert model into container.
Definition GModels.cpp:459
std::string classname(void) const
Return class name.
Definition GModels.hpp:217
bool contains(const std::string &name) const
Signals if model name exists.
Definition GModels.cpp:670
bool is_empty(void) const
Signals if there are no models in container.
Definition GModels.hpp:273
void free_members(void)
Delete class members.
Definition GModels.cpp:1048
GModel * at(const int &index)
Return pointer to model.
Definition GModels.cpp:269
void clear(void)
Clear object.
Definition GModels.cpp:233
void load(const GFilename &filename)
Load models from XML file.
Definition GModels.cpp:688
GOptimizerPars pars(void) const
Return optimizer parameter container.
Definition GModels.cpp:878
int get_index(const std::string &name) const
Return model index by name.
Definition GModels.cpp:1070
void write(GXml &xml) const
Write models into XML document.
Definition GModels.cpp:828
GModels(void)
Void constructor.
Definition GModels.cpp:73
GModels & operator=(const GModels &models)
Assignment operator.
Definition GModels.cpp:147
int size(void) const
Return number of models in container.
Definition GModels.hpp:259
GModel * set(const int &index, const GModel &model)
Set model in container.
Definition GModels.cpp:321
std::vector< GModel * > m_models
List of models.
Definition GModels.hpp:207
GModels * clone(void) const
Clone instance.
Definition GModels.cpp:253
GModel * operator[](const int &index)
Return pointer to model.
Definition GModels.hpp:231
void read(const GXml &xml)
Read models from XML document.
Definition GModels.cpp:773
int npars(void) const
Return number of model parameters in container.
Definition GModels.cpp:853
void remove(const int &index)
Remove model from container.
Definition GModels.cpp:560
std::string print(const GChatter &chatter=NORMAL) const
Print models.
Definition GModels.cpp:972
double eval(const GEvent &event, const GObservation &obs, const bool &gradients=false) const
Evaluate sum of all models.
Definition GModels.cpp:911
GModel * append(const GModel &model)
Append model to container.
Definition GModels.cpp:420
void init_members(void)
Initialise class members.
Definition GModels.cpp:1010
virtual ~GModels(void)
Destructor.
Definition GModels.cpp:125
void extend(const GModels &models)
Append model container.
Definition GModels.cpp:621
void reserve(const int &num)
Reserves space for models in container.
Definition GModels.hpp:287
void copy_members(const GModels &models)
Copy class members.
Definition GModels.cpp:1029
Abstract observation base class.
Optimizer parameter container class.
Vector class.
Definition GVector.hpp:46
XML class.
Definition GXml.hpp:172