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