20#include "BDSEventInfo.hh"
21#include "BDSException.hh"
22#include "BDSExtent.hh"
23#include "BDSGlobalConstants.hh"
24#include "BDSIonDefinition.hh"
25#include "BDSOutputLoader.hh"
26#include "BDSParticleDefinition.hh"
27#include "BDSPhysicsUtilities.hh"
28#include "BDSPrimaryGeneratorAction.hh"
29#include "BDSPrimaryGeneratorFile.hh"
30#include "BDSPrimaryVertexInformation.hh"
31#include "BDSPTCOneTurnMap.hh"
32#include "BDSRunAction.hh"
33#include "BDSRandom.hh"
34#include "BDSUtilities.hh"
35#include "BDSWarning.hh"
37#include "parser/beam.h"
39#include "CLHEP/Random/Random.h"
43#include "G4EventManager.hh"
44#include "G4HEPEvtInterface.hh"
45#include "G4IonTable.hh"
46#include "G4ParticleGun.hh"
47#include "G4ParticleDefinition.hh"
49#include "G4RunManager.hh"
55 recreateFile(nullptr),
58 distrFileMatchLength(beam.distrFileMatchLength),
61 generatorFromFile(nullptr)
64 {
throw BDSException(__METHOD_NAME__,
"valid BDSBunch required");}
80 particleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
87BDSPrimaryGeneratorAction::~BDSPrimaryGeneratorAction()
91 delete generatorFromFile;
96 G4int thisEventID = anEvent->GetEventID();
103 G4cout << __METHOD_NAME__ <<
"setting seed state from file" << G4endl;
110 {BDSRandom::WriteSeedState();}
115 G4String fileName = globals->SeedStateFileName();
116 BDSRandom::LoadSeedState(fileName);
122 anEvent->SetUserInformation(eventInfo);
126 if (generatorFromFile)
152 anEvent->SetEventAborted();
153 G4cout << exception.
what() << G4endl;
154 G4cout <<
"Aborting this event (#" << thisEventID <<
")" << G4endl;
174 G4cout << __METHOD_NAME__ <<
"Event #" << thisEventID
175 <<
" - Particle kinetic energy smaller than 0! "
176 <<
"This will not be tracked." << G4endl;
177 anEvent->SetEventAborted();
184 G4cerr << __METHOD_NAME__ <<
"point: " << coords.global
185 <<
"mm lies outside the world volume with extent ("
186 <<
worldExtent <<
" - event aborted!" << G4endl << G4endl;
187 anEvent->SetEventAborted();
191 G4cout << __METHOD_NAME__ << coords << G4endl;
194 G4ThreeVector PartMomDir(coords.global.xp,coords.global.yp,coords.global.zp);
195 G4ThreeVector PartPosition(coords.global.x,coords.global.y,coords.global.z);
199 particleGun->SetParticleMomentumDirection(PartMomDir);
205 auto vertex = anEvent->GetPrimaryVertex();
206 vertex->SetWeight(coords.local.weight);
222 G4bool generatedVertexOK =
false;
223 if (!distributionFinished)
227 if (distributionFinished && !generatedVertexOK)
229 G4bool endRunNow =
false;
233 G4cout << __METHOD_NAME__ <<
"distribution file finished (matched in length): ending run." << G4endl;
235 {BDS::Warning(__METHOD_NAME__,
"no events passed filters and were simulated.");}
242 G4int currentEventIndex = G4RunManager::GetRunManager()->GetCurrentRun()->GetNumberOfEvent();
243 G4cerr << __METHOD_NAME__ <<
"unable to generate " << nGenerateRequested
244 <<
" events as fewer events passed the filters in the file." << G4endl;
245 G4cerr << __METHOD_NAME__ << currentEventIndex <<
" events generated" << G4endl;
250 anEvent->SetEventAborted();
251 G4EventManager::GetEventManager()->AbortCurrentEvent();
252 G4RunManager::GetRunManager()->AbortRun();
256 else if (!generatedVertexOK)
258 anEvent->SetEventAborted();
259 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)