Wrapping Tree/Treebuffer to map objects

7 views
Skip to first unread message

Luke Kreczko

unread,
Sep 11, 2015, 4:17:32 AM9/11/15
to rootp...@googlegroups.com
Dear all,

I am currently trying to figure out the best way to use your Tree implementation to map plain ntuples to objects.
For this I am following your event loop example [1].
In my ROOT files I have a tree with the following branches (short list for example purposes):
- Event.Run
- Jets.px, .py, .pz, .energy
The behaviour I want is:
```python
for event in tree:
    print(event.Run)
    for jet in event.Jets:
        print(jet.Pt())
```
The jet class is just an object with a four vector (in this example):
```python
class Jet(object):
    def __init__(self, energy, px, py, pz):
        self.vector = LorentzVector(energy, px, py, pz)
    def Pt():
         return self.vector.Pt()
```
The first one, 'print(event.Run)’, was relatively easy to include [2] although I doubt that this is the best way to do so (maybe better to adapt the ntuple format). The second bit however (jets) is where I struggle.
Using TreeBuffer.define_collection it is easy enough to mix the classes, but I am not sure how to fill the four vector.
Is there a way I can customise the constructor of the mixing classes such that they read attributes from the TreeObject?

Similarly I would like (at a later stage) to map the ‘event’ object in 'for event in tree’ to my own class (to provide more understandable access to selections). 

I would appreciate if you have some useful hints in this regard.


Cheers,
Luke

[1]

[2]

Noel Dawe

unread,
Sep 16, 2015, 9:53:21 AM9/16/15
to rootp...@googlegroups.com
Hi Luke,

This is how I've initialized the four-vectors in some of my previous analysis code:


The @cached_property decorator is defined here:


This way the TLorenzVector is created only the first time obj.fourvect is requested and the same instance is used thereafter for the lifetime of the TreeObject.

I'm not sure your tree is structured in a compatible way. We had separate vector<float> branches named jet_pt, jet_eta, jet_phi, jet_m that could be mapped onto these TreeObjects in rootpy. obj.pt would access tree.obj_pt[object_index], for example. Are you dealing with something similar?

Cheers,
Noel


--
You received this message because you are subscribed to the Google Groups "rootpy dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rootpy-dev+...@googlegroups.com.
To post to this group, send email to rootp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rootpy-dev/CANW5eBCkxSWK%3DS-hfYcLcxzVvUtRzSi4%3D0iceRt1N%3DTjoyRrAw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages