Project
Loading...
Searching...
No Matches
TrackParametrization.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
16
20#include <MathUtils/Cartesian.h>
21#include <GPUCommonLogger.h>
22
23#ifndef GPUCA_GPUCODE_DEVICE
24#include <iostream>
25#endif
26
27#ifndef GPUCA_ALIGPUCODE
28#include <fmt/printf.h>
29#endif
30
31using namespace o2::gpu;
32using namespace o2::track;
33
34//______________________________________________________________
35template <typename value_T>
36GPUd() TrackParametrization<value_T>::TrackParametrization(const dim3_t& xyz, const dim3_t& pxpypz, int charge, bool sectorAlpha, const PID pid)
37 : mX{0.f}, mAlpha{0.f}, mP{0.f}
38{
39 // construct track param from kinematics
40
41 // Alpha of the frame is defined as:
42 // sectorAlpha == false : -> angle of pt direction
43 // sectorAlpha == true : -> angle of the sector from X,Y coordinate for r>1
44 // angle of pt direction for r==0
45 //
46 //
47 constexpr value_t kSafe = 1e-5;
48 value_t radPos2 = xyz[0] * xyz[0] + xyz[1] * xyz[1];
49 value_t alp = 0;
50 if (sectorAlpha || radPos2 < 1) {
51 alp = gpu::CAMath::ATan2(pxpypz[1], pxpypz[0]);
52 } else {
53 alp = gpu::CAMath::ATan2(xyz[1], xyz[0]);
54 }
55 if (sectorAlpha) {
56 alp = math_utils::detail::angle2Alpha<value_t>(alp);
57 }
58 //
59 value_t sn, cs;
60 math_utils::detail::sincos(alp, sn, cs);
61 // protection against cosp<0
62 if (cs * pxpypz[0] + sn * pxpypz[1] < 0) {
63 LOG(debug) << "alpha from phiPos() will invalidate this track parameters, overriding to alpha from phi()";
64 alp = gpu::CAMath::ATan2(pxpypz[1], pxpypz[0]);
65 if (sectorAlpha) {
66 alp = math_utils::detail::angle2Alpha<value_t>(alp);
67 }
68 math_utils::detail::sincos(alp, sn, cs);
69 }
70
71 // protection: avoid alpha being too close to 0 or +-pi/2
72 if (gpu::CAMath::Abs(sn) < 2 * kSafe) {
73 if (alp > 0) {
74 alp += alp < constants::math::PIHalf ? 2 * kSafe : -2 * kSafe;
75 } else {
76 alp += alp > -constants::math::PIHalf ? -2 * kSafe : 2 * kSafe;
77 }
78 math_utils::detail::sincos(alp, sn, cs);
79 } else if (gpu::CAMath::Abs(cs) < 2 * kSafe) {
80 if (alp > 0) {
81 alp += alp > constants::math::PIHalf ? 2 * kSafe : -2 * kSafe;
82 } else {
83 alp += alp > -constants::math::PIHalf ? 2 * kSafe : -2 * kSafe;
84 }
85 math_utils::detail::sincos(alp, sn, cs);
86 }
87 // get the vertex of origin and the momentum
88 dim3_t ver{xyz[0], xyz[1], xyz[2]};
89 dim3_t mom{pxpypz[0], pxpypz[1], pxpypz[2]};
90 //
91 // Rotate to the local coordinate system
92 math_utils::detail::rotateZ<value_t>(ver, -alp);
93 math_utils::detail::rotateZ<value_t>(mom, -alp);
94 //
95 value_t ptI = 1.f / gpu::CAMath::Sqrt(mom[0] * mom[0] + mom[1] * mom[1]);
96 mX = ver[0];
97 mAlpha = alp;
98 mP[kY] = ver[1];
99 mP[kZ] = ver[2];
100 mP[kSnp] = mom[1] * ptI;
101 mP[kTgl] = mom[2] * ptI;
102 mAbsCharge = gpu::CAMath::Abs(charge);
103 mP[kQ2Pt] = charge ? ptI * charge : ptI;
104 mPID = pid;
105 //
106 if (gpu::CAMath::Abs(1 - getSnp()) < kSafe) {
107 mP[kSnp] = 1.f - kSafe; // Protection
108 } else if (gpu::CAMath::Abs(-1 - getSnp()) < kSafe) {
109 mP[kSnp] = -1.f + kSafe; // Protection
110 }
111 //
112}
113
114//_______________________________________________________
115template <typename value_T>
116GPUd() bool TrackParametrization<value_T>::getPxPyPzGlo(dim3_t& pxyz) const
117{
118 // track momentum
119 if (gpu::CAMath::Abs(getQ2Pt()) < constants::math::Almost0 || gpu::CAMath::Abs(getSnp()) > constants::math::Almost1) {
120 return false;
121 }
122 value_t cs, sn, pt = getPt();
123 value_t r = gpu::CAMath::Sqrt((1.f - getSnp()) * (1.f + getSnp()));
124 math_utils::detail::sincos(getAlpha(), sn, cs);
125 pxyz[0] = pt * (r * cs - getSnp() * sn);
126 pxyz[1] = pt * (getSnp() * cs + r * sn);
127 pxyz[2] = pt * getTgl();
128 return true;
129}
130
131//____________________________________________________
132template <typename value_T>
133GPUd() bool TrackParametrization<value_T>::getPosDirGlo(std::array<value_t, 9>& posdirp) const
134{
135 // fill vector with lab x,y,z,px/p,py/p,pz/p,p,sinAlpha,cosAlpha
136 value_t ptI = getPtInv();
137 value_t snp = getSnp();
138 if (gpu::CAMath::Abs(snp) > constants::math::Almost1) {
139 return false;
140 }
141 value_t &sn = posdirp[7], &cs = posdirp[8];
142 value_t csp = gpu::CAMath::Sqrt((1.f - snp) * (1.f + snp));
143 value_t cstht = gpu::CAMath::Sqrt(1.f + getTgl() * getTgl());
144 value_t csthti = 1.f / cstht;
145 math_utils::detail::sincos(getAlpha(), sn, cs);
146 posdirp[0] = getX() * cs - getY() * sn;
147 posdirp[1] = getX() * sn + getY() * cs;
148 posdirp[2] = getZ();
149 posdirp[3] = (csp * cs - snp * sn) * csthti; // px/p
150 posdirp[4] = (snp * cs + csp * sn) * csthti; // py/p
151 posdirp[5] = getTgl() * csthti; // pz/p
152 posdirp[6] = cstht / ptI; // p
153 return true;
154}
155
156//______________________________________________________________
157template <typename value_T>
158GPUd() bool TrackParametrization<value_T>::rotateParam(value_t alpha)
159{
160 // rotate to alpha frame
161 if (gpu::CAMath::Abs(getSnp()) > constants::math::Almost1) {
162 LOGP(debug, "Precondition is not satisfied: |sin(phi)|>1 ! {:f}", getSnp());
163 return false;
164 }
165 //
166 math_utils::detail::bringToPMPi<value_t>(alpha);
167 //
168 value_t ca = 0, sa = 0;
169 math_utils::detail::sincos(alpha - getAlpha(), sa, ca);
170 value_t snp = getSnp(), csp = gpu::CAMath::Sqrt((1.f - snp) * (1.f + snp)); // Improve precision
171 // RS: check if rotation does no invalidate track model (cos(local_phi)>=0, i.e. particle
172 // direction in local frame is along the X axis
173 if ((csp * ca + snp * sa) < 0) {
174 // LOGF(warning,"Rotation failed: local cos(phi) would become {:.2f}", csp * ca + snp * sa);
175 return false;
176 }
177 //
178 value_t tmp = snp * ca - csp * sa;
179 if (gpu::CAMath::Abs(tmp) > constants::math::Almost1) {
180 LOGP(debug, "Rotation failed: new snp {:.2f}", tmp);
181 return false;
182 }
183 value_t xold = getX(), yold = getY();
184 mAlpha = alpha;
185 mX = xold * ca + yold * sa;
186 mP[kY] = -xold * sa + yold * ca;
187 mP[kSnp] = tmp;
188 return true;
189}
190
191//____________________________________________________________
192template <typename value_T>
193GPUd() bool TrackParametrization<value_T>::propagateParamTo(value_t xk, const dim3_t& b)
194{
195 //----------------------------------------------------------------
196 // Extrapolate this track params (w/o cov matrix) to the plane X=xk in the field b[].
197 //
198 // X [cm] is in the "tracking coordinate system" of this track.
199 // b[]={Bx,By,Bz} [kG] is in the Global coordidate system.
200 //----------------------------------------------------------------
201 value_t dx = xk - getX();
202 if (gpu::CAMath::Abs(dx) < constants::math::Almost0) {
203 return true;
204 }
205 // Do not propagate tracks outside the ALICE detector
206 if (gpu::CAMath::Abs(dx) > 1e5 || gpu::CAMath::Abs(getY()) > 1e5 || gpu::CAMath::Abs(getZ()) > 1e5) {
207 LOG(warning) << "Anomalous track, traget X:" << xk;
208 return false;
209 }
210 value_t crv = getCurvature(b[2]);
211 if (crv == 0.) {
212 return propagateParamTo(xk, 0.); // for the straight-line propagation use 1D field method
213 }
214
215 value_t x2r = crv * dx;
216 value_t f1 = getSnp(), f2 = f1 + x2r;
217 if (gpu::CAMath::Abs(f1) > constants::math::Almost1 || gpu::CAMath::Abs(f2) > constants::math::Almost1) {
218 return false;
219 }
220 value_t r1 = gpu::CAMath::Sqrt((1.f - f1) * (1.f + f1));
221 if (gpu::CAMath::Abs(r1) < constants::math::Almost0) {
222 return false;
223 }
224 value_t r2 = gpu::CAMath::Sqrt((1.f - f2) * (1.f + f2));
225 if (gpu::CAMath::Abs(r2) < constants::math::Almost0) {
226 return false;
227 }
228 value_t dy2dx = (f1 + f2) / (r1 + r2);
229 value_t step = (gpu::CAMath::Abs(x2r) < 0.05f) ? dx * gpu::CAMath::Abs(r2 + f2 * dy2dx) // chord
230 : 2.f * CAMath::ASin(0.5f * dx * gpu::CAMath::Sqrt(1.f + dy2dx * dy2dx) * crv) / crv; // arc
231 step *= gpu::CAMath::Sqrt(1.f + getTgl() * getTgl());
232 //
233 // get the track x,y,z,px/p,py/p,pz/p,p,sinAlpha,cosAlpha in the Global System
234 std::array<value_t, 9> vecLab{0.f};
235 if (!getPosDirGlo(vecLab)) {
236 return false;
237 }
238
239 // rotate to the system where Bx=By=0.
240 value_t bxy2 = b[0] * b[0] + b[1] * b[1];
241 value_t bt = gpu::CAMath::Sqrt(bxy2);
242 value_t cosphi = 1.f, sinphi = 0.f;
243 if (bt > constants::math::Almost0) {
244 cosphi = b[0] / bt;
245 sinphi = b[1] / bt;
246 }
247 value_t bb = gpu::CAMath::Sqrt(bxy2 + b[2] * b[2]);
248 value_t costet = 1.f, sintet = 0.f;
249 if (bb > constants::math::Almost0) {
250 costet = b[2] / bb;
251 sintet = bt / bb;
252 }
253 std::array<value_t, 7> vect{costet * cosphi * vecLab[0] + costet * sinphi * vecLab[1] - sintet * vecLab[2],
254 -sinphi * vecLab[0] + cosphi * vecLab[1],
255 sintet * cosphi * vecLab[0] + sintet * sinphi * vecLab[1] + costet * vecLab[2],
256 costet * cosphi * vecLab[3] + costet * sinphi * vecLab[4] - sintet * vecLab[5],
257 -sinphi * vecLab[3] + cosphi * vecLab[4],
258 sintet * cosphi * vecLab[3] + sintet * sinphi * vecLab[4] + costet * vecLab[5],
259 vecLab[6]};
260
261 // Do the helix step
262 value_t q = getCharge();
263 g3helx3(q * bb, step, vect);
264
265 // rotate back to the Global System
266 vecLab[0] = cosphi * costet * vect[0] - sinphi * vect[1] + cosphi * sintet * vect[2];
267 vecLab[1] = sinphi * costet * vect[0] + cosphi * vect[1] + sinphi * sintet * vect[2];
268 vecLab[2] = -sintet * vect[0] + costet * vect[2];
269
270 vecLab[3] = cosphi * costet * vect[3] - sinphi * vect[4] + cosphi * sintet * vect[5];
271 vecLab[4] = sinphi * costet * vect[3] + cosphi * vect[4] + sinphi * sintet * vect[5];
272 vecLab[5] = -sintet * vect[3] + costet * vect[5];
273
274 // rotate back to the Tracking System
275 value_t sinalp = -vecLab[7], cosalp = vecLab[8];
276 value_t t = cosalp * vecLab[0] - sinalp * vecLab[1];
277 vecLab[1] = sinalp * vecLab[0] + cosalp * vecLab[1];
278 vecLab[0] = t;
279 t = cosalp * vecLab[3] - sinalp * vecLab[4];
280 vecLab[4] = sinalp * vecLab[3] + cosalp * vecLab[4];
281 vecLab[3] = t;
282
283 // Do the final correcting step to the target plane (linear approximation)
284 value_t x = vecLab[0], y = vecLab[1], z = vecLab[2];
285 if (gpu::CAMath::Abs(dx) > constants::math::Almost0) {
286 if (gpu::CAMath::Abs(vecLab[3]) < constants::math::Almost0) {
287 return false;
288 }
289 dx = xk - vecLab[0];
290 x += dx;
291 y += vecLab[4] / vecLab[3] * dx;
292 z += vecLab[5] / vecLab[3] * dx;
293 }
294
295 // Calculate the track parameters
296 t = 1.f / gpu::CAMath::Sqrt(vecLab[3] * vecLab[3] + vecLab[4] * vecLab[4]);
297 mX = xk;
298 mP[kY] = y;
299 mP[kZ] = z;
300 mP[kSnp] = vecLab[4] * t;
301 mP[kTgl] = vecLab[5] * t;
302 mP[kQ2Pt] = q * t / vecLab[6];
303
304 return true;
305}
306
307//____________________________________________________________
308template <typename value_T>
309GPUd() bool TrackParametrization<value_T>::propagateParamTo(value_t xk, value_t b)
310{
311 //----------------------------------------------------------------
312 // propagate this track to the plane X=xk (cm) in the field "b" (kG)
313 // Only parameters are propagated, not the matrix. To be used for small
314 // distances only (<mm, i.e. misalignment)
315 //----------------------------------------------------------------
316 value_t dx = xk - getX();
317 if (gpu::CAMath::Abs(dx) < constants::math::Almost0) {
318 return true;
319 }
320 value_t crv = (gpu::CAMath::Abs(b) < constants::math::Almost0) ? 0.f : getCurvature(b);
321 value_t x2r = crv * dx;
322 value_t f1 = getSnp(), f2 = f1 + x2r;
323 if ((gpu::CAMath::Abs(f1) > constants::math::Almost1) || (gpu::CAMath::Abs(f2) > constants::math::Almost1)) {
324 return false;
325 }
326 value_t r1 = gpu::CAMath::Sqrt((1.f - f1) * (1.f + f1));
327 if (gpu::CAMath::Abs(r1) < constants::math::Almost0) {
328 return false;
329 }
330 value_t r2 = gpu::CAMath::Sqrt((1.f - f2) * (1.f + f2));
331 if (gpu::CAMath::Abs(r2) < constants::math::Almost0) {
332 return false;
333 }
334 double dy2dx = (f1 + f2) / (r1 + r2);
335 bool arcz = gpu::CAMath::Abs(x2r) > 0.05f;
336 if (arcz) {
337 // for small dx/R the linear apporximation of the arc by the segment is OK,
338 // but at large dx/R the error is very large and leads to incorrect Z propagation
339 // angle traversed delta = 2*asin(dist_start_end / R / 2), hence the arc is: R*deltaPhi
340 // The dist_start_end is obtained from sqrt(dx^2+dy^2) = x/(r1+r2)*sqrt(2+f1*f2+r1*r2)
341 // double chord = dx*TMath::Sqrt(1+dy2dx*dy2dx); // distance from old position to new one
342 // double rot = 2*TMath::ASin(0.5*chord*crv); // angular difference seen from the circle center
343 // track1 += rot/crv*track3;
344 //
345 auto arg = r1 * f2 - r2 * f1;
346 if (gpu::CAMath::Abs(arg) > constants::math::Almost1) {
347 return false;
348 }
349 value_t rot = CAMath::ASin(arg); // more economic version from Yura.
350 if (f1 * f1 + f2 * f2 > 1.f && f1 * f2 < 0.f) { // special cases of large rotations or large abs angles
351 if (f2 > 0.f) {
352 rot = constants::math::PI - rot; //
353 } else {
354 rot = -constants::math::PI - rot;
355 }
356 }
357 mP[kZ] += getTgl() / crv * rot;
358 } else {
359 mP[kZ] += dx * (r2 + f2 * dy2dx) * getTgl();
360 }
361 mX = xk;
362 mP[kY] += dx * dy2dx;
363 mP[kSnp] += x2r;
364 return true;
365}
366
367//_______________________________________________________________________
368template <typename value_T>
369GPUd() bool TrackParametrization<value_T>::propagateParamToDCA(const math_utils::Point3D<value_t>& vtx, value_t b, dim2_t* dca, value_t maxD)
370{
371 // propagate track to DCA to the vertex
372 value_t sn, cs, alp = getAlpha();
373 math_utils::detail::sincos(alp, sn, cs);
374 value_t x = getX(), y = getY(), snp = getSnp(), csp = gpu::CAMath::Sqrt((1.f - snp) * (1.f + snp));
375 value_t xv = vtx.X() * cs + vtx.Y() * sn, yv = -vtx.X() * sn + vtx.Y() * cs, zv = vtx.Z();
376 x -= xv;
377 y -= yv;
378 // Estimate the impact parameter neglecting the track curvature
379 value_t d = gpu::CAMath::Abs(x * snp - y * csp);
380 if (d > maxD) {
381 if (dca) { // provide default DCA for failed propag
382 (*dca)[0] = o2::track::DefaultDCA;
383 (*dca)[1] = o2::track::DefaultDCA;
384 }
385 return false;
386 }
387 value_t crv = getCurvature(b);
388 value_t tgfv = -(crv * x - snp) / (crv * y + csp);
389 sn = tgfv / gpu::CAMath::Sqrt(1.f + tgfv * tgfv);
390 cs = gpu::CAMath::Sqrt((1.f - sn) * (1.f + sn));
391 cs = (gpu::CAMath::Abs(tgfv) > constants::math::Almost0) ? sn / tgfv : constants::math::Almost1;
392
393 x = xv * cs + yv * sn;
394 yv = -xv * sn + yv * cs;
395 xv = x;
396
397 auto tmpT(*this); // operate on the copy to recover after the failure
398 alp += gpu::CAMath::ASin(sn);
399 if (!tmpT.rotateParam(alp) || !tmpT.propagateParamTo(xv, b)) {
400#ifndef GPUCA_ALIGPUCODE
401 LOG(debug) << "failed to propagate to alpha=" << alp << " X=" << xv << " for vertex "
402 << vtx.X() << ' ' << vtx.Y() << ' ' << vtx.Z() << " | Track is: " << tmpT.asString();
403#else
404 LOG(debug) << "failed to propagate to alpha=" << alp << " X=" << xv << " for vertex " << vtx.X() << ' ' << vtx.Y() << ' ' << vtx.Z();
405#endif
406 if (dca) { // provide default DCA for failed propag
407 (*dca)[0] = o2::track::DefaultDCA;
408 (*dca)[1] = o2::track::DefaultDCA;
409 }
410 return false;
411 }
412 *this = tmpT;
413 if (dca) {
414 (*dca)[0] = getY() - yv;
415 (*dca)[1] = getZ() - zv;
416 }
417 return true;
418}
419
420//____________________________________________________________
421template <typename value_T>
422GPUd() bool TrackParametrization<value_T>::getYZAt(value_t xk, value_t b, value_t& y, value_t& z) const
423{
424 //----------------------------------------------------------------
425 // estimate Y,Z in tracking frame at given X
426 //----------------------------------------------------------------
427 value_t dx = xk - getX();
428 y = mP[kY];
429 z = mP[kZ];
430 if (gpu::CAMath::Abs(dx) < constants::math::Almost0) {
431 return true;
432 }
433 value_t crv = getCurvature(b);
434 value_t x2r = crv * dx;
435 value_t f1 = getSnp(), f2 = f1 + x2r;
436 if ((gpu::CAMath::Abs(f1) > constants::math::Almost1) || (gpu::CAMath::Abs(f2) > constants::math::Almost1)) {
437 return false;
438 }
439 value_t r1 = gpu::CAMath::Sqrt((1.f - f1) * (1.f + f1));
440 if (gpu::CAMath::Abs(r1) < constants::math::Almost0) {
441 return false;
442 }
443 value_t r2 = gpu::CAMath::Sqrt((1.f - f2) * (1.f + f2));
444 if (gpu::CAMath::Abs(r2) < constants::math::Almost0) {
445 return false;
446 }
447 double dy2dx = (f1 + f2) / (r1 + r2);
448 y += dx * dy2dx;
449 if (gpu::CAMath::Abs(x2r) < 0.05f) {
450 z += dx * (r2 + f2 * dy2dx) * getTgl();
451 } else {
452 // for small dx/R the linear apporximation of the arc by the segment is OK,
453 // but at large dx/R the error is very large and leads to incorrect Z propagation
454 // angle traversed delta = 2*asin(dist_start_end / R / 2), hence the arc is: R*deltaPhi
455 // The dist_start_end is obtained from sqrt(dx^2+dy^2) = x/(r1+r2)*sqrt(2+f1*f2+r1*r2)
456 // double chord = dx*TMath::Sqrt(1+dy2dx*dy2dx); // distance from old position to new one
457 // double rot = 2*TMath::ASin(0.5*chord*crv); // angular difference seen from the circle center
458 // track1 += rot/crv*track3;
459 //
460 value_t rot = CAMath::ASin(r1 * f2 - r2 * f1); // more economic version from Yura.
461 if (f1 * f1 + f2 * f2 > 1.f && f1 * f2 < 0.f) { // special cases of large rotations or large abs angles
462 if (f2 > 0.f) {
463 rot = constants::math::PI - rot; //
464 } else {
465 rot = -constants::math::PI - rot;
466 }
467 }
468 z += getTgl() / crv * rot;
469 }
470 return true;
471}
472
473//______________________________________________________________
474template <typename value_T>
475GPUd() void TrackParametrization<value_T>::invertParam()
476{
477 // Transform this track to the local coord. system rotated by 180 deg.
478 mX = -mX;
479 mAlpha += constants::math::PI;
480 math_utils::detail::bringToPMPi<value_t>(mAlpha);
481 //
482 mP[0] = -mP[0];
483 mP[3] = -mP[3];
484 mP[4] = -mP[4];
485 //
486}
487
488//______________________________________________________________
489template <typename value_T>
490GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getZAt(value_t xk, value_t b) const
491{
493 value_t y, z;
494 return getYZAt(xk, b, y, z) ? z : -9999.f;
495}
496
497//______________________________________________________________
498template <typename value_T>
499GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getYAt(value_t xk, value_t b) const
500{
502 value_t y, z;
503 return getYZAt(xk, b, y, z) ? y : -9999.f;
504}
505
506//______________________________________________________________
507template <typename value_T>
508GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getSnpAt(value_t xk, value_t b) const
509{
511 value_t dx = xk - getX();
512 if (gpu::CAMath::Abs(dx) < constants::math::Almost0) {
513 return getSnp();
514 }
515 value_t crv = (gpu::CAMath::Abs(b) < constants::math::Almost0) ? 0.f : getCurvature(b);
516 value_t x2r = crv * dx;
517 return mP[kSnp] + x2r;
518}
519
520//______________________________________________________________
521template <typename value_T>
522GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getPhiAt(value_t xk, value_t b) const
523{
525 value_t dx = xk - getX();
526 if (gpu::CAMath::Abs(dx) < constants::math::Almost0) {
527 return getPhi();
528 }
529 value_t crv = (gpu::CAMath::Abs(b) < constants::math::Almost0) ? 0.f : getCurvature(b);
530 value_t x2r = crv * dx;
531 value_t snp = mP[kSnp] + x2r;
532 value_t phi = 999.;
533 if (gpu::CAMath::Abs(snp) < constants::math::Almost1) {
534 phi = gpu::CAMath::ASin(snp) + getAlpha();
535 math_utils::detail::bringTo02Pi<value_t>(phi);
536 }
537 return phi;
538}
539
540//______________________________________________________________
541template <typename value_T>
542GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getPhiPosAt(value_t xk, value_t b) const
543{
545 value_t phi = 999.;
546 auto y = getYAt(xk, b);
547 if (y > -9998.) {
548 phi = gpu::CAMath::ATan2(y, xk) + getAlpha();
549 math_utils::detail::bringTo02Pi<value_t>(phi);
550 }
551 return phi;
552}
553
554//______________________________________________________________
555template <typename value_T>
556GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getSnpAt(value_t alpha, value_t xk, value_t b) const
557{
559 math_utils::detail::bringToPMPi<value_t>(alpha);
560 value_t ca = 0, sa = 0;
561 math_utils::detail::sincos(alpha - getAlpha(), sa, ca);
562 value_t snp = getSnp(), csp = gpu::CAMath::Sqrt((1.f - snp) * (1.f + snp)); // Improve precision
563 // RS: check if rotation does no invalidate track model (cos(local_phi)>=0, i.e. particle direction in local frame is along the X axis
564 if ((csp * ca + snp * sa) < 0.) {
565 // LOGF(warning,"Rotation failed: local cos(phi) would become {:.2f}", csp * ca + snp * sa);
566 return -999;
567 }
568 value_t tmp = snp * ca - csp * sa;
569 if (gpu::CAMath::Abs(tmp) > constants::math::Almost1) {
570 LOGP(debug, "Rotation failed: new snp {:.2f}", tmp);
571 return -999;
572 }
573 value_t xrot = getX() * ca + getY() * sa;
574 value_t dx = xk - xrot;
575 value_t crv = (gpu::CAMath::Abs(b) < constants::math::Almost0) ? 0.f : getCurvature(b);
576 value_t x2r = crv * dx;
577 return tmp + x2r;
578}
579
580#ifndef GPUCA_ALIGPUCODE
581//_____________________________________________________________
582template <typename value_T>
584{
585 // print parameters as string
586 return fmt::format("X:{:+.4e} Alp:{:+.3e} Par: {:+.4e} {:+.4e} {:+.4e} {:+.4e} {:+.4e} |Q|:{:d} {:s}",
587 getX(), getAlpha(), getY(), getZ(), getSnp(), getTgl(), getQ2Pt(), getAbsCharge(), getPID().getName());
588}
589
590//_____________________________________________________________
591template <typename value_T>
593{
594 auto _X = getX();
595 auto _Alpha = getAlpha();
596 auto _Y = getY();
597 auto _Z = getZ();
598 auto _Snp = getSnp();
599 auto _Tgl = getTgl();
600 float _Q2Pt = getQ2Pt();
601 float _AbsCharge = getAbsCharge();
602 // print parameters as string
603 return fmt::format("X:{:x} Alp:{:x} Par: {:x} {:x} {:x} {:x} {:x} |Q|:{:x} {:s}\n",
604 reinterpret_cast<const unsigned int&>(_X),
605 reinterpret_cast<const unsigned int&>(_Alpha),
606 reinterpret_cast<const unsigned int&>(_Y),
607 reinterpret_cast<const unsigned int&>(_Z),
608 reinterpret_cast<const unsigned int&>(_Snp),
609 reinterpret_cast<const unsigned int&>(_Tgl),
610 reinterpret_cast<const unsigned int&>(_Q2Pt),
611 reinterpret_cast<const unsigned int&>(_AbsCharge),
612 getPID().getName());
613}
614#endif
615
616//______________________________________________________________
617template <typename value_T>
618GPUd() void TrackParametrization<value_T>::printParam() const
619{
620 // print parameters
621#ifndef GPUCA_ALIGPUCODE
622 printf("%s\n", asString().c_str());
623#elif !defined(GPUCA_GPUCODE_DEVICE) || (!defined(__OPENCL__) && defined(GPUCA_GPU_DEBUG_PRINT))
624 printf("X:%+.4e Alp:%+.3e Par: %+.4e %+.4e %+.4e %+.4e %+.4e |Q|:%d %s\n",
625 getX(), getAlpha(), getY(), getZ(), getSnp(), getTgl(), getQ2Pt(), getAbsCharge(), getPID().getName());
626#endif
627}
628
629//______________________________________________________________
630template <typename value_T>
631GPUd() void TrackParametrization<value_T>::printParamHexadecimal()
632{
633 // print parameters
634#ifndef GPUCA_ALIGPUCODE
635 printf("%s\n", asStringHexadecimal().c_str());
636#elif !defined(GPUCA_GPUCODE_DEVICE) || (!defined(__OPENCL__) && defined(GPUCA_GPU_DEBUG_PRINT))
637 printf("X:%x Alp:%x Par: %x %x %x %x %x |Q|:%x %s\n",
638 gpu::CAMath::Float2UIntReint(getX()),
639 gpu::CAMath::Float2UIntReint(getAlpha()),
640 gpu::CAMath::Float2UIntReint(getY()),
641 gpu::CAMath::Float2UIntReint(getZ()),
642 gpu::CAMath::Float2UIntReint(getSnp()),
643 gpu::CAMath::Float2UIntReint(getTgl()),
644 gpu::CAMath::Float2UIntReint(getQ2Pt()),
645 gpu::CAMath::Float2UIntReint(getAbsCharge()),
646 getPID().getName());
647#endif
648}
649
650//______________________________________________________________
651template <typename value_T>
652GPUd() bool TrackParametrization<value_T>::getXatLabR(value_t r, value_t& x, value_t bz, track::DirType dir) const
653{
654 // Get local X of the track position estimated at the radius lab radius r.
655 // The track curvature is accounted exactly
656 //
657 // The flag "dir" can be used to remove the ambiguity of which intersection to take (out of 2 possible)
658 // DirAuto (==0) - take the intersection closest to the current track position
659 // DirOutward (==1) - go along the track (increasing mX)
660 // DirInward (==-1) - go backward (decreasing mX)
661 //
662 const double fy = mP[0], sn = mP[2];
663 const value_t kEps = 1.e-6;
664 //
665 if (gpu::CAMath::Abs(getSnp()) > constants::math::Almost1) {
666 return false;
667 }
668 auto crv = getCurvature(bz);
669 while (gpu::CAMath::Abs(crv) > constants::math::Almost0) { // helix ?
670 // get center of the track circle
672 getCircleParamsLoc(bz, circle);
673 if (circle.rC == 0.) {
674 crv = 0.;
675 break;
676 }
677 value_t r0 = gpu::CAMath::Sqrt(circle.getCenterD2());
678 if (r0 <= constants::math::Almost0) {
679 return false; // the track is concentric to circle
680 }
681 double tR2r0 = 1., g = 0., tmp = 0.;
682 if (gpu::CAMath::Abs(circle.rC - r0) > kEps) {
683 tR2r0 = circle.rC / r0;
684 g = 0.5f * (r * r / (r0 * circle.rC) - tR2r0 - 1.f / tR2r0);
685 tmp = 1.f + g * tR2r0;
686 } else {
687 tR2r0 = 1.0;
688 g = 0.5 * r * r / (r0 * circle.rC) - 1.;
689 tmp = 0.5 * r * r / (r0 * r0);
690 }
691 auto det = (1. - g) * (1. + g);
692 if (det < 0.) {
693 return false; // does not reach raduis r
694 }
695 det = gpu::CAMath::Sqrt(det);
696 //
697 // the intersection happens in 2 points: {circle.xC+tR*C,circle.yC+tR*S}
698 // with C=f*c0+-|s0|*det and S=f*s0-+c0 sign(s0)*det
699 // where s0 and c0 make direction for the circle center (=circle.xC/r0 and circle.yC/r0)
700 //
701 x = circle.xC * tmp;
702 auto y = circle.yC * tmp;
703 if (gpu::CAMath::Abs(circle.yC) > constants::math::Almost0) { // when circle.yC==0 the x,y is unique
704 auto dfx = tR2r0 * gpu::CAMath::Abs(circle.yC) * det;
705 auto dfy = tR2r0 * circle.xC * (circle.yC > 0. ? det : -det);
706 if (dir == DirAuto) { // chose the one which corresponds to smallest step
707 auto delta = (x - mX) * dfx - (y - fy) * dfy; // the choice of + in C will lead to smaller step if delta<0
708 x += delta < 0. ? dfx : -dfx;
709 } else if (dir == DirOutward) { // along track direction: x must be > mX
710 x -= dfx; // try the smallest step (dfx is positive)
711 auto dfeps = mX - x; // handle special case of very small step
712 if (dfeps < -kEps) {
713 return true;
714 }
715 if (gpu::CAMath::Abs(dfeps) < kEps && gpu::CAMath::Abs(mX * mX + fy * fy - r * r) < kEps) { // are we already in right r?
716 x = mX;
717 return true;
718 }
719 x += dfx + dfx;
720 auto dxm = x - mX;
721 if (dxm > 0.) {
722 return true;
723 } else if (dxm < -kEps) {
724 return false;
725 }
726 x = mX; // don't move
727 } else { // backward: x must be < mX
728 x += dfx; // try the smallest step (dfx is positive)
729 auto dfeps = x - mX; // handle special case of very small step
730 if (dfeps < -kEps) {
731 return true;
732 }
733 if (gpu::CAMath::Abs(dfeps) < kEps && gpu::CAMath::Abs(mX * mX + fy * fy - r * r) < kEps) { // are we already in right r?
734 x = mX;
735 return true;
736 }
737 x -= dfx + dfx;
738 auto dxm = x - mX;
739 if (dxm < 0.) {
740 return true;
741 }
742 if (dxm > kEps) {
743 return false;
744 }
745 x = mX; // don't move
746 }
747 } else { // special case: track touching the circle just in 1 point
748 if ((dir == DirOutward && x < mX) || (dir == DirInward && x > mX)) {
749 return false;
750 }
751 }
752 return true;
753 }
754 // this is a straight track
755 if (gpu::CAMath::Abs(sn) >= constants::math::Almost1) { // || to Y axis
756 double det = (r - mX) * (r + mX);
757 if (det < 0.f) {
758 return false; // does not reach raduis r
759 }
760 x = mX;
761 if (dir == DirAuto) {
762 return true;
763 }
764 det = gpu::CAMath::Sqrt(det);
765 if (dir == DirOutward) { // along the track direction
766 if (sn > 0.) {
767 if (fy > det) {
768 return false; // track is along Y axis and above the circle
769 }
770 } else {
771 if (fy < -det) {
772 return false; // track is against Y axis amd belo the circle
773 }
774 }
775 } else if (dir == DirInward) { // against track direction
776 if (sn > 0.) {
777 if (fy < -det) {
778 return false; // track is along Y axis
779 }
780 } else if (fy > det) {
781 return false; // track is against Y axis
782 }
783 }
784 } else if (gpu::CAMath::Abs(sn) <= constants::math::Almost0) { // || to X axis
785 double det = (r - fy) * (r + fy);
786 if (det < 0.) {
787 return false; // does not reach raduis r
788 }
789 det = gpu::CAMath::Sqrt(det);
790 if (dir == DirAuto) {
791 x = mX > 0. ? det : -det; // choose the solution requiring the smalest step
792 return true;
793 } else if (dir == DirOutward) { // along the track direction
794 if (mX > det) {
795 return false; // current point is in on the right from the circle
796 } else {
797 x = (mX < -det) ? -det : det; // on the left : within the circle
798 }
799 } else { // against the track direction
800 if (mX < -det) {
801 return false;
802 } else {
803 x = mX > det ? det : -det;
804 }
805 }
806 } else { // general case of straight line
807 auto cs = gpu::CAMath::Sqrt((1. - sn) * (1. + sn));
808 auto xsyc = mX * sn - fy * cs;
809 auto det = (r - xsyc) * (r + xsyc);
810 if (det < 0.) {
811 return false; // does not reach raduis r
812 }
813 det = gpu::CAMath::Sqrt(det);
814 auto xcys = mX * cs + fy * sn;
815 auto t = -xcys;
816 if (dir == DirAuto) {
817 t += t > 0. ? -det : det; // chose the solution requiring the smalest step
818 } else if (dir > 0) { // go in increasing mX direction. ( t+-det > 0)
819 if (t >= -det) {
820 t += det; // take minimal step giving t>0
821 } else {
822 return false; // both solutions have negative t
823 }
824 } else { // go in decreasing mX direction. (t+-det < 0)
825 if (t < det) {
826 t -= det; // take minimal step giving t<0
827 } else {
828 return false; // both solutions have positive t
829 }
830 }
831 x = mX + cs * t;
832 }
833 //
834 return true;
835}
836
837//______________________________________________
838template <typename value_T>
839GPUd() bool TrackParametrization<value_T>::correctForELoss(value_t xrho, bool anglecorr)
840{
841 //------------------------------------------------------------------
842 // This function corrects the track parameters for the energy loss in crossed material.
843 // "xrho" - is the product length*density (g/cm^2).
844 // It should be passed as negative when propagating tracks
845 // from the intreaction point to the outside of the central barrel.
846 // "dedx" - mean enery loss (GeV/(g/cm^2), if <=kCalcdEdxAuto : calculate on the fly
847 // "anglecorr" - switch for the angular correction
848 //------------------------------------------------------------------
849 constexpr value_t kMinP = 0.01f; // kill below this momentum
850
851 auto m = getPID().getMass();
852 if (m > 0 && xrho != 0.f) {
853 // Apply angle correction, if requested
854 if (anglecorr) {
855 value_t csp2 = (1.f - getSnp()) * (1.f + getSnp()); // cos(phi)^2
856 value_t cst2I = (1.f + getTgl() * getTgl()); // 1/cos(lambda)^2
857 value_t angle = gpu::CAMath::Sqrt(cst2I / (csp2));
858 xrho *= angle;
859 }
860 int charge2 = getAbsCharge() * getAbsCharge();
861 value_t p = getP(), p0 = p, p2 = p * p, e2 = p2 + getPID().getMass2(), massInv = 1. / m, bg = p * massInv;
862 value_t e = gpu::CAMath::Sqrt(e2), ekin = e - m, dedx = getdEdxBBOpt(bg);
863#ifdef _BB_NONCONST_CORR_
864 value_t dedxDer = 0., dedx1 = dedx;
865#endif
866 if (charge2 != 1) {
867 dedx *= charge2;
868 }
869 value_t dE = dedx * xrho;
870 int na = 1 + int(gpu::CAMath::Abs(dE) / ekin * ELoss2EKinThreshInv);
871 if (na > MaxELossIter) {
872 na = MaxELossIter;
873 }
874 if (na > 1) {
875 dE /= na;
876 xrho /= na;
877#ifdef _BB_NONCONST_CORR_
878 dedxDer = getBetheBlochSolidDerivativeApprox(dedx1, bg); // require correction for non-constantness of dedx vs betagamma
879 if (charge2 != 1) {
880 dedxDer *= charge2;
881 }
882#endif
883 }
884 while (na--) {
885#ifdef _BB_NONCONST_CORR_
886 if (dedxDer != 0.) { // correction for non-constantness of dedx vs beta*gamma (in linear approximation): for a single step dE -> dE * [(exp(dedxDer) - 1)/dedxDer]
887 if (xrho < 0) {
888 dedxDer = -dedxDer; // E.loss ( -> positive derivative)
889 }
890 auto corrC = (gpu::CAMath::Exp(dedxDer) - 1.) / dedxDer;
891 dE *= corrC;
892 }
893#endif
894 e += dE;
895 if (e > m) { // stopped
896 p = gpu::CAMath::Sqrt(e * e - getPID().getMass2());
897 } else {
898 return false;
899 }
900 if (na) {
901 bg = p * massInv;
902 dedx = getdEdxBBOpt(bg);
903#ifdef _BB_NONCONST_CORR_
904 dedxDer = getBetheBlochSolidDerivativeApprox(dedx, bg);
905#endif
906 if (charge2 != 1) {
907 dedx *= charge2;
908#ifdef _BB_NONCONST_CORR_
909 dedxDer *= charge2;
910#endif
911 }
912 dE = dedx * xrho;
913 }
914 }
915
916 if (p < kMinP) {
917 return false;
918 }
919 setQ2Pt(getQ2Pt() * p0 / p);
920 }
921
922 return true;
923}
924
925//______________________________________________
926template <typename value_T>
927GPUd() typename o2::track::TrackParametrization<value_T>::yzerr_t TrackParametrization<value_T>::getVertexInTrackFrame(const o2::dataformats::VertexBase& v) const
928{
929 // rotate vertex to track frame and return parameters used by getPredictedChi2 and update of TrackParametrizationWithError
930 value_t sn, cs;
931 math_utils::detail::sincos(-mAlpha, sn, cs); // use -alpha since we rotate from lab to tracking frame
932 value_t sn2 = sn * sn, cs2 = cs * cs, sncs = sn * cs;
933 value_t dsxysncs = 2. * v.getSigmaXY() * sncs;
934 return {{/*v.getX()*cs-v.getY()*sn,*/ v.getX() * sn + v.getY() * cs, v.getZ()},
935 {v.getSigmaX2() * sn2 + dsxysncs + v.getSigmaY2() * cs2, (sn + cs) * v.getSigmaYZ(), v.getSigmaZ2()}};
936}
937
938//______________________________________________
939template <typename value_T>
940GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getDCAYtoMV(value_t b, value_t xmv, value_t ymv, value_t zmv) const
941{
942 auto ttmp = *this;
943 dim2_t dca;
944 return ttmp.propagateParamToDCA({xmv, ymv, zmv}, b, &dca) ? dca[0] : -9999.;
945}
946
947//______________________________________________
948template <typename value_T>
949GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getDCAZtoMV(value_t b, value_t xmv, value_t ymv, value_t zmv) const
950{
951 auto ttmp = *this;
952 dim2_t dca;
953 return ttmp.propagateParamToDCA({xmv, ymv, zmv}, b, &dca) ? dca[1] : -9999.;
954}
955
956namespace o2::track
957{
958#if !defined(GPUCA_GPUCODE) || defined(GPUCA_GPUCODE_DEVICE) // FIXME: DR: WORKAROUND to avoid CUDA bug creating host symbols for device code.
959template class TrackParametrization<float>;
960#endif
961#ifndef GPUCA_GPUCODE
962template class TrackParametrization<double>;
963#endif
964} // namespace o2::track
std::string getName(const TDataMember *dm, int index, int size)
std::string asString(TDataMember const &dm, char *pointer)
int16_t charge
Definition RawEventData.h:5
const int16_t bb
constexpr int p2()
uint16_t pid
Definition RawData.h:2
std::ostringstream debug
std::string asString() const
GLfloat GLfloat GLfloat alpha
Definition glcorearb.h:279
GLint GLenum GLint x
Definition glcorearb.h:403
const GLfloat * m
Definition glcorearb.h:4066
const GLdouble * v
Definition glcorearb.h:832
GLenum array
Definition glcorearb.h:4274
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLint y
Definition glcorearb.h:270
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLboolean GLboolean g
Definition glcorearb.h:1233
GLfloat angle
Definition glcorearb.h:4071
GLboolean r
Definition glcorearb.h:1233
GLdouble GLdouble GLdouble z
Definition glcorearb.h:843
typename trackParam_t::dim3_t dim3_t
Definition utils.h:32
typename trackParam_t::dim2_t dim2_t
Definition utils.h:31
typename trackParam_t::value_t value_t
Definition utils.h:30
constexpr float Almost1
std::array< int, 24 > p0
double * getX(double *xyDxy, int N)
double * getY(double *xyDxy, int N)
value_T bg
Definition TrackUtils.h:194
value_T step
Definition TrackUtils.h:42
value_T f1
Definition TrackUtils.h:91
const value_T x
Definition TrackUtils.h:136
value_T std::array< value_T, 7 > & vect
Definition TrackUtils.h:42
GPUd() value_T BetheBlochSolid(value_T bg
Definition TrackUtils.h:150
value_T f2
Definition TrackUtils.h:92
constexpr float DefaultDCA
constexpr int MaxELossIter
constexpr float ELoss2EKinThreshInv
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Defining DataPointCompositeObject explicitly as copiable.
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"