ATLAS Offline Software
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 
10 
11 #include <unordered_set>
12 namespace {
13  using IdSet_t = std::unordered_set<Identifier>;
15  using TruthPartWithIds_t = std::tuple<const xAOD::TruthParticle*, IdSet_t>;
17 }
18 
19 namespace MuonR4{
21 
22 
28 
29  ATH_CHECK(m_idHelperSvc.retrieve());
30  ATH_CHECK(m_edmHelperSvc.retrieve());
31 
32  for (const std::string& hitIds : m_simHitIds) {
33  m_simHitKeys.emplace_back(m_truthMuonKey, hitIds);
34  }
38  return StatusCode::SUCCESS;
39  }
40  StatusCode TrackToTruthPartAssocAlg::execute(const EventContext& ctx) const {
41 
42  const xAOD::TrackParticleContainer* tracks{nullptr};
43  ATH_CHECK(SG::get(tracks, m_trkKey, ctx));
44 
50  std::vector<IdDecorHandle_t> idDecorHandles{};
52  idDecorHandles.emplace_back(hitKey, ctx);
53  }
54 
55  std::vector<TruthPartWithIds_t> truthWithIds{};
57  const xAOD::TruthParticleContainer* truthMuonCont{nullptr};
58  ATH_CHECK(SG::get(truthMuonCont, m_truthMuonKey, ctx));
59  for (const xAOD::TruthParticle* truthMuon : *truthMuonCont) {
60  IdSet_t assocIds{};
61  ATH_MSG_DEBUG("Truth muon: pT:"<<truthMuon->pt()<<" [GeV], eta: "<<truthMuon->eta()<<", phi: "
62  <<truthMuon->phi()<<", q: "<<truthMuon->charge()<<", truthType: "<<xAOD::TruthHelpers::getParticleTruthType(*truthMuon)
64  for (const IdDecorHandle_t& hitDecor : idDecorHandles) {
65  std::ranges::transform(hitDecor(*truthMuon), std::inserter(assocIds, assocIds.begin()),
66  [this](unsigned long long rawId) {
67  const Identifier id{rawId};
68  ATH_MSG_VERBOSE(" --- associated hit id: "<<m_idHelperSvc->toString(id));
69  return id;
70  });
71  }
72  truthWithIds.emplace_back(std::make_tuple(truthMuon, std::move(assocIds)));
73  }
74 
75  for (const xAOD::TrackParticle* trackPart : *tracks){
76  const Trk::Track* track = trackPart->track();
77  if (!track) {
78  ATH_MSG_ERROR("Associated reconstructed track is not available for "<<m_trkKey);
79  return StatusCode::FAILURE;
80  }
82  IdSet_t trackIds{};
83  for (const Trk::TrackStateOnSurface* tsos : *track->trackStateOnSurfaces()) {
84  const Trk::MeasurementBase* meas = tsos->measurementOnTrack();
86  if (!meas) {
87  continue;
88  }
89  const Identifier measId = m_edmHelperSvc->getIdentifier(*meas);
90  if (!measId.is_valid() || !m_idHelperSvc->isMuon(measId)){
91  ATH_MSG_VERBOSE("Measurement is not a muon one");
92  continue;
93  }
94  trackIds.insert(measId);
95  }
97  int bestMatchFrac{-1};
98  const xAOD::TruthParticle* bestMatch{nullptr};
99  for (const auto& [truthPart, truthIds]: truthWithIds) {
100  const int matchedReco = std::ranges::count_if(trackIds,[&truthIds](const Identifier& recoId){
101  return truthIds.count(recoId);
102  });
104  if (!matchedReco || matchedReco < bestMatchFrac){
105  continue;
106  }
107  bestMatchFrac = matchedReco;
108  bestMatch = truthPart;
109  }
111  if (!bestMatch) {
112  continue;
113  }
114  acc_truthOrigin(*trackPart) = xAOD::TruthHelpers::getParticleTruthOrigin(*bestMatch);
115  acc_truthType(*trackPart) = xAOD::TruthHelpers::getParticleTruthType(*bestMatch);
116  acc_truthLink(*trackPart) = TruthLink_t{truthMuonCont, bestMatch->index()};
117  }
118  return StatusCode::SUCCESS;
119  }
120 }
MuonR4::TrackToTruthPartAssocAlg::m_truMuOriginKey
TruthDecor_t m_truMuOriginKey
Definition: TrackToTruthPartAssocAlg.h:57
MuonR4::TrackToTruthPartAssocAlg::m_originWriteKey
TrkWriteDecorKey_t m_originWriteKey
Decorations to be written to the TrackParticle truthOrigin/truthType/truthParticleLink.
Definition: TrackToTruthPartAssocAlg.h:42
MuonR4::TrackToTruthPartAssocAlg::m_truthMuonKey
TruthReadKey_t m_truthMuonKey
Definition: TrackToTruthPartAssocAlg.h:50
Trk::Track
The ATLAS Track class.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/Track.h:73
MuonR4::TrackToTruthPartAssocAlg::m_truMuTypeKey
TruthDecor_t m_truMuTypeKey
Definition: TrackToTruthPartAssocAlg.h:58
TrackToTruthPartAssocAlg.h
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
MuonR4::TrackToTruthPartAssocAlg::m_edmHelperSvc
ServiceHandle< Muon::IMuonEDMHelperSvc > m_edmHelperSvc
Helper service to handle the Identifiers of measurements.
Definition: TrackToTruthPartAssocAlg.h:34
xAODTruthHelpers.h
Identifier::is_valid
bool is_valid() const
Check if id is in a valid state.
MuonR4::TrackToTruthPartAssocAlg::m_typeWriteKey
TrkWriteDecorKey_t m_typeWriteKey
Definition: TrackToTruthPartAssocAlg.h:43
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
SG::ReadDecorHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:287
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
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
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:794
Trk::MeasurementBase
Definition: MeasurementBase.h:58
Trk::TrackStateOnSurface
represents the track state (measurement, material, fit parameters and quality) at a surface.
Definition: TrackStateOnSurface.h:71
SG::VarHandleKeyArrayCommon::initialize
StatusCode initialize(bool used=true)
forward the initialization to the member VarHandleKeys
MuonR4::TrackToTruthPartAssocAlg::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: TrackToTruthPartAssocAlg.cxx:40
MuonR4::TrackToTruthPartAssocAlg::m_linkWriteKey
TrkWriteDecorKey_t m_linkWriteKey
Definition: TrackToTruthPartAssocAlg.h:44
MuonR4::TrackToTruthPartAssocAlg::m_simHitKeys
TruthDecorArr_t m_simHitKeys
Declaration of the dependency on the simHit decorations.
Definition: TrackToTruthPartAssocAlg.h:55
MuonR4::TrackToTruthPartAssocAlg::initialize
StatusCode initialize() override final
Definition: TrackToTruthPartAssocAlg.cxx:20
xAOD::TruthHelpers::getParticleTruthOrigin
int getParticleTruthOrigin(const xAOD::IParticle &p)
Return the particle's truth origin (as defined by the MC Truth Classifier)
Definition: xAODTruthHelpers.cxx:109
python.TruthMuonD3PDObject.truthMuon
truthMuon
Definition: TruthMuonD3PDObject.py:51
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
MuonR4::TrackToTruthPartAssocAlg::m_trkKey
TrkReadKey_t m_trkKey
Definition: TrackToTruthPartAssocAlg.h:40
MuonR4::TrackToTruthPartAssocAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
IdHelperSvc to decode the Identifiers.
Definition: TrackToTruthPartAssocAlg.h:32
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.
xAOD::track
@ track
Definition: TrackingPrimitives.h:513
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
MuonR4::TrackToTruthPartAssocAlg::m_simHitIds
Gaudi::Property< std::vector< std::string > > m_simHitIds
List of simHit id decorations to read from the truth particle.
Definition: TrackToTruthPartAssocAlg.h:53
xAOD::TruthHelpers::getParticleTruthType
int getParticleTruthType(const xAOD::IParticle &p)
Return the particle's truth type (as defined by the MC Truth Classifier)
Definition: xAODTruthHelpers.cxx:91
SG::ReadDecorHandleKey< xAOD::TruthParticleContainer >
ReadDecorHandleKeyArray.h
Identifier
Definition: IdentifierFieldParser.cxx:14