Getting a histogram from a tree in pyroot

Hi,

I have a root file with a tree “btree” inside and few branches in that tree, e.g. “bb”. I can open the file and print number of entries in the tree, but don’t how to get the histogram from “bb” and draw it. I guess I am missing one line at the end:

import ROOT

File1 = ROOT.TFile.Open(“test.root” ,“READ”)
btree = File1. btree
entries = btree.GetEntries()
print(“entries:”, entries) # this works
#now i need to get the bb histogram from the btree somehow and the I want to do
bb.Draw()

Thanks for help, seems super basic, but I couldn’t find a way…
Marek

Hi Marek,

One way to plot the histogram is the TBrowser. You can call

rootbrowse /path/to/file.root`

navigate through the branches of the tree and double click bb to get the histogram.

In your Python code, you can use

btree.Draw("bb")

Cheers,
Jakob

Hi Jakob,

thanks, “btree.Draw(“bb”)” works when I run it from the command line, when it’s in the code, it doesn’t do anything, although I get:

Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1

But what I really want is tho have a TH1 in my code, so that I can do something with it (change title and how it looks) and then save it as pdf. So I need to retrieve this as a TH1, how do I do that?

Thanks!
Marek

OK, I found what I needed:

histo1 = ROOT.TH1F(‘histo1’,‘histo1’ ,100 ,-2 ,18)
btree.Project(“histo1”, “bb”)
histo1.Draw()