ATLAS Offline Software
Loading...
Searching...
No Matches
AlignResidualCalculator.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5
8
16
18
19#include <vector>
20
21namespace Trk {
22
23 //______________________________________________________________
24 AlignResidualCalculator::AlignResidualCalculator(const std::string& type, const std::string& name, const IInterface* parent)
25 : AthAlgTool(type,name,parent)
26 {
27 declareInterface<IAlignResidualCalculator>(this);
28 }
29
30 //________________________________________________________________________
35
36 //________________________________________________________________________
38 {
39 // get residual pull calculator
40 if (m_pullCalculator.retrieve().isFailure()) {
41 ATH_MSG_FATAL("Could not get " << m_pullCalculator);
42 return StatusCode::FAILURE;
43 }
44 ATH_MSG_INFO("Retrieved " << m_pullCalculator);
45
46 // get updator
47 if(m_resType==Unbiased) {
48 if (m_updator.retrieve().isFailure()) {
49 ATH_MSG_FATAL("Could not get " << m_updator);
50 return StatusCode::FAILURE;
51 }
52 ATH_MSG_INFO("Retrieved " << m_pullCalculator);
53 }
54
55 ATH_MSG_INFO("Using"<<static_cast<AlignResidualType>(m_resType.value())<<"residuals.");
56
57 return StatusCode::SUCCESS;
58 }
59
60 //________________________________________________________________________
62 {
63 return StatusCode::SUCCESS;
64 }
65
66 //________________________________________________________________________
68 {
69 bool useNewTrack = (track!=nullptr);
70 return setResiduals(atsosColl->begin(), atsosColl->end(), track, useNewTrack);
71 }
72
73 //________________________________________________________________________
75 {
76 bool useNewTrack = (track!=nullptr);
77 const Track* newTrack = (useNewTrack) ? track : alignTrack;
78 return setResiduals(alignTrack->firstAtsos(), alignTrack->lastAtsos(), newTrack, useNewTrack);
79 }
80
81 //________________________________________________________________________
84 const Track* track, bool newTrack)
85 {
86 m_nDoF=0;
87 m_matchedTSOS.clear();
88
89 delete [] m_chi2ForMeasType;
91 for (int i=0;i<TrackState::NumberOfMeasurementTypes;i++)
93
94 //ATH_MSG_DEBUG("in setResiduals with newTrack="<<newTrack);
95
96 if (!track&&newTrack) { ATH_MSG_ERROR("no track!"); return 0.; }
97
98 int ntsos(0);
99 double chi2(0.);
100 for (AlignTSOSCollection::iterator atsos=firstAtsos; atsos != lastAtsos; ++atsos,ntsos++) {
101
102 const AlignTSOS* atsosp = *atsos;
103 // get TSOS from AlignTSOS or closest TSOS if track given
104 const TrackStateOnSurface* tsos =
105 (newTrack) ? getMatchingTSOS(*atsos,track) : dynamic_cast<const TrackStateOnSurface*>(atsosp);
106
107 ATH_MSG_DEBUG("ntsos "<<ntsos);
108
109 const MaterialEffectsBase* meb = tsos->materialEffectsOnTrack();
110 const Trk::MaterialEffectsOnTrack* meot = dynamic_cast<const MaterialEffectsOnTrack*>(meb);
111
112 // if scatterer, add scattering parameters
113 if (m_includeScatterers && meb && meot) {
115 }
116
117 // set residuals for alignTSOS
118 double dchi2 = setResidualsOnATSOS(*atsos,tsos);
119 chi2 += dchi2;
120 int imeasType=(**atsos).measType();
121 m_chi2ForMeasType[imeasType] += dchi2;
122 ATH_MSG_DEBUG("adding "<<dchi2<<", m_chi2ForMeasType["<<imeasType<<"]="<<m_chi2ForMeasType[imeasType]);
123 }
124
125 return chi2;
126 }
127
128 //_______________________________________________________________________
129 double
131 {
132 // this method does the following:
133 // 1. gets residuals (measurement and scattering dimensions),
134 // 2. adds to AlignTSOS, and
135 // 3. returns contribution to chi2 from this ATSOS
136
137 ATH_MSG_DEBUG("in setResidualsOnATSOS");
138
139 atsos->clearResiduals();
140
141 double dchi2(0.);
142
143 // scattering residual(s) and/or energy loss first
145 ATH_MSG_DEBUG("scatterer");
146
147 // when using unbiased residuals including scatterers doesn't make sense
148 if(m_resType == Unbiased)
149 ATH_MSG_WARNING("When using unbiased residuals including scatterers doesn't make sense!");
150
151 const MaterialEffectsBase* meb = tsos->materialEffectsOnTrack();
152 const Trk::MaterialEffectsOnTrack* meot = dynamic_cast<const MaterialEffectsOnTrack*>(meb);
153 const ScatteringAngles* scatterer = (meot) ? meot->scatteringAngles() : nullptr;
154
155 int nscatparam=0;
156 if (meb && meot)
157 nscatparam = (scatterer) ? 2 : 1;
158
159 for (int iparam=0;iparam<nscatparam;iparam++) {
160
161 double errSq=0.;
162 double residual(-999.);
163
164 if (nscatparam==2) {
165
166 const Trk::TrackParameters * tparp = tsos->trackParameters();
167 if (tparp) {
168 if (iparam==0) {
169 residual=-scatterer->deltaPhi()* sin (tparp->parameters()[Trk::theta]);
170 errSq = scatterer->sigmaDeltaTheta();
171 ATH_MSG_DEBUG("sigmaDeltaTheta="<<scatterer->sigmaDeltaTheta());
172 ATH_MSG_DEBUG("sigmaDeltaPhi ="<<scatterer->sigmaDeltaPhi());
173 ATH_MSG_DEBUG("residual:"<<residual<<", errSq: "<<errSq<<", err="<<std::sqrt(errSq));
174 errSq *= errSq;
175 }
176 else{
177 residual=-scatterer->deltaTheta();
178 errSq = scatterer->sigmaDeltaTheta();
179 errSq *= errSq;
180 }
181 }
182 else{
183 ATH_MSG_WARNING("scatterer has no TrackParameters!");
184 }
185
186 Residual res(HitOnly,Scatterer,ParamDefs(iparam),residual,errSq);
187 atsos->addResidual(res);
188
189 dchi2 += res.residualNorm()*res.residualNorm();
190 m_nDoF++;
191 }
192 else if (!atsos->rio() && !atsos->crio()) {
193 ATH_MSG_DEBUG("energy deposit");
194
195 // energy deposit
196 double E0 = 1./std::fabs(m_previousQOverP);
197 double E1 = 1./std::fabs(m_qOverP);
198 double energyLoss= std::fabs(meot->energyLoss()->deltaE());
199
200 double residual = .001*(E0-E1-energyLoss);
201 double errSq = .001*meot->energyLoss()->sigmaDeltaE();
202
203 ATH_MSG_DEBUG("E0/E1/energyLoss: "<<E0<<"/"<<E1<<"/"<<energyLoss);
204 ATH_MSG_DEBUG("calorimeter residual: "<<residual/errSq);
205
206 errSq*=errSq;
207
208 Residual res(HitOnly,EnergyDeposit,ParamDefs(0),residual,errSq);
209 atsos->addResidual(res);
210 dchi2 += res.residualNorm()*res.residualNorm();
211 m_nDoF++;
212 }
213 }
214 }
215
216 // residuals from measurement
217 if (atsos->rio() || atsos->crio()) {
218
219 int nparams = (atsos->measType()==TrackState::Pixel) ? 2 : 1;
220 for (int iparam=0;iparam<nparams;iparam++) {
221
222 double errSq=0.;
223 double residual(-999.);
224
225 if ( atsos->measType()!=TrackState::unidentified &&
226 (atsos->rio()!=nullptr || atsos->crio()!=nullptr) ) {
227
228 const MeasurementBase* mesb = tsos->measurementOnTrack();
229
230 const TrackParameters * trackPars = tsos->trackParameters();
231 std::optional<ResidualPull> resPull = std::nullopt;
232
233 if ( trackPars ) {
234
235 if (m_resType == Unbiased) {
236 // Get unbiased state
237 const Trk::TrackParameters * unbiasedTrackPars =
238 m_updator->removeFromState(*trackPars,
239 mesb->localParameters(),
240 mesb->localCovariance()).release();
241 if (unbiasedTrackPars) {
242 trackPars = unbiasedTrackPars;
243 // AlignTSOS desctructor takes care of deleting unbiasedTrackPars
244 atsos->setUnbiasedTrackPars(unbiasedTrackPars);
245 }
246 else
247 ATH_MSG_WARNING("Could not get unbiased track parameters, use normal parameters");
248 }
249
250 ATH_MSG_DEBUG("Calling ResidualPullCalculator for residual type "
251 <<ResidualPullType(static_cast<AlignResidualType>(m_resType.value()))
252 <<" (AlignResidualType "<<static_cast<AlignResidualType>(m_resType.value())
253 <<" "<<m_resType<<")");
254 ATH_MSG_DEBUG("mesb->localErrorMatrix().covValue(Trk::loc1): "<<mesb->localCovariance()(Trk::loc1,Trk::loc1));
255 resPull = m_pullCalculator->residualPull(mesb, trackPars,
256 ResidualPullType(static_cast<AlignResidualType>(m_resType.value())),
257 atsos->measType());
258 if ( resPull ) {
259 residual = (resPull->residual())[iparam];
260 double pull=(resPull->pull())[iparam];
261 if (pull!=0.) {
262 errSq = residual/pull;
263 errSq *= errSq;
264 }
265 ATH_MSG_DEBUG("residual="<<residual<<", pull="<<pull);
266 ATH_MSG_DEBUG("pos: ("<<mesb->globalPosition().x()<<", "
267 <<mesb->globalPosition().y()<<", "
268 <<mesb->globalPosition().z()<<")");
269 ATH_MSG_DEBUG("residual:"<<residual<<", errSq: "<<errSq<<", err="<<std::sqrt(errSq));
270 }
271 }
272 }
273 else {
274 ATH_MSG_WARNING("Expected measurement for atsos of type "
275 << atsos->dumpType()<<", meas type "<<atsos->measType() );
276 }
277
278 Residual res(static_cast<AlignResidualType>(m_resType.value()),
279 Measurement, static_cast<ParamDefs>(iparam),
280 residual, errSq);
281 atsos->addResidual(res);
282 dchi2 += res.residualNorm()*res.residualNorm();
283 m_nDoF++;
284 }
285 }
286 return dchi2;
287 }
288
289 //________________________________________________________________________
291 {
292
293 const MaterialEffectsOnTrack* meot = dynamic_cast<const MaterialEffectsOnTrack*>(tsos->materialEffectsOnTrack());
294 const TrackParameters* tpar = tsos->trackParameters();
295
296 m_qOverP = tpar->charge()/tpar->pT()*sin( tpar->parameters()[Trk::theta]);
297
298 if(!meot) return;
299 const EnergyLoss* energyLoss = meot->energyLoss();
300
301
302 if (!dynamic_cast<const CaloEnergy*>(energyLoss)) {
304 }
305
306 }
307
308 //________________________________________________________________________
311 {
312 const TrackStateOnSurface* tsos(nullptr);
313
314 if (atsos->rio() || atsos->crio()) {
315
316 for (const TrackStateOnSurface* itTsos : *track->trackStateOnSurfaces()) {
317 if (itTsos->type(TrackStateOnSurface::Outlier))
318 continue;
319
320 const MeasurementBase* mesb = itTsos->measurementOnTrack();
321 const RIO_OnTrack* rio = dynamic_cast<const RIO_OnTrack*>(mesb);
322 const CompetingRIOsOnTrack* crio = dynamic_cast<const CompetingRIOsOnTrack*>(mesb);
323
324 if (!rio && crio) {
325 rio = &(crio->rioOnTrack(0));
326 }
327 if (!rio) continue;
328 if (rio->identify() == atsos->rio()->identify()) {
329 ATH_MSG_DEBUG("matched TSOS with identifier: "<<rio->identify());
330 tsos=itTsos;
331 break;
332 }
333 }
334 ATH_MSG_DEBUG("done with measurement");
335 }
336 else {
337
338 const Amg::Vector3D origPosition=atsos->trackParameters()->position();
339 double distance2(1.e27);
340
341 // loop over track and get closest TSOS
342 for (const TrackStateOnSurface* itTsos : *track->trackStateOnSurfaces()) {
343 if (itTsos->type(TrackStateOnSurface::Outlier))
344 continue;
345 if (!dynamic_cast<const MaterialEffectsOnTrack*>(itTsos->materialEffectsOnTrack())) continue;
346 if (!itTsos->trackParameters()) { ATH_MSG_WARNING("no track parameters!"); continue; }
347 const Amg::Vector3D newPosition=itTsos->trackParameters()->position();
348 ATH_MSG_DEBUG("origPos: "<<origPosition<<", newPos: "<<newPosition);
349 double newdist2=(newPosition - origPosition).mag2();
350 if (newdist2<distance2) {
351 distance2=newdist2;
352 tsos=itTsos;
353 }
354 }
355 ATH_MSG_DEBUG("done with scatterer");
356 }
357 if (!tsos) return nullptr;
358 const Amg::Vector3D addPosition=tsos->trackParameters()->position();
359 if (std::find(m_matchedTSOS.begin(),m_matchedTSOS.end(),tsos)==m_matchedTSOS.end()) {
360 m_matchedTSOS.push_back(tsos);
361 ATH_MSG_DEBUG("added tsos with pos: "<<addPosition);
362 }
363 else {
364 ATH_MSG_WARNING("TSOS already found with position "<<addPosition<<"!");
365 }
366 return tsos;
367 }
368
369} // end namespace
Scalar mag2() const
mag2 method - forward to squaredNorm()
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::pair< std::vector< unsigned int >, bool > res
AlignTSOS is a TSOS with extra variables useful for alignment.
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
class extending the basic Trk::EnergyLoss to describe the measured or parameterised muon energy loss ...
Definition CaloEnergy.h:28
DataModel_detail::iterator< DataVector > iterator
Definition DataVector.h:842
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
const TrackStateOnSurface * getMatchingTSOS(const AlignTSOS *atsos, const Track *track)
void accumulateScattering(const TrackStateOnSurface *tsos)
virtual StatusCode initialize() override
virtual double setResiduals(DataVector< AlignTSOS > *alignTSOSColl, const Track *track) override
sets residuals for AlignTSOS on AlignTrack and returns total chi2
ToolHandle< IResidualPullCalculator > m_pullCalculator
std::vector< const TrackStateOnSurface * > m_matchedTSOS
AlignResidualCalculator(const std::string &type, const std::string &name, const IInterface *parent)
virtual StatusCode finalize() override
double setResidualsOnATSOS(AlignTSOS *atsos, const TrackStateOnSurface *tsos)
const RIO_OnTrack * rio() const
returns RIO_OnTrack or leading RIO of CompetingRIOsOnTrack (assigned by c'tor)
void setUnbiasedTrackPars(const TrackParameters *trkPars)
setter for unbiased track parameters
Definition AlignTSOS.h:137
void addResidual(const Residual &residual)
pushes back vector of Residuals to alignTSOS residuals
Definition AlignTSOS.h:95
const CompetingRIOsOnTrack * crio() const
returns CompetingRIOsOnTrack
Definition AlignTSOS.h:92
void clearResiduals()
clears vector of residuals
Definition AlignTSOS.h:101
TrackState::MeasurementType measType() const
returns measurement type enum
Definition AlignTSOS.h:80
AlignTSOSCollection::const_iterator lastAtsos() const
returns iterator pointer to last element in collection
Definition AlignTrack.h:280
AlignTSOSCollection::const_iterator firstAtsos() const
retrieve iterator pointer to first element in collection
Definition AlignTrack.h:279
Base class for all CompetingRIOsOnTack implementations, extends the common MeasurementBase.
virtual const RIO_OnTrack & rioOnTrack(unsigned int) const =0
returns the RIO_OnTrack (also known as ROT) objects depending on the integer.
This class describes energy loss material effects in the ATLAS tracking EDM.
Definition EnergyLoss.h:34
double sigmaDeltaE() const
returns the symmatric error
double deltaE() const
returns the
base class to integrate material effects on Trk::Track in a flexible way.
represents the full description of deflection and e-loss of a track in material.
const EnergyLoss * energyLoss() const
returns the energy loss object.
const ScatteringAngles * scatteringAngles() const
returns the MCS-angles object.
This class is the pure abstract base class for all fittable tracking measurements.
const LocalParameters & localParameters() const
Interface method to get the LocalParameters.
virtual const Amg::Vector3D & globalPosition() const =0
Interface method to get the global Position.
const Amg::MatrixX & localCovariance() const
Interface method to get the localError.
const Amg::Vector3D & position() const
Access method for the position.
double charge() const
Returns the charge.
double pT() const
Access method for transverse momentum.
Class to handle RIO On Tracks ROT) for InDet and Muons, it inherits from the common MeasurementBase.
Definition RIO_OnTrack.h:70
Identifier identify() const
return the identifier -extends MeasurementBase
represents a deflection of the track caused through multiple scattering in material.
double sigmaDeltaPhi() const
returns the
double deltaPhi() const
returns the
double sigmaDeltaTheta() const
returns the
double deltaTheta() const
returns the
represents the track state (measurement, material, fit parameters and quality) at a surface.
const MeasurementBase * measurementOnTrack() const
returns MeasurementBase const overload
const TrackParameters * trackParameters() const
return ptr to trackparameters const overload
std::string dumpType() const
returns a string with the expanded type of the object (i.e.
@ Outlier
This TSoS contains an outlier, that is, it contains a MeasurementBase/RIO_OnTrack which was not used ...
const MaterialEffectsBase * materialEffectsOnTrack() const
return material effects const overload
double chi2(TH1 *h0, TH1 *h1)
Eigen::Matrix< double, 3, 1 > Vector3D
Ensure that the ATLAS eigen extensions are properly loaded.
ResidualPull::ResidualType ResidualPullType(AlignResidualType type)
DataVector< AlignTSOS > AlignTSOSCollection
Definition AlignTrack.h:37
ParamDefs
This file defines the parameter enums in the Trk namespace.
Definition ParamDefs.h:32
@ theta
Definition ParamDefs.h:66
@ loc1
Definition ParamDefs.h:34
ParametersBase< TrackParametersDim, Charged > TrackParameters