ATLAS Offline Software
ConditionsSingleton.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include "TMap.h"
7 #include "TObjString.h"
8 #include <iostream>
9 #include <map>
10 #include <sstream>
11 #include <iomanip>
12 #include <set>
13 #include "boost/tokenizer.hpp"
14 #include <boost/algorithm/string.hpp>
15 #include "boost/algorithm/string/erase.hpp"
16 #include "boost/algorithm/string/split.hpp"
17 
18 namespace dqi{
19 
22  return instance;
23  }
24 
25  void ConditionsSingleton::setCondition(const std::string& cond){
27  }
28 
29  const std::string& ConditionsSingleton::getCondition() const {
30  return m_currentConditions;
31  }
33  return m_numRefHisto;
34  }
35 
36  void ConditionsSingleton::setRefSourceMapping(const TMap* refsourcedata) {
37  if (! refsourcedata) {
38  std::cerr << "You are setting a bad refsourcedata! This will cause you trouble later!" << std::endl;
39  }
40  m_refsourcedata = refsourcedata;
41  }
42 
43  std::string ConditionsSingleton::getRefSourceData(const std::string& rawref) const {
44  if (! m_refsourcedata) {
45  // Suppress error message as it will occur in cases of backwards compatibility
46  // std::cerr << "Null refsourcedata: THIS IS REALLY BAD!!!" << std::endl;
47  return "";
48  }
49  const TObjString* value = dynamic_cast<TObjString*>(m_refsourcedata->GetValue(rawref.c_str()));
50  if (!value) {
51  std::cerr << "Unable to figure out refsource mapping: THIS IS ALSO REALLY BAD!!!" << std::endl;
52  std::cerr << "This happened for reference " << rawref << std::endl;
53  return "";
54  }
55  return value->GetName();
56  }
57 
59  std::stringstream oss;
60  oss<<"reference-"<<std::setfill('0')<<std::setw(9)<<m_numRefHisto;
61  m_numRefHisto++;
62  //std::cout<<__PRETTY_FUNCTION__<<"new histo name"<<oss.str()<<std::endl;
63  return oss.str();
64  }
65 
66  std::vector<std::string> ConditionsSingleton::getAllReferenceNames(std::string inp) const {
67  boost::algorithm::erase_all(inp," ");
68  // if(cleanCond.empty())return inp;
69  //std::set<std::string> referenceSet;
70  std::vector<std::string> refs;
71 
72  if(inp.find('=')!=std::string::npos){//we have conditions defined on reference
73  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
74  boost::char_separator<char> refSep(";");//field seperator for conditional references
75  boost::char_separator<char> condSep(",:"); //filed seperator for conditions and respective reference
76  tokenizer referenceConditionPairs(inp, refSep);
77  std::string defaultRef("");
78  for(tokenizer::iterator tok_iter=referenceConditionPairs.begin();
79  tok_iter!=referenceConditionPairs.end();++tok_iter){//look at each condition reference pair
80  if(std::string((*tok_iter)).find('=')!=std::string::npos){//have conditions defined
81  if(std::string(*tok_iter).find(':')!=std::string::npos){//do a reference split
82  std::vector<std::string> conds;
83  boost::split(conds,*tok_iter,boost::is_any_of(std::string(":")));
84  if(conds.size()>2){
85  std::cerr<<"Warning malformed reference \""<<inp<<"\""<<std::endl;
86  }else if(conds.size()<2){
87  std::cerr<<"Warning malformed reference \""<<inp<<"\""<<std::endl;
88  continue;
89  }
90  refs.push_back(conds.at(conds.size()-1));
91  //referenceSet.insert(conds.at(conds.size()-1));
92  }
93  }else{// no = sign. Then this should be a default reference.
94  if(defaultRef.empty()){
95  defaultRef=*tok_iter;
96  }else{
97  std::cout<<"WARNING! overwriting old default reference \""
98  <<defaultRef<<"\" with \""
99  <<*tok_iter<<"\""<<std::endl;
100  defaultRef=*tok_iter;
101  }
102  std::cout<<"Default reference = "<<defaultRef<<std::endl;
103  }
104 
105  }
106  // for(std::set<std::string>::iterator it=referenceSet.begin();it!=referenceSet.end();++it){
107  // refs.push_back(*it);
108  // }
109  if(!defaultRef.empty())refs.push_back(defaultRef);
110  return refs;
111  }else{
112  refs.push_back(inp);
113  return refs;
114  }
115  }
116 
117  void ConditionsSingleton::makeConditionMap(std::map<std::string, std::string>& cmap,
118  const std::string& condition){
119  if (condition.empty()) return;
120 
121  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
122  std::string cleanCond(condition);
123  boost::algorithm::erase_all(cleanCond," ");//delete all spaces
124  boost::char_separator<char> condSep(","); //filed seperator for conditions and respective reference
125  tokenizer conditionPairs(condition, condSep);
126  for (tokenizer::const_iterator tok_iter = conditionPairs.begin();
127  tok_iter != conditionPairs.end(); ++tok_iter) {
128  std::vector<std::string> splitpairs;
129  boost::split(splitpairs, *tok_iter, boost::is_any_of(std::string("=")));
130  if (splitpairs.size() != 2) {
131  std::cerr << "WARNING: malformed condition \"" << *tok_iter << "\"" << std::endl;
132  continue;
133  } else {
134  if (cmap.find(splitpairs[0]) != cmap.end()) {
135  std::cerr << "WARNING: redefinition of condition " << splitpairs[0] << std::endl;
136  }
137  cmap[splitpairs[0]] = splitpairs[1];
138  }
139  }
140  }
141 
142  bool ConditionsSingleton::conditionsMatch(std::map<std::string, std::string>& refConds,
143  std::map<std::string, std::string>& currentConds) const {
144  for (std::map<std::string, std::string>::const_iterator cond_iter = refConds.begin();
145  cond_iter != refConds.end(); ++cond_iter) {
146  // reject if reference key is not in current conditions
147  if (currentConds.find((*cond_iter).first) == currentConds.end()) return false;
148  if ((*cond_iter).second != currentConds[(*cond_iter).first]) return false;
149  }
150  // all specified reference conditions match
151  return true;
152  }
153 
154  std::string ConditionsSingleton::conditionalSelect(std::string inp,const std::string& condition){
155  std::string cleanCond(condition);
156  boost::algorithm::erase_all(cleanCond," ");//delete all spaces
157  std::map<std::string, std::string> condition_map, reference_map;
158  makeConditionMap(condition_map, condition);
159  boost::algorithm::erase_all(inp," ");
160  // if(cleanCond.empty())return inp;
161  if(inp.find('=')!=std::string::npos){//we have conditions defined on reference
162  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
163  boost::char_separator<char> refSep(";");//field seperator for conditional references
164  boost::char_separator<char> condSep(",:"); //filed seperator for conditions and respective reference
165  tokenizer referenceConditionPairs(inp, refSep);
166  std::string defaultRef("");
167  std::map<std::string,std::string> conditionalReferences;
168  for(tokenizer::iterator tok_iter=referenceConditionPairs.begin();
169  tok_iter!=referenceConditionPairs.end();++tok_iter){//look at each condition reference pair
170  if(std::string((*tok_iter)).find('=')!=std::string::npos){//have conditions defined
171  if(std::string(*tok_iter).find(':')!=std::string::npos){//do a reference split
172  std::vector<std::string> conds;
173  boost::split(conds,*tok_iter,boost::is_any_of(std::string(":")));
174  if(conds.size()>2){
175  std::cerr<<"Warning malformed reference \""<<inp<<"\""<<std::endl;
176  }else if(conds.size()<2){
177  std::cerr<<"Warning malformed reference \""<<inp<<"\""<<std::endl;
178  continue;
179  }
180 // std::cout<<"Conditions = \""<<conds.at(conds.size()-2)
181 // <<"\",reference= "<<conds.at(conds.size()-1)<<std::endl;
182  reference_map.clear();
183  makeConditionMap(reference_map, conds.at(conds.size()-2));
184  if (conditionsMatch(reference_map, condition_map)) {
185  return conds.at(conds.size()-1);
186  }
187  //conditionalReferences[conds.at(conds.size()-2)]=conds.at(conds.size()-1);
188  }
189  }else{// no = sign. Then this should be a default reference.
190  if(defaultRef.empty()){
191  defaultRef=*tok_iter;
192  }else{
193  std::cout<<"WARNING! overwriting old default reference \""
194  <<defaultRef<<"\" with \""
195  <<*tok_iter<<"\""<<std::endl;
196  defaultRef=*tok_iter;
197  }
198 // std::cout<<"Default reference = "<<defaultRef<<std::endl;
199  }
200  }
201  // if(cleanCond.empty()||conditionalReferences.find(cleanCond)==conditionalReferences.end()){
202 // std::cout<<"returning default reference "<<defaultRef<<std::endl;
203 // return defaultRef;
204 // }
205 // std::cout<<"returning reference "<<conditionalReferences[cleanCond]<<std::endl;
206 // return conditionalReferences[cleanCond];
207  return defaultRef;
208  }else{
209  return inp;
210  }
211  }
212  //
213  //
214  // This method is needed to replace the reference names in algorithm with conditions
215  //
216 
217  std::vector<std::pair<std::string,std::string> > ConditionsSingleton::getConditionReferencePairs(std::string inp) const {
218  std::vector<std::pair<std::string,std::string> > condPairs;
219  std::map<std::string,std::string> pairMap;//unique condition-reference pairs;
220  boost::algorithm::erase_all(inp," ");
221  std::string defaultRef("");
222  if(inp.find('=')!=std::string::npos){//we have conditions defined on reference
223  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
224  boost::char_separator<char> refSep(";");//field seperator for conditional references
225  boost::char_separator<char> condSep(",:"); //filed seperator for conditions and respective reference
226  tokenizer referenceConditionPairs(inp, refSep);
227  std::map<std::string,std::string> conditionalReferences;
228  for(tokenizer::iterator tok_iter=referenceConditionPairs.begin();
229  tok_iter!=referenceConditionPairs.end();++tok_iter){//look at each condition reference pair
230  if(std::string((*tok_iter)).find('=')!=std::string::npos){//have conditions defined
231  if(std::string(*tok_iter).find(':')!=std::string::npos){//do a reference split
232  std::vector<std::string> conds;
233  boost::split(conds,*tok_iter,boost::is_any_of(std::string(":")));
234  if(conds.size()>2){
235  std::cerr<<"Warning malformed reference \""<<inp<<"\""<<std::endl;
236  }else if(conds.size()<2){
237  std::cerr<<"Warning malformed reference \""<<inp<<"\""<<std::endl;
238  continue;
239  }
240  condPairs.push_back(std::make_pair(conds.at(conds.size()-2),conds.at(conds.size()-1)));
241  //pairMap[conds.at(conds.size()-2)]=conds.at(conds.size()-1);
242  }
243  }else{// no = sign. Then this should be a default reference.
244  if(defaultRef.empty()){
245  defaultRef=*tok_iter;
246  }else{
247  std::cout<<"WARNING! overwriting old default reference \""
248  <<defaultRef<<"\" with \""
249  <<*tok_iter<<"\""<<std::endl;
250  defaultRef=*tok_iter;
251  }
252  // std::cout<<"Default reference = "<<defaultRef<<std::endl;
253  }
254  }
255  // for(std::map<std::string,std::string>::iterator it=pairMap.begin();it!=pairMap.end();++it){
256  // condPairs.push_back(std::make_pair<std::string,std::string>(it->first,it->second));
257  // }
258  if(!defaultRef.empty())condPairs.push_back(std::make_pair("",defaultRef));
259  //return condPairs;
260  }else{
261  condPairs.push_back(std::make_pair("",inp));
262  }
263  return condPairs;
264  }
265  void ConditionsSingleton::setNewReferenceName(const std::string& oldName,const std::string& newName){
266  if(oldName.empty()){
267  std::cerr<<"Warning old name of the reference is empty. New name is \""<<newName<<"\""<<std::endl;
268  return;
269  }
270  if(newName.empty()){
271  std::cerr<<"Warning new name of the reference is empty. Old name is \""<<oldName<<"\""<<std::endl;
272  return;
273  }
274  if(m_referenceMap.find(oldName)==m_referenceMap.end()){
275  m_referenceMap[oldName]=newName;
276  }else{
277  std::cerr<<"Warning reference \""<<oldName<<"\" is added to map before as \""
278  << m_referenceMap[oldName]<<"\". New name is \""<<newName<<"\""<<std::endl;
279  return;
280  }
281  }
282  std::string ConditionsSingleton::getNewReferenceName(const std::string& oldName,bool quiet) const {
283  if(oldName.empty()){
284  std::cerr<<"Reference must have a name"<<std::endl;
285  return {};
286  }
287 
288  const auto itr = m_referenceMap.find(oldName);
289  if(itr==m_referenceMap.end()){
290  if(!quiet)std::cerr<<"Non-existent reference\""<<oldName<<"\". Returning empty string"<<std::endl;
291  return {};
292  }
293  return itr->second;
294  }
295 
296 }
297 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
dqi::ConditionsSingleton::m_referenceMap
std::map< std::string, std::string > m_referenceMap
Definition: ConditionsSingleton.h:26
dqi::ConditionsSingleton::getRefSourceData
std::string getRefSourceData(const std::string &rawref) const
Definition: ConditionsSingleton.cxx:43
dqi::ConditionsSingleton::getInstance
static ConditionsSingleton & getInstance()
Definition: ConditionsSingleton.cxx:20
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
dqi::ConditionsSingleton::getNewReferenceName
std::string getNewReferenceName(const std::string &, bool quiet=false) const
Definition: ConditionsSingleton.cxx:282
dqi::ConditionsSingleton::getNewRefHistoName
std::string getNewRefHistoName()
Definition: ConditionsSingleton.cxx:58
quiet
bool quiet
Definition: TrigGlobEffCorrValidation.cxx:190
dqi::ConditionsSingleton::m_refsourcedata
const TMap * m_refsourcedata
Definition: ConditionsSingleton.h:27
athena.value
value
Definition: athena.py:122
LArG4GenerateShowerLib.condition
condition
Definition: LArG4GenerateShowerLib.py:19
dqi::ConditionsSingleton::getCondition
const std::string & getCondition() const
Definition: ConditionsSingleton.cxx:29
dqi::ConditionsSingleton::m_currentConditions
std::string m_currentConditions
Definition: ConditionsSingleton.h:25
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
dqi::ConditionsSingleton::setNewReferenceName
void setNewReferenceName(const std::string &, const std::string &)
Definition: ConditionsSingleton.cxx:265
dqi::ConditionsSingleton::conditionsMatch
bool conditionsMatch(std::map< std::string, std::string > &refConds, std::map< std::string, std::string > &currentConds) const
Definition: ConditionsSingleton.cxx:142
ConditionsSingleton.h
dqi::ConditionsSingleton::getAllReferenceNames
std::vector< std::string > getAllReferenceNames(std::string inp) const
Definition: ConditionsSingleton.cxx:66
dqi::ConditionsSingleton::getNumReferenceHistos
int getNumReferenceHistos() const
Definition: ConditionsSingleton.cxx:32
dqi::ConditionsSingleton::makeConditionMap
void makeConditionMap(std::map< std::string, std::string > &cmap, const std::string &condition)
Definition: ConditionsSingleton.cxx:117
MakeNewFileFromOldAndSubstitution.newName
dictionary newName
Definition: ICHEP2016/MakeNewFileFromOldAndSubstitution.py:95
dqi::ConditionsSingleton::setRefSourceMapping
void setRefSourceMapping(const TMap *refsourcedata)
Definition: ConditionsSingleton.cxx:36
dqi::ConditionsSingleton::getConditionReferencePairs
std::vector< std::pair< std::string, std::string > > getConditionReferencePairs(std::string inp) const
Definition: ConditionsSingleton.cxx:217
dqi::ConditionsSingleton::conditionalSelect
std::string conditionalSelect(std::string inp, const std::string &condition)
Definition: ConditionsSingleton.cxx:154
dqi::ConditionsSingleton::setCondition
void setCondition(const std::string &c)
Definition: ConditionsSingleton.cxx:25
dqi
Definition: CompositeAlgorithm.h:16
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
dqi::ConditionsSingleton::m_numRefHisto
int m_numRefHisto
Definition: ConditionsSingleton.h:24
dqi::ConditionsSingleton
Definition: ConditionsSingleton.h:18