Commit 3ba7606cc27019cc5ca81ea30bc6cca916b8efbc

Authored by Jürgen Knödlseder
1 parent 37d7e7d6

Implement normalize attribute in GModelSpatialDiffuseMap.

The normalize attribute provides control over whether a map should be normalize or not. By default a map will be normalize to that the spectral model provides the correct fluxes, but for some specific analyses, normalization can now be switched off by adding normalize="0" in the XML file. The attribute has also be added as optional argument to the constructors. A unit test has been added that checks the functionnality.
ChangeLog
1   -2014-02-17 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu>
  1 +2014-02-19 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu>
2 2  
3 3 * Bug fix Version 0.8.2 released
4 4 ================================
... ... @@ -12,6 +12,7 @@
12 12 Correct bug in Fermi/LAT PSF computation (#1004)
13 13 Implement CTA instrument background (#1146)
14 14 Add GModelSpectralSuperExpPlaw spectral model (#1147)
  15 + Added normalize attribute to GModelSpatialDiffuseMap (#1134)
15 16  
16 17  
17 18 2014-02-05 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu>
... ...
... ... @@ -2,7 +2,7 @@ New Features and Important Changes in GammaLib 00-08-02
2 2  
3 3 Author(s) : Juergen Knoedlseder
4 4  
5   -17 February 2014
  5 +19 February 2014
6 6  
7 7  
8 8 1. Introduction
... ... @@ -55,6 +55,10 @@ memory. This property is useful for Fermi/LAT analysis.
55 55 Implement GModelSpectralSuperExpPlaw class to model a super exponentially
56 56 cut off power law spectral functions.
57 57  
  58 +Added normalize attribute to GModelSpatialDiffuseMap that allows control
  59 +over the internal normalization of the map. By default, the map is normalized,
  60 +but by specifying normalize="0" the normalization can be switched off.
  61 +
58 62  
59 63 9. Numerics module
60 64 ------------------
... ...
include/GModelSpatialDiffuseMap.hpp
1 1 /***************************************************************************
2 2 * GModelSpatialDiffuseMap.hpp - Spatial map model class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2012-2013 by Juergen Knoedlseder *
  4 + * copyright (C) 2012-2014 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -51,10 +51,12 @@ public:
51 51 // Constructors and destructors
52 52 GModelSpatialDiffuseMap(void);
53 53 explicit GModelSpatialDiffuseMap(const GXmlElement& xml);
54   - explicit GModelSpatialDiffuseMap(const std::string& filename,
55   - const double& value = 1.0);
56   - explicit GModelSpatialDiffuseMap(const GSkymap& map,
57   - const double& value = 1.0);
  54 + GModelSpatialDiffuseMap(const std::string& filename,
  55 + const double& value = 1.0,
  56 + const bool& normalize = true);
  57 + GModelSpatialDiffuseMap(const GSkymap& map,
  58 + const double& value = 1.0,
  59 + const bool& normalize = true);
58 60 GModelSpatialDiffuseMap(const GModelSpatialDiffuseMap& model);
59 61 virtual ~GModelSpatialDiffuseMap(void);
60 62  
... ... @@ -81,6 +83,7 @@ public:
81 83 void load(const std::string& filename);
82 84 const GSkymap& map(void) const;
83 85 void map(const GSkymap& map);
  86 + bool normalize(void) const;
84 87  
85 88 protected:
86 89 // Protected methods
... ... @@ -90,10 +93,11 @@ protected:
90 93 void prepare_map(void);
91 94  
92 95 // Protected members
93   - GModelPar m_value; //!< Value
94   - GSkymap m_map; //!< Skymap
95   - std::string m_filename; //!< Name of skymap
96   - std::vector<double> m_mc_cache; //!< Monte Carlo cache
  96 + GModelPar m_value; //!< Value
  97 + GSkymap m_map; //!< Skymap
  98 + std::string m_filename; //!< Name of skymap
  99 + std::vector<double> m_mc_cache; //!< Monte Carlo cache
  100 + bool m_normalize; //!< Normalize map (default: true)
97 101 };
98 102  
99 103 /***********************************************************************//**
... ... @@ -182,4 +186,18 @@ void GModelSpatialDiffuseMap::map(const GSkymap&amp; map)
182 186 return;
183 187 }
184 188  
  189 +
  190 +/***********************************************************************//**
  191 + * @brief Return normalization flag
  192 + *
  193 + * @return True is the map has been normalized, false otherwise.
  194 + *
  195 + * Signals whether a map has been normalized or not.
  196 + ***************************************************************************/
  197 +inline
  198 +bool GModelSpatialDiffuseMap::normalize(void) const
  199 +{
  200 + return (m_normalize);
  201 +}
  202 +
185 203 #endif /* GMODELSPATIALDIFFUSEMAP_HPP */
... ...
pyext/GModelSpatialDiffuseMap.i
1 1 /***************************************************************************
2 2 * GModelSpatialDiffuseMap.i - Spatial map model class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2012-2013 by Juergen Knoedlseder *
  4 + * copyright (C) 2012-2014 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -40,10 +40,12 @@ public:
40 40 // Constructors and destructors
41 41 GModelSpatialDiffuseMap(void);
42 42 explicit GModelSpatialDiffuseMap(const GXmlElement& xml);
43   - explicit GModelSpatialDiffuseMap(const std::string& filename,
44   - const double& value = 1.0);
45   - explicit GModelSpatialDiffuseMap(const GSkymap& map,
46   - const double& value = 1.0);
  43 + GModelSpatialDiffuseMap(const std::string& filename,
  44 + const double& value = 1.0,
  45 + const bool& normalize = true);
  46 + GModelSpatialDiffuseMap(const GSkymap& map,
  47 + const double& value = 1.0,
  48 + const bool& normalize = true);
47 49 GModelSpatialDiffuseMap(const GModelSpatialDiffuseMap& model);
48 50 virtual ~GModelSpatialDiffuseMap(void);
49 51  
... ... @@ -66,6 +68,7 @@ public:
66 68 void load(const std::string& filename);
67 69 const GSkymap& map(void) const;
68 70 void map(const GSkymap& map);
  71 + bool normalize(void) const;
69 72 };
70 73  
71 74  
... ...
src/model/GModelSpatialDiffuseMap.cpp
1 1 /***************************************************************************
2 2 * GModelSpatialDiffuseMap.cpp - Spatial map model class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2012-2013 by Juergen Knoedlseder *
  4 + * copyright (C) 2012-2014 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -100,12 +100,14 @@ GModelSpatialDiffuseMap::GModelSpatialDiffuseMap(const GXmlElement&amp; xml) :
100 100 *
101 101 * @param[in] filename File name.
102 102 * @param[in] value Normalization factor (defaults to 1).
  103 + * @param[in] normalize Normalize map (defaults to true).
103 104 *
104 105 * Constructs spatial map model by loading a skymap from the file specified
105 106 * by @p filename and by setting the normalization @p value.
106 107 ***************************************************************************/
107 108 GModelSpatialDiffuseMap::GModelSpatialDiffuseMap(const std::string& filename,
108   - const double& value) :
  109 + const double& value,
  110 + const bool& normalize) :
109 111 GModelSpatialDiffuse()
110 112 {
111 113 // Initialise members
... ... @@ -114,6 +116,9 @@ GModelSpatialDiffuseMap::GModelSpatialDiffuseMap(const std::string&amp; filename,
114 116 // Set normalization parameter
115 117 m_value.value(value);
116 118  
  119 + // Set normalization flag
  120 + m_normalize = normalize;
  121 +
117 122 // Load skymap
118 123 load(filename);
119 124  
... ... @@ -127,12 +132,14 @@ GModelSpatialDiffuseMap::GModelSpatialDiffuseMap(const std::string&amp; filename,
127 132 *
128 133 * @param[in] map Sky map.
129 134 * @param[in] value Normalization factor (defaults to 1).
  135 + * @param[in] normalize Normalize map (defaults to true).
130 136 *
131 137 * Constructs spatial map model by setting the sky @p map and by setting the
132 138 * normalization @p value.
133 139 ***************************************************************************/
134 140 GModelSpatialDiffuseMap::GModelSpatialDiffuseMap(const GSkymap& map,
135   - const double& value) :
  141 + const double& value,
  142 + const bool& normalize) :
136 143 GModelSpatialDiffuse()
137 144 {
138 145 // Initialise members
... ... @@ -141,6 +148,9 @@ GModelSpatialDiffuseMap::GModelSpatialDiffuseMap(const GSkymap&amp; map,
141 148 // Set normalization parameter
142 149 m_value.value(value);
143 150  
  151 + // Set normalization flag
  152 + m_normalize = normalize;
  153 +
144 154 // Set and prepare skymap
145 155 m_map = map;
146 156 prepare_map();
... ... @@ -402,6 +412,15 @@ void GModelSpatialDiffuseMap::read(const GXmlElement&amp; xml)
402 412 " \"Normalization\" parameter.");
403 413 }
404 414  
  415 + // Get optional normalization attribute
  416 + std::string arg = xml.attribute("normalize");
  417 + if (arg == "" || arg == "1" || gammalib::tolower(arg) == "true") {
  418 + m_normalize = true;
  419 + }
  420 + else {
  421 + m_normalize = false;
  422 + }
  423 +
405 424 // Load skymap
406 425 load(xml.attribute("file"));
407 426  
... ... @@ -470,6 +489,11 @@ void GModelSpatialDiffuseMap::write(GXmlElement&amp; xml) const
470 489 " \"Normalization\" parameter.");
471 490 }
472 491  
  492 + // Set optional normalization attribute
  493 + if (!normalize()) {
  494 + xml.attribute("normalize", "0");
  495 + }
  496 +
473 497 // Return
474 498 return;
475 499 }
... ... @@ -494,6 +518,13 @@ std::string GModelSpatialDiffuseMap::print(const GChatter&amp; chatter) const
494 518  
495 519 // Append parameters
496 520 result.append("\n"+gammalib::parformat("Sky map file")+m_filename);
  521 + result.append("\n"+gammalib::parformat("Map normalization"));
  522 + if (normalize()) {
  523 + result.append("yes");
  524 + }
  525 + else {
  526 + result.append("no");
  527 + }
497 528 result.append("\n"+gammalib::parformat("Number of parameters"));
498 529 result.append(gammalib::str(size()));
499 530 for (int i = 0; i < size(); ++i) {
... ... @@ -566,6 +597,7 @@ void GModelSpatialDiffuseMap::init_members(void)
566 597 m_map.clear();
567 598 m_filename.clear();
568 599 m_mc_cache.clear();
  600 + m_normalize = true;
569 601  
570 602 // Return
571 603 return;
... ... @@ -580,10 +612,11 @@ void GModelSpatialDiffuseMap::init_members(void)
580 612 void GModelSpatialDiffuseMap::copy_members(const GModelSpatialDiffuseMap& model)
581 613 {
582 614 // Copy members
583   - m_value = model.m_value;
584   - m_map = model.m_map;
585   - m_filename = model.m_filename;
586   - m_mc_cache = model.m_mc_cache;
  615 + m_value = model.m_value;
  616 + m_map = model.m_map;
  617 + m_filename = model.m_filename;
  618 + m_mc_cache = model.m_mc_cache;
  619 + m_normalize = model.m_normalize;
587 620  
588 621 // Set parameter pointer(s)
589 622 m_pars.clear();
... ... @@ -651,12 +684,19 @@ void GModelSpatialDiffuseMap::prepare_map(void)
651 684 m_mc_cache.push_back(sum);
652 685 }
653 686  
654   - // Normalize skymap and pixel fluxes in the cache so that the values
655   - // in the cache run from 0 to 1
  687 + // Normalize fluxes in the cache so that the values in the cache
  688 + // run from 0 to 1. Optionally also normalize the sky map.
656 689 if (sum > 0.0) {
657   - for (int i = 0; i < npix; ++i) {
658   - m_map(i) /= sum;
659   - m_mc_cache[i] /= sum;
  690 + if (normalize()) {
  691 + for (int i = 0; i < npix; ++i) {
  692 + m_map(i) /= sum;
  693 + m_mc_cache[i] /= sum;
  694 + }
  695 + }
  696 + else {
  697 + for (int i = 0; i < npix; ++i) {
  698 + m_mc_cache[i] /= sum;
  699 + }
660 700 }
661 701 }
662 702  
... ...
test/test_GModel.cpp
... ... @@ -739,6 +739,32 @@ void TestGModel::test_diffuse_map(void)
739 739 test_try_failure(e);
740 740 }
741 741  
  742 + // Initialize photon direction for testing
  743 + GSkyDir dir;
  744 + GEnergy energy;
  745 + GTime time;
  746 + dir.radec_deg(201.68111, -42.68742);
  747 + GPhoton photon(dir, energy, time);
  748 +
  749 + // Test normalized map
  750 + GModelSpatialDiffuseMap map_norm(m_map_file, 3.0, true);
  751 + test_assert(map_norm.normalize(),"Model should be normalized.");
  752 + test_value(map_norm.eval(photon), 13069.603989, 1e-6, "Unexpected skymap intensity.");
  753 + GXmlElement element;
  754 + map_norm.write(element);
  755 + map_norm.read(element);
  756 + test_assert(map_norm.normalize(),"Model should be normalized.");
  757 + test_value(map_norm.eval(photon), 13069.603989, 1e-6, "Unexpected skymap intensity.");
  758 +
  759 + // Test non-normalized map
  760 + GModelSpatialDiffuseMap map_nonnorm(m_map_file, 3.0, false);
  761 + test_assert(!map_nonnorm.normalize(),"Model should not be normalized.");
  762 + test_value(map_nonnorm.eval(photon), 13069.6002755, 1e-6, "Unexpected skymap intensity.");
  763 + GXmlElement element2;
  764 + map_nonnorm.write(element2);
  765 + map_nonnorm.read(element2);
  766 + test_assert(!map_nonnorm.normalize(),"Model should not be normalized.");
  767 +
742 768 // Exit test
743 769 return;
744 770 }
... ...