ATLAS Offline Software
Loading...
Searching...
No Matches
VarHolder.h
Go to the documentation of this file.
1// This is -*- c++ -*-
2
3/*
4 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5*/
6
7#ifndef PROMPT_VARHOLDER_H
8#define PROMPT_VARHOLDER_H
9
10/**********************************************************************************
11 * @Package: LeptonTaggers
12 * @Class : VarHolder
13 * @Author : Rustem Ospanov
14 * @Author : Rhys Roberts
15 *
16 * @Brief :
17 *
18 * VarHolder is a generic analysis object that holds variables as (int, double)
19 *
20 **********************************************************************************/
21
22// C/C++
23#include <algorithm>
24#include <iostream>
25#include <string>
26#include <vector>
27
28// Athena
30
31namespace Prompt
32{
33 //======================================================================================================
77
78 //======================================================================================================
80 {
81 public:
82
83 VarEntry();
84 VarEntry(unsigned key, double value);
86
87 unsigned getKey () const { return m_fKey; }
88 double getVar () const { return m_fData; }
89 double getData() const { return m_fData; }
90
91 private:
92
93 uint32_t m_fKey; // variable key
94 double m_fData; // variable value
95 };
96
97 typedef std::vector<Prompt::VarEntry> VarEntryVec;
98 typedef std::map<Def::Var, std::string> VarMap;
99
100 //======================================================================================================
102 {
103 public:
104 VarHolder();
105 virtual ~VarHolder() {}
106
107 static std::string getObjectType() { return "VarHolder"; }
108
109 bool replaceVar(unsigned key, double value);
110 bool addVar (unsigned key, double value);
111 bool delVar (unsigned key);
112
113 double getVar (const unsigned key) const;
114 bool getVar (const unsigned key, double &value) const;
115 bool getVar (const unsigned key, float &value) const;
116
117 bool hasKey(unsigned key) const;
118 bool hasVar(unsigned key) const;
119
120 int registerAllVars();
121 bool registerVar(Prompt::Def::Var var, const std::string &name);
122 Prompt::Def::Var registerDynamicVar(const std::string &name);
123
124 std::string convert2Str(const Prompt::Def::Var var) const;
125 Prompt::Def::Var convert2Var (const std::string &var);
126 Prompt::Def::Var convert2Var(const uint32_t key);
127
128 void getAllVarEnums();
129
130 std::string asStr(const uint32_t key, const double val);
131 std::string asStr(const Prompt::Def::Var var);
132
133 std::vector<Prompt::Def::Var> readVars(const std::string &config);
134 std::vector<Prompt::Def::Var> readVectorVars(
135 const std::vector<std::string> &keys
136 );
137
138 virtual void clearVars();
139
140 private:
141
144
145 std::vector<Prompt::Def::Var> m_varEnums;
146 };
147
148 //======================================================================================================
149 // VarEntry inline functions and comparison operators
150 //
152 {
153 }
154
155 inline VarEntry::VarEntry(unsigned int key, double data)
156 :m_fKey(key), m_fData(data)
157 {
158 }
159
160 inline bool operator==(const VarEntry &lhs, const VarEntry &rhs)
161 {
162 return lhs.getKey() == rhs.getKey();
163 }
164 inline bool operator<(const VarEntry &lhs, const VarEntry &rhs)
165 {
166 return lhs.getKey() < rhs.getKey();
167 }
168
169 inline bool operator==(const VarEntry &var, unsigned key) { return var.getKey() == key; }
170 inline bool operator==(unsigned key, const VarEntry &var) { return var.getKey() == key; }
171
172 inline bool operator<(const VarEntry &var, unsigned key) { return var.getKey() < key; }
173 inline bool operator<(unsigned key, const VarEntry &var) { return key < var.getKey(); }
174
175 //======================================================================================================
176 // VarHolder inline functions
177 //
178 inline bool VarHolder::replaceVar(const unsigned key, const double value)
179 {
180 if(!hasKey(key)) {
181 m_fVars.push_back(VarEntry(key, value));
182 return true;
183 }
184 else{
185 delVar(key);
186 addVar(key, value);
187 }
188 return false;
189 }
190
200 inline bool VarHolder::addVar(const unsigned key, const double value)
201 {
202 using namespace asg::msgUserCode;
203
204 if(!hasKey(key)) {
205 m_fVars.push_back(VarEntry(key, value));
206 return true;
207 }
208
209 ANA_MSG_DEBUG(getObjectType() << "::addVar(" << key << ", " << value << ") - key already exists");
210 return false;
211 }
212
213 inline bool VarHolder::delVar(const unsigned key)
214 {
215 VarEntryVec::iterator vit = m_fVars.begin();
216 while(vit != m_fVars.end()) {
217 if(vit->getKey() == key) {
218 vit = m_fVars.erase(vit);
219 }
220 else {
221 ++vit;
222 }
223 }
224
225 return false;
226 }
227
228 inline bool VarHolder::hasKey(unsigned key) const
229 {
230 return std::find(m_fVars.begin(), m_fVars.end(), key) != m_fVars.end();
231 }
232 inline bool VarHolder::hasVar(unsigned key) const
233 {
234 return std::find(m_fVars.begin(), m_fVars.end(), key) != m_fVars.end();
235 }
236
237 inline bool VarHolder::getVar(const unsigned key, float &value) const
238 {
239 //
240 // Read variable
241 //
242 const VarEntryVec::const_iterator ivar = std::find(m_fVars.begin(), m_fVars.end(), key);
243 if(ivar != m_fVars.end()) {
244 value = ivar->getData();
245 return true;
246 }
247
248 return false;
249 }
250
251 inline bool VarHolder::getVar(const unsigned key, double &value) const
252 {
253 //
254 // Read variable
255 //
256 const VarEntryVec::const_iterator ivar = std::find(m_fVars.begin(), m_fVars.end(), key);
257 if(ivar != m_fVars.end()) {
258 value = ivar->getData();
259 return true;
260 }
261
262 return false;
263 }
264
265 inline double VarHolder::getVar(const unsigned key) const
266 {
267 //
268 // Find and return, if exists, value stored at key
269 //
270 double val = -1.0e9;
271 getVar(key, val);
272 return val;
273 }
274
276 {
277 m_fVars.clear();
278 }
279}
280
281#endif // PROMPT_VARHOLDER_H
macros for messaging and checking status codes
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
uint32_t m_fKey
Definition VarHolder.h:93
double getData() const
Definition VarHolder.h:89
VarEntry(unsigned key, double value)
double getVar() const
Definition VarHolder.h:88
unsigned getKey() const
Definition VarHolder.h:87
VarEntryVec m_fVars
Definition VarHolder.h:143
std::vector< Prompt::Def::Var > readVectorVars(const std::vector< std::string > &keys)
virtual void clearVars()
Definition VarHolder.h:275
std::string convert2Str(const Prompt::Def::Var var) const
Definition VarHolder.cxx:77
std::vector< Prompt::Def::Var > m_varEnums
Definition VarHolder.h:145
std::vector< Prompt::Def::Var > readVars(const std::string &config)
Prompt::Def::Var registerDynamicVar(const std::string &name)
Definition VarHolder.cxx:41
double getVar(const unsigned key) const
Definition VarHolder.h:265
static std::string getObjectType()
Definition VarHolder.h:107
Prompt::Def::Var convert2Var(const std::string &var)
Definition VarHolder.cxx:90
std::string asStr(const uint32_t key, const double val)
virtual ~VarHolder()
Definition VarHolder.h:105
bool registerVar(Prompt::Def::Var var, const std::string &name)
Definition VarHolder.cxx:21
bool hasKey(unsigned key) const
Definition VarHolder.h:228
bool delVar(unsigned key)
Definition VarHolder.h:213
bool hasVar(unsigned key) const
Definition VarHolder.h:232
bool addVar(unsigned key, double value)
Adds a variable to the VarHolder.
Definition VarHolder.h:200
bool replaceVar(unsigned key, double value)
Definition VarHolder.h:178
@ NumberOfPixelHoles
Definition VarHolder.h:53
@ NumberOfPIXHits
Definition VarHolder.h:48
@ NumberOfSCTHits
Definition VarHolder.h:49
@ PromptLeptonRNN_prompt
Definition VarHolder.h:67
@ Topoetcone30rel
Definition VarHolder.h:60
@ NumberOfSharedSiHits
Definition VarHolder.h:51
@ TrackPtOverTrackJetPt
Definition VarHolder.h:55
@ CandVertex_normDistToPriVtxLongitudinalBest_ThetaCutVtx
Definition VarHolder.h:69
@ CandVertex_NPassVtx
Definition VarHolder.h:70
@ Ptvarcone30_TightTTVA_pt500rel
Definition VarHolder.h:62
@ CaloClusterSumEtRel
Definition VarHolder.h:66
@ CaloClusterERel
Definition VarHolder.h:65
@ NumberOfSiHoles
Definition VarHolder.h:52
@ CandVertex_normDistToPriVtxLongitudinalBest
Definition VarHolder.h:68
void StringTok(std::vector< std::string > &ls, const std::string &str, const std::string &tok)
bool operator<(const VarEntry &lhs, const VarEntry &rhs)
Definition VarHolder.h:164
std::vector< Prompt::VarEntry > VarEntryVec
Definition VarHolder.h:97
std::map< Def::Var, std::string > VarMap
Definition VarHolder.h:98
bool operator==(const VarEntry &lhs, const VarEntry &rhs)
Definition VarHolder.h:160