Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4TransportationParameters.cc
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// Author: J. Apostolakis Nov 2022
27
29
30#include "G4ApplicationState.hh"
31#include "G4StateManager.hh"
32#include "G4Threading.hh"
33#include "G4AutoLock.hh"
34
35#include "G4SystemOfUnits.hh"
36
37G4TransportationParameters* G4TransportationParameters::theInstance = nullptr;
38
39#ifdef G4MULTITHREADED
40namespace
41{
42 G4Mutex transportParamsMutex = G4MUTEX_INITIALIZER;
43}
44#endif
45
46//------------------------------------------------------------------------------
47
48G4TransportationParameters::G4TransportationParameters()
49{
51}
52
53//------------------------------------------------------------------------------
54
56{
57 if(theInstance == nullptr) {
58 G4MUTEXLOCK(&transportParamsMutex);
59 if(theInstance == nullptr) {
60 static G4TransportationParameters manager;
61 theInstance = &manager;
62 }
63 G4MUTEXUNLOCK(&transportParamsMutex);
64 }
65 return theInstance;
66}
67
68//------------------------------------------------------------------------------
69
70G4bool G4TransportationParameters::IsLocked() const
71{
72 auto stateManager = G4StateManager::GetStateManager();
73
74 auto state = stateManager->GetCurrentState();
75 bool goodState = state == G4State_PreInit
76 || state == G4State_Init
77 || state == G4State_Idle ;
78 return ( !G4Threading::IsMasterThread() || !goodState );
79}
80
81//------------------------------------------------------------------------------
82
84{
85 if(IsLocked()) { ReportLockError(__func__); return false; }
86 fNumberOfTrials = val;
87 return true;
88}
89
90//------------------------------------------------------------------------------
91
93{
94 if(IsLocked()) { ReportLockError(__func__); return false; }
95 fWarningEnergy = val;
96
97 // Consistency check -- and trial fix
98 if( fWarningEnergy > fImportantEnergy ) {
99 G4cerr << "G4TransportationParameters::GetWarningEnergy enforcing warning-E <= important-E "
100 << " resetting important energy from " << fImportantEnergy
101 << " to " << val << G4endl;
102 // "Enforcing Important Energy >= Warning Energy"
103 fImportantEnergy = fWarningEnergy;
104 }
105 return true;
106}
107
108//------------------------------------------------------------------------------
109
111{
112 if(IsLocked()) { ReportLockError(__func__); return false; }
113 fImportantEnergy = val;
114
115 // Consistency check -- and trial fix
116 if( fImportantEnergy < fWarningEnergy ) {
117 G4String mthd= G4String("G4TransportationParameters")+G4String(__func__);
119 ed<<"enforcing hierarchy (warning-E <= important-E): resetting important"
120 <<" energy from " << fImportantEnergy << " to " << val << G4endl;
121 G4Exception( mthd, "Enforcing Warning Energy <= Important Energy",
122 JustWarning, ed );
123
124 fWarningEnergy = fImportantEnergy;
125 }
126 return true;
127}
128
129//------------------------------------------------------------------------------
130
132 G4double importE )
133{
134 if(IsLocked()) {
135 ReportLockError(__func__);
136 return false;
137 }
138
139 if( warnE <= importE )
140 {
141 fWarningEnergy = warnE;
142 fImportantEnergy = importE;
143 }
144 else
145 {
146 fWarningEnergy = importE;
147 fImportantEnergy = warnE;
148
149 G4String mthd= G4String("G4TransportationParameters")+G4String(__func__);
151 ed << "To enforce hierarchy (warning-E <= important-E): "
152 << " using smaller value= " << importE << " as Warning Energy "
153 << " and larger value= " << warnE << " as Important Energy." << G4endl;
154 G4Exception( mthd, "Enforcing Warning Energy <= Important Energy",
155 JustWarning, ed );
156 }
157 return true;
158}
159
160//------------------------------------------------------------------------------
161
163{
164 // Report Incompatible States: GeomClosed , EventProc, (also Quit, Abort)
165 G4String namesMethodClass= G4String("G4TransportationParameters") + methodName;
166
167 auto stateManager = G4StateManager::GetStateManager();
168 auto state = stateManager->GetCurrentState();
169
171 ed << "Cannot change values of G4TransportationParameters when G4State is "
172 << stateManager->GetStateString(state) << G4endl;
173 ed << "Only the following Geant4 state are compatible: Pre_Init, Init and Idle." << G4endl;
174 if( verbose ) {
175 ed << G4endl << "Values remain as follows:" << G4endl;
176 StreamInfo( ed );
177 }
178 G4Exception( namesMethodClass,
179 "Locked, due to incompatible G4state: it not possible to change its parameters.",
180 JustWarning, ed );
181}
182
183//------------------------------------------------------------------------------
184
185void G4TransportationParameters::StreamInfo(std::ostream& os) const
186{
187 auto prec = os.precision(5);
188
189 os << "Transport Parameters: " << G4endl;
190 os << " Warning energy = " << GetWarningEnergy() / CLHEP::MeV << " MeV " << G4endl;
191 os << " Important energy = " << GetImportantEnergy() / CLHEP::MeV << " MeV " << G4endl;
192 os << " Number of trials = " << GetNumberOfTrials() << G4endl;
193 os.precision(prec);
194}
195
196//------------------------------------------------------------------------------
197
199{
200 G4MUTEXLOCK(&transportParamsMutex);
202 G4MUTEXUNLOCK(&transportParamsMutex);
203}
204
205//------------------------------------------------------------------------------
206
208{
209 if(IsLocked()) { return false; }
210
211 // Restores the old high values -- potentially appropriate for energy-frontier
212 // HEP experiments.
213 // Caution: All tracks with E < 100 MeV that are found to loop are
214 SetWarningEnergy( 100.0 * CLHEP::MeV ); // Warn above this energy
215 SetImportantEnergy( 250.0 * CLHEP::MeV ); // Extra trial above this En
216
217 G4int maxTrials = 10;
218 SetNumberOfTrials( maxTrials );
219
220 return true;
221}
222
223//------------------------------------------------------------------------------
224
226{
227 if(IsLocked()) { return false; } // Currently must not change during loop - may relax
228
229 // Medium values -- reasonable default for an unknown application
230 fWarningEnergy= 1.0 * CLHEP::MeV; // Warn above this energy
231 fImportantEnergy= 10.0 * CLHEP::MeV; // Extra trial above this En
232 fNumberOfTrials= 10;
233 return true;
234}
235
236//------------------------------------------------------------------------------
237
239{
240 if(IsLocked()) { return false; }
241
242 // These values were the default in Geant4 10.5 - beta
243 SetWarningEnergy( 1.0 * CLHEP::keV ); // Warn above this En
244 SetImportantEnergy( 1.0 * CLHEP::MeV ); // Extra trials above it
245
246 G4int maxTrials = 30;
247 SetNumberOfTrials( maxTrials );
248
249 return true;
250}
251
252//------------------------------------------------------------------------------
253
255{
256 if(IsLocked()) { return false; }
257 fUseMagneticMoment= useMoment;
258 return true;
259}
260
261//------------------------------------------------------------------------------
262
263
265{
266 if(IsLocked()) { return false; }
267 fSilenceLooperWarnings= val;
268 return true;
269}
@ G4State_Init
@ G4State_Idle
@ G4State_PreInit
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
#define G4MUTEX_INITIALIZER
#define G4MUTEXLOCK(mutex)
#define G4MUTEXUNLOCK(mutex)
std::mutex G4Mutex
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
static G4StateManager * GetStateManager()
G4bool SetSilenceAllLooperWarnings(G4bool val=true)
static G4TransportationParameters * Instance()
G4bool EnableUseOfMagneticMoment(G4bool useMoment=true)
void ReportLockError(G4String methodName, G4bool verbose=false) const
void StreamInfo(std::ostream &os) const
G4bool SetWarningAndImportantEnergies(G4double warnE, G4double imprtE)
G4bool IsMasterThread()