26 std::vector<double>& hadronEnergies,
27 std::vector<int>& hadronTypes,
28 std::vector<std::string>& hadronNames,
29 std::vector<double>& hadronMasses,
30 std::vector<double>& hadronPMin,
32 std::vector<double>& lengthRatio,
33 std::vector< std::vector<double> >&
ratios,
34 std::map<int,int >& idMap,
36 unsigned int distAlgo,
40 thePionEN(hadronEnergies),
41 thePionID(hadronTypes),
42 thePionNA(hadronNames),
43 thePionMA(hadronMasses),
44 thePionPMin(hadronPMin),
45 thePionEnergy(pionEnergy),
46 theLengthRatio(lengthRatio),
49 theDistAlgo(distAlgo),
51 currentValuesWereSet(
false)
57 TFile* aVFile=
nullptr;
58 std::vector<TTree*> aVTree(
thePionEN.size(),
static_cast<TTree*
>(
nullptr));
59 std::vector<TBranch*> aVBranch(
thePionEN.size(),
static_cast<TBranch*
>(
nullptr));
60 std::vector<NUEvent*> aVNUEvents(
thePionEN.size(),
static_cast<NUEvent*
>(
nullptr));
61 std::vector<unsigned> aVCurrentEntry(
thePionEN.size(),
static_cast<unsigned>(0));
62 std::vector<unsigned> aVCurrentInteraction(
thePionEN.size(),
static_cast<unsigned>(0));
63 std::vector<unsigned> aVNumberOfEntries(
thePionEN.size(),
static_cast<unsigned>(0));
64 std::vector<unsigned> aVNumberOfInteractions(
thePionEN.size(),
static_cast<unsigned>(0));
66 std::vector<double> aVPionCM(
thePionEN.size(),
static_cast<double>(0));
78 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
93 std::cout <<
"***WARNING*** You are reading nuclear-interaction information from the file " 94 << inputFile <<
" created in an earlier run." 104 edm::FileInPath myDataFile(
"FastSimulation/MaterialEffects/data/NuclearInteractions.root");
107 theFile = TFile::Open(fullPath.c_str());
110 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
111 for (
unsigned iene=0; iene<
thePionEN.size(); ++iene ) {
115 filename <<
"NuclearInteractionsVal_" <<
thePionNA[iname] <<
"_E"<< theEne <<
".root";
124 <<
"Tree with name " << treeName <<
" not found ";
129 <<
"Branch with name nuEvent not found in " <<
theFileNames[iname][iene];
139 unsigned NInteractions =
theNUEvents[iname][iene]->nInteractions();
152 thePionCM[iname][iene] = (Reference+Proton).M();
189 for(
auto evtPtr: vEvents) {
205 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
206 for (
unsigned iene=0; iene<
thePionEN.size(); ++iene ) {
210 unsigned NInteractions =
theNUEvents[iname][iene]->nInteractions();
232 unsigned fPid =
abs(thePid);
233 if ( fPid != 211 && fPid != 130 && fPid != 321 && fPid != 2112 && fPid != 2212 ) {
240 unsigned thePidIndex =
index(thePid);
245 double ee = pHadron > 0.6 ?
247 double theElasticLength = ( 0.8753 * ee + 0.15 )
250 * theInelasticLength;
253 double theTotalInteractionLength = theInelasticLength + theElasticLength;
257 if ( aNuclInteraction < theTotalInteractionLength ) {
261 if ( elastic < theElasticLength/theTotalInteractionLength ) {
270 double phi = 2. * 3.14159265358979323 * random->
flatShoot();
297 const std::vector<double>& aPionCM =
thePionCM[thePidIndex];
298 const std::vector<double>& aRatios =
theRatios[thePidIndex];
310 double ecm = (Proton+Hadron).M();
319 double ecm1= (Proton+Hadron0).M();
323 double ecm2=aPionCM[0];
325 double ratio2=aRatios[0];
326 if ( ecm > aPionCM[0] && ecm < aPionCM [ aPionCM.size()-1 ] ) {
327 for (
unsigned ene=1;
328 ene < aPionCM.size() && ecm > aPionCM[ene-1];
330 if ( ecm<aPionCM[ene] ) {
333 ecm1 = aPionCM[ene1];
334 ecm2 = aPionCM[ene2];
335 ratio1 = aRatios[ene1];
336 ratio2 = aRatios[ene2];
339 }
else if ( ecm > aPionCM[ aPionCM.size()-1 ] ) {
340 ene1 = aPionCM.size()-1;
341 ene2 = aPionCM.size()-2;
342 ecm1 = aPionCM[ene1];
343 ecm2 = aPionCM[ene2];
344 ratio1 = aRatios[ene2];
345 ratio2 = aRatios[ene2];
350 double slope = (std::log10(ecm )-std::log10(ecm1))
351 / (std::log10(ecm2)-std::log10(ecm1));
352 double inelastic = ratio1 + (ratio2-ratio1) * slope;
353 double inelastic4 = pHadron < 4. ? aRatios[
ien4] : 1.;
360 if ( elastic > 1.- (inelastic*theInelasticLength)
361 /theTotalInteractionLength ) {
366 std::vector<NUEvent*>& aNUEvents =
theNUEvents[thePidIndex];
375 if ( random->
flatShoot() < slope || aNumberOfInteractions[ene1] == 0 )
390 theBoost /= theBoost.e();
401 if ( aCurrentInteraction[ene] == aNumberOfInteractions[ene] ) {
405 std::vector<TTree*>& aTrees =
theTrees[thePidIndex];
406 ++aCurrentEntry[ene];
409 aCurrentInteraction[ene] = 0;
410 if ( aCurrentEntry[ene] == aNumberOfEntries[ene] ) {
411 aCurrentEntry[ene] = 0;
414 unsigned myEntry = aCurrentEntry[ene];
417 aTrees[ene]->GetEntry(myEntry);
419 aNumberOfInteractions[ene] = aNUEvents[ene]->nInteractions();
425 = aNUEvents[ene]->theNUInteractions()[aCurrentInteraction[ene]];
427 unsigned firstTrack = anInteraction.
first;
428 unsigned lastTrack = anInteraction.
last;
434 double distMin = 1E99;
437 XYZVector theAxis = theBoost.Vect().Unit();
438 double theAngle = random->
flatShoot() * 2. * 3.14159265358979323;
444 XYZVector orthAxis = (zAxis.Cross(theBoost.Vect())).Unit();
445 double orthAngle = acos(theBoost.Vect().Unit().Z());
452 for (
unsigned iTrack=firstTrack; iTrack<=lastTrack; ++iTrack ) {
454 unsigned idaugh = iTrack - firstTrack;
467 + aParticle.
py*aParticle.
py 468 + aParticle.
pz*aParticle.
pz 469 + aParticle.
mass*aParticle.
mass/(ecm*ecm) );
474 aParticle.
px*ecm,aParticle.
py*ecm,
475 aParticle.
pz*ecm,energy*ecm)));
477 aDaughter.
rotate(orthRotation);
480 aDaughter.
rotate(axisRotation);
483 aDaughter.
boost(axisBoost);
488 if ( distance < distMin && distance <
theDistCut ) {
512 ++aCurrentInteraction[ene];
515 }
else if ( pHadron < 4. &&
516 elastic > 1.- (inelastic4*theInelasticLength)
517 /theTotalInteractionLength ) {
540 if ( fabs(Particle.
charge()) > 1E-12 ) {
543 double chargeDiff = fabs(aDaughter.
charge()-Particle.
charge());
544 if ( fabs(chargeDiff) < 1E-12 ) {
551 distance = (aDaughter.
Vect().Unit().Cross(Particle.
Vect().Unit())).R();
556 distance = (aDaughter.
Vect().Cross(Particle.
Vect())).R()
557 /aDaughter.
Vect().Mag2();
591 std::vector<unsigned> theCurrentEntries;
592 theCurrentEntries.resize(size1);
593 size1*=
sizeof(unsigned);
598 std::vector<unsigned> theCurrentInteractions;
599 theCurrentInteractions.resize(size2);
600 size2 *=
sizeof(unsigned);
603 std::vector< std::vector<unsigned> >::const_iterator aCurrentEntry =
theCurrentEntry.begin();
604 std::vector< std::vector<unsigned> >::const_iterator lastCurrentEntry =
theCurrentEntry.end();
605 unsigned allEntries=0;
606 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
607 unsigned size = aCurrentEntry->size();
608 for (
unsigned iene=0; iene<
size; ++iene )
609 theCurrentEntries[allEntries++] = (*aCurrentEntry)[iene];
613 std::vector< std::vector<unsigned> >::const_iterator aCurrentInteraction =
theCurrentInteraction.begin();
614 std::vector< std::vector<unsigned> >::const_iterator lastCurrentInteraction =
theCurrentInteraction.end();
615 unsigned allInteractions=0;
616 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
617 unsigned size = aCurrentInteraction->size();
618 for (
unsigned iene=0; iene<
size; ++iene )
619 theCurrentInteractions[allInteractions++] = (*aCurrentInteraction)[iene];
622 myOutputFile.write((
const char*)(&theCurrentEntries.front()), size1);
623 myOutputFile.write((
const char*)(&theCurrentInteractions.front()), size2);
631 std::ifstream myInputFile;
637 std::vector<unsigned> theCurrentEntries;
638 theCurrentEntries.resize(size1);
639 size1*=
sizeof(unsigned);
644 std::vector<unsigned> theCurrentInteractions;
645 theCurrentInteractions.resize(size2);
646 size2 *=
sizeof(unsigned);
652 myInputFile.open (inputFile.c_str());
653 if ( myInputFile.is_open() ) {
656 if (
stat(inputFile.c_str(), &
results) == 0 ) size = results.st_size;
660 myInputFile.seekg(size-size1-size2);
661 myInputFile.read((
char*)(&theCurrentEntries.front()),size1);
662 myInputFile.read((
char*)(&theCurrentInteractions.front()),size2);
666 std::vector< std::vector<unsigned> >::iterator aCurrentEntry =
theCurrentEntry.begin();
667 std::vector< std::vector<unsigned> >::iterator lastCurrentEntry =
theCurrentEntry.end();
668 unsigned allEntries=0;
669 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
670 unsigned size = aCurrentEntry->size();
671 for (
unsigned iene=0; iene<
size; ++iene )
672 (*aCurrentEntry)[iene] = theCurrentEntries[allEntries++];
678 unsigned allInteractions=0;
679 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
680 unsigned size = aCurrentInteraction->size();
681 for (
unsigned iene=0; iene<
size; ++iene )
682 (*aCurrentInteraction)[iene] = theCurrentInteractions[allInteractions++];
696 while ( thePid !=
thePionID[myIndex] ) ++myIndex;
std::map< int, int > theIDMap
void boost(double bx, double by, double bz)
const HepPDT::ParticleDataTable * particleDataTable() const
XYZVector Vect() const
the momentum threevector
double flatShoot(double xmin=0.0, double xmax=1.0) const
static const double slope[3]
Sin< T >::type sin(const T &t)
void compute(ParticlePropagator &Particle, RandomEngineAndDistribution const *) override
Generate a nuclear interaction according to the probability that it happens.
Geom::Theta< T > theta() const
XYZVector orthogonal(const XYZVector &) const
A vector orthogonal to another one (because it's not in XYZTLorentzVector)
std::vector< std::vector< unsigned > > theNumberOfInteractions
std::vector< double > thePionMA
std::vector< std::vector< TTree * > > theTrees
int pid() const
get the HEP particle ID number
std::vector< int > thePionID
RawParticle const & particle() const
The particle being propagated.
double mass() const
get the MEASURED mass
std::vector< std::vector< unsigned > > theCurrentEntry
RawParticle makeParticle(HepPDT::ParticleDataTable const *, int id, const math::XYZTLorentzVector &p)
std::vector< std::vector< std::string > > theFileNames
std::vector< std::vector< unsigned > > theNumberOfEntries
bool currentValuesWereSet
std::vector< std::vector< double > > thePionCM
Abs< T >::type abs(const T &t)
void rotate(double rphi, const XYZVector &raxis)
bool read(std::string inputFile)
Read former nuclear interaction (from previous run)
double charge() const
get the MEASURED charge
unsigned index(int thePid)
Return a hashed index for a given pid.
std::vector< std::string > thePionNA
~NuclearInteractionSimulator() override
Default Destructor.
double M2() const
mass squared
std::vector< std::vector< TBranch * > > theBranches
std::vector< std::vector< unsigned > > theCurrentInteraction
std::vector< std::vector< double > > theRatios
NuclearInteractionSimulator(std::vector< double > &hadronEnergies, std::vector< int > &hadronTypes, std::vector< std::string > &hadronNames, std::vector< double > &hadronMasses, std::vector< double > &hadronPMin, double pionEnergy, std::vector< double > &lengthRatio, std::vector< std::vector< double > > &ratios, std::map< int, int > &idMap, std::string inputFile, unsigned int distAlgo, double distCut)
Constructor.
std::ofstream myOutputFile
std::vector< double > theLengthRatio
std::vector< RawParticle > _theUpdatedState
ROOT::Math::AxisAngle Rotation
std::vector< double > thePionEN
std::string fullPath() const
void save() override
Save current nuclear interaction (for later use)
std::vector< std::vector< NUEvent * > > theNUEvents
math::XYZVector XYZVector
int theClosestChargedDaughterId
Power< A, B >::type pow(const A &a, const B &b)
std::vector< double > thePionPMin
double distanceToPrimary(const RawParticle &Particle, const RawParticle &aDaughter) const
Compute distance between secondary and primary.
math::XYZTLorentzVector XYZTLorentzVector