I have a file in cern root format which contains a number of TH1D and TH2D histograms. I'd like to be able to plot these by using uproot4 to read them, then matplotlib to do the plotting.
I can open the file with uproot.open(path) ok, and print(file.keys()) gives a list of the histogram names, but I can't list the histogram contents or convert them to any other meaningful forms.
Can anyone point me to example code?
Asked
Active
Viewed 881 times
0

user17449861
- 9
- 1
1 Answers
1
Following this section of the tutorial, particularly the end, you can do:
import uproot
file = uproot.open("https://scikit-hep.org/uproot3/examples/hepdata-example.root")
file["hpxpy"].to_hist().plot()
plt.show()
The to_hist()
part sends the histogram to the hist library (which must be installed). The hist documentation has sections on installation and plotting.

Jim Pivarski
- 5,568
- 2
- 35
- 47
-
Thanks. That with mplhep has got me working. – user17449861 Nov 19 '21 at 12:07
-
I am curious how you can work with THnSparse instead. It has got my head scratching and I cannot seem to find anything in Uproot for THnSparse. – MycrofD Sep 21 '22 at 13:33
-
There isn't anything in Uproot for THnSparse, which is not a subclass of TH1. There _might_ be enough implemented to read a THnSparse object in Uproot without any convenience methods, which would make it usable but all of the functionality would be hidden in its `all_members` dict. If that's the case, then we should add a [behavior](https://github.com/scikit-hep/uproot5/tree/main/src/uproot/behaviors) for THnSparse to make it convenient to work with, or at least convert to sparse [hist](https://hist.readthedocs.io/) objects. – Jim Pivarski Sep 22 '22 at 14:59