diff --git a/.vscode/launch.json b/.vscode/launch.json index 107c629..5e144c2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,9 +9,12 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/mana_auto_replay", - "args": ["~/Documents/is690-9Li_3H_run094_241020_124254_part001.zip"], + "args": [ + "~/Documents/is690-9Li_3H_run094_241020_124254_part001.zip", + "--plugin=src/libmana-plugin-c-test.so" + ], "stopAtEntry": false, - "cwd": "${fileDirname}", + "cwd": "${workspaceFolder}/build", "environment": [], "externalConsole": false, "MIMode": "gdb", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f1e0c46..fb34c23 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,7 +25,11 @@ if (MNODE_BUILD_TESTS) endif() +add_library(rxi-logc INTERFACE) +target_sources(rxi-logc INTERFACE internal/rxi/log.c) + add_library(mana-plugin-c-test SHARED mana_plugin_c_test.c) +target_link_libraries(mana-plugin-c-test PRIVATE rxi-logc) find_package(ROOT COMPONENTS Hist) if (ROOT_FOUND) diff --git a/src/internal/mana_lib.hpp b/src/internal/mana_lib.hpp index e73fef5..0d57806 100644 --- a/src/internal/mana_lib.hpp +++ b/src/internal/mana_lib.hpp @@ -141,7 +141,7 @@ struct ManaCPlugin: public ManaPlugin mana_plugin_t plugin_; void *context_ = nullptr; - ManaCPlugin(mana_plugin_t plugin) + explicit ManaCPlugin(mana_plugin_t plugin) : plugin_(plugin) { } diff --git a/src/mana_plugin_c_test.c b/src/mana_plugin_c_test.c index a4c9642..f22cceb 100644 --- a/src/mana_plugin_c_test.c +++ b/src/mana_plugin_c_test.c @@ -1,27 +1,47 @@ #include "internal/mana_api.h" +#include "internal/rxi/log.h" #include +#include struct Context { }; -struct Context ctx_; - MANA_DEFINE_PLUGIN_INIT(init) { - memset(&ctx_, 0, sizeof(ctx_)); - return &ctx_; + struct Context *ctx = calloc(1, sizeof(*ctx)); + log_debug("init: ctx=%p", ctx); + return ctx; } -MANA_DEFINE_PLUGIN_SHUTDOWN(shutdown) {} +MANA_DEFINE_PLUGIN_SHUTDOWN(shutdown) +{ + (void)context; + log_debug("shutdown"); +} -MANA_DEFINE_PLUGIN_BEGIN_RUN(begin_run) {} +MANA_DEFINE_PLUGIN_BEGIN_RUN(begin_run) +{ + log_debug("begin_run: ctx=%p, descriptor_json=%s", context, descriptor_json); + /* pretty useless as the json needs to be parsed back into some c structure describing the input + */ +} -MANA_DEFINE_PLUGIN_END_RUN(end_run) {} +MANA_DEFINE_PLUGIN_END_RUN(end_run) +{ + log_debug("end: ctx=%p, descriptor_json=%s", context, descriptor_json); +} -MANA_DEFINE_PLUGIN_EVENT_DATA(process_event) {} +MANA_DEFINE_PLUGIN_EVENT_DATA(process_event) +{ + log_trace("event: ctx=%p, eventIndex=%d, arrayCount=%zu, totalBytes=%zu", context, eventIndex, + arrayCount, totalBytes); +} -MANA_DEFINE_PLUGIN_SYSTEM_EVENT(process_system_event) {} +MANA_DEFINE_PLUGIN_SYSTEM_EVENT(process_system_event) +{ + log_debug("system_event: ctx=%p, size=%zu", context, size); +} MANA_DEFINE_GET_PLUGIN(mana_get_plugin) { diff --git a/src/tools/mana_auto_replay.cc b/src/tools/mana_auto_replay.cc index d220191..a8cc34e 100644 --- a/src/tools/mana_auto_replay.cc +++ b/src/tools/mana_auto_replay.cc @@ -31,6 +31,7 @@ // - create root trees or the new rntuples(?) // -> want plugins. similar to the mvme listfile_reader but on analysis data +#include #include // mnode::resources #include #include @@ -38,6 +39,7 @@ #include #include #include +#include "internal/argh.h" #include "internal/mana_arena.h" #include "internal/mana_lib.hpp" #include "internal/mana_analysis.h" @@ -157,21 +159,50 @@ int main(int argc, char *argv[]) auto f = cmrc::mnode::resources::get_filesystem().open("data/vme_module_data_sources.json"); const auto jModuleDataSources = nlohmann::json::parse(f.begin(), f.end()); - if (argc < 2) + argh::parser parser({"-h", "--help", "--plugin"}); + parser.parse(argv); + + auto filename = parser[1]; + + if (parser["-h"] || parser["--help"] || filename.empty()) { - std::cout << fmt::format("Usage: {} \n", argv[0]); - return 1; + std::cout << fmt::format("usage: {} [--plugin=] \n", argv[0]); + return 0; } - std::string filename = argv[1]; auto listfileContext = make_listfile_context(filename); if (!listfileContext) + { + std::cerr << fmt::format("Error: could not open {}\n", filename); return 1; + } + + std::unique_ptr manaPlugin; + boost::dll::shared_library pluginHandle; + + if (parser("--plugin")) + { + auto pluginFile = parser("--plugin").str(); + try + { + pluginHandle = boost::dll::shared_library(pluginFile); + manaPlugin = std::make_unique( + pluginHandle.get("mana_get_plugin")()); + } + catch (const std::exception &e) + { + std::cerr << fmt::format("Error loading plugin: {}\n", e.what()); + return 1; + } + } + else + { + manaPlugin = std::make_unique(); + } - mana::ManaCountingSink manaPlugin; auto mana = mana::make_module_data_stage(filename, mana::Arena(), listfileContext->crateConfig, - jModuleDataSources, &manaPlugin, nullptr); + jModuleDataSources, manaPlugin.get(), nullptr); auto event_data = [](void *ctx_, int crateIndex, int eventIndex, const mvlc::readout_parser::ModuleData *moduleDataList, @@ -235,18 +266,22 @@ int main(int argc, char *argv[]) mana.sink->end_run(mana.sinkContext, mana.runDescriptor.dump().c_str()); mana.sink->shutdown(mana.sinkContext); - spdlog::info( - "ManaCountingSink: eventCounts=[{}], totalBytes={}, systemEvents={}, systemEventBytes={}", - fmt::join(manaPlugin.eventCounts, ", "), manaPlugin.totalBytes, manaPlugin.systemEventCount, - manaPlugin.systemEventBytes); - - for (size_t ei = 0; ei < manaPlugin.eventArrayHits.size(); ++ei) + if (auto cp = dynamic_cast(manaPlugin.get())) { - spdlog::info("event[{}]: {} hits", ei, manaPlugin.eventCounts[ei]); - for (size_t ai = 0; ai < manaPlugin.eventArrayHits[ei].size(); ++ai) + + spdlog::info("ManaCountingSink: eventCounts=[{}], totalBytes={}, systemEvents={}, " + "systemEventBytes={}", + fmt::join(cp->eventCounts, ", "), cp->totalBytes, cp->systemEventCount, + cp->systemEventBytes); + + for (size_t ei = 0; ei < cp->eventArrayHits.size(); ++ei) { - auto name = mana.runDescriptor["events"][ei]["outputs"][ai]["name"]; - spdlog::info(" {}: {}", name, manaPlugin.eventArrayHits[ei][ai]); + spdlog::info("event[{}]: {} hits", ei, cp->eventCounts[ei]); + for (size_t ai = 0; ai < cp->eventArrayHits[ei].size(); ++ai) + { + auto name = mana.runDescriptor["events"][ei]["outputs"][ai]["name"]; + spdlog::info(" {}: {}", name, cp->eventArrayHits[ei][ai]); + } } }