include/GModels.hpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                     GModels.hpp - Model container class                 *
00003  * ----------------------------------------------------------------------- *
00004  *  copyright (C) 2009-2016 by Juergen Knoedlseder                         *
00005  * ----------------------------------------------------------------------- *
00006  *                                                                         *
00007  *  This program is free software: you can redistribute it and/or modify   *
00008  *  it under the terms of the GNU General Public License as published by   *
00009  *  the Free Software Foundation, either version 3 of the License, or      *
00010  *  (at your option) any later version.                                    *
00011  *                                                                         *
00012  *  This program is distributed in the hope that it will be useful,        *
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00015  *  GNU General Public License for more details.                           *
00016  *                                                                         *
00017  *  You should have received a copy of the GNU General Public License      *
00018  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
00019  *                                                                         *
00020  ***************************************************************************/
00021 /**
00022  * @file GModels.hpp
00023  * @brief Model container class definition
00024  * @author Juergen Knoedlseder
00025  */
00026 
00027 #ifndef GMODELS_HPP
00028 #define GMODELS_HPP
00029 
00030 /* __ Includes ___________________________________________________________ */
00031 #include <string>
00032 #include "GContainer.hpp"
00033 #include "GModel.hpp"
00034 #include "GOptimizerPars.hpp"
00035 #include "GXml.hpp"
00036 
00037 /* __ Forward declarations _______________________________________________ */
00038 class GEvent;
00039 class GObservation;
00040 class GFilename;
00041 
00042 
00043 /***********************************************************************//**
00044  * @class GModels
00045  *
00046  * @brief Model container class
00047  *
00048  * This container class collects models of type GModel that are used to
00049  * describe the data. The names of all models in the container have to be
00050  * unique, i.e. every name can occur only once. This allows for accessing the
00051  * models by name and by index.
00052  *
00053  * The GModels class provides methods to manage and to access the models
00054  * in the container. The number of models in the container is retrieved
00055  * using the size() method. The is_empty() method can be used to check
00056  * whether the container is empty or whether it contains models:
00057  *
00058  *     GModels models;                 // Allocate container
00059  *     int n = models.size();          // Number of models in container
00060  *     if (models.is_empty())           // Check for emptiness
00061  *         std::cout << "Empty container";
00062  *
00063  * Access operators exist for accessing of models by index or by name:
00064  *
00065  *     GModel* mptr = models[i];       // Get i'th model
00066  *     GModel* mptr = models["Crab"];  // Get a model named "Crab"
00067  *
00068  * The index access operator does not check the validity of the provided
00069  * index. For index validation, use the at() method:
00070  *
00071  *     GModel* mptr = models.at(i);    // Get i'th model with index check
00072  *
00073  * The append() method add a model to the container:
00074  *
00075  *     models.append(model);           // Append model
00076  *
00077  * The append() method clones the model that is passed as argument. The
00078  * method returns a pointer to the cloned model so that the attributes of
00079  * the cloned model can be manipulated:
00080  *
00081  *     GModel* mptr = models.append(model);
00082  *
00083  * The insert() methods insert a model before a given index or before
00084  * a model with a given name (the methods also return a pointer to the
00085  * cloned model):
00086  *
00087  *     models.insert(i, model);        // Insert before i'th model
00088  *     models.insert("Crab", model);   // Insert before "Crab" model
00089  *
00090  * The set() methods replace an existing model by index or by name (also
00091  * these methods return a pointer to the cloned model):
00092  *
00093  *     models.set(i, model);           // Replace i'th model
00094  *     models.set("Crab", model);      // Replace "Crab" model
00095  *
00096  * The remove() methods remove an existing model by index or by name:
00097  *
00098  *     models.remove(i);               // Remove i'th model
00099  *     models.remove("Crab");          // Remove "Crab" model
00100  *
00101  * The existence of a model with a given name can be checked using
00102  *
00103  *     if (models.contains("Crab"))
00104  *         std::cout << "We have the Crab!";
00105  *
00106  * The extend() method extends a container by all models found in another
00107  * container:
00108  *
00109  *     models.extend(other_models);    // Extend container
00110  *
00111  * For repeated container manipulations, a given @p number of model slots can
00112  * be reserved using
00113  *
00114  *     models.reserve(number);         // Reserves model slots
00115  *
00116  * which will speed up the memory allocations for new models.
00117  *
00118  * Models can be saved into or loaded from an XML file using
00119  *
00120  *     models.save("mymodels.xml");    // Save models in XML file
00121  *     models.load("mymodels.xml");    // Load models from XML file
00122  *
00123  * The models can also be loaded upon construction from an XML file:
00124  *
00125  *     GModels models("mymodels.xml"); // Construct by loading models from XML file
00126  *
00127  * The class can also directly operate on a GXml object using the read() and
00128  * write() methods:
00129  *
00130  *     GXml xml;                       // Allocate GXml object
00131  *     models.write(xml);              // Write into GXml object
00132  *     models.read(xml);               // Read models from GXml object
00133  *
00134  * The sum of all models in the container are evaluated for a given @p event
00135  * and @p observation using the eval() method:
00136  *
00137  *     double value = models.eval(event, observation);
00138  *
00139  * If the eval() method is called with the optional gradients parameter set
00140  * to true, i.e.
00141  *
00142  *     double value = models.eval(event, observation, true);
00143  *
00144  * the method computes also the parameter gradients for all free model
00145  * parameters that have an analytical parameter gradient.
00146  *
00147  * The only member of GModels is a list of model pointers. The class handles
00148  * the proper allocation and deallocation of the model memory.
00149  ***************************************************************************/
00150 class GModels : public GContainer {
00151 
00152 public:
00153     // Constructors and destructors
00154     GModels(void);
00155     GModels(const GModels& models);
00156     explicit GModels(const GFilename& filename);
00157     virtual ~GModels(void);
00158 
00159     // Operators
00160     GModels&      operator=(const GModels& models);
00161     GModel*       operator[](const int& index);
00162     const GModel* operator[](const int& index) const;
00163     GModel*       operator[](const std::string& name);
00164     const GModel* operator[](const std::string& name) const;
00165 
00166     // Methods
00167     void           clear(void);
00168     GModels*       clone(void) const;
00169     std::string    classname(void) const;
00170     GModel*        at(const int& index);
00171     const GModel*  at(const int& index) const;
00172     int            size(void) const;
00173     bool           is_empty(void) const;
00174     GModel*        set(const int& index, const GModel& model);
00175     GModel*        set(const std::string& name, const GModel& model);
00176     GModel*        append(const GModel& model);
00177     GModel*        insert(const int& index, const GModel& model);
00178     GModel*        insert(const std::string& name, const GModel& model);
00179     void           remove(const int& index);
00180     void           remove(const std::string& name);
00181     void           reserve(const int& num);
00182     void           extend(const GModels& models);
00183     bool           contains(const std::string& name) const;
00184     void           load(const GFilename& filename);
00185     void           save(const GFilename& filename) const;
00186     void           read(const GXml& xml);
00187     void           write(GXml& xml) const;
00188     int            npars(void) const;
00189     GOptimizerPars pars(void) const;
00190     double         eval(const GEvent& event,
00191                         const GObservation& obs,
00192                         const bool& gradients = false) const;
00193     std::string    print(const GChatter& chatter = NORMAL) const;
00194 
00195 protected:
00196     // Protected methods
00197     void          init_members(void);
00198     void          copy_members(const GModels& models);
00199     void          free_members(void);
00200     int           get_index(const std::string& name) const;
00201 
00202     // Proteced members
00203     std::vector<GModel*> m_models;  //!< List of models
00204 };
00205 
00206 
00207 /***********************************************************************//**
00208  * @brief Return class name
00209  *
00210  * @return String containing the class name ("GModels").
00211  ***************************************************************************/
00212 inline
00213 std::string GModels::classname(void) const
00214 {
00215     return ("GModels");
00216 }
00217 
00218 
00219 /***********************************************************************//**
00220  * @brief Return pointer to model
00221  *
00222  * @param[in] index Model index [0,...,size()-1].
00223  *
00224  * Returns a pointer to the model with the specified @p index.
00225  ***************************************************************************/
00226 inline
00227 GModel* GModels::operator[](const int& index)
00228 {
00229     return (m_models[index]);
00230 }
00231 
00232 
00233 /***********************************************************************//**
00234  * @brief Return pointer to model (const version)
00235  *
00236  * @param[in] index Model index [0,...,size()-1].
00237  *
00238  * Returns a const pointer to the model with the specified @p index.
00239  ***************************************************************************/
00240 inline
00241 const GModel* GModels::operator[](const int& index) const
00242 {
00243     return (m_models[index]);
00244 }
00245 
00246 
00247 /***********************************************************************//**
00248  * @brief Return number of models in container
00249  *
00250  * @return Number of models in container.
00251  *
00252  * Returns the number of models in the model container.
00253  ***************************************************************************/
00254 inline
00255 int GModels::size(void) const
00256 {
00257     return (int)m_models.size();
00258 }
00259 
00260 
00261 /***********************************************************************//**
00262  * @brief Signals if there are no models in container
00263  *
00264  * @return True if container is empty, false otherwise.
00265  *
00266  * Signals if the model container does not contain any model.
00267  ***************************************************************************/
00268 inline
00269 bool GModels::is_empty(void) const
00270 {
00271     return (m_models.empty());
00272 }
00273 
00274 
00275 /***********************************************************************//**
00276  * @brief Reserves space for models in container
00277  *
00278  * @param[in] num Number of models
00279  *
00280  * Reserves space for @p num models in the container.
00281  ***************************************************************************/
00282 inline
00283 void GModels::reserve(const int& num)
00284 {
00285     m_models.reserve(num);
00286     return;
00287 }
00288 
00289 #endif /* GMODELS_HPP */

Generated on Tue Jan 24 12:37:18 2017 for GammaLib by  doxygen 1.4.7