ATLAS Offline Software
Loading...
Searching...
No Matches
TruthSegConnectionAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
5
10
12using SegLinkVec_t = std::vector<SegLink_t>;
13
14namespace{
15 inline std::string print(const xAOD::MuonSegment& segment) {
16 static const SG::ConstAccessor<float> acc_pt{"pt"};
17 ServiceHandle<Muon::IMuonIdHelperSvc> idHelperSvc{"Muon::MuonIdHelperSvc/MuonIdHelperSvc", "MuonReadoutElement"};
18
19
20 std::stringstream sstr{};
21 sstr<<segment.chamberIndex()<<(segment.etaIndex() > 0 ? 'A' : 'C') << segment.sector()
22 <<" @"<<Amg::toString(segment.position())<<",dir: "<<Amg::toString(segment.direction())
23 <<", pT: "<<acc_pt(segment);
24 auto hitSet = MuonR4::getMatchingSimHits(segment);
25 sstr<<", hits: "<<hitSet.size()<<std::endl;
26 std::vector<const xAOD::MuonSimHit*> hits{hitSet.begin(), hitSet.end()};
27 std::ranges::sort(hits, [](const auto* a, const auto* b){ return a->identify() < b->identify(); });
28 for (const xAOD::MuonSimHit* hit : hits){
29 sstr<<" --- "<<idHelperSvc->toString(hit->identify())<<", pdgId:"<<hit->pdgId()
30 <<", e: "<<hit->kineticEnergy()<<", link: "<<hit->genParticleLink()<<std::endl;
31 }
32 return sstr.str();
33 }
34 inline std::string print(const std::vector<const xAOD::MuonSegment*>& segments) {
35 std::stringstream sstr{};
36 for (const xAOD::MuonSegment* seg : segments) {
37 sstr<<" **** "<<print(*seg)<<std::endl;
38 }
39 return sstr.str();
40 }
41}
42
43namespace MuonR4 {
45 ATH_CHECK(m_segmentKey.initialize());
46 ATH_CHECK(m_truthPartLinkKey.initialize());
47 ATH_CHECK(m_connectKey.initialize());
48 return StatusCode::SUCCESS;
49 }
50
51 StatusCode TruthSegConnectionAlg::execute(const EventContext& ctx) const {
52
53 const xAOD::MuonSegmentContainer* segments{nullptr};
54 ATH_CHECK(SG::get(segments, m_segmentKey, ctx));
55
57
58 std::map<int, std::vector<const xAOD::MuonSegment*>> bkgParticles{};
59 for (const xAOD::MuonSegment* segment : *segments) {
60 SegLinkVec_t& links{connectHandle(*segment)};
61
62 const xAOD::TruthParticle* truth = getTruthMatchedParticle(*segment);
63 ATH_MSG_VERBOSE(__func__<<"() "<<__LINE__<<" - Try to associate other truth segments to "<<
64 print(*segment));
65 if (truth == nullptr) {
66 const auto simHits = getMatchingSimHits(*segment);
67 ATH_CHECK(!simHits.empty());
68 const int trackId = (*simHits.begin())->genParticleLink().id();
69 ATH_MSG_VERBOSE(__func__<<"() "<<__LINE__<<" - No associated truth particle found. Rely on track id:"<<trackId);
70 bkgParticles[trackId].push_back(segment);
71 continue;
72 }
73 ATH_MSG_VERBOSE(__func__<<"() "<<__LINE__<<" - Found truth particle with "<<truth->pt()<<", eta: "<<truth->eta()
74 <<", phi: "<<truth->phi()<<", charge: "<<truth->charge());
75 for (const xAOD::MuonSegment* matched : getTruthSegments(*truth)) {
76 if(matched == segment) {
77 continue;
78 }
79 ATH_CHECK(matched->container() == segments);
80 links.emplace_back(*segments, matched->index());
81 }
82 ATH_MSG_VERBOSE("Associated "<<links.size()<<" other truth segments.");
83 }
85 for (const auto& [trackId, assocSegs] : bkgParticles){
86 ATH_MSG_VERBOSE("Associate to id: "<<trackId<<" corresponding to "
87 <<assocSegs.size()<<" segments\n"<<print(assocSegs));
88 for (const xAOD::MuonSegment* segment : assocSegs) {
89 SegLinkVec_t& links{connectHandle(*segment)};
90 for (const xAOD::MuonSegment* matched : assocSegs) {
91 if (matched == segment) {
92 continue;
93 }
94 ATH_CHECK(matched->container() == segments);
95 links.emplace_back(*segments, matched->index());
96 }
97 }
98
99 }
100 return StatusCode::SUCCESS;
101 }
102
103}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
static Double_t a
Handle class for adding a decoration to an object.
void print(char *figname, TCanvas *c1)
ElementLink< xAOD::MuonSegmentContainer > SegLink_t
std::vector< SegLink_t > SegLinkVec_t
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_segmentKey
Declare the input truth segment key.
virtual StatusCode execute(const EventContext &ctx) const override final
SG::ReadDecorHandleKey< xAOD::MuonSegmentContainer > m_truthPartLinkKey
Declare the dependency on the truth particle link.
virtual StatusCode initialize() override final
SG::WriteDecorHandleKey< xAOD::MuonSegmentContainer > m_connectKey
Declare the decoration written by the algorithm.
Helper class to provide constant type-safe access to aux data.
Handle class for adding a decoration to an object.
Amg::Vector3D direction() const
Returns the direction as Amg::Vector.
::Muon::MuonStationIndex::ChIndex chamberIndex() const
Returns the chamber index.
Amg::Vector3D position() const
Returns the position as Amg::Vector.
int etaIndex() const
Returns the eta index, which corresponds to stationEta in the offline identifiers (and the ).
virtual double pt() const override final
The transverse momentum ( ) of the particle.
double charge() const
Physical charge.
virtual double eta() const override final
The pseudorapidity ( ) of the particle.
virtual double phi() const override final
The azimuthal angle ( ) of the particle.
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
This header ties the generic definitions in this package.
const xAOD::TruthParticle * getTruthMatchedParticle(const xAOD::MuonSegment &segment)
Returns the particle truth-matched to the segment.
std::unordered_set< const xAOD::MuonSimHit * > getMatchingSimHits(const xAOD::MuonSegment &segment)
: Returns all sim hits matched to a xAOD::MuonSegment
std::vector< const xAOD::MuonSegment * > getTruthSegments(const xAOD::TruthParticle &truthMuon)
Returns the segments associated to the truth muon.
std::vector< SegLink_t > SegLinkVec_t
std::string print(const cont_t &container)
Print a space point container to string.
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
MuonSegmentContainer_v1 MuonSegmentContainer
Definition of the current "MuonSegment container version".
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12
TruthParticle_v1 TruthParticle
Typedef to implementation.
MuonSegment_v1 MuonSegment
Reference the current persistent version: