ATLAS Offline Software
Loading...
Searching...
No Matches
ImpactPoint3dEstimator.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5/*********************************************************************
6 ImpactPoint3dEstimator.cxx - Description in header file
7*********************************************************************/
8
14
15// #define IMPACTPOINT3DESTIMATOR_DEBUG
16
17//added for cuts in case of displaced vertex
20
21#include <cmath>
22
23namespace Trk
24{
25
26 ImpactPoint3dEstimator::ImpactPoint3dEstimator(const std::string& t, const std::string& n, const IInterface* p) :
27 base_class(t,n,p),
30 m_precision(1e-10)//DeltaPhi
31 {
32 declareProperty("Extrapolator",m_extrapolator);
33 declareProperty("MaxIterations",m_maxiterations);
34 declareProperty("Precision",m_precision);
35 }
36
38
40 {
41 if (!m_extrapolator.empty()) {
42 ATH_CHECK( m_extrapolator.retrieve() );
43 } else {
44 m_extrapolator.disable();
45 }
47
48 ATH_MSG_DEBUG( "Initialize successful" );
49 return StatusCode::SUCCESS;
50 }
51
53 {
54 ATH_MSG_DEBUG( "Finalize successful" );
55 return StatusCode::SUCCESS;
56 }
57
58
59 template<typename T>
60 std::unique_ptr<PlaneSurface>
62 const Amg::Vector3D* theVertex,
63 double& distance) const
64 {
65 const Amg::Vector3D momentumUnit = thePerigee->momentum().unit();
66 double pathLength = ( *theVertex - thePerigee->position() ).dot( momentumUnit )
67 / ( momentumUnit.dot( momentumUnit )) ;
68 //first vector at 3d impact point
69
70
71 Amg::Vector3D POCA = thePerigee->position() + pathLength * momentumUnit;// Position of closest approach
72 Amg::Vector3D DeltaR = *theVertex - POCA;
73 distance=DeltaR.mag();
74 DeltaR=DeltaR.unit();
75
76
77 //correct DeltaR from small deviations from orthogonality to DeltaR -- DeltaR.dot(momentumUnit) should equal 0 if the above is correct
78
79 Amg::Vector3D DeltaRcorrected=DeltaR-(DeltaR.dot(momentumUnit))*momentumUnit;
80
81 if ((DeltaR-DeltaRcorrected).mag()>1e-4)
82 {
83 ATH_MSG_WARNING( " DeltaR and MomentumDir are not orthogonal " );
84 ATH_MSG_DEBUG( std::setprecision(10) << " DeltaR-DeltaRcorrected: " << (DeltaR-DeltaRcorrected).mag() );
85 }
86
87 Amg::Vector3D YDir=momentumUnit.cross(DeltaRcorrected);
88
89 ATH_MSG_VERBOSE( "final minimal distance is: " << distance);
90 ATH_MSG_DEBUG( "POCA in 3D is: " << POCA );
91
92 //store the plane...
93 ATH_MSG_VERBOSE( "plane to which to extrapolate X " << DeltaRcorrected << " Y " << YDir << " Z " << momentumUnit);
94
95 Amg::Transform3D thePlane(DeltaRcorrected, YDir, momentumUnit, *theVertex);
96
97#ifdef IMPACTPOINT3DESTIMATOR_DEBUG
98 std::cout << "the translation is, directly from Transform3d: " << thePlane->getTranslation() << endmsg;
99#endif
100
101 return std::make_unique<PlaneSurface>(thePlane);
102
103 }
104
105 std::unique_ptr<PlaneSurface>
106 ImpactPoint3dEstimator::Estimate3dIP(const EventContext& /*ctx*/,
107 const NeutralParameters* neutralPerigee,
108 const Amg::Vector3D* theVertex,
109 double& distance) const
110 {
111 ATH_MSG_DEBUG("Neutral particle -- propagate like a straight line");
112 return Estimate3dIPNoCurvature(neutralPerigee, theVertex, distance);
113 }
114
115 std::unique_ptr<PlaneSurface>
117 const TrackParameters* trackPerigee,
118 const Amg::Vector3D* theVertex,
119 double& distance) const
120 {
122 const AtlasFieldCacheCondObj* fieldCondObj{*readHandle};
123
124 MagField::AtlasFieldCache fieldCache;
125 fieldCondObj->getInitializedCache (fieldCache);
126
127 double magnFieldVect[3];
128 fieldCache.getField(trackPerigee->associatedSurface().center().data(),magnFieldVect);
129 if(magnFieldVect[2] == 0 ){
130 ATH_MSG_DEBUG("Magnetic field in the Z direction is 0 -- propagate like a straight line");
131 return Estimate3dIPNoCurvature(trackPerigee, theVertex, distance);
132 }
133
134 const Trk::Perigee* thePerigee=dynamic_cast<const Trk::Perigee*>(trackPerigee);
135 if (thePerigee==nullptr)
136 {
138 " ImpactPoint3dEstimator didn't get a Perigee* as ParametersBase*: "
139 "cast not possible. Need to EXTRAPOLATE...");
140 Trk::PerigeeSurface perigeeSurface(*theVertex);
141 std::unique_ptr<const Trk::TrackParameters> tmp =
142 m_extrapolator->extrapolateDirectly(
143 ctx, *trackPerigee, perigeeSurface);
144 if (tmp && tmp->associatedSurface().type() == Trk::SurfaceType::Perigee) {
145 thePerigee = static_cast<const Trk::Perigee*>(tmp.release());
146 }
147 if (thePerigee == nullptr){
148 return nullptr;
149 }
150 }
151
152 ATH_MSG_VERBOSE( " Now running ImpactPoint3dEstimator::Estimate3dIP" );
153 double phi0=thePerigee->parameters()[Trk::phi0];
154 double dCosPhi0=-std::sin(phi0);
155 double dSinPhi0=std::cos(phi0);
156 double theta=thePerigee->parameters()[Trk::theta];
157 double cotTheta=1./std::tan(thePerigee->parameters()[Trk::theta]);
158 double d0=thePerigee->parameters()[Trk::d0];
159
160 //I need the radius (magnetic field...)
161 double Bz=magnFieldVect[2]*299.792;
162 double Rt=std::sin(theta)/(Bz*thePerigee->parameters()[Trk::qOverP]);
163
164 double x0=thePerigee->associatedSurface().center().x()+(d0-Rt)*dCosPhi0;
165 double y0=thePerigee->associatedSurface().center().y()+(d0-Rt)*dSinPhi0;
166 double z0=thePerigee->associatedSurface().center().z()+thePerigee->parameters()[Trk::z0]+Rt*phi0*cotTheta;
167
168 if (thePerigee!=trackPerigee) {
169 delete thePerigee;
170 thePerigee=nullptr;
171 }
172
173 double xc=theVertex->x();
174 double yc=theVertex->y();
175 double zc=theVertex->z();
176
177 double phiActual=phi0;
178 double dCosPhiActual=-std::sin(phiActual);
179 double dSinPhiActual=std::cos(phiActual);
180
181 double secderivative=0.;
182 double derivative=0.;
183
184 int ncycle=0;
185 bool isok=false;
186
187 double deltaphi=0.;
188
189#ifdef IMPACTPOINT3DESTIMATOR_DEBUG
190 std::cout << std::setprecision(25) << "actual distance before cycle is: " << std::hypot(x0-xc+Rt*dCosPhiActual,
191 y0-yc+Rt*dSinPhiActual,
192 z0-zc-Rt*cotTheta*phiActual) << std::endl;
193#endif
194
195 do {
196
197#ifdef IMPACTPOINT3DESTIMATOR_DEBUG
198 ATH_MSG_VERBOSE( "Cycle number: " << ncycle << " old phi: " << phiActual );
199#endif
200
201
202 derivative=(x0-xc)*(-Rt*dSinPhiActual)+(y0-yc)*Rt*dCosPhiActual+(z0-zc-Rt*phiActual*cotTheta)*(-Rt*cotTheta);
203 secderivative=Rt*(-(x0-xc)*dCosPhiActual-(y0-yc)*dSinPhiActual+Rt*cotTheta*cotTheta);
204#ifdef IMPACTPOINT3DESTIMATOR_DEBUG
205 ATH_MSG_VERBOSE( "derivative is: " << derivative << " sec derivative is: " << secderivative );
206#endif
207
208 deltaphi=-derivative/secderivative;
209
210#ifdef IMPACTPOINT3DESTIMATOR_DEBUG
211 std::cout << std::setprecision(25) << "deltaphi: " << deltaphi << std::endl;
212#endif
213
214 phiActual+=deltaphi;
215 dCosPhiActual=-std::sin(phiActual);
216 dSinPhiActual=std::cos(phiActual);
217
218#ifdef IMPACTPOINT3DESTIMATOR_DEBUG
219 ATH_MSG_VERBOSE( "derivative is: " << derivative << " sec derivative is: " << secderivative );
220 std::cout << std::setprecision(25) << std::hypot(x0-xc+Rt*dCosPhiActual, y0-yc+Rt*dSinPhiActual,
221 z0-zc-Rt*cotTheta*phiActual) << std::endl;
222 ATH_MSG_VERBOSE( "actual distance is: " << std::hypot(x0-xc+Rt*dCosPhiActual,
223 y0-yc+Rt*dSinPhiActual,
224 z0-zc-Rt*cotTheta*phiActual));
225#endif
226
227 if (secderivative<0) throw error::ImpactPoint3dEstimatorProblem("Second derivative is negative");
228
229 if (ncycle>m_maxiterations) throw error::ImpactPoint3dEstimatorProblem("Too many loops: could not find minimum distance to vertex");
230
231 ncycle+=1;
232 if (ncycle>m_maxiterations||std::abs(deltaphi)<m_precision) {
233#ifdef IMPACTPOINT3DESTIMATOR_DEBUG
234 ATH_MSG_VERBOSE( "found minimum at: " << phiActual );
235#endif
236 isok=true;
237 }
238
239 } while (!isok);
240
241 //now you have to construct the plane with PlaneSurface
242 //first vector at 3d impact point
243 Amg::Vector3D MomentumDir(std::cos(phiActual)*std::sin(theta),std::sin(phiActual)*std::sin(theta),std::cos(theta));
244 Amg::Vector3D DeltaR(x0-xc+Rt*dCosPhiActual,y0-yc+Rt*dSinPhiActual,z0-zc-Rt*cotTheta*phiActual);
245 distance=DeltaR.mag();
246 if (distance==0.){
247 ATH_MSG_WARNING("DeltaR is zero in ImpactPoint3dEstimator::Estimate3dIP, returning nullptr");
248 return nullptr;
249 }
250 DeltaR=DeltaR.unit();
251
252
253 //correct DeltaR from small deviations from orthogonality to DeltaR
254
255 Amg::Vector3D DeltaRcorrected=DeltaR-(DeltaR.dot(MomentumDir))*MomentumDir;
256
257 if ((DeltaR-DeltaRcorrected).mag()>1e-4)
258 {
259 ATH_MSG_WARNING( " DeltaR and MomentumDir are not orthogonal " );
260 ATH_MSG_DEBUG( std::setprecision(10) << " DeltaR-DeltaRcorrected: " << (DeltaR-DeltaRcorrected).mag() );
261 }
262
263 Amg::Vector3D YDir=MomentumDir.cross(DeltaRcorrected);
264
265 //store the impact 3d point
266 Amg::Vector3D vertex(x0+Rt*dCosPhiActual,y0+Rt*dSinPhiActual,z0-Rt*cotTheta*phiActual);
267
268 ATH_MSG_VERBOSE( "final minimal distance is: " << std::hypot(x0-xc+Rt*dCosPhiActual,
269 y0-yc+Rt*dSinPhiActual,
270 z0-zc-Rt*cotTheta*phiActual));
271
272 ATH_MSG_DEBUG( "POCA in 3D is: " << vertex );
273
274
275 //store the plane...
276 ATH_MSG_VERBOSE( "plane to which to extrapolate X " << DeltaRcorrected << " Y " << YDir << " Z " << MomentumDir );
277
278 Amg::Transform3D thePlane(DeltaRcorrected, YDir, MomentumDir, *theVertex);
279
280#ifdef IMPACTPOINT3DESTIMATOR_DEBUG
281 std::cout << "the translation is, directly from Transform3d: " << thePlane.getTranslation() << endmsg;
282#endif
283 return std::make_unique<PlaneSurface>(thePlane);
284 }//end of estimate 3dIP method
285
286 bool
287 ImpactPoint3dEstimator::addIP3dAtaPlane(const EventContext& ctx, VxTrackAtVertex & vtxTrack,const Amg::Vector3D & vertex) const
288 {
289 if (vtxTrack.initialPerigee()) {
290 const AtaPlane* myPlane=IP3dAtaPlane(ctx,vtxTrack,vertex);
291 if (myPlane)
292 {
293 vtxTrack.setImpactPoint3dAtaPlane(myPlane);
294 return true;
295 }
296 } else { //for neutrals
298 if (myPlane) {
299 ATH_MSG_VERBOSE ("Adding plane: " << myPlane->associatedSurface() );
300 vtxTrack.setImpactPoint3dNeutralAtaPlane(myPlane);
301 return true;
302 }
303 }
304 return false;
305 }
306
307
308 const Trk::AtaPlane *
309 ImpactPoint3dEstimator::IP3dAtaPlane(const EventContext& ctx,VxTrackAtVertex & vtxTrack,const Amg::Vector3D & vertex) const
310 {
311 if (!vtxTrack.initialPerigee() && vtxTrack.initialNeutralPerigee())
312 ATH_MSG_WARNING( "Calling ImpactPoint3dEstimator::IP3dAtaPlane cannot return NeutralAtaPlane" );
313 std::unique_ptr<PlaneSurface> theSurfaceAtIP;
314 try
315 {
316 double distance = 0;
317 theSurfaceAtIP = Estimate3dIP(ctx,vtxTrack.initialPerigee(),&vertex,distance);
318 }
320 {
321 ATH_MSG_WARNING( " ImpactPoint3dEstimator failed to find minimum distance between track and vertex seed: " << err.p );
322 return nullptr;
323 }
324 if(!theSurfaceAtIP){
325 ATH_MSG_WARNING( " ImpactPoint3dEstimator failed to find minimum distance and returned 0 " );
326 return nullptr;
327 }
328#ifdef ImpactPoint3dAtaPlaneFactory_DEBUG
329 ATH_MSG_VERBOSE( "Original perigee was: " << *(vtxTrack.initialPerigee()) );
330 ATH_MSG_VERBOSE( "The resulting surface is: " << *theSurfaceAtIP );
331#endif
332 const auto* pTrackPar = m_extrapolator->extrapolate(
333 ctx,
334 *(vtxTrack.initialPerigee()),
335 *theSurfaceAtIP).release();
336 if (const Trk::AtaPlane* res = dynamic_cast<const Trk::AtaPlane *>(pTrackPar); res){
337 return res;
338 }
339 ATH_MSG_DEBUG("TrackParameters ptr returned from extrapolate could not be cast to Trk::AtaPlane* in IP3dAtaPlane(..)");
340 return nullptr;
341 }
342
343
345 ImpactPoint3dEstimator::IP3dNeutralAtaPlane(const EventContext& ctx,const NeutralParameters * initNeutPerigee,const Amg::Vector3D & vertex) const
346 {
347 std::unique_ptr<PlaneSurface> theSurfaceAtIP;
348
349 try
350 {
351 double distance = 0;
352 theSurfaceAtIP = Estimate3dIP(ctx,initNeutPerigee,&vertex,distance);
353 }
355 {
356 ATH_MSG_WARNING( " ImpactPoint3dEstimator failed to find minimum distance between track and vertex seed: " << err.p );
357 return nullptr;
358 }
359 if(!theSurfaceAtIP){
360 ATH_MSG_WARNING( " ImpactPoint3dEstimator failed to find minimum distance and returned 0 " );
361 return nullptr;
362 }
363#ifdef ImpactPoint3dAtaPlaneFactory_DEBUG
364 ATH_MSG_VERBOSE( "Original neutral perigee was: " << *initNeutPerigee );
365 ATH_MSG_VERBOSE( "The resulting surface is: " << *theSurfaceAtIP );
366#endif
367
368 const Trk::NeutralAtaPlane* res = nullptr;
369 std::unique_ptr<const Trk::NeutralParameters> tmp = m_extrapolator->extrapolate(*initNeutPerigee,*theSurfaceAtIP);
370 if(dynamic_cast<const Trk::NeutralAtaPlane*> (tmp.get())){
371 res = static_cast<const Trk::NeutralAtaPlane*> (tmp.release());
372 }
373 return res;
374 }
375
376
377}
Scalar mag() const
mag method
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::pair< std::vector< unsigned int >, bool > res
void getInitializedCache(MagField::AtlasFieldCache &cache) const
get B field cache for evaluation as a function of 2-d or 3-d position.
Local cache for magnetic field (based on MagFieldServices/AtlasFieldSvcTLS.h).
void getField(const double *ATH_RESTRICT xyz, double *ATH_RESTRICT bxyz, double *ATH_RESTRICT deriv=nullptr)
get B field value at given position xyz[3] is in mm, bxyz[3] is in kT if deriv[9] is given,...
virtual StatusCode finalize() override
virtual bool addIP3dAtaPlane(const EventContext &ctx, VxTrackAtVertex &, const Amg::Vector3D &vertex) const override
Actual estimate method, changing the state of Trk::VxTrackAtVertex.
virtual const Trk::NeutralAtaPlane * IP3dNeutralAtaPlane(const EventContext &ctx, const NeutralParameters *initNeutPerigee, const Amg::Vector3D &vertex) const override
virtual StatusCode initialize() override
virtual std::unique_ptr< PlaneSurface > Estimate3dIP(const EventContext &ctx, const Trk::TrackParameters *trackPerigee, const Amg::Vector3D *theVertex, double &distance) const override
SG::ReadCondHandleKey< AtlasFieldCacheCondObj > m_fieldCacheCondObjInputKey
virtual const Trk::AtaPlane * IP3dAtaPlane(const EventContext &ctx, VxTrackAtVertex &vtxTrack, const Amg::Vector3D &vertex) const override
This method creates the ImpactPoint3dAtaPlane as the parameters of the track at the point of closest ...
std::unique_ptr< PlaneSurface > Estimate3dIPNoCurvature(const T *, const Amg::Vector3D *theVertex, double &distance) const
ImpactPoint3dEstimator(const std::string &t, const std::string &n, const IInterface *p)
Default constructor due to Athena interface.
ToolHandle< Trk::IExtrapolator > m_extrapolator
virtual const Surface & associatedSurface() const override=0
Access to the Surface associated to the Parameters.
virtual const S & associatedSurface() const override final
Access to the Surface method.
Class describing the Line to which the Perigee refers to.
const Amg::Vector3D & center() const
Returns the center position of the Surface.
The VxTrackAtVertex is a common class for all present TrkVertexFitters The VxTrackAtVertex is designe...
void setImpactPoint3dAtaPlane(const AtaPlane *myIP3dAtaPlane)
Set method for ImpactPoint3dAtaPlane.
const NeutralParameters * initialNeutralPerigee(void) const
Access to the initial perigee parameters of trajectory.
const TrackParameters * initialPerigee(void) const
Access to the initial perigee parameters of trajectory.
void setImpactPoint3dNeutralAtaPlane(const NeutralAtaPlane *myIP3dNeutralAtaPlane)
Set method for ImpactPoint3dNeutralAtaPlane.
Eigen::Affine3d Transform3D
Eigen::Matrix< double, 3, 1 > Vector3D
Ensure that the ATLAS eigen extensions are properly loaded.
ParametersT< TrackParametersDim, Charged, PerigeeSurface > Perigee
ParametersBase< NeutralParametersDim, Neutral > NeutralParameters
@ phi0
Definition ParamDefs.h:65
@ theta
Definition ParamDefs.h:66
@ qOverP
perigee
Definition ParamDefs.h:67
@ d0
Definition ParamDefs.h:63
@ z0
Definition ParamDefs.h:64
ParametersT< NeutralParametersDim, Neutral, PlaneSurface > NeutralAtaPlane
ParametersBase< TrackParametersDim, Charged > TrackParameters
ParametersT< TrackParametersDim, Charged, PlaneSurface > AtaPlane