25 std::vector<double>& hadronEnergies,
26 std::vector<int>& hadronTypes,
27 std::vector<std::string>& hadronNames,
28 std::vector<double>& hadronMasses,
29 std::vector<double>& hadronPMin,
31 std::vector<double>& lengthRatio,
32 std::vector< std::vector<double> >&
ratios,
33 std::map<int,int >& idMap,
35 unsigned int distAlgo,
39 thePionEN(hadronEnergies),
40 thePionID(hadronTypes),
41 thePionNA(hadronNames),
42 thePionMA(hadronMasses),
43 thePionPMin(hadronPMin),
44 thePionEnergy(pionEnergy),
45 theLengthRatio(lengthRatio),
48 theDistAlgo(distAlgo),
50 currentValuesWereSet(
false)
56 TFile* aVFile=
nullptr;
57 std::vector<TTree*> aVTree(
thePionEN.size(),
static_cast<TTree*
>(
nullptr));
58 std::vector<TBranch*> aVBranch(
thePionEN.size(),
static_cast<TBranch*
>(
nullptr));
59 std::vector<NUEvent*> aVNUEvents(
thePionEN.size(),
static_cast<NUEvent*
>(
nullptr));
60 std::vector<unsigned> aVCurrentEntry(
thePionEN.size(),
static_cast<unsigned>(0));
61 std::vector<unsigned> aVCurrentInteraction(
thePionEN.size(),
static_cast<unsigned>(0));
62 std::vector<unsigned> aVNumberOfEntries(
thePionEN.size(),
static_cast<unsigned>(0));
63 std::vector<unsigned> aVNumberOfInteractions(
thePionEN.size(),
static_cast<unsigned>(0));
65 std::vector<double> aVPionCM(
thePionEN.size(),
static_cast<double>(0));
77 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
92 std::cout <<
"***WARNING*** You are reading nuclear-interaction information from the file " 93 << inputFile <<
" created in an earlier run." 103 edm::FileInPath myDataFile(
"FastSimulation/MaterialEffects/data/NuclearInteractions.root");
106 theFile = TFile::Open(fullPath.c_str());
109 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
110 for (
unsigned iene=0; iene<
thePionEN.size(); ++iene ) {
114 filename <<
"NuclearInteractionsVal_" <<
thePionNA[iname] <<
"_E"<< theEne <<
".root";
123 <<
"Tree with name " << treeName <<
" not found ";
128 <<
"Branch with name nuEvent not found in " <<
theFileNames[iname][iene];
138 unsigned NInteractions =
theNUEvents[iname][iene]->nInteractions();
151 thePionCM[iname][iene] = (Reference+Proton).M();
188 for(
auto evtPtr: vEvents) {
204 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
205 for (
unsigned iene=0; iene<
thePionEN.size(); ++iene ) {
209 unsigned NInteractions =
theNUEvents[iname][iene]->nInteractions();
219 double pHadron =
std::sqrt(Particle.Vect().Mag2());
226 std::map<int,int>::const_iterator thePit =
theIDMap.find(Particle.
pid());
228 int thePid = thePit !=
theIDMap.end() ? thePit->second : Particle.
pid();
231 unsigned fPid =
abs(thePid);
232 if ( fPid != 211 && fPid != 130 && fPid != 321 && fPid != 2112 && fPid != 2212 ) {
239 unsigned thePidIndex =
index(thePid);
244 double ee = pHadron > 0.6 ?
246 double theElasticLength = ( 0.8753 * ee + 0.15 )
249 * theInelasticLength;
252 double theTotalInteractionLength = theInelasticLength + theElasticLength;
256 if ( aNuclInteraction < theTotalInteractionLength ) {
260 if ( elastic < theElasticLength/theTotalInteractionLength ) {
269 double phi = 2. * 3.14159265358979323 * random->
flatShoot();
274 Particle.
rotate(rotation1);
275 Particle.
rotate(rotation2);
284 Particle.Pz(), Particle.E());
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;
433 double distMin = 1E99;
436 XYZVector theAxis = theBoost.Vect().Unit();
437 double theAngle = random->
flatShoot() * 2. * 3.14159265358979323;
443 XYZVector orthAxis = (zAxis.Cross(theBoost.Vect())).Unit();
444 double orthAngle = acos(theBoost.Vect().Unit().Z());
451 for (
unsigned iTrack=firstTrack; iTrack<=lastTrack; ++iTrack ) {
453 unsigned idaugh = iTrack - firstTrack;
466 + aParticle.
py*aParticle.
py 467 + aParticle.
pz*aParticle.
pz 468 + aParticle.
mass*aParticle.
mass/(ecm*ecm) );
471 aDaughter.SetXYZT(aParticle.
px*ecm,aParticle.
py*ecm,
472 aParticle.
pz*ecm,energy*ecm);
476 aDaughter.
rotate(orthRotation);
479 aDaughter.
rotate(axisRotation);
482 aDaughter.
boost(axisBoost);
487 if ( distance < distMin && distance <
theDistCut ) {
511 ++aCurrentInteraction[ene];
514 }
else if ( pHadron < 4. &&
515 elastic > 1.- (inelastic4*theInelasticLength)
516 /theTotalInteractionLength ) {
538 if ( fabs(Particle.
charge()) > 1E-12 ) {
541 double chargeDiff = fabs(aDaughter.
charge()-Particle.
charge());
542 if ( fabs(chargeDiff) < 1E-12 ) {
549 distance = (aDaughter.Vect().Unit().Cross(Particle.Vect().Unit())).R();
554 distance = (aDaughter.Vect().Cross(Particle.Vect())).R()
555 /aDaughter.Vect().Mag2();
589 std::vector<unsigned> theCurrentEntries;
590 theCurrentEntries.resize(size1);
591 size1*=
sizeof(unsigned);
596 std::vector<unsigned> theCurrentInteractions;
597 theCurrentInteractions.resize(size2);
598 size2 *=
sizeof(unsigned);
601 std::vector< std::vector<unsigned> >::const_iterator aCurrentEntry =
theCurrentEntry.begin();
602 std::vector< std::vector<unsigned> >::const_iterator lastCurrentEntry =
theCurrentEntry.end();
603 unsigned allEntries=0;
604 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
605 unsigned size = aCurrentEntry->size();
606 for (
unsigned iene=0; iene<
size; ++iene )
607 theCurrentEntries[allEntries++] = (*aCurrentEntry)[iene];
611 std::vector< std::vector<unsigned> >::const_iterator aCurrentInteraction =
theCurrentInteraction.begin();
612 std::vector< std::vector<unsigned> >::const_iterator lastCurrentInteraction =
theCurrentInteraction.end();
613 unsigned allInteractions=0;
614 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
615 unsigned size = aCurrentInteraction->size();
616 for (
unsigned iene=0; iene<
size; ++iene )
617 theCurrentInteractions[allInteractions++] = (*aCurrentInteraction)[iene];
620 myOutputFile.write((
const char*)(&theCurrentEntries.front()), size1);
621 myOutputFile.write((
const char*)(&theCurrentInteractions.front()), size2);
629 std::ifstream myInputFile;
635 std::vector<unsigned> theCurrentEntries;
636 theCurrentEntries.resize(size1);
637 size1*=
sizeof(unsigned);
642 std::vector<unsigned> theCurrentInteractions;
643 theCurrentInteractions.resize(size2);
644 size2 *=
sizeof(unsigned);
650 myInputFile.open (inputFile.c_str());
651 if ( myInputFile.is_open() ) {
654 if (
stat(inputFile.c_str(), &
results) == 0 ) size = results.st_size;
658 myInputFile.seekg(size-size1-size2);
659 myInputFile.read((
char*)(&theCurrentEntries.front()),size1);
660 myInputFile.read((
char*)(&theCurrentInteractions.front()),size2);
664 std::vector< std::vector<unsigned> >::iterator aCurrentEntry =
theCurrentEntry.begin();
665 std::vector< std::vector<unsigned> >::iterator lastCurrentEntry =
theCurrentEntry.end();
666 unsigned allEntries=0;
667 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
668 unsigned size = aCurrentEntry->size();
669 for (
unsigned iene=0; iene<
size; ++iene )
670 (*aCurrentEntry)[iene] = theCurrentEntries[allEntries++];
676 unsigned allInteractions=0;
677 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
678 unsigned size = aCurrentInteraction->size();
679 for (
unsigned iene=0; iene<
size; ++iene )
680 (*aCurrentInteraction)[iene] = theCurrentInteractions[allInteractions++];
694 while ( thePid !=
thePionID[myIndex] ) ++myIndex;
std::map< int, int > theIDMap
void boost(double bx, double by, double bz)
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
double mass() const
get the MEASURED mass
std::vector< std::vector< unsigned > > theCurrentEntry
std::vector< std::vector< std::string > > theFileNames
math::XYZVector XYZVector
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.
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
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