ATLAS Offline Software
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 
16 namespace {
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 
27 namespace 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()}));
32  ATH_CHECK(m_ambiguityProcessor.retrieve());
33  ATH_CHECK(m_trackSummaryTool.retrieve());
34  ATH_CHECK(m_idHelperSvc.retrieve());
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);
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) {
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
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
MuonCombined::MuonCandidateTool::m_commissioning
Gaudi::Property< bool > m_commissioning
Definition: MuonCandidateTool.h:56
Trk::Track::fitQuality
const FitQuality * fitQuality() const
return a pointer to the fit quality const-overload
MuonCombined::MuonCandidateTool::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MuonCandidateTool.h:45
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
MuonTrackSummary.h
Trk::Track
The ATLAS Track class.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/Track.h:73
Muon::MuonStationIndex
Definition: MuonStationIndex.h:13
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
index
Definition: index.py:1
MuonCandidateTool.h
Trk::Track::info
const TrackInfo & info() const
Returns a const ref to info of a const tracks.
MuonCombined::MuonCandidateTool::m_trackBuilder
ToolHandle< Rec::ICombinedMuonTrackBuilder > m_trackBuilder
Definition: MuonCandidateTool.h:38
Trk::TrackInfo::MuidStandAlone
@ MuidStandAlone
MuidStandalone.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/TrackInfo.h:165
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
MuonCombined::MuonCandidateTool::initialize
virtual StatusCode initialize() override
Definition: MuonCandidateTool.cxx:28
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
Trk::MuonTrackSummary::chamberHitSummary
const std::vector< ChamberHitSummary > & chamberHitSummary() const
access to the vector of chamber hit summaries on the track
Definition: MuonTrackSummary.h:148
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Ringer::EM
@ EM
Definition: CaloRingsDefs.h:19
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
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::MuonTrackSummary
Detailed track summary for the muon system Give access to hit counts per chamber.
Definition: MuonTrackSummary.h:26
Trk::muon
@ muon
Definition: ParticleHypothesis.h:31
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
postInclude.outputCollection
outputCollection
Definition: postInclude.SortInput.py:27
Trk::Track::perigeeParameters
const Perigee * perigeeParameters() const
return Perigee.
Definition: Tracking/TrkEvent/TrkTrack/src/Track.cxx:163
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
MuonCombined::MuonCandidateTool::m_trackSegmentAssociationTool
PublicToolHandle< MuonCombined::IMuonTrackToSegmentAssociationTool > m_trackSegmentAssociationTool
Definition: MuonCandidateTool.h:51
MuonCombined::MuonCandidateTool::m_extrapolationStrategy
Gaudi::Property< unsigned int > m_extrapolationStrategy
Definition: MuonCandidateTool.h:54
DeMoScan.index
string index
Definition: DeMoScan.py:362
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Trk::Track::trackSummary
const Trk::TrackSummary * trackSummary() const
Returns a pointer to the const Trk::TrackSummary owned by this const track (could be nullptr)
MuonCombined
The MuonTagToSegMap is an auxillary construct that links the MuonSegments associated with a combined ...
Definition: IMuonSystemExtensionTool.h:23
MuonCombined::MuonCandidateTool::m_trackExtrapolationTool
ToolHandle< Muon::IMuonTrackExtrapolationTool > m_trackExtrapolationTool
Definition: MuonCandidateTool.h:39
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
Trk::TrackInfo::setParticleHypothesis
void setParticleHypothesis(const ParticleHypothesis &hypothesis)
Method re-setting the ParticleHypothesis.
Trk::FitQuality::chiSquared
double chiSquared() const
returns the of the overall track fit
Definition: FitQuality.h:56
xAOD::track
@ track
Definition: TrackingPrimitives.h:513
Trk::TrackInfo::setPatternRecognitionInfo
void setPatternRecognitionInfo(const TrackPatternRecoInfo &patternReco)
Method setting the pattern recognition algorithm.
Trk::TrackSummary::muonTrackSummary
const MuonTrackSummary * muonTrackSummary() const
returns a pointer to the MuonTrackSummary if available
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
MuonCombined::MuonCandidateTool::m_segmentKey
SG::ReadHandleKey< Trk::SegmentCollection > m_segmentKey
Retrieve the segment container to perform the segment association offline.
Definition: MuonCandidateTool.h:50
MuonCombined::MuonCandidateTool::m_printer
PublicToolHandle< Muon::MuonEDMPrinterTool > m_printer
Definition: MuonCandidateTool.h:37
MuonCombined::MuonCandidateTool::m_beamSpotKey
SG::ReadCondHandleKey< InDet::BeamSpotData > m_beamSpotKey
Definition: MuonCandidateTool.h:47
MuonCombined::MuonCandidateTool::m_ambiguityProcessor
ToolHandle< Trk::ITrackAmbiguityProcessorTool > m_ambiguityProcessor
Definition: MuonCandidateTool.h:41
Trk::FitQuality::doubleNumberDoF
double doubleNumberDoF() const
returns the number of degrees of freedom of the overall track or vertex fit as double
Definition: FitQuality.h:68
MuonCombined::MuonCandidateTool::m_trackSummaryTool
ToolHandle< Trk::IExtendedTrackSummaryTool > m_trackSummaryTool
Definition: MuonCandidateTool.h:43
MuonCombined::MuonCandidateTool::create
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 ...
Definition: MuonCandidateTool.cxx:41