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  // Prepare for systematics
71  ANA_CHECK (m_truthWeightTool.retrieve());
72 
73  return StatusCode::SUCCESS;
74  }
75 
76 
77 
79  fileExecute ()
80  {
81  ANA_MSG_DEBUG ("Updating CutBookkeeper information");
82 
83  // Update MC channel number if needed
84  if (m_mcChannelNumber == 0)
85  {
86  const xAOD::FileMetaData *fmd{};
87  ANA_CHECK (inputMetaStore()->retrieve(fmd, "FileMetaData"));
88 
89  float flt_channel_number{};
90  if (fmd->value(xAOD::FileMetaData::mcProcID, flt_channel_number)) {
91  m_mcChannelNumber = static_cast<uint32_t>(flt_channel_number);
92  }
93  }
94 
95  // Retrieve complete CutBookkeeperContainer
96  const xAOD::CutBookkeeperContainer *completeCBC{};
97  ANA_CHECK (inputMetaStore()->retrieve(completeCBC, "CutBookkeepers"));
98 
99  // Find the max cycle
100  int maxCycle{-1};
101  const xAOD::CutBookkeeper *allEvents{};
102  for (const xAOD::CutBookkeeper *cbk : *completeCBC)
103  {
104  ANA_MSG_DEBUG ("Complete cbk name: " << cbk->name() << " - stream: " << cbk->inputStream());
105 
106  if (cbk->cycle() > maxCycle && cbk->name() == "AllExecutedEvents" && cbk->inputStream() == "StreamAOD")
107  {
108  allEvents = cbk;
109  maxCycle = cbk->cycle();
110  }
111  }
112 
113  if (allEvents == nullptr)
114  {
115  ANA_MSG_ERROR ("Could not find AllExecutedEvents CutBookkeeper information.");
116  return StatusCode::FAILURE;
117  }
118 
119  size_t counter{};
120  for (const xAOD::CutBookkeeper *cbk : *completeCBC)
121  {
122  if (cbk->cycle() == maxCycle && cbk->name().find("AllExecutedEvents") == 0 && cbk->inputStream() == "StreamAOD")
123  {
124  static const std::regex re ("AllExecutedEvents.*_([0-9]+)");
125  // Get the CBK index
126  size_t index{0};
127  std::smatch match;
128  if (std::regex_match(cbk->name(), match, re))
129  {
130  index = std::stoi(match[1]);
131  }
132 
134 
135  counter++;
136  }
137  }
138 
139  // now try systematics-aware containers
140  for (size_t index{1}; index < m_truthWeightTool->getWeightNames().size(); ++index)
141  {
142  std::string cbkName = "CutBookkeepers_weight_" + std::to_string(index);
143  if (!inputMetaStore()->contains<xAOD::CutBookkeeperContainer>(cbkName))
144  {
145  ANA_MSG_VERBOSE ("No container named " << cbkName << "available");
146  continue;
147  }
148 
149  ANA_CHECK (inputMetaStore()->retrieve(completeCBC, cbkName));
150  for (const xAOD::CutBookkeeper *cbk : *completeCBC)
151  {
152  if (cbk->cycle() == maxCycle && cbk->name().find("AllExecutedEvents") == 0 && cbk->inputStream() == "StreamAOD")
153  {
155  counter++;
156  }
157  }
158  }
159 
160  // check if we actually had systematics available
161  if (counter == 1 && m_enableSystematics) {
162  ANA_MSG_WARNING ("This sample does not support CutBookkeeper systematics. Disabling...");
163  m_enableSystematics = false;
164  }
165 
166  ANA_CHECK (m_systematics.retrieve());
167 
168  return StatusCode::SUCCESS;
169  }
170 
171 
172 
175  size_t index)
176  {
177  uint64_t nEventsProcessed = cbk->nAcceptedEvents();
178  double sumOfWeights = cbk->sumOfEventWeights();
179  double sumOfWeightsSquared = cbk->sumOfEventWeightsSquared();
180 
181  // Write CutBookkeeper information to the info
182  ANA_MSG_VERBOSE ("CutBookkeeper information from the current file for index " << index << ":");
183  ANA_MSG_VERBOSE ("Initial events = " << nEventsProcessed);
184  ANA_MSG_VERBOSE ("Initial sum of weights = " << sumOfWeights);
185  ANA_MSG_VERBOSE ("Initial sum of weights squared = " << sumOfWeightsSquared);
186 
187  auto it = m_weights.emplace(index, WeightsGroup()).first;
188  it->second.nEventsProcessed += nEventsProcessed;
189  it->second.sumOfWeights += sumOfWeights;
190  it->second.sumOfWeightsSquared += sumOfWeightsSquared;
191  }
192 
193 
194 
196  finalize ()
197  {
198  // Temporarily handle systematics directly here
199  std::vector<CP::SystematicSet> systematics;
200  systematics.emplace_back();
202  {
203  for (const CP::SystematicVariation &variation : m_truthWeightTool->affectingSystematics())
204  {
205  systematics.emplace_back(CP::SystematicSet({variation}));
206  }
207  }
208 
209  for (const CP::SystematicSet &sys : systematics)
210  {
213  ANA_CHECK (m_systematics->makeSystematicsName (name, name, sys));
214 
215  ANA_CHECK (book(TH1F(name.c_str(), "CutBookkeeper Information", 3, 0.5, 3.5)));
216  TH1 *h = hist(name);
217  assert(h != nullptr);
218 
219  h->GetXaxis()->SetBinLabel (1, "Initial events");
220  h->GetXaxis()->SetBinLabel (2, "Initial sum of weights");
221  h->GetXaxis()->SetBinLabel (3, "Initial sum of weights squared");
222 
223  ANA_MSG_INFO ("CutBookkeeper information will be stored in " << name);
224 
225  ANA_MSG_VERBOSE ("Running systematics " << sys.name() << " with index " << m_truthWeightTool->getSysWeightIndex(sys));
226 
227  const WeightsGroup &weights = m_weights.at (m_truthWeightTool->getSysWeightIndex(sys));
228  h->SetBinContent (1, weights.nEventsProcessed);
229  h->SetBinContent (2, weights.sumOfWeights);
230  h->SetBinContent (3, weights.sumOfWeightsSquared);
231  }
232 
233  return StatusCode::SUCCESS;
234  }
235 } // 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
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:423
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:78
CP::AsgCutBookkeeperAlg::WeightsGroup::sumOfWeightsSquared
float sumOfWeightsSquared
Definition: AsgCutBookkeeperAlg.h:73
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:174
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
CP::AsgCutBookkeeperAlg::WeightsGroup::nEventsProcessed
float nEventsProcessed
Definition: AsgCutBookkeeperAlg.h:71
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
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:196
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
CP::AsgCutBookkeeperAlg::WeightsGroup::sumOfWeights
float sumOfWeights
Definition: AsgCutBookkeeperAlg.h:72
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:82
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:195
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
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:79
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:362
FileMetaData.h
h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TH1
Definition: rootspy.cxx:268
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