61 const std::shared_ptr<GOptions>& gopts,
62 const std::shared_ptr<GLogger>& log,
63 const std::shared_ptr<const GDetectorConstruction>&
gdetector) -> std::vector<std::shared_ptr<GEventDataCollection>> {
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 ", tid,
" started");
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 ", sdname,
" loadConstants for run ", runNumber);
109 if (digi_routine->loadConstants(runNumber, variation) ==
false) {
110 log->error(
ERR_LOADCONSTANTFAIL,
"Failed to load constants for ", sdname,
" for run ", runNumber,
" with variation ", variation);
113 log->debug(
NORMAL,
"Calling ", sdname,
" loadTT for run ", runNumber);
114 if (digi_routine->loadTT(runNumber, variation) ==
false) {
115 log->error(
ERR_LOADTTFAIL,
"Failed to load translation table for ", sdname,
" for run ", runNumber,
" with variation ", variation);
118 for (
unsigned i = 1; i < 11; i++) {
120 auto true_data = digi_routine->collectTrueInformation(hit, i);
121 auto digi_data = digi_routine->digitizeHit(hit, i);
123 eventData->addDetectorDigitizedData(
"flux", std::move(digi_data));
124 eventData->addDetectorTrueInfoData(
"flux", std::move(true_data));
127 const auto& flux_data_it = eventData->getDataCollectionMap().find(
"flux");
130 if (flux_data_it != eventData->getDataCollectionMap().end()) {
131 const auto& digitized_data = flux_data_it->second->getDigitizedData();
132 log->info(0,
"worker ", tid,
" event ", evn,
" has ", digitized_data.size(),
" digitized hits");
143 std::scoped_lock lk(collectorMtx);
144 for (
auto& evt : localRunData) { collected.emplace_back(evt); }
145 localRunData.clear();
149 log->info(0,
"worker ", tid,
" processed ", localCount,
" events");
170int main(
int argc,
char* argv[]) {
177 constexpr int nevents = 20;
178 constexpr int nthreads = 2;
180 auto runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
181 auto physicsList =
new QBBC;
183 runManager->SetUserInitialization(physicsList);
185 auto gdetector = std::make_shared<GDetectorConstruction>(gopts);
auto run_simulation_in_threads(int nevents, int nthreads, const std::shared_ptr< GOptions > &gopts, const std::shared_ptr< GLogger > &log, const std::shared_ptr< const GDetectorConstruction > &gdetector) -> std::vector< std::shared_ptr< GEventDataCollection > >
Runs a digitization loop across multiple worker threads and collects event data.