Skip to content

Instantly share code, notes, and snippets.

@ram1123
Created November 29, 2024 08:38
Show Gist options
  • Save ram1123/d6263be1502eaedcc7131a64c13feab5 to your computer and use it in GitHub Desktop.
Save ram1123/d6263be1502eaedcc7131a64c13feab5 to your computer and use it in GitHub Desktop.
Print the histogram content like: binNo: label: content
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