7 #include "CLHEP/Random/RandFlat.h"
12 charAddress(
auto &
val){
13 return reinterpret_cast<char *
> (&
val);
21 const std::vector<double>& intervalUpperBinEdges): m_upperBinEdge(upperBinEdge) {
44 , m_numberOfParticles(0)
47 , m_intervalUpperBinEdges()
48 , m_intervalUpperBinEdge(0)
49 , m_writeFlag(false) {
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.;
131 x =
particle->positionAtScoringPlane().x();
132 y =
particle->positionAtScoringPlane().y();
133 z =
particle->positionAtScoringPlane().z();
134 t =
particle->positionAtScoringPlane().t();
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;