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),
46 currentValuesWereSet(
false)
52 std::vector<TFile*> aVFile(
thePionEN.size(),
static_cast<TFile*
>(0));
53 std::vector<TTree*> aVTree(
thePionEN.size(),
static_cast<TTree*
>(0));
54 std::vector<TBranch*> aVBranch(
thePionEN.size(),
static_cast<TBranch*
>(0));
56 std::vector<unsigned> aVCurrentEntry(
thePionEN.size(),
static_cast<unsigned>(0));
57 std::vector<unsigned> aVCurrentInteraction(
thePionEN.size(),
static_cast<unsigned>(0));
58 std::vector<unsigned> aVNumberOfEntries(
thePionEN.size(),
static_cast<unsigned>(0));
59 std::vector<unsigned> aVNumberOfInteractions(
thePionEN.size(),
static_cast<unsigned>(0));
61 std::vector<double> aVPionCM(
thePionEN.size(),
static_cast<double>(0));
72 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
88 std::cout <<
"***WARNING*** You are reading nuclear-interaction information from the file "
89 << inputFile <<
" created in an earlier run."
100 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
101 for (
unsigned iene=0; iene<
thePionEN.size(); ++iene ) {
105 filename <<
"NuclearInteractionsVal_" <<
thePionNA[iname] <<
"_E"<< theEne <<
".root";
112 theFiles[iname][iene] = TFile::Open(fullPath.c_str());
114 <<
"File " <<
theFileNames[iname][iene] <<
" " << fullPath <<
" not found ";
117 theTrees[iname][iene] = (TTree*)
theFiles[iname][iene]->Get(
"NuclearInteractions");
119 <<
"Tree with name NuclearInteractions not found in " <<
theFileNames[iname][iene];
124 <<
"Branch with name nuEvent not found in " <<
theFileNames[iname][iene];
134 unsigned NInteractions =
theNUEvents[iname][iene]->nInteractions();
147 thePionCM[iname][iene] = (Reference+Proton).M();
181 for (
unsigned iene=0; iene<
theFiles[
ifile].size(); ++iene ) {
198 for (
unsigned iname=0; iname<
thePionNA.size(); ++iname ) {
199 for (
unsigned iene=0; iene<
thePionEN.size(); ++iene ) {
203 unsigned NInteractions =
theNUEvents[iname][iene]->nInteractions();
213 double pHadron =
std::sqrt(Particle.Vect().Mag2());
220 std::map<int,int>::const_iterator thePit =
theIDMap.find(Particle.
pid());
222 int thePid = thePit !=
theIDMap.end() ? thePit->second : Particle.
pid();
225 unsigned fPid =
abs(thePid);
226 if ( fPid != 211 && fPid != 130 && fPid != 321 && fPid != 2112 && fPid != 2212 ) {
233 unsigned thePidIndex =
index(thePid);
238 double ee = pHadron > 0.6 ?
240 double theElasticLength = ( 0.8753 * ee + 0.15 )
243 * theInelasticLength;
246 double theTotalInteractionLength = theInelasticLength + theElasticLength;
250 if ( aNuclInteraction < theTotalInteractionLength ) {
254 if ( elastic < theElasticLength/theTotalInteractionLength ) {
263 double phi = 2. * 3.14159265358979323 * random->
flatShoot();
268 Particle.
rotate(rotation1);
269 Particle.
rotate(rotation2);
278 Particle.Pz(), Particle.E());
291 const std::vector<double>& aPionCM =
thePionCM[thePidIndex];
292 const std::vector<double>& aRatios =
theRatios[thePidIndex];
304 double ecm = (Proton+Hadron).M();
313 double ecm1= (Proton+Hadron0).M();
317 double ecm2=aPionCM[0];
319 double ratio2=aRatios[0];
320 if ( ecm > aPionCM[0] && ecm < aPionCM [ aPionCM.size()-1 ] ) {
321 for (
unsigned ene=1;
322 ene < aPionCM.size() && ecm > aPionCM[ene-1];
324 if ( ecm<aPionCM[ene] ) {
327 ecm1 = aPionCM[ene1];
328 ecm2 = aPionCM[ene2];
329 ratio1 = aRatios[ene1];
330 ratio2 = aRatios[ene2];
333 }
else if ( ecm > aPionCM[ aPionCM.size()-1 ] ) {
334 ene1 = aPionCM.size()-1;
335 ene2 = aPionCM.size()-2;
336 ecm1 = aPionCM[ene1];
337 ecm2 = aPionCM[ene2];
338 ratio1 = aRatios[ene2];
339 ratio2 = aRatios[ene2];
344 double slope = (std::log10(ecm )-std::log10(ecm1))
345 / (std::log10(ecm2)-std::log10(ecm1));
346 double inelastic = ratio1 + (ratio2-ratio1) * slope;
347 double inelastic4 = pHadron < 4. ? aRatios[
ien4] : 1.;
354 if ( elastic > 1.- (inelastic*theInelasticLength)
355 /theTotalInteractionLength ) {
360 std::vector<NUEvent*>& aNUEvents =
theNUEvents[thePidIndex];
369 if ( random->
flatShoot() < slope || aNumberOfInteractions[ene1] == 0 )
384 theBoost /= theBoost.e();
395 if ( aCurrentInteraction[ene] == aNumberOfInteractions[ene] ) {
399 std::vector<TTree*>& aTrees =
theTrees[thePidIndex];
400 ++aCurrentEntry[ene];
403 aCurrentInteraction[ene] = 0;
404 if ( aCurrentEntry[ene] == aNumberOfEntries[ene] ) {
405 aCurrentEntry[ene] = 0;
408 unsigned myEntry = aCurrentEntry[ene];
411 aTrees[ene]->GetEntry(myEntry);
413 aNumberOfInteractions[ene] = aNUEvents[ene]->nInteractions();
419 = aNUEvents[ene]->theNUInteractions()[aCurrentInteraction[ene]];
421 unsigned firstTrack = anInteraction.
first;
422 unsigned lastTrack = anInteraction.
last;
427 double distMin = 1E99;
430 XYZVector theAxis = theBoost.Vect().Unit();
431 double theAngle = random->
flatShoot() * 2. * 3.14159265358979323;
437 XYZVector orthAxis = (zAxis.Cross(theBoost.Vect())).Unit();
438 double orthAngle = acos(theBoost.Vect().Unit().Z());
445 for (
unsigned iTrack=firstTrack; iTrack<=lastTrack; ++iTrack ) {
447 unsigned idaugh = iTrack - firstTrack;
460 + aParticle.
py*aParticle.
py
461 + aParticle.
pz*aParticle.
pz
462 + aParticle.
mass*aParticle.
mass/(ecm*ecm) );
465 aDaughter.SetXYZT(aParticle.
px*ecm,aParticle.
py*ecm,
466 aParticle.
pz*ecm,energy*ecm);
470 aDaughter.
rotate(orthRotation);
473 aDaughter.
rotate(axisRotation);
476 aDaughter.
boost(axisBoost);
481 if ( distance < distMin && distance <
theDistCut ) {
505 ++aCurrentInteraction[ene];
508 }
else if ( pHadron < 4. &&
509 elastic > 1.- (inelastic4*theInelasticLength)
510 /theTotalInteractionLength ) {
529 double distance = 2E99;
532 if ( fabs(Particle.
charge()) > 1E-12 ) {
535 double chargeDiff = fabs(aDaughter.
charge()-Particle.
charge());
536 if ( fabs(chargeDiff) < 1E-12 ) {
543 distance = (aDaughter.Vect().Unit().Cross(Particle.Vect().Unit())).R();
548 distance = (aDaughter.Vect().Cross(Particle.Vect())).R()
549 /aDaughter.Vect().Mag2();
583 std::vector<unsigned> theCurrentEntries;
584 theCurrentEntries.resize(size1);
585 size1*=
sizeof(unsigned);
590 std::vector<unsigned> theCurrentInteractions;
591 theCurrentInteractions.resize(size2);
592 size2 *=
sizeof(unsigned);
595 std::vector< std::vector<unsigned> >::const_iterator aCurrentEntry =
theCurrentEntry.begin();
596 std::vector< std::vector<unsigned> >::const_iterator lastCurrentEntry =
theCurrentEntry.end();
597 unsigned allEntries=0;
598 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
599 unsigned size = aCurrentEntry->size();
600 for (
unsigned iene=0; iene<
size; ++iene )
601 theCurrentEntries[allEntries++] = (*aCurrentEntry)[iene];
605 std::vector< std::vector<unsigned> >::const_iterator aCurrentInteraction =
theCurrentInteraction.begin();
606 std::vector< std::vector<unsigned> >::const_iterator lastCurrentInteraction =
theCurrentInteraction.end();
607 unsigned allInteractions=0;
608 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
609 unsigned size = aCurrentInteraction->size();
610 for (
unsigned iene=0; iene<
size; ++iene )
611 theCurrentInteractions[allInteractions++] = (*aCurrentInteraction)[iene];
614 myOutputFile.write((
const char*)(&theCurrentEntries.front()), size1);
615 myOutputFile.write((
const char*)(&theCurrentInteractions.front()), size2);
623 std::ifstream myInputFile;
629 std::vector<unsigned> theCurrentEntries;
630 theCurrentEntries.resize(size1);
631 size1*=
sizeof(unsigned);
636 std::vector<unsigned> theCurrentInteractions;
637 theCurrentInteractions.resize(size2);
638 size2 *=
sizeof(unsigned);
644 myInputFile.open (inputFile.c_str());
645 if ( myInputFile.is_open() ) {
648 if ( stat(inputFile.c_str(), &
results) == 0 ) size = results.st_size;
652 myInputFile.seekg(size-size1-size2);
653 myInputFile.read((
char*)(&theCurrentEntries.front()),size1);
654 myInputFile.read((
char*)(&theCurrentInteractions.front()),size2);
658 std::vector< std::vector<unsigned> >::iterator aCurrentEntry =
theCurrentEntry.begin();
659 std::vector< std::vector<unsigned> >::iterator lastCurrentEntry =
theCurrentEntry.end();
660 unsigned allEntries=0;
661 for ( ; aCurrentEntry!=lastCurrentEntry; ++aCurrentEntry ) {
662 unsigned size = aCurrentEntry->size();
663 for (
unsigned iene=0; iene<
size; ++iene )
664 (*aCurrentEntry)[iene] = theCurrentEntries[allEntries++];
670 unsigned allInteractions=0;
671 for ( ; aCurrentInteraction!=lastCurrentInteraction; ++aCurrentInteraction ) {
672 unsigned size = aCurrentInteraction->size();
673 for (
unsigned iene=0; iene<
size; ++iene )
674 (*aCurrentInteraction)[iene] = theCurrentInteractions[allInteractions++];
688 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
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
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