ATLAS Offline Software
Loading...
Searching...
No Matches
TrackToTruthPartAssocAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
11
12#include <unordered_set>
13namespace {
14 using IdSet_t = std::unordered_set<Identifier>;
16 using TruthPartWithIds_t = std::tuple<const xAOD::TruthParticle*, IdSet_t>;
18}
19
20namespace MuonR4{
22
23
24 ATH_CHECK(m_trkKey.initialize());
25 ATH_CHECK(m_originWriteKey.initialize());
26 ATH_CHECK(m_typeWriteKey.initialize());
28 ATH_CHECK(m_linkWriteKey.initialize());
29 ATH_CHECK(m_truthMuonKey.initialize());
30
31 ATH_CHECK(m_idHelperSvc.retrieve());
32 ATH_CHECK(m_edmHelperSvc.retrieve());
33
34 for (const std::string& hitIds : m_simHitIds) {
35 m_simHitKeys.emplace_back(m_truthMuonKey, hitIds);
36 }
37 ATH_CHECK(m_simHitKeys.initialize());
38 ATH_CHECK(m_truMuOriginKey.initialize());
39 ATH_CHECK(m_truMuTypeKey.initialize());
41 return StatusCode::SUCCESS;
42 }
43 StatusCode TrackToTruthPartAssocAlg::execute(const EventContext& ctx) const {
44
45 const xAOD::TrackParticleContainer* tracks{nullptr};
46 ATH_CHECK(SG::get(tracks, m_trkKey, ctx));
47
54 std::vector<IdDecorHandle_t> idDecorHandles{};
56 idDecorHandles.emplace_back(hitKey, ctx);
57 }
58
59 std::vector<TruthPartWithIds_t> truthWithIds{};
61 const xAOD::TruthParticleContainer* truthMuonCont{};
62 ATH_CHECK(SG::get(truthMuonCont, m_truthMuonKey, ctx));
63 for (const xAOD::TruthParticle* truthMuon : *truthMuonCont) {
64 IdSet_t assocIds{};
65 ATH_MSG_DEBUG("Truth muon: pT:"<<truthMuon->pt()<<" [GeV], eta: "<<truthMuon->eta()<<", phi: "
66 <<truthMuon->phi()<<", q: "<<truthMuon->charge()<<", truthType: "<<xAOD::TruthHelpers::getParticleTruthType(*truthMuon)
67 <<", origin: "<<xAOD::TruthHelpers::getParticleTruthOrigin(*truthMuon)
68 <<", classification: " <<xAOD::TruthHelpers::getParticleTruthClassification(*truthMuon));
69 for (const IdDecorHandle_t& hitDecor : idDecorHandles) {
70 std::ranges::transform(hitDecor(*truthMuon), std::inserter(assocIds, assocIds.begin()),
71 [this](unsigned long long rawId) {
72 const Identifier id{rawId};
73 ATH_MSG_VERBOSE(" --- associated hit id: "<<m_idHelperSvc->toString(id));
74 return id;
75 });
76 }
77 truthWithIds.emplace_back(std::make_tuple(truthMuon, std::move(assocIds)));
78 }
79
80 for (const xAOD::TrackParticle* trackPart : *tracks){
81 const Trk::Track* track = trackPart->track();
82 if (!track) {
83 ATH_MSG_ERROR("Associated reconstructed track is not available for "<<m_trkKey);
84 return StatusCode::FAILURE;
85 }
87 IdSet_t trackIds{};
88 for (const Trk::TrackStateOnSurface* tsos : *track->trackStateOnSurfaces()) {
89 const Trk::MeasurementBase* meas = tsos->measurementOnTrack();
91 if (!meas) {
92 continue;
93 }
94 const Identifier measId = m_edmHelperSvc->getIdentifier(*meas);
95 if (!measId.is_valid() || !m_idHelperSvc->isMuon(measId)){
96 ATH_MSG_VERBOSE("Measurement is not a muon one");
97 continue;
98 }
99 trackIds.insert(measId);
100 }
102 int bestMatchFrac{-1};
103 const xAOD::TruthParticle* bestMatch{nullptr};
104 for (const auto& [truthPart, truthIds]: truthWithIds) {
105 const int matchedReco = std::ranges::count_if(trackIds,[&truthIds](const Identifier& recoId){
106 return truthIds.count(recoId);
107 });
109 if (!matchedReco || matchedReco < bestMatchFrac){
110 continue;
111 }
112 bestMatchFrac = matchedReco;
113 bestMatch = truthPart;
114 }
116 if (!bestMatch) {
117 continue;
118 }
119 acc_truthOrigin(*trackPart) = xAOD::TruthHelpers::getParticleTruthOrigin(*bestMatch);
120 acc_truthType(*trackPart) = xAOD::TruthHelpers::getParticleTruthType(*bestMatch);
121 acc_truthLink(*trackPart) = TruthLink_t{truthMuonCont, bestMatch->index()};
122 acc_truthClassification(*trackPart) = xAOD::TruthHelpers::getParticleTruthClassification(*bestMatch);
123 }
124 return StatusCode::SUCCESS;
125 }
126}
#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)
Handle class for adding a decoration to an object.
ElementLink< xAOD::TruthParticleContainer > TruthLink_t
bool is_valid() const
Check if id is in a valid state.
TruthReadDecorKey_t m_truMuOriginKey
FIXME ReadDecorHandle should not be used to access dynamic variables applied by the algorithm which c...
TruthReadDecorKeyArr_t m_simHitKeys
Declaration of the dependency on the simHit decorations.
TrkWriteDecorKey_t m_originWriteKey
Decorations to be written to the TrackParticle truthOrigin/truthType/truthParticleLink.
ServiceHandle< Muon::IMuonEDMHelperSvc > m_edmHelperSvc
Helper service to handle the Identifiers of measurements.
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
IdHelperSvc to decode the Identifiers.
StatusCode execute(const EventContext &ctx) const override final
Gaudi::Property< std::vector< std::string > > m_simHitIds
List of simHit id decorations to read from the truth particle.
Property holding a SG store/key/clid/attr name from which a ReadDecorHandle is made.
Handle class for reading a decoration on an object.
Auxiliary class to instantiate WriteDecorHandles.The handles can be created in an empty state.
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.
int getParticleTruthClassification(const xAOD::IParticle &p)
Return the particle's truth classification (as defined by the MC Truth Classifier).
int getParticleTruthType(const xAOD::IParticle &p)
Return the particle's truth type (as defined by the MC Truth Classifier).
int getParticleTruthOrigin(const xAOD::IParticle &p)
Return the particle's truth origin (as defined by the MC Truth Classifier).
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TruthParticle_v1 TruthParticle
Typedef to implementation.
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.