BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSRunManager.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSRunManager.hh"
19#include "BDSDebug.hh"
20#include "BDSDetectorConstruction.hh"
21#include "BDSExceptionHandler.hh"
22#include "BDSExtent.hh"
23#include "BDSFieldQuery.hh"
24#include "BDSPrimaryGeneratorAction.hh"
25
26#include "G4UImanager.hh"
27
28#include "CLHEP/Random/Random.h"
29
30BDSRunManager::BDSRunManager()
31{
32 // Construct an exception handler to catch Geant4 aborts.
33 // This has to be done after G4RunManager::G4RunManager() which constructs
34 // its own default exception handler which overwrites the one in G4StateManager
35 exceptionHandler = new BDSExceptionHandler();
36}
37
38BDSRunManager::~BDSRunManager()
39{
40 delete exceptionHandler;
41}
42
44{
45 G4RunManager::Initialize();
46
47 BDSExtent worldExtent;
48 if (const auto detectorConstruction = dynamic_cast<BDSDetectorConstruction*>(userDetector))
49 {
50 worldExtent = detectorConstruction->WorldExtent();
51
53 const auto& fieldQueries = detectorConstruction->FieldQueries();
54 if (!fieldQueries.empty())
55 {
56 BDSFieldQuery querier;
57 querier.QueryFields(fieldQueries);
58 }
59 }
60 if (const auto primaryGeneratorAction = dynamic_cast<BDSPrimaryGeneratorAction*>(userPrimaryGeneratorAction))
61 {primaryGeneratorAction->SetWorldExtent(worldExtent);}
62}
63
64void BDSRunManager::BeamOn(G4int n_event,const char* macroFile,G4int n_select)
65{
66 G4RunManager::BeamOn(n_event,macroFile,n_select);
67}
68
69void BDSRunManager::DoEventLoop(G4int n_event,const char* macroFile,G4int n_select)
70{
71 // save event loop state
72 if (verboseLevel > 0)
73 {
74 // Print seed to try and recreate an event in a run
75 G4cout << __METHOD_NAME__ << "Random number generator's seed="
76 << CLHEP::HepRandom::getTheSeed() << G4endl;
77 // Print generator full state to output
78 G4cout << __METHOD_NAME__ << "Random number generator's state: " << G4endl;
79 CLHEP::HepRandom::saveFullState(G4cout);
80 }
81
82 G4RunManager::DoEventLoop(n_event, macroFile, n_select);
83}
84
86{
87 // additional output
88 if (verboseLevel > 3)
89 {
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 // This is the same as in G4RunManager, but we check the aborted event after the primary generator action
102 currentEvent = GenerateEvent(i_event);
103 if (currentEvent->IsAborted())
104 {return;}
105 eventManager->ProcessOneEvent(currentEvent);
106 AnalyzeEvent(currentEvent);
107 UpdateScoring();
108 if (i_event < n_select_msg)
109 {G4UImanager::GetUIpointer()->ApplyCommand(msgText);}
110}
111
113{
114 G4cout << "Terminate run - trying to write and close output file" << G4endl;
115 G4RunManager::AbortRun();
116}
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:38
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.