ATLAS Offline Software
Loading...
Searching...
No Matches
L1TopoRatesCalculator_submatrix_plotter.py
Go to the documentation of this file.
1#!/usr/bin/env athena.py
2# Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3
4import ROOT
5import numpy as np
6import matplotlib.pyplot as plt
7from matplotlib.colors import Normalize
8import argparse
9
10def load_selected_matrix(root_file, matrix_name, selected_vars, with_errors=True):
11 matrix = root_file.Get(matrix_name)
12 if not matrix:
13 raise RuntimeError(f"Matrix '{matrix_name}' not found in ROOT file.")
14
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]
18
19 selected_matrix = np.zeros((len(indices), len(indices)))
20 errors_matrix = np.zeros((len(indices), len(indices))) if with_errors else None
21
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)
25 if with_errors:
26 errors_matrix[i, j] = matrix.GetBinError(ix+1, iy+1)
27
28 return selected_matrix, errors_matrix
29
30def 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)
33
34 fig, ax = plt.subplots(figsize=(46, 44))
35 cax = ax.matshow(selected_matrix, cmap='coolwarm', norm=Normalize(vmin=min_value, vmax=max_value))
36
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}'
41 else:
42 text = f'{val:.2f}'
43 ax.text(j, i, text, ha='center', va='center', color='black', fontsize=25)
44
45 ax.set_xticks(np.arange(len(selected_vars)))
46 ax.set_yticks([])
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')
52
53 colorbar = fig.colorbar(cax)
54 colorbar.ax.tick_params(labelsize=25)
55 colorbar.set_label(colorbar_label, fontsize=25)
56
57 plt.title(title, fontsize=30)
58 plt.savefig(output_name, bbox_inches='tight')
59 plt.close()
60
61if __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()
66
67 file = ROOT.TFile.Open(args.file_name)
68
69 # Plot rates_matrix
70 # Note: change the matrix name with the name of the matrix from the .root file, if ran on the grid your matrix might end with a "_combined"
71 rates, rates_err = load_selected_matrix(file, "rates_matrix_combined", args.selected_vars, with_errors=True)
72 plot_matrix(rates, rates_err, args.selected_vars, "Rates Matrix", "rates_matrix_selected.png", "Hz")
73
74 # Plot counts_matrix (no errors used)
75 counts, _ = load_selected_matrix(file, "counts_matrix_combined", args.selected_vars, with_errors=False)
76 plot_matrix(counts, None, args.selected_vars, "Counts Matrix", "counts_matrix_selected.png", "# Events")
77
78 # Plot L1TopoScore_matrix
79 topo, topo_err = load_selected_matrix(file, "L1TopoScore_matrix_combined", args.selected_vars, with_errors=True)
80 plot_matrix(topo, topo_err, args.selected_vars, "L1Topo Score Matrix", "toposcore_matrix_selected.png", "Topo Score")
81
load_selected_matrix(root_file, matrix_name, selected_vars, with_errors=True)
plot_matrix(selected_matrix, errors_matrix, selected_vars, title, output_name, colorbar_label)