ATLAS Offline Software
Loading...
Searching...
No Matches
SimHitToTruthPartAssocAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
5
9
11
12namespace {
13 using SimHitVec_t = std::vector<const xAOD::MuonSimHit*>;
14 using MCPartSimMap_t = std::unordered_map<HepMC::ConstGenParticlePtr, SimHitVec_t>;
16}
17
18namespace MuonR4{
20 ATH_CHECK(m_idHelperSvc.retrieve());
21 ATH_CHECK(m_truthKey.initialize());
22 ATH_CHECK(m_hitDecorKey.initialize());
23 ATH_CHECK(m_simHitKey.initialize());
24 return StatusCode::SUCCESS;
25 }
26 StatusCode SimHitToTruthPartAssocAlg::execute(const EventContext& ctx) const {
27
28 const xAOD::MuonSimHitContainer* simHits{nullptr};
29 ATH_CHECK(SG::get(simHits, m_simHitKey, ctx));
30
31 MCPartSimMap_t hitIdMap{};
32 for (const xAOD::MuonSimHit* hit : *simHits) {
33 if (!hit->genParticleLink().isValid()){
34 continue;
35 }
36 const auto& pl{hit->genParticleLink()};
37 hitIdMap[pl].push_back(hit);
38 }
39
40 const xAOD::TruthParticleContainer* truthMuons{nullptr};
41 ATH_CHECK(SG::get(truthMuons,m_truthKey, ctx));
42 IdDecorHandle_t idDecorator{m_hitDecorKey, ctx};
43 if (hitIdMap.empty()) {
44 ATH_MSG_DEBUG(__func__<<"() "<<__LINE__<<" - Empty hits "<<m_hitDecorKey.fullKey());
45 return StatusCode::SUCCESS;
46 }
47 for (const xAOD::TruthParticle* muon : *truthMuons) {
48 std::vector<unsigned long long>& hitIds{idDecorator(*muon)};
49 auto history = HepMC::simulation_history(muon, 1); // Returns a list of unique IDs
50 ATH_MSG_DEBUG(__func__<<"() "<<__LINE__<<" - muon: pT: "<<(muon->pt() *1.e-3)
51 <<", eta: "<<muon->eta()<<", phi: "<<muon->phi()<<", q: "<<muon->charge()
52 <<", uuid: "<<HepMC::uniqueID(muon)<<", history: "<<history);
53
54 for (const auto& [pl, hits] : hitIdMap) {
55 const auto linkId = HepMC::uniqueID(pl);
56 if (std::ranges::any_of(history,[&linkId](const auto uniqueId){
57 return linkId == uniqueId;
58 })){
59 std::ranges::transform(hits, std::back_inserter(hitIds),
60 [&](const xAOD::MuonSimHit* hit) {
61 ATH_MSG_VERBOSE(__func__<<"() "<<__LINE__<<" - Associate hit: "<<m_idHelperSvc->toString(hit->identify())
62 <<", uuid: "<<linkId);
63 return hit->identify().get_compact();
64 });
65 }
66 }
67 }
68 return StatusCode::SUCCESS;
69 }
70}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_VERBOSE(x,...)
bool hit(const Container &ids, int pdgId)
Handle class for reading from StoreGate.
Handle class for adding a decoration to an object.
virtual StatusCode initialize() override final
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_hitDecorKey
Decoration to the hit identifier vector.
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthKey
Data dependency on the truth input container to decorate.
SG::ReadHandleKey< xAOD::MuonSimHitContainer > m_simHitKey
Data dependency on the sim hit container.
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
IdHelperSvc for Identifier printing / manipulation.
virtual StatusCode execute(const EventContext &ctx) const override final
Auxiliary class to instantiate WriteDecorHandles.The handles can be created in an empty state.
int uniqueID(const T &p)
std::vector< int > simulation_history(const T &p, const int direction)
Function to calculate all the descendants(direction=1)/ancestors(direction=-1) of the particle.
This header ties the generic definitions in this package.
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
MuonSimHitContainer_v1 MuonSimHitContainer
Define the version of the pixel cluster container.
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12
TruthParticle_v1 TruthParticle
Typedef to implementation.
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.