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 %1 started", tid);
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(
89 0, "worker %1 event %2 has %3 digitized hits", tid, evn,
90 eventData->getDataCollectionMap().at("ctof")->getDigitizedData().size());
91
92 localRunData.emplace_back(std::move(eventData));
93 ++localCount;
94 }
95
96 // Lock only while moving selected events into the shared output container.
97 {
98 std::scoped_lock lk(collectorMtx);
99 for (auto& evt : localRunData) {
100 if (collected.size() >= 2) break;
101 collected.emplace_back(std::move(evt));
102 }
103 localRunData.clear();
104 }
105
106 log->info(0, "worker %1 processed %2 events", tid, localCount);
107 });
108 }
109
110 // pool destructor joins all threads.
111 return collected;
112}
113
128int main(int argc, char* argv[]) {
129 // Create GOptions using gdynamicdigitization::defineOptions(), which aggregates options
130 // from this module and its dependencies.
131 auto gopts = std::make_shared<GOptions>(argc, argv, gdynamicdigitization::defineOptions());
132
133 // Example-level logger.
134 auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, PLUGIN_LOGGER);
135
136 constexpr int nevents = 10;
137 constexpr int nthreads = 8;
138
139 auto dynamicRoutinesMap = gdynamicdigitization::dynamicRoutinesMap({plugin_name}, gopts);
140
141 if (dynamicRoutinesMap->at(plugin_name)->loadConstants(1, "default") == false) {
142 log->error(
143 1, "Failed to load constants for dynamic routine %1 for run number 1 with variation 'default'.",
145 }
146
147 auto runData = run_simulation_in_threads(nevents, nthreads, gopts, log, dynamicRoutinesMap);
148
149 // Print the collected events (not all processed events are collected in this example).
150 for (size_t i = 0; i < runData.size(); i++) {
151 log->info(" > Event %1 collected with local event number: %2", i + 1, runData[i]->getEventNumber());
152 }
153
154 return EXIT_SUCCESS;
155}
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
const std::string plugin_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.
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[])