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; }
177 [[nodiscard]] int GetNumberOfThreads() const { return fNThreads; }
182 void SetPrintModulo(int n_ev) { fPrintModulo = n_ev > 0 ? n_ev : -1; }
183
188 void IncludeMacroFile(std::string filename) { fMacroFilesOrContents.emplace_back(filename); }
194 void RegisterG4Alias(std::string alias, std::string value) { fG4Aliases.emplace(alias, value); }
203 void Initialize();
210 void Run();
211
216 void SetRandEngine(std::string name);
221 void SetRandEngineSeed(int seed);
226 void SetRandEngineInternalSeed(int index);
240 [[nodiscard]] bool GetRandIsControlled() const { return fIsRandControlled; }
245 [[nodiscard]] bool GetRandEngineSelected() const { return !fRandEngineName.empty(); }
246
251 void SetLogLevel(std::string level);
252
257 void EnableMultiProcessing(int proc_num) {
258 fProcessNumber = proc_num;
259 fMultiProcessing = true;
260 }
261
266 [[nodiscard]] bool HadWarning() const {
267 return fExceptionHandler->HadWarning() || RMGLog::HadWarning();
268 }
269
273 [[nodiscard]] bool HadError() const {
274 return fExceptionHandler->HadError() || RMGLog::HadError();
275 }
276
281 // NOLINTNEXTLINE(readability-make-member-function-const)
282 void ActivateOptionalOutputScheme(std::string name) {
283 GetUserInit()->ActivateOptionalOutputScheme(name);
284 }
285
289 static void AbortRunGracefully() {
290 if (!fAbortRun.is_lock_free()) {
291 // A non-lock-free atomic cannot be safely set from the signal handler that calls this.
292 // Should not happen for bool on real platforms.
293 RMGLog::Out(RMGLog::error, "cannot abort run gracefully: abort flag is not lock-free");
294 return;
295 }
296 fAbortRun = true;
297 }
298
302 static bool ShouldAbortRun() { return fAbortRun; }
303
304 private:
305
306 void SetUpDefaultG4RunManager(G4RunManagerType type = G4RunManagerType::Default);
307 void SetUpDefaultG4VisManager();
308 void SetUpDefaultDetectorConstruction();
309 void SetUpDefaultProcessesList();
310 void SetUpDefaultUserAction();
311 void CheckRandEngineMTState();
312
313 std::unique_ptr<G4UIExecutive> StartInteractiveSession();
314
315 std::string fApplicationName;
316 int fArgc;
317 char** fArgv;
318 std::map<std::string, std::string> fG4Aliases;
319 std::vector<std::string> fMacroFilesOrContents;
320 bool fInteractive = false;
321 int fPrintModulo = -1;
322 int fNThreads = 1;
323 int fProcessNumber = 0;
324 bool fMultiProcessing = false;
325
326 bool fIsRandControlled = false;
327 bool fIsRandControlledAtEngineChange = false;
328 std::string fRandEngineName;
329
330 static RMGManager* fRMGManager;
331 static std::atomic<bool> fAbortRun;
332
333 std::unique_ptr<G4RunManager> fG4RunManager = nullptr;
334 std::unique_ptr<G4VisManager> fG4VisManager = nullptr;
335
336 RMGExceptionHandler* fExceptionHandler = nullptr;
337 G4VUserPhysicsList* fPhysicsList = nullptr;
338 RMGHardware* fDetectorConstruction = nullptr;
339
340 RMGUserAction* fUserAction = nullptr;
341 // store partial custom actions to be used later in RMGUserAction
342 std::shared_ptr<RMGUserInit> fUserInit;
343
344 // messenger stuff
345 std::unique_ptr<G4GenericMessenger> fMessenger;
346 std::unique_ptr<G4GenericMessenger> fLogMessenger;
347 std::unique_ptr<G4GenericMessenger> fRandMessenger;
348 void DefineCommands();
349};
350
351#endif
352
353// vim: tabstop=2 shiftwidth=2 expandtab
Geant4 exception handler that records whether warnings/errors have occurred.
Definition RMGExceptionHandler.hh:29
Definition RMGHardware.hh:39
@ error
Print only errors.
Definition RMGLog.hh:72
@ fatal
Print only errors, stop execution.
Definition RMGLog.hh:73
static void Out(RMGLog::LogLevel loglevel, const T &message)
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:257
void SetRandEngineInternalSeed(int index)
Sets the internal seed index for the random engine.
Definition RMGManager.cc:340
void IncludeMacroFile(std::string filename)
Includes a macro or single macro command file for execution.
Definition RMGManager.hh:188
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:194
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:292
void ActivateOptionalOutputScheme(std::string name)
Activates an optional output scheme.
Definition RMGManager.hh:282
bool GetRandIsControlled() const
Checks if the random engine is under user controlled seeding.
Definition RMGManager.hh:240
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:273
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:358
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:266
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:289
void SetRandEngineSeed(int seed)
Sets the seed for the random engine.
Definition RMGManager.cc:321
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:302
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:245
int GetNumberOfThreads() const
Returns the number of threads.
Definition RMGManager.hh:177
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:182
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:282
static RMGOutputManager * Instance()
Gets the singleton instance of RMGOutputManager.
Definition RMGOutputManager.hh:56
Action initialization assembling all remage user actions.
Definition RMGUserAction.hh:29