ATLAS Offline Software
TrackParticleTruthAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
14 
15 
16 TrackParticleTruthAlg::TrackParticleTruthAlg(const std::string &name,ISvcLocator *pSvcLocator) :
17  AthAlgorithm(name,pSvcLocator),
18  m_particlesLinkKey("MuonSpectrometerTrackParticles"),
19  m_particlesTypeKey("MuonSpectrometerTrackParticles"),
20  m_particlesOriginKey("MuonSpectrometerTrackParticles"),
21  m_particleName("InDetTrackParticles"),
22  m_truthParticleLinkVecKey("xAODTruthLinks"),
23  m_truthTracksKey("TrackTruthCollection"),
24  m_truthClassifier("MCTruthClassifier/MCTruthClassifier")
25 {
26  declareProperty("ParticleLinkKey", m_particlesLinkKey);
27  declareProperty("ParticleTypeKey", m_particlesTypeKey);
28  declareProperty("ParticleOriginKey", m_particlesOriginKey);
29  declareProperty("MCTruthClassifier", m_truthClassifier);
30  declareProperty("TrackTruthName", m_truthTracksKey);
31  declareProperty("TrackParticleName", m_particleName);
32  declareProperty("xAODTruthLinkVector",m_truthParticleLinkVecKey);
33 }
34 
35 // -----------------------------------------------------------------------------------------------------
37 {
38  std::string keyName=m_particleName+".truthParticleLink";
41  m_particlesOriginKey=m_particleName+".truthOrigin";
47  ATH_CHECK(m_truthClassifier.retrieve());
48  return StatusCode::SUCCESS;
49 }
50 
51 // -----------------------------------------------------------------------------------------------------
53  return StatusCode::SUCCESS;
54 }
55 
56 // -----------------------------------------------------------------------------------------------------
58 
64 
65  if(!particlesLink.isPresent()){ //no track particle container of this name is in SG
66  ATH_MSG_WARNING("TrackParticleTruthAlg: "<<particlesLink.name()<<" not found");
67  return StatusCode::SUCCESS;
68  }
69 
70  if(!particlesLink.isValid()){
71  ATH_MSG_ERROR("Could not read " << particlesLink.name());
72  return StatusCode::FAILURE;
73  }
74 
75  if(!truthParticleLinkVec.isValid()){
76  ATH_MSG_ERROR("Could not read " << truthParticleLinkVec.name());
77  return StatusCode::FAILURE;
78  }
79 
80  if(!truthTracks.isValid()){
81  ATH_MSG_ERROR("Could not read " << truthTracks.name());
82  return StatusCode::FAILURE;
83  }
84 
85  const TrackTruthCollection truthTrackColl=*truthTracks;
86  const xAODTruthParticleLinkVector truthParticleLinks=*truthParticleLinkVec;
87 
88  int partInd=0;
89  for( const xAOD::TrackParticle* particle : *particlesLink ){
90 
91  ATH_MSG_DEBUG("Looking up truth for pt " << particle->pt() << " eta " << particle->eta() << " phi " << particle->phi());
94  if( !particle->trackLink().isValid()){
95  if (!particle->patternRecoInfo()[xAOD::STACO])
96  ATH_MSG_WARNING("Found TrackParticle with Invalid element link, skipping");
97  //add dummy truth link
99  particlesType(*particle) = 0;
100  particlesOrigin(*particle)= 0;
101  continue;
102  }
103 
104  MCTruthPartClassifier::ParticleType type = MCTruthPartClassifier::Unknown;
107  // look-up associated truth particle
108 
109  Trk::TrackTruthKey key(particle->trackLink());
110  auto result = truthTrackColl.find(key);
111 
112  // if we found a match use it
113  if( result != truthTrackColl.end() ){
114  ATH_MSG_VERBOSE("Found track Truth: uniqueID " << HepMC::uniqueID(&(result->second.particleLink())) << " evt " << result->second.particleLink().eventIndex());
115  link = truthParticleLinks.find(result->second.particleLink());
116 
117  // if configured also get truth classification
118 
119  if( link.isValid()&& !m_truthClassifier.empty() ){
120  auto truthClass = m_truthClassifier->particleTruthClassifier(*link);
121  type = truthClass.first;
122  origin = truthClass.second;
123  ATH_MSG_VERBOSE("Got truth type " << static_cast<int>(type) << " origin " << static_cast<int>(origin));
124  }
125  }
126 
127  if( link.isValid() ){
128  ATH_MSG_DEBUG("Found matching xAOD Truth: uniqueID " << HepMC::uniqueID(*link) << " pt " << (*link)->pt() << " eta " << (*link)->eta() << " phi " << (*link)->phi());
129  // set element link
130  link.toPersistent();
131  particlesLink(*particle)=link;
132 
133  }
134  else{ //no truth link, add a dummy
136  }
137 
138  if( !m_truthClassifier.empty() ){
139  //use index for these since it's the same particle
140  particlesType(partInd)=static_cast<int>(type);
141  particlesOrigin(partInd)=static_cast<int>(origin);
142  }
143  partInd++;
144  }
145  return StatusCode::SUCCESS;
146 }
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
get_generator_info.result
result
Definition: get_generator_info.py:21
Trk::TrackTruthKey
Definition: TrackTruthKey.h:22
AuxStoreAccessorMacros.h
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
SG::VarHandleBase::name
const std::string & name() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:75
xAODTruthParticleLinkVector::find
ElementLink< xAOD::TruthParticleContainer > find(const HepMcParticleLink &hepMCLink) const
Definition: xAODTruthParticleLink.h:28
TrackParticleTruthAlg::TrackParticleTruthAlg
TrackParticleTruthAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: TrackParticleTruthAlg.cxx:16
TrackParticleTruthAlg::m_particlesTypeKey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_particlesTypeKey
Definition: TrackParticleTruthAlg.h:29
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
TrackTruthKey.h
TrackParticleTruthAlg::m_truthTracksKey
SG::ReadHandleKey< TrackTruthCollection > m_truthTracksKey
Definition: TrackParticleTruthAlg.h:33
TrackParticleTruthAlg::m_particlesLinkKey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_particlesLinkKey
Definition: TrackParticleTruthAlg.h:28
TrackParticleTruthAlg::m_particlesOriginKey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_particlesOriginKey
Definition: TrackParticleTruthAlg.h:30
NonDefined
@ NonDefined
Definition: TruthClasses.h:52
TrackTruthCollection
Definition: TrackTruthCollection.h:21
IMCTruthClassifier.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
McEventCollection.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ParticleOrigin
ParticleOrigin
Definition: TruthClasses.h:51
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
TrackParticleTruthAlg::m_truthParticleLinkVecKey
SG::ReadHandleKey< xAODTruthParticleLinkVector > m_truthParticleLinkVecKey
TrackParticle input name.
Definition: TrackParticleTruthAlg.h:32
WriteDecorHandle.h
Handle class for adding a decoration to an object.
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:109
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
plotmaker.keyName
keyName
Definition: plotmaker.py:145
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
TrackParticleTruthAlg::execute
virtual StatusCode execute() override
Definition: TrackParticleTruthAlg.cxx:57
AthAlgorithm
Definition: AthAlgorithm.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
MagicNumbers.h
TrackParticleTruthAlg::finalize
virtual StatusCode finalize() override
Definition: TrackParticleTruthAlg.cxx:52
SG::WriteDecorHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
xAODTruthParticleLinkVector
Definition: xAODTruthParticleLink.h:26
TrackParticleTruthAlg.h
xAOD::STACO
@ STACO
Tracks produced by STACO.
Definition: TrackingPrimitives.h:99
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
TrackParticleTruthAlg::m_truthClassifier
ToolHandle< IMCTruthClassifier > m_truthClassifier
Definition: TrackParticleTruthAlg.h:36
TrackParticleTruthAlg::m_particleName
std::string m_particleName
Definition: TrackParticleTruthAlg.h:31
SG::WriteDecorHandle::isPresent
bool isPresent() const
Is the referenced container present in SG?
TrackParticleTruthAlg::initialize
virtual StatusCode initialize() override
Definition: TrackParticleTruthAlg.cxx:36
ParticleType
ParticleType
Definition: TruthClasses.h:8
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37