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 std::vector<const xAOD::MuonSimHit*> hits{MuonR4::getMatchingSimHits(segment)};
25 sstr<<", hits: "<<hits.size()<<std::endl;
26 for (const xAOD::MuonSimHit* hit : hits){
27 sstr<<" --- "<<idHelperSvc->toString(hit->identify())<<", pdgId:"<<hit->pdgId()
28 <<", e: "<<hit->kineticEnergy()<<", link: "<<hit->genParticleLink()<<std::endl;
29 }
30 return sstr.str();
31 }
32 inline std::string print(const std::vector<const xAOD::MuonSegment*>& segments) {
33 std::stringstream sstr{};
34 for (const xAOD::MuonSegment* seg : segments) {
35 sstr<<" **** "<<print(*seg)<<std::endl;
36 }
37 return sstr.str();
38 }
39}
40
41namespace MuonR4 {
43 ATH_CHECK(m_segmentKey.initialize());
44 ATH_CHECK(m_truthPartLinkKey.initialize());
45 ATH_CHECK(m_connectKey.initialize());
46 return StatusCode::SUCCESS;
47 }
48
49 StatusCode TruthSegConnectionAlg::execute(const EventContext& ctx) const {
50
51 const xAOD::MuonSegmentContainer* segments{nullptr};
52 ATH_CHECK(SG::get(segments, m_segmentKey, ctx));
53
55
56 std::map<int, std::vector<const xAOD::MuonSegment*>> bkgParticles{};
57 for (const xAOD::MuonSegment* segment : *segments) {
58 SegLinkVec_t& links{connectHandle(*segment)};
59
60 const xAOD::TruthParticle* truth = getTruthMatchedParticle(*segment);
61 ATH_MSG_VERBOSE(__func__<<"() "<<__LINE__<<" - Try to associate other truth segments to "<<
62 print(*segment));
63 if (truth == nullptr) {
64 const auto simHits = getMatchingSimHits(*segment);
65 ATH_CHECK(!simHits.empty());
66 const int trackId = simHits.front()->genParticleLink().id();
67 ATH_MSG_VERBOSE(__func__<<"() "<<__LINE__<<" - No associated truth particle found. Rely on track id:"<<trackId);
68 bkgParticles[trackId].push_back(segment);
69 continue;
70 }
71 ATH_MSG_VERBOSE(__func__<<"() "<<__LINE__<<" - Found truth particle with "<<truth->pt()<<", eta: "<<truth->eta()
72 <<", phi: "<<truth->phi()<<", charge: "<<truth->charge());
73 for (const xAOD::MuonSegment* matched : getTruthSegments(*truth)) {
74 if(matched == segment) {
75 continue;
76 }
77 ATH_CHECK(matched->container() == segments);
78 links.emplace_back(*segments, matched->index());
79 }
80 ATH_MSG_VERBOSE("Associated "<<links.size()<<" other truth segments.");
81 }
83 for (const auto& [trackId, assocSegs] : bkgParticles){
84 ATH_MSG_VERBOSE("Associate to id: "<<trackId<<" corresponding to "
85 <<assocSegs.size()<<" segments\n"<<print(assocSegs));
86 for (const xAOD::MuonSegment* segment : assocSegs) {
87 SegLinkVec_t& links{connectHandle(*segment)};
88 for (const xAOD::MuonSegment* matched : assocSegs) {
89 if (matched == segment) {
90 continue;
91 }
92 ATH_CHECK(matched->container() == segments);
93 links.emplace_back(*segments, matched->index());
94 }
95 }
96
97 }
98 return StatusCode::SUCCESS;
99 }
100
101}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x,...)
bool hit(const Container &ids, int pdgId)
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::vector< 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: