Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:13:21

0001 /// ---------------------------------------------------------------------------
0002 /*! \file   ParTools.cc
0003  *  \author Derek Anderson
0004  *  \date   03.06.2024
0005  *
0006  *  Collection of frequent particle-related methods utilized in
0007  *  the sPHENIX Cold QCD Energy-Energy Correlator analysis.
0008  */
0009 /// ---------------------------------------------------------------------------
0010 
0011 #define SCORRELATORUTILITIES_PARTOOLS_CC
0012 
0013 // namespace definition
0014 #include "ParTools.h"
0015 
0016 // make common namespaces implicit
0017 using namespace std;
0018 
0019 
0020 
0021 // particle methods ===========================================================
0022 
0023 namespace SColdQcdCorrelatorAnalysis {
0024 
0025   // --------------------------------------------------------------------------
0026   //! Get index of signal subevent for embedding vs. not
0027   // --------------------------------------------------------------------------
0028   int Tools::GetSignal(const bool isEmbed) {
0029 
0030     return isEmbed ? Const::SubEvt::EmbedSignal : Const::SubEvt::NotEmbedSignal;
0031 
0032   }
0033 
0034 
0035 
0036   // --------------------------------------------------------------------------
0037   //! Get Embedding ID from a subevent
0038   // --------------------------------------------------------------------------
0039   int Tools::GetEmbedID(PHCompositeNode* topNode, const int iEvtToGrab) {
0040 
0041     // grab mc event & return embedding id
0042     PHHepMCGenEvent* mcEvt = Interfaces::GetMcEvent(topNode, iEvtToGrab);
0043     return mcEvt -> get_embedding_id();
0044 
0045   }  // end 'GetEmbedID(PHCompositeNode*, int)'
0046 
0047 
0048 
0049   // --------------------------------------------------------------------------
0050   //! Get an embedding ID for a given barcode
0051   // --------------------------------------------------------------------------
0052   int Tools::GetEmbedIDFromBarcode(const int barcode, PHCompositeNode* topNode) {
0053 
0054     // by default, return signal
0055     int  idEmbed      = Const::SubEvt::NotEmbedSignal;
0056     bool foundBarcode = false;
0057 
0058     // loop over all subevents to search
0059     PHHepMCGenEventMap* mcEvtMap = Interfaces::GetMcEventMap(topNode);
0060     for (
0061       PHHepMCGenEventMap::ConstIter genEvt = mcEvtMap -> begin();
0062       genEvt != mcEvtMap -> end();
0063       ++genEvt
0064     ) {
0065 
0066       // loop over particles
0067       for (
0068         HepMC::GenEvent::particle_const_iterator hepPar = genEvt -> second -> getEvent() -> particles_begin();
0069         hepPar != genEvt -> second -> getEvent() -> particles_end();
0070         ++hepPar
0071       ) {
0072 
0073         // break if barcode found
0074         if ((*hepPar) -> barcode() == barcode) {
0075           idEmbed      = genEvt -> first;
0076           foundBarcode = true;
0077           break;
0078         }
0079       }  // end particle loop
0080 
0081       // if found barcode, break
0082       if (foundBarcode) break;
0083 
0084     }  // end subevent loop
0085     return idEmbed;
0086 
0087   }  // end 'GetEmbedIDFromBarcode(int, PHCompositeNode*)'
0088 
0089 
0090 
0091   // --------------------------------------------------------------------------
0092   //! Get an embedding ID for a given track ID
0093   // --------------------------------------------------------------------------
0094   int Tools::GetEmbedIDFromTrackID(const int idTrack, PHCompositeNode* topNode) {
0095 
0096     // grab truth container
0097     PHG4TruthInfoContainer* info = Interfaces::GetTruthContainer(topNode);
0098 
0099     // return embedding id
0100     return info -> isEmbeded(idTrack);
0101 
0102   }  // end 'GetEmbedIDFromTrackID(int, PHCompositeNode*)'
0103 
0104 
0105 
0106   // --------------------------------------------------------------------------
0107   //! Check if a status is final state
0108   // --------------------------------------------------------------------------
0109   bool Tools::IsFinalState(const int status) {
0110 
0111     return (status == 1);
0112 
0113   }  // end 'IsFinalState()'
0114 
0115 
0116 
0117   // --------------------------------------------------------------------------
0118   //! Check if a subevent is good wrt. a provided subevent option
0119   // --------------------------------------------------------------------------
0120   bool Tools::IsSubEvtGood(
0121     const int embedID,
0122     const int option,
0123     const bool isEmbed
0124   ) {
0125 
0126     // set ID of signal
0127     int signalID = Const::SubEvt::NotEmbedSignal;
0128     if (isEmbed) {
0129       signalID = Const::SubEvt::EmbedSignal;
0130     }
0131 
0132     bool isSubEvtGood = true;
0133     switch (option) {
0134 
0135       // consider everything
0136       case Const::SubEvtOpt::Everything:
0137         isSubEvtGood = true;
0138         break;
0139 
0140       // only consider signal event
0141       case Const::SubEvtOpt::OnlySignal:
0142         isSubEvtGood = (embedID == signalID);
0143         break;
0144 
0145       // only consider background events
0146       case Const::SubEvtOpt::AllBkgd:
0147         isSubEvtGood = (embedID <= Const::SubEvt::Background);
0148         break;
0149 
0150       // only consider primary background event
0151       case Const::SubEvtOpt::PrimaryBkgd:
0152         isSubEvtGood = (embedID == Const::SubEvt::Background);
0153         break;
0154 
0155       // only consider pileup events
0156       case Const::SubEvtOpt::Pileup:
0157         isSubEvtGood = (embedID < Const::SubEvt::Background);
0158         break;
0159 
0160       // by default do nothing
0161       default:
0162         isSubEvtGood = true;
0163         break;
0164     }
0165     return isSubEvtGood;
0166 
0167   }  // end 'IsSubEvtGood(int, int, bool)'
0168 
0169 
0170 
0171   // --------------------------------------------------------------------------
0172   //! Check if a subevent falls in a list of subevents to use 
0173   // --------------------------------------------------------------------------
0174   bool Tools::IsSubEvtGood(const int embedID, vector<int> subEvtsToUse) {
0175 
0176     bool isSubEvtGood = false;
0177     for (const int evtToUse : subEvtsToUse) {
0178       if (embedID == evtToUse) {
0179         isSubEvtGood = true;
0180         break;
0181       }
0182     }
0183     return isSubEvtGood;
0184 
0185   }  // end 'IsSubEvtGood(int, vector<int>)'
0186 
0187 
0188 
0189   // --------------------------------------------------------------------------
0190   //! Get charge of a particle based on PID
0191   // --------------------------------------------------------------------------
0192   float Tools::GetParticleCharge(const int pid) {
0193 
0194     // particle charge
0195     float charge = Const::MapPidOntoCharge()[abs(pid)];
0196 
0197     // if antiparticle, flip charge and return
0198     if (pid < 0) {
0199       charge *= -1.;
0200     }
0201     return charge;
0202 
0203   }  // end 'GetParticleCharge(int)'
0204 
0205 
0206 
0207   // --------------------------------------------------------------------------
0208   //! Get list of embedding IDs to use
0209   // --------------------------------------------------------------------------
0210   vector<int> Tools::GrabSubevents(
0211     PHCompositeNode* topNode,
0212     vector<int> subEvtsToUse
0213   ) {
0214 
0215     // instantiate vector to hold subevents
0216     vector<int> subevents;
0217   
0218     PHHepMCGenEventMap* mcEvtMap = Interfaces::GetMcEventMap(topNode);
0219     for (
0220       PHHepMCGenEventMap::ConstIter itEvt = mcEvtMap -> begin();
0221       itEvt != mcEvtMap -> end();
0222       ++itEvt
0223     ) {
0224 
0225       // grab event id
0226       const int embedID = itEvt -> second -> get_embedding_id();
0227 
0228       // check to see if event is in provided list
0229       bool addToList = false;
0230       for (const int idToCheck : subEvtsToUse) {
0231         if (embedID == idToCheck) {
0232           addToList = true;
0233           break;
0234         }
0235       }  // end evtsToGrab loop
0236 
0237       // if on list, add to list of good subevents
0238       if (addToList) subevents.push_back(embedID);
0239 
0240     }
0241     return subevents;
0242 
0243   }  // end 'GrabSubevents(PHCompositeNode*, vector<int>)'
0244 
0245 
0246 
0247   // --------------------------------------------------------------------------
0248   //! Get subevents based on a specified option
0249   // --------------------------------------------------------------------------
0250   vector<int> Tools::GrabSubevents(
0251     PHCompositeNode* topNode,
0252     const int option,
0253     const bool isEmbed
0254   ) {
0255 
0256     // instantiate vector to hold subevents
0257     vector<int> subevents;
0258   
0259     PHHepMCGenEventMap* mcEvtMap = Interfaces::GetMcEventMap(topNode);
0260     for (
0261       PHHepMCGenEventMap::ConstIter itEvt = mcEvtMap -> begin();
0262       itEvt != mcEvtMap -> end();
0263       ++itEvt
0264     ) {
0265 
0266       // grab event id
0267       const int embedID = itEvt -> second -> get_embedding_id();
0268 
0269       // if subevent satisfies option, add to list
0270       const bool isSubEvtGood = IsSubEvtGood(embedID, option, isEmbed);
0271       if (isSubEvtGood) subevents.push_back(embedID);
0272     }
0273     return subevents;
0274 
0275   }  // end 'GrabSubevents(PHCompositeNode*, optional<vector<int>>)'
0276 
0277 
0278 
0279   // --------------------------------------------------------------------------
0280   //! Find a PHG4Particle based on its barcode
0281   // --------------------------------------------------------------------------
0282   PHG4Particle* Tools::GetPHG4ParticleFromBarcode(
0283     const int barcode,
0284     PHCompositeNode* topNode
0285   ) {
0286 
0287     // by default, return null pointer
0288     PHG4Particle* parToGrab = NULL;
0289 
0290     // grab truth info container
0291     PHG4TruthInfoContainer* container = Interfaces::GetTruthContainer(topNode);
0292 
0293     // loop over all particles in container to search
0294     PHG4TruthInfoContainer::ConstRange particles = container -> GetParticleRange();
0295     for (
0296       PHG4TruthInfoContainer::ConstIterator itPar = particles.first;
0297       itPar != particles.second;
0298       ++itPar
0299     ) {
0300       if (itPar -> second -> get_barcode() == barcode) {
0301         parToGrab = itPar -> second;
0302         break;
0303       }
0304     }  // end particle loop
0305     return parToGrab;
0306 
0307   }  // end 'GetPHG4ParticleFromBarcode(int, PHCompositeNode*)'
0308 
0309 
0310 
0311   // --------------------------------------------------------------------------
0312   //! Find a PHG4Particle based on its "track code"
0313   // --------------------------------------------------------------------------
0314   PHG4Particle* Tools::GetPHG4ParticleFromTrackID(const int id, PHCompositeNode* topNode) {
0315 
0316     // by default, return null pointer
0317     PHG4Particle* parToGrab = NULL;
0318 
0319     // grab truth info container
0320     PHG4TruthInfoContainer* container = Interfaces::GetTruthContainer(topNode);
0321 
0322     // loop over all particles in container to search
0323     PHG4TruthInfoContainer::ConstRange particles = container -> GetParticleRange();
0324     for (
0325       PHG4TruthInfoContainer::ConstIterator itPar = particles.first;
0326       itPar != particles.second;
0327       ++itPar
0328     ) {
0329       if (itPar -> second -> get_track_id() == id) {
0330         parToGrab = itPar -> second;
0331         break;
0332       }
0333     }  // end particle loop
0334     return parToGrab;
0335 
0336   }  // end 'GetPHG4ParticleFromTrackID(int, PHCompositeNode*)'
0337 
0338 
0339 
0340   // --------------------------------------------------------------------------
0341   //! Find a HepMC GenParticle based on its barcode
0342   // --------------------------------------------------------------------------
0343   HepMC::GenParticle* Tools::GetHepMCGenParticleFromBarcode(
0344     const int barcode,
0345     PHCompositeNode* topNode
0346   ) {
0347 
0348     // by default, return null pointer
0349     HepMC::GenParticle* parToGrab    = NULL;
0350     bool                foundBarcode = false;
0351 
0352     // loop over all subevents to search
0353     PHHepMCGenEventMap* mcEvtMap = Interfaces::GetMcEventMap(topNode);
0354     for (
0355       PHHepMCGenEventMap::ConstIter genEvt = mcEvtMap -> begin();
0356       genEvt != mcEvtMap -> end();
0357       ++genEvt
0358     ) {
0359 
0360       // loop over particles
0361       for (
0362         HepMC::GenEvent::particle_const_iterator hepPar = genEvt -> second -> getEvent() -> particles_begin();
0363         hepPar != genEvt -> second -> getEvent() -> particles_end();
0364         ++hepPar
0365       ) {
0366 
0367         // break if barcode found
0368         if ((*hepPar) -> barcode() == barcode) {
0369           parToGrab    = *hepPar;
0370           foundBarcode = true;
0371           break;
0372         }
0373       }  // end particle loop
0374 
0375       // if found barcode, break
0376       if (foundBarcode) break;
0377 
0378     }  // end subevent loop
0379     return parToGrab;
0380 
0381   }  // end 'GetHepMCGenParticleFromBarcode(int, PHCompositeNode*)'
0382 
0383 }  // end SColdQcdCorrealtorAnalysis namespace
0384 
0385 // end ------------------------------------------------------------------------