ATLAS Offline Software
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 
28 namespace CP
29 {
30 
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
56  const xAOD::TruthMetaDataContainer *tmd{};
57  if (inputMetaStore()->contains<xAOD::TruthMetaDataContainer>("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) {
64  m_mcChannelNumber = tmd->at(0)->mcChannelNumber();
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 
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);
147  if (!inputMetaStore()->contains<xAOD::CutBookkeeperContainer>(cbkName))
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 
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 
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  {
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  const WeightsGroup &weights = m_weights.at (m_truthWeightTool->getSysWeightIndex(sys));
233  h->SetBinContent (1, weights.nEventsProcessed);
234  h->SetBinContent (2, weights.sumOfWeights);
235  h->SetBinContent (3, weights.sumOfWeightsSquared);
236  }
237 
238  return StatusCode::SUCCESS;
239  }
240 } // namespace CP
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
EL::AnaAlgorithm::requestFileExecute
::StatusCode requestFileExecute()
register this algorithm to have an implementation of fileexecute
Definition: AnaAlgorithm.cxx:250
CutBookkeeper.h
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
CP::AsgCutBookkeeperAlg::m_mcChannelNumber
uint32_t m_mcChannelNumber
MC channel number we are processing.
Definition: AsgCutBookkeeperAlg.h:65
AthHistogramming::book
StatusCode book(const TH1 &hist, const std::string &tDir="", const std::string &stream="")
Simplify the booking and registering (into THistSvc) of histograms.
Definition: AthHistogramming.h:303
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
index
Definition: index.py:1
xAOD::CutBookkeeper_v1
Description of the class that is used to keep track of event counts.
Definition: CutBookkeeper_v1.h:29
EL::AnaAlgorithm::inputMetaStore
ConstMetaStorePtr_t inputMetaStore() const
Definition: AnaAlgorithm.cxx:72
skel.it
it
Definition: skel.GENtoEVGEN.py:396
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
CP::AsgCutBookkeeperAlg::m_weights
std::unordered_map< size_t, WeightsGroup > m_weights
weights map
Definition: AsgCutBookkeeperAlg.h:82
xAOD::CutBookkeeper_v1::sumOfEventWeightsSquared
double sumOfEventWeightsSquared() const
Get the sum-of-(event-weights-squared) that this CutBookkeeper has seen.
Definition: CutBookkeeper_v1.cxx:327
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
CP::AsgCutBookkeeperAlg::m_systematics
ServiceHandle< ISystematicsSvc > m_systematics
the systematics service
Definition: AsgCutBookkeeperAlg.h:57
CP::AsgCutBookkeeperAlg::processCutBookkeeper
void processCutBookkeeper(const xAOD::CutBookkeeper *cbk, size_t index)
process a CutBookkeeper
Definition: AsgCutBookkeeperAlg.cxx:179
CP::SystematicVariation
Definition: SystematicVariation.h:47
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:48
StringUtil.h
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
TruthMetaData.h
CP::AsgCutBookkeeperAlg::m_runNumber
Gaudi::Property< uint32_t > m_runNumber
run number we are processing
Definition: AsgCutBookkeeperAlg.h:61
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
CP::AsgCutBookkeeperAlg::WeightsGroup
weights helper struct
Definition: AsgCutBookkeeperAlg.h:70
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
CP::AsgCutBookkeeperAlg::m_truthWeightTool
ToolHandle< PMGTools::IPMGTruthWeightTool > m_truthWeightTool
the truth weight tool
Definition: AsgCutBookkeeperAlg.h:53
CP::AsgCutBookkeeperAlg::m_enableSystematics
Gaudi::Property< bool > m_enableSystematics
flag to enable systematics
Definition: AsgCutBookkeeperAlg.h:49
CP::AsgCutBookkeeperAlg::m_allowed_streams
std::vector< std::string > m_allowed_streams
input streams that we can run the CutBookkeepers against
Definition: AsgCutBookkeeperAlg.h:78
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
CP::AsgCutBookkeeperAlg::finalize
StatusCode finalize() override
finalize
Definition: AsgCutBookkeeperAlg.cxx:201
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ANA_MSG_INFO
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:290
ANA_MSG_WARNING
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:292
CutBookkeeperContainer.h
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
CP::AsgCutBookkeeperAlg::m_histPattern
Gaudi::Property< std::string > m_histPattern
the pattern for histogram names
Definition: AsgCutBookkeeperAlg.h:86
AsgCutBookkeeperAlg.h
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
xAOD::CutBookkeeperContainer_v1
Container that holds the Container of all CutBookkeepers.
Definition: CutBookkeeperContainer_v1.h:27
xAOD::FileMetaData_v1::mcProcID
@ mcProcID
Same as mc_channel_number [float].
Definition: FileMetaData_v1.h:74
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
xAOD::FileMetaData_v1
Class holding file-level metadata about an xAOD file.
Definition: FileMetaData_v1.h:34
xAOD::CutBookkeeper_v1::nAcceptedEvents
uint64_t nAcceptedEvents() const
Get the number of accepted events that this CutBookkeeper has seen.
Definition: CutBookkeeper_v1.cxx:291
CheckAppliedSFs.systematics
def systematics
Definition: CheckAppliedSFs.py:231
CP::AsgCutBookkeeperAlg::initialize
StatusCode initialize() override
initialize
Definition: AsgCutBookkeeperAlg.cxx:32
ANA_MSG_VERBOSE
#define ANA_MSG_VERBOSE(xmsg)
Macro printing verbose messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:286
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
weights
Definition: herwig7_interface.h:44
xAOD::CutBookkeeper_v1::sumOfEventWeights
double sumOfEventWeights() const
Get the sum-of-event-weights that this CutBookkeeper has seen.
Definition: CutBookkeeper_v1.cxx:309
CP::AsgCutBookkeeperAlg::fileExecute
StatusCode fileExecute() override
run once on each file
Definition: AsgCutBookkeeperAlg.cxx:81
RCU::substitute
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: StringUtil.cxx:24
DeMoScan.index
string index
Definition: DeMoScan.py:364
FileMetaData.h
h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
re
const boost::regex re(r_e)
AthHistogramming::hist
TH1 * hist(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered histograms of any type.
Definition: AthHistogramming.cxx:198
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
test_pyathena.counter
counter
Definition: test_pyathena.py:15
TruthMetaDataContainer.h
match
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition: hcg.cxx:356
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288