ATLAS Offline Software
MuidMuonRecovery.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 // MuidMuonRecovery
7 // AlgTool performing MS hit reallocation for a likely spectrometer-indet
8 // match which has given combined fit problems.
9 // Extrapolates indet track to MS.
10 // Returns a combined track with full track fit.
11 //
13 
14 #include "MuidMuonRecovery.h"
15 
16 #include <cmath>
17 #include <iomanip>
18 #include <vector>
19 
20 #include "GaudiKernel/ServiceHandle.h"
21 #include "GaudiKernel/SystemOfUnits.h"
31 #include "TrkTrack/Track.h"
33 
34 namespace Rec {
35 
36  MuidMuonRecovery::MuidMuonRecovery(const std::string& type, const std::string& name, const IInterface* parent) :
38  declareInterface<IMuidMuonRecovery>(this);
39  }
40 
41  //<<<<<< PUBLIC MEMBER FUNCTION DEFINITIONS >>>>>>
42 
44  ATH_MSG_INFO("Initializing MuidMuonRecovery");
45 
46  // get the Tools
47  ATH_CHECK(m_extrapolator.retrieve());
48  ATH_MSG_INFO("Retrieved tool " << m_extrapolator);
49  ATH_CHECK(m_edmHelperSvc.retrieve());
50  ATH_MSG_INFO("Retrieved tool " << m_edmHelperSvc);
51  ATH_CHECK(m_idHelperSvc.retrieve());
52  ATH_MSG_INFO("Retrieved tool " << m_idHelperSvc);
53  ATH_CHECK(m_printer.retrieve());
54  ATH_MSG_INFO("Retrieved tool " << m_printer);
55  ATH_CHECK(m_residualCalculator.retrieve());
56  ATH_MSG_INFO("Retrieved tool " << m_residualCalculator);
57 
58  if (!m_trackBuilder.empty()) {
59  ATH_CHECK(m_trackBuilder.retrieve());
60  ATH_MSG_INFO("Retrieved tool " << m_trackBuilder);
61  }
62 
63  return StatusCode::SUCCESS;
64  }
65 
67  ATH_MSG_INFO("Recovery attempts " << m_recoveryAttempts << ", failedFit " << m_recoveryFitFailure << ", success "
69 
70  return StatusCode::SUCCESS;
71  }
72  std::unique_ptr<Trk::Track> MuidMuonRecovery::recoverableMatch(const Trk::Track& indetTrack, const Trk::Track& spectrometerTrack,
73  const EventContext& ctx) const {
74  // skip low pt ID tracks
75  if (!indetTrack.perigeeParameters() || indetTrack.perigeeParameters()->momentum().mag() < m_minP ||
76  indetTrack.perigeeParameters()->momentum().perp() < m_minPt) {
77  return nullptr;
78  }
79 
81 
82  ATH_MSG_DEBUG("Entering new recovery" << std::endl
83  << " ID track " << m_printer->print(indetTrack) << std::endl
84  << " MS track " << m_printer->print(spectrometerTrack) << std::endl
85  << m_printer->printStations(spectrometerTrack));
86 
87  const Trk::TrackParameters* lastIndetPars = nullptr;
88  int index = static_cast<int>(indetTrack.trackParameters()->size());
89 
90  while (!lastIndetPars && index > 0) {
91  --index;
92  lastIndetPars = (*indetTrack.trackParameters())[index] ? (*indetTrack.trackParameters())[index] : nullptr;
93  }
94 
95  if (!lastIndetPars) {
96  ATH_MSG_WARNING("ID track parameters don't have error matrix!");
97  return nullptr;
98  }
99 
100  // track builder prefers estimate of inner, middle and outer spectrometer track parameters
101  std::unique_ptr<Trk::TrackParameters> innerParameters, middleParameters, outerParameters;
102  std::unique_ptr<Trk::TrackParameters> lastPars = lastIndetPars->uniqueClone();
103  bool innerParsSet{false};
104 
105  std::vector<const Trk::TrackStateOnSurface*> stations;
106  std::set<Muon::MuonStationIndex::StIndex> etaIndices, phiIndices, badEtaIndices, badPhiIndices;
107 
108  unsigned int nmeas = 0;
109 
110  for (const Trk::TrackStateOnSurface* tsosit : *spectrometerTrack.trackStateOnSurfaces()) {
111  const Trk::MeasurementBase* meas = tsosit->measurementOnTrack();
112  if (!meas) continue;
113 
114  if (tsosit->type(Trk::TrackStateOnSurface::Outlier)) continue;
115 
116  Identifier id = m_edmHelperSvc->getIdentifier(*meas);
117  if (!id.is_valid()) continue;
118 
120  bool measuresPhi = m_idHelperSvc->measuresPhi(id);
121  ++nmeas;
122 
123  if (measuresPhi) {
124  if (phiIndices.count(index)) continue;
125  ATH_MSG_DEBUG("Adding phi station " << m_idHelperSvc->toString(id));
126  phiIndices.insert(index);
127  } else if (m_idHelperSvc->isMdt(id) || (m_idHelperSvc->isCsc(id))) {
128  if (etaIndices.count(index)) continue;
129 
130  ATH_MSG_DEBUG("Adding eta station " << m_idHelperSvc->toString(id));
131  etaIndices.insert(index);
132  } else {
133  continue;
134  }
135 
136  std::unique_ptr<Trk::TrackParameters> exPars{};
137  if (lastPars->associatedSurface() == meas->associatedSurface()) {
138  ATH_MSG_DEBUG("Using existing pars");
139  exPars = std::move(lastPars);
140  } else {
141  exPars = m_extrapolator->extrapolate(ctx, *lastPars, meas->associatedSurface(), Trk::alongMomentum, false, Trk::muon);
142  }
143 
144  if (!exPars) {
145  ATH_MSG_DEBUG("Failed to extrapolate to station" << m_idHelperSvc->toStringChamber(id));
146  continue;
147  }
148 
149  std::optional<Trk::ResidualPull> res {m_residualCalculator->residualPull(meas, exPars.get(), Trk::ResidualPull::Unbiased)};
150 
151  ATH_MSG_DEBUG(" " << m_idHelperSvc->toStringChamber(id) << " residual " << m_printer->print(*res));
152 
153  if (std::abs(res->pull().front()) > m_pullCut) {
154  if (measuresPhi) {
155  badPhiIndices.insert(index);
156  } else {
157  badEtaIndices.insert(index);
158  }
159  }
160 
161 
162  if (msgLvl(MSG::DEBUG)) {
163  if (!m_idHelperSvc->measuresPhi(id)) {
164  const MuonGM::MuonReadoutElement* detEl = nullptr;
165  if (m_idHelperSvc->isMdt(id)) {
166  const Muon::MdtDriftCircleOnTrack* mdt = dynamic_cast<const Muon::MdtDriftCircleOnTrack*>(meas);
167  if (mdt) { detEl = mdt->detectorElement(); }
168  } else if (m_idHelperSvc->isCsc(id)) {
169  const Muon::CscClusterOnTrack* csc = dynamic_cast<const Muon::CscClusterOnTrack*>(meas);
170  if (csc) { detEl = csc->detectorElement(); }
171  }
172 
173  if (detEl) {
174  const Trk::PlaneSurface* detSurf = dynamic_cast<const Trk::PlaneSurface*>(&detEl->surface());
175  if (detSurf) {
176  Trk::LocalDirection idDir{};
177  detSurf->globalToLocalDirection(exPars->momentum(), idDir);
178 
179  const Trk::TrackParameters* pars = tsosit->trackParameters();
180  Trk::LocalDirection msDir{};
181  detSurf->globalToLocalDirection(pars->momentum(), msDir);
182  ATH_MSG_DEBUG(" local Angles: id (" << idDir.angleXZ() << "," << idDir.angleYZ() << ") ms ("
183  << msDir.angleXZ() << "," << msDir.angleYZ() << ")");
184  }
185  }
186  }
187  } // end DEBUG toggle
188 
189  if (!innerParsSet && !innerParameters && exPars && lastPars) {
190  innerParameters = std::move(exPars);
191  } else if (exPars && innerParameters && !middleParameters ) {
192  middleParameters = std::move(exPars);
193  } else {
194  lastPars = std::move(exPars);
195  }
196  innerParsSet = true;
197  }
198 
199  if (middleParameters) {
200  outerParameters = std::move(lastPars);
201  } else {
202  middleParameters = std::move(innerParameters);
203  if (!middleParameters) {
204  ATH_MSG_DEBUG("parameter extrapolation failed");
205  return nullptr;
206  }
207  }
208 
209  bool cleanEta = badEtaIndices.size() == 1 && etaIndices.size() > 1;
210  bool cleanPhi = badPhiIndices.size() == 1;
211 
212  if (!cleanPhi && !cleanEta) {
213  ATH_MSG_DEBUG("No layers removed");
214  return nullptr;
215  }
216 
217  if (badEtaIndices.size() == etaIndices.size()) {
218  ATH_MSG_DEBUG("All layers removed");
219  return nullptr;
220  }
221 
222  Trk::MeasurementSet spectrometerMeasurements;
223  for (const Trk::TrackStateOnSurface* tsosit : *spectrometerTrack.trackStateOnSurfaces()) {
224  const Trk::MeasurementBase* meas = tsosit->measurementOnTrack();
225  if (!meas) continue;
226  if (tsosit->type(Trk::TrackStateOnSurface::Outlier)) continue;
227 
228  Identifier id = m_edmHelperSvc->getIdentifier(*meas);
229  if (!id.is_valid()) continue;
230 
232  bool measuresPhi = m_idHelperSvc->measuresPhi(id);
233  if (cleanEta && !measuresPhi && badEtaIndices.count(index)) continue;
234  if (cleanPhi && measuresPhi && badPhiIndices.count(index)) continue;
235  spectrometerMeasurements.push_back(meas);
236  }
237 
238  ATH_MSG_DEBUG("Number of measurements before cleaning " << nmeas << " after cleaning " << spectrometerMeasurements.size());
239 
240  if (spectrometerMeasurements.size() < 6) {
241  ATH_MSG_DEBUG("Too few hits left - discarding fit");
242  return nullptr;
243  }
244 
245  // fit the combined track
246  std::unique_ptr<Trk::Track> combinedTrack;
247  if (!m_trackBuilder.empty()) {
248  combinedTrack = m_trackBuilder->indetExtension(ctx, indetTrack, spectrometerMeasurements, std::move(innerParameters), std::move(middleParameters),
249  std::move(outerParameters));
250  }
251  if (combinedTrack) {
253  combinedTrack->info().setPatternRecognitionInfo(Trk::TrackInfo::MuidMuonRecoveryTool);
254 
255  ATH_MSG_DEBUG("Recovered track " << std::endl
256  << m_printer->print(*combinedTrack) << std::endl
257  << m_printer->printStations(*combinedTrack));
258  } else {
260  ATH_MSG_DEBUG("track fit failure ");
261  }
262  return combinedTrack;
263  }
264 
265 } // namespace Rec
make_hlt_rep.pars
pars
Definition: make_hlt_rep.py:90
Rec::MuidMuonRecovery::m_trackBuilder
ToolHandle< ICombinedMuonTrackBuilder > m_trackBuilder
Definition: MuidMuonRecovery.h:78
TrackParameters.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Trk::Track
The ATLAS Track class.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/Track.h:73
Rec::MuidMuonRecovery::finalize
StatusCode finalize() override
Definition: MuidMuonRecovery.cxx:66
index
Definition: index.py:1
Trk::ParametersBase::associatedSurface
virtual const Surface & associatedSurface() const override=0
Access to the Surface associated to the Parameters.
Trk::ParametersBase::uniqueClone
std::unique_ptr< ParametersBase< DIM, T > > uniqueClone() const
clone method for polymorphic deep copy returning unique_ptr; it is not overriden, but uses the existi...
Definition: ParametersBase.h:97
Trk::Track::trackStateOnSurfaces
const Trk::TrackStates * trackStateOnSurfaces() const
return a pointer to a const DataVector of const TrackStateOnSurfaces.
Trk::ResidualPull::Unbiased
@ Unbiased
RP with track state that has measurement not included.
Definition: ResidualPull.h:57
AthCommonMsg< AlgTool >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
Rec::MuidMuonRecovery::m_extrapolator
ToolHandle< Trk::IExtrapolator > m_extrapolator
Definition: MuidMuonRecovery.h:45
Trk::alongMomentum
@ alongMomentum
Definition: PropDirection.h:20
Rec::MuidMuonRecovery::m_residualCalculator
ToolHandle< Trk::IResidualPullCalculator > m_residualCalculator
Definition: MuidMuonRecovery.h:71
Rec::MuidMuonRecovery::m_recoveryFitFailure
std::atomic< unsigned int > m_recoveryFitFailure
Definition: MuidMuonRecovery.h:92
Muon::MdtDriftCircleOnTrack::detectorElement
virtual const MuonGM::MdtReadoutElement * detectorElement() const override final
Returns the detector element, assoicated with the PRD of this class.
Definition: MdtDriftCircleOnTrack.h:268
Rec::MuidMuonRecovery::m_edmHelperSvc
ServiceHandle< Muon::IMuonEDMHelperSvc > m_edmHelperSvc
Definition: MuidMuonRecovery.h:52
MdtDriftCircleOnTrack.h
Trk::combinedTrack
void combinedTrack(long int ICH, double *pv0, double *covi, double BMAG, double *par, double *covo)
Definition: XYZtrp.cxx:113
Muon::CscClusterOnTrack::detectorElement
virtual const MuonGM::CscReadoutElement * detectorElement() const override final
Returns the detector element, associated with the PRD of this class.
Definition: CscClusterOnTrack.h:167
MuonGM::MuonReadoutElement
Base class for the XxxReadoutElement, with Xxx = Mdt, Rpc, Tgc, Csc.
Definition: MuonDetDescr/MuonReadoutGeometry/MuonReadoutGeometry/MuonReadoutElement.h:40
Trk::TrackStateOnSurface::Outlier
@ Outlier
This TSoS contains an outlier, that is, it contains a MeasurementBase/RIO_OnTrack which was not used ...
Definition: TrackStateOnSurface.h:122
Track.h
Rec::MuidMuonRecovery::m_minP
Gaudi::Property< double > m_minP
Definition: MuidMuonRecovery.h:86
ResidualPull.h
Rec
Name: MuonSpContainer.h Package : offline/Reconstruction/MuonIdentification/muonEvent.
Definition: FakeTrackBuilder.h:10
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
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
CscClusterOnTrack.h
PseudoMeasurementOnTrack.h
test_pyathena.parent
parent
Definition: test_pyathena.py:15
Trk::TrkDetElementBase::surface
virtual const Surface & surface() const =0
Return surface associated with this detector element.
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::PlaneSurface::globalToLocalDirection
void globalToLocalDirection(const Amg::Vector3D &glodir, Trk::LocalDirection &locdir) const
This method transforms the global direction to a local direction wrt the plane.
Definition: PlaneSurface.cxx:260
Rec::MuidMuonRecovery::MuidMuonRecovery
MuidMuonRecovery(const std::string &type, const std::string &name, const IInterface *parent)
Definition: MuidMuonRecovery.cxx:36
TrackSummary.h
Trk::ParametersBase
Definition: ParametersBase.h:55
MuonReadoutElement.h
Trk::muon
@ muon
Definition: ParticleHypothesis.h:28
Trk::LocalDirection
represents the three-dimensional global direction with respect to a planar surface frame.
Definition: LocalDirection.h:81
Rec::MuidMuonRecovery::initialize
StatusCode initialize() override
Definition: MuidMuonRecovery.cxx:43
MuidMuonRecovery.h
Trk::MeasurementSet
std::vector< const MeasurementBase * > MeasurementSet
vector of fittable measurements
Definition: FitterTypes.h:30
Rec::MuidMuonRecovery::m_pullCut
Gaudi::Property< double > m_pullCut
Definition: MuidMuonRecovery.h:88
Trk::MeasurementBase
Definition: MeasurementBase.h:58
Trk::Track::trackParameters
const DataVector< const TrackParameters > * trackParameters() const
Return a pointer to a vector of TrackParameters.
Definition: Tracking/TrkEvent/TrkTrack/src/Track.cxx:97
Trk::Track::perigeeParameters
const Perigee * perigeeParameters() const
return Perigee.
Definition: Tracking/TrkEvent/TrkTrack/src/Track.cxx:163
Trk::TrackStateOnSurface
represents the track state (measurement, material, fit parameters and quality) at a surface.
Definition: TrackStateOnSurface.h:71
Rec::MuidMuonRecovery::m_recoveryAttempts
std::atomic< unsigned int > m_recoveryAttempts
Definition: MuidMuonRecovery.h:91
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
Trk::MeasurementBase::associatedSurface
virtual const Surface & associatedSurface() const =0
Interface method to get the associated Surface.
Muon::MdtDriftCircleOnTrack
This class represents the corrected MDT measurements, where the corrections include the effects of wi...
Definition: MdtDriftCircleOnTrack.h:37
Rec::MuidMuonRecovery::m_recoverySuccess
std::atomic< unsigned int > m_recoverySuccess
Definition: MuidMuonRecovery.h:93
DeMoScan.index
string index
Definition: DeMoScan.py:364
Rec::MuidMuonRecovery::m_printer
PublicToolHandle< Muon::MuonEDMPrinterTool > m_printer
Definition: MuidMuonRecovery.h:64
Muon::CscClusterOnTrack
Class to represent the calibrated clusters created from CSC strips.
Definition: CscClusterOnTrack.h:47
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Trk::PlaneSurface
Definition: PlaneSurface.h:64
PlaneSurface.h
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
Rec::MuidMuonRecovery::m_minPt
Gaudi::Property< double > m_minPt
Definition: MuidMuonRecovery.h:87
DEBUG
#define DEBUG
Definition: page_access.h:11
Rec::MuidMuonRecovery::recoverableMatch
std::unique_ptr< Trk::Track > recoverableMatch(const Trk::Track &indetTrack, const Trk::Track &spectrometerTrack, const EventContext &ctx) const override
IMuidMuonRecovery interface: algorithmic code for recovering muon spectrometer using the inner detect...
Definition: MuidMuonRecovery.cxx:72
Trk::TrackInfo::MuidMuonRecoveryTool
@ MuidMuonRecoveryTool
Muons found by the ID seeded muon recovery.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/TrackInfo.h:229
Muon::MuonStationIndex::StIndex
StIndex
enum to classify the different station layers in the muon spectrometer
Definition: MuonStationIndex.h:23
LocalDirection.h
Rec::MuidMuonRecovery::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MuidMuonRecovery.h:59
AthAlgTool
Definition: AthAlgTool.h:26
SegmentCollection.h
Identifier
Definition: IdentifierFieldParser.cxx:14