# Getting a histogram from a tree in pyroot

**URL:** <https://root-forum.cern.ch/t/getting-a-histogram-from-a-tree-in-pyroot/45936>\
**Category:** Newbie\
**Tags:** python\
**Created:** [July 20, 2021, 4:40pm UTC](https://root-forum.cern.ch/t/getting-a-histogram-from-a-tree-in-pyroot/45936 "2021-07-20T16:40:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwalczak](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwalczak/32/24390_2.png) [@mwalczak](https://root-forum.cern.ch/u/mwalczak)\
**Post date:** [July 20, 2021, 4:40pm UTC](https://root-forum.cern.ch/t/getting-a-histogram-from-a-tree-in-pyroot/45936/1 "2021-07-20T16:40:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [July 20, 2021, 8:04pm UTC](https://root-forum.cern.ch/t/getting-a-histogram-from-a-tree-in-pyroot/45936/2 "2021-07-20T20:04:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mwalczak](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwalczak/32/24390_2.png) [@mwalczak](https://root-forum.cern.ch/u/mwalczak)\
**Post date:** [July 21, 2021, 7:27am UTC](https://root-forum.cern.ch/t/getting-a-histogram-from-a-tree-in-pyroot/45936/3 "2021-07-21T07:27:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mwalczak](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwalczak/32/24390_2.png) [@mwalczak](https://root-forum.cern.ch/u/mwalczak)\
**Post date:** [July 21, 2021, 8:38am UTC](https://root-forum.cern.ch/t/getting-a-histogram-from-a-tree-in-pyroot/45936/4 "2021-07-21T08:38:18Z")

</div>

OK, I found what I needed:

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