19#include "BDSBunchEventGenerator.hh"
21#include "BDSException.hh"
22#include "BDSOutputLoaderSampler.hh"
23#include "BDSROOTSamplerReader.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"
33#include "G4LorentzVector.hh"
34#include "G4PrimaryParticle.hh"
35#include "G4PrimaryVertex.hh"
36#include "G4RunManager.hh"
37#include "G4TransportationManager.hh"
40#include "CLHEP/Units/SystemOfUnits.h"
47 const G4String& fileNameIn,
49 G4bool removeUnstableWithoutDecayIn,
50 G4bool warnAboutSkippedParticlesIn):
51 currentFileEventIndex(0),
56 removeUnstableWithoutDecay(removeUnstableWithoutDecayIn),
57 warnAboutSkippedParticles(warnAboutSkippedParticlesIn),
59 anyParticlesFoundAtAll(false)
62 samplerName = ba.second;
63 G4cout << __METHOD_NAME__ <<
"reading sampler named: \"" << samplerName <<
"\"" << G4endl;
66 nEventsInFile = reader->NEventsInFile();
69BDSROOTSamplerReader::~BDSROOTSamplerReader()
77 {
throw BDSException(__METHOD_NAME__,
"no file reader available");}
80 currentVertices.clear();
81 while (nParticles < 1)
83 if (currentFileEventIndex > nEventsInFile)
85 G4cout << __METHOD_NAME__ <<
"End of file reached. Return to beginning of file for next event." << G4endl;
86 currentFileEventIndex = 0;
89 nParticles = (G4int)currentVertices.size();
92 currentFileEventIndex++;
94 {
throw BDSException(__METHOD_NAME__,
"no events in file provide any suitable particles from the sampler "+samplerName);}
96 for (
auto* v : currentVertices)
97 {anEvent->AddPrimaryVertex(v);}
98 currentVertices.clear();
103 G4cout <<
"BDSROOTSamplerLoader::RecreateAdvanceToEvent> Advancing file to event: " << eventOffset << G4endl;
104 for (G4int i = 0; i < eventOffset; i++)
106 G4Event*
event =
new G4Event();
112void BDSROOTSamplerReader::ReadPrimaryParticlesFloat(G4long index)
115 const auto sampler = reader->SamplerDataFloat(index);
118 for (
int i = 0; i < n; i++)
120 G4int pdgID = (G4int)sampler->partID[i];
121 G4double xp = (G4double)sampler->xp[i];
122 G4double yp = (G4double)sampler->yp[i];
123 G4double zp = (G4double)sampler->zp[i];
124 G4double p = (G4double)sampler->p[i];
125 G4ThreeVector momentum = G4ThreeVector(xp,yp,zp) * p * CLHEP::GeV;
126 G4double x = (G4double)sampler->x[i] * CLHEP::m;
127 G4double y = (G4double)sampler->y[i] * CLHEP::m;
128 G4ThreeVector localPosition(x,y,0);
129 auto g4prim =
new G4PrimaryParticle(pdgID, momentum.x(), momentum.y(), momentum.z());
130 vertices.emplace_back(DisplacedVertex{localPosition, g4prim});
134void BDSROOTSamplerReader::ReadPrimaryParticlesDouble(G4long index)
137 const auto sampler = reader->SamplerDataDouble(index);
140 for (
int i = 0; i < n; i++)
142 G4int pdgID = (G4int)sampler->partID[i];
143 G4double xp = (G4double)sampler->xp[i];
144 G4double yp = (G4double)sampler->yp[i];
145 G4double zp = (G4double)sampler->zp[i];
146 G4double p = (G4double)sampler->p[i];
147 G4ThreeVector momentum = G4ThreeVector(xp,yp,zp) * p;
148 G4double x = (G4double)sampler->x[i] * CLHEP::m;
149 G4double y = (G4double)sampler->y[i] * CLHEP::m;
150 G4ThreeVector localPosition(x,y,0);
151 auto g4prim =
new G4PrimaryParticle(pdgID, momentum.x(), momentum.y(), momentum.z());
152 vertices.emplace_back(DisplacedVertex{localPosition, g4prim});
158 if (reader->DoublePrecision())
159 {ReadPrimaryParticlesDouble(index);}
161 {ReadPrimaryParticlesFloat(index);}
163 double overallWeight = 1.0;
164 G4int nParticlesSkipped = 0;
165 for (
const auto& xyzVertex :
vertices)
167 const auto vertex = xyzVertex.vertex;
172 const G4ParticleDefinition* pd = vertex->GetParticleDefinition();
173 G4bool deleteIt = !pd;
174 if (pd && removeUnstableWithoutDecay)
175 {deleteIt = !(pd->GetPDGStable()) && !pd->GetDecayTable();}
180 G4cout << __METHOD_NAME__ <<
"skipping particle with PDG ID: " << pd->GetPDGEncoding() << G4endl;
186 G4ThreeVector unitMomentum = vertex->GetMomentumDirection();
187 unitMomentum.transform(referenceBeamMomentumOffset);
188 G4double rp = unitMomentum.perp();
201 vertex->GetTotalEnergy(),
204 if (!bunch->
AcceptParticle(local, rp, vertex->GetKineticEnergy(), vertex->GetPDGcode()))
212 auto g4vtx =
new G4PrimaryVertex(fullCoordsGlobal.global.x,
213 fullCoordsGlobal.global.y,
214 fullCoordsGlobal.global.z,
215 fullCoordsGlobal.global.T);
226 G4double charge = vertex->GetCharge();
227 G4double momentum = vertex->GetTotalMomentum();
231 brho *= CLHEP::tesla*CLHEP::m;
234 vertex->GetTotalMomentum(),
238 vertex->GetPDGcode());
241 auto prim =
new G4PrimaryParticle(*vertex);
242 prim->SetMomentumDirection(G4ThreeVector(fullCoordsGlobal.global.xp,
243 fullCoordsGlobal.global.yp,
244 fullCoordsGlobal.global.zp));
245 g4vtx->SetPrimary(prim);
246 g4vtx->SetUserInformation(vertexInfo);
247 currentVertices.push_back(g4vtx);
250 if (nParticlesSkipped > 0 && warnAboutSkippedParticles)
251 {G4cout << __METHOD_NAME__ << nParticlesSkipped <<
" particles skipped" << G4endl;}
263 G4Navigator* navigator = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
264 G4VPhysicalVolume* world = navigator->GetWorldVolume();
265 worldSolid = world->GetLogicalVolume()->GetSolid();
267 EInside qinside = worldSolid->Inside(pos);
268 return qinside == kInside;
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.
G4bool AcceptParticle(const BDSParticleCoordsFull &coords, G4double rpOriginal, G4double kineticEnergy, G4int pdgID)
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 GeneratePrimaryVertex(G4Event *anEvent)
Read the next non-empty sampler entry from the file.
virtual void RecreateAdvanceToEvent(G4int eventOffset)
Advance to the correct event number in the file for recreation.
std::vector< DisplacedVertex > vertices
Used for transiently loading information.
void ReadSingleEvent(G4long index)
Clear the hepmcEvent object, reallocate and read a single event and fill that member.
G4bool anyParticlesFoundAtAll
virtual G4bool VertexInsideWorld(const G4ThreeVector &pos) const
Conversion from HepMC::GenEvent to G4Event.
BDSROOTSamplerReader()=delete
Do not require default constructor.
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())