ATLAS Offline Software
VarHolder.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 // C/C++
7 #include <iomanip>
8 #include <iostream>
9 #include <map>
10 #include <sstream>
11 #include <mutex>
12 
13 // Local
15 
18 }
19 
20 //======================================================================================================
22 {
23  for(const VarMap::value_type &v: m_gPromptVars) {
24  if(name == v.second) {
25  asg::msgUserCode::ANA_MSG_WARNING("Def::registerVar - WARNING - variable with this name already exists: var=" << var << ", name=" << name);
26  return false;
27  }
28  }
29 
30  std::pair<VarMap::iterator, bool> vit = m_gPromptVars.insert(VarMap::value_type(var, name));
31 
32  if(!vit.second) {
33  asg::msgUserCode::ANA_MSG_WARNING("Def::registerVar - WARNING - variable with this key already exists: var=" << var << ", name=" << name);
34  return false;
35  }
36 
37  return true;
38 }
39 
40 //======================================================================================================
42 {
43  unsigned last_key = Prompt::Def::NONE;
44 
45  for(const Prompt::VarMap::value_type &v: m_gPromptVars) {
46  if(name == v.second) {
47  asg::msgUserCode::ANA_MSG_WARNING("Def::registerDynamicVar - variable with this name already exists: " << name);
48 
49  return v.first;
50  }
51 
52  last_key = std::max<unsigned>(last_key, v.first);
53  }
54 
55  const Def::Var new_key = static_cast<Def::Var>(last_key+1);
56 
57 
58  if(!registerVar(new_key, name)) {
59  asg::msgUserCode::ANA_MSG_WARNING("Def::registerDynamicVar - WARNING - failed to register variable name=" << name);
60  return Prompt::Def::NONE;
61  }
62 
63  return new_key;
64 }
65 
66 //======================================================================================================
68 {
69  if(m_varEnums.empty()) {
70  for(const Prompt::VarMap::value_type &v: m_gPromptVars) {
71  m_varEnums.push_back(v.first);
72  }
73  }
74 }
75 
76 //======================================================================================================
78 {
79  const VarMap::const_iterator vit = m_gPromptVars.find(var);
80 
81  if(vit == m_gPromptVars.end()) {
82  asg::msgUserCode::ANA_MSG_WARNING("Def::convert2Str - WARNING - unknown variable: " << var);
83  return "UNKNOWN";
84  }
85 
86  return vit->second;
87 }
88 
89 //======================================================================================================
91 {
92  for (const VarMap::value_type &v: m_gPromptVars) {
93  if(var == v.second) {
94  return v.first;
95  }
96  }
97 
98  asg::msgUserCode::ANA_MSG_WARNING("Def::convert2Var - WARNING - unknown variable: " << var);
99 
100  return Prompt::Def::NONE;
101 }
102 
103 //======================================================================================================
105 {
106  getAllVarEnums();
107 
108  //
109  // Find matching enum by value
110  //
111  for(Prompt::Def::Var var: m_varEnums) {
112  if(static_cast<uint32_t>(var) == key) {
113  return var;
114  }
115  }
116 
117  asg::msgUserCode::ANA_MSG_WARNING("Def::convert2Var - WARNING - unknown key: " << key);
118 
119  return Prompt::Def::NONE;
120 }
121 
122 //======================================================================================================
123 std::vector<Prompt::Def::Var> Prompt::VarHolder::readVars(
124  const std::string &config
125 )
126 {
127  //
128  // Read vector of variable names and convert to Var enums
129  //
130  std::vector<std::string> keys;
131  Def::StringTok(keys, config, ", ");
132 
133  std::vector<Prompt::Def::Var> vars;
134 
135  for(const std::string &key: keys) {
136  const Prompt::Def::Var var = convert2Var(key);
137  if(var != Prompt::Def::NONE) {
138  vars.push_back(var);
139  }
140  else {
141  asg::msgUserCode::ANA_MSG_WARNING("Prompt::Def::readVars - unknown variable name: " << key);
142  }
143  }
144 
145  return vars;
146 }
147 
148 //======================================================================================================
149 std::vector<Prompt::Def::Var> Prompt::VarHolder::readVectorVars(
150  const std::vector<std::string> &keys
151 )
152 {
153  //
154  // Read vector of variable names and convert to Var enums
155  //
156  std::vector<Prompt::Def::Var> vars;
157 
158  for(const std::string &key: keys) {
159  const Prompt::Def::Var var = convert2Var(key);
160  if(var != Prompt::Def::NONE) {
161  vars.push_back(var);
162  }
163  else {
164  asg::msgUserCode::ANA_MSG_WARNING("Prompt::Def::readVars - unknown variable name: " << key);
165  }
166  }
167 
168  return vars;
169 }
170 
171 //======================================================================================================
173 {
174  return convert2Str(var);
175 }
176 
177 //======================================================================================================
178 std::string Prompt::VarHolder::asStr(const uint32_t key, const double val)
179 {
180  std::stringstream s;
181 
182  const Prompt::Def::Var var = convert2Var(key);
183 
184  if(var != Prompt::Def::NONE) {
185  s << asStr(var) << ": " << val;
186  }
187  else {
188  s << var << ": " << val;
189  }
190 
191  return s.str();
192 }
193 
194 //======================================================================================================
195 void Prompt::Def::StringTok(std::vector<std::string>& ls,
196  const std::string& str,
197  const std::string& tok)
198 {
199  //======================================================================
200  // Split a long string into a set of shorter strings spliting along
201  // divisions makers by the characters listed in the token string
202  //======================================================================
203  const std::string::size_type S = str.size();
204  std::string::size_type i = 0;
205 
206  while (i < S) {
207  // eat leading whitespace
208  while (i < S && tok.find(str[i]) != std::string::npos) {
209  ++i;
210  }
211  if (i == S) break; // nothing left but WS
212 
213  // find end of word
214  std::string::size_type j = i+1;
215  while (j < S && tok.find(str[j]) == std::string::npos) {
216  ++j;
217  }
218 
219  // add word
220  ls.push_back(str.substr(i,j-i));
221 
222  // set up for next loop
223  i = j+1;
224  }
225 }
226 
227 //======================================================================================================
229 {
230  using namespace Prompt::Def;
231 
232  int result = 0;
233 
234  result += registerVar( NONE, "NONE");
235  result += registerVar( TrackJetNTrack, "TrackJetNTrack");
236  result += registerVar( DRlj, "DRlj");
237  result += registerVar( PtFrac, "PtFrac");
238  result += registerVar( PtRel, "PtRel");
239 
240  // track VarHolder
241  result += registerVar( LepTrackDR, "LepTrackDR");
242  result += registerVar( Pt, "Pt");
243  result += registerVar( AbsEta, "AbsEta");
244  result += registerVar( NumberOfPIXHits, "NumberOfPIXHits");
245  result += registerVar( NumberOfSCTHits, "NumberOfSCTHits");
246  result += registerVar( NumberOfSiHits, "NumberOfSiHits");
247  result += registerVar( NumberOfSharedSiHits, "NumberOfSharedSiHits");
248  result += registerVar( NumberOfSiHoles, "NumberOfSiHoles");
249  result += registerVar( NumberOfPixelHoles, "NumberOfPixelHoles");
250  result += registerVar( TrackJetDR, "TrackJetDR");
251  result += registerVar( TrackPtOverTrackJetPt, "TrackPtOverTrackJetPt");
252  result += registerVar( Z0Sin, "Z0Sin");
253  result += registerVar( D0Sig, "D0Sig");
254 
255  // PromptLeptonImproved
256  result += registerVar( MVAXBin, "MVAXBin");
257  result += registerVar( RawPt, "RawPt");
258  result += registerVar( PromptLeptonRNN_prompt, "PromptLeptonRNN_prompt");
259  result += registerVar( CaloClusterERel, "CaloClusterERel");
260  result += registerVar( Topoetcone30rel, "topoetcone30rel");
261  result += registerVar( Ptvarcone30rel, "ptvarcone30rel");
262  result += registerVar( Ptvarcone30_TightTTVA_pt500rel, "ptvarcone30_TightTTVA_pt500rel");
263  result += registerVar( CaloClusterSumEtRel, "CaloClusterSumEtRel");
264  result += registerVar( CandVertex_normDistToPriVtxLongitudinalBest, "CandVertex_normDistToPriVtxLongitudinalBest");
265  result += registerVar( CandVertex_normDistToPriVtxLongitudinalBest_ThetaCutVtx, "CandVertex_normDistToPriVtxLongitudinalBest_ThetaCutVtx");
266  result += registerVar( CandVertex_NPassVtx, "CandVertex_NPassVtx");
267 
268  return result;
269 }
270 
Prompt::VarHolder::registerAllVars
int registerAllVars()
Definition: VarHolder.cxx:228
Prompt::Def::RawPt
@ RawPt
Definition: VarHolder.h:94
Prompt::Def::LepTrackDR
@ LepTrackDR
Definition: VarHolder.h:75
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
Prompt::Def
Definition: VarHolder.h:45
Prompt::Def::NumberOfSCTHits
@ NumberOfSCTHits
Definition: VarHolder.h:79
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
get_generator_info.result
result
Definition: get_generator_info.py:21
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::registerDynamicVar
Prompt::Def::Var registerDynamicVar(const std::string &name)
Definition: VarHolder.cxx:41
Prompt::VarHolder::VarHolder
VarHolder()
Definition: VarHolder.cxx:16
JetTiledMap::S
@ S
Definition: TiledEtaPhiMap.h:44
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::Def::NONE
@ NONE
Definition: VarHolder.h:68
Prompt::Def::PromptLeptonRNN_prompt
@ PromptLeptonRNN_prompt
Definition: VarHolder.h:97
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
lumiFormat.i
int i
Definition: lumiFormat.py:85
Prompt::Def::NumberOfSiHoles
@ NumberOfSiHoles
Definition: VarHolder.h:82
Prompt::VarHolder::getAllVarEnums
void getAllVarEnums()
Definition: VarHolder.cxx:67
ANA_MSG_WARNING
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:292
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::Def::CandVertex_NPassVtx
@ CandVertex_NPassVtx
Definition: VarHolder.h:100
Prompt::Def::CaloClusterSumEtRel
@ CaloClusterSumEtRel
Definition: VarHolder.h:96
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
VarHolder.h
python.PyAthena.v
v
Definition: PyAthena.py:154
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
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::Def::CandVertex_normDistToPriVtxLongitudinalBest
@ CandVertex_normDistToPriVtxLongitudinalBest
Definition: VarHolder.h:98
Prompt::Def::PtRel
@ PtRel
Definition: VarHolder.h:72
str
Definition: BTagTrackIpAccessor.cxx:11
python.DiskUtils.ls
def ls(path, longls=False)
Definition: DiskUtils.py:112
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
Prompt::Def::Var
Var
Definition: VarHolder.h:57
Prompt::Def::AbsEta
@ AbsEta
Definition: VarHolder.h:77
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