Skip to content

Commit 4354fae

Browse files
ataviragDelloStritto
authored andcommitted
[PWGDQ] Remove reinterpret_cast and add extra protections in MCH tracking eff macro (#18379)
1 parent 1bd4227 commit 4354fae

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

‎PWGDQ/Macros/evalMchTrackingEfficiency.cxx‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727

2828
#include <Rtypes.h>
2929

30+
#include <iostream>
3031
#include <vector>
3132

3233
double computeEfficiencyPerChamber(THnF* hnf, int iAxis, int iCh, double binLimits[2]);
3334
double computeEfficiencyPerChamber(THnF* hnf, const int iAxis[3], int iCh, double binLimits[3][2]);
34-
double computeStationEfficiency(double effPerChamber[4], int iStation);
35-
double computeTotalEfficiency(double effPerStation[5]);
35+
double computeStationEfficiency(double const effPerChamber[4], int iStation);
36+
double computeTotalEfficiency(double const effPerStation[5]);
3637

3738
void getBinLimits(THnF* hnf, int nBins, int iAxis, std::vector<double>& limits);
3839
void setHistoMinPt(THnF* hnf, int iAxis, float minPt);
@@ -46,8 +47,26 @@ void evalMchTrackingEfficiency(const char* inFile = "AnalysisResults.root", cons
4647
{
4748
// read ouput from the taskComputeMchEfficiency
4849
TFile* file = TFile::Open(inFile, "read");
49-
TDirectoryFile* dir = dynamic_cast<TDirectoryFile*>(file->Get("task-muon-mch-trk-efficiency"));
50-
THnF* hHitsEtaPtPhi = dynamic_cast<THnF*>(dir->Get("hHitsEtaPtPhi"));
50+
if (!file || file->IsZombie()) {
51+
std::cerr << "Cannot open input file: " << inFile << std::endl;
52+
return;
53+
}
54+
55+
TDirectoryFile* dir = nullptr;
56+
file->GetObject("task-muon-mch-trk-efficiency", dir);
57+
58+
THnF* hHitsEtaPtPhi = nullptr;
59+
if (dir) {
60+
dir->GetObject("hHitsEtaPtPhi", hHitsEtaPtPhi);
61+
} else {
62+
std::cerr << "Directory task-muon-mch-trk-efficiency not found in file " << inFile << std::endl;
63+
return;
64+
}
65+
66+
if (!hHitsEtaPtPhi) {
67+
std::cerr << "Histogram hHitsEtaPtPhi not found in directory." << std::endl;
68+
return;
69+
}
5170

5271
// retrieve the eta/pt/phi axis binning configuration
5372
const int etaAx = 1, ptAx = 2, phiAx = 3;
@@ -63,8 +82,6 @@ void evalMchTrackingEfficiency(const char* inFile = "AnalysisResults.root", cons
6382
getBinLimits(hHitsEtaPtPhi, nBinsPt, ptAx, limitsPt); // these are values!
6483
getBinLimits(hHitsEtaPtPhi, nBinsPhi, phiAx, limitsPhi); // these are values!
6584

66-
int nBinMinPt = hHitsEtaPtPhi->GetAxis(ptAx)->FindBin(minPt);
67-
6885
// Define the output file
6986
TFile* outFile = new TFile(outFileName, "recreate");
7087

@@ -83,15 +100,13 @@ void evalMchTrackingEfficiency(const char* inFile = "AnalysisResults.root", cons
83100
// as well as the one per station
84101
// the total integrated value is stored in both histos as last quantity for reference
85102
TH1F* hEffIntegratedChamber = new TH1F("hEffIntegratedChamber", "integrated efficiency per chamber; ;Efficiency", kChamber + 1, -0.5, kChamber + 0.5);
86-
const char* hChNames[kChamber + 1];
87103
for (int i = 0; i < kChamber; i++) {
88104
hEffIntegratedChamber->GetXaxis()->SetBinLabel(i + 1, Form("Chamber %d", i));
89105
}
90106
hEffIntegratedChamber->GetXaxis()->SetBinLabel(kChamber + 1, "Total");
91107

92108
// Define histogram for integrated efficiency per station
93109
TH1F* hEffIntegratedStation = new TH1F("hEffIntegratedStation", "integrated efficiency per station; ;Efficiency", kStation, -0.5, kStation + 0.5);
94-
const char* hStNames[kStation];
95110
for (int i = 0; i < 3; i++) {
96111
hEffIntegratedStation->GetXaxis()->SetBinLabel(i + 1, Form("Station %d", i));
97112
}
@@ -274,7 +289,7 @@ double computeEfficiencyPerChamber(THnF* hnf, int iAxis, int iCh, double binLimi
274289
hnf->GetAxis(iAxis)->SetRangeUser(binLimits[0], binLimits[1]);
275290

276291
// Project onto the Nhits axis
277-
TH1F* htmp = dynamic_cast<TH1F*>(hnf->Projection(0)); // FIXME: invalid cast
292+
TH1D* htmp = hnf->Projection(0);
278293
double NhitPairing[16];
279294
for (int i = 0; i < 16; i++) {
280295
NhitPairing[i] = 0;
@@ -322,7 +337,7 @@ double computeEfficiencyPerChamber(THnF* hnf, const int iAxis[3], int iCh, doubl
322337
}
323338

324339
// Project onto the Nhits axis
325-
TH1F* htmp = dynamic_cast<TH1F*>(hnf->Projection(0)); // FIXME: invalid cast
340+
TH1D* htmp = hnf->Projection(0);
326341
double NhitPairing[16];
327342
for (int i = 0; i < 16; i++) {
328343
NhitPairing[i] = 0;
@@ -365,7 +380,7 @@ double computeEfficiencyPerChamber(THnF* hnf, const int iAxis[3], int iCh, doubl
365380
// Compute the efficiency per station
366381
// Stations 1, 2, 3: Eff = 1 - ( 1 - E(i) ) * ( 1 - (E(j) )
367382
// Stations 4 & 5 together: Eff = product(i=6...9) E(i) + sum(i=6...9) [ ( 1 - E(i) )* product(j=6...9, i!=i) E(j) ]
368-
double computeStationEfficiency(double effPerChamber[4], int iStation)
383+
double computeStationEfficiency(double const effPerChamber[4], int iStation)
369384
{
370385
double effSt = 0.;
371386
if (iStation < 3) { // Calculation for Station 1, 2, 3
@@ -390,7 +405,7 @@ double computeStationEfficiency(double effPerChamber[4], int iStation)
390405
}
391406

392407
// Total efficiency corresponds to the product of the efficiency per station
393-
double computeTotalEfficiency(double effPerStation[5])
408+
double computeTotalEfficiency(double const effPerStation[5])
394409
{
395410
double effTot = 1.;
396411
for (int i = 0; i < 4; i++) {

0 commit comments

Comments
 (0)