13#include "boost/tokenizer.hpp"
14#include <boost/algorithm/string.hpp>
15#include "boost/algorithm/string/split.hpp"
18 const std::string equalStr{
"="};
40 if (! refsourcedata) {
41 std::cerr <<
"You are setting a bad refsourcedata! This will cause you trouble later!" << std::endl;
52 const TObjString* value =
dynamic_cast<TObjString*
>(
m_refsourcedata->GetValue(rawref.c_str()));
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;
58 return value->GetName();
62 std::stringstream oss;
63 oss<<
"reference-"<<std::setfill(
'0')<<std::setw(9)<<
m_numRefHisto;
73 std::vector<std::string> refs;
75 if(inp.find(
'=')!=std::string::npos){
76 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
77 boost::char_separator<char> refSep(
";");
78 boost::char_separator<char> condSep(
",:");
79 tokenizer referenceConditionPairs(inp, refSep);
80 std::string defaultRef(
"");
81 for(tokenizer::iterator tok_iter=referenceConditionPairs.begin();
82 tok_iter!=referenceConditionPairs.end();++tok_iter){
83 if(std::string((*tok_iter)).
find(
'=')!=std::string::npos){
84 if(std::string(*tok_iter).find(
':')!=std::string::npos){
85 std::vector<std::string> conds;
86 boost::split(conds,*tok_iter,boost::is_any_of(std::string(
":")));
88 std::cerr<<
"Warning malformed reference \""<<inp<<
"\""<<std::endl;
89 }
else if(conds.size()<2){
90 std::cerr<<
"Warning malformed reference \""<<inp<<
"\""<<std::endl;
93 refs.push_back(conds.at(conds.size()-1));
97 if(defaultRef.empty()){
100 std::cout<<
"WARNING! overwriting old default reference \""
101 <<defaultRef<<
"\" with \""
102 <<*tok_iter<<
"\""<<std::endl;
103 defaultRef=*tok_iter;
105 std::cout<<
"Default reference = "<<defaultRef<<std::endl;
112 if(!defaultRef.empty())refs.push_back(std::move(defaultRef));
115 refs.push_back(std::move(inp));
121 const std::string& condition){
122 if (condition.empty())
return;
124 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
125 std::string cleanCond(condition);
126 std::erase(cleanCond,
' ');
127 boost::char_separator<char> condSep(
",");
128 tokenizer conditionPairs(condition, condSep);
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;
138 if (cmap.find(splitpairs[0]) != cmap.end()) {
139 std::cerr <<
"WARNING: redefinition of condition " << splitpairs[0] << std::endl;
141 cmap[splitpairs[0]] = splitpairs[1];
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) {
151 if (currentConds.find((*cond_iter).first) == currentConds.end())
return false;
152 if ((*cond_iter).second != currentConds[(*cond_iter).first])
return false;
159 std::string cleanCond(condition);
160 std::erase(cleanCond,
' ');
161 std::map<std::string, std::string> condition_map, reference_map;
165 if(inp.find(
'=')!=std::string::npos){
166 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
167 boost::char_separator<char> refSep(
";");
168 boost::char_separator<char> condSep(
",:");
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){
174 if(std::string((*tok_iter)).
find(
'=')!=std::string::npos){
175 if(std::string(*tok_iter).find(
':')!=std::string::npos){
176 std::vector<std::string> conds;
177 boost::split(conds,*tok_iter,boost::is_any_of(std::string(
":")));
179 std::cerr<<
"Warning malformed reference \""<<inp<<
"\""<<std::endl;
180 }
else if(conds.size()<2){
181 std::cerr<<
"Warning malformed reference \""<<inp<<
"\""<<std::endl;
186 reference_map.clear();
189 return conds.at(conds.size()-1);
194 if(defaultRef.empty()){
195 defaultRef=*tok_iter;
197 std::cout<<
"WARNING! overwriting old default reference \""
198 <<defaultRef<<
"\" with \""
199 <<*tok_iter<<
"\""<<std::endl;
200 defaultRef=*tok_iter;
222 std::vector<std::pair<std::string,std::string> > condPairs;
223 std::map<std::string,std::string> pairMap;
225 std::string defaultRef(
"");
226 if(inp.find(
'=')!=std::string::npos){
227 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
228 boost::char_separator<char> refSep(
";");
229 boost::char_separator<char> condSep(
",:");
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){
234 if(std::string((*tok_iter)).
find(
'=')!=std::string::npos){
235 if(std::string(*tok_iter).find(
':')!=std::string::npos){
236 std::vector<std::string> conds;
237 boost::split(conds,*tok_iter,boost::is_any_of(std::string(
":")));
239 std::cerr<<
"Warning malformed reference \""<<inp<<
"\""<<std::endl;
240 }
else if(conds.size()<2){
241 std::cerr<<
"Warning malformed reference \""<<inp<<
"\""<<std::endl;
244 condPairs.push_back(std::make_pair(conds.at(conds.size()-2),conds.at(conds.size()-1)));
248 if(defaultRef.empty()){
249 defaultRef=*tok_iter;
251 std::cout<<
"WARNING! overwriting old default reference \""
252 <<defaultRef<<
"\" with \""
253 <<*tok_iter<<
"\""<<std::endl;
254 defaultRef=*tok_iter;
262 if(!defaultRef.empty())condPairs.push_back(std::make_pair(
"",defaultRef));
265 condPairs.push_back(std::make_pair(
"",inp));
271 std::cerr<<
"Warning old name of the reference is empty. New name is \""<<newName<<
"\""<<std::endl;
275 std::cerr<<
"Warning new name of the reference is empty. Old name is \""<<oldName<<
"\""<<std::endl;
281 std::cerr<<
"Warning reference \""<<oldName<<
"\" is added to map before as \""
282 <<
m_referenceMap[oldName]<<
"\". New name is \""<<newName<<
"\""<<std::endl;
288 std::cerr<<
"Reference must have a name"<<std::endl;
294 if(!
quiet)std::cerr<<
"Non-existent reference\""<<oldName<<
"\". Returning empty string"<<std::endl;
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
const TMap * m_refsourcedata
void setRefSourceMapping(const TMap *refsourcedata)
void setCondition(const std::string &c)
std::string m_currentConditions
void setNewReferenceName(const std::string &, const std::string &)
bool conditionsMatch(std::map< std::string, std::string > &refConds, std::map< std::string, std::string > ¤tConds) const
std::string getNewRefHistoName()
std::vector< std::pair< std::string, std::string > > getConditionReferencePairs(std::string inp) const
std::string getNewReferenceName(const std::string &, bool quiet=false) const
ConditionsSingleton()=default
const std::string & getCondition() const
int getNumReferenceHistos() const
std::string conditionalSelect(std::string inp, const std::string &condition)
std::string find(const std::string &s)
return a remapped string