# Extracting Root data from tuple

**URL:** <https://root-forum.cern.ch/t/extracting-root-data-from-tuple/21775>\
**Category:** ROOT\
**Created:** [August 30, 2016, 12:58pm UTC](https://root-forum.cern.ch/t/extracting-root-data-from-tuple/21775 "2016-08-30T12:58:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeonyboy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jeonyboy/32/19388_2.png) [@jeonyboy](https://root-forum.cern.ch/u/jeonyboy)\
**Post date:** [August 30, 2016, 12:58pm UTC](https://root-forum.cern.ch/t/extracting-root-data-from-tuple/21775/1 "2016-08-30T12:58:11Z")

</div>

Hello, root users. I am a complete novice that is trying to extract data from the attached root file. I found a similar thread: [How to extract a data from dna.root file?](https://root-forum.cern.ch/t/how-to-extract-a-data-from-dna-root-file/16182/1) where the guy solved the question.

My data goes 1 more folder down and I just don’t know how to write the code to extract it. I tried it by editing the dump.cxx file but it’s not working. Thank you for help in advance!

[dump.cxx](https://root-forum.cern.ch/uploads/default/original/2X/1/1a7f950f8e0dae61aa683d147c9b1467856a91a8.cxx) (904 Bytes)[AnaEx01.root](https://root-forum.cern.ch/uploads/default/original/2X/4/4f427c7ff197d31f6888c6def0872dd2e635b0df.root) (69.9 KB)

---

<div class="post-metadata">

**Author:** ![mato](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mato/32/12317_2.png) [@mato](https://root-forum.cern.ch/u/mato)\
**Post date:** [August 30, 2016, 3:43pm UTC](https://root-forum.cern.ch/t/extracting-root-data-from-tuple/21775/2 "2016-08-30T15:43:29Z")

</div>

Hi,  
I am not sure what you want to dump from this file, which contains 4 histograms and 2 ntuples. A simple python script to dump the contents can be the following:

```auto
from ROOT import TFile, TCanvas
f = TFile('AnaEx01.root')

# Get the histograms and draw them
histos = [f.Get('histo/%d'%(i+1)) for i in range(4)]
c = TCanvas()
c.Divide(2,2)
for i in range(4):
    c.cd(i+1)
    histos[i].Draw()
c.Draw()

# Get the tuples and print their contents
ntuples = [f.Get('ntuple/%d'%(i+101)) for i in range(2)]
for i in range(2):
    ntuple = ntuples[i]
    names = [b.GetName() for b in ntuple.GetListOfBranches()]
    print '--------NTuple %s------------------'% ntuple.GetName()
    for n in names : print n, 
    print ';'
    for e in ntuple:
        for n in names:
            print getattr(e,n),
        print ';'    
```

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [August 30, 2016, 4:14pm UTC](https://root-forum.cern.ch/t/extracting-root-data-from-tuple/21775/3 "2016-08-30T16:14:54Z")

</div>

.  
[dump.cxx](https://root-forum.cern.ch/uploads/default/original/2X/5/5d9709642a6fc85133e5938b92dd3c0561b22926.cxx) (1.46 KB)

---

<div class="post-metadata">

**Author:** ![jeonyboy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jeonyboy/32/19388_2.png) [@jeonyboy](https://root-forum.cern.ch/u/jeonyboy)\
**Post date:** [August 31, 2016, 12:54am UTC](https://root-forum.cern.ch/t/extracting-root-data-from-tuple/21775/4 "2016-08-31T00:54:57Z")

</div>

> [@Pepe Le Pew](#):
>
> .

Thank you that totally worked but it didn’t quite do what I wanted. Basically I want to get the x-axis and y-axis data for ntuple 101 so I can recreate graph for example on excel based on data. Thanks for your helps!  
[dump.txt](https://root-forum.cern.ch/uploads/default/original/2X/a/aa519aa3fbc2ec08b1b1f6380c5fa08955a77489.txt) (74.5 KB)

---

<div class="post-metadata">

**Author:** ![jeonyboy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jeonyboy/32/19388_2.png) [@jeonyboy](https://root-forum.cern.ch/u/jeonyboy)\
**Post date:** [August 31, 2016, 1:50am UTC](https://root-forum.cern.ch/t/extracting-root-data-from-tuple/21775/5 "2016-08-31T01:50:35Z")

</div>

[quote=“mato”]Hi,  
I am not sure what you want to dump from this file, which contains 4 histograms and 2 ntuples. A simple python script to dump the contents can be the following:

[code]  
from ROOT import TFile, TCanvas  
f = TFile(‘AnaEx01.root’)

# Get the histograms and draw them

histos = [f.Get(‘histo/%d’%(i+1)) for i in range(4)]  
c = TCanvas()  
c.Divide(2,2)  
for i in range(4):  
[c.cd](http://c.cd)(i+1)  
histos[i].Draw()  
c.Draw()

# Get the tuples and print their contents

ntuples = [f.Get(‘ntuple/%d’%(i+101)) for i in range(2)]  
for i in range(2):  
ntuple = ntuples[i]  
names = [b.GetName() for b in ntuple.GetListOfBranches()]  
print ‘--------NTuple %s------------------’% ntuple.GetName()  
for n in names : print n,  
print ';'  
for e in ntuple:  
for n in names:  
print getattr(e,n),  
print ‘;’  
[/code][/quote]

I tried running the code and I got error message saying ImportError: No module named ROOT.

---

<div class="post-metadata">

**Author:** ![mato](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mato/32/12317_2.png) [@mato](https://root-forum.cern.ch/u/mato)\
**Post date:** [August 31, 2016, 6:31am UTC](https://root-forum.cern.ch/t/extracting-root-data-from-tuple/21775/6 "2016-08-31T06:31:39Z")

</div>

> [@](#):
>
> I tried running the code and I got error message saying ImportError: No module named ROOT.

Does your installation of ROOT has the python option enabled?  
Did you set the environment by sourcing /bin/thisroot.sh

---

<div class="post-metadata">

**Author:** ![jeonyboy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jeonyboy/32/19388_2.png) [@jeonyboy](https://root-forum.cern.ch/u/jeonyboy)\
**Post date:** [August 31, 2016, 1:42pm UTC](https://root-forum.cern.ch/t/extracting-root-data-from-tuple/21775/7 "2016-08-31T13:42:49Z")

</div>

[quote=“mato”][quote]  
I tried running the code and I got error message saying ImportError: No module named ROOT.  
[/quote]  
Does your installation of ROOT has the python option enabled?  
Did you set the environment by sourcing /bin/thisroot.sh[/quote]

I am using Windows 7 and not sure where to find this. Is there a different way to get this done? For histogram, I did the following and it worked before.

TFile \*f = TFile::Open(“run1.root”);  
TH1D \*h2 = (TH1D \*)f-\>Get(“1”);  
h2-\>Print(“all”); \> h4.txt

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [August 31, 2016, 6:53pm UTC](https://root-forum.cern.ch/t/extracting-root-data-from-tuple/21775/8 "2016-08-31T18:53:49Z")

</div>

`{
  TFile *f = TFile::Open("AnaEx01.root");
  TTree *t; f->GetObject("ntuple/101", t);
  // t->Print();
  t->Draw("Egap:Eabs");
  gPad->Update();
  TGraph *g = ((TGraph *)(gPad->GetPrimitive("Graph")));
  g->Print();
}`

---

<div class="post-metadata">

**Author:** ![jeonyboy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jeonyboy/32/19388_2.png) [@jeonyboy](https://root-forum.cern.ch/u/jeonyboy)\
**Post date:** [September 1, 2016, 7:30am UTC](https://root-forum.cern.ch/t/extracting-root-data-from-tuple/21775/9 "2016-09-01T07:30:18Z")

</div>

[quote=“Pepe Le Pew”]`{
  TFile *f = TFile::Open("AnaEx01.root");
  TTree *t; f->GetObject("ntuple/101", t);
  // t->Print();
  t->Draw("Egap:Eabs");
  gPad->Update();
  TGraph *g = ((TGraph *)(gPad->GetPrimitive("Graph")));
  g->Print();
}`[/quote]

Thanks for your continued help! However, I just don’t quite get the solution still ;(

I tried running the code you wrote for me in the command line inside (AnaEx01.root-\>ntuple-\>101-\>Eabs) and it doesn’t seem to do anything.

I just want to get the branch data Eabs for example the x-axis data and corresponding y-axis data into a txt file preferrably.

I tried code below for example but I keep getting error message. Thanks again!

TFile \*f = TFile::Open(“AnaEx01.root”);  
TTree \*t; f-\>GetObject(“ntuple/101”,t);  
TBranch \*z; t-\>Branch(“Eabs”,z);  
z-\>Print(“all”); \> h3.txt
