ATLAS Offline Software
Loading...
Searching...
No Matches
TruthMuonMakerAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "TruthMuonMakerAlg.h"
11
12namespace {
14}
15namespace Muon {
16
17 // Initialize method:
19 if (m_truthRecordKeys.empty()) {
20 ATH_MSG_ERROR("No truth container configured");
21 return StatusCode::FAILURE;
22 }
23 ATH_CHECK(m_truthRecordKeys.initialize());
24 ATH_CHECK(m_writeKey.initialize());
25 ATH_CHECK(m_truthOriginKey.initialize());
26 ATH_CHECK(m_truthTypeKey.initialize());
28 ATH_CHECK(m_truthLinkKey.initialize());
29 ATH_CHECK(m_idHelperSvc.retrieve());
30 ATH_CHECK(m_truthClassifier.retrieve());
31 return StatusCode::SUCCESS;
32 }
33
34 // Execute method:
35 StatusCode TruthMuonMakerAlg::execute(const EventContext& ctx) const {
36 // create output container
37 SG::WriteHandle muonTruthContainer(m_writeKey, ctx);
38 ATH_CHECK(muonTruthContainer.record(std::make_unique<xAOD::TruthParticleContainer>(),
39 std::make_unique<xAOD::TruthParticleAuxContainer>()));
40 ATH_MSG_DEBUG("Recorded TruthParticleContainer with key: " << m_writeKey);
41
46
47 // loop over truth coll
48 const xAOD::TruthParticleContainer* truthContainer{nullptr};
49
51 ATH_CHECK(SG::get(truthContainer,key, ctx));
52
53 for (const xAOD::TruthParticle* truth : *truthContainer) {
54 if (!MC::isStable(truth) || !m_pdgIds.value().count(truth->absPdgId()) || truth->pt() < m_pt) continue;
55 xAOD::TruthParticle* truthParticle = muonTruthContainer->push_back(std::make_unique<xAOD::TruthParticle>());
56 truthParticle->setPdgId(truth->pdgId());
57 truthParticle->setUid(HepMC::uniqueID(truth));
58 truthParticle->setStatus(truth->status());
59 truthParticle->setPx(truth->px());
60 truthParticle->setPy(truth->py());
61 truthParticle->setPz(truth->pz());
62 truthParticle->setE(truth->e());
63 truthParticle->setM(truth->m());
64 if (truth->hasProdVtx()) truthParticle->setProdVtxLink(truth->prodVtxLink());
65
66 TruthLink_t itruthLink(*truthContainer, truth->index());
67 itruthLink.toPersistent();
68 truthLink(*truthParticle) = itruthLink;
69 ATH_MSG_DEBUG("Found stable muon: " << truth->pt() << " eta " << truth->eta() << " phi " << truth->phi() << " mass "
70 << truth->m() << " unique ID " << HepMC::uniqueID(truth) << " HepMC::uniqueID(truthParticle) "
71 << HepMC::uniqueID(truthParticle) << " HepMC::uniqueID(*itruthLink) " << HepMC::uniqueID(*itruthLink) << " "
72 << itruthLink);
73 int iType{0}, iOrigin{0};
74 unsigned int iClassification{0};
75
76 // if configured look up truth classification
77 if (!m_truthClassifier.empty()) {
78 // if configured also get truth classification
81 m_truthClassifier->particleTruthClassifier(truth);
82 iType = truthClass.first;
83 iOrigin = truthClass.second;
84 iClassification = std::get<0>(MCTruthPartClassifier::defOrigOfParticle(truth)); // See AGENE-2351
85 ATH_MSG_VERBOSE("Got truth type " << iType << " origin " << iOrigin << " classification " << iClassification);
86 }
87 truthOrigin(*truthParticle) = iOrigin;
88 truthType(*truthParticle) = iType;
89 truthClassification(*truthParticle) = iClassification;
90
91 ATH_MSG_DEBUG("good muon with type " << iType << " origin" << iOrigin << " and classification " << iClassification);
92 }
93 }
94
95 ATH_MSG_DEBUG("Registered " << muonTruthContainer->size() << " truth muons ");
96
97 return StatusCode::SUCCESS;
98 }
99
100} // namespace Muon
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
ATLAS-specific HepMC functions.
Handle class for adding a decoration to an object.
ElementLink< xAOD::TruthParticleContainer > TruthLink_t
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_truthOriginKey
FIXME WriteDecorHandle should not be used for additional dynamic variables applied by the same algori...
Gaudi::Property< float > m_pt
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_truthClassificationKey
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
SG::ReadHandleKeyArray< xAOD::TruthParticleContainer > m_truthRecordKeys
ToolHandle< IMCTruthClassifier > m_truthClassifier
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_truthLinkKey
virtual StatusCode execute(const EventContext &ctx) const override
Gaudi::Property< std::set< int > > m_pdgIds
virtual StatusCode initialize() override
SG::WriteHandleKey< xAOD::TruthParticleContainer > m_writeKey
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_truthTypeKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Handle class for adding a decoration to an object.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
void setPy(float value)
Set the y component of the particle's momentum.
void setUid(int value)
Set unique ID.
void setM(float value)
Also store the mass.
void setE(float value)
Set the energy of the particle.
void setPz(float value)
Set the z component of the particle's momentum.
void setStatus(int value)
Set status code.
void setPdgId(int pid)
Set PDG ID code.
void setPx(float value)
Set the x component of the particle's momentum.
void setProdVtxLink(const ElementLink< TruthVertexContainer > &link)
Set the production vertex of the particle.
int uniqueID(const T &p)
std::tuple< unsigned int, T > defOrigOfParticle(T thePart)
bool isStable(const T &p)
Identify if the particle is stable, i.e. has not decayed.
NRpcCablingAlg reads raw condition data and writes derived condition data to the condition store.
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
TruthParticle_v1 TruthParticle
Typedef to implementation.
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.