ATLAS Offline Software
Loading...
Searching...
No Matches
MuonCandidateTool.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// MuonCandidateTool
7// AlgTool performing pre-selection on MS tracks, extrapolation and creation
8// of MuonCandidate collection.
9//
11
12#include "MuonCandidateTool.h"
13
15
16namespace {
17 // Temporary collection for extrapolated tracks and links with correspondent MS tracks
18 struct track_link {
19 std::unique_ptr<Trk::Track> track{};
20 unsigned int container_index{0};
21 bool extp_succeed{false};
22 track_link(std::unique_ptr<Trk::Track> trk, unsigned int idx, bool succeed) :
23 track{std::move(trk)}, container_index{idx}, extp_succeed{succeed} {}
24 };
25} // namespace
26
27namespace MuonCombined {
29 ATH_CHECK(m_printer.retrieve());
30 ATH_CHECK(m_trackBuilder.retrieve(EnableTool{!m_trackBuilder.empty()}));
31 ATH_CHECK(m_trackExtrapolationTool.retrieve(EnableTool{!m_trackExtrapolationTool.empty()}));
33 ATH_CHECK(m_trackSummaryTool.retrieve());
34 ATH_CHECK(m_idHelperSvc.retrieve());
35 ATH_CHECK(m_beamSpotKey.initialize());
36
37 ATH_CHECK(m_segmentKey.initialize(!m_segmentKey.empty()));
38 ATH_CHECK(m_trackSegmentAssociationTool.retrieve(EnableTool{!m_segmentKey.empty()}));
39 return StatusCode::SUCCESS;
40 }
42 TrackCollection& outputTracks, const EventContext& ctx) const {
43 ATH_MSG_DEBUG("Producing MuonCandidates for " << tracks.size());
44 unsigned int ntracks = 0;
45
47 if (!beamSpotHandle.isValid()) {
48 ATH_MSG_ERROR("Could not retrieve the BeamSpot data from key " << m_beamSpotKey.objKey());
49 return;
50 }
51
52 ATH_MSG_DEBUG("Beamspot position bs_x=" << beamSpotHandle->beamPos());
53
54 std::vector<track_link> trackLinks;
55
56 unsigned int index = -1;
57 // Loop over MS tracks
58 for (const auto* track : tracks) {
59 ++index;
60
61 if (!track->trackLink().isValid() || !track->track()) {
62 ATH_MSG_WARNING("MuonStandalone track particle without Trk::Track");
63 continue;
64 }
65 const Trk::Track& msTrack = *track->track();
66
67 ATH_MSG_VERBOSE("Re-Fitting track " << std::endl
68 << m_printer->print(msTrack) << std::endl
69 << m_printer->printStations(msTrack));
70 std::unique_ptr<Trk::Track> standaloneTrack;
71 if (m_extrapolationStrategy == 0u) {
72 standaloneTrack = m_trackBuilder->standaloneFit(ctx, msTrack, beamSpotHandle->beamPos(), nullptr);
73 } else {
74 standaloneTrack = m_trackExtrapolationTool->extrapolate(msTrack, ctx);
75 }
76 if (standaloneTrack) {
77 // Reject the track if its fit quality is much (much much) worse than that of the non-extrapolated track
78 if (standaloneTrack->fitQuality()->doubleNumberDoF() == 0) {
79 standaloneTrack.reset();
80 ATH_MSG_DEBUG("extrapolated track has no DOF, don't use it");
81 } else {
82 double mschi2 = 2.5; // a default we should hopefully never have to use (taken from CombinedMuonTrackBuilder)
83 if (msTrack.fitQuality()->doubleNumberDoF() > 0)
84 mschi2 = msTrack.fitQuality()->chiSquared() / msTrack.fitQuality()->doubleNumberDoF();
85 // choice of 1000 is slightly arbitrary, the point is that the fit should be really be terrible
86 if (standaloneTrack->fitQuality()->chiSquared() / standaloneTrack->fitQuality()->doubleNumberDoF() > 1000 * mschi2) {
87 standaloneTrack.reset();
88 ATH_MSG_DEBUG("extrapolated track has a degraded fit, don't use it");
89 }
90 }
91 }
92 if (standaloneTrack) {
93 standaloneTrack->info().setParticleHypothesis(Trk::muon);
94 standaloneTrack->info().setPatternRecognitionInfo(Trk::TrackInfo::MuidStandAlone);
95 ATH_MSG_VERBOSE("Extrapolated track " << std::endl
96 << m_printer->print(*standaloneTrack) << std::endl
97 << m_printer->printStations(*standaloneTrack));
98 ++ntracks;
99 if (!standaloneTrack->perigeeParameters())
100 ATH_MSG_WARNING(" Track without perigee " << (*standaloneTrack));
101 else if (!standaloneTrack->perigeeParameters()->covariance())
102 ATH_MSG_WARNING(" Track with perigee without covariance " << (*standaloneTrack));
103 trackLinks.emplace_back(std::move(standaloneTrack), index, true);
104 } else {
105 // We can create tracks from EM segments+TGC hits
106 // If these are not successfully extrapolated, they are too low quality to be useful
107 // So only make candidates from un-extrapolated tracks if they are not EM-only
108 bool skipTrack = true;
109 const Trk::MuonTrackSummary* msMuonTrackSummary = nullptr;
110 std::unique_ptr<Trk::TrackSummary> msTrackSummary;
111 // If reading from an ESD, the track will not have a track summary yet
112 if (!msTrack.trackSummary()) {
113 msTrackSummary = m_trackSummaryTool->summary(msTrack);
114 msMuonTrackSummary = msTrackSummary->muonTrackSummary();
115 } else
116 msMuonTrackSummary = msTrack.trackSummary()->muonTrackSummary();
117 for (const auto& chs : msMuonTrackSummary->chamberHitSummary()) {
118 using namespace Muon::MuonStationIndex;
119 if ((chs.isMdt() && m_idHelperSvc->stationIndex(chs.chamberId()) != StIndex::EM) ||
120 m_idHelperSvc->isCsc(chs.chamberId())) {
121 skipTrack = false;
122 break;
123 }
124 }
125 if (!skipTrack) { trackLinks.emplace_back(std::make_unique<Trk::Track>(msTrack), index, false); }
126 }
127 }
129 std::unique_ptr<TrackCollection> extrapTracks = std::make_unique<TrackCollection>(SG::VIEW_ELEMENTS);
130 extrapTracks->reserve(trackLinks.size());
131 for (const track_link& link : trackLinks) extrapTracks->push_back(link.track.get());
132 ATH_MSG_DEBUG("Finished back-tracking, total number of successfull fits " << ntracks);
133
134 // Resolve ambiguity between extrapolated tracks (where available)
135 std::unique_ptr<const TrackCollection> resolvedTracks(m_ambiguityProcessor->process(extrapTracks.get()));
136
137 ATH_MSG_DEBUG("Finished ambiguity solving: " << extrapTracks->size() << " track(s) in -> " << resolvedTracks->size()
138 << " track(s) out");
139
140 const Trk::SegmentCollection* segments{nullptr};
141 if (!m_segmentKey.empty()) {
143 if (!readHandle.isValid()) {
144 ATH_MSG_WARNING("Failed to retrieve the segment container " << m_segmentKey.fullKey());
145 } else
146 segments = readHandle.cptr();
147 }
148
149 // Loop over resolved tracks and build MuonCondidate collection
150 for (const Trk::Track* track : *resolvedTracks) {
151 std::vector<track_link>::iterator tLink =
152 std::find_if(trackLinks.begin(), trackLinks.end(), [&track](const track_link& link) { return link.track.get() == track; });
153
154 if (tLink == trackLinks.end()) {
155 ATH_MSG_WARNING("Unable to find internal link between MS and SA tracks!");
156 continue;
157 }
158
159 std::unique_ptr<MuonCandidate> muon_candidate;
160 ElementLink<xAOD::TrackParticleContainer> MS_TrkLink{tracks, tLink->container_index, ctx};
161 if (tLink->extp_succeed) {
162 outputTracks.push_back(std::move(tLink->track));
163 ElementLink<TrackCollection> saLink(outputTracks, outputTracks.size() - 1, ctx);
164 muon_candidate = std::make_unique<MuonCandidate>(MS_TrkLink, saLink, outputTracks.size() - 1);
165 // remove track from set so it is not deleted
166 } else {
167 // in this case the extrapolation failed
168 muon_candidate = std::make_unique<MuonCandidate>(MS_TrkLink);
169 }
170 muon_candidate->setCommissioning(m_commissioning);
172 if (segments) {
173 std::vector<const Muon::MuonSegment*> assoc_segs;
174 m_trackSegmentAssociationTool->associatedSegments(*muon_candidate->primaryTrack(), segments, assoc_segs);
175 muon_candidate->setSegments(std::move(assoc_segs));
176 }
177
178 outputCollection.push_back(std::move(muon_candidate));
179 }
180 }
181
182} // namespace MuonCombined
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
DataVector< MuonCombined::MuonCandidate > MuonCandidateCollection
This typedef represents a collection of MuonCandidate objects.
DataVector< Trk::Track > TrackCollection
This typedef represents a collection of Trk::Track objects.
value_type push_back(value_type pElem)
Add an element to the end of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
virtual StatusCode initialize() override
ToolHandle< Trk::ITrackAmbiguityProcessorTool > m_ambiguityProcessor
ToolHandle< Trk::IExtendedTrackSummaryTool > m_trackSummaryTool
ToolHandle< Muon::IMuonTrackExtrapolationTool > m_trackExtrapolationTool
virtual void create(const xAOD::TrackParticleContainer &tracks, MuonCandidateCollection &outputCollection, TrackCollection &outputTracks, const EventContext &ctx) const override
IMuonCandidateTool interface: build a MuonCandidateCollection from a TrackCollection of spectrometer ...
PublicToolHandle< MuonCombined::IMuonTrackToSegmentAssociationTool > m_trackSegmentAssociationTool
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
PublicToolHandle< Muon::MuonEDMPrinterTool > m_printer
SG::ReadHandleKey< Trk::SegmentCollection > m_segmentKey
Retrieve the segment container to perform the segment association offline.
SG::ReadCondHandleKey< InDet::BeamSpotData > m_beamSpotKey
ToolHandle< Rec::ICombinedMuonTrackBuilder > m_trackBuilder
Gaudi::Property< unsigned int > m_extrapolationStrategy
Gaudi::Property< bool > m_commissioning
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const_pointer_type cptr()
Dereference the pointer.
double chiSquared() const
returns the of the overall track fit
Definition FitQuality.h:56
double doubleNumberDoF() const
returns the number of degrees of freedom of the overall track or vertex fit as double
Definition FitQuality.h:68
Detailed track summary for the muon system Give access to hit counts per chamber.
const std::vector< ChamberHitSummary > & chamberHitSummary() const
access to the vector of chamber hit summaries on the track
const MuonTrackSummary * muonTrackSummary() const
returns a pointer to the MuonTrackSummary if available
const Trk::TrackSummary * trackSummary() const
Returns a pointer to the const Trk::TrackSummary owned by this const track (could be nullptr)
const FitQuality * fitQuality() const
return a pointer to the fit quality const-overload
The MuonTagToSegMap is an auxillary construct that links the MuonSegments associated with a combined ...
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
DataVector< Trk::Segment > SegmentCollection
Definition index.py:1
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".