19 asg::AsgMessaging(
"TrigGlobalEfficiencyCorrectionTool"),
21 m_dictionary(*
new std::map<std::size_t,std::string>),
22 m_hasher(*
new std::
hash<std::string>)
29 m_dictionary(
parent.m_dictionary),
59 std::ifstream
f(
name.c_str(),std::ios_base::in);
65 if(std::getline(
f,
line))
67 const std::string::size_type
i =
line.find(
'#');
68 if(
i != std::string::npos)
line.resize(
i);
110 std::vector<std::string>
config;
111 if(!
readDataFile(
"TrigGlobalEfficiencyCorrection/Triggers.cfg",
config))
return false;
112 std::stringstream
ss;
113 std::string triggerName, token;
125 bool found_tau_leg =
false;
127 while((
ss >> token) && !found_tau_leg)
132 if(found_tau_leg)
continue;
139 for(std::size_t&
leg : def.leg)
141 if(!(
ss >> token))
break;
147 ATH_MSG_ERROR(
"Unknown trigger leg '" << token <<
"' found in Triggers.cfg");
156 ATH_MSG_ERROR(
"Unknown trigger leg '" << triggerName <<
"' (inferred from trigger name) found in Triggers.cfg");
160 if(!success)
continue;
172 if(!ne)
std::swap(def.leg[0],def.leg[1]);
186 if(!ne)
std::swap(def.leg[0], def.leg[2]);
187 else if(ne==1)
std::swap(def.leg[1], def.leg[2]);
193 else if((ne+
nm)==1)
std::swap(def.leg[1], def.leg[2]);
206 else success =
false;
208 else if(ne+
nm==0 || ne==3 ||
nm==3)
214 bool sym = (def.leg[0]==def.leg[1] || def.leg[1]==def.leg[2]);
218 else success =
false;
226 else if(ne==1 &&
nm==1) def.type =
TT_E_MU;
227 else if(ne==1) def.type =
TT_E_G;
238 ATH_MSG_ERROR(
"Configuration issue for trigger " << triggerName);
247 std::vector<std::string>
config;
248 if(!
readDataFile(
"TrigGlobalEfficiencyCorrection/Thresholds.cfg",
config))
return false;
249 std::stringstream
ss;
261 if(
unit ==
"GeV")
pt *= 1e3f;
262 else if(
unit !=
"MeV")
264 ATH_MSG_ERROR(
"Unable to import pT threshold for leg \"" <<
leg <<
"\" (missing unit)");
282 bool belowRecommended =
false;
283 for(
auto& kv : overridenThresholds)
289 try {
pt = std::stof(kv.second); }
292 ATH_MSG_ERROR(
"Unable to convert threshold argument \""<<kv.second<<
"\" to floating-point value");
298 ATH_MSG_WARNING(
"Suspiciously low threshold (" <<
pt <<
" MeV) set for trigger leg " << kv.first
299 <<
", please make sure you provided the threshold in MeV and not in GeV!");
301 if(pt < itr->
second) belowRecommended =
true;
306 ATH_MSG_ERROR(
"Can't override threshold for unknown trigger leg " << kv.first);
312 ATH_MSG_WARNING(
"Tool configured to use trigger thresholds below those recommended!");
320 std::vector<std::string>
config;
321 if(!
readDataFile(
"TrigGlobalEfficiencyCorrection/DataPeriods.cfg",
config))
return false;
322 std::stringstream
ss;
324 std::pair<unsigned,unsigned>
runs;
345 std::vector<std::string>
config;
346 if(!
readDataFile(
"TrigGlobalEfficiencyCorrection/Hierarchies.cfg",
config))
return false;
347 std::stringstream
ss;
348 std::string token,
unit;
349 std::map<std::size_t, std::vector<std::size_t> > aliases;
358 if(
line[1]==
'-' &&
line[2]==
']')
ss.ignore(3);
362 if(
line[1]==
'>')
ss.ignore(2) >> meta.minPt >>
unit;
363 else if(
line[1]==
'<')
ss.ignore(2) >> meta.maxPt >>
unit;
364 else ss.ignore(1) >> meta.minPt >>
sep >> meta.maxPt >>
unit;
367 ATH_MSG_ERROR(
"Unable to parse pT restrictions in Hierarchies.cfg");
379 auto itr = aliases.find(
h);
380 if(itr == aliases.end())
384 ATH_MSG_ERROR(
"Unknown trigger leg '" << token <<
"' found in Hierarchies.cfg");
391 if(
ss >> token && token!=
">") success =
false;
394 success = success && meta.nLegs;
399 auto& legs = aliases[
m_hasher(token)];
400 if(
ss >> token && token==
":=")
410 ATH_MSG_ERROR(
"Unknown trigger leg '" << token <<
"' found in Hierarchies.cfg");
413 if(
ss >> token && token!=
">") success =
false;
415 success = success && legs.size();
417 else success =
false;
433 std::vector<std::string>
config;
435 std::stringstream
ss;
437 bool reading =
false;
440 std::size_t
pos =
line.find(
"[VERSION]");
441 if(
pos != std::string::npos)
446 while(std::getline(
ss, token,
','))
456 if(!reading)
continue;
469 else insertion.first->second |=
year;
472 if(!keysPerLeg.size())
486 boundaries = itr->second;
491 if(
sep!=std::string::npos)
493 std::string kwMin =
period.substr(0,
sep);
494 std::string kwMax =
period.substr(
sep+1);
500 boundaries = std::minmax({itrMin->second.first, itrMax->second.first, itrMin->second.second, itrMax->second.second});
506 boundaries = std::minmax(std::stoi(kwMin), std::stoi(kwMax));
523 return xAOD::Type::Other;
535 return xAOD::Type::Other;
541 if(
s.find(
"|||") != std::string::npos)
543 ATH_MSG_ERROR(
"Invalid format for the trigger combination '" << triggerString <<
"'");
550 auto i =
s.find(
"||");
551 if(
i == std::string::npos)
break;
556 ATH_MSG_ERROR(
"Invalid format for the trigger combination '" << triggerString <<
"'");
561 std::set<std::size_t>
hashes;
562 std::stringstream
ss(
s);
563 while(std::getline(
ss,
s,
'|'))
570 ATH_MSG_ERROR(
"Unknown trigger '" <<
s <<
"' found while parsing trigger combination");
574 if(!
hashes.insert(trig).second)
576 ATH_MSG_ERROR(
"The trigger '" <<
s <<
"' is present more than once in the combination");
582 success = success &&
triggers.size();
588 std::map<std::string,std::string>& legsPerKey,
595 std::map<std::size_t,int> legs;
597 for(
auto& kv : triggerCombination)
607 for(
int k=0;
k<32;++
k)
610 if(itr !=
m_dataPeriods.end() && itrPeriod->second.first <= itr->second.second
611 && itrPeriod->second.second >= itr->second.first)
617 if(!success)
continue;
620 for(std::size_t
leg : trig.leg)
624 auto insertion = legs.emplace(
leg,
years);
625 if(!insertion.second) insertion.first->second |=
years;
630 if(!success)
return false;
634 std::map<std::size_t,std::map<std::size_t,int> > allKeys;
636 std::map<std::size_t,std::vector<std::size_t> > allLegsPerKey;
637 std::set<std::size_t> legsWithMultipleKeys;
638 bool sameKeyForAllyears =
true;
641 allLegsPerKey.clear();
642 for(
auto& kvLegs : legs)
644 std::size_t
leg = kvLegs.first;
645 int years = kvLegs.second;
646 auto itrKeys = allKeys.find(
leg);
647 if(itrKeys != allKeys.end())
649 for(
auto& kvKeys : itrKeys->second)
651 auto y = (kvKeys.second &
years);
652 if((
y==
years) || (!sameKeyForAllyears &&
y!=0))
654 auto insertion = allLegsPerKey.emplace(kvKeys.first,std::vector<std::size_t>{
leg});
655 if(!insertion.second) insertion.first->second.push_back(
leg);
661 ATH_MSG_ERROR(
"Sorry, no idea what the map key should be for the trigger leg '"
668 if(!allLegsPerKey.size())
670 if(sameKeyForAllyears)
672 sameKeyForAllyears =
false;
679 using T = decltype(allLegsPerKey)::
value_type;
680 auto itrKey = std::max_element(allLegsPerKey.begin(), allLegsPerKey.end(),
681 [](T&
x,T&
y){return x.second.size()<y.second.size();});
682 std::string& strLegs = legsPerKey[
m_dictionary.at(itrKey->first)];
683 for(std::size_t
leg : itrKey->second)
685 int& wantedYears = legs.at(
leg);
686 int supportedYears = (allKeys.at(
leg).at(itrKey->first)) & wantedYears;
687 if(supportedYears!=wantedYears || legsWithMultipleKeys.count(
leg))
689 legsWithMultipleKeys.insert(
leg);
690 for(
int i=0;
i<32;++
i)
692 if(supportedYears & (1
u<<
i))
694 if(strLegs.length() && strLegs.back()!=
',') strLegs +=
',';
701 if(strLegs.length() && strLegs.back()!=
',') strLegs +=
',';
704 if(supportedYears == wantedYears) legs.erase(
leg);
705 else wantedYears &= ~supportedYears;
714 legsPerKey.emplace(
std::to_string(legsPerKey.size()), m_dictionary.at(kv.first));
717 for (
auto& [
key, legs]: legsPerKey)
722 "a default scale factor of 1 being returned");
725 if(!success) legsPerKey.clear();
732 std::set<std::size_t> extraItems;
733 std::vector<ImportData::TrigDef> updatedTriggers;
737 std::size_t
pos = 0, len =
name.find(
"_OR_");
738 if(len == std::string::npos)
740 updatedTriggers.emplace_back(trig);
750 ATH_MSG_ERROR(
"while listing triggers for trigger matching; trigger \"" <<
item <<
"\" extracted from \"" <<
name <<
"\" is not recognized");
753 if(extraItems.emplace(
h).second) updatedTriggers.emplace_back(def->second);
754 if(len == std::string::npos)
break;
757 if(len != std::string::npos) len -=
pos;