Commit 9470fa053b0ddfce0f5ecf3da7677d2d4fcffab8

Authored by Jürgen Knödlseder
1 parent f1261a34

Change GModel access operator interface.

The GModel access operator returns now a reference to a pointer instead of a reference. This allows proper assignment in C++ using constructs such as m[0] = &model;. Also in Python assignment is now possible. For the Python interface we selected to make a deep copy to get a behavior that is compliant with Python list access.
include/GModels.hpp
1 1 /***************************************************************************
2 2 * GModels.hpp - Model container class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2009-2011 by Jurgen Knodlseder *
  4 + * copyright (C) 2009-2012 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -21,7 +21,7 @@
21 21 /**
22 22 * @file GModels.hpp
23 23 * @brief Model container class definition
24   - * @author J. Knodlseder
  24 + * @author Juergen Knoedlseder
25 25 */
26 26  
27 27 #ifndef GMODELS_HPP
... ... @@ -47,6 +47,10 @@ class GObservation;
47 47 * This container class collects models of gamma-ray data that are used
48 48 * for maximum likelihood fitting. It derives from the optimizer parameter
49 49 * class GOptimizerPars.
  50 + *
  51 + * @todo Add extend method to append a container to the container
  52 + * @todo Add insert method to insert a model into the container
  53 + * @todo Add pop method to delete a model from the container (default: last)
50 54 ***************************************************************************/
51 55 class GModels : public GOptimizerPars {
52 56  
... ... @@ -63,10 +67,10 @@ public:
63 67  
64 68 // Operators
65 69 GModels& operator=(const GModels& models);
66   - GModel& operator[](const int& index);
67   - const GModel& operator[](const int& index) const;
68   - GModel& operator[](const std::string& name);
69   - const GModel& operator[](const std::string& name) const;
  70 + GModel*& operator[](const int& index);
  71 + const GModel* operator[](const int& index) const;
  72 + GModel*& operator[](const std::string& name);
  73 + const GModel* operator[](const std::string& name) const;
70 74  
71 75 // Methods
72 76 void clear(void);
... ...
pyext/GModels.i
... ... @@ -26,11 +26,6 @@
26 26 %{
27 27 /* Put headers and other declarations here that are needed for compilation */
28 28 #include "GModels.hpp"
29   -#include "GModelSky.hpp"
30   -#include "GModelDiffuseSource.hpp"
31   -#include "GModelExtendedSource.hpp"
32   -#include "GModelPointSource.hpp"
33   -#include "GModelData.hpp"
34 29 #include "GTools.hpp"
35 30 %}
36 31  
... ... @@ -72,26 +67,30 @@ public:
72 67 char *__str__() {
73 68 return tochar(self->print());
74 69 }
75   - GModel& __getitem__(const int& index) {
  70 + GModel* __getitem__(const int& index) {
76 71 if (index >= 0 && index < self->size()) {
77 72 return (*self)[index];
78 73 }
79   - else
80   - throw GException::out_of_range("__getitem__(int)", index, self->size());
  74 + else {
  75 + throw GException::out_of_range("__getitem__(int)", index,
  76 + 0, self->size()-1);
  77 + }
81 78 }
82   - GModel& __getitem__(const std::string& name) {
  79 + GModel* __getitem__(const std::string& name) {
83 80 return (*self)[name];
84 81 }
85   - void __setitem__(const int& index, const GModel& val) {
  82 + void __setitem__(const int& index, GModel* val) {
86 83 if (index>=0 && index < self->size()) {
87   - (*self)[index] = val;
  84 + (*self)[index] = val->clone();
88 85 return;
89 86 }
90   - else
91   - throw GException::out_of_range("__setitem__(int)", index, self->size());
  87 + else {
  88 + throw GException::out_of_range("__setitem__(int)", index,
  89 + 0, self->size()-1);
  90 + }
92 91 }
93   - void __setitem__(const std::string& name, const GModel& val) {
94   - (*self)[name] = val;
  92 + void __setitem__(const std::string& name, GModel* val) {
  93 + (*self)[name] = val->clone();
95 94 return;
96 95 }
97 96 GModels copy() {
... ...
src/model/GModels.cpp
... ... @@ -21,7 +21,7 @@
21 21 /**
22 22 * @file GModels.cpp
23 23 * @brief Model container class implementation
24   - * @author J. Knoedlseder
  24 + * @author Juergen Knoedlseder
25 25 */
26 26  
27 27 /* __ Includes ___________________________________________________________ */
... ... @@ -56,6 +56,8 @@
56 56  
57 57 /***********************************************************************//**
58 58 * @brief Void constructor
  59 + *
  60 + * Constructs an empty model container.
59 61 ***************************************************************************/
60 62 GModels::GModels(void) : GOptimizerPars()
61 63 {
... ... @@ -71,6 +73,8 @@ GModels::GModels(void) : GOptimizerPars()
71 73 * @brief Copy constructor
72 74 *
73 75 * @param[in] models Model container.
  76 + *
  77 + * Constructs a copy of a model container.
74 78 ***************************************************************************/
75 79 GModels::GModels(const GModels& models) : GOptimizerPars(models)
76 80 {
... ... @@ -89,6 +93,8 @@ GModels::GModels(const GModels&amp; models) : GOptimizerPars(models)
89 93 * @brief Load constructor
90 94 *
91 95 * @param[in] filename XML filename.
  96 + *
  97 + * Constructs model container from the model information of an XML file.
92 98 ***************************************************************************/
93 99 GModels::GModels(const std::string& filename)
94 100 {
... ... @@ -107,6 +113,8 @@ GModels::GModels(const std::string&amp; filename)
107 113  
108 114 /***********************************************************************//**
109 115 * @brief Destructor
  116 + *
  117 + * Destroys the model container.
110 118 ***************************************************************************/
111 119 GModels::~GModels(void)
112 120 {
... ... @@ -128,6 +136,8 @@ GModels::~GModels(void)
128 136 * @brief Assignment operator
129 137 *
130 138 * @param[in] models Model container.
  139 + *
  140 + * Assigns a model container to the instance.
131 141 ***************************************************************************/
132 142 GModels& GModels::operator= (const GModels& models)
133 143 {
... ... @@ -154,14 +164,16 @@ GModels&amp; GModels::operator= (const GModels&amp; models)
154 164  
155 165  
156 166 /***********************************************************************//**
157   - * @brief Returns reference to model
  167 + * @brief Returns reference to pointer to model
158 168 *
159 169 * @param[in] index Model index [0,...,size()-1].
160 170 *
161 171 * @exception GException::out_of_range
162 172 * Model index is out of range.
  173 + *
  174 + * Returns a reference to the model pointer by index.
163 175 ***************************************************************************/
164   -GModel& GModels::operator[](const int& index)
  176 +GModel*& GModels::operator[](const int& index)
165 177 {
166 178 // Compile option: raise exception if index is out of range
167 179 #if defined(G_RANGE_CHECK)
... ... @@ -170,20 +182,22 @@ GModel&amp; GModels::operator[](const int&amp; index)
170 182 }
171 183 #endif
172 184  
173   - // Return reference
174   - return *(m_models[index]);
  185 + // Return pointer
  186 + return m_models[index];
175 187 }
176 188  
177 189  
178 190 /***********************************************************************//**
179   - * @brief Returns reference to model
  191 + * @brief Returns pointer to model (const version)
180 192 *
181 193 * @param[in] index Model index [0,...,size()-1].
182 194 *
183 195 * @exception GException::out_of_range
184 196 * Model index is out of range.
  197 + *
  198 + * Returns a constant model pointer by index.
185 199 ***************************************************************************/
186   -const GModel& GModels::operator[](const int& index) const
  200 +const GModel* GModels::operator[](const int& index) const
187 201 {
188 202 // Compile option: raise exception if index is out of range
189 203 #if defined(G_RANGE_CHECK)
... ... @@ -192,20 +206,22 @@ const GModel&amp; GModels::operator[](const int&amp; index) const
192 206 }
193 207 #endif
194 208  
195   - // Return reference
196   - return *(m_models[index]);
  209 + // Return pointer
  210 + return m_models[index];
197 211 }
198 212  
199 213  
200 214 /***********************************************************************//**
201   - * @brief Returns reference to model
  215 + * @brief Returns pointer to model
202 216 *
203 217 * @param[in] name Model name.
204 218 *
205 219 * @exception GException::model_not_found
206 220 * Model with specified name not found in container.
  221 + *
  222 + * Returns a reference to the model pointer by model name.
207 223 ***************************************************************************/
208   -GModel& GModels::operator[](const std::string& name)
  224 +GModel*& GModels::operator[](const std::string& name)
209 225 {
210 226 // Get parameter index
211 227 int index = 0;
... ... @@ -220,20 +236,22 @@ GModel&amp; GModels::operator[](const std::string&amp; name)
220 236 throw GException::model_not_found(G_ACCESS2, name);
221 237 }
222 238  
223   - // Return reference
224   - return *(m_models[index]);
  239 + // Return pointer
  240 + return m_models[index];
225 241 }
226 242  
227 243  
228 244 /***********************************************************************//**
229   - * @brief Returns reference to model (const version)
  245 + * @brief Returns pointer to model (const version)
230 246 *
231 247 * @param[in] name Model name.
232 248 *
233 249 * @exception GException::model_not_found
234 250 * Model with specified name not found in container.
  251 + *
  252 + * Returns a constant model pointer by model name.
235 253 ***************************************************************************/
236   -const GModel& GModels::operator[](const std::string& name) const
  254 +const GModel* GModels::operator[](const std::string& name) const
237 255 {
238 256 // Get parameter index
239 257 int index = 0;
... ... @@ -248,8 +266,8 @@ const GModel&amp; GModels::operator[](const std::string&amp; name) const
248 266 throw GException::model_not_found(G_ACCESS2, name);
249 267 }
250 268  
251   - // Return reference
252   - return *(m_models[index]);
  269 + // Return pointer
  270 + return m_models[index];
253 271 }
254 272  
255 273  
... ... @@ -260,9 +278,9 @@ const GModel&amp; GModels::operator[](const std::string&amp; name) const
260 278 ==========================================================================*/
261 279  
262 280 /***********************************************************************//**
263   - * @brief Clear object.
  281 + * @brief Clear object
264 282 *
265   - * This method properly resets the object to an initial state.
  283 + * Removes all models from the container.
266 284 ***************************************************************************/
267 285 void GModels::clear(void)
268 286 {
... ... @@ -281,6 +299,8 @@ void GModels::clear(void)
281 299  
282 300 /***********************************************************************//**
283 301 * @brief Clone instance
  302 + *
  303 + * Makes a deep copy of the model container instance.
284 304 ***************************************************************************/
285 305 GModels* GModels::clone(void) const
286 306 {
... ... @@ -293,8 +313,7 @@ GModels* GModels::clone(void) const
293 313 *
294 314 * @param[in] model Model.
295 315 *
296   - * This method appends one model to the container by cloning the model. The
297   - * model provided to the method can be deleted after calling this method.
  316 + * Appends one model to the container by making a deep copy of the model.
298 317 ***************************************************************************/
299 318 void GModels::append(const GModel& model)
300 319 {
... ... @@ -310,9 +329,11 @@ void GModels::append(const GModel&amp; model)
310 329  
311 330  
312 331 /***********************************************************************//**
313   - * @brief Load models from XML file.
  332 + * @brief Load models from XML file
314 333 *
315   - * @param[in] filename Name of XML file.
  334 + * @param[in] filename XML filename.
  335 + *
  336 + * Loads all models that are defined in an XML file.
316 337 ***************************************************************************/
317 338 void GModels::load(const std::string& filename)
318 339 {
... ... @@ -331,9 +352,11 @@ void GModels::load(const std::string&amp; filename)
331 352  
332 353  
333 354 /***********************************************************************//**
334   - * @brief Save models into XML file.
  355 + * @brief Save models into XML file
335 356 *
336   - * @param[in] filename Name of XML file.
  357 + * @param[in] filename XML filename.
  358 + *
  359 + * Saves all models in the container into an XML file.
337 360 ***************************************************************************/
338 361 void GModels::save(const std::string& filename) const
339 362 {
... ... @@ -442,10 +465,12 @@ void GModels::write(GXml&amp; xml) const
442 465  
443 466  
444 467 /***********************************************************************//**
445   - * @brief Evaluate function
  468 + * @brief Evaluate sum of all models
446 469 *
447 470 * @param[in] event Observed event.
448 471 * @param[in] obs Observation.
  472 + *
  473 + * Evaluates the sum of all models for the specified event and observation.
449 474 ***************************************************************************/
450 475 double GModels::eval(const GEvent& event, const GObservation& obs) const
451 476 {
... ... @@ -463,10 +488,13 @@ double GModels::eval(const GEvent&amp; event, const GObservation&amp; obs) const
463 488  
464 489  
465 490 /***********************************************************************//**
466   - * @brief Evaluate function and gradients
  491 + * @brief Evaluate sum and gradients of all models
467 492 *
468 493 * @param[in] event Observed event.
469 494 * @param[in] obs Observation.
  495 + *
  496 + * Evaluates the sum and the parameter gradients of all models for the
  497 + * specified event and observation.
470 498 ***************************************************************************/
471 499 double GModels::eval_gradients(const GEvent& event,
472 500 const GObservation& obs) const
... ... @@ -486,6 +514,8 @@ double GModels::eval_gradients(const GEvent&amp; event,
486 514  
487 515 /***********************************************************************//**
488 516 * @brief Print models
  517 + *
  518 + * Prints all models into a string.
489 519 ***************************************************************************/
490 520 std::string GModels::print(void) const
491 521 {
... ... @@ -531,7 +561,9 @@ void GModels::init_members(void)
531 561 *
532 562 * @param[in] models Model container.
533 563 *
534   - * This method clones all models.
  564 + * Makes a copy of all class members. All models are deep copied, and the
  565 + * linear pointer array for parameter access through the GOptimizerPars
  566 + * base class is set.
535 567 ***************************************************************************/
536 568 void GModels::copy_members(const GModels& models)
537 569 {
... ... @@ -552,9 +584,8 @@ void GModels::copy_members(const GModels&amp; models)
552 584 /***********************************************************************//**
553 585 * @brief Delete class members
554 586 *
555   - * As container classes that hold pointers need to handle themselves the
556   - * proper deallocation of memory, we loop here over all pointers and make
557   - * sure that we deallocate all models in the container.
  587 + * Deallocates all models. The method loops over the model container and
  588 + * deallocates the memory that has been allocated before.
558 589 ***************************************************************************/
559 590 void GModels::free_members(void)
560 591 {
... ... @@ -573,8 +604,8 @@ void GModels::free_members(void)
573 604 * @brief Set parameter pointers
574 605 *
575 606 * Gathers all parameter pointers from the models into a linear array of
576   - * GModelPar pointers. This allows the optimizer to see all parameters in a
577   - * linear array.
  607 + * GModelPar pointers. This exposes all model parameters to the base class
  608 + * GOptimizerPars in form of a linear array.
578 609 ***************************************************************************/
579 610 void GModels::set_pointers(void)
580 611 {
... ... @@ -604,6 +635,8 @@ void GModels::set_pointers(void)
604 635 *
605 636 * @param[in] os Output stream.
606 637 * @param[in] models Model container.
  638 + *
  639 + * Dumps a model container into the output stream.
607 640 ***************************************************************************/
608 641 std::ostream& operator<< (std::ostream& os, const GModels& models)
609 642 {
... ... @@ -620,6 +653,8 @@ std::ostream&amp; operator&lt;&lt; (std::ostream&amp; os, const GModels&amp; models)
620 653 *
621 654 * @param[in] log Logger.
622 655 * @param[in] models Model container.
  656 + *
  657 + * Logs a model container into a logger.
623 658 ***************************************************************************/
624 659 GLog& operator<< (GLog& log, const GModels& models)
625 660 {
... ...