# Filling a histogram with a ntuple data \[PyROOT\]

**URL:** <https://root-forum.cern.ch/t/filling-a-histogram-with-a-ntuple-data-pyroot/46416>\
**Category:** Newbie\
**Created:** [August 24, 2021, 2:49am UTC](https://root-forum.cern.ch/t/filling-a-histogram-with-a-ntuple-data-pyroot/46416 "2021-08-24T02:49:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cesar1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@Cesar1](https://root-forum.cern.ch/u/Cesar1)\
**Post date:** [August 24, 2021, 2:49am UTC](https://root-forum.cern.ch/t/filling-a-histogram-with-a-ntuple-data-pyroot/46416/1 "2021-08-24T02:49:41Z")

</div>

Dear experts,

I am trying to take the data from a ntuple and fill a histogram with it using pyroot. Everything that i found until now is on c++, and i wrote my script based on that. But i am having trouble with the .SetBranchAddres(), i dont know how to implement it in python. The code that i wrote is down below:

```auto
histFile = root.TFile.Open(histFileName, "READ")
tree = histFile.Get("T_s2thh_NOMINAL")
nLeafs = tree.GetListOfBranches()
histo = root.TH1F("histo", "My histogram", 100, -3, 3)
tree.SetBranchAddres("TauEta", &TauEta)
entries = tree.GetEntries()
for k in range(entries):
    tree.GetEntry(k)
    histo.Fill(TauEta)
histo.Write()

```

And i keep getting the error:

> SyntaxError: invalid syntax

and we i dont use & before TauEta,i get:

> AttributeError: ‘TTree’ object has no attribute ‘SetBranchAddres’

And here is how the .root file was configured:

 ![Screenshot from 2021-08-23 23-42-35](https://root-forum.cern.ch/uploads/default/original/3X/5/6/56199edea6f35ffb168e941e44d7b8d23245a4f0.png)

I really would appreciate any tips or some indication of materials.

Best Regards,  
Caio.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 24, 2021, 5:03am UTC](https://root-forum.cern.ch/t/filling-a-histogram-with-a-ntuple-data-pyroot/46416/2 "2021-08-24T05:03:10Z")

</div>

Address

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 24, 2021, 7:36am UTC](https://root-forum.cern.ch/t/filling-a-histogram-with-a-ntuple-data-pyroot/46416/3 "2021-08-24T07:36:49Z")

</div>

Hi,  
as @dastudillo points out there is a typo: `SetBranchAddres` instead of `SetBranchAddress`.

The next problem will be that `&TauEta` is not a valid Python construct (you need `ROOT.addressof(TauEta)`).

The final problem will be that `for k in range(entries): tree.GetEntry(k)` will be _extremely_ slow (because the event loop will run at Python speed rather than C++ speed).

I can recommend to use [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) instead which simplifies the task. Plotting a histogram of `"TauEta"` from tree "`T_s2thh_NOMINAL"` is very simple with RDataFrame:

```python
df = ROOT.RDataFrame("T_s2thh_NOMINAL", histFileName)
h = df.Histo1D("TauEta")
h.Draw()

```

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Cesar1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@Cesar1](https://root-forum.cern.ch/u/Cesar1)\
**Post date:** [August 24, 2021, 3:45pm UTC](https://root-forum.cern.ch/t/filling-a-histogram-with-a-ntuple-data-pyroot/46416/4 "2021-08-24T15:45:45Z")

</div>

Thank you so much, it worked! Just another question, there is a way to loop over the branches in the df object?

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 24, 2021, 4:07pm UTC](https://root-forum.cern.ch/t/filling-a-histogram-with-a-ntuple-data-pyroot/46416/5 "2021-08-24T16:07:49Z")

</div>

`df.GetColumnNames()` gives you a list of available columns, if that’s what you are looking for.

---

<div class="post-metadata">

**Author:** ![Cesar1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@Cesar1](https://root-forum.cern.ch/u/Cesar1)\
**Post date:** [August 24, 2021, 4:13pm UTC](https://root-forum.cern.ch/t/filling-a-histogram-with-a-ntuple-data-pyroot/46416/6 "2021-08-24T16:13:46Z")

</div>

Yep, that is what i am looking for. Once more, thank you so much.
