17#include "G4UImanager.hh"
26 const std::shared_ptr<const gdynamicdigitization::dRoutinesMap>& gdynamicDigitizationMap)
29 string filename = gopt->getScalarString(
"run_weights");
30 userRunno = gopt->getScalarInt(
"run");
31 neventsToProcess = gopt->getScalarInt(
"n");
34 if (neventsToProcess == 0)
return;
38 runEvents[userRunno] = neventsToProcess;
43 ifstream in(filename.c_str());
47 "Error: can't open run weights input file >", filename,
"<. Check your spelling. Exiting.");
50 log->
info(1,
"Loading run weights from ", filename);
56 while (in >> run >> weight) {
57 listOfRuns.push_back(run);
58 runWeights[run] = weight;
63 distributeEvents(neventsToProcess);
68 log->
info(0,
"EventDispenser initialized with ", neventsToProcess,
" events distributed among ",
69 runWeights.size(),
" runs:");
70 log->
info(0,
" run\t weight\t n. events");
71 for (
const auto& weight : runWeights) {
72 log->
info(0,
" ", weight.first,
"\t ", weight.second,
"\t ", runEvents[weight.first]);
82 runEvents[userRunno] = nevents_to_process;
88void EventDispenser::distributeEvents(
int nevents_to_process) {
90 random_device randomDevice;
91 mt19937 generator(randomDevice());
92 uniform_real_distribution<> randomDistribution(0, 1);
96 for (
int i = 0; i < nevents_to_process; i++) {
97 double randomNumber = randomDistribution(generator);
99 double cumulativeWeight = 0;
100 for (
const auto& weight : runWeights) {
101 cumulativeWeight += weight.second;
102 if (randomNumber <= cumulativeWeight) {
103 runEvents[weight.first]++;
115 for (
auto rEvents : runEvents) { totalEvents += rEvents.second; }
126 G4UImanager* g4uim = G4UImanager::GetUIpointer();
129 for (
auto& run : runEvents) {
130 int runNumber = run.first;
131 int nevents = run.second;
134 if (runNumber != currentRunno) {
137 for (
const auto& [plugin, digiRoutine] : *gDigitizationMap) {
139 if (digiRoutine->loadConstants(runNumber, variation) ==
false) {
141 "Failed to load constants for ", plugin,
" for run ", runNumber,
" with variation ",
146 if (digiRoutine->loadTT(runNumber, variation) ==
false) {
148 "Failed to load translation table for ", plugin,
" for run ", runNumber,
149 " with variation ", variation);
152 currentRunno = runNumber;
155 log->
info(1,
"Starting run ", runNumber,
" with ", nevents,
" events.");
159 log->
info(1,
"Processing ", nevents,
" events in one go");
160 g4uim->ApplyCommand(
"/run/beamOn " +
to_string(nevents));
162 log->
info(1,
"Run ", runNumber,
" done with ", nevents,
" events");
int processEvents()
Processes all runs by initializing digitization routines and dispatching events.
EventDispenser(const std::shared_ptr< GOptions > &gopt, const std::shared_ptr< const gdynamicdigitization::dRoutinesMap > &gdynamicDigitizationMap)
Constructs an EventDispenser and prepares the run event distribution.
void setNumberOfEvents(int nevts)
Sets the total number of events to process in single-run mode.
int getTotalNumberOfEvents() const
Computes the total number of events across all runs.
std::shared_ptr< GLogger > log
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
Event Dispenser module error-code conventions.
#define ERR_EVENTDISTRIBUTIONFILENOTFOUND
Run-weight file could not be opened/read.
Declares the EventDispenser class.
Public declaration of the Event Dispenser module command-line / configuration options.
constexpr const char * EVENTDISPENSER_LOGGER
Logger name used by this module when creating a GLogger through the base infrastructure.
constexpr int ERR_LOADCONSTANTFAIL
constexpr int ERR_LOADTTFAIL
#define UNINITIALIZEDSTRINGQUANTITY
constexpr const char * to_string(randomModel m) noexcept