Created
November 29, 2024 08:38
-
-
Save ram1123/d6263be1502eaedcc7131a64c13feab5 to your computer and use it in GitHub Desktop.
Print the histogram content like: binNo: label: content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uproot | |
def print_histogram_bins(file_name, hist_name): | |
with uproot.open(file_name) as root_file: | |
hist = root_file[hist_name] | |
bin_contents = hist.values() | |
x_axis = hist.member("fXaxis") | |
if hasattr(x_axis, "labels") and callable(x_axis.labels): | |
bin_labels = x_axis.labels() | |
else: | |
bin_labels = [f"Bin{i}" for i in range(len(bin_contents))] | |
print(f"Histogram: {hist_name}") | |
for i, (content, label) in enumerate(zip(bin_contents, bin_labels)): | |
print(f"Bin: {i} : {label} : {content}") | |
file_name = "skimmed_nano.root" | |
hist_name = "cutFlow" | |
print_histogram_bins(file_name, hist_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment