ATLAS Offline Software
EnhancedBiasWeighter.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef ENHANCEDBIASWEIGHTER_ENHANCEDBIASWEIGHTER_H
6 #define ENHANCEDBIASWEIGHTER_ENHANCEDBIASWEIGHTER_H 1
7 
8 #include "AsgTools/AsgTool.h"
10 #include "GaudiKernel/ToolHandle.h"
11 #include "Gaudi/Accumulators.h"
12 
15 
18 
19 #include <unordered_map>
20 #include <mutex>
21 
25 struct ChainDetail {
26  std::string m_name;
27  std::string m_lowerName;
28  std::string m_comment;
30  double m_prescale;
33  double m_rate;
34  double m_rateErr;
38  double m_efficiency;
42 };
43 
48  public:
49  //constructor for athena can be created using special macro
50  //Note: if you add a second interface to your tool, you must use: ASG_TOOL_CLASS2( ToolName, Interface1, Interface2)
52  //add another constructor for non-athena use cases
53  EnhancedBiasWeighter( const std::string& name );
54 
55  constexpr static double LHC_FREQUENCY = 11245.5; //<! LHC revolution frequency in Hz
56  constexpr static uint32_t FULL_RING = 2738;
57 
59  virtual StatusCode initialize() override;
60  virtual StatusCode finalize() override;
61  //
62 
67  virtual StatusCode addBranches() const override;
68 
73  virtual double getEBWeight(const xAOD::EventInfo* eventInfo) const override;
74  virtual double getEBWeight(const EventContext& context) const override;
75 
82  virtual double getEBLiveTime(const xAOD::EventInfo* eventInfo) const override;
83  virtual double getEBLiveTime(const EventContext& context) const override;
84 
89  virtual double getLBLength(const xAOD::EventInfo* eventInfo) const override;
90  virtual double getLBLength(const EventContext& context) const override;
91 
98  virtual double getLBLumi(const xAOD::EventInfo* eventInfo) const override;
99  virtual double getLBLumi(const EventContext& context) const override;
100 
104  virtual double getAverageLumi() const override;
105 
109  virtual double getAverageMu() const override;
110 
116  virtual double getDeadtime(const int lumiblock = -1) const override;
117 
123  virtual uint32_t getPairedBunches() const override;
124 
129  virtual StatusCode getDistanceIntoTrain(const xAOD::EventInfo* eventInfo, uint32_t& distance) const override;
130 
134  virtual uint32_t getRunNumber() const override;
135 
143  virtual bool isUnbiasedEvent(const xAOD::EventInfo* eventInfo) const override;
144 
150  virtual bool isGoodLB(const xAOD::EventInfo* eventInfo) const override;
151  virtual bool isGoodLB(const EventContext& context) const override;
152 
156  virtual bool isMC() const override;
157 
164  virtual std::unordered_map<std::string, ChainDetail> parsePrescaleXML(const std::string& prescaleXML) const override;
165 
169  virtual const std::vector<int32_t>& getBunchGroups() const override { return m_bunches; }
170 
171  private:
173  StatusCode loadLumi();
174  StatusCode trackAverages(const xAOD::EventInfo* eventInfo) const;
175  StatusCode trackAverages(const EventContext& context) const;
180  int32_t getEventEBID(const xAOD::EventInfo* eventInfo) const;
181  int32_t getEventEBID(const EventContext& context) const;
182 
183  std::string findLocalFile (const std::string& fileName) const;
184 
185  SG::ReadCondHandleKey<BunchCrossingCondData> m_bunchCrossingKey{this, "BunchCrossingKey", "BunchCrossingData", "Key BunchCrossing CDO" };
186 
187  Gaudi::Property<uint32_t> m_runNumber{this, "RunNumber", 0, "Run we're processing (if data), needed at initialize to locate and read in extra configuration."};
188  Gaudi::Property<bool> m_errorOnMissingEBWeights{this, "ErrorOnMissingEBWeights", false, "If true, Throws error if EB weights are missing."};
189  Gaudi::Property<bool> m_enforceEBGRL{this, "EnforceEBGRL", true, "Each Enhanced Bias run has a 'good run list' style veto on some LB. If this flag is true, events in these LB get weight 0"};
190  Gaudi::Property<bool> m_useBunchCrossingData{this, "UseBunchCrossingData", true, "BunchCrossing data requires CONDBR2 access. Can be disabled here if this is a problem."};
191  Gaudi::Property<bool> m_isMC{this, "IsMC", false, "MC mode? If so we need a cross section and filter efficiency"};
192  Gaudi::Property<double> m_mcCrossSection{this, "MCCrossSection", 0.0, "If running over MC. The process cross section in nb (AMI gives thins in nb)"};
193  Gaudi::Property<double> m_mcFilterEfficiency{this, "MCFilterEfficiency", 1.0, "If running over MC. The process filter efficiency (0.0-1.0)"};
194  Gaudi::Property<double> m_mcKFactor{this, "MCKFactor", 1.0, "If running over MC. The process filter efficiency (0.0-1.0)"};
195  Gaudi::Property<bool> m_mcIgnoreGeneratorWeights{this, "MCIgnoreGeneratorWeights", false, "If running over MC. Flag to ignore the generator weight."};
196  Gaudi::Property<double> m_inelasticCrossSection{this, "InelasticCrossSection", 8e-26, "Inelastic cross section in units cm^2. Default 80 mb at 13 TeV."};
197  Gaudi::Property<std::string> m_weightsDirectory {this, "EBWeightsDirectory", "", "Path to directory with EB XML weights files, if empty they will be read from calibration area"};
198 
199 
200  double m_deadtime;
203  std::unordered_map<uint64_t, int32_t> m_eventNumberToIdMap;
204 
205  mutable std::unordered_map<uint32_t, float> m_eventLivetime ATLAS_THREAD_SAFE;
206  mutable std::mutex m_mutex;
207  mutable Gaudi::Accumulators::AveragingCounter<double> m_lumiAverage ATLAS_THREAD_SAFE;
208  mutable Gaudi::Accumulators::AveragingCounter<double> m_muAverage ATLAS_THREAD_SAFE;
209 
210  std::unordered_map<int32_t, double> m_idToWeightMap;
211  std::unordered_map<int32_t, uint8_t> m_idToUnbiasedMap;
212  std::unordered_map<uint32_t, uint32_t> m_eventsPerLB;
213  std::unordered_map<uint32_t, double> m_lumiPerLB;
214  std::unordered_map<uint32_t, uint8_t> m_goodLB;
215  std::unordered_map<uint32_t, double> m_deadtimePerLB;
216 
217  std::vector<int32_t> m_bunches;
218 
220 };
221 
222 #endif //> !ENHANCEDBIASWEIGHTER_ENHANCEDBIASWEIGHTER_H
EnhancedBiasWeighter::getEBLiveTime
virtual double getEBLiveTime(const xAOD::EventInfo *eventInfo) const override
Definition: EnhancedBiasWeighter.cxx:467
ReadLumiBlock.h
asg::AsgTool
Base class for the dual-use tool implementation classes.
Definition: AsgTool.h:47
ChainDetail::m_rerunPrescale
double m_rerunPrescale
Definition: EnhancedBiasWeighter.h:36
EnhancedBiasWeighter::m_mcCrossSection
Gaudi::Property< double > m_mcCrossSection
Definition: EnhancedBiasWeighter.h:192
EnhancedBiasWeighter::initialize
virtual StatusCode initialize() override
Initialize is required by AsgTool base class.
Definition: EnhancedBiasWeighter.cxx:36
EnhancedBiasWeighter::m_runNumber
Gaudi::Property< uint32_t > m_runNumber
Definition: EnhancedBiasWeighter.h:187
ChainDetail::m_expressPrescale
double m_expressPrescale
Definition: EnhancedBiasWeighter.h:37
python.MagFieldUtils.lumiblock
lumiblock
Definition: MagFieldUtils.py:188
EnhancedBiasWeighter::ATLAS_THREAD_SAFE
Gaudi::Accumulators::AveragingCounter< double > m_lumiAverage ATLAS_THREAD_SAFE
The average instantaneous lumionosity over all events.
Definition: EnhancedBiasWeighter.h:207
EnhancedBiasWeighter::getDistanceIntoTrain
virtual StatusCode getDistanceIntoTrain(const xAOD::EventInfo *eventInfo, uint32_t &distance) const override
Definition: EnhancedBiasWeighter.cxx:694
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
EnhancedBiasWeighter::m_pairedBunches
uint32_t m_pairedBunches
Online number of paired bunches.
Definition: EnhancedBiasWeighter.h:201
EnhancedBiasWeighter
Tool to calculate data-driven rates using EnhancedBias datasets.
Definition: EnhancedBiasWeighter.h:47
ASG_TOOL_CLASS2
#define ASG_TOOL_CLASS2(CLASSNAME, INT1, INT2)
Definition: AsgToolMacros.h:77
ChainDetail::m_prescaledEfficiencyErr
double m_prescaledEfficiencyErr
Definition: EnhancedBiasWeighter.h:41
EnhancedBiasWeighter::m_isMC
Gaudi::Property< bool > m_isMC
Definition: EnhancedBiasWeighter.h:191
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
IAugmentationTool.h
EnhancedBiasWeighter::trackAverages
StatusCode trackAverages(const xAOD::EventInfo *eventInfo) const
Internal function to keep track of the mean instantaneous lumi & mean pileup of the EB/MC sample bein...
Definition: EnhancedBiasWeighter.cxx:460
EnhancedBiasWeighter::getLBLumi
virtual double getLBLumi(const xAOD::EventInfo *eventInfo) const override
Definition: EnhancedBiasWeighter.cxx:638
EnhancedBiasWeighter::m_mcIgnoreGeneratorWeights
Gaudi::Property< bool > m_mcIgnoreGeneratorWeights
Definition: EnhancedBiasWeighter.h:195
EnhancedBiasWeighter::getLBLength
virtual double getLBLength(const xAOD::EventInfo *eventInfo) const override
Definition: EnhancedBiasWeighter.cxx:546
EnhancedBiasWeighter::m_idToUnbiasedMap
std::unordered_map< int32_t, uint8_t > m_idToUnbiasedMap
Map a weighting ID to a flag if this weight is from an unbiased (RD) trigger online.
Definition: EnhancedBiasWeighter.h:211
EnhancedBiasWeighter::m_deadtimePerLB
std::unordered_map< uint32_t, double > m_deadtimePerLB
Map of average deadtime per LB.
Definition: EnhancedBiasWeighter.h:215
EnhancedBiasWeighter::m_eventsPerLB
std::unordered_map< uint32_t, uint32_t > m_eventsPerLB
Map of how many EnhancedBias events were recorded per LB.
Definition: EnhancedBiasWeighter.h:212
ChainDetail::m_counter
uint32_t m_counter
Definition: EnhancedBiasWeighter.h:29
EnhancedBiasWeighter::getEventEBID
int32_t getEventEBID(const xAOD::EventInfo *eventInfo) const
Definition: EnhancedBiasWeighter.cxx:353
ChainDetail::m_prescaledEfficiency
double m_prescaledEfficiency
Definition: EnhancedBiasWeighter.h:40
EnhancedBiasWeighter::getDeadtime
virtual double getDeadtime(const int lumiblock=-1) const override
Definition: EnhancedBiasWeighter.cxx:678
ChainDetail::m_lowerName
std::string m_lowerName
Definition: EnhancedBiasWeighter.h:27
EnhancedBiasWeighter::parsePrescaleXML
virtual std::unordered_map< std::string, ChainDetail > parsePrescaleXML(const std::string &prescaleXML) const override
Parse a presscale XML and return a ordered summary of its content To make most use of the XML parsing...
Definition: EnhancedBiasWeighter.cxx:249
ChainDetail::m_comment
std::string m_comment
Definition: EnhancedBiasWeighter.h:28
ChainDetail::m_prescale
double m_prescale
Definition: EnhancedBiasWeighter.h:30
DerivationFramework::IAugmentationTool
Definition: IAugmentationTool.h:24
EnhancedBiasWeighter::m_inelasticCrossSection
Gaudi::Property< double > m_inelasticCrossSection
Definition: EnhancedBiasWeighter.h:196
EnhancedBiasWeighter::m_enforceEBGRL
Gaudi::Property< bool > m_enforceEBGRL
Definition: EnhancedBiasWeighter.h:189
EnhancedBiasWeighter::m_weightsDirectory
Gaudi::Property< std::string > m_weightsDirectory
Definition: EnhancedBiasWeighter.h:197
IEnhancedBiasWeighter
Define interface of EnhancedBias weighting class.
Definition: IEnhancedBiasWeighter.h:20
EnhancedBiasWeighter::findLocalFile
std::string findLocalFile(const std::string &fileName) const
Definition: EnhancedBiasWeighter.cxx:745
EnhancedBiasWeighter::m_bunches
std::vector< int32_t > m_bunches
Number of BCIDs in each bunch group.
Definition: EnhancedBiasWeighter.h:217
EnhancedBiasWeighter::isGoodLB
virtual bool isGoodLB(const xAOD::EventInfo *eventInfo) const override
Definition: EnhancedBiasWeighter.cxx:590
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
EnhancedBiasWeighter::getAverageMu
virtual double getAverageMu() const override
Definition: EnhancedBiasWeighter.cxx:711
EnhancedBiasWeighter::m_readLumiBlock
ReadLumiBlock m_readLumiBlock
Cache lumi block lengths.
Definition: EnhancedBiasWeighter.h:219
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
EnhancedBiasWeighter::getAverageLumi
virtual double getAverageLumi() const override
Definition: EnhancedBiasWeighter.cxx:706
EnhancedBiasWeighter::m_useBunchCrossingData
Gaudi::Property< bool > m_useBunchCrossingData
Definition: EnhancedBiasWeighter.h:190
EnhancedBiasWeighter::isMC
virtual bool isMC() const override
Definition: EnhancedBiasWeighter.cxx:630
EnhancedBiasWeighter::FULL_RING
constexpr static uint32_t FULL_RING
Number of bunches in a full ring.
Definition: EnhancedBiasWeighter.h:56
ChainDetail::m_eventsPassedWeighted
double m_eventsPassedWeighted
Definition: EnhancedBiasWeighter.h:32
EnhancedBiasWeighter::m_bunchCrossingKey
SG::ReadCondHandleKey< BunchCrossingCondData > m_bunchCrossingKey
Tool to get distance into bunch train.
Definition: EnhancedBiasWeighter.h:185
EnhancedBiasWeighter::m_eventNumberToIdMap
std::unordered_map< uint64_t, int32_t > m_eventNumberToIdMap
Map event number to a weighting ID.
Definition: EnhancedBiasWeighter.h:203
EnhancedBiasWeighter::getEBWeight
virtual double getEBWeight(const xAOD::EventInfo *eventInfo) const override
Definition: EnhancedBiasWeighter.cxx:391
EnhancedBiasWeighter::m_mcFilterEfficiency
Gaudi::Property< double > m_mcFilterEfficiency
Definition: EnhancedBiasWeighter.h:193
ChainDetail::m_passthroughPrescale
double m_passthroughPrescale
Definition: EnhancedBiasWeighter.h:35
EnhancedBiasWeighter::LHC_FREQUENCY
constexpr static double LHC_FREQUENCY
Definition: EnhancedBiasWeighter.h:55
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
EnhancedBiasWeighter::m_errorOnMissingEBWeights
Gaudi::Property< bool > m_errorOnMissingEBWeights
Definition: EnhancedBiasWeighter.h:188
ChainDetail::m_efficiencyErr
double m_efficiencyErr
Definition: EnhancedBiasWeighter.h:39
EnhancedBiasWeighter::getPairedBunches
virtual uint32_t getPairedBunches() const override
Definition: EnhancedBiasWeighter.cxx:689
EnhancedBiasWeighter::addBranches
virtual StatusCode addBranches() const override
Decorate the AOD with EnhancedBias weighting quantities such that no CVMFS or DB access is required o...
Definition: EnhancedBiasWeighter.cxx:717
EnhancedBiasWeighter::EnhancedBiasWeighter
EnhancedBiasWeighter(const std::string &name)
Definition: EnhancedBiasWeighter.cxx:29
EnhancedBiasWeighter::getRunNumber
virtual uint32_t getRunNumber() const override
Definition: EnhancedBiasWeighter.cxx:634
EnhancedBiasWeighter::m_goodLB
std::unordered_map< uint32_t, uint8_t > m_goodLB
Like a Good Run List flag for EnhancedBias runs.
Definition: EnhancedBiasWeighter.h:214
ChainDetail::m_eventsPassed
double m_eventsPassed
Definition: EnhancedBiasWeighter.h:31
EnhancedBiasWeighter::m_mutex
std::mutex m_mutex
Protection for above map.
Definition: EnhancedBiasWeighter.h:206
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
EnhancedBiasWeighter::finalize
virtual StatusCode finalize() override
Definition: EnhancedBiasWeighter.cxx:69
IEnhancedBiasWeighter.h
EnhancedBiasWeighter::isUnbiasedEvent
virtual bool isUnbiasedEvent(const xAOD::EventInfo *eventInfo) const override
Definition: EnhancedBiasWeighter.cxx:571
SG::ReadCondHandleKey< BunchCrossingCondData >
ChainDetail::m_efficiency
double m_efficiency
Definition: EnhancedBiasWeighter.h:38
EnhancedBiasWeighter::ATLAS_THREAD_SAFE
Gaudi::Accumulators::AveragingCounter< double > m_muAverage ATLAS_THREAD_SAFE
The average mu over all events.
Definition: EnhancedBiasWeighter.h:208
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
EnhancedBiasWeighter::loadWeights
StatusCode loadWeights()
Read into memory from XML event weights for this EnhancedBias run.
Definition: EnhancedBiasWeighter.cxx:75
EnhancedBiasWeighter::m_idToWeightMap
std::unordered_map< int32_t, double > m_idToWeightMap
Map a weighting ID to a Enhanced Bias event weight.
Definition: EnhancedBiasWeighter.h:210
EnhancedBiasWeighter::m_deadtime
double m_deadtime
Online deadtime to correct for in rate prediction.
Definition: EnhancedBiasWeighter.h:200
EnhancedBiasWeighter::m_mcModifiedCrossSection
double m_mcModifiedCrossSection
Product of xsec, filter & kfactor.
Definition: EnhancedBiasWeighter.h:202
EnhancedBiasWeighter::getBunchGroups
virtual const std::vector< int32_t > & getBunchGroups() const override
Definition: EnhancedBiasWeighter.h:169
EnhancedBiasWeighter::m_lumiPerLB
std::unordered_map< uint32_t, double > m_lumiPerLB
Map of instantaneous luminosity per LB.
Definition: EnhancedBiasWeighter.h:213
ChainDetail::m_rate
double m_rate
Definition: EnhancedBiasWeighter.h:33
ChainDetail::m_name
std::string m_name
Definition: EnhancedBiasWeighter.h:26
EnhancedBiasWeighter::m_mcKFactor
Gaudi::Property< double > m_mcKFactor
Definition: EnhancedBiasWeighter.h:194
BunchCrossingCondData.h
Replaces the BunchCrossing AlgTool used in run1/2.
AsgTool.h
EnhancedBiasWeighter::ATLAS_THREAD_SAFE
std::unordered_map< uint32_t, float > m_eventLivetime ATLAS_THREAD_SAFE
Cache of per-event livetime as a function of LB [LB -> effective walltime per event].
Definition: EnhancedBiasWeighter.h:205
checker_macros.h
Define macros for attributes used to control the static checker.
EnhancedBiasWeighter::loadLumi
StatusCode loadLumi()
Read into memory this EnhancedBias run's XML.
Definition: EnhancedBiasWeighter.cxx:146
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
ReadLumiBlock
Helper class to read in lumi block length info from COOL for a run.
Definition: ReadLumiBlock.h:28
ChainDetail
Structure to encompass the data stored in a prescales XML generated by the RuleBook.
Definition: EnhancedBiasWeighter.h:25
ChainDetail::m_rateErr
double m_rateErr
Definition: EnhancedBiasWeighter.h:34