From 939f76c5a41fc4b616f795c1142e6ad76c030b41 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Tue, 6 Nov 2018 10:05:36 +0100 Subject: [PATCH] Add GCTAResponseCache class (#2715) --- ChangeLog | 3 ++- NEWS | 4 ++++ dev/codegen.py | 2 +- doc/source/admin/release_history/1.6.rst | 2 ++ inst/cta/Makefile.am | 2 ++ inst/cta/include/GCTALib.hpp | 1 + inst/cta/include/GCTAResponseCache.hpp | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ inst/cta/pyext/GCTAResponseCache.i | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ inst/cta/pyext/cta.i | 2 +- inst/cta/src/GCTAResponseCache.cpp | 405 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ inst/cta/test/test_CTA.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ inst/cta/test/test_CTA.hpp | 1 + 12 files changed, 733 insertions(+), 3 deletions(-) create mode 100644 inst/cta/include/GCTAResponseCache.hpp create mode 100644 inst/cta/pyext/GCTAResponseCache.i create mode 100644 inst/cta/src/GCTAResponseCache.cpp diff --git a/ChangeLog b/ChangeLog index 760a2b3..70914a0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,9 @@ -2018-11-01 +2018-11-06 * Version 1.6.0 released ======================== + Add generic CTA response cache (#2715) Fix precision issue with GCTABackground3D Monte-Carlo simulations (#2708) Revise GCTAEdisp class interface (#2704) GCTAEdisp returns now energy dispersion values per MeV (#2704) diff --git a/NEWS b/NEWS index 7fbcfe4..e5ac27e 100644 --- a/NEWS +++ b/NEWS @@ -14,6 +14,7 @@ made since the last release of GammaLib. The following classes have been added: - GModelSpectralExponential - GWcsARC +- GCTAResponseCache - GCTAModelBackground - GCTAModelSpatial - GCTAModelSpatialMultiplicative @@ -310,6 +311,9 @@ None 20. CTA interface ----------------- +The GCTAResponseCache class was added to implement a general response cache for +CTA (#2715). + GCTABackground3D::init_mc_cache now pre-computes the Monte Carlo cache at energy nodes of the background cube, and in addition, inserts 8 logarithmically spaced nodes between each pair of nodes. This assures that the energy variations of the diff --git a/dev/codegen.py b/dev/codegen.py index bfa0b1e..e16d9ce 100755 --- a/dev/codegen.py +++ b/dev/codegen.py @@ -225,7 +225,7 @@ def set_container_tokens(classname, author, what, object): # Set tokens tokens = [{'pattern': 'GTPLContainer', 'string': classname}, - {'pattern': 'GTPLContainer', 'string': classname.upper()}, + {'pattern': 'GTPLCONTAINER', 'string': classname.upper()}, {'pattern': '[WHAT]', 'string': what}, {'pattern': '[AUTHOR]', 'string': author}, {'pattern': 'TPL_CONTAINER', 'string': object.lower()}, diff --git a/doc/source/admin/release_history/1.6.rst b/doc/source/admin/release_history/1.6.rst index 3d342a6..46542d7 100644 --- a/doc/source/admin/release_history/1.6.rst +++ b/doc/source/admin/release_history/1.6.rst @@ -47,6 +47,8 @@ Bug fixes Improvements ------------ +* [`2715 `_] - + Add generic CTA response cache * [`2704 `_] - ``GCTAEdisp`` returns now energy dispersion values per MeV. The ``GCTAEdisp`` interface was revised. diff --git a/inst/cta/Makefile.am b/inst/cta/Makefile.am index 082e2bb..e83c025 100644 --- a/inst/cta/Makefile.am +++ b/inst/cta/Makefile.am @@ -47,6 +47,7 @@ sources = src/GCTASupport.cpp \ src/GCTAResponseCube.cpp \ src/GCTAResponse_helpers.cpp \ src/GCTAResponseTable.cpp \ + src/GCTAResponseCache.cpp \ src/GCTAAeff.cpp \ src/GCTAAeffPerfTable.cpp \ src/GCTAAeffArf.cpp \ @@ -105,6 +106,7 @@ pkginclude_HEADERS = include/GCTATypemaps.hpp \ include/GCTAResponseIrf.hpp \ include/GCTAResponseCube.hpp \ include/GCTAResponseTable.hpp \ + include/GCTAResponseCache.hpp \ include/GCTAAeff.hpp \ include/GCTAAeffPerfTable.hpp \ include/GCTAAeffArf.hpp \ diff --git a/inst/cta/include/GCTALib.hpp b/inst/cta/include/GCTALib.hpp index 9e34aae..ebe19ca 100644 --- a/inst/cta/include/GCTALib.hpp +++ b/inst/cta/include/GCTALib.hpp @@ -48,6 +48,7 @@ #include "GCTAResponseIrf.hpp" #include "GCTAResponseCube.hpp" #include "GCTAResponseTable.hpp" +#include "GCTAResponseCache.hpp" #include "GCTAAeff.hpp" #include "GCTAAeffPerfTable.hpp" #include "GCTAAeffArf.hpp" diff --git a/inst/cta/include/GCTAResponseCache.hpp b/inst/cta/include/GCTAResponseCache.hpp new file mode 100644 index 0000000..df0b4ca --- /dev/null +++ b/inst/cta/include/GCTAResponseCache.hpp @@ -0,0 +1,118 @@ +/*************************************************************************** + * GCTAResponseCache.hpp - CTA response cache class * + * ----------------------------------------------------------------------- * + * copyright (C) 2018 by Juergen Knoedlseder * + * ----------------------------------------------------------------------- * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + * * + ***************************************************************************/ +/** + * @file GCTAResponseCache.hpp + * @brief CTA response cache class definition + * @author Juergen Knoedlseder + */ + +#ifndef GCTARESPONSECACHE_HPP +#define GCTARESPONSECACHE_HPP + +/* __ Includes ___________________________________________________________ */ +#include +#include +#include "GBase.hpp" +#include "GEnergy.hpp" + +/* __ Forward declarations _______________________________________________ */ + +/* __ Constants __________________________________________________________ */ + +/* __ Typedefs ___________________________________________________________ */ +typedef std::map GCTAResponseCacheEtrue; +typedef std::map GCTAResponseCacheEreco; +typedef std::map GCTAResponseCacheName; + + +/***********************************************************************//** + * @class GCTAResponseCache + * + * @brief CTA response cache class + * + * @todo Add class description. + ***************************************************************************/ +class GCTAResponseCache : public GBase { + +public: + // Constructors and destructors + GCTAResponseCache(void); + GCTAResponseCache(const GCTAResponseCache& cache); + virtual ~GCTAResponseCache(void); + + // Operators + GCTAResponseCache& operator=(const GCTAResponseCache& cache); + + // Methods + void clear(void); + GCTAResponseCache* clone(void) const; + std::string classname(void) const; + int size(void) const; + bool is_empty(void) const; + int nerecos(void) const; + int netrues(void) const; + void set(const std::string& name, + const GEnergy& ereco, + const GEnergy& etrue, + const double& value); + bool contains(const std::string& name, + const GEnergy& ereco, + const GEnergy& etrue, + double* value = NULL) const; + std::string print(const GChatter& chatter = NORMAL) const; + +protected: + // Protected methods + void init_members(void); + void copy_members(const GCTAResponseCache& cache); + void free_members(void); + + // Protected members + GCTAResponseCacheName m_cache; +}; + + +/***********************************************************************//** + * @brief Return class name + * + * @return String containing the class name ("GCTAResponseCache"). + ***************************************************************************/ +inline +std::string GCTAResponseCache::classname(void) const +{ + return ("GCTAResponseCache"); +} + + +/***********************************************************************//** + * @brief Checks whether the cache is empty + * + * @return True if cache is empty, false otherwise. + * + * Checks whether the response cache is empty. + ***************************************************************************/ +inline +bool GCTAResponseCache::is_empty(void) const +{ + return (m_cache.empty()); +} + +#endif /* GCTARESPONSECACHE_HPP */ diff --git a/inst/cta/pyext/GCTAResponseCache.i b/inst/cta/pyext/GCTAResponseCache.i new file mode 100644 index 0000000..97daf57 --- /dev/null +++ b/inst/cta/pyext/GCTAResponseCache.i @@ -0,0 +1,83 @@ +/*************************************************************************** + * GCTAResponseCache.i - CTA response cache class * + * ----------------------------------------------------------------------- * + * copyright (C) 2018 by Juergen Knoedlseder * + * ----------------------------------------------------------------------- * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + * * + ***************************************************************************/ +/** + * @file GCTAResponseCache.i + * @brief CTA response cache class definition + * @author Juergen Knoedlseder + */ +%{ +/* Put headers and other declarations here that are needed for compilation */ +#include "GCTAResponseCache.hpp" +%} + +/* __ Typemaps ___________________________________________________________ */ +/* + * Typemap so that the contains() method returns two values, first the + * flag indicating whether the value exists or not, and second the + * value, i.e. + * + * flag, value = cache.contains('Crab', GEnergy(1.0,'TeV'), GEnergy(1.0,'TeV')) + * + */ +%include "typemaps.i" +%apply double *OUTPUT { double* value }; + + +/***********************************************************************//** + * @class GCTAResponseCache + * + * @brief CTA response cache class + ***************************************************************************/ +class GCTAResponseCache : public GBase { + +public: + // Constructors and destructors + GCTAResponseCache(void); + GCTAResponseCache(const GCTAResponseCache& cache); + virtual ~GCTAResponseCache(void); + + // Methods + void clear(void); + GCTAResponseCache* clone(void) const; + std::string classname(void) const; + int size(void) const; + bool is_empty(void) const; + int nerecos(void) const; + int netrues(void) const; + void set(const std::string& name, + const GEnergy& ereco, + const GEnergy& etrue, + const double& value); + bool contains(const std::string& name, + const GEnergy& ereco, + const GEnergy& etrue, + double* value) const; +}; + + +/***********************************************************************//** + * @brief GCTAResponseCache class extension + ***************************************************************************/ +%extend GCTAResponseCache { + GCTAResponseCache copy() { + return (*self); + } +}; diff --git a/inst/cta/pyext/cta.i b/inst/cta/pyext/cta.i index 4553e42..2945860 100644 --- a/inst/cta/pyext/cta.i +++ b/inst/cta/pyext/cta.i @@ -281,6 +281,7 @@ %include "GCTAResponseIrf.i" %include "GCTAResponseCube.i" %include "GCTAResponseTable.i" +%include "GCTAResponseCache.i" %include "GCTAAeff.i" %include "GCTAAeffPerfTable.i" %include "GCTAAeffArf.i" @@ -316,4 +317,3 @@ %include "GCTAModelRadialPolynom.i" %include "GCTAModelRadialProfile.i" %include "GCTAModelRadialAcceptance.i" - diff --git a/inst/cta/src/GCTAResponseCache.cpp b/inst/cta/src/GCTAResponseCache.cpp new file mode 100644 index 0000000..3fd81f5 --- /dev/null +++ b/inst/cta/src/GCTAResponseCache.cpp @@ -0,0 +1,405 @@ +/*************************************************************************** + * GCTAResponseCache.cpp - CTA response cache class * + * ----------------------------------------------------------------------- * + * copyright (C) 2018 by Juergen Knoedlseder * + * ----------------------------------------------------------------------- * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + * * + ***************************************************************************/ +/** + * @file GCTAResponseCache.cpp + * @brief CTA response cache class implementation + * @author Juergen Knoedlseder + */ + +/* __ Includes ___________________________________________________________ */ +#ifdef HAVE_CONFIG_H +#include +#endif +#include "GTools.hpp" +#include "GCTAResponseCache.hpp" + +/* __ Method name definitions ____________________________________________ */ + +/* __ Macros _____________________________________________________________ */ + +/* __ Coding definitions _________________________________________________ */ + +/* __ Debug definitions __________________________________________________ */ + + + +/*========================================================================== + = = + = Constructors/destructors = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Void constructor + ***************************************************************************/ +GCTAResponseCache::GCTAResponseCache(void) +{ + // Initialise class members + init_members(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Copy constructor + * + * @param[in] cache CTA response cache. + ***************************************************************************/ +GCTAResponseCache::GCTAResponseCache(const GCTAResponseCache& cache) +{ + // Initialise class members + init_members(); + + // Copy members + copy_members(cache); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Destructor + ***************************************************************************/ +GCTAResponseCache::~GCTAResponseCache(void) +{ + // Free members + free_members(); + + // Return + return; +} + + +/*========================================================================== + = = + = Operators = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Assignment operator + * + * @param[in] cache CTA response cache. + * @return CTA response cache. + ***************************************************************************/ +GCTAResponseCache& GCTAResponseCache::operator=(const GCTAResponseCache& cache) +{ + // Execute only if object is not identical + if (this != &cache) { + + // Free members + free_members(); + + // Initialise private members + init_members(); + + // Copy members + copy_members(cache); + + } // endif: object was not identical + + // Return this object + return *this; +} + + +/*========================================================================== + = = + = Public methods = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Clear CTA response cache + ***************************************************************************/ +void GCTAResponseCache::clear(void) +{ + // Free members + free_members(); + + // Initialise private members + init_members(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Return number of elements in cache + * + * @return Number of elements in cache + * + * Returns the number of elements in the response cache. + ***************************************************************************/ +int GCTAResponseCache::size(void) const +{ + // Initialize size + int size = 0; + + // Compute size + for (GCTAResponseCacheName::const_iterator it_name = m_cache.begin(); + it_name != m_cache.end(); ++it_name) { + for (GCTAResponseCacheEreco::const_iterator it_ereco = it_name->second.begin(); + it_ereco != it_name->second.end(); ++it_ereco) { + size += it_ereco->second.size(); + } + } + + // Return size + return size; +} + + +/***********************************************************************//** + * @brief Return number of reconstructed energies in cache + * + * @return Number of reconstructed energies in cache + * + * Returns the number of reconstructed energies in the response cache. + ***************************************************************************/ +int GCTAResponseCache::nerecos(void) const +{ + // Initialize number of reconstructed energies + int nerecos = 0; + + // Compute number of elements + for (GCTAResponseCacheName::const_iterator it_name = m_cache.begin(); + it_name != m_cache.end(); ++it_name) { + for (GCTAResponseCacheEreco::const_iterator it_ereco = it_name->second.begin(); + it_ereco != it_name->second.end(); ++it_ereco) { + nerecos++; + } + } + + // Return number of reconstructed energies + return nerecos; +} + + +/***********************************************************************//** + * @brief Return number of true energies in cache + * + * @return Number of true energies in cache + * + * Returns the number of true energies in the response cache. + ***************************************************************************/ +int GCTAResponseCache::netrues(void) const +{ + // Initialize number of true energies + int netrues = 0; + + // Compute number of elements + for (GCTAResponseCacheName::const_iterator it_name = m_cache.begin(); + it_name != m_cache.end(); ++it_name) { + for (GCTAResponseCacheEreco::const_iterator it_ereco = it_name->second.begin(); + it_ereco != it_name->second.end(); ++it_ereco) { + netrues += it_ereco->second.size(); + } + } + + // Return number of true energies + return netrues; +} + + +/***********************************************************************//** + * @brief Set cache value + * + * @param[in] name Cache name. + * @param[in] ereco Reconstructed energy. + * @param[in] etrue True energy. + * @param[in] value Cache value. + * + * Set cache value for a given @p name, reconstructed energy @p ereco, and + * true energy @p etrue. + ***************************************************************************/ +void GCTAResponseCache::set(const std::string& name, + const GEnergy& ereco, + const GEnergy& etrue, + const double& value) +{ + // Set cache value + m_cache[name][ereco][etrue] = value; + + // Return + return; +} + + +/***********************************************************************//** + * @brief Check if cache contains a value for specific parameters + * + * @param[in] name Cache name. + * @param[in] ereco Reconstructed energy. + * @param[in] etrue True energy. + * @param[out] value Pointer to cached value (only if found). + * @return True if cached value was found, otherwise false. + * + * Check if the cache contains a value for a specific set of parameters, and + * optionally returns that value through the @p value pointer argument. If + * @p value is set to NULL, no cached value is returned. + ***************************************************************************/ +bool GCTAResponseCache::contains(const std::string& name, + const GEnergy& ereco, + const GEnergy& etrue, + double* value) const +{ + // Initialise containment flag + bool contains = false; + + // Search for name in cache + GCTAResponseCacheName::const_iterator it_name = m_cache.find(name); + if (it_name != m_cache.end()) { + + // Search for reconstructed energy in cache + GCTAResponseCacheEreco::const_iterator it_ereco; + it_ereco = it_name->second.find(ereco); + if (it_ereco != it_name->second.end()) { + + // Search for true energy in cache + GCTAResponseCacheEtrue::const_iterator it_etrue = it_ereco->second.find(etrue); + if (it_etrue != it_ereco->second.end()) { + contains = true; + if (value != NULL) { + *value = it_etrue->second; + } + } // endif: true energy found + + } // endif: reconstructed energy found + + } // endif: name found + + // Return containment flag + return contains; +} + + +/***********************************************************************//** + * @brief Clone CTA response cache + * + * @return Pointer to deep copy of CTA response cache. + ***************************************************************************/ +GCTAResponseCache* GCTAResponseCache::clone(void) const +{ + return new GCTAResponseCache(*this); +} + + +/***********************************************************************//** + * @brief Print CTA response cache + * + * @param[in] chatter Chattiness. + * @return String containing CTA response cache information. + * + * @todo Implement method. + ***************************************************************************/ +std::string GCTAResponseCache::print(const GChatter& chatter) const +{ + // Initialise result string + std::string result; + + // Continue only if chatter is not silent + if (chatter != SILENT) { + + // Append header + result.append("=== GCTAResponseCache ==="); + + // Append total cache size + result.append("\n"+gammalib::parformat("Number of cached values")); + result.append(gammalib::str(m_cache.size())); + + // Append information + for (GCTAResponseCacheName::const_iterator it_name = m_cache.begin(); + it_name != m_cache.end(); ++it_name) { + + // Append name + result.append("\n"+gammalib::parformat("Name")+it_name->first); + + // Compute number of reconstructed and true energies + int nereco = 0; + int netrue = 0; + for (GCTAResponseCacheEreco::const_iterator it_ereco = it_name->second.begin(); + it_ereco != it_name->second.end(); ++it_ereco) { + nereco++; + netrue += it_ereco->second.size(); + } + + // Append number of reconstructed and true energies + result.append("\n"+gammalib::parformat("Reconstructed energies")); + result.append(gammalib::str(nereco)); + result.append("\n"+gammalib::parformat("True energies")); + result.append(gammalib::str(netrue)); + + } // endfor: looped over names + + } // endif: chatter was not silent + + // Return result + return result; +} + + +/*========================================================================== + = = + = Private methods = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Initialise class members + ***************************************************************************/ +void GCTAResponseCache::init_members(void) +{ + // Initialise members + m_cache.clear(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Copy class members + * + * @param[in] cache CTA response cache. + ***************************************************************************/ +void GCTAResponseCache::copy_members(const GCTAResponseCache& cache) +{ + // Copy members + m_cache = cache.m_cache; + + // Return + return; +} + + +/***********************************************************************//** + * @brief Delete class members + ***************************************************************************/ +void GCTAResponseCache::free_members(void) +{ + // Return + return; +} diff --git a/inst/cta/test/test_CTA.cpp b/inst/cta/test/test_CTA.cpp index c929327..8a9c1aa 100644 --- a/inst/cta/test/test_CTA.cpp +++ b/inst/cta/test/test_CTA.cpp @@ -1409,6 +1409,119 @@ void TestGCTAResponse::test_response_edispcube(void) /***********************************************************************//** + * @brief Test response cache handling + ***************************************************************************/ +void TestGCTAResponse::test_response_cache(void) +{ + // Set some energies + const GEnergy eng1(1.0, "TeV"); + const GEnergy eng2(2.0, "TeV"); + const GEnergy eng3(3.0, "TeV"); + + // Initialise results + double value = 0.0; + bool flag = false; + + // Test empty response cache + GCTAResponseCache cache1; + test_assert(cache1.is_empty(), "Test is_empty() method for empty cache"); + test_value(cache1.size(), 0, "Test size() method for empty cache"); + test_value(cache1.nerecos(), 0, "Test nerecos() method for empty cache"); + test_value(cache1.netrues(), 0, "Test netrues() method for empty cache"); + flag = cache1.contains("Crab", eng1, eng1, &value); + test_assert(!flag, "Test contains() method flag for empty cache"); + + // Test filled response cube (one model, two ereco=etrue) + GCTAResponseCache cache2; + cache2.set("Crab", eng1, eng1, 1.0); + cache2.set("Crab", eng2, eng2, 2.0); + test_assert(!cache2.is_empty(), "Test is_empty() method for filled cache"); + test_value(cache2.size(), 2, "Test size() method for filled cache"); + test_value(cache2.nerecos(), 2, "Test nerecos() method for filled cache"); + test_value(cache2.netrues(), 2, "Test netrues() method for filled cache"); + flag = cache2.contains("Crab", eng1, eng1, &value); + test_assert(flag, "Test contains() method flag for filled cache"); + test_value(value, 1.0, "Test contains() method value for filled cache"); + flag = cache2.contains("Crab", eng2, eng2, &value); + test_assert(flag, "Test contains() method flag for filled cache"); + test_value(value, 2.0, "Test contains() method value for filled cache"); + flag = cache2.contains("Crab", eng2, eng1, &value); + test_assert(!flag, "Test contains() method flag for non-existing true energy"); + flag = cache2.contains("Crab", eng1, eng2, &value); + test_assert(!flag, "Test contains() method flag for non-existing reco. energy"); + flag = cache2.contains("Vela", eng2, eng2, &value); + test_assert(!flag, "Test contains() method flag for non-existing name"); + + // Test clearing of cache + cache2.clear(); + test_assert(cache2.is_empty(), "Test is_empty() method for cleared cache"); + test_value(cache2.size(), 0, "Test size() method for cleared cache"); + test_value(cache2.nerecos(), 0, "Test nerecos() method for cleared cache"); + test_value(cache2.netrues(), 0, "Test netrues() method for cleared cache"); + flag = cache2.contains("Crab", eng1, eng1, &value); + test_assert(!flag, "Test contains() method flag for cleared cache"); + + // Test filled response cube (two models, two ereco, two etrue per ereco) + GCTAResponseCache cache3; + cache3.set("Crab", eng1, eng1, 1.0); + cache3.set("Crab", eng1, eng2, 2.0); + cache3.set("Crab", eng2, eng1, 3.0); + cache3.set("Crab", eng2, eng2, 4.0); + cache3.set("Vela", eng1, eng1, 10.0); + cache3.set("Vela", eng1, eng2, 11.0); + cache3.set("Vela", eng2, eng1, 12.0); + cache3.set("Vela", eng2, eng2, 13.0); + test_assert(!cache3.is_empty(), "Test is_empty() method for filled cache"); + test_value(cache3.size(), 8, "Test size() method for filled cache"); + test_value(cache3.nerecos(), 4, "Test nerecos() method for filled cache"); + test_value(cache3.netrues(), 8, "Test netrues() method for filled cache"); + + // Test Crab cache + flag = cache3.contains("Crab", eng1, eng1, &value); + test_assert(flag, "Test contains() method flag for filled cache"); + test_value(value, 1.0, "Test contains() method value for filled cache"); + flag = cache3.contains("Crab", eng1, eng2, &value); + test_assert(flag, "Test contains() method flag for filled cache"); + test_value(value, 2.0, "Test contains() method value for filled cache"); + flag = cache3.contains("Crab", eng2, eng1, &value); + test_assert(flag, "Test contains() method flag for filled cache"); + test_value(value, 3.0, "Test contains() method value for filled cache"); + flag = cache3.contains("Crab", eng2, eng2, &value); + test_assert(flag, "Test contains() method flag for filled cache"); + test_value(value, 4.0, "Test contains() method value for filled cache"); + flag = cache3.contains("Crab", eng3, eng1, &value); + test_assert(!flag, "Test contains() method flag for non-existing true energy"); + flag = cache3.contains("Crab", eng1, eng3, &value); + test_assert(!flag, "Test contains() method flag for non-existing reco. energy"); + + // Test Vela cache + flag = cache3.contains("Vela", eng1, eng1, &value); + test_assert(flag, "Test contains() method flag for filled cache"); + test_value(value, 10.0, "Test contains() method value for filled cache"); + flag = cache3.contains("Vela", eng1, eng2, &value); + test_assert(flag, "Test contains() method flag for filled cache"); + test_value(value, 11.0, "Test contains() method value for filled cache"); + flag = cache3.contains("Vela", eng2, eng1, &value); + test_assert(flag, "Test contains() method flag for filled cache"); + test_value(value, 12.0, "Test contains() method value for filled cache"); + flag = cache3.contains("Vela", eng2, eng2, &value); + test_assert(flag, "Test contains() method flag for filled cache"); + test_value(value, 13.0, "Test contains() method value for filled cache"); + flag = cache3.contains("Vela", eng3, eng1, &value); + test_assert(!flag, "Test contains() method flag for non-existing true energy"); + flag = cache3.contains("Vela", eng1, eng3, &value); + test_assert(!flag, "Test contains() method flag for non-existing reco. energy"); + + // Test non-existing name + flag = cache3.contains("Orion", eng1, eng1, &value); + test_assert(!flag, "Test contains() method flag for non-existing name"); + + // Return + return; +} + + +/***********************************************************************//** * @brief Utility function for energy dispersion tests * * @param[in] rsp Response. diff --git a/inst/cta/test/test_CTA.hpp b/inst/cta/test/test_CTA.hpp index 4845ab1..46caff6 100644 --- a/inst/cta/test/test_CTA.hpp +++ b/inst/cta/test/test_CTA.hpp @@ -90,6 +90,7 @@ public: void test_response_psfcube(void); void test_response_bkgcube(void); void test_response_edispcube(void); + void test_response_cache(void); // Utility methods void test_response_edisp_integration(const GCTAResponseIrf& rsp, -- libgit2 0.21.4