9#include <boost/histogram.hpp>
19#include <boost/json.hpp>
28 static const std::string top_group =
"cutBookkeeper";
33 cons.add(#NAME,[](const OriginalAodCounts& c) {return c.NAME;})
34 ADD(nEventsProcessed);
36 ADD(sumOfWeightsSquared);
43#define ADD(NAME) {#NAME, counts.NAME}
44 ADD(nEventsProcessed),
46 ADD(sumOfWeightsSquared),
65 const std::string& name,
66 ISvcLocator* pSvcLocator):
74 return cbk.
name() ==
"AllExecutedEvents"
82 CHECK( incSvc.retrieve() );
84 incSvc->removeListener(
this, IncidentType::BeginInputFile );
85 incSvc->addListener(
this, IncidentType::BeginInputFile, 0,
true );
96 return StatusCode::SUCCESS;
101 return StatusCode::SUCCESS;
108 if (inc.type() != IncidentType::BeginInputFile)
return;
115 if (!
rc.isSuccess())
throw std::runtime_error(
116 "could not retrieve CutBookkeepers");
124 "Complete cbk name: " << cbk->
name() <<
131 maxCycle = cbk->
cycle();
135 if (allEvents ==
nullptr)
137 throw std::runtime_error(
138 "Could not find AllExecutedEvents CutBookkeeper information.");
145 static const std::regex
re(
"AllExecutedEvents.*_([0-9]+)");
160 std::string cbkName =
"CutBookkeepers_weight_" + std::to_string(
index);
168 throw std::runtime_error(
"could not retrieve " + cbkName);
185 ATH_MSG_WARNING(
"No CutBookkeeper weights collected; skipping output.");
186 return StatusCode::SUCCESS;
200 std::optional<H5::Group> h5_cbk;
202 h5_cbk = H5::Group(
m_output_svc->group()->createGroup(top_group));
204 std::optional<boost::json::object> json_cbk;
206 json_cbk = boost::json::object{};
211 const std::string sysname = sys.name().empty() ?
"nominal": sys.name();
216 H5::Group sysgroup(h5_cbk->createGroup(sysname));
220 boost::json::object& cbk_root = *json_cbk;
221 addCounts(cbk_root[sysname].emplace_object(),
weights);
226 boost::json::object jroot {
227 {top_group, *json_cbk}
229 out << jroot << std::endl;
233 namespace bh = boost::histogram;
236 using sys_map_t = std::map<size_t, std::string>;
238 std::vector<size_t> indices;
241 sys_map[idx] = sys.name().empty() ?
"nominal" : sys.name();
242 indices.push_back(idx);
246 using map_meta_t = std::pair<std::string, sys_map_t>;
247 using sys_ax_t = bh::axis::category<size_t, map_meta_t>;
248 const sys_ax_t ax(indices, map_meta_t{
"systematic", sys_map});
251 auto h_w = bh::make_weighted_histogram(ax);
254 using int64_storage = bh::dense_storage<int64_t>;
255 auto h_n = bh::make_histogram_with(int64_storage{}, ax);
258 for (
size_t idx : indices) {
260 const auto bin = ax.index(idx);
262 bh::accumulators::weighted_sum<double>(
263 w.sumOfWeights, w.sumOfWeightsSquared);
265 static_cast<int64_t
>(w.nEventsProcessed);
270 ->createGroup(
"cutBookkeeperHists"));
272 hist_grp, h_w,
"sumOfWeights");
274 hist_grp, h_n,
"nEventsProcessed");
277 return StatusCode::SUCCESS;
const boost::regex re(r_e)
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define CHECK(...)
Evaluate an expression and check for errors.
std::shared_ptr< HepMC3::Writer > writer
static const std::vector< std::string > systematics
size_t size() const
Number of registered mappings.
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Class to wrap a set of SystematicVariations.
std::string name() const
returns: the systematics joined into a single string.
double sumOfEventWeightsSquared() const
Get the sum-of-(event-weights-squared) that this CutBookkeeper has seen.
int cycle() const
Get the skimming cycle that this CutBookkeeper was running in.
const std::string & inputStream() const
Get the name of the input-file stream object that was seen by this CutBookkeeper.
double sumOfEventWeights() const
Get the sum-of-event-weights that this CutBookkeeper has seen.
const std::string & name() const
Get the name of this CutBookkeeper.
uint64_t nAcceptedEvents() const
Get the number of accepted events that this CutBookkeeper has seen.
bool match(std::string s1, std::string s2)
match the individual directories of two strings
void write_hist_to_group(H5::Group &group, const T &hist, const std::string &name)
CutBookkeeper_v1 CutBookkeeper
Define the latest version of the CutBookkeeper class.
CutBookkeeperContainer_v1 CutBookkeeperContainer
Define the latest version of the CutBookkeeperContainer class.