Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // Author: J. Apostolakis Nov 2022 26 // Author: J. Apostolakis Nov 2022 27 27 28 #include "G4TransportationParameters.hh" 28 #include "G4TransportationParameters.hh" 29 29 30 #include "G4ApplicationState.hh" 30 #include "G4ApplicationState.hh" 31 #include "G4StateManager.hh" 31 #include "G4StateManager.hh" 32 #include "G4Threading.hh" 32 #include "G4Threading.hh" 33 #include "G4AutoLock.hh" 33 #include "G4AutoLock.hh" 34 34 35 #include "G4SystemOfUnits.hh" 35 #include "G4SystemOfUnits.hh" 36 36 37 G4TransportationParameters* G4TransportationPa 37 G4TransportationParameters* G4TransportationParameters::theInstance = nullptr; 38 38 39 #ifdef G4MULTITHREADED 39 #ifdef G4MULTITHREADED 40 namespace 40 namespace 41 { 41 { 42 G4Mutex transportParamsMutex = G4MUTEX_INIT 42 G4Mutex transportParamsMutex = G4MUTEX_INITIALIZER; 43 } 43 } 44 #endif 44 #endif 45 45 46 //-------------------------------------------- 46 //------------------------------------------------------------------------------ 47 47 48 G4TransportationParameters::G4TransportationPa 48 G4TransportationParameters::G4TransportationParameters() 49 { 49 { 50 SetIntermediateLooperThresholds(); 50 SetIntermediateLooperThresholds(); 51 } 51 } 52 52 53 //-------------------------------------------- 53 //------------------------------------------------------------------------------ 54 54 55 G4TransportationParameters* G4TransportationPa 55 G4TransportationParameters* G4TransportationParameters::Instance() 56 { 56 { 57 if(theInstance == nullptr) { 57 if(theInstance == nullptr) { 58 G4MUTEXLOCK(&transportParamsMutex); 58 G4MUTEXLOCK(&transportParamsMutex); 59 if(theInstance == nullptr) { 59 if(theInstance == nullptr) { 60 static G4TransportationParameters manage 60 static G4TransportationParameters manager; 61 theInstance = &manager; 61 theInstance = &manager; 62 } 62 } 63 G4MUTEXUNLOCK(&transportParamsMutex); 63 G4MUTEXUNLOCK(&transportParamsMutex); 64 } 64 } 65 return theInstance; 65 return theInstance; 66 } 66 } 67 67 68 //-------------------------------------------- 68 //------------------------------------------------------------------------------ 69 69 70 G4bool G4TransportationParameters::IsLocked() 70 G4bool G4TransportationParameters::IsLocked() const 71 { 71 { 72 auto stateManager = G4StateManager::GetState 72 auto stateManager = G4StateManager::GetStateManager(); 73 73 74 auto state = stateManager->GetCurrentState() 74 auto state = stateManager->GetCurrentState(); 75 bool goodState = state == G4State_PreInit 75 bool goodState = state == G4State_PreInit 76 || state == G4State_Init 76 || state == G4State_Init 77 || state == G4State_Idle ; 77 || state == G4State_Idle ; 78 return ( !G4Threading::IsMasterThread() || ! 78 return ( !G4Threading::IsMasterThread() || !goodState ); 79 } 79 } 80 80 81 //-------------------------------------------- 81 //------------------------------------------------------------------------------ 82 82 83 G4bool G4TransportationParameters::SetNumberO 83 G4bool G4TransportationParameters::SetNumberOfTrials( G4int val ) 84 { 84 { 85 if(IsLocked()) { ReportLockError(__func__); 85 if(IsLocked()) { ReportLockError(__func__); return false; } 86 fNumberOfTrials = val; 86 fNumberOfTrials = val; 87 return true; 87 return true; 88 } 88 } 89 89 90 //-------------------------------------------- 90 //------------------------------------------------------------------------------ 91 91 92 G4bool G4TransportationParameters::SetWarning 92 G4bool G4TransportationParameters::SetWarningEnergy( double val ) 93 { 93 { 94 if(IsLocked()) { ReportLockError(__func__); 94 if(IsLocked()) { ReportLockError(__func__); return false; } 95 fWarningEnergy = val; 95 fWarningEnergy = val; 96 96 97 // Consistency check -- and trial fix 97 // Consistency check -- and trial fix 98 if( fWarningEnergy > fImportantEnergy ) { 98 if( fWarningEnergy > fImportantEnergy ) { 99 G4cerr << "G4TransportationParameters::G 99 G4cerr << "G4TransportationParameters::GetWarningEnergy enforcing warning-E <= important-E " 100 << " resetting important energy 100 << " resetting important energy from " << fImportantEnergy 101 << " to " << val << G4endl; 101 << " to " << val << G4endl; 102 // "Enforcing Important Energy >= Warnin 102 // "Enforcing Important Energy >= Warning Energy" 103 fImportantEnergy = fWarningEnergy; 103 fImportantEnergy = fWarningEnergy; 104 } 104 } 105 return true; 105 return true; 106 } 106 } 107 107 108 //-------------------------------------------- 108 //------------------------------------------------------------------------------ 109 109 110 G4bool G4TransportationParameters::SetImporta 110 G4bool G4TransportationParameters::SetImportantEnergy( double val ) 111 { 111 { 112 if(IsLocked()) { ReportLockError(__func__); 112 if(IsLocked()) { ReportLockError(__func__); return false; } 113 fImportantEnergy = val; 113 fImportantEnergy = val; 114 114 115 // Consistency check -- and trial fix 115 // Consistency check -- and trial fix 116 if( fImportantEnergy < fWarningEnergy ) { 116 if( fImportantEnergy < fWarningEnergy ) { 117 G4String mthd= G4String("G4Transportatio 117 G4String mthd= G4String("G4TransportationParameters")+G4String(__func__); 118 G4ExceptionDescription ed; 118 G4ExceptionDescription ed; 119 ed<<"enforcing hierarchy (warning-E <= i 119 ed<<"enforcing hierarchy (warning-E <= important-E): resetting important" 120 <<" energy from " << fImportantEnergy 120 <<" energy from " << fImportantEnergy << " to " << val << G4endl; 121 G4Exception( mthd, "Enforcing Warning En 121 G4Exception( mthd, "Enforcing Warning Energy <= Important Energy", 122 JustWarning, ed ); 122 JustWarning, ed ); 123 123 124 fWarningEnergy = fImportantEnergy; 124 fWarningEnergy = fImportantEnergy; 125 } 125 } 126 return true; 126 return true; 127 } 127 } 128 128 129 //-------------------------------------------- 129 //------------------------------------------------------------------------------ 130 130 131 G4bool G4TransportationParameters::SetWarningA 131 G4bool G4TransportationParameters::SetWarningAndImportantEnergies( G4double warnE, 132 132 G4double importE ) 133 { 133 { 134 if(IsLocked()) { 134 if(IsLocked()) { 135 ReportLockError(__func__); 135 ReportLockError(__func__); 136 return false; 136 return false; 137 } 137 } 138 138 139 if( warnE <= importE ) 139 if( warnE <= importE ) 140 { 140 { 141 fWarningEnergy = warnE; 141 fWarningEnergy = warnE; 142 fImportantEnergy = importE; 142 fImportantEnergy = importE; 143 } 143 } 144 else 144 else 145 { 145 { 146 fWarningEnergy = importE; 146 fWarningEnergy = importE; 147 fImportantEnergy = warnE; 147 fImportantEnergy = warnE; 148 148 149 G4String mthd= G4String("G4Transportation 149 G4String mthd= G4String("G4TransportationParameters")+G4String(__func__); 150 G4ExceptionDescription ed; 150 G4ExceptionDescription ed; 151 ed << "To enforce hierarchy (warning-E <= 151 ed << "To enforce hierarchy (warning-E <= important-E): " 152 << " using smaller value= " << import 152 << " using smaller value= " << importE << " as Warning Energy " 153 << " and larger value= " << warnE << 153 << " and larger value= " << warnE << " as Important Energy." << G4endl; 154 G4Exception( mthd, "Enforcing Warning En 154 G4Exception( mthd, "Enforcing Warning Energy <= Important Energy", 155 JustWarning, ed ); 155 JustWarning, ed ); 156 } 156 } 157 return true; 157 return true; 158 } 158 } 159 159 160 //-------------------------------------------- 160 //------------------------------------------------------------------------------ 161 161 162 void G4TransportationParameters::ReportLockErr 162 void G4TransportationParameters::ReportLockError(G4String methodName, G4bool verbose) const 163 { 163 { 164 // Report Incompatible States: GeomClosed , 164 // Report Incompatible States: GeomClosed , EventProc, (also Quit, Abort) 165 G4String namesMethodClass= G4String("G4Trans 165 G4String namesMethodClass= G4String("G4TransportationParameters") + methodName; 166 166 167 auto stateManager = G4StateManager::GetState 167 auto stateManager = G4StateManager::GetStateManager(); 168 auto state = stateManager->GetCurrentStat 168 auto state = stateManager->GetCurrentState(); 169 169 170 G4ExceptionDescription ed; 170 G4ExceptionDescription ed; 171 ed << "Cannot change values of G4Transportat 171 ed << "Cannot change values of G4TransportationParameters when G4State is " 172 << stateManager->GetStateString(state) << 172 << stateManager->GetStateString(state) << G4endl; 173 ed << "Only the following Geant4 state are c 173 ed << "Only the following Geant4 state are compatible: Pre_Init, Init and Idle." << G4endl; 174 if( verbose ) { 174 if( verbose ) { 175 ed << G4endl << "Values remain as follows 175 ed << G4endl << "Values remain as follows:" << G4endl; 176 StreamInfo( ed ); 176 StreamInfo( ed ); 177 } 177 } 178 G4Exception( namesMethodClass, 178 G4Exception( namesMethodClass, 179 "Locked, due to incompatible G4 179 "Locked, due to incompatible G4state: it not possible to change its parameters.", 180 JustWarning, ed ); 180 JustWarning, ed ); 181 } 181 } 182 182 183 //-------------------------------------------- 183 //------------------------------------------------------------------------------ 184 184 185 void G4TransportationParameters::StreamInfo(st 185 void G4TransportationParameters::StreamInfo(std::ostream& os) const 186 { 186 { 187 auto prec = os.precision(5); 187 auto prec = os.precision(5); 188 188 189 os << "Transport Parameters: " << G4endl; 189 os << "Transport Parameters: " << G4endl; 190 os << " Warning energy = " << GetWarning 190 os << " Warning energy = " << GetWarningEnergy() / CLHEP::MeV << " MeV " << G4endl; 191 os << " Important energy = " << GetImporta 191 os << " Important energy = " << GetImportantEnergy() / CLHEP::MeV << " MeV " << G4endl; 192 os << " Number of trials = " << GetNumberO 192 os << " Number of trials = " << GetNumberOfTrials() << G4endl; 193 os.precision(prec); 193 os.precision(prec); 194 } 194 } 195 195 196 //-------------------------------------------- 196 //------------------------------------------------------------------------------ 197 197 198 void G4TransportationParameters::Dump() const 198 void G4TransportationParameters::Dump() const 199 { 199 { 200 G4MUTEXLOCK(&transportParamsMutex); 200 G4MUTEXLOCK(&transportParamsMutex); 201 StreamInfo(G4cout); 201 StreamInfo(G4cout); 202 G4MUTEXUNLOCK(&transportParamsMutex); 202 G4MUTEXUNLOCK(&transportParamsMutex); 203 } 203 } 204 204 205 //-------------------------------------------- 205 //------------------------------------------------------------------------------ 206 206 207 G4bool G4TransportationParameters::SetHighLoop 207 G4bool G4TransportationParameters::SetHighLooperThresholds() 208 { 208 { 209 if(IsLocked()) { return false; } 209 if(IsLocked()) { return false; } 210 210 211 // Restores the old high values -- potential 211 // Restores the old high values -- potentially appropriate for energy-frontier 212 // HEP experiments. 212 // HEP experiments. 213 // Caution: All tracks with E < 100 MeV tha 213 // Caution: All tracks with E < 100 MeV that are found to loop are 214 SetWarningEnergy( 100.0 * CLHEP::MeV ); / 214 SetWarningEnergy( 100.0 * CLHEP::MeV ); // Warn above this energy 215 SetImportantEnergy( 250.0 * CLHEP::MeV ); / 215 SetImportantEnergy( 250.0 * CLHEP::MeV ); // Extra trial above this En 216 216 217 G4int maxTrials = 10; 217 G4int maxTrials = 10; 218 SetNumberOfTrials( maxTrials ); 218 SetNumberOfTrials( maxTrials ); 219 219 220 return true; 220 return true; 221 } 221 } 222 222 223 //-------------------------------------------- 223 //------------------------------------------------------------------------------ 224 224 225 G4bool G4TransportationParameters::SetIntermed 225 G4bool G4TransportationParameters::SetIntermediateLooperThresholds() 226 { 226 { 227 if(IsLocked()) { return false; } // Currentl 227 if(IsLocked()) { return false; } // Currently must not change during loop - may relax 228 228 229 // Medium values -- reasonable default for a 229 // Medium values -- reasonable default for an unknown application 230 fWarningEnergy= 1.0 * CLHEP::MeV; // War 230 fWarningEnergy= 1.0 * CLHEP::MeV; // Warn above this energy 231 fImportantEnergy= 10.0 * CLHEP::MeV; // Ext 231 fImportantEnergy= 10.0 * CLHEP::MeV; // Extra trial above this En 232 fNumberOfTrials= 10; 232 fNumberOfTrials= 10; 233 return true; 233 return true; 234 } 234 } 235 235 236 //-------------------------------------------- 236 //------------------------------------------------------------------------------ 237 237 238 G4bool G4TransportationParameters::SetLowLoope 238 G4bool G4TransportationParameters::SetLowLooperThresholds() // Values for low-E applications 239 { 239 { 240 if(IsLocked()) { return false; } 240 if(IsLocked()) { return false; } 241 241 242 // These values were the default in Geant4 1 242 // These values were the default in Geant4 10.5 - beta 243 SetWarningEnergy( 1.0 * CLHEP::keV ); // 243 SetWarningEnergy( 1.0 * CLHEP::keV ); // Warn above this En 244 SetImportantEnergy( 1.0 * CLHEP::MeV ); // 244 SetImportantEnergy( 1.0 * CLHEP::MeV ); // Extra trials above it 245 245 246 G4int maxTrials = 30; 246 G4int maxTrials = 30; 247 SetNumberOfTrials( maxTrials ); 247 SetNumberOfTrials( maxTrials ); 248 248 249 return true; 249 return true; 250 } 250 } 251 251 252 //-------------------------------------------- 252 //------------------------------------------------------------------------------ 253 253 254 G4bool G4TransportationParameters::EnableUseOf 254 G4bool G4TransportationParameters::EnableUseOfMagneticMoment(G4bool useMoment) 255 { 255 { 256 if(IsLocked()) { return false; } 256 if(IsLocked()) { return false; } 257 fUseMagneticMoment= useMoment; 257 fUseMagneticMoment= useMoment; 258 return true; 258 return true; 259 } 259 } 260 260 261 //-------------------------------------------- 261 //------------------------------------------------------------------------------ 262 262 263 263 264 G4bool G4TransportationParameters::SetSilenceA 264 G4bool G4TransportationParameters::SetSilenceAllLooperWarnings(G4bool val) 265 { 265 { 266 if(IsLocked()) { return false; } 266 if(IsLocked()) { return false; } 267 fSilenceLooperWarnings= val; 267 fSilenceLooperWarnings= val; 268 return true; 268 return true; 269 } 269 } 270 270