Commit 1545fc8b97b672456c62aa26987ae6fd415e579b
1 parent
60616f4d
Added scale to spatial composite model and implemented read/write methods.
Showing
3 changed files
with
32 additions
and
7 deletions
include/GModelSpatialComposite.hpp
| ... | ... | @@ -78,7 +78,8 @@ public: |
| 78 | 78 | // Other methods |
| 79 | 79 | int components(void) const; |
| 80 | 80 | void append(const GModelSpatial& component, |
| 81 | - const std::string& name = ""); | |
| 81 | + const std::string& name = "", | |
| 82 | + double scale = 1.); | |
| 82 | 83 | const GModelSpatial* component(const int& index) const; |
| 83 | 84 | const GModelSpatial* component(const std::string& name) const; |
| 84 | 85 | |
| ... | ... | @@ -90,9 +91,10 @@ protected: |
| 90 | 91 | void free_members(void); |
| 91 | 92 | |
| 92 | 93 | // Protected members |
| 93 | - std::string m_type; //!< Model type | |
| 94 | - std::vector<GModelSpatial*> m_components; //!< Components | |
| 95 | - std::vector<std::string> m_component_names; //!< Component names | |
| 94 | + std::string m_type; //!< Model type | |
| 95 | + std::vector<GModelSpatial*> m_components; //!< Components | |
| 96 | + std::vector<std::string> m_component_names; //!< Component names | |
| 97 | + std::vector<double> m_component_scales; //!< Component scales | |
| 96 | 98 | }; |
| 97 | 99 | |
| 98 | 100 | ... | ... |
src/model/GModelSpatialComposite.cpp
| ... | ... | @@ -343,8 +343,16 @@ void GModelSpatialComposite::read(const GXmlElement& xml) |
| 343 | 343 | // Get component attribute from XML file |
| 344 | 344 | std::string component_name = spec->attribute("component"); |
| 345 | 345 | |
| 346 | + // Initialise scale | |
| 347 | + double scale = 1.; | |
| 348 | + | |
| 349 | + // Get scale value | |
| 350 | + if (spec-> has_attribute("scale")) { | |
| 351 | + scale = gammalib::todouble(spec->attribute("scale")); | |
| 352 | + } | |
| 353 | + | |
| 346 | 354 | // Append spatial component to container |
| 347 | - append(*ptr, component_name); | |
| 355 | + append(*ptr, component_name, scale); | |
| 348 | 356 | |
| 349 | 357 | } // endfor: loop over components |
| 350 | 358 | |
| ... | ... | @@ -424,12 +432,22 @@ void GModelSpatialComposite::write(GXmlElement& xml) const |
| 424 | 432 | // If an XML element exists then use it ... |
| 425 | 433 | if (matching_model != NULL) { |
| 426 | 434 | spatial->write(*matching_model); |
| 435 | + | |
| 436 | + // Write scale to XML element if needed | |
| 437 | + if (m_component_scales[i] != 1. || matching_model->has_attribute("scale")) { | |
| 438 | + matching_model->attribute("scale", gammalib::str(m_component_scales[i])); | |
| 439 | + } | |
| 427 | 440 | } |
| 428 | 441 | |
| 429 | 442 | // ... otherwise create new XML element |
| 430 | 443 | else { |
| 431 | 444 | GXmlElement element("spatialModel"); |
| 432 | 445 | spatial->write(element); |
| 446 | + | |
| 447 | + // Write scale to XML element if needed | |
| 448 | + if (m_component_scales[i] != 1.) { | |
| 449 | + element.attribute("scale", gammalib::str(m_component_scales[i])); | |
| 450 | + } | |
| 433 | 451 | xml.append(element); |
| 434 | 452 | } |
| 435 | 453 | |
| ... | ... | @@ -454,7 +472,8 @@ void GModelSpatialComposite::write(GXmlElement& xml) const |
| 454 | 472 | * Appends a spatial component to the composite model |
| 455 | 473 | ***************************************************************************/ |
| 456 | 474 | void GModelSpatialComposite::append(const GModelSpatial& component, |
| 457 | - const std::string& name) | |
| 475 | + const std::string& name, | |
| 476 | + double scale) | |
| 458 | 477 | { |
| 459 | 478 | // Append model container |
| 460 | 479 | m_components.push_back(component.clone()); |
| ... | ... | @@ -495,6 +514,8 @@ void GModelSpatialComposite::append(const GModelSpatial& component, |
| 495 | 514 | |
| 496 | 515 | } // endfor: loop over model parameters |
| 497 | 516 | |
| 517 | + m_component_scales.push_back(scale); | |
| 518 | + | |
| 498 | 519 | // Return |
| 499 | 520 | return; |
| 500 | 521 | } |
| ... | ... | @@ -610,6 +631,7 @@ void GModelSpatialComposite::init_members(void) |
| 610 | 631 | // Initialise models vector |
| 611 | 632 | m_components.clear(); |
| 612 | 633 | m_component_names.clear(); |
| 634 | + m_component_scales.clear(); | |
| 613 | 635 | |
| 614 | 636 | // Return |
| 615 | 637 | return; |
| ... | ... | @@ -626,6 +648,7 @@ void GModelSpatialComposite::copy_members(const GModelSpatialComposite& model) |
| 626 | 648 | // Copy members |
| 627 | 649 | m_type = model.m_type; |
| 628 | 650 | m_component_names = model.m_component_names; |
| 651 | + m_component_scales = model.m_component_scales; | |
| 629 | 652 | |
| 630 | 653 | // Initialise components |
| 631 | 654 | m_components.clear(); | ... | ... |
test/test_GModel.cpp
| ... | ... | @@ -1423,7 +1423,7 @@ void TestGModel::test_spatial_composite(void) |
| 1423 | 1423 | |
| 1424 | 1424 | // Test append method |
| 1425 | 1425 | model1.append(GModelSpatialPointSource(dir)); |
| 1426 | - model1.append(GModelSpatialRadialGauss(dir, 0.2)); | |
| 1426 | + model1.append(GModelSpatialRadialGauss(dir, 0.2), "", 1.5); | |
| 1427 | 1427 | test_value(model1.components(), 2); |
| 1428 | 1428 | test_value(model1.size(), 5); |
| 1429 | 1429 | ... | ... |