ATLAS Offline Software
Loading...
Searching...
No Matches
VertexPointEstimator.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5/***************************************************************************
6 VertexPointEstimator.cxx - Description
7 -------------------
8 begin : 01-01-2008
9 authors : Tatjana Lenz, Thomas Koffas
10 email : tatjana.lenz@cern.ch, Thomas.Koffas@cern.ch
11 changes : M.ELSING
12***************************************************************************/
14
15constexpr double pi = M_PI;
16constexpr double twopi = 2.*pi;
17
18namespace InDet {
19
20 //define some statics
21 static const InterfaceID IID_IVertexPointEstimator("InDet::VertexPointEstimator", 1, 0);
22 const double VertexPointEstimator::s_bmagnt = 2.083;
23
24 // ----------------------------------
25 VertexPointEstimator::VertexPointEstimator(const std::string& type, const std::string& name, const IInterface* parent) :
26 AthAlgTool(type, name, parent)
27 {
28 declareInterface<VertexPointEstimator>(this);
29 }
30
31 // ----------------------------------
32 const InterfaceID& VertexPointEstimator::interfaceID() {
34 }
35
36 // ----------------------------------
38 return StatusCode::SUCCESS;
39 }
40
41 // ----------------------------------
43 return StatusCode::SUCCESS;
44 }
45
46 // ----------------------------------
50 const Trk::Perigee *per2,
51 unsigned int flag,
52 int& errorcode) const
53 {
54 float deltaPhi;
55 float deltaR;
56 return intersectionImpl (per1, per2, flag, errorcode, deltaPhi, deltaR);
57 }
58
59
62 const Trk::Perigee *per2,
63 unsigned int flag,
64 int& errorcode,
65 Values_t& decors) const
66 {
67 decors.clear();
68 return intersectionImpl (per1, per2, flag, errorcode,
69 decors["deltaPhiTracks"],
70 decors["DR1R2"]);
71 }
72
73
74 std::vector<std::string> VertexPointEstimator::decorKeys()
75 {
76 return {"deltaPhiTracks", "DR1R2"};
77 }
78
79
80 // ----------------------------------
84 const Trk::Perigee *per2,
85 unsigned int flag,
86 int& errorcode,
87 float& deltaPhi,
88 float& deltaR) const
89 {
90 /*
91 Calculates the initial approximation to the vertex position. Based on CTVMFT.
92 Tries to intersect circles that are (R,Phi) projections of the helical trajectories of the two tracks
93 If the circles intersect, the intersection with smaller z difference between the 2 helices extrapolated to the intersection is chosen.
94 If the circles do not intersect, the vertex approximation is taken as the point of closest approach of the two circles.
95 per1 should be that of the track with the smaller radius
96 */
97 Amg::Vector3D intPoint(0.,0.,0.);
98 double aconst = -1.49898*s_bmagnt; //is it correct???????????????????
99 double DRMAX = 0.; // maximum XY separation, non-intersecting circles
100 double DZMAX = 0.; // maximum allowed track Z separation at the vertex
101 double RVMAX = 0.; // maximum allowed vertex radius
102 double minArcLength(0.);
103 double maxArcLength(0.);
104 double minDr(0.);
105 double maxDr(0.);
106 double maxHl(0.);
107 double maxPhi(0.);
108 if (flag <= 2) {
109 DRMAX = m_maxDR[flag]; //maximum XY separation, non-intersecting circles
110 DZMAX = m_maxDZ[flag]; //maximum allowed track Z separation at the vertex
111 RVMAX = m_maxR[flag] ; //maximum allowed vertex radius
112 minArcLength = m_minArcLength[flag];
113 maxArcLength = m_maxArcLength[flag];
114 minDr = m_minDr[flag];
115 maxDr = m_maxDr[flag];
116 maxHl = m_maxHl[flag];
117 maxPhi= m_maxPhi[flag];
118 }
119
120 double d0[2];
121 double z0[2];
122 double phi[2];
123 double cotTheta[2];
124 double qOverPt[2];
125 double RC[2];
126 double XC[2];
127 double YC[2];
128 double RA[2];
129 double AB[2];
130 double XSVI[2];
131 double YSVI[2];
132 double ZSVI[2];
133 double RVI[2];
134 double DZSVI[2];
135 double SS1[2];
136 double SS2[2];
137 double ZZ1[2];
138 double ZZ2[2];
139
140 double X=0.;
141 double Y=0.;
142 double Z=0.;
143 for (int I=0; I<2; ++I) {
144 d0[I] = -999.;
145 z0[I] = -999.;
146 phi[I] = -999.;
147 cotTheta[I] = -999.;
148 qOverPt[I] = -999.;
149 RC[I] = -999.;
150 XC[I] = -999.;
151 YC[I] = -999.;
152 RA[I] = +999.;
153 AB[I] = +999.;
154 XSVI[I] = 0.;
155 YSVI[I] = 0.;
156 ZSVI[I] = 0.;
157 RVI[I] = 2.0*RVMAX;
158 DZSVI[I] = 2.0*DZMAX;
159 SS1[I] = 0.;
160 SS2[I] = 0.;
161 ZZ1[I] = 0.;
162 ZZ2[I] = 0.;
163 }
164
165 double U(0.);
166 const AmgVector(5)& perParam1 = per1->parameters();
167 double pt1 = fabs(sin(perParam1[Trk::theta])/perParam1[Trk::qOverP]);
168 const AmgVector(5)& perParam2 = per2->parameters();
169 double pt2 = fabs(sin(perParam2[Trk::theta])/perParam2[Trk::qOverP]);
170 if (pt1 < pt2) {
171 d0[0] = perParam1[Trk::d0]; d0[1] = perParam2[Trk::d0];
172 z0[0] = perParam1[Trk::z0]; z0[1] = perParam2[Trk::z0];
173 phi[0] = perParam1[Trk::phi]; phi[1] = perParam2[Trk::phi];
174 double theta1 = perParam1[Trk::theta]; cotTheta[0] = 1./tan(theta1);
175 double theta2 = perParam2[Trk::theta]; cotTheta[1] = 1./tan(theta2);
176 double qOverP1 = perParam1[Trk::qOverP]; qOverPt[0] = qOverP1/sin(perParam1[Trk::theta]);
177 double qOverP2 = perParam2[Trk::qOverP]; qOverPt[1] = qOverP2/sin(perParam2[Trk::theta]);
178 RC[0] = 10.*0.5/(qOverPt[0]*aconst); RC[1] = 10.*0.5/(qOverPt[1]*aconst);//Radius of curvature
179 U = RC[0] + d0[0];
180 XC[0] = -1*U*sin(phi[0]); //Circle center x-coordinate
181 YC[0] = U*cos(phi[0]); //Circle center y-coordinate
182 RA[0] = fabs(RC[0]);
183 U = RC[1] + d0[1];
184 XC[1] = -1*U*sin(phi[1]); // Circle center x-coordinate
185 YC[1] = U*cos(phi[1]); // Circle center y-coordinate
186 RA[1] = fabs(RC[1]);
187 } else {
188 d0[0] = perParam2[Trk::d0]; d0[1] = perParam1[Trk::d0];
189 z0[0] = perParam2[Trk::z0]; z0[1] = perParam1[Trk::z0];
190 phi[0] = perParam2[Trk::phi]; phi[1] = perParam1[Trk::phi];
191 double theta2 = perParam2[Trk::theta]; cotTheta[0] = 1./tan(theta2);
192 double theta1 = perParam1[Trk::theta]; cotTheta[1] = 1./tan(theta1);
193 double qOverP2 = perParam2[Trk::qOverP]; qOverPt[0] = qOverP2/sin(perParam2[Trk::theta]);
194 double qOverP1 = perParam1[Trk::qOverP]; qOverPt[1] = qOverP1/sin(perParam1[Trk::theta]);
195 RC[0] = 10.*0.5/(qOverPt[0]*aconst); RC[1] = 10.*0.5/(qOverPt[1]*aconst);//Radius of curvature
196 double U = RC[0] + d0[0];
197 XC[0] = -1*U*sin(phi[0]); //Circle center x-coordinate
198 YC[0] = U*cos(phi[0]); //Circle center y-coordinate
199 RA[0] = fabs(RC[0]);
200 U = RC[1] + d0[1];
201 XC[1] = -1*U*sin(phi[1]); // Circle center x-coordinate
202 YC[1] = U*cos(phi[1]); // Circle center y-coordinate
203 RA[1] = fabs(RC[1]);
204 }
205
206 // get the circle centre separation
207 double DX = XC[1] - XC[0];
208 double DY = YC[1] - YC[0];
209 double D = DX*DX + DY*DY;
210 D = sqrt(D);
211 if (D == 0.) {
212 ATH_MSG_DEBUG("Concentric circles, should not happen return (0,0,0)");
213 errorcode = 1;
214 if(m_returnOnError) return intPoint;
215 }
216 U = D - RA[0] - RA[1]; // signed separation, if > 0., the circles do not intersect, if < 0., they might
217 // rotate, translate to a system, where the two circle centres lie on the X axis
218
219 //New cut from Mauro
220 deltaR = U; double PHI = -99999.;
221 double hl = areaVar(XC[0], YC[0], RA[0], XC[1], YC[1], RA[1], PHI);
222
223 double COST = DX/D;
224 double SINT = DY/D;
225 double Y0 = (-1*XC[0]*YC[1] + XC[1]*YC[0])/D; //Translation along the y-axis
226 for (int I=0; I<2; ++I) {
227 AB[I] = COST*XC[I] + SINT*YC[I]; // The new circle center positions in the rotated system. Essentially the new x-coordinates, since the y-coordinates are 0.
228 }
229 U = (XC[1] + XC[0])*(XC[1] - XC[0]) + (YC[1] + YC[0])*(YC[1] - YC[0]);
230 double V = (RA[1] + RA[0])*(RA[1] - RA[0]);
231 double XX = 0.5*(U - V)/D; // X of intersection in rotated coordinate system (a+AB[0]). Again the y is 0.
232 if ((XX - AB[0])*(XX - AB[0]) > 0.) {
233 U = sqrt((XX - AB[0])*(XX - AB[0])); //This is the a in my logbook
234 }
235 double YY2 = (RA[0] + U)*(RA[0] - U); //This is the h^2 in my logbook
236 double YY;
237 int nsol = 0;
238 if (YY2 > 0.) {
239
240 // the circles intersect
241 YY = sqrt(YY2); // two intersection points (+/- Y) or (+/- h) in my logbook
242
243 for (int I=0; I<2; ++I) {
244 U = YY + Y0; // invert the translation
245 XSVI[I] = COST*XX - SINT*U; // Invert the rotation
246 YSVI[I] = SINT*XX + COST*U;
247 YY = -1*YY;
248 }
249
250 nsol = 2;
251
252 } else {
253 // circles do not intersect - find how close they approach each other
254 // in the XY plane and take the point half way between them
255 U = D - RA[0] - RA[1];
256 if (U > 0.) {
257 //Circles do not overlap at all and are outside from each other.See logbook
258 V = U;
259 XX = AB[1] + RA[1];
260
261 if (AB[0] < AB[1]) XX = AB[0] + RA[0];
262 } else {
263 //Circles inside each other but off-centered.See logbook
264 if (AB[0] < AB[1]) { //To the left
265 XX = AB[1] - RA[1];
266 V = AB[0] - RA[0] - XX;
267 } else {
268 //To the right
269 XX = AB[0] + RA[0];
270 V = AB[1] + RA[1] - XX;
271 }
272 }
273 XX = XX + 0.5*V;
274 XSVI[0] = COST*XX - SINT*Y0; // rotate back to the original system
275 YSVI[0] = SINT*XX + COST*Y0; // one solution
276 nsol = 1;
277 if (V > DRMAX) {
278 //Cut if distance of minimum approach is too big
279 ATH_MSG_DEBUG("XY distance of minimum approach is too large, return (0,0,0)");
280 errorcode = 2;
281 if(m_returnOnError) return intPoint;
282 }
283 }
284
285 for (int J=0; J<nsol; ++J) { // loop over solutions
286 U = (XSVI[J] - XC[0])/RC[0];
287 V = -1*(YSVI[J] - YC[0])/RC[0];
288 U = atan2(U,V) - phi[0]; // turning angle from the track origin
289 if (U < -1*pi) U = U + twopi;
290 if (U > pi) U = U - twopi;
291 SS1[J] = RC[0]*U; // arc length
292 ZZ1[J] = z0[0] + SS1[J]*cotTheta[0];
293 U = (XSVI[J] - XC[1])/RC[1];
294 V = -1*(YSVI[J] - YC[1])/RC[1];
295 U = atan2(U,V) - phi[1];
296 if (U < -1*pi) U = U + twopi;
297 if (U > pi) U = U - twopi;
298 SS2[J] = RC[1]*U;
299 ZZ2[J] = z0[1] + SS1[J]*cotTheta[1];
300 RVI[J] = sqrt(XSVI[J]*XSVI[J] + YSVI[J]*YSVI[J]);
301 DZSVI[J] = ZZ2[J] - ZZ1[J];
302 }
303 ZSVI[0] = 0.5*(ZZ1[0] + ZZ2[0]);
304 ZSVI[1] = 0.5*(ZZ1[1] + ZZ2[1]);
305
306 if (std::min(RVI[0],RVI[1]) > RVMAX) { // check the vertex radius is acceptable
307 ATH_MSG_DEBUG("Unacceptable vertex radius");
308 errorcode = 3;
309 }
310
311 if (std::min(fabs(DZSVI[0]),fabs(DZSVI[1])) > DZMAX) { // check the Z difference
312 ATH_MSG_DEBUG("Unacceptable Z difference");
313 errorcode = 4;
314 }
315
316 double A = std::min(SS1[0],SS2[0]); // minimum track arc length, solution 1
317 double B = std::min(SS1[1],SS2[1]); // minimum track arc length, solution 2
318 if (std::max(A,B) < minArcLength || std::max(A,B) > maxArcLength) { // limit the minimum arc length
319 ATH_MSG_DEBUG("Unacceptable arc length");
320 errorcode = 5;
321 if(m_returnOnError) return intPoint;
322 }
323
324 int J = 0;
325 if ( nsol == 2 && (fabs(DZSVI[1]) < fabs(DZSVI[0])) ) J = 1;
326
327 X = XSVI[J];
328 Y = YSVI[J];
329 Z = ZSVI[J];
330 intPoint(0)=X; intPoint(1)=Y; intPoint(2)=Z;
331
332 if(deltaR>maxDr || deltaR<minDr){
333 ATH_MSG_DEBUG("Unaceptable circle distance");
334 errorcode = 6;
335 if(m_returnOnError) return intPoint;
336 }
337
338 if(hl>maxHl){
339 ATH_MSG_DEBUG("Unacceptable h/D ratio");
340 errorcode = 7;
341 if(m_returnOnError) return intPoint;
342 }
343
344 deltaPhi = PHI; // quick fix: cannot get rid of (double) PHI as it is passed by ref and deltaPhi is a float
345 if(deltaPhi>maxPhi){
346 ATH_MSG_DEBUG("Unacceptable difference in phi");
347 errorcode = 8;
348 if(m_returnOnError) return intPoint;
349 }
350
351 return intPoint;
352 }
353
354 // ----------------------------------------------------------------
355 double VertexPointEstimator::areaVar(double xc1, double yc1, double r1, double xc2, double yc2, double r2, double& phi)
356 {
357 double ret = -999999;
358 double xi1;
359 double yi1;
360 double xi2;
361 double yi2;
362 if (circleIntersection( xc1, yc1, r1, xc2, yc2, r2, xi1, yi1, xi2, yi2 ))
363 {
364 double h = 0.5*(sqrt( pow(xi1-xi2,2) + pow(yi1-yi2,2) ));
365 double l = sqrt( pow(xc1-xc2,2) + pow(yc1-yc2,2) );
366 if (l!=0) ret = h/l;
367
368 double norm1 = sqrt((xi1-xc1)*(xi1-xc1)+(yi1-yc1)*(yi1-yc1));
369 double norm2 = sqrt((xi1-xc2)*(xi1-xc2)+(yi1-yc2)*(yi1-yc2));
370 if((norm1!=0.) && (norm2!=0.)){ //rejecting pathology
371 //centers at the origin
372 norm1 = 1./norm1;
373 norm2 = 1./norm2;
374 double xa = (xi1-xc1)*norm1;
375 double ya = (yi1-yc1)*norm1;
376 double xb = (xi1-xc2)*norm2;
377 double yb = (yi1-yc2)*norm2;
378 double costheta = xa*xb + ya*yb;
379 phi = M_PI-std::acos(costheta);
380 }
381 }
382 return ret;
383 }
384
385 //recreate the logic of TMath::Acos
386 double internal_acos(double x)
387 {
388 if (x < -1.) return M_PI;
389 if (x > 1.) return 0;
390 return std::acos(x);
391 }
392
393
394 // ----------------------------------
395 double VertexPointEstimator::areaVar(double xc1, double yc1, double r1, double xc2, double yc2, double r2, double& h, double& hl, double &ddphi)
396 {
397 double ret = -999999;
398 double xi1;
399 double yi1;
400 double xi2;
401 double yi2;
402 h = 0;
403 hl = 0;
404 ddphi = 0.;
405 if (circleIntersection( xc1, yc1, r1, xc2, yc2, r2, xi1, yi1, xi2, yi2 )) {
406
407 // the two triangles have identical area
408 ret = areaTriangle(xc1,yc1, xc2,yc2, xi1,yi1) ;
409
410 h = 0.5*(sqrt( pow(xi1-xi2,2) + pow(yi1-yi2,2) ));
411
412 double l = sqrt( pow(xc1-xc2,2) + pow(yc1-yc2,2) );
413 if (l!=0) hl = h/l;
414
415 // |AxB| = |A||B|sin(phi) => sinphi = |AxB| / |A||B|
416 // (xi1,yi1) = first intersection point; this is the one I use
417 // (xi2,yi2) = second intersection point; this is symmetric to the first
418 // (xc1,yc1) = centre first circle
419 // (xc2,yc2) = centre second circle
420 double norm1 = sqrt(pow(xi1-xc1,2)+pow(yi1-yc1,2));
421 double norm2 = sqrt(pow(xi1-xc2,2)+pow(yi1-yc2,2));
422 if ((norm1 != 0) && (norm2 != 0)){ // rejecting pathology
423 // centres at the origin
424 double xa = (xi1 - xc1)/norm1;
425 double ya = (yi1 - yc1)/norm1;
426 double xb = (xi1 - xc2)/norm2;
427 double yb = (yi1 - yc2)/norm2;
428 double costheta = xa*xb +ya*yb;
429 double phi = M_PI-internal_acos(costheta);
430 ddphi = phi;
431 }
432 }
433
434 return ret;
435 }
436
437 // ----------------------------------
438 bool VertexPointEstimator::circleIntersection(double xc1, double yc1, double r1,
439 double xc2, double yc2, double r2,
440 double& xi1, double& yi1,
441 double& xi2, double& yi2)
442 {
443 // Calculate the intersection of the two circles:
444 //
445 // (x-xc1)^2 + (y-yc1)^2 = R1^2
446 // (x-xc2)^2 + (y-yc2)^2 = R2^2
447
448 xi1 = -999999999.;
449 xi2 = -999999999.;
450 yi1 = -999999999.;
451 yi2 = -999999999.;
452
453 if ( yc1 != yc2) {
454 double A = (xc1 - xc2) / (yc2- yc1);
455 double B = (r1*r1 - r2*r2 - xc1*xc1 + xc2*xc2 - yc1*yc1 + yc2*yc2) / 2. / ( yc2 -yc1);
456 double a = 1 + A*A;
457 double b = 2*A*B - 2*xc1 -2*A*yc1;
458 double c = B*B - 2*B*yc1 + xc1*xc1 + yc1*yc1 - r1*r1;
459 if (secondDegree(a,b,c,xi1,xi2) ){
460 yi1 = A*xi1 + B;
461 yi2 = A*xi2 + B;
462 return true;
463 }
464 return false;
465 }
466 if (xc1 != xc2){
467 double A = (yc1 - yc2) / (xc2- xc1);
468 double B = (r1*r1 - r2*r2 - xc1*xc1 + xc2*xc2 - yc1*yc1 + yc2*yc2) / 2. / ( xc2 -xc1);
469 double a = 1 + A*A;
470 double b = 2*A*B - 2*yc1 -2*A*xc1;
471 double c = B*B - 2*B*xc1 + xc1*xc1 + yc1*yc1 - r1*r1;
472 if (secondDegree(a,b,c,yi1,yi2) ){
473 xi1 = A*yi1 + B;
474 xi2 = A*yi2 + B;
475 return true;
476 }
477 return false;
478 }
479
480 // circles are concentric and we don't care
481 return false;
482
483 return false;
484 }
485
486 // ----------------------------------
487 bool VertexPointEstimator::secondDegree(double a, double b, double c, double& y1, double& y2)
488 {
489 y1 = -999999999;
490 y2 = -999999999;
491 double discr = b*b - 4*a*c;
492 if (discr < 0) return false;
493 y1 = (-b+sqrt(discr))/2./a;
494 y2 = (-b-sqrt(discr))/2./a;
495 return true;
496
497 }
498
499 // ----------------------------------
500 double VertexPointEstimator::areaTriangle(double a, double b, // double c,
501 double d, double e, // double f,
502 double g, double h) { // double i)
503 double c = 1;
504 double f = 1;
505 double i = 1;
506 return fabs(0.5* ( (a*e*i + d*h*c + b*f*g) - (g*e*c + d*b*i + a*f*h) ) );
507 }
508
509}
#define M_PI
Scalar deltaPhi(const MatrixBase< Derived > &vec) const
Scalar deltaR(const MatrixBase< Derived > &vec) const
Scalar phi() const
phi method
#define ATH_MSG_DEBUG(x)
#define AmgVector(rows)
static Double_t a
#define I(x, y, z)
Definition MD5.cxx:116
#define pi
#define x
constexpr double twopi
constexpr int pow(int base, int exp) noexcept
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Header file for AthHistogramAlgorithm.
static double areaVar(double, double, double, double, double, double, double &)
static std::vector< std::string > decorKeys()
Return list of keys used for decorations.
static bool secondDegree(double, double, double, double &, double &)
Amg::Vector3D intersectionImpl(const Trk::Perigee *per1, const Trk::Perigee *per2, unsigned int flag, int &errorcode, float &deltaPhi, float &deltaR) const
internal implementation
static bool circleIntersection(double, double, double, double, double, double, double &, double &, double &, double &)
virtual StatusCode initialize() override
VertexPointEstimator(const std::string &type, const std::string &name, const IInterface *parent)
static const InterfaceID & interfaceID()
static double areaTriangle(double, double, double, double, double, double)
Amg::Vector3D getCirclesIntersectionPoint(const Trk::Perigee *per1, const Trk::Perigee *per2, unsigned int flag, int &errorcode) const
Get intersection point of two track helices.
virtual StatusCode finalize() override
std::map< std::string, float > Values_t
Eigen::Matrix< double, 3, 1 > Vector3D
Primary Vertex Finder.
static const InterfaceID IID_IVertexPointEstimator("InDet::VertexPointEstimator", 1, 0)
double internal_acos(double x)
ParametersT< TrackParametersDim, Charged, PerigeeSurface > Perigee
@ theta
Definition ParamDefs.h:66
@ qOverP
perigee
Definition ParamDefs.h:67
@ phi
Definition ParamDefs.h:75
@ d0
Definition ParamDefs.h:63
@ z0
Definition ParamDefs.h:64
hold the test vectors and ease the comparison