Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MetadataAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <src/MetadataAlg.h>
6 
7 #include "HDF5Utils/Writer.h"
8 
14 
15 #include <regex>
16 #include <fstream>
17 #include <boost/json.hpp>
18 
19 //
20 // method implementations
21 //
22 
23 namespace {
24 
25  // name for top level object in json or h5
26  static const std::string top_group = "cutBookkeeper";
27 
28  void addCounts(H5::Group& grp, const OriginalAodCounts& counts){
30 #define ADD(NAME, TYPE) \
31  cons.add<TYPE>(#NAME,[](const OriginalAodCounts& c) {return c.NAME;})
32  ADD(nEventsProcessed, long long);
33  ADD(sumOfWeights, double);
34  ADD(sumOfWeightsSquared, double);
35 #undef ADD
37  writer.fill(counts);
38  }
39  void addCounts(boost::json::object& grp, const OriginalAodCounts& counts) {
40  grp["counts"] = {
41 #define ADD(NAME) {#NAME, counts.NAME}
42  ADD(nEventsProcessed),
43  ADD(sumOfWeights),
44  ADD(sumOfWeightsSquared),
45 #undef ADD
46  };
47  }
48 
49  OriginalAodCounts getCounts(const xAOD::CutBookkeeper& cbk)
50  {
51  OriginalAodCounts counts;
52  counts.nEventsProcessed = cbk.nAcceptedEvents();
53  counts.sumOfWeights = cbk.sumOfEventWeights();
55  return counts;
56  }
57 
58  static const std::vector<std::string> allowed_streams{
59  "StreamAOD", "StreamEVGEN", "StreamEVNT"};
60  bool isGoodBook(const xAOD::CutBookkeeper& cbk) {
61  const auto& s = allowed_streams;
62  return cbk.name() == "AllExecutedEvents"
63  && std::find(s.begin(), s.end(), cbk.inputStream()) != s.end();
64  }
65 
66 }
67 
68 namespace ftag {
69 
71  const std::string& name,
72  ISvcLocator* pSvcLocator):
73  AthAlgorithm(name, pSvcLocator),
74  m_inputMetaStore("StoreGateSvc/InputMetaDataStore", name)
75  {
76  }
77 
79  {
80  // register with incident service
81  ServiceHandle<IIncidentSvc> incSvc( "IncidentSvc", name() );
82  CHECK( incSvc.retrieve() );
83  // remove first to make sure it's not called twice
84  incSvc->removeListener( this, IncidentType::BeginInputFile );
85  incSvc->addListener( this, IncidentType::BeginInputFile, 0, true );
86 
87  CHECK(m_truthWeightTool.retrieve());
88 
89  if (!m_output_svc.empty()) {
90  ATH_CHECK(m_output_svc.retrieve());
91  }
92 
93  return StatusCode::SUCCESS;
94  }
95 
97  {
98  return StatusCode::SUCCESS;
99  }
100 
101  void MetadataAlg::handle(const Incident& inc)
102  {
103 
104  // skip all incidents beyond the start of input files
105  if (inc.type() != IncidentType::BeginInputFile) return;
106 
107  ATH_MSG_DEBUG("Updating CutBookkeeper information");
108 
109  // Retrieve complete CutBookkeeperContainer
110  const xAOD::CutBookkeeperContainer *completeCBC{};
111  auto rc = m_inputMetaStore->retrieve(completeCBC, "CutBookkeepers");
112  if (!rc.isSuccess()) throw std::runtime_error(
113  "could not retrieve CutBookkeepers");
114 
115  // Find the max cycle
116  int maxCycle{-1};
117  const xAOD::CutBookkeeper *allEvents{};
118  for (const xAOD::CutBookkeeper *cbk : *completeCBC)
119  {
121  "Complete cbk name: " << cbk->name() <<
122  " - stream: " << cbk->inputStream()
123  );
124 
125  if (cbk->cycle() > maxCycle && isGoodBook(*cbk))
126  {
127  allEvents = cbk;
128  maxCycle = cbk->cycle();
129  }
130  }
131 
132  if (allEvents == nullptr)
133  {
134  std::string error(
135  "Could not find AllExecutedEvents CutBookkeeper information.");
136  throw std::runtime_error(error);
137  }
138 
139  for (const xAOD::CutBookkeeper *cbk : *completeCBC)
140  {
141  if (cbk->cycle() == maxCycle && isGoodBook(*cbk))
142  {
143  static const std::regex re("AllExecutedEvents.*_([0-9]+)");
144  // Get the CBK index
145  size_t index{0};
146  std::smatch match;
147  if (std::regex_match(cbk->name(), match, re))
148  {
149  index = std::stoi(match[1]);
150  }
151  m_weights[index] += getCounts(*cbk);
152  }
153  }
154 
155  // now try systematics-aware containers
156  for (size_t index{1}; index < m_truthWeightTool->getWeightNames().size(); ++index)
157  {
158  std::string cbkName = "CutBookkeepers_weight_" + std::to_string(index);
159  if (!m_inputMetaStore->contains<xAOD::CutBookkeeperContainer>(cbkName))
160  {
161  ATH_MSG_VERBOSE("No container named " << cbkName << "available");
162  continue;
163  }
164 
165  if (!m_inputMetaStore->retrieve(completeCBC, cbkName).isSuccess()) {
166  throw std::runtime_error("could not retrieve " + cbkName);
167  }
168  for (const xAOD::CutBookkeeper *cbk : *completeCBC)
169  {
170  if (cbk->cycle() == maxCycle && isGoodBook(*cbk))
171  {
172  m_weights[index] += getCounts(*cbk);
173  }
174  }
175  }
176  }
177 
178 
180  {
181 
182  std::vector<CP::SystematicSet> systematics;
183  systematics.emplace_back();
184  for (const CP::SystematicVariation &variation : m_truthWeightTool->affectingSystematics())
185  {
186  auto set = CP::SystematicSet({variation});
187  ATH_MSG_DEBUG("using systematic " << set.name());
188  systematics.emplace_back(set);
189  }
190 
191  std::optional<H5::Group> h5_cbk;
192  if (!m_output_svc.empty()) {
193  h5_cbk = H5::Group(m_output_svc->group()->createGroup(top_group));
194  }
195  std::optional<boost::json::object> json_cbk;
196  if (!m_json_output.empty()) {
197  json_cbk = boost::json::object{};
198  }
199 
200  for (const CP::SystematicSet &sys : systematics)
201  {
202  const std::string sysname = sys.name().empty() ? "nominal": sys.name();
203  const OriginalAodCounts &weights = m_weights.at(
204  m_truthWeightTool->getSysWeightIndex(sys));
205 
206  if (h5_cbk) {
207  H5::Group sysgroup(h5_cbk->createGroup(sysname));
208  addCounts(sysgroup, weights);
209  }
210  if (json_cbk) {
211  boost::json::object& cbk_root = *json_cbk;
212  addCounts(cbk_root[sysname].emplace_object(), weights);
213  }
214  }
215  if (json_cbk) {
216  std::ofstream out(m_json_output.value());
217  boost::json::object jroot {
218  {top_group, *json_cbk}
219  };
220  out << jroot << std::endl;
221  }
222 
223  return StatusCode::SUCCESS;
224  }
225 
226 }
OriginalAodCounts::nEventsProcessed
unsigned long long nEventsProcessed
Definition: OriginalAodCounts.h:21
CutBookkeeper.h
xAOD::CutBookkeeper_v1::cycle
int cycle() const
Get the skimming cycle that this CutBookkeeper was running in.
Definition: CutBookkeeper_v1.cxx:205
TrigDefs::Group
Group
Properties of a chain group.
Definition: GroupProperties.h:13
ftag::MetadataAlg::m_output_svc
ServiceHandle< IH5GroupSvc > m_output_svc
Definition: MetadataAlg.h:40
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
index
Definition: index.py:1
ftag::MetadataAlg::m_inputMetaStore
ServiceHandle< StoreGateSvc > m_inputMetaStore
Definition: MetadataAlg.h:35
xAOD::CutBookkeeper_v1
Description of the class that is used to keep track of event counts.
Definition: CutBookkeeper_v1.h:29
CheckAppliedSFs.systematics
systematics
Definition: CheckAppliedSFs.py:231
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
MetadataAlg.h
xAOD::CutBookkeeper_v1::sumOfEventWeightsSquared
double sumOfEventWeightsSquared() const
Get the sum-of-(event-weights-squared) that this CutBookkeeper has seen.
Definition: CutBookkeeper_v1.cxx:327
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
CP::SystematicVariation
Definition: SystematicVariation.h:47
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
TruthMetaData.h
Writer.h
ftag::MetadataAlg::m_weights
std::unordered_map< size_t, OriginalAodCounts > m_weights
Definition: MetadataAlg.h:45
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
ftag::MetadataAlg::m_json_output
Gaudi::Property< std::string > m_json_output
Definition: MetadataAlg.h:42
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
ftag::MetadataAlg::handle
void handle(const Incident &) override
Definition: MetadataAlg.cxx:101
H5Utils::Writer
Writer.
Definition: Writer.h:349
OriginalAodCounts::sumOfWeightsSquared
double sumOfWeightsSquared
Definition: OriginalAodCounts.h:23
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ftag::MetadataAlg::m_truthWeightTool
ToolHandle< PMGTools::IPMGTruthWeightTool > m_truthWeightTool
Definition: MetadataAlg.h:37
CutBookkeeperContainer.h
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
xAOD::CutBookkeeperContainer_v1
Container that holds the Container of all CutBookkeepers.
Definition: CutBookkeeperContainer_v1.h:27
ftag::MetadataAlg::initialize
StatusCode initialize() override
Definition: MetadataAlg.cxx:78
AthAlgorithm
Definition: AthAlgorithm.h:47
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
xAOD::CutBookkeeper_v1::nAcceptedEvents
uint64_t nAcceptedEvents() const
Get the number of accepted events that this CutBookkeeper has seen.
Definition: CutBookkeeper_v1.cxx:291
ftag::MetadataAlg::finalize
StatusCode finalize() override
Definition: MetadataAlg.cxx:179
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
ADD
#define ADD(NAME, TYPE)
weights
Definition: herwig7_interface.h:38
xAOD::CutBookkeeper_v1::sumOfEventWeights
double sumOfEventWeights() const
Get the sum-of-event-weights that this CutBookkeeper has seen.
Definition: CutBookkeeper_v1.cxx:309
xAOD::CutBookkeeper_v1::name
const std::string & name() const
Get the name of this CutBookkeeper.
Definition: CutBookkeeper_v1.cxx:156
H5Utils::Consumers
Definition: Writer.h:129
DeMoScan.index
string index
Definition: DeMoScan.py:364
FileMetaData.h
xAOD::CutBookkeeper_v1::inputStream
const std::string & inputStream() const
Get the name of the input-file stream object that was seen by this CutBookkeeper.
Definition: CutBookkeeper_v1.cxx:220
OriginalAodCounts::sumOfWeights
double sumOfWeights
Definition: OriginalAodCounts.h:22
re
const boost::regex re(r_e)
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:146
ftag::MetadataAlg::MetadataAlg
MetadataAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: MetadataAlg.cxx:70
OriginalAodCounts
Definition: OriginalAodCounts.h:19
ftag
Definition: FlowSelectorAlg.cxx:12
pickleTool.object
object
Definition: pickleTool.py:30
ftag::MetadataAlg::execute
StatusCode execute() override
Definition: MetadataAlg.cxx:96
get_generator_info.error
error
Definition: get_generator_info.py:40
TruthMetaDataContainer.h
example.writer
writer
show summary of content
Definition: example.py:36
error
Definition: IImpactPoint3dEstimator.h:70
match
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition: hcg.cxx:356
ServiceHandle< IIncidentSvc >