remage
Simulation framework for HPGe-based experiments
 
Loading...
Searching...
No Matches
RMGManager.hh
1// Copyright (C) 2022 Luigi Pertoldi <https://orcid.org/0000-0002-0467-2571>
2//
3// This program is free software: you can redistribute it and/or modify it under
4// the terms of the GNU Lesser General Public License as published by the Free
5// Software Foundation, either version 3 of the License, or (at your option) any
6// later version.
7//
8// This program is distributed in the hope that it will be useful, but WITHOUT
9// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11// details.
12//
13// You should have received a copy of the GNU Lesser General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16#ifndef _RMG_MANAGER_HH_
17#define _RMG_MANAGER_HH_
18
19#include <atomic>
20#include <memory>
21#include <set>
22#include <vector>
23
24#include "G4RunManager.hh"
25#include "G4RunManagerFactory.hh"
26#include "G4Threading.hh"
27#include "G4VisManager.hh"
28#include "globals.hh"
29
30#include "RMGExceptionHandler.hh"
31#include "RMGLog.hh"
32#include "RMGOutputManager.hh"
33#include "RMGUserInit.hh"
34
36class RMGHardware;
37class RMGUserAction;
39class G4UIExecutive;
47class RMGManager {
48
49 public:
50
51 RMGManager() = delete;
59 RMGManager(std::string app_name, int argc, char** argv);
63 ~RMGManager() = default;
64
65 RMGManager(RMGManager const&) = delete;
66 RMGManager& operator=(RMGManager const&) = delete;
67 RMGManager(RMGManager&&) = delete;
68 RMGManager& operator=(RMGManager&&) = delete;
69
70 // getters
75 static RMGManager* Instance() { return fRMGManager; }
100 [[nodiscard]] auto GetUserInit() const { return fUserInit; }
105 [[nodiscard]] auto GetOutputManager() const { return RMGOutputManager::Instance(); }
110 [[nodiscard]] int GetPrintModulo() const { return fPrintModulo; }
111
116 [[nodiscard]] bool IsExecSequential() const {
117 return fG4RunManager->GetRunManagerType() == G4RunManager::RMType::sequentialRM;
118 }
119
126 [[nodiscard]] int GetProcessNumberOffset() const {
127 if (fProcessNumber > 0 && (fG4RunManager && !IsExecSequential()))
128 RMGLog::OutDev(RMGLog::fatal, "wrong setup for process-based parallelization.");
129 return fProcessNumber;
130 }
131
138 [[nodiscard]] bool IsMultiProcessing() const { return fMultiProcessing; }
139
140 // setters
145 void SetUserInit(G4RunManager* g4_manager) {
146 fG4RunManager = std::unique_ptr<G4RunManager>(g4_manager);
147 }
148
152 void SetUserInit(G4VisManager* vis) { fG4VisManager = std::unique_ptr<G4VisManager>(vis); }
157 void SetUserInit(RMGHardware* det) { fDetectorConstruction = det; }
162 void SetUserInit(G4VUserPhysicsList* proc) { fPhysicsList = proc; }
163
168 void SetInteractive(bool flag = true) { fInteractive = flag; }
173 void SetNumberOfThreads(int nthreads) { fNThreads = nthreads; }
178 void SetPrintModulo(int n_ev) { fPrintModulo = n_ev > 0 ? n_ev : -1; }
179
184 void IncludeMacroFile(std::string filename) { fMacroFilesOrContents.emplace_back(filename); }
190 void RegisterG4Alias(std::string alias, std::string value) { fG4Aliases.emplace(alias, value); }
199 void Initialize();
206 void Run();
207
212 void SetRandEngine(std::string name);
217 void SetRandEngineSeed(int seed);
222 void SetRandEngineInternalSeed(int index);
236 [[nodiscard]] bool GetRandIsControlled() const { return fIsRandControlled; }
241 [[nodiscard]] bool GetRandEngineSelected() const { return !fRandEngineName.empty(); }
242
247 void SetLogLevel(std::string level);
248
253 void EnableMultiProcessing(int proc_num) {
254 fProcessNumber = proc_num;
255 fMultiProcessing = true;
256 }
257
262 [[nodiscard]] bool HadWarning() const {
263 return fExceptionHandler->HadWarning() || RMGLog::HadWarning();
264 }
265
269 [[nodiscard]] bool HadError() const {
270 return fExceptionHandler->HadError() || RMGLog::HadError();
271 }
272
277 // NOLINTNEXTLINE(readability-make-member-function-const)
278 void ActivateOptionalOutputScheme(std::string name) {
279 GetUserInit()->ActivateOptionalOutputScheme(name);
280 }
281
285 static void AbortRunGracefully() {
286 if (!fAbortRun.is_lock_free()) return;
287 fAbortRun = true;
288 }
289
293 static bool ShouldAbortRun() { return fAbortRun; }
294
295 private:
296
297 void SetUpDefaultG4RunManager(G4RunManagerType type = G4RunManagerType::Default);
298 void SetUpDefaultG4VisManager();
299 void SetUpDefaultDetectorConstruction();
300 void SetUpDefaultProcessesList();
301 void SetUpDefaultUserAction();
302 void CheckRandEngineMTState();
303
304 std::unique_ptr<G4UIExecutive> StartInteractiveSession();
305
306 std::string fApplicationName;
307 int fArgc;
308 char** fArgv;
309 std::map<std::string, std::string> fG4Aliases;
310 std::vector<std::string> fMacroFilesOrContents;
311 bool fInteractive = false;
312 int fPrintModulo = -1;
313 int fNThreads = 1;
314 int fProcessNumber = 0;
315 bool fMultiProcessing = false;
316
317 bool fIsRandControlled = false;
318 bool fIsRandControlledAtEngineChange = false;
319 std::string fRandEngineName;
320
321 static RMGManager* fRMGManager;
322 static std::atomic<bool> fAbortRun;
323
324 std::unique_ptr<G4RunManager> fG4RunManager = nullptr;
325 std::unique_ptr<G4VisManager> fG4VisManager = nullptr;
326
327 RMGExceptionHandler* fExceptionHandler = nullptr;
328 G4VUserPhysicsList* fPhysicsList = nullptr;
329 RMGHardware* fDetectorConstruction = nullptr;
330
331 RMGUserAction* fUserAction = nullptr;
332 // store partial custom actions to be used later in RMGUserAction
333 std::shared_ptr<RMGUserInit> fUserInit;
334
335 // messenger stuff
336 std::unique_ptr<G4GenericMessenger> fMessenger;
337 std::unique_ptr<G4GenericMessenger> fLogMessenger;
338 std::unique_ptr<G4GenericMessenger> fRandMessenger;
339 void DefineCommands();
340};
341
342#endif
343
344// vim: tabstop=2 shiftwidth=2 expandtab
Geant4 exception handler that records whether warnings/errors have occurred.
Definition RMGExceptionHandler.hh:27
Definition RMGHardware.hh:39
@ fatal
Print only errors, stop execution.
Definition RMGLog.hh:73
Main manager class for the remage simulation.
Definition RMGManager.hh:47
G4RunManager * GetG4RunManager()
Retrieves the Geant4 run manager.
Definition RMGManager.cc:251
void Run()
Executes the supplied macro files and commands and switch to interactive session if requested.
Definition RMGManager.cc:140
void EnableMultiProcessing(int proc_num)
Sets the process number for offset calculations in process-parallelized mode.
Definition RMGManager.hh:253
void SetRandEngineInternalSeed(int index)
Sets the internal seed index for the random engine.
Definition RMGManager.cc:338
void IncludeMacroFile(std::string filename)
Includes a macro or single macro command file for execution.
Definition RMGManager.hh:184
auto GetOutputManager() const
Retrieves the output manager.
Definition RMGManager.hh:105
void SetUserInit(G4VUserPhysicsList *proc)
Sets the physics list.
Definition RMGManager.hh:162
void RegisterG4Alias(std::string alias, std::string value)
Registers a Geant4 alias for use in macro commands.
Definition RMGManager.hh:190
void SetLogLevel(std::string level)
Sets the logging level.
Definition RMGManager.cc:271
~RMGManager()=default
Default destructor.
bool ApplyRandEngineForCurrentThread()
Applies the random engine settings for the current thread.
Definition RMGManager.cc:290
void ActivateOptionalOutputScheme(std::string name)
Activates an optional output scheme.
Definition RMGManager.hh:278
bool GetRandIsControlled() const
Checks if the random engine is under user controlled seeding.
Definition RMGManager.hh:236
int GetPrintModulo() const
Returns the print modulo value.
Definition RMGManager.hh:110
RMGHardware * GetDetectorConstruction()
Retrieves the detector construction.
Definition RMGManager.cc:261
bool HadError() const
Checks if any errors have been recorded.
Definition RMGManager.hh:269
void SetUserInit(G4VisManager *vis)
Sets the Geant4 visualization manager.
Definition RMGManager.hh:152
void SetInteractive(bool flag=true)
Enables or disables interactive mode.
Definition RMGManager.hh:168
void SetRandSystemEntropySeed()
Sets the random engine seed using system entropy.
Definition RMGManager.cc:356
bool IsExecSequential() const
Checks if the execution is sequential (single-threaded).
Definition RMGManager.hh:116
void SetUserInit(RMGHardware *det)
Sets the detector construction.
Definition RMGManager.hh:157
void Initialize()
Initialize the simulation components (run manager, visualization, random engine, detector constructio...
Definition RMGManager.cc:82
bool HadWarning() const
Checks if any warnings have been recorded.
Definition RMGManager.hh:262
void SetNumberOfThreads(int nthreads)
Sets the number of threads.
Definition RMGManager.hh:173
static void AbortRunGracefully()
Set a flag to gracefully aborts the simulation run at the next possible time.
Definition RMGManager.hh:285
void SetRandEngineSeed(int seed)
Sets the seed for the random engine.
Definition RMGManager.cc:319
bool IsMultiProcessing() const
Checks if the execution is part of a process-parallelized run.
Definition RMGManager.hh:138
static bool ShouldAbortRun()
Checks whether an abort signal has been triggered.
Definition RMGManager.hh:293
auto GetUserInit() const
Retrieves the user initialization object.
Definition RMGManager.hh:100
static RMGManager * Instance()
Retrieves the singleton instance of RMGManager.
Definition RMGManager.hh:75
int GetProcessNumberOffset() const
Gets the process number offset.
Definition RMGManager.hh:126
bool GetRandEngineSelected() const
Checks if a random engine has been selected by the user.
Definition RMGManager.hh:241
G4VUserPhysicsList * GetProcessesList()
Retrieves the physics list.
Definition RMGManager.cc:266
G4VisManager * GetG4VisManager()
Retrieves the Geant4 visualization manager.
Definition RMGManager.cc:256
void SetPrintModulo(int n_ev)
Sets the print modulo value.
Definition RMGManager.hh:178
void SetUserInit(G4RunManager *g4_manager)
Sets the Geant4 run manager.
Definition RMGManager.hh:145
void SetRandEngine(std::string name)
Sets the random engine by name.
Definition RMGManager.cc:280
static RMGOutputManager * Instance()
Gets the singleton instance of RMGOutputManager.
Definition RMGOutputManager.hh:56
Action initialization assembling all remage user actions.
Definition RMGUserAction.hh:29