Commit ccb262f4c6dd94a183b30dead8592ebb8c44c893

Authored by Domenico Tiziani
1 parent 1545fc8b

Added scale support to remaining methods of GModelSpatialComposite

include/GModelSpatialComposite.hpp
... ... @@ -86,9 +86,10 @@ public:
86 86  
87 87 protected:
88 88 // Protected methods
89   - void init_members(void);
90   - void copy_members(const GModelSpatialComposite& model);
91   - void free_members(void);
  89 + void init_members(void);
  90 + void copy_members(const GModelSpatialComposite& model);
  91 + void free_members(void);
  92 + double sum_of_scales(void) const;
92 93  
93 94 // Protected members
94 95 std::string m_type; //!< Model type
... ...
src/model/GModelSpatialComposite.cpp
... ... @@ -234,23 +234,24 @@ GModelSpatialComposite* GModelSpatialComposite::clone(void) const
234 234 * @param[in] gradients Compute gradients?
235 235 * @return Model value.
236 236 *
237   - * Evaluates the spatial composite model by summing all model components and
238   - * dividing the sum by the number of components.
  237 + * Evaluates the spatial composite model by summing all model components,
  238 + * weighting by their scale and dividing the sum of all scales.
239 239 ***************************************************************************/
240 240 double GModelSpatialComposite::eval(const GPhoton& photon,
241 241 const bool& gradients) const
242 242 {
243   - // Initalise value
  243 + // Initialise value
244 244 double value = 0.0;
245 245  
246 246 // Sum over all components
247 247 for (int i = 0; i < m_components.size(); ++i) {
248   - value += m_components[i]->eval(photon, gradients);
  248 + value += m_components[i]->eval(photon, gradients)
  249 + * m_component_scales[i];
249 250 }
250 251  
251   - // Normalise sum by the number of components
252   - if (m_components.size() > 0) {
253   - value /= double(m_components.size());
  252 + // Normalise sum by sum of scales
  253 + if (sum_of_scales() > 0) {
  254 + value /= sum_of_scales();
254 255 }
255 256  
256 257 // Return value
... ... @@ -273,8 +274,22 @@ GSkyDir GModelSpatialComposite::mc(const GEnergy&amp; energy,
273 274 const GTime& time,
274 275 GRan& ran) const
275 276 {
276   - // Randomly choose one model component
277   - int index = int(ran.uniform() * double(m_components.size()));
  277 + // Randomly choose one model component weighted by scale
  278 +
  279 + // Calculate random number
  280 + double random = ran.uniform() * sum_of_scales();
  281 +
  282 + // Find index of chosen model component
  283 + double sum = 0.;
  284 + int index = 0;
  285 + for (int i = 0; i < m_component_scales.size(); ++i) {
  286 + sum += m_component_scales[i];
  287 + if (random <= sum) {
  288 + index = i;
  289 + break;
  290 + }
  291 + }
  292 +
278 293 if (index >= m_components.size()) {
279 294 index = m_components.size();
280 295 }
... ... @@ -305,7 +320,8 @@ bool GModelSpatialComposite::contains(const GSkyDir&amp; dir,
305 320  
306 321 // Loop over all components
307 322 for (int i = 0; i < m_components.size(); ++i) {
308   - if (m_components[i]->contains(dir, margin)) {
  323 + if (m_components[i]->contains(dir, margin)
  324 + && m_component_scales[i] != 0) {
309 325 containment = true;
310 326 break;
311 327 }
... ... @@ -695,3 +711,25 @@ void GModelSpatialComposite::free_members(void)
695 711 // Return
696 712 return;
697 713 }
  714 +
  715 +/***********************************************************************//**
  716 + * @brief Returns sum over all scales
  717 + *
  718 + * @return sum
  719 + ***************************************************************************/
  720 +double GModelSpatialComposite::sum_of_scales(void) const
  721 +{
  722 + // Initialise sum
  723 + double sum = 0.;
  724 +
  725 + // Loop over all component scales
  726 + for (int i = 0; i < m_component_scales.size(); ++i) {
  727 +
  728 + // Add scale to sum
  729 + sum += m_component_scales[i];
  730 + }
  731 +
  732 + // Return sum
  733 + return sum;
  734 +}
  735 +
... ...