Commit 8b221ddd7008bd975d789686b704b088f7033db2
1 parent
d6d79688
Add generic response cache class GResponseCache (#3202)
Showing
8 changed files
with
744 additions
and
7 deletions
NEWS
| 1 | 1 | New Features and Important Changes in GammaLib 1.7.0 |
| 2 | 2 | |
| 3 | -17 February 2020 | |
| 3 | +9 April 2020 | |
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | 1. Introduction |
| ... | ... | @@ -12,7 +12,7 @@ made since the last release of GammaLib. |
| 12 | 12 | 2. Public interface modifications |
| 13 | 13 | --------------------------------- |
| 14 | 14 | The following classes have been added: |
| 15 | -- none | |
| 15 | +- GResponseCache | |
| 16 | 16 | |
| 17 | 17 | The following classes have been removed: |
| 18 | 18 | - GCTAResponseCache::ndirs() |
| ... | ... | @@ -49,7 +49,7 @@ The following methods have been removed: |
| 49 | 49 | - none |
| 50 | 50 | |
| 51 | 51 | The following methods have been renamed: |
| 52 | -- none | |
| 52 | +- GInstDir::hash() - Returns a cached value | |
| 53 | 53 | |
| 54 | 54 | The arguments for the following methods have been changed: |
| 55 | 55 | - GTime::utc(int&) - Add optional precision value |
| ... | ... | @@ -99,6 +99,12 @@ None |
| 99 | 99 | |
| 100 | 100 | 10. Observation module |
| 101 | 101 | ---------------------- |
| 102 | +The GResponseCache class was added as a copy of GCTAResponseCache to implement | |
| 103 | +a generic response cache for response computation. | |
| 104 | + | |
| 105 | +An GInstDir::hash() method was added that returns a unique double precision | |
| 106 | +value for every class instance that can be used for response caching. | |
| 107 | + | |
| 102 | 108 | Add GObservations::npred() methods that compute the number of predicted events |
| 103 | 109 | for a model with a given name or for a given reference to a model. Optionally |
| 104 | 110 | an instrument string can be passed so that the number of predicted events are | ... | ... |
include/GResponseCache.hpp
0 → 100644
| 1 | +/*************************************************************************** | |
| 2 | + * GResponseCache.hpp - Response cache class * | |
| 3 | + * ----------------------------------------------------------------------- * | |
| 4 | + * copyright (C) 2020 by Juergen Knoedlseder * | |
| 5 | + * ----------------------------------------------------------------------- * | |
| 6 | + * * | |
| 7 | + * This program is free software: you can redistribute it and/or modify * | |
| 8 | + * it under the terms of the GNU General Public License as published by * | |
| 9 | + * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | + * (at your option) any later version. * | |
| 11 | + * * | |
| 12 | + * This program is distributed in the hope that it will be useful, * | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | + * GNU General Public License for more details. * | |
| 16 | + * * | |
| 17 | + * You should have received a copy of the GNU General Public License * | |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | + * * | |
| 20 | + ***************************************************************************/ | |
| 21 | +/** | |
| 22 | + * @file GResponseCache.hpp | |
| 23 | + * @brief Response cache class definition | |
| 24 | + * @author Juergen Knoedlseder | |
| 25 | + */ | |
| 26 | + | |
| 27 | +#ifndef GRESPONSECACHE_HPP | |
| 28 | +#define GRESPONSECACHE_HPP | |
| 29 | + | |
| 30 | +/* __ Includes ___________________________________________________________ */ | |
| 31 | +#include <string> | |
| 32 | +#include <map> | |
| 33 | +#include "GBase.hpp" | |
| 34 | +#include "GEnergy.hpp" | |
| 35 | + | |
| 36 | +/* __ Forward declarations _______________________________________________ */ | |
| 37 | +class GInstDir; | |
| 38 | + | |
| 39 | +/* __ Constants __________________________________________________________ */ | |
| 40 | + | |
| 41 | +/* __ Typedefs ___________________________________________________________ */ | |
| 42 | +typedef std::map<double,double> GResponseCacheElement; | |
| 43 | +typedef std::map<std::string,GResponseCacheElement> GResponseCacheName; | |
| 44 | + | |
| 45 | + | |
| 46 | +/***********************************************************************//** | |
| 47 | + * @class GResponseCache | |
| 48 | + * | |
| 49 | + * @brief Response cache class | |
| 50 | + * | |
| 51 | + * The class implements a cache for the Instrument Response Function values | |
| 52 | + * so that the values do not need to be recomputed each time but can be | |
| 53 | + * fetched from the cache. | |
| 54 | + ***************************************************************************/ | |
| 55 | +class GResponseCache : public GBase { | |
| 56 | + | |
| 57 | +public: | |
| 58 | + // Constructors and destructors | |
| 59 | + GResponseCache(void); | |
| 60 | + GResponseCache(const GResponseCache& cache); | |
| 61 | + virtual ~GResponseCache(void); | |
| 62 | + | |
| 63 | + // Operators | |
| 64 | + GResponseCache& operator=(const GResponseCache& cache); | |
| 65 | + | |
| 66 | + // Methods | |
| 67 | + void clear(void); | |
| 68 | + GResponseCache* clone(void) const; | |
| 69 | + std::string classname(void) const; | |
| 70 | + int size(void) const; | |
| 71 | + bool is_empty(void) const; | |
| 72 | + void set(const std::string& name, | |
| 73 | + const GEnergy& ereco, | |
| 74 | + const GEnergy& etrue, | |
| 75 | + const double& value); | |
| 76 | + void set(const std::string& name, | |
| 77 | + const GInstDir& dir, | |
| 78 | + const GEnergy& ereco, | |
| 79 | + const GEnergy& etrue, | |
| 80 | + const double& value); | |
| 81 | + void remove(const std::string& name); | |
| 82 | + bool contains(const std::string& name, | |
| 83 | + const GEnergy& ereco, | |
| 84 | + const GEnergy& etrue, | |
| 85 | + double* value = NULL) const; | |
| 86 | + bool contains(const std::string& name, | |
| 87 | + const GInstDir& dir, | |
| 88 | + const GEnergy& ereco, | |
| 89 | + const GEnergy& etrue, | |
| 90 | + double* value = NULL) const; | |
| 91 | + std::string print(const GChatter& chatter = NORMAL) const; | |
| 92 | + | |
| 93 | +protected: | |
| 94 | + // Protected methods | |
| 95 | + void init_members(void); | |
| 96 | + void copy_members(const GResponseCache& cache); | |
| 97 | + void free_members(void); | |
| 98 | + double encode(const GEnergy& ereco, | |
| 99 | + const GEnergy& etrue) const; | |
| 100 | + double encode(const GInstDir& dir, | |
| 101 | + const GEnergy& ereco, | |
| 102 | + const GEnergy& etrue) const; | |
| 103 | + | |
| 104 | + // Protected members | |
| 105 | + GResponseCacheName m_cache; | |
| 106 | +}; | |
| 107 | + | |
| 108 | + | |
| 109 | +/***********************************************************************//** | |
| 110 | + * @brief Return class name | |
| 111 | + * | |
| 112 | + * @return String containing the class name ("GResponseCache"). | |
| 113 | + ***************************************************************************/ | |
| 114 | +inline | |
| 115 | +std::string GResponseCache::classname(void) const | |
| 116 | +{ | |
| 117 | + return ("GResponseCache"); | |
| 118 | +} | |
| 119 | + | |
| 120 | + | |
| 121 | +/***********************************************************************//** | |
| 122 | + * @brief Checks whether the cache is empty | |
| 123 | + * | |
| 124 | + * @return True if cache is empty, false otherwise. | |
| 125 | + * | |
| 126 | + * Checks whether the response cache is empty. | |
| 127 | + ***************************************************************************/ | |
| 128 | +inline | |
| 129 | +bool GResponseCache::is_empty(void) const | |
| 130 | +{ | |
| 131 | + return (m_cache.empty()); | |
| 132 | +} | |
| 133 | + | |
| 134 | + | |
| 135 | +/***********************************************************************//** | |
| 136 | + * @brief Remove cache for source | |
| 137 | + * | |
| 138 | + * @param[in] name Source name. | |
| 139 | + * | |
| 140 | + * Remove cache for source with @p name. | |
| 141 | + ***************************************************************************/ | |
| 142 | +inline | |
| 143 | +void GResponseCache::remove(const std::string& name) | |
| 144 | +{ | |
| 145 | + m_cache.erase(name); | |
| 146 | + return; | |
| 147 | +} | |
| 148 | + | |
| 149 | +#endif /* GRESPONSECACHE_HPP */ | ... | ... |
include/GammaLib.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GammaLib.hpp - Gamma-Ray Astronomy Library Header file * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2006-2018 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2006-2020 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -188,6 +188,7 @@ |
| 188 | 188 | #include "GPhases.hpp" |
| 189 | 189 | #include "GInstDir.hpp" |
| 190 | 190 | #include "GResponse.hpp" |
| 191 | +#include "GResponseCache.hpp" | |
| 191 | 192 | #include "GPhotons.hpp" |
| 192 | 193 | #include "GPhoton.hpp" |
| 193 | 194 | #include "GSource.hpp" | ... | ... |
include/Makefile.am
| 1 | 1 | ############################################################################# |
| 2 | 2 | # Makefile.am for include module # |
| 3 | 3 | # ------------------------------------------------------------------------- # |
| 4 | -# Copyright (C) 2016-2018 Juergen Knoedlseder # | |
| 4 | +# Copyright (C) 2016-2020 Juergen Knoedlseder # | |
| 5 | 5 | # ------------------------------------------------------------------------- # |
| 6 | 6 | # # |
| 7 | 7 | # This program is free software: you can redistribute it and/or modify # |
| ... | ... | @@ -148,6 +148,7 @@ pkginclude_HEADERS = GammaLib.hpp \ |
| 148 | 148 | GPhases.hpp \ |
| 149 | 149 | GInstDir.hpp \ |
| 150 | 150 | GResponse.hpp \ |
| 151 | + GResponseCache.hpp \ | |
| 151 | 152 | GPhotons.hpp \ |
| 152 | 153 | GPhoton.hpp \ |
| 153 | 154 | GSource.hpp \ | ... | ... |
pyext/GResponseCache.i
0 → 100644
| 1 | +/*************************************************************************** | |
| 2 | + * GResponseCache.i - Response cache class * | |
| 3 | + * ----------------------------------------------------------------------- * | |
| 4 | + * copyright (C) 2020 by Juergen Knoedlseder * | |
| 5 | + * ----------------------------------------------------------------------- * | |
| 6 | + * * | |
| 7 | + * This program is free software: you can redistribute it and/or modify * | |
| 8 | + * it under the terms of the GNU General Public License as published by * | |
| 9 | + * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | + * (at your option) any later version. * | |
| 11 | + * * | |
| 12 | + * This program is distributed in the hope that it will be useful, * | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | + * GNU General Public License for more details. * | |
| 16 | + * * | |
| 17 | + * You should have received a copy of the GNU General Public License * | |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | + * * | |
| 20 | + ***************************************************************************/ | |
| 21 | +/** | |
| 22 | + * @file GResponseCache.i | |
| 23 | + * @brief Response cache class definition | |
| 24 | + * @author Juergen Knoedlseder | |
| 25 | + */ | |
| 26 | +%{ | |
| 27 | +/* Put headers and other declarations here that are needed for compilation */ | |
| 28 | +#include "GResponseCache.hpp" | |
| 29 | +%} | |
| 30 | + | |
| 31 | +/* __ Typemaps ___________________________________________________________ */ | |
| 32 | +/* | |
| 33 | + * Typemap so that the contains() method returns two values, first the | |
| 34 | + * flag indicating whether the value exists or not, and second the | |
| 35 | + * value, i.e. | |
| 36 | + * | |
| 37 | + * flag, value = cache.contains('Crab', GEnergy(1.0,'TeV'), GEnergy(1.0,'TeV')) | |
| 38 | + * | |
| 39 | + */ | |
| 40 | +%include "typemaps.i" | |
| 41 | +%apply double *OUTPUT { double* value }; | |
| 42 | + | |
| 43 | + | |
| 44 | +/***********************************************************************//** | |
| 45 | + * @class GResponseCache | |
| 46 | + * | |
| 47 | + * @brief Response cache class | |
| 48 | + ***************************************************************************/ | |
| 49 | +class GResponseCache : public GBase { | |
| 50 | + | |
| 51 | +public: | |
| 52 | + // Constructors and destructors | |
| 53 | + GResponseCache(void); | |
| 54 | + GResponseCache(const GResponseCache& cache); | |
| 55 | + virtual ~GResponseCache(void); | |
| 56 | + | |
| 57 | + // Methods | |
| 58 | + void clear(void); | |
| 59 | + GResponseCache* clone(void) const; | |
| 60 | + std::string classname(void) const; | |
| 61 | + int size(void) const; | |
| 62 | + bool is_empty(void) const; | |
| 63 | + void set(const std::string& name, | |
| 64 | + const GEnergy& ereco, | |
| 65 | + const GEnergy& etrue, | |
| 66 | + const double& value); | |
| 67 | + void set(const std::string& name, | |
| 68 | + const GInstDir& dir, | |
| 69 | + const GEnergy& ereco, | |
| 70 | + const GEnergy& etrue, | |
| 71 | + const double& value); | |
| 72 | + void remove(const std::string& name); | |
| 73 | + bool contains(const std::string& name, | |
| 74 | + const GEnergy& ereco, | |
| 75 | + const GEnergy& etrue, | |
| 76 | + double* value) const; | |
| 77 | + bool contains(const std::string& name, | |
| 78 | + const GInstDir& dir, | |
| 79 | + const GEnergy& ereco, | |
| 80 | + const GEnergy& etrue, | |
| 81 | + double* value) const; | |
| 82 | +}; | |
| 83 | + | |
| 84 | + | |
| 85 | +/***********************************************************************//** | |
| 86 | + * @brief GResponseCache class extension | |
| 87 | + ***************************************************************************/ | |
| 88 | +%extend GResponseCache { | |
| 89 | + GResponseCache copy() { | |
| 90 | + return (*self); | |
| 91 | + } | |
| 92 | +}; | ... | ... |
pyext/gammalib/obs.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * obs.i - Observation module * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2011-2017 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2011-2020 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -196,6 +196,7 @@ |
| 196 | 196 | %include "GRoi.i" |
| 197 | 197 | %include "GPhases.i" |
| 198 | 198 | %include "GResponse.i" |
| 199 | +%include "GResponseCache.i" | |
| 199 | 200 | %include "GPhoton.i" |
| 200 | 201 | %include "GPhotons.i" |
| 201 | 202 | %include "GSource.i" | ... | ... |
src/obs/GResponseCache.cpp
0 → 100644
| 1 | +/*************************************************************************** | |
| 2 | + * GResponseCache.cpp - Response cache class * | |
| 3 | + * ----------------------------------------------------------------------- * | |
| 4 | + * copyright (C) 2020 by Juergen Knoedlseder * | |
| 5 | + * ----------------------------------------------------------------------- * | |
| 6 | + * * | |
| 7 | + * This program is free software: you can redistribute it and/or modify * | |
| 8 | + * it under the terms of the GNU General Public License as published by * | |
| 9 | + * the Free Software Foundation, either version 3 of the License, or * | |
| 10 | + * (at your option) any later version. * | |
| 11 | + * * | |
| 12 | + * This program is distributed in the hope that it will be useful, * | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 15 | + * GNU General Public License for more details. * | |
| 16 | + * * | |
| 17 | + * You should have received a copy of the GNU General Public License * | |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * | |
| 19 | + * * | |
| 20 | + ***************************************************************************/ | |
| 21 | +/** | |
| 22 | + * @file GResponseCache.cpp | |
| 23 | + * @brief Response cache class implementation | |
| 24 | + * @author Juergen Knoedlseder | |
| 25 | + */ | |
| 26 | + | |
| 27 | +/* __ Includes ___________________________________________________________ */ | |
| 28 | +#ifdef HAVE_CONFIG_H | |
| 29 | +#include <config.h> | |
| 30 | +#endif | |
| 31 | +#include "GTools.hpp" | |
| 32 | +#include "GResponseCache.hpp" | |
| 33 | +#include "GInstDir.hpp" | |
| 34 | + | |
| 35 | +/* __ Method name definitions ____________________________________________ */ | |
| 36 | + | |
| 37 | +/* __ Macros _____________________________________________________________ */ | |
| 38 | + | |
| 39 | +/* __ Coding definitions _________________________________________________ */ | |
| 40 | + | |
| 41 | +/* __ Debug definitions __________________________________________________ */ | |
| 42 | + | |
| 43 | + | |
| 44 | +/*========================================================================== | |
| 45 | + = = | |
| 46 | + = Constructors/destructors = | |
| 47 | + = = | |
| 48 | + ==========================================================================*/ | |
| 49 | + | |
| 50 | +/***********************************************************************//** | |
| 51 | + * @brief Void constructor | |
| 52 | + ***************************************************************************/ | |
| 53 | +GResponseCache::GResponseCache(void) | |
| 54 | +{ | |
| 55 | + // Initialise class members | |
| 56 | + init_members(); | |
| 57 | + | |
| 58 | + // Return | |
| 59 | + return; | |
| 60 | +} | |
| 61 | + | |
| 62 | + | |
| 63 | +/***********************************************************************//** | |
| 64 | + * @brief Copy constructor | |
| 65 | + * | |
| 66 | + * @param[in] cache Response cache. | |
| 67 | + ***************************************************************************/ | |
| 68 | +GResponseCache::GResponseCache(const GResponseCache& cache) | |
| 69 | +{ | |
| 70 | + // Initialise class members | |
| 71 | + init_members(); | |
| 72 | + | |
| 73 | + // Copy members | |
| 74 | + copy_members(cache); | |
| 75 | + | |
| 76 | + // Return | |
| 77 | + return; | |
| 78 | +} | |
| 79 | + | |
| 80 | + | |
| 81 | +/***********************************************************************//** | |
| 82 | + * @brief Destructor | |
| 83 | + ***************************************************************************/ | |
| 84 | +GResponseCache::~GResponseCache(void) | |
| 85 | +{ | |
| 86 | + // Free members | |
| 87 | + free_members(); | |
| 88 | + | |
| 89 | + // Return | |
| 90 | + return; | |
| 91 | +} | |
| 92 | + | |
| 93 | + | |
| 94 | +/*========================================================================== | |
| 95 | + = = | |
| 96 | + = Operators = | |
| 97 | + = = | |
| 98 | + ==========================================================================*/ | |
| 99 | + | |
| 100 | +/***********************************************************************//** | |
| 101 | + * @brief Assignment operator | |
| 102 | + * | |
| 103 | + * @param[in] cache Response cache. | |
| 104 | + * @return Response cache. | |
| 105 | + ***************************************************************************/ | |
| 106 | +GResponseCache& GResponseCache::operator=(const GResponseCache& cache) | |
| 107 | +{ | |
| 108 | + // Execute only if object is not identical | |
| 109 | + if (this != &cache) { | |
| 110 | + | |
| 111 | + // Free members | |
| 112 | + free_members(); | |
| 113 | + | |
| 114 | + // Initialise private members | |
| 115 | + init_members(); | |
| 116 | + | |
| 117 | + // Copy members | |
| 118 | + copy_members(cache); | |
| 119 | + | |
| 120 | + } // endif: object was not identical | |
| 121 | + | |
| 122 | + // Return this object | |
| 123 | + return *this; | |
| 124 | +} | |
| 125 | + | |
| 126 | + | |
| 127 | +/*========================================================================== | |
| 128 | + = = | |
| 129 | + = Public methods = | |
| 130 | + = = | |
| 131 | + ==========================================================================*/ | |
| 132 | + | |
| 133 | +/***********************************************************************//** | |
| 134 | + * @brief Clear response cache | |
| 135 | + ***************************************************************************/ | |
| 136 | +void GResponseCache::clear(void) | |
| 137 | +{ | |
| 138 | + // Free members | |
| 139 | + free_members(); | |
| 140 | + | |
| 141 | + // Initialise private members | |
| 142 | + init_members(); | |
| 143 | + | |
| 144 | + // Return | |
| 145 | + return; | |
| 146 | +} | |
| 147 | + | |
| 148 | + | |
| 149 | +/***********************************************************************//** | |
| 150 | + * @brief Return number of elements in cache | |
| 151 | + * | |
| 152 | + * @return Number of elements in cache | |
| 153 | + * | |
| 154 | + * Returns the number of elements in the response cache. | |
| 155 | + ***************************************************************************/ | |
| 156 | +int GResponseCache::size(void) const | |
| 157 | +{ | |
| 158 | + // Initialize size | |
| 159 | + int size = 0; | |
| 160 | + | |
| 161 | + // Compute size | |
| 162 | + for (GResponseCacheName::const_iterator it_name = m_cache.begin(); | |
| 163 | + it_name != m_cache.end(); ++it_name) { | |
| 164 | + size += it_name->second.size(); | |
| 165 | + } | |
| 166 | + | |
| 167 | + // Return size | |
| 168 | + return size; | |
| 169 | +} | |
| 170 | + | |
| 171 | + | |
| 172 | +/***********************************************************************//** | |
| 173 | + * @brief Set cache value | |
| 174 | + * | |
| 175 | + * @param[in] name Cache name. | |
| 176 | + * @param[in] ereco Reconstructed energy. | |
| 177 | + * @param[in] etrue True energy. | |
| 178 | + * @param[in] value Cache value. | |
| 179 | + * | |
| 180 | + * Set cache value for a given @p name, reconstructed energy @p ereco, and | |
| 181 | + * true energy @p etrue. | |
| 182 | + ***************************************************************************/ | |
| 183 | +void GResponseCache::set(const std::string& name, | |
| 184 | + const GEnergy& ereco, | |
| 185 | + const GEnergy& etrue, | |
| 186 | + const double& value) | |
| 187 | +{ | |
| 188 | + // Get element identifier | |
| 189 | + double element = encode(ereco, etrue); | |
| 190 | + | |
| 191 | + // Set cache value | |
| 192 | + m_cache[name][element] = value; | |
| 193 | + | |
| 194 | + // Return | |
| 195 | + return; | |
| 196 | +} | |
| 197 | + | |
| 198 | + | |
| 199 | +/***********************************************************************//** | |
| 200 | + * @brief Set cache value | |
| 201 | + * | |
| 202 | + * @param[in] name Cache name. | |
| 203 | + * @param[in] dir Instrument direction. | |
| 204 | + * @param[in] ereco Reconstructed energy. | |
| 205 | + * @param[in] etrue True energy. | |
| 206 | + * @param[in] value Cache value. | |
| 207 | + * | |
| 208 | + * Set cache value for a given @p name, instrument direction @p dir, | |
| 209 | + * reconstructed energy @p ereco, and true energy @p etrue. | |
| 210 | + ***************************************************************************/ | |
| 211 | +void GResponseCache::set(const std::string& name, | |
| 212 | + const GInstDir& dir, | |
| 213 | + const GEnergy& ereco, | |
| 214 | + const GEnergy& etrue, | |
| 215 | + const double& value) | |
| 216 | +{ | |
| 217 | + // Get element identifier | |
| 218 | + double element = encode(dir, ereco, etrue); | |
| 219 | + | |
| 220 | + // Set cache value | |
| 221 | + m_cache[name][element] = value; | |
| 222 | + | |
| 223 | + // Return | |
| 224 | + return; | |
| 225 | +} | |
| 226 | + | |
| 227 | + | |
| 228 | +/***********************************************************************//** | |
| 229 | + * @brief Check if cache contains a value for specific parameters | |
| 230 | + * | |
| 231 | + * @param[in] name Cache name. | |
| 232 | + * @param[in] ereco Reconstructed energy. | |
| 233 | + * @param[in] etrue True energy. | |
| 234 | + * @param[out] value Pointer to cached value (only if found). | |
| 235 | + * @return True if cached value was found, otherwise false. | |
| 236 | + * | |
| 237 | + * Check if the cache contains a value for a given @p name, reconstructed | |
| 238 | + * energy @p ereco, and true energy @p etrue. | |
| 239 | + * | |
| 240 | + * If the @p value pointer argument is not NULL, the method will return the | |
| 241 | + * cached value through this argument in case that the value exists. | |
| 242 | + ***************************************************************************/ | |
| 243 | +bool GResponseCache::contains(const std::string& name, | |
| 244 | + const GEnergy& ereco, | |
| 245 | + const GEnergy& etrue, | |
| 246 | + double* value) const | |
| 247 | +{ | |
| 248 | + // Initialise containment flag | |
| 249 | + bool contains = false; | |
| 250 | + | |
| 251 | + // Get element identifier | |
| 252 | + double element = encode(ereco, etrue); | |
| 253 | + | |
| 254 | + // Search for name in cache | |
| 255 | + GResponseCacheName::const_iterator it_name = m_cache.find(name); | |
| 256 | + if (it_name != m_cache.end()) { | |
| 257 | + | |
| 258 | + // Search for element in cache | |
| 259 | + GResponseCacheElement::const_iterator it_element = | |
| 260 | + it_name->second.find(element); | |
| 261 | + if (it_element != it_name->second.end()) { | |
| 262 | + contains = true; | |
| 263 | + if (value != NULL) { | |
| 264 | + *value = it_element->second; | |
| 265 | + } | |
| 266 | + } // endif: element found | |
| 267 | + | |
| 268 | + } // endif: name found | |
| 269 | + | |
| 270 | + // Return containment flag | |
| 271 | + return contains; | |
| 272 | +} | |
| 273 | + | |
| 274 | + | |
| 275 | +/***********************************************************************//** | |
| 276 | + * @brief Check if cache contains a value for specific parameters | |
| 277 | + * | |
| 278 | + * @param[in] name Cache name. | |
| 279 | + * @param[in] dir Instrument direction. | |
| 280 | + * @param[in] ereco Reconstructed energy. | |
| 281 | + * @param[in] etrue True energy. | |
| 282 | + * @param[out] value Pointer to cached value (only if found). | |
| 283 | + * @return True if cached value was found, otherwise false. | |
| 284 | + * | |
| 285 | + * Check if the cache contains a value for a given @p name, instrument | |
| 286 | + * direction @p dir, reconstructed energy @p ereco, and true energy @p etrue. | |
| 287 | + * | |
| 288 | + * If the @p value pointer argument is not NULL, the method will return the | |
| 289 | + * cached value through this argument in case that the value exists. | |
| 290 | + ***************************************************************************/ | |
| 291 | +bool GResponseCache::contains(const std::string& name, | |
| 292 | + const GInstDir& dir, | |
| 293 | + const GEnergy& ereco, | |
| 294 | + const GEnergy& etrue, | |
| 295 | + double* value) const | |
| 296 | +{ | |
| 297 | + // Initialise containment flag | |
| 298 | + bool contains = false; | |
| 299 | + | |
| 300 | + // Get element identifier | |
| 301 | + double element = encode(dir, ereco, etrue); | |
| 302 | + | |
| 303 | + // Search for name in cache | |
| 304 | + GResponseCacheName::const_iterator it_name = m_cache.find(name); | |
| 305 | + if (it_name != m_cache.end()) { | |
| 306 | + | |
| 307 | + // Search for element in cache | |
| 308 | + GResponseCacheElement::const_iterator it_element = | |
| 309 | + it_name->second.find(element); | |
| 310 | + if (it_element != it_name->second.end()) { | |
| 311 | + contains = true; | |
| 312 | + if (value != NULL) { | |
| 313 | + *value = it_element->second; | |
| 314 | + } | |
| 315 | + } // endif: element found | |
| 316 | + | |
| 317 | + } // endif: name found | |
| 318 | + | |
| 319 | + // Return containment flag | |
| 320 | + return contains; | |
| 321 | +} | |
| 322 | + | |
| 323 | + | |
| 324 | +/***********************************************************************//** | |
| 325 | + * @brief Clone response cache | |
| 326 | + * | |
| 327 | + * @return Pointer to deep copy of response cache. | |
| 328 | + ***************************************************************************/ | |
| 329 | +GResponseCache* GResponseCache::clone(void) const | |
| 330 | +{ | |
| 331 | + return new GResponseCache(*this); | |
| 332 | +} | |
| 333 | + | |
| 334 | + | |
| 335 | +/***********************************************************************//** | |
| 336 | + * @brief Print response cache | |
| 337 | + * | |
| 338 | + * @param[in] chatter Chattiness. | |
| 339 | + * @return String containing response cache information. | |
| 340 | + ***************************************************************************/ | |
| 341 | +std::string GResponseCache::print(const GChatter& chatter) const | |
| 342 | +{ | |
| 343 | + // Initialise result string | |
| 344 | + std::string result; | |
| 345 | + | |
| 346 | + // Continue only if chatter is not silent | |
| 347 | + if (chatter != SILENT) { | |
| 348 | + | |
| 349 | + // Append header | |
| 350 | + result.append("=== GResponseCache ==="); | |
| 351 | + | |
| 352 | + // Append total cache size | |
| 353 | + result.append("\n"+gammalib::parformat("Number of cached values")); | |
| 354 | + result.append(gammalib::str(size())); | |
| 355 | + | |
| 356 | + // Append information | |
| 357 | + for (GResponseCacheName::const_iterator it_name = m_cache.begin(); | |
| 358 | + it_name != m_cache.end(); ++it_name) { | |
| 359 | + | |
| 360 | + // Append name | |
| 361 | + result.append("\n"+gammalib::parformat("Name")+it_name->first); | |
| 362 | + | |
| 363 | + // Compute number of elements | |
| 364 | + int nelement = it_name->second.size(); | |
| 365 | + | |
| 366 | + // Append number of elements | |
| 367 | + result.append("\n"+gammalib::parformat("Elements")); | |
| 368 | + result.append(gammalib::str(nelement)); | |
| 369 | + | |
| 370 | + } // endfor: looped over names | |
| 371 | + | |
| 372 | + } // endif: chatter was not silent | |
| 373 | + | |
| 374 | + // Return result | |
| 375 | + return result; | |
| 376 | +} | |
| 377 | + | |
| 378 | + | |
| 379 | +/*========================================================================== | |
| 380 | + = = | |
| 381 | + = Private methods = | |
| 382 | + = = | |
| 383 | + ==========================================================================*/ | |
| 384 | + | |
| 385 | +/***********************************************************************//** | |
| 386 | + * @brief Initialise class members | |
| 387 | + ***************************************************************************/ | |
| 388 | +void GResponseCache::init_members(void) | |
| 389 | +{ | |
| 390 | + // Initialise members | |
| 391 | + m_cache.clear(); | |
| 392 | + | |
| 393 | + // Return | |
| 394 | + return; | |
| 395 | +} | |
| 396 | + | |
| 397 | + | |
| 398 | +/***********************************************************************//** | |
| 399 | + * @brief Copy class members | |
| 400 | + * | |
| 401 | + * @param[in] cache Response cache. | |
| 402 | + ***************************************************************************/ | |
| 403 | +void GResponseCache::copy_members(const GResponseCache& cache) | |
| 404 | +{ | |
| 405 | + // Copy members | |
| 406 | + m_cache = cache.m_cache; | |
| 407 | + | |
| 408 | + // Return | |
| 409 | + return; | |
| 410 | +} | |
| 411 | + | |
| 412 | + | |
| 413 | +/***********************************************************************//** | |
| 414 | + * @brief Delete class members | |
| 415 | + ***************************************************************************/ | |
| 416 | +void GResponseCache::free_members(void) | |
| 417 | +{ | |
| 418 | + // Return | |
| 419 | + return; | |
| 420 | +} | |
| 421 | + | |
| 422 | + | |
| 423 | +/***********************************************************************//** | |
| 424 | + * @brief Encode reconstructued and true energy | |
| 425 | + * | |
| 426 | + * @param[in] ereco Reconstructed energy. | |
| 427 | + * @param[in] etrue True energy. | |
| 428 | + * @return Encoded reconstructued and true energy | |
| 429 | + * | |
| 430 | + * Encodes the reconstructued and true energy in a single double precision | |
| 431 | + * value. The encoding is done using the following formula: | |
| 432 | + * | |
| 433 | + * \f[ | |
| 434 | + * {\tt encode} = E_{\rm reco} + E_{\rm true} \times 10^2 | |
| 435 | + * \f] | |
| 436 | + * | |
| 437 | + * where | |
| 438 | + * - \f$E_{\rm reco}\f$ is the reconstructued energy in TeV, and | |
| 439 | + * - \f$E_{\rm true}\f$ is the true energy in TeV. | |
| 440 | + ***************************************************************************/ | |
| 441 | +double GResponseCache::encode(const GEnergy& ereco, | |
| 442 | + const GEnergy& etrue) const | |
| 443 | +{ | |
| 444 | + // Construct encoded instrument direction and reconstructued energy | |
| 445 | + double encoded = ereco.MeV() + etrue.MeV() * 1.0e2; | |
| 446 | + | |
| 447 | + // Return encoded value | |
| 448 | + return encoded; | |
| 449 | +} | |
| 450 | + | |
| 451 | + | |
| 452 | +/***********************************************************************//** | |
| 453 | + * @brief Encode instrument direction, reconstructued and true energy | |
| 454 | + * | |
| 455 | + * @param[in] dir Instrument direction. | |
| 456 | + * @param[in] ereco Reconstructed energy. | |
| 457 | + * @param[in] etrue True energy. | |
| 458 | + * @return Encoded instrument direction, reconstructued and true energy | |
| 459 | + * | |
| 460 | + * Encodes the instrument direction, reconstructued and true energy in a | |
| 461 | + * single double precision value. The encoding is done using the following | |
| 462 | + * formula: | |
| 463 | + * | |
| 464 | + * \f[ | |
| 465 | + * {\tt encode} = {\tt dir.hash()} + E_{\rm reco} \times 10^6 + | |
| 466 | + E_{\rm true} \times 10^8 | |
| 467 | + * \f] | |
| 468 | + * | |
| 469 | + * where | |
| 470 | + * - \f${\tt dir.encode()}\f$ is an encoding value returned by @p dir, | |
| 471 | + * - \f$\delta\f$ is the Declination of the instrument direction in degrees, | |
| 472 | + * - \f$E_{\rm reco}\f$ is the reconstructued energy in TeV, and | |
| 473 | + * - \f$E_{\rm true}\f$ is the true energy in TeV. | |
| 474 | + * | |
| 475 | + * @todo Use instrument direction encode method | |
| 476 | + ***************************************************************************/ | |
| 477 | +double GResponseCache::encode(const GInstDir& dir, | |
| 478 | + const GEnergy& ereco, | |
| 479 | + const GEnergy& etrue) const | |
| 480 | +{ | |
| 481 | + // Construct encoded instrument direction and reconstructued energy | |
| 482 | + double encoded = dir.hash() + ereco.MeV() * 1.0e6 + etrue.MeV() * 1.0e8; | |
| 483 | + | |
| 484 | + // Return encoded value | |
| 485 | + return encoded; | |
| 486 | +} | ... | ... |
src/obs/Makefile.am
| 1 | 1 | ############################################################################# |
| 2 | 2 | # Makefile.am for obs module # |
| 3 | 3 | # ------------------------------------------------------------------------- # |
| 4 | -# Copyright (C) 2016-2017 Juergen Knoedlseder # | |
| 4 | +# Copyright (C) 2016-2020 Juergen Knoedlseder # | |
| 5 | 5 | # ------------------------------------------------------------------------- # |
| 6 | 6 | # # |
| 7 | 7 | # This program is free software: you can redistribute it and/or modify # |
| ... | ... | @@ -46,6 +46,7 @@ sources = GEnergies.cpp \ |
| 46 | 46 | GEbounds.cpp \ |
| 47 | 47 | GPhases.cpp \ |
| 48 | 48 | GResponse.cpp \ |
| 49 | + GResponseCache.cpp \ | |
| 49 | 50 | GInstDir.cpp \ |
| 50 | 51 | GPhotons.cpp \ |
| 51 | 52 | GPhoton.cpp \ | ... | ... |