12 #include <simulation/kernel/ExtManager.h>
13 #include <simulation/kernel/RunManager.h>
14 #include <simulation/kernel/MagneticField.h>
15 #include <simulation/kernel/DetectorConstruction.h>
16 #include <simulation/kernel/ExtPhysicsList.h>
21 #include <G4UImanager.hh>
22 #include <G4RunManager.hh>
23 #include <G4EventManager.hh>
24 #include <G4RegionStore.hh>
25 #include <G4ProductionCutsTable.hh>
26 #include <G4ErrorPropagatorData.hh>
27 #include <G4ErrorPropagator.hh>
28 #include <G4StateManager.hh>
29 #include <G4TransportationManager.hh>
30 #include <G4FieldManager.hh>
31 #include <G4CachedMagneticField.hh>
32 #include <G4Mag_UsualEqRhs.hh>
33 #include <G4ErrorMag_UsualEqRhs.hh>
34 #include <G4MagIntegratorStepper.hh>
35 #include <G4NystromRK4.hh>
36 #include <G4HelixExplicitEuler.hh>
37 #include <G4HelixSimpleRunge.hh>
38 #include <G4EquationOfMotion.hh>
39 #include <G4ChordFinder.hh>
40 #include <G4LossTableManager.hh>
41 #include <G4VisExecutive.hh>
42 #include <G4VExceptionHandler.hh>
44 #include <framework/logging/Logger.h>
47 using namespace Belle2::Simulation;
59 virtual bool Notify(
const char* origin,
const char* code, G4ExceptionSeverity,
const char* description)
62 if (strstr(description,
"Error returned: 3") !=
nullptr) {
63 B2DEBUG(1,
"In " << origin <<
", " << code <<
": " << description);
65 }
else if (strstr(code,
"GeomNav0003") !=
nullptr) {
66 B2DEBUG(20,
"In " << origin <<
", " << code <<
": " << description);
69 B2ERROR(
"In " << origin <<
", " << code <<
": " << description);
82 m_G4State(G4StateManager::GetStateManager()->GetCurrentState()),
83 m_Propagator(nullptr),
85 m_TrackingAction(nullptr),
86 m_SteppingAction(nullptr),
87 m_MagneticField(nullptr),
88 m_UncachedField(nullptr),
89 m_MagFldEquation(nullptr),
91 m_ChordFinder(nullptr),
92 m_StdStepper(nullptr),
93 m_ForwardEquationOfMotion(nullptr),
94 m_BackwardEquationOfMotion(nullptr),
98 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_PreInit);
112 m_G4RunMgr->SetUserAction((G4UserTrackingAction*)NULL);
113 m_G4RunMgr->SetUserAction((G4UserSteppingAction*)NULL);
115 if (mode == G4ErrorMode_PropBackwards) {
122 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_Propagating);
127 G4ErrorPropagatorData::GetErrorPropagatorData()->SetMode(mode);
129 G4VExceptionHandler* savedHandler = G4StateManager::GetStateManager()->GetExceptionHandler();
133 G4int result =
m_Propagator->PropagateOneStep(currentTS);
135 G4StateManager::GetStateManager()->SetExceptionHandler(savedHandler);
141 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_Init);
146 if (mode == G4ErrorMode_PropBackwards) {
155 if (G4ErrorPropagatorData::GetErrorPropagatorData()->GetState() == G4ErrorState_PreInit)
return;
176 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_PreInit);
180 double magneticCacheDistance,
181 double deltaChordInMagneticField,
182 bool enableVisualization,
183 int trackingVerbosity,
184 const std::vector<std::string>& uiCommands)
187 int status = (
m_G4State == G4State_PreInit) ? 0 : 2;
189 if (G4ErrorPropagatorData::GetErrorPropagatorData()->GetState() == G4ErrorState_Init) {
192 B2INFO(
"ExtManager::Initialize(): " << caller <<
" will run without FullSim; the extrapolator has already been initialized");
194 B2INFO(
"ExtManager::Initialize(): " << caller <<
" will run with FullSim; the extrapolator has already been initialized");
199 G4FieldManager* fieldManager = G4TransportationManager::GetTransportationManager()->GetFieldManager();
212 if (magneticFieldName !=
"none") {
214 if (magneticCacheDistance > 0.0) {
219 if (magneticFieldName !=
"default") {
223 if (magneticFieldName ==
"nystrom") {
225 }
else if (magneticFieldName ==
"expliciteuler") {
227 }
else if (magneticFieldName ==
"simplerunge") {
230 B2FATAL(
"Unknown magnetic field option: " << magneticFieldName);
242 G4ChordFinder* chordFinder = fieldManager->GetChordFinder();
243 B2DEBUG(1,
"Geant4 default deltaChord = " << chordFinder->GetDeltaChord());
244 chordFinder->SetDeltaChord(deltaChordInMagneticField * CLHEP::mm);
245 B2DEBUG(1,
"DeltaChord after reset = " << chordFinder->GetDeltaChord());
250 int g4VerboseLevel = 0;
256 default: g4VerboseLevel = 0;
258 G4EventManager::GetEventManager()->SetVerboseLevel(g4VerboseLevel);
259 G4RunManager::GetRunManager()->SetVerboseLevel(g4VerboseLevel);
260 G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(
262 G4LossTableManager::Instance()->SetVerbose(g4VerboseLevel);
264 if (enableVisualization) {
270 if (uiCommands.size() > 0) {
271 G4UImanager* uiManager = G4UImanager::GetUIpointer();
272 for (std::vector<std::string>::const_iterator iter = uiCommands.begin(); iter != uiCommands.end(); ++iter) {
273 uiManager->ApplyCommand(*iter);
279 G4Region* region = (*(G4RegionStore::GetInstance()))[0];
280 region->SetProductionCuts(G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
283 B2INFO(
"ExtManager::Initialize(): Perform Geant4 final initialization: Geometry optimization, ExtPhysicsList calculations...");
285 B2INFO(
"ExtManager::Initialize(): done, Geant4 ready (for extrapolation only)");
286 G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
291 m_StdStepper =
const_cast<G4MagIntegratorStepper*
>(fieldManager->GetChordFinder()->GetIntegrationDriver()->GetStepper());
296 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_Init);
298 B2INFO(
"ExtManager::Initialize(): " << caller <<
" will run without FullSim and has initialized the extrapolator");
300 B2INFO(
"ExtManager::Initialize(): " << caller <<
" will run with FullSim and has initialized the extrapolator");