Commit eaa5b95330fdca91b6aeafefde83c345708f9040

Authored by Domenico Tiziani
1 parent 703a0409

Improved support for fitting of component scales

include/GModelSpatialComposite.hpp
... ... @@ -81,7 +81,8 @@ public:
81 81 int components(void) const;
82 82 void append(const GModelSpatial& component,
83 83 const std::string& name = "",
84   - const double& scale = 1.0);
  84 + const double& scale = 1.0,
  85 + const bool& freeScale = false);
85 86 const GModelSpatial* component(const int& index) const;
86 87 const GModelSpatial* component(const std::string& name) const;
87 88 double scale(const int& index) const;
... ...
src/model/GModelSky.cpp
... ... @@ -37,6 +37,7 @@
37 37 #include "GModelSpatialDiffuseCube.hpp"
38 38 #include "GModelSpatialRadial.hpp"
39 39 #include "GModelSpatialElliptical.hpp"
  40 +#include "GModelSpatialComposite.hpp"
40 41 #include "GModelSpatialRegistry.hpp"
41 42 #include "GModelSpectralRegistry.hpp"
42 43 #include "GModelTemporalRegistry.hpp"
... ... @@ -1211,6 +1212,11 @@ void GModelSky::set_type(void)
1211 1212 m_type = "ExtendedSource";
1212 1213 }
1213 1214  
  1215 + // ... otherwise, is spatial model a composite source?
  1216 + else if (dynamic_cast<const GModelSpatialComposite*>(m_spatial) != NULL) {
  1217 + m_type = "CompositeSource";
  1218 + }
  1219 +
1214 1220 // ... otherwise we have a diffuse model
1215 1221 else {
1216 1222 m_type = "DiffuseSource";
... ...
src/model/GModelSpatialComposite.cpp
... ... @@ -360,13 +360,21 @@ void GModelSpatialComposite::read(const GXmlElement&amp; xml)
360 360 // Initialise scale
361 361 double scale = 1.0;
362 362  
  363 + // Initialise freeScale
  364 + double freeScale = false;
  365 +
363 366 // Get scale value
364 367 if (spec-> has_attribute("scale")) {
365 368 scale = gammalib::todouble(spec->attribute("scale"));
  369 +
  370 + // Check if scale is fixed
  371 + if (spec->has_attribute("free_scale")) {
  372 + freeScale = gammalib::toint(spec->attribute("free_scale"));
  373 + }
366 374 }
367 375  
368 376 // Append spatial component to container
369   - append(*ptr, name, scale);
  377 + append(*ptr, name, scale, freeScale);
370 378  
371 379 } // endfor: loop over components
372 380  
... ... @@ -416,7 +424,7 @@ void GModelSpatialComposite::write(GXmlElement&amp; xml) const
416 424 // Find XML element with matching name
417 425 GXmlElement *matching_model = NULL;
418 426 for (int k = 0; k < xml.elements("spatialModel"); ++k) {
419   - if (xml.element("spatialModel", k)->attribute("name") == m_names[i]) {
  427 + if (xml.element("spatialModel", k)->attribute("component") == m_names[i]) {
420 428 matching_model = xml.element("spatialModel", k);
421 429 break;
422 430 }
... ... @@ -447,8 +455,11 @@ void GModelSpatialComposite::write(GXmlElement&amp; xml) const
447 455 spatial->write(*matching_model);
448 456  
449 457 // Write scale to XML element if needed
450   - if (m_scales_fit[i]->value() != 1.0 || matching_model->has_attribute("scale")) {
  458 + if (m_scales_fit[i]->value() != 1.0 || matching_model->has_attribute("scale")
  459 + || m_scales_fit[i]->is_free()) {
451 460 matching_model->attribute("scale", gammalib::str(m_scales_fit[i]->value()));
  461 + matching_model->attribute("free_scale",
  462 + gammalib::str(m_scales_fit[i]->is_free()));
452 463 }
453 464 }
454 465  
... ... @@ -457,9 +468,14 @@ void GModelSpatialComposite::write(GXmlElement&amp; xml) const
457 468 GXmlElement element("spatialModel");
458 469 spatial->write(element);
459 470  
  471 + // Write component name
  472 + element.attribute("component", m_names[i]);
  473 +
460 474 // Write scale to XML element if needed
461   - if (m_scales_fit[i]->value() != 1.0) {
  475 + if (m_scales_fit[i]->value() != 1.0 || m_scales_fit[i]->is_free()) {
462 476 element.attribute("scale", gammalib::str(m_scales_fit[i]->value()));
  477 + element.attribute("free_scale",
  478 + gammalib::str(m_scales_fit[i]->is_free()));
463 479 }
464 480 xml.append(element);
465 481 }
... ... @@ -482,12 +498,14 @@ void GModelSpatialComposite::write(GXmlElement&amp; xml) const
482 498 * @param[in] component Spatial model component to append.
483 499 * @param[in] name Name of spatial model (can be empty).
484 500 * @param[in] scale Optional spatial model scaling factor.
  501 + * @param[in] freeScale Free scale parameter (default:false).
485 502 *
486 503 * Appends a spatial component to the composite model
487 504 ***************************************************************************/
488 505 void GModelSpatialComposite::append(const GModelSpatial& component,
489 506 const std::string& name,
490   - const double& scale)
  507 + const double& scale,
  508 + const bool& freeScale)
491 509 {
492 510 // Append model container
493 511 m_components.push_back(component.clone());
... ... @@ -532,13 +550,13 @@ void GModelSpatialComposite::append(const GModelSpatial&amp; component,
532 550 // Initialise i-th scale parameter
533 551 m_scales_fit.push_back(new GModelPar());
534 552 m_scales_fit[index]->clear();
535   - m_scales_fit[index]->name("scales_fit" + gammalib::str(m_components.size()));
  553 + m_scales_fit[index]->name(component_name + ":scale");
536 554 m_scales_fit[index]->value(scale);
537   - m_scales_fit[index]->range(0.001,1000.);
538   - if(index==0)
539   - m_scales_fit[index]->fix();
540   - else
  555 + m_scales_fit[index]->range(1e-10,1e13);
  556 + if(freeScale)
541 557 m_scales_fit[index]->free();
  558 + else
  559 + m_scales_fit[index]->fix();
542 560 m_scales_fit[index]->scale(1.0);
543 561 m_scales_fit[index]->gradient(0.0);
544 562 m_scales_fit[index]->has_grad(false); // for the moment use numerical gradient
... ...
test/test_GModel.cpp
... ... @@ -1263,13 +1263,13 @@ void TestGModel::test_spatial_composite(void)
1263 1263 model1.append(GModelSpatialPointSource(dir1));
1264 1264 model1.append(GModelSpatialRadialGauss(dir1, 0.2), "", 1.5);
1265 1265 test_value(model1.components(), 2);
1266   - test_value(model1.size(), 5);
  1266 + test_value(model1.size(), 7);
1267 1267  
1268 1268 // Test XML constructor
1269 1269 GXml xml(m_xml_model_spatial_composite);
1270 1270 GXmlElement* element = xml.element(0)->element(0)->element("spatialModel", 0);
1271 1271 GModelSpatialComposite model2(*element);
1272   - test_value(model2.size(), 5);
  1272 + test_value(model2.size(), 7);
1273 1273 test_value(model2.type(), "Composite", "Check model type");
1274 1274 test_value(model2.scale(0), 1.0);
1275 1275 test_value(model2.scale(1), 3.0);
... ...