Skip to content

Commit 044ff3b

Browse files
sgorbunodavidrohr
authored andcommitted
TPC Splines: fix the inverse correction
1 parent 2ef961e commit 044ff3b

File tree

4 files changed

+179
-124
lines changed

4 files changed

+179
-124
lines changed

Detectors/TPC/calibration/src/TPCFastSpaceChargeCorrectionHelper.cxx

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ void TPCFastSpaceChargeCorrectionHelper::fillSpaceChargeCorrectionFromMap(TPCFas
146146
int nDataPoints = data.size();
147147
auto& info = correction.getSliceRowInfo(slice, row);
148148
info.resetMaxValues();
149-
info.resetMaxValuesInv();
150149
if (nDataPoints >= 4) {
151150
std::vector<double> pointSU(nDataPoints);
152151
std::vector<double> pointSV(nDataPoints);
@@ -160,7 +159,6 @@ void TPCFastSpaceChargeCorrectionHelper::fillSpaceChargeCorrectionFromMap(TPCFas
160159
pointCorr[3 * i + 1] = du;
161160
pointCorr[3 * i + 2] = dv;
162161
info.updateMaxValues(20. * dx, 20. * du, 20. * dv);
163-
info.updateMaxValuesInv(-20. * dx, -20. * du, -20. * dv);
164162
}
165163
helper.approximateDataPoints(spline, splineParameters, 0., spline.getGridX1().getUmax(), 0., spline.getGridX2().getUmax(), &pointSU[0],
166164
&pointSV[0], &pointCorr[0], nDataPoints);
@@ -908,46 +906,60 @@ void TPCFastSpaceChargeCorrectionHelper::initInverse(std::vector<o2::gpu::TPCFas
908906

909907
for (int slice = 0; slice < mGeo.getNumberOfSlices(); slice++) {
910908
// LOG(info) << "inverse transform for slice " << slice ;
911-
double vLength = (slice < mGeo.getNumberOfSlicesA()) ? mGeo.getTPCzLengthA() : mGeo.getTPCzLengthC();
912909

913910
auto myThread = [&](int iThread) {
914911
Spline2DHelper<float> helper;
915912
std::vector<float> splineParameters;
916-
ChebyshevFit1D chebFitterX, chebFitterU, chebFitterV;
917913

918914
for (int row = iThread; row < mGeo.getNumberOfRows(); row += mNthreads) {
919915
TPCFastSpaceChargeCorrection::SplineType spline = correction.getSpline(slice, row);
920916
helper.setSpline(spline, 10, 10);
921-
std::vector<double> dataPointCU, dataPointCV, dataPointF;
922-
923-
float u0, u1, v0, v1;
924-
mGeo.convScaledUVtoUV(slice, row, 0., 0., u0, v0);
925-
mGeo.convScaledUVtoUV(slice, row, 1., 1., u1, v1);
926917

927918
double x = mGeo.getRowInfo(row).x;
928-
int nPointsU = (spline.getGridX1().getNumberOfKnots() - 1) * 10;
929-
int nPointsV = (spline.getGridX2().getNumberOfKnots() - 1) * 10;
930-
931-
double stepU = (u1 - u0) / (nPointsU - 1);
932-
double stepV = (v1 - v0) / (nPointsV - 1);
919+
auto& sliceRowInfo = correction.getSliceRowInfo(slice, row);
933920

934-
if (prn) {
935-
LOG(info) << "u0 " << u0 << " u1 " << u1 << " v0 " << v0 << " v1 " << v1;
921+
std::vector<double> gridU;
922+
{
923+
const auto& grid = spline.getGridX1();
924+
for (int i = 0; i < grid.getNumberOfKnots(); i++) {
925+
if (i == grid.getNumberOfKnots() - 1) {
926+
gridU.push_back(grid.getKnot(i).u);
927+
break;
928+
}
929+
for (double s = 1.; s > 0.; s -= 0.1) {
930+
gridU.push_back(s * grid.getKnot(i).u + (1. - s) * grid.getKnot(i + 1).u);
931+
}
932+
}
933+
}
934+
std::vector<double> gridV;
935+
{
936+
const auto& grid = spline.getGridX2();
937+
for (int i = 0; i < grid.getNumberOfKnots(); i++) {
938+
if (i == grid.getNumberOfKnots() - 1) {
939+
gridV.push_back(grid.getKnot(i).u);
940+
break;
941+
}
942+
for (double s = 1.; s > 0.; s -= 0.1) {
943+
gridV.push_back(s * grid.getKnot(i).u + (1. - s) * grid.getKnot(i + 1).u);
944+
}
945+
}
936946
}
937-
TPCFastSpaceChargeCorrection::RowActiveArea& area = correction.getSliceRowInfo(slice, row).activeArea;
947+
948+
std::vector<double> dataPointCU, dataPointCV, dataPointF;
949+
dataPointCU.reserve(gridU.size() * gridV.size());
950+
dataPointCV.reserve(gridU.size() * gridV.size());
951+
dataPointF.reserve(gridU.size() * gridV.size());
952+
953+
TPCFastSpaceChargeCorrection::RowActiveArea& area = sliceRowInfo.activeArea;
938954
area.cuMin = 1.e10;
939955
area.cuMax = -1.e10;
956+
double cvMin = 1.e10;
940957

941-
/*
942-
v1 = area.vMax;
943-
stepV = (v1 - v0) / (nPointsU - 1);
944-
if (stepV < 1.f) {
945-
stepV = 1.f;
946-
}
947-
*/
958+
for (int iu = 0; iu < gridU.size(); iu++) {
959+
for (int iv = 0; iv < gridV.size(); iv++) {
960+
float u, v;
961+
correction.convGridToUV(slice, row, gridU[iu], gridV[iv], u, v);
948962

949-
for (double u = u0; u < u1 + stepU; u += stepU) {
950-
for (double v = v0; v < v1 + stepV; v += stepV) {
951963
float dx, du, dv;
952964
correction.getCorrection(slice, row, u, v, dx, du, dv);
953965
dx *= scaling[0];
@@ -976,39 +988,41 @@ void TPCFastSpaceChargeCorrectionHelper::initInverse(std::vector<o2::gpu::TPCFas
976988
dataPointF.push_back(dx);
977989
dataPointF.push_back(du);
978990
dataPointF.push_back(dv);
979-
980-
if (prn) {
981-
LOG(info) << "measurement cu " << cu << " cv " << cv << " dx " << dx << " du " << du << " dv " << dv;
982-
}
983-
} // v
984-
} // u
991+
}
992+
}
985993

986994
if (area.cuMax - area.cuMin < 0.2) {
987995
area.cuMax = .1;
988996
area.cuMin = -.1;
989997
}
990-
if (area.cvMax < 0.1) {
998+
if (area.cvMax - cvMin < 0.2) {
991999
area.cvMax = .1;
1000+
cvMin = -.1;
9921001
}
1002+
9931003
if (prn) {
9941004
LOG(info) << "slice " << slice << " row " << row << " max drift L = " << correction.getMaxDriftLength(slice, row)
9951005
<< " active area: cuMin " << area.cuMin << " cuMax " << area.cuMax << " vMax " << area.vMax << " cvMax " << area.cvMax;
9961006
}
9971007

998-
TPCFastSpaceChargeCorrection::SliceRowInfo& info = correction.getSliceRowInfo(slice, row);
999-
info.gridCorrU0 = area.cuMin;
1000-
info.scaleCorrUtoGrid = spline.getGridX1().getUmax() / (area.cuMax - area.cuMin);
1001-
info.scaleCorrVtoGrid = spline.getGridX2().getUmax() / area.cvMax;
1008+
// define the grid for the inverse correction
10021009

1003-
info.gridCorrU0 = u0;
1004-
info.gridCorrV0 = info.gridV0;
1005-
info.scaleCorrUtoGrid = spline.getGridX1().getUmax() / (u1 - info.gridCorrU0);
1006-
info.scaleCorrVtoGrid = spline.getGridX2().getUmax() / (v1 - info.gridCorrV0);
1010+
sliceRowInfo.gridCorrU0 = area.cuMin;
1011+
sliceRowInfo.gridCorrV0 = cvMin;
1012+
sliceRowInfo.scaleCorrUtoGrid = spline.getGridX1().getUmax() / (area.cuMax - area.cuMin);
1013+
sliceRowInfo.scaleCorrVtoGrid = spline.getGridX2().getUmax() / area.cvMax;
1014+
1015+
/*
1016+
sliceRowInfo.gridCorrU0 = sliceRowInfo.gridU0;
1017+
sliceRowInfo.gridCorrV0 = sliceRowInfo.gridV0;
1018+
sliceRowInfo.scaleCorrUtoGrid = sliceRowInfo.scaleUtoGrid;
1019+
sliceRowInfo.scaleCorrVtoGrid = sliceRowInfo.scaleVtoGrid;
1020+
*/
10071021

10081022
int nDataPoints = dataPointCU.size();
10091023
for (int i = 0; i < nDataPoints; i++) {
1010-
dataPointCU[i] = (dataPointCU[i] - info.gridCorrU0) * info.scaleCorrUtoGrid;
1011-
dataPointCV[i] = (dataPointCV[i] - info.gridCorrV0) * info.scaleCorrVtoGrid;
1024+
dataPointCU[i] = (dataPointCU[i] - sliceRowInfo.gridCorrU0) * sliceRowInfo.scaleCorrUtoGrid;
1025+
dataPointCV[i] = (dataPointCV[i] - sliceRowInfo.gridCorrV0) * sliceRowInfo.scaleCorrVtoGrid;
10121026
}
10131027

10141028
splineParameters.resize(spline.getNumberOfParameters());

GPU/TPCFastTransformation/TPCFastSpaceChargeCorrection.cxx

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#if !defined(GPUCA_GPUCODE)
2121
#include <iostream>
22+
#include <string>
2223
#include <cmath>
2324
#include "Spline2DHelper.h"
2425
#endif
@@ -514,25 +515,54 @@ double TPCFastSpaceChargeCorrection::testInverse(bool prn)
514515
tpcR2max = tpcR2max / cos(2 * M_PI / mGeo.getNumberOfSlicesA() / 2) + 1.;
515516
tpcR2max = tpcR2max * tpcR2max;
516517

517-
double maxDtpc[3] = {0, 0, 0};
518-
double maxD = 0;
518+
struct MaxValue {
519+
double V{0.};
520+
int Roc{-1};
521+
int Row{-1};
522+
523+
void update(double v, int roc, int row)
524+
{
525+
if (fabs(v) > fabs(V)) {
526+
V = v;
527+
Roc = roc;
528+
Row = row;
529+
}
530+
}
531+
void update(const MaxValue& other)
532+
{
533+
update(other.V, other.Roc, other.Row);
534+
}
535+
536+
std::string toString()
537+
{
538+
std::stringstream ss;
539+
ss << V << "(" << Roc << "," << Row << ")";
540+
return ss.str();
541+
}
542+
};
543+
544+
MaxValue maxDtpc[3];
545+
MaxValue maxD;
519546

520547
for (int32_t slice = 0; slice < mGeo.getNumberOfSlices(); slice++) {
521548
if (prn) {
522549
LOG(info) << "check inverse transform for slice " << slice;
523550
}
524-
double vLength = (slice < mGeo.getNumberOfSlicesA()) ? mGeo.getTPCzLengthA() : mGeo.getTPCzLengthC();
525-
double maxDslice[3] = {0, 0, 0};
551+
double vLength = mGeo.getTPCzLength(slice);
552+
MaxValue maxDslice[3];
526553
for (int32_t row = 0; row < mGeo.getNumberOfRows(); row++) {
527554
float u0, u1, v0, v1;
528555
mGeo.convScaledUVtoUV(slice, row, 0., 0., u0, v0);
529556
mGeo.convScaledUVtoUV(slice, row, 1., 1., u1, v1);
530557
double x = mGeo.getRowInfo(row).x;
531558
double stepU = (u1 - u0) / 100.;
532559
double stepV = (v1 - v0) / 100.;
533-
double maxDrow[3] = {0, 0, 0};
560+
MaxValue maxDrow[3];
534561
for (double u = u0; u < u1; u += stepU) {
535562
for (double v = v0; v < v1; v += stepV) {
563+
if (v < getSliceRowInfo(slice, row).gridV0) {
564+
continue;
565+
}
536566
float dx, du, dv;
537567
getCorrection(slice, row, u, v, dx, du, dv);
538568
double cx = x + dx;
@@ -545,11 +575,9 @@ double TPCFastSpaceChargeCorrection::testInverse(bool prn)
545575
float nx, nu, nv;
546576
getCorrectionInvCorrectedX(slice, row, cu, cv, nx);
547577
getCorrectionInvUV(slice, row, cu, cv, nu, nv);
548-
double d[3] = {nx - cx, nu - u, nv - v};
578+
double d[3] = {(cx - nx) - dx, (cu - nu) - du, (cv - nv) - dv};
549579
for (int32_t i = 0; i < 3; i++) {
550-
if (fabs(d[i]) > fabs(maxDrow[i])) {
551-
maxDrow[i] = d[i];
552-
}
580+
maxDrow[i].update(d[i], slice, row);
553581
}
554582

555583
if (0 && prn && fabs(d[0]) + fabs(d[1]) + fabs(d[2]) > 0.1) {
@@ -560,32 +588,26 @@ double TPCFastSpaceChargeCorrection::testInverse(bool prn)
560588
}
561589
}
562590
}
563-
if (0 && prn) {
591+
if (1 && prn) {
564592
LOG(info) << "slice " << slice << " row " << row
565-
<< " dx " << maxDrow[0] << " du " << maxDrow[1] << " dv " << maxDrow[2];
593+
<< " dx " << maxDrow[0].V << " du " << maxDrow[1].V << " dv " << maxDrow[2].V;
566594
}
567595
for (int32_t i = 0; i < 3; i++) {
568-
if (fabs(maxDslice[i]) < fabs(maxDrow[i])) {
569-
maxDslice[i] = maxDrow[i];
570-
}
571-
if (fabs(maxDtpc[i]) < fabs(maxDrow[i])) {
572-
maxDtpc[i] = maxDrow[i];
573-
}
574-
if (fabs(maxD) < fabs(maxDrow[i])) {
575-
maxD = maxDrow[i];
576-
}
596+
maxDslice[i].update(maxDrow[i]);
597+
maxDtpc[i].update(maxDrow[i]);
598+
maxD.update(maxDrow[i]);
577599
}
578600
}
579601
if (prn) {
580-
LOG(info) << "inverse correction: slice " << slice
581-
<< " dx " << maxDslice[0] << " du " << maxDslice[1] << " dv " << maxDslice[2];
602+
LOG(info) << "inverse correction: slice " << slice << ". Max deviations: "
603+
<< " dx " << maxDslice[0].toString() << " du " << maxDslice[1].toString() << " dv " << maxDslice[2].toString();
582604
}
583605
} // slice
584606

585607
LOG(info) << "Test inverse TPC correction. max deviations: "
586-
<< " dx " << maxDtpc[0] << " du " << maxDtpc[1] << " dv " << maxDtpc[2] << " cm";
608+
<< " dx " << maxDtpc[0].toString() << " du " << maxDtpc[1].toString() << " dv " << maxDtpc[2].toString() << " cm";
587609

588-
return maxD;
610+
return maxD.V;
589611
}
590612

591613
#endif // GPUCA_GPUCODE

GPU/TPCFastTransformation/TPCFastSpaceChargeCorrection.h

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ class TPCFastSpaceChargeCorrection : public FlatObject
6868
float scaleCorrVtoGrid{0.f}; ///< scale corrected V to V-grid coordinate
6969
float maxCorr[3]{10.f, 10.f, 10.f}; ///< max correction for dX, dU, dV
7070
float minCorr[3]{-10.f, -10.f, -10.f}; ///< min correction for dX, dU, dV
71-
float maxInvCorr[3]{10.f, 10.f, 10.f}; ///< max inverse correction for dX, dU, dV
72-
float minInvCorr[3]{-10.f, -10.f, -10.f}; ///< min inverse correction for dX, dU, dV
7371
RowActiveArea activeArea;
7472

7573
void resetMaxValues()
@@ -94,28 +92,6 @@ class TPCFastSpaceChargeCorrection : public FlatObject
9492
minCorr[2] = GPUCommonMath::Min(minCorr[2], dv);
9593
}
9694

97-
void resetMaxValuesInv()
98-
{
99-
maxInvCorr[0] = 1.f;
100-
minInvCorr[0] = -1.f;
101-
maxInvCorr[1] = 1.f;
102-
minInvCorr[1] = -1.f;
103-
maxInvCorr[2] = 1.f;
104-
minInvCorr[2] = -1.f;
105-
}
106-
107-
void updateMaxValuesInv(float dx, float du, float dv)
108-
{
109-
maxInvCorr[0] = GPUCommonMath::Max(maxInvCorr[0], dx);
110-
minInvCorr[0] = GPUCommonMath::Min(minInvCorr[0], dx);
111-
112-
maxInvCorr[1] = GPUCommonMath::Max(maxInvCorr[1], du);
113-
minInvCorr[1] = GPUCommonMath::Min(minInvCorr[1], du);
114-
115-
maxInvCorr[2] = GPUCommonMath::Max(maxInvCorr[2], dv);
116-
minInvCorr[2] = GPUCommonMath::Min(minInvCorr[2], dv);
117-
}
118-
11995
ClassDefNV(SliceRowInfo, 2);
12096
};
12197

@@ -494,7 +470,15 @@ GPUdi() void TPCFastSpaceChargeCorrection::getCorrectionInvCorrectedX(
494470
float dx = 0;
495471
spline.interpolateU(splineData, gridU, gridV, &dx);
496472
const auto& info = getSliceRowInfo(slice, row);
497-
dx = GPUCommonMath::Max(info.minInvCorr[0], GPUCommonMath::Min(info.maxInvCorr[0], dx));
473+
474+
float s = corrV / info.gridCorrV0;
475+
if (s < 0.) {
476+
s = 0.;
477+
}
478+
if (s > 1.) {
479+
s = 1.;
480+
}
481+
dx = GPUCommonMath::Clamp(s * dx, info.minCorr[0], info.maxCorr[0]);
498482
x = mGeo.getRowInfo(row).x + dx;
499483
}
500484

@@ -510,8 +494,15 @@ GPUdi() void TPCFastSpaceChargeCorrection::getCorrectionInvUV(
510494
float duv[2];
511495
spline.interpolateU(splineData, gridU, gridV, duv);
512496
const auto& info = getSliceRowInfo(slice, row);
513-
duv[0] = GPUCommonMath::Max(info.minInvCorr[1], GPUCommonMath::Min(info.maxInvCorr[1], duv[0]));
514-
duv[1] = GPUCommonMath::Max(info.minInvCorr[2], GPUCommonMath::Min(info.maxInvCorr[2], duv[1]));
497+
float s = corrV / info.gridCorrV0;
498+
if (s < 0.) {
499+
s = 0.;
500+
}
501+
if (s > 1.) {
502+
s = 1.;
503+
}
504+
duv[0] = GPUCommonMath::Clamp(s * duv[0], info.minCorr[1], info.maxCorr[1]);
505+
duv[1] = GPUCommonMath::Clamp(s * duv[1], info.minCorr[2], info.maxCorr[2]);
515506
nomU = corrU - duv[0];
516507
nomV = corrV - duv[1];
517508
}

0 commit comments

Comments
 (0)