From 0eff4ea0b6bbdea9fef045edbe10b496c117aede Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Fri, 10 Apr 2020 23:45:57 +0200 Subject: [PATCH] Rename GResponseCache::energy() method to GResponseCache::hash() --- include/GResponseCache.hpp | 2 +- include/GResponseCacheKey.hpp | 218 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- include/GammaLib.hpp | 1 - include/Makefile.am | 1 - pyext/GResponseCacheKey.i | 80 -------------------------------------------------------------------------------- pyext/gammalib/obs.i | 1 - src/obs/GResponseCache.cpp | 32 +++++++++++--------------------- src/obs/GResponseCacheKey.cpp | 407 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 8 files changed, 12 insertions(+), 730 deletions(-) delete mode 100644 include/GResponseCacheKey.hpp delete mode 100644 pyext/GResponseCacheKey.i delete mode 100644 src/obs/GResponseCacheKey.cpp diff --git a/include/GResponseCache.hpp b/include/GResponseCache.hpp index 7dc12e4..2970ce5 100644 --- a/include/GResponseCache.hpp +++ b/include/GResponseCache.hpp @@ -98,7 +98,7 @@ protected: void init_members(void); void copy_members(const GResponseCache& cache); void free_members(void); - uint64_t energy(const GEnergy& ereco, const GEnergy& etrue) const; + uint64_t hash(const GEnergy& ereco, const GEnergy& etrue) const; // Protected members GResponseCacheName m_cache; //. * - * * - ***************************************************************************/ -/** - * @file GResponseCacheKey.hpp - * @brief Response cache key class definition - * @author Juergen Knoedlseder - */ - -#ifndef GRESPONSECACHEKEY_HPP -#define GRESPONSECACHEKEY_HPP - -/* __ Includes ___________________________________________________________ */ -#include -#include -#include "GContainer.hpp" - -/* __ Forward declarations _______________________________________________ */ - -/* __ Constants __________________________________________________________ */ - -/* __ Typedefs ___________________________________________________________ */ - - -/***********************************************************************//** - * @class GResponseCacheKey - * - * @brief Response cache key class - * - * The class implements the key for the Instrument Response Function cache. - * The key aggregates all relevant attributes to describe a cached value, - * such as model name, true and measured event energies and instrument - * direction. - ***************************************************************************/ -class GResponseCacheKey : public GContainer { - -public: - // Constructors and destructors - GResponseCacheKey(void); - explicit GResponseCacheKey(const std::string& name); - GResponseCacheKey(const GResponseCacheKey& key); - virtual ~GResponseCacheKey(void); - - // Operators - GResponseCacheKey& operator=(const GResponseCacheKey& key); - float& operator[](const int& index); - const float& operator[](const int& index) const; - bool operator<(const GResponseCacheKey& key) const; - - // Methods - void clear(void); - GResponseCacheKey* clone(void) const; - std::string classname(void) const; - bool is_empty(void) const; - int size(void) const; - void set(const int& index, const float& value); - void append(const float& value); - void insert(const int& index, const float& value); - void remove(const int& index); - void reserve(const int& num); - void name(const std::string& name); - const std::string& name(void) const; - std::string print(const GChatter& chatter = NORMAL) const; - -protected: - // Protected methods - void init_members(void); - void copy_members(const GResponseCacheKey& key); - void free_members(void); - - // Protected members - std::string m_name; //!< Key name (e.g. model name, observation) - std::vector m_values; //!< Key values -}; - - -/***********************************************************************//** - * @brief Return reference to key value - * - * @param[in] index Key index. - * - * Returns a reference to a key value. - ***************************************************************************/ -float& GResponseCacheKey::operator[](const int& index) -{ - return m_values[index]; -} - - -/***********************************************************************//** - * @brief Return reference to key value (const version) - * - * @param[in] index Key index. - * - * Returns a reference to a key value. - ***************************************************************************/ -const float& GResponseCacheKey::operator[](const int& index) const -{ - return m_values[index]; -} - - -/***********************************************************************//** - * @brief Return class name - * - * @return String containing the class name ("GResponseCacheKey"). - ***************************************************************************/ -inline -std::string GResponseCacheKey::classname(void) const -{ - return ("GResponseCacheKey"); -} - - -/***********************************************************************//** - * @brief Checks whether the key has no values - * - * @return True if key has no values, false otherwise. - * - * Checks whether the key has no values. - ***************************************************************************/ -inline -bool GResponseCacheKey::is_empty(void) const -{ - return (m_values.empty()); -} - - -/***********************************************************************//** - * @brief Return number of key values - * - * @return Number of key values. - * - * Returns the number of key values. - ***************************************************************************/ -inline -int GResponseCacheKey::size(void) const -{ - return (m_values.size()); -} - - -/***********************************************************************//** - * @brief Append key value - * - * @param[in] value Key value. - * - * Append @p value to key values. - ***************************************************************************/ -inline -void GResponseCacheKey::append(const float& value) -{ - m_values.push_back(value); - return; -} - - -/***********************************************************************//** - * @brief Reserves space for key values - * - * @param[in] num Number of key values - * - * Reserves space for @p num key values. - ***************************************************************************/ -inline -void GResponseCacheKey::reserve(const int& num) -{ - m_values.reserve(num); - return; -} - - -/***********************************************************************//** - * @brief Set key name - * - * @param[in] name Key name - * - * Sets the key name. - ***************************************************************************/ -inline -void GResponseCacheKey::name(const std::string& name) -{ - m_name = name; - return; -} - - -/***********************************************************************//** - * @brief Return key name - * - * @returns Key name - * - * Returns the key name. - ***************************************************************************/ -inline -const std::string& GResponseCacheKey::name(void) const -{ - return m_name; -} - -#endif /* GRESPONSECACHEKEY_HPP */ diff --git a/include/GammaLib.hpp b/include/GammaLib.hpp index effcd6f..11c0343 100644 --- a/include/GammaLib.hpp +++ b/include/GammaLib.hpp @@ -189,7 +189,6 @@ #include "GInstDir.hpp" #include "GResponse.hpp" #include "GResponseCache.hpp" -#include "GResponseCacheKey.hpp" #include "GPhotons.hpp" #include "GPhoton.hpp" #include "GSource.hpp" diff --git a/include/Makefile.am b/include/Makefile.am index aef6bbf..6a848bc 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -149,7 +149,6 @@ pkginclude_HEADERS = GammaLib.hpp \ GInstDir.hpp \ GResponse.hpp \ GResponseCache.hpp \ - GResponseCacheKey.hpp \ GPhotons.hpp \ GPhoton.hpp \ GSource.hpp \ diff --git a/pyext/GResponseCacheKey.i b/pyext/GResponseCacheKey.i deleted file mode 100644 index d2c8afa..0000000 --- a/pyext/GResponseCacheKey.i +++ /dev/null @@ -1,80 +0,0 @@ -/*************************************************************************** - * GResponseCacheKey.i - Response cache key class * - * ----------------------------------------------------------------------- * - * copyright (C) 2020 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 GResponseCacheKey.i - * @brief Response cache key class definition - * @author Juergen Knoedlseder - */ -%{ -/* Put headers and other declarations here that are needed for compilation */ -#include "GResponseCacheKey.hpp" -%} - - -/***********************************************************************//** - * @class GResponseCacheKey - * - * @brief Response cache key class - * - * The class implements the key for the Instrument Response Function cache. - * The key aggregates all relevant attributes to describe a cached value, - * such as model name, true and measured event energies and instrument - * direction. - ***************************************************************************/ -class GResponseCacheKey : public GContainer { - -public: - // Constructors and destructors - GResponseCacheKey(void); - explicit GResponseCacheKey(const std::string& name); - GResponseCacheKey(const GResponseCacheKey& key); - virtual ~GResponseCacheKey(void); - - // Operators - GResponseCacheKey& operator=(const GResponseCacheKey& key); - float& operator[](const int& index); - const float& operator[](const int& index) const; - bool operator<(const GResponseCacheKey& key) const; - - // Methods - void clear(void); - GResponseCacheKey* clone(void) const; - std::string classname(void) const; - bool is_empty(void) const; - int size(void) const; - void set(const int& index, const float& value); - void append(const float& value); - void insert(const int& index, float& value); - void remove(const int& index); - void reserve(const int& num); - void name(const std::string& name); - const std::string& name(void) const; -}; - - -/***********************************************************************//** - * @brief GResponseCacheKey class extension - ***************************************************************************/ -%extend GResponseCacheKey { - GResponseCacheKey copy() { - return (*self); - } -}; diff --git a/pyext/gammalib/obs.i b/pyext/gammalib/obs.i index 0650113..daf57b9 100644 --- a/pyext/gammalib/obs.i +++ b/pyext/gammalib/obs.i @@ -197,7 +197,6 @@ %include "GPhases.i" %include "GResponse.i" %include "GResponseCache.i" -%include "GResponseCacheKey.i" %include "GPhoton.i" %include "GPhotons.i" %include "GSource.i" diff --git a/src/obs/GResponseCache.cpp b/src/obs/GResponseCache.cpp index 492cfec..895a905 100644 --- a/src/obs/GResponseCache.cpp +++ b/src/obs/GResponseCache.cpp @@ -224,7 +224,7 @@ void GResponseCache::set(const std::string& name, const double& value) { // Get energy identifier - uint64_t energy = this->energy(ereco, etrue); + uint64_t energy = hash(ereco, etrue); // Set cache value m_cache[name][energy][0] = value; @@ -253,7 +253,7 @@ void GResponseCache::set(const std::string& name, const double& value) { // Get energy and direction identifier - uint64_t energy = this->energy(ereco, etrue); + uint64_t energy = hash(ereco, etrue); uint64_t instdir = dir.hash(); // Set cache value @@ -288,7 +288,7 @@ bool GResponseCache::contains(const std::string& name, bool contains = false; // Get energy identifier - uint64_t energy = this->energy(ereco, etrue); + uint64_t energy = hash(ereco, etrue); uint64_t instdir = 0; // Search for name in cache @@ -345,7 +345,7 @@ bool GResponseCache::contains(const std::string& name, bool contains = false; // Get energy identifier - uint64_t energy = this->energy(ereco, etrue); + uint64_t energy = hash(ereco, etrue); uint64_t instdir = dir.hash(); // Search for name in cache @@ -489,30 +489,20 @@ void GResponseCache::free_members(void) /***********************************************************************//** - * @brief Encode reconstructued and true energy + * @brief Encode reconstructued and true energy into hash value * * @param[in] ereco Reconstructed energy. * @param[in] etrue True energy. * @return Encoded reconstructued and true energy * * Encodes the reconstructued and true energy in a 64 Bit unsigned integer - * value. The energy are converted into floating - - single double precision - * value. The encoding is done using the following formula: - * - * \f[ - * {\tt encode} = E_{\rm reco} + E_{\rm true} \times 10^2 - * \f] - * - * where - * - \f$E_{\rm reco}\f$ is the reconstructued energy in TeV, and - * - \f$E_{\rm true}\f$ is the true energy in TeV. - * - * @todo Verify unique encoding for all instruments!!! + * value. The energy are converted into single precision floating point + * values and mapped on a 64 Bit unsigned integer value. This provides unique + * hash values for all instruments up to the precision of floating point + * singles. ***************************************************************************/ -uint64_t GResponseCache::energy(const GEnergy& ereco, - const GEnergy& etrue) const +uint64_t GResponseCache::hash(const GEnergy& ereco, + const GEnergy& etrue) const { // Allocate static array to store the two energies as floats static float buffer[2]; diff --git a/src/obs/GResponseCacheKey.cpp b/src/obs/GResponseCacheKey.cpp deleted file mode 100644 index 5fb54c3..0000000 --- a/src/obs/GResponseCacheKey.cpp +++ /dev/null @@ -1,407 +0,0 @@ -/*************************************************************************** - * GResponseCacheKey.cpp - Response cache key class * - * ----------------------------------------------------------------------- * - * copyright (C) 2020 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 GResponseCacheKey.cpp - * @brief Response cache key class implementation - * @author Juergen Knoedlseder - */ - -/* __ Includes ___________________________________________________________ */ -#ifdef HAVE_CONFIG_H -#include -#endif -#include "GException.hpp" -#include "GResponseCacheKey.hpp" - -/* __ Method name definitions ____________________________________________ */ -#define G_SET "GResponseCacheKey::set(int&, float&)" -#define G_INSERT "GResponseCacheKey::insert(int&, float&)" -#define G_REMOVE "GResponseCacheKey::remove(int&)" - -/* __ Macros _____________________________________________________________ */ - -/* __ Coding definitions _________________________________________________ */ - -/* __ Debug definitions __________________________________________________ */ - - -/*========================================================================== - = = - = Constructors/destructors = - = = - ==========================================================================*/ - -/***********************************************************************//** - * @brief Void constructor - ***************************************************************************/ -GResponseCacheKey::GResponseCacheKey(void) -{ - // Initialise class members - init_members(); - - // Return - return; -} - - -/***********************************************************************//** - * @brief Key name constructor - * - * @param[in] name Key name. - ***************************************************************************/ -GResponseCacheKey::GResponseCacheKey(const std::string& name) -{ - // Initialise class members - init_members(); - - // Set name - this->name(name); - - // Return - return; -} - - -/***********************************************************************//** - * @brief Copy constructor - * - * @param[in] key Response cache key. - ***************************************************************************/ -GResponseCacheKey::GResponseCacheKey(const GResponseCacheKey& key) -{ - // Initialise class members - init_members(); - - // Copy members - copy_members(key); - - // Return - return; -} - - -/***********************************************************************//** - * @brief Destructor - ***************************************************************************/ -GResponseCacheKey::~GResponseCacheKey(void) -{ - // Free members - free_members(); - - // Return - return; -} - - -/*========================================================================== - = = - = Operators = - = = - ==========================================================================*/ - -/***********************************************************************//** - * @brief Assignment operator - * - * @param[in] key Response cache key. - * @return Response cache key. - ***************************************************************************/ -GResponseCacheKey& GResponseCacheKey::operator=(const GResponseCacheKey& key) -{ - // Execute only if object is not identical - if (this != &key) { - - // Free members - free_members(); - - // Initialise private members - init_members(); - - // Copy members - copy_members(key); - - } // endif: object was not identical - - // Return this object - return *this; -} - - -/***********************************************************************//** - * @brief Compare operator - * - * @param[in] key Response cache key. - * @return True if instance is smaller than @p key. - ***************************************************************************/ -bool GResponseCacheKey::operator<(const GResponseCacheKey& key) const -{ - // Get key size - int sz = size(); - - // Compare key sizes - bool smaller = sz < key.size(); - - // If sizes are identical then continue witl comparison - if (sz == key.size()) { - - // Compare key name - smaller = m_name < key.m_name; - - // If names are identical then compare values. For small sizes we - // explicitly unroll the loop to make sure that the code is fast. - if (m_name == key.m_name) { - if (sz == 1) { - smaller = m_values[0] < key.m_values[0]; - } - else if (sz == 2) { - smaller = (m_values[0] < key.m_values[0]) || - (m_values[1] < key.m_values[1]); - } - else if (sz == 3) { - smaller = (m_values[0] < key.m_values[0]) || - (m_values[1] < key.m_values[1]) || - (m_values[2] < key.m_values[2]); - } - else if (sz == 4) { - smaller = (m_values[0] < key.m_values[0]) || - (m_values[1] < key.m_values[1]) || - (m_values[2] < key.m_values[2]) || - (m_values[3] < key.m_values[3]); - } - else if (sz == 5) { - smaller = (m_values[0] < key.m_values[0]) || - (m_values[1] < key.m_values[1]) || - (m_values[2] < key.m_values[2]) || - (m_values[3] < key.m_values[3]) || - (m_values[4] < key.m_values[4]); - } - else { - for (int i = 0; i < size(); ++i) { - smaller = m_values[i] < key.m_values[i]; - if (smaller) { - break; - } - } - } - } // endif: names were identical - - } // endif: sizes were identical - - // Return smaller flag - return smaller; -} - - -/*========================================================================== - = = - = Public methods = - = = - ==========================================================================*/ - -/***********************************************************************//** - * @brief Clear response cache - ***************************************************************************/ -void GResponseCacheKey::clear(void) -{ - // Free members - free_members(); - - // Initialise private members - init_members(); - - // Return - return; -} - - -/***********************************************************************//** - * @brief Clone instance - * - * @return Pointer to deep copy of response cache key - * - * Makes a deep copy of the response cache key. - ***************************************************************************/ -GResponseCacheKey* GResponseCacheKey::clone(void) const -{ - return new GResponseCacheKey(*this); -} - - -/***********************************************************************//** - * @brief Set key value - * - * @param[in] index Key value index [0,...,size()-1]. - * @param[in] value Key value. - * - * @exception GException::out_of_range - * Invalid key value index. - * - * Set key value in the container. - ***************************************************************************/ -void GResponseCacheKey::set(const int& index, const float& value) -{ - // Compile option: raise exception if index is out of range - #if defined(G_RANGE_CHECK) - if (index < 0 || index >= size()) { - throw GException::out_of_range(G_SET, "Key value index", index, size()); - } - #endif - - // Assign key value - m_values[index] = value; - - // Return - return; -} - - -/***********************************************************************//** - * @brief Insert key value - * - * @param[in] index Key value index [0,...,size()-1]. - * @param[in] value Key value. - * - * @exception GException::out_of_range - * Invalid key value index. - * - * Inserts a key value before the value with the specified @p index. - ***************************************************************************/ -void GResponseCacheKey::insert(const int& index, const float& value) -{ - // Compile option: raise exception if index is out of range - #if defined(G_RANGE_CHECK) - if (index < 0 || index >= size()) { - throw GException::out_of_range(G_INSERT, "Key value index", index, size()); - } - #endif - - // Inserts key value - m_values.insert(m_values.begin()+index, value); - - // Return - return; -} - - -/***********************************************************************//** - * @brief Remove key value - * - * @param[in] index Key value index [0,...,size()-1]. - * - * @exception GException::out_of_range - * Invalid key value index. - * - * Removes key value with the specified @p index. - ***************************************************************************/ -void GResponseCacheKey::remove(const int& index) -{ - // Compile option: raise exception if index is out of range - #if defined(G_RANGE_CHECK) - if (index < 0 || index >= size()) { - throw GException::out_of_range(G_REMOVE, "Key value index", index, size()); - } - #endif - - // Erase key value - m_values.erase(m_values.begin()+index); - - // Return - return; -} - - -/***********************************************************************//** - * @brief Print response cache key - * - * @param[in] chatter Chattiness. - * @return String containing response cache key information. - ***************************************************************************/ -std::string GResponseCacheKey::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("=== GResponseCacheKey ==="); - - // Append total cache size - result.append("\n"+gammalib::parformat("Key name")); - result.append(m_name); - result.append("\n"+gammalib::parformat("Number of key values")); - result.append(gammalib::str(size())); - for (int i = 0; i < size(); ++i) { - result.append("\n"+gammalib::parformat("Key value "+gammalib::str(i+1))); - result.append(gammalib::str(m_values[i])); - } - - } // endif: chatter was not silent - - // Return result - return result; -} - - -/*========================================================================== - = = - = Private methods = - = = - ==========================================================================*/ - -/***********************************************************************//** - * @brief Initialise class members - ***************************************************************************/ -void GResponseCacheKey::init_members(void) -{ - // Initialise members - m_name.clear(); - m_values.clear(); - - // Return - return; -} - - -/***********************************************************************//** - * @brief Copy class members - * - * @param[in] key Response cache key. - ***************************************************************************/ -void GResponseCacheKey::copy_members(const GResponseCacheKey& key) -{ - // Copy members - m_name = key.m_name; - m_values = key.m_values; - - // Return - return; -} - - -/***********************************************************************//** - * @brief Delete class members - ***************************************************************************/ -void GResponseCacheKey::free_members(void) -{ - // Return - return; -} -- libgit2 0.21.4