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 for (const xAOD::TruthParticle* muon : *truthMuons) {
44 std::vector<unsigned long long>& hitIds{idDecorator(*muon)};
45 auto history = HepMC::simulation_history(muon, -1); // Returns a list of unique IDs
46 for (const auto& [pl, hits] : hitIdMap) {
47 const auto linkId = HepMC::uniqueID(pl);
48 if (std::ranges::any_of(history,[&linkId](const auto uniqueId){
49 return linkId == uniqueId;
50 })){
51 std::ranges::transform(hits, std::back_inserter(hitIds),
52 [](const xAOD::MuonSimHit* hit) {
53 return hit->identify().get_compact();
54 });
55 }
56 }
57 }
58 return StatusCode::SUCCESS;
59 }
60}
#define ATH_CHECK
Evaluate an expression and check for errors.
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::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.
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.