6 import matplotlib.pyplot
as plt
7 from matplotlib.colors
import Normalize
11 matrix = root_file.Get(matrix_name)
13 raise RuntimeError(f
"Matrix '{matrix_name}' not found in ROOT file.")
15 n = matrix.GetNbinsX()
16 all_vars = [matrix.GetXaxis().GetBinLabel(i)
for i
in range(1, n+1)]
17 indices = [all_vars.index(var)
for var
in selected_vars]
19 selected_matrix = np.zeros((len(indices), len(indices)))
20 errors_matrix = np.zeros((len(indices), len(indices)))
if with_errors
else None
22 for i, ix
in enumerate(indices):
23 for j, iy
in enumerate(indices):
24 selected_matrix[i, j] = matrix.GetBinContent(ix+1, iy+1)
26 errors_matrix[i, j] = matrix.GetBinError(ix+1, iy+1)
28 return selected_matrix, errors_matrix
30 def plot_matrix(selected_matrix, errors_matrix, selected_vars, title, output_name, colorbar_label):
31 min_value = np.min(selected_matrix)
32 max_value = np.max(selected_matrix)
34 fig, ax = plt.subplots(figsize=(46, 44))
35 cax = ax.matshow(selected_matrix, cmap=
'coolwarm', norm=Normalize(vmin=min_value, vmax=max_value))
37 for (i, j), val
in np.ndenumerate(selected_matrix):
38 if errors_matrix
is not None:
39 error = errors_matrix[i, j]
40 text = f
'{val:.2f}\n±{error:.2f}'
43 ax.text(j, i, text, ha=
'center', va=
'center', color=
'black', fontsize=25)
45 ax.set_xticks(np.arange(len(selected_vars)))
47 ax.set_xticklabels(selected_vars, rotation=90, fontsize=25)
48 ax.set_yticklabels([])
49 plt.xticks(rotation=45)
50 for tick
in ax.get_xticklabels():
51 tick.set_horizontalalignment(
'left')
53 colorbar = fig.colorbar(cax)
54 colorbar.ax.tick_params(labelsize=25)
55 colorbar.set_label(colorbar_label, fontsize=25)
57 plt.title(title, fontsize=30)
58 plt.savefig(output_name, bbox_inches=
'tight')
61 if __name__ ==
'__main__':
62 parser = argparse.ArgumentParser(description=
'Plot correlation, counts, and L1TopoScore matrices from a ROOT file.')
63 parser.add_argument(
'file_name', type=str, help=
'Path to the ROOT file.')
64 parser.add_argument(
'selected_vars', type=str, nargs=
'+', help=
'List of selected variables.')
65 args = parser.parse_args()
67 file = ROOT.TFile.Open(args.file_name)
71 plot_matrix(rates, rates_err, args.selected_vars,
"Rates Matrix",
"rates_matrix_selected.png",
"Hz")
75 plot_matrix(counts,
None, args.selected_vars,
"Counts Matrix",
"counts_matrix_selected.png",
"# Events")
79 plot_matrix(topo, topo_err, args.selected_vars,
"L1Topo Score Matrix",
"toposcore_matrix_selected.png",
"Topo Score")