remage
Simulation framework for HPGe-based experiments
Loading...
Searching...
No Matches
RMGWorkerInitialization.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_WORKER_INITIALIZATION_HH_
17#define _RMG_WORKER_INITIALIZATION_HH_
18
19#include <type_traits>
20
21#include "G4AutoLock.hh"
22#include "G4StateManager.hh"
23#include "G4UserTaskThreadInitialization.hh"
24#include "Randomize.hh"
25
26#include "RMGExceptionHandler.hh"
27#include "RMGManager.hh"
28
30namespace {
31 G4Mutex RMGWorkerInitializationRNGMutex = G4MUTEX_INITIALIZER;
32} // namespace
34
43template<typename ThreadInitialization>
44class RMGWorkerInitialization : public ThreadInitialization {
45 static_assert(std::is_base_of_v<G4UserWorkerThreadInitialization, ThreadInitialization>);
46
47 public:
48
49 RMGWorkerInitialization() = default;
50 ~RMGWorkerInitialization() = default;
51
55 void SetupRNGEngine(const CLHEP::HepRandomEngine* aRNGEngine) const override {
56 // Install our exception handler on this worker thread, mirroring the master-thread setup.
57 // G4StateManager is thread-local in MT mode.
58 if (G4StateManager::GetStateManager()->GetExceptionHandler() == nullptr) {
59 G4StateManager::GetStateManager()->SetExceptionHandler(new RMGExceptionHandler());
60 }
61
62 G4AutoLock l(&RMGWorkerInitializationRNGMutex);
63
64 auto rmg_man = RMGManager::Instance();
65 // Implement custom logic if the user had chosen a random engine, otherwise apply Geant4's
66 // default strategy.
67 if (rmg_man->GetRandEngineSelected()) rmg_man->ApplyRandEngineForCurrentThread();
68 else ThreadInitialization::SetupRNGEngine(aRNGEngine);
69 }
70};
71
72#endif
73
74// vim: tabstop=2 shiftwidth=2 expandtab
Geant4 exception handler that records whether warnings/errors have occurred.
Definition RMGExceptionHandler.hh:29
static RMGManager * Instance()
Retrieves the singleton instance of RMGManager.
Definition RMGManager.hh:75
void SetupRNGEngine(const CLHEP::HepRandomEngine *aRNGEngine) const override
Install the user-selected random engine on this worker, or fall back to Geant4's.
Definition RMGWorkerInitialization.hh:55