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,
36 thePionEN(pionEnergies),
39 thePionMA(pionMasses),
40 thePionPMin(pionPMin),
41 thePionEnergy(pionEnergy),
42 theLengthRatio(lengthRatio),
45 theDistAlgo(distAlgo),
56 std::vector<TFile*> aVFile(
thePionEN.size(),
static_cast<TFile*
>(0));
57 std::vector<TTree*> aVTree(
thePionEN.size(),
static_cast<TTree*
>(0));
58 std::vector<TBranch*> aVBranch(
thePionEN.size(),
static_cast<TBranch*
>(0));
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));
64 std::vector<std::string> aVFileName(
thePionEN.size(),
static_cast<std::string
>(
""));
65 std::vector<double> aVPionCM(
thePionEN.size(),
static_cast<double>(0));
76 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."
104 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
105 for (
unsigned iene=0; iene<
thePionEN.size(); ++iene ) {
109 filename <<
"NuclearInteractionsVal_" <<
thePionNA[iname] <<
"_E"<< theEne <<
".root";
116 theFiles[iname][iene] = TFile::Open(fullPath.c_str());
118 <<
"File " <<
theFileNames[iname][iene] <<
" " << fullPath <<
" not found ";
121 theTrees[iname][iene] = (TTree*)
theFiles[iname][iene]->Get(
"NuclearInteractions");
123 <<
"Tree with name NuclearInteractions not found in " <<
theFileNames[iname][iene];
128 <<
"Branch with name nuEvent not found in " <<
theFileNames[iname][iene];
141 unsigned NInteractions =
theNUEvents[iname][iene]->nInteractions();
164 thePionCM[iname][iene] = (Reference+Proton).M();
199 for (
unsigned iene=0; iene<
theFiles[
ifile].size(); ++iene ) {
220 double pHadron =
std::sqrt(Particle.Vect().Mag2());
227 std::map<int,int>::const_iterator thePit =
theIDMap.find(Particle.
pid());
229 int thePid = thePit !=
theIDMap.end() ? thePit->second : Particle.
pid();
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 ) {
275 Particle.
rotate(rotation1);
276 Particle.
rotate(rotation2);
285 Particle.Pz(), Particle.E());
298 const std::vector<double>& aPionCM =
thePionCM[thePidIndex];
299 const std::vector<double>& aRatios =
theRatios[thePidIndex];
311 double ecm = (Proton+Hadron).M();
320 double ecm1= (Proton+Hadron0).M();
324 double ecm2=aPionCM[0];
326 double ratio2=aRatios[0];
327 if ( ecm > aPionCM[0] && ecm < aPionCM [ aPionCM.size()-1 ] ) {
328 for (
unsigned ene=1;
329 ene < aPionCM.size() && ecm > aPionCM[ene-1];
331 if ( ecm<aPionCM[ene] ) {
334 ecm1 = aPionCM[ene1];
335 ecm2 = aPionCM[ene2];
336 ratio1 = aRatios[ene1];
337 ratio2 = aRatios[ene2];
340 }
else if ( ecm > aPionCM[ aPionCM.size()-1 ] ) {
341 ene1 = aPionCM.size()-1;
342 ene2 = aPionCM.size()-2;
343 ecm1 = aPionCM[ene1];
344 ecm2 = aPionCM[ene2];
345 ratio1 = aRatios[ene2];
346 ratio2 = aRatios[ene2];
351 double slope = (std::log10(ecm )-std::log10(ecm1))
352 / (std::log10(ecm2)-std::log10(ecm1));
353 double inelastic = ratio1 + (ratio2-ratio1) * slope;
354 double inelastic4 = pHadron < 4. ? aRatios[
ien4] : 1.;
361 if ( elastic > 1.- (inelastic*theInelasticLength)
362 /theTotalInteractionLength ) {
367 std::vector<NUEvent*>& aNUEvents =
theNUEvents[thePidIndex];
391 theBoost /= theBoost.e();
402 if ( aCurrentInteraction[ene] == aNumberOfInteractions[ene] ) {
406 std::vector<TTree*>& aTrees =
theTrees[thePidIndex];
407 ++aCurrentEntry[ene];
410 aCurrentInteraction[ene] = 0;
411 if ( aCurrentEntry[ene] == aNumberOfEntries[ene] ) {
412 aCurrentEntry[ene] = 0;
415 unsigned myEntry = aCurrentEntry[ene];
418 aTrees[ene]->GetEntry(myEntry);
420 aNumberOfInteractions[ene] = aNUEvents[ene]->nInteractions();
426 = aNUEvents[ene]->theNUInteractions()[aCurrentInteraction[ene]];
428 unsigned firstTrack = anInteraction.
first;
429 unsigned lastTrack = anInteraction.
last;
434 double distMin = 1E99;
437 XYZVector theAxis = theBoost.Vect().Unit();
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) );
472 aDaughter.SetXYZT(aParticle.
px*ecm,aParticle.
py*ecm,
473 aParticle.
pz*ecm,energy*ecm);
477 aDaughter.
rotate(orthRotation);
480 aDaughter.
rotate(axisRotation);
483 aDaughter.
boost(axisBoost);
488 if ( distance < distMin && distance <
theDistCut ) {
508 ++aCurrentInteraction[ene];
511 }
else if ( pHadron < 4. &&
512 elastic > 1.- (inelastic4*theInelasticLength)
513 /theTotalInteractionLength ) {
532 double distance = 2E99;
535 if ( fabs(Particle.
charge()) > 1E-12 ) {
538 double chargeDiff = fabs(aDaughter.
charge()-Particle.
charge());
539 if ( fabs(chargeDiff) < 1E-12 ) {
546 distance = (aDaughter.Vect().Unit().Cross(Particle.Vect().Unit())).R();
551 distance = (aDaughter.Vect().Cross(Particle.Vect())).R()
552 /aDaughter.Vect().Mag2();
586 std::vector<unsigned> theCurrentEntries;
587 theCurrentEntries.resize(size1);
588 size1*=
sizeof(unsigned);
593 std::vector<unsigned> theCurrentInteractions;
594 theCurrentInteractions.resize(size2);
595 size2 *=
sizeof(unsigned);
598 std::vector< std::vector<unsigned> >::const_iterator aCurrentEntry =
theCurrentEntry.begin();
599 std::vector< std::vector<unsigned> >::const_iterator lastCurrentEntry =
theCurrentEntry.end();
600 unsigned allEntries=0;
601 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
602 unsigned size = aCurrentEntry->size();
603 for (
unsigned iene=0; iene<
size; ++iene )
604 theCurrentEntries[allEntries++] = (*aCurrentEntry)[iene];
608 std::vector< std::vector<unsigned> >::const_iterator aCurrentInteraction =
theCurrentInteraction.begin();
609 std::vector< std::vector<unsigned> >::const_iterator lastCurrentInteraction =
theCurrentInteraction.end();
610 unsigned allInteractions=0;
611 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
612 unsigned size = aCurrentInteraction->size();
613 for (
unsigned iene=0; iene<
size; ++iene )
614 theCurrentInteractions[allInteractions++] = (*aCurrentInteraction)[iene];
617 myOutputFile.write((
const char*)(&theCurrentEntries.front()), size1);
618 myOutputFile.write((
const char*)(&theCurrentInteractions.front()), size2);
626 ifstream myInputFile;
632 std::vector<unsigned> theCurrentEntries;
633 theCurrentEntries.resize(size1);
634 size1*=
sizeof(unsigned);
639 std::vector<unsigned> theCurrentInteractions;
640 theCurrentInteractions.resize(size2);
641 size2 *=
sizeof(unsigned);
647 myInputFile.open (inputFile.c_str());
648 if ( myInputFile.is_open() ) {
651 if ( stat(inputFile.c_str(), &
results) == 0 ) size = results.st_size;
655 myInputFile.seekg(size-size1-size2);
656 myInputFile.read((
char*)(&theCurrentEntries.front()),size1);
657 myInputFile.read((
char*)(&theCurrentInteractions.front()),size2);
661 std::vector< std::vector<unsigned> >::iterator aCurrentEntry =
theCurrentEntry.begin();
662 std::vector< std::vector<unsigned> >::iterator lastCurrentEntry =
theCurrentEntry.end();
663 unsigned allEntries=0;
664 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
665 unsigned size = aCurrentEntry->size();
666 for (
unsigned iene=0; iene<
size; ++iene )
667 (*aCurrentEntry)[iene] = theCurrentEntries[allEntries++];
673 unsigned allInteractions=0;
674 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
675 unsigned size = aCurrentInteraction->size();
676 for (
unsigned iene=0; iene<
size; ++iene )
677 (*aCurrentInteraction)[iene] = theCurrentInteractions[allInteractions++];
691 while ( thePid !=
thePionID[myIndex] ) ++myIndex;
void compute(ParticlePropagator &Particle)
Generate a nuclear interaction according to the probability that it happens.
std::map< int, int > theIDMap
void boost(double bx, double by, double bz)
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
std::vector< std::vector< std::string > > theFileNames
math::XYZVector XYZVector
std::vector< std::vector< unsigned > > theNumberOfEntries
std::vector< std::vector< double > > thePionCM
const RandomEngine * random
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
std::vector< std::vector< TBranch * > > theBranches
std::vector< std::vector< unsigned > > theCurrentInteraction
std::vector< std::vector< double > > theRatios
~NuclearInteractionSimulator()
Default Destructor.
double flatShoot(double xmin=0.0, double xmax=1.0) const
std::ofstream myOutputFile
std::vector< double > theLengthRatio
std::vector< RawParticle > _theUpdatedState
ROOT::Math::AxisAngle Rotation
std::vector< double > thePionEN
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, const RandomEngine *engine)
Constructor.
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