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 "BDSLinkBunch.hh"
37#include "BDSBunchSixTrackLink.hh"
38#include "BDSCavityFactory.hh"
39#include "BDSColours.hh"
40#include "BDSComponentFactoryUser.hh"
42#include "BDSException.hh"
43#include "BDSExecOptions.hh"
44#include "BDSFieldFactory.hh"
45#include "BDSFieldLoader.hh"
46#include "BDSGeometryFactory.hh"
47#include "BDSGeometryFactorySQL.hh"
48#include "BDSGeometryWriter.hh"
49#include "BDSGlobalConstants.hh"
50#include "BDSLinkComponent.hh"
51#include "BDSLinkDetectorConstruction.hh"
52#include "BDSLinkEventAction.hh"
53#include "BDSLinkPrimaryGeneratorAction.hh"
54#include "BDSLinkRunAction.hh"
55#include "BDSLinkRunManager.hh"
56#include "BDSLinkStackingAction.hh"
57#include "BDSLinkTrackingAction.hh"
58#include "BDSMaterials.hh"
59#include "BDSOutput.hh"
60#include "BDSOutputFactory.hh"
61#include "BDSParallelWorldUtilities.hh"
62#include "BDSParser.hh"
63#include "BDSParticleExternal.hh"
64#include "BDSParticleDefinition.hh"
65#include "BDSPhysicsUtilities.hh"
66#include "BDSRandom.hh"
67#include "BDSSamplerRegistry.hh"
68#include "BDSSDManager.hh"
69#include "BDSTemporaryFiles.hh"
70#include "BDSUtilities.hh"
71#include "BDSVisManager.hh"
81 initialisationResult(1),
87 internalBdsBunch(false),
89 construction(nullptr),
91 currentElementIndex(0),
92 userPhysicsList(nullptr)
102 usualPrintOut(usualPrintOutIn),
104 initialisationResult(1),
111 construction(nullptr),
113 currentElementIndex(0),
114 userPhysicsList(nullptr)
121 bool usualPrintOutIn,
122 double minimumKineticEnergy,
123 bool protonsAndIonsOnly)
127 for (
int i = 0; i < argc; ++i) {
128 size_t len = std::strlen(argv[i]) + 1;
140 bool protonsAndIonsOnly)
142 minimumKineticEnergy *= CLHEP::GeV;
143 G4bool trackerDebug =
false;
148 {execOptions->
Print();}
152 G4cout << __METHOD_NAME__ <<
"DEBUG mode is on." << G4endl;
175 BDSRandom::CreateRandomNumberGenerator(globalConstants->RandomEngine());
176 BDSRandom::SetSeed();
180 globalConstants->OutputFileName());
185 G4cerr <<
"No Geant4 environmental variables found - please source geant4.sh environment" << G4endl;
186 G4cout <<
"A common fault is the wrong Geant4 environment as compared to the one BDSIM was compiled with." << G4endl;
203 BDSSDManager::Instance()->SetLinkMinimumEK(minimumKineticEnergy);
204 BDSSDManager::Instance()->SetLinkProtonsAndIonsOnly(protonsAndIonsOnly);
207 G4cout << __METHOD_NAME__ <<
"> Constructing physics processes" << G4endl;
211#if G4VERSION_NUMBER > 1049
217 G4PhysicsListHelper::GetPhysicsListHelper()->UseLowLooperThresholds();
221 G4VModularPhysicsList* physList;
224 G4cout <<
"Using externally registered user defined physics list" << G4endl;
237 G4bool beamDifferentFromDesignParticle =
false;
239 globalConstants->FFact(),
242 beamDifferentFromDesignParticle);
245 G4cout <<
"Design particle properties: " << G4endl << *designParticle;
246 if (beamDifferentFromDesignParticle)
247 {G4cout <<
"Beam particle properties: " << G4endl << *beamParticle;}
265 globalConstants->BeamlineTransform(),
266 globalConstants->BeamlineS(),
267 globalConstants->GeneratePrimariesOnly());
278 G4GeometryTolerance* theGeometryTolerance = G4GeometryTolerance::GetInstance();
281 G4cout << __METHOD_NAME__ <<
"Geometry Tolerances: " << G4endl;
282 G4cout << __METHOD_NAME__ << std::setw(12) <<
"Surface: " << std::setw(7) << theGeometryTolerance->GetSurfaceTolerance() <<
" mm" << G4endl;
283 G4cout << __METHOD_NAME__ << std::setw(12) <<
"Angular: " << std::setw(7) << theGeometryTolerance->GetAngularTolerance() <<
" rad" << G4endl;
284 G4cout << __METHOD_NAME__ << std::setw(12) <<
"Radial: " << std::setw(7) << theGeometryTolerance->GetRadialTolerance() <<
" mm" << G4endl;
291 G4int verboseSteppingEventStart = globalConstants->VerboseSteppingEventStart();
293 globalConstants->VerboseSteppingEventContinueFor());
296 verboseSteppingEventStart,
297 verboseSteppingEventStop,
298 globalConstants->VerboseSteppingPrimaryOnly(),
299 globalConstants->VerboseSteppingLevel()));
316 construction->SetPrimaryGeneratorAction(primaryGeneratorAction);
317 runManager->SetUserAction(primaryGeneratorAction);
335 runManager->SetVerboseLevel(std::min(globalConstants->VerboseRunLevel(), globalConstants->PhysicsVerbosity()));
336 G4EventManager::GetEventManager()->SetVerboseLevel(globalConstants->VerboseEventLevel());
337 G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(globalConstants->VerboseTrackingLevel());
340 G4bool bCloseGeometry = G4GeometryManager::GetInstance()->CloseGeometry();
343 G4cerr << __METHOD_NAME__ <<
"error - geometry not closed." << G4endl;
347 if (globalConstants->ExportGeometry())
351 globalConstants->ExportFileName());
354 const auto& nameInds =
construction->NameToElementIndex();
355 nameToElementIndex.insert(nameInds.begin(), nameInds.end());
372 G4cout.precision(10);
374 struct sigaction act;
376 sigemptyset(&act.sa_mask);
379 {sigaction(SIGINT, &act,
nullptr);}
380 sigaction(SIGABRT, &act,
nullptr);
381 sigaction(SIGTERM, &act,
nullptr);
382 sigaction(SIGSEGV, &act,
nullptr);
405 G4cout << exception.
what() << G4endl;
413 G4GeometryManager::GetInstance()->OpenGeometry();
416 G4cout << __METHOD_NAME__ <<
"deleting..." << G4endl;
428 delete BDSAcceleratorModel::Instance();
458 {G4cout << __METHOD_NAME__ <<
"End of Run. Thank you for using BDSIM!" << G4endl;}
464 auto search = nameToElementIndex.find(elementName);
465 if (search != nameToElementIndex.end())
466 {result = search->second;}
475 if (linkID > (
int)bl->
size())
478 const auto linkComponent =
dynamic_cast<const BDSLinkComponent*
>(rawAccComponent);
479 return linkComponent;
493 int beamlineIndex = linkIDToBeamlineIndex[linkID];
508 int beamlineIndex = linkIDToBeamlineIndex[linkID];
512BDSBunch* BDSIMLink::GetBunch()
const
517void BDSIMLink::SelectLinkElement(
const std::string& elementName, G4bool debug)
520 {G4cout <<
"Searching for " << elementName;}
526void BDSIMLink::SelectLinkElement(
int index, G4bool debug)
529 {G4cout <<
"Searching for " << index << G4endl;}
534 const std::string& materialName,
536 double halfApertureLeft,
537 double halfApertureRight,
549 G4GeometryManager* gm = G4GeometryManager::GetInstance();
550 if (gm->IsGeometryClosed())
551 {gm->OpenGeometry();}
570 nameToElementIndex =
construction->NameToElementIndex();
571 linkIDToBeamlineIndex =
construction->LinkIDToBeamlineIndex();
577 G4bool bCloseGeometry = gm->CloseGeometry();
579 {
throw BDSException(__METHOD_NAME__,
"error - geometry not closed.");}
584 const std::string& materialName,
585 const std::string& tipMaterialName,
588 double halfApertureLeft,
589 double halfApertureRight,
598 G4GeometryManager* gm = G4GeometryManager::GetInstance();
599 if (gm->IsGeometryClosed())
600 {gm->OpenGeometry();}
617 nameToElementIndex =
construction->NameToElementIndex();
618 linkIDToBeamlineIndex =
construction->LinkIDToBeamlineIndex();
624 G4bool bCloseGeometry = gm->CloseGeometry();
626 {
throw BDSException(__METHOD_NAME__,
"error - geometry not closed.");}
632 G4GeometryManager* gm = G4GeometryManager::GetInstance();
633 if (gm->IsGeometryClosed())
634 {gm->OpenGeometry();}
638 nameToElementIndex =
construction->NameToElementIndex();
639 linkIDToBeamlineIndex =
construction->LinkIDToBeamlineIndex();
645 G4bool bCloseGeometry = gm->CloseGeometry();
647 {
throw BDSException(__METHOD_NAME__,
"error - geometry not closed.");}
657int BDSIMLink::GetCurrentMaximumSixTrackParticleID()
const
662void BDSIMLink::SetCurrentMaximumExternalParticleID(
int currentMaximumExternalParticleID)
665 {
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(const G4String &geometryType, const 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.
int Initialise(int argc, char **argv, bool usualPrintOut=true, double minimumKineticEnergy=0, bool protonsAndIonsOnly=false)
Initialise everything given these arguments. The minimumKinetic energy should be in GeV.
G4RunManager * runManager
Cache of main object in BDSIM.
BDSLinkDetectorConstruction * construction
Cache of main object in BDSIM.
int argcCache
Cache of argc.
BDSBunch * bdsBunch
Cache of main object in BDSIM.
BDSIMLink(BDSBunch *bunchIn=nullptr)
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 AddLinkElement(GMAD::Element &el)
int currentElementIndex
Element to track in.
bool internalBdsBunch
Cache of main object in BDSIM.
double GetChordLengthOfLinkElement(int beamlineIndex) const
Access the length of a component. If bad name or ID given, -1 will be returned.
int AddLinkCollimatorTipJaw(const std::string &collimatorName, const std::string &materialName, const std::string &tipMaterialName, double tipThickness, 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)
void BeamOn(int nGenerate=-1)
A bunch distribution that holds a bunch from a Link.
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 AddLinkCollimatorTipJaw(const std::string &collimatorName, const std::string &materialName, const std::string &tipMaterialName, G4double tipThickness, 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)
Interface to append a tip collimator jaw to the linking.
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.
G4int AddLinkElement(GMAD::Element el)
Interface to append an element.
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)