diff --git a/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonRecToolInterfaces/MuonRecToolInterfaces/IMuonTrackTruthTool.h b/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonRecToolInterfaces/MuonRecToolInterfaces/IMuonTrackTruthTool.h index 2248a83df751d3fa0b9d641394a19b3573c99bcb..0421d00f3543d5a19ce632cbcf6c7e20c53b63de 100644 --- a/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonRecToolInterfaces/MuonRecToolInterfaces/IMuonTrackTruthTool.h +++ b/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonRecToolInterfaces/MuonRecToolInterfaces/IMuonTrackTruthTool.h @@ -130,15 +130,15 @@ namespace Muon { /// returns the mother particle of the particle with barcodeIn if it is found in the truth trajectory /// It traces the decay chain until if finds the first particle that is different flavor from the starting one. - virtual const HepMC::GenParticle& getMother( const TruthTrajectory& traj, const int barcodeIn ) const = 0; + virtual const HepMC::GenParticle* getMother( const TruthTrajectory& traj, const int barcodeIn ) const = 0; /// Returns the ancestor particle of the particle with barcodeIn if it is found in the truth trajectory. /// Ancestor here means the last particle at generator level that has a status code different from final state, e.g. Z - virtual const HepMC::GenParticle& getAncestor( const TruthTrajectory& traj, const int barcodeIn ) const = 0; + virtual const HepMC::GenParticle* getAncestor( const TruthTrajectory& traj, const int barcodeIn ) const = 0; /// Returns the initial particle of the particle with barcodeIn if it is found in the truth trajectory. /// For example a mu undergoing a mubrem would create a second mu, in which case this method returns the mu prior to bremsstrahlung. - virtual const HepMC::GenParticle& getInitial( const TruthTrajectory& traj, const int barcodeIn ) const = 0; + virtual const HepMC::GenParticle* getInitial( const TruthTrajectory& traj, const int barcodeIn ) const = 0; /// Returns the number of steps a particle took while maintaining its PDG ID virtual unsigned int getNumberOfScatters( const TruthTrajectory& traj, const int barcodeIn ) const = 0; diff --git a/MuonSpectrometer/MuonTruthAlgs/MuonTruthAlgs/MuonTrackTruthTool.h b/MuonSpectrometer/MuonTruthAlgs/MuonTruthAlgs/MuonTrackTruthTool.h index a3402457145cf056ee31eaa0660a151ab019a75c..63b6c0c53aa3350f39689f06155451940725bb80 100644 --- a/MuonSpectrometer/MuonTruthAlgs/MuonTruthAlgs/MuonTrackTruthTool.h +++ b/MuonSpectrometer/MuonTruthAlgs/MuonTruthAlgs/MuonTrackTruthTool.h @@ -95,16 +95,16 @@ namespace Muon { /// Returns the mother particle of the particle with barcodeIn if it is found in the truth trajectory. /// It traces the decay chain until if finds the first particle that is different flavor from the starting one. - const HepMC::GenParticle& getMother( const TruthTrajectory& traj, const int barcodeIn ) const; + const HepMC::GenParticle* getMother( const TruthTrajectory& traj, const int barcodeIn ) const; /// Returns the ancestor particle of the particle with barcodeIn if it is found in the truth trajectory. /// Ancestor here means the last particle at generator level that has a status code different from final state, e.g. Z - const HepMC::GenParticle& getAncestor( const TruthTrajectory& traj, const int barcodeIn ) const; + const HepMC::GenParticle* getAncestor( const TruthTrajectory& traj, const int barcodeIn ) const; /// Returns the initial particle of the particle with barcodeIn if it is found in the truth trajectory. /// For example a mu undergoing a mubrem would create a second mu, in which case this method returns the mu prior to bremsstrahlung. /// This interface calls the method getInitialPair. - const HepMC::GenParticle& getInitial( const TruthTrajectory& traj, const int barcodeIn ) const; + const HepMC::GenParticle* getInitial( const TruthTrajectory& traj, const int barcodeIn ) const; /// Returns the number of steps a particle took while maintaining its PDG ID. /// This method calls getInitialPair for calculating this number. diff --git a/MuonSpectrometer/MuonTruthAlgs/src/MuonTrackTruthTool.cxx b/MuonSpectrometer/MuonTruthAlgs/src/MuonTrackTruthTool.cxx index bab21d78ff88bb6728556d4ea7771a80bb4c737b..aabb23eef5f3b3c0916cd9def39d40727f2a68ec 100644 --- a/MuonSpectrometer/MuonTruthAlgs/src/MuonTrackTruthTool.cxx +++ b/MuonSpectrometer/MuonTruthAlgs/src/MuonTrackTruthTool.cxx @@ -741,57 +741,49 @@ namespace Muon { m_truthTrajectoriesToBeDeleted.clear(); } - const HepMC::GenParticle& MuonTrackTruthTool::getMother( const TruthTrajectory& traj, const int barcodeIn ) const { - std::vector<HepMcParticleLink>::const_iterator pit = traj.begin(); - std::vector<HepMcParticleLink>::const_iterator pit_end = traj.end(); + const HepMC::GenParticle* MuonTrackTruthTool::getMother( const TruthTrajectory& traj, const int barcodeIn ) const { ATH_MSG_DEBUG( "getMother() : size = " << traj.size() ); - const HepMC::GenParticle* theMother = 0; - int pdgFinal = (*pit)->pdg_id(); + int pdgFinal = ( (traj.size()==0)? -999 : traj.front()->pdg_id()); bool foundBC = false; - for( ;pit!=pit_end;++pit ){ - if ((*pit)->barcode()==barcodeIn || foundBC){ + for( auto pit: traj){ + if (!pit) continue; + if (pit->barcode()==barcodeIn || foundBC){ foundBC = true; - ATH_MSG_DEBUG( "getMother() : pdg = " << (*pit)->pdg_id() << " barcode = " << (*pit)->barcode () ); - if( (*pit)->pdg_id() != pdgFinal ) { // the first case a track had a different flavour - theMother = *pit; + ATH_MSG_DEBUG( "getMother() : pdg = " << pit->pdg_id() << " barcode = " << pit->barcode () ); + if( pit->pdg_id() != pdgFinal ) { // the first case a track had a different flavour + if (pit->pdg_id()==pdgFinal) ATH_MSG_ERROR( "Wrong pdgId association in getMother() " ); + return pit; break; } } } - - // sanity check - if (theMother && theMother->pdg_id()==pdgFinal) ATH_MSG_ERROR( "Wrong pdgId association in getMother() " ); - return *theMother; + return nullptr; } - const HepMC::GenParticle& MuonTrackTruthTool::getAncestor( const TruthTrajectory& traj, const int barcodeIn ) const { - std::vector<HepMcParticleLink>::const_iterator pit = traj.begin(); - std::vector<HepMcParticleLink>::const_iterator pit_end = traj.end(); - const HepMC::GenParticle* theAncestor = 0; + const HepMC::GenParticle* MuonTrackTruthTool::getAncestor( const TruthTrajectory& traj, const int barcodeIn ) const { bool foundBC = false; - for( ;pit!=pit_end;++pit ){ - if ((*pit)->barcode()==barcodeIn || foundBC){ + for(auto pit: traj){ + if (!pit) continue; + if (pit->barcode()==barcodeIn || foundBC){ foundBC = true; - if( (*pit)->status() > 1 ) {//first non final state particle - theAncestor = *pit; + if( pit->status() > 1 ) {//first non final state particle + return pit; break; } } } - return *theAncestor; + return nullptr; } const std::pair<const HepMC::GenParticle*, unsigned int> MuonTrackTruthTool::getInitialPair( const TruthTrajectory& traj, const int barcodeIn ) const { - std::pair<const HepMC::GenParticle*,unsigned int> thePair; + std::pair<const HepMC::GenParticle*,unsigned int> thePair(nullptr,0); unsigned int scat = 0; ATH_MSG_DEBUG( "getFirst() : size = " << traj.size() ); - std::vector<HepMcParticleLink>::const_iterator pit = traj.begin(); - std::vector<HepMcParticleLink>::const_iterator pit_end = traj.end(); - const HepMC::GenParticle* theFirst = 0; bool foundBC = false; int pdgFinal = 0; double ePrev = 0.; - for( ;pit!=pit_end;++pit ){ + const HepMC::GenParticle* theFirst=nullptr; + for(auto pit=traj.begin();pit!=traj.end();++pit){ if ((*pit)->barcode()==barcodeIn || foundBC){ if (!foundBC){ foundBC = true; @@ -799,7 +791,7 @@ namespace Muon { pdgFinal = (*pit)->pdg_id(); } else { if( (*pit)->pdg_id() == pdgFinal ) { - const HepMC::GenParticle* pit_p = *pit; + auto pit_p = *pit; if ( (theFirst != pit_p) && ((*pit)->momentum().t()!=ePrev) ) ++scat; // if the particle has not changed pdgid after the first step count as scatter. also avoid counting pure interface changes as scatter } else { // the first time this particle appears --pit; @@ -820,8 +812,8 @@ namespace Muon { return thePair; } - const HepMC::GenParticle& MuonTrackTruthTool::getInitial( const TruthTrajectory& traj, const int barcodeIn ) const { - return *((getInitialPair( traj, barcodeIn )).first); + const HepMC::GenParticle* MuonTrackTruthTool::getInitial( const TruthTrajectory& traj, const int barcodeIn ) const { + return getInitialPair( traj, barcodeIn ).first; } unsigned int MuonTrackTruthTool::getNumberOfScatters( const TruthTrajectory& traj, const int barcodeIn ) const {