GammaLib
2.0.0
|
Model container class. More...
#include <GModels.hpp>
Public Member Functions | |
GModels (void) | |
Void constructor. More... | |
GModels (const GModels &models) | |
Copy constructor. More... | |
GModels (const GFilename &filename) | |
Load constructor. More... | |
virtual | ~GModels (void) |
Destructor. More... | |
GModels & | operator= (const GModels &models) |
Assignment operator. More... | |
GModel * | operator[] (const int &index) |
Return pointer to model. More... | |
const GModel * | operator[] (const int &index) const |
Return pointer to model (const version) More... | |
GModel * | operator[] (const std::string &name) |
Return pointer to model. More... | |
const GModel * | operator[] (const std::string &name) const |
Return pointer to model (const version) More... | |
void | clear (void) |
Clear object. More... | |
GModels * | clone (void) const |
Clone instance. More... | |
std::string | classname (void) const |
Return class name. More... | |
GModel * | at (const int &index) |
Return pointer to model. More... | |
const GModel * | at (const int &index) const |
Return pointer to model (const version) More... | |
int | size (void) const |
Return number of models in container. More... | |
bool | is_empty (void) const |
Signals if there are no models in container. More... | |
GModel * | set (const int &index, const GModel &model) |
Set model in container. More... | |
GModel * | set (const std::string &name, const GModel &model) |
Set model in container. More... | |
GModel * | append (const GModel &model) |
Append model to container. More... | |
GModel * | insert (const int &index, const GModel &model) |
Insert model into container. More... | |
GModel * | insert (const std::string &name, const GModel &model) |
Insert model into container. More... | |
void | remove (const int &index) |
Remove model from container. More... | |
void | remove (const std::string &name) |
Remove model from container. More... | |
void | reserve (const int &num) |
Reserves space for models in container. More... | |
void | extend (const GModels &models) |
Append model container. More... | |
bool | contains (const std::string &name) const |
Signals if model name exists. More... | |
void | load (const GFilename &filename) |
Load models from XML file. More... | |
void | save (const GFilename &filename) const |
Save models into XML file. More... | |
void | read (const GXml &xml) |
Read models from XML document. More... | |
void | write (GXml &xml) const |
Write models into XML document. More... | |
int | npars (void) const |
Return number of model parameters in container. More... | |
GOptimizerPars | pars (void) const |
Return optimizer parameter container. More... | |
double | eval (const GEvent &event, const GObservation &obs, const bool &gradients=false) const |
Evaluate sum of all models. More... | |
GVector | eval (const GObservation &obs, GMatrixSparse *gradients=NULL) const |
Evaluate sum vector of all models. More... | |
std::string | print (const GChatter &chatter=NORMAL) const |
Print models. More... | |
Public Member Functions inherited from GContainer | |
virtual | ~GContainer (void) |
Destructor. More... | |
Public Member Functions inherited from GBase | |
virtual | ~GBase (void) |
Destructor. More... | |
Protected Member Functions | |
void | init_members (void) |
Initialise class members. More... | |
void | copy_members (const GModels &models) |
Copy class members. More... | |
void | free_members (void) |
Delete class members. More... | |
int | get_index (const std::string &name) const |
Return model index by name. More... | |
Protected Attributes | |
std::vector< GModel * > | m_models |
List of models. More... | |
Model container class.
This container class collects models of type GModel that are used to describe the data. The names of all models in the container have to be unique, i.e. every name can occur only once. This allows for accessing the models by name and by index.
The GModels class provides methods to manage and to access the models in the container. The number of models in the container is retrieved using the size() method. The is_empty() method can be used to check whether the container is empty or whether it contains models:
GModels models; // Allocate container int n = models.size(); // Number of models in container if (models.is_empty()) // Check for emptiness std::cout << "Empty container";
Access operators exist for accessing of models by index or by name:
GModel* mptr = models[i]; // Get i'th model GModel* mptr = models["Crab"]; // Get a model named "Crab"
The index access operator does not check the validity of the provided index. For index validation, use the at() method:
GModel* mptr = models.at(i); // Get i'th model with index check
The append() method add a model to the container:
models.append(model); // Append model
The append() method clones the model that is passed as argument. The method returns a pointer to the cloned model so that the attributes of the cloned model can be manipulated:
GModel* mptr = models.append(model);
The insert() methods insert a model before a given index or before a model with a given name (the methods also return a pointer to the cloned model):
models.insert(i, model); // Insert before i'th model models.insert("Crab", model); // Insert before "Crab" model
The set() methods replace an existing model by index or by name (also these methods return a pointer to the cloned model):
models.set(i, model); // Replace i'th model models.set("Crab", model); // Replace "Crab" model
The remove() methods remove an existing model by index or by name:
models.remove(i); // Remove i'th model models.remove("Crab"); // Remove "Crab" model
The existence of a model with a given name can be checked using
if (models.contains("Crab")) std::cout << "We have the Crab!";
The extend() method extends a container by all models found in another container:
models.extend(other_models); // Extend container
For repeated container manipulations, a given number
of model slots can be reserved using
models.reserve(number); // Reserves model slots
which will speed up the memory allocations for new models.
Models can be saved into or loaded from an XML file using
models.save("mymodels.xml"); // Save models in XML file models.load("mymodels.xml"); // Load models from XML file
The models can also be loaded upon construction from an XML file:
GModels models("mymodels.xml"); // Construct by loading models from XML file
The class can also directly operate on a GXml object using the read() and write() methods:
GXml xml; // Allocate GXml object models.write(xml); // Write into GXml object models.read(xml); // Read models from GXml object
The sum of all models in the container are evaluated for a given event
and observation
using the eval() method:
double value = models.eval(event, observation);
If the eval() method is called with the optional gradients parameter set to true, i.e.
double value = models.eval(event, observation, true);
the method computes also the parameter gradients for all free model parameters that have an analytical parameter gradient.
The only member of GModels is a list of model pointers. The class handles the proper allocation and deallocation of the model memory.
Definition at line 152 of file GModels.hpp.
GModels::GModels | ( | void | ) |
Void constructor.
Definition at line 73 of file GModels.cpp.
References init_members().
Referenced by clone().
GModels::GModels | ( | const GModels & | models | ) |
Copy constructor.
[in] | models | Model container. |
Definition at line 88 of file GModels.cpp.
References copy_members(), and init_members().
|
explicit |
Load constructor.
[in] | filename | XML filename. |
Constructs model container from an XML file. See the read() method for more information about the expected structure of the XML file.
Definition at line 109 of file GModels.cpp.
References init_members(), and load().
|
virtual |
Append model to container.
[in] | model | Model. |
GException::invalid_value | Name of model exists already in container. |
Appends model to the container by making a deep copy of the model and storing its pointer.
Definition at line 420 of file GModels.cpp.
References GModel::clone(), G_APPEND, get_index(), m_models, GModel::name(), and gammalib::str().
Referenced by read(), and GCTAOnOffObservation::set().
GModel * GModels::at | ( | const int & | index | ) |
Return pointer to model.
[in] | index | Model index [0,...,size()-1]. |
GException::out_of_range | Model index is out of range. |
Returns a pointer to the model with the specified index
.
Definition at line 269 of file GModels.cpp.
const GModel * GModels::at | ( | const int & | index | ) | const |
Return pointer to model (const version)
[in] | index | Model index [0,...,size()-1]. |
GException::out_of_range | Model index is out of range. |
Returns a const pointer to the model with the specified index
.
Definition at line 293 of file GModels.cpp.
|
inlinevirtual |
Return class name.
Implements GBase.
Definition at line 217 of file GModels.hpp.
Referenced by GCTAOnOffObservation::set().
|
virtual |
Clear object.
Removes all models from the container.
Implements GBase.
Definition at line 233 of file GModels.cpp.
References free_members(), and init_members().
Referenced by GObservations::init_members(), and load().
|
virtual |
Clone instance.
Makes a deep copy of the model container instance.
Implements GBase.
Definition at line 253 of file GModels.cpp.
References GModels().
Referenced by extend().
bool GModels::contains | ( | const std::string & | name | ) | const |
Signals if model name exists.
[in] | name | Model name. |
name
exists.Searches all model names for a match with the specified name
. If the specified name has been found, true is returned.
Definition at line 670 of file GModels.cpp.
References get_index().
Referenced by GObservations::npred().
|
protected |
Copy class members.
[in] | models | Model container. |
Makes a copy of all class members. All models are deep copied, and the linear pointer array for parameter access through the GOptimizerPars base class is set.
Definition at line 1029 of file GModels.cpp.
References m_models.
Referenced by GModels(), and operator=().
double GModels::eval | ( | const GEvent & | event, |
const GObservation & | obs, | ||
const bool & | gradients = false |
||
) | const |
Evaluate sum of all models.
[in] | event | Observed event. |
[in] | obs | Observation. |
[in] | gradients | Compute gradients? |
Evaluates the sum of all models for the specified event and observation. Only valid models are considered in this evaluation.
If the gradients
flag is true the method will also set the parameter gradients of the model parameters.
Definition at line 911 of file GModels.cpp.
References GObservation::id(), GObservation::instrument(), m_models, and size().
Referenced by GCTAOnOffObservation::compute_alpha(), GCTAOnOffObservation::compute_bgd(), and GCOMObservation::drm().
GVector GModels::eval | ( | const GObservation & | obs, |
GMatrixSparse * | gradients = NULL |
||
) | const |
Evaluate sum vector of all models.
[in] | obs | Observation. |
[in] | gradients | Pointer to gradients matrix. |
Evaluates the sum vector of all models for all events in the observation. Only valid models are considered in this evaluation.
If the gradients
pointer is not NULL the method will fill the corresponding matrix with the parameter gradients of the model.
Definition at line 943 of file GModels.cpp.
References GObservation::events(), GObservation::id(), GObservation::instrument(), m_models, GEvents::size(), and size().
void GModels::extend | ( | const GModels & | models | ) |
Append model container.
[in] | models | Model container. |
Append model container to the container.
Definition at line 621 of file GModels.cpp.
References clone(), G_EXTEND, get_index(), is_empty(), m_models, reserve(), size(), and gammalib::str().
|
protected |
Delete class members.
Deallocates all models. The method loops over the model container and deallocates the memory that has been allocated before.
Definition at line 1048 of file GModels.cpp.
References m_models.
Referenced by clear(), operator=(), and ~GModels().
|
protected |
Return model index by name.
[in] | name | Model name. |
Returns model index based on the specified name
. If no model with the specified name
is found the method returns -1.
Definition at line 1070 of file GModels.cpp.
References m_models, and size().
Referenced by append(), contains(), extend(), insert(), operator[](), remove(), and set().
|
protected |
Initialise class members.
Definition at line 1010 of file GModels.cpp.
References m_models.
Referenced by clear(), GModels(), and operator=().
Insert model into container.
[in] | index | Model index [0,...,size()[. |
[in] | model | Model. |
GException::out_of_range | Model index is out of range. |
GException::invalid_value | Name of model exists already in container. |
Inserts a model
into the container before the model with the specified index
.
Definition at line 459 of file GModels.cpp.
References GModel::clone(), G_INSERT1, get_index(), is_empty(), m_models, GModel::name(), size(), and gammalib::str().
Insert model into container.
[in] | name | Model name. |
[in] | model | Model. |
GException::invalid_argument | Model with specified name not found in container. |
GException::invalid_value | Name of model exists already in container. |
Inserts a model
into the container before the model with the specified name
.
Definition at line 515 of file GModels.cpp.
References GModel::clone(), G_INSERT2, get_index(), m_models, GModel::name(), and gammalib::str().
|
inlinevirtual |
Signals if there are no models in container.
Signals if the model container does not contain any model.
Implements GContainer.
Definition at line 273 of file GModels.hpp.
References m_models.
void GModels::load | ( | const GFilename & | filename | ) |
Load models from XML file.
[in] | filename | XML filename. |
Loads all models from an XML file. See the read() method for more information about the expected structure of the XML file.
Definition at line 688 of file GModels.cpp.
References clear(), read(), and GFilename::url().
Referenced by GModels(), and GObservations::models().
int GModels::npars | ( | void | ) | const |
Return number of model parameters in container.
Definition at line 853 of file GModels.cpp.
References m_models, and size().
Referenced by GCTAOnOffObservation::likelihood_cstat(), GCTAOnOffObservation::likelihood_wstat(), GObservation::model(), GCTAOnOffObservation::model_background(), GCTAOnOffObservation::model_gamma(), GCTAOnOffObservation::N_bgd(), GCTAOnOffObservation::N_gamma(), GObservation::npred(), pars(), and print().
Assignment operator.
[in] | models | Model container. |
Definition at line 147 of file GModels.cpp.
References copy_members(), free_members(), and init_members().
|
inline |
Return pointer to model.
[in] | index | Model index [0,...,size()-1]. |
Returns a pointer to the model with the specified index
.
Definition at line 231 of file GModels.hpp.
References m_models.
|
inline |
Return pointer to model (const version)
[in] | index | Model index [0,...,size()-1]. |
Returns a const pointer to the model with the specified index
.
Definition at line 245 of file GModels.hpp.
References m_models.
GModel * GModels::operator[] | ( | const std::string & | name | ) |
Return pointer to model.
[in] | name | Model name. |
GException::invalid_argument | Model with specified name not found in container. |
Returns a pointer to the model with the specified name
.
Definition at line 178 of file GModels.cpp.
References G_ACCESS, get_index(), and m_models.
const GModel * GModels::operator[] | ( | const std::string & | name | ) | const |
Return pointer to model (const version)
[in] | name | Model name. |
GException::invalid_argument | Model with specified name not found in container. |
Returns a const pointer to the model with the specified name
.
Definition at line 205 of file GModels.cpp.
References G_ACCESS, get_index(), and m_models.
GOptimizerPars GModels::pars | ( | void | ) | const |
Return optimizer parameter container.
Returns an optimizer parameter container that is built by extracting all model pointers from the model and attaching them to the parameter container. The optimizer parameter container will thus contains a flat array of a model parameters.
Definition at line 878 of file GModels.cpp.
References GOptimizerPars::attach(), m_models, npars(), GModel::size(), and size().
Referenced by GObservations::errors(), GObservations::errors_hessian(), GObservations::eval(), and GObservations::optimize().
Print models.
[in] | chatter | Chattiness. |
Prints all models into a string.
Implements GBase.
Definition at line 972 of file GModels.cpp.
References m_models, npars(), gammalib::parformat(), SILENT, size(), and gammalib::str().
Referenced by GObservations::print().
void GModels::read | ( | const GXml & | xml | ) |
Read models from XML document.
[in] | xml | XML document. |
GException::invalid_value | Invalid model type encountered. |
Read models from the first source library found in the XML document. The XML document is expected to have the following structure
<source_library title="source library"> <source name="Source1" type="PointSource"> ... </source> <source name="Source2" type="DiffuseSource"> ... </source> </source_library>
Each source
tag will be interpreted as a model component.
Definition at line 773 of file GModels.cpp.
References GModelRegistry::alloc(), append(), GXmlElement::attribute(), GRegistry::content(), GXmlNode::element(), GXml::element(), GXmlNode::elements(), G_READ, and GModel::read().
Referenced by load().
|
virtual |
Remove model from container.
[in] | index | Model index [0,...,size()[. |
GException::out_of_range | Model index is out of range. |
Remove model of specified index
from container.
Implements GContainer.
Definition at line 560 of file GModels.cpp.
void GModels::remove | ( | const std::string & | name | ) |
Remove model from container.
[in] | name | Model name. |
GException::invalid_argument | Model with specified name not found in container. |
Remove model of specified name
from container.
Definition at line 591 of file GModels.cpp.
References G_REMOVE2, get_index(), and m_models.
|
inlinevirtual |
Reserves space for models in container.
[in] | num | Number of models |
Reserves space for num
models in the container.
Implements GContainer.
Definition at line 287 of file GModels.hpp.
References m_models.
Referenced by extend().
void GModels::save | ( | const GFilename & | filename | ) | const |
Save models into XML file.
[in] | filename | XML filename. |
Saves all models in the container into an XML file.
Definition at line 711 of file GModels.cpp.
References GXmlNode::append(), GXml::append(), GXmlNode::element(), m_models, GXml::save(), and size().
Set model in container.
[in] | index | Model index [0,...,size()[. |
[in] | model | Model. |
GException::out_of_range | Model index is out of range. |
GException::invalid_value | Name of model exists already in container. |
Set model in the container. A deep copy of the model will be made.
Definition at line 321 of file GModels.cpp.
References GModel::clone(), G_SET1, get_index(), m_models, GModel::name(), size(), and gammalib::str().
Set model in container.
[in] | name | Model name. |
[in] | model | Model pointer. |
GException::invalid_argument | Model with specified name not found in container. |
GException::invalid_value | Name of model exists already in container. |
Set model in the container. A deep copy of the model will be made.
Definition at line 370 of file GModels.cpp.
References GModel::clone(), G_SET2, get_index(), m_models, GModel::name(), and gammalib::str().
|
inlinevirtual |
Return number of models in container.
Returns the number of models in the model container.
Implements GContainer.
Definition at line 259 of file GModels.hpp.
References m_models.
Referenced by at(), eval(), extend(), get_index(), insert(), GObservation::model(), GCTAOnOffObservation::N_bgd(), GCTAOnOffObservation::N_gamma(), npars(), GObservation::npred(), pars(), GObservations::print(), print(), remove(), GObservations::remove_response_cache(), save(), GCTAOnOffObservation::set(), set(), and write().
void GModels::write | ( | GXml & | xml | ) | const |
Write models into XML document.
[in] | xml | XML document. |
Write models into the first source library that is found in the XML document. In case that no source library exists, one is added to the document.
Definition at line 828 of file GModels.cpp.
References GXml::append(), GXml::element(), GXml::elements(), m_models, and size().
|
protected |
List of models.
Definition at line 207 of file GModels.hpp.
Referenced by append(), at(), copy_members(), eval(), extend(), free_members(), get_index(), init_members(), insert(), is_empty(), npars(), operator[](), pars(), print(), remove(), reserve(), save(), set(), size(), and write().