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(gpu::gpustd::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
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 gpu::gpustd::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 return false;
382 }
383 value_t crv = getCurvature(b);
384 value_t tgfv = -(crv * x - snp) / (crv * y + csp);
385 sn = tgfv / gpu::CAMath::Sqrt(1.f + tgfv * tgfv);
386 cs = gpu::CAMath::Sqrt((1.f - sn) * (1.f + sn));
387 cs = (gpu::CAMath::Abs(tgfv) > constants::math::Almost0) ? sn / tgfv : constants::math::Almost1;
388
389 x = xv * cs + yv * sn;
390 yv = -xv * sn + yv * cs;
391 xv = x;
392
393 auto tmpT(*this); // operate on the copy to recover after the failure
394 alp += gpu::CAMath::ASin(sn);
395 if (!tmpT.rotateParam(alp) || !tmpT.propagateParamTo(xv, b)) {
396#ifndef GPUCA_ALIGPUCODE
397 LOG(debug) << "failed to propagate to alpha=" << alp << " X=" << xv << " for vertex "
398 << vtx.X() << ' ' << vtx.Y() << ' ' << vtx.Z() << " | Track is: " << tmpT.asString();
399#else
400 LOG(debug) << "failed to propagate to alpha=" << alp << " X=" << xv << " for vertex " << vtx.X() << ' ' << vtx.Y() << ' ' << vtx.Z();
401#endif
402 return false;
403 }
404 *this = tmpT;
405 if (dca) {
406 (*dca)[0] = getY() - yv;
407 (*dca)[1] = getZ() - zv;
408 }
409 return true;
410}
411
412//____________________________________________________________
413template <typename value_T>
414GPUd() bool TrackParametrization<value_T>::getYZAt(value_t xk, value_t b, value_t& y, value_t& z) const
415{
416 //----------------------------------------------------------------
417 // estimate Y,Z in tracking frame at given X
418 //----------------------------------------------------------------
419 value_t dx = xk - getX();
420 y = mP[kY];
421 z = mP[kZ];
422 if (gpu::CAMath::Abs(dx) < constants::math::Almost0) {
423 return true;
424 }
425 value_t crv = getCurvature(b);
426 value_t x2r = crv * dx;
427 value_t f1 = getSnp(), f2 = f1 + x2r;
428 if ((gpu::CAMath::Abs(f1) > constants::math::Almost1) || (gpu::CAMath::Abs(f2) > constants::math::Almost1)) {
429 return false;
430 }
431 value_t r1 = gpu::CAMath::Sqrt((1.f - f1) * (1.f + f1));
432 if (gpu::CAMath::Abs(r1) < constants::math::Almost0) {
433 return false;
434 }
435 value_t r2 = gpu::CAMath::Sqrt((1.f - f2) * (1.f + f2));
436 if (gpu::CAMath::Abs(r2) < constants::math::Almost0) {
437 return false;
438 }
439 double dy2dx = (f1 + f2) / (r1 + r2);
440 y += dx * dy2dx;
441 if (gpu::CAMath::Abs(x2r) < 0.05f) {
442 z += dx * (r2 + f2 * dy2dx) * getTgl();
443 } else {
444 // for small dx/R the linear apporximation of the arc by the segment is OK,
445 // but at large dx/R the error is very large and leads to incorrect Z propagation
446 // angle traversed delta = 2*asin(dist_start_end / R / 2), hence the arc is: R*deltaPhi
447 // The dist_start_end is obtained from sqrt(dx^2+dy^2) = x/(r1+r2)*sqrt(2+f1*f2+r1*r2)
448 // double chord = dx*TMath::Sqrt(1+dy2dx*dy2dx); // distance from old position to new one
449 // double rot = 2*TMath::ASin(0.5*chord*crv); // angular difference seen from the circle center
450 // track1 += rot/crv*track3;
451 //
452 value_t rot = CAMath::ASin(r1 * f2 - r2 * f1); // more economic version from Yura.
453 if (f1 * f1 + f2 * f2 > 1.f && f1 * f2 < 0.f) { // special cases of large rotations or large abs angles
454 if (f2 > 0.f) {
455 rot = constants::math::PI - rot; //
456 } else {
457 rot = -constants::math::PI - rot;
458 }
459 }
460 z += getTgl() / crv * rot;
461 }
462 return true;
463}
464
465//______________________________________________________________
466template <typename value_T>
467GPUd() void TrackParametrization<value_T>::invertParam()
468{
469 // Transform this track to the local coord. system rotated by 180 deg.
470 mX = -mX;
471 mAlpha += constants::math::PI;
472 math_utils::detail::bringToPMPi<value_t>(mAlpha);
473 //
474 mP[0] = -mP[0];
475 mP[3] = -mP[3];
476 mP[4] = -mP[4];
477 //
478}
479
480//______________________________________________________________
481template <typename value_T>
482GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getZAt(value_t xk, value_t b) const
483{
485 value_t y, z;
486 return getYZAt(xk, b, y, z) ? z : -9999.f;
487}
488
489//______________________________________________________________
490template <typename value_T>
491GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getYAt(value_t xk, value_t b) const
492{
494 value_t y, z;
495 return getYZAt(xk, b, y, z) ? y : -9999.f;
496}
497
498//______________________________________________________________
499template <typename value_T>
500GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getSnpAt(value_t xk, value_t b) const
501{
503 value_t dx = xk - getX();
504 if (gpu::CAMath::Abs(dx) < constants::math::Almost0) {
505 return getSnp();
506 }
507 value_t crv = (gpu::CAMath::Abs(b) < constants::math::Almost0) ? 0.f : getCurvature(b);
508 value_t x2r = crv * dx;
509 return mP[kSnp] + x2r;
510}
511
512//______________________________________________________________
513template <typename value_T>
514GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getPhiAt(value_t xk, value_t b) const
515{
517 value_t dx = xk - getX();
518 if (gpu::CAMath::Abs(dx) < constants::math::Almost0) {
519 return getPhi();
520 }
521 value_t crv = (gpu::CAMath::Abs(b) < constants::math::Almost0) ? 0.f : getCurvature(b);
522 value_t x2r = crv * dx;
523 value_t snp = mP[kSnp] + x2r;
524 value_t phi = 999.;
525 if (gpu::CAMath::Abs(snp) < constants::math::Almost1) {
526 phi = gpu::CAMath::ASin(snp) + getAlpha();
527 math_utils::detail::bringTo02Pi<value_t>(phi);
528 }
529 return phi;
530}
531
532//______________________________________________________________
533template <typename value_T>
534GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getPhiPosAt(value_t xk, value_t b) const
535{
537 value_t phi = 999.;
538 auto y = getYAt(xk, b);
539 if (y > -9998.) {
540 phi = gpu::CAMath::ATan2(y, xk) + getAlpha();
541 math_utils::detail::bringTo02Pi<value_t>(phi);
542 }
543 return phi;
544}
545
546//______________________________________________________________
547template <typename value_T>
548GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getSnpAt(value_t alpha, value_t xk, value_t b) const
549{
551 math_utils::detail::bringToPMPi<value_t>(alpha);
552 value_t ca = 0, sa = 0;
553 math_utils::detail::sincos(alpha - getAlpha(), sa, ca);
554 value_t snp = getSnp(), csp = gpu::CAMath::Sqrt((1.f - snp) * (1.f + snp)); // Improve precision
555 // 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
556 if ((csp * ca + snp * sa) < 0.) {
557 // LOGF(warning,"Rotation failed: local cos(phi) would become {:.2f}", csp * ca + snp * sa);
558 return -999;
559 }
560 value_t tmp = snp * ca - csp * sa;
561 if (gpu::CAMath::Abs(tmp) > constants::math::Almost1) {
562 LOGP(debug, "Rotation failed: new snp {:.2f}", tmp);
563 return -999;
564 }
565 value_t xrot = getX() * ca + getY() * sa;
566 value_t dx = xk - xrot;
567 value_t crv = (gpu::CAMath::Abs(b) < constants::math::Almost0) ? 0.f : getCurvature(b);
568 value_t x2r = crv * dx;
569 return tmp + x2r;
570}
571
572#ifndef GPUCA_ALIGPUCODE
573//_____________________________________________________________
574template <typename value_T>
576{
577 // print parameters as string
578 return fmt::format("X:{:+.4e} Alp:{:+.3e} Par: {:+.4e} {:+.4e} {:+.4e} {:+.4e} {:+.4e} |Q|:{:d} {:s}\n",
579 getX(), getAlpha(), getY(), getZ(), getSnp(), getTgl(), getQ2Pt(), getAbsCharge(), getPID().getName());
580}
581
582//_____________________________________________________________
583template <typename value_T>
585{
586 auto _X = getX();
587 auto _Alpha = getAlpha();
588 auto _Y = getY();
589 auto _Z = getZ();
590 auto _Snp = getSnp();
591 auto _Tgl = getTgl();
592 float _Q2Pt = getQ2Pt();
593 float _AbsCharge = getAbsCharge();
594 // print parameters as string
595 return fmt::format("X:{:x} Alp:{:x} Par: {:x} {:x} {:x} {:x} {:x} |Q|:{:x} {:s}\n",
596 reinterpret_cast<const unsigned int&>(_X),
597 reinterpret_cast<const unsigned int&>(_Alpha),
598 reinterpret_cast<const unsigned int&>(_Y),
599 reinterpret_cast<const unsigned int&>(_Z),
600 reinterpret_cast<const unsigned int&>(_Snp),
601 reinterpret_cast<const unsigned int&>(_Tgl),
602 reinterpret_cast<const unsigned int&>(_Q2Pt),
603 reinterpret_cast<const unsigned int&>(_AbsCharge),
604 getPID().getName());
605}
606#endif
607
608//______________________________________________________________
609template <typename value_T>
610GPUd() void TrackParametrization<value_T>::printParam() const
611{
612 // print parameters
613#ifndef GPUCA_ALIGPUCODE
614 printf("%s\n", asString().c_str());
615#elif !defined(GPUCA_GPUCODE_DEVICE) || (!defined(__OPENCL__) && defined(GPUCA_GPU_DEBUG_PRINT))
616 printf("X:%+.4e Alp:%+.3e Par: %+.4e %+.4e %+.4e %+.4e %+.4e |Q|:%d %s\n",
617 getX(), getAlpha(), getY(), getZ(), getSnp(), getTgl(), getQ2Pt(), getAbsCharge(), getPID().getName());
618#endif
619}
620
621//______________________________________________________________
622template <typename value_T>
623GPUd() void TrackParametrization<value_T>::printParamHexadecimal()
624{
625 // print parameters
626#ifndef GPUCA_ALIGPUCODE
627 printf("%s\n", asStringHexadecimal().c_str());
628#elif !defined(GPUCA_GPUCODE_DEVICE) || (!defined(__OPENCL__) && defined(GPUCA_GPU_DEBUG_PRINT))
629 printf("X:%x Alp:%x Par: %x %x %x %x %x |Q|:%x %s\n",
630 gpu::CAMath::Float2UIntReint(getX()),
631 gpu::CAMath::Float2UIntReint(getAlpha()),
632 gpu::CAMath::Float2UIntReint(getY()),
633 gpu::CAMath::Float2UIntReint(getZ()),
634 gpu::CAMath::Float2UIntReint(getSnp()),
635 gpu::CAMath::Float2UIntReint(getTgl()),
636 gpu::CAMath::Float2UIntReint(getQ2Pt()),
637 gpu::CAMath::Float2UIntReint(getAbsCharge()),
638 getPID().getName());
639#endif
640}
641
642//______________________________________________________________
643template <typename value_T>
644GPUd() bool TrackParametrization<value_T>::getXatLabR(value_t r, value_t& x, value_t bz, track::DirType dir) const
645{
646 // Get local X of the track position estimated at the radius lab radius r.
647 // The track curvature is accounted exactly
648 //
649 // The flag "dir" can be used to remove the ambiguity of which intersection to take (out of 2 possible)
650 // DirAuto (==0) - take the intersection closest to the current track position
651 // DirOutward (==1) - go along the track (increasing mX)
652 // DirInward (==-1) - go backward (decreasing mX)
653 //
654 const auto fy = mP[0], sn = mP[2];
655 const value_t kEps = 1.e-6;
656 //
657 if (gpu::CAMath::Abs(getSnp()) > constants::math::Almost1) {
658 return false;
659 }
660 auto crv = getCurvature(bz);
661 while (gpu::CAMath::Abs(crv) > constants::math::Almost0) { // helix ?
662 // get center of the track circle
664 getCircleParamsLoc(bz, circle);
665 if (circle.rC == 0.) {
666 crv = 0.;
667 break;
668 }
669 value_t r0 = gpu::CAMath::Sqrt(circle.getCenterD2());
670 if (r0 <= constants::math::Almost0) {
671 return false; // the track is concentric to circle
672 }
673 value_t tR2r0 = 1.f, g = 0.f, tmp = 0.f;
674 if (gpu::CAMath::Abs(circle.rC - r0) > kEps) {
675 tR2r0 = circle.rC / r0;
676 g = 0.5f * (r * r / (r0 * circle.rC) - tR2r0 - 1.f / tR2r0);
677 tmp = 1.f + g * tR2r0;
678 } else {
679 tR2r0 = 1.0;
680 g = 0.5f * r * r / (r0 * circle.rC) - 1.f;
681 tmp = 0.5f * r * r / (r0 * r0);
682 }
683 value_t det = (1.f - g) * (1.f + g);
684 if (det < 0.f) {
685 return false; // does not reach raduis r
686 }
687 det = gpu::CAMath::Sqrt(det);
688 //
689 // the intersection happens in 2 points: {circle.xC+tR*C,circle.yC+tR*S}
690 // with C=f*c0+-|s0|*det and S=f*s0-+c0 sign(s0)*det
691 // where s0 and c0 make direction for the circle center (=circle.xC/r0 and circle.yC/r0)
692 //
693 x = circle.xC * tmp;
694 value_t y = circle.yC * tmp;
695 if (gpu::CAMath::Abs(circle.yC) > constants::math::Almost0) { // when circle.yC==0 the x,y is unique
696 value_t dfx = tR2r0 * gpu::CAMath::Abs(circle.yC) * det;
697 value_t dfy = tR2r0 * circle.xC * (circle.yC > 0.f ? det : -det);
698 if (dir == DirAuto) { // chose the one which corresponds to smallest step
699 value_t delta = (x - mX) * dfx - (y - fy) * dfy; // the choice of + in C will lead to smaller step if delta<0
700 x += delta < 0.f ? dfx : -dfx;
701 } else if (dir == DirOutward) { // along track direction: x must be > mX
702 x -= dfx; // try the smallest step (dfx is positive)
703 value_t dfeps = mX - x; // handle special case of very small step
704 if (dfeps < -kEps) {
705 return true;
706 }
707 if (gpu::CAMath::Abs(dfeps) < kEps && gpu::CAMath::Abs(mX * mX + fy * fy - r * r) < kEps) { // are we already in right r?
708 return mX;
709 }
710 x += dfx + dfx;
711 value_t dxm = x - mX;
712 if (dxm > 0.f) {
713 return true;
714 } else if (dxm < -kEps) {
715 return false;
716 }
717 x = mX; // don't move
718 } else { // backward: x must be < mX
719 x += dfx; // try the smallest step (dfx is positive)
720 value_t dfeps = x - mX; // handle special case of very small step
721 if (dfeps < -kEps) {
722 return true;
723 }
724 if (gpu::CAMath::Abs(dfeps) < kEps && gpu::CAMath::Abs(mX * mX + fy * fy - r * r) < kEps) { // are we already in right r?
725 return mX;
726 }
727 x -= dfx + dfx;
728 value_t dxm = x - mX;
729 if (dxm < 0.f) {
730 return true;
731 }
732 if (dxm > kEps) {
733 return false;
734 }
735 x = mX; // don't move
736 }
737 } else { // special case: track touching the circle just in 1 point
738 if ((dir == DirOutward && x < mX) || (dir == DirInward && x > mX)) {
739 return false;
740 }
741 }
742 return x;
743 }
744 // this is a straight track
745 if (gpu::CAMath::Abs(sn) >= constants::math::Almost1) { // || to Y axis
746 value_t det = (r - mX) * (r + mX);
747 if (det < 0.f) {
748 return false; // does not reach raduis r
749 }
750 x = mX;
751 if (dir == DirAuto) {
752 return true;
753 }
754 det = gpu::CAMath::Sqrt(det);
755 if (dir == DirOutward) { // along the track direction
756 if (sn > 0.f) {
757 if (fy > det) {
758 return false; // track is along Y axis and above the circle
759 }
760 } else {
761 if (fy < -det) {
762 return false; // track is against Y axis amd belo the circle
763 }
764 }
765 } else if (dir == DirInward) { // against track direction
766 if (sn > 0.f) {
767 if (fy < -det) {
768 return false; // track is along Y axis
769 }
770 } else if (fy > det) {
771 return false; // track is against Y axis
772 }
773 }
774 } else if (gpu::CAMath::Abs(sn) <= constants::math::Almost0) { // || to X axis
775 value_t det = (r - fy) * (r + fy);
776 if (det < 0.f) {
777 return false; // does not reach raduis r
778 }
779 det = gpu::CAMath::Sqrt(det);
780 if (dir == DirAuto) {
781 x = mX > 0.f ? det : -det; // choose the solution requiring the smalest step
782 return true;
783 } else if (dir == DirOutward) { // along the track direction
784 if (mX > det) {
785 return false; // current point is in on the right from the circle
786 } else {
787 x = (mX < -det) ? -det : det; // on the left : within the circle
788 }
789 } else { // against the track direction
790 if (mX < -det) {
791 return false;
792 } else {
793 x = mX > det ? det : -det;
794 }
795 }
796 } else { // general case of straight line
797 value_t cs = gpu::CAMath::Sqrt((1.f - sn) * (1.f + sn));
798 value_t xsyc = mX * sn - fy * cs;
799 value_t det = (r - xsyc) * (r + xsyc);
800 if (det < 0.f) {
801 return false; // does not reach raduis r
802 }
803 det = gpu::CAMath::Sqrt(det);
804 value_t xcys = mX * cs + fy * sn;
805 value_t t = -xcys;
806 if (dir == DirAuto) {
807 t += t > 0.f ? -det : det; // chose the solution requiring the smalest step
808 } else if (dir > 0) { // go in increasing mX direction. ( t+-det > 0)
809 if (t >= -det) {
810 t += det; // take minimal step giving t>0
811 } else {
812 return false; // both solutions have negative t
813 }
814 } else { // go in decreasing mX direction. (t+-det < 0)
815 if (t < det) {
816 t -= det; // take minimal step giving t<0
817 } else {
818 return false; // both solutions have positive t
819 }
820 }
821 x = mX + cs * t;
822 }
823 //
824 return true;
825}
826
827//______________________________________________
828template <typename value_T>
829GPUd() bool TrackParametrization<value_T>::correctForELoss(value_t xrho, bool anglecorr)
830{
831 //------------------------------------------------------------------
832 // This function corrects the track parameters for the energy loss in crossed material.
833 // "xrho" - is the product length*density (g/cm^2).
834 // It should be passed as negative when propagating tracks
835 // from the intreaction point to the outside of the central barrel.
836 // "dedx" - mean enery loss (GeV/(g/cm^2), if <=kCalcdEdxAuto : calculate on the fly
837 // "anglecorr" - switch for the angular correction
838 //------------------------------------------------------------------
839 constexpr value_t kMinP = 0.01f; // kill below this momentum
840
841 auto m = getPID().getMass();
842 if (m > 0 && xrho != 0.f) {
843 // Apply angle correction, if requested
844 if (anglecorr) {
845 value_t csp2 = (1.f - getSnp()) * (1.f + getSnp()); // cos(phi)^2
846 value_t cst2I = (1.f + getTgl() * getTgl()); // 1/cos(lambda)^2
847 value_t angle = gpu::CAMath::Sqrt(cst2I / (csp2));
848 xrho *= angle;
849 }
850 int charge2 = getAbsCharge() * getAbsCharge();
851 value_t p = getP(), p0 = p, p2 = p * p, e2 = p2 + getPID().getMass2(), massInv = 1. / m, bg = p * massInv;
852 value_t e = gpu::CAMath::Sqrt(e2), ekin = e - m, dedx = getdEdxBBOpt(bg);
853#ifdef _BB_NONCONST_CORR_
854 value_t dedxDer = 0., dedx1 = dedx;
855#endif
856 if (charge2 != 1) {
857 dedx *= charge2;
858 }
859 value_t dE = dedx * xrho;
860 int na = 1 + int(gpu::CAMath::Abs(dE) / ekin * ELoss2EKinThreshInv);
861 if (na > MaxELossIter) {
862 na = MaxELossIter;
863 }
864 if (na > 1) {
865 dE /= na;
866 xrho /= na;
867#ifdef _BB_NONCONST_CORR_
868 dedxDer = getBetheBlochSolidDerivativeApprox(dedx1, bg); // require correction for non-constantness of dedx vs betagamma
869 if (charge2 != 1) {
870 dedxDer *= charge2;
871 }
872#endif
873 }
874 while (na--) {
875#ifdef _BB_NONCONST_CORR_
876 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]
877 if (xrho < 0) {
878 dedxDer = -dedxDer; // E.loss ( -> positive derivative)
879 }
880 auto corrC = (gpu::CAMath::Exp(dedxDer) - 1.) / dedxDer;
881 dE *= corrC;
882 }
883#endif
884 e += dE;
885 if (e > m) { // stopped
886 p = gpu::CAMath::Sqrt(e * e - getPID().getMass2());
887 } else {
888 return false;
889 }
890 if (na) {
891 bg = p * massInv;
892 dedx = getdEdxBBOpt(bg);
893#ifdef _BB_NONCONST_CORR_
894 dedxDer = getBetheBlochSolidDerivativeApprox(dedx, bg);
895#endif
896 if (charge2 != 1) {
897 dedx *= charge2;
898#ifdef _BB_NONCONST_CORR_
899 dedxDer *= charge2;
900#endif
901 }
902 dE = dedx * xrho;
903 }
904 }
905
906 if (p < kMinP) {
907 return false;
908 }
909 setQ2Pt(getQ2Pt() * p0 / p);
910 }
911
912 return true;
913}
914
915//______________________________________________
916template <typename value_T>
917GPUd() typename o2::track::TrackParametrization<value_T>::yzerr_t TrackParametrization<value_T>::getVertexInTrackFrame(const o2::dataformats::VertexBase& v) const
918{
919 // rotate vertex to track frame and return parameters used by getPredictedChi2 and update of TrackParametrizationWithError
920 value_t sn, cs;
921 math_utils::detail::sincos(-mAlpha, sn, cs); // use -alpha since we rotate from lab to tracking frame
922 value_t sn2 = sn * sn, cs2 = cs * cs, sncs = sn * cs;
923 value_t dsxysncs = 2. * v.getSigmaXY() * sncs;
924 return {{/*v.getX()*cs-v.getY()*sn,*/ v.getX() * sn + v.getY() * cs, v.getZ()},
925 {v.getSigmaX2() * sn2 + dsxysncs + v.getSigmaY2() * cs2, (sn + cs) * v.getSigmaYZ(), v.getSigmaZ2()}};
926}
927
928//______________________________________________
929template <typename value_T>
930GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getDCAYtoMV(value_t b, value_t xmv, value_t ymv, value_t zmv) const
931{
932 auto ttmp = *this;
933 dim2_t dca;
934 return ttmp.propagateParamToDCA({xmv, ymv, zmv}, b, &dca) ? dca[0] : -9999.;
935}
936
937//______________________________________________
938template <typename value_T>
939GPUd() typename TrackParametrization<value_T>::value_t TrackParametrization<value_T>::getDCAZtoMV(value_t b, value_t xmv, value_t ymv, value_t zmv) const
940{
941 auto ttmp = *this;
942 dim2_t dca;
943 return ttmp.propagateParamToDCA({xmv, ymv, zmv}, b, &dca) ? dca[1] : -9999.;
944}
945
946namespace o2::track
947{
948#if !defined(GPUCA_GPUCODE) || defined(GPUCA_GPUCODE_DEVICE) // FIXME: DR: WORKAROUND to avoid CUDA bug creating host symbols for device code.
949template class TrackParametrization<float>;
950#endif
951#ifndef GPUCA_GPUCODE
952template class TrackParametrization<double>;
953#endif
954} // 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
constexpr float Almost1
std::array< T, N > array
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 gpu::gpustd::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 int MaxELossIter
constexpr float ELoss2EKinThreshInv
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"