mesytec-mnode/src/mana_plugin_root_histogram.cc

98 lines
2.6 KiB
C++
Raw Normal View History

#include <TFile.h>
#include <TH1.h>
#include "internal/mana_lib.hpp"
extern "C"
{
#include "internal/rxi/log.h"
}
using namespace mesytec::mnode;
struct Context
{
std::unique_ptr<TFile> outputFile;
std::vector<std::vector<TH1 *>> histograms;
};
MANA_DEFINE_PLUGIN_INIT(init)
{
log_set_level(LOG_DEBUG);
static Context g_ctx;
log_debug("init: ctx=%p", &g_ctx);
return &g_ctx;
}
MANA_DEFINE_PLUGIN_SHUTDOWN(shutdown)
{
(void)context;
log_debug("shutdown");
}
MANA_DEFINE_PLUGIN_BEGIN_RUN(begin_run)
{
log_debug("begin_run: context=%p, descriptor_json=%s", context, descriptor_json);
auto jRun = nlohmann::json::parse(descriptor_json);
auto ctx = reinterpret_cast<Context *>(context);
ctx->histograms.clear();
ctx->outputFile = std::make_unique<TFile>("output.root", "RECREATE");
for (const auto &jEvent: jRun["events"])
{
std::vector<TH1 *> eventHistograms;
for (const auto &jOutput: jEvent["outputs"])
{
auto name = jOutput["name"].get<std::string>();
auto size = jOutput["size"].get<size_t>();
auto th1 = new TH1F(name.c_str(), name.c_str(), size, 0.0, size);
eventHistograms.emplace_back(std::move(th1));
}
ctx->histograms.emplace_back(std::move(eventHistograms));
}
}
MANA_DEFINE_PLUGIN_END_RUN(end_run)
{
log_debug("end: context=%p, descriptor_json=%s", context, descriptor_json);
auto ctx = reinterpret_cast<Context *>(context);
ctx->outputFile->Write();
*ctx = {};
}
MANA_DEFINE_PLUGIN_EVENT_DATA(process_event)
{
log_trace("event: ctx=%p, eventIndex=%d, arrayCount=%zu, totalBytes=%zu", context, eventIndex,
arrayCount, totalBytes);
auto ctx = reinterpret_cast<Context *>(context);
auto &eventHistograms = ctx->histograms.at(eventIndex);
for (size_t ai = 0; ai < arrayCount; ++ai)
{
auto &histogram = eventHistograms.at(ai);
auto input = mana::get_span<float>(arrays[ai]);
for (size_t i = 0; i < input.size(); ++i)
{
if (!std::isnan(input[i]))
{
histogram->Fill(i);
}
}
}
}
MANA_DEFINE_PLUGIN_SYSTEM_EVENT(process_system_event)
{
log_trace("system_event: ctx=%p, size=%zu", context, size);
}
extern "C" MANA_DEFINE_GET_PLUGIN(mana_get_plugin)
{
mana_plugin_t plugin;
plugin.init = init;
plugin.shutdown = shutdown;
plugin.begin_run = begin_run;
plugin.end_run = end_run;
plugin.process_event = process_event;
plugin.process_system_event = process_system_event;
return plugin;
}