Skip to content

Commit a01425c

Browse files
authored
Refactor validation code (#431)
* Refactor utilities * Add copyright. Add tuple header. Fix path. * Add header guards * Move compare.py * Factor out plotting part of validation * Fix plotting factorisation * Factor out MakePlots into a header file. * Fix casting * Break line * Rename headers * Fix header guards * Improve logging * Improve legends in validation plots * Fix JE validation plots and factor out code * Remove link to utils_plot.h * Improve y-range calculation and plotting, add protections * Improve AliPhysics task macros * Add documentation comments in AliPhysics macros
1 parent 390f6f1 commit a01425c

File tree

13 files changed

+493
-523
lines changed

13 files changed

+493
-523
lines changed

codeHF/Compare.C

Lines changed: 15 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
1-
// Comparison of AliPhysics and O2 histograms
2-
3-
#include "utils_plot.h"
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.
411

5-
// vectors of histogram specifications
6-
using VecSpecHis = std::vector<std::tuple<TString, TString, TString, int, bool, bool, TString>>;
12+
// Comparison of AliPhysics and O2 histograms
713

8-
// Add histogram specification in the vector.
9-
void AddHistogram(VecSpecHis& vec, TString label, TString nameAli, TString nameO2, int rebin, bool logH, bool logR, TString proj = "x")
10-
{
11-
vec.push_back(std::make_tuple(label, nameAli, nameO2, rebin, logH, logR, proj));
12-
}
14+
#include "../exec/utilitiesValidation.h"
1315

14-
Int_t Compare(TString fileO2 = "AnalysisResults_O2.root", TString fileAli = "AnalysisResults_ALI.root", TString options = "", bool doRatio = false)
16+
Int_t Compare(TString pathFileO2 = "AnalysisResults_O2.root", TString pathFileAli = "AnalysisResults_ALI.root", TString options = "", bool doRatio = false)
1517
{
16-
gStyle->SetOptStat(0);
17-
gStyle->SetPalette(0);
18-
gStyle->SetCanvasColor(0);
19-
gStyle->SetFrameFillColor(0);
20-
21-
TFile* fO2 = new TFile(fileO2.Data());
22-
if (fO2->IsZombie()) {
23-
printf("Failed to open file %s\n", fileO2.Data());
24-
return 1;
25-
}
26-
TFile* fAli = new TFile(fileAli.Data());
27-
if (fAli->IsZombie()) {
28-
printf("Failed to open file %s\n", fileAli.Data());
29-
return 1;
30-
}
31-
3218
TString pathListAli = "HFVertices/clistHFVertices";
33-
TList* lAli = nullptr;
34-
fAli->GetObject(pathListAli.Data(), lAli);
35-
if (!lAli) {
36-
printf("Failed to load list %s from %s\n", pathListAli.Data(), fileAli.Data());
37-
return 1;
38-
}
39-
4019
TString labelParticle = "";
4120

4221
// Histogram specification: axis label, AliPhysics name, O2Physics path/name, rebin, log scale histogram, log scale ratio, projection axis
@@ -251,7 +230,7 @@ Int_t Compare(TString fileO2 = "AnalysisResults_O2.root", TString fileAli = "Ana
251230
AddHistogram(vecHisJetSubstructureMC, "#it{n}_{SD,gen}", "fHistJetNsd_Part", "jet-substructure-hf-mcp/h_jet_nsd", 1, 0, 0);
252231

253232
// vector of specifications of vectors: name, VecSpecHis, pads X, pads Y
254-
std::vector<std::tuple<TString, VecSpecHis, int, int>> vecSpecVecSpec;
233+
VecSpecVecSpec vecSpecVecSpec;
255234

256235
// Add vector specifications in the vector.
257236
if (options.Contains(" events "))
@@ -295,138 +274,5 @@ Int_t Compare(TString fileO2 = "AnalysisResults_O2.root", TString fileAli = "Ana
295274
if (options.Contains(" jets-substructure-mc "))
296275
vecSpecVecSpec.push_back(std::make_tuple("jets-substructure-mc", vecHisJetSubstructureMC, 5, 3));
297276

298-
// Histogram plot vertical margins
299-
Float_t marginHigh = 0.05;
300-
Float_t marginLow = 0.05;
301-
bool logScaleH = false;
302-
// Ratio plot vertical margins
303-
Float_t marginRHigh = 0.05;
304-
Float_t marginRLow = 0.05;
305-
bool logScaleR = false;
306-
Float_t yMin, yMax;
307-
Int_t nAli, nO2, rebin;
308-
309-
TH1F* hAli = nullptr;
310-
TH1D* hO2 = nullptr;
311-
TH1F* hRatio = nullptr;
312-
TString labelAxis = "";
313-
TString nameHisAli = "";
314-
TString nameHisO2 = "";
315-
TString projAx = "";
316-
TCanvas* canHis = nullptr;
317-
TCanvas* canRat = nullptr;
318-
319-
// loop over lists
320-
for (const auto& specVecSpec : vecSpecVecSpec) {
321-
auto nameSpec = std::get<0>(specVecSpec); // list name
322-
auto vecSpec = std::get<1>(specVecSpec); // list of histogram specs.
323-
int nPadsX = std::get<2>(specVecSpec); // number of horizontal pads
324-
int nPadsY = std::get<3>(specVecSpec); // number of vertical pads
325-
Printf("\nProcessing histogram list: %s (%d)", nameSpec.Data(), (int)vecSpec.size());
326-
if (nPadsX * nPadsY < vecSpec.size()) {
327-
Printf("Not enough pads (%d)", nPadsX * nPadsY);
328-
return 1;
329-
}
330-
331-
canHis = new TCanvas(Form("canHis_%s", nameSpec.Data()), Form("Histos_%s", nameSpec.Data()), 3000, 1600);
332-
SetCanvas(canHis, nPadsX, nPadsY);
333-
if (doRatio) {
334-
canRat = new TCanvas(Form("canRat_%s", nameSpec.Data()), Form("Ratios_%s", nameSpec.Data()), 3000, 1600);
335-
SetCanvas(canRat, nPadsX, nPadsY);
336-
}
337-
338-
// loop over histograms
339-
for (int index = 0; index < vecSpec.size(); index++) {
340-
auto spec = vecSpec[index];
341-
labelAxis = std::get<0>(spec);
342-
nameHisAli = std::get<1>(spec);
343-
nameHisO2 = std::get<2>(spec);
344-
rebin = std::get<3>(spec);
345-
logScaleH = std::get<4>(spec);
346-
logScaleR = std::get<5>(spec);
347-
projAx = std::get<6>(spec);
348-
349-
// Get AliPhysics histogram.
350-
hAli = (TH1F*)lAli->FindObject(nameHisAli.Data());
351-
if (!hAli) {
352-
printf("Failed to load %s from %s\n", nameHisAli.Data(), fileAli.Data());
353-
return 1;
354-
}
355-
356-
// Get O2 histogram.
357-
auto oO2 = fO2->Get(nameHisO2.Data());
358-
if (!oO2) {
359-
printf("Failed to load %s from %s\n", nameHisO2.Data(), fileO2.Data());
360-
return 1;
361-
}
362-
363-
if (oO2->InheritsFrom("TH3")) {
364-
if (projAx == "x") {
365-
hO2 = ((TH3D*)oO2)->ProjectionX();
366-
} else if (projAx == "y") {
367-
hO2 = ((TH3D*)oO2)->ProjectionY();
368-
}
369-
} else if (oO2->InheritsFrom("TH2")) {
370-
if (projAx == "x") {
371-
hO2 = ((TH2D*)oO2)->ProjectionX();
372-
} else if (projAx == "y") {
373-
hO2 = ((TH2D*)oO2)->ProjectionY();
374-
}
375-
} else {
376-
hO2 = (TH1D*)oO2;
377-
}
378-
379-
Printf("%d (%s, %s): bins: %d, %d, ranges: %g-%g, %g-%g",
380-
index, nameHisAli.Data(), nameHisO2.Data(),
381-
hAli->GetNbinsX(), hO2->GetNbinsX(),
382-
hAli->GetXaxis()->GetBinLowEdge(1), hAli->GetXaxis()->GetBinUpEdge(hAli->GetNbinsX()),
383-
hO2->GetXaxis()->GetBinLowEdge(1), hO2->GetXaxis()->GetBinUpEdge(hO2->GetNbinsX()));
384-
385-
nAli = hAli->GetEntries();
386-
nO2 = hO2->GetEntries();
387-
388-
// Histograms
389-
auto padH = canHis->cd(index + 1);
390-
hAli->Rebin(rebin);
391-
hO2->Rebin(rebin);
392-
hAli->SetLineColor(1);
393-
hAli->SetLineWidth(2);
394-
hO2->SetLineColor(2);
395-
hO2->SetLineWidth(1);
396-
hAli->SetTitle(Form("Entries: Ali: %d, O^{2}: %d;%s;Entries", nAli, nO2, labelAxis.Data()));
397-
hAli->GetYaxis()->SetMaxDigits(3);
398-
yMin = TMath::Min(hO2->GetMinimum(0), hAli->GetMinimum(0));
399-
yMax = TMath::Max(hO2->GetMaximum(), hAli->GetMaximum());
400-
SetHistogram(hAli, yMin, yMax, marginLow, marginHigh, logScaleH);
401-
SetPad(padH, logScaleH);
402-
hAli->Draw();
403-
hO2->Draw("same");
404-
TLegend* legend = new TLegend(0.8, 0.72, 1., 0.92);
405-
legend->AddEntry(hAli, "Ali", "L");
406-
legend->AddEntry(hO2, "O^{2}", "L");
407-
legend->Draw();
408-
409-
// Ratio
410-
if (doRatio) {
411-
auto padR = canRat->cd(index + 1);
412-
hRatio = (TH1F*)hO2->Clone(Form("hRatio%d", index));
413-
hRatio->Divide(hAli);
414-
hRatio->SetTitle(Form("Entries ratio: %g;%s;O^{2}/Ali", (double)nO2 / (double)nAli, labelAxis.Data()));
415-
yMin = hRatio->GetMinimum(0);
416-
yMax = hRatio->GetMaximum();
417-
SetHistogram(hRatio, yMin, yMax, marginRLow, marginRHigh, logScaleR);
418-
SetPad(padR, logScaleR);
419-
hRatio->Draw();
420-
}
421-
}
422-
canHis->SaveAs(Form("comparison_histos_%s.pdf", nameSpec.Data()));
423-
canHis->SaveAs(Form("comparison_histos_%s.png", nameSpec.Data()));
424-
if (doRatio) {
425-
canRat->SaveAs(Form("comparison_ratios_%s.pdf", nameSpec.Data()));
426-
canRat->SaveAs(Form("comparison_ratios_%s.png", nameSpec.Data()));
427-
}
428-
delete canHis;
429-
delete canRat;
430-
}
431-
return 0;
277+
return MakePlots(vecSpecVecSpec, pathFileO2, pathFileAli, pathListAli, doRatio);
432278
}

codeHF/PlotEfficiency.C

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
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+
112
// Plotting of reconstruction efficiency
213

3-
#include "utils_plot.h"
14+
#include "../exec/utilitiesPlot.h"
415

516
Int_t PlotEfficiency(TString pathFile = "AnalysisResults.root", TString particles = "d0")
617
{
@@ -88,12 +99,12 @@ Int_t PlotEfficiency(TString pathFile = "AnalysisResults.root", TString particle
8899
bool okPrompt = true;
89100
TH1F* hPtRecPrompt = (TH1F*)file->Get(nameHistRec.Data());
90101
if (!hPtRecPrompt) {
91-
Printf("Warning: Failed to load %s from %s", nameHistRec.Data(), pathFile.Data());
102+
Warning("PlotEfficiency", "Failed to load %s from %s", nameHistRec.Data(), pathFile.Data());
92103
okPrompt = false;
93104
}
94105
TH1F* hPtGenPrompt = (TH1F*)file->Get(nameHistgen.Data());
95106
if (!hPtGenPrompt) {
96-
Printf("Warning: Failed to load %s from %s", nameHistgen.Data(), pathFile.Data());
107+
Warning("PlotEfficiency", "Failed to load %s from %s", nameHistgen.Data(), pathFile.Data());
97108
okPrompt = false;
98109
}
99110

@@ -109,12 +120,12 @@ Int_t PlotEfficiency(TString pathFile = "AnalysisResults.root", TString particle
109120
bool okNonPrompt = true;
110121
TH1F* hPtRecNonPrompt = (TH1F*)file->Get(nameHistRec.Data());
111122
if (!hPtRecNonPrompt) {
112-
Printf("Warning: Failed to load %s from %s", nameHistRec.Data(), pathFile.Data());
123+
Warning("PlotEfficiency", "Failed to load %s from %s", nameHistRec.Data(), pathFile.Data());
113124
okNonPrompt = false;
114125
}
115126
TH1F* hPtGenNonPrompt = (TH1F*)file->Get(nameHistgen.Data());
116127
if (!hPtGenNonPrompt) {
117-
Printf("Warning: Failed to load %s from %s", nameHistgen.Data(), pathFile.Data());
128+
Warning("PlotEfficiency", "Failed to load %s from %s", nameHistgen.Data(), pathFile.Data());
118129
okNonPrompt = false;
119130
}
120131

codeHF/PlotEfficiencyRecoStep.C

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
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+
112
// Computation and plotting of reconstruction efficiency step-by-step
213
// Four steps defined: RecoHFFlag, RecoTopol, RecoCand, RecoPID
314
// RecoHFFlag: candidates properly flagged (e.g. in HFD0CandidateSelector --> hfflag() is D0ToPiK)
@@ -10,7 +21,7 @@
1021
// .L PlotEfficiencyRecoStep.C
1122
// PlotEfficiencyRecoStep("InputName.root","particlename",true);
1223

13-
#include "utils_plot.h"
24+
#include "../exec/utilitiesPlot.h"
1425

1526
void SetProperAxisRange(TH1F** histo, int NIteration, float marginHigh, float marginLow, bool logScaleH);
1627

codeHF/RunHFTaskLocal.C

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
TChain* CreateLocalChain(const char* txtfile);
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+
12+
// Macro to run the HF AliPhysics task that produces validation histograms
13+
14+
#include "../exec/utilitiesAli.h"
215

316
Long64_t RunHFTaskLocal(TString txtfile = "./list_ali.txt",
417
TString jsonfilename = "dpl-config_std.json",
@@ -26,7 +39,7 @@ Long64_t RunHFTaskLocal(TString txtfile = "./list_ali.txt",
2639

2740
TChain* chainESD = CreateLocalChain(txtfile.Data());
2841
if (!chainESD) {
29-
Error("CreateLocalChain", "Failed to create chain from file %s", txtfile.Data());
42+
Fatal("CreateLocalChain", "Failed to create chain from file %s", txtfile.Data());
3043
return -1;
3144
}
3245

@@ -60,34 +73,3 @@ Long64_t RunHFTaskLocal(TString txtfile = "./list_ali.txt",
6073
mgr->PrintStatus();
6174
return mgr->StartAnalysis("local", chainESD);
6275
};
63-
64-
TChain* CreateLocalChain(const char* txtfile)
65-
{
66-
// Open the file
67-
ifstream in;
68-
in.open(txtfile);
69-
Int_t count = 0;
70-
// Read the input list of files and add them to the chain
71-
TString line;
72-
TChain* chain = new TChain("esdTree");
73-
while (in.good()) {
74-
in >> line;
75-
if (line.IsNull() || line.BeginsWith("#"))
76-
continue;
77-
TString esdFile(line);
78-
TFile* file = TFile::Open(esdFile);
79-
if (file && !file->IsZombie()) {
80-
chain->Add(esdFile);
81-
file->Close();
82-
} else {
83-
Error("CreateLocalChain", "Skipping un-openable file: %s", esdFile.Data());
84-
}
85-
}
86-
in.close();
87-
if (!chain->GetListOfFiles()->GetEntries()) {
88-
Error("CreateLocalChain", "No file from %s could be opened", txtfile);
89-
delete chain;
90-
return nullptr;
91-
}
92-
return chain;
93-
}

codeHF/utils_plot.h

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)