Skip to content

Commit d9895b7

Browse files
matthias-kleineralcaliva
authored andcommitted
TPC: improving simulation of distortions in MC (#13345)
* TPC: improving simulation of distortions in MC - adding option to recalculate (merging) the distortions in case of large scaling - accessing the distortions of the derivative map at the distorted position for better consistency with corrections * TPC: Renaming variables (cherry picked from commit 91dd4df)
1 parent 60325f4 commit d9895b7

File tree

5 files changed

+144
-15
lines changed

5 files changed

+144
-15
lines changed

Detectors/TPC/simulation/include/TPCSimulation/Digitizer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ class Digitizer
138138
void setMeanLumiDistortions(float meanLumi);
139139
void setMeanLumiDistortionsDerivative(float meanLumi);
140140

141+
/// in case of scaled distortions, the distortions can be recalculated to ensure consistent distortions and corrections
142+
void recalculateDistortions();
143+
141144
private:
142145
DigitContainer mDigitContainer; ///< Container for the Digits
143146
std::unique_ptr<SC> mSpaceCharge; ///< Handler of full distortions (static + IR dependant)
@@ -151,7 +154,8 @@ class Digitizer
151154
bool mUseSCDistortions = false; ///< Flag to switch on the use of space-charge distortions
152155
int mDistortionScaleType = 0; ///< type=0: no scaling of distortions, type=1 distortions without any scaling, type=2 distortions scaling with lumi
153156
float mLumiScaleFactor = 0; ///< value used to scale the derivative map
154-
ClassDefNV(Digitizer, 2);
157+
bool mUseScaledDistortions = false; ///< whether the distortions are already scaled
158+
ClassDefNV(Digitizer, 3);
155159
};
156160
} // namespace tpc
157161
} // namespace o2

Detectors/TPC/simulation/src/Digitizer.cxx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void Digitizer::process(const std::vector<o2::tpc::HitGroup>& hits,
8383
if (mDistortionScaleType == 1) {
8484
mSpaceCharge->distortElectron(posEle);
8585
} else if (mDistortionScaleType == 2) {
86-
mSpaceCharge->distortElectron(posEle, mSpaceChargeDer.get(), mLumiScaleFactor);
86+
mSpaceCharge->distortElectron(posEle, (mUseScaledDistortions ? nullptr : mSpaceChargeDer.get()), mLumiScaleFactor);
8787
}
8888

8989
/// Remove electrons that end up more than three sigma of the hit's average diffusion away from the current sector
@@ -237,3 +237,33 @@ void Digitizer::setMeanLumiDistortionsDerivative(float meanLumi)
237237
{
238238
mSpaceChargeDer->setMeanLumi(meanLumi);
239239
}
240+
241+
void Digitizer::recalculateDistortions()
242+
{
243+
if (!mSpaceCharge || !mSpaceChargeDer) {
244+
LOGP(info, "Average or derivative distortions not set");
245+
return;
246+
}
247+
248+
// recalculate distortions only in case the inst lumi differs from the avg lumi
249+
if (mSpaceCharge->getMeanLumi() != CorrMapParam::Instance().lumiInst) {
250+
for (int iside = 0; iside < 2; ++iside) {
251+
const o2::tpc::Side side = (iside == 0) ? Side::A : Side::C;
252+
// this needs to be done only once
253+
LOGP(info, "Calculating corrections for average distortions");
254+
mSpaceCharge->calcGlobalCorrWithGlobalDistIterative(side, nullptr, 0);
255+
256+
LOGP(info, "Calculating corrections for derivative distortions");
257+
mSpaceChargeDer->calcGlobalCorrWithGlobalDistIterative(side, nullptr, 0);
258+
259+
LOGP(info, "Calculating scaled distortions with scaling factor {}", mLumiScaleFactor);
260+
mSpaceCharge->calcGlobalDistWithGlobalCorrIterative(side, mSpaceChargeDer.get(), mLumiScaleFactor);
261+
}
262+
// set new lumi of avg map
263+
mSpaceCharge->setMeanLumi(CorrMapParam::Instance().lumiInst);
264+
} else {
265+
LOGP(info, "Inst. lumi {} is same as mean lumi {}. Skip recalculation of distortions", CorrMapParam::Instance().lumiInst, mSpaceCharge->getMeanLumi());
266+
}
267+
268+
mUseScaledDistortions = true;
269+
}

Detectors/TPC/spacecharge/include/TPCSpaceCharge/SpaceCharge.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,20 @@ class SpaceCharge
354354
/// \param approachR when the difference between the desired r coordinate and the position of the global correction is deltaR, approach the desired r coordinate by deltaR * \p approachR.
355355
/// \param approachPhi when the difference between the desired phi coordinate and the position of the global correction is deltaPhi, approach the desired phi coordinate by deltaPhi * \p approachPhi.
356356
/// \param diffCorr if the absolute differences from the interpolated values for the global corrections from the last iteration compared to the current iteration is smaller than this value, set converged to true for current global distortion
357-
void calcGlobalDistWithGlobalCorrIterative(const DistCorrInterpolator<DataT>& globCorr, const int maxIter = 100, const DataT approachZ = 0.5, const DataT approachR = 0.5, const DataT approachPhi = 0.5, const DataT diffCorr = 1e-6);
357+
/// \param type whether to calculate distortions or corrections
358+
void calcGlobalDistWithGlobalCorrIterative(const DistCorrInterpolator<DataT>& globCorr, const int maxIter = 100, const DataT approachZ = 1, const DataT approachR = 1, const DataT approachPhi = 1, const DataT diffCorr = 50e-6, const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0);
359+
360+
/// step 5: calculate global distortions using the global corrections (FAST)
361+
/// \param scSCale possible second sc object
362+
/// \param scale scaling for second sc object
363+
void calcGlobalDistWithGlobalCorrIterative(const Side side, const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachZ = 1, const DataT approachR = 1, const DataT approachPhi = 1, const DataT diffCorr = 50e-6);
364+
void calcGlobalDistWithGlobalCorrIterative(const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachZ = 1, const DataT approachR = 1, const DataT approachPhi = 1, const DataT diffCorr = 50e-6);
365+
366+
/// calculate global corrections from global distortions
367+
/// \param scSCale possible second sc object
368+
/// \param scale scaling for second sc object
369+
void calcGlobalCorrWithGlobalDistIterative(const Side side, const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachZ = 1, const DataT approachR = 1, const DataT approachPhi = 1, const DataT diffCorr = 50e-6);
370+
void calcGlobalCorrWithGlobalDistIterative(const SpaceCharge<DataT>* scSCale = nullptr, float scale = 0, const int maxIter = 100, const DataT approachZ = 1, const DataT approachR = 1, const DataT approachPhi = 1, const DataT diffCorr = 50e-6);
358371

359372
/// \return returns number of vertices in z direction
360373
unsigned short getNZVertices() const { return mParamGrid.NZVertices; }
@@ -1359,6 +1372,8 @@ class SpaceCharge
13591372
/// set potentialsdue to ROD misalignment
13601373
void initRodAlignmentVoltages(const MisalignmentType misalignmentType, const FCType fcType, const int sector, const Side side, const float deltaPot);
13611374

1375+
void calcGlobalDistCorrIterative(const DistCorrInterpolator<DataT>& globCorr, const int maxIter, const DataT approachZ, const DataT approachR, const DataT approachPhi, const DataT diffCorr, const SpaceCharge<DataT>* scSCale, float scale, const Type type);
1376+
13621377
ClassDefNV(SpaceCharge, 6);
13631378
};
13641379

Detectors/TPC/spacecharge/src/SpaceCharge.cxx

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -697,12 +697,59 @@ void SpaceCharge<DataT>::calcEField(const Side side)
697697
}
698698

699699
template <typename DataT>
700-
void SpaceCharge<DataT>::calcGlobalDistWithGlobalCorrIterative(const DistCorrInterpolator<DataT>& globCorr, const int maxIter, const DataT approachZ, const DataT approachR, const DataT approachPhi, const DataT diffCorr)
700+
void SpaceCharge<DataT>::calcGlobalDistWithGlobalCorrIterative(const DistCorrInterpolator<DataT>& globCorr, const int maxIter, const DataT approachZ, const DataT approachR, const DataT approachPhi, const DataT diffCorr, const SpaceCharge<DataT>* scSCale, float scale)
701+
{
702+
calcGlobalDistCorrIterative(globCorr, maxIter, approachZ, approachR, approachPhi, diffCorr, scSCale, scale, Type::Distortions);
703+
}
704+
705+
template <typename DataT>
706+
void SpaceCharge<DataT>::calcGlobalDistWithGlobalCorrIterative(const Side side, const SpaceCharge<DataT>* scSCale, float scale, const int maxIter, const DataT approachZ, const DataT approachR, const DataT approachPhi, const DataT diffCorr)
707+
{
708+
calcGlobalDistCorrIterative(getGlobalCorrInterpolator(side), maxIter, approachZ, approachR, approachPhi, diffCorr, scSCale, scale, Type::Distortions);
709+
}
710+
711+
template <typename DataT>
712+
void SpaceCharge<DataT>::calcGlobalDistWithGlobalCorrIterative(const SpaceCharge<DataT>* scSCale, float scale, const int maxIter, const DataT approachZ, const DataT approachR, const DataT approachPhi, const DataT diffCorr)
713+
{
714+
#pragma omp parallel for num_threads(sNThreads)
715+
for (int iside = 0; iside < FNSIDES; ++iside) {
716+
const o2::tpc::Side side = (iside == 0) ? Side::A : Side::C;
717+
calcGlobalDistWithGlobalCorrIterative(side, scSCale, scale, maxIter, approachZ, approachR, approachPhi, diffCorr);
718+
}
719+
}
720+
721+
template <typename DataT>
722+
void SpaceCharge<DataT>::calcGlobalCorrWithGlobalDistIterative(const Side side, const SpaceCharge<DataT>* scSCale, float scale, const int maxIter, const DataT approachZ, const DataT approachR, const DataT approachPhi, const DataT diffCorr)
723+
{
724+
calcGlobalDistCorrIterative(getGlobalDistInterpolator(side), maxIter, approachZ, approachR, approachPhi, diffCorr, scSCale, scale, Type::Corrections);
725+
}
726+
727+
template <typename DataT>
728+
void SpaceCharge<DataT>::calcGlobalCorrWithGlobalDistIterative(const SpaceCharge<DataT>* scSCale, float scale, const int maxIter, const DataT approachZ, const DataT approachR, const DataT approachPhi, const DataT diffCorr)
729+
{
730+
#pragma omp parallel for num_threads(sNThreads)
731+
for (int iside = 0; iside < FNSIDES; ++iside) {
732+
const o2::tpc::Side side = (iside == 0) ? Side::A : Side::C;
733+
calcGlobalCorrWithGlobalDistIterative(side, scSCale, scale, maxIter, approachZ, approachR, approachPhi, diffCorr);
734+
}
735+
}
736+
737+
template <typename DataT>
738+
void SpaceCharge<DataT>::calcGlobalDistCorrIterative(const DistCorrInterpolator<DataT>& globCorr, const int maxIter, const DataT approachZ, const DataT approachR, const DataT approachPhi, const DataT diffCorr, const SpaceCharge<DataT>* scSCale, float scale, const Type type)
701739
{
702740
const Side side = globCorr.getSide();
703-
initContainer(mGlobalDistdR[side], true);
704-
initContainer(mGlobalDistdZ[side], true);
705-
initContainer(mGlobalDistdRPhi[side], true);
741+
if (type == Type::Distortions) {
742+
initContainer(mGlobalDistdR[side], true);
743+
initContainer(mGlobalDistdZ[side], true);
744+
initContainer(mGlobalDistdRPhi[side], true);
745+
} else {
746+
initContainer(mGlobalCorrdR[side], true);
747+
initContainer(mGlobalCorrdZ[side], true);
748+
initContainer(mGlobalCorrdRPhi[side], true);
749+
}
750+
751+
const auto& scSCaleInterpolator = (type == Type::Distortions) ? scSCale->mInterpolatorGlobalCorr[side] : scSCale->mInterpolatorGlobalDist[side];
752+
706753
#pragma omp parallel for num_threads(sNThreads)
707754
for (unsigned int iPhi = 0; iPhi < mParamGrid.NPhiVertices; ++iPhi) {
708755
const DataT phi = getPhiVertex(iPhi, side);
@@ -754,13 +801,25 @@ void SpaceCharge<DataT>::calcGlobalDistWithGlobalCorrIterative(const DistCorrInt
754801

755802
// interpolate global correction at new point and calculate position of global correction
756803
corrdR = globCorr.evaldR(zCurrPos, rCurrPos, phiCurrPos);
804+
if (scSCale && scale != 0) {
805+
corrdR += scale * scSCaleInterpolator.evaldR(zCurrPos, rCurrPos, phiCurrPos);
806+
}
757807
const DataT rNewPos = rCurrPos + corrdR;
758808

759-
const DataT corrPhi = globCorr.evaldRPhi(zCurrPos, rCurrPos, phiCurrPos) / rCurrPos;
809+
DataT corrPhi = 0;
810+
if (scSCale && scale != 0) {
811+
corrPhi = scale * scSCaleInterpolator.evaldRPhi(zCurrPos, rCurrPos, phiCurrPos);
812+
}
813+
corrPhi += globCorr.evaldRPhi(zCurrPos, rCurrPos, phiCurrPos);
814+
corrPhi /= rCurrPos;
815+
760816
corrdRPhi = corrPhi * rNewPos; // normalize to new r coordinate
761817
const DataT phiNewPos = phiCurrPos + corrPhi;
762818

763819
corrdZ = globCorr.evaldZ(zCurrPos, rCurrPos, phiCurrPos);
820+
if (scSCale && scale != 0) {
821+
corrdZ += scale * scSCaleInterpolator.evaldZ(zCurrPos, rCurrPos, phiCurrPos);
822+
}
764823
const DataT zNewPos = zCurrPos + corrdZ;
765824

766825
// approach desired coordinate
@@ -783,15 +842,27 @@ void SpaceCharge<DataT>::calcGlobalDistWithGlobalCorrIterative(const DistCorrInt
783842
lastCorrdRPhi = corrdRPhi;
784843
}
785844
// set global distortions if algorithm converged or iterations exceed max numbers of iterations
786-
mGlobalDistdR[side](iZ, iR, iPhi) = -corrdR;
787-
mGlobalDistdRPhi[side](iZ, iR, iPhi) = -corrdRPhi;
788-
mGlobalDistdZ[side](iZ, iR, iPhi) = -corrdZ;
845+
if (type == Type::Distortions) {
846+
mGlobalDistdR[side](iZ, iR, iPhi) = -corrdR;
847+
mGlobalDistdRPhi[side](iZ, iR, iPhi) = -corrdRPhi;
848+
mGlobalDistdZ[side](iZ, iR, iPhi) = -corrdZ;
849+
} else {
850+
mGlobalCorrdR[side](iZ, iR, iPhi) = -corrdR;
851+
mGlobalCorrdRPhi[side](iZ, iR, iPhi) = -corrdRPhi;
852+
mGlobalCorrdZ[side](iZ, iR, iPhi) = -corrdZ;
853+
}
789854
}
790855
}
791856
for (unsigned int iR = 0; iR < mParamGrid.NRVertices; ++iR) {
792-
mGlobalDistdR[side](0, iR, iPhi) = 3 * (mGlobalDistdR[side](1, iR, iPhi) - mGlobalDistdR[side](2, iR, iPhi)) + mGlobalDistdR[side](3, iR, iPhi);
793-
mGlobalDistdRPhi[side](0, iR, iPhi) = 3 * (mGlobalDistdRPhi[side](1, iR, iPhi) - mGlobalDistdRPhi[side](2, iR, iPhi)) + mGlobalDistdRPhi[side](3, iR, iPhi);
794-
mGlobalDistdZ[side](0, iR, iPhi) = 3 * (mGlobalDistdZ[side](1, iR, iPhi) - mGlobalDistdZ[side](2, iR, iPhi)) + mGlobalDistdZ[side](3, iR, iPhi);
857+
if (type == Type::Distortions) {
858+
mGlobalDistdR[side](0, iR, iPhi) = 3 * (mGlobalDistdR[side](1, iR, iPhi) - mGlobalDistdR[side](2, iR, iPhi)) + mGlobalDistdR[side](3, iR, iPhi);
859+
mGlobalDistdRPhi[side](0, iR, iPhi) = 3 * (mGlobalDistdRPhi[side](1, iR, iPhi) - mGlobalDistdRPhi[side](2, iR, iPhi)) + mGlobalDistdRPhi[side](3, iR, iPhi);
860+
mGlobalDistdZ[side](0, iR, iPhi) = 3 * (mGlobalDistdZ[side](1, iR, iPhi) - mGlobalDistdZ[side](2, iR, iPhi)) + mGlobalDistdZ[side](3, iR, iPhi);
861+
} else {
862+
mGlobalCorrdR[side](0, iR, iPhi) = 3 * (mGlobalCorrdR[side](1, iR, iPhi) - mGlobalCorrdR[side](2, iR, iPhi)) + mGlobalCorrdR[side](3, iR, iPhi);
863+
mGlobalCorrdRPhi[side](0, iR, iPhi) = 3 * (mGlobalCorrdRPhi[side](1, iR, iPhi) - mGlobalCorrdRPhi[side](2, iR, iPhi)) + mGlobalCorrdRPhi[side](3, iR, iPhi);
864+
mGlobalCorrdZ[side](0, iR, iPhi) = 3 * (mGlobalCorrdZ[side](1, iR, iPhi) - mGlobalCorrdZ[side](2, iR, iPhi)) + mGlobalCorrdZ[side](3, iR, iPhi);
865+
}
795866
}
796867
}
797868
}
@@ -1535,7 +1606,7 @@ void SpaceCharge<DataT>::distortElectron(GlobalPosition3D& point, const SpaceCha
15351606

15361607
// scale distortions if requested
15371608
if (scSCale && scale != 0) {
1538-
scSCale->getDistortions(point.X(), point.Y(), point.Z(), side, distXTmp, distYTmp, distZTmp);
1609+
scSCale->getDistortions(point.X() + distX, point.Y() + distY, point.Z() + distZ, side, distXTmp, distYTmp, distZTmp);
15391610
distX += distXTmp * scale;
15401611
distY += distYTmp * scale;
15411612
distZ += distZTmp * scale;

Steer/DigitizerWorkflow/src/TPCDigitizerSpec.cxx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ class TPCDPLDigitizerTask : public BaseDPLDigitizer
120120

121121
mWithMCTruth = o2::conf::DigiParams::Instance().mctruth;
122122
auto triggeredMode = ic.options().get<bool>("TPCtriggered");
123+
mRecalcDistortions = !(ic.options().get<bool>("do-not-recalculate-distortions"));
124+
const int nthreadsDist = ic.options().get<int>("n-threads-distortions");
125+
SC::setNThreads(nthreadsDist);
123126
mUseCalibrationsFromCCDB = ic.options().get<bool>("TPCuseCCDB");
124127
mMeanLumiDistortions = ic.options().get<float>("meanLumiDistortions");
125128
mMeanLumiDistortionsDerivative = ic.options().get<float>("meanLumiDistortionsDerivative");
@@ -220,6 +223,9 @@ class TPCDPLDigitizerTask : public BaseDPLDigitizer
220223
if (mDistortionType == 2) {
221224
pc.inputs().get<SC*>("tpcdistortionsderiv");
222225
mDigitizer.setLumiScaleFactor();
226+
if (mRecalcDistortions) {
227+
mDigitizer.recalculateDistortions();
228+
}
223229
}
224230
}
225231

@@ -475,6 +481,7 @@ class TPCDPLDigitizerTask : public BaseDPLDigitizer
475481
int mDistortionType = 0;
476482
float mMeanLumiDistortions = -1;
477483
float mMeanLumiDistortionsDerivative = -1;
484+
bool mRecalcDistortions = false;
478485
};
479486

480487
o2::framework::DataProcessorSpec getTPCDigitizerSpec(int channel, bool writeGRP, bool mctruth, bool internalwriter, int distortionType)
@@ -513,6 +520,8 @@ o2::framework::DataProcessorSpec getTPCDigitizerSpec(int channel, bool writeGRP,
513520
{"TPCuseCCDB", VariantType::Bool, false, {"true: load calibrations from CCDB; false: use random calibratoins"}},
514521
{"meanLumiDistortions", VariantType::Float, -1.f, {"override lumi of distortion object if >=0"}},
515522
{"meanLumiDistortionsDerivative", VariantType::Float, -1.f, {"override lumi of derivative distortion object if >=0"}},
523+
{"do-not-recalculate-distortions", VariantType::Bool, false, {"Do not recalculate the distortions"}},
524+
{"n-threads-distortions", VariantType::Int, 4, {"Number of threads used for the calculation of the distortions"}},
516525
}};
517526
}
518527

0 commit comments

Comments
 (0)