Skip to content

Commit 1bf0789

Browse files
authored
[PWGHF] Cut variation script enhancement (#15342)
1 parent a8d82e9 commit 1bf0789

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed

PWGHF/D2H/Macros/compute_fraction_cutvar.py

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def main(config):
3737
infile_rawy = ROOT.TFile.Open(os.path.join(cfg["rawyields"]["inputdir"], filename_rawy))
3838
hist_rawy_name = cfg["rawyields"]["histoname"]
3939
hist_rawy.append(infile_rawy.Get(hist_rawy_name))
40-
if(hist_rawy[-1] is None):
41-
sys.exit(f"Fatal error: Histogram with raw yield \"{hist_rawy_name}\" is absent. Exit.")
40+
if not isinstance(hist_rawy[-1], ROOT.TH1):
41+
sys.exit(f"\33[31mFatal error: Histogram with raw yield \"{hist_rawy_name}\" is absent. Exit.\33[0m")
4242
hist_rawy[-1].SetDirectory(0)
4343
infile_rawy.Close()
4444

@@ -47,31 +47,43 @@ def main(config):
4747
hist_effnp_name = cfg["efficiencies"]["histonames"]["nonprompt"]
4848
hist_effp.append(infile_eff.Get(hist_effp_name))
4949
hist_effnp.append(infile_eff.Get(hist_effnp_name))
50-
if(hist_effp[-1] is None):
51-
sys.exit(f"Fatal error: Histogram with efficiency for prompt \"{hist_effp_name}\" is absent. Exit.")
52-
if(hist_effnp[-1] is None):
53-
sys.exit(f"Fatal error: Histogram with efficiency for nonprompt \"{hist_effnp}\" is absent. Exit.")
50+
if not isinstance(hist_effp[-1], ROOT.TH1):
51+
sys.exit(f"\33[31mFatal error: Histogram with efficiency for prompt \"{hist_effp_name}\" is absent. Exit.\33[0m")
52+
if not isinstance(hist_effnp[-1], ROOT.TH1):
53+
sys.exit(f"\33[31mFatal error: Histogram with efficiency for nonprompt \"{hist_effnp}\" is absent. Exit.\33[0m")
5454
hist_effp[-1].SetDirectory(0)
5555
hist_effnp[-1].SetDirectory(0)
5656
infile_eff.Close()
5757

5858
pt_bin_to_process = cfg.get("pt_bin_to_process", -1)
5959
if not isinstance(pt_bin_to_process, int):
60-
sys.exit("Fatal error: pt_bin_to_process must be an integer value. Exit.")
60+
sys.exit("\33[31mFatal error: pt_bin_to_process must be an integer value. Exit.")
6161
if (pt_bin_to_process != -1 and pt_bin_to_process < 1) or pt_bin_to_process > hist_rawy[0].GetNbinsX():
62-
sys.exit("Fatal error: pt_bin_to_process must be a positive value up to number of bins in raw yield histogram. Exit.")
62+
sys.exit("\33[31mFatal error: pt_bin_to_process must be a positive value up to number of bins in raw yield histogram. Exit.")
63+
64+
is_draw_title_rawy = cfg.get("is_draw_title", {}).get("rawy", True)
65+
is_draw_title_eff = cfg.get("is_draw_title", {}).get("eff", False)
66+
is_draw_title_frac = cfg.get("is_draw_title", {}).get("frac", False)
67+
is_draw_title_cov = cfg.get("is_draw_title", {}).get("cov", False)
68+
is_draw_title_unc = cfg.get("is_draw_title", {}).get("unc", True)
69+
70+
is_save_canvas_as_macro_rawy = cfg.get("is_save_canvas_as_macro", {}).get("rawy", False)
71+
is_save_canvas_as_macro_eff = cfg.get("is_save_canvas_as_macro", {}).get("eff", False)
72+
is_save_canvas_as_macro_frac = cfg.get("is_save_canvas_as_macro", {}).get("frac", False)
73+
is_save_canvas_as_macro_cov = cfg.get("is_save_canvas_as_macro", {}).get("cov", False)
74+
is_save_canvas_as_macro_unc = cfg.get("is_save_canvas_as_macro", {}).get("unc", False)
6375

6476
if cfg["central_efficiency"]["computerawfrac"]:
6577
infile_name = os.path.join(cfg["central_efficiency"]["inputdir"], cfg["central_efficiency"]["inputfile"])
6678
infile_central_eff = ROOT.TFile.Open(infile_name)
6779
hist_central_effp_name = cfg["central_efficiency"]["histonames"]["prompt"]
6880
hist_central_effp = infile_central_eff.Get(hist_central_effp_name)
69-
if(hist_central_effp is None):
70-
sys.exit(f"Fatal error: Histogram with central efficiency for prompt \"{hist_central_effp_name}\" is absent. Exit.")
81+
if not isinstance(hist_central_effp[-1], ROOT.TH1):
82+
sys.exit(f"\33[31mFatal error: Histogram with central efficiency for prompt \"{hist_central_effp_name}\" is absent. Exit.\33[0m")
7183
hist_central_effnp_name = cfg["central_efficiency"]["histonames"]["nonprompt"]
7284
hist_central_effnp = infile_central_eff.Get(hist_central_effnp_name)
73-
if(hist_central_effnp is None):
74-
sys.exit(f"Fatal error: Histogram with central efficiency for nonprompt \"{hist_central_effnp_name}\" is absent. Exit.")
85+
if not isinstance(hist_central_effnp[-1], ROOT.TH1):
86+
sys.exit(f"\33[31mFatal error: Histogram with central efficiency for nonprompt \"{hist_central_effnp_name}\" is absent. Exit.\33[0m")
7587
hist_central_effp.SetDirectory(0)
7688
hist_central_effnp.SetDirectory(0)
7789
infile_central_eff.Close()
@@ -171,10 +183,10 @@ def main(config):
171183

172184
if cfg["minimisation"]["correlated"]:
173185
if not (np.all(rawy[1:] > rawy[:-1]) or np.all(rawy[1:] < rawy[:-1])):
174-
print("WARNING! main(): the raw yield vector is not monotonous. Check the input for stability.")
186+
print("\0\33[33mWARNING! main(): the raw yield vector is not monotonous. Check the input for stability.\0\33[0m")
175187
print(f"raw yield vector elements = {rawy}\n")
176188
if not (np.all(unc_rawy[1:] > unc_rawy[:-1]) or np.all(unc_rawy[1:] < unc_rawy[:-1])):
177-
print("WARNING! main(): the raw yield uncertainties vector is not monotonous. Check the input for stability.")
189+
print("\0\33[33mWARNING! main(): the raw yield uncertainties vector is not monotonous. Check the input for stability.\0\33[0m")
178190
print(f"raw yield uncertainties vector elements = {unc_rawy}\n")
179191

180192
minimiser = CutVarMinimiser(rawy, effp, effnp, unc_rawy, unc_effp, unc_effnp)
@@ -211,34 +223,49 @@ def main(config):
211223

212224
hist_bin_title = f"bin # {ipt+1}; {pt_axis_title}#in ({pt_min}; {pt_max})"
213225

214-
canv_rawy, histos_rawy, leg_r = minimiser.plot_result(f"_pt{pt_min}_{pt_max}", hist_bin_title)
226+
hist_bin_title_rawy = hist_bin_title if is_draw_title_rawy else ""
227+
canv_rawy, histos_rawy, leg_r = minimiser.plot_result(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_rawy)
215228
output.cd()
216229
canv_rawy.Write()
217230
for _, hist in histos_rawy.items():
218231
hist.Write()
232+
if (is_save_canvas_as_macro_rawy):
233+
canv_rawy.SaveAs(f"canv_rawy_{ipt+1}.C")
219234

220-
canv_unc, histos_unc, leg_unc = minimiser.plot_uncertainties(f"_pt{pt_min}_{pt_max}", hist_bin_title)
235+
hist_bin_title_unc = hist_bin_title if is_draw_title_unc else ""
236+
canv_unc, histos_unc, leg_unc = minimiser.plot_uncertainties(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_unc)
221237
output.cd()
222238
canv_unc.Write()
223239
for _, hist in histos_unc.items():
224240
hist.Write()
241+
if (is_save_canvas_as_macro_unc):
242+
canv_unc.SaveAs(f"canv_unc_{ipt+1}.C")
225243

226-
canv_eff, histos_eff, leg_e = minimiser.plot_efficiencies(f"_pt{pt_min}_{pt_max}", hist_bin_title)
244+
hist_bin_title_eff = hist_bin_title if is_draw_title_eff else ""
245+
canv_eff, histos_eff, leg_e = minimiser.plot_efficiencies(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_eff)
227246
output.cd()
228247
canv_eff.Write()
229248
for _, hist in histos_eff.items():
230249
hist.Write()
250+
if (is_save_canvas_as_macro_eff):
251+
canv_eff.SaveAs(f"canv_eff_{ipt+1}.C")
231252

232-
canv_frac, histos_frac, leg_f = minimiser.plot_fractions(f"_pt{pt_min}_{pt_max}", hist_bin_title)
253+
hist_bin_title_frac = hist_bin_title if is_draw_title_frac else ""
254+
canv_frac, histos_frac, leg_f = minimiser.plot_fractions(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_frac)
233255
output.cd()
234256
canv_frac.Write()
235257
for _, hist in histos_frac.items():
236258
hist.Write()
259+
if (is_save_canvas_as_macro_frac):
260+
canv_frac.SaveAs(f"canv_frac_{ipt+1}.C")
237261

238-
canv_cov, histo_cov = minimiser.plot_cov_matrix(True, f"_pt{pt_min}_{pt_max}", hist_bin_title)
262+
hist_bin_title_cov = hist_bin_title if is_draw_title_cov else ""
263+
canv_cov, histo_cov = minimiser.plot_cov_matrix(True, f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_cov)
239264
output.cd()
240265
canv_cov.Write()
241266
histo_cov.Write()
267+
if (is_save_canvas_as_macro_cov):
268+
canv_cov.SaveAs(f"canv_cov_{ipt+1}.C")
242269
else:
243270
print(f"Minimization for pT {pt_min}, {pt_max} not successful")
244271
canv_rawy = ROOT.TCanvas("c_rawy_minimization_error", "Minimization error", 500, 500)

PWGHF/D2H/Macros/config_cutvar_example.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@
5757
"minimisation": {
5858
"correlated": true
5959
},
60+
"is_draw_title": {
61+
"rawy": true,
62+
"frac": false,
63+
"eff": false,
64+
"cov": false,
65+
"unc": true
66+
},
67+
"is_save_canvas_as_macro": {
68+
"rawy": false,
69+
"frac": false,
70+
"eff": false,
71+
"cov": false,
72+
"unc": false
73+
},
6074
"central_efficiency": {
6175
"computerawfrac": true,
6276
"inputdir": "path/to/central/efficiency",

PWGHF/D2H/Macros/cut_variation.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ def __check_input_consistency(self):
7979
"""
8080

8181
if len(self.eff_prompt) != self.n_sets or len(self.eff_nonprompt) != self.n_sets:
82-
print("ERROR: number of raw yields and efficiencies not consistent! Exit")
82+
print("33[31mERROR: number of raw yields and efficiencies not consistent! Exit\033[0m")
8383
sys.exit()
8484

8585
if len(self.unc_raw_yields) != self.n_sets:
86-
print("ERROR: number of raw yields and raw-yield uncertainties not consistent! Exit")
86+
print("33[31mERROR: number of raw yields and raw-yield uncertainties not consistent! Exit\033[0m")
8787
sys.exit()
8888

8989
if len(self.unc_eff_prompt) != self.n_sets or len(self.unc_eff_nonprompt) != self.n_sets:
90-
print("ERROR: number of raw yields and efficiency uncertainties not consistent! Exit")
90+
print("33[31mERROR: number of raw yields and efficiency uncertainties not consistent! Exit\033[0m")
9191
sys.exit()
9292

9393
def __initialise_objects(self):
@@ -199,7 +199,7 @@ def minimise_system(self, correlated=True, precision=1.0e-8, max_iterations=100)
199199
if correlated:
200200
m_cov_sets_diag = np.diag(self.m_cov_sets)
201201
if not (np.all(m_cov_sets_diag[1:] > m_cov_sets_diag[:-1]) or np.all(m_cov_sets_diag[1:] < m_cov_sets_diag[:-1])):
202-
print("WARNING! minimise_system(): the residual vector uncertainties elements are not monotonous. Check the input for stability.")
202+
print("\033[33mWARNING! minimise_system(): the residual vector uncertainties elements are not monotonous. Check the input for stability.\033[0m")
203203
print(f"residual vector uncertainties elements = {np.sqrt(m_cov_sets_diag)}\n")
204204

205205
# chi2
@@ -484,6 +484,7 @@ def plot_result(self, suffix="", title=""):
484484
- leg: ROOT.TLegend
485485
needed otherwise it is destroyed
486486
"""
487+
suffix = suffix.replace(".", "_")
487488

488489
set_global_style(padleftmargin=0.16, padbottommargin=0.12, padtopmargin=0.075, titleoffsety=1.6)
489490

@@ -604,6 +605,7 @@ def plot_cov_matrix(self, correlated=True, suffix="", title=""):
604605
- hist_corr_matrix: ROOT.TH2F
605606
histogram of correlation matrix
606607
"""
608+
suffix = suffix.replace(".", "_")
607609

608610
set_global_style(
609611
padleftmargin=0.14,
@@ -667,6 +669,7 @@ def plot_efficiencies(self, suffix="", title=""):
667669
- leg: ROOT.TLegend
668670
needed otherwise it is destroyed
669671
"""
672+
suffix = suffix.replace(".", "_")
670673

671674
set_global_style(padleftmargin=0.14, padbottommargin=0.12, titleoffset=1.2, padtopmargin = 0.075)
672675

@@ -758,6 +761,7 @@ def plot_fractions(self, suffix="", title=""):
758761
- leg: ROOT.TLegend
759762
needed otherwise it is destroyed
760763
"""
764+
suffix = suffix.replace(".", "_")
761765

762766
set_global_style(padleftmargin=0.14, padbottommargin=0.12, titleoffset=1.2, padtopmargin = 0.075)
763767

@@ -844,6 +848,7 @@ def plot_uncertainties(self, suffix="", title=""):
844848
- leg: ROOT.TLegend
845849
needed otherwise it is destroyed
846850
"""
851+
suffix = suffix.replace(".", "_")
847852

848853
set_global_style(padleftmargin=0.16, padbottommargin=0.12, padtopmargin=0.075, titleoffsety=1.6)
849854

0 commit comments

Comments
 (0)