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),
60 static const std::vector<std::string> allowed_streams{
61 "StreamAOD",
"StreamEVGEN",
"StreamEVNT"};
63 const auto&
s = allowed_streams;
64 return cbk.
name() ==
"AllExecutedEvents"
73 const std::string& name,
74 ISvcLocator* pSvcLocator):
84 CHECK( incSvc.retrieve() );
86 incSvc->removeListener(
this, IncidentType::BeginInputFile );
87 incSvc->addListener(
this, IncidentType::BeginInputFile, 0,
true );
98 return StatusCode::SUCCESS;
103 return StatusCode::SUCCESS;
110 if (inc.type() != IncidentType::BeginInputFile)
return;
117 if (!
rc.isSuccess())
throw std::runtime_error(
118 "could not retrieve CutBookkeepers");
126 "Complete cbk name: " << cbk->
name() <<
130 if (cbk->
cycle() > maxCycle && isGoodBook(*cbk))
133 maxCycle = cbk->
cycle();
137 if (allEvents ==
nullptr)
140 "Could not find AllExecutedEvents CutBookkeeper information.");
141 throw std::runtime_error(
error);
146 if (cbk->
cycle() == maxCycle && isGoodBook(*cbk))
148 static const std::regex
re(
"AllExecutedEvents.*_([0-9]+)");
163 std::string cbkName =
"CutBookkeepers_weight_" + std::to_string(
index);
171 throw std::runtime_error(
"could not retrieve " + cbkName);
175 if (cbk->
cycle() == maxCycle && isGoodBook(*cbk))
198 std::optional<H5::Group> h5_cbk;
200 h5_cbk = H5::Group(
m_output_svc->group()->createGroup(top_group));
202 std::optional<boost::json::object> json_cbk;
204 json_cbk = boost::json::object{};
209 const std::string sysname = sys.name().empty() ?
"nominal": sys.name();
214 H5::Group sysgroup(h5_cbk->createGroup(sysname));
218 boost::json::object& cbk_root = *json_cbk;
219 addCounts(cbk_root[sysname].emplace_object(),
weights);
224 boost::json::object jroot {
225 {top_group, *json_cbk}
227 out << jroot << std::endl;
231 namespace bh = boost::histogram;
234 using sys_map_t = std::map<size_t, std::string>;
236 std::vector<size_t> indices;
239 sys_map[idx] = sys.name().empty() ?
"nominal" : sys.name();
240 indices.push_back(idx);
244 using map_meta_t = std::pair<std::string, sys_map_t>;
245 using sys_ax_t = bh::axis::category<size_t, map_meta_t>;
246 const sys_ax_t ax(indices, map_meta_t{
"systematic", sys_map});
249 auto h_w = bh::make_weighted_histogram(ax);
252 using int64_storage = bh::dense_storage<int64_t>;
253 auto h_n = bh::make_histogram_with(int64_storage{}, ax);
256 for (
size_t idx : indices) {
258 const auto bin = ax.index(idx);
260 bh::accumulators::weighted_sum<double>(
261 w.sumOfWeights, w.sumOfWeightsSquared);
263 static_cast<int64_t
>(w.nEventsProcessed);
268 ->createGroup(
"cutBookkeeperHists"));
270 hist_grp, h_w,
"sumOfWeights");
272 hist_grp, h_n,
"nEventsProcessed");
275 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 CHECK(...)
Evaluate an expression and check for errors.
std::shared_ptr< HepMC3::Writer > writer
static const std::vector< std::string > systematics
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.