Skip to content

Commit a6968b6

Browse files
committed
[EMCAL-688] Update ClusterFactory
- Remove TMath calls with std function calls - Fix wrong comments for `thetaToEta` and `etaToTheta` in the .cxx - Update `evalDispersion` function to calculate weights, eta and phi per cell only once in a single loop instead of having two loops
1 parent 2aa6d61 commit a6968b6

2 files changed

Lines changed: 79 additions & 78 deletions

File tree

Detectors/EMCAL/base/include/EMCALBase/ClusterFactory.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,14 @@ class ClusterFactory
409409
void evalTime(std::span<const int> inputsIndices, AnalysisCluster& clusterAnalysis) const;
410410

411411
///
412-
/// Converts Theta (Radians) to Eta (Radians)
413-
float thetaToEta(float arg) const;
412+
/// \brief Converts Theta (Radians) to Eta (Radians)
413+
/// \param theta theta
414+
float thetaToEta(float theta) const;
414415

415416
///
416-
/// Converts Eta (Radians) to Theta (Radians)
417-
float etaToTheta(float arg) const;
417+
/// \brief Converts Eta (Radians) to Theta (Radians)
418+
/// \param eta eta
419+
float etaToTheta(float eta) const;
418420

419421
private:
420422
o2::emcal::Geometry* mGeomPtr = nullptr;

Detectors/EMCAL/base/src/ClusterFactory.cxx

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
#include "EMCALBase/Geometry.h"
2222
// #include "MathUtils/Cartesian.h"
2323

24+
#include "CommonConstants/MathConstants.h"
25+
2426
#include <Rtypes.h>
2527

28+
#include <algorithm>
2629
#include <array>
30+
#include <cmath>
2731
#include <span>
2832

2933
using namespace o2::emcal;
@@ -141,83 +145,75 @@ o2::emcal::AnalysisCluster ClusterFactory<InputType>::buildCluster(int clusterIn
141145
}
142146

143147
///
144-
/// Calculates the dispersion of the shower at the origin of the cluster
145-
/// in cell units
148+
/// \brief Calculates the dispersion of the shower at the origin of the cluster in cell units
149+
/// \param inputsIndices span of the input cell Indices
150+
/// \param clusterAnalysis AnalysisCluster for which the elips axis is calculated
146151
//____________________________________________________________________________
147152
template <class InputType>
148153
void ClusterFactory<InputType>::evalDispersion(std::span<const int> inputsIndices, AnalysisCluster& clusterAnalysis) const
149154
{
150155
double d = 0., wtot = 0.;
151-
int nstat = 0;
152156

153-
// Calculates the dispersion in cell units
157+
if (clusterAnalysis.E() <= 0) {
158+
clusterAnalysis.setDispersion(0.);
159+
return;
160+
}
161+
162+
struct CellWeight {
163+
double eta, phi, w;
164+
};
165+
std::vector<CellWeight> cellData;
166+
cellData.reserve(inputsIndices.size());
167+
154168
double etaMean = 0.0, phiMean = 0.0;
155169

156-
// Calculate mean values
157170
for (auto iInput : inputsIndices) {
171+
if (mInputsContainer[iInput].getEnergy() <= 0) {
172+
continue;
173+
}
158174

159-
if (clusterAnalysis.E() > 0 && mInputsContainer[iInput].getEnergy() > 0) {
160-
auto [nSupMod, nModule, nIphi, nIeta] = mGeomPtr->GetCellIndex(mInputsContainer[iInput].getTower());
161-
auto [iphi, ieta] = mGeomPtr->GetCellPhiEtaIndexInSModule(nSupMod, nModule, nIphi, nIeta);
175+
auto [nSupMod, nModule, nIphi, nIeta] = mGeomPtr->GetCellIndex(mInputsContainer[iInput].getTower());
176+
auto [iphi, ieta] = mGeomPtr->GetCellPhiEtaIndexInSModule(nSupMod, nModule, nIphi, nIeta);
162177

163-
// In case of a shared cluster, index of SM in C side, columns start at 48 and ends at 48*2
164-
// C Side impair SM, nSupMod%2=1; A side pair SM nSupMod%2=0
165-
if (mSharedCluster && nSupMod % 2) {
166-
ieta += EMCAL_COLS;
167-
}
178+
// In case of a shared cluster, index of SM in C side, columns start at 48 and ends at 48*2
179+
// C Side impair SM, nSupMod%2=1; A side pair SM, nSupMod%2=0
180+
if (mSharedCluster && nSupMod % 2) {
181+
ieta += EMCAL_COLS;
182+
}
168183

169-
auto etai = static_cast<double>(ieta);
170-
auto phii = static_cast<double>(iphi);
171-
double w = TMath::Max(0., mLogWeight + TMath::Log(mInputsContainer[iInput].getEnergy() / clusterAnalysis.E()));
184+
auto etai = static_cast<double>(ieta);
185+
auto phii = static_cast<double>(iphi);
186+
double w = std::max(0., static_cast<double>(mLogWeight + std::log(mInputsContainer[iInput].getEnergy() / clusterAnalysis.E())));
172187

173-
if (w > 0.0) {
174-
phiMean += phii * w;
175-
etaMean += etai * w;
176-
wtot += w;
177-
}
188+
if (w > 0.0) {
189+
cellData.push_back({etai, phii, w});
190+
phiMean += phii * w;
191+
etaMean += etai * w;
192+
wtot += w;
178193
}
179194
}
180195

181196
if (wtot > 0) {
182197
phiMean /= wtot;
183198
etaMean /= wtot;
184199
} else {
185-
LOG(error) << Form("Wrong weight %f\n", wtot);
200+
LOG(error) << "Wrong weight " << wtot;
186201
}
187202

188-
// Calculate dispersion
189-
for (auto iInput : inputsIndices) {
190-
191-
if (clusterAnalysis.E() > 0 && mInputsContainer[iInput].getEnergy() > 0) {
192-
auto [nSupMod, nModule, nIphi, nIeta] = mGeomPtr->GetCellIndex(mInputsContainer[iInput].getTower());
193-
auto [iphi, ieta] = mGeomPtr->GetCellPhiEtaIndexInSModule(nSupMod, nModule, nIphi, nIeta);
194-
195-
// In case of a shared cluster, index of SM in C side, columns start at 48 and ends at 48*2
196-
// C Side impair SM, nSupMod%2=1; A side pair SM, nSupMod%2=0
197-
if (mSharedCluster && nSupMod % 2) {
198-
ieta += EMCAL_COLS;
199-
}
200-
201-
auto etai = static_cast<double>(ieta);
202-
auto phii = static_cast<double>(iphi);
203-
double w = TMath::Max(0., mLogWeight + TMath::Log(mInputsContainer[iInput].getEnergy() / clusterAnalysis.E()));
204-
205-
if (w > 0.0) {
206-
nstat++;
207-
d += w * ((etai - etaMean) * (etai - etaMean) + (phii - phiMean) * (phii - phiMean));
208-
}
209-
}
203+
for (const auto& c : cellData) {
204+
d += c.w * ((c.eta - etaMean) * (c.eta - etaMean) + (c.phi - phiMean) * (c.phi - phiMean));
210205
}
211206

212-
if (wtot > 0 && nstat > 1) {
207+
if (wtot > 0 && cellData.size() > 1) {
213208
d /= wtot;
214209
} else {
215210
d = 0.;
216211
}
217212

218-
clusterAnalysis.setDispersion(TMath::Sqrt(d));
213+
clusterAnalysis.setDispersion(std::sqrt(d));
219214
}
220215

216+
221217
///
222218
/// Calculates the center of gravity in the local EMCAL-module coordinates
223219
//____________________________________________________________________________
@@ -247,7 +243,7 @@ void ClusterFactory<InputType>::evalLocalPosition(std::span<const int> inputsInd
247243
}
248244

249245
if (mLogWeight > 0.0) {
250-
w = TMath::Max(0., mLogWeight + TMath::Log(mInputsContainer[iInput].getEnergy() / clusterAnalysis.E()));
246+
w = std::max(0., static_cast<double>(mLogWeight + std::log(mInputsContainer[iInput].getEnergy() / clusterAnalysis.E())));
251247
} else {
252248
w = mInputsContainer[iInput].getEnergy(); // just energy
253249
}
@@ -266,7 +262,7 @@ void ClusterFactory<InputType>::evalLocalPosition(std::span<const int> inputsInd
266262
// cout << " wtot " << wtot << endl;
267263

268264
if (wtot > 0) {
269-
// xRMS = TMath::Sqrt(x2m - xMean*xMean);
265+
// xRMS = std::sqrt(x2m - xMean*xMean);
270266
for (int i = 0; i < 3; i++) {
271267
clXYZ[i] /= wtot;
272268

@@ -275,7 +271,7 @@ void ClusterFactory<InputType>::evalLocalPosition(std::span<const int> inputsInd
275271
clRmsXYZ[i] = clRmsXYZ[i] - clXYZ[i] * clXYZ[i];
276272

277273
if (clRmsXYZ[i] > 0.0) {
278-
clRmsXYZ[i] = TMath::Sqrt(clRmsXYZ[i]);
274+
clRmsXYZ[i] = std::sqrt(clRmsXYZ[i]);
279275
} else {
280276
clRmsXYZ[i] = 0;
281277
}
@@ -320,7 +316,7 @@ void ClusterFactory<InputType>::evalGlobalPosition(std::span<const int> inputsIn
320316
mGeomPtr->GetGlobal(lxyzi, xyzi, mGeomPtr->GetSuperModuleNumber(mInputsContainer[iInput].getTower()));
321317

322318
if (mLogWeight > 0.0) {
323-
w = TMath::Max(0., mLogWeight + TMath::Log(mInputsContainer[iInput].getEnergy() / clusterAnalysis.E()));
319+
w = std::max(0., static_cast<double>(mLogWeight + std::log(mInputsContainer[iInput].getEnergy() / clusterAnalysis.E())));
324320
} else {
325321
w = mInputsContainer[iInput].getEnergy(); // just energy
326322
}
@@ -339,7 +335,7 @@ void ClusterFactory<InputType>::evalGlobalPosition(std::span<const int> inputsIn
339335
// cout << " wtot " << wtot << endl;
340336

341337
if (wtot > 0) {
342-
// xRMS = TMath::Sqrt(x2m - xMean*xMean);
338+
// xRMS = std::sqrt(x2m - xMean*xMean);
343339
for (i = 0; i < 3; i++) {
344340
clXYZ[i] /= wtot;
345341

@@ -348,7 +344,7 @@ void ClusterFactory<InputType>::evalGlobalPosition(std::span<const int> inputsIn
348344
clRmsXYZ[i] = clRmsXYZ[i] - clXYZ[i] * clXYZ[i];
349345

350346
if (clRmsXYZ[i] > 0.0) {
351-
clRmsXYZ[i] = TMath::Sqrt(clRmsXYZ[i]);
347+
clRmsXYZ[i] = std::sqrt(clRmsXYZ[i]);
352348
} else {
353349
clRmsXYZ[i] = 0;
354350
}
@@ -386,7 +382,7 @@ void ClusterFactory<InputType>::evalLocalPositionFit(double deff, double mLogWei
386382
}
387383

388384
if (mLogWeight > 0.0) {
389-
w = TMath::Max(0., mLogWeight + TMath::Log(mInputsContainer[iInput].getEnergy() / clusterAnalysis.E()));
385+
w = std::max(0., static_cast<double>(mLogWeight + std::log(mInputsContainer[iInput].getEnergy() / clusterAnalysis.E())));
390386
} else {
391387
w = mInputsContainer[iInput].getEnergy(); // just energy
392388
}
@@ -405,7 +401,7 @@ void ClusterFactory<InputType>::evalLocalPositionFit(double deff, double mLogWei
405401
// cout << " wtot " << wtot << endl;
406402

407403
if (wtot > 0) {
408-
// xRMS = TMath::Sqrt(x2m - xMean*xMean);
404+
// xRMS = std::sqrt(x2m - xMean*xMean);
409405
for (i = 0; i < 3; i++) {
410406
clXYZ[i] /= wtot;
411407

@@ -414,7 +410,7 @@ void ClusterFactory<InputType>::evalLocalPositionFit(double deff, double mLogWei
414410
clRmsXYZ[i] = clRmsXYZ[i] - clXYZ[i] * clXYZ[i];
415411

416412
if (clRmsXYZ[i] > 0.0) {
417-
clRmsXYZ[i] = TMath::Sqrt(clRmsXYZ[i]);
413+
clRmsXYZ[i] = std::sqrt(clRmsXYZ[i]);
418414
} else {
419415
clRmsXYZ[i] = 0;
420416
}
@@ -458,8 +454,8 @@ void ClusterFactory<InputType>::getDeffW0(const double esum, double& deff, doubl
458454
e = esum < 0.5 ? 0.5 : esum;
459455
e = e > 100. ? 100. : e;
460456

461-
deff = kdp0 + kdp1 * TMath::Log(e);
462-
w0 = kwp0 / (1. + TMath::Exp(kwp1 * (e + kwp2)));
457+
deff = kdp0 + kdp1 * std::log(e);
458+
w0 = kwp0 / (1. + std::exp(kwp1 * (e + kwp2)));
463459
}
464460

465461
///
@@ -485,9 +481,9 @@ void ClusterFactory<InputType>::evalCoreEnergy(std::span<const int> inputsIndice
485481
for (auto iInput : inputsIndices) {
486482

487483
auto [eta, phi] = mGeomPtr->EtaPhiFromIndex(mInputsContainer[iInput].getTower());
488-
phi = phi * TMath::DegToRad();
484+
phi = phi * o2::constants::math::Deg2Rad;
489485

490-
double distance = TMath::Sqrt((eta - etaPoint) * (eta - etaPoint) + (phi - phiPoint) * (phi - phiPoint));
486+
double distance = std::sqrt((eta - etaPoint) * (eta - etaPoint) + (phi - phiPoint) * (phi - phiPoint));
491487

492488
if (distance < mCoreRadius) {
493489
coreEnergy += mInputsContainer[iInput].getEnergy();
@@ -555,8 +551,9 @@ void ClusterFactory<InputType>::evalNExMax(std::span<const int> inputsIndices, A
555551
}
556552

557553
///
558-
/// Calculates the axis of the shower ellipsoid in eta and phi
559-
/// in cell units
554+
/// \brief Calculates the axis of the shower ellipsoid in eta and phi in cell units
555+
/// \param inputsIndices span of the input cell Indices
556+
/// \param clusterAnalysis AnalysisCluster for which the elips axis is calculated
560557
//____________________________________________________________________________
561558
template <class InputType>
562559
void ClusterFactory<InputType>::evalElipsAxis(std::span<const int> inputsIndices, AnalysisCluster& clusterAnalysis) const
@@ -584,7 +581,7 @@ void ClusterFactory<InputType>::evalElipsAxis(std::span<const int> inputsIndices
584581
auto etai = static_cast<double>(ieta);
585582
auto phii = static_cast<double>(iphi);
586583

587-
double w = TMath::Max(0., mLogWeight + TMath::Log(mInputsContainer[iInput].getEnergy() / clusterAnalysis.E()));
584+
double w = std::max(0., static_cast<double>(mLogWeight + std::log(mInputsContainer[iInput].getEnergy() / clusterAnalysis.E())));
588585
// clusterAnalysis.E() summed amplitude of inputs, i.e. energy of cluster
589586
// Gives smaller value of lambda than log weight
590587
// w = mEnergyList[iInput] / clusterAnalysis.E(); // Nov 16, 2006 - try just energy
@@ -609,18 +606,18 @@ void ClusterFactory<InputType>::evalElipsAxis(std::span<const int> inputsIndices
609606
dxz /= wtot;
610607
dxz -= x * z;
611608

612-
lambda[0] = 0.5 * (dxx + dzz) + TMath::Sqrt(0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz);
609+
lambda[0] = 0.5 * (dxx + dzz) + std::sqrt(0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz);
613610

614611
if (lambda[0] > 0) {
615-
lambda[0] = TMath::Sqrt(lambda[0]);
612+
lambda[0] = std::sqrt(lambda[0]);
616613
} else {
617614
lambda[0] = 0;
618615
}
619616

620-
lambda[1] = 0.5 * (dxx + dzz) - TMath::Sqrt(0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz);
617+
lambda[1] = 0.5 * (dxx + dzz) - std::sqrt(0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz);
621618

622619
if (lambda[1] > 0) { // To avoid exception if numerical errors lead to negative lambda.
623-
lambda[1] = TMath::Sqrt(lambda[1]);
620+
lambda[1] = std::sqrt(lambda[1]);
624621
} else {
625622
lambda[1] = 0.;
626623
}
@@ -862,13 +859,13 @@ void ClusterFactory<InputType>::evalTime(std::span<const int> inputsIndices, Ana
862859
template <class InputType>
863860
double ClusterFactory<InputType>::tMaxInCm(const double e, const int key) const
864861
{
865-
const double ca = 4.82; // shower max parameter - first guess; ca=TMath::Log(1000./8.07)
862+
const double ca = 4.82; // shower max parameter - first guess; ca=std::log(1000./8.07)
866863
double tmax = 0.; // position of electromagnetic shower max in cm
867864

868865
const double x0 = 1.31; // radiation lenght (cm)
869866

870867
if (e > 0.1) {
871-
tmax = TMath::Log(e) + ca;
868+
tmax = std::log(e) + ca;
872869
if (key == 0) {
873870
tmax += 0.5;
874871
} else {
@@ -881,21 +878,23 @@ double ClusterFactory<InputType>::tMaxInCm(const double e, const int key) const
881878
}
882879

883880
///
884-
/// Converts Theta (Radians) to Eta (Radians)
881+
/// \brief Converts Eta (Radians) to Theta (Radians)
882+
/// \param eta eta
885883
//______________________________________________________________________________
886884
template <class InputType>
887-
float ClusterFactory<InputType>::etaToTheta(float arg) const
885+
float ClusterFactory<InputType>::etaToTheta(float eta) const
888886
{
889-
return (2. * TMath::ATan(TMath::Exp(-arg)));
887+
return (2.f * std::atan(std::exp(-eta)));
890888
}
891889

892890
///
893-
/// Converts Eta (Radians) to Theta (Radians)
891+
/// \brief Converts Theta (Radians) to Eta (Radians)
892+
/// \param theta theta
894893
//______________________________________________________________________________
895894
template <class InputType>
896-
float ClusterFactory<InputType>::thetaToEta(float arg) const
895+
float ClusterFactory<InputType>::thetaToEta(float theta) const
897896
{
898-
return (-1 * TMath::Log(TMath::Tan(0.5 * arg)));
897+
return (-1.f * std::log(std::tan(0.5f * theta)));
899898
}
900899

901900
template <class InputType>

0 commit comments

Comments
 (0)