diff --git a/PWGHF/D2H/Macros/compute_fraction_cutvar.py b/PWGHF/D2H/Macros/compute_fraction_cutvar.py index 0e5ced6f8a5..74432688725 100644 --- a/PWGHF/D2H/Macros/compute_fraction_cutvar.py +++ b/PWGHF/D2H/Macros/compute_fraction_cutvar.py @@ -11,13 +11,13 @@ import json import os import sys +from enum import IntEnum, auto import numpy as np # pylint: disable=import-error import ROOT # pylint: disable=import-error -from enum import IntEnum, auto + sys.path.insert(0, '..') -from cut_variation import CutVarMinimiser -from cut_variation import MinimisationStatus +from cut_variation import CutVarMinimiser, MinimisationStatus from style_formatter import set_object_style # pylint: disable=no-member,too-many-locals,too-many-statements @@ -28,6 +28,7 @@ class PlotType(IntEnum): Frac = auto() Cov = auto() Unc = auto() + RelUnc = auto() N = auto() class ObjectToSave(IntEnum): @@ -54,6 +55,10 @@ def main(config): with open(config, encoding="utf8") as fil: cfg = json.load(fil) + zero_eff_unc = cfg.get("zero_eff_unc", False) + effp_shift_nsigma = cfg.get("effp_shift_nsigma", 0.0) + effnp_shift_nsigma = cfg.get("effnp_shift_nsigma", 0.0) + hist_rawy, hist_effp, hist_effnp = ([] for _ in range(3)) for filename_rawy, filename_eff in zip(cfg["rawyields"]["inputfiles"], cfg["efficiencies"]["inputfiles"]): infile_rawy = ROOT.TFile.Open(os.path.join(cfg["rawyields"]["inputdir"], filename_rawy)) @@ -75,6 +80,12 @@ def main(config): sys.exit(f"\33[31mFatal error: Histogram with efficiency for nonprompt \"{hist_effnp}\" is absent. Exit.\33[0m") hist_effp[-1].SetDirectory(0) hist_effnp[-1].SetDirectory(0) + for i_bin in range(1, hist_effp[-1].GetNbinsX() + 1): + hist_effp[-1].SetBinContent(i_bin, hist_effp[-1].GetBinContent(i_bin) + effp_shift_nsigma*hist_effp[-1].GetBinError(i_bin)) + hist_effnp[-1].SetBinContent(i_bin, hist_effnp[-1].GetBinContent(i_bin) + effnp_shift_nsigma*hist_effnp[-1].GetBinError(i_bin)) + if zero_eff_unc: + hist_effp[-1].SetBinError(i_bin, 0.0) + hist_effnp[-1].SetBinError(i_bin, 0.0) infile_eff.Close() pt_bin_to_process = cfg.get("pt_bin_to_process", -1) @@ -89,6 +100,7 @@ def main(config): is_draw_title[PlotType.Frac] = cfg.get("is_draw_title", {}).get("frac", False) is_draw_title[PlotType.Cov] = cfg.get("is_draw_title", {}).get("cov", False) is_draw_title[PlotType.Unc] = cfg.get("is_draw_title", {}).get("unc", True) + is_draw_title[PlotType.RelUnc] = cfg.get("is_draw_title", {}).get("relunc", True) is_save_canvas_as_macro = [False] * PlotType.N is_save_canvas_as_macro[PlotType.Rawy] = cfg.get("is_save_canvas_as_macro", {}).get("rawy", False) @@ -96,6 +108,7 @@ def main(config): is_save_canvas_as_macro[PlotType.Frac] = cfg.get("is_save_canvas_as_macro", {}).get("frac", False) is_save_canvas_as_macro[PlotType.Cov] = cfg.get("is_save_canvas_as_macro", {}).get("cov", False) is_save_canvas_as_macro[PlotType.Unc] = cfg.get("is_save_canvas_as_macro", {}).get("unc", False) + is_save_canvas_as_macro[PlotType.RelUnc] = cfg.get("is_save_canvas_as_macro", {}).get("relunc", False) is_save_to_root_file = [False] * ObjectToSave.N is_save_to_root_file[ObjectToSave.Canvas] = cfg.get("is_save_to_root_file", {}).get("canvas", True) @@ -205,14 +218,16 @@ def main(config): ) pt_bin_to_process_name_suffix = "" - if pt_bin_to_process != -1: pt_bin_to_process_name_suffix = "_bin_" + str(pt_bin_to_process) + if pt_bin_to_process != -1: + pt_bin_to_process_name_suffix = "_bin_" + str(pt_bin_to_process) output_name_template = cfg['output']['file'].replace(".root", "") + pt_bin_to_process_name_suffix + ".root" output = ROOT.TFile(os.path.join(cfg["output"]["directory"], output_name_template), "recreate") n_sets = len(hist_rawy) pt_axis_title = hist_rawy[0].GetXaxis().GetTitle() for ipt in range(hist_rawy[0].GetNbinsX()): - if pt_bin_to_process !=-1 and ipt+1 != pt_bin_to_process: continue + if pt_bin_to_process !=-1 and ipt+1 != pt_bin_to_process: + continue all_vectors_monotonous = MinimisationStatus.Success pt_min = hist_rawy[0].GetXaxis().GetBinLowEdge(ipt + 1) pt_max = hist_rawy[0].GetXaxis().GetBinUpEdge(ipt + 1) @@ -237,9 +252,9 @@ def main(config): print("\0\33[33mWARNING! main(): the raw yield uncertainties vector is not monotonous. Check the input for stability.\0\33[0m") print(f"raw yield uncertainties vector elements = {unc_rawy}\n") if not (np.all(effp[1:] > effp[:-1]) or np.all(effp[1:] < effp[:-1])): - sys.exit(f"\33[31mFatal error: the prompt efficiency vector is not monotonous. Check the input. Exit.\33[0m") + sys.exit("\33[31mFatal error: the prompt efficiency vector is not monotonous. Check the input. Exit.\33[0m") if not (np.all(effnp[1:] > effnp[:-1]) or np.all(effnp[1:] < effnp[:-1])): - sys.exit(f"\33[31mFatal error: the nonprompt efficiency vector is not monotonous. Check the input. Exit.\33[0m") + sys.exit("\33[31mFatal error: the nonprompt efficiency vector is not monotonous. Check the input. Exit.\33[0m") minimiser = CutVarMinimiser(rawy, effp, effnp, unc_rawy, unc_effp, unc_effnp) status = minimiser.minimise_system(cfg["minimisation"]["correlated"]) @@ -278,47 +293,69 @@ def main(config): hist_bin_title = f"bin # {ipt+1}; {pt_axis_title}#in ({pt_min}; {pt_max})" hist_bin_title_rawy = hist_bin_title if is_draw_title[PlotType.Rawy] else "" - canv_rawy, histos_rawy, leg_r = minimiser.plot_result(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_rawy) + canv_rawy, histos_rawy, _leg_r = minimiser.plot_result(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_rawy) output.cd() - if is_save_to_root_file[ObjectToSave.Canvas]: canv_rawy.Write() + if is_save_to_root_file[ObjectToSave.Canvas]: + canv_rawy.Write() if is_save_to_root_file[ObjectToSave.RawYield]: - for _, hist in histos_rawy.items(): + for _, hist in histos_rawy.values(): hist.Write() - if is_save_canvas_as_macro[PlotType.Rawy]: canv_rawy.SaveAs(f"canv_rawy_{ipt+1}.C") + if is_save_canvas_as_macro[PlotType.Rawy]: + canv_rawy.SaveAs(f"canv_rawy_{ipt+1}.C") hist_bin_title_unc = hist_bin_title if is_draw_title[PlotType.Unc] else "" - canv_unc, histos_unc, leg_unc = minimiser.plot_uncertainties(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_unc) + canv_unc, histos_unc, _leg_unc = minimiser.plot_uncertainties(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_unc) + output.cd() + if is_save_to_root_file[ObjectToSave.Canvas]: + canv_unc.Write() + if is_save_to_root_file[ObjectToSave.Uncertainty]: + for _, hist in histos_unc.values(): + hist.Write() + if is_save_canvas_as_macro[PlotType.Unc]: + canv_unc.SaveAs(f"canv_unc_{ipt+1}.C") + + hist_bin_title_rel_unc = hist_bin_title if is_draw_title[PlotType.RelUnc] else "" + 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) output.cd() - if is_save_to_root_file[ObjectToSave.Canvas]: canv_unc.Write() + if is_save_to_root_file[ObjectToSave.Canvas]: + canv_rel_unc.Write() if is_save_to_root_file[ObjectToSave.Uncertainty]: - for _, hist in histos_unc.items(): + for _, hist in histos_rel_unc.values(): hist.Write() - if is_save_canvas_as_macro[PlotType.Unc]: canv_unc.SaveAs(f"canv_unc_{ipt+1}.C") + if is_save_canvas_as_macro[PlotType.RelUnc]: + canv_rel_unc.SaveAs(f"canv_rel_unc_{ipt+1}.C") hist_bin_title_eff = hist_bin_title if is_draw_title[PlotType.Eff] else "" - canv_eff, histos_eff, leg_e = minimiser.plot_efficiencies(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_eff) + canv_eff, histos_eff, _leg_e = minimiser.plot_efficiencies(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_eff) output.cd() - if is_save_to_root_file[ObjectToSave.Canvas]: canv_eff.Write() + if is_save_to_root_file[ObjectToSave.Canvas]: + canv_eff.Write() if is_save_to_root_file[ObjectToSave.Efficiency]: - for _, hist in histos_eff.items(): + for _, hist in histos_eff.values(): hist.Write() - if is_save_canvas_as_macro[PlotType.Eff]: canv_eff.SaveAs(f"canv_eff_{ipt+1}.C") + if is_save_canvas_as_macro[PlotType.Eff]: + canv_eff.SaveAs(f"canv_eff_{ipt+1}.C") hist_bin_title_frac = hist_bin_title if is_draw_title[PlotType.Frac] else "" - canv_frac, histos_frac, leg_f = minimiser.plot_fractions(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_frac) + canv_frac, histos_frac, _leg_f = minimiser.plot_fractions(f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_frac) output.cd() - if is_save_to_root_file[ObjectToSave.Canvas]: canv_frac.Write() + if is_save_to_root_file[ObjectToSave.Canvas]: + canv_frac.Write() if is_save_to_root_file[ObjectToSave.Fraction]: - for _, hist in histos_frac.items(): + for _, hist in histos_frac.values(): hist.Write() - if is_save_canvas_as_macro[PlotType.Frac]: canv_frac.SaveAs(f"canv_frac_{ipt+1}.C") + if is_save_canvas_as_macro[PlotType.Frac]: + canv_frac.SaveAs(f"canv_frac_{ipt+1}.C") hist_bin_title_cov = hist_bin_title if is_draw_title[PlotType.Cov] else "" canv_cov, histo_cov = minimiser.plot_cov_matrix(True, f"_pt_{pt_min}_to_{pt_max}", hist_bin_title_cov) output.cd() - if is_save_to_root_file[ObjectToSave.Canvas]: canv_cov.Write() - if is_save_to_root_file[ObjectToSave.CorrelationMatrix]: histo_cov.Write() - if is_save_canvas_as_macro[PlotType.Cov]: canv_cov.SaveAs(f"canv_cov_{ipt+1}.C") + if is_save_to_root_file[ObjectToSave.Canvas]: + canv_cov.Write() + if is_save_to_root_file[ObjectToSave.CorrelationMatrix]: + histo_cov.Write() + if is_save_canvas_as_macro[PlotType.Cov]: + canv_cov.SaveAs(f"canv_cov_{ipt+1}.C") else: print(f"Minimization for pT {pt_min}, {pt_max} not successful") hist_minimisation_status.SetBinContent(ipt + 1, MinimisationStatus.Fail) @@ -327,6 +364,7 @@ def main(config): canv_frac = ROOT.TCanvas("c_frac_minimization_error", "Minimization error", 500, 500) canv_cov = ROOT.TCanvas("c_conv_minimization_error", "Minimization error", 500, 500) canv_unc = ROOT.TCanvas("c_unc_minimization_error", "Minimization error", 500, 500) + canv_rel_unc = ROOT.TCanvas("c_rel_unc_minimization_error", "Minimization error", 500, 500) canv_combined = ROOT.TCanvas(f"canv_combined_{ipt}", "", 1000, 1000) canv_combined.Divide(2, 2) @@ -346,6 +384,7 @@ def main(config): output_name_frac_pdf = f"Frac_{output_name_template}" output_name_covmat_pdf = f"CovMatrix_{output_name_template}" output_name_unc_pdf = f"Unc_{output_name_template}" + output_name_rel_unc_pdf = f"RelUnc_{output_name_template}" output_name_pdf = f"{output_name_template}" if hist_rawy[0].GetNbinsX() == 1 or pt_bin_to_process != -1: @@ -362,6 +401,7 @@ def main(config): canv_cov.Print(f"{os.path.join(cfg['output']['directory'], output_name_covmat_pdf)}{print_bracket}") canv_combined.Print(f"{os.path.join(cfg['output']['directory'], output_name_pdf)}{print_bracket}") canv_unc.Print(f"{os.path.join(cfg['output']['directory'], output_name_unc_pdf)}{print_bracket}") + canv_rel_unc.Print(f"{os.path.join(cfg['output']['directory'], output_name_rel_unc_pdf)}{print_bracket}") output.cd() if is_save_to_root_file[ObjectToSave.CorrectedYield]: diff --git a/PWGHF/D2H/Macros/config_cutvar_example.json b/PWGHF/D2H/Macros/config_cutvar_example.json index f13a2d53d7f..d645db1a6d6 100644 --- a/PWGHF/D2H/Macros/config_cutvar_example.json +++ b/PWGHF/D2H/Macros/config_cutvar_example.json @@ -62,14 +62,16 @@ "frac": false, "eff": false, "cov": false, - "unc": true + "unc": true, + "relunc": true }, "is_save_canvas_as_macro": { "rawy": false, "frac": false, "eff": false, "cov": false, - "unc": false + "unc": false, + "relunc": false }, "is_save_to_root_file": { "canvas": true, @@ -95,5 +97,8 @@ "output": { "directory": ".", "file": "CutVarDplus_pp13TeV_MB.root" - } + }, + "zero_eff_unc": false, + "effp_shift_nsigma": 0, + "effnp_shift_nsigma": 0 } diff --git a/PWGHF/D2H/Macros/cut_variation.py b/PWGHF/D2H/Macros/cut_variation.py index 6d45ac25607..8a6c31a96b4 100644 --- a/PWGHF/D2H/Macros/cut_variation.py +++ b/PWGHF/D2H/Macros/cut_variation.py @@ -8,13 +8,15 @@ """ import sys +from enum import IntEnum, auto import numpy as np # pylint: disable=import-error import ROOT # pylint: disable=import-error -from enum import IntEnum, auto + sys.path.insert(0, '..') from style_formatter import set_global_style, set_object_style + class MinimisationStatus(IntEnum): Undefined = 0 Success = auto() @@ -46,13 +48,26 @@ class CutVarMinimiser: def __init__( # pylint: disable=too-many-arguments self, - raw_yields=np.zeros(0), - eff_prompt=np.zeros(0), - eff_nonprompt=np.zeros(0), - unc_raw_yields=np.zeros(0), - unc_eff_prompt=np.zeros(0), - unc_eff_nonprompt=np.zeros(0), + raw_yields=None, + eff_prompt=None, + eff_nonprompt=None, + unc_raw_yields=None, + unc_eff_prompt=None, + unc_eff_nonprompt=None, ): + if raw_yields is None: + raw_yields = np.zeros(0) + if eff_prompt is None: + eff_prompt = np.zeros(0) + if eff_nonprompt is None: + eff_nonprompt = np.zeros(0) + if unc_raw_yields is None: + unc_raw_yields = np.zeros(0) + if unc_eff_prompt is None: + unc_eff_prompt = np.zeros(0) + if unc_eff_nonprompt is None: + unc_eff_nonprompt = np.zeros(0) + self.raw_yields = raw_yields self.eff_prompt = eff_prompt self.eff_nonprompt = eff_nonprompt @@ -161,17 +176,10 @@ def minimise_system(self, correlated=True, precision=1.0e-8, max_iterations=100) ) if correlated and unc_row > 0 and unc_col > 0: - if unc_row < unc_col: - rho = unc_row / unc_col - else: - rho = unc_col / unc_row + self.m_cov_sets[i_row, i_col] = min(unc_row **2, unc_col ** 2) else: - if i_row == i_col: - rho = 1.0 - else: - rho = 0.0 - cov_row_col = rho * unc_row * unc_col - self.m_cov_sets[i_row, i_col] = cov_row_col + self.m_cov_sets[i_row, i_col] = unc_row ** 2 if i_row == i_col else 0.0 + self.m_cov_sets = np.matrix(self.m_cov_sets) try: @@ -580,7 +588,8 @@ def plot_result(self, suffix="", title=""): hist_raw_yield_sum.Draw("histsame") tex = ROOT.TLatex() tex.SetTextSize(0.04) - tex.DrawLatexNDC(0.05, 0.95, title) + tex.SetTextAlign(31) + tex.DrawLatexNDC(0.95, 0.95, title) canvas.Modified() canvas.Update() @@ -636,22 +645,17 @@ def plot_cov_matrix(self, correlated=True, suffix="", title=""): for i_row, unc_row in enumerate(self.unc_raw_yields): for i_col, unc_col in enumerate(self.unc_raw_yields): if correlated and unc_row > 0 and unc_col > 0: - if unc_row < unc_col: - rho = unc_row / unc_col - else: - rho = unc_col / unc_row + rho = min(unc_row / unc_col, unc_col / unc_row) else: - if i_row == i_col: - rho = 1.0 - else: - rho = 0.0 + rho = 1.0 if i_row == i_col else 0.0 hist_corr_matrix.SetBinContent(i_row + 1, i_col + 1, rho) canvas = ROOT.TCanvas(f"cCorrMatrixCutSets{suffix}", "", 500, 500) hist_corr_matrix.Draw("colz") tex = ROOT.TLatex() tex.SetTextSize(0.04) - tex.DrawLatexNDC(0.05, 0.95, title) + tex.SetTextAlign(31) + tex.DrawLatexNDC(0.95, 0.95, title) canvas.Modified() canvas.Update() @@ -743,7 +747,8 @@ def plot_efficiencies(self, suffix="", title=""): leg.Draw() tex = ROOT.TLatex() tex.SetTextSize(0.04) - tex.DrawLatexNDC(0.05, 0.95, title) + tex.SetTextAlign(31) + tex.DrawLatexNDC(0.95, 0.95, title) canvas.Modified() canvas.Update() @@ -828,7 +833,8 @@ def plot_fractions(self, suffix="", title=""): leg.Draw() tex = ROOT.TLatex() tex.SetTextSize(0.04) - tex.DrawLatexNDC(0.05, 0.95, title) + tex.SetTextAlign(31) + tex.DrawLatexNDC(0.95, 0.95, title) canvas.Modified() canvas.Update() @@ -905,7 +911,8 @@ def plot_uncertainties(self, suffix="", title=""): hist_residual_unc.Draw("histsame") tex = ROOT.TLatex() tex.SetTextSize(0.04) - tex.DrawLatexNDC(0.05, 0.95, title) + tex.SetTextAlign(31) + tex.DrawLatexNDC(0.95, 0.95, title) canvas.Modified() canvas.Update() @@ -915,3 +922,97 @@ def plot_uncertainties(self, suffix="", title=""): } return canvas, histos, leg + + + # pylint: disable=no-member + def plot_relative_uncertainties(self, suffix="", title=""): + """ + Helper function to plot uncertainties as a function of cut set + + Parameters + ----------------------------------------------------- + - suffix: str + suffix to be added in the name of the output objects + - title: str + title to be written at the top margin of the output objects + + Returns + ----------------------------------------------------- + - canvas: ROOT.TCanvas + canvas with plot + - histos: dict + dictionary of ROOT.TH1F with relative uncertainties distributions + for raw yield and efficiencies + - leg: ROOT.TLegend + needed otherwise it is destroyed + """ + suffix = suffix.replace(".", "_") + + set_global_style(padleftmargin=0.16, padbottommargin=0.12, padtopmargin=0.075, titleoffsety=1.6) + + hist_raw_yield_rel_unc = ROOT.TH1F( + f"hRawYieldRelUncVsCut{suffix}", + ";cut set;relative unc.", + self.n_sets, + -0.5, + self.n_sets - 0.5, + ) + + hist_eff_prompt_rel_unc = ROOT.TH1F( + f"hEffPromptRelUncVsCut{suffix}", + ";cut set;relative unc.", + self.n_sets, + -0.5, + self.n_sets - 0.5, + ) + + hist_eff_nonprompt_rel_unc = ROOT.TH1F( + f"hEffNonPromptRelUncVsCut{suffix}", + ";cut set;relative unc.", + self.n_sets, + -0.5, + self.n_sets - 0.5, + ) + + for i_bin, (unc_rawy, rawy, unc_eff_prompt, eff_prompt, unc_eff_nonprompt, eff_nonprompt) in enumerate(zip(self.unc_raw_yields, self.raw_yields, self.unc_eff_prompt, self.eff_prompt, self.unc_eff_nonprompt, self.eff_nonprompt)): + hist_raw_yield_rel_unc.SetBinContent(i_bin + 1, unc_rawy / rawy) + hist_eff_prompt_rel_unc.SetBinContent(i_bin+1, unc_eff_prompt / eff_prompt) + hist_eff_nonprompt_rel_unc.SetBinContent(i_bin+1, unc_eff_nonprompt / eff_nonprompt) + + set_object_style(hist_raw_yield_rel_unc, color=ROOT.kBlack, fillstyle=0) + set_object_style(hist_eff_prompt_rel_unc, color=ROOT.kRed + 1, fillstyle=0) + set_object_style(hist_eff_nonprompt_rel_unc, color=ROOT.kAzure + 4, fillstyle=0) + + canvas = ROOT.TCanvas(f"cRelUncVsCut{suffix}", "", 500, 500) + canvas.DrawFrame( + -0.5, + 0.0, + self.n_sets - 0.5, + max(hist_raw_yield_rel_unc.GetMaximum(), hist_eff_prompt_rel_unc.GetMaximum(), hist_eff_nonprompt_rel_unc.GetMaximum()) * 1.2, + ";cut set;relative unc.", + ) + leg = ROOT.TLegend(0.2, 0.75, 0.4, 0.85) + leg.SetBorderSize(0) + leg.SetFillStyle(0) + leg.SetTextSize(0.04) + leg.AddEntry(hist_raw_yield_rel_unc, "raw yield", "l") + leg.AddEntry(hist_eff_prompt_rel_unc, "efficiency prompt", "l") + leg.AddEntry(hist_eff_nonprompt_rel_unc, "efficiency nonprompt", "l") + leg.Draw() + hist_raw_yield_rel_unc.Draw("histsame") + hist_eff_prompt_rel_unc.Draw("histsame") + hist_eff_nonprompt_rel_unc.Draw("histsame") + tex = ROOT.TLatex() + tex.SetTextSize(0.04) + tex.SetTextAlign(31) + tex.DrawLatexNDC(0.95, 0.95, title) + canvas.Modified() + canvas.Update() + + histos = { + "rawy": hist_raw_yield_rel_unc, + "prompt": hist_eff_prompt_rel_unc, + "nonprompt": hist_eff_nonprompt_rel_unc + } + + return canvas, histos, leg