19#include "BDSIMClass.hh"
21#include "BDSExecOptions.hh"
22#include "BDSGlobalConstants.hh"
29#include "G4EventManager.hh"
30#include "G4GenericBiasingPhysics.hh"
31#include "G4GeometryManager.hh"
32#include "G4GeometryTolerance.hh"
33#include "G4PhysicsListHelper.hh"
34#include "G4ParallelWorldPhysics.hh"
35#include "G4ParticleDefinition.hh"
36#include "G4SteppingManager.hh"
37#include "G4TrackingManager.hh"
38#include "G4Version.hh"
39#include "G4VModularPhysicsList.hh"
41#include "CLHEP/Units/SystemOfUnits.h"
43#include "BDSAcceleratorModel.hh"
44#include "BDSAperturePointsLoader.hh"
45#include "BDSBeamPipeFactory.hh"
47#include "BDSBunchFactory.hh"
48#include "BDSCavityFactory.hh"
49#include "BDSColours.hh"
50#include "BDSComponentFactoryUser.hh"
52#include "BDSDetectorConstruction.hh"
53#include "BDSEventAction.hh"
54#include "BDSException.hh"
55#include "BDSFieldFactory.hh"
56#include "BDSFieldLoader.hh"
57#include "BDSGeometryFactory.hh"
58#include "BDSGeometryFactorySQL.hh"
59#include "BDSGeometryWriter.hh"
60#include "BDSIonDefinition.hh"
61#include "BDSMaterials.hh"
62#include "BDSOutput.hh"
63#include "BDSOutputFactory.hh"
64#include "BDSParallelWorldUtilities.hh"
65#include "BDSParser.hh"
66#include "BDSParticleDefinition.hh"
67#include "BDSPhysicsUtilities.hh"
68#include "BDSPrimaryGeneratorAction.hh"
69#include "BDSRandom.hh"
70#include "BDSRunAction.hh"
71#include "BDSRunManager.hh"
72#include "BDSSamplerRegistry.hh"
73#include "BDSSDManager.hh"
74#include "BDSSteppingAction.hh"
75#include "BDSStackingAction.hh"
76#include "BDSTemporaryFiles.hh"
77#include "BDSTrackingAction.hh"
78#include "BDSUtilities.hh"
79#include "BDSVisManager.hh"
80#include "BDSWarning.hh"
86 initialisationResult(1),
93 userComponentFactory(nullptr),
94 userPhysicsList(nullptr),
100 usualPrintOut(usualPrintOutIn),
102 initialisationResult(1),
109 userComponentFactory(nullptr),
110 userPhysicsList(nullptr),
130 {execOptions->
Print();}
135 G4cout << __METHOD_NAME__ <<
"DEBUG mode is on." << G4endl;
158 BDSRandom::CreateRandomNumberGenerator(globals->RandomEngine());
159 BDSRandom::SetSeed();
163 globals->OutputFileName());
168 G4cerr <<
"No Geant4 environmental variables found - please source geant4.sh environment" << G4endl;
169 G4cout <<
"A common fault is the wrong Geant4 environment as compared to the one BDSIM was compiled with." << G4endl;
189 G4cout << __METHOD_NAME__ <<
"> Constructing physics processes" << G4endl;
193#if G4VERSION_NUMBER > 1049
199 G4PhysicsListHelper::GetPhysicsListHelper()->UseLowLooperThresholds();
207 G4int physicsVerbosity = globals->PhysicsVerbosity();
208 G4VModularPhysicsList* physList;
211 G4cout <<
"Using externally registered user defined physics list" << G4endl;
228 G4bool beamDifferentFromDesignParticle =
false;
233 beamDifferentFromDesignParticle);
234 G4double minEK = globals->MinimumKineticEnergy();
236 {
throw BDSException(
"option, minimumKineticEnergy is higher than kinetic energy of the beam - all primary particles wil be killed!");}
239 G4cout <<
"Design particle properties: " << G4endl << *designParticle;
240 if (beamDifferentFromDesignParticle)
241 {G4cout <<
"Beam particle properties: " << G4endl << *beamParticle;}
246 BDSGeometryFactorySQL::SetDefaultRigidity(designParticle->
BRho());
254 {physList->RegisterPhysics(biasPhysics);}
260 globals->BeamlineTransform(),
261 globals->BeamlineS(),
262 globals->GeneratePrimariesOnly());
263 G4cout <<
"Bunch distribution: \"" <<
bdsBunch->
Name() <<
"\"" << G4endl;
274 if (globals->GeneratePrimariesOnly())
281 G4GeometryTolerance* theGeometryTolerance = G4GeometryTolerance::GetInstance();
284 G4cout << __METHOD_NAME__ <<
"Geometry Tolerances: " << G4endl;
285 G4cout << __METHOD_NAME__ << std::setw(12) <<
"Surface: " << std::setw(7) << theGeometryTolerance->GetSurfaceTolerance() <<
" mm" << G4endl;
286 G4cout << __METHOD_NAME__ << std::setw(12) <<
"Angular: " << std::setw(7) << theGeometryTolerance->GetAngularTolerance() <<
" rad" << G4endl;
287 G4cout << __METHOD_NAME__ << std::setw(12) <<
"Radial: " << std::setw(7) << theGeometryTolerance->GetRadialTolerance() <<
" mm" << G4endl;
298 globals->StoreTrajectorySamplerID());
302 G4int verboseSteppingEventStart = globals->VerboseSteppingEventStart();
304 globals->VerboseSteppingEventContinueFor());
305 if (globals->VerboseSteppingBDSIM())
308 verboseSteppingEventStart,
309 verboseSteppingEventStop));
313 globals->StoreTrajectory(),
316 verboseSteppingEventStart,
317 verboseSteppingEventStop,
318 globals->VerboseSteppingPrimaryOnly(),
319 globals->VerboseSteppingLevel()));
324 runManager->SetUserAction(primaryGeneratorAction);
346 runManager->SetVerboseLevel(std::min(globals->VerboseRunLevel(), globals->PhysicsVerbosity()));
347 G4EventManager::GetEventManager()->SetVerboseLevel(globals->VerboseEventLevel());
348 G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(globals->VerboseTrackingLevel());
351 G4bool bCloseGeometry = G4GeometryManager::GetInstance()->CloseGeometry();
354 G4cerr << __METHOD_NAME__ <<
"error - geometry not closed." << G4endl;
358 if (globals->ExportGeometry())
362 globals->ExportFileName());
374 G4cout.precision(10);
376 struct sigaction act;
378 sigemptyset(&act.sa_mask);
381 {sigaction(SIGINT, &act,
nullptr);}
382 sigaction(SIGABRT, &act,
nullptr);
383 sigaction(SIGTERM, &act,
nullptr);
384 sigaction(SIGSEGV, &act,
nullptr);
415 G4GeometryManager::GetInstance()->OpenGeometry();
418 G4cout << __METHOD_NAME__ <<
"deleting..." << G4endl;
425 delete BDSSDManager::Instance();
429 delete BDSAcceleratorModel::Instance();
452 {G4cout << __METHOD_NAME__ <<
"End of Run. Thank you for using BDSIM!" << G4endl;}
459 {BDS::Warning(__METHOD_NAME__,
"BDSIM kernel already initialised - this component will not be available");}
464 componentConstructor);
471 const G4int nToGenerate = globals->NGenerate();
472 const G4int printModulo = globals->PrintModuloEvents();
474 auto flagsCache(G4cout.flags());
475 for (G4int i = 0; i < nToGenerate; i++)
477 if (i%printModulo == 0)
478 {G4cout <<
"\r Primary> " << std::fixed << i <<
" of " << nToGenerate << G4endl;}
484 G4cout.flags(flagsCache);
static BDSAperturePointsCache * Instance()
Access the singleton instance.
void ClearCachedFiles()
Delete all cached points from memory and clear the map of files loaded.
static BDSBeamPipeFactory * Instance()
Singleton accessor.
static BDSBunch * CreateBunch(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const G4Transform3D &beamlineTransform=G4Transform3D::Identity, G4double beamlineS=0, G4bool generatePrimariesOnlyIn=false)
factory method
virtual void BeginOfRunAction(G4int numberOfEvents, G4bool batchMode)
virtual G4bool ExpectChangingParticleType() const
A hint of whether we expect to require and extended particle set (ie pions, kaons,...
virtual const BDSParticleDefinition * ParticleDefinition() const
Access the beam particle definition.
G4String Name() const
Distribution name.
virtual BDSParticleCoordsFullGlobal GetNextParticleValid(G4int maxTries=100)
static BDSCavityFactory * Instance()
Singleton accessor.
static BDSColours * Instance()
singleton pattern
Interface class the developer should derive to construct their element.
Factory for user specified accelerator components.
void RegisterComponent(const G4String &componentTypeName, BDSComponentConstructor *componentConstructor)
Register a constructor instance by a given name.
Class that constructs a Geant4 model of an accelerator.
void SetDesignParticle(const BDSParticleDefinition *defIn)
Set the design particle definition.
G4bool BuildSamplerWorld() const
Process information at the event level.
General exception with possible name of object and message.
Executable option processing for BDSIM.
const GMAD::Options & Options() const
Accessor for options generated by command line parsing.
void Print() const
Print out the commands and their set values.
G4bool IgnoreSIGINT() const
Accessor.
const GMAD::Beam & Beam() const
Accessor for beam options generate by command line parsing.
void PrintCopyright() const
Print out the copyright information (no exit).
G4String InputFileName() const
Acessor for convenience for the one thing that's needed before the parser options.
static void SetDesignParticle(const BDSParticleDefinition *designParticleIn)
Update the internal cache of the rigidity.
static void SetPrimaryGeneratorAction(BDSPrimaryGeneratorAction *pgaIn)
Update the internal cache of the primary generator action.
static BDSFieldFactory * Instance()
Public accessor method for singleton pattern.
static BDSFieldLoader * Instance()
Singleton accessor.
static BDSGeometryFactory * Instance()
Singleton accessor.
A class for writing fully constructed geometry from BDSIM out to other formats.
void ExportGeometry(G4String geometryType, G4String geometryFileName)
A class that holds global options and constants.
static BDSGlobalConstants * Instance()
Access method.
BDS::TrajectoryOptions StoreTrajectoryOptions() const
options that require some implementation.
G4bool UseImportanceSampling() const
Is importance sampling being used.
BDSRunManager * runManager
Cache of main objects in BDSIM.
BDSOutput * bdsOutput
Cache of main objects in BDSIM.
void RegisterUserComponent(const G4String &componentTypeName, BDSComponentConstructor *componentConstructor)
void BeamOn(int nGenerate=-1)
int Initialise()
The main function where everything is constructed.
~BDSIM()
The destructor opens the geometry in Geant4 and deletes everything.
BDSComponentFactoryUser * userComponentFactory
Optional user registered component factory.
BDSBunch * bdsBunch
Cache of main objects in BDSIM.
bool ignoreSIGINT
For cmake testing.
void GeneratePrimariesOnly(const BDSGlobalConstants *globals)
BDSDetectorConstruction * realWorld
Cache of main objects in BDSIM.
int initialisationResult
Possible to not finish initialisation but have completed ok - flag for this.
G4VModularPhysicsList * userPhysicsList
Optional user registered physics list.
int argcCache
Cache of argc.
char ** argvCache
Cache of argv.
bool usualPrintOut
Whether to allow the usual cout output.
BDSParser * parser
Cache of main objects in BDSIM.
bool initialised
Whether initialisation was completed safely.
static BDSMaterials * Instance()
Singleton pattern access.
void PrepareRequiredMaterials(G4bool verbose=false)
converts parser material list
static BDSOutput * CreateOutput(BDSOutputType format, const G4String &fileName, G4int fileNumberOffset=-1, G4int compressionLevel=-1)
factory method
virtual void NewFile()=0
Open a new file. This should call WriteHeader() in it.
void FillEventPrimaryOnly(const BDSParticleCoordsFullGlobal &coords, const BDSParticleDefinition *particle)
void FillBeam(const GMAD::BeamBase *beam)
void FillOptions(const GMAD::OptionsBase *options)
virtual void CloseFile()=0
void AmalgamateBeam(const GMAD::Beam &beamIn, bool recreate)
Amalgamate the input beam definition with the ones stored in teh parser.
const GMAD::OptionsBase * GetOptionsBase() const
Return bare options base class.
void CheckOptions()
Check options for consistency. This also checks the beam options.
static BDSParser * Instance()
Access method.
const GMAD::Beam & GetBeam() const
Return beam.
const GMAD::Options & GetOptions() const
Return options.
void AmalgamateOptions(const GMAD::Options &optionsIn)
Amalgamate the input options with the ones stored in the parser.
const GMAD::BeamBase * GetBeamBase() const
Return bare beam base class.
const GMAD::FastList< GMAD::PhysicsBiasing > & GetBiasing() const
Return biasing list.
A set of particle coordinates in both local and global.
Wrapper for particle definition.
G4String Name() const
Accessor.
G4double KineticEnergy() const
Accessor.
G4double BRho() const
Accessor.
G4bool IsAnIon() const
Accessor.
Generates primary particle vertices using BDSBunch.
Control over the beginning and end of run actions.
Wrapper from G4RunManager that provides more output.
virtual void Initialize()
virtual void BeamOn(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1)
Altered BeamOn function to account for Placet synchronisation.
static BDSSamplerRegistry * Instance()
Accessor for registry.
BDSIM's Geant4 stacking action.
Provide extra output for Geant4 through a verbose stepping action.
static BDSTemporaryFiles * Instance()
Singleton accessor.
Action to decide whether or not to store trajectory information.
The BDS Visualisation Manager.
void StartSession(int argc, char **argv)
Start interactive mode.
Options for a beam distribution.
Basic options class independent of Geant4.
std::string physicsList
list of physics processes
bool verbose
General verbosity.
bool recreate
Whether to recreate from a file or not.
std::vector< G4ParallelWorldPhysics * > ConstructParallelWorldPhysics(const std::vector< G4VUserParallelWorld * > &worlds)
Construct the parallel physics process for each sampler world.
void PrintDefinedParticles()
void RegisterSamplerPhysics(const std::vector< G4ParallelWorldPhysics * > &processes, G4VModularPhysicsList *physicsList)
Register each parallel physics process to the main physics list.
void RegisterImportanceBiasing(const std::vector< G4VUserParallelWorld * > &worlds, G4VModularPhysicsList *physicsList)
Create importance geometry sampler and register importance biasing with physics list.
std::vector< G4VUserParallelWorld * > ConstructAndRegisterParallelWorlds(G4VUserDetectorConstruction *massWorld, G4bool buildSamplerWorld, G4bool buildPlacementFieldsWorld)
void PrintPrimaryParticleProcesses(const G4String &primaryParticleName)
void BuildMuonBiasing(G4VModularPhysicsList *physicsList)
Build muon splitting biasing and wrap the various processes in the physics list.
G4bool Geant4EnvironmentIsSet()
Check if the geant4 environmental variables necessary for a run are set.
void ConstructExtendedParticleSet()
void HandleAborts(int signal_number)
void ConstructDesignAndBeamParticle(const GMAD::Beam &beamDefinition, G4double ffact, BDSParticleDefinition *&designParticle, BDSParticleDefinition *&beamParticle, G4bool &beamDifferentFromDesignParticle)
G4int VerboseEventStop(G4int verboseEventStart, G4int verboseEventContinueFor)
void AddIStore(const std::vector< G4VUserParallelWorld * > &worlds)
Get store, and prepare importance sampling for importance geometry sampler.
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())
G4VModularPhysicsList * BuildPhysics(const G4String &physicsList, G4int verbosity=1)