gdynamicDigitization
Loading...
Searching...
No Matches
plugin_load_example.cc
Go to the documentation of this file.
1
14
15// gdynamic
18
19// gemc
20#include "gfactory.h"
22#include "gthreads.h"
23
24const std::string plugin_name = "test_gdynamic_plugin";
25
49 int nthreads,
50 const std::shared_ptr<GOptions>& gopt,
51 const std::shared_ptr<GLogger>& log,
52 const std::shared_ptr<const gdynamicdigitization::dRoutinesMap>& dynamicRoutinesMap)
53 -> std::vector<std::unique_ptr<GEventDataCollection>> {
54 std::mutex collectorMtx;
55 std::vector<std::unique_ptr<GEventDataCollection>> collected;
56
57 // Thread-safe event counter starts at 1; fetch_add returns the old value and increments.
58 std::atomic<int> next{1};
59
60 // Pool of threads. jthread_alias joins in its destructor.
61 std::vector<jthread_alias> pool;
62 pool.reserve(nthreads);
63
64 for (int tid = 0; tid < nthreads; ++tid) {
65 pool.emplace_back([&, tid] {
66 log->info(0, "worker ", tid, " started");
67
68 int localCount = 0; // events processed by this worker
69 thread_local std::vector<std::unique_ptr<GEventDataCollection>> localRunData;
70
71 while (true) {
72 int evn = next.fetch_add(1, std::memory_order_relaxed);
73 if (evn > nevents) break;
74
75 auto gevent_header = GEventHeader::create(gopt);
76 auto eventData = std::make_unique<GEventDataCollection>(gopt, std::move(gevent_header));
77
78 // Each event has 2 hits in this example.
79 for (unsigned i = 1; i < 3; i++) {
80 auto hit = GHit::create(gopt);
81 auto true_data = dynamicRoutinesMap->at(plugin_name)->collectTrueInformation(hit, i);
82 auto digi_data = dynamicRoutinesMap->at(plugin_name)->digitizeHit(hit, i);
83
84 eventData->addDetectorDigitizedData("ctof", std::move(digi_data));
85 eventData->addDetectorTrueInfoData("ctof", std::move(true_data));
86 }
87
88 log->info(0, "worker ", tid, " event ", evn, " has ",
89 eventData->getDataCollectionMap().at("ctof")->getDigitizedData().size(), " digitized hits");
90
91 localRunData.emplace_back(std::move(eventData));
92 ++localCount;
93 }
94
95 // Lock only while moving selected events into the shared output container.
96 {
97 std::scoped_lock lk(collectorMtx);
98 for (auto& evt : localRunData) {
99 if (collected.size() >= 2) break;
100 collected.emplace_back(std::move(evt));
101 }
102 localRunData.clear();
103 }
104
105 log->info(0, "worker ", tid, " processed ", localCount, " events");
106 });
107 }
108
109 // pool destructor joins all threads.
110 return collected;
111}
112
127int main(int argc, char* argv[]) {
128 // Create GOptions using gdynamicdigitization::defineOptions(), which aggregates options
129 // from this module and its dependencies.
130 auto gopts = std::make_shared<GOptions>(argc, argv, gdynamicdigitization::defineOptions());
131
132 // Example-level logger.
133 auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, PLUGIN_LOGGER);
134
135 constexpr int nevents = 10;
136 constexpr int nthreads = 8;
137
138 auto dynamicRoutinesMap = gdynamicdigitization::dynamicRoutinesMap({plugin_name}, gopts);
139
140 if (dynamicRoutinesMap->at(plugin_name)->loadConstants(1, "default") == false) {
141 log->error(1, "Failed to load constants for dynamic routine ", plugin_name,
142 " for run number 1 with variation 'default'.");
143 }
144
145 auto runData = run_simulation_in_threads(nevents, nthreads, gopts, log, dynamicRoutinesMap);
146
147 // Print the collected events (not all processed events are collected in this example).
148 for (size_t i = 0; i < runData.size(); i++) {
149 log->info(" > Event ", i + 1, " collected with local event number: ", runData[i]->getEventNumber());
150 }
151
152 return EXIT_SUCCESS;
153}
static std::unique_ptr< GEventHeader > create(const std::shared_ptr< GOptions > &gopts, int tid=-1)
static GHit * create(const std::shared_ptr< GOptions > &gopts)
Options for the GDynamicDigitization module.
constexpr const char * PLUGIN_LOGGER
#define SFUNCTION_NAME
GOptions defineOptions()
Builds and returns the option set for gdynamic digitization.
std::shared_ptr< const dRoutinesMap > dynamicRoutinesMap(const std::vector< std::string > &plugin_names, const std::shared_ptr< GOptions > &gopts)
Loads multiple dynamic routines and returns an immutable shared map.
const std::string plugin_name
auto run_simulation_in_threads(int nevents, int nthreads, const std::shared_ptr< GOptions > &gopt, const std::shared_ptr< GLogger > &log, const std::shared_ptr< const gdynamicdigitization::dRoutinesMap > &dynamicRoutinesMap) -> std::vector< std::unique_ptr< GEventDataCollection > >
Runs a small simulated event loop using multiple worker threads.
int main(int argc, char *argv[])