ATLAS Offline Software
Loading...
Searching...
No Matches
AsgCutBookkeeperAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7//
8// includes
9//
10
12
14#include <TH1.h>
15
21
22#include <regex>
23
24//
25// method implementations
26//
27
28namespace CP
29{
30
31 StatusCode AsgCutBookkeeperAlg ::
32 initialize ()
33 {
34 if (m_runNumber.value() == 0)
35 {
36 ANA_MSG_ERROR ("Run number should be set");
37 return StatusCode::FAILURE;
38 }
39
41
42 // Read the channel number from FileMetaData
43 const xAOD::FileMetaData *fmd{};
44 ANA_CHECK (inputMetaStore()->retrieve(fmd, "FileMetaData"));
45
46 float flt_channel_number{};
47 if (fmd->value(xAOD::FileMetaData::mcProcID, flt_channel_number))
48 {
49 m_mcChannelNumber = static_cast<uint32_t>(flt_channel_number);
50 }
51 if (m_mcChannelNumber == 0)
52 {
53 ANA_MSG_WARNING ("MC channel number could not be read from the FileMetaData.");
54
55 // Try also TruthMetaData
58 {
59 ATH_CHECK (inputMetaStore()->retrieve(tmd, "TruthMetaData"));
60 ATH_MSG_DEBUG("Loaded xAOD::TruthMetaDataContainer");
61
62 // If we only have one metadata item take MC channel from there if needed
63 if (tmd->size() == 1) {
65 ATH_MSG_WARNING("... MC channel number taken from the metadata as " << m_mcChannelNumber);
66 }
67 }
68 }
69
70 // input streams supported by the algorithm
71
72 // Prepare for systematics
73 ANA_CHECK (m_truthWeightTool.retrieve());
74
75 return StatusCode::SUCCESS;
76 }
77
78
79
80 StatusCode AsgCutBookkeeperAlg ::
81 fileExecute ()
82 {
83 ANA_MSG_DEBUG ("Updating CutBookkeeper information");
84
85 // Update MC channel number if needed
86 if (m_mcChannelNumber == 0)
87 {
88 const xAOD::FileMetaData *fmd{};
89 ANA_CHECK (inputMetaStore()->retrieve(fmd, "FileMetaData"));
90
91 float flt_channel_number{};
92 if (fmd->value(xAOD::FileMetaData::mcProcID, flt_channel_number)) {
93 m_mcChannelNumber = static_cast<uint32_t>(flt_channel_number);
94 }
95 }
96
97 // Retrieve complete CutBookkeeperContainer
98 const xAOD::CutBookkeeperContainer *completeCBC{};
99 ANA_CHECK (inputMetaStore()->retrieve(completeCBC, "CutBookkeepers"));
100
101 // Find the max cycle
102 int maxCycle{-1};
103 const xAOD::CutBookkeeper *allEvents{};
104 for (const xAOD::CutBookkeeper *cbk : *completeCBC)
105 {
106 ANA_MSG_DEBUG ("Complete cbk name: " << cbk->name() << " - stream: " << cbk->inputStream());
107
108 if (cbk->cycle() > maxCycle && cbk->name() == "AllExecutedEvents"
109 && std::find(m_allowed_streams.begin(), m_allowed_streams.end(), cbk->inputStream()) != m_allowed_streams.end())
110 {
111 allEvents = cbk;
112 maxCycle = cbk->cycle();
113 }
114 }
115
116 if (allEvents == nullptr)
117 {
118 ANA_MSG_ERROR ("Could not find AllExecutedEvents CutBookkeeper information.");
119 return StatusCode::FAILURE;
120 }
121
122 size_t counter{};
123 for (const xAOD::CutBookkeeper *cbk : *completeCBC)
124 {
125 if (cbk->cycle() == maxCycle && cbk->name().find("AllExecutedEvents") == 0
126 && std::find(m_allowed_streams.begin(), m_allowed_streams.end(), cbk->inputStream()) != m_allowed_streams.end())
127 {
128 static const std::regex re ("AllExecutedEvents.*_([0-9]+)");
129 // Get the CBK index
130 size_t index{0};
131 std::smatch match;
132 if (std::regex_match(cbk->name(), match, re))
133 {
134 index = std::stoi(match[1]);
135 }
136
138
139 counter++;
140 }
141 }
142
143 // now try systematics-aware containers
144 for (size_t index{1}; index < m_truthWeightTool->getWeightNames().size(); ++index)
145 {
146 std::string cbkName = "CutBookkeepers_weight_" + std::to_string(index);
148 {
149 ANA_MSG_VERBOSE ("No container named " << cbkName << "available");
150 continue;
151 }
152
153 ANA_CHECK (inputMetaStore()->retrieve(completeCBC, cbkName));
154 for (const xAOD::CutBookkeeper *cbk : *completeCBC)
155 {
156 if (cbk->cycle() == maxCycle && cbk->name().find("AllExecutedEvents") == 0
157 && std::find(m_allowed_streams.begin(), m_allowed_streams.end(), cbk->inputStream()) != m_allowed_streams.end())
158 {
160 counter++;
161 }
162 }
163 }
164
165 // check if we actually had systematics available
166 if (counter == 1 && m_enableSystematics) {
167 ANA_MSG_WARNING ("This sample does not support CutBookkeeper systematics. Disabling...");
168 m_enableSystematics = false;
169 }
170
171 ANA_CHECK (m_systematics.retrieve());
172
173 return StatusCode::SUCCESS;
174 }
175
176
177
178 void AsgCutBookkeeperAlg ::
179 processCutBookkeeper (const xAOD::CutBookkeeper *cbk,
180 size_t index)
181 {
182 uint64_t nEventsProcessed = cbk->nAcceptedEvents();
183 double sumOfWeights = cbk->sumOfEventWeights();
184 double sumOfWeightsSquared = cbk->sumOfEventWeightsSquared();
185
186 // Write CutBookkeeper information to the info
187 ANA_MSG_VERBOSE ("CutBookkeeper information from the current file for index " << index << ":");
188 ANA_MSG_VERBOSE ("Initial events = " << nEventsProcessed);
189 ANA_MSG_VERBOSE ("Initial sum of weights = " << sumOfWeights);
190 ANA_MSG_VERBOSE ("Initial sum of weights squared = " << sumOfWeightsSquared);
191
192 auto it = m_weights.emplace(index, WeightsGroup()).first;
193 it->second.nEventsProcessed += nEventsProcessed;
194 it->second.sumOfWeights += sumOfWeights;
195 it->second.sumOfWeightsSquared += sumOfWeightsSquared;
196 }
197
198
199
200 StatusCode AsgCutBookkeeperAlg ::
201 finalize ()
202 {
203 // Temporarily handle systematics directly here
204 std::vector<CP::SystematicSet> systematics;
205 systematics.emplace_back();
207 {
208 for (const CP::SystematicVariation &variation : m_truthWeightTool->affectingSystematics())
209 {
210 systematics.emplace_back(CP::SystematicSet({variation}));
211 }
212 }
213
214 for (const CP::SystematicSet &sys : systematics)
215 {
216 std::string name = RCU::substitute(m_histPattern, "%DSID%", std::to_string(m_mcChannelNumber));
217 name = RCU::substitute(name, "%RUN%", std::to_string(m_runNumber));
218 ANA_CHECK (m_systematics->makeSystematicsName (name, name, sys));
219
220 ANA_CHECK (book(TH1F(name.c_str(), "CutBookkeeper Information", 3, 0.5, 3.5)));
221 TH1 *h = hist(name);
222 assert(h != nullptr);
223
224 h->GetXaxis()->SetBinLabel (1, "Initial events");
225 h->GetXaxis()->SetBinLabel (2, "Initial sum of weights");
226 h->GetXaxis()->SetBinLabel (3, "Initial sum of weights squared");
227
228 ANA_MSG_INFO ("CutBookkeeper information will be stored in " << name);
229
230 ANA_MSG_VERBOSE ("Running systematics " << sys.name() << " with index " << m_truthWeightTool->getSysWeightIndex(sys));
231
232 // If first event is skipped, fileExecute() is not ran, and the m_weights will be empty.
233 // Ideally we should check eventRange.m_beginEvent in the if statement, and return statusCode::Failure if eventRange.m_beginEvent is 0.
234 if (m_weights.empty()) {
235 ANA_MSG_ERROR ("No weight is available for CutBookkeeper histogram. \nDid you set skip-n-events > 0? This is a known issue in EventLoop.");
236 break;
237 }
238
239 const WeightsGroup &weights = m_weights.at (m_truthWeightTool->getSysWeightIndex(sys));
240 h->SetBinContent (1, weights.nEventsProcessed);
241 h->SetBinContent (2, weights.sumOfWeights);
242 h->SetBinContent (3, weights.sumOfWeightsSquared);
243 }
244
245 return StatusCode::SUCCESS;
246 }
247} // namespace CP
const boost::regex re(r_e)
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
#define ANA_MSG_VERBOSE(xmsg)
Macro printing verbose messages.
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
static const std::vector< std::string > systematics
Header file for AthHistogramAlgorithm.
StatusCode book(const TH1 &hist, const std::string &tDir="", const std::string &stream="")
Simplify the booking and registering (into THistSvc) of histograms.
TH1 * hist(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered histograms of any type.
ServiceHandle< ISystematicsSvc > m_systematics
the systematics service
Gaudi::Property< uint32_t > m_runNumber
run number we are processing
uint32_t m_mcChannelNumber
MC channel number we are processing.
Gaudi::Property< bool > m_enableSystematics
flag to enable systematics
Gaudi::Property< std::string > m_histPattern
the pattern for histogram names
ToolHandle< PMGTools::IPMGTruthWeightTool > m_truthWeightTool
the truth weight tool
std::vector< std::string > m_allowed_streams
input streams that we can run the CutBookkeepers against
void processCutBookkeeper(const xAOD::CutBookkeeper *cbk, size_t index)
process a CutBookkeeper
std::unordered_map< size_t, WeightsGroup > m_weights
weights map
Class to wrap a set of SystematicVariations.
const T * at(size_type n) const
Access an element, as an rvalue.
size_type size() const noexcept
Returns the number of elements in the collection.
ConstMetaStorePtr_t inputMetaStore() const
::StatusCode requestFileExecute()
register this algorithm to have an implementation of fileexecute
double sumOfEventWeightsSquared() const
Get the sum-of-(event-weights-squared) that this CutBookkeeper has seen.
double sumOfEventWeights() const
Get the sum-of-event-weights that this CutBookkeeper has seen.
uint64_t nAcceptedEvents() const
Get the number of accepted events that this CutBookkeeper has seen.
@ mcProcID
Same as mc_channel_number [float].
bool value(MetaDataType type, std::string &val) const
Get a pre-defined string value out of the object.
uint32_t mcChannelNumber() const
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition hcg.cxx:357
Select isolated Photons, Electrons and Muons.
std::string substitute(const std::string &str, const std::string &pattern, const std::string &with)
effects: substitute all occurences of "pattern" with "with" in the string "str" returns: the substitu...
Definition index.py:1
TruthMetaDataContainer_v1 TruthMetaDataContainer
Declare the latest version of the truth vertex container.
CutBookkeeper_v1 CutBookkeeper
Define the latest version of the CutBookkeeper class.
FileMetaData_v1 FileMetaData
Declare the latest version of the class.
setEventNumber uint32_t
CutBookkeeperContainer_v1 CutBookkeeperContainer
Define the latest version of the CutBookkeeperContainer class.