Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4MoleculeCounterManager.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26
27// Author: Christian Velten (2025)
28
29#ifndef G4MOLECULECOUNTERMANAGER_HH
30#define G4MOLECULECOUNTERMANAGER_HH 1
31
32#include "G4Exception.hh"
33#include "G4Threading.hh"
34#include "G4Types.hh"
35#include "G4VMoleculeCounter.hh"
37#include "G4ios.hh"
38
39#include <functional>
40#include <map>
41#include <vector>
42
43class G4Event;
44class G4Run;
45class G4Step;
46class G4StepPoint;
47class G4Track;
49
51{
52 private:
53 struct Private
54 {
55 explicit Private() = default;
56 };
57
58 public:
61
62 private:
63 G4bool fInstancesRegistered{false};
64 void RegisterInstance();
65
66 public:
69 static void DeleteInstance();
70
71 void Initialize();
72
73 //
74 // Management
75
76 G4int RegisterCounter(std::unique_ptr<G4VMoleculeCounter>);
77 G4int RegisterCounter(std::unique_ptr<G4VMoleculeReactionCounter>);
79
80 private:
81 template<typename T>
82 G4int RegisterCounter(std::map<G4int, T*>&, std::unique_ptr<T>, std::function<G4int()>);
83 void InitializeMaster();
84 void InitializeWorker();
85
86 public:
87 // methods to receive forwarding from G4DNAChemistryManager
88 void BeginOfEventAction(const G4Event*);
89 void BeginOfRunAction(const G4Run*);
90 void EndOfEventAction(const G4Event*);
91 void EndOfRunAction(const G4Run*);
92 // Dumping counters
93 void DumpMasterCounters() const;
94 void DumpWorkerCounters() const;
95 // Accumulation of counters into master
97
98 //
99 // Calls to Molecule Counters
100 public:
101 // [[deprecated("This should only be used for IRT and may be replaced as well.")]]
103 // [[deprecated("This should only be used for IRT and may be replaced as well.")]]
105
106 void AddMolecule(const G4Track*, G4double, G4int = 1);
107 void RemoveMolecule(const G4Track*, G4double, G4int = 1);
108
109 void AddMolecule(const G4Track*, const G4StepPoint*, G4double, G4int = 1);
110 void RemoveMolecule(const G4Track*, const G4StepPoint*, G4double, G4int = 1);
111
113
114 //
115 // Calls to Molecule Reaction Counters
116
117 // [[deprecated("TBD")]]
119 // void RecordReaction(const G4Track*, const G4Track*, const G4DNAMolecularReactionData*, G4double,
120 // G4int = 1);
121
123
124 //
125 // Other Calls to both types or only one (e.g., BroadcastIgnoreMolecule)
126
127 void ResetCounters();
128
129 void NotifyOfStep(const G4Step*);
130 void NotifyOfFinalize();
131
132 // These will be broadcast to all counters registered at time of call
136
137 private:
138 static const G4MoleculeCounterManager* fpMasterInstance;
139 static std::vector<const G4MoleculeCounterManager*> fWorkerInstances;
140 G4ThreadLocalStatic std::unique_ptr<G4MoleculeCounterManager> fpInstance;
141
142 std::unique_ptr<G4MoleculeCounterManagerMessenger> fpMessenger;
143
144 G4int fVerbosity;
145 G4bool fIsInitialized;
146 G4bool fIsActive;
147 static std::atomic<G4bool> fResetCountersBeforeEvent;
148 static std::atomic<G4bool> fResetCountersBeforeRun;
149 static std::atomic<G4bool> fResetMasterCounterWithWorkers;
150 static std::atomic<G4bool> fAccumulateCounterIntoMaster;
151
152 std::map<G4int, G4VMoleculeCounter*> fCounters{};
153 std::map<G4int, G4VMoleculeReactionCounter*> fReactionCounters{};
154
155 public:
156 G4bool GetIsActive() const { return fIsActive; }
157 void SetIsActive(G4bool flag) { fIsActive = flag; }
158
159 G4int GetVerbosity() const { return fVerbosity; }
160 void SetVerbosity(G4int v) { fVerbosity = v; }
161
164
167
170
173
174 std::vector<const G4VMoleculeCounter*> GetMoleculeCounters() const;
175 std::vector<const G4VMoleculeCounter*> GetMoleculeCounters(G4String) const;
176 template<typename T = G4VMoleculeCounter>
177 const T* GetMoleculeCounter(G4int) const;
179
180 std::vector<const G4VMoleculeReactionCounter*> GetMoleculeReactionCounters() const;
181 std::vector<const G4VMoleculeReactionCounter*> GetMoleculeReactionCounters(G4String) const;
183 template<typename T = G4VMoleculeReactionCounter>
184 const T* GetMoleculeReactionCounter(G4int) const;
185
186 private:
187 G4ThreadLocalStatic std::atomic<G4bool> fBeginOfEventTriggered;
188 static std::atomic<G4bool> fBeginOfRunTriggered;
189};
190
191//------------------------------------------------------------------------------
192
193template<typename T>
195 std::unique_ptr<T> counter,
196 std::function<G4int()> idProvider)
197{
198 // this template allows for more than one counter type to be registered in the same way without
199 // duplicating code; see public methods for G4VMoleculeCounter and G4VMoleculeReactionCounter
200
201 if (fVerbosity > 0) {
202 G4cout << "G4MoleculeCounterManager::RegisterCounter ("
203 << (G4Threading::IsMasterThread() ? "master" : "worker") << ")" << G4endl;
204 }
205
206 if (counter->GetManagedId() >= 0) {
207 G4ExceptionDescription description;
208 description << "Trying to add a counter whose id was already altered to be non-negative!\n";
209 description << " Id: " << counter->GetManagedId() << "\n";
210 description << "Name: " << counter->GetName();
211 G4Exception("G4MoleculeCounterManager::RegisterCounter", "MOLMAN002", FatalErrorInArgument,
212 description);
213 }
214
215 T* sp = counter.release();
216 // Set managed Id
217 sp->SetManagedId(idProvider());
218
219 map.emplace(sp->GetManagedId(), sp);
220
221 return sp->GetManagedId();
222}
223
224//------------------------------------------------------------------------------
225
226template<typename T>
228{
229 static_assert(std::is_base_of<G4VMoleculeCounter, T>::value,
230 "T must be derived from G4VMoleculeCounter!");
231 auto base_ptr = GetEditableMoleculeCounter(id);
232 if (base_ptr == nullptr) return nullptr;
233 const T* ptr = dynamic_cast<const T*>(base_ptr);
234 return ptr;
235}
236
237template<typename T>
239{
240 static_assert(std::is_base_of<G4VMoleculeReactionCounter, T>::value,
241 "T must be derived from G4VMoleculeReactionCounter!");
242 auto base_ptr = GetEditableMoleculeReactionCounter(id);
243 if (base_ptr == nullptr) return nullptr;
244 const T* ptr = dynamic_cast<const T*>(base_ptr);
245 return ptr;
246}
247
248//------------------------------------------------------------------------------
249
250#endif // G4MOLECULECOUNTERMANAGER_HH
@ FatalErrorInArgument
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
void ActivateReactionCounterAtTimes(G4int, G4double, G4double, G4bool=true, G4bool=true)
void BroadcastIgnoreMolecule(const G4MoleculeDefinition *)
const T * GetMoleculeReactionCounter(G4int) const
void RecordReaction(const G4DNAMolecularReactionData *, G4double, G4int=1)
void EndOfEventAction(const G4Event *)
void SetResetMasterCounterWithWorkers(G4bool=true)
void AddMoleculeWithoutTrack(const G4MolecularConfiguration *, G4double, G4int=1)
G4int RegisterCounter(std::unique_ptr< G4VMoleculeCounter >)
G4VMoleculeReactionCounter * GetEditableMoleculeReactionCounter(G4int) const
void SetAccumulateCounterIntoMaster(G4bool=true)
void AddMolecule(const G4Track *, G4double, G4int=1)
void BeginOfEventAction(const G4Event *)
void RemoveMoleculeWithoutTrack(const G4MolecularConfiguration *, G4double, G4int=1)
static G4MoleculeCounterManager * GetInstanceIfExists()
G4VMoleculeCounter * GetEditableMoleculeCounter(G4int) const
void AbsorbWorkerManagerCounters(const G4MoleculeCounterManager *=nullptr)
static G4MoleculeCounterManager * Instance()
void ActivateCounterAtTimes(G4int, G4double, G4double, G4bool=true, G4bool=true)
std::vector< const G4VMoleculeReactionCounter * > GetMoleculeReactionCounters() const
void RemoveMolecule(const G4Track *, G4double, G4int=1)
void SetResetCountersBeforeEvent(G4bool=true)
const T * GetMoleculeCounter(G4int) const
void BroadcastIgnoreReactant(const G4MolecularConfiguration *)
std::vector< const G4VMoleculeCounter * > GetMoleculeCounters() const
Definition G4Run.hh:48
G4bool IsMasterThread()
#define G4ThreadLocalStatic
Definition tls.hh:76