7#include "CLHEP/Random/RandFlat.h"
12 charAddress(
auto &val){
13 return reinterpret_cast<char *
> (&
val);
64 std::cerr <<
"Error: could not open output file" <<
m_fileName << std::endl;
75 std::cerr <<
"Error: the number of particles in the input file is not known." << std::endl;
83 std::cerr <<
"Error: could not open input file" <<
m_fileName << std::endl;
119 std::cerr <<
"Error: the file " <<
m_fileName <<
" is currently open for reading." << std::endl;
124 double px = 0., py = 0., pz = 0., e = 0.,
x = 0.,
y = 0.,
z = 0., t = 0., weight = 0.;
126 pdgId = particle->pdgId();
127 px = particle->fourVector().px();
128 py = particle->fourVector().py();
129 pz = particle->fourVector().pz();
130 e = particle->fourVector().e();
131 x = particle->positionAtScoringPlane().x();
132 y = particle->positionAtScoringPlane().y();
133 z = particle->positionAtScoringPlane().z();
134 t = particle->positionAtScoringPlane().t();
135 weight = particle->weight();
180 double generatedWeightSum;
185 std::cerr <<
"Error: the file " <<
m_fileName <<
" is currently open for writing." << std::endl;
190 std::cerr <<
"Error: the RandomEngine pointer is null." << std::endl;
194 long particleIndex = 0;
197 generatedWeightSum = CLHEP::RandFlat::shoot(engine)*
m_upperBinEdge;
205 while(superBinItr!=superBinItr_end && !found) {
209 if(generatedWeightSum <= (*superBinItr).upperBinEdge()) {
214 particleIndex += (*superBinItr).intervalUpperBinEdges()->size();
220 std::cerr <<
"Could not find an interval for the weight sum " << generatedWeightSum << std::endl;
224 const std::vector<double> *intervalUpperBinEdges = (*superBinItr).intervalUpperBinEdges();
226 double intervalWeightSum = intervalUpperBinEdges->back();
230 generatedWeightSum = CLHEP::RandFlat::shoot(engine)*intervalWeightSum;
235 std::vector<double>::const_iterator intervalItr = intervalUpperBinEdges->begin();
236 std::vector<double>::const_iterator intervalItr_end = intervalUpperBinEdges->end();
238 while(intervalItr!=intervalItr_end && !found) {
242 if(generatedWeightSum <= (*intervalItr)) {
253 std::cerr <<
"Could not find a particle for the weight sum " << generatedWeightSum << std::endl;
258 std::cerr <<
"Particle index " << particleIndex <<
" is out of range." << std::endl;
265 if(!beamHaloParticle)
return 0;
267 return beamHaloParticle;
274 std::cerr <<
"Error: the file " <<
m_fileName <<
" is current open for writing." << std::endl;
279 double px = 0., py = 0., pz = 0., e = 0.,
x = 0.,
y = 0.,
z = 0., t = 0., weight = 0.;
292 HepMC::FourVector fourVector(px,py,pz,e);
293 HepMC::FourVector positionAtScoringPlane(
x,
y,
z,t);
296 return beamHaloParticle;
double m_intervalUpperBinEdge
The upper bin edge of a bin within a given interval.
BeamHaloParticle * readRandomParticle(CLHEP::HepRandomEngine *engine)
A member function to read a random particle from the binary file.
bool m_writeFlag
A flag to select read or write.
unsigned int m_particlesPerInterval
The number of particles per interval.
int writeParticle(BeamHaloParticle *particle)
A member function to append a particle to the binary file.
BeamHaloParticle * readParticle(void)
A member function to read a particle from the current position in the binary file.
BeamHaloParticleBuffer(const std::string &fileName)
std::ofstream m_ofstream
A data member to store the output file stream.
std::string m_fileName
File name.
double m_upperBinEdge
The upper edge of the bin.
std::vector< double > m_intervalUpperBinEdges
A vector of bin limits for a given interval.
std::vector< BinnedInterval > m_binnedIntervals
A map of the upper bin limits and an vector of bin limits within the given interval.
int m_recordSize
The size of 1 binary data record.
long m_numberOfParticles
The number of particles present within the output or input file.
std::ifstream m_ifstream
A data member to store the input file stream.
A class to describe a generic beam halo particle.
BinnedInterval(double upperBinEdge, const std::vector< double > &intervalUpperBinEdges)
const std::vector< double > * intervalUpperBinEdges() const
std::vector< double > * m_intervalUpperBinEdges
double upperBinEdge() const