BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSRunManager.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSRunManager.hh"
20#include "BDSDebug.hh"
21#include "BDSDetectorConstruction.hh"
22#include "BDSExceptionHandler.hh"
23#include "BDSExtent.hh"
24#include "BDSFieldQuery.hh"
25#include "BDSPrimaryGeneratorAction.hh"
26
27#include "CLHEP/Random/Random.h"
28
29BDSRunManager::BDSRunManager()
30{
31 // Construct an exception handler to catch Geant4 aborts.
32 // This has to be done after G4RunManager::G4RunManager() which constructs
33 // its own default exception handler which overwrites the one in G4StateManager
34 exceptionHandler = new BDSExceptionHandler();
35}
36
37BDSRunManager::~BDSRunManager()
38{
39 delete exceptionHandler;
40}
41
43{
44#ifdef BDSDEBUG
45 G4cout << __METHOD_NAME__ << "> Initialising Geant4 kernel"<<G4endl;
46#endif
47 G4RunManager::Initialize();
48
49 BDSExtent worldExtent;
50 if (const auto detectorConstruction = dynamic_cast<BDSDetectorConstruction*>(userDetector))
51 {
52 worldExtent = detectorConstruction->WorldExtent();
53
55 const auto& fieldQueries = detectorConstruction->FieldQueries();
56 if (!fieldQueries.empty())
57 {
58 BDSFieldQuery querier;
59 querier.QueryFields(fieldQueries);
60 }
61 }
62 if (const auto primaryGeneratorAction = dynamic_cast<BDSPrimaryGeneratorAction*>(userPrimaryGeneratorAction))
63 {primaryGeneratorAction->SetWorldExtent(worldExtent);}
64}
65
66void BDSRunManager::BeamOn(G4int n_event,const char* macroFile,G4int n_select)
67{
68 G4RunManager::BeamOn(n_event,macroFile,n_select);
69}
70
71void BDSRunManager::DoEventLoop(G4int n_event,const char* macroFile,G4int n_select)
72{
73 // save event loop state
74 if(verboseLevel>0){
75 // Print seed to try and recreate an event in a run
76 G4cout << __METHOD_NAME__ << "Random number generator's seed="
77 << CLHEP::HepRandom::getTheSeed() << G4endl;
78 // Print generator full state to output
79 G4cout << __METHOD_NAME__ << "Random number generator's state: " << G4endl;
80 CLHEP::HepRandom::saveFullState(G4cout);
81 }
82
83 G4RunManager::DoEventLoop(n_event,macroFile,n_select);
84}
85
87{
88 // additional output
89 if(verboseLevel>3){
90 G4cout << __METHOD_NAME__ << "Event="<<i_event<<G4endl;
91 // Print seed to try and recreate an event in a run
92 G4cout << __METHOD_NAME__ << "Random number generator's seed="
93 << CLHEP::HepRandom::getTheSeed() << G4endl;
94 // Print generator full state to output
95 G4cout << __METHOD_NAME__ << "Random number generator's state: " << G4endl;
96 CLHEP::HepRandom::saveFullState(G4cout);
97 }
98
99 G4RunManager::ProcessOneEvent(i_event);
100}
101
103{
104 G4cout << "Terminate run - trying to write and close output file" << G4endl;
105 G4RunManager::AbortRun();
106}
107
Class that constructs a Geant4 model of an accelerator.
Handler that overrides Geant4's behaviour back to a normal exception.
Holder for +- extents in 3 dimensions.
Definition: BDSExtent.hh:39
Class for querying the Geant4 model for field at any point.
void QueryFields(const std::vector< BDSFieldQueryInfo * > &fieldQueries)
Vector version of above function. Unique output files for each query.
Generates primary particle vertices using BDSBunch.
virtual void Initialize()
virtual void ProcessOneEvent(G4int i_event)
For additional output.
virtual void DoEventLoop(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1)
For additional output.
virtual void AbortRun(G4bool)
Run G4RunManager:AbortRun(), but give some print out feedback for the user.
virtual void BeamOn(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1)
Altered BeamOn function to account for Placet synchronisation.