implement some rntuple thing. no idea what the output file actually contains
This commit is contained in:
parent
0ef89eac14
commit
fe73397582
1 changed files with 43 additions and 30 deletions
|
@ -3,6 +3,7 @@
|
||||||
#include <ROOT/RNTupleWriter.hxx>
|
#include <ROOT/RNTupleWriter.hxx>
|
||||||
#include <ROOT/RVec.hxx>
|
#include <ROOT/RVec.hxx>
|
||||||
#include <TFile.h>
|
#include <TFile.h>
|
||||||
|
#include <algorithm>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
@ -25,9 +26,8 @@ using RNTupleWriter = ROOT::Experimental::RNTupleWriter;
|
||||||
|
|
||||||
struct Context
|
struct Context
|
||||||
{
|
{
|
||||||
std::unique_ptr<TFile> outputFile;
|
std::vector<std::shared_ptr<std::vector<float>>> rvecs;
|
||||||
std::unique_ptr<RNTupleModel> model;
|
std::unique_ptr<RNTupleWriter> writer;
|
||||||
std::vector<std::vector<std::shared_ptr<ROOT::RVecF>>> eventRVecs;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static Context *g_ctx = nullptr;
|
static Context *g_ctx = nullptr;
|
||||||
|
@ -56,48 +56,48 @@ MANA_DEFINE_PLUGIN_SHUTDOWN(shutdown)
|
||||||
g_ctx = nullptr;
|
g_ctx = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void make_things(const nlohmann::json &jRun)
|
|
||||||
{
|
|
||||||
std::vector<std::vector<std::shared_ptr<ROOT::RVecF>>> result;
|
|
||||||
for (const auto &jEvent: jRun["events"])
|
|
||||||
{
|
|
||||||
auto dir = gDirectory->mkdir(jEvent["name"].get<std::string>().c_str(), "", true);
|
|
||||||
auto model = RNTupleModel::Create();
|
|
||||||
std::vector<std::shared_ptr<ROOT::RVecF>> rvecs;
|
|
||||||
for (const auto &jOutput: jEvent["outputs"])
|
|
||||||
{
|
|
||||||
auto rvec = model->MakeField<ROOT::RVecF>(jOutput["name"].get<std::string>());
|
|
||||||
rvec->resize(jOutput["size"].get<size_t>());
|
|
||||||
rvecs.emplace_back(std::move(rvec));
|
|
||||||
}
|
|
||||||
result.emplace_back(std::move(rvecs));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MANA_DEFINE_PLUGIN_BEGIN_RUN(begin_run)
|
MANA_DEFINE_PLUGIN_BEGIN_RUN(begin_run)
|
||||||
{
|
{
|
||||||
|
assert(context);
|
||||||
|
assert(context == g_ctx);
|
||||||
log_debug("begin_run: context=%p, descriptor_json=%s", context, descriptor_json);
|
log_debug("begin_run: context=%p, descriptor_json=%s", context, descriptor_json);
|
||||||
auto jRun = nlohmann::json::parse(descriptor_json);
|
auto jRun = nlohmann::json::parse(descriptor_json);
|
||||||
|
|
||||||
|
if (jRun["events"].size() > 1)
|
||||||
|
{
|
||||||
|
log_error("root-rntuple-writer: currently only supports a single event/trigger");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ctx = reinterpret_cast<Context *>(context);
|
||||||
|
ctx->rvecs.clear();
|
||||||
|
auto jEvent = jRun["events"][0];
|
||||||
|
auto model = RNTupleModel::Create();
|
||||||
|
for (const auto &jOutput: jEvent["outputs"])
|
||||||
|
{
|
||||||
|
auto name = jOutput["name"].get<std::string>();
|
||||||
|
std::replace(std::begin(name), std::end(name), '.', '_');
|
||||||
|
auto rvec = model->MakeField<std::vector<float>>(name);
|
||||||
|
rvec->resize(jOutput["size"].get<size_t>());
|
||||||
|
ctx->rvecs.emplace_back(std::move(rvec));
|
||||||
|
}
|
||||||
|
|
||||||
std::filesystem::path rp(jRun["name"].get<std::string>());
|
std::filesystem::path rp(jRun["name"].get<std::string>());
|
||||||
auto filename =
|
auto filename =
|
||||||
fmt::format("{}_rntuple_events.root", rp.filename().replace_extension().string());
|
fmt::format("{}_rntuple_events.root", rp.filename().replace_extension().string());
|
||||||
auto ctx = reinterpret_cast<Context *>(context);
|
|
||||||
ctx->outputFile = std::make_unique<TFile>(filename.c_str(), "RECREATE");
|
|
||||||
ctx->outputFile->cd();
|
|
||||||
|
|
||||||
make_things(jRun);
|
|
||||||
|
|
||||||
ctx->model = ROOT::Experimental::RNTupleModel::Create();
|
|
||||||
auto field = ctx->model->MakeField<float>("value");
|
|
||||||
|
|
||||||
log_info("writing rntuple into: %s", filename.c_str());
|
log_info("writing rntuple into: %s", filename.c_str());
|
||||||
|
|
||||||
|
ctx->writer =
|
||||||
|
RNTupleWriter::Recreate(std::move(model), jEvent["name"].get<std::string>(), filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
MANA_DEFINE_PLUGIN_END_RUN(end_run)
|
MANA_DEFINE_PLUGIN_END_RUN(end_run)
|
||||||
{
|
{
|
||||||
|
assert(context);
|
||||||
|
assert(context == g_ctx);
|
||||||
log_debug("end: context=%p, descriptor_json=%s", context, descriptor_json);
|
log_debug("end: context=%p, descriptor_json=%s", context, descriptor_json);
|
||||||
auto ctx = reinterpret_cast<Context *>(context);
|
auto ctx = reinterpret_cast<Context *>(context);
|
||||||
ctx->outputFile->Write();
|
|
||||||
*ctx = {};
|
*ctx = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,7 +105,20 @@ MANA_DEFINE_PLUGIN_EVENT_DATA(process_event)
|
||||||
{
|
{
|
||||||
log_trace("event: ctx=%p, eventIndex=%d, arrayCount=%zu, totalBytes=%zu", context, eventIndex,
|
log_trace("event: ctx=%p, eventIndex=%d, arrayCount=%zu, totalBytes=%zu", context, eventIndex,
|
||||||
arrayCount, totalBytes);
|
arrayCount, totalBytes);
|
||||||
|
if (eventIndex != 0)
|
||||||
|
return;
|
||||||
|
|
||||||
auto ctx = reinterpret_cast<Context *>(context);
|
auto ctx = reinterpret_cast<Context *>(context);
|
||||||
|
|
||||||
|
for (size_t ai = 0; ai < arrayCount; ++ai)
|
||||||
|
{
|
||||||
|
auto &rvec = ctx->rvecs.at(ai);
|
||||||
|
auto input = mana::get_span<float>(arrays[ai]);
|
||||||
|
|
||||||
|
rvec->resize(input.size());
|
||||||
|
std::copy(std::begin(input), std::end(input), std::begin(*rvec));
|
||||||
|
}
|
||||||
|
ctx->writer->Fill();
|
||||||
}
|
}
|
||||||
|
|
||||||
MANA_DEFINE_PLUGIN_SYSTEM_EVENT(process_system_event)
|
MANA_DEFINE_PLUGIN_SYSTEM_EVENT(process_system_event)
|
||||||
|
|
Loading…
Reference in a new issue