ATLAS Offline Software
MuidMuonRecovery.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 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  }
194  lastPars = std::move(exPars);
195  innerParsSet = true;
196  }
197 
198  if (middleParameters) {
199  outerParameters = std::move(lastPars);
200  } else {
201  middleParameters = std::move(innerParameters);
202  if (!middleParameters) {
203  ATH_MSG_DEBUG("parameter extrapolation failed");
204  return nullptr;
205  }
206  }
207 
208  bool cleanEta = badEtaIndices.size() == 1 && etaIndices.size() > 1;
209  bool cleanPhi = badPhiIndices.size() == 1;
210 
211  if (!cleanPhi && !cleanEta) {
212  ATH_MSG_DEBUG("No layers removed");
213  return nullptr;
214  }
215 
216  if (badEtaIndices.size() == etaIndices.size()) {
217  ATH_MSG_DEBUG("All layers removed");
218  return nullptr;
219  }
220 
221  Trk::MeasurementSet spectrometerMeasurements;
222  for (const Trk::TrackStateOnSurface* tsosit : *spectrometerTrack.trackStateOnSurfaces()) {
223  const Trk::MeasurementBase* meas = tsosit->measurementOnTrack();
224  if (!meas) continue;
225  if (tsosit->type(Trk::TrackStateOnSurface::Outlier)) continue;
226 
227  Identifier id = m_edmHelperSvc->getIdentifier(*meas);
228  if (!id.is_valid()) continue;
229 
231  bool measuresPhi = m_idHelperSvc->measuresPhi(id);
232  if (cleanEta && !measuresPhi && badEtaIndices.count(index)) continue;
233  if (cleanPhi && measuresPhi && badPhiIndices.count(index)) continue;
234  spectrometerMeasurements.push_back(meas);
235  }
236 
237  ATH_MSG_DEBUG("Number of measurements before cleaning " << nmeas << " after cleaning " << spectrometerMeasurements.size());
238 
239  if (spectrometerMeasurements.size() < 6) {
240  ATH_MSG_DEBUG("Too few hits left - discarding fit");
241  return nullptr;
242  }
243 
244  // fit the combined track
245  std::unique_ptr<Trk::Track> combinedTrack;
246  if (!m_trackBuilder.empty()) {
247  combinedTrack = m_trackBuilder->indetExtension(ctx, indetTrack, spectrometerMeasurements, std::move(innerParameters), std::move(middleParameters),
248  std::move(outerParameters));
249  }
250  if (combinedTrack) {
252  combinedTrack->info().setPatternRecognitionInfo(Trk::TrackInfo::MuidMuonRecoveryTool);
253 
254  ATH_MSG_DEBUG("Recovered track " << std::endl
255  << m_printer->print(*combinedTrack) << std::endl
256  << m_printer->printStations(*combinedTrack));
257  } else {
259  ATH_MSG_DEBUG("track fit failure ");
260  }
261  return combinedTrack;
262  }
263 
264 } // 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
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
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
Rec::MuidMuonRecovery::m_printer
ToolHandle< Muon::MuonEDMPrinterTool > m_printer
Definition: MuidMuonRecovery.h:64
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:192
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:362
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