Commit 4c6e26fcdf556d5ff1ff6129e6f2470cf29ea8a7
1 parent
4104319b
Remove Npred cache in GCTAModelSpatial since the model parameters may change, he…
…nce caching is not an option (#2694)
Showing
3 changed files
with
69 additions
and
97 deletions
inst/cta/include/GCTAModelSpatial.hpp
| ... | ... | @@ -94,6 +94,7 @@ protected: |
| 94 | 94 | void free_members(void); |
| 95 | 95 | |
| 96 | 96 | // RoI integration kernel over theta |
| 97 | + /* | |
| 97 | 98 | class npred_roi_kern_theta : public GFunction { |
| 98 | 99 | public: |
| 99 | 100 | npred_roi_kern_theta(const GCTAModelSpatial* spatial, |
| ... | ... | @@ -111,6 +112,27 @@ protected: |
| 111 | 112 | GTime m_time; //!< Time |
| 112 | 113 | int m_iter; //!< Romberg iterations |
| 113 | 114 | }; |
| 115 | + */ | |
| 116 | + class npred_roi_kern_theta : public GFunction { | |
| 117 | + public: | |
| 118 | + npred_roi_kern_theta(const GCTAModelSpatial* spatial, | |
| 119 | + const GEnergy& energy, | |
| 120 | + const GTime& time, | |
| 121 | + const int& min_iter, | |
| 122 | + const int& max_iter) : | |
| 123 | + m_spatial(spatial), | |
| 124 | + m_energy(energy), | |
| 125 | + m_time(time), | |
| 126 | + m_min_iter(min_iter), | |
| 127 | + m_max_iter(max_iter) { } | |
| 128 | + double eval(const double& theta); | |
| 129 | + protected: | |
| 130 | + const GCTAModelSpatial* m_spatial; //!< Pointer to spatial component | |
| 131 | + GEnergy m_energy; //!< Energy | |
| 132 | + GTime m_time; //!< Time | |
| 133 | + int m_min_iter; //!< Minimum number of Romberg iterations | |
| 134 | + int m_max_iter; //!< Maximum number of Romberg iterations | |
| 135 | + }; | |
| 114 | 136 | |
| 115 | 137 | // RoI integration kernel over phi |
| 116 | 138 | class npred_roi_kern_phi : public GFunction { |
| ... | ... | @@ -133,12 +155,6 @@ protected: |
| 133 | 155 | |
| 134 | 156 | // Proteced members |
| 135 | 157 | std::vector<GModelPar*> m_pars; //!< Parameter pointers |
| 136 | - | |
| 137 | - // Npred cache | |
| 138 | - mutable std::vector<std::string> m_npred_names; //!< Model names | |
| 139 | - mutable std::vector<GEnergy> m_npred_energies; //!< Model energy | |
| 140 | - mutable std::vector<GTime> m_npred_times; //!< Model time | |
| 141 | - mutable std::vector<double> m_npred_values; //!< Model values | |
| 142 | 158 | }; |
| 143 | 159 | |
| 144 | 160 | ... | ... |
inst/cta/src/GCTAModelBackground.cpp
| ... | ... | @@ -409,7 +409,7 @@ double GCTAModelBackground::eval(const GEvent& event, |
| 409 | 409 | { |
| 410 | 410 | // Get reference on CTA pointing from observation and reference on CTA |
| 411 | 411 | // instrument direction from event |
| 412 | - const GCTAInstDir& dir = gammalib::cta_dir(G_EVAL, event); | |
| 412 | + const GCTAInstDir& dir = gammalib::cta_dir(G_EVAL, event); | |
| 413 | 413 | |
| 414 | 414 | // Evaluate function and gradients |
| 415 | 415 | double spat = (spatial() != NULL) | ... | ... |
inst/cta/src/GCTAModelSpatial.cpp
| ... | ... | @@ -36,6 +36,7 @@ |
| 36 | 36 | #include "GCTAEventList.hpp" |
| 37 | 37 | #include "GCTAInstDir.hpp" |
| 38 | 38 | #include "GCTAModelSpatial.hpp" |
| 39 | +#include "GCTAResponse_helpers.hpp" | |
| 39 | 40 | |
| 40 | 41 | /* __ Method name definitions ____________________________________________ */ |
| 41 | 42 | #define G_ACCESS1 "GCTAModelSpatial::operator[](int&)" |
| ... | ... | @@ -45,10 +46,12 @@ |
| 45 | 46 | /* __ Macros _____________________________________________________________ */ |
| 46 | 47 | |
| 47 | 48 | /* __ Coding definitions _________________________________________________ */ |
| 48 | -#define G_USE_NPRED_CACHE | |
| 49 | 49 | |
| 50 | 50 | /* __ Debug definitions __________________________________________________ */ |
| 51 | -//#define G_DEBUG_NPRED //!< Dump Npred information | |
| 51 | + | |
| 52 | +/* __ Constants __________________________________________________________ */ | |
| 53 | +const double g_npred_resolution = 0.1*gammalib::deg2rad; //!< Scale of bkg. | |
| 54 | + // variation | |
| 52 | 55 | |
| 53 | 56 | |
| 54 | 57 | /*========================================================================== |
| ... | ... | @@ -258,9 +261,6 @@ const GModelPar& GCTAModelSpatial::operator[](const std::string& name) const |
| 258 | 261 | * The method uses a 2D Romberg integration to numerically integrate the |
| 259 | 262 | * spatial background model component. |
| 260 | 263 | * |
| 261 | - * The method implements an integration cache so that for a given | |
| 262 | - * observation, energy and time the values are cached and not recomputed. | |
| 263 | - * | |
| 264 | 264 | * @todo The method currently assumes that the RoI is centred on DETX=DETY=0 |
| 265 | 265 | ***************************************************************************/ |
| 266 | 266 | double GCTAModelSpatial::npred(const GEnergy& energy, |
| ... | ... | @@ -268,90 +268,54 @@ double GCTAModelSpatial::npred(const GEnergy& energy, |
| 268 | 268 | const GObservation& obs) const |
| 269 | 269 | { |
| 270 | 270 | // Set number of iterations for Romberg integration. |
| 271 | - static const int iter_theta = 6; | |
| 272 | - static const int iter_phi = 6; | |
| 271 | + static const int min_iter_theta = 5; | |
| 272 | + static const int min_iter_phi = 5; | |
| 273 | + static const int max_iter_theta = 8; | |
| 274 | + static const int max_iter_phi = 8; | |
| 273 | 275 | |
| 274 | 276 | // Initialise result |
| 275 | 277 | double npred = 0.0; |
| 276 | 278 | |
| 277 | - // Initialise flag | |
| 278 | - bool has_npred = false; | |
| 279 | - | |
| 280 | - // Build unique identifier | |
| 281 | - std::string id = obs.instrument() + "::" + obs.id(); | |
| 282 | - | |
| 283 | - // Check if Npred value is already in cache | |
| 284 | - #if defined(G_USE_NPRED_CACHE) | |
| 285 | - if (!m_npred_names.empty()) { | |
| 286 | - | |
| 287 | - // Search for unique identifier, and if found, recover Npred | |
| 288 | - // value and break | |
| 289 | - for (int i = 0; i < m_npred_names.size(); ++i) { | |
| 290 | - if ((m_npred_names[i] == id) && | |
| 291 | - (m_npred_energies[i] == energy) && | |
| 292 | - (m_npred_times[i] == time)) { | |
| 293 | - npred = m_npred_values[i]; | |
| 294 | - has_npred = true; | |
| 295 | - #if defined(G_DEBUG_NPRED) | |
| 296 | - std::cout << "GCTAModelSpatial::npred:"; | |
| 297 | - std::cout << " cache=" << i; | |
| 298 | - std::cout << " npred=" << npred << std::endl; | |
| 299 | - #endif | |
| 300 | - break; | |
| 301 | - } | |
| 302 | - } | |
| 303 | - | |
| 304 | - } // endif: there were values in the Npred cache | |
| 305 | - #endif | |
| 279 | + // Retrieve CTA event list | |
| 280 | + const GCTAEventList& events = gammalib::cta_event_list(G_NPRED, obs); | |
| 306 | 281 | |
| 307 | - // Continue only if no Npred cache value has been found | |
| 308 | - if (!has_npred) { | |
| 282 | + // Get reference to RoI centre | |
| 283 | + //const GSkyDir& roi_centre = events.roi().centre().dir(); | |
| 309 | 284 | |
| 310 | - // Retrieve CTA event list | |
| 311 | - const GCTAEventList& events = gammalib::cta_event_list(G_NPRED, obs); | |
| 285 | + // Get RoI radius in radians | |
| 286 | + double roi_radius = events.roi().radius() * gammalib::deg2rad; | |
| 312 | 287 | |
| 313 | - // Get reference to RoI centre | |
| 314 | - //const GSkyDir& roi_centre = events.roi().centre().dir(); | |
| 288 | + // Set number of radial integration iterations | |
| 289 | + int iter_theta = gammalib::iter_rho(roi_radius, g_npred_resolution, | |
| 290 | + min_iter_theta, max_iter_theta); | |
| 315 | 291 | |
| 316 | - // Get RoI radius in radians | |
| 317 | - double roi_radius = events.roi().radius() * gammalib::deg2rad; | |
| 292 | + // Setup integration function | |
| 293 | + GCTAModelSpatial::npred_roi_kern_theta integrand(this, | |
| 294 | + energy, | |
| 295 | + time, | |
| 296 | + min_iter_phi, | |
| 297 | + max_iter_phi); | |
| 318 | 298 | |
| 319 | - // Setup integration function | |
| 320 | - GCTAModelSpatial::npred_roi_kern_theta integrand(this, | |
| 321 | - energy, | |
| 322 | - time, | |
| 323 | - iter_phi); | |
| 299 | + // Setup integration | |
| 300 | + GIntegral integral(&integrand); | |
| 324 | 301 | |
| 325 | - // Setup integration | |
| 326 | - GIntegral integral(&integrand); | |
| 302 | + // Set fixed number of iterations | |
| 303 | + integral.fixed_iter(iter_theta); | |
| 327 | 304 | |
| 328 | - // Set fixed number of iterations | |
| 329 | - integral.fixed_iter(iter_theta); | |
| 330 | - | |
| 331 | - // Spatially integrate radial component (assumes that RoI centre is | |
| 332 | - // at DETX=DETY=0) | |
| 333 | - npred = integral.romberg(0.0, roi_radius); | |
| 334 | - | |
| 335 | - // Store result in Npred cache | |
| 336 | - #if defined(G_USE_NPRED_CACHE) | |
| 337 | - m_npred_names.push_back(id); | |
| 338 | - m_npred_energies.push_back(energy); | |
| 339 | - m_npred_times.push_back(time); | |
| 340 | - m_npred_values.push_back(npred); | |
| 341 | - #endif | |
| 305 | + // Spatially integrate radial component (assumes that RoI centre is | |
| 306 | + // at DETX=DETY=0) | |
| 307 | + npred = integral.romberg(0.0, roi_radius); | |
| 342 | 308 | |
| 343 | - // Debug: Check for NaN | |
| 344 | - #if defined(G_NAN_CHECK) | |
| 345 | - if (gammalib::is_notanumber(npred) || gammalib::is_infinite(npred)) { | |
| 346 | - std::string origin = "GCTAModelSpatial::npred"; | |
| 347 | - std::string message = " NaN/Inf encountered (npred=" + | |
| 348 | - gammalib::str(npred) + ", roi_radius=" + | |
| 349 | - gammalib::str(roi_radius) + ")"; | |
| 350 | - gammalib::warning(origin, message); | |
| 351 | - } | |
| 352 | - #endif | |
| 353 | - | |
| 354 | - } // endif: Npred computation required | |
| 309 | + // Debug: Check for NaN | |
| 310 | + #if defined(G_NAN_CHECK) | |
| 311 | + if (gammalib::is_notanumber(npred) || gammalib::is_infinite(npred)) { | |
| 312 | + std::string origin = "GCTAModelSpatial::npred"; | |
| 313 | + std::string message = " NaN/Inf encountered (npred=" + | |
| 314 | + gammalib::str(npred) + ", roi_radius=" + | |
| 315 | + gammalib::str(roi_radius) + ")"; | |
| 316 | + gammalib::warning(origin, message); | |
| 317 | + } | |
| 318 | + #endif | |
| 355 | 319 | |
| 356 | 320 | // Return Npred |
| 357 | 321 | return npred; |
| ... | ... | @@ -372,12 +336,6 @@ void GCTAModelSpatial::init_members(void) |
| 372 | 336 | // Initialise members |
| 373 | 337 | m_pars.clear(); |
| 374 | 338 | |
| 375 | - // Initialise Npred cache | |
| 376 | - m_npred_names.clear(); | |
| 377 | - m_npred_energies.clear(); | |
| 378 | - m_npred_times.clear(); | |
| 379 | - m_npred_values.clear(); | |
| 380 | - | |
| 381 | 339 | // Return |
| 382 | 340 | return; |
| 383 | 341 | } |
| ... | ... | @@ -393,12 +351,6 @@ void GCTAModelSpatial::copy_members(const GCTAModelSpatial& model) |
| 393 | 351 | // Copy members |
| 394 | 352 | m_pars = model.m_pars; |
| 395 | 353 | |
| 396 | - // Copy cache | |
| 397 | - m_npred_names = model.m_npred_names; | |
| 398 | - m_npred_energies = model.m_npred_energies; | |
| 399 | - m_npred_times = model.m_npred_times; | |
| 400 | - m_npred_values = model.m_npred_values; | |
| 401 | - | |
| 402 | 354 | // Return |
| 403 | 355 | return; |
| 404 | 356 | } |
| ... | ... | @@ -447,8 +399,12 @@ double GCTAModelSpatial::npred_roi_kern_theta::eval(const double& theta) |
| 447 | 399 | // Setup integration |
| 448 | 400 | GIntegral integral(&integrand); |
| 449 | 401 | |
| 402 | + // Set number of azimuthal integration iterations | |
| 403 | + int iter = gammalib::iter_phi(theta, g_npred_resolution, | |
| 404 | + m_min_iter, m_max_iter); | |
| 405 | + | |
| 450 | 406 | // Set fixed number of iterations |
| 451 | - integral.fixed_iter(m_iter); | |
| 407 | + integral.fixed_iter(iter); | |
| 452 | 408 | |
| 453 | 409 | // Integrate over phi |
| 454 | 410 | value = integral.romberg(0.0, gammalib::twopi) * std::sin(theta); | ... | ... |