ATLAS Offline Software
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 
8 #include "StoreGate/ReadHandle.h"
9 
10 namespace {
11  using SimHitVec_t = std::vector<const xAOD::MuonSimHit*>;
12  using MCPartSimMap_t = std::unordered_map<HepMC::ConstGenParticlePtr, SimHitVec_t>;
14 }
15 
16 namespace MuonR4{
18  ATH_CHECK(m_idHelperSvc.retrieve());
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 }
xAOD::MuonSimHit_v1
Definition: MuonSimHit_v1.h:18
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:196
TRTCalib_Extractor.hits
hits
Definition: TRTCalib_Extractor.py:35
xAOD::MuonSimHit_v1::identify
Identifier identify() const
Returns the global ATLAS identifier of the SimHit.
Definition: xAODMuonSimHit_V1.cxx:42
MuonR4::SimHitToTruthPartAssocAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
IdHelperSvc for Identifier printing / manipulation.
Definition: SimHitToTruthPartAssocAlg.h:29
MuonR4::SimHitToTruthPartAssocAlg::m_simHitKey
SG::ReadHandleKey< xAOD::MuonSimHitContainer > m_simHitKey
Data dependency on the sim hit container.
Definition: SimHitToTruthPartAssocAlg.h:36
MuonR4::SimHitToTruthPartAssocAlg::m_hitDecorKey
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_hitDecorKey
Decoration to the hit identifier vector.
Definition: SimHitToTruthPartAssocAlg.h:34
MuonR4::SimHitToTruthPartAssocAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: SimHitToTruthPartAssocAlg.cxx:24
SimHitToTruthPartAssocAlg.h
MuonR4::SimHitToTruthPartAssocAlg::m_truthKey
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthKey
Data dependency on the truth input container to decorate.
Definition: SimHitToTruthPartAssocAlg.h:32
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:283
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
MuonR4::SimHitToTruthPartAssocAlg::initialize
virtual StatusCode initialize() override final
Definition: SimHitToTruthPartAssocAlg.cxx:17
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
WriteDecorHandle.h
Handle class for adding a decoration to an object.
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:117
HepMC::simulation_history
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.
Definition: MagicNumbers.h:193
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
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
DataVector
Derived DataVector<T>.
Definition: DataVector.h:795
TruthVertex.h
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
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.
ReadHandle.h
Handle class for reading from StoreGate.