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 <G4StateManager.hh>
25#include <G4TransportationManager.hh>
26#include <G4FieldManager.hh>
27#include <G4CachedMagneticField.hh>
28#include <G4Mag_UsualEqRhs.hh>
29#include <G4ErrorMag_UsualEqRhs.hh>
30#include <G4MagIntegratorStepper.hh>
31#include <G4NystromRK4.hh>
32#include <G4HelixExplicitEuler.hh>
33#include <G4HelixSimpleRunge.hh>
34#include <G4EquationOfMotion.hh>
35#include <G4ChordFinder.hh>
36#include <G4LossTableManager.hh>
37#include <G4VisExecutive.hh>
38#include <G4VExceptionHandler.hh>
40#include <framework/logging/Logger.h>
43using namespace Belle2::Simulation;
55 virtual bool Notify(
const char* origin,
const char* code, G4ExceptionSeverity,
const char* description)
58 if (strstr(description,
"Error returned: 3") !=
nullptr) {
59 B2DEBUG(1,
"In " << origin <<
", " << code <<
": " << description);
61 }
else if (strstr(code,
"GeomNav0003") !=
nullptr) {
62 B2DEBUG(20,
"In " << origin <<
", " << code <<
": " << description);
65 B2ERROR(
"In " << origin <<
", " << code <<
": " << description);
78 m_G4State(G4StateManager::GetStateManager()->GetCurrentState()),
94 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_PreInit);
108 m_G4RunMgr->SetUserAction((G4UserTrackingAction*)NULL);
109 m_G4RunMgr->SetUserAction((G4UserSteppingAction*)NULL);
111 if (mode == G4ErrorMode_PropBackwards) {
118 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_Propagating);
123 G4ErrorPropagatorData::GetErrorPropagatorData()->SetMode(mode);
125 G4VExceptionHandler* savedHandler = G4StateManager::GetStateManager()->GetExceptionHandler();
129 G4int result =
m_Propagator->PropagateOneStep(currentTS);
131 G4StateManager::GetStateManager()->SetExceptionHandler(savedHandler);
137 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_Init);
142 if (mode == G4ErrorMode_PropBackwards) {
151 if (G4ErrorPropagatorData::GetErrorPropagatorData()->GetState() == G4ErrorState_PreInit)
return;
172 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_PreInit);
176 double magneticCacheDistance,
177 double deltaChordInMagneticField,
178 bool enableVisualization,
179 int trackingVerbosity,
180 const std::vector<std::string>& uiCommands)
183 int status = (
m_G4State == G4State_PreInit) ? 0 : 2;
185 if (G4ErrorPropagatorData::GetErrorPropagatorData()->GetState() == G4ErrorState_Init) {
188 B2INFO(
"ExtManager::Initialize(): " << caller <<
" will run without FullSim; the extrapolator has already been initialized");
190 B2INFO(
"ExtManager::Initialize(): " << caller <<
" will run with FullSim; the extrapolator has already been initialized");
195 G4FieldManager* fieldManager = G4TransportationManager::GetTransportationManager()->GetFieldManager();
208 if (magneticFieldName !=
"none") {
210 if (magneticCacheDistance > 0.0) {
215 if (magneticFieldName !=
"default") {
219 if (magneticFieldName ==
"nystrom") {
221 }
else if (magneticFieldName ==
"expliciteuler") {
223 }
else if (magneticFieldName ==
"simplerunge") {
226 B2FATAL(
"Unknown magnetic field option: " << magneticFieldName);
238 G4ChordFinder* chordFinder = fieldManager->GetChordFinder();
239 B2DEBUG(1,
"Geant4 default deltaChord = " << chordFinder->GetDeltaChord());
240 chordFinder->SetDeltaChord(deltaChordInMagneticField * CLHEP::mm);
241 B2DEBUG(1,
"DeltaChord after reset = " << chordFinder->GetDeltaChord());
246 int g4VerboseLevel = 0;
252 default: g4VerboseLevel = 0;
254 G4EventManager::GetEventManager()->SetVerboseLevel(g4VerboseLevel);
255 G4RunManager::GetRunManager()->SetVerboseLevel(g4VerboseLevel);
256 G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(
258 G4LossTableManager::Instance()->SetVerbose(g4VerboseLevel);
260 if (enableVisualization) {
266 if (uiCommands.size() > 0) {
267 G4UImanager* uiManager = G4UImanager::GetUIpointer();
268 for (std::vector<std::string>::const_iterator iter = uiCommands.begin(); iter != uiCommands.end(); ++iter) {
269 uiManager->ApplyCommand(*iter);
275 G4Region* region = (*(G4RegionStore::GetInstance()))[0];
276 region->SetProductionCuts(G4ProductionCutsTable::GetProductionCutsTable()->GetDefaultProductionCuts());
279 B2INFO(
"ExtManager::Initialize(): Perform Geant4 final initialization: Geometry optimization, ExtPhysicsList calculations...");
281 B2INFO(
"ExtManager::Initialize(): done, Geant4 ready (for extrapolation only)");
282 G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
287 m_StdStepper =
const_cast<G4MagIntegratorStepper*
>(fieldManager->GetChordFinder()->GetIntegrationDriver()->GetStepper());
292 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState(G4ErrorState_Init);
294 B2INFO(
"ExtManager::Initialize(): " << caller <<
" will run without FullSim and has initialized the extrapolator");
296 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.