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