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 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));
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];
138 unsigned NInteractions =
theNUEvents[iname][iene]->nInteractions();
151 thePionCM[iname][iene] = (Reference+Proton).M();
185 for (
unsigned iene=0; iene<
theFiles[
ifile].size(); ++iene ) {
202 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
203 for (
unsigned iene=0; iene<
thePionEN.size(); ++iene ) {
207 unsigned NInteractions =
theNUEvents[iname][iene]->nInteractions();
217 double pHadron =
std::sqrt(Particle.Vect().Mag2());
224 std::map<int,int>::const_iterator thePit =
theIDMap.find(Particle.
pid());
226 int thePid = thePit !=
theIDMap.end() ? thePit->second : Particle.
pid();
229 unsigned fPid =
abs(thePid);
230 if ( fPid != 211 && fPid != 130 && fPid != 321 && fPid != 2112 && fPid != 2212 ) {
237 unsigned thePidIndex =
index(thePid);
242 double ee = pHadron > 0.6 ?
244 double theElasticLength = ( 0.8753 * ee + 0.15 )
247 * theInelasticLength;
250 double theTotalInteractionLength = theInelasticLength + theElasticLength;
254 if ( aNuclInteraction < theTotalInteractionLength ) {
258 if ( elastic < theElasticLength/theTotalInteractionLength ) {
267 double phi = 2. * 3.14159265358979323 * random->
flatShoot();
272 Particle.
rotate(rotation1);
273 Particle.
rotate(rotation2);
282 Particle.Pz(), Particle.E());
295 const std::vector<double>& aPionCM =
thePionCM[thePidIndex];
296 const std::vector<double>& aRatios =
theRatios[thePidIndex];
308 double ecm = (Proton+Hadron).M();
317 double ecm1= (Proton+Hadron0).M();
321 double ecm2=aPionCM[0];
323 double ratio2=aRatios[0];
324 if ( ecm > aPionCM[0] && ecm < aPionCM [ aPionCM.size()-1 ] ) {
325 for (
unsigned ene=1;
326 ene < aPionCM.size() && ecm > aPionCM[ene-1];
328 if ( ecm<aPionCM[ene] ) {
331 ecm1 = aPionCM[ene1];
332 ecm2 = aPionCM[ene2];
333 ratio1 = aRatios[ene1];
334 ratio2 = aRatios[ene2];
337 }
else if ( ecm > aPionCM[ aPionCM.size()-1 ] ) {
338 ene1 = aPionCM.size()-1;
339 ene2 = aPionCM.size()-2;
340 ecm1 = aPionCM[ene1];
341 ecm2 = aPionCM[ene2];
342 ratio1 = aRatios[ene2];
343 ratio2 = aRatios[ene2];
348 double slope = (std::log10(ecm )-std::log10(ecm1))
349 / (std::log10(ecm2)-std::log10(ecm1));
350 double inelastic = ratio1 + (ratio2-ratio1) * slope;
351 double inelastic4 = pHadron < 4. ? aRatios[
ien4] : 1.;
358 if ( elastic > 1.- (inelastic*theInelasticLength)
359 /theTotalInteractionLength ) {
364 std::vector<NUEvent*>& aNUEvents =
theNUEvents[thePidIndex];
373 if ( random->
flatShoot() < slope || aNumberOfInteractions[ene1] == 0 )
388 theBoost /= theBoost.e();
399 if ( aCurrentInteraction[ene] == aNumberOfInteractions[ene] ) {
403 std::vector<TTree*>& aTrees =
theTrees[thePidIndex];
404 ++aCurrentEntry[ene];
407 aCurrentInteraction[ene] = 0;
408 if ( aCurrentEntry[ene] == aNumberOfEntries[ene] ) {
409 aCurrentEntry[ene] = 0;
412 unsigned myEntry = aCurrentEntry[ene];
415 aTrees[ene]->GetEntry(myEntry);
417 aNumberOfInteractions[ene] = aNUEvents[ene]->nInteractions();
423 = aNUEvents[ene]->theNUInteractions()[aCurrentInteraction[ene]];
425 unsigned firstTrack = anInteraction.
first;
426 unsigned lastTrack = anInteraction.
last;
431 double distMin = 1E99;
434 XYZVector theAxis = theBoost.Vect().Unit();
435 double theAngle = random->
flatShoot() * 2. * 3.14159265358979323;
441 XYZVector orthAxis = (zAxis.Cross(theBoost.Vect())).Unit();
442 double orthAngle = acos(theBoost.Vect().Unit().Z());
449 for (
unsigned iTrack=firstTrack; iTrack<=lastTrack; ++iTrack ) {
451 unsigned idaugh = iTrack - firstTrack;
464 + aParticle.
py*aParticle.
py
465 + aParticle.
pz*aParticle.
pz
466 + aParticle.
mass*aParticle.
mass/(ecm*ecm) );
469 aDaughter.SetXYZT(aParticle.
px*ecm,aParticle.
py*ecm,
470 aParticle.
pz*ecm,energy*ecm);
474 aDaughter.
rotate(orthRotation);
477 aDaughter.
rotate(axisRotation);
480 aDaughter.
boost(axisBoost);
485 if ( distance < distMin && distance <
theDistCut ) {
509 ++aCurrentInteraction[ene];
512 }
else if ( pHadron < 4. &&
513 elastic > 1.- (inelastic4*theInelasticLength)
514 /theTotalInteractionLength ) {
536 if ( fabs(Particle.
charge()) > 1E-12 ) {
539 double chargeDiff = fabs(aDaughter.
charge()-Particle.
charge());
540 if ( fabs(chargeDiff) < 1E-12 ) {
547 distance = (aDaughter.Vect().Unit().Cross(Particle.Vect().Unit())).R();
552 distance = (aDaughter.Vect().Cross(Particle.Vect())).R()
553 /aDaughter.Vect().Mag2();
587 std::vector<unsigned> theCurrentEntries;
588 theCurrentEntries.resize(size1);
589 size1*=
sizeof(unsigned);
594 std::vector<unsigned> theCurrentInteractions;
595 theCurrentInteractions.resize(size2);
596 size2 *=
sizeof(unsigned);
599 std::vector< std::vector<unsigned> >::const_iterator aCurrentEntry =
theCurrentEntry.begin();
600 std::vector< std::vector<unsigned> >::const_iterator lastCurrentEntry =
theCurrentEntry.end();
601 unsigned allEntries=0;
602 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
603 unsigned size = aCurrentEntry->size();
604 for (
unsigned iene=0; iene<
size; ++iene )
605 theCurrentEntries[allEntries++] = (*aCurrentEntry)[iene];
609 std::vector< std::vector<unsigned> >::const_iterator aCurrentInteraction =
theCurrentInteraction.begin();
610 std::vector< std::vector<unsigned> >::const_iterator lastCurrentInteraction =
theCurrentInteraction.end();
611 unsigned allInteractions=0;
612 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
613 unsigned size = aCurrentInteraction->size();
614 for (
unsigned iene=0; iene<
size; ++iene )
615 theCurrentInteractions[allInteractions++] = (*aCurrentInteraction)[iene];
618 myOutputFile.write((
const char*)(&theCurrentEntries.front()), size1);
619 myOutputFile.write((
const char*)(&theCurrentInteractions.front()), size2);
627 std::ifstream myInputFile;
633 std::vector<unsigned> theCurrentEntries;
634 theCurrentEntries.resize(size1);
635 size1*=
sizeof(unsigned);
640 std::vector<unsigned> theCurrentInteractions;
641 theCurrentInteractions.resize(size2);
642 size2 *=
sizeof(unsigned);
648 myInputFile.open (inputFile.c_str());
649 if ( myInputFile.is_open() ) {
652 if ( stat(inputFile.c_str(), &
results) == 0 ) size = results.st_size;
656 myInputFile.seekg(size-size1-size2);
657 myInputFile.read((
char*)(&theCurrentEntries.front()),size1);
658 myInputFile.read((
char*)(&theCurrentInteractions.front()),size2);
662 std::vector< std::vector<unsigned> >::iterator aCurrentEntry =
theCurrentEntry.begin();
663 std::vector< std::vector<unsigned> >::iterator lastCurrentEntry =
theCurrentEntry.end();
664 unsigned allEntries=0;
665 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
666 unsigned size = aCurrentEntry->size();
667 for (
unsigned iene=0; iene<
size; ++iene )
668 (*aCurrentEntry)[iene] = theCurrentEntries[allEntries++];
674 unsigned allInteractions=0;
675 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
676 unsigned size = aCurrentInteraction->size();
677 for (
unsigned iene=0; iene<
size; ++iene )
678 (*aCurrentInteraction)[iene] = theCurrentInteractions[allInteractions++];
692 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
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
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
std::vector< std::vector< TBranch * > > theBranches
std::vector< std::vector< unsigned > > theCurrentInteraction
std::vector< std::vector< double > > theRatios
~NuclearInteractionSimulator()
Default Destructor.
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::vector< std::vector< NUEvent * > > theNUEvents
volatile std::atomic< bool > shutdown_flag false
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