19#include "BDSIMLink.hh"
25#include "G4EventManager.hh"
26#include "G4GeometryManager.hh"
27#include "G4GeometryTolerance.hh"
28#include "G4Version.hh"
29#include "G4VModularPhysicsList.hh"
31#include "BDSAcceleratorModel.hh"
32#include "BDSAperturePointsLoader.hh"
33#include "BDSBeamPipeFactory.hh"
35#include "BDSBunchFactory.hh"
36#include "BDSBunchSixTrackLink.hh"
37#include "BDSCavityFactory.hh"
38#include "BDSColours.hh"
39#include "BDSComponentFactoryUser.hh"
41#include "BDSException.hh"
42#include "BDSExecOptions.hh"
43#include "BDSFieldFactory.hh"
44#include "BDSFieldLoader.hh"
45#include "BDSGeometryFactory.hh"
46#include "BDSGeometryFactorySQL.hh"
47#include "BDSGeometryWriter.hh"
48#include "BDSGlobalConstants.hh"
49#include "BDSLinkComponent.hh"
50#include "BDSLinkDetectorConstruction.hh"
51#include "BDSLinkEventAction.hh"
52#include "BDSLinkPrimaryGeneratorAction.hh"
53#include "BDSLinkRunAction.hh"
54#include "BDSLinkRunManager.hh"
55#include "BDSLinkStackingAction.hh"
56#include "BDSLinkTrackingAction.hh"
57#include "BDSMaterials.hh"
58#include "BDSOutput.hh"
59#include "BDSOutputFactory.hh"
60#include "BDSParallelWorldUtilities.hh"
61#include "BDSParser.hh"
62#include "BDSParticleExternal.hh"
63#include "BDSParticleDefinition.hh"
64#include "BDSPhysicsUtilities.hh"
65#include "BDSRandom.hh"
66#include "BDSSamplerRegistry.hh"
67#include "BDSSDManager.hh"
68#include "BDSTemporaryFiles.hh"
69#include "BDSUtilities.hh"
70#include "BDSVisManager.hh"
79 initialisationResult(1),
86 construction(nullptr),
88 currentElementIndex(0),
89 userPhysicsList(nullptr)
94 usualPrintOut(usualPrintOutIn),
96 initialisationResult(1),
103 construction(nullptr),
105 currentElementIndex(0),
106 userPhysicsList(nullptr)
113 bool usualPrintOutIn,
114 double minimumKineticEnergy,
115 bool protonsAndIonsOnly)
125 bool protonsAndIonsOnly)
127 minimumKineticEnergy *= CLHEP::GeV;
128 G4bool trackerDebug =
false;
133 {execOptions->
Print();}
137 G4cout << __METHOD_NAME__ <<
"DEBUG mode is on." << G4endl;
160 BDSRandom::CreateRandomNumberGenerator(globalConstants->RandomEngine());
161 BDSRandom::SetSeed();
165 globalConstants->OutputFileName());
170 G4cerr <<
"No Geant4 environmental variables found - please source geant4.sh environment" << G4endl;
171 G4cout <<
"A common fault is the wrong Geant4 environment as compared to the one BDSIM was compiled with." << G4endl;
188 BDSSDManager::Instance()->SetLinkMinimumEK(minimumKineticEnergy);
189 BDSSDManager::Instance()->SetLinkProtonsAndIonsOnly(protonsAndIonsOnly);
192 G4cout << __METHOD_NAME__ <<
"> Constructing physics processes" << G4endl;
196#if G4VERSION_NUMBER > 1049
202 G4PhysicsListHelper::GetPhysicsListHelper()->UseLowLooperThresholds();
206 G4VModularPhysicsList* physList;
209 G4cout <<
"Using externally registered user defined physics list" << G4endl;
222 G4bool beamDifferentFromDesignParticle =
false;
224 globalConstants->FFact(),
227 beamDifferentFromDesignParticle);
230 G4cout <<
"Design particle properties: " << G4endl << *designParticle;
231 if (beamDifferentFromDesignParticle)
232 {G4cout <<
"Beam particle properties: " << G4endl << *beamParticle;}
250 globalConstants->BeamlineTransform(),
251 globalConstants->BeamlineS(),
252 globalConstants->GeneratePrimariesOnly());
263 G4GeometryTolerance* theGeometryTolerance = G4GeometryTolerance::GetInstance();
266 G4cout << __METHOD_NAME__ <<
"Geometry Tolerances: " << G4endl;
267 G4cout << __METHOD_NAME__ << std::setw(12) <<
"Surface: " << std::setw(7) << theGeometryTolerance->GetSurfaceTolerance() <<
" mm" << G4endl;
268 G4cout << __METHOD_NAME__ << std::setw(12) <<
"Angular: " << std::setw(7) << theGeometryTolerance->GetAngularTolerance() <<
" rad" << G4endl;
269 G4cout << __METHOD_NAME__ << std::setw(12) <<
"Radial: " << std::setw(7) << theGeometryTolerance->GetRadialTolerance() <<
" mm" << G4endl;
276 G4int verboseSteppingEventStart = globalConstants->VerboseSteppingEventStart();
278 globalConstants->VerboseSteppingEventContinueFor());
281 verboseSteppingEventStart,
282 verboseSteppingEventStop,
283 globalConstants->VerboseSteppingPrimaryOnly(),
284 globalConstants->VerboseSteppingLevel()));
301 construction->SetPrimaryGeneratorAction(primaryGeneratorAction);
302 runManager->SetUserAction(primaryGeneratorAction);
320 runManager->SetVerboseLevel(std::min(globalConstants->VerboseRunLevel(), globalConstants->PhysicsVerbosity()));
321 G4EventManager::GetEventManager()->SetVerboseLevel(globalConstants->VerboseEventLevel());
322 G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(globalConstants->VerboseTrackingLevel());
325 G4bool bCloseGeometry = G4GeometryManager::GetInstance()->CloseGeometry();
328 G4cerr << __METHOD_NAME__ <<
"error - geometry not closed." << G4endl;
332 if (globalConstants->ExportGeometry())
336 globalConstants->ExportFileName());
339 const auto& nameInds =
construction->NameToElementIndex();
340 nameToElementIndex.insert(nameInds.begin(), nameInds.end());
357 G4cout.precision(10);
359 struct sigaction act;
361 sigemptyset(&act.sa_mask);
364 {sigaction(SIGINT, &act,
nullptr);}
365 sigaction(SIGABRT, &act,
nullptr);
366 sigaction(SIGTERM, &act,
nullptr);
367 sigaction(SIGSEGV, &act,
nullptr);
390 G4cout << exception.
what() << G4endl;
398 G4GeometryManager::GetInstance()->OpenGeometry();
401 G4cout << __METHOD_NAME__ <<
"deleting..." << G4endl;
413 delete BDSAcceleratorModel::Instance();
436 {G4cout << __METHOD_NAME__ <<
"End of Run. Thank you for using BDSIM!" << G4endl;}
442 auto search = nameToElementIndex.find(elementName);
443 if (search != nameToElementIndex.end())
444 {result = search->second;}
453 if (linkID > (
int)bl->
size())
456 const auto linkComponent =
dynamic_cast<const BDSLinkComponent*
>(rawAccComponent);
457 return linkComponent;
471 int beamlineIndex = linkIDToBeamlineIndex[linkID];
486 int beamlineIndex = linkIDToBeamlineIndex[linkID];
490void BDSIMLink::SelectLinkElement(
const std::string& elementName, G4bool debug)
493 {G4cout <<
"Searching for " << elementName;}
499void BDSIMLink::SelectLinkElement(
int index, G4bool debug)
502 {G4cout <<
"Searching for " << index << G4endl;}
507 const std::string& materialName,
509 double halfApertureLeft,
510 double halfApertureRight,
522 G4GeometryManager* gm = G4GeometryManager::GetInstance();
523 if (gm->IsGeometryClosed())
524 {gm->OpenGeometry();}
542 nameToElementIndex =
construction->NameToElementIndex();
543 linkIDToBeamlineIndex =
construction->LinkIDToBeamlineIndex();
549 G4bool bCloseGeometry = gm->CloseGeometry();
551 {
throw BDSException(__METHOD_NAME__,
"error - geometry not closed.");}
560int BDSIMLink::GetCurrentMaximumSixTrackParticleID()
const
565void BDSIMLink::SetCurrentMaximumExternalParticleID(
int currentMaximumExternalParticleID)
568 {
runAction->SetMaximumExternalParticleID(currentMaximumExternalParticleID);}
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.
BDSAcceleratorComponent * GetAcceleratorComponent() const
Accessor.
A vector of BDSBeamlineElement instances - a beamline.
const BDSBeamlineElement * at(int iElement) const
Return a reference to the element at i.
BeamlineVector::size_type size() const
Get the number of elements.
static BDSBunch * CreateBunch(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const G4Transform3D &beamlineTransform=G4Transform3D::Identity, G4double beamlineS=0, G4bool generatePrimariesOnlyIn=false)
factory method
The base class for bunch distribution generators.
virtual const BDSParticleDefinition * ParticleDefinition() const
Access the beam particle definition.
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
static BDSCavityFactory * Instance()
Singleton accessor.
static BDSColours * Instance()
singleton pattern
General exception with possible name of object and message.
const char * what() const noexcept override
Override message in std::exception.
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 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.
G4VModularPhysicsList * userPhysicsList
Optional user registered physics list.
bool initialised
Whether initialisation was completed safely.
int AddLinkCollimatorJaw(const std::string &collimatorName, const std::string &materialName, double length, double halfApertureLeft, double halfApertureRight, double rotation, double xOffset, double yOffset, double jawTiltLeft=0.0, double jawTiltRight=0.0, bool buildLeftJaw=true, bool buildRightJaw=true, bool isACrystal=false, double crystalAngle=0, bool sampleIn=false)
Use standard C++ types as expected to be used externally.
int initialisationResult
Possible to not finish initialisation but have completed ok - flag for this.
int GetLinkIndex(const std::string &elementName) const
Get the internal index of a component by name.
BDSLinkRunAction * runAction
Cache of main object in BDSIM.
BDSIMLink(BDSBunch *bunchIn)
G4RunManager * runManager
Cache of main object in BDSIM.
int Initialise(int argc, char **argv, bool usualPrintOut=true, double minimumKineticEnergy=0, bool protonsAndIonsOnly=true)
Initialise everything given these arguments. The minimumKinetic energy should be in GeV.
BDSLinkDetectorConstruction * construction
Cache of main object in BDSIM.
int argcCache
Cache of argc.
BDSBunch * bdsBunch
Cache of main object in BDSIM.
BDSParser * parser
Cache of main object in BDSIM.
char ** argvCache
Cache of argv.
~BDSIMLink()
The destructor opens the geometry in Geant4 and deletes everything.
BDSOutput * bdsOutput
Cache of main object in BDSIM.
bool ignoreSIGINT
For cmake testing.
bool usualPrintOut
Whether to allow the usual cout output.
double GetArcLengthOfLinkElement(int beamlineIndex) const
Access the length of a component. If bad name or ID given, -1 will be returned.
int currentElementIndex
Element to track in.
double GetChordLengthOfLinkElement(int beamlineIndex) const
Access the length of a component. If bad name or ID given, -1 will be returned.
void BeamOn(int nGenerate=-1)
A BDSAcceleratorComponent wrapper for BDSLinkOpaqueBox.
G4double ComponentChordLength() const
Accessor.
G4double ComponentArcLength() const
Accessor.
Construction of the geometry in the case of a link model.
G4int AddLinkCollimatorJaw(const std::string &collimatorName, const std::string &materialName, G4double length, G4double halfApertureLeft, G4double halfApertureRight, G4double rotation, G4double xOffset, G4double yOffset, G4double jawTiltLeft=0.0, G4double jawTiltRight=0.0, G4bool buildLeftJaw=true, G4bool buildRightJaw=true, G4bool isACrystal=false, G4double crystalAngle=0, G4bool sampleIn=false)
Interface to append a collimator of jaw style to the linking.
void SetDesignParticle(const BDSParticleDefinition *defIn)
Set the design particle definition.
Process information at the event level for Link to trackers.
Generates primary particle vertices using BDSBunch.
Simplified run action to hold link hits.
Wrapper from G4RunManager that provides our exception handler.
BDSIM's Geant4 stacking action.
Action to decide whether or not to store trajectory information.
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.
virtual void InitialiseGeometryDependent()
virtual void UpdateSamplers()
Interface to allow updating samplers with dynamic construction. Only for link - not for regular use.
virtual void CloseFile()=0
void AmalgamateBeam(const GMAD::Beam &beamIn, bool recreate)
Amalgamate the input beam definition with the ones stored in teh parser.
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.
Wrapper for particle definition.
G4String Name() const
Accessor.
static BDSSamplerRegistry * Instance()
Accessor for registry.
static BDSTemporaryFiles * Instance()
Singleton accessor.
The BDS Visualisation Manager.
void StartSession(int argc, char **argv)
Start interactive mode.
std::string distrType
beam parameters
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.
std::vector< G4VUserParallelWorld * > ConstructAndRegisterParallelWorlds(G4VUserDetectorConstruction *massWorld, G4bool buildSamplerWorld, G4bool buildPlacementFieldsWorld)
void PrintPrimaryParticleProcesses(const G4String &primaryParticleName)
G4bool Geant4EnvironmentIsSet()
Check if the geant4 environmental variables necessary for a run are set.
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)
G4VModularPhysicsList * BuildPhysics(const G4String &physicsList, G4int verbosity=1)