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 "TPCFastTransformPOD.h"
32#include "GPUTPCConvertImpl.h"
33#include "GPUTPCGMMergerTypes.h"
34#include "GPUParam.inc"
35#include "GPUGetConstexpr.h"
36
37#ifdef GPUCA_CADEBUG_ENABLED
38#include "GPUSettings.h"
40#endif
41
42#ifndef GPUCA_GPUCODE_DEVICE
43#include <cmath>
44#include <cstdlib>
45#endif
46
47using namespace o2::gpu;
48using namespace o2::tpc;
49
50GPUd() 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)
51{
52 constexpr float kDeg2Rad = M_PI / 180.f;
53 CADEBUG(static constexpr float kSectAngle = 2 * M_PI / 18.f);
54
55 const GPUParam& GPUrestrict() param = merger->Param();
56
57 GPUdEdx dEdx, dEdxAlt;
60 prop.SetMaterialTPC();
61 prop.SetPolynomialField(&param.polynomialField);
62 prop.SetMaxSinPhi(maxSinPhi);
63 if (param.rec.tpc.mergerInterpolateErrors) {
64 for (int32_t i = 0; i < N; i++) {
65 interpolation.hit[i].errorY = -1;
66 }
67 }
68
69 const int32_t nWays = param.rec.tpc.nWays;
70 const int32_t maxN = N;
71 int32_t ihitStart = 0;
72 float covYYUpd = 0.f;
73 float lastUpdateX = -1.f;
74 uint8_t lastRow = 255;
75 uint8_t lastSector = 255;
76 float deltaZ = 0.f;
77
78 for (int32_t iWay = 0; iWay < nWays; iWay++) {
79 int32_t nMissed = 0, nMissed2 = 0;
80 float sumInvSqrtCharge = 0.f;
81 int32_t nAvgCharge = 0;
82
83 if (iWay && (iWay & 1) == 0) {
84 StoreOuter(&track.OuterParam(), prop.GetAlpha());
85 }
86
87 int32_t resetT0 = initResetT0();
88 const bool refit = (nWays == 1 || iWay >= 1);
89 const bool finalOutInFit = iWay + 2 >= nWays;
90 const bool finalFit = iWay == nWays - 1;
91 const float maxSinForUpdate = CAMath::Sin(70.f * kDeg2Rad);
92
93 ResetCovariance();
94 prop.SetSeedingErrors(!(refit && attempt == 0));
95 prop.SetFitInProjections(true); // param.rec.fitInProjections == -1 ? (iWay == 0) : param.rec.fitInProjections); // TODO: Reenable once fixed
96 prop.SetPropagateBzOnly(param.rec.fitPropagateBzOnly == -1 ? !finalFit : param.rec.fitPropagateBzOnly);
97 prop.SetMatLUT((param.rec.useMatLUT && finalFit) ? merger->GetConstantMem()->calibObjects.matLUT : nullptr);
98 prop.SetTrack(this, iWay ? prop.GetAlpha() : Alpha);
99 ConstrainSinPhi(iWay == 0 ? 0.95f : constants::MAX_SIN_PHI_LOW);
100 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()));
101
102 N = 0;
103 lastUpdateX = -1;
104 const bool inFlyDirection = iWay & 1;
105 const int32_t wayDirection = (iWay & 1) ? -1 : 1;
106
107 int32_t goodRows = 0;
108 for (int32_t ihit = ihitStart; ihit >= 0 && ihit < maxN; ihit += wayDirection) {
109 const bool crossCE = lastSector != 255 && ((lastSector < 18) ^ (clusters[ihit].sector < 18));
110 if (crossCE) {
111 lastSector = clusters[ihit].sector;
112 }
113
114 if ((param.rec.tpc.trackFitRejectMode > 0 && nMissed >= param.rec.tpc.trackFitRejectMode) || nMissed2 >= param.rec.tpc.trackFitMaxRowMissedHard || clusters[ihit].state & GPUTPCGMMergedTrackHit::flagReject) {
115 CADEBUG(printf("\tSkipping hit %d, %d hits rejected, flag %X\n", ihit, nMissed, (int32_t)clusters[ihit].state));
116 if (finalOutInFit && !(clusters[ihit].state & GPUTPCGMMergedTrackHit::flagReject)) {
118 }
119 continue;
120 }
121
122 const bool allowChangeClusters = finalOutInFit && (nWays == 1 || ((iWay & 1) ? (ihit <= CAMath::Max(maxN / 2, maxN - 30)) : (ihit >= CAMath::Min(maxN / 2, 30))));
123
124 int32_t ihitMergeFirst = ihit;
125 uint8_t clusterState = clusters[ihit].state;
126 const float clAlpha = param.Alpha(clusters[ihit].sector);
127 float xx, yy, zz;
128 {
129 const ClusterNative& GPUrestrict() cl = merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[clusters[ihit].num];
130 merger->GetConstantMem()->calibObjects.fastTransform->Transform(clusters[ihit].sector, clusters[ihit].row, cl.getPad(), cl.getTime(), xx, yy, zz, mTOffset);
131 }
132 // clang-format off
133 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));
134 // CADEBUG(if ((uint32_t)merger->GetTrackingChain()->mIOPtrs.nMCLabelsTPC > clusters[ihit].num))
135 // 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"));
136 // clang-format on
137 if (MergeDoubleRowClusters(ihit, wayDirection, clusters, merger, prop, xx, yy, zz, maxN, clAlpha, clusterState, allowChangeClusters) == -1) {
138 nMissed++;
139 nMissed2++;
140 continue;
141 }
142
143 if (param.rec.tpc.rejectIFCLowRadiusCluster) {
144 const float r2 = xx * xx + yy * yy;
145 const float rmax = (83.5f + param.rec.tpc.sysClusErrorMinDist);
146 if (r2 < rmax * rmax) {
147 MarkClusters(clusters, ihitMergeFirst, ihit, wayDirection, GPUTPCGMMergedTrackHit::flagRejectErr);
148 continue;
149 }
150 }
151
152 const auto& cluster = clusters[ihit];
153
154 // clang-format off
155 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]));
156 // clang-format on
157 if (allowChangeClusters && lastRow != 255 && CAMath::Abs(cluster.row - lastRow) > 1) {
159 bool dodEdx = param.dodEdxEnabled && param.rec.tpc.adddEdxSubThresholdClusters && finalFit && CAMath::Abs(cluster.row - lastRow) == 2;
160 dodEdx = AttachClustersPropagate(merger, cluster.sector, lastRow, cluster.row, iTrk, track.Leg() == 0, prop, inFlyDirection, constants::MAX_SIN_PHI, dodEdx);
161 if (dodEdx) {
162 dEdx.fillSubThreshold(lastRow - wayDirection);
163 if GPUCA_RTC_CONSTEXPR (GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMask) != GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMaskAlt)) {
164 dEdxAlt.fillSubThreshold(lastRow - wayDirection);
165 }
166 }
167 }
168 }
169
170 int32_t retValProp = prop.PropagateToXAlpha(xx, clAlpha, inFlyDirection);
171 // clang-format off
172 CADEBUG(if (!CheckCov()){printf("INVALID COV AFTER PROPAGATE!!!\n");});
173 // clang-format on
174 if (retValProp == -2) // Rotation failed, try to bring to new x with old alpha first, rotate, and then propagate to x, alpha
175 {
176 CADEBUG(printf("REROTATE\n"));
177 if (prop.PropagateToXAlpha(xx, prop.GetAlpha(), inFlyDirection) == 0) {
178 retValProp = prop.PropagateToXAlpha(xx, clAlpha, inFlyDirection);
179 }
180 }
181 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)) {
182 goodRows = 0;
183 } else {
184 goodRows++;
185 }
186 if (retValProp == 0) {
187 lastRow = cluster.row;
188 lastSector = cluster.sector;
189 }
190 // clang-format off
191 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));
192 // clang-format on
193
194 if (crossCE) {
195 if (param.rec.tpc.addErrorsCECrossing) {
196 if (param.rec.tpc.addErrorsCECrossing >= 2) {
197 AddCovDiagErrorsWithCorrelations(param.rec.tpc.errorsCECrossing);
198 } else {
199 AddCovDiagErrors(param.rec.tpc.errorsCECrossing);
200 }
201 } else if (mC[2] < 0.5f) {
202 mC[2] = 0.5f;
203 }
204 }
205
206 float uncorrectedY = -1e6f;
207 if (allowChangeClusters) {
208 uncorrectedY = AttachClusters(merger, cluster.sector, cluster.row, iTrk, track.Leg() == 0, prop);
209 }
210
211 const bool sinPhiErr = mNDF > 0 && CAMath::Abs(prop.GetSinPhi0()) >= maxSinForUpdate;
212 if (mNDF >= 0 && (mC[0] > param.rec.tpc.trackFitCovLimit || mC[2] > param.rec.tpc.trackFitCovLimit)) {
213 break;
214 }
215 if (retValProp || sinPhiErr) {
216 MarkClusters(clusters, ihitMergeFirst, ihit, wayDirection, GPUTPCGMMergedTrackHit::flagHighIncl);
217 nMissed2++;
218 NTolerated++;
219 CADEBUG(printf(", %d --- break\n", (int32_t)sinPhiErr));
220 continue;
221 }
222 CADEBUG(printf("\n"));
223
224 int32_t retValUpd = 0, retValInt = 0;
225 float threshold = 3.f + (lastUpdateX >= 0 ? (CAMath::Abs(mX - lastUpdateX) / 2) : 0.f);
226 if (mNDF > (int32_t)param.rec.tpc.mergerNonInterpolateRejectMinNDF && (CAMath::Abs(yy - mP[0]) > threshold || CAMath::Abs(zz - mP[1]) > threshold)) {
228 } else {
229 int8_t rejectChi2 = 0;
230 if (attempt == 0) {
231 if (param.rec.tpc.mergerInterpolateErrors && CAMath::Abs(ihit - ihitMergeFirst) <= 1) {
232 if (iWay == nWays - 3) {
234 } else if (iWay == nWays - 2) {
236 } else if (iWay == nWays - 1) {
237 rejectChi2 = (param.rec.tpc.mergerInterpolateRejectAlsoOnCurrentPosition && GetNDF() > (int32_t)param.rec.tpc.mergerNonInterpolateRejectMinNDF) ? GPUTPCGMPropagator::rejectDirect : 0;
238 }
239 } else {
240 rejectChi2 = allowChangeClusters && goodRows > 5;
241 }
242 }
243
244 float err2Y, err2Z;
245 const float time = merger->GetConstantMem()->ioPtrs.clustersNative ? merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[cluster.num].getTime() : -1.f;
246 const float invSqrtCharge = merger->GetConstantMem()->ioPtrs.clustersNative ? CAMath::InvSqrt(merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[cluster.num].qMax) : 0.f;
247 const float invCharge = merger->GetConstantMem()->ioPtrs.clustersNative ? (1.f / merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[cluster.num].qMax) : 0.f;
248 float invAvgCharge = (sumInvSqrtCharge += invSqrtCharge) / ++nAvgCharge;
249 invAvgCharge *= invAvgCharge;
250 prop.GetErr2(err2Y, err2Z, param, zz, cluster.row, clusterState, cluster.sector, time, invAvgCharge, invCharge);
251
252 if (rejectChi2 >= GPUTPCGMPropagator::rejectInterFill) {
255 } else {
256 retValInt = prop.InterpolateReject(param, yy, zz, clusterState, rejectChi2, &interpolation.hit[ihit], err2Y, err2Z, deltaZ);
257 }
258 }
259
260 if (param.rec.tpc.rejectEdgeClustersInTrackFit && uncorrectedY > -1e6f && param.rejectEdgeClusterByY(uncorrectedY, cluster.row, CAMath::Sqrt(mC[0]))) { // uncorrectedY > -1e6f implies allowChangeClusters
262 } else {
263 retValUpd = prop.Update(yy, zz, cluster.row, param, clusterState, rejectChi2, refit, err2Y, err2Z);
264 }
265 GPUCA_DEBUG_STREAMER_CHECK(if (o2::utils::DebugStreamer::checkStream(o2::utils::StreamFlags::streamUpdateTrack, iTrk)) {
266 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);
267 });
268 }
269 // clang-format off
270 CADEBUG(if (!CheckCov()) GPUError("INVALID COV AFTER UPDATE!!!"));
271 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));
272 // clang-format on
273
274 ConstrainSinPhi(); // TODO: Limit using ConstrainSinPhi everywhere!
275 if (!retValUpd && !retValInt) // track is updated
276 {
277 lastUpdateX = mX;
278 covYYUpd = mC[0];
279 nMissed = nMissed2 = 0;
280 UnmarkClusters(clusters, ihitMergeFirst, ihit, wayDirection, GPUTPCGMMergedTrackHit::flagHighIncl);
281 N++;
282 ihitStart = ihit;
283 float dy = mP[0] - prop.Model().Y();
284 float dz = mP[1] - prop.Model().Z();
285 if (CAMath::Abs(mP[4]) * param.qptB5Scaler > 10 && --resetT0 <= 0 && CAMath::Abs(mP[2]) < 0.15f && dy * dy + dz * dz > 1) {
286 CADEBUG(printf("Reinit linearization\n"));
287 prop.SetTrack(this, prop.GetAlpha());
288 }
290 if (param.dodEdxEnabled && finalFit) { // TODO: Costimize flag to remove, and option to remove double-clusters
291 bool acc = (clusterState & param.rec.tpc.dEdxClusterRejectionFlagMask) == 0, accAlt = (clusterState & param.rec.tpc.dEdxClusterRejectionFlagMaskAlt) == 0;
292 if (acc || accAlt) {
293 float qtot = 0, qmax = 0, pad = 0, relTime = 0;
294 const int32_t clusterCount = (ihit - ihitMergeFirst) * wayDirection + 1;
295 for (int32_t iTmp = ihitMergeFirst; iTmp != ihit + wayDirection; iTmp += wayDirection) {
296 const ClusterNative& cl = merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[cluster.num];
297 qtot += cl.getQtot();
298 qmax = CAMath::Max<float>(qmax, cl.qMax);
299 pad += cl.getPad();
300 relTime += cl.getTime();
301 }
302 qtot /= clusterCount; // TODO: Weighted Average
303 pad /= clusterCount;
304 relTime /= clusterCount;
305 relTime = relTime - CAMath::Round(relTime);
306 if (acc) {
307 dEdx.fillCluster(qtot, qmax, cluster.row, cluster.sector, mP[2], mP[3], merger->GetConstantMem()->calibObjects, zz, pad, relTime GPUCA_DEBUG_STREAMER_CHECK(, iTrk, clusterState));
308 }
309 if GPUCA_RTC_CONSTEXPR (GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMask) != GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMaskAlt)) {
310 if (accAlt) {
311 dEdxAlt.fillCluster(qtot, qmax, cluster.row, cluster.sector, mP[2], mP[3], merger->GetConstantMem()->calibObjects, zz, pad, relTime GPUCA_DEBUG_STREAMER_CHECK(, iTrk, clusterState));
312 }
313 }
314 }
315 }
316 }
317 } else if (retValInt || retValUpd >= GPUTPCGMPropagator::updateErrorClusterRejected) { // cluster far away form the track
318 if (retValInt || allowChangeClusters) {
319 MarkClusters(clusters, ihitMergeFirst, ihit, wayDirection, GPUTPCGMMergedTrackHit::flagRejectDistance);
320 } else if (finalFit) {
321 MarkClusters(clusters, ihitMergeFirst, ihit, wayDirection, GPUTPCGMMergedTrackHit::flagRejectErr);
322 }
323 if (!retValInt) {
324 nMissed++;
325 nMissed2++;
326 }
327 } else {
328 break; // bad chi2 for the whole track, stop the fit
329 }
330 }
331 if (finalOutInFit && !(merger->Param().rec.tpc.disableRefitAttachment & 4) && lastRow != 255 && lastSector != 255) {
332 StoreLoopPropagation(merger, lastSector, lastRow, iTrk, lastRow > clusters[(iWay & 1) ? (maxN - 1) : 0].row, prop.GetAlpha());
333 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));
334 }
335 if (!(iWay & 1) && !finalFit && !track.CCE() && !track.Looper()) {
336 deltaZ = ShiftZ(clusters, merger, maxN);
337 } else {
338 deltaZ = 0.f;
339 }
340 }
341 ConstrainSinPhi();
342
343 GPUCA_DEBUG_STREAMER_CHECK(if (o2::utils::DebugStreamer::checkStream(o2::utils::StreamFlags::streamUpdateTrack, iTrk)) {
344 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";
345 })
346
347 if (!(N + NTolerated >= (int32_t)param.tpcMinHitsB5(mP[4] * param.qptB5Scaler) && 2 * NTolerated <= CAMath::Max(10, N) && CheckNumericalQuality(covYYUpd))) {
348 return false; // TODO: NTolerated should never become that large, check what is going wrong!
349 }
350 if (param.rec.tpc.minNClustersFinalTrack != -1 && N + NTolerated < param.rec.tpc.minNClustersFinalTrack) {
351 return false;
352 }
353
354 if (param.par.dodEdx && param.dodEdxEnabled) {
355 dEdx.computedEdx(merger->MergedTracksdEdx()[iTrk], param);
356 if GPUCA_RTC_CONSTEXPR (GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMask) != GPUCA_GET_CONSTEXPR(param.rec.tpc, dEdxClusterRejectionFlagMaskAlt)) {
357 dEdxAlt.computedEdx(merger->MergedTracksdEdxAlt()[iTrk], param);
358 }
359 }
360 Alpha = prop.GetAlpha();
361 MoveToReference(prop, param, Alpha);
362 NormalizeAlpha(Alpha);
363
364 return true;
365}
366
367GPUdni() void GPUTPCGMTrackParam::MoveToReference(GPUTPCGMPropagator& prop, const GPUParam& param, float& Alpha)
368{
369 constexpr float kDeg2Rad = M_PI / 180.f;
370 constexpr float kSectAngle = 2 * M_PI / 18.f;
371
372 if (param.rec.tpc.trackReferenceX <= 500) {
373 GPUTPCGMTrackParam save = *this;
374 float saveAlpha = Alpha;
375 for (int32_t attempt = 0; attempt < 3; attempt++) {
376 float dAngle = CAMath::Round(CAMath::ATan2(mP[0], mX) / kDeg2Rad / 20.f) * kSectAngle;
377 Alpha += dAngle;
378 if (prop.PropagateToXAlpha(param.rec.tpc.trackReferenceX, Alpha, 0)) {
379 break;
380 }
381 ConstrainSinPhi();
382 if (CAMath::Abs(mP[0]) <= mX * CAMath::Tan(kSectAngle / 2.f)) {
383 return;
384 }
385 }
386 *this = save;
387 Alpha = saveAlpha;
388 }
389 if (CAMath::Abs(mP[0]) > mX * CAMath::Tan(kSectAngle / 2.f)) {
390 float dAngle = CAMath::Round(CAMath::ATan2(mP[0], mX) / kDeg2Rad / 20.f) * kSectAngle;
391 Rotate(dAngle);
392 ConstrainSinPhi();
393 Alpha += dAngle;
394 }
395}
396
397GPUd() 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)
398{
399 if (mirrorParameters) {
400 prop.Mirror(inFlyDirection);
401 }
402 float err2Y, err2Z;
403 prop.GetErr2(err2Y, err2Z, param, toZ, row, clusterState, sector, -1.f, 0.f, 0.f); // Use correct time / avgCharge
404 prop.Model().Y() = mP[0] = toY;
405 prop.Model().Z() = mP[1] = toZ;
406 if (mC[0] < err2Y) {
407 mC[0] = err2Y;
408 }
409 if (mC[2] < err2Z) {
410 mC[2] = err2Z;
411 }
412 if (CAMath::Abs(mC[5]) < 0.1f) {
413 mC[5] = mC[5] > 0 ? 0.1f : -0.1f;
414 }
415 if (mC[9] < 1.f) {
416 mC[9] = 1.f;
417 }
418 mC[1] = mC[4] = mC[6] = mC[8] = mC[11] = mC[13] = 0;
419 prop.SetTrack(this, prop.GetAlpha());
420 mNDF = -3;
421 mChi2 = 0;
422}
423
424GPUd() 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)
425{
426 if (ihit + wayDirection >= 0 && ihit + wayDirection < maxN && clusters[ihit].row == clusters[ihit + wayDirection].row && clusters[ihit].sector == clusters[ihit + wayDirection].sector) {
427 float maxDistY, maxDistZ;
428 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
429 maxDistY = (maxDistY + mC[0]) * 20.f;
430 maxDistZ = (maxDistZ + mC[2]) * 20.f;
431 int32_t noReject = 0; // Cannot reject if simple estimation of y/z fails (extremely unlike case)
432 if (CAMath::Abs(clAlpha - prop.GetAlpha()) > 1.e-4f) {
433 noReject = prop.RotateToAlpha(clAlpha);
434 }
435 float projY = 0, projZ = 0;
436 if (noReject == 0) {
437 noReject |= prop.GetPropagatedYZ(xx, projY, projZ);
438 }
439 float count = 0.f;
440 xx = yy = zz = 0.f;
441 clusterState = 0;
442 while (true) {
443 const ClusterNative& GPUrestrict() cl = merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear[clusters[ihit].num];
444 float clamp = cl.getQtot();
445 float clx, cly, clz;
446 merger->GetConstantMem()->calibObjects.fastTransform->Transform(clusters[ihit].sector, clusters[ihit].row, cl.getPad(), cl.getTime(), clx, cly, clz, mTOffset);
447 float dy = cly - projY;
448 float dz = clz - projZ;
449 if (noReject == 0 && (dy * dy > maxDistY || dz * dz > maxDistZ)) {
450 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));
451 if (rejectChi2) {
453 }
454 } else {
455 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)));
456 xx += clx * clamp; // TODO: Weight in pad/time instead of XYZ
457 yy += cly * clamp;
458 zz += clz * clamp;
459 clusterState |= clusters[ihit].state;
460 count += clamp;
461 }
462 if (!(ihit + wayDirection >= 0 && ihit + wayDirection < maxN && clusters[ihit].row == clusters[ihit + wayDirection].row && clusters[ihit].sector == clusters[ihit + wayDirection].sector)) {
463 break;
464 }
465 ihit += wayDirection;
466 }
467 if (count < 0.1f) {
468 CADEBUG(printf("\t\tNo matching cluster in double-row, skipping\n"));
469 return -1;
470 }
471 xx /= count;
472 yy /= count;
473 zz /= count;
474 }
475 return 0;
476}
477
478GPUd() float GPUTPCGMTrackParam::AttachClusters(const GPUTPCGMMerger* GPUrestrict() Merger, int32_t sector, int32_t iRow, int32_t iTrack, bool goodLeg, GPUTPCGMPropagator& prop)
479{
480 float Y, Z;
481 float X = 0;
482 Merger->GetConstantMem()->calibObjects.fastTransform->InverseTransformYZtoX(sector, iRow, mP[0], mP[1], X);
483 if (prop.GetPropagatedYZ(X, Y, Z)) {
484 Y = mP[0];
485 Z = mP[1];
486 }
487 return AttachClusters(Merger, sector, iRow, iTrack, goodLeg, Y, Z);
488}
489
490GPUd() float GPUTPCGMTrackParam::AttachClusters(const GPUTPCGMMerger* GPUrestrict() Merger, int32_t sector, int32_t iRow, int32_t iTrack, bool goodLeg, float Y, float Z)
491{
492 const auto& param = Merger->Param();
493 if (param.rec.tpc.disableRefitAttachment & 1) {
494 return -1e6f;
495 }
496 const GPUTPCTracker& GPUrestrict() tracker = *(Merger->GetConstantMem()->tpcTrackers + sector);
497 const GPUTPCRow& GPUrestrict() row = tracker.Row(iRow);
498 GPUglobalref() const cahit2* hits = tracker.HitData(row);
499 GPUglobalref() const calink* firsthit = tracker.FirstHitInBin(row);
500 if (row.NHits() == 0) {
501 return -1e6f;
502 }
503
504 const float zOffset = param.par.continuousTracking ? Merger->GetConstantMem()->calibObjects.fastTransform->convVertexTimeToZOffset(sector, mTOffset, param.continuousMaxTimeBin) : 0; // TODO: do some validatiomns for the transform conv functions...
505 const float y0 = row.Grid().YMin();
506 const float stepY = row.HstepY();
507 const float z0 = row.Grid().ZMin() - zOffset; // We can use our own ZOffset, since this is only used temporarily anyway
508 const float stepZ = row.HstepZ();
509 int32_t bin, ny, nz;
510
511 float uncorrectedY, uncorrectedZ;
512 Merger->GetConstantMem()->calibObjects.fastTransform->InverseTransformYZtoNominalYZ(sector, iRow, Y, Z, uncorrectedY, uncorrectedZ);
513 if (CAMath::Abs(uncorrectedY) > row.getTPCMaxY()) {
514 return uncorrectedY;
515 }
516
517 bool protect = CAMath::Abs(GetQPt() * param.qptB5Scaler) <= param.rec.tpc.rejectQPtB5 && goodLeg;
518 float err2Y, err2Z;
519 param.GetClusterErrors2(sector, iRow, Z, mP[2], mP[3], -1.f, 0.f, 0.f, err2Y, err2Z); // TODO: Use correct time/avgCharge
520 const float tubeMaxSize2 = protect ? param.rec.tpc.tubeProtectMaxSize2 : param.rec.tpc.tubeRemoveMaxSize2;
521 const float tubeMinSize2 = protect ? param.rec.tpc.tubeProtectMinSize2 : 0.f;
522 float tubeSigma2 = protect ? param.rec.tpc.tubeProtectSigma2 : param.rec.tpc.tubeRemoveSigma2;
523 uint32_t pad = CAMath::Float2UIntRn(GPUTPCGeometry::LinearY2Pad(sector, iRow, uncorrectedY));
524 float time = Merger->GetConstantMem()->calibObjects.fastTransform->InverseTransformInTimeFrame(sector, uncorrectedZ + (param.par.continuousTracking ? Merger->GetConstantMem()->calibObjects.fastTransform->convVertexTimeToZOffset(sector, mTOffset, param.continuousMaxTimeBin) : 0), param.continuousMaxTimeBin); // TODO: Simplify this call in TPCFastTransform
525 if (iRow < param.rec.tpc.tubeExtraProtectMinRow ||
526 pad < param.rec.tpc.tubeExtraProtectEdgePads || pad >= (uint32_t)(GPUTPCGeometry::NPads(iRow) - param.rec.tpc.tubeExtraProtectEdgePads) ||
527 param.GetUnscaledMult(time) / GPUTPCGeometry::Row2X(iRow) > param.rec.tpc.tubeExtraProtectMinOccupancy) {
528 tubeSigma2 *= protect ? 2 : 0.5;
529 }
530 const float sy2 = CAMath::Max(tubeMinSize2, CAMath::Min(tubeMaxSize2, tubeSigma2 * (err2Y + CAMath::Abs(mC[0])))); // Cov can be bogus when following circle
531 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
532 const float tubeY = CAMath::Sqrt(sy2);
533 const float tubeZ = CAMath::Sqrt(sz2);
534 const float sy21 = 1.f / sy2;
535 const float sz21 = 1.f / sz2;
536
537 row.Grid().GetBinArea(uncorrectedY, uncorrectedZ + zOffset, tubeY, tubeZ, bin, ny, nz);
538 const int32_t nBinsY = row.Grid().Ny();
539 const int32_t idOffset = tracker.Data().ClusterIdOffset();
540 const int32_t* ids = &(tracker.Data().ClusterDataIndex()[row.HitNumberOffset()]);
541 uint32_t myWeight = Merger->TrackOrderAttach()[iTrack] | gputpcgmmergertypes::attachAttached | gputpcgmmergertypes::attachTube;
542 GPUAtomic(uint32_t)* const weights = Merger->ClusterAttachment();
543 if (goodLeg) {
545 }
546 if (protect) {
548 }
549
550 for (int32_t k = 0; k <= nz; k++) {
551 const int32_t mybin = bin + k * nBinsY;
552 const uint32_t hitFst = firsthit[mybin];
553 const uint32_t hitLst = firsthit[mybin + ny + 1];
554 for (uint32_t ih = hitFst; ih < hitLst; ih++) {
555 int32_t id = idOffset + ids[ih];
556 GPUAtomic(uint32_t)* const weight = weights + id;
557 if constexpr (GPUCA_PAR_NO_ATOMIC_PRECHECK == 0) {
558 if (myWeight <= *weight) {
559 continue;
560 }
561 }
562 const cahit2 hh = hits[ih];
563 const float y = y0 + hh.x * stepY;
564 const float z = z0 + hh.y * stepZ;
565 const float dy = y - uncorrectedY;
566 const float dz = z - uncorrectedZ;
567 if (dy * dy * sy21 + dz * dz * sz21 <= CAMath::Sqrt(2.f)) {
568 // CADEBUG(printf("Found Y %f Z %f\n", y, z));
569 CAMath::AtomicMax(weight, myWeight);
570 }
571 }
572 }
573 return uncorrectedY;
574}
575
576GPUd() 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)
577{
578 constexpr float kSectAngle = 2 * M_PI / 18.f;
579 if (Merger->Param().rec.tpc.disableRefitAttachment & 2) {
580 return dodEdx;
581 }
582 if (CAMath::Abs(lastRow - toRow) < 2) {
583 return dodEdx;
584 }
585 int32_t step = toRow > lastRow ? 1 : -1;
586 float xx = mX - GPUTPCGeometry::Row2X(lastRow);
587 for (int32_t iRow = lastRow + step; iRow != toRow; iRow += step) {
588 if (CAMath::Abs(mP[2]) > maxSinPhi) {
589 return dodEdx;
590 }
591 if (CAMath::Abs(mP[0]) > CAMath::Abs(mX) * CAMath::Tan(kSectAngle / 2.f)) {
592 return dodEdx;
593 }
594 int32_t err = prop.PropagateToXAlpha(xx + GPUTPCGeometry::Row2X(iRow), prop.GetAlpha(), inFlyDirection);
595 if (err) {
596 return dodEdx;
597 }
598 if (dodEdx && iRow + step == toRow) {
599 float yUncorrected, zUncorrected;
600 Merger->GetConstantMem()->calibObjects.fastTransform->InverseTransformYZtoNominalYZ(sector, iRow, mP[0], mP[1], yUncorrected, zUncorrected);
601 uint32_t pad = CAMath::Float2UIntRn(GPUTPCGeometry::LinearY2Pad(sector, iRow, yUncorrected));
602 if (pad >= GPUTPCGeometry::NPads(iRow) || (Merger->GetConstantMem()->calibObjects.dEdxCalibContainer && Merger->GetConstantMem()->calibObjects.dEdxCalibContainer->isDead(sector, iRow, pad))) {
603 dodEdx = false;
604 }
605 }
606 CADEBUG(printf("Attaching in row %d\n", iRow));
607 AttachClusters(Merger, sector, iRow, iTrack, goodLeg, prop);
608 }
609 return dodEdx;
610}
611
612GPUdii() void GPUTPCGMTrackParam::StoreOuter(gputpcgmmergertypes::GPUTPCOuterParam* outerParam, float alpha)
613{
614 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])));
615 for (int32_t i = 0; i < 5; i++) {
616 outerParam->P[i] = mP[i];
617 }
618 for (int32_t i = 0; i < 15; i++) {
619 outerParam->C[i] = mC[i];
620 }
621 outerParam->X = mX;
622 outerParam->alpha = alpha;
623}
624
625GPUdic(0, 1) void GPUTPCGMTrackParam::StoreLoopPropagation(const GPUTPCGMMerger* GPUrestrict() Merger, int32_t sector, int32_t iRow, int32_t iTrack, bool outwards, float alpha)
626{
627 if (iRow == 0 || iRow == GPUTPCGeometry::NROWS - 1) {
628 return;
629 }
630 if (CAMath::Abs(mP[2]) >= constants::MAX_SIN_PHI) { // TODO: How can we avoid this?
631 return;
632 }
633 if (CAMath::Abs(mP[2]) < 0.75) {
634 return;
635 }
636 if ((mP[2] * mP[4] < 0) ^ outwards) {
637 return;
638 }
639
640 uint32_t nLoopData = CAMath::AtomicAdd(&Merger->Memory()->nLoopData, 1u);
641 if (nLoopData >= Merger->NMaxTracks()) {
642 Merger->raiseError(GPUErrors::ERROR_MERGER_LOOPER_OVERFLOW, nLoopData, Merger->NMaxTracks());
643 CAMath::AtomicExch(&Merger->Memory()->nLoopData, Merger->NMaxTracks());
644 return;
645 }
647 data.param = *this;
648 data.track = iTrack;
649 data.alpha = alpha;
650 data.sector = sector;
651 data.row = iRow;
652 data.outwards = outwards;
653 Merger->LoopData()[nLoopData] = data;
654}
655
656GPUdii() void GPUTPCGMTrackParam::PropagateLooper(const GPUTPCGMMerger* GPUrestrict() Merger, int32_t loopIdx)
657{
659 prop.SetMaterialTPC();
660 prop.SetPolynomialField(&Merger->Param().polynomialField);
661 prop.SetMaxSinPhi(constants::MAX_SIN_PHI);
662 prop.SetMatLUT(Merger->Param().rec.useMatLUT ? Merger->GetConstantMem()->calibObjects.matLUT : nullptr);
663 prop.SetSeedingErrors(false);
664 prop.SetFitInProjections(true);
665 prop.SetPropagateBzOnly(false);
666
667 GPUTPCGMLoopData& data = Merger->LoopData()[loopIdx];
668 prop.SetTrack(&data.param, data.alpha);
669 if (false) {
670 data.param.AttachClustersLooper(Merger, data.sector, data.row, data.track, data.outwards, prop);
671 } else {
672 data.param.AttachClustersLooperFollow(Merger, prop, data.sector, data.track, data.outwards);
673 }
674}
675
676GPUdi() void GPUTPCGMTrackParam::AttachClustersLooperFollow(const GPUTPCGMMerger* GPUrestrict() Merger, GPUTPCGMPropagator& GPUrestrict() prop, int32_t sector, int32_t iTrack, bool up)
677{
678 float toX = mX;
679 bool inFlyDirection = (Merger->MergedTracks()[iTrack].Leg() & 1) ^ up;
680
681 constexpr float kSectAngle = 2 * M_PI / 18.f;
682 const GPUParam& GPUrestrict() param = Merger->Param();
683 bool right = (mP[2] < 0) ^ up;
684 const int32_t sectorSide = sector >= (int32_t)(GPUTPCGeometry::NSECTORS / 2) ? (GPUTPCGeometry::NSECTORS / 2) : 0;
685 float lrFactor = right ^ !up ? 1.f : -1.f;
686 // clang-format off
687 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));
688 // clang-format on
689
690 if (prop.RotateToAlpha(prop.GetAlpha() + (CAMath::Pi() / 2.f) * lrFactor)) {
691 return;
692 }
693 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));
694 uint32_t maxTries = 100;
695 while (true) {
696 while (CAMath::Abs(mX) <= CAMath::Abs(mP[0]) * CAMath::Tan(kSectAngle / 2.f) + 0.1f) {
697 if (maxTries-- == 0) {
698 return;
699 }
700 if (CAMath::Abs(mP[2]) > 0.7f) {
701 return;
702 }
703 if (up ? (-mP[0] * lrFactor > GPUTPCGeometry::Row2X(GPUTPCGeometry::NROWS - 1)) : (-mP[0] * lrFactor < GPUTPCGeometry::Row2X(0))) {
704 return;
705 }
706 if (!((up ? (-mP[0] * lrFactor >= toX) : (-mP[0] * lrFactor <= toX)) || (right ^ (mP[2] > 0)))) {
707 return;
708 }
709 int32_t err = prop.PropagateToXAlpha(mX + (up ? 1.f : -1.f), prop.GetAlpha(), inFlyDirection);
710 if (err) {
711 CADEBUG(printf("\t\tpropagation error (%d)\n", err));
712 return;
713 }
714 CADEBUG(printf("\tPropagated to y = %f: X %f Z %f SinPhi %f\n", mX, mP[0], mP[1], mP[2]));
715 for (uint32_t j = 0; j < GPUTPCGeometry::NROWS; j++) { // TODO: Avoid iterating over all rows
716 float rowX = GPUTPCGeometry::Row2X(j);
717 if (CAMath::Abs(rowX - (-mP[0] * lrFactor)) < 1.5f) {
718 CADEBUG(printf("\t\tAttempt row %d (X %f Y %f Z %f)\n", j, rowX, mX * lrFactor, mP[1]));
719 AttachClusters(Merger, sector, j, iTrack, false, mX * lrFactor, mP[1]);
720 }
721 }
722 }
723 if (maxTries-- == 0) {
724 return;
725 }
726 if (right) {
727 if (++sector >= sectorSide + 18) {
728 sector -= 18;
729 }
730 } else {
731 if (--sector < sectorSide) {
732 sector += 18;
733 }
734 }
735 CADEBUG(printf("\tRotating to sector %d: %f --> %f\n", sector, prop.GetAlpha(), param.Alpha(sector) + (CAMath::Pi() / 2.f) * lrFactor));
736 int32_t err = prop.RotateToAlpha(param.Alpha(sector) + (CAMath::Pi() / 2.f) * lrFactor);
737 if (err) {
738 CADEBUG(printf("Rotation Error %d\n", err));
739 return;
740 }
741 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]));
742 }
743}
744
745GPUdi() void GPUTPCGMTrackParam::AttachClustersLooper(const GPUTPCGMMerger* GPUrestrict() Merger, int32_t sector, int32_t iRow, int32_t iTrack, bool outwards, GPUTPCGMPropagator& GPUrestrict() prop)
746{
747 constexpr float kSectAngle = 2 * M_PI / 18.f;
748 // Note that the coordinate system is rotated by 90 degree swapping X and Y!
749 float X = mP[2] > 0 ? mP[0] : -mP[0];
750 float Y = mP[2] > 0 ? -mX : mX;
751 float Z = mP[1];
752 float SinPhi = CAMath::Sqrt(1 - mP[2] * mP[2]) * (mP[2] > 0 ? -1 : 1);
753 float b = prop.GetBz(prop.GetAlpha(), mX, mP[0], mP[1]);
754
755 float dx = outwards ? 1.f : -1.f;
756 const float myRowX = GPUTPCGeometry::Row2X(iRow);
757 // printf("\nAttachMirror sector %d row %d outwards %d\n", (int)sector, (int)iRow, (int)outwards);
758 // printf("X %f Y %f Z %f SinPhi %f -->\n", mX, mP[0], mP[1], mP[2]);
759 // printf("X %f Y %f Z %f SinPhi %f, dx %f\n", X, Y, Z, SinPhi, dx);
760 uint32_t maxTries = 100;
761 while (maxTries--) {
762 float ex = CAMath::Sqrt(1 - SinPhi * SinPhi);
763 float exi = 1.f / ex;
764 float dxBzQ = dx * -b * mP[4];
765 float newSinPhi = SinPhi + dxBzQ;
766 if (CAMath::Abs(newSinPhi) > constants::MAX_SIN_PHI_LOW) {
767 // printf("Abort, newSinPhi %f\n", newSinPhi);
768 return;
769 }
770 if (mP[2] > 0 ? (newSinPhi > 0.5) : (newSinPhi < -0.5)) {
771 // printf("Finished, newSinPhi %f\n", newSinPhi);
772 return;
773 }
774 float dS = dx * exi;
775 float h2 = dS * exi * exi;
776 float h4 = .5f * h2 * dxBzQ;
777
778 X += dx;
779 Y += dS * SinPhi + h4;
780 Z += dS * mP[3];
781 SinPhi = newSinPhi;
782 if (CAMath::Abs(X) > CAMath::Abs(Y) * CAMath::Tan(kSectAngle / 2.f)) {
783 // printf("Abort, sector edge\n");
784 return;
785 }
786
787 // 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);
788 float paramX = mP[2] > 0 ? -Y : Y;
789 int32_t step = outwards ? 1 : -1;
790 int32_t found = 0;
791 for (int32_t j = iRow; j >= 0 && j < (int32_t)GPUTPCGeometry::NROWS && found < 3; j += step) {
792 float rowX = mX + GPUTPCGeometry::Row2X(j) - myRowX;
793 if (CAMath::Abs(rowX - paramX) < 1.5f) {
794 // printf("Attempt row %d at y %f\n", j, X);
795 AttachClusters(Merger, sector, j, iTrack, false, mP[2] > 0 ? X : -X, Z);
796 }
797 }
798 }
799}
800
801GPUd() float GPUTPCGMTrackParam::ShiftZ(const GPUTPCGMMergedTrackHit* clusters, const GPUTPCGMMerger* merger, int32_t N)
802{
803 if (N == 0) {
804 N = 1;
805 }
806 const auto& GPUrestrict() cls = merger->GetConstantMem()->ioPtrs.clustersNative->clustersLinear;
807 float z0 = cls[clusters[0].num].getTime(), zn = cls[clusters[N - 1].num].getTime();
808 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)};
809 return ShiftZ(merger, clusters[0].sector, tmp[0], tmp[1], tmp[2]);
810}
811
812GPUd() float GPUTPCGMTrackParam::ShiftZ(const GPUTPCGMMerger* GPUrestrict() merger, uint32_t sector, float cltmax, float cltmin, float clx)
813{
814 if (!merger->Param().par.continuousTracking) {
815 return 0.f;
816 }
817 float deltaZ = 0.f;
818 bool beamlineReached = false;
819 const float r1 = CAMath::Max(0.0001f, CAMath::Abs(mP[4] * merger->Param().polynomialField.GetNominalBz()));
820 if (r1 < 0.01501) { // 100 MeV @ 0.5T ~ 0.66m cutof
821 const float dist2 = mX * mX + mP[0] * mP[0];
822 const float dist1r2 = dist2 * r1 * r1;
823 if (dist1r2 < 4) {
824 const float alpha = CAMath::ACos(1 - 0.5f * dist1r2); // Angle of a circle, such that |(cosa, sina) - (1,0)| == dist
825 const float beta = CAMath::ATan2(mP[0], mX);
826 const int32_t comp = mP[2] > CAMath::Sin(beta);
827 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
828 const float res = CAMath::Abs(sinab - mP[2]);
829
830 if (res < 0.2) {
831 const float r = 1.f / r1;
832 const float dS = alpha * r;
833 float z0 = dS * mP[3];
834 if (CAMath::Abs(z0) > GPUTPCGeometry::TPCLength()) {
835 z0 = z0 > 0 ? GPUTPCGeometry::TPCLength() : -GPUTPCGeometry::TPCLength();
836 }
837 deltaZ = mP[1] - z0;
838 beamlineReached = true;
839
840 // 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);
841 }
842 }
843 }
844
845 if (!beamlineReached) {
846 float refZ = ((sector < GPUTPCGeometry::NSECTORS / 2) ? merger->Param().rec.tpc.defaultZOffsetOverR : -merger->Param().rec.tpc.defaultZOffsetOverR) * clx;
847 float basez;
848 merger->GetConstantMem()->calibObjects.fastTransform->TransformIdealZ(sector, cltmax, basez, mTOffset);
849 deltaZ = basez - refZ;
850 }
851 {
852 float deltaT = merger->GetConstantMem()->calibObjects.fastTransform->convDeltaZtoDeltaTimeInTimeFrame(sector, deltaZ);
853 mTOffset += deltaT;
854 const float maxT = cltmin - merger->GetConstantMem()->calibObjects.fastTransform->getT0();
855 const float minT = cltmax - merger->GetConstantMem()->calibObjects.fastTransform->getMaxDriftTime(sector);
856 // printf("T Check: Clusters %f %f, min %f max %f vtx %f\n", tz1, tz2, minT, maxT, mTOffset);
857 deltaT = 0.f;
858 if (mTOffset < minT) {
859 deltaT = minT - mTOffset;
860 }
861 if (mTOffset + deltaT > maxT) {
862 deltaT = maxT - mTOffset;
863 }
864 if (deltaT != 0.f) {
865 deltaZ += merger->GetConstantMem()->calibObjects.fastTransform->convDeltaTimeToDeltaZinTimeFrame(sector, deltaT);
866 // 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);
867 mTOffset += deltaT;
868 }
869 mP[1] -= deltaZ;
870 }
871 // printf("\n");
872 return -deltaZ;
873}
874
875GPUd() bool GPUTPCGMTrackParam::CheckCov() const
876{
877 const float* c = mC;
878 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]) &&
879 (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]);
880 return ok;
881}
882
883GPUd() bool GPUTPCGMTrackParam::CheckNumericalQuality(float overrideCovYY) const
884{
885 //* Check that the track parameters and covariance matrix are reasonable
886 bool ok = CAMath::Finite(mX) && CAMath::Finite(mChi2);
887 // 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"));
888 const float* c = mC;
889 for (int32_t i = 0; i < 15; i++) {
890 ok = ok && CAMath::Finite(c[i]);
891 }
892 for (int32_t i = 0; i < 5; i++) {
893 ok = ok && CAMath::Finite(mP[i]);
894 }
895 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) {
896 ok = 0;
897 }
898 if (CAMath::Abs(mP[2]) > constants::MAX_SIN_PHI) {
899 ok = 0;
900 }
901 if (!CheckCov()) {
902 ok = false;
903 }
904 return ok;
905}
906
907GPUdii() 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.
908{
909 if (!track.OK()) {
910 return;
911 }
912
913 // clang-format off
914 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; } );
915 // clang-format on
916
917 int32_t nTrackHits = track.NClusters();
918 int32_t NTolerated = 0; // Clusters not fit but tollerated for track length cut
919 GPUTPCGMTrackParam t = track.Param();
920 float Alpha = track.Alpha();
921 CADEBUG(int32_t nTrackHitsOld = nTrackHits; float ptOld = t.QPt());
922 bool ok = t.Fit(merger, iTrk, merger->Clusters() + track.FirstClusterRef(), nTrackHits, NTolerated, Alpha, attempt, constants::MAX_SIN_PHI, track);
923 CADEBUG(printf("Finished Fit Track %d\n", iTrk));
924 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)));
925
926 if (!ok && attempt == 0 && merger->Param().rec.tpc.retryRefit) {
927 for (uint32_t i = 0; i < track.NClusters(); i++) {
928 merger->Clusters()[track.FirstClusterRef() + i].state &= GPUTPCGMMergedTrackHit::clustererAndSharedFlags;
929 }
930 CADEBUG(printf("Track rejected, marking for retry\n"));
931 if (merger->Param().rec.tpc.retryRefit == 2) {
932 nTrackHits = track.NClusters();
933 NTolerated = 0; // Clusters not fit but tollerated for track length cut
934 t = track.Param();
935 Alpha = track.Alpha();
936 ok = t.Fit(merger, iTrk, merger->Clusters() + track.FirstClusterRef(), nTrackHits, NTolerated, Alpha, 1, constants::MAX_SIN_PHI, track);
937 } else {
938 uint32_t nRefit = CAMath::AtomicAdd(&merger->Memory()->nRetryRefit, 1u);
939 merger->RetryRefitIds()[nRefit] = iTrk;
940 return;
941 }
942 }
943 if (CAMath::Abs(t.QPt()) < 1.e-4f) {
944 t.QPt() = 1.e-4f;
945 }
946
947 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()); });
948
949 track.SetOK(ok);
950 track.SetNClustersFitted(nTrackHits);
951 track.Param() = t;
952 track.Alpha() = Alpha;
953
954 // if (track.OK()) merger->DebugRefitMergedTrack(track);
955}
956
957GPUd() void GPUTPCGMTrackParam::Rotate(float alpha)
958{
959 float cA, sA;
960 CAMath::SinCos(alpha, sA, cA);
961 float x0 = mX;
962 float sinPhi0 = mP[2], cosPhi0 = CAMath::Sqrt(1 - mP[2] * mP[2]);
963 float cosPhi = cosPhi0 * cA + sinPhi0 * sA;
964 float sinPhi = -cosPhi0 * sA + sinPhi0 * cA;
965 float j0 = cosPhi0 / cosPhi;
966 float j2 = cosPhi / cosPhi0;
967 mX = x0 * cA + mP[0] * sA;
968 mP[0] = -x0 * sA + mP[0] * cA;
969 mP[2] = sinPhi;
970 mC[0] *= j0 * j0;
971 mC[1] *= j0;
972 mC[3] *= j0;
973 mC[6] *= j0;
974 mC[10] *= j0;
975
976 mC[3] *= j2;
977 mC[4] *= j2;
978 mC[5] *= j2 * j2;
979 mC[8] *= j2;
980 mC[12] *= j2;
981 if (cosPhi < 0) { // change direction ( t0 direction is already changed in t0.UpdateValues(); )
982 SinPhi() = -SinPhi();
983 DzDs() = -DzDs();
984 QPt() = -QPt();
985 mC[3] = -mC[3];
986 mC[4] = -mC[4];
987 mC[6] = -mC[6];
988 mC[7] = -mC[7];
989 mC[10] = -mC[10];
990 mC[11] = -mC[11];
991 }
992}
993
994GPUd() void GPUTPCGMTrackParam::AddCovDiagErrors(const float* GPUrestrict() errors2)
995{
996 mC[0] += errors2[0];
997 mC[2] += errors2[1];
998 mC[5] += errors2[2];
999 mC[9] += errors2[3];
1000 mC[14] += errors2[4];
1001}
1002
1003GPUd() void GPUTPCGMTrackParam::AddCovDiagErrorsWithCorrelations(const float* GPUrestrict() errors2)
1004{
1005 const int32_t diagMap[5] = {0, 2, 5, 9, 14};
1006 const float oldDiag[5] = {mC[0], mC[2], mC[5], mC[9], mC[14]};
1007 for (int32_t i = 0; i < 5; i++) {
1008 mC[diagMap[i]] += errors2[i];
1009 for (int32_t j = 0; j < i; j++) {
1010 mC[diagMap[i - 1] + j + 1] *= gpu::CAMath::Sqrt(mC[diagMap[i]] * mC[diagMap[j]] / (oldDiag[i] * oldDiag[j]));
1011 }
1012 }
1013}
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_PAR_MERGER_INTERPOLATION_ERROR_TYPE_A
#define CADEBUG(...)
Definition GPUDef.h:82
#define GPUCA_GET_CONSTEXPR(obj, val)
int32_t lastRow
configStandalone Light OpenGL display display_light Apply smoothing to points Enable Z buffer Highlight links Highlight seeds Highlight sector tracks Highlight final tracks Show individual sector
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by when above this local occupancy rowx Shift of TPC revert cluster flag edge bit distance to edge exceeds this ZOffset for secondary multiple of radius max P of electron K BB bands crossing max P of electron p BB bands crossing max P of electron d BB bands crossing max P of electron t BB bands crossing min accepted dEdxMaxTPC of the track Additive extra cluster error for Y2 if edge flag set Additive extra cluster error for Y2 if single set Additive extra cluster error for Y2 if or single set Additive extra cluster error for Z2 if or single set Extra errors to add to track when crossing depending on addErrorsCECrossing Inner portion of y range in sector that is not used in searching for extrapolated track candidates factor2YS for track merging factor2K for track merging Margin in cm of Y position when rejecting edge clusters based on uncorrected track Y Max fraction of shared clusters for tracklet Except pad from cluster finding if total number of charges in a fragment is above this Except pad from cluster finding if number of consecutive charges in a fragment is above this Threshold where a timebin is considered disabling the noisy pad check for that pad Treat signal above this minimum as saturated Smoothing factor for the exponential Highly Ionising Particle tail filter Number of timebins to use for the averaging Add additional custom track errors when crossing relative as fraction of Do N fit passes in final fit of< 0:reject at max -n hits") AddOptionRTC(rejectIFCLowRadiusCluster, uint8_t, 1, "", 0, "Reject clusters that get the IFC mask error during refit") AddOptionRTC(dEdxTruncLow, uint8_t, 2, "", 0, "Low truncation threshold, fraction of 128") AddOptionRTC(dEdxTruncHigh, uint8_t, 77, "", 0, "High truncation threshold, fraction of 128") AddOptionRTC(extrapolationTracking, int8_t, 1, "", 0, "Enable Extrapolation Tracking(prolong tracks to adjacent sectors to find short segments)") AddOptionRTC(disableRefitAttachment, uint8_t, 0, "", 0, "Bitmask to disable certain attachment steps during refit(1:attachment, 2:propagation, 4:loop following, 8:mirroring)") AddOptionRTC(rejectionStrategy, uint8_t, o2::gpu::GPUSettings::RejectionStrategyA, "", 0, "Enable rejection of TPC clusters for compression(0=no, 1=strategy A, 2=strategy B)") AddOptionRTC(mergeLoopersAfterburner, uint8_t, 1, "", 0, "Run afterburner for additional looper merging") AddOptionRTC(compressionTypeMask, uint8_t, o2::gpu::GPUSettings::CompressionFull, "", 0, "TPC Compression mode bits(1=truncate charge/width LSB, 2=differences, 4=track-model)") AddOptionRTC(compressionSortOrder, uint8_t, o2::gpu::GPUSettings::SortTime, "", 0, "Sort order of TPC compression(0=time, 1=pad, 2=Z-time-pad, 3=Z-pad-time, 4=no sorting(use incoming order))") AddOptionRTC(sigBitsCharge, uint8_t, 4, "", 0, "Number of significant bits for TPC cluster charge in compression mode 1") AddOptionRTC(sigBitsWidth, uint8_t, 3, "", 0, "Number of significant bits for TPC cluster width in compression mode 1") AddOptionRTC(dropLoopers, uint8_t, 0, "", 0, "Drop looping tracks starting from second loop") AddOptionRTC(mergerInterpolateErrors, uint8_t, 1, "", 0, "Use interpolation instead of extrapolation for chi2 based cluster rejection") AddOptionRTC(mergerInterpolateRejectAlsoOnCurrentPosition, uint8_t, 1, "", 0, "When using mergerInterpolateErrors, reject based on chi2 twice computed with interpolated and current track position starting from NDF > mergerNonInterpolateRejectMinNDF Merge tracks accross the central electrode Enable PID response Add sub threshold clusters in TPC dEdx computation OR mask of TPC flags that will reject the cluster in alternative dEdx Reject edge clusters based on uncorrected track Y during track fit Increase decrease removal by when on this number of pads from the edge h configStandalone Reconstruction rec_trd Max chi2 for TRD tracklets to be matched to a track Chi2 cut for strict matching mode Number of sigmas for ITS TPC track time error estimate Addition to search road around track prolongation along Y in cm Optimal tracklet rphi resolution in maximum chi2 per attached tracklet for TRD tracks take into account the full likelihood of the slope including exponential tails Tracks with less attached tracklets are discarded after the tracking Post trigger Pile up integration time in BCs fits all iterations but the last one in projections Use material lookup table for TPC refit Apply workarounds to avoid cyclic merge should not be needed tpc dyn configStandalone Processing proc_rtc Replace constant variables static by constexpr expressions Compile RTC in deterministic with NO_FAST_MATH flags and GPUCA_DETERMINISTIC_MODE define Use RTC to optimize GPU code h configStandalone Processing proc_rtctech Use a file lock to serialize access to the cache folder int32_t
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by when above this local occupancy rowx Shift of TPC revert cluster flag edge bit distance to edge exceeds this ZOffset for secondary multiple of radius max P of electron K BB bands crossing max P of electron p BB bands crossing max P of electron d BB bands crossing max P of electron t BB bands crossing min accepted dEdxMaxTPC of the track Additive extra cluster error for Y2 if edge flag set Additive extra cluster error for Y2 if single set Additive extra cluster error for Y2 if or single set Additive extra cluster error for Z2 if or single set Extra errors to add to track when crossing depending on addErrorsCECrossing Inner portion of y range in sector that is not used in searching for extrapolated track candidates factor2YS for track merging factor2K for track merging Margin in cm of Y position when rejecting edge clusters based on uncorrected track Y Max fraction of shared clusters for tracklet Except pad from cluster finding if total number of charges in a fragment is above this Except pad from cluster finding if number of consecutive charges in a fragment is above this Threshold where a timebin is considered disabling the noisy pad check for that pad Treat signal above this minimum as saturated Smoothing factor for the exponential Highly Ionising Particle tail filter Number of timebins to use for the averaging Add additional custom track errors when crossing relative as fraction of Do N fit passes in final fit of merger(must be odd to end with inward fit)") AddOptionRTC(trackFitRejectMode
GPUdii() void GPUTPCGMTrackParam
GPUdni() void GPUTPCGMTrackParam
#define DEBUG_SINGLE_TRACK
SurfaceTrackState state
uint32_t j
Definition RawData.h:0
uint32_t res
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
POD correction map.
double num
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
GLdouble GLdouble right
Definition glcorearb.h:4077
GLuint GLuint GLfloat weight
Definition glcorearb.h:5477
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLint y
Definition glcorearb.h:270
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
const bool const int TrackITSInternal< NLayers > & track
Global TPC definitions and constants.
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
std::string getTime(uint64_t ts)
GPUCA_PAR_MERGER_INTERPOLATION_ERROR_TYPE_A errorY
InterpolationErrorHit hit[constants::MERGER_MAX_TRACK_CLUSTERS]
std::vector< Cluster > clusters
std::vector< int > row