Extracting Root data from tuple

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? 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 (904 Bytes)AnaEx01.root (69.9 KB)

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:

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 ';'    

.
dump.cxx (1.46 KB)

1 Like

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 (74.5 KB)

[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(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.

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

[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

{ 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=“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