Commit 887cfc5e0fbdd0a2aea79c6784875a063b0e7ff9

Authored by Jürgen Knödlseder
1 parent 2b791076

Correct bug in set_mc_cone() that corrupted the pre-computed spectra.

set_mc_cone() called fetch_cube() which itself called load() which itself called set_mc_cone() with a larger cone again. This led to a neverending recursion.

I now introduced a load_cube() method that does the proper loading and that does not call set_mc_cone() again. This method is now used in fetch_cube().
include/GModelSpatialDiffuseCube.hpp
... ... @@ -108,6 +108,7 @@ protected:
108 108 void copy_members(const GModelSpatialDiffuseCube& model);
109 109 void free_members(void);
110 110 void fetch_cube(void) const;
  111 + void load_cube(const std::string& filename);
111 112 void set_energy_boundaries(void);
112 113 void update_mc_cache(void);
113 114  
... ...
src/model/GModelSpatialDiffuseCube.cpp
... ... @@ -49,8 +49,8 @@ const GModelSpatialRegistry g_spatial_cube_registry(&g_spatial_cube_seed);
49 49 #define G_MC "GModelSpatialDiffuseCube::mc(GEnergy&, GTime&, GRan&)"
50 50 #define G_READ "GModelSpatialDiffuseCube::read(GXmlElement&)"
51 51 #define G_WRITE "GModelSpatialDiffuseCube::write(GXmlElement&)"
52   -#define G_LOAD "GModelSpatialDiffuseCube::load(std::string&)"
53 52 #define G_ENERGIES "GModelSpatialDiffuseCube::energies(GEnergies&)"
  53 +#define G_LOAD_CUBE "GModelSpatialDiffuseCube::load_cube()"
54 54  
55 55 /* __ Macros _____________________________________________________________ */
56 56  
... ... @@ -704,56 +704,12 @@ void GModelSpatialDiffuseCube::write(GXmlElement& xml) const
704 704 *
705 705 * @param[in] filename cube file.
706 706 *
707   - * @exception GException::invalid_value
708   - * Number of maps in cube mismatches number of energy bins.
709   - *
710 707 * Loads cube into the model class.
711 708 ***************************************************************************/
712 709 void GModelSpatialDiffuseCube::load(const std::string& filename)
713 710 {
714   - // Initialise skymap
715   - m_cube.clear();
716   - m_logE.clear();
717   -
718   - // Store filename of cube (for XML writing). Note that we do not
719   - // expand any environment variable at this level, so that if we write
720   - // back the XML element we write the filepath with the environment
721   - // variables
722   - m_filename = filename;
723   -
724   - // Get expanded filename
725   - std::string fname = gammalib::expand_env(filename);
726   -
727 711 // Load cube
728   - m_cube.load(fname);
729   -
730   - // Load energies
731   - GEnergies energies(fname);
732   -
733   - // Extract number of energy bins
734   - int num = energies.size();
735   -
736   - // Check if energy binning is consistent with primary image hdu
737   - if (num != m_cube.nmaps() ) {
738   - std::string msg = "Number of energies in \"ENERGIES\" extension"
739   - " ("+gammalib::str(num)+") does not match the"
740   - " number of maps ("+gammalib::str(m_cube.nmaps())+""
741   - " in the map cube.\n"
742   - "The \"ENERGIES\" extension table shall provide"
743   - " one enegy value for each map in the cube.";
744   - throw GException::invalid_value(G_LOAD, msg);
745   - }
746   -
747   - // Set log10(energy) nodes, where energy is in units of MeV
748   - for (int i = 0; i < num; ++i) {
749   - m_logE.append(energies[i].log10MeV());
750   - }
751   -
752   - // Signal that cube has been loaded
753   - m_loaded = true;
754   -
755   - // Set energy boundaries
756   - set_energy_boundaries();
  712 + load_cube(filename);
757 713  
758 714 // Update Monte Carlo cache
759 715 update_mc_cache();
... ... @@ -1009,12 +965,25 @@ void GModelSpatialDiffuseCube::set_mc_cone(const GSkyDir&amp; centre,
1009 965  
1010 966 // Dump cache values for debugging
1011 967 #if defined(G_DEBUG_CACHE)
  968 + std::cout << "GModelSpatialDiffuseCube::set_mc_cone: cache";
  969 + std::cout << std::endl;
1012 970 for (int i = 0; i < m_mc_cache.size(); ++i) {
1013 971 std::cout << "i=" << i;
1014 972 std::cout << " c=" << m_mc_cache[i] << std::endl;
1015 973 }
1016 974 #endif
1017 975  
  976 + // Dump spectrum for debugging
  977 + #if defined(G_DEBUG_CACHE)
  978 + std::cout << "GModelSpatialDiffuseCube::set_mc_cone: spectrum";
  979 + std::cout << std::endl;
  980 + for (int i = 0; i < m_mc_spectrum.nodes(); ++i) {
  981 + std::cout << i;
  982 + std::cout << " " << m_mc_spectrum.energy(i);
  983 + std::cout << " " << m_mc_spectrum.intensity(i) << std::endl;
  984 + }
  985 + #endif
  986 +
1018 987 } // endif: there were cube pixels and maps
1019 988  
1020 989 // Return
... ... @@ -1199,12 +1168,78 @@ void GModelSpatialDiffuseCube::fetch_cube(void) const
1199 1168 {
1200 1169 // Load cube if it is not yet loaded
1201 1170 if (!m_loaded && !m_filename.empty()) {
  1171 +
  1172 + // Put in a OMP critical zone
1202 1173 #pragma omp critical
1203 1174 {
1204   - const_cast<GModelSpatialDiffuseCube*>(this)->load(m_filename);
1205   - }
  1175 + const_cast<GModelSpatialDiffuseCube*>(this)->load_cube(m_filename);
  1176 + } // end of pragma
  1177 +
  1178 + } // endif: file not
  1179 +
  1180 + // Return
  1181 + return;
  1182 +}
  1183 +
  1184 +
  1185 +/***********************************************************************//**
  1186 + * @brief Load cube
  1187 + *
  1188 + * @param[in] filename Cube file.
  1189 + *
  1190 + * @exception GException::invalid_value
  1191 + * Number of maps in cube mismatches number of energy bins.
  1192 + *
  1193 + * Load diffuse cube.
  1194 + ***************************************************************************/
  1195 +void GModelSpatialDiffuseCube::load_cube(const std::string& filename)
  1196 +{
  1197 + // Initialise skymap
  1198 + m_cube.clear();
  1199 + m_logE.clear();
  1200 +
  1201 + // Store filename of cube (for XML writing). Note that we do
  1202 + // not expand any environment variable at this level, so that
  1203 + // if we write back the XML element we write the filepath with
  1204 + // the environment variables
  1205 + m_filename = filename;
  1206 +
  1207 + // Get expanded filename
  1208 + std::string fname = gammalib::expand_env(filename);
  1209 +
  1210 + // Load cube
  1211 + m_cube.load(fname);
  1212 +
  1213 + // Load energies
  1214 + GEnergies energies(fname);
  1215 +
  1216 + // Extract number of energy bins
  1217 + int num = energies.size();
  1218 +
  1219 + // Check if energy binning is consistent with primary image hdu
  1220 + if (num != m_cube.nmaps() ) {
  1221 + std::string msg = "Number of energies in \"ENERGIES\""
  1222 + " extension ("+gammalib::str(num)+")"
  1223 + " does not match the number of maps ("+
  1224 + gammalib::str(m_cube.nmaps())+" in the"
  1225 + " map cube.\n"
  1226 + "The \"ENERGIES\" extension table shall"
  1227 + " provide one enegy value for each map"
  1228 + " in the cube.";
  1229 + throw GException::invalid_value(G_LOAD_CUBE, msg);
1206 1230 }
1207 1231  
  1232 + // Set log10(energy) nodes, where energy is in units of MeV
  1233 + for (int i = 0; i < num; ++i) {
  1234 + m_logE.append(energies[i].log10MeV());
  1235 + }
  1236 +
  1237 + // Signal that cube has been loaded
  1238 + m_loaded = true;
  1239 +
  1240 + // Set energy boundaries
  1241 + set_energy_boundaries();
  1242 +
1208 1243 // Return
1209 1244 return;
1210 1245 }
... ...