61 const std::shared_ptr<GOptions>& gopts,
62 const std::shared_ptr<GLogger>& log,
64 std::mutex collectorMtx;
65 std::vector<std::shared_ptr<GEventDataCollection>> collected;
70 std::atomic<int> next{1};
76 std::vector<jthread_alias> pool;
78 pool.reserve(nthreads);
80 for (
int tid = 0; tid < nthreads; ++tid) {
82 pool.emplace_back([&, tid]
84 log->info(0,
"worker %1 started", tid);
90 thread_local std::vector<std::shared_ptr<GEventDataCollection>> localRunData;
96 int evn = next.fetch_add(1, std::memory_order_relaxed);
97 if (evn > nevents)
break;
100 std::string sdname =
"flux";
102 std::string variation =
"default";
105 auto eventData = std::make_shared<GEventDataCollection>(gopts, std::move(gevent_header));
106 auto digi_routine =
gdetector->get_digitization_routines_for_sdname(sdname);
107 log->debug(
NORMAL,
"Calling %1 loadConstants for run %2", sdname, runNumber);
109 if (digi_routine->loadConstants(runNumber, variation) ==
false) {
112 sdname, runNumber, variation);
115 log->debug(
NORMAL,
"Calling %1 loadTT for run %2", sdname, runNumber);
116 if (digi_routine->loadTT(runNumber, variation) ==
false) {
118 ERR_LOADTTFAIL,
"Failed to load translation table for %1 for run %2 with variation %3",
119 sdname, runNumber, variation);
122 for (
unsigned i = 1; i < 11; i++) {
124 auto true_data = digi_routine->collectTrueInformation(hit, i);
125 auto digi_data = digi_routine->digitizeHit(hit, i);
127 eventData->addDetectorDigitizedData(
"flux", std::move(digi_data));
128 eventData->addDetectorTrueInfoData(
"flux", std::move(true_data));
131 const auto& flux_data_it = eventData->getDataCollectionMap().find(
"flux");
134 if (flux_data_it != eventData->getDataCollectionMap().end()) {
135 const auto& digitized_data = flux_data_it->second->getDigitizedData();
136 log->info(0,
"worker %1 event %2 has %3 digitized hits", tid, evn, digitized_data.size());
147 std::scoped_lock lk(collectorMtx);
148 for (
auto& evt : localRunData) { collected.emplace_back(evt); }
149 localRunData.clear();
153 log->info(0,
"worker %1 processed %2 events", tid, localCount);
174int main(
int argc,
char* argv[]) {
181 constexpr int nevents = 20;
182 constexpr int nthreads = 2;
184 auto runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
185 auto physicsList =
new QBBC;
187 runManager->SetUserInitialization(physicsList);
190 runManager->SetUserInitialization(
gdetector);
auto run_simulation_in_threads(int nevents, int nthreads, const std::shared_ptr< GOptions > &gopts, const std::shared_ptr< GLogger > &log, const GDetectorConstruction *gdetector) -> std::vector< std::shared_ptr< GEventDataCollection > >
Runs a digitization loop across multiple worker threads and collects event data.