From 068cf93604d5866000e16b44e5ab50ea13df64a0 Mon Sep 17 00:00:00 2001 From: Lars Bogner Date: Thu, 9 Jul 2026 10:04:47 +0200 Subject: [PATCH] Allow MINICALOSIM_SEED env var to override RNG seed in run_pbwo4/run_sampling Concurrent job launches can land in the same wall-clock second, causing G4System::init's time-based seed (seed=-1) to collide and produce byte-identical physics across separately-named shards. Falls back to the existing time-based seed if the variable is unset, empty, or invalid. Co-Authored-By: Claude Sonnet 5 --- run_pbwo4.cc | 15 ++++++++++++++- run_sampling.cc | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/run_pbwo4.cc b/run_pbwo4.cc index 1754eb1..22e3895 100644 --- a/run_pbwo4.cc +++ b/run_pbwo4.cc @@ -1,6 +1,7 @@ #include "GeometryDescriptor.hh" #include "G4System.hh" +#include #include #include #include @@ -8,6 +9,18 @@ int main(int argc, char** argv) { int nEvents = 10; + int seed = -1; + if (const char* seedEnv = std::getenv("MINICALOSIM_SEED")) { + try { + std::size_t pos = 0; + std::string seedStr(seedEnv); + int parsed = std::stoi(seedStr, &pos); + if (pos == seedStr.size()) seed = parsed; + } catch (const std::exception&) { + // Not a valid integer; fall back to time-based seed. + } + } + if (argc > 2) { std::cerr << "Usage: run_pbwo4 [nEvents]" << std::endl; return 1; @@ -28,7 +41,7 @@ int main(int argc, char** argv) { gd.addLayer(20.0, "G4_PbWO4", true, 10, 10); G4System g4; - g4.init(gd, -1); + g4.init(gd, seed); g4.run_batch(nEvents, {"e-"}, 1.0, 1.0, outfile); std::cout << "Saved " << nEvents << " events to " << outfile << std::endl; diff --git a/run_sampling.cc b/run_sampling.cc index 1f329c6..b2da2b1 100644 --- a/run_sampling.cc +++ b/run_sampling.cc @@ -1,6 +1,7 @@ #include "GeometryDescriptor.hh" #include "G4System.hh" +#include #include #include #include @@ -111,12 +112,24 @@ int main(int argc, char** argv) { return 1; } + int seed = -1; + if (const char* seedEnv = std::getenv("MINICALOSIM_SEED")) { + try { + std::size_t pos = 0; + std::string seedStr(seedEnv); + int parsed = std::stoi(seedStr, &pos); + if (pos == seedStr.size()) seed = parsed; + } catch (const std::exception&) { + // Not a valid integer; fall back to time-based seed. + } + } + GeometryDescriptor gd = builder(); std::string outfile = "sampling_" + configName + "_" + std::to_string(nEvents) + "events_hits.root"; G4System g4; - g4.init(gd, -1); + g4.init(gd, seed); g4.run_batch(nEvents, {"e-"}, 1.0, 1.0, outfile); std::cout << "Saved " << nEvents << " events to " << outfile << std::endl;