9#include "G4Threading.hh"
10#include "G4UImanager.hh"
11#include "G4UnitsTable.hh"
23 int get_nthreads(
const std::shared_ptr<GOptions>& gopts,
const std::shared_ptr<GLogger>& log) {
24 int useThreads = gopts->getScalarInt(
"nthreads");
27 int ncores = G4Threading::G4GetNumberOfCores();
32 if (useThreads == 0 || useThreads > ncores) useThreads = ncores;
34 log->info(0,
"Using ", useThreads,
" threads out of ", ncores,
" available cores.");
39 std::vector<std::string>
verbosity_commands([[maybe_unused]]
const std::shared_ptr<GOptions>& gopts,
40 [[maybe_unused]]
const std::shared_ptr<GLogger>& log) {
41 std::vector<std::string> cmds;
45 cmds.emplace_back(
"/control/verbose 0");
46 cmds.emplace_back(
"/hit/verbose 0");
48 cmds.emplace_back(
"/process/verbose 0");
49 cmds.emplace_back(
"/process/setVerbose 0 all");
50 cmds.emplace_back(
"/process/had/verbose 0");
51 cmds.emplace_back(
"/process/had/deex/verbose 0");
52 cmds.emplace_back(
"/process/had/cascade 0");
53 cmds.emplace_back(
"/process/em/verbose 0");
54 cmds.emplace_back(
"/process/eLoss/verbose 0");
56 cmds.emplace_back(
"/tracking/verbose 0");
57 cmds.emplace_back(
"/geometry/navigator/verbose 0");
59 cmds.emplace_back(
"/event/verbose 0");
60 cmds.emplace_back(
"/event/stack/verbose 0");
62 cmds.emplace_back(
"/cuts/verbose 0");
64 cmds.emplace_back(
"/run/particle/verbose 0");
65 cmds.emplace_back(
"/run/verbose 0");
67 cmds.emplace_back(
"/material/verbose 0");
69 cmds.emplace_back(
"/vis/verbose 0");
70 cmds.emplace_back(
"/particle/verbose 0");
79 [[maybe_unused]]
const std::shared_ptr<GLogger>& log,
80 bool configure_visualization) {
82 auto check_overlaps = gopts->getScalarInt(
"check_overlaps");
83 auto gui = gopts->getSwitch(
"gui");
86 std::vector<std::string> cmds;
90 if (check_overlaps == 2) {
91 log->info(0,
"Running /geometry/test/run with 50 points.");
92 cmds.emplace_back(
"/geometry/test/resolution 50");
93 cmds.emplace_back(
"/geometry/test/run");
95 else if (check_overlaps >= 100) {
96 log->info(0,
"Running /geometry/test/run with ", check_overlaps,
" points.");
97 cmds.emplace_back(
"/geometry/test/resolution " + std::to_string(check_overlaps));
98 cmds.emplace_back(
"/geometry/test/run");
104 if (gui && !configure_visualization) {
return cmds; }
109 cmds.emplace_back(
"/run/initialize");
111 if (!configure_visualization)
return cmds;
115 if (!gui && g4view.driver !=
"TOOLSSG_OFFSCREEN")
return cmds;
118 if (g4view.driver !=
"TOOLSSG_OFFSCREEN") {
121 const double toDegrees = 180.0 / M_PI;
126 if (lightThetaValue == 0.0 && lightPhiValue == 0.0) {
127 lightThetaValue = thetaValue;
128 lightPhiValue = phiValue;
131 cmds.emplace_back(
"/vis/drawVolume");
133 cmds.emplace_back(
"/vis/viewer/set/autoRefresh false");
134 cmds.emplace_back(
"/vis/viewer/set/viewpointThetaPhi " + std::to_string(thetaValue) +
" " + std::to_string(phiValue));
135 cmds.emplace_back(
"/vis/viewer/set/lightsThetaPhi " + std::to_string(lightThetaValue) +
" " + std::to_string(lightPhiValue));
139 cmds.emplace_back(
"/vis/scene/add/trajectories smooth");
140 cmds.emplace_back(
"/vis/modeling/trajectories/create/drawByCharge");
141 cmds.emplace_back(
"/vis/modeling/trajectories/drawByCharge-0/default/setDrawStepPts true");
142 cmds.emplace_back(
"/vis/modeling/trajectories/drawByCharge-0/default/setStepPtsSize 2");
144 cmds.emplace_back(
"/vis/modeling/trajectories/create/drawByParticleID");
145 cmds.emplace_back(
"/vis/modeling/trajectories/drawByParticleID-0/set opticalphoton cyan");
147 cmds.emplace_back(
"/vis/scene/add/hits");
148 cmds.emplace_back(
"/vis/scene/endOfEventAction accumulate 10000");
149 cmds.push_back(
"/vis/viewer/set/background " + g4view.background);
150 cmds.push_back(
"/vis/viewer/set/numberOfCloudPoints " + std::to_string(g4view.cloudPoints));
153 for (
const auto& command : g4SceneProperties.
addSceneDecorations(gopts)) { cmds.emplace_back(command); }
154 for (
const auto& command : g4SceneProperties.
addSceneTexts(gopts)) { cmds.emplace_back(command); }
155 if (decorations.eventID) { cmds.emplace_back(
"/vis/scene/add/eventID"); }
158 if (g4view.driver !=
"TOOLSSG_OFFSCREEN") {
160 cmds.emplace_back(
"/vis/viewer/set/autoRefresh true");
161 cmds.emplace_back(
"/vis/viewer/flush");
171 const std::shared_ptr<GLogger>& log,
const std::vector<std::string>& commands) {
172 auto* g4uim = G4UImanager::GetUIpointer();
175 for (
const auto& cmd : commands) {
176 log->info(2,
"Executing UIManager command: ", cmd);
177 g4uim->ApplyCommand(cmd);
192 auto randomEngineName = gopts->getScalarString(
"randomEngine");
193 auto seed = gopts->getScalarInt(
"seed");
198 auto timed = time(NULL);
199 auto clockd = clock();
200 auto getpidi = getpid();
201 seed = (G4int)( timed - clockd - getpidi );
202 log->info(1,
"Using random seed ", seed);
204 log->info(1,
"User defined seed ", seed);
212 if (randomEngineName ==
"DRand48Engine")
213 G4Random::setTheEngine(
new CLHEP::DRand48Engine(seed));
214 else if (randomEngineName ==
"DualRand")
215 G4Random::setTheEngine(
new CLHEP::DualRand(seed));
216 else if (randomEngineName ==
"Hurd160Engine")
217 G4Random::setTheEngine(
new CLHEP::Hurd160Engine(seed));
218 else if (randomEngineName ==
"HepJamesRandom")
219 G4Random::setTheEngine(
new CLHEP::HepJamesRandom(seed));
220 else if (randomEngineName ==
"MTwistEngine")
221 G4Random::setTheEngine(
new CLHEP::MTwistEngine(seed));
222 else if (randomEngineName ==
"MixMaxRng")
223 G4Random::setTheEngine(
new CLHEP::MixMaxRng(seed));
224 else if (randomEngineName ==
"RandEngine")
225 G4Random::setTheEngine(
new CLHEP::RandEngine(seed));
226 else if (randomEngineName ==
"RanecuEngine")
227 G4Random::setTheEngine(
new CLHEP::RanecuEngine(seed));
228 else if (randomEngineName ==
"Ranlux64Engine")
229 G4Random::setTheEngine(
new CLHEP::Ranlux64Engine(seed));
230 else if (randomEngineName ==
"RanluxEngine")
231 G4Random::setTheEngine(
new CLHEP::RanluxEngine(seed));
232 else if (randomEngineName ==
"RanshiEngine")
233 G4Random::setTheEngine(
new CLHEP::RanshiEngine(seed));
234 else if (randomEngineName ==
"Hurd288Engine")
235 G4Random::setTheEngine(
new CLHEP::Hurd288Engine(seed));
236 else if (randomEngineName ==
"TripleRand")
237 G4Random::setTheEngine(
new CLHEP::TripleRand(seed));
241 log->info(0,
"Starting random engine ", randomEngineName,
" with seed ", seed);
242 G4Random::setTheSeed(seed);
std::vector< std::string > addSceneTexts(const std::shared_ptr< GOptions > &gopts)
std::vector< std::string > addSceneDecorations(const std::shared_ptr< GOptions > &gopts)
Conventional constants used by GEMC utility helpers.
Utility helpers for runtime setup, command preparation, and random-engine initialization.
#define SEEDNOTSET
Sentinel value used to mark that the random seed has not been explicitly set.
#define EC__RANDOMENGINENOTFOUND
Error code used when the configured random engine name is not recognized.
void define_new_gemc_units()
std::vector< std::string > initial_commands(const std::shared_ptr< GOptions > &gopts, const std::shared_ptr< GLogger > &log, bool configure_visualization)
Build a list of Geant4 UI commands needed at startup.
void start_random_engine(const std::shared_ptr< GOptions > &gopts, const std::shared_ptr< GLogger > &log)
Select and start the random engine, then seed it.
std::vector< std::string > verbosity_commands(const std::shared_ptr< GOptions > &gopts, const std::shared_ptr< GLogger > &log)
Build a list of Geant4 UI commands that reduce verbosity across subsystems.
void run_manager_commands(const std::shared_ptr< GOptions > &gopts, const std::shared_ptr< GLogger > &log, const std::vector< std::string > &commands)
Execute a sequence of Geant4 UI commands through the UI manager.
int get_nthreads(const std::shared_ptr< GOptions > &gopts, const std::shared_ptr< GLogger > &log)
Determine the number of worker threads to use for the run.
G4Light getG4Light(const std::shared_ptr< GOptions > &gopts)
G4View getG4View(const std::shared_ptr< GOptions > &gopts)
G4Camera getG4Camera(const std::shared_ptr< GOptions > &gopts)
G4Decorations getG4Decorations(const std::shared_ptr< GOptions > &gopts)
constexpr G4double milligray
constexpr G4double nanogray
constexpr G4double microgray
constexpr G4double picogray
double getG4Number(const string &v, bool warnIfNotUnit=false)