21 std::vector<double>& pionEnergies,
22 std::vector<int>& pionTypes,
23 std::vector<std::string>& pionNames,
24 std::vector<double>& pionMasses,
25 std::vector<double>& pionPMin,
27 std::vector<double>& lengthRatio,
28 std::vector< std::vector<double> >&
ratios,
29 std::map<int,int >& idMap,
31 unsigned int distAlgo,
35 thePionEN(pionEnergies),
38 thePionMA(pionMasses),
39 thePionPMin(pionPMin),
40 thePionEnergy(pionEnergy),
41 theLengthRatio(lengthRatio),
44 theDistAlgo(distAlgo),
55 std::vector<TFile*> aVFile(
thePionEN.size(),
static_cast<TFile*
>(0));
56 std::vector<TTree*> aVTree(
thePionEN.size(),
static_cast<TTree*
>(0));
57 std::vector<TBranch*> aVBranch(
thePionEN.size(),
static_cast<TBranch*
>(0));
59 std::vector<unsigned> aVCurrentEntry(
thePionEN.size(),
static_cast<unsigned>(0));
60 std::vector<unsigned> aVCurrentInteraction(
thePionEN.size(),
static_cast<unsigned>(0));
61 std::vector<unsigned> aVNumberOfEntries(
thePionEN.size(),
static_cast<unsigned>(0));
62 std::vector<unsigned> aVNumberOfInteractions(
thePionEN.size(),
static_cast<unsigned>(0));
64 std::vector<double> aVPionCM(
thePionEN.size(),
static_cast<double>(0));
75 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
91 std::cout <<
"***WARNING*** You are reading nuclear-interaction information from the file "
92 << inputFile <<
" created in an earlier run."
103 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
104 for (
unsigned iene=0; iene<
thePionEN.size(); ++iene ) {
108 filename <<
"NuclearInteractionsVal_" <<
thePionNA[iname] <<
"_E"<< theEne <<
".root";
115 theFiles[iname][iene] = TFile::Open(fullPath.c_str());
117 <<
"File " <<
theFileNames[iname][iene] <<
" " << fullPath <<
" not found ";
120 theTrees[iname][iene] = (TTree*)
theFiles[iname][iene]->Get(
"NuclearInteractions");
122 <<
"Tree with name NuclearInteractions not found in " <<
theFileNames[iname][iene];
127 <<
"Branch with name nuEvent not found in " <<
theFileNames[iname][iene];
144 unsigned NInteractions =
theNUEvents[iname][iene]->nInteractions();
171 thePionCM[iname][iene] = (Reference+Proton).M();
206 for (
unsigned iene=0; iene<
theFiles[
ifile].size(); ++iene ) {
227 double pHadron =
std::sqrt(Particle.Vect().Mag2());
234 std::map<int,int>::const_iterator thePit =
theIDMap.find(Particle.
pid());
236 int thePid = thePit !=
theIDMap.end() ? thePit->second : Particle.
pid();
239 unsigned fPid =
abs(thePid);
240 if ( fPid != 211 && fPid != 130 && fPid != 321 && fPid != 2112 && fPid != 2212 ) {
247 unsigned thePidIndex =
index(thePid);
252 double ee = pHadron > 0.6 ?
254 double theElasticLength = ( 0.8753 * ee + 0.15 )
257 * theInelasticLength;
260 double theTotalInteractionLength = theInelasticLength + theElasticLength;
264 if ( aNuclInteraction < theTotalInteractionLength ) {
268 if ( elastic < theElasticLength/theTotalInteractionLength ) {
277 double phi = 2. * 3.14159265358979323 * random->
flatShoot();
282 Particle.
rotate(rotation1);
283 Particle.
rotate(rotation2);
292 Particle.Pz(), Particle.E());
305 const std::vector<double>& aPionCM =
thePionCM[thePidIndex];
306 const std::vector<double>& aRatios =
theRatios[thePidIndex];
318 double ecm = (Proton+Hadron).M();
327 double ecm1= (Proton+Hadron0).M();
331 double ecm2=aPionCM[0];
333 double ratio2=aRatios[0];
334 if ( ecm > aPionCM[0] && ecm < aPionCM [ aPionCM.size()-1 ] ) {
335 for (
unsigned ene=1;
336 ene < aPionCM.size() && ecm > aPionCM[ene-1];
338 if ( ecm<aPionCM[ene] ) {
341 ecm1 = aPionCM[ene1];
342 ecm2 = aPionCM[ene2];
343 ratio1 = aRatios[ene1];
344 ratio2 = aRatios[ene2];
347 }
else if ( ecm > aPionCM[ aPionCM.size()-1 ] ) {
348 ene1 = aPionCM.size()-1;
349 ene2 = aPionCM.size()-2;
350 ecm1 = aPionCM[ene1];
351 ecm2 = aPionCM[ene2];
352 ratio1 = aRatios[ene2];
353 ratio2 = aRatios[ene2];
358 double slope = (std::log10(ecm )-std::log10(ecm1))
359 / (std::log10(ecm2)-std::log10(ecm1));
360 double inelastic = ratio1 + (ratio2-ratio1) * slope;
361 double inelastic4 = pHadron < 4. ? aRatios[
ien4] : 1.;
368 if ( elastic > 1.- (inelastic*theInelasticLength)
369 /theTotalInteractionLength ) {
374 std::vector<NUEvent*>& aNUEvents =
theNUEvents[thePidIndex];
383 if ( random->
flatShoot() < slope || aNumberOfInteractions[ene1] == 0 )
398 theBoost /= theBoost.e();
409 if ( aCurrentInteraction[ene] == aNumberOfInteractions[ene] ) {
413 std::vector<TTree*>& aTrees =
theTrees[thePidIndex];
414 ++aCurrentEntry[ene];
417 aCurrentInteraction[ene] = 0;
418 if ( aCurrentEntry[ene] == aNumberOfEntries[ene] ) {
419 aCurrentEntry[ene] = 0;
422 unsigned myEntry = aCurrentEntry[ene];
425 aTrees[ene]->GetEntry(myEntry);
427 aNumberOfInteractions[ene] = aNUEvents[ene]->nInteractions();
433 = aNUEvents[ene]->theNUInteractions()[aCurrentInteraction[ene]];
435 unsigned firstTrack = anInteraction.
first;
436 unsigned lastTrack = anInteraction.
last;
441 double distMin = 1E99;
444 XYZVector theAxis = theBoost.Vect().Unit();
445 double theAngle = random->
flatShoot() * 2. * 3.14159265358979323;
451 XYZVector orthAxis = (zAxis.Cross(theBoost.Vect())).Unit();
452 double orthAngle = acos(theBoost.Vect().Unit().Z());
459 for (
unsigned iTrack=firstTrack; iTrack<=lastTrack; ++iTrack ) {
461 unsigned idaugh = iTrack - firstTrack;
474 + aParticle.
py*aParticle.
py
475 + aParticle.
pz*aParticle.
pz
476 + aParticle.
mass*aParticle.
mass/(ecm*ecm) );
479 aDaughter.SetXYZT(aParticle.
px*ecm,aParticle.
py*ecm,
480 aParticle.
pz*ecm,energy*ecm);
484 aDaughter.
rotate(orthRotation);
487 aDaughter.
rotate(axisRotation);
490 aDaughter.
boost(axisBoost);
495 if ( distance < distMin && distance <
theDistCut ) {
515 ++aCurrentInteraction[ene];
518 }
else if ( pHadron < 4. &&
519 elastic > 1.- (inelastic4*theInelasticLength)
520 /theTotalInteractionLength ) {
539 double distance = 2E99;
542 if ( fabs(Particle.
charge()) > 1E-12 ) {
545 double chargeDiff = fabs(aDaughter.
charge()-Particle.
charge());
546 if ( fabs(chargeDiff) < 1E-12 ) {
553 distance = (aDaughter.Vect().Unit().Cross(Particle.Vect().Unit())).R();
558 distance = (aDaughter.Vect().Cross(Particle.Vect())).R()
559 /aDaughter.Vect().Mag2();
593 std::vector<unsigned> theCurrentEntries;
594 theCurrentEntries.resize(size1);
595 size1*=
sizeof(unsigned);
600 std::vector<unsigned> theCurrentInteractions;
601 theCurrentInteractions.resize(size2);
602 size2 *=
sizeof(unsigned);
605 std::vector< std::vector<unsigned> >::const_iterator aCurrentEntry =
theCurrentEntry.begin();
606 std::vector< std::vector<unsigned> >::const_iterator lastCurrentEntry =
theCurrentEntry.end();
607 unsigned allEntries=0;
608 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
609 unsigned size = aCurrentEntry->size();
610 for (
unsigned iene=0; iene<
size; ++iene )
611 theCurrentEntries[allEntries++] = (*aCurrentEntry)[iene];
615 std::vector< std::vector<unsigned> >::const_iterator aCurrentInteraction =
theCurrentInteraction.begin();
616 std::vector< std::vector<unsigned> >::const_iterator lastCurrentInteraction =
theCurrentInteraction.end();
617 unsigned allInteractions=0;
618 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
619 unsigned size = aCurrentInteraction->size();
620 for (
unsigned iene=0; iene<
size; ++iene )
621 theCurrentInteractions[allInteractions++] = (*aCurrentInteraction)[iene];
624 myOutputFile.write((
const char*)(&theCurrentEntries.front()), size1);
625 myOutputFile.write((
const char*)(&theCurrentInteractions.front()), size2);
633 std::ifstream myInputFile;
639 std::vector<unsigned> theCurrentEntries;
640 theCurrentEntries.resize(size1);
641 size1*=
sizeof(unsigned);
646 std::vector<unsigned> theCurrentInteractions;
647 theCurrentInteractions.resize(size2);
648 size2 *=
sizeof(unsigned);
654 myInputFile.open (inputFile.c_str());
655 if ( myInputFile.is_open() ) {
658 if ( stat(inputFile.c_str(), &
results) == 0 ) size = results.st_size;
662 myInputFile.seekg(size-size1-size2);
663 myInputFile.read((
char*)(&theCurrentEntries.front()),size1);
664 myInputFile.read((
char*)(&theCurrentInteractions.front()),size2);
668 std::vector< std::vector<unsigned> >::iterator aCurrentEntry =
theCurrentEntry.begin();
669 std::vector< std::vector<unsigned> >::iterator lastCurrentEntry =
theCurrentEntry.end();
670 unsigned allEntries=0;
671 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
672 unsigned size = aCurrentEntry->size();
673 for (
unsigned iene=0; iene<
size; ++iene )
674 (*aCurrentEntry)[iene] = theCurrentEntries[allEntries++];
680 unsigned allInteractions=0;
681 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
682 unsigned size = aCurrentInteraction->size();
683 for (
unsigned iene=0; iene<
size; ++iene )
684 (*aCurrentInteraction)[iene] = theCurrentInteractions[allInteractions++];
698 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)
std::vector< std::vector< TFile * > > theFiles
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
static std::string const input
std::vector< std::vector< std::string > > theFileNames
void compute(ParticlePropagator &Particle, RandomEngineAndDistribution const *)
Generate a nuclear interaction according to the probability that it happens.
math::XYZVector XYZVector
std::vector< std::vector< unsigned > > theNumberOfEntries
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(std::vector< double > &pionEnergies, std::vector< int > &pionTypes, std::vector< std::string > &pionNames, std::vector< double > &pionMasses, std::vector< double > &pionPMin, 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::vector< std::vector< TBranch * > > theBranches
std::vector< std::vector< unsigned > > theCurrentInteraction
std::vector< std::vector< double > > theRatios
~NuclearInteractionSimulator()
Default Destructor.
std::ofstream myOutputFile
std::vector< double > theLengthRatio
std::vector< RawParticle > _theUpdatedState
ROOT::Math::AxisAngle Rotation
std::vector< double > thePionEN
std::vector< std::vector< NUEvent * > > theNUEvents
std::string fullPath() const
int theClosestChargedDaughterId
tuple size
Write out results.
Power< A, B >::type pow(const A &a, const B &b)
std::vector< double > thePionPMin
void save()
Save current nuclear interaction (for later use)
double distanceToPrimary(const RawParticle &Particle, const RawParticle &aDaughter) const
Compute distance between secondary and primary.
math::XYZTLorentzVector XYZTLorentzVector