5 import matplotlib.pyplot
as plt
6 from matplotlib.colors
import Normalize
11 root_file = ROOT.TFile.Open(file_name)
14 rates_matrix = root_file.Get(matrix_name)
17 n = rates_matrix.GetNbinsX()
18 variables = [rates_matrix.GetXaxis().GetBinLabel(i)
for i
in range(1, n+1)]
21 indices = [variables.index(var)
for var
in selected_vars]
24 selected_matrix = np.zeros((len(indices), len(indices)))
25 errors_matrix = np.zeros((len(indices), len(indices)))
27 for i, ix
in enumerate(indices):
28 for j, iy
in enumerate(indices):
29 selected_matrix[i, j] = rates_matrix.GetBinContent(ix+1, iy+1)
30 errors_matrix[i, j] = rates_matrix.GetBinError(ix+1, iy+1)
32 min_value = np.min(selected_matrix)
33 max_value = np.max(selected_matrix)
36 fig, ax = plt.subplots(figsize=(24, 22))
37 cax = ax.matshow(selected_matrix, cmap=
'coolwarm', norm=Normalize(vmin=min_value, vmax=max_value))
40 for (i, j), val
in np.ndenumerate(selected_matrix):
41 error = errors_matrix[i, j]
42 ax.text(j, i, f
'{val:.2f}\n±{error:.2f}', ha=
'center', va=
'center', color=
'black', fontsize=25)
45 ax.set_xticks(np.arange(len(indices)))
47 ax.set_xticklabels([selected_vars[i]
for i
in range(len(indices))], rotation=90, fontsize=25)
48 ax.set_yticklabels([])
49 plt.xticks(rotation=45)
52 for tick
in ax.get_xticklabels():
53 tick.set_horizontalalignment(
'left')
55 colorbar = fig.colorbar(cax)
56 colorbar.ax.tick_params(labelsize=25)
57 colorbar.set_label(
'Hz', fontsize=25)
60 plt.savefig(output_name, bbox_inches=
'tight')
63 if __name__ ==
'__main__':
65 parser = argparse.ArgumentParser(description=
'Plot a correlation matrix from a ROOT file.')
66 parser.add_argument(
'file_name', type=str, help=
'Path to the ROOT file.')
67 parser.add_argument(
'matrix_name', type=str, help=
'Name of the matrix in the ROOT file.')
68 parser.add_argument(
'selected_vars', type=str, nargs=
'+', help=
'List of selected variables.')
69 parser.add_argument(
'output_name', type=str, help=
'Name of the output PNG file.')
72 args = parser.parse_args()