Reading Ntuple in PyROOT

Hi,
I am trying to read events from an ntuple. I have the tree “mytree”, and want to loop over it’s entries, extract all the variables, do some calculations and fill a histogram based on some cuts, which may involve calculations on the ntuple variables.

Here is a simple snippet of code, for example:

from ROOT import TStyle, TF1, TFile, TCanvas, gDirectory, TTree, TH1F, TH2F, TBrowser, gStyle, gMinuit, TText, TCut

f = TFile(‘brunel_ntuple.root’, ‘read’)
mytree=f.Get(‘myAlg2/101’)
nentry = mytree.GetEntries()

dx = TH1F(“dx”, ‘Delta X’, 500, -5.0, 5.0)

How do I loop over “mytree” and extract the variables for calculations to be made on them and then fill the histogram “dx”?

Thanks,
Steve

Steve,

depending on your work style, the actual layout of the TTree that you’re working with, etc., this is one possible way:for event in mytree: dx.Fill( event.dx )
HTH,
Wim

P.S. You didn’t say which version of ROOT you were using, so I’m just assuming that you’re using a recent one.

Thanks,
I tried your suggestion, and I get the error message:

Traceback (most recent call last):
File “myROOT.py”, line 17, in ?
dx.Fill( event.dx )
TypeError: none of the 3 overloaded methods succeeded. Full details:
Int_t TH1::Fill(Double_t x) =>
could not convert argument 1 (a float is required)
Int_t TH1::Fill(Double_t x,Double_t w) =>
takes at least 2 arguments (1 given)
Int_t TH1::Fill(const char* name,Double_t w) =>
takes at least 2 arguments (1 given)

I’m using ROOT 5.12.00c, in case that helps…

Thanks again for your help…

Steve,

there’s a problem with accessing double in v5.12 (see this report: http://root.cern.ch/root/roottalk/roottalk06/0941.html). You get a buffer returned, instead of the actual value. Do a “print event.dx” to see the type. IIRC, event.dx[0] will be proper, but I may be mistaken there. In any case, I’d like to see the output from “print event.dx”, because I don’t know the structure of your TTree, so I may be all wrong to begin with. Otherwise, if you can point me to the file, or attach a sample here, I can try it out myself.

Cheers,
Wim

Hi,
I put that line in and I get the response:
<Float_t>

(one for each enety in the loop)

I have to run off to a thesis defense. I also sent you an email to your address with some details…

Cheers,
Steve

Steve,

not sure where something like “<Float_t>” can possibly come from … I’ll await (a sample of) the file.

Cheers,
Wim

Hi Wim,
I have attached a zip file with the ROOT file and the very short
python script. I hope this helps… Sorry, as this is probably so basic,
and I really appreciate your help…

Steve
root.zip (1.5 MB)

Steve,

sorry, but I don’t see the <Float_t> … what I do see for 5.12 (as expected) is the buffer, rather than the value. In that case, you have to access it by dereferencing (and in 5.13 the code will have to be changed :frowning: ).

That is, this works for me in 5.12 (the histo name is somewhat different, b/c you sent an hbook file, and the names was what I got after h2root conversion, so I have MYALG2/h101 rather than myAlg2/101):[code]from ROOT import *

f = TFile(‘brunel_ntuple.root’, ‘read’)
gDirectory.cd( “MYALG2” )

dxh = TH1F(“dx”, ‘Delta X’, 500, -5.0, 5.0)

dxcut = TCut(“fabs(dx)<50”)
dycut = TCut(“fabs(dy)<50”)
dyscut = TCut(“fabs(thyt-thyv)<0.005”)

for event in h101:
print event.dx[0]
dxh.Fill( event.dx[0] )[/code]

and note the indexing into the dx “array”, whereas for 5.13, it’ll be:[code]from ROOT import *

f = TFile(‘brunel_ntuple.root’, ‘read’)
gDirectory.cd( “MYALG2” )

dxh = TH1F(“dx”, ‘Delta X’, 500, -5.0, 5.0)

dxcut = TCut(“fabs(dx)<50”)
dycut = TCut(“fabs(dy)<50”)
dyscut = TCut(“fabs(thyt-thyv)<0.005”)

for event in h101:
print event.dx
dxh.Fill( event.dx )[/code]

which is as I promised it to be originally in the first mail …

HTH,
Wim

Thanks,
Yes, dereferencing was the key, as you said. Now it
seems to work just great!!

Thanks for your help

Steve