# Extracting Data from a TH1F and a TNtuple (Pyroot)

**URL:** <https://root-forum.cern.ch/t/extracting-data-from-a-th1f-and-a-tntuple-pyroot/40236>\
**Category:** Newbie\
**Created:** [July 3, 2020, 10:13pm UTC](https://root-forum.cern.ch/t/extracting-data-from-a-th1f-and-a-tntuple-pyroot/40236 "2020-07-03T22:13:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Ricardo\_Vega](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/daniel_ricardo_vega/32/22462_2.png) [@Daniel\_Ricardo\_Vega](https://root-forum.cern.ch/u/Daniel_Ricardo_Vega)\
**Post date:** [July 3, 2020, 10:13pm UTC](https://root-forum.cern.ch/t/extracting-data-from-a-th1f-and-a-tntuple-pyroot/40236/1 "2020-07-03T22:13:52Z")

</div>

_ROOT Version:_ root-6.20.04  
_Platform:_ Any/All  
_Compiler:_ Python 2.7.16

* * *

I am trying to extract different data points from a root file. Inside the root file it contains TH1F and a TNtuple with multiple leaves.

I have tried to extract the data from the TH1F called mass using

```auto
f = ROOT.TFile ( "file.root" , "r")

mass = f.Get("mass")

entries = mass.GetEntries()

```

Put the entries I get out are the number of events per bin not the actual data. How do I extract the actual data.

Also how do I do the same for the TNtuple as well for each leaf?

Thanks!

---

<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 4, 2020, 10:10pm UTC](https://root-forum.cern.ch/t/extracting-data-from-a-th1f-and-a-tntuple-pyroot/40236/2 "2020-07-04T22:10:24Z")

</div>

You can find the data points in the ntuple (not in the histogram). The ROOT primer has [some information](https://root.cern/primer/#n-tuples-in-root) on how to access an ntuple in a ROOT file. It might help to first open a browser to visually inspect the file: just open `root -l /path/to/file.root` and run

```
new TBrowser

```

You can then double-click on the ROOT file on the left side to view its contents.

Cheers,  
Jakob

---

<div class="post-metadata">

**Author:** ![sanjeeda](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sanjeeda/32/37613_2.png) [@sanjeeda](https://root-forum.cern.ch/u/sanjeeda)\
**Post date:** [July 5, 2020, 3:06am UTC](https://root-forum.cern.ch/t/extracting-data-from-a-th1f-and-a-tntuple-pyroot/40236/3 "2020-07-05T03:06:53Z")

</div>

Hi @Daniel_Ricardo_Vega ,

I think the following lines should work:

> # get the data in a TTree format
> 
> fi = r.TFile(‘\<your\_file\_name\>.root’, ‘READ’)  
> tr = r.TTree()  
> fi.GetObject(the\_name\_of\_your\_tree, tr)
> 
> # check we have the tree
> 
> print(tr.GetEntries())
> 
> # get the data in RooFit land
> 
> deltaM = r.RooRealVar(name\_of\_your\_leaf, ‘delta\_M’, 0.14, 0.159, ‘GeV/c^{2}’)  
> rds = r.RooDataSet(  
> ‘data’, ‘D^{0}#rightarrow K^{0}_{S} K^{0}_{S}’, tr, r.RooArgSet(deltaM))  
> Here, 0.14 to 0.159 is the range I specified and deltaM is simply a name I specified. You can set these as per your requirement.

Regards,  
Sanjeeda
