From 992521f3d360a626e15632a3c17732860ea5f152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Mon, 10 Feb 2025 17:16:42 +0100 Subject: [PATCH 01/13] Plot inclusive jets --- .../plotting/plot_jetsubstructure_run3.py | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py index 7dbba6acfd..9cfb2534c8 100644 --- a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py +++ b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py @@ -694,9 +694,13 @@ def plot(self): if self.species == "D0": # list_iptjet = [0, 1, 2, 3] # indices of jet pt bins to process - list_iptjet = [2, 3] # indices of jet pt bins to process + # list_iptjet = [0, 1, 2, 3, 4, 5, 6, 7] # indices of jet pt bins to process + list_iptjet = [0, 2, 4, 6] # indices of jet pt bins to process + # list_iptjet = [2, 3] # indices of jet pt bins to process if self.species == "Lc": list_iptjet = [1] # indices of jet pt bins to process + if self.species == "incl": + list_iptjet = [2] plot_lc_vs_d0 = True list_stat_all = [] list_syst_all = [] @@ -794,10 +798,11 @@ def plot(self): # Results self.logger.info("Plotting results") - plot_run2_data = True + plot_run2_data = False + # self.list_latex = [ + # self.text_alice, self.list_latex = [ - self.text_alice, - # self.list_latex = [f"{self.text_alice}, {self.text_run3}", + f"{self.text_alice}, {self.text_run3}", self.text_jets, f"{self.get_text_range_ptjet(iptjet)}, {self.text_etajet}", self.get_text_range_pthf(-1, iptjet), @@ -846,24 +851,24 @@ def plot(self): self.title_full = self.title_full_default # Plot additional stuff. - plot_run2_lc_ff_data = True + plot_run2_lc_ff_data = False plot_run2_lc_ff_sim = False - plot_run2_d0_ff_data = True + plot_run2_d0_ff_data = False - plot_run2_d0_sd = True - plot_run2_d0_sd_hf_data = True + plot_run2_d0_sd = False + plot_run2_d0_sd_hf_data = False plot_run2_d0_sd_hf_sim = False plot_run2_d0_sd_incl_data = False plot_run2_d0_sd_incl_sim = False plot_run3_sim = False - plot_run3_d0_sd_hf_sim = True - plot_run3_d0_sd_incl_sim = True + plot_run3_d0_sd_hf_sim = False + plot_run3_d0_sd_incl_sim = False plot_data = True - plot_sim = True - plot_incl = True + plot_sim = False + plot_incl = False # Plot Run 2, Lc, FF, data, 5-7, 7-15, 15-35 GeV/c if ( @@ -1168,7 +1173,7 @@ def plot(self): self.plot_errors_x = False self.list_latex = [ self.text_alice, - f"{self.text_tagged} {self.text_jets}", + self.text_jets, f"{self.get_text_range_pthf(-1, iptjet)}, {self.text_etajet}", ] if not plot_run2_data: @@ -1184,7 +1189,12 @@ def plot(self): self.list_markers = list_markers_all * (1 + int(bool(list_syst_all))) if plot_run2_data: h_run2, g_run2 = None, None - if plot_run2_d0_sd and self.species == "D0" and self.var in ("zg", "rg", "nsd"): + if ( + plot_run2_d0_sd + and self.species == "D0" + and self.var in ("zg", "rg", "nsd") + and string_ptjet == string_range_ptjet((15, 30)) + ): # FIXME h_run2 = run2_d0_sd[self.var]["hf"]["data"]["stat"] g_run2 = run2_d0_sd[self.var]["hf"]["data"]["syst"] if self.var == "nsd": @@ -1218,7 +1228,7 @@ def plot(self): self.labels_obj = [] self.list_latex = [] self.y_margin_up = 0.06 # to fix cropped number on the axis - self.title_full = f";{self.latex_obs};ratio to " + self.title_full = f";{self.latex_obs};ratio to " can, new = self.make_plot( name_can, can=can, pad=2, scale=pad_heights[1], colours=self.list_colours, markers=self.list_markers ) From 407739309853f7d9e4ab834dd280106f2a0e12dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Wed, 12 Feb 2025 15:10:52 +0100 Subject: [PATCH 02/13] Fix colour indices. Crop systematics outside x range. --- .../analysis/do_systematics.py | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/machine_learning_hep/analysis/do_systematics.py b/machine_learning_hep/analysis/do_systematics.py index cea89469e2..4e9f28458b 100644 --- a/machine_learning_hep/analysis/do_systematics.py +++ b/machine_learning_hep/analysis/do_systematics.py @@ -351,7 +351,7 @@ def do_jet_systematics(self, var: str): input_histograms_default.append(input_file_default.Get(name_his)) if not input_histograms_default[iptjet]: self.logger.critical(make_message_notfound(name_his, path_def)) - # self.crop_histogram(input_histograms_default[iptjet], var) + self.crop_histogram(input_histograms_default[iptjet], var) print(f"Default histogram ({range_ptjet[0]} to {range_ptjet[1]})") print_histogram(input_histograms_default[iptjet], self.verbose) # name_eff = f"h_ptjet-pthf_effnew_pr_{string_range_ptjet(range_ptjet)}" @@ -419,7 +419,7 @@ def do_jet_systematics(self, var: str): path_eff_file = path_eff.replace(self.string_default, string_catvar) if not sys_var_histo_eff: self.logger.critical(make_message_notfound(name_eff, path_eff_file)) - # self.crop_histogram(sys_var_histo, var) + self.crop_histogram(sys_var_histo, var) input_histograms_syscatvar.append(sys_var_histo) input_histograms_eff.append(sys_var_histo_eff) print_histogram(sys_var_histo_eff, self.verbose) @@ -515,7 +515,7 @@ def do_jet_systematics(self, var: str): ) ) ) - setup_histogram(input_histograms_sys[iptjet][sys_cat][sys_var], get_colour(nsys + 1)) + setup_histogram(input_histograms_sys[iptjet][sys_cat][sys_var], get_colour(nsys)) input_histograms_sys[iptjet][sys_cat][sys_var].Draw("same") nsys = nsys + 1 @@ -562,7 +562,7 @@ def do_jet_systematics(self, var: str): input_histograms_sys[iptjet][sys_cat][sys_var], self.systematic_varlabels[sys_cat][sys_var], "P" ) setup_histogram( - input_histograms_sys[iptjet][sys_cat][sys_var], get_colour(nsys + 1), get_marker(nsys + 1) + input_histograms_sys[iptjet][sys_cat][sys_var], get_colour(nsys), get_marker(nsys) ) input_histograms_sys[iptjet][sys_cat][sys_var].Draw("same") nsys = nsys + 1 @@ -628,7 +628,7 @@ def do_jet_systematics(self, var: str): histo_ratio[sys_var].SetYTitle("variation/default") histo_ratio[sys_var].Draw() leg_sysvar_ratio.AddEntry(histo_ratio[sys_var], self.systematic_varlabels[sys_cat][sys_var], "P") - setup_histogram(histo_ratio[sys_var], get_colour(nsys + 1), get_marker(nsys + 1)) + setup_histogram(histo_ratio[sys_var], get_colour(nsys), get_marker(nsys)) histo_ratio[sys_var].Draw("same") nsys = nsys + 1 latex = TLatex( @@ -703,8 +703,8 @@ def do_jet_systematics(self, var: str): ) setup_histogram( input_histograms_sys_eff[iptjet][sys_cat][sys_var], - get_colour(nsys + 1), - get_marker(nsys + 1), + get_colour(nsys), + get_marker(nsys), ) input_histograms_sys_eff[iptjet][sys_cat][sys_var].Draw("same") nsys = nsys + 1 @@ -754,7 +754,7 @@ def do_jet_systematics(self, var: str): leg_sysvar_eff_ratio.AddEntry( histo_ratio[sys_var], self.systematic_varlabels[sys_cat][sys_var], "P" ) - setup_histogram(histo_ratio[sys_var], get_colour(nsys + 1), get_marker(nsys + 1)) + setup_histogram(histo_ratio[sys_var], get_colour(nsys), get_marker(nsys)) histo_ratio[sys_var].Draw("same") nsys = nsys + 1 # line = TLine(obs_rec_min, 1, obs_rec_max, 1) @@ -1048,9 +1048,9 @@ def do_jet_systematics(self, var: str): leg_finalwsys = TLegend(0.7, 0.78, 0.85, 0.88) setup_legend(leg_finalwsys) leg_finalwsys.AddEntry(input_histograms_default[iptjet], "data", "P") - # self.crop_histogram(input_histograms_default[iptjet], var) - # self.crop_graph(tgsys[iptjet], var) - setup_histogram(input_histograms_default[iptjet], get_colour(0, 0)) + self.crop_histogram(input_histograms_default[iptjet], var) + self.crop_graph(tgsys[iptjet], var) + setup_histogram(input_histograms_default[iptjet], get_colour(-1)) y_min_g, y_max_g = get_y_window_gr([tgsys[iptjet]]) y_min_h, y_max_h = get_y_window_his([input_histograms_default[iptjet]]) y_min = min(y_min_g, y_min_h) @@ -1071,7 +1071,7 @@ def do_jet_systematics(self, var: str): input_histograms_default[iptjet].SetYTitle(latex_y) input_histograms_default[iptjet].Draw("AXIS") # input_histograms_default[iptjet].Draw("") - setup_tgraph(tgsys[iptjet], get_colour(7, 0)) + setup_tgraph(tgsys[iptjet], get_colour(-1, 2)) tgsys[iptjet].Draw("5") input_histograms_default[iptjet].Draw("SAME") leg_finalwsys.AddEntry(tgsys[iptjet], "syst. unc.", "F") @@ -1122,9 +1122,9 @@ def do_jet_systematics(self, var: str): crelativesys.SetRightMargin(0.25) leg_relativesys = TLegend(0.77, 0.2, 0.95, 0.85) setup_legend(leg_relativesys, textsize=self.fontsize) - # for g in tgsys_cat[iptjet]: - # self.crop_graph(g, var) - # self.crop_histogram(h_default_stat_err[iptjet], var) + for g in tgsys_cat[iptjet]: + self.crop_graph(g, var) + self.crop_histogram(h_default_stat_err[iptjet], var) y_min_g, y_max_g = get_y_window_gr(tgsys_cat[iptjet]) y_min_h, y_max_h = get_y_window_his([h_default_stat_err[iptjet]]) y_min = min(y_min_g, y_min_h) @@ -1137,7 +1137,7 @@ def do_jet_systematics(self, var: str): h_default_stat_err[iptjet].SetMarkerSize(0) leg_relativesys.AddEntry(h_default_stat_err[iptjet], "stat. unc.", "E") for sys_cat in range(self.n_sys_cat): - setup_tgraph(tgsys_cat[iptjet][sys_cat], get_colour(sys_cat + 1, 0)) + setup_tgraph(tgsys_cat[iptjet][sys_cat], get_colour(sys_cat, 0)) tgsys_cat[iptjet][sys_cat].SetTitle("") tgsys_cat[iptjet][sys_cat].SetLineWidth(3) tgsys_cat[iptjet][sys_cat].SetFillStyle(0) @@ -1229,9 +1229,9 @@ def do_jet_systematics(self, var: str): # leg_relativesys_gr = TLegend(.77, .2, 0.95, .85) leg_relativesys_gr = TLegend(0.77 * 9 / 10, 0.5, 0.95, 0.85) # scale for width 900 -> 1000 setup_legend(leg_relativesys_gr, textsize=self.fontsize) - # for g in tgsys_gr[iptjet]: - # self.crop_graph(g, var) - # self.crop_histogram(h_default_stat_err[iptjet], var) + for g in tgsys_gr[iptjet]: + self.crop_graph(g, var) + self.crop_histogram(h_default_stat_err[iptjet], var) y_min_g, y_max_g = get_y_window_gr(tgsys_gr[iptjet]) y_min_h, y_max_h = get_y_window_his([h_default_stat_err[iptjet]]) y_min = min(y_min_g, y_min_h) @@ -1244,7 +1244,7 @@ def do_jet_systematics(self, var: str): h_default_stat_err[iptjet].SetMarkerSize(0) leg_relativesys_gr.AddEntry(h_default_stat_err[iptjet], "stat. unc.", "E") for sys_gr, gr in enumerate(self.systematic_catgroups_list): - setup_tgraph(tgsys_gr[iptjet][sys_gr], get_colour(sys_gr + 1, 0)) + setup_tgraph(tgsys_gr[iptjet][sys_gr], get_colour(sys_gr, 0)) tgsys_gr[iptjet][sys_gr].SetTitle("") tgsys_gr[iptjet][sys_gr].SetLineWidth(3) tgsys_gr[iptjet][sys_gr].SetFillStyle(0) From 4a0846b633dc4e1da7dcf1baae410da43570b059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Wed, 12 Feb 2025 15:21:19 +0100 Subject: [PATCH 03/13] Add number magnitude helper function --- machine_learning_hep/utilities.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/machine_learning_hep/utilities.py b/machine_learning_hep/utilities.py index f70a02b26d..b4fa43ddab 100644 --- a/machine_learning_hep/utilities.py +++ b/machine_learning_hep/utilities.py @@ -1308,18 +1308,23 @@ def format_number_prec(num, prec): return f"{round(num, prec):.{max(0, prec)}f}" +def magnitude(num): + """Decimal magnitude of a number.""" + return math.floor(math.log10(abs(num))) + + def format_value_with_unc(y, e_stat=None, e_syst_plus=None, e_syst_minus=None, n_sig=2): # pylint: disable=invalid-name """Format a value with uncertainties so that the main value is reported with a decimal precision given by the number of significant figures of the smallest uncertainty.""" - mag_y = math.floor(math.log10(y)) + mag_y = magnitude(y) mag_e_stat = mag_y mag_e_syst = mag_y if e_stat: - mag_e_stat = math.floor(math.log10(e_stat)) + mag_e_stat = magnitude(e_stat) if e_syst_plus: if not e_syst_minus: e_syst_minus = e_syst_plus - mag_e_syst = math.floor(math.log10(min(e_syst_plus, e_syst_minus))) + mag_e_syst = magnitude(min(e_syst_plus, e_syst_minus)) mag_y = min(mag_y, mag_e_stat, mag_e_syst) # print(f"Mag stat {mag_e_stat}, sys {mag_e_syst}") prec_y = n_sig - 1 - mag_y From 24daf5e891ede9f64ff313bdfdf1e3937e759ddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Wed, 12 Feb 2025 15:23:28 +0100 Subject: [PATCH 04/13] Set default scale_title=1. in plotting utils --- machine_learning_hep/utilities.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/machine_learning_hep/utilities.py b/machine_learning_hep/utilities.py index b4fa43ddab..7d0720d599 100644 --- a/machine_learning_hep/utilities.py +++ b/machine_learning_hep/utilities.py @@ -641,7 +641,7 @@ def get_markersize(marker: int, size_def=1.5): return size_def -def setup_histogram(hist, colour=1, markerstyle=kOpenCircle, size=1.5, textsize=0.05, scale_title=1.3): +def setup_histogram(hist, colour=1, markerstyle=kOpenCircle, size=1.5, textsize=0.05, scale_title=1.0): hist.SetStats(0) hist.GetXaxis().SetLabelSize(textsize) hist.GetYaxis().SetLabelSize(textsize) @@ -658,7 +658,7 @@ def setup_histogram(hist, colour=1, markerstyle=kOpenCircle, size=1.5, textsize= def setup_tgraph( - tg_, colour=1, markerstyle=kOpenCircle, size=1.5, alphastyle=0.8, fillstyle=1001, textsize=0.05, scale_title=1.3 + tg_, colour=1, markerstyle=kOpenCircle, size=1.5, alphastyle=0.8, fillstyle=1001, textsize=0.05, scale_title=1.0 ): tg_.GetXaxis().SetLabelSize(textsize) tg_.GetYaxis().SetLabelSize(textsize) From bc2cbe5639fcd4380d74b5ba8f10ccf0496719e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Wed, 12 Feb 2025 15:27:22 +0100 Subject: [PATCH 05/13] Set scale_title=1.3 in make_plot --- machine_learning_hep/utilities.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/machine_learning_hep/utilities.py b/machine_learning_hep/utilities.py index 7d0720d599..6db18f23c2 100644 --- a/machine_learning_hep/utilities.py +++ b/machine_learning_hep/utilities.py @@ -840,7 +840,7 @@ def get_my_size(i: int): def plot_graph(graph): setup_tgraph( - graph, get_my_colour(i_obj), get_my_marker(i_obj), get_my_size(i_obj), textsize=(font_size / scale) + graph, get_my_colour(i_obj), get_my_marker(i_obj), get_my_size(i_obj), textsize=(font_size / scale), scale_title=1.3 ) graph.SetTitle(title) graph.GetXaxis().SetLimits(x_min_plot, x_max_plot) @@ -861,7 +861,7 @@ def plot_histogram(histogram): # If nothing has been plotted yet, plot an empty graph to set the exact ranges. if counter_plot_obj == 0: gr = TGraph(histogram) - setup_tgraph(gr, textsize=font_size / scale) + setup_tgraph(gr, textsize=(font_size / scale), scale_title=1.3) gr.SetMarkerSize(0) gr.SetTitle(title) gr.GetXaxis().SetLimits(x_min_plot, x_max_plot) @@ -877,7 +877,7 @@ def plot_histogram(histogram): gr.DrawClone("AP") list_new.append(gr) setup_histogram( - histogram, get_my_colour(i_obj), get_my_marker(i_obj), get_my_size(i_obj), textsize=(font_size / scale) + histogram, get_my_colour(i_obj), get_my_marker(i_obj), get_my_size(i_obj), textsize=(font_size / scale), scale_title=1.3 ) histogram.GetXaxis().SetLimits(x_min_plot, x_max_plot) histogram.GetXaxis().SetRangeUser(x_min_plot, x_max_plot) From db1f25ccf19d2e58c67909a6926a65532992ca83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Thu, 13 Feb 2025 10:35:50 +0100 Subject: [PATCH 06/13] Plot more pt bins --- machine_learning_hep/plotting/plot_jetsubstructure_run3.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py index 9cfb2534c8..12e3c83bb3 100644 --- a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py +++ b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py @@ -695,10 +695,10 @@ def plot(self): if self.species == "D0": # list_iptjet = [0, 1, 2, 3] # indices of jet pt bins to process # list_iptjet = [0, 1, 2, 3, 4, 5, 6, 7] # indices of jet pt bins to process - list_iptjet = [0, 2, 4, 6] # indices of jet pt bins to process + list_iptjet = [2, 3, 4, 5, 6] # indices of jet pt bins to process # list_iptjet = [2, 3] # indices of jet pt bins to process if self.species == "Lc": - list_iptjet = [1] # indices of jet pt bins to process + list_iptjet = [1, 2] # indices of jet pt bins to process if self.species == "incl": list_iptjet = [2] plot_lc_vs_d0 = True @@ -1228,7 +1228,7 @@ def plot(self): self.labels_obj = [] self.list_latex = [] self.y_margin_up = 0.06 # to fix cropped number on the axis - self.title_full = f";{self.latex_obs};ratio to " + self.title_full = f";{self.latex_obs};ratio to " can, new = self.make_plot( name_can, can=can, pad=2, scale=pad_heights[1], colours=self.list_colours, markers=self.list_markers ) From 4188f00325907c9ab2ff59877cefc5187f605379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Thu, 13 Feb 2025 13:52:13 +0100 Subject: [PATCH 07/13] Adjust spacing in ratio to --- machine_learning_hep/plotting/plot_jetsubstructure_run3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py index 12e3c83bb3..46f6f137e8 100644 --- a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py +++ b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py @@ -1228,7 +1228,7 @@ def plot(self): self.labels_obj = [] self.list_latex = [] self.y_margin_up = 0.06 # to fix cropped number on the axis - self.title_full = f";{self.latex_obs};ratio to " + self.title_full = f";{self.latex_obs};ratio to " can, new = self.make_plot( name_can, can=can, pad=2, scale=pad_heights[1], colours=self.list_colours, markers=self.list_markers ) From d7fa0dce0184f4773554cdd49869f80e2f691919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Tue, 11 Mar 2025 13:29:46 +0100 Subject: [PATCH 08/13] Add protection --- machine_learning_hep/plotting/plot_jetsubstructure_run3.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py index 46f6f137e8..1fceca4d4a 100644 --- a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py +++ b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py @@ -99,8 +99,10 @@ def __init__(self, path_input_file: str, path_database_analysis: str, typean: st self.species = None if "D0Jet" in path_database_analysis: self.species = "D0" - if "LcJet" in path_database_analysis: + elif "LcJet" in path_database_analysis: self.species = "Lc" + else: + self.logger.critical("Jet species could not be determined.") with open(path_database_analysis, encoding="utf-8") as file_db: db_analysis = yaml.safe_load(file_db) From fe17125a0a23c5af3953597885d3322779ede329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Fri, 14 Mar 2025 23:51:44 +0100 Subject: [PATCH 09/13] Fixes --- .../plotting/plot_jetsubstructure_run3.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py index 1fceca4d4a..d45c634351 100644 --- a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py +++ b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py @@ -143,7 +143,7 @@ def __init__(self, path_input_file: str, path_database_analysis: str, typean: st # LaTeX string self.latex_hadron = self.db_typean["latexnamehadron"] self.latex_ptjet = "#it{p}_{T}^{jet ch}" - self.latex_pthf = "#it{p}_{T}^{%s} (GeV/#it{c})" % self.latex_hadron + self.latex_pthf = f"#it{{p}}_{{T}}^{{{self.latex_hadron}}} (GeV/#it{{c}})" self.latex_obs = self.db_typean["observables"][self.var]["label"] self.latex_y = self.db_typean["observables"][self.var]["label_y"] @@ -255,7 +255,7 @@ def __init__(self, path_input_file: str, path_database_analysis: str, typean: st # text self.text_alice = "ALICE Preliminary, pp" # preliminaries # self.text_alice = "#bf{ALICE}, pp, #sqrt{#it{s}} = 13.6 TeV" # paper - self.text_tagged = "%s-tagged" % self.latex_hadron + self.text_tagged = f"{self.latex_hadron}-tagged" # self.text_jets = "charged-particle jets, anti-#it{k}_{T}, #it{R} = 0.4" self.text_jets = self.text_tagged + " " + "charged-particle jets, anti-#it{k}_{T}, #it{R} = 0.4" self.text_ptjet = "%g #leq %s (GeV/#it{c}) < %g" @@ -329,7 +329,9 @@ def crop_graph(self, graph, var: str): def get_object(self, name: str, file=None): if file is None: file = self.file_results - if not (obj := file.Get(name)): + if file is None: + self.logger.critical("No result file set") + if not (obj := file.Get(name)): # type: ignore self.logger.fatal(make_message_notfound(name)) if isinstance(obj, TH1): obj.SetDirectory(0) # Decouple the object from the file. @@ -694,15 +696,14 @@ def plot(self): # Results + list_iptjet = [] if self.species == "D0": # list_iptjet = [0, 1, 2, 3] # indices of jet pt bins to process # list_iptjet = [0, 1, 2, 3, 4, 5, 6, 7] # indices of jet pt bins to process list_iptjet = [2, 3, 4, 5, 6] # indices of jet pt bins to process # list_iptjet = [2, 3] # indices of jet pt bins to process - if self.species == "Lc": + elif self.species == "Lc": list_iptjet = [1, 2] # indices of jet pt bins to process - if self.species == "incl": - list_iptjet = [2] plot_lc_vs_d0 = True list_stat_all = [] list_syst_all = [] @@ -1176,7 +1177,7 @@ def plot(self): self.list_latex = [ self.text_alice, self.text_jets, - f"{self.get_text_range_pthf(-1, iptjet)}, {self.text_etajet}", + f"{self.get_text_range_pthf(-1, -1)}, {self.text_etajet}", ] if not plot_run2_data: self.list_latex[0] = f"{self.text_alice}, {self.text_run3}" @@ -1267,7 +1268,7 @@ def plot(self): self.list_latex = [ self.text_alice, self.text_jets, - f"{self.get_text_range_pthf(-1, iptjet)}, {self.text_etajet}", + f"{self.get_text_range_pthf(-1, -1)}, {self.text_etajet}", ] if self.var in ("zg", "rg", "nsd"): self.list_latex.append(self.text_sd) From d9e9595ca30a3de1e29d0caaefa7ff32bc5e7ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Fri, 14 Mar 2025 23:52:13 +0100 Subject: [PATCH 10/13] Add type annotations --- machine_learning_hep/utilities.py | 86 +++++++++++++++++-------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/machine_learning_hep/utilities.py b/machine_learning_hep/utilities.py index 6db18f23c2..1ae2d90d3c 100644 --- a/machine_learning_hep/utilities.py +++ b/machine_learning_hep/utilities.py @@ -439,7 +439,7 @@ def folding(h_input, response_matrix, h_output): return h_output -def get_plot_range(val_min, val_max, margin_min, margin_max, logscale=False): +def get_plot_range(val_min, val_max, margin_min, margin_max, logscale=False) -> tuple[float, float] | tuple[None, None]: """Return the minimum and maximum of the plotting range so that there are margins expressed as fractions of the plotting range.""" k = 1 - margin_min - margin_max @@ -461,7 +461,7 @@ def get_plot_range(val_min, val_max, margin_min, margin_max, logscale=False): return val_min_plot, val_max_plot -def get_x_window_gr(l_gr: list, with_errors=True): +def get_x_window_gr(l_gr: list, with_errors=True) -> tuple[float, float]: """Return the minimum and maximum x value so that all the points of the graphs in the list fit in the range (by default including the error bars).""" @@ -482,7 +482,7 @@ def err_high(graph): return x_min, x_max -def get_x_window_his(l_his: list): +def get_x_window_his(l_his: list) -> tuple[float, float]: """Return the minimum and maximum x value so that all the bins of the histograms in the list fit in the range.""" if not isinstance(l_his, list): @@ -495,7 +495,7 @@ def get_x_window_his(l_his: list): return x_min, x_max -def get_y_window_gr(l_gr: list, with_errors=True, range_x=None): +def get_y_window_gr(l_gr: list, with_errors=True, range_x: list[float] | None = None) -> tuple[float, float]: """Return the minimum and maximum y value so that all the points of the graphs in the list fit in the range (by default including the error bars). Consider only points within range_x if provided.""" @@ -521,7 +521,7 @@ def err_high(graph): return y_min, y_max -def get_y_window_his(l_his: list, with_errors=True, range_x=None): +def get_y_window_his(l_his: list, with_errors=True, range_x: list[float] | None = None) -> tuple[float, float]: """Return the minimum and maximum y value so that all the points of the histograms in the list fit in the range (by default including the error bars). Consider only bins within range_x if provided.""" @@ -745,36 +745,36 @@ def count_graphs(l_obj: list) -> int: return sum(is_graph(o) for o in l_obj) -def make_plot( # pylint: disable=too-many-arguments, too-many-branches, too-many-statements, too-many-locals - name, - can=None, - pad=0, - path=None, - suffix="eps", - title="", - size=None, - margins_c=None, - list_obj=None, - labels_obj=None, - leg_pos=None, - opt_leg_h="P", - opt_leg_g="P", - opt_plot_h="", - opt_plot_g="P0", - offsets_xy=None, - maxdigits=3, - colours=None, - markers=None, - sizes=None, - range_x=None, - range_y=None, - margins_y=None, - with_errors="xy", - logscale=None, - font_size=0.032, - scale=1.0, - plot_order=None, -): +def make_plot( + name: str, + can: TCanvas | None = None, + pad: int = 0, + path: str | None = None, + suffix: str = "eps", + title: str = "", + size: list[int] | None = None, + margins_c: list[float] | None = None, + list_obj: list | None = None, + labels_obj: list[str] | None = None, + leg_pos: list[float] | None = None, + opt_leg_h: list[str] | str = "P", + opt_leg_g: list[str] | str = "P", + opt_plot_h: list[str] | str = "", + opt_plot_g: list[str] | str = "P0", + offsets_xy: list[float] | None = None, + maxdigits: int = 3, + colours: list[int] | None = None, + markers: list[int] | None = None, + sizes: list[int] | None = None, + range_x: list[float] | None = None, + range_y: list[float] | None = None, + margins_y: list[float] | None = None, + with_errors: str = "xy", + logscale: str | None = None, + font_size: float = 0.032, + scale: float = 1.0, + plot_order: list[int] | None = None, +) -> tuple[TCanvas, list]: """ Make a plot with objects from a list (list_obj). Returns a TCanvas and a list of other created ROOT objects. @@ -840,7 +840,12 @@ def get_my_size(i: int): def plot_graph(graph): setup_tgraph( - graph, get_my_colour(i_obj), get_my_marker(i_obj), get_my_size(i_obj), textsize=(font_size / scale), scale_title=1.3 + graph, + get_my_colour(i_obj), + get_my_marker(i_obj), + get_my_size(i_obj), + textsize=(font_size / scale), + scale_title=1.3, ) graph.SetTitle(title) graph.GetXaxis().SetLimits(x_min_plot, x_max_plot) @@ -877,7 +882,12 @@ def plot_histogram(histogram): gr.DrawClone("AP") list_new.append(gr) setup_histogram( - histogram, get_my_colour(i_obj), get_my_marker(i_obj), get_my_size(i_obj), textsize=(font_size / scale), scale_title=1.3 + histogram, + get_my_colour(i_obj), + get_my_marker(i_obj), + get_my_size(i_obj), + textsize=(font_size / scale), + scale_title=1.3, ) histogram.GetXaxis().SetLimits(x_min_plot, x_max_plot) histogram.GetXaxis().SetRangeUser(x_min_plot, x_max_plot) @@ -893,7 +903,7 @@ def plot_latex(latex): if not (isinstance(list_obj, list) and len(list_obj) > 0): print("Error: Empty list of objects") - return None, None + return None, [] list_new = [] # list of created objects that need to exist outside the function if not (isinstance(offsets_xy, list) and len(offsets_xy) == 2): From 03dc3f5de14ebf11ce428a04a9a47cc744701df0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Sat, 15 Mar 2025 00:07:49 +0100 Subject: [PATCH 11/13] Convert to f-strings --- .../analysis/do_systematics.py | 93 +++++-------------- 1 file changed, 25 insertions(+), 68 deletions(-) diff --git a/machine_learning_hep/analysis/do_systematics.py b/machine_learning_hep/analysis/do_systematics.py index 4e9f28458b..0bcb9e0f14 100644 --- a/machine_learning_hep/analysis/do_systematics.py +++ b/machine_learning_hep/analysis/do_systematics.py @@ -210,7 +210,7 @@ def __init__(self, path_database_analysis: str, typean: str): # text self.text_alice = "ALICE Preliminary, pp, #sqrt{#it{s}} = 13.6 TeV" # self.text_alice = "#bf{ALICE}, pp, #sqrt{#it{s}} = 13.6 TeV" - self.text_jets = "%s-tagged charged jets, anti-#it{k}_{T}, #it{R} = 0.4" % self.latex_hadron + self.text_jets = f"{self.latex_hadron}-tagged charged jets, anti-#it{{k}}_{{T}}, #it{{R}} = 0.4" self.text_ptjet = "%g #leq %s < %g GeV/#it{c}, |#it{#eta}_{jet ch}| < 0.5" self.text_pth = "%g #leq #it{p}_{T}^{%s} < %g GeV/#it{c}, |#it{y}_{%s}| < 0.8" self.text_sd = "Soft drop (#it{z}_{cut} = 0.1, #it{#beta} = 0)" @@ -275,7 +275,7 @@ def process(self, list_vars: "list[str]"): """Do systematics for all variables""" for var in list_vars: # self.logger.info("Processing observable %s", var) - print("Processing observable %s" % var) + print(f"Processing observable {var}") self.do_jet_systematics(var) def do_jet_systematics(self, var: str): @@ -397,8 +397,7 @@ def do_jet_systematics(self, var: str): input_histograms_eff = [] for sys_var in range(self.systematic_variations[sys_cat]): print( - "Variation: %s, %s" - % (self.systematic_catnames[sys_cat], self.systematic_varnames[sys_cat][sys_var]) + f"Variation: {self.systematic_catnames[sys_cat]}, {self.systematic_varnames[sys_cat][sys_var]}" ) name_hist_unfold_2d = f"h_ptjet-{var}_{self.method}_unfolded_data_0" if not (hist_unfold := input_files_sys[sys_cat][sys_var].Get(name_hist_unfold_2d)): @@ -426,22 +425,10 @@ def do_jet_systematics(self, var: str): print_histogram(sys_var_histo, self.verbose) if self.debug: print( - "Variation: %s, %s: got histogram %s from file %s" - % ( - self.systematic_catnames[sys_cat], - self.systematic_varnames[sys_cat][sys_var], - name_his, - path_file, - ) + f"Variation: {self.systematic_catnames[sys_cat]}, {self.systematic_varnames[sys_cat][sys_var]}: got histogram {name_his} from file {path_file}" ) print( - "Variation: %s, %s: got efficiency histogram %s from file %s" - % ( - self.systematic_catnames[sys_cat], - self.systematic_varnames[sys_cat][sys_var], - name_eff, - path_eff_file, - ) + f"Variation: {self.systematic_catnames[sys_cat]}, {self.systematic_varnames[sys_cat][sys_var]}: got efficiency histogram {name_eff} from file {path_eff_file}" ) input_histograms_syscat.append(input_histograms_syscatvar) input_histograms_syscat_eff.append(input_histograms_eff) @@ -457,7 +444,7 @@ def do_jet_systematics(self, var: str): # plot all the variations together suffix = self.get_suffix_ptjet(iptjet) nsys = 0 - csysvar = TCanvas("csysvar_%s" % suffix, "systematic variations" + suffix) + csysvar = TCanvas(f"csysvar_{suffix}", "systematic variations" + suffix) setup_canvas(csysvar) leg_sysvar = TLegend(0.75, 0.15, 0.95, 0.85, "variation") setup_legend(leg_sysvar) @@ -482,38 +469,25 @@ def do_jet_systematics(self, var: str): print_histogram(input_histograms_default[iptjet], self.verbose) self.logger.info("Categories: %d", self.n_sys_cat) - print("Categories: %d" % self.n_sys_cat) + print(f"Categories: {self.n_sys_cat}") for sys_cat in range(self.n_sys_cat): self.logger.info("Category: %s", self.systematic_catlabels[sys_cat]) - print("Category: %s" % self.systematic_catlabels[sys_cat]) + print(f"Category: {self.systematic_catlabels[sys_cat]}") for sys_var in range(self.systematic_variations[sys_cat]): self.logger.info("Variation: %s", self.systematic_varlabels[sys_cat][sys_var]) - print("Variation: %s" % self.systematic_varlabels[sys_cat][sys_var]) + print(f"Variation: {self.systematic_varlabels[sys_cat][sys_var]}") leg_sysvar.AddEntry( input_histograms_sys[iptjet][sys_cat][sys_var], - ( - "{}, {}".format( - self.systematic_catlabels[sys_cat], self.systematic_varlabels[sys_cat][sys_var] - ) - ), + (f"{self.systematic_catlabels[sys_cat]}, {self.systematic_varlabels[sys_cat][sys_var]}"), "P", ) self.logger.info( "Adding label %s", - ( - "{}, {}".format( - self.systematic_catlabels[sys_cat], self.systematic_varlabels[sys_cat][sys_var] - ) - ), + (f"{self.systematic_catlabels[sys_cat]}, {self.systematic_varlabels[sys_cat][sys_var]}"), ) print( - "Adding label %s" - % ( - "{}, {}".format( - self.systematic_catlabels[sys_cat], self.systematic_varlabels[sys_cat][sys_var] - ) - ) + f"Adding label {self.systematic_catlabels[sys_cat]}, {self.systematic_varlabels[sys_cat][sys_var]}" ) setup_histogram(input_histograms_sys[iptjet][sys_cat][sys_var], get_colour(nsys)) input_histograms_sys[iptjet][sys_cat][sys_var].Draw("same") @@ -522,8 +496,7 @@ def do_jet_systematics(self, var: str): latex = TLatex( 0.15, 0.82, - "%g #leq %s < %g GeV/#it{c}" - % (self.edges_ptjet_gen_min[iptjet], self.latex_ptjet, self.edges_ptjet_gen_max[iptjet]), + f"{self.edges_ptjet_gen_min[iptjet]:g} #leq {self.latex_ptjet} < {self.edges_ptjet_gen_max[iptjet]:g} GeV/#it{{c}}", ) draw_latex(latex) # leg_sysvar.Draw("same") @@ -561,16 +534,10 @@ def do_jet_systematics(self, var: str): leg_sysvar_each.AddEntry( input_histograms_sys[iptjet][sys_cat][sys_var], self.systematic_varlabels[sys_cat][sys_var], "P" ) - setup_histogram( - input_histograms_sys[iptjet][sys_cat][sys_var], get_colour(nsys), get_marker(nsys) - ) + setup_histogram(input_histograms_sys[iptjet][sys_cat][sys_var], get_colour(nsys), get_marker(nsys)) input_histograms_sys[iptjet][sys_cat][sys_var].Draw("same") nsys = nsys + 1 - latex_text = "{:g} #leq {} < {:g} GeV/#it{{c}}".format( - self.edges_ptjet_gen_min[iptjet], - self.latex_ptjet, - self.edges_ptjet_gen_max[iptjet], - ) + latex_text = f"{self.edges_ptjet_gen_min[iptjet]:g} #leq {self.latex_ptjet} < {self.edges_ptjet_gen_max[iptjet]:g} GeV/#it{{c}}" latex = TLatex( 0.15, 0.82, @@ -634,8 +601,7 @@ def do_jet_systematics(self, var: str): latex = TLatex( 0.15, 0.82, - "%g #leq %s < %g GeV/#it{c}" - % (self.edges_ptjet_gen_min[iptjet], self.latex_ptjet, self.edges_ptjet_gen_max[iptjet]), + f"{self.edges_ptjet_gen_min[iptjet]:g} #leq {self.latex_ptjet} < {self.edges_ptjet_gen_max[iptjet]:g} GeV/#it{{c}}", ) draw_latex(latex) # line = TLine(obs_rec_min, 1, obs_rec_max, 1) @@ -693,8 +659,8 @@ def do_jet_systematics(self, var: str): *get_plot_range(y_min, y_max, y_margin_down, y_margin_up) ) eff_default[iptjet].SetTitle("") - eff_default[iptjet].SetXTitle("#it{p}_{T}^{%s} (GeV/#it{c})" % self.latex_hadron) - eff_default[iptjet].SetYTitle("prompt %s-jet efficiency" % self.latex_hadron) + eff_default[iptjet].SetXTitle(f"#it{{p}}_{{T}}^{{{self.latex_hadron}}} (GeV/#it{{c}})") + eff_default[iptjet].SetYTitle(f"prompt {self.latex_hadron}-jet efficiency") eff_default[iptjet].Draw() leg_sysvar_eff.AddEntry( input_histograms_sys_eff[iptjet][sys_cat][sys_var], @@ -711,8 +677,7 @@ def do_jet_systematics(self, var: str): latex = TLatex( 0.15, 0.82, - "%g #leq %s < %g GeV/#it{c}" - % (self.edges_ptjet_gen_min[iptjet], self.latex_ptjet, self.edges_ptjet_gen_max[iptjet]), + f"{self.edges_ptjet_gen_min[iptjet]:g} #leq {self.latex_ptjet} < {self.edges_ptjet_gen_max[iptjet]:g} GeV/#it{{c}}", ) draw_latex(latex) leg_sysvar_eff.Draw("same") @@ -763,8 +728,7 @@ def do_jet_systematics(self, var: str): latex = TLatex( 0.15, 0.82, - "%g #leq %s < %g GeV/#it{c}" - % (self.edges_ptjet_gen_min[iptjet], self.latex_ptjet, self.edges_ptjet_gen_max[iptjet]), + f"{self.edges_ptjet_gen_min[iptjet]:g} #leq {self.latex_ptjet} < {self.edges_ptjet_gen_max[iptjet]:g} GeV/#it{{c}}", ) draw_latex(latex) leg_sysvar_eff_ratio.Draw("same") @@ -1003,14 +967,14 @@ def do_jet_systematics(self, var: str): suffix = self.get_suffix_ptjet(iptjet) self.file_sys_out.WriteObject(tgsys[iptjet], f"sys_{var}_{suffix}") unc_hist_up = TH1F( - "unc_hist_up_%s" % suffix, + f"unc_hist_up_{suffix}", "", n_bins_obs_gen, obs_gen_min, obs_gen_max, ) unc_hist_down = TH1F( - "unc_hist_down_%s" % suffix, + f"unc_hist_down_{suffix}", "", n_bins_obs_gen, obs_gen_min, @@ -1079,25 +1043,18 @@ def do_jet_systematics(self, var: str): latex = TLatex(0.15, 0.82, self.text_alice) # latex = TLatex(0.15, 0.82, "pp, #sqrt{#it{s}} = 13.6 TeV") draw_latex(latex) - latex1 = TLatex(0.15, 0.77, "%s in charged jets, anti-#it{k}_{T}, #it{R} = 0.4" % self.latex_hadron) + latex1 = TLatex(0.15, 0.77, f"{self.latex_hadron} in charged jets, anti-#it{{k}}_{{T}}, #it{{R}} = 0.4") draw_latex(latex1) latex2 = TLatex( 0.15, 0.72, - "%g #leq %s < %g GeV/#it{c}, #left|#it{#eta}_{jet}#right| #leq 0.5" - % (self.edges_ptjet_rec[iptjet], self.latex_ptjet, self.edges_ptjet_rec[iptjet + 1]), + f"{self.edges_ptjet_rec[iptjet]:g} #leq {self.latex_ptjet} < {self.edges_ptjet_rec[iptjet + 1]:g} GeV/#it{{c}}, #left|#it{{#eta}}_{{jet}}#right| #leq 0.5", ) draw_latex(latex2) latex3 = TLatex( 0.15, 0.67, - "%g #leq #it{p}_{T}^{%s} < %g GeV/#it{c}, #left|#it{y}_{%s}#right| #leq 0.8" - % ( - self.edges_pthf_min[0], - self.latex_hadron, - min(self.edges_pthf_max[-1], self.edges_ptjet_rec[iptjet + 1]), - self.latex_hadron, - ), + f"{self.edges_pthf_min[0]:g} #leq #it{{p}}_{{T}}^{{{self.latex_hadron}}} < {min(self.edges_pthf_max[-1], self.edges_ptjet_rec[iptjet + 1]):g} GeV/#it{{c}}, #left|#it{{y}}_{{{self.latex_hadron}}}#right| #leq 0.8", ) draw_latex(latex3) leg_finalwsys.Draw("same") From 9319df2e0cae7b3c75e7113d4ddba5da5b8ff8c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Thu, 27 Mar 2025 19:54:04 +0100 Subject: [PATCH 12/13] Ruff: Silence Pylint too-many-... errors --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 2bd0212ca3..609fed85cf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,6 +41,7 @@ extend-select = [ # in addition to defaults ["E4", "E7", "E9", "F"], see https:/ ] ignore = [ "PD901", # Avoid using the generic variable name `df` for DataFrames + "PLR09", # Too many ... ] [project] From 61661929e1f96dd9e63ca887be626e23faf30f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ku=C4=8Dera?= Date: Thu, 27 Mar 2025 20:15:58 +0100 Subject: [PATCH 13/13] Lint --- .../analysis/do_systematics.py | 42 ++++++++----------- .../plotting/plot_jetsubstructure_run3.py | 14 +++---- machine_learning_hep/utilities.py | 38 +++++++---------- 3 files changed, 37 insertions(+), 57 deletions(-) diff --git a/machine_learning_hep/analysis/do_systematics.py b/machine_learning_hep/analysis/do_systematics.py index 0bcb9e0f14..273573589f 100644 --- a/machine_learning_hep/analysis/do_systematics.py +++ b/machine_learning_hep/analysis/do_systematics.py @@ -15,9 +15,6 @@ Author: Vit Kucera """ -# pylint: disable=too-many-lines, too-many-instance-attributes, too-many-statements, too-many-locals -# pylint: disable=too-many-nested-blocks, too-many-branches, consider-using-f-string - import argparse import logging import os @@ -94,7 +91,7 @@ def __init__(self, path_database_analysis: str, typean: str): with open(path_database_analysis, encoding="utf-8") as file_in: db_analysis = yaml.safe_load(file_in) - case = list(db_analysis.keys())[0] + case = next(iter(db_analysis.keys())) self.datap = db_analysis[case] self.db_typean = self.datap["analysis"][self.typean] @@ -790,28 +787,21 @@ def do_jet_systematics(self, var: str): count_sys_up = count_sys_up + 1 else: error_var_up = max(error_var_up, error) - else: - if self.systematic_rms[sys_cat] is True: - if self.systematic_rms_both_sides[sys_cat] is True: - error_var_up += error * error - if not out_sys: - count_sys_up = count_sys_up + 1 - else: - error_var_down += error * error - if not out_sys: - count_sys_down = count_sys_down + 1 + elif self.systematic_rms[sys_cat] is True: + if self.systematic_rms_both_sides[sys_cat] is True: + error_var_up += error * error + if not out_sys: + count_sys_up = count_sys_up + 1 else: - error_var_down = max(error_var_down, abs(error)) - if self.systematic_rms[sys_cat] is True: - if count_sys_up != 0: - error_var_up = error_var_up / count_sys_up + error_var_down += error * error + if not out_sys: + count_sys_down = count_sys_down + 1 else: - error_var_up = 0.0 + error_var_down = max(error_var_down, abs(error)) + if self.systematic_rms[sys_cat] is True: + error_var_up = error_var_up / count_sys_up if count_sys_up != 0 else 0.0 error_var_up = sqrt(error_var_up) - if count_sys_down != 0: - error_var_down = error_var_down / count_sys_down - else: - error_var_down = 0.0 + error_var_down = error_var_down / count_sys_down if count_sys_down != 0 else 0.0 if self.systematic_rms_both_sides[sys_cat] is True: error_var_down = error_var_up else: @@ -918,7 +908,8 @@ def do_jet_systematics(self, var: str): shapebins_error_down_cat = [] for ibinshape in range(n_bins_obs_gen): shapebins_contents_cat.append(0) - if abs(input_histograms_default[iptjet].GetBinContent(ibinshape + 1)) < 1.0e-7: + epsilon_float = 1.0e-7 + if abs(input_histograms_default[iptjet].GetBinContent(ibinshape + 1)) < epsilon_float: print("WARNING!!! Input histogram at bin", iptjet, " equal 0", suffix) e_up = 0 e_down = 0 @@ -992,7 +983,8 @@ def do_jet_systematics(self, var: str): suffix = self.get_suffix_ptjet(iptjet) h_default_stat_err.append(input_histograms_default[iptjet].Clone("h_default_stat_err" + suffix)) for i in range(h_default_stat_err[iptjet].GetNbinsX()): - if abs(input_histograms_default[iptjet].GetBinContent(i + 1)) < 1.0e-7: + epsilon_float = 1.0e-7 + if abs(input_histograms_default[iptjet].GetBinContent(i + 1)) < epsilon_float: print("WARNING!!! Input histogram at bin", iptjet, " equal 0", suffix) h_default_stat_err[iptjet].SetBinContent(i + 1, 0) h_default_stat_err[iptjet].SetBinError(i + 1, 0) diff --git a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py index d45c634351..abaf202aab 100644 --- a/machine_learning_hep/plotting/plot_jetsubstructure_run3.py +++ b/machine_learning_hep/plotting/plot_jetsubstructure_run3.py @@ -15,10 +15,6 @@ Author: Vit Kucera """ -# pylint: disable=too-many-lines, too-many-instance-attributes, too-many-statements, too-many-locals -# pylint: disable=too-many-nested-blocks, too-many-branches, consider-using-f-string -# pylint: disable=unused-variable - import argparse import logging import os @@ -106,7 +102,7 @@ def __init__(self, path_input_file: str, path_database_analysis: str, typean: st with open(path_database_analysis, encoding="utf-8") as file_db: db_analysis = yaml.safe_load(file_db) - case = list(db_analysis.keys())[0] + case = next(iter(db_analysis.keys())) self.datap = db_analysis[case] self.db_typean = self.datap["analysis"][self.typean] @@ -406,7 +402,7 @@ def get_run3_sim(self) -> dict: source = {"monash": "M", "mode2": "SM2"} dict_obj = {} with TFile.Open(path_file) as file: - for s_obs, obs in obs.items(): + for s_obs, n_obs in obs.items(): dict_obj[s_obs] = {} for s_spec, spec in species.items(): dict_obj[s_obs][s_spec] = {} @@ -418,7 +414,7 @@ def get_run3_sim(self) -> dict: name = pattern % ( spec, src, - obs, + n_obs, self.edges_ptjet_gen[iptjet], self.edges_ptjet_gen[iptjet + 1], ) @@ -681,7 +677,7 @@ def plot(self): self.make_plot(f"{self.species}_efficiency_{self.var}") bins_ptjet = (0, 1, 2, 3) - for cat, label in zip(("pr", "np"), ("prompt", "non-prompt")): + for cat, label in zip(("pr", "np"), ("prompt", "non-prompt"), strict=False): self.list_obj = self.get_objects( *( f"h_ptjet-pthf_effnew_{cat}_{string_range_ptjet(get_bin_limits(axis_ptjet, iptjet + 1))}" @@ -1206,7 +1202,7 @@ def plot(self): # TODO: if plot_run2_lc_ff_data if h_run2 is not None: n_obj = len(self.list_obj) - self.plot_order = list(range(n_obj)) + [-1, -0.5] + self.plot_order = [*list(range(n_obj)), -1, -0.5] self.list_obj += [g_run2, h_run2] self.labels_obj += [f"{self.text_run2}, {self.get_text_range_ptjet(2)}", ""] self.list_colours += [get_colour(-1)] * 2 diff --git a/machine_learning_hep/utilities.py b/machine_learning_hep/utilities.py index 1ae2d90d3c..9919c0fee0 100644 --- a/machine_learning_hep/utilities.py +++ b/machine_learning_hep/utilities.py @@ -128,10 +128,7 @@ def write_df(dfo, path): def read_df(path, **kwargs): try: - if path.endswith(".parquet"): - df = pd.read_parquet(path, **kwargs) - else: - df = pickle.load(openfile(path, "rb")) + df = pd.read_parquet(path, **kwargs) if path.endswith(".parquet") else pickle.load(openfile(path, "rb")) except Exception as e: # pylint: disable=broad-except logger.critical("failed to open file <%s>: %s", path, str(e)) sys.exit() @@ -294,12 +291,12 @@ def make_latex_table(column_names, row_names, rows, caption=None, save_path="./t columns = "|".join(["c"] * (len(column_names) + 1)) f.write("\\begin{tabular}{" + columns + "}\n") f.write("\\hline\n") - columns = "&".join([""] + column_names) + columns = "&".join(["", *column_names]) columns = columns.replace("_", "\\_") f.write(columns + "\\\\\n") f.write("\\hline\\hline\n") - for rn, row in zip(row_names, rows): - row_string = "&".join([rn] + row) + for rn, row in zip(row_names, rows, strict=False): + row_string = "&".join([rn, *row]) row_string = row_string.replace("_", "\\_") f.write(row_string + "\\\\\n") f.write("\\end{tabular}\n") @@ -349,12 +346,12 @@ def make_message_notfound(name, location=None): def z_calc(pt_1, phi_1, eta_1, pt_2, phi_2, eta_2): - np_pt_1 = pt_1.values - np_pt_2 = pt_2.values - np_phi_1 = phi_1.values - np_phi_2 = phi_2.values - np_eta_1 = eta_1.values - np_eta_2 = eta_2.values + np_pt_1 = pt_1.to_numpy() + np_pt_2 = pt_2.to_numpy() + np_phi_1 = phi_1.to_numpy() + np_phi_2 = phi_2.to_numpy() + np_eta_1 = eta_1.to_numpy() + np_eta_2 = eta_2.to_numpy() cos_phi_1 = np.cos(np_phi_1) cos_phi_2 = np.cos(np_phi_2) @@ -397,10 +394,7 @@ def equal_axis_list(axis1, list2, precision=10): bins = get_bins(axis1) if len(bins) != len(list2): return False - for i, j in zip(bins, list2): - if round(i, precision) != round(j, precision): - return False - return True + return all(round(i, precision) == round(j, precision) for i, j in zip(bins, list2, strict=False)) def equal_binning(his1, his2): @@ -418,9 +412,7 @@ def equal_binning_lists(his, list_x=None, list_y=None, list_z=None): return False if list_y is not None and not equal_axis_list(his.GetYaxis(), list_y): return False - if list_z is not None and not equal_axis_list(his.GetZaxis(), list_z): - return False - return True + return not (list_z is not None and not equal_axis_list(his.GetZaxis(), list_z)) def folding(h_input, response_matrix, h_output): @@ -939,13 +931,13 @@ def plot_latex(latex): # set canvas margins if isinstance(margins_c, list) and len(margins_c) > 0: for setter, value in zip( - [can.SetBottomMargin, can.SetLeftMargin, can.SetTopMargin, can.SetRightMargin], margins_c + [can.SetBottomMargin, can.SetLeftMargin, can.SetTopMargin, can.SetRightMargin], margins_c, strict=False ): setter(value) # set logarithmic scale for selected axes log_y = False if isinstance(logscale, str) and len(logscale) > 0: - for setter, axis in zip([can.SetLogx, can.SetLogy, can.SetLogz], ["x", "y", "z"]): + for setter, axis in zip([can.SetLogx, can.SetLogy, can.SetLogz], ["x", "y", "z"], strict=False): if axis in logscale: setter() if axis == "y": @@ -1349,7 +1341,7 @@ def format_value_with_unc(y, e_stat=None, e_syst_plus=None, e_syst_minus=None, n if str_e_syst_plus == str_e_syst_minus: str_value += f" ± {str_e_syst_plus} (syst.)" else: - str_value += f" +{str_e_syst_plus} −{str_e_syst_minus} (syst.)" + str_value += f" +{str_e_syst_plus} −{str_e_syst_minus} (syst.)" # noqa: RUF001 return str_value