Reading from a tree: A simple example?

578 views
Skip to first unread message

Akin

unread,
Mar 25, 2013, 6:37:00 PM3/25/13
to rootpy...@googlegroups.com
Hi,

I am trying to find a simple example that shows how to read from an already existing tree with a given user-defined structure. On the rootpy.org website I was only able to find information on how to fill a tree, not how to read one (did I miss it?). The root file that I am trying to read has not been created with rootpy and is not using TreeModel. I somehow feel that this should be easy enough, but I was unable to produce a working program. Any suggestions?

Best,

Akin

Noel Dawe

unread,
Mar 25, 2013, 7:54:00 PM3/25/13
to rootpy...@googlegroups.com
Hi Akin,

You have inspired a new example (and a minor bugfix):


Essentially you should be able to read in any type as long as "from ROOT import MyType" works (by using rootpy.compiled or gSystem.Load()'ing your library). At the moment I do not think this will work for things in namespaces (we can work on that).

Cheers,
Noel


Vancouver: +1 778 373 9738
Geneva: +41 76 631 44 50 (cell)
Skype: noel.dawe



--
You received this message because you are subscribed to the Google Groups "rootpy users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rootpy-users...@googlegroups.com.
To post to this group, send email to rootpy...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rootpy-users/-/3hS43xcnQt8J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Noel Dawe

unread,
Mar 26, 2013, 8:58:55 AM3/26/13
to rootpy...@googlegroups.com

I think I should clarify a bit more. Using TreeModel to construct your tree is only a convenience and does nothing that would make the tree unreadable by independent software. The TreeModel is only used to construct the branches for you in the Tree __init__(). Since we also have class inheritance with TreeModels, complex models can be constructed by defining a hierarchy of simple TreeModels.

Another point I would like to clarify is that independent of how the tree was originally created, you will still be able to access all branch values by name (mytree.branchname) as long as they are simple types or libraries with dictionaries for the types have been previously loaded.

Cheers,
Noel


Vancouver: +1 778 373 9738
Geneva: +41 76 631 44 50 (cell)
Skype: noel.dawe


Akin

unread,
Mar 26, 2013, 9:27:33 AM3/26/13
to rootpy...@googlegroups.com
Hi Noel,

thank you very much for the example (which works like a charm!) and the clarifications. I tried to modify your program to adapt it to my needs, but I did not quite succeed, so I was trying to figure out what I might be doing wrong before posting another question. So, here is how I modified the program:

import rootpy
rootpy.log.basic_config_colorized()
from rootpy.tree import Tree, TreeModel
from rootpy.io import root_open as ropen
from rootpy.types import IntCol, ObjectCol
import rootpy.compiled as C
from random import gauss

# alternatively you can ROOT.gSystem.Load() your library
import ROOT
ROOT.gSystem.Load("Delphes_C")

# define the model
class Event(TreeModel):

    event_number = IntCol()
    thingy = ObjectCol(ROOT.Delphes)


f = ropen("test.root", "recreate")
tree = Tree("test", model=Event)

# fill the tree
for i in xrange(20):
    tree.event_number = i
    tree.thingy.Track_size = 1
    tree.fill()

tree.write()
f.close()

# now to read the same tree
with ropen("test.root") as f:

    tree = f.test

    for event in tree:
        thing = event.thingy
        print event.event_number, thing.Track_size
       

Why is this not working? I loaded my own (rather Delphes') class definitions by

1) opening the file mymain.root generated by Delphes in root and TBrowser,
2) generating Delphes.C and Delphes.h by the MakeClass command,
3) precompiling the code with "echo .L Delpges.C++ | root -b".

Pyroot seems to recognize the new class, but filling the tree with "tree.thingy.Track_size = 1" seems to have no effect (output is thing.Track_size --> 0). Any ideas? For ease of inspection, I am attaching

1) the code,
2) Delphes.C,
3) Delphes.h.

I am using Python 2.7.1., Root 5.28 and the most recent pyroot (updated this morning via git).

Best,

Akin
Delphes.C
Delphes.h
mymain.py

Noel Dawe

unread,
Mar 27, 2013, 3:47:18 PM3/27/13
to rootpy...@googlegroups.com
Hi Akin,

I tried this out and got the same result as you. Why are you attempting to fill a tree with TSelectors? I'll admit I am not very familiar with TSelector but I thought a single TSelector would be used to select entries in a tree.

Can you describe a little more about what you are trying to accomplish?

I was able to get the expected behaviour by extracting all the class members, sticking them into a new simple Delphes class and removing the "ClassDef(Delphes,0);". I also had to prune down the class members because with the full list I got a segfault from ROOT.

Cheers,
Noel

Vancouver: +1 778 373 9738
Geneva: +41 76 631 44 50 (cell)
Skype: noel.dawe


To view this discussion on the web visit https://groups.google.com/d/msg/rootpy-users/-/Eylay9SGaS8J.

Akin

unread,
Mar 27, 2013, 6:31:47 PM3/27/13
to rootpy...@googlegroups.com
Hi Noel,

I created some output data ("mymain.root") with Delphes, and now I am trying to read it. To that end, I created ROOT programs via the MakeClass command and slightly modified them ( attached files "Delphes.cc" and "Delphes.h").

Should it not be possible or too complicated to read all the information for one event at once, I am also fine with reading one branch at a time, like in the program "mymain.cc" (attached).

I have uploaded the data file "mymain.root" to dropbox

https://www.dropbox.com/sh/az9p1z62rsbgb25/nHcwkYPf2Z

in case you want to test the programs.

Best,

Akin
Delphes.cc
Delphes.h
mymain.cc
Reply all
Reply to author
Forward
0 new messages