21#include "BDSEventInfo.hh"
22#include "BDSException.hh"
23#include "BDSExtent.hh"
24#include "BDSGlobalConstants.hh"
25#include "BDSIonDefinition.hh"
26#include "BDSOutputLoader.hh"
27#include "BDSParticleDefinition.hh"
28#include "BDSPhysicsUtilities.hh"
29#include "BDSPrimaryGeneratorAction.hh"
30#include "BDSPrimaryGeneratorFile.hh"
31#include "BDSPrimaryVertexInformation.hh"
32#include "BDSPTCOneTurnMap.hh"
33#include "BDSRunAction.hh"
34#include "BDSRandom.hh"
35#include "BDSUtilities.hh"
36#include "BDSWarning.hh"
38#include "parser/beam.h"
40#include "CLHEP/Random/Random.h"
44#include "G4EventManager.hh"
45#include "G4HEPEvtInterface.hh"
46#include "G4IonTable.hh"
47#include "G4ParticleGun.hh"
48#include "G4ParticleDefinition.hh"
50#include "G4RunManager.hh"
56 recreateFile(nullptr),
59 distrFileMatchLength(beam.distrFileMatchLength),
62 generatorFromFile(nullptr)
65 {
throw BDSException(__METHOD_NAME__,
"valid BDSBunch required");}
81 particleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
88BDSPrimaryGeneratorAction::~BDSPrimaryGeneratorAction()
92 delete generatorFromFile;
97 G4int thisEventID = anEvent->GetEventID();
104 G4cout << __METHOD_NAME__ <<
"setting seed state from file" << G4endl;
111 {BDSRandom::WriteSeedState();}
116 G4String fileName = globals->SeedStateFileName();
117 BDSRandom::LoadSeedState(fileName);
123 anEvent->SetUserInformation(eventInfo);
127 if (generatorFromFile)
153 anEvent->SetEventAborted();
154 G4cout << exception.
what() << G4endl;
155 G4cout <<
"Aborting this event (#" << thisEventID <<
")" << G4endl;
175 G4cout << __METHOD_NAME__ <<
"Event #" << thisEventID
176 <<
" - Particle kinetic energy smaller than 0! "
177 <<
"This will not be tracked." << G4endl;
178 anEvent->SetEventAborted();
185 G4cerr << __METHOD_NAME__ <<
"point: " << coords.global
186 <<
"mm lies outside the world volume with extent ("
187 <<
worldExtent <<
" - event aborted!" << G4endl << G4endl;
188 anEvent->SetEventAborted();
192 G4cout << __METHOD_NAME__ << coords << G4endl;
195 G4ThreeVector PartMomDir(coords.global.xp,coords.global.yp,coords.global.zp);
196 G4ThreeVector PartPosition(coords.global.x,coords.global.y,coords.global.z);
200 particleGun->SetParticleMomentumDirection(PartMomDir);
206 auto vertex = anEvent->GetPrimaryVertex();
207 vertex->SetWeight(coords.local.weight);
223 G4bool generatedVertexOK =
false;
224 if (!distributionFinished)
228 if (distributionFinished && !generatedVertexOK)
230 G4bool endRunNow =
false;
234 G4cout << __METHOD_NAME__ <<
"distribution file finished (matched in length): ending run." << G4endl;
236 {BDS::Warning(__METHOD_NAME__,
"no events passed filters and were simulated.");}
243 G4int currentEventIndex = G4RunManager::GetRunManager()->GetCurrentRun()->GetNumberOfEvent();
244 G4cerr << __METHOD_NAME__ <<
"unable to generate " << nGenerateRequested
245 <<
" events as fewer events passed the filters in the file." << G4endl;
246 G4cerr << __METHOD_NAME__ << currentEventIndex <<
" events generated" << G4endl;
251 anEvent->SetEventAborted();
252 G4EventManager::GetEventManager()->AbortCurrentEvent();
253 G4RunManager::GetRunManager()->AbortRun();
257 else if (!generatedVertexOK)
259 anEvent->SetEventAborted();
260 G4EventManager::GetEventManager()->AbortCurrentEvent();
The base class for bunch distribution generators.
virtual void RecreateAdvanceToEvent(G4int eventOffset)
virtual const BDSParticleDefinition * ParticleDefinition() const
Access the beam particle definition.
void CalculateBunchIndex(G4int eventIndex)
Calculate which bunch index we should be at given an event index.
G4int CurrentBunchIndex() const
Get the current bunch index for writing to output.
G4bool BeamParticleIsAnIon() const
Access whether the beam particle is an ion or not.
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 SetBunchIndex(int bunchIndexIn)
Setters.
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 of ROOT Event output for recreating events.
G4String SeedState(G4int eventNumber=0)
Access the seed state for a given event index in the file (0 counting).
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, G4bool batchMode)
Bunch must have a valid particle definition (ie not nullptr).
G4bool distrFileMatchLength
Match external file length for event generator.
void GeneratePrimariesFromFile(G4Event *anEvent)
For a file-based event generator there are a few checks we have to do - put in a function to keep tid...
BDSExtent worldExtent
World extent that particle coordinates are checked against to ensure they're inside it.
BDSBunch * bunch
BDSIM particle generator.
virtual void GeneratePrimaries(G4Event *)
Main interface for Geant4. Prepare primary(ies) for the event.
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.
G4long NEventsReadThatPassedFilters() const
Accessor.
G4bool DistributionIsFinished() const
Report whether the distribution is finished generating.
G4bool GeneratePrimaryVertexSafe(G4Event *event)
Return false if not able to generate a primary vertex.
static BDSPrimaryGeneratorFile * ConstructGenerator(const GMAD::Beam &beam, BDSBunch *bunchIn, G4bool recreate, G4int eventOffset, G4bool batchMode)