Skip to content

Commit 8c59468

Browse files
lubynetsalibuild
andauthored
[PWGHF] Cut variation: add efficiency-related functional (#17929)
Co-authored-by: ALICE Builder <alibuild@users.noreply.github.com>
1 parent 29c68ad commit 8c59468

3 files changed

Lines changed: 205 additions & 59 deletions

File tree

‎PWGHF/D2H/Macros/compute_fraction_cutvar.py‎

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
import json
1212
import os
1313
import sys
14+
from enum import IntEnum, auto
1415

1516
import numpy as np # pylint: disable=import-error
1617
import ROOT # pylint: disable=import-error
17-
from enum import IntEnum, auto
18+
1819
sys.path.insert(0, '..')
19-
from cut_variation import CutVarMinimiser
20-
from cut_variation import MinimisationStatus
20+
from cut_variation import CutVarMinimiser, MinimisationStatus
2121
from style_formatter import set_object_style
2222

2323
# pylint: disable=no-member,too-many-locals,too-many-statements
@@ -28,6 +28,7 @@ class PlotType(IntEnum):
2828
Frac = auto()
2929
Cov = auto()
3030
Unc = auto()
31+
RelUnc = auto()
3132
N = auto()
3233

3334
class ObjectToSave(IntEnum):
@@ -54,6 +55,10 @@ def main(config):
5455
with open(config, encoding="utf8") as fil:
5556
cfg = json.load(fil)
5657

58+
zero_eff_unc = cfg.get("zero_eff_unc", False)
59+
effp_shift_nsigma = cfg.get("effp_shift_nsigma", 0.0)
60+
effnp_shift_nsigma = cfg.get("effnp_shift_nsigma", 0.0)
61+
5762
hist_rawy, hist_effp, hist_effnp = ([] for _ in range(3))
5863
for filename_rawy, filename_eff in zip(cfg["rawyields"]["inputfiles"], cfg["efficiencies"]["inputfiles"]):
5964
infile_rawy = ROOT.TFile.Open(os.path.join(cfg["rawyields"]["inputdir"], filename_rawy))
@@ -75,6 +80,12 @@ def main(config):
7580
sys.exit(f"\33[31mFatal error: Histogram with efficiency for nonprompt \"{hist_effnp}\" is absent. Exit.\33[0m")
7681
hist_effp[-1].SetDirectory(0)
7782
hist_effnp[-1].SetDirectory(0)
83+
for i_bin in range(1, hist_effp[-1].GetNbinsX() + 1):
84+
hist_effp[-1].SetBinContent(i_bin, hist_effp[-1].GetBinContent(i_bin) + effp_shift_nsigma*hist_effp[-1].GetBinError(i_bin))
85+
hist_effnp[-1].SetBinContent(i_bin, hist_effnp[-1].GetBinContent(i_bin) + effnp_shift_nsigma*hist_effnp[-1].GetBinError(i_bin))
86+
if zero_eff_unc:
87+
hist_effp[-1].SetBinError(i_bin, 0.0)
88+
hist_effnp[-1].SetBinError(i_bin, 0.0)
7889
infile_eff.Close()
7990

8091
pt_bin_to_process = cfg.get("pt_bin_to_process", -1)
@@ -89,13 +100,15 @@ def main(config):
89100
is_draw_title[PlotType.Frac] = cfg.get("is_draw_title", {}).get("frac", False)
90101
is_draw_title[PlotType.Cov] = cfg.get("is_draw_title", {}).get("cov", False)
91102
is_draw_title[PlotType.Unc] = cfg.get("is_draw_title", {}).get("unc", True)
103+
is_draw_title[PlotType.RelUnc] = cfg.get("is_draw_title", {}).get("relunc", True)
92104

93105
is_save_canvas_as_macro = [False] * PlotType.N
94106
is_save_canvas_as_macro[PlotType.Rawy] = cfg.get("is_save_canvas_as_macro", {}).get("rawy", False)
95107
is_save_canvas_as_macro[PlotType.Eff] = cfg.get("is_save_canvas_as_macro", {}).get("eff", False)
96108
is_save_canvas_as_macro[PlotType.Frac] = cfg.get("is_save_canvas_as_macro", {}).get("frac", False)
97109
is_save_canvas_as_macro[PlotType.Cov] = cfg.get("is_save_canvas_as_macro", {}).get("cov", False)
98110
is_save_canvas_as_macro[PlotType.Unc] = cfg.get("is_save_canvas_as_macro", {}).get("unc", False)
111+
is_save_canvas_as_macro[PlotType.RelUnc] = cfg.get("is_save_canvas_as_macro", {}).get("relunc", False)
99112

100113
is_save_to_root_file = [False] * ObjectToSave.N
101114
is_save_to_root_file[ObjectToSave.Canvas] = cfg.get("is_save_to_root_file", {}).get("canvas", True)
@@ -205,14 +218,16 @@ def main(config):
205218
)
206219

207220
pt_bin_to_process_name_suffix = ""
208-
if pt_bin_to_process != -1: pt_bin_to_process_name_suffix = "_bin_" + str(pt_bin_to_process)
221+
if pt_bin_to_process != -1:
222+
pt_bin_to_process_name_suffix = "_bin_" + str(pt_bin_to_process)
209223

210224
output_name_template = cfg['output']['file'].replace(".root", "") + pt_bin_to_process_name_suffix + ".root"
211225
output = ROOT.TFile(os.path.join(cfg["output"]["directory"], output_name_template), "recreate")
212226
n_sets = len(hist_rawy)
213227
pt_axis_title = hist_rawy[0].GetXaxis().GetTitle()
214228
for ipt in range(hist_rawy[0].GetNbinsX()):
215-
if pt_bin_to_process !=-1 and ipt+1 != pt_bin_to_process: continue
229+
if pt_bin_to_process !=-1 and ipt+1 != pt_bin_to_process:
230+
continue
216231
all_vectors_monotonous = MinimisationStatus.Success
217232
pt_min = hist_rawy[0].GetXaxis().GetBinLowEdge(ipt + 1)
218233
pt_max = hist_rawy[0].GetXaxis().GetBinUpEdge(ipt + 1)
@@ -237,9 +252,9 @@ def main(config):
237252
print("\0\33[33mWARNING! main(): the raw yield uncertainties vector is not monotonous. Check the input for stability.\0\33[0m")
238253
print(f"raw yield uncertainties vector elements = {unc_rawy}\n")
239254
if not (np.all(effp[1:] > effp[:-1]) or np.all(effp[1:] < effp[:-1])):
240-
sys.exit(f"\33[31mFatal error: the prompt efficiency vector is not monotonous. Check the input. Exit.\33[0m")
255+
sys.exit("\33[31mFatal error: the prompt efficiency vector is not monotonous. Check the input. Exit.\33[0m")
241256
if not (np.all(effnp[1:] > effnp[:-1]) or np.all(effnp[1:] < effnp[:-1])):
242-
sys.exit(f"\33[31mFatal error: the nonprompt efficiency vector is not monotonous. Check the input. Exit.\33[0m")
257+
sys.exit("\33[31mFatal error: the nonprompt efficiency vector is not monotonous. Check the input. Exit.\33[0m")
243258

244259
minimiser = CutVarMinimiser(rawy, effp, effnp, unc_rawy, unc_effp, unc_effnp)
245260
status = minimiser.minimise_system(cfg["minimisation"]["correlated"])
@@ -278,47 +293,69 @@ def main(config):
278293
hist_bin_title = f"bin # {ipt+1}; {pt_axis_title}#in ({pt_min}; {pt_max})"
279294

280295
hist_bin_title_rawy = hist_bin_title if is_draw_title[PlotType.Rawy] else ""
281-
canv_rawy, histos_rawy, leg_r = minimiser.plot_result(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_rawy)
296+
canv_rawy, histos_rawy, _leg_r = minimiser.plot_result(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_rawy)
282297
output.cd()
283-
if is_save_to_root_file[ObjectToSave.Canvas]: canv_rawy.Write()
298+
if is_save_to_root_file[ObjectToSave.Canvas]:
299+
canv_rawy.Write()
284300
if is_save_to_root_file[ObjectToSave.RawYield]:
285-
for _, hist in histos_rawy.items():
301+
for _, hist in histos_rawy.values():
286302
hist.Write()
287-
if is_save_canvas_as_macro[PlotType.Rawy]: canv_rawy.SaveAs(f"canv_rawy_{ipt+1}.C")
303+
if is_save_canvas_as_macro[PlotType.Rawy]:
304+
canv_rawy.SaveAs(f"canv_rawy_{ipt+1}.C")
288305

289306
hist_bin_title_unc = hist_bin_title if is_draw_title[PlotType.Unc] else ""
290-
canv_unc, histos_unc, leg_unc = minimiser.plot_uncertainties(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_unc)
307+
canv_unc, histos_unc, _leg_unc = minimiser.plot_uncertainties(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_unc)
308+
output.cd()
309+
if is_save_to_root_file[ObjectToSave.Canvas]:
310+
canv_unc.Write()
311+
if is_save_to_root_file[ObjectToSave.Uncertainty]:
312+
for _, hist in histos_unc.values():
313+
hist.Write()
314+
if is_save_canvas_as_macro[PlotType.Unc]:
315+
canv_unc.SaveAs(f"canv_unc_{ipt+1}.C")
316+
317+
hist_bin_title_rel_unc = hist_bin_title if is_draw_title[PlotType.RelUnc] else ""
318+
canv_rel_unc, histos_rel_unc, _leg_rel_unc = minimiser.plot_relative_uncertainties(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_rel_unc)
291319
output.cd()
292-
if is_save_to_root_file[ObjectToSave.Canvas]: canv_unc.Write()
320+
if is_save_to_root_file[ObjectToSave.Canvas]:
321+
canv_rel_unc.Write()
293322
if is_save_to_root_file[ObjectToSave.Uncertainty]:
294-
for _, hist in histos_unc.items():
323+
for _, hist in histos_rel_unc.values():
295324
hist.Write()
296-
if is_save_canvas_as_macro[PlotType.Unc]: canv_unc.SaveAs(f"canv_unc_{ipt+1}.C")
325+
if is_save_canvas_as_macro[PlotType.RelUnc]:
326+
canv_rel_unc.SaveAs(f"canv_rel_unc_{ipt+1}.C")
297327

298328
hist_bin_title_eff = hist_bin_title if is_draw_title[PlotType.Eff] else ""
299-
canv_eff, histos_eff, leg_e = minimiser.plot_efficiencies(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_eff)
329+
canv_eff, histos_eff, _leg_e = minimiser.plot_efficiencies(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_eff)
300330
output.cd()
301-
if is_save_to_root_file[ObjectToSave.Canvas]: canv_eff.Write()
331+
if is_save_to_root_file[ObjectToSave.Canvas]:
332+
canv_eff.Write()
302333
if is_save_to_root_file[ObjectToSave.Efficiency]:
303-
for _, hist in histos_eff.items():
334+
for _, hist in histos_eff.values():
304335
hist.Write()
305-
if is_save_canvas_as_macro[PlotType.Eff]: canv_eff.SaveAs(f"canv_eff_{ipt+1}.C")
336+
if is_save_canvas_as_macro[PlotType.Eff]:
337+
canv_eff.SaveAs(f"canv_eff_{ipt+1}.C")
306338

307339
hist_bin_title_frac = hist_bin_title if is_draw_title[PlotType.Frac] else ""
308-
canv_frac, histos_frac, leg_f = minimiser.plot_fractions(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_frac)
340+
canv_frac, histos_frac, _leg_f = minimiser.plot_fractions(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_frac)
309341
output.cd()
310-
if is_save_to_root_file[ObjectToSave.Canvas]: canv_frac.Write()
342+
if is_save_to_root_file[ObjectToSave.Canvas]:
343+
canv_frac.Write()
311344
if is_save_to_root_file[ObjectToSave.Fraction]:
312-
for _, hist in histos_frac.items():
345+
for _, hist in histos_frac.values():
313346
hist.Write()
314-
if is_save_canvas_as_macro[PlotType.Frac]: canv_frac.SaveAs(f"canv_frac_{ipt+1}.C")
347+
if is_save_canvas_as_macro[PlotType.Frac]:
348+
canv_frac.SaveAs(f"canv_frac_{ipt+1}.C")
315349

316350
hist_bin_title_cov = hist_bin_title if is_draw_title[PlotType.Cov] else ""
317351
canv_cov, histo_cov = minimiser.plot_cov_matrix(True, f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_cov)
318352
output.cd()
319-
if is_save_to_root_file[ObjectToSave.Canvas]: canv_cov.Write()
320-
if is_save_to_root_file[ObjectToSave.CorrelationMatrix]: histo_cov.Write()
321-
if is_save_canvas_as_macro[PlotType.Cov]: canv_cov.SaveAs(f"canv_cov_{ipt+1}.C")
353+
if is_save_to_root_file[ObjectToSave.Canvas]:
354+
canv_cov.Write()
355+
if is_save_to_root_file[ObjectToSave.CorrelationMatrix]:
356+
histo_cov.Write()
357+
if is_save_canvas_as_macro[PlotType.Cov]:
358+
canv_cov.SaveAs(f"canv_cov_{ipt+1}.C")
322359
else:
323360
print(f"Minimization for pT {pt_min}, {pt_max} not successful")
324361
hist_minimisation_status.SetBinContent(ipt + 1, MinimisationStatus.Fail)
@@ -327,6 +364,7 @@ def main(config):
327364
canv_frac = ROOT.TCanvas("c_frac_minimization_error", "Minimization error", 500, 500)
328365
canv_cov = ROOT.TCanvas("c_conv_minimization_error", "Minimization error", 500, 500)
329366
canv_unc = ROOT.TCanvas("c_unc_minimization_error", "Minimization error", 500, 500)
367+
canv_rel_unc = ROOT.TCanvas("c_rel_unc_minimization_error", "Minimization error", 500, 500)
330368

331369
canv_combined = ROOT.TCanvas(f"canv_combined_{ipt}", "", 1000, 1000)
332370
canv_combined.Divide(2, 2)
@@ -346,6 +384,7 @@ def main(config):
346384
output_name_frac_pdf = f"Frac_{output_name_template}"
347385
output_name_covmat_pdf = f"CovMatrix_{output_name_template}"
348386
output_name_unc_pdf = f"Unc_{output_name_template}"
387+
output_name_rel_unc_pdf = f"RelUnc_{output_name_template}"
349388
output_name_pdf = f"{output_name_template}"
350389

351390
if hist_rawy[0].GetNbinsX() == 1 or pt_bin_to_process != -1:
@@ -362,6 +401,7 @@ def main(config):
362401
canv_cov.Print(f"{os.path.join(cfg['output']['directory'], output_name_covmat_pdf)}{print_bracket}")
363402
canv_combined.Print(f"{os.path.join(cfg['output']['directory'], output_name_pdf)}{print_bracket}")
364403
canv_unc.Print(f"{os.path.join(cfg['output']['directory'], output_name_unc_pdf)}{print_bracket}")
404+
canv_rel_unc.Print(f"{os.path.join(cfg['output']['directory'], output_name_rel_unc_pdf)}{print_bracket}")
365405

366406
output.cd()
367407
if is_save_to_root_file[ObjectToSave.CorrectedYield]:

‎PWGHF/D2H/Macros/config_cutvar_example.json‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@
6262
"frac": false,
6363
"eff": false,
6464
"cov": false,
65-
"unc": true
65+
"unc": true,
66+
"relunc": true
6667
},
6768
"is_save_canvas_as_macro": {
6869
"rawy": false,
6970
"frac": false,
7071
"eff": false,
7172
"cov": false,
72-
"unc": false
73+
"unc": false,
74+
"relunc": false
7375
},
7476
"is_save_to_root_file": {
7577
"canvas": true,
@@ -95,5 +97,8 @@
9597
"output": {
9698
"directory": ".",
9799
"file": "CutVarDplus_pp13TeV_MB.root"
98-
}
100+
},
101+
"zero_eff_unc": false,
102+
"effp_shift_nsigma": 0,
103+
"effnp_shift_nsigma": 0
99104
}

0 commit comments

Comments
 (0)