20#include "BDSBunchEventGenerator.hh"
22#include "BDSEventInfo.hh"
23#include "BDSException.hh"
24#include "BDSExtent.hh"
25#include "BDSGlobalConstants.hh"
26#include "BDSIonDefinition.hh"
27#include "BDSOutputLoader.hh"
28#include "BDSParticleDefinition.hh"
29#include "BDSPhysicsUtilities.hh"
30#include "BDSPrimaryGeneratorAction.hh"
31#include "BDSPrimaryVertexInformation.hh"
32#include "BDSPTCOneTurnMap.hh"
33#include "BDSROOTSamplerReader.hh"
34#include "BDSRandom.hh"
35#include "BDSUtilities.hh"
38#include "BDSHepMC3Reader.hh"
41#include "parser/beam.h"
43#include "CLHEP/Random/Random.h"
47#include "G4HEPEvtInterface.hh"
48#include "G4IonTable.hh"
49#include "G4ParticleGun.hh"
50#include "G4ParticleDefinition.hh"
55 recreateFile(nullptr),
57 ionPrimary(bunchIn->BeamParticleIsAnIon()),
58 useEventGeneratorFile(false),
59 useSamplerLoader(false),
67 samplerReader(nullptr)
82 particleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
83 particleGun->SetParticlePosition(G4ThreeVector(0.*CLHEP::cm,0.*CLHEP::cm,0.*CLHEP::cm));
95 {
throw BDSException(__METHOD_NAME__,
"no distrFile specified for event generator beam distribution.");}
99 {
throw BDSException(__METHOD_NAME__,
"must be used with a BDSBunchEventGenerator instance");}
105 throw BDSException(__METHOD_NAME__,
"event generator file being used but BDSIM not compiled with HEPMC3");
111 {
throw BDSException(__METHOD_NAME__,
"no distrFile specified for event generator beam distribution.");}
115 {
throw BDSException(__METHOD_NAME__,
"must be used with a BDSBunchEventGenerator instance");}
120BDSPrimaryGeneratorAction::~BDSPrimaryGeneratorAction()
127 delete samplerReader;
135 G4cout << __METHOD_NAME__ <<
"setting seed state from file" << G4endl;
141 {BDSRandom::WriteSeedState();}
146 G4String fileName = globals->SeedStateFileName();
147 BDSRandom::LoadSeedState(fileName);
152 anEvent->SetUserInformation(eventInfo);
185 anEvent->SetEventAborted();
186 G4cout << exception.
what() << G4endl;
187 G4cout <<
"Aborting this event (#" << anEvent->GetEventID() <<
")" << G4endl;
207 G4cout << __METHOD_NAME__ <<
"Event #" << anEvent->GetEventID()
208 <<
" - Particle kinetic energy smaller than 0! "
209 <<
"This will not be tracked." << G4endl;
210 anEvent->SetEventAborted();
217 std::ofstream ofstr(
"output.primary.txt");
218 ofstr << coords.local.x <<
" " << coords.local.y <<
" " << coords.local.z <<
" "
219 << coords.local.xp <<
" " << coords.local.yp <<
" " << coords.local.zp <<
" "
220 << coords.local.T <<
" " << coords.local.totalEnergy <<
" " << coords.local.weight << std::endl;
227 G4cerr << __METHOD_NAME__ <<
"point: " << coords.global
228 <<
"mm lies outside the world volume with extent ("
229 <<
worldExtent <<
" - event aborted!" << G4endl << G4endl;
230 anEvent->SetEventAborted();
234 G4cout << __METHOD_NAME__ << coords << G4endl;
237 G4ThreeVector PartMomDir(coords.global.xp,coords.global.yp,coords.global.zp);
238 G4ThreeVector PartPosition(coords.global.x,coords.global.y,coords.global.z);
242 particleGun->SetParticleMomentumDirection(PartMomDir);
248 auto vertex = anEvent->GetPrimaryVertex();
249 vertex->SetWeight(coords.local.weight);
A wrapper of BDSBunch to include a filter for the events loaded by an event generator.
The base class for bunch distribution generators.
virtual void RecreateAdvanceToEvent(G4int)
virtual const BDSParticleDefinition * ParticleDefinition() const
Access the beam particle definition.
virtual void UpdateIonDefinition()
virtual BDSParticleCoordsFullGlobal GetNextParticleValid(G4int maxTries=100)
G4bool UseCurvilinearTransform() const
Access whether there's a finite S offset and therefore we're using a CL transform.
Interface to store event information use G4 hooks.
void SetSeedStateAtStart(const G4String &seedStateAtStartIn)
Setters.
General exception with possible name of object and message.
const char * what() const noexcept override
Override message in std::exception.
G4bool Encompasses(const G4ThreeVector &point) const
Return whether the extent encompasses the point. True if point lies inside the extent.
A class that holds global options and constants.
static BDSGlobalConstants * Instance()
Access method.
Loader to use any HepMC3 compatible file.
virtual void RecreateAdvanceToEvent(G4int eventOffset)
Advance to the correct event number in the file for recreation.
Loader of ROOT Event output for recreating events.
void SetInitialPrimaryCoordinates(const BDSParticleCoordsFullGlobal &coords, G4bool offsetS0)
Load initial coordinates where the beam started and convert to PTC coordinates.
A set of particle coordinates in both local and global.
G4double Mass() const
Accessor.
G4double Charge() const
Accessor.
G4ParticleDefinition * ParticleDefinition() const
Accessor.
G4bool recreate
Whether to load seed state at start of event from rootevent file.
G4ParticleGun * particleGun
Geant4 particle gun that creates single particles.
G4int eventOffset
The offset in the file to read events from when setting the seed.
G4bool useASCIISeedState
Whether to use the ascii seed state each time.
G4bool ionPrimary
The primary particle will be an ion.
BDSPrimaryGeneratorAction(BDSBunch *bunchIn, const GMAD::Beam &beam)
Bunch must have a valid particle definition (ie not nullptr).
G4bool useEventGeneratorFile
Whether to use event generator file.
BDSExtent worldExtent
World extent that particle coordinates are checked against to ensure they're inside it.
G4bool useSamplerLoader
Whether to use a sampler loader.
BDSBunch * bunch
BDSIM particle generator.
virtual void GeneratePrimaries(G4Event *)
Main interface for Geant4. Prepare primary(ies) for the event.
BDSHepMC3Reader * hepMC3Reader
Event generator file loader.
BDSPTCOneTurnMap * oneTurnMap
Cached OTM for setting first turn primary coords.
G4bool writeASCIISeedState
Cache of whether to write seed state as ASCII per event.
BDSOutputLoader * recreateFile
Optional output handler for restoring seed state.
Loader to read a specific sampler from a BDSIM ROOT output file.
virtual void GeneratePrimaryVertex(G4Event *anEvent)
Read the next non-empty sampler entry from the file.
Improve type-safety of native enum data type in C++.
bool eventGeneratorWarnSkippedParticles
Event generator file filter.
std::string distrFile
beam parameters
bool distrFileFromExecOptions
Required to know how to build the absolute path properly.
bool removeUnstableWithoutDecay
beam parameters
std::string distrType
beam parameters
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false)