19#include "BDSBunchEventGenerator.hh"
21#include "BDSException.hh"
22#include "BDSOutputLoaderSampler.hh"
23#include "BDSPrimaryGeneratorFileSampler.hh"
24#include "BDSParticleCoords.hh"
25#include "BDSParticleCoordsFull.hh"
26#include "BDSParticleCoordsFullGlobal.hh"
27#include "BDSPhysicalConstants.hh"
28#include "BDSPrimaryVertexInformation.hh"
29#include "BDSPrimaryVertexInformationV.hh"
30#include "BDSUtilities.hh"
31#include "BDSWarning.hh"
34#include "G4EventManager.hh"
35#include "G4LorentzVector.hh"
36#include "G4PrimaryParticle.hh"
37#include "G4PrimaryVertex.hh"
39#include "CLHEP/Units/SystemOfUnits.h"
46 const G4String& fileNameIn,
49 G4bool removeUnstableWithoutDecayIn,
50 G4bool warnAboutSkippedParticlesIn):
54 removeUnstableWithoutDecay(removeUnstableWithoutDecayIn),
55 warnAboutSkippedParticles(warnAboutSkippedParticlesIn)
58 samplerName = ba.second;
59 G4cout << __METHOD_NAME__ <<
"reading sampler named: \"" << samplerName <<
"\"" << G4endl;
62 nEventsInFile = reader->NEventsInFile();
63 bunch->SetNEventsInFile(nEventsInFile);
64 bunch->SetNOriginalEvents(reader->NOriginalEvents());
65 G4cout << __METHOD_NAME__ << nEventsInFile <<
" events found in file" << G4endl;
67 {
throw BDSException(__METHOD_NAME__,
"must be constructed with a valid BDSBunchEventGenerator instance");}
71BDSPrimaryGeneratorFileSampler::~BDSPrimaryGeneratorFileSampler()
79 {
throw BDSException(__METHOD_NAME__,
"no file reader available");}
81 currentVertices.clear();
84 if (currentFileEventIndex >= nEventsInFile)
86 endOfFileReached =
true;
87 G4cout << __METHOD_NAME__ <<
"End of file reached. ";
93 G4cout <<
"Returning to beginning of file for next event." << G4endl;
94 currentFileEventIndex = 0;
95 endOfFileReached =
false;
99 BDS::Warning(__METHOD_NAME__,
"file looping requested, but 0 events passed filters - ending.");
115 G4cout <<
"BDSROOTSamplerLoader::RecreateAdvanceToEvent> Advancing file to event: " << eventOffset << G4endl;
123 const auto sampler = reader->SamplerDataFloat(index);
126 for (
int i = 0; i < n; i++)
128 G4int pdgID = (G4int)sampler->partID[i];
129 G4double xp = (G4double)sampler->xp[i];
130 G4double yp = (G4double)sampler->yp[i];
131 G4double zp = (G4double)sampler->zp[i];
132 G4double p = (G4double)sampler->p[i];
133 G4ThreeVector momentum = G4ThreeVector(xp,yp,zp) * p * CLHEP::GeV;
134 G4double x = (G4double)sampler->x[i] * CLHEP::m;
135 G4double y = (G4double)sampler->y[i] * CLHEP::m;
136 G4ThreeVector localPosition(x,y,0);
137 auto g4prim =
new G4PrimaryParticle(pdgID, momentum.x(), momentum.y(), momentum.z());
142void BDSPrimaryGeneratorFileSampler::ReadPrimaryParticlesDouble(G4long index)
145 const auto sampler = reader->SamplerDataDouble(index);
148 for (
int i = 0; i < n; i++)
150 G4int pdgID = (G4int)sampler->partID[i];
151 G4double xp = (G4double)sampler->xp[i];
152 G4double yp = (G4double)sampler->yp[i];
153 G4double zp = (G4double)sampler->zp[i];
154 G4double p = (G4double)sampler->p[i];
155 G4ThreeVector momentum = G4ThreeVector(xp,yp,zp) * p;
156 G4double x = (G4double)sampler->x[i] * CLHEP::m;
157 G4double y = (G4double)sampler->y[i] * CLHEP::m;
158 G4ThreeVector localPosition(x,y,0);
159 auto g4prim =
new G4PrimaryParticle(pdgID, momentum.x(), momentum.y(), momentum.z());
160 vertices.emplace_back(DisplacedVertex{localPosition, g4prim});
166 if (reader->DoublePrecision())
167 {ReadPrimaryParticlesDouble(index);}
171 double overallWeight = 1.0;
172 G4int nParticlesSkipped = 0;
173 for (
const auto& xyzVertex :
vertices)
175 const auto vertex = xyzVertex.vertex;
180 const G4ParticleDefinition* pd = vertex->GetParticleDefinition();
181 G4bool deleteIt = !pd;
182 if (pd && removeUnstableWithoutDecay)
183 {deleteIt = !(pd->GetPDGStable()) && !pd->GetDecayTable();}
191 G4ThreeVector unitMomentum = vertex->GetMomentumDirection();
192 unitMomentum.transform(referenceBeamMomentumOffset);
193 G4double rp = unitMomentum.perp();
206 vertex->GetTotalEnergy(),
209 if (!bunch->
AcceptParticle(local, rp, vertex->GetKineticEnergy(), vertex->GetPDGcode()))
217 auto g4vtx =
new G4PrimaryVertex(fullCoordsGlobal.global.x,
218 fullCoordsGlobal.global.y,
219 fullCoordsGlobal.global.z,
220 fullCoordsGlobal.global.T);
231 G4double charge = vertex->GetCharge();
232 G4double momentum = vertex->GetTotalMomentum();
236 brho *= CLHEP::tesla*CLHEP::m;
239 vertex->GetTotalMomentum(),
243 vertex->GetPDGcode());
246 auto prim =
new G4PrimaryParticle(*vertex);
247 prim->SetMomentumDirection(G4ThreeVector(fullCoordsGlobal.global.xp,
248 fullCoordsGlobal.global.yp,
249 fullCoordsGlobal.global.zp));
250 g4vtx->SetPrimary(prim);
251 g4vtx->SetUserInformation(vertexInfo);
252 currentVertices.push_back(g4vtx);
255 if (nParticlesSkipped > 0 && warnAboutSkippedParticles)
256 {G4cout << __METHOD_NAME__ << nParticlesSkipped <<
" particles skipped" << G4endl;}
258 if (currentVertices.empty())
261 if (warnAboutSkippedParticles)
262 {G4cout << __METHOD_NAME__ <<
"no particles found in event number: " << currentFileEventIndex <<
" in the file" << G4endl;}
266 vertexGeneratedSuccessfully =
true;
267 nEventsReadThatPassedFilters++;
275 currentFileEventIndex++;
277 for (
auto* v : currentVertices)
278 {anEvent->AddPrimaryVertex(v);}
279 currentVertices.clear();
284 if (nEventsToSkip > 0)
285 {G4cout << __METHOD_NAME__ <<
"skipping " << nEventsToSkip <<
" into file." << G4endl;}
289 G4long nAvailable = nEventsInFile * distrFileLoopNTimes;
290 if ((G4long)nEventsToSkip > nAvailable)
292 G4String msg =
"number of events to skip (" + std::to_string(nEventsToSkip) +
") is greater than the number of events (";
293 msg += std::to_string(nEventsInFile);
294 if (distrFileLoopNTimes > 1)
295 {msg +=
" x " + std::to_string(distrFileLoopNTimes) +
" loops of file";}
296 msg +=
") in this file.";
297 throw BDSException(
"BDSBunchUserFile::RecreateAdvanceToEvent>", msg);
299 G4long nToSkipSinglePass = nAvailable % nEventsToSkip;
300 currentFileEventIndex = nToSkipSinglePass;
A wrapper of BDSBunch to include a filter for the events loaded by an event generator.
G4RotationMatrix ReferenceBeamMomentumOffset() const
Get a rotation matrix according to Xp0 and Yp0.
G4int eventGeneratorNEventsSkip
Cache of limit.
G4bool AcceptParticle(const BDSParticleCoordsFull &coords, G4double rpOriginal, G4double kineticEnergy, G4int pdgID)
G4int DistrFileLoopNTimes() const
Accessor.
BDSParticleCoordsFullGlobal ApplyTransform(const BDSParticleCoordsFull &localIn) const
virtual BDSParticleCoordsFull GetNextParticleLocal()
General exception with possible name of object and message.
Loader of ROOT Event output for receating events.
A set of particle coordinates in both local and global.
A set of particle coordinates including energy and weight.
G4double s
TODO - remove this. Unused. S (global) is calculated from S0 + z.
void AddOffset(const G4ThreeVector &offset)
Apply an offset to the spatial coordinates only - assignment.
virtual void RecreateAdvanceToEvent(G4int eventOffset)
Advance to the correct event number in the file for recreation.
void ReadSingleEvent(G4long index, G4Event *anEvent)
Read sampler hits and put into primary vertices if they pass filters.
std::vector< DisplacedVertex > vertices
Used for transiently loading information.
BDSPrimaryGeneratorFileSampler()=delete
Do not require default constructor.
void ReadPrimaryParticlesFloat(G4long index)
Conversion from HepMC::GenEvent to G4Event.
void SkipEvents(G4long nEventsToSkip)
virtual void GeneratePrimaryVertex(G4Event *anEvent)
Read the next non-empty sampler entry from the file.
Common interface for any primary generators that are file based.
void ThrowExceptionIfRecreateOffsetTooHigh(G4long eventOffset) const
G4bool VertexInsideWorld(const G4ThreeVector &pos) const
Utility function for derived classes to check a position is inside the world.
G4bool OKToLoopFile() const
std::pair< G4String, G4String > SplitOnColon(const G4String &formatAndPath)
static const G4double cOverGeV
speed of light / 1 GeV, used for scaling in brho calculation
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())