18#include "BDSIMLink.hh"
24#include "G4EventManager.hh"
25#include "G4GeometryManager.hh"
26#include "G4GeometryTolerance.hh"
27#include "G4Version.hh"
28#include "G4VModularPhysicsList.hh"
30#include "BDSAcceleratorModel.hh"
31#include "BDSAperturePointsLoader.hh"
32#include "BDSBeamPipeFactory.hh"
34#include "BDSBunchFactory.hh"
35#include "BDSLinkBunch.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"
81 initialisationResult(1),
87 internalBdsBunch(false),
89 construction(nullptr),
91 currentElementIndex(0),
92 userPhysicsList(nullptr)
101 bool usualPrintOutIn,
104 usualPrintOut(usualPrintOutIn),
106 initialisationResult(1),
113 construction(nullptr),
115 currentElementIndex(0),
116 userPhysicsList(nullptr)
123 bool usualPrintOutIn,
124 double minimumKineticEnergy,
125 bool protonsAndIonsOnly)
129 for (
int i = 0; i < argc; ++i) {
130 size_t len = std::strlen(argv[i]) + 1;
142 bool protonsAndIonsOnly)
144 minimumKineticEnergy *= CLHEP::GeV;
145 G4bool trackerDebug =
false;
150 {execOptions->
Print();}
154 G4cout << __METHOD_NAME__ <<
"DEBUG mode is on." << G4endl;
178 BDSRandom::CreateRandomNumberGenerator(globalConstants->RandomEngine());
179 BDSRandom::SetSeed();
183 globalConstants->OutputFileName());
188 G4cerr <<
"No Geant4 environmental variables found - please source geant4.sh environment" << G4endl;
189 G4cout <<
"A common fault is the wrong Geant4 environment as compared to the one BDSIM was compiled with." << G4endl;
206 BDSSDManager::Instance()->SetLinkMinimumEK(minimumKineticEnergy);
207 BDSSDManager::Instance()->SetLinkProtonsAndIonsOnly(protonsAndIonsOnly);
210 G4cout << __METHOD_NAME__ <<
"> Constructing physics processes" << G4endl;
214#if G4VERSION_NUMBER > 1049
220 G4PhysicsListHelper::GetPhysicsListHelper()->UseLowLooperThresholds();
224 G4VModularPhysicsList* physList;
227 G4cout <<
"Using externally registered user defined physics list" << G4endl;
240 G4bool beamDifferentFromDesignParticle =
false;
242 globalConstants->FFact(),
245 beamDifferentFromDesignParticle);
248 G4cout <<
"Design particle properties: " << G4endl << *designParticle;
249 if (beamDifferentFromDesignParticle)
250 {G4cout <<
"Beam particle properties: " << G4endl << *beamParticle;}
268 globalConstants->BeamlineTransform(),
269 globalConstants->BeamlineS(),
270 globalConstants->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;
294 G4int verboseSteppingEventStart = globalConstants->VerboseSteppingEventStart();
296 globalConstants->VerboseSteppingEventContinueFor());
299 verboseSteppingEventStart,
300 verboseSteppingEventStop,
301 globalConstants->VerboseSteppingPrimaryOnly(),
302 globalConstants->VerboseSteppingLevel()));
319 construction->SetPrimaryGeneratorAction(primaryGeneratorAction);
320 runManager->SetUserAction(primaryGeneratorAction);
338 runManager->SetVerboseLevel(std::min(globalConstants->VerboseRunLevel(), globalConstants->PhysicsVerbosity()));
339 G4EventManager::GetEventManager()->SetVerboseLevel(globalConstants->VerboseEventLevel());
340 G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(globalConstants->VerboseTrackingLevel());
343 G4bool bCloseGeometry = G4GeometryManager::GetInstance()->CloseGeometry();
346 G4cerr << __METHOD_NAME__ <<
"error - geometry not closed." << G4endl;
350 if (globalConstants->ExportGeometry())
354 globalConstants->ExportFileName());
357 const auto& nameInds =
construction->NameToElementIndex();
358 nameToElementIndex.insert(nameInds.begin(), nameInds.end());
375 G4cout.precision(10);
377 struct sigaction act;
379 sigemptyset(&act.sa_mask);
382 {sigaction(SIGINT, &act,
nullptr);}
383 sigaction(SIGABRT, &act,
nullptr);
384 sigaction(SIGTERM, &act,
nullptr);
385 sigaction(SIGSEGV, &act,
nullptr);
408 G4cout << exception.
what() << G4endl;
416 G4GeometryManager::GetInstance()->OpenGeometry();
419 G4cout << __METHOD_NAME__ <<
"deleting..." << G4endl;
431 delete BDSAcceleratorModel::Instance();
461 {G4cout << __METHOD_NAME__ <<
"End of Run. Thank you for using BDSIM!" << G4endl;}
467 auto search = nameToElementIndex.find(elementName);
468 if (search != nameToElementIndex.end())
469 {result = search->second;}
478 if (linkID > (
int)bl->
size())
481 const auto linkComponent =
dynamic_cast<const BDSLinkComponent*
>(rawAccComponent);
482 return linkComponent;
496 int beamlineIndex = linkIDToBeamlineIndex[linkID];
511 int beamlineIndex = linkIDToBeamlineIndex[linkID];
515BDSBunch* BDSIMLink::GetBunch()
const
520void BDSIMLink::SelectLinkElement(
const std::string& elementName, G4bool debug)
523 {G4cout <<
"Searching for " << elementName;}
530 {
throw std::runtime_error(
"Element not found " + elementName);}
533void BDSIMLink::SelectLinkElement(
int index, G4bool debug)
536 {G4cout <<
"Searching for " << index << G4endl;}
541 const std::string& materialName,
543 double halfApertureLeft,
544 double halfApertureRight,
556 G4GeometryManager* gm = G4GeometryManager::GetInstance();
557 if (gm->IsGeometryClosed())
558 {gm->OpenGeometry();}
577 nameToElementIndex =
construction->NameToElementIndex();
578 linkIDToBeamlineIndex =
construction->LinkIDToBeamlineIndex();
584 G4bool bCloseGeometry = gm->CloseGeometry();
586 {
throw BDSException(__METHOD_NAME__,
"error - geometry not closed.");}
591 const std::string& materialName,
592 const std::string& tipMaterialName,
595 double halfApertureLeft,
596 double halfApertureRight,
605 G4GeometryManager* gm = G4GeometryManager::GetInstance();
606 if (gm->IsGeometryClosed())
607 {gm->OpenGeometry();}
624 nameToElementIndex =
construction->NameToElementIndex();
625 linkIDToBeamlineIndex =
construction->LinkIDToBeamlineIndex();
631 G4bool bCloseGeometry = gm->CloseGeometry();
633 {
throw BDSException(__METHOD_NAME__,
"error - geometry not closed.");}
639 G4GeometryManager* gm = G4GeometryManager::GetInstance();
640 if (gm->IsGeometryClosed())
641 {gm->OpenGeometry();}
645 nameToElementIndex =
construction->NameToElementIndex();
646 linkIDToBeamlineIndex =
construction->LinkIDToBeamlineIndex();
652 G4bool bCloseGeometry = gm->CloseGeometry();
654 {
throw BDSException(__METHOD_NAME__,
"error - geometry not closed.");}
664int BDSIMLink::GetCurrentMaximumSixTrackParticleID()
const
669void BDSIMLink::SetCurrentMaximumExternalParticleID(
int currentMaximumExternalParticleID)
672 {
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.
BDSIMLink(BDSBunch *bunchIn=nullptr, BDSParser *parserIn=nullptr)
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.
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
Const entry to parser objects.
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)