ATLAS Offline Software
RatesGroup.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 
8 void RatesCPS::execute(const double prescale) {
9  m_weight *= 1. - (1. / ( prescale / m_coherentFactor ) );
10 }
11 
12 double RatesCPS::getWeight() const {
13  return (1. - m_weight) / m_coherentFactor;
14 }
15 
16 RatesGroup::RatesGroup(const std::string& name, IMessageSvc* msgSvc, const bool doHistograms, const bool doExtrapolation) :
17  RatesHistoBase(name, msgSvc, doHistograms),
18  m_name(name),
19  m_nameHash(std::hash<std::string>{}(name)),
20  m_rateAccumulatorOR(0.),
21  m_rateAccumulatorAND(0.),
22  m_rateAccumulatorOR2(0.),
23  m_rateAccumulatorAND2(0.),
24  m_doCachedWeights(false),
25  m_cachedWeights(),
26  m_useCachedWeights(false),
27  m_extrapolationStrategy(doExtrapolation ? ExtrapStrat_t::kLINEAR : ExtrapStrat_t::kNONE),
28  m_masterGroup(nullptr),
29  m_uniqueTrigger(nullptr),
30  m_isExpressGroup(false),
31  m_children()
32  {}
33 
35 
36 const std::string RatesGroup::printConfig() const {
37  std::stringstream ss;
38  ss << m_name << std::endl;
39  for (const auto& child : m_children) {
40  if (child.first != std::hash<std::string>{}("")) ss << "\t" << (**(child.second.begin())).getSeedName() << " ->" << std::endl;
41  for (const auto& chain : child.second) {
42  ss << "\t\t" << chain->getName() << std::endl;
43  }
44  }
45  return ss.str();
46 }
47 
48 const std::string RatesGroup::printRate(const double ratesDenominator) const {
49  std::stringstream ss;
50  ss <<"RateOR: " << std::setw(11) << std::right << m_rateAccumulatorOR/ratesDenominator
51  << " +- " << std::setw(11) << std::left << sqrt(m_rateAccumulatorOR2)/ratesDenominator << " Hz, "
52  << " RateAND: " << std::setw(11) << std::right << m_rateAccumulatorAND/ratesDenominator
53  << " +- " << std::setw(11) << std::left << sqrt(m_rateAccumulatorAND2)/ratesDenominator << " Hz"
54  << " : " << m_name
56  return ss.str();
57 }
58 
60  if (m_children.count(toAdd->getSeedHash()) == 0) {
61  m_children.insert( std::make_pair(toAdd->getSeedHash(), std::set<const RatesTrigger*>() ) );
62  m_cachedWeights.insert( std::make_pair(toAdd->getSeedHash(), 1.) );
63  }
64  m_children.at(toAdd->getSeedHash()).insert(toAdd);
65 }
66 
68  if (m_children.count(toRemove->getSeedHash()) == 0) return;
69  m_children.at(toRemove->getSeedHash()).erase(toRemove);
70 }
71 
73  for (auto iterator = m_children.begin(); iterator != m_children.end(); /*noop*/) {
74  if (iterator->first == toKeep->getSeedHash()) {
75  ++iterator; // Keep
76  } else {
77  iterator = m_children.erase(iterator);
78  }
79  }
80 }
81 
83  double weightOR = 1., weightAND = 1.;
84  if (m_doCachedWeights == true) { // Reset cache
85  for (auto& cacheElement : m_cachedWeights) cacheElement.second = 1.;
86  }
87 
88  // Need a check for an empty group. Otherwise the "AND" logic will
89  if (m_children.size() == 0) weightAND = 0;
90 
91  for (const auto& element : m_children) { // Loop over all children, partitioned by L1 seed
92  const std::set<const RatesTrigger*>& triggers = element.second; // I'm the set of triggers seeded off this item
93 
94  double weightL1 = 0;
95  double weightHLT_OR = 1.;
96  double weightHLT_AND = 1.;
97  std::unordered_map<size_t, RatesCPS> weightHLT_CPS; // Map of CPS-group-hash to RatesCPS accumulators
98 
99  for (const auto& trigger : triggers) { //
100  if (trigger->getPassed()) {
101 
102  const double trigPrescaleReciprocal = 1. / trigger->getPrescale( m_isExpressGroup );
103 
104  weightL1 = 1. / trigger->getSeedPrescale();
105  weightHLT_AND *= trigPrescaleReciprocal; // TODO - check that CPS doesn't affect AND
106 
107  const size_t CPSID = trigger->getCPSID();
108  if (CPSID == 0) { // No CPS
109  weightHLT_OR *= 1. - trigPrescaleReciprocal;
110  } else { // Keep track for each CPS group
111  RatesCPS& cps = weightHLT_CPS[CPSID];
112  cps.setCoherentFactor( trigger->getCoherentFactor() );
113  cps.execute( 1. / trigPrescaleReciprocal );
114  }
115 
116  } else { // Trigger FAILED
117  weightHLT_AND *= 0;
118  }
119 
120  // Efficiency check.
121  // If any PS=1 chain passes, the weightHLT_OR = (1 - 1/1) = 0 [which gets inverted to 1 in weightOR below]
122  // If any chain fails, weightHLT_AND = 0
123  if (isZero(weightHLT_OR) && isZero(weightHLT_AND)) break;
124  }
125 
126  // Include the CPS chain's contributions
127  for (const auto& cps : weightHLT_CPS) weightHLT_OR *= 1. - cps.second.getWeight();
128 
129  weightOR *= 1. - (weightL1 * (1. - weightHLT_OR));
130  weightAND *= weightL1 * weightHLT_AND;
131 
132  //if (m_name == "Main") std::cout << "|M|weightL1:" << weightL1 << ",weightHLT_OR:" << weightHLT_OR << ",weightOR:" << weightOR;
133 
134  // If we are caching this result for use by the Unique groups
135  if (m_doCachedWeights) {
136  for (auto& cacheElement : m_cachedWeights) {
137  if (cacheElement.first == element.first) continue; // This is the one L1 seed in the combination we're excluding from this cache
138  cacheElement.second *= 1. - (weightL1 * (1. - weightHLT_OR));
139  }
140  }
141 
142  }
143 
144  // If we are a unique group then we only actually have one element above - the set of L1 items which have
145  // a common seed with our unique trigger. Everything else is identical to the global rate calc so we can fetch it from the cache
146  if (m_useCachedWeights == true) {
147  size_t myOneAndOnlyL1SeedHash = (m_children.begin())->first;
148  weightOR *= m_masterGroup->getCachedWeight( myOneAndOnlyL1SeedHash );
149  }
150 
151  //TODO - we currently only let groups scale linearly. Should change this.
152  const double w = weights.m_enhancedBiasWeight * getExtrapolationFactor(weights, m_extrapolationStrategy);
153  const double wOR = w * (1. - weightOR);
154  const double wAND = w * weightAND;
155 
156  m_rateAccumulatorOR += wOR;
157  m_rateAccumulatorAND += wAND;
158  m_rateAccumulatorOR2 += wOR * wOR;
159  m_rateAccumulatorAND2 += wAND * wAND;
160 
161  if (m_rateVsMuCachedPtr != nullptr) {
162  m_rateVsMuCachedPtr->Fill(weights.m_eventMu, wOR);
163  }
164  if (m_rateVsTrainCachedPtr != nullptr) {
165  m_rateVsTrainCachedPtr->Fill(weights.m_distanceInTrain, wOR);
166  }
167  if (m_dataCachedPtr != nullptr) {
170  }
171 
172  if (m_uniqueTrigger != nullptr && m_uniqueTrigger->getDataHist() != nullptr && !m_uniqueTrigger->getDisabled()) {
174  }
175 
176 }
177 
178 double RatesGroup::getUniqueWeight(const double ratesDenominator) const {
180  if (isZero(diff)) return 0.;
181  return diff / ratesDenominator;
182 }
183 
185 
187 
189 
190 void RatesGroup::duplicateChildren(const RatesGroup* toDuplicate) {
191  m_children = toDuplicate->getChildren(); m_masterGroup = toDuplicate;
192 }
193 
194 double RatesGroup::getCachedWeight(const size_t l1Hash) const { return m_cachedWeights.at(l1Hash); }
195 
197 
199 
200 const std::unordered_map<size_t, std::set<const RatesTrigger*>>& RatesGroup::getChildren() const {
201  return m_children;
202 }
203 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
RatesGroup::getUniqueWeight
double getUniqueWeight(const double ratesDenominator) const
Get the unique rate of a unique-rate group For a group being used to get a unique rate,...
Definition: RatesGroup.cxx:178
RatesGroup::removeFromGroup
void removeFromGroup(const RatesTrigger *toRemove)
Remove a trigger from this group.
Definition: RatesGroup.cxx:67
runLayerRecalibration.chain
chain
Definition: runLayerRecalibration.py:175
kUNIQUE_BIN
@ kUNIQUE_BIN
Bin used to store data needed to get the unique rate.
Definition: RatesHistoBase.h:48
RatesTrigger::getSeedHash
size_t getSeedHash() const
Definition: RatesTrigger.cxx:145
RatesGroup::m_uniqueTrigger
RatesTrigger * m_uniqueTrigger
If not nullptr, then a trigger this group is calculating the unique rate for.
Definition: RatesGroup.h:123
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
RatesHistoBase::m_dataCachedPtr
TH1 * m_dataCachedPtr
Cached, non-owning pointer.
Definition: RatesHistoBase.h:115
WeightingValuesSummary_t
Structure to hold per-event weights for distribution.
Definition: RatesHistoBase.h:55
RatesTrigger.h
RatesGroup::m_isExpressGroup
bool m_isExpressGroup
If this group is calculating for the express stream - also include express prescales.
Definition: RatesGroup.h:125
RatesGroup::m_name
const std::string m_name
Name of the group.
Definition: RatesGroup.h:110
RatesGroup::setUseCachedWeights
void setUseCachedWeights(const bool i)
Set to use cached weights from the Master group (need ptr to m_masterGroup)
Definition: RatesGroup.cxx:188
RatesGroup::RatesGroup
RatesGroup(const std::string &name, IMessageSvc *msgSvc, const bool doHistograms=true, const bool doExtrapolation=true)
Construct new RatesGroup to enumerate the combined union (OR) and intersection (AND) rate of a set of...
Definition: RatesGroup.cxx:16
RatesGroup::m_rateAccumulatorAND2
double m_rateAccumulatorAND2
For stat error.
Definition: RatesGroup.h:116
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
RatesGroup::m_extrapolationStrategy
const ExtrapStrat_t m_extrapolationStrategy
How this group is to scale with luminosity.
Definition: RatesGroup.h:121
RatesGroup::setExpressGroup
void setExpressGroup(const bool i)
Flag this group as the express group (modifies group trigger's prescales)
Definition: RatesGroup.cxx:184
RatesGroup.h
RatesGroup::getCachedWeight
double getCachedWeight(const size_t l1Hash) const
Get cached weight from triggers seeding from a given L1 item.
Definition: RatesGroup.cxx:194
python.TrigTLAMonitorAlgorithm.triggers
triggers
Definition: TrigTLAMonitorAlgorithm.py:196
RatesGroup::m_masterGroup
const RatesGroup * m_masterGroup
If not nullptr, then use the cached weights info in this master group object.
Definition: RatesGroup.h:122
RatesGroup::m_children
std::unordered_map< size_t, std::set< const RatesTrigger * > > m_children
Definition: RatesGroup.h:127
RatesTrigger
Used to calculate the rate for a single trigger at L1 or the HLT.
Definition: RatesTrigger.h:15
kPASS_WEIGHTED_OR_BIN
@ kPASS_WEIGHTED_OR_BIN
Bin used to store the total rate (OR)
Definition: RatesHistoBase.h:45
RatesGroup
Used to calculate the rate for a group of RatesTrigger objects at L1 or the HLT.
Definition: RatesGroup.h:29
RatesCPS::setCoherentFactor
void setCoherentFactor(const double coherentFactor)
Set a common prescale factor for this group.
Definition: RatesGroup.h:17
python.UpdateManyBadChannelIOVs.toRemove
tuple toRemove
Definition: UpdateManyBadChannelIOVs.py:87
RatesCPS::m_coherentFactor
double m_coherentFactor
Definition: RatesGroup.h:23
kLINEAR
@ kLINEAR
Scale trigger linearly with luminosity.
Definition: RatesHistoBase.h:31
RatesHistoBase::getDataHist
TH1 * getDataHist()
Definition: RatesHistoBase.cxx:83
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:36
lumiFormat.i
int i
Definition: lumiFormat.py:85
RatesHistoBase::getExtrapolationFactor
double getExtrapolationFactor(const WeightingValuesSummary_t &weights, const ExtrapStrat_t strat) const
Definition: RatesHistoBase.cxx:94
RatesGroup::printRate
const std::string printRate(const double ratesDenominator) const
Prints the RatesGroup's rate.
Definition: RatesGroup.cxx:48
RatesGroup::setDoCachedWeights
void setDoCachedWeights(const bool i)
Flag group to cache weights.
Definition: RatesGroup.cxx:186
RatesGroup::printConfig
const std::string printConfig() const
Prints the RatesGroup's configuration.
Definition: RatesGroup.cxx:36
RatesHistoBase::m_rateVsTrainCachedPtr
TH1 * m_rateVsTrainCachedPtr
Cached, non-owning pointer.
Definition: RatesHistoBase.h:114
RatesGroup::removeOtherL1
void removeOtherL1(const RatesTrigger *toKeep)
Remove from the groups mapping all triggers which have a dissimilar seed to the supplied trigger.
Definition: RatesGroup.cxx:72
RatesGroup::m_doCachedWeights
bool m_doCachedWeights
Used in the global rates group.
Definition: RatesGroup.h:118
DeMoUpdate.toAdd
bool toAdd
Definition: DeMoUpdate.py:1304
RatesTrigger::getDisabled
bool getDisabled() const
If I or my seed were prescaled out.
Definition: RatesTrigger.cxx:159
RatesHistoBase::m_rateVsMuCachedPtr
TH1 * m_rateVsMuCachedPtr
Cached, non-owning pointer.
Definition: RatesHistoBase.h:113
RatesGroup::m_rateAccumulatorOR2
double m_rateAccumulatorOR2
For stat error.
Definition: RatesGroup.h:115
kPASS_WEIGHTED_AND_BIN
@ kPASS_WEIGHTED_AND_BIN
Bin used to store the total rate (AND)
Definition: RatesHistoBase.h:46
RatesHistoBase
Basic base class for any common functionality between RatesTrigger and RatesGroup This means that eve...
Definition: RatesHistoBase.h:84
RatesHistoBase::isZero
static bool isZero(double v)
Definition: RatesHistoBase.h:103
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
RatesCPS::execute
void execute(const double prescale)
Keep track of a union product (OR of many chains) excluding the coherent part.
Definition: RatesGroup.cxx:8
weights
Definition: herwig7_interface.h:44
RatesGroup::execute
void execute(const WeightingValuesSummary_t &weights)
Perform group rates evaluation.
Definition: RatesGroup.cxx:82
RatesGroup::setUniqueTrigger
void setUniqueTrigger(RatesTrigger *trigger)
Set trigger I am doing unique rates for.
Definition: RatesGroup.cxx:196
RatesGroup::~RatesGroup
virtual ~RatesGroup()
Definition: RatesGroup.cxx:34
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
RatesGroup::getChildren
const std::unordered_map< size_t, std::set< const RatesTrigger * > > & getChildren() const
Definition: RatesGroup.cxx:200
DeMoScan.first
bool first
Definition: DeMoScan.py:536
RatesCPS::m_weight
double m_weight
Definition: RatesGroup.h:22
RatesGroup::addToGroup
void addToGroup(const RatesTrigger *toAdd)
Add a trigger to this group.
Definition: RatesGroup.cxx:59
RatesGroup::m_rateAccumulatorOR
double m_rateAccumulatorOR
Numerator for the rate of the OR of all triggers.
Definition: RatesGroup.h:113
RatesGroup::m_rateAccumulatorAND
double m_rateAccumulatorAND
Numerator for the rate of the AND of all triggers.
Definition: RatesGroup.h:114
RatesGroup::getUniqueTrigger
RatesTrigger * getUniqueTrigger()
Get the trigger I am doing unique rates for.
Definition: RatesGroup.cxx:198
RatesGroup::m_cachedWeights
std::unordered_map< size_t, double > m_cachedWeights
Cached weight of the OR of all triggers except for the L1 seed-hash of the key here.
Definition: RatesGroup.h:119
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
RatesCPS::getWeight
double getWeight() const
Return the union product (OR of many chains) coherently weighted by the coherent factor.
Definition: RatesGroup.cxx:12
RatesCPS
Helper class to keep track of coherent prescales between chains in a group This class knows about its...
Definition: RatesGroup.h:14
RatesGroup::m_useCachedWeights
bool m_useCachedWeights
Efficiency.
Definition: RatesGroup.h:120
RatesGroup::duplicateChildren
void duplicateChildren(const RatesGroup *toDuplicate)
Copy in triggers from another group.
Definition: RatesGroup.cxx:190
RatesHistoBase::getExtrapolationFactorString
const std::string & getExtrapolationFactorString(ExtrapStrat_t strat) const
Definition: RatesHistoBase.cxx:88
kNONE
@ kNONE
Do not scale this trigger for changes in luminosity.
Definition: RatesHistoBase.h:35