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
10namespace {
11 using SimHitVec_t = std::vector<const xAOD::MuonSimHit*>;
12 using MCPartSimMap_t = std::unordered_map<HepMC::ConstGenParticlePtr, SimHitVec_t>;
14}
15
16namespace MuonR4{
18 ATH_CHECK(m_idHelperSvc.retrieve());
19 ATH_CHECK(m_truthKey.initialize());
20 ATH_CHECK(m_hitDecorKey.initialize());
21 ATH_CHECK(m_simHitKey.initialize());
22 return StatusCode::SUCCESS;
23 }
24 StatusCode SimHitToTruthPartAssocAlg::execute(const EventContext& ctx) const {
25
26 const xAOD::MuonSimHitContainer* simHits{nullptr};
27 ATH_CHECK(SG::get(simHits, m_simHitKey, ctx));
28
29 MCPartSimMap_t hitIdMap{};
30 for (const xAOD::MuonSimHit* hit : *simHits) {
31 if (!hit->genParticleLink().isValid()){
32 continue;
33 }
34 const auto& pl{hit->genParticleLink()};
35 hitIdMap[pl].push_back(hit);
36 }
37
38 const xAOD::TruthParticleContainer* truthMuons{nullptr};
39 ATH_CHECK(SG::get(truthMuons,m_truthKey, ctx));
40 IdDecorHandle_t idDecorator{m_hitDecorKey, ctx};
41 for (const xAOD::TruthParticle* muon : *truthMuons) {
42 std::vector<unsigned long long>& hitIds{idDecorator(*muon)};
43 auto history = HepMC::simulation_history(muon, -1); // Returns a list of unique IDs
44 for (const auto& [pl, hits] : hitIdMap) {
45 const auto linkId = HepMC::uniqueID(pl);
46 if (std::ranges::any_of(history,[&linkId](const auto uniqueId){
47 return linkId == uniqueId;
48 })){
49 std::ranges::transform(hits, std::back_inserter(hitIds),
50 [](const xAOD::MuonSimHit* hit) {
51 return hit->identify().get_compact();
52 });
53 }
54 }
55 }
56 return StatusCode::SUCCESS;
57 }
58}
#define ATH_CHECK
Evaluate an expression and check for errors.
Handle class for reading from StoreGate.
Handle class for adding a decoration to an object.
value_type get_compact() const
Get the compact id.
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
Handle class for adding a decoration to an object.
Identifier identify() const
Returns the global ATLAS identifier of the SimHit.
int uniqueID(const T &p)
std::deque< 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.
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12
TruthParticle_v1 TruthParticle
Typedef to implementation.
MuonSimHitContainer_v1 MuonSimHitContainer
Define the version of the pixel cluster container.
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.