From 7f9599641cf9a16a953127a25ea26a092eb794b4 Mon Sep 17 00:00:00 2001 From: "s.zharko@gsi.de" <s.zharko@gsi.de> Date: Tue, 15 Feb 2022 14:14:11 +0100 Subject: [PATCH] New init for L1Algo: moved L1Utils.h to L1Algo directory --- reco/L1/CMakeLists.txt | 2 +- reco/L1/L1Algo/L1InitManager.h | 2 +- reco/L1/L1Algo/L1Utils.h | 73 ++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 reco/L1/L1Algo/L1Utils.h diff --git a/reco/L1/CMakeLists.txt b/reco/L1/CMakeLists.txt index f56f35c3b..291f5f7ce 100644 --- a/reco/L1/CMakeLists.txt +++ b/reco/L1/CMakeLists.txt @@ -276,7 +276,7 @@ Install(FILES CbmL1Counters.h L1Algo/L1InitManager.h L1Algo/L1CAIteration.h L1Algo/L1Parameters.h - L1Algo/utils/L1Utils.h + L1Algo/L1Utils.h vectors/vec_arithmetic.h vectors/std_alloc.h DESTINATION include diff --git a/reco/L1/L1Algo/L1InitManager.h b/reco/L1/L1Algo/L1InitManager.h index fd3e51d7a..4038e725a 100644 --- a/reco/L1/L1Algo/L1InitManager.h +++ b/reco/L1/L1Algo/L1InitManager.h @@ -14,7 +14,7 @@ #include "L1BaseStationInfo.h" #include "L1Field.h" #include "L1Parameters.h" -#include "utils/L1Utils.h" +#include "L1Utils.h" //#include <string> #include <bitset> diff --git a/reco/L1/L1Algo/L1Utils.h b/reco/L1/L1Algo/L1Utils.h new file mode 100644 index 000000000..97fff9bee --- /dev/null +++ b/reco/L1/L1Algo/L1Utils.h @@ -0,0 +1,73 @@ +/* Copyright (C) 2016-2022 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt + SPDX-License-Identifier: GPL-3.0-only + Authors: Sergey Gorbunov, Sergei Zharko [committer] */ + +/************************************************************************************************************ + * @file L1Utils.h + * @brief File contains some general purpose functions for L1Algo + * @since 12.01.2022 + ***********************************************************************************************************/ + +#include <iomanip> +#include <map> +#include <sstream> +#include <string> +#include <unordered_map> + +/// Class contains some utility functions for L1Algo +struct L1Utils { + + /// Hash for unordered_map with enum class keys + struct EnumClassHash { + template<typename T> + int operator()(T t) const + { + return static_cast<int>(t); + } + }; + + /// Template function, which sets a value to an element of the map with a particular key + /// \param key Key of the element to be modified + /// \param value New value of the element under the selected key + /// \param aMap A reference to the map, which element is to be modified + template<class Key, class T, class Hash = std::hash<Key>> + static void SetSingleValueToMap(Key key, T value, std::unordered_map<Key, T, Hash>& aMap) + { + aMap[key] = value; + } + + /// Template function, which sets a value to ALL elements of the map + /// \param value New value of the element under the selected key + /// \param aMap A reference to the map, which element is to be modified + template<class Key, class T, class Hash = std::hash<Key>> + static void SetSameValueToMap(T value, std::unordered_map<Key, T, Hash>& aMap) + { + for (auto it = aMap.begin(); it != aMap.end(); ++it) { + it->second = value; + } + } + + /// Template function, which resets the elements of one map with the values defined in another map + /// \param inMap A constant reference to the map containing new parameters + /// \param aMap A reference to the map, which is going to be modified + template<class Key, class T, class Hash = std::hash<Key>> + static void SetMappedValuesToMap(const std::unordered_map<Key, T, Hash>& inMap, + std::unordered_map<Key, T, Hash>& aMap) + { + for (auto it = aMap.begin(); it != aMap.end(); ++it) { + if (inMap.find(it->first) != inMap.end()) { it->second = inMap.at(it->first); } + } + } + + /// Template function to represent mapped contents into std::string + /// NOTE: operator<< must be defined for value of the map + template<class Key, class T, class Hash = std::hash<Key>> + static std::string RepresentMapWithString(const std::unordered_map<Key, T, Hash>& aMap, int entryWidth = 15) + { + std::stringstream token; + for (auto it = aMap.begin(); it != aMap.end(); ++it) { + token << std::setw(entryWidth) << std::setfill(' ') << it->second << ' '; + } + return token.str(); + } +}; -- GitLab