Commit de08d7066f132ac6998d44ee2774130632695593

Authored by Jürgen Knödlseder
1 parent f2a928d4

Monte Carlo for empty maps or map cubes no longer throws an exception (#1915)

ChangeLog
... ... @@ -3,7 +3,7 @@
3 3 * Version 1.2.0 released
4 4 =========================
5 5  
6   - Improve exception message in GModelSpatialDiffuseCube::mc (#1915)
  6 + Monte Carlo for empty maps or map cubes no longer throws an exception (#1915)
7 7 Add GModelTemporalFunc call (#1914)
8 8 Add GSkyMap FITS HDU constructor
9 9 Add leap second for 1-1-2017 (#1887)
... ...
... ... @@ -175,8 +175,10 @@ function for the simulation of transient sources. A unit test has been
175 175 added and the mc() and eval() methods have been validated using a dedicated
176 176 test script (#1914).
177 177  
178   -Improve the exception message for empty map cubes in GModelSpatialDiffuseCube::mc
179   -(#1915).
  178 +The GModelSpatialDiffuseMap::mc and GModelSpatialDiffuseCube::mc methods
  179 +now throw an GException::invalid_return_value exception that is catched
  180 +in the GModelSky::mc method and leads to the skipping of the concerned
  181 +photon. This allows handling of empty diffuse maps or map cubes (#1915).
180 182  
181 183  
182 184 9. Numerics module
... ...
include/GException.hpp
1 1 /***************************************************************************
2 2 * GException.hpp - Exception handler *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2006-2015 by Juergen Knoedlseder *
  4 + * copyright (C) 2006-2017 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -82,6 +82,13 @@ public:
82 82 const std::string& message);
83 83 };
84 84  
  85 + // Invalid return value
  86 + class invalid_return_value : public GExceptionHandler {
  87 + public:
  88 + invalid_return_value(const std::string& origin,
  89 + const std::string& message);
  90 + };
  91 +
85 92 // Out of range
86 93 class out_of_range : public GExceptionHandler {
87 94 public:
... ...
src/model/GModelSky.cpp
... ... @@ -942,8 +942,15 @@ GPhotons GModelSky::mc(const double& area,
942 942 std::cout << " Energy=" << photon.energy() << std::endl;
943 943 #endif
944 944  
945   - // Set incident photon direction
946   - photon.dir(m_spatial->mc(photon.energy(), photon.time(), ran));
  945 + // Set incident photon direction. If an invalid_return_value
  946 + // exception occurs the sky direction returned by the spatial
  947 + // Monte Carlo method is invalid and the photon is skipped.
  948 + try {
  949 + photon.dir(m_spatial->mc(photon.energy(), photon.time(), ran));
  950 + }
  951 + catch (GException::invalid_return_value) {
  952 + continue;
  953 + }
947 954  
948 955 // Debug option: dump direction
949 956 #if defined(G_DUMP_MC_DETAIL)
... ...
src/model/GModelSpatialDiffuseCube.cpp
... ... @@ -363,11 +363,15 @@ double GModelSpatialDiffuseCube::eval(const GPhoton&amp; photon,
363 363 * @exception GException::invalid_value
364 364 * No map cube defined.
365 365 * No energy boundaries defined.
366   - * Simulation cone not defined or does not overlap with map cube.
  366 + * @exception GException::invalid_return_value
  367 + * Simulation cone not defined, does not overlap with map cube or
  368 + * map cube is empty for the specified energy.
367 369 *
368 370 * Returns a random sky direction according to the intensity distribution of
369 371 * the model sky map and the specified energy. The method uses a rejection
370   - * method to determine the sky direction.
  372 + * method to determine the sky direction. If no sky direction could be
  373 + * determined, the method throws an GException::invalid_return_value
  374 + * exception.
371 375 ***************************************************************************/
372 376 GSkyDir GModelSpatialDiffuseCube::mc(const GEnergy& energy,
373 377 const GTime& time,
... ... @@ -430,7 +434,7 @@ GSkyDir GModelSpatialDiffuseCube::mc(const GEnergy&amp; energy,
430 434 "restrict the simulated energies to values where the map "
431 435 "cube is non-zero within the simulation cone.";
432 436 }
433   - throw GException::invalid_value(G_MC, msg);
  437 + throw GException::invalid_return_value(G_MC, msg);
434 438 }
435 439  
436 440 // Allocate sky direction
... ...
src/model/GModelSpatialDiffuseMap.cpp
... ... @@ -350,12 +350,15 @@ double GModelSpatialDiffuseMap::eval(const GPhoton&amp; photon,
350 350 * @return Sky direction.
351 351 *
352 352 * @exception GException::invalid_value
353   - * Simulation cone not defined or does not overlap with sky map.
354 353 * No sky map defined.
  354 + * @exception GException::invalid_return_value
  355 + * Simulation cone not defined, does not overlap with map or map
  356 + * is empty.
355 357 *
356 358 * Returns a random sky direction according to the intensity distribution of
357 359 * the model sky map. The method uses a rejection method to determine the sky
358   - * direction.
  360 + * direction. If no sky direction could be determined, the method throws an
  361 + * GException::invalid_return_value exception.
359 362 ***************************************************************************/
360 363 GSkyDir GModelSpatialDiffuseMap::mc(const GEnergy& energy,
361 364 const GTime& time,
... ... @@ -368,7 +371,7 @@ GSkyDir GModelSpatialDiffuseMap::mc(const GEnergy&amp; energy,
368 371 std::string msg = "Simulation cone has not been defined or does not "
369 372 "overlap with the sky map. Please specify a valid "
370 373 "simulation cone.";
371   - throw GException::invalid_value(G_MC, msg);
  374 + throw GException::invalid_return_value(G_MC, msg);
372 375 }
373 376  
374 377 // Determine number of skymap pixels
... ...
src/support/GException.cpp
1 1 /***************************************************************************
2 2 * GException.cpp - exception handlers *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2006-2015 by Juergen Knoedlseder *
  4 + * copyright (C) 2006-2017 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -89,6 +89,29 @@ GException::invalid_argument::invalid_argument(const std::string&amp; origin,
89 89  
90 90  
91 91 /***********************************************************************//**
  92 + * @brief Invalid return value
  93 + *
  94 + * @param[in] origin Name of method that has thrown the exception.
  95 + * @param[in] message Error message.
  96 + ***************************************************************************/
  97 +GException::invalid_return_value::invalid_return_value(const std::string& origin,
  98 + const std::string& message)
  99 +{
  100 + // Set origin
  101 + m_origin = origin;
  102 +
  103 + // Set message string
  104 + m_message = "Invalid return value.";
  105 + if (message.length() > 0) {
  106 + m_message += (" " + message);
  107 + }
  108 +
  109 + // Return
  110 + return;
  111 +}
  112 +
  113 +
  114 +/***********************************************************************//**
92 115 * @brief Index is out of range [0,elements-1]
93 116 *
94 117 * @param[in] origin Method throwing the exception.
... ...