Commit f369b08f46ce44634f83fabbc312982367bd5049

Authored by Jürgen Knödlseder
1 parent f89f94b3

Integrate code (#1872).

Changed method name component_scale() to scale(). Changed member names from m_component_names to m_names and m_component_scales to m_scales.
include/GModelSpatialComposite.hpp
... ... @@ -79,10 +79,10 @@ public:
79 79 int components(void) const;
80 80 void append(const GModelSpatial& component,
81 81 const std::string& name = "",
82   - const double& scale = 1.);
  82 + const double& scale = 1.0);
83 83 const GModelSpatial* component(const int& index) const;
84 84 const GModelSpatial* component(const std::string& name) const;
85   - double component_scale(const int& index) const;
  85 + double scale(const int& index) const;
86 86 double sum_of_scales(void) const;
87 87  
88 88  
... ... @@ -93,10 +93,10 @@ protected:
93 93 void free_members(void);
94 94  
95 95 // Protected members
96   - std::string m_type; //!< Model type
97   - std::vector<GModelSpatial*> m_components; //!< Components
98   - std::vector<std::string> m_component_names; //!< Component names
99   - std::vector<double> m_component_scales; //!< Component scales
  96 + std::string m_type; //!< Model type
  97 + std::vector<GModelSpatial*> m_components; //!< Components
  98 + std::vector<std::string> m_names; //!< Component names
  99 + std::vector<double> m_scales; //!< Component scales
100 100 };
101 101  
102 102  
... ...
inst/cta/src/GCTAResponseCube.cpp
... ... @@ -1530,14 +1530,16 @@ double GCTAResponseCube::irf_composite(const GEvent&amp; event,
1530 1530 GSource src(source.name(), spat, source.energy(), source.time());
1531 1531  
1532 1532 // Compute irf value
1533   - irf += this->irf(event, source, obs) * model->component_scale(i);
  1533 + irf += this->irf(event, source, obs) * model->scale(i);
1534 1534  
1535 1535 }
1536 1536  
1537 1537 // Divide by number of model components
1538   - if (model->sum_of_scales() > 0) {
1539   - irf /= model->sum_of_scales();
  1538 + double sum = model->sum_of_scales();
  1539 + if (sum > 0.0) {
  1540 + irf /= sum;
1540 1541 }
1541 1542  
  1543 + // Return IRF value
1542 1544 return irf;
1543 1545 }
... ...
inst/cta/src/GCTAResponseIrf.cpp
... ... @@ -2883,13 +2883,14 @@ double GCTAResponseIrf::irf_composite(const GEvent&amp; event,
2883 2883 GSource src(source.name(), spat, source.energy(), source.time());
2884 2884  
2885 2885 // Compute irf value
2886   - irf += this->irf(event, src, obs) * model->component_scale(i);
  2886 + irf += this->irf(event, src, obs) * model->scale(i);
2887 2887  
2888 2888 }
2889 2889  
2890 2890 // Divide by number of model components
2891   - if (model->sum_of_scales() > 0) {
2892   - irf /= model->sum_of_scales();
  2891 + double sum = model->sum_of_scales();
  2892 + if (sum > 0.0) {
  2893 + irf /= sum;
2893 2894 }
2894 2895  
2895 2896 // Return IRF value
... ... @@ -3536,14 +3537,15 @@ double GCTAResponseIrf::nroi_composite(const GModelSky&amp; model,
3536 3537 sky.spatial(comp->component(i));
3537 3538  
3538 3539 // Compute nroi
3539   - nroi += this->nroi(sky, srcEng, srcTime, obsEng, obsTime, obs)
3540   - * comp->component_scale(i);
  3540 + nroi += this->nroi(sky, srcEng, srcTime, obsEng, obsTime, obs) *
  3541 + comp->scale(i);
3541 3542  
3542 3543 }
3543 3544  
3544 3545 // Divide by number of model components
3545   - if (comp->sum_of_scales() > 0) {
3546   - nroi /= comp->sum_of_scales();
  3546 + double sum = comp->sum_of_scales();
  3547 + if (sum > 0.0) {
  3548 + nroi /= sum;
3547 3549 }
3548 3550  
3549 3551 // Return nroi
... ...
pyext/GModelSpatialComposite.i
... ... @@ -64,10 +64,10 @@ public:
64 64 int components(void) const;
65 65 void append(const GModelSpatial& component,
66 66 const std::string& name = "",
67   - const double& scale = 1.);
  67 + const double& scale = 1.0);
68 68 const GModelSpatial* component(const int& index) const;
69 69 const GModelSpatial* component(const std::string& name) const;
70   - double component_scale(const int& index) const;
  70 + double scale(const int& index) const;
71 71 double sum_of_scales(void) const;
72 72 };
73 73  
... ...
src/model/GModelSpatialComposite.cpp
... ... @@ -245,8 +245,7 @@ double GModelSpatialComposite::eval(const GPhoton&amp; photon,
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)
249   - * m_component_scales[i];
  248 + value += m_components[i]->eval(photon, gradients) * m_scales[i];
250 249 }
251 250  
252 251 // Normalise sum by sum of scales
... ... @@ -280,10 +279,10 @@ GSkyDir GModelSpatialComposite::mc(const GEnergy&amp; energy,
280 279 double random = ran.uniform() * sum_of_scales();
281 280  
282 281 // 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];
  282 + double sum = 0.0;
  283 + int index = 0;
  284 + for (int i = 0; i < m_scales.size(); ++i) {
  285 + sum += m_scales[i];
287 286 if (random <= sum) {
288 287 index = i;
289 288 break;
... ... @@ -320,8 +319,7 @@ bool GModelSpatialComposite::contains(const GSkyDir&amp; dir,
320 319  
321 320 // Loop over all components
322 321 for (int i = 0; i < m_components.size(); ++i) {
323   - if (m_components[i]->contains(dir, margin)
324   - && m_component_scales[i] != 0) {
  322 + if (m_components[i]->contains(dir, margin) && m_scales[i] != 0.0) {
325 323 containment = true;
326 324 break;
327 325 }
... ... @@ -357,10 +355,10 @@ void GModelSpatialComposite::read(const GXmlElement&amp; xml)
357 355 GModelSpatial* ptr = registry.alloc(*spec);
358 356  
359 357 // Get component attribute from XML file
360   - std::string component_name = spec->attribute("component");
  358 + std::string name = spec->attribute("component");
361 359  
362 360 // Initialise scale
363   - double scale = 1.;
  361 + double scale = 1.0;
364 362  
365 363 // Get scale value
366 364 if (spec-> has_attribute("scale")) {
... ... @@ -368,7 +366,7 @@ void GModelSpatialComposite::read(const GXmlElement&amp; xml)
368 366 }
369 367  
370 368 // Append spatial component to container
371   - append(*ptr, component_name, scale);
  369 + append(*ptr, name, scale);
372 370  
373 371 } // endfor: loop over components
374 372  
... ... @@ -418,8 +416,7 @@ void GModelSpatialComposite::write(GXmlElement&amp; xml) const
418 416 // Find XML element with matching name
419 417 GXmlElement *matching_model = NULL;
420 418 for (int k = 0; k < xml.elements("spatialModel"); ++k) {
421   - if (xml.element("spatialModel", k)->attribute("name") ==
422   - m_component_names[i]) {
  419 + if (xml.element("spatialModel", k)->attribute("name") == m_names[i]) {
423 420 matching_model = xml.element("spatialModel", k);
424 421 break;
425 422 }
... ... @@ -450,8 +447,8 @@ void GModelSpatialComposite::write(GXmlElement&amp; xml) const
450 447 spatial->write(*matching_model);
451 448  
452 449 // Write scale to XML element if needed
453   - if (m_component_scales[i] != 1. || matching_model->has_attribute("scale")) {
454   - matching_model->attribute("scale", gammalib::str(m_component_scales[i]));
  450 + if (m_scales[i] != 1.0 || matching_model->has_attribute("scale")) {
  451 + matching_model->attribute("scale", gammalib::str(m_scales[i]));
455 452 }
456 453 }
457 454  
... ... @@ -461,8 +458,8 @@ void GModelSpatialComposite::write(GXmlElement&amp; xml) const
461 458 spatial->write(element);
462 459  
463 460 // Write scale to XML element if needed
464   - if (m_component_scales[i] != 1.) {
465   - element.attribute("scale", gammalib::str(m_component_scales[i]));
  461 + if (m_scales[i] != 1.0) {
  462 + element.attribute("scale", gammalib::str(m_scales[i]));
466 463 }
467 464 xml.append(element);
468 465 }
... ... @@ -482,14 +479,15 @@ void GModelSpatialComposite::write(GXmlElement&amp; xml) const
482 479 /***********************************************************************//**
483 480 * @brief Append spatial component
484 481 *
485   - * @param[in] component Spatial model component to append
486   - * @param[in] name Name of spatial model (can be empty)
487   - *
  482 + * @param[in] component Spatial model component to append.
  483 + * @param[in] name Name of spatial model (can be empty).
  484 + * @param[in] scale Optional spatial model scaling factor.
  485 + *
488 486 * Appends a spatial component to the composite model
489 487 ***************************************************************************/
490 488 void GModelSpatialComposite::append(const GModelSpatial& component,
491 489 const std::string& name,
492   - const double& scale)
  490 + const double& scale)
493 491 {
494 492 // Append model container
495 493 m_components.push_back(component.clone());
... ... @@ -502,7 +500,7 @@ void GModelSpatialComposite::append(const GModelSpatial&amp; component,
502 500 : gammalib::str(m_components.size());
503 501  
504 502 // Check if component name is unique, throw exception if not
505   - if (gammalib::contains(m_component_names, component_name)) {
  503 + if (gammalib::contains(m_names, component_name)) {
506 504 std::string msg = "Attempt to append component \""+component_name+"\" "
507 505 "to composite spatial model, but a component with the "
508 506 "same name exists already. Each component needs a "
... ... @@ -511,7 +509,7 @@ void GModelSpatialComposite::append(const GModelSpatial&amp; component,
511 509 }
512 510  
513 511 // Add component name
514   - m_component_names.push_back(component_name);
  512 + m_names.push_back(component_name);
515 513  
516 514 // Get number of spectral parameters from model
517 515 int npars = m_components[index]->size();
... ... @@ -530,7 +528,8 @@ void GModelSpatialComposite::append(const GModelSpatial&amp; component,
530 528  
531 529 } // endfor: loop over model parameters
532 530  
533   - m_component_scales.push_back(scale);
  531 + // Push back model scale
  532 + m_scales.push_back(scale);
534 533  
535 534 // Return
536 535 return;
... ... @@ -576,8 +575,8 @@ const GModelSpatial* GModelSpatialComposite::component(const std::string&amp; name)
576 575 {
577 576 // Check if model name is found
578 577 int index = -1;
579   - for (int i = 0; i < m_component_names.size(); ++i) {
580   - if (m_component_names[i] == name) {
  578 + for (int i = 0; i < m_names.size(); ++i) {
  579 + if (m_names[i] == name) {
581 580 index = i;
582 581 break;
583 582 }
... ... @@ -605,7 +604,7 @@ const GModelSpatial* GModelSpatialComposite::component(const std::string&amp; name)
605 604 *
606 605 * Returns the scale of a spatial component to the composite model
607 606 ***************************************************************************/
608   -double GModelSpatialComposite::component_scale(const int& index) const
  607 +double GModelSpatialComposite::scale(const int& index) const
609 608 {
610 609 // Check if index is in validity range
611 610 if (index < 0 || index >= m_components.size()) {
... ... @@ -613,25 +612,26 @@ double GModelSpatialComposite::component_scale(const int&amp; index) const
613 612 index, m_components.size());
614 613 }
615 614  
616   - // Return spatial component
617   - return m_component_scales[index];
  615 + // Return spatial component scale
  616 + return m_scales[index];
618 617 }
619 618  
  619 +
620 620 /***********************************************************************//**
621   - * @brief Returns sum over all scales
  621 + * @brief Returns sum of all model scales
622 622 *
623   - * @return sum
  623 + * @return Sum of all model scales
624 624 ***************************************************************************/
625 625 double GModelSpatialComposite::sum_of_scales(void) const
626 626 {
627 627 // Initialise sum
628   - double sum = 0.;
  628 + double sum = 0.0;
629 629  
630 630 // Loop over all component scales
631   - for (int i = 0; i < m_component_scales.size(); ++i) {
  631 + for (int i = 0; i < m_scales.size(); ++i) {
632 632  
633 633 // Add scale to sum
634   - sum += m_component_scales[i];
  634 + sum += m_scales[i];
635 635 }
636 636  
637 637 // Return sum
... ... @@ -690,8 +690,8 @@ void GModelSpatialComposite::init_members(void)
690 690  
691 691 // Initialise models vector
692 692 m_components.clear();
693   - m_component_names.clear();
694   - m_component_scales.clear();
  693 + m_names.clear();
  694 + m_scales.clear();
695 695  
696 696 // Return
697 697 return;
... ... @@ -706,9 +706,9 @@ void GModelSpatialComposite::init_members(void)
706 706 void GModelSpatialComposite::copy_members(const GModelSpatialComposite& model)
707 707 {
708 708 // Copy members
709   - m_type = model.m_type;
710   - m_component_names = model.m_component_names;
711   - m_component_scales = model.m_component_scales;
  709 + m_type = model.m_type;
  710 + m_names = model.m_names;
  711 + m_scales = model.m_scales;
712 712  
713 713 // Initialise components
714 714 m_components.clear();
... ... @@ -755,5 +755,3 @@ void GModelSpatialComposite::free_members(void)
755 755 // Return
756 756 return;
757 757 }
758   -
759   -
... ...
test/test_GModel.cpp
... ... @@ -1433,8 +1433,8 @@ void TestGModel::test_spatial_composite(void)
1433 1433 GModelSpatialComposite model2(*element);
1434 1434 test_value(model2.size(), 5);
1435 1435 test_value(model2.type(), "Composite", "Check model type");
1436   - test_value(model2.component_scale(0), 1.0);
1437   - test_value(model2.component_scale(1), 3.0);
  1436 + test_value(model2.scale(0), 1.0);
  1437 + test_value(model2.scale(1), 3.0);
1438 1438 test_value(model2.sum_of_scales(), 4.0);
1439 1439  
1440 1440 // Test access of individual parameters
... ...