Project
Loading...
Searching...
No Matches
GPUTPCGMTrackParam.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
14
15#define GPUCA_CADEBUG 0
16#define DEBUG_SINGLE_TRACK -1
17
18#include "GPUTPCDef.h"
19#include "GPUTPCGMTrackParam.h"
21#include "GPUTPCGMPropagator.h"
22#include "GPUTPCGMBorderTrack.h"
23#include "GPUTPCGMMergedTrack.h"
25#include "GPUTPCGMMerger.h"
26#include "GPUTPCTracker.h"
27#include "GPUdEdx.h"
28#include "GPUParam.h"
29#include "GPUO2DataTypes.h"
30#include "GPUConstantMem.h"
31#include "TPCFastTransform.h"
33#include "GPUTPCConvertImpl.h"
34#include "GPUTPCGMMergerTypes.h"
35#include "GPUParam.inc"
36#include "GPUGetConstexpr.h"
37
38#ifdef GPUCA_CADEBUG_ENABLED
39#include "GPUSettings.h"
41#endif
42
43#ifndef GPUCA_GPUCODE_DEVICE
44#include <cmath>
45#include <cstdlib>
46#endif
47
48using namespace o2::gpu;
49using namespace o2::tpc;
50
51GPUd() bool GPUTPCGMTrackParam::Fit(GPUTPCGMMerger* GPUrestrict() merger, int32_t iTrk, GPUTPCGMMergedTrackHit* GPUrestrict() clusters, int32_t& GPUrestrict() N, int32_t& GPUrestrict() NTolerated, float& GPUrestrict() Alpha, int32_t attempt, float maxSinPhi, GPUTPCGMMergedTrack& GPUrestrict() track)
52{
53 static constexpr float kDeg2Rad = M_PI / 180.f;
54 CADEBUG(static constexpr float kSectAngle = 2 * M_PI / 18.f);
55
56 const GPUParam& GPUrestrict() param = merger->Param();
57
58 GPUdEdx dEdx, dEdxAlt;
61 prop.SetMaterialTPC();
62 prop.SetPolynomialField(&param.polynomialField);
63 prop.SetMaxSinPhi(maxSinPhi);
64 if (param.rec.tpc.mergerInterpolateErrors) {
65 for (int32_t i = 0; i < N; i++) {
66 interpolation.hit[i].errorY = -1;
67 }
68 }
69
70 const int32_t nWays = param.rec.tpc.nWays;
71 const int32_t maxN = N;
72 int32_t ihitStart = 0;
73 float covYYUpd = 0.f;
74 float lastUpdateX = -1.f;
75 uint8_t lastRow = 255;
76 uint8_t lastSector = 255;
77 float deltaZ = 0.f;
78
79 for (int32_t iWay = 0; iWay < nWays; iWay++) {
80 int32_t nMissed = 0, nMissed2 = 0;
81 float sumInvSqrtCharge = 0.f;
82 int32_t nAvgCharge = 0;
83
84 if (iWay && (iWay & 1) == 0) {
85 StoreOuter(&track.OuterParam(), prop.GetAlpha());
86 }
87
88 int32_t resetT0 = initResetT0();
89 const bool refit = (nWays == 1 || iWay >= 1);
90 const bool finalOutInFit = iWay + 2 >= nWays;
91 const bool finalFit = iWay == nWays - 1;
92 const float maxSinForUpdate = CAMath::Sin(70.f * kDeg2Rad);
93
94 ResetCovariance();
95 prop.SetSeedingErrors(!(refit && attempt == 0));
96 prop.SetFitInProjections(true); // param.rec.fitInProjections == -1 ? (iWay == 0) : param.rec.fitInProjections); // TODO: Reenable once fixed
97 prop.SetPropagateBzOnly(param.rec.fitPropagateBzOnly == -1 ? !finalFit : param.rec.fitPropagateBzOnly);
98 prop.SetMatLUT((param.rec.useMatLUT && finalFit) ? merger->GetConstantMem()->calibObjects.matLUT : nullptr);
99 prop.SetTrack(this, iWay ? prop.GetAlpha() : Alpha);
100 ConstrainSinPhi(iWay == 0 ? 0.95f : GPUCA_MAX_SIN_PHI_LOW);
101 CADEBUG(printf("Fitting track %d way %d (sector %d, alpha %f) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", iTrk, iWay, CAMath::Float2IntRn(prop.GetAlpha() / kSectAngle) + (mP[1] < 0 ? 18 : 0), prop.GetAlpha()));
102
103 N = 0;
104 lastUpdateX = -1;
105 const bool inFlyDirection = iWay & 1;
106 const int32_t wayDirection = (iWay & 1) ? -1 : 1;
107
108 int32_t goodRows = 0;
109 for (int32_t ihit = ihitStart; ihit >= 0 && ihit < maxN; ihit += wayDirection) {
110 const bool crossCE = lastSector != 255 && ((lastSector < 18) ^ (clusters[ihit].sector < 18));
111 if (crossCE) {
112 lastSector = clusters[ihit].sector;
113 }
114
115 if ((param.rec.tpc.trackFitRejectMode > 0 && nMissed >= param.rec.tpc.trackFitRejectMode) || nMissed2 >= param.rec.tpc.trackFitMaxRowMissedHard || clusters[ihit].state & GPUTPCGMMergedTrackHit::flagReject) {
116 CADEBUG(printf("\tSkipping hit %d, %d hits rejected, flag %X\n", ihit, nMissed, (int32_t)clusters[ihit].state));
117 if (finalOutInFit && !(clusters[ihit].state & GPUTPCGMMergedTrackHit::flagReject)) {
119 }
120 continue;
121 }
122
123 const bool allowChangeClusters = finalOutInFit && (nWays == 1 || ((iWay & 1) ? (ihit <= CAMath::Max(maxN / 2, maxN - 30)) : (ihit >= CAMath::Min(maxN / 2, 30))));
124
125 int32_t ihitMergeFirst = ihit;
126 uint8_t clusterState = clusters[ihit].state;
127 const float clAlpha = param.Alpha(clusters[ihit].sector);
128 float xx, yy, zz;
129 {
130 const ClusterNative& GPUrestrict() cl = merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[clusters[ihit].num];
131 merger->GetConstantMem()->calibObjects.fastTransformHelper->Transform(clusters[ihit].sector, clusters[ihit].row, cl.getPad(), cl.getTime(), xx, yy, zz, mTOffset);
132 }
133 // clang-format off
134 CADEBUG(printf("\tHit %3d/%3d Row %3d: Cluster Alpha %8.3f %3d, X %8.3f - Y %8.3f, Z %8.3f (Missed %d)\n", ihit, maxN, (int32_t)clusters[ihit].row, clAlpha, (int32_t)clusters[ihit].sector, xx, yy, zz, nMissed));
135 // CADEBUG(if ((uint32_t)merger->GetTrackingChain()->mIOPtrs.nMCLabelsTPC > clusters[ihit].num))
136 // CADEBUG({printf(" MC:"); for (int32_t i = 0; i < 3; i++) {int32_t mcId = merger->GetTrackingChain()->mIOPtrs.mcLabelsTPC[clusters[ihit].num].fClusterID[i].fMCID; if (mcId >= 0) printf(" %d", mcId); } } printf("\n"));
137 // clang-format on
138 if (MergeDoubleRowClusters(ihit, wayDirection, clusters, merger, prop, xx, yy, zz, maxN, clAlpha, clusterState, allowChangeClusters) == -1) {
139 nMissed++;
140 nMissed2++;
141 continue;
142 }
143
144 if (param.rec.tpc.rejectIFCLowRadiusCluster) {
145 const float r2 = xx * xx + yy * yy;
146 const float rmax = (83.5f + param.rec.tpc.sysClusErrorMinDist);
147 if (r2 < rmax * rmax) {
148 MarkClusters(clusters, ihitMergeFirst, ihit, wayDirection, GPUTPCGMMergedTrackHit::flagRejectErr);
149 continue;
150 }
151 }
152
153 const auto& cluster = clusters[ihit];
154
155 // clang-format off
156 CADEBUG(printf("\tSector %2d %11sTrack Alpha %8.3f %s, X %8.3f - Y %8.3f, Z %8.3f - QPt %7.2f (%7.2f), SP %5.2f (%5.2f) %28s --- Cov sY %8.3f sZ %8.3f sSP %8.3f sPt %8.3f - YPt %8.3f\n", (int32_t)cluster.sector, "", prop.GetAlpha(), (CAMath::Abs(prop.GetAlpha() - clAlpha) < 0.01 ? " " : " R!"), mX, mP[0], mP[1], mP[4], prop.GetQPt0(), mP[2], prop.GetSinPhi0(), "", sqrtf(mC[0]), sqrtf(mC[2]), sqrtf(mC[5]), sqrtf(mC[14]), mC[10]));
157 // clang-format on
158 if (allowChangeClusters && lastRow != 255 && CAMath::Abs(cluster.row - lastRow) > 1) {
160 bool dodEdx = param.dodEdxEnabled && param.rec.tpc.adddEdxSubThresholdClusters && finalFit && CAMath::Abs(cluster.row - lastRow) == 2;
161 dodEdx = AttachClustersPropagate(merger, cluster.sector, lastRow, cluster.row, iTrk, track.Leg() == 0, prop, inFlyDirection, GPUCA_MAX_SIN_PHI, dodEdx);
162 if (dodEdx) {
163 dEdx.fillSubThreshold(lastRow - wayDirection);
164 if GPUCA_RTC_CONSTEXPR (GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMask) != GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMaskAlt)) {
165 dEdxAlt.fillSubThreshold(lastRow - wayDirection);
166 }
167 }
168 }
169 }
170
171 int32_t retValProp = prop.PropagateToXAlpha(xx, clAlpha, inFlyDirection);
172 // clang-format off
173 CADEBUG(if (!CheckCov()){printf("INVALID COV AFTER PROPAGATE!!!\n");});
174 // clang-format on
175 if (retValProp == -2) // Rotation failed, try to bring to new x with old alpha first, rotate, and then propagate to x, alpha
176 {
177 CADEBUG(printf("REROTATE\n"));
178 if (prop.PropagateToXAlpha(xx, prop.GetAlpha(), inFlyDirection) == 0) {
179 retValProp = prop.PropagateToXAlpha(xx, clAlpha, inFlyDirection);
180 }
181 }
182 if (lastRow == 255 || CAMath::Abs((int32_t)lastRow - (int32_t)cluster.row) > 5 || lastSector != cluster.sector || (param.rec.tpc.trackFitRejectMode < 0 && -nMissed <= param.rec.tpc.trackFitRejectMode)) {
183 goodRows = 0;
184 } else {
185 goodRows++;
186 }
187 if (retValProp == 0) {
188 lastRow = cluster.row;
189 lastSector = cluster.sector;
190 }
191 // clang-format off
192 CADEBUG(printf("\t%21sPropaga Alpha %8.3f , X %8.3f - Y %8.3f, Z %8.3f - QPt %7.2f (%7.2f), SP %5.2f (%5.2f) --- Res %8.3f %8.3f --- Cov sY %8.3f sZ %8.3f sSP %8.3f sPt %8.3f - YPt %8.3f - PErr %d", "", prop.GetAlpha(), mX, mP[0], mP[1], mP[4], prop.GetQPt0(), mP[2], prop.GetSinPhi0(), mP[0] - yy, mP[1] - zz, sqrtf(mC[0]), sqrtf(mC[2]), sqrtf(mC[5]), sqrtf(mC[14]), mC[10], retValProp));
193 // clang-format on
194
195 if (crossCE) {
196 if (param.rec.tpc.addErrorsCECrossing) {
197 if (param.rec.tpc.addErrorsCECrossing >= 2) {
198 AddCovDiagErrorsWithCorrelations(param.rec.tpc.errorsCECrossing);
199 } else {
200 AddCovDiagErrors(param.rec.tpc.errorsCECrossing);
201 }
202 } else if (mC[2] < 0.5f) {
203 mC[2] = 0.5f;
204 }
205 }
206
207 float uncorrectedY = -1e6f;
208 if (allowChangeClusters) {
209 uncorrectedY = AttachClusters(merger, cluster.sector, cluster.row, iTrk, track.Leg() == 0, prop);
210 }
211
212 const bool sinPhiErr = mNDF > 0 && CAMath::Abs(prop.GetSinPhi0()) >= maxSinForUpdate;
213 if (mNDF >= 0 && (mC[0] > param.rec.tpc.trackFitCovLimit || mC[2] > param.rec.tpc.trackFitCovLimit)) {
214 break;
215 }
216 if (retValProp || sinPhiErr) {
217 MarkClusters(clusters, ihitMergeFirst, ihit, wayDirection, GPUTPCGMMergedTrackHit::flagHighIncl);
218 nMissed2++;
219 NTolerated++;
220 CADEBUG(printf(", %d --- break\n", (int32_t)sinPhiErr));
221 continue;
222 }
223 CADEBUG(printf("\n"));
224
225 int32_t retValUpd = 0, retValInt = 0;
226 float threshold = 3.f + (lastUpdateX >= 0 ? (CAMath::Abs(mX - lastUpdateX) / 2) : 0.f);
227 if (mNDF > (int32_t)param.rec.tpc.mergerNonInterpolateRejectMinNDF && (CAMath::Abs(yy - mP[0]) > threshold || CAMath::Abs(zz - mP[1]) > threshold)) {
229 } else {
230 int8_t rejectChi2 = 0;
231 if (attempt == 0) {
232 if (param.rec.tpc.mergerInterpolateErrors && CAMath::Abs(ihit - ihitMergeFirst) <= 1) {
233 if (iWay == nWays - 3) {
235 } else if (iWay == nWays - 2) {
237 } else if (iWay == nWays - 1) {
238 rejectChi2 = (param.rec.tpc.mergerInterpolateRejectAlsoOnCurrentPosition && GetNDF() > (int32_t)param.rec.tpc.mergerNonInterpolateRejectMinNDF) ? GPUTPCGMPropagator::rejectDirect : 0;
239 }
240 } else {
241 rejectChi2 = allowChangeClusters && goodRows > 5;
242 }
243 }
244
245 float err2Y, err2Z;
246 const float time = merger->GetConstantMem()->ioPtrs.clustersNative ? merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[cluster.num].getTime() : -1.f;
247 const float invSqrtCharge = merger->GetConstantMem()->ioPtrs.clustersNative ? CAMath::InvSqrt(merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[cluster.num].qMax) : 0.f;
248 const float invCharge = merger->GetConstantMem()->ioPtrs.clustersNative ? (1.f / merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[cluster.num].qMax) : 0.f;
249 float invAvgCharge = (sumInvSqrtCharge += invSqrtCharge) / ++nAvgCharge;
250 invAvgCharge *= invAvgCharge;
251
252 prop.GetErr2(err2Y, err2Z, param, zz, cluster.row, clusterState, cluster.sector, time, invAvgCharge, invCharge);
253
254 if (rejectChi2 >= GPUTPCGMPropagator::rejectInterFill) {
257 } else {
258 retValInt = prop.InterpolateReject(param, yy, zz, clusterState, rejectChi2, &interpolation.hit[ihit], err2Y, err2Z, deltaZ);
259 }
260 }
261
262 if (param.rec.tpc.rejectEdgeClustersInTrackFit && uncorrectedY > -1e6f && param.rejectEdgeClusterByY(uncorrectedY, cluster.row, CAMath::Sqrt(mC[0]))) { // uncorrectedY > -1e6f implies allowChangeClusters
264 } else {
265 retValUpd = prop.Update(yy, zz, cluster.row, param, clusterState, rejectChi2, refit, err2Y, err2Z);
266 }
267 GPUCA_DEBUG_STREAMER_CHECK(if (o2::utils::DebugStreamer::checkStream(o2::utils::StreamFlags::streamUpdateTrack, iTrk)) {
268 merger->DebugStreamerUpdate(iTrk, ihit, xx, yy, zz, cluster, merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[cluster.num], *this, prop, interpolation.hit[ihit], rejectChi2, refit, retValUpd, sumInvSqrtCharge / nAvgCharge * sumInvSqrtCharge / nAvgCharge, yy, zz, clusterState, retValInt, err2Y, err2Z);
269 });
270 }
271 // clang-format off
272 CADEBUG(if (!CheckCov()) GPUError("INVALID COV AFTER UPDATE!!!"));
273 CADEBUG(printf("\t%21sFit Alpha %8.3f , X %8.3f - Y %8.3f, Z %8.3f - QPt %7.2f (%7.2f), SP %5.2f (%5.2f), DzDs %5.2f %16s --- Cov sY %8.3f sZ %8.3f sSP %8.3f sPt %8.3f - YPt %8.3f - FErr %d %d\n", "", prop.GetAlpha(), mX, mP[0], mP[1], mP[4], prop.GetQPt0(), mP[2], prop.GetSinPhi0(), mP[3], "", sqrtf(mC[0]), sqrtf(mC[2]), sqrtf(mC[5]), sqrtf(mC[14]), mC[10], retValUpd, retValInt));
274 // clang-format on
275
276 ConstrainSinPhi(); // TODO: Limit using ConstrainSinPhi everywhere!
277 if (!retValUpd && !retValInt) // track is updated
278 {
279 lastUpdateX = mX;
280 covYYUpd = mC[0];
281 nMissed = nMissed2 = 0;
282 UnmarkClusters(clusters, ihitMergeFirst, ihit, wayDirection, GPUTPCGMMergedTrackHit::flagHighIncl);
283 N++;
284 ihitStart = ihit;
285 float dy = mP[0] - prop.Model().Y();
286 float dz = mP[1] - prop.Model().Z();
287 if (CAMath::Abs(mP[4]) * param.qptB5Scaler > 10 && --resetT0 <= 0 && CAMath::Abs(mP[2]) < 0.15f && dy * dy + dz * dz > 1) {
288 CADEBUG(printf("Reinit linearization\n"));
289 prop.SetTrack(this, prop.GetAlpha());
290 }
292 if (param.dodEdxEnabled && finalFit) { // TODO: Costimize flag to remove, and option to remove double-clusters
293 bool acc = (clusterState & param.rec.tpc.dEdxClusterRejectionFlagMask) == 0, accAlt = (clusterState & param.rec.tpc.dEdxClusterRejectionFlagMaskAlt) == 0;
294 if (acc || accAlt) {
295 float qtot = 0, qmax = 0, pad = 0, relTime = 0;
296 const int32_t clusterCount = (ihit - ihitMergeFirst) * wayDirection + 1;
297 for (int32_t iTmp = ihitMergeFirst; iTmp != ihit + wayDirection; iTmp += wayDirection) {
298 const ClusterNative& cl = merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[cluster.num];
299 qtot += cl.qTot;
300 qmax = CAMath::Max<float>(qmax, cl.qMax);
301 pad += cl.getPad();
302 relTime += cl.getTime();
303 }
304 qtot /= clusterCount; // TODO: Weighted Average
305 pad /= clusterCount;
306 relTime /= clusterCount;
307 relTime = relTime - CAMath::Round(relTime);
308 if (acc) {
309 dEdx.fillCluster(qtot, qmax, cluster.row, cluster.sector, mP[2], mP[3], merger->GetConstantMem()->calibObjects, zz, pad, relTime);
310 }
311 if GPUCA_RTC_CONSTEXPR (GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMask) != GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMaskAlt)) {
312 if (accAlt) {
313 dEdxAlt.fillCluster(qtot, qmax, cluster.row, cluster.sector, mP[2], mP[3], merger->GetConstantMem()->calibObjects, zz, pad, relTime);
314 }
315 }
316 }
317 }
318 }
319 } else if (retValInt || retValUpd >= GPUTPCGMPropagator::updateErrorClusterRejected) { // cluster far away form the track
320 if (retValInt || allowChangeClusters) {
321 MarkClusters(clusters, ihitMergeFirst, ihit, wayDirection, GPUTPCGMMergedTrackHit::flagRejectDistance);
322 } else if (finalFit) {
323 MarkClusters(clusters, ihitMergeFirst, ihit, wayDirection, GPUTPCGMMergedTrackHit::flagRejectErr);
324 }
325 if (!retValInt) {
326 nMissed++;
327 nMissed2++;
328 }
329 } else {
330 break; // bad chi2 for the whole track, stop the fit
331 }
332 }
333 if (finalOutInFit && !(merger->Param().rec.tpc.disableRefitAttachment & 4) && lastRow != 255 && lastSector != 255) {
334 StoreLoopPropagation(merger, lastSector, lastRow, iTrk, lastRow > clusters[(iWay & 1) ? (maxN - 1) : 0].row, prop.GetAlpha());
335 CADEBUG(printf("\t\tSTORING %d lastRow %d row %d out %d\n", iTrk, (int)lastRow, (int)clusters[(iWay & 1) ? (maxN - 1) : 0].row, lastRow > clusters[(iWay & 1) ? (maxN - 1) : 0].row));
336 }
337 if (!(iWay & 1) && !finalFit && !track.CCE() && !track.Looper()) {
338 deltaZ = ShiftZ(clusters, merger, maxN);
339 } else {
340 deltaZ = 0.f;
341 }
342 }
343 ConstrainSinPhi();
344
345 GPUCA_DEBUG_STREAMER_CHECK(if (o2::utils::DebugStreamer::checkStream(o2::utils::StreamFlags::streamUpdateTrack, iTrk)) {
346 o2::utils::DebugStreamer::instance()->getStreamer("debug_accept_track", "UPDATE") << o2::utils::DebugStreamer::instance()->getUniqueTreeName("debug_accept_track").data() << "iTrk=" << iTrk << "outerParam=" << track.OuterParam() << "track=" << this << "ihitStart=" << ihitStart << "\n";
347 })
348
349 if (!(N + NTolerated >= GPUCA_TRACKLET_SELECTOR_MIN_HITS_B5(mP[4] * param.qptB5Scaler) && 2 * NTolerated <= CAMath::Max(10, N) && CheckNumericalQuality(covYYUpd))) {
350 return false; // TODO: NTolerated should never become that large, check what is going wrong!
351 }
352 if (param.rec.tpc.minNClustersFinalTrack != -1 && N + NTolerated < param.rec.tpc.minNClustersFinalTrack) {
353 return false;
354 }
355
356 if (param.par.dodEdx && param.dodEdxEnabled) {
357 dEdx.computedEdx(merger->MergedTracksdEdx()[iTrk], param);
358 if GPUCA_RTC_CONSTEXPR (GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMask) != GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMaskAlt)) {
359 dEdxAlt.computedEdx(merger->MergedTracksdEdxAlt()[iTrk], param);
360 }
361 }
362 Alpha = prop.GetAlpha();
363 MoveToReference(prop, param, Alpha);
364 NormalizeAlpha(Alpha);
365
366 return true;
367}
368
369GPUdni() void GPUTPCGMTrackParam::MoveToReference(GPUTPCGMPropagator& prop, const GPUParam& param, float& Alpha)
370{
371 static constexpr float kDeg2Rad = M_PI / 180.f;
372 static constexpr float kSectAngle = 2 * M_PI / 18.f;
373
374 if (param.rec.tpc.trackReferenceX <= 500) {
375 GPUTPCGMTrackParam save = *this;
376 float saveAlpha = Alpha;
377 for (int32_t attempt = 0; attempt < 3; attempt++) {
378 float dAngle = CAMath::Round(CAMath::ATan2(mP[0], mX) / kDeg2Rad / 20.f) * kSectAngle;
379 Alpha += dAngle;
380 if (prop.PropagateToXAlpha(param.rec.tpc.trackReferenceX, Alpha, 0)) {
381 break;
382 }
383 ConstrainSinPhi();
384 if (CAMath::Abs(mP[0]) <= mX * CAMath::Tan(kSectAngle / 2.f)) {
385 return;
386 }
387 }
388 *this = save;
389 Alpha = saveAlpha;
390 }
391 if (CAMath::Abs(mP[0]) > mX * CAMath::Tan(kSectAngle / 2.f)) {
392 float dAngle = CAMath::Round(CAMath::ATan2(mP[0], mX) / kDeg2Rad / 20.f) * kSectAngle;
393 Rotate(dAngle);
394 ConstrainSinPhi();
395 Alpha += dAngle;
396 }
397}
398
399GPUd() void GPUTPCGMTrackParam::MirrorTo(GPUTPCGMPropagator& GPUrestrict() prop, float toY, float toZ, bool inFlyDirection, const GPUParam& param, uint8_t row, uint8_t clusterState, bool mirrorParameters, int8_t sector)
400{
401 if (mirrorParameters) {
402 prop.Mirror(inFlyDirection);
403 }
404 float err2Y, err2Z;
405 prop.GetErr2(err2Y, err2Z, param, toZ, row, clusterState, sector, -1.f, 0.f, 0.f); // Use correct time / avgCharge
406 prop.Model().Y() = mP[0] = toY;
407 prop.Model().Z() = mP[1] = toZ;
408 if (mC[0] < err2Y) {
409 mC[0] = err2Y;
410 }
411 if (mC[2] < err2Z) {
412 mC[2] = err2Z;
413 }
414 if (CAMath::Abs(mC[5]) < 0.1f) {
415 mC[5] = mC[5] > 0 ? 0.1f : -0.1f;
416 }
417 if (mC[9] < 1.f) {
418 mC[9] = 1.f;
419 }
420 mC[1] = mC[4] = mC[6] = mC[8] = mC[11] = mC[13] = 0;
421 prop.SetTrack(this, prop.GetAlpha());
422 mNDF = -3;
423 mChi2 = 0;
424}
425
426GPUd() int32_t GPUTPCGMTrackParam::MergeDoubleRowClusters(int32_t& ihit, int32_t wayDirection, GPUTPCGMMergedTrackHit* GPUrestrict() clusters, const GPUTPCGMMerger* GPUrestrict() merger, GPUTPCGMPropagator& GPUrestrict() prop, float& GPUrestrict() xx, float& GPUrestrict() yy, float& GPUrestrict() zz, int32_t maxN, float clAlpha, uint8_t& GPUrestrict() clusterState, bool rejectChi2)
427{
428 if (ihit + wayDirection >= 0 && ihit + wayDirection < maxN && clusters[ihit].row == clusters[ihit + wayDirection].row && clusters[ihit].sector == clusters[ihit + wayDirection].sector) {
429 float maxDistY, maxDistZ;
430 prop.GetErr2(maxDistY, maxDistZ, merger->Param(), zz, clusters[ihit].row, 0, clusters[ihit].sector, -1.f, 0.f, 0.f); // TODO: Use correct time, avgCharge
431 maxDistY = (maxDistY + mC[0]) * 20.f;
432 maxDistZ = (maxDistZ + mC[2]) * 20.f;
433 int32_t noReject = 0; // Cannot reject if simple estimation of y/z fails (extremely unlike case)
434 if (CAMath::Abs(clAlpha - prop.GetAlpha()) > 1.e-4f) {
435 noReject = prop.RotateToAlpha(clAlpha);
436 }
437 float projY = 0, projZ = 0;
438 if (noReject == 0) {
439 noReject |= prop.GetPropagatedYZ(xx, projY, projZ);
440 }
441 float count = 0.f;
442 xx = yy = zz = 0.f;
443 clusterState = 0;
444 while (true) {
445 const ClusterNative& GPUrestrict() cl = merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[clusters[ihit].num];
446 float clamp = cl.qTot;
447 float clx, cly, clz;
448 merger->GetConstantMem()->calibObjects.fastTransformHelper->Transform(clusters[ihit].sector, clusters[ihit].row, cl.getPad(), cl.getTime(), clx, cly, clz, mTOffset);
449 float dy = cly - projY;
450 float dz = clz - projZ;
451 if (noReject == 0 && (dy * dy > maxDistY || dz * dz > maxDistZ)) {
452 CADEBUG(printf("\t\tRejecting double-row cluster: dy %f, dz %f, chiY %f, chiZ %f (Y: trk %f prj %f cl %f - Z: trk %f prj %f cl %f)\n", dy, dz, sqrtf(maxDistY), sqrtf(maxDistZ), mP[0], projY, cly, mP[1], projZ, clz));
453 if (rejectChi2) {
455 }
456 } else {
457 CADEBUG(printf("\t\tMerging hit row %d X %f Y %f Z %f (dy %f, dz %f, chiY %f, chiZ %f)\n", clusters[ihit].row, clx, cly, clz, dy, dz, sqrtf(maxDistY), sqrtf(maxDistZ)));
458 xx += clx * clamp; // TODO: Weight in pad/time instead of XYZ
459 yy += cly * clamp;
460 zz += clz * clamp;
461 clusterState |= clusters[ihit].state;
462 count += clamp;
463 }
464 if (!(ihit + wayDirection >= 0 && ihit + wayDirection < maxN && clusters[ihit].row == clusters[ihit + wayDirection].row && clusters[ihit].sector == clusters[ihit + wayDirection].sector)) {
465 break;
466 }
467 ihit += wayDirection;
468 }
469 if (count < 0.1f) {
470 CADEBUG(printf("\t\tNo matching cluster in double-row, skipping\n"));
471 return -1;
472 }
473 xx /= count;
474 yy /= count;
475 zz /= count;
476 }
477 return 0;
478}
479
480GPUd() float GPUTPCGMTrackParam::AttachClusters(const GPUTPCGMMerger* GPUrestrict() Merger, int32_t sector, int32_t iRow, int32_t iTrack, bool goodLeg, GPUTPCGMPropagator& prop)
481{
482 float Y, Z;
483 float X = 0;
484 Merger->GetConstantMem()->calibObjects.fastTransformHelper->InverseTransformYZtoX(sector, iRow, mP[0], mP[1], X);
485 if (prop.GetPropagatedYZ(X, Y, Z)) {
486 Y = mP[0];
487 Z = mP[1];
488 }
489 return AttachClusters(Merger, sector, iRow, iTrack, goodLeg, Y, Z);
490}
491
492GPUd() float GPUTPCGMTrackParam::AttachClusters(const GPUTPCGMMerger* GPUrestrict() Merger, int32_t sector, int32_t iRow, int32_t iTrack, bool goodLeg, float Y, float Z)
493{
494 if (Merger->Param().rec.tpc.disableRefitAttachment & 1) {
495 return -1e6f;
496 }
497 const GPUTPCTracker& GPUrestrict() tracker = *(Merger->GetConstantMem()->tpcTrackers + sector);
498 const GPUTPCRow& GPUrestrict() row = tracker.Row(iRow);
499 GPUglobalref() const cahit2* hits = tracker.HitData(row);
500 GPUglobalref() const calink* firsthit = tracker.FirstHitInBin(row);
501 if (row.NHits() == 0) {
502 return -1e6f;
503 }
504
505 const float zOffset = Merger->Param().par.continuousTracking ? Merger->GetConstantMem()->calibObjects.fastTransformHelper->getCorrMap()->convVertexTimeToZOffset(sector, mTOffset, Merger->Param().continuousMaxTimeBin) : 0;
506 const float y0 = row.Grid().YMin();
507 const float stepY = row.HstepY();
508 const float z0 = row.Grid().ZMin() - zOffset; // We can use our own ZOffset, since this is only used temporarily anyway
509 const float stepZ = row.HstepZ();
510 int32_t bin, ny, nz;
511
512 bool protect = CAMath::Abs(GetQPt() * Merger->Param().qptB5Scaler) <= Merger->Param().rec.tpc.rejectQPtB5 && goodLeg;
513
514 float err2Y, err2Z;
515 Merger->Param().GetClusterErrors2(sector, iRow, Z, mP[2], mP[3], -1.f, 0.f, 0.f, err2Y, err2Z); // TODO: Use correct time/avgCharge
516 const float tubeMaxSize2 = protect ? Merger->Param().rec.tpc.tubeProtectMaxSize2 : Merger->Param().rec.tpc.tubeRemoveMaxSize2;
517 const float tubeMinSize2 = protect ? Merger->Param().rec.tpc.tubeProtectMinSize2 : 0.f;
518 const float tubeSigma2 = protect ? Merger->Param().rec.tpc.tubeProtectSigma2 : Merger->Param().rec.tpc.tubeRemoveSigma2;
519 const float sy2 = CAMath::Max(tubeMinSize2, CAMath::Min(tubeMaxSize2, tubeSigma2 * (err2Y + CAMath::Abs(mC[0])))); // Cov can be bogus when following circle
520 const float sz2 = CAMath::Max(tubeMinSize2, CAMath::Min(tubeMaxSize2, tubeSigma2 * (err2Z + CAMath::Abs(mC[2])))); // In that case we should provide the track error externally
521 const float tubeY = CAMath::Sqrt(sy2);
522 const float tubeZ = CAMath::Sqrt(sz2);
523 const float sy21 = 1.f / sy2;
524 const float sz21 = 1.f / sz2;
525 float uncorrectedY, uncorrectedZ;
526 Merger->GetConstantMem()->calibObjects.fastTransformHelper->InverseTransformYZtoNominalYZ(sector, iRow, Y, Z, uncorrectedY, uncorrectedZ);
527
528 if (CAMath::Abs(uncorrectedY) > row.getTPCMaxY()) {
529 return uncorrectedY;
530 }
531 row.Grid().GetBinArea(uncorrectedY, uncorrectedZ + zOffset, tubeY, tubeZ, bin, ny, nz);
532
533 const int32_t nBinsY = row.Grid().Ny();
534 const int32_t idOffset = tracker.Data().ClusterIdOffset();
535 const int32_t* ids = &(tracker.Data().ClusterDataIndex()[row.HitNumberOffset()]);
536 uint32_t myWeight = Merger->TrackOrderAttach()[iTrack] | gputpcgmmergertypes::attachAttached | gputpcgmmergertypes::attachTube;
537 GPUAtomic(uint32_t)* const weights = Merger->ClusterAttachment();
538 if (goodLeg) {
540 }
541 if (protect) {
543 }
544
545 for (int32_t k = 0; k <= nz; k++) {
546 const int32_t mybin = bin + k * nBinsY;
547 const uint32_t hitFst = firsthit[mybin];
548 const uint32_t hitLst = firsthit[mybin + ny + 1];
549 for (uint32_t ih = hitFst; ih < hitLst; ih++) {
550 int32_t id = idOffset + ids[ih];
551 GPUAtomic(uint32_t)* const weight = weights + id;
552 if constexpr (GPUCA_PAR_NO_ATOMIC_PRECHECK == 0) {
553 if (myWeight <= *weight) {
554 continue;
555 }
556 }
557 const cahit2 hh = hits[ih];
558 const float y = y0 + hh.x * stepY;
559 const float z = z0 + hh.y * stepZ;
560 const float dy = y - uncorrectedY;
561 const float dz = z - uncorrectedZ;
562 if (dy * dy * sy21 + dz * dz * sz21 <= CAMath::Sqrt(2.f)) {
563 // CADEBUG(printf("Found Y %f Z %f\n", y, z));
564 CAMath::AtomicMax(weight, myWeight);
565 }
566 }
567 }
568 return uncorrectedY;
569}
570
571GPUd() bool GPUTPCGMTrackParam::AttachClustersPropagate(const GPUTPCGMMerger* GPUrestrict() Merger, int32_t sector, int32_t lastRow, int32_t toRow, int32_t iTrack, bool goodLeg, GPUTPCGMPropagator& GPUrestrict() prop, bool inFlyDirection, float maxSinPhi, bool dodEdx)
572{
573 static constexpr float kSectAngle = 2 * M_PI / 18.f;
574 if (Merger->Param().rec.tpc.disableRefitAttachment & 2) {
575 return dodEdx;
576 }
577 if (CAMath::Abs(lastRow - toRow) < 2) {
578 return dodEdx;
579 }
580 int32_t step = toRow > lastRow ? 1 : -1;
581 float xx = mX - GPUTPCGeometry::Row2X(lastRow);
582 for (int32_t iRow = lastRow + step; iRow != toRow; iRow += step) {
583 if (CAMath::Abs(mP[2]) > maxSinPhi) {
584 return dodEdx;
585 }
586 if (CAMath::Abs(mP[0]) > CAMath::Abs(mX) * CAMath::Tan(kSectAngle / 2.f)) {
587 return dodEdx;
588 }
589 int32_t err = prop.PropagateToXAlpha(xx + GPUTPCGeometry::Row2X(iRow), prop.GetAlpha(), inFlyDirection);
590 if (err) {
591 return dodEdx;
592 }
593 if (dodEdx && iRow + step == toRow) {
594 float yUncorrected, zUncorrected;
595 Merger->GetConstantMem()->calibObjects.fastTransformHelper->InverseTransformYZtoNominalYZ(sector, iRow, mP[0], mP[1], yUncorrected, zUncorrected);
596 uint32_t pad = CAMath::Float2UIntRn(GPUTPCGeometry::LinearY2Pad(sector, iRow, yUncorrected));
597 if (pad >= GPUTPCGeometry::NPads(iRow) || (Merger->GetConstantMem()->calibObjects.dEdxCalibContainer && Merger->GetConstantMem()->calibObjects.dEdxCalibContainer->isDead(sector, iRow, pad))) {
598 dodEdx = false;
599 }
600 }
601 CADEBUG(printf("Attaching in row %d\n", iRow));
602 AttachClusters(Merger, sector, iRow, iTrack, goodLeg, prop);
603 }
604 return dodEdx;
605}
606
607GPUdii() void GPUTPCGMTrackParam::StoreOuter(gputpcgmmergertypes::GPUTPCOuterParam* outerParam, float alpha)
608{
609 CADEBUG(printf("\t%21sStorO Alpha %8.3f , X %8.3f - Y %8.3f, Z %8.3f - QPt %7.2f, SP %5.2f --- Cov sY %8.3f sZ %8.3f sSP %8.3f sPt %8.3f\n", "", alpha, mX, mP[0], mP[1], mP[4], mP[2], sqrtf(mC[0]), sqrtf(mC[2]), sqrtf(mC[5]), sqrtf(mC[14])));
610 for (int32_t i = 0; i < 5; i++) {
611 outerParam->P[i] = mP[i];
612 }
613 for (int32_t i = 0; i < 15; i++) {
614 outerParam->C[i] = mC[i];
615 }
616 outerParam->X = mX;
617 outerParam->alpha = alpha;
618}
619
620GPUdic(0, 1) void GPUTPCGMTrackParam::StoreLoopPropagation(const GPUTPCGMMerger* GPUrestrict() Merger, int32_t sector, int32_t iRow, int32_t iTrack, bool outwards, float alpha)
621{
622 if (iRow == 0 || iRow == GPUCA_ROW_COUNT - 1) {
623 return;
624 }
625 if (CAMath::Abs(mP[2]) >= GPUCA_MAX_SIN_PHI) { // TODO: How can we avoid this?
626 return;
627 }
628 if (CAMath::Abs(mP[2]) < 0.75) {
629 return;
630 }
631 if ((mP[2] * mP[4] < 0) ^ outwards) {
632 return;
633 }
634
635 uint32_t nLoopData = CAMath::AtomicAdd(&Merger->Memory()->nLoopData, 1u);
636 if (nLoopData >= Merger->NMaxTracks()) {
637 Merger->raiseError(GPUErrors::ERROR_MERGER_LOOPER_OVERFLOW, nLoopData, Merger->NMaxTracks());
638 CAMath::AtomicExch(&Merger->Memory()->nLoopData, Merger->NMaxTracks());
639 return;
640 }
642 data.param = *this;
643 data.track = iTrack;
644 data.alpha = alpha;
645 data.sector = sector;
646 data.row = iRow;
647 data.outwards = outwards;
648 Merger->LoopData()[nLoopData] = data;
649}
650
651GPUdii() void GPUTPCGMTrackParam::PropagateLooper(const GPUTPCGMMerger* GPUrestrict() Merger, int32_t loopIdx)
652{
654 prop.SetMaterialTPC();
655 prop.SetPolynomialField(&Merger->Param().polynomialField);
656 prop.SetMaxSinPhi(GPUCA_MAX_SIN_PHI);
657 prop.SetMatLUT(Merger->Param().rec.useMatLUT ? Merger->GetConstantMem()->calibObjects.matLUT : nullptr);
658 prop.SetSeedingErrors(false);
659 prop.SetFitInProjections(true);
660 prop.SetPropagateBzOnly(false);
661
662 GPUTPCGMLoopData& data = Merger->LoopData()[loopIdx];
663 prop.SetTrack(&data.param, data.alpha);
664 if (false) {
665 data.param.AttachClustersLooper(Merger, data.sector, data.row, data.track, data.outwards, prop);
666 } else {
667 data.param.AttachClustersLooperFollow(Merger, prop, data.sector, data.row, data.track, data.outwards);
668 }
669}
670
671GPUdi() void GPUTPCGMTrackParam::AttachClustersLooperFollow(const GPUTPCGMMerger* GPUrestrict() Merger, GPUTPCGMPropagator& GPUrestrict() prop, int32_t sector, int32_t iRow, int32_t iTrack, bool up)
672{
673 float toX = mX;
674 bool inFlyDirection = (Merger->MergedTracks()[iTrack].Leg() & 1) ^ up;
675
676 static constexpr float kSectAngle = 2 * M_PI / 18.f;
677 const GPUParam& GPUrestrict() param = Merger->Param();
678 bool right = (mP[2] < 0) ^ up;
679 const int32_t sectorSide = sector >= (GPUCA_NSECTORS / 2) ? (GPUCA_NSECTORS / 2) : 0;
680 float lrFactor = right ^ !up ? 1.f : -1.f;
681 // clang-format off
682 CADEBUG(printf("\nCIRCLE Track %d: Sector %d Alpha %f X %f Y %f Z %f SinPhi %f DzDs %f QPt %f - Right %d Up %d lrFactor %f\n", iTrack, sector, prop.GetAlpha(), mX, mP[0], mP[1], mP[2], mP[3], mP[4], (int32_t)right, (int32_t)up, lrFactor));
683 // clang-format on
684
685 if (prop.RotateToAlpha(prop.GetAlpha() + (CAMath::Pi() / 2.f) * lrFactor)) {
686 return;
687 }
688 CADEBUG(printf("\tRotated: X %f Y %f Z %f SinPhi %f (Alpha %f / %f)\n", mP[0], mX, mP[1], mP[2], prop.GetAlpha(), prop.GetAlpha() + CAMath::Pi() / 2.f));
689 uint32_t maxTries = 100;
690 while (true) {
691 while (CAMath::Abs(mX) <= CAMath::Abs(mP[0]) * CAMath::Tan(kSectAngle / 2.f) + 0.1f) {
692 if (maxTries-- == 0) {
693 return;
694 }
695 if (CAMath::Abs(mP[2]) > 0.7f) {
696 return;
697 }
698 if (up ? (-mP[0] * lrFactor > GPUTPCGeometry::Row2X(GPUCA_ROW_COUNT - 1)) : (-mP[0] * lrFactor < GPUTPCGeometry::Row2X(0))) {
699 return;
700 }
701 if (!((up ? (-mP[0] * lrFactor >= toX) : (-mP[0] * lrFactor <= toX)) || (right ^ (mP[2] > 0)))) {
702 return;
703 }
704 int32_t err = prop.PropagateToXAlpha(mX + (up ? 1.f : -1.f), prop.GetAlpha(), inFlyDirection);
705 if (err) {
706 CADEBUG(printf("\t\tpropagation error (%d)\n", err));
707 return;
708 }
709 CADEBUG(printf("\tPropagated to y = %f: X %f Z %f SinPhi %f\n", mX, mP[0], mP[1], mP[2]));
710 for (int32_t j = 0; j < GPUCA_ROW_COUNT; j++) { // TODO: Avoid iterating over all rows
711 float rowX = GPUTPCGeometry::Row2X(j);
712 if (CAMath::Abs(rowX - (-mP[0] * lrFactor)) < 1.5f) {
713 CADEBUG(printf("\t\tAttempt row %d (X %f Y %f Z %f)\n", j, rowX, mX * lrFactor, mP[1]));
714 AttachClusters(Merger, sector, j, iTrack, false, mX * lrFactor, mP[1]);
715 }
716 }
717 }
718 if (maxTries-- == 0) {
719 return;
720 }
721 if (right) {
722 if (++sector >= sectorSide + 18) {
723 sector -= 18;
724 }
725 } else {
726 if (--sector < sectorSide) {
727 sector += 18;
728 }
729 }
730 CADEBUG(printf("\tRotating to sector %d: %f --> %f\n", sector, prop.GetAlpha(), param.Alpha(sector) + (CAMath::Pi() / 2.f) * lrFactor));
731 int32_t err = prop.RotateToAlpha(param.Alpha(sector) + (CAMath::Pi() / 2.f) * lrFactor);
732 if (err) {
733 CADEBUG(printf("Rotation Error %d\n", err));
734 return;
735 }
736 CADEBUG(printf("\tAfter Rotating Alpha %f Position X %f Y %f Z %f SinPhi %f\n", prop.GetAlpha(), mP[0], mX, mP[1], mP[2]));
737 }
738}
739
740GPUdi() void GPUTPCGMTrackParam::AttachClustersLooper(const GPUTPCGMMerger* GPUrestrict() Merger, int32_t sector, int32_t iRow, int32_t iTrack, bool outwards, GPUTPCGMPropagator& GPUrestrict() prop)
741{
742 static constexpr float kSectAngle = 2 * M_PI / 18.f;
743 // Note that the coordinate system is rotated by 90 degree swapping X and Y!
744 float X = mP[2] > 0 ? mP[0] : -mP[0];
745 float Y = mP[2] > 0 ? -mX : mX;
746 float Z = mP[1];
747 float SinPhi = CAMath::Sqrt(1 - mP[2] * mP[2]) * (mP[2] > 0 ? -1 : 1);
748 float b = prop.GetBz(prop.GetAlpha(), mX, mP[0], mP[1]);
749
750 float dx = outwards ? 1.f : -1.f;
751 const float myRowX = GPUTPCGeometry::Row2X(iRow);
752 // printf("\nAttachMirror sector %d row %d outwards %d\n", (int)sector, (int)iRow, (int)outwards);
753 // printf("X %f Y %f Z %f SinPhi %f -->\n", mX, mP[0], mP[1], mP[2]);
754 // printf("X %f Y %f Z %f SinPhi %f, dx %f\n", X, Y, Z, SinPhi, dx);
755 uint32_t maxTries = 100;
756 while (maxTries--) {
757 float ex = CAMath::Sqrt(1 - SinPhi * SinPhi);
758 float exi = 1.f / ex;
759 float dxBzQ = dx * -b * mP[4];
760 float newSinPhi = SinPhi + dxBzQ;
761 if (CAMath::Abs(newSinPhi) > GPUCA_MAX_SIN_PHI_LOW) {
762 // printf("Abort, newSinPhi %f\n", newSinPhi);
763 return;
764 }
765 if (mP[2] > 0 ? (newSinPhi > 0.5) : (newSinPhi < -0.5)) {
766 // printf("Finished, newSinPhi %f\n", newSinPhi);
767 return;
768 }
769 float dS = dx * exi;
770 float h2 = dS * exi * exi;
771 float h4 = .5f * h2 * dxBzQ;
772
773 X += dx;
774 Y += dS * SinPhi + h4;
775 Z += dS * mP[3];
776 SinPhi = newSinPhi;
777 if (CAMath::Abs(X) > CAMath::Abs(Y) * CAMath::Tan(kSectAngle / 2.f)) {
778 // printf("Abort, sector edge\n");
779 return;
780 }
781
782 // printf("count %d: At X %f Y %f Z %f SinPhi %f\n", maxTries, mP[2] > 0 ? -Y : Y, mP[2] > 0 ? X : -X, Z, SinPhi);
783 float paramX = mP[2] > 0 ? -Y : Y;
784 int32_t step = outwards ? 1 : -1;
785 int32_t found = 0;
786 for (int32_t j = iRow; j >= 0 && j < GPUCA_ROW_COUNT && found < 3; j += step) {
787 float rowX = mX + GPUTPCGeometry::Row2X(j) - myRowX;
788 if (CAMath::Abs(rowX - paramX) < 1.5f) {
789 // printf("Attempt row %d at y %f\n", j, X);
790 AttachClusters(Merger, sector, j, iTrack, false, mP[2] > 0 ? X : -X, Z);
791 }
792 }
793 }
794}
795
796GPUd() float GPUTPCGMTrackParam::ShiftZ(const GPUTPCGMMergedTrackHit* clusters, const GPUTPCGMMerger* merger, int32_t N)
797{
798 if (N == 0) {
799 N = 1;
800 }
801 const auto& GPUrestrict() cls = merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear;
802 float z0 = cls[clusters[0].num].getTime(), zn = cls[clusters[N - 1].num].getTime();
803 const auto tmp = zn > z0 ? std::array<float, 3>{zn, z0, GPUTPCGeometry::Row2X(clusters[N - 1].row)} : std::array<float, 3>{z0, zn, GPUTPCGeometry::Row2X(clusters[0].row)};
804 return ShiftZ(merger, clusters[0].sector, tmp[0], tmp[1], tmp[2]);
805}
806
807GPUd() float GPUTPCGMTrackParam::ShiftZ(const GPUTPCGMMerger* GPUrestrict() merger, int32_t sector, float cltmax, float cltmin, float clx)
808{
809 if (!merger->Param().par.continuousTracking) {
810 return 0.f;
811 }
812 float deltaZ = 0.f;
813 bool beamlineReached = false;
814 const float r1 = CAMath::Max(0.0001f, CAMath::Abs(mP[4] * merger->Param().polynomialField.GetNominalBz()));
815 if (r1 < 0.01501) { // 100 MeV @ 0.5T ~ 0.66m cutof
816 const float dist2 = mX * mX + mP[0] * mP[0];
817 const float dist1r2 = dist2 * r1 * r1;
818 if (dist1r2 < 4) {
819 const float alpha = CAMath::ACos(1 - 0.5f * dist1r2); // Angle of a circle, such that |(cosa, sina) - (1,0)| == dist
820 const float beta = CAMath::ATan2(mP[0], mX);
821 const int32_t comp = mP[2] > CAMath::Sin(beta);
822 const float sinab = CAMath::Sin((comp ? 0.5f : -0.5f) * alpha + beta); // Angle of circle through origin and track position, to be compared to Snp
823 const float res = CAMath::Abs(sinab - mP[2]);
824
825 if (res < 0.2) {
826 const float r = 1.f / r1;
827 const float dS = alpha * r;
828 float z0 = dS * mP[3];
829 if (CAMath::Abs(z0) > GPUTPCGeometry::TPCLength()) {
830 z0 = z0 > 0 ? GPUTPCGeometry::TPCLength() : -GPUTPCGeometry::TPCLength();
831 }
832 deltaZ = mP[1] - z0;
833 beamlineReached = true;
834
835 // printf("X %9.3f Y %9.3f QPt %9.3f R %9.3f --> Alpha %9.3f Snp %9.3f Snab %9.3f Res %9.3f dS %9.3f z0 %9.3f\n", mX, mP[0], mP[4], r, alpha / 3.1415 * 180, mP[2], sinab, res, dS, z0);
836 }
837 }
838 }
839
840 if (!beamlineReached) {
841 float refZ = ((sector < GPUCA_NSECTORS / 2) ? merger->Param().rec.tpc.defaultZOffsetOverR : -merger->Param().rec.tpc.defaultZOffsetOverR) * clx;
842 float basez;
843 merger->GetConstantMem()->calibObjects.fastTransformHelper->getCorrMap()->TransformIdealZ(sector, cltmax, basez, mTOffset);
844 deltaZ = basez - refZ;
845 }
846 {
847 float deltaT = merger->GetConstantMem()->calibObjects.fastTransformHelper->getCorrMap()->convDeltaZtoDeltaTimeInTimeFrame(sector, deltaZ);
848 mTOffset += deltaT;
849 const float maxT = cltmin - merger->GetConstantMem()->calibObjects.fastTransformHelper->getCorrMap()->getT0();
850 const float minT = cltmax - merger->GetConstantMem()->calibObjects.fastTransformHelper->getCorrMap()->getMaxDriftTime(sector);
851 // printf("T Check: Clusters %f %f, min %f max %f vtx %f\n", tz1, tz2, minT, maxT, mTOffset);
852 deltaT = 0.f;
853 if (mTOffset < minT) {
854 deltaT = minT - mTOffset;
855 }
856 if (mTOffset + deltaT > maxT) {
857 deltaT = maxT - mTOffset;
858 }
859 if (deltaT != 0.f) {
860 deltaZ += merger->GetConstantMem()->calibObjects.fastTransformHelper->getCorrMap()->convDeltaTimeToDeltaZinTimeFrame(sector, deltaT);
861 // printf("Moving clusters to TPC Range: QPt %f, New mTOffset %f, t1 %f, t2 %f, Shift %f in Z: %f to %f --> %f to %f in T\n", mP[4], mTOffset + deltaT, tz1, tz2, deltaZ, tz2 - mTOffset, tz1 - mTOffset, tz2 - mTOffset - deltaT, tz1 - mTOffset - deltaT);
862 mTOffset += deltaT;
863 }
864 mP[1] -= deltaZ;
865 }
866 // printf("\n");
867 return -deltaZ;
868}
869
870GPUd() bool GPUTPCGMTrackParam::CheckCov() const
871{
872 const float* c = mC;
873 bool ok = c[0] >= 0 && c[2] >= 0 && c[5] >= 0 && c[9] >= 0 && c[14] >= 0 && (c[1] * c[1] <= c[2] * c[0]) && (c[3] * c[3] <= c[5] * c[0]) && (c[4] * c[4] <= c[5] * c[2]) && (c[6] * c[6] <= c[9] * c[0]) && (c[7] * c[7] <= c[9] * c[2]) && (c[8] * c[8] <= c[9] * c[5]) &&
874 (c[10] * c[10] <= c[14] * c[0]) && (c[11] * c[11] <= c[14] * c[2]) && (c[12] * c[12] <= c[14] * c[5]) && (c[13] * c[13] <= c[14] * c[9]);
875 return ok;
876}
877
878GPUd() bool GPUTPCGMTrackParam::CheckNumericalQuality(float overrideCovYY) const
879{
880 //* Check that the track parameters and covariance matrix are reasonable
881 bool ok = CAMath::Finite(mX) && CAMath::Finite(mChi2);
882 // CADEBUG(printf("OK %d - %f - ", (int32_t)ok, mX); for (int32_t i = 0; i < 5; i++) { printf("%f ", mP[i]); } printf(" - "); for (int32_t i = 0; i < 15; i++) { printf("%f ", mC[i]); } printf("\n"));
883 const float* c = mC;
884 for (int32_t i = 0; i < 15; i++) {
885 ok = ok && CAMath::Finite(c[i]);
886 }
887 for (int32_t i = 0; i < 5; i++) {
888 ok = ok && CAMath::Finite(mP[i]);
889 }
890 if ((overrideCovYY > 0 ? overrideCovYY : c[0]) > 4.f * 4.f || c[2] > 4.f * 4.f || c[5] > 2.f * 2.f || c[9] > 2.f * 2.f) {
891 ok = 0;
892 }
893 if (CAMath::Abs(mP[2]) > GPUCA_MAX_SIN_PHI) {
894 ok = 0;
895 }
896 if (!CheckCov()) {
897 ok = false;
898 }
899 return ok;
900}
901
902GPUdii() void GPUTPCGMTrackParam::RefitTrack(GPUTPCGMMergedTrack& GPUrestrict() track, int32_t iTrk, GPUTPCGMMerger* GPUrestrict() merger, int32_t attempt) // VS: GPUd changed to GPUdii. No change in output and no performance penalty.
903{
904 if (!track.OK()) {
905 return;
906 }
907
908 // clang-format off
909 CADEBUG(if (DEBUG_SINGLE_TRACK != -1 && iTrk != ((DEBUG_SINGLE_TRACK == -2 && getenv("DEBUG_TRACK")) ? atoi(getenv("DEBUG_TRACK")) : DEBUG_SINGLE_TRACK)) { track.SetNClusters(0); track.SetOK(0); return; } );
910 // clang-format on
911
912 int32_t nTrackHits = track.NClusters();
913 int32_t NTolerated = 0; // Clusters not fit but tollerated for track length cut
914 GPUTPCGMTrackParam t = track.Param();
915 float Alpha = track.Alpha();
916 CADEBUG(int32_t nTrackHitsOld = nTrackHits; float ptOld = t.QPt());
917 bool ok = t.Fit(merger, iTrk, merger->Clusters() + track.FirstClusterRef(), nTrackHits, NTolerated, Alpha, attempt, GPUCA_MAX_SIN_PHI, track);
918 CADEBUG(printf("Finished Fit Track %d\n", iTrk));
919 CADEBUG(printf("OUTPUT hits %d -> %d+%d = %d, QPt %f -> %f, SP %f, OK %d chi2 %f chi2ndf %f\n", nTrackHitsOld, nTrackHits, NTolerated, nTrackHits + NTolerated, ptOld, t.QPt(), t.SinPhi(), (int32_t)ok, t.Chi2(), t.Chi2() / CAMath::Max(1, nTrackHits)));
920
921 if (!ok && attempt == 0 && merger->Param().rec.tpc.retryRefit) {
922 for (uint32_t i = 0; i < track.NClusters(); i++) {
923 merger->Clusters()[track.FirstClusterRef() + i].state &= GPUTPCGMMergedTrackHit::clustererAndSharedFlags;
924 }
925 CADEBUG(printf("Track rejected, marking for retry\n"));
926 if (merger->Param().rec.tpc.retryRefit == 2) {
927 nTrackHits = track.NClusters();
928 NTolerated = 0; // Clusters not fit but tollerated for track length cut
929 t = track.Param();
930 Alpha = track.Alpha();
931 ok = t.Fit(merger, iTrk, merger->Clusters() + track.FirstClusterRef(), nTrackHits, NTolerated, Alpha, 1, GPUCA_MAX_SIN_PHI, track);
932 } else {
933 uint32_t nRefit = CAMath::AtomicAdd(&merger->Memory()->nRetryRefit, 1u);
934 merger->RetryRefitIds()[nRefit] = iTrk;
935 return;
936 }
937 }
938 if (CAMath::Abs(t.QPt()) < 1.e-4f) {
939 t.QPt() = 1.e-4f;
940 }
941
942 CADEBUG(if (t.GetX() > 250) { printf("ERROR, Track %d at impossible X %f, Pt %f, Looper %d\n", iTrk, t.GetX(), CAMath::Abs(1.f / t.QPt()), (int32_t)merger->MergedTracks()[iTrk].Looper()); });
943
944 track.SetOK(ok);
945 track.SetNClustersFitted(nTrackHits);
946 track.Param() = t;
947 track.Alpha() = Alpha;
948
949 // if (track.OK()) merger->DebugRefitMergedTrack(track);
950}
951
952GPUd() void GPUTPCGMTrackParam::Rotate(float alpha)
953{
954 float cA, sA;
955 CAMath::SinCos(alpha, sA, cA);
956 float x0 = mX;
957 float sinPhi0 = mP[2], cosPhi0 = CAMath::Sqrt(1 - mP[2] * mP[2]);
958 float cosPhi = cosPhi0 * cA + sinPhi0 * sA;
959 float sinPhi = -cosPhi0 * sA + sinPhi0 * cA;
960 float j0 = cosPhi0 / cosPhi;
961 float j2 = cosPhi / cosPhi0;
962 mX = x0 * cA + mP[0] * sA;
963 mP[0] = -x0 * sA + mP[0] * cA;
964 mP[2] = sinPhi;
965 mC[0] *= j0 * j0;
966 mC[1] *= j0;
967 mC[3] *= j0;
968 mC[6] *= j0;
969 mC[10] *= j0;
970
971 mC[3] *= j2;
972 mC[4] *= j2;
973 mC[5] *= j2 * j2;
974 mC[8] *= j2;
975 mC[12] *= j2;
976 if (cosPhi < 0) { // change direction ( t0 direction is already changed in t0.UpdateValues(); )
977 SinPhi() = -SinPhi();
978 DzDs() = -DzDs();
979 QPt() = -QPt();
980 mC[3] = -mC[3];
981 mC[4] = -mC[4];
982 mC[6] = -mC[6];
983 mC[7] = -mC[7];
984 mC[10] = -mC[10];
985 mC[11] = -mC[11];
986 }
987}
988
989GPUd() void GPUTPCGMTrackParam::AddCovDiagErrors(const float* GPUrestrict() errors2)
990{
991 mC[0] += errors2[0];
992 mC[2] += errors2[1];
993 mC[5] += errors2[2];
994 mC[9] += errors2[3];
995 mC[14] += errors2[4];
996}
997
998GPUd() void GPUTPCGMTrackParam::AddCovDiagErrorsWithCorrelations(const float* GPUrestrict() errors2)
999{
1000 const int32_t diagMap[5] = {0, 2, 5, 9, 14};
1001 const float oldDiag[5] = {mC[0], mC[2], mC[5], mC[9], mC[14]};
1002 for (int32_t i = 0; i < 5; i++) {
1003 mC[diagMap[i]] += errors2[i];
1004 for (int32_t j = 0; j < i; j++) {
1005 mC[diagMap[i - 1] + j + 1] *= gpu::CAMath::Sqrt(mC[diagMap[i]] * mC[diagMap[j]] / (oldDiag[i] * oldDiag[j]));
1006 }
1007 }
1008}
benchmark::State & state
Helper class to access correction maps.
int16_t time
Definition RawEventData.h:4
int32_t i
#define GPUdic(...)
#define GPUAtomic(type)
#define GPUrestrict()
#define GPUglobalref()
#define GPUCA_DEBUG_STREAMER_CHECK(...)
#define GPUCA_RTC_CONSTEXPR
#define GPUCA_MAX_SIN_PHI_LOW
#define GPUCA_TRACKLET_SELECTOR_MIN_HITS_B5(QPTB5)
#define GPUCA_MAX_SIN_PHI
#define GPUCA_PAR_NO_ATOMIC_PRECHECK
#define GPUCA_PAR_MERGER_INTERPOLATION_ERROR_TYPE_A
#define CADEBUG(...)
Definition GPUDef.h:72
#define GPUCA_GET_CONSTEXPR(obj, val)
GPUdii() void GPUTPCGMTrackParam
GPUdni() void GPUTPCGMTrackParam
#define DEBUG_SINGLE_TRACK
#define GPUCA_NSECTORS
#define GPUCA_ROW_COUNT
uint32_t j
Definition RawData.h:0
uint32_t res
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
Definition of TPCFastTransform class.
double num
float int32_t const GPUParam int16_t int8_t bool int8_t sector
float int32_t const GPUParam & param
int32_t GPUTPCGMMergedTrackHit int32_t int32_t float & Alpha
int32_t GPUTPCGMMergedTrackHit int32_t int32_t float int32_t float GPUTPCGMMergedTrack & track
int32_t int32_t int32_t bool float Y
GLfloat GLfloat GLfloat alpha
Definition glcorearb.h:279
GLsizei const GLuint const GLfloat * weights
Definition glcorearb.h:5475
GLint GLsizei count
Definition glcorearb.h:399
GLuint * ids
Definition glcorearb.h:647
GLenum array
Definition glcorearb.h:4274
GLdouble GLdouble right
Definition glcorearb.h:4077
GLuint GLuint GLfloat weight
Definition glcorearb.h:5477
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLboolean * data
Definition glcorearb.h:298
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLuint GLfloat x0
Definition glcorearb.h:5034
GLenum clamp
Definition glcorearb.h:1245
GLboolean r
Definition glcorearb.h:1233
GLenum GLfloat param
Definition glcorearb.h:271
GLuint id
Definition glcorearb.h:650
GLuint GLfloat GLfloat y0
Definition glcorearb.h:5034
GLdouble GLdouble GLdouble z
Definition glcorearb.h:843
uint32_t calink
Definition GPUTPCDef.h:30
Global TPC definitions and constants.
Definition SimTraits.h:168
GPUd() void PIDResponse
Definition PIDResponse.h:71
GPUdi() T BetheBlochAleph(T bg
value_T step
Definition TrackUtils.h:42
value_T maxT
Definition TrackUtils.h:132
@ streamUpdateTrack
stream update track informations
Defining DataPointCompositeObject explicitly as copiable.
std::string getTime(uint64_t ts)
GPUCA_PAR_MERGER_INTERPOLATION_ERROR_TYPE_A errorY
InterpolationErrorHit hit[GPUCA_MERGER_MAX_TRACK_CLUSTERS]
std::vector< Cluster > clusters
std::vector< int > row