27#include "CLHEP/Random/RandFlat.h"
43 : base_class(
type,name,parent)
49 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] ==>" << name() <<
"::initialize()");
54 ATH_MSG_WARNING(
"[PunchThroughG4Tool] unable to open or read the inverse CDF config");
55 return StatusCode::FAILURE;
61 ATH_MSG_WARNING(
"[PunchThroughG4Tool] unable to open or read the inverse PCA config");
62 return StatusCode::FAILURE;
68 ATH_MSG_WARNING(
"[PunchThroughG4Tool] size of infoMap for PCA and CDF differs! Something is wrong with input xml files.");
69 return StatusCode::FAILURE;
72 ATH_MSG_INFO(
"[PunchThroughG4Tool] initialization is successful" );
73 return StatusCode::SUCCESS;
77 G4ParticleTable *ptable = G4ParticleTable::GetParticleTable();
97 ATH_MSG_VERBOSE(
"VERBOSE: [PunchThroughG4Tool] registering punch-through particle type with pdg = " << pdg );
98 if (
registerPunchThroughParticle( *ptable, pdg, doAnti, minEnergy, maxNum, numFactor, energyFactor, posAngleFactor, momAngleFactor ) != StatusCode::SUCCESS)
100 ATH_MSG_ERROR(
"[PunchThroughG4Tool] unable to register punch-through particle type with pdg = " << pdg);
101 return StatusCode::FAILURE;
105 return StatusCode::SUCCESS;
111 ATH_MSG_INFO(
"[PunchThroughG4Tool] PunchThroughG4Tool::initializePhysics() called");
113 if (resolvedFileName.empty()) {
115 return StatusCode::FAILURE;
117 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] Parametrisation file found: " << resolvedFileName );
121 ATH_MSG_WARNING(
"[PunchThroughG4Tool] unable to open the lookup-table for the punch-through simulation (file does not exist)");
122 return StatusCode::FAILURE;
126 ATH_MSG_WARNING(
"[PunchThroughG4Tool] unable to open the lookup-table for the punch-through simulation (wrong or empty file?)");
127 return StatusCode::FAILURE;
145 ATH_MSG_FATAL (
"[PunchThroughG4Tool] Could not retrieve GeometryIdentifier Service. Abort");
146 return StatusCode::FAILURE;
153 return StatusCode::FAILURE;
159 const std::vector<std::pair<double, double>>* rzMS = &(
m_envDefSvc->getMuonRZBoundary());
160 const std::vector<std::pair<double, double>>* rzCalo = &(
m_envDefSvc->getCaloRZBoundary());
162 return StatusCode::SUCCESS;
175 ATH_MSG_WARNING(
"[PunchThroughG4Tool] more punch-through particle correlations are given, than punch-through particle types are registered (skipping the last ones)");
180 for (
unsigned int num = 0; num < numCorrelations; num++ )
188 if ( ! pdg2)
continue;
190 if (
registerCorrelation(pdg1, pdg2, minCorrEnergy, fullCorrEnergy) != StatusCode::SUCCESS )
192 ATH_MSG_ERROR(
"[PunchThroughG4Tool] unable to register punch-through particle correlation for pdg1=" << pdg1 <<
" pdg2=" << pdg2 );
193 return StatusCode::FAILURE;
197 return StatusCode::SUCCESS;
203 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] getCaloMSVars()==> m_R1 = "<< caloMSVarsVect[0] <<
", m_R2 = " << caloMSVarsVect[1] <<
", m_z1 = " << caloMSVarsVect[2] <<
", m_z2 = " << caloMSVarsVect[3]);
205 return caloMSVarsVect;
209 const std::vector<std::pair<double, double>>* rzCalo)
212 found1=
false; found2=
false;
216 for (
const auto & [r_tempCalo , z_tempCalo] : *rzCalo )
221 for (
const auto & [r_tempMS , z_tempMS] : *rzMS )
224 if (r_tempCalo==r_tempMS && z_tempCalo==z_tempMS && found1==
false )
227 m_z1=std::fabs(z_tempMS);
232 else if (r_tempCalo==r_tempMS && z_tempCalo==z_tempMS && r_tempCalo!=
m_R1 && std::fabs(z_tempCalo)!=
m_z1)
235 m_z2=std::fabs(z_tempMS);
240 if (found1==
true && found2==
true)
break;
245 if (found1 ==
false){
246 ATH_MSG_ERROR (
"[PunchThroughG4Tool] first coordinate of calo-MS border not found");
247 return StatusCode::FAILURE;
249 if (found2 ==
false){
250 ATH_MSG_ERROR (
"[PunchThroughG4Tool] second coordinate of calo-MS border not found; first one is: R1 ="<<
m_R1<<
" z1 ="<<
m_z1);
251 return StatusCode::FAILURE;
260 return StatusCode::FAILURE;
265 return StatusCode::SUCCESS;
275 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] finalize() successful" );
277 return StatusCode::SUCCESS;
282 G4ParticleDefinition* secG4Particle = ptable.FindParticle(secondarySignedPDG);
283 double mass = secG4Particle->GetPDGMass();
284 ATH_MSG_DEBUG(
"[PunchThroughG4Tool::checkParticleTable] secondarySignedPDG = " << secondarySignedPDG <<
", mass = "<< mass);
289 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] starting punch-through simulation");
292 std::vector<std::map<std::string, double>> secKinematicsMapVect;
295 const G4Track * g4PrimaryTrack = fastTrack.GetPrimaryTrack();
298 const G4ParticleDefinition * mainG4Particle = g4PrimaryTrack->GetDefinition();
301 ATH_MSG_DEBUG(
"PunchThroughG4Tool::computePunchThroughParticles Debug:"
302 <<
" Incoming particle:"
303 <<
" E=" << g4PrimaryTrack->GetKineticEnergy()/CLHEP::GeV <<
" GeV"
304 <<
" theta=" << g4PrimaryTrack->GetMomentumDirection().theta()
305 <<
" phi=" << g4PrimaryTrack->GetMomentumDirection().phi()
306 <<
" PDG=" << (mainG4Particle ? mainG4Particle->GetPDGEncoding() : -999)
307 <<
" Name=" << (mainG4Particle ? mainG4Particle->GetParticleName() :
"UNKNOWN"));
310 int pdgID = mainG4Particle->GetPDGEncoding();
311 float mainPartMass = mainG4Particle->GetPDGMass();
312 float mainMomMag2 = g4PrimaryTrack->GetMomentum().mag2();
313 float mainPartEta = g4PrimaryTrack->GetPosition().eta();
315 const G4ThreeVector mainMomentumDir = g4PrimaryTrack->GetMomentumDirection();
316 const G4ThreeVector mainMomentum = g4PrimaryTrack->GetMomentum();
317 const G4ThreeVector mainPosition = g4PrimaryTrack->GetPosition();
320 const double initEnergy = std::sqrt( mainMomMag2 + mainPartMass*mainPartMass );
321 const double initEta = mainPartEta;
323 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] position of the input particle: r"<<mainPosition.perp()<<
" z= "<<mainPosition.z() );
327 const Amg::Vector3D amg3DPosVec(mainPosition.x(), mainPosition.y(), mainPosition.z());
328 const Amg::Vector3D amg3DMomDirVec(mainMomentumDir.x(), mainMomentumDir.y(), mainMomentumDir.z());
329 const Amg::Vector3D amg3DMomVec(mainMomentum.x(), mainMomentum.y(), mainMomentum.z());
334 ATH_MSG_DEBUG(
"[PunchThroughG4Tool](GeoIDSvc) input particle doesn't point to calorimeter"<<
"Next GeoID: "<< nextGeoID );
346 for ( ; pdgIt != pdgItEnd; ++pdgIt, ++minEnergyIt)
348 if (std::abs(pdgID) == *pdgIt){
349 if(initEnergy < *minEnergyIt){
350 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] particle does not meet initiator min energy requirement. Dropping it in the calo.");
359 if (pdgIt == pdgItEnd)
361 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] particle is not registered as punch-through initiator. Dropping it in the calo.");
368 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] particle does not meet initiator eta range requirement. Dropping it in the calo.");
373 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] punchThroughProbability output: " << punchThroughProbability <<
" RandFlat: " << punchThroughClassifierRand );
376 if( punchThroughClassifierRand > punchThroughProbability){
377 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] particle not classified to create punch through. Dropping it in the calo.");
395 std::map<int, int> corrPdgNumDone;
401 ATH_MSG_DEBUG(
"[DEBUG] Enter computePunchThroughParticles: starting while (retry) loop");
402 while(secKinematicsMapVect.empty() && nTries < maxTries) {
404 ATH_MSG_DEBUG(
"[DEBUG] Retry " << nTries <<
", secKinematicsMapVect.size=" << secKinematicsMapVect.size());
408 int doPdg = currentParticle.first;
410 int corrPdg = currentParticle.second->getCorrelatedPdg();
416 std::map<int,int>::iterator itPdgPos = corrPdgNumDone.find(doPdg);
419 if ( itPdgPos == corrPdgNumDone.end() ) itPdgPos = corrPdgNumDone.find(corrPdg);
423 if ( itPdgPos == corrPdgNumDone.end() )
426 if ( CLHEP::RandFlat::shoot(rndmEngine) > 0.5 ) doPdg = corrPdg;
429 corrPdgNumDone[doPdg] =
getAllParticles(*g4PrimaryTrack, secKinematicsMapVect, rndmEngine, doPdg, interpEnergy, interpEta);
438 const int donePdg = itPdgPos->first;
439 const int doneNumPart = itPdgPos->second;
441 if (donePdg == doPdg) doPdg = corrPdg;
444 getCorrelatedParticles(*g4PrimaryTrack, secKinematicsMapVect, doPdg, doneNumPart, rndmEngine, interpEnergy, interpEta);
454 getAllParticles(*g4PrimaryTrack, secKinematicsMapVect, rndmEngine, doPdg, interpEnergy, interpEta);
459 if (msgLvl(MSG::DEBUG) && !secKinematicsMapVect.empty()) {
460 for (
size_t i = 0; i < secKinematicsMapVect.size(); ++i) {
462 for (
const auto& kv : secKinematicsMapVect[i]) {
472 return secKinematicsMapVect;
475int PunchThroughG4Tool::getAllParticles(
const G4Track& g4PrimaryTrack, std::vector<std::map<std::string, double>> &secKinematicsMapVect, CLHEP::HepRandomEngine* rndmEngine,
int pdg,
double interpEnergy,
double interpEta,
int numParticles)
478 float initParticleTheta = g4PrimaryTrack.GetPosition().theta();
479 float initParticlePhi = g4PrimaryTrack.GetPosition().phi();
483 double minAllowedEnergy = p->getMinEnergy();
487 if ( numParticles < 0 )
490 std::vector<int> parameters;
491 parameters.push_back( std::round(interpEnergy) );
492 parameters.push_back( std::round(interpEta*100) );
495 int maxParticles = p->getMaxNumParticles();
501 numParticles = int( p->getNumParticlesPDF()->getRand(rndmEngine, parameters) );
504 numParticles = lround( numParticles *= p->getNumParticlesFactor() );
506 while ( (maxParticles >= 0.) && (numParticles > maxParticles) );
509 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] adding " << numParticles <<
" punch-through particles with pdg code: " << pdg);
512 for (
int numCreated = 0; numCreated < numParticles; numCreated++ )
515 std::map<std::string, double> secondaryKinematicsMap =
getOneParticleKinematics(rndmEngine, pdg, initParticleTheta, initParticlePhi, interpEnergy, interpEta);
518 double energy = secondaryKinematicsMap.at(
"energy");
519 if(energy > minAllowedEnergy){
521 secKinematicsMapVect.push_back(secondaryKinematicsMap);
526 std::size_t numSecondaries = secKinematicsMapVect.size();
529 return (numSecondaries);
532int PunchThroughG4Tool::getCorrelatedParticles(
const G4Track& g4PrimaryTrack, std::vector<std::map<std::string, double>> &secKinematicsMapVect,
int pdg,
int corrParticles, CLHEP::HepRandomEngine* rndmEngine,
double interpEnergy,
double interpEta)
535 const G4ParticleDefinition * mainG4Particle = g4PrimaryTrack.GetDefinition();
537 float mainMomMag2 = g4PrimaryTrack.GetMomentum().mag2();
538 float mainPartMass = mainG4Particle->GetPDGMass();
543 const double initEnergy = std::sqrt( mainMomMag2 + mainPartMass*mainPartMass );
546 double rand = CLHEP::RandFlat::shoot(rndmEngine)
547 *(p->getFullCorrelationEnergy() - p->getMinCorrelationEnergy())
548 + p->getMinCorrelationEnergy();
551 if ( initEnergy < rand )
554 return getAllParticles(g4PrimaryTrack, secKinematicsMapVect, rndmEngine, pdg, interpEnergy, interpEta);
559 double *histDomains = p->getCorrelationHistDomains();
560 TH2F *hist2d =
nullptr;
563 if ( initEnergy < histDomains[1])
567 hist2d = p->getCorrelationLowEHist();
571 double rand = CLHEP::RandFlat::shoot(rndmEngine)*(histDomains[2]-histDomains[1]) + histDomains[1];
572 hist2d = ( initEnergy < rand) ? p->getCorrelationLowEHist() : p->getCorrelationHighEHist();
578 Int_t xbin = hist2d->GetXaxis()->FindFixBin(corrParticles);
579 int numParticles = 0;
580 int maxParticles = p->getMaxNumParticles();
585 double rand = CLHEP::RandFlat::shoot(rndmEngine);
587 for (
int ybin = 1; ybin <= hist2d->GetNbinsY(); ybin++ )
589 sum += hist2d->GetBinContent(xbin, ybin);
593 numParticles = ybin - 1;
598 numParticles = lround( numParticles * p->getNumParticlesFactor() );
600 while ( (maxParticles >= 0.) && (numParticles > maxParticles) );
603 return getAllParticles(g4PrimaryTrack, secKinematicsMapVect, rndmEngine, pdg, interpEnergy, interpEta, numParticles);
614 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] passedPCAIterator ==> pcaCdfIterator = "<< pcaCdfIterator <<
" , pdg = "<< secondaryPDG <<
" , interpEnergy = "<< interpEnergy <<
" MeV, interpEta(*100) = "<< interpEta*100);
619 if ( p->getdoAnti() )
622 double rand = CLHEP::RandFlat::shoot(rndmEngine);
624 if (rand > 0.5) anti = -1;
629 std::vector<int> parInitEnergyEta;
630 parInitEnergyEta.push_back( std::round(interpEnergy) );
631 parInitEnergyEta.push_back( std::round(interpEta*100) );
635 double deltaTheta = 0.;
637 double momDeltaTheta = 0.;
638 double momDeltaPhi = 0.;
640 double principal_component_0 = 0.;
641 double principal_component_1 = 0.;
642 double principal_component_2 = 0.;
643 double principal_component_3 = 0.;
644 double principal_component_4 = 0.;
645 std::vector<double> transformed_variables;
647 principal_component_0 = p->getPCA0PDF()->getRand(rndmEngine, parInitEnergyEta);
648 principal_component_1 = p->getPCA1PDF()->getRand(rndmEngine, parInitEnergyEta);
649 principal_component_2 = p->getPCA2PDF()->getRand(rndmEngine, parInitEnergyEta);
650 principal_component_3 = p->getPCA3PDF()->getRand(rndmEngine, parInitEnergyEta);
651 principal_component_4 = p->getPCA4PDF()->getRand(rndmEngine, parInitEnergyEta);
653 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] Drawn punch through kinematics PCA components: PCA0 = "<< principal_component_0 <<
" PCA1 = "<< principal_component_1 <<
" PCA2 = "<< principal_component_2 <<
" PCA3 = "<< principal_component_3 <<
" PCA4 = "<< principal_component_4 );
655 std::vector<double> principal_components {
656 principal_component_0,
657 principal_component_1,
658 principal_component_2,
659 principal_component_3,
660 principal_component_4
663 transformed_variables =
inversePCA(pcaCdfIterator,principal_components);
671 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] Transformed punch through kinematics: energy = "<< energy <<
" MeV deltaTheta = "<< deltaTheta <<
" deltaPhi = "<<
deltaPhi <<
" momDeltaTheta = "<< momDeltaTheta <<
" momDeltaPhi = "<< momDeltaPhi );
673 energy *= p->getEnergyFactor();
676 if (energy < p->getMinEnergy()) {
677 energy = p->getMinEnergy() + 10;
686 deltaTheta *= ( CLHEP::RandFlat::shoot(rndmEngine) > 0.5 ) ? 1. : -1.;
690 theta = initParticleTheta + deltaTheta*p->getPosAngleFactor();
695 deltaPhi *= ( CLHEP::RandFlat::shoot(rndmEngine) > 0.5 ) ? 1. : -1.;
698 double phi = initParticlePhi +
deltaPhi*p->getPosAngleFactor();
706 double momTheta = 0.;
710 momDeltaTheta *= ( CLHEP::RandFlat::shoot(rndmEngine) > 0.5 ) ? 1. : -1.;
713 momTheta =
theta + momDeltaTheta*p->getMomAngleFactor();
715 while ( (momTheta >
M_PI) || (momTheta < 0.) );
718 momDeltaPhi *= ( CLHEP::RandFlat::shoot(rndmEngine) > 0.5 ) ? 1. : -1.;
720 double momPhi =
phi + momDeltaPhi*p->getMomAngleFactor();
722 while ( std::fabs(momPhi) > 2*
M_PI) momPhi /= 2.;
723 while (momPhi >
M_PI) momPhi -= 2*
M_PI;
724 while (momPhi < -
M_PI) momPhi += 2*
M_PI;
727 std::map<std::string, double> secondaryKinematicsMap;
728 secondaryKinematicsMap.insert({
"anti" , anti });
729 secondaryKinematicsMap.insert({
"secondaryPDG" , secondaryPDG });
730 secondaryKinematicsMap.insert({
"energy" , energy });
731 secondaryKinematicsMap.insert({
"theta" ,
theta });
732 secondaryKinematicsMap.insert({
"phi" ,
phi });
733 secondaryKinematicsMap.insert({
"momTheta" , momTheta });
734 secondaryKinematicsMap.insert({
"momPhi" , momPhi });
736 return secondaryKinematicsMap;
742 double totEnergySecondaries = 0;
746 for (std::size_t i = 0; i < secKinematicsMapVect.size(); i++) {
747 energy = secKinematicsMapVect[i].at(
"energy");
748 totEnergySecondaries += energy;
753 secKinematicsMapVect.begin(),
754 secKinematicsMapVect.end(),
755 [](
const std::map<std::string, double>& lhs,
const std::map<std::string, double>& rhs) {
756 return lhs.at(
"energy") > rhs.at(
"energy");
761 if(totEnergySecondaries > mainEnergyInit){
762 ATH_MSG_DEBUG(
"[PunchThroughG4Tool::checkEnergySumFromSecondaries] Case where energy of created secondaries more than parent track identified! ");
763 ATH_MSG_DEBUG(
"[PunchThroughG4Tool::checkEnergySumFromSecondaries] ==> TotalSecondariesEnergy = " << totEnergySecondaries <<
", ParentEnergy = "<< mainEnergyInit);
767 while (totEnergySecondaries > mainEnergyInit && !secKinematicsMapVect.empty()) {
769 std::map<std::string, double> lastMap = secKinematicsMapVect.back();
772 double lastEnergy = lastMap.at(
"energy");
775 secKinematicsMapVect.pop_back();
778 totEnergySecondaries -= lastEnergy;
782 return secKinematicsMapVect;
785void PunchThroughG4Tool::createAllSecondaryTracks(G4ParticleTable &ptable, G4FastStep& fastStep,
const G4Track& g4PrimaryTrack, std::vector<std::map<std::string, double>> &secKinematicsMapVect, G4TrackVector& secTrackCont,
const std::vector<double> &caloMSVars){
787 const G4ParticleDefinition* mainG4Particle = g4PrimaryTrack.GetDefinition();
788 float mainMomMag2 = g4PrimaryTrack.GetMomentum().mag2();
789 float mainPartMass = mainG4Particle->GetPDGMass();
790 double mainEnergyInit = std::sqrt(mainMomMag2 + mainPartMass * mainPartMass);
796 int numSecondaries = secKinematicsMapVect.size();
798 if(numSecondaries>0){
800 fastStep.SetNumberOfSecondaryTracks(numSecondaries);
803 double currentTime = g4PrimaryTrack.GetGlobalTime();
806 for(
int i=0;i < numSecondaries; i++){
807 int anti = (int)(secKinematicsMapVect[i].at(
"anti"));
808 int secondaryPDG = (int)(secKinematicsMapVect[i].at(
"secondaryPDG"));
809 int signedPDG = secondaryPDG*anti;
810 double energy = secKinematicsMapVect[i].at(
"energy");
811 double theta = secKinematicsMapVect[i].at(
"theta");
812 double phi = secKinematicsMapVect[i].at(
"phi");
813 double momTheta = secKinematicsMapVect[i].at(
"momTheta");
814 double momPhi = secKinematicsMapVect[i].at(
"momPhi");
817 ATH_MSG_DEBUG(
"[PunchThroughG4Tool::createAllSecondaryTracks] createSecondaryTrack input parameters: currentTime = " << currentTime <<
" anti? = "<< anti <<
" signedPDG = "<< signedPDG <<
" energy = "<< energy <<
" theta = "<<
theta <<
" phi = "<<
phi <<
" momTheta = "<< momTheta <<
" momPhi " << momPhi);
818 G4Track* newSecTrack =
createSecondaryTrack( ptable, fastStep, currentTime, signedPDG, energy,
theta,
phi, momTheta, momPhi, caloMSVars);
823 G4Exception(
"[PunchThroughG4Tool::createAllSecondaryTracks]",
"ExceptionError", FatalException,
"something went wrong while creating punch-through particle tracks");
828 secTrackCont.push_back( newSecTrack );
836 double energy,
double theta,
double phi,
double momTheta,
double momPhi,
const std::vector<double> &caloMSVars)
839 G4Track *newSecTrack =
nullptr;
841 G4ParticleDefinition* secG4Particle = ptable.FindParticle(secondarySignedPDG);
842 double mass = secG4Particle->GetPDGMass();
851 G4ThreeVector momVec;
852 double momMag = std::sqrt(energy*energy - mass*mass);
853 momVec.setRThetaPhi(momMag,momTheta,momPhi);
854 ATH_MSG_DEBUG(
"[PunchThroughG4Tool]::createSecondaryTrack] setRThetaPhi pre input parameters: energy = " << energy <<
" mass = "<< mass);
855 ATH_MSG_DEBUG(
"[PunchThroughG4Tool]::createSecondaryTrack] setRThetaPhi input parameters: std::sqrt(energy*energy - mass*mass) = " << std::sqrt(energy*energy - mass*mass) <<
" momTheta = "<< momTheta <<
" momPhi = "<< momPhi);
858 G4DynamicParticle dynParticle(secG4Particle,momVec);
859 newSecTrack = fastStep.CreateSecondaryTrack(dynParticle, posVec, currentTime,
false);
865 return 0.5 * TMath::Erfc(-
x * M_SQRT1_2);
870 std::vector<double> result;
871 result.reserve(m.size());
872 for (
const auto&
r : m){
873 result.push_back(std::inner_product(v.begin(), v.end(),
r.begin(), 0.0));
882 constexpr char delimiters =
',';
883 std::vector<T> tokens;
885 std::string_view::size_type lastPos =
str.find_first_not_of(delimiters, 0);
887 std::string_view::size_type pos =
str.find_first_of(delimiters, lastPos);
889 while (std::string_view::npos != pos || std::string_view::npos != lastPos) {
891 std::string_view numbStr =
str.substr(lastPos, pos - lastPos);
893 std::from_chars(numbStr.data(), numbStr.data() + numbStr.size(), num);
894 tokens.push_back(num);
896 lastPos =
str.find_first_not_of(delimiters, pos);
898 pos =
str.find_first_of(delimiters, lastPos);
906 int pidStrSingle = std::abs(pid);
910 for (
unsigned int i = 0; i < mapvect.size(); i++){
911 const std::string &pidStr = mapvect[i].at(
"pidStr");
913 if(std::find(v.begin(), v.end(),pidStrSingle)==v.end())
continue;
914 const std::string &etaMinsStr = mapvect[i].at(
"etaMins");
915 const std::string &etaMaxsStr = mapvect[i].at(
"etaMaxs");
918 assert(etaMaxsVect.size() == etaMinsVect.size());
919 for (
unsigned int j = 0; j < etaMinsVect.size(); j++){
920 double etaMinToCompare = etaMinsVect[j];
921 double etaMaxToCompare = etaMaxsVect[j];
922 if((
eta >= etaMinToCompare) && (
eta < etaMaxToCompare)){
936 std::vector<std::map<std::string, std::string>> xml_info;
937 doc = xmlParseFile( xmlFilePath.c_str() );
940 for( xmlNodePtr nodeRoot = doc->children; nodeRoot !=
nullptr; nodeRoot = nodeRoot->next) {
941 if (xmlStrEqual( nodeRoot->name, BAD_CAST mainNode.c_str() )) {
942 for( xmlNodePtr nodeRootChild = nodeRoot->children; nodeRootChild !=
nullptr; nodeRootChild = nodeRootChild->next ) {
943 if (xmlStrEqual( nodeRootChild->name, BAD_CAST
"info" )) {
944 if (nodeRootChild->children != NULL) {
945 for( xmlNodePtr infoNode = nodeRootChild->children; infoNode !=
nullptr; infoNode = infoNode->next) {
946 if(xmlStrEqual( infoNode->name, BAD_CAST
"item" )){
947 std::map<std::string, std::string> xml_info_item;
952 xml_info.emplace_back(std::move(xml_info_item));
971 std::transform (transformed_variables.begin(), transformed_variables.end(),
m_PCA_means[pcaCdfIterator].begin(), transformed_variables.begin(), std::plus<double>());
973 return transformed_variables;
980 doc = xmlParseFile( inversePCAConfigFile.c_str() );
982 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] Loading inversePCA: " << inversePCAConfigFile);
989 std::vector<std::vector<double>> PCA_matrix;
992 for( xmlNodePtr nodeRoot = doc->children; nodeRoot !=
nullptr; nodeRoot = nodeRoot->next) {
993 if (xmlStrEqual( nodeRoot->name, BAD_CAST
"PCAinverse" )) {
994 for( xmlNodePtr nodePCAinverse = nodeRoot->children; nodePCAinverse !=
nullptr; nodePCAinverse = nodePCAinverse->next ) {
996 if (xmlStrEqual( nodePCAinverse->name, BAD_CAST
m_xml_info_pca[i].at(
"name").c_str() )) {
997 if (nodePCAinverse->children != NULL) {
998 for( xmlNodePtr pcaNode = nodePCAinverse->children; pcaNode !=
nullptr; pcaNode = pcaNode->next) {
1000 if (xmlStrEqual( pcaNode->name, BAD_CAST
"PCAmatrix" )) {
1001 std::vector<double> PCA_matrix_row;
1002 for (
int i = 0; i <= 4; ++i) {
1003 std::string propName =
"comp_" + std::to_string(i);
1006 PCA_matrix.push_back(std::move(PCA_matrix_row));
1008 else if (xmlStrEqual( pcaNode->name, BAD_CAST
"PCAmeans" )) {
1009 std::vector<double> PCA_means_row;
1010 for (
int i = 0; i <= 4; ++i) {
1011 std::string propName =
"mean_" + std::to_string(i);
1031 return StatusCode::SUCCESS;
1035 std::map<double, double> variable0_inverse_cdf_row;
1036 std::map<double, double> variable1_inverse_cdf_row;
1037 std::map<double, double> variable2_inverse_cdf_row;
1038 std::map<double, double> variable3_inverse_cdf_row;
1039 std::map<double, double> variable4_inverse_cdf_row;
1046 doc = xmlParseFile( inverseCdfConfigFile.c_str() );
1048 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] Loading inverse CDF: " << inverseCdfConfigFile);
1057 for( xmlNodePtr nodeRoot = doc->children; nodeRoot !=
nullptr; nodeRoot = nodeRoot->next) {
1058 if (xmlStrEqual( nodeRoot->name, BAD_CAST
"CDFMappings" )) {
1059 for( xmlNodePtr typeMappings = nodeRoot->children; typeMappings !=
nullptr; typeMappings = typeMappings->next ) {
1060 if (xmlStrEqual( typeMappings->name, BAD_CAST
m_xml_info_cdf[i].at(
"name").c_str() )) {
1061 if (typeMappings->children != NULL) {
1062 for( xmlNodePtr nodeMappings = typeMappings->children; nodeMappings !=
nullptr; nodeMappings = nodeMappings->next) {
1064 if (xmlStrEqual( nodeMappings->name, BAD_CAST
"variable0" )) {
1067 else if (xmlStrEqual( nodeMappings->name, BAD_CAST
"variable1" )) {
1070 else if (xmlStrEqual( nodeMappings->name, BAD_CAST
"variable2" )) {
1073 else if (xmlStrEqual( nodeMappings->name, BAD_CAST
"variable3" )) {
1076 else if (xmlStrEqual( nodeMappings->name, BAD_CAST
"variable4" )) {
1096 return StatusCode::SUCCESS;
1100 std::map<double, double> mappings;
1103 for( xmlNodePtr
node = nodeParent->children;
node !=
nullptr;
node =
node->next ) {
1105 if (xmlStrEqual(
node->
name, BAD_CAST
"CDFmap" )) {
1119 auto upper = inverse_cdf_map.upper_bound(norm_cdf);
1120 auto lower =
upper--;
1122 double m = (
upper->second - lower->second)/(
upper->first - lower->first);
1123 double c = lower->second - m * lower->first;
1124 double transformed = m * norm_cdf + c;
1132 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] interpolating incoming energy: " << energy);
1134 std::string energyPointsString;
1136 energyPointsString += std::to_string(element) +
" ";
1139 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] available energy points: " << energyPointsString);
1144 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] incoming energy > largest energy point, returning greatest energy point: " <<
m_energyPoints.back());
1148 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] incoming energy < smallest energy point, returning smallest energy point: " << *upperEnergy);
1149 return *upperEnergy;
1152 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] energy points upper_bound: "<< *upperEnergy);
1154 double randomShoot = CLHEP::RandFlat::shoot(rndmEngine);
1156 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] Shooting random number: "<< randomShoot);
1158 double midPoint = *std::prev(upperEnergy)*M_SQRT2;
1160 if(energy < midPoint){
1162 double distance = std::abs(energy - *std::prev(upperEnergy))/((midPoint) - *std::prev(upperEnergy));
1164 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] incoming energy is closest to prev(upper_bound) in log(energy), distance: " << distance );
1166 if(randomShoot < distance){
1167 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] randomShoot < distance, returning upper_bound " << *upperEnergy );
1168 return *upperEnergy;
1170 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] randomShoot > distance, returning prev(upper_bound) " << *std::prev(upperEnergy) );
1172 return *std::prev(upperEnergy);
1174 else if(energy > midPoint){
1176 double distance = std::abs(energy - *upperEnergy)/((*upperEnergy - midPoint));
1178 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] incoming energy is closest to upper_bound in log(energy), distance: " << distance );
1180 if(randomShoot < distance){
1181 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] randomShoot < distance, returning prev(upper_bound) " << *std::prev(upperEnergy) );
1182 return *std::prev(upperEnergy);
1184 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] randomShoot > distance, returning upper_bound " << *upperEnergy );
1185 return *upperEnergy;
1188 return *upperEnergy;
1193 double absEta = std::abs(
eta);
1195 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] interpolating incoming abs(eta): " << absEta);
1197 std::string etaPointsString;
1199 etaPointsString += std::to_string(element) +
" ";
1202 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] available eta points: " << etaPointsString);
1207 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] incoming abs(eta) > largest eta point, returning greatest eta point: " <<
m_etaPoints.back());
1212 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] eta points upper_bound: "<< *upperEta);
1214 double randomShoot = CLHEP::RandFlat::shoot(rndmEngine);
1216 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] Shooting random number: "<< randomShoot);
1218 if(std::abs(absEta - *upperEta) < std::abs(absEta - *std::prev(upperEta))){
1220 double distance = std::abs(absEta - *upperEta)/((*upperEta - *std::prev(upperEta))/2);
1222 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] abs(eta) is closer to eta points upper_bound, distance: " << distance );
1224 if(randomShoot > distance){
1225 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] randomShoot > distance, returning upper_bound " << *upperEta );
1229 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] randomShoot < distance, returning prev(upper_bound) " << *std::prev(upperEta) );
1231 return *std::prev(upperEta);
1233 else if(std::abs(absEta - *std::prev(upperEta)) < std::abs(absEta - *upperEta)){
1236 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] prev of upper bound is begin, returning that: " << *std::prev(upperEta) );
1237 return *std::prev(upperEta);
1240 double distance = std::abs(absEta - *std::prev(upperEta))/((*std::prev(upperEta) - *std::prev(std::prev(upperEta)))/2);
1242 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] abs(eta) is closer to eta points prev(upper_bound), distance: " << distance );
1244 if(randomShoot > distance){
1245 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] randomShoot > distance, returning prev(prev(upper_bound)) " << *std::prev(std::prev(upperEta)) );
1247 return *std::prev(std::prev(upperEta));
1249 ATH_MSG_DEBUG(
"[PunchThroughG4Tool] randomShoot < distance, returning prev(upper_bound) " << *std::prev(upperEta) );
1251 return *std::prev(upperEta);
1254 return *std::prev(upperEta);
1259 double minEnergy,
int maxNumParticles,
double numParticlesFactor,
1260 double energyFactor,
double posAngleFactor,
double momAngleFactor)
1262 G4ParticleDefinition* secG4Particle = ptable.FindParticle(std::abs(pdg));
1263 double restMass = secG4Particle->GetPDGMass();
1268 if (!pdf_num )
return StatusCode::FAILURE;
1274 return StatusCode::FAILURE;
1282 return StatusCode::FAILURE;
1290 return StatusCode::FAILURE;
1297 return StatusCode::FAILURE;
1304 return StatusCode::FAILURE;
1309 particle->setNumParticlesPDF(std::move(pdf_num));
1310 particle->setPCA0PDF(std::move(pdf_pca0));
1311 particle->setPCA1PDF(std::move(pdf_pca1));
1312 particle->setPCA2PDF(std::move(pdf_pca2));
1313 particle->setPCA3PDF(std::move(pdf_pca3));
1314 particle->setPCA4PDF(std::move(pdf_pca4));
1317 minEnergy = ( minEnergy > restMass ) ? minEnergy : restMass;
1318 particle->setMinEnergy(minEnergy);
1319 particle->setMaxNumParticles(maxNumParticles);
1320 particle->setNumParticlesFactor(numParticlesFactor);
1321 particle->setEnergyFactor(energyFactor);
1322 particle->setPosAngleFactor(posAngleFactor);
1323 particle->setMomAngleFactor(momAngleFactor);
1328 return StatusCode::SUCCESS;
1332 double minCorrEnergy,
double fullCorrEnergy)
1335 std::map<int, PunchThroughParticle*>::iterator location1 =
m_particles.find(pdgID1);
1336 std::map<int, PunchThroughParticle*>::iterator location2 =
m_particles.find(pdgID2);
1340 return StatusCode::FAILURE;
1343 std::stringstream name;
1344 name <<
"NumExitCorrelations/x_PDG" << std::abs(pdgID1) <<
"__y_PDG" << std::abs(pdgID2) <<
"__lowE";
1347 name <<
"NumExitCorrelations/x_PDG" << std::abs(pdgID1) <<
"__y_PDG" << std::abs(pdgID2) <<
"__highE";
1350 name <<
"NumExitCorrelations/x_PDG" << std::abs(pdgID2) <<
"__y_PDG" << std::abs(pdgID1) <<
"__lowE";
1353 name <<
"NumExitCorrelations/x_PDG" << std::abs(pdgID2) <<
"__y_PDG" << std::abs(pdgID1) <<
"__highE";
1356 if ( (!hist1_2_lowE) || (!hist2_1_lowE) || (!hist1_2_highE) || (!hist2_1_highE) )
1358 ATH_MSG_ERROR(
"[PunchThroughG4Tool] unable to retrieve the correlation data for PDG IDs " << pdgID1 <<
" and " << pdgID2);
1359 return StatusCode::FAILURE;
1370 m_particles[pdgID1]->setCorrelation(pdgID2, hist2_1_lowE, hist2_1_highE,
1371 minCorrEnergy, fullCorrEnergy,
1372 lowE, midE, upperE);
1373 m_particles[pdgID2]->setCorrelation(pdgID1, hist1_2_lowE, hist1_2_highE,
1374 minCorrEnergy, fullCorrEnergy,
1375 lowE, midE, upperE);
1376 return StatusCode::SUCCESS;
1386 std::unique_ptr<PunchThroughPDFCreator> pdf = std::make_unique<PunchThroughPDFCreator>();
1389 std::stringstream dirName;
1390 dirName << folderName << pdg;
1391 pdf->setName(dirName.str());
1393 TDirectory * dir = (TDirectory*)fileLookupTable->Get(dirName.str().c_str());
1396 ATH_MSG_ERROR(
"[PunchThroughG4Tool] unable to retrieve directory object ("<< folderName << pdg <<
")" );
1401 TIter keyList(dir->GetListOfKeys());
1404 while ((key = (TKey*)keyList())) {
1407 TH1* hist =
nullptr;
1409 std::string histName;
1410 if(strcmp(key->GetClassName(),
"TH1F") == 0){
1411 hist = (TH1*)key->ReadObj();
1412 histName = hist->GetName();
1416 std::string strEnergy = histName.substr( histName.find_first_of(
'E') + 1, histName.find_first_of(
'_')-histName.find_first_of(
'E') - 1 );
1417 histName.erase(0, histName.find_first_of(
'_') + 1);
1418 std::string strEtaMin = histName.substr( histName.find(
"etaMin") + 6, histName.find_first_of(
'_') - histName.find(
"etaMin") - 6 );
1419 histName.erase(0, histName.find(
'_') + 1);
1420 std::string strEtaMax = histName.substr( histName.find(
"etaMax") + 6, histName.length());
1423 const int energy = std::stoi(strEnergy);
1424 const int etaMin = std::stoi(strEtaMin);
1427 pdf->addToEnergyEtaHist1DMap(energy, etaMin, hist);
1430 const double energyDbl =
static_cast<double>(energy);
1431 const double etaDbl =
static_cast<double>(etaMin)/100.;
1450 const std::string_view
str( cstr);
1451 const std::string_view pattern( cpattern);
1452 const size_t pos =
str.find(pattern);
1454 if ( pos == std::string::npos)
1456 ATH_MSG_WARNING(
"[PunchThroughG4Tool] unable to retrieve floating point number from string");
1457 return -999999999999.;
1459 const std::string_view substring =
str.substr(pos+pattern.length());
1460 std::from_chars(substring.data(), substring.data() + substring.size(), num);
1474 const double theta1 = atan (R1 / z1);
1475 const double theta2 = atan (R1 / z2);
1476 const double theta3 = atan (R2 / z2);
1482 r = std::fabs (z1 * tan(
theta));
1492 r = std::fabs(z2 * tan(
theta));;
1494 else if (
theta >= theta3 &&
theta < (TMath::Pi() - theta3) )
1499 else if (
theta >= (TMath::Pi() - theta3) &&
theta < (TMath::Pi() - theta2) )
1502 r = std::fabs(z2 * tan(
theta));
1504 else if (
theta >= (TMath::Pi() - theta2) &&
theta < (TMath::Pi() - theta1) )
1509 else if (
theta >= (TMath::Pi() - theta1) &&
theta <= TMath::Pi() )
1512 r = std::fabs(z1 * tan(
theta));
1518 ATH_MSG_WARNING(
"[PunchThroughG4Tool::punchTroughPosPropagator] Given theta angle is incorrect, setting particle position to (0, 0, 0)");
1519 x = 0.0;
y = 0.0;
z = 0.0;
r = 0.0;
1524 G4ThreeVector posVec(
x,
y,
z);
1526 ATH_MSG_DEBUG(
"[PunchThroughG4Tool::punchTroughPosPropagator] position of produced punch-through particle: x = " <<
x <<
" y = "<<
y <<
" z = "<<
z<<
" r = "<< posVec.perp() <<
"std::sqrt(x^2+y^2) = "<< std::sqrt(
x *
x +
y *
y));
const boost::regex ref(r_ef)
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
Scalar theta() const
theta method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
This class holds information for different properties of a punch-through particle (energy,...
void name(const std::string &n)
Eigen::Matrix< double, 3, 1 > Vector3D
AtlasRegion
A simple enum of ATLAS regions and sub-detectors.
void AddXmlToCollectionMap(xmlNodePtr node, const char *attrName, COL &collection)
void AddXmlToCollection(xmlNodePtr node, const char *attrName, COL &collection)
T deltaPhi(T phiA, T phiB)
Return difference phiA - phiB in range [-pi, pi].
T GetXmlAttr(xmlNodePtr node, const char *attrName, const T &defaultValue=T{}) noexcept(std::is_arithmetic_v< T >)
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
void swap(ElementLinkVector< DOBJ > &lhs, ElementLinkVector< DOBJ > &rhs)