ATLAS Offline Software
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 
31 namespace Prompt
32 {
33  //======================================================================================================
34  namespace Def
35  {
36  enum Var
37  {
38  NONE = 0,
40  DRlj,
41  PtFrac,
42  PtRel,
43 
44  // track VarHolder
45  LepTrackDR,
46  Pt,
47  AbsEta,
54  TrackJetDR,
57  D0Sig,
58 
59  // PromptLeptonImproved
63  MVAXBin,
64  RawPt,
71  };
72 
73  void StringTok(std::vector<std::string>& ls,
74  const std::string& str,
75  const std::string& tok);
76  }
77 
78  //======================================================================================================
79  class VarEntry
80  {
81  public:
82 
84  VarEntry(unsigned key, double value);
85  ~VarEntry() {}
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  //======================================================================================================
101  class VarHolder
102  {
103  public:
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);
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  //
151  inline VarEntry::VarEntry() :m_fKey(0), m_fData(0.)
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 
275  inline void VarHolder::clearVars()
276  {
277  m_fVars.clear();
278  }
279 }
280 
281 #endif // PROMPT_VARHOLDER_H
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
Prompt::VarHolder::registerAllVars
int registerAllVars()
Definition: VarHolder.cxx:228
Prompt::Def::RawPt
@ RawPt
Definition: VarHolder.h:94
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
Prompt::Def::LepTrackDR
@ LepTrackDR
Definition: VarHolder.h:75
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
Prompt
Definition: DecoratePLIT.h:28
Prompt::Def::NumberOfSCTHits
@ NumberOfSCTHits
Definition: VarHolder.h:79
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
Prompt::Def::NumberOfSiHits
@ NumberOfSiHits
Definition: VarHolder.h:80
Prompt::Def::TrackJetNTrack
@ TrackJetNTrack
Definition: VarHolder.h:69
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
Prompt::VarHolder::addVar
bool addVar(unsigned key, double value)
Adds a variable to the VarHolder.
Definition: VarHolder.h:210
Prompt::VarHolder::~VarHolder
virtual ~VarHolder()
Definition: VarHolder.h:115
Prompt::VarHolder::registerDynamicVar
Prompt::Def::Var registerDynamicVar(const std::string &name)
Definition: VarHolder.cxx:41
Prompt::VarHolder
Definition: VarHolder.h:112
Prompt::VarEntryVec
std::vector< Prompt::VarEntry > VarEntryVec
Definition: VarHolder.h:107
Prompt::VarHolder::VarHolder
VarHolder()
Definition: VarHolder.cxx:16
athena.value
value
Definition: athena.py:124
Prompt::VarEntry
Definition: VarHolder.h:90
Prompt::Def::DRlj
@ DRlj
Definition: VarHolder.h:70
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
Prompt::Def::Z0Sin
@ Z0Sin
Definition: VarHolder.h:86
Prompt::Def::NumberOfPIXHits
@ NumberOfPIXHits
Definition: VarHolder.h:78
Prompt::Def::PtFrac
@ PtFrac
Definition: VarHolder.h:71
Prompt::operator<
bool operator<(const VarEntry &lhs, const VarEntry &rhs)
Definition: VarHolder.h:174
Prompt::Def::NONE
@ NONE
Definition: VarHolder.h:68
Prompt::Def::PromptLeptonRNN_prompt
@ PromptLeptonRNN_prompt
Definition: VarHolder.h:97
Prompt::VarHolder::getVar
double getVar(const unsigned key) const
Definition: VarHolder.h:275
Prompt::VarHolder::convert2Str
std::string convert2Str(const Prompt::Def::Var var) const
Definition: VarHolder.cxx:77
Prompt::Def::D0Sig
@ D0Sig
Definition: VarHolder.h:87
Prompt::Def::Ptvarcone30rel
@ Ptvarcone30rel
Definition: VarHolder.h:91
Prompt::Def::MVAXBin
@ MVAXBin
Definition: VarHolder.h:93
Prompt::VarHolder::getObjectType
static std::string getObjectType()
Definition: VarHolder.h:117
Prompt::Def::NumberOfSiHoles
@ NumberOfSiHoles
Definition: VarHolder.h:82
Prompt::VarHolder::getAllVarEnums
void getAllVarEnums()
Definition: VarHolder.cxx:67
MessageCheck.h
macros for messaging and checking status codes
Prompt::VarHolder::hasVar
bool hasVar(unsigned key) const
Definition: VarHolder.h:242
plotBeamSpotCompare.ivar
int ivar
Definition: plotBeamSpotCompare.py:383
Prompt::Def::StringTok
void StringTok(std::vector< std::string > &ls, const std::string &str, const std::string &tok)
Definition: VarHolder.cxx:195
Prompt::VarHolder::readVectorVars
std::vector< Prompt::Def::Var > readVectorVars(const std::vector< std::string > &keys)
Definition: VarHolder.cxx:149
Prompt::VarEntry::getVar
double getVar() const
Definition: VarHolder.h:98
Prompt::Def::CandVertex_NPassVtx
@ CandVertex_NPassVtx
Definition: VarHolder.h:100
Prompt::VarHolder::m_fVars
VarEntryVec m_fVars
Definition: VarHolder.h:153
Prompt::VarHolder::m_varEnums
std::vector< Prompt::Def::Var > m_varEnums
Definition: VarHolder.h:155
Prompt::Def::CaloClusterSumEtRel
@ CaloClusterSumEtRel
Definition: VarHolder.h:96
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
Prompt::VarEntry::getData
double getData() const
Definition: VarHolder.h:99
Prompt::VarEntry::~VarEntry
~VarEntry()
Definition: VarHolder.h:95
Prompt::VarHolder::m_gPromptVars
VarMap m_gPromptVars
Definition: VarHolder.h:152
Prompt::VarHolder::clearVars
virtual void clearVars()
Definition: VarHolder.h:285
Prompt::VarHolder::delVar
bool delVar(unsigned key)
Definition: VarHolder.h:223
Prompt::Def::NumberOfSharedSiHits
@ NumberOfSharedSiHits
Definition: VarHolder.h:81
Prompt::Def::Pt
@ Pt
Definition: VarHolder.h:76
Prompt::VarHolder::registerVar
bool registerVar(Prompt::Def::Var var, const std::string &name)
Definition: VarHolder.cxx:21
Prompt::VarEntry::getKey
unsigned getKey() const
Definition: VarHolder.h:97
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
Prompt::Def::TrackPtOverTrackJetPt
@ TrackPtOverTrackJetPt
Definition: VarHolder.h:85
Prompt::Def::TrackJetDR
@ TrackJetDR
Definition: VarHolder.h:84
Prompt::Def::Ptvarcone30_TightTTVA_pt500rel
@ Ptvarcone30_TightTTVA_pt500rel
Definition: VarHolder.h:92
Prompt::Def::NumberOfPixelHoles
@ NumberOfPixelHoles
Definition: VarHolder.h:83
Prompt::operator==
bool operator==(const VarEntry &lhs, const VarEntry &rhs)
Definition: VarHolder.h:170
Prompt::VarMap
std::map< Def::Var, std::string > VarMap
Definition: VarHolder.h:108
Prompt::VarHolder::hasKey
bool hasKey(unsigned key) const
Definition: VarHolder.h:238
Prompt::Def::CandVertex_normDistToPriVtxLongitudinalBest
@ CandVertex_normDistToPriVtxLongitudinalBest
Definition: VarHolder.h:98
Prompt::Def::PtRel
@ PtRel
Definition: VarHolder.h:72
Prompt::VarEntry::VarEntry
VarEntry()
Definition: VarHolder.h:161
str
Definition: BTagTrackIpAccessor.cxx:11
Prompt::VarHolder::replaceVar
bool replaceVar(unsigned key, double value)
Definition: VarHolder.h:188
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
Prompt::Def::Var
Var
Definition: VarHolder.h:57
Prompt::VarEntry::m_fData
double m_fData
Definition: VarHolder.h:104
getReferenceRun.ls
def ls(fpath)
Definition: getReferenceRun.py:29
Prompt::Def::AbsEta
@ AbsEta
Definition: VarHolder.h:77
Prompt::VarEntry::m_fKey
uint32_t m_fKey
Definition: VarHolder.h:103
Prompt::Def::CandVertex_normDistToPriVtxLongitudinalBest_ThetaCutVtx
@ CandVertex_normDistToPriVtxLongitudinalBest_ThetaCutVtx
Definition: VarHolder.h:99
Prompt::Def::Topoetcone30rel
@ Topoetcone30rel
Definition: VarHolder.h:90
Prompt::Def::CaloClusterERel
@ CaloClusterERel
Definition: VarHolder.h:95
Prompt::VarHolder::convert2Var
Prompt::Def::Var convert2Var(const std::string &var)
Definition: VarHolder.cxx:90
Prompt::VarHolder::readVars
std::vector< Prompt::Def::Var > readVars(const std::string &config)
Definition: VarHolder.cxx:123
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
Prompt::VarHolder::asStr
std::string asStr(const uint32_t key, const double val)
Definition: VarHolder.cxx:178
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288