9#include <simulation/kernel/ExtManager.h>
10#include <simulation/kernel/RunManager.h>
11#include <simulation/kernel/MagneticField.h>
12#include <simulation/kernel/DetectorConstruction.h>
13#include <simulation/kernel/ExtPhysicsList.h>
18#include <G4UImanager.hh>
19#include <G4RunManager.hh>
20#include <G4EventManager.hh>
21#include <G4RegionStore.hh>
22#include <G4ProductionCutsTable.hh>
23#include <G4ErrorPropagatorData.hh>
24#include <G4ErrorPropagator.hh>
25#include <G4StateManager.hh>
26#include <G4TransportationManager.hh>
27#include <G4FieldManager.hh>
28#include <G4CachedMagneticField.hh>
29#include <G4Mag_UsualEqRhs.hh>
30#include <G4ErrorMag_UsualEqRhs.hh>
31#include <G4MagIntegratorStepper.hh>
32#include <G4NystromRK4.hh>
33#include <G4HelixExplicitEuler.hh>
34#include <G4HelixSimpleRunge.hh>
35#include <G4EquationOfMotion.hh>
36#include <G4ChordFinder.hh>
37#include <G4LossTableManager.hh>
38#include <G4VisExecutive.hh>
39#include <G4VExceptionHandler.hh>
41#include <framework/logging/Logger.h>
44using namespace Belle2::Simulation;
56 virtual bool Notify(
const char* origin,
const char* code, G4ExceptionSeverity,
const char* description)
59 if (strstr(description,
"Error returned: 3") !=
nullptr) {
60 B2DEBUG(1,
"In " << origin <<
", " << code <<
": " << description);
62 }
else if (strstr(code,
"GeomNav0003") !=
nullptr) {
63 B2DEBUG(20,
"In " << origin <<
", " << code <<
": " << description);
66 B2ERROR(
"In " << origin <<
", " << code <<
": " << description);
79 m_G4State(G4StateManager::GetStateManager()->GetCurrentState()),
80 m_Propagator(nullptr),
82 m_TrackingAction(nullptr),
83 m_SteppingAction(nullptr),
84 m_MagneticField(nullptr),
85 m_UncachedField(nullptr),
86 m_MagFldEquation(nullptr),
88 m_ChordFinder(nullptr),
89 m_StdStepper(nullptr),
90 m_ForwardEquationOfMotion(nullptr),
91 m_BackwardEquationOfMotion(nullptr),
95 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_PreInit);
109 m_G4RunMgr->SetUserAction((G4UserTrackingAction*)NULL);
110 m_G4RunMgr->SetUserAction((G4UserSteppingAction*)NULL);
112 if (mode == G4ErrorMode_PropBackwards) {
119 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_Propagating);
124 G4ErrorPropagatorData::GetErrorPropagatorData()->SetMode(mode);
126 G4VExceptionHandler* savedHandler = G4StateManager::GetStateManager()->GetExceptionHandler();
130 G4int result =
m_Propagator->PropagateOneStep(currentTS);
132 G4StateManager::GetStateManager()->SetExceptionHandler(savedHandler);
138 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_Init);
143 if (mode == G4ErrorMode_PropBackwards) {
152 if (G4ErrorPropagatorData::GetErrorPropagatorData()->GetState() == G4ErrorState_PreInit)
return;
173 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_PreInit);
177 double magneticCacheDistance,
178 double deltaChordInMagneticField,
179 bool enableVisualization,
180 int trackingVerbosity,
181 const std::vector<std::string>& uiCommands)
184 int status = (
m_G4State == G4State_PreInit) ? 0 : 2;
186 if (G4ErrorPropagatorData::GetErrorPropagatorData()->GetState() == G4ErrorState_Init) {
189 B2INFO(
"ExtManager::Initialize(): " << caller <<
" will run without FullSim; the extrapolator has already been initialized");
191 B2INFO(
"ExtManager::Initialize(): " << caller <<
" will run with FullSim; the extrapolator has already been initialized");
196 G4FieldManager* fieldManager = G4TransportationManager::GetTransportationManager()->GetFieldManager();
209 if (magneticFieldName !=
"none") {
211 if (magneticCacheDistance > 0.0) {
216 if (magneticFieldName !=
"default") {
220 if (magneticFieldName ==
"nystrom") {
222 }
else if (magneticFieldName ==
"expliciteuler") {
224 }
else if (magneticFieldName ==
"simplerunge") {
227 B2FATAL(
"Unknown magnetic field option: " << magneticFieldName);
239 G4ChordFinder* chordFinder = fieldManager->GetChordFinder();
240 B2DEBUG(1,
"Geant4 default deltaChord = " << chordFinder->GetDeltaChord());
241 chordFinder->SetDeltaChord(deltaChordInMagneticField * CLHEP::mm);
242 B2DEBUG(1,
"DeltaChord after reset = " << chordFinder->GetDeltaChord());
247 int g4VerboseLevel = 0;
253 default: g4VerboseLevel = 0;
255 G4EventManager::GetEventManager()->SetVerboseLevel(g4VerboseLevel);
256 G4RunManager::GetRunManager()->SetVerboseLevel(g4VerboseLevel);
257 G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(
259 G4LossTableManager::Instance()->SetVerbose(g4VerboseLevel);
261 if (enableVisualization) {
267 if (uiCommands.size() > 0) {
268 G4UImanager* uiManager = G4UImanager::GetUIpointer();
269 for (std::vector<std::string>::const_iterator iter = uiCommands.begin(); iter != uiCommands.end(); ++iter) {
270 uiManager->ApplyCommand(*iter);
276 G4Region* region = (*(G4RegionStore::GetInstance()))[0];
277 region->SetProductionCuts(G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
280 B2INFO(
"ExtManager::Initialize(): Perform Geant4 final initialization: Geometry optimization, ExtPhysicsList calculations...");
282 B2INFO(
"ExtManager::Initialize(): done, Geant4 ready (for extrapolation only)");
283 G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
288 m_StdStepper =
const_cast<G4MagIntegratorStepper*
>(fieldManager->GetChordFinder()->GetIntegrationDriver()->GetStepper());
293 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_Init);
295 B2INFO(
"ExtManager::Initialize(): " << caller <<
" will run without FullSim and has initialized the extrapolator");
297 B2INFO(
"ExtManager::Initialize(): " << caller <<
" will run with FullSim and has initialized the extrapolator");
Class responsible to connect to geometry to simulation.
@ c_Info
Info: for informational messages, e.g.
@ c_Debug
Debug: for code development.
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
It is the main interface for the user to define the setup and start the propagation.
G4VisManager * m_VisManager
Pointer to the visualization manager (if used)
G4RunManager * m_G4RunMgr
Pointer to the simulation's G4RunManager (if any)
G4MagIntegratorStepper * m_Stepper
Pointer to the equation-of-motion stepper (if not the default)
G4VExceptionHandler * m_StepExceptionHandler
Pointer to the G4VExceptionHandler used in PropagateOneStep()
static ExtManager * GetManager()
Get pointer to the instance of this singleton class (create if needed)
G4MagIntegratorStepper * m_StdStepper
Pointer to the standard equation-of-motion stepper.
void RunTermination()
Terminate a run and set state to G4ErrorState_Init.
void EventTermination(G4ErrorMode)
Terminate an event and set state to G4ErrorState_Init.
static ExtManager * m_Manager
Stores pointer to the singleton class.
G4MagneticField * m_UncachedField
Pointer to the uncached magnetic field (might be superseded by its cached version)
G4Mag_UsualEqRhs * m_MagFldEquation
Pointer to the equation of motion in the magnetic field (if not the default)
G4MagneticField * m_MagneticField
Pointer to the (un)cached magnetic field.
G4UserTrackingAction * m_TrackingAction
Pointer to the simulation's TrackingAction (if any)
G4EquationOfMotion * m_ForwardEquationOfMotion
Pointer to the forward-propagation equation of motion.
G4ApplicationState m_G4State
Initial state of the G4RunManager (=PreInitif FullSimModule not present)
G4UserSteppingAction * m_SteppingAction
Pointer to the simulation's SteppingAction (if any)
void InitTrackPropagation(G4ErrorMode)
Initialize for propagation of a track and set state to G4ErrorState_Propagating.
ExtManager()
constructor is hidden: user calls ExtManager::GetManager() instead
G4EquationOfMotion * m_BackwardEquationOfMotion
Pointer to the equation of motion that accommodates back-propagation.
void Initialize(const char[], const std::string &, double, double, bool, int, const std::vector< std::string > &)
Initialize Geant4 and Geant4e.
G4int PropagateOneStep(G4ErrorTrajState *currentTS, G4ErrorMode mode=G4ErrorMode_PropForwards)
Propagate a track by one step.
G4ChordFinder * m_ChordFinder
Pointer to the equation-of-motion chord finder (if not the default)
G4ErrorPropagator * m_Propagator
Stores pointer to the propagator.
Standalone physics list for the geant4e extrapolator.
The Class for the Belle2 magnetic field implementation for Geant4.
The run manager controls the flow of the Geant4 program and manages the event loop(s) within a run.
void beginRun(int runNumber)
Prepares Geant4 for a new run.
void destroy()
Destroys the RunManager at the end of the simulation.
void endRun()
Terminates a Geant4 run.
void Initialize()
Initialize the Kernel.
static RunManager & Instance()
Static method to get a reference to the RunManager instance.
Class to handle G4Exception raised during PropagateOneStep()
virtual bool Notify(const char *origin, const char *code, G4ExceptionSeverity, const char *description)
G4VExceptionHandler method called when an exception is raised.
~StepExceptionHandler()
Destructor.
StepExceptionHandler()
Constructor.
Abstract base class for different kinds of events.