/afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00/x86_64-slc5-gcc43-opt/root/tutorials/pyroot/staff.py shows us how to read data from an ascii file and create a root file with a Tree.
But how to read a tree in pyroot, e.g. how to apply milk1->SetBranchAddress(“production”,&production) in pyroot? Do we have such an example? I can’t find such example in /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00/x86_64-slc5-gcc43-opt/root/tutorials/pyroot/..
did you add the gROOT.ProcessLine() to define the “milk” data structure? Beyond that, the AddressOf() should take an instance of that data structure, not the TTree instance.
w/o the file to reproduce, it would be hard to see. However, are you certain that this:fout = TFile("../ntuple/SinglePhotonPt70.root","read") and this root [8] TFile *f = new TFile("SinglePhotonPt70.root","r") are pointing to the same file?
if you do tree_shape.Print(), you will find that e1 through e22 are doubles, not floats. Thus, if you change the declaration of staff_t to use Double_t rather than Float_t, you will find that this all works.
Note that this is why I tend to recommend to simply do: tree_shape.GetEntry(3)
print tree_shape.e17
and not use SetBranchAddress. Sure, that is a bit slower in execution, but at least the type (which ROOT has stored with the tree) will be correct.
Thanks, now it works. But I have one more question, I want to read all leaves automatically, but the following doesn’t work:
list_leaf = [e1,e2,e3,e4,e5]
for i in range(len(list_leaf)):
for j in range(tree_shape.GetEntries()):
tree_shape.GetEntry(j)
print tree_shape.list_leaf[i]
it does not work of course. How should I change the code to reach this purpose?
not understanding that code at all. I’ll assume that the e1 etc. in the leaf_list are strings? If so getattr(shape_tree, leaf_list[i]) should do the trick.
i’m quite new to python and pyROOT. I have used ROOT with CINT a little, but decided that for future learing i want to use pyroot, which seems to me a lot more comfortable in some ways.
I found my problem to fit in this thread, so i thought it best to post here.
My problem is the following:
I have root files like the one attached, and want to add them to a Chain and access the leafs in a loop.
With CINT i needed to define doubles x1 and x2 and use
TChain *c1 = new TChain("t1");
c1->Add("data.root");
c1->SetBranchAddress("x1",&x1);
c1->SetBranchAddress("x2",&x2);
for (int i=0; i<(c1->GetEntries());++i){
c1->GetEntry(i);
atgraph->SetPoint(i,x1,x2);
}
I want to use c1.GetEntry() with pyroot likewise, but can’t get it to work.
This thread hasn’t helped me too much.
In this one however:
is a solution quite related to what i need, it’s a bit complicated though …
if someone could help, i’d be delighted . regards,
Martin
it’s better to start a new thread than to reply to an ancient one: your question will be more visible that way.
At issue is that you can not take the address of a double in Python: you’d have to resort to the data members of a struct or to an array of size 1. None of those solution will look pretty.
This may be easier, and seems to be what you want:from ROOT import TChain
c1 = TChain('t1')
c1.Add('data.root')
for event in c1:
print event.x1, event.x2
Cheers,
Wim
This is what I usually do and it works. Regardless of moving “struct” in C to Python, maybe a simple example would make sense.
var1 = array( 'i', [0] )
tree.SetBranchAddress( "var1", var1 )
for i in range( tree.GetEntries() ):
tree.GetEntry( i )
print var1[0]
Then it would iterate through individual entry and prints the corresponding “var1”
If things get more complex and now you want to add a new branch which is a array to a different tree, you just need to alter your code a bit, basically the same idea. Use jets_pt as an example,
mxlen = 20
jets_pt = array( 'd', mxlen*[ 0 ] )
treeOUT.Branch( "jets_pt", jets_pt, "jets_pt/D" )
for i in range( treeIN.GetEntries() ) :
treeIN.GetEntry( i )
for k in range( len( treeIN.jets_pt ) ) :
jets_pt[ k ] = treeIN.jets_pt[ k ]
treeOUT.Fill()
then you could update a new branch to your output tree without changing any existing branches.