ATLAS Offline Software
MuonCandidateTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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 {
28 
29  MuonCandidateTool::MuonCandidateTool(const std::string& type, const std::string& name, const IInterface* parent) :
31  declareInterface<IMuonCandidateTool>(this);
32  }
33 
35  ATH_CHECK(m_printer.retrieve());
36  ATH_CHECK(m_trackBuilder.retrieve(EnableTool{!m_trackBuilder.empty()}));
37  ATH_CHECK(m_trackExtrapolationTool.retrieve(EnableTool{!m_trackExtrapolationTool.empty()}));
38  ATH_CHECK(m_ambiguityProcessor.retrieve());
39  ATH_CHECK(m_trackSummaryTool.retrieve());
40  ATH_CHECK(m_idHelperSvc.retrieve());
42 
43  ATH_CHECK(m_segmentKey.initialize(!m_segmentKey.empty()));
44  ATH_CHECK(m_trackSegmentAssociationTool.retrieve(EnableTool{!m_segmentKey.empty()}));
45  return StatusCode::SUCCESS;
46  }
48  TrackCollection& outputTracks, const EventContext& ctx) const {
49  ATH_MSG_DEBUG("Producing MuonCandidates for " << tracks.size());
50  unsigned int ntracks = 0;
51 
53  if (!beamSpotHandle.isValid()) {
54  ATH_MSG_ERROR("Could not retrieve the BeamSpot data from key " << m_beamSpotKey.objKey());
55  return;
56  }
57 
58  ATH_MSG_DEBUG("Beamspot position bs_x=" << beamSpotHandle->beamPos());
59 
60  std::vector<track_link> trackLinks;
61 
62  unsigned int index = -1;
63  // Loop over MS tracks
64  for (const auto* track : tracks) {
65  ++index;
66 
67  if (!track->trackLink().isValid() || !track->track()) {
68  ATH_MSG_WARNING("MuonStandalone track particle without Trk::Track");
69  continue;
70  }
71  const Trk::Track& msTrack = *track->track();
72 
73  ATH_MSG_VERBOSE("Re-Fitting track " << std::endl
74  << m_printer->print(msTrack) << std::endl
75  << m_printer->printStations(msTrack));
76  std::unique_ptr<Trk::Track> standaloneTrack;
77  if (m_extrapolationStrategy == 0u) {
78  standaloneTrack = m_trackBuilder->standaloneFit(ctx, msTrack, beamSpotHandle->beamPos(), nullptr);
79  } else {
80  standaloneTrack = m_trackExtrapolationTool->extrapolate(msTrack, ctx);
81  }
82  if (standaloneTrack) {
83  // Reject the track if its fit quality is much (much much) worse than that of the non-extrapolated track
84  if (standaloneTrack->fitQuality()->doubleNumberDoF() == 0) {
85  standaloneTrack.reset();
86  ATH_MSG_DEBUG("extrapolated track has no DOF, don't use it");
87  } else {
88  double mschi2 = 2.5; // a default we should hopefully never have to use (taken from CombinedMuonTrackBuilder)
89  if (msTrack.fitQuality()->doubleNumberDoF() > 0)
90  mschi2 = msTrack.fitQuality()->chiSquared() / msTrack.fitQuality()->doubleNumberDoF();
91  // choice of 1000 is slightly arbitrary, the point is that the fit should be really be terrible
92  if (standaloneTrack->fitQuality()->chiSquared() / standaloneTrack->fitQuality()->doubleNumberDoF() > 1000 * mschi2) {
93  standaloneTrack.reset();
94  ATH_MSG_DEBUG("extrapolated track has a degraded fit, don't use it");
95  }
96  }
97  }
98  if (standaloneTrack) {
99  standaloneTrack->info().setParticleHypothesis(Trk::muon);
101  ATH_MSG_VERBOSE("Extrapolated track " << std::endl
102  << m_printer->print(*standaloneTrack) << std::endl
103  << m_printer->printStations(*standaloneTrack));
104  ++ntracks;
105  if (!standaloneTrack->perigeeParameters())
106  ATH_MSG_WARNING(" Track without perigee " << (*standaloneTrack));
107  else if (!standaloneTrack->perigeeParameters()->covariance())
108  ATH_MSG_WARNING(" Track with perigee without covariance " << (*standaloneTrack));
109  trackLinks.emplace_back(std::move(standaloneTrack), index, true);
110  } else {
111  // We can create tracks from EM segments+TGC hits
112  // If these are not successfully extrapolated, they are too low quality to be useful
113  // So only make candidates from un-extrapolated tracks if they are not EM-only
114  bool skipTrack = true;
115  const Trk::MuonTrackSummary* msMuonTrackSummary = nullptr;
116  std::unique_ptr<Trk::TrackSummary> msTrackSummary;
117  // If reading from an ESD, the track will not have a track summary yet
118  if (!msTrack.trackSummary()) {
119  msTrackSummary = m_trackSummaryTool->summary(msTrack);
120  msMuonTrackSummary = msTrackSummary->muonTrackSummary();
121  } else
122  msMuonTrackSummary = msTrack.trackSummary()->muonTrackSummary();
123  for (const auto& chs : msMuonTrackSummary->chamberHitSummary()) {
124  if ((chs.isMdt() && m_idHelperSvc->stationIndex(chs.chamberId()) != Muon::MuonStationIndex::EM) ||
125  m_idHelperSvc->isCsc(chs.chamberId())) {
126  skipTrack = false;
127  break;
128  }
129  }
130  if (!skipTrack) { trackLinks.emplace_back(std::make_unique<Trk::Track>(msTrack), index, false); }
131  }
132  }
134  std::unique_ptr<TrackCollection> extrapTracks = std::make_unique<TrackCollection>(SG::VIEW_ELEMENTS);
135  extrapTracks->reserve(trackLinks.size());
136  for (const track_link& link : trackLinks) extrapTracks->push_back(link.track.get());
137  ATH_MSG_DEBUG("Finished back-tracking, total number of successfull fits " << ntracks);
138 
139  // Resolve ambiguity between extrapolated tracks (where available)
140  std::unique_ptr<const TrackCollection> resolvedTracks(m_ambiguityProcessor->process(extrapTracks.get()));
141 
142  ATH_MSG_DEBUG("Finished ambiguity solving: " << extrapTracks->size() << " track(s) in -> " << resolvedTracks->size()
143  << " track(s) out");
144 
145  const Trk::SegmentCollection* segments{nullptr};
146  if (!m_segmentKey.empty()) {
148  if (!readHandle.isValid()) {
149  ATH_MSG_WARNING("Failed to retrieve the segment container " << m_segmentKey.fullKey());
150  } else
151  segments = readHandle.cptr();
152  }
153 
154  // Loop over resolved tracks and build MuonCondidate collection
155  for (const Trk::Track* track : *resolvedTracks) {
157  std::find_if(trackLinks.begin(), trackLinks.end(), [&track](const track_link& link) { return link.track.get() == track; });
158 
159  if (tLink == trackLinks.end()) {
160  ATH_MSG_WARNING("Unable to find internal link between MS and SA tracks!");
161  continue;
162  }
163 
164  std::unique_ptr<MuonCandidate> muon_candidate;
165  ElementLink<xAOD::TrackParticleContainer> MS_TrkLink{tracks, tLink->container_index, ctx};
166  if (tLink->extp_succeed) {
167  outputTracks.push_back(std::move(tLink->track));
168  ElementLink<TrackCollection> saLink(outputTracks, outputTracks.size() - 1, ctx);
169  muon_candidate = std::make_unique<MuonCandidate>(MS_TrkLink, saLink, outputTracks.size() - 1);
170  // remove track from set so it is not deleted
171  } else {
172  // in this case the extrapolation failed
173  muon_candidate = std::make_unique<MuonCandidate>(MS_TrkLink);
174  }
175  muon_candidate->setCommissioning(m_commissioning);
177  if (segments) {
178  std::vector<const Muon::MuonSegment*> assoc_segs;
179  m_trackSegmentAssociationTool->associatedSegments(*muon_candidate->primaryTrack(), segments, assoc_segs);
180  muon_candidate->setSegments(std::move(assoc_segs));
181  }
182 
183  outputCollection.push_back(std::move(muon_candidate));
184  }
185  }
186 
187 } // namespace MuonCombined
DataVector::reserve
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
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
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:70
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:34
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
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
test_pyathena.parent
parent
Definition: test_pyathena.py:15
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:28
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
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
MuonCombined::MuonCandidateTool::MuonCandidateTool
MuonCandidateTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: MuonCandidateTool.cxx:29
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:364
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)
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
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
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:512
Trk::TrackInfo::setPatternRecognitionInfo
void setPatternRecognitionInfo(const TrackPatternRecoInfo &patternReco)
Method setting the pattern recognition algorithm.
AthAlgTool
Definition: AthAlgTool.h:26
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
Muon::MuonStationIndex::EM
@ EM
Definition: MuonStationIndex.h:26
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:47