Write python user class to a TTree

Hi all,
Is there a way to write python custom user class object to the TTree?

I saw few posts which use dictionaries and compiled libraries of their classes on c++
But are there any more “pythonic” solution for this in 2019?

If no, are there any example/manual of how to do it the best way possible with c++ class?

thanks,
Bohdan

ROOT Version: 6.18
Platform: SL7
Compiler: gcc 4.8.5


Pickle it into a TObjString.

Hi, thanks for the answer!
But it doesn’t sound quite “pythonic” to me…
Is there any example how to do it with linking c++?

EDIT:
I also encountered rootpy. Which looks very fancy, but one of their examples resulted in the error.
I think this module hasn’t been supported for a 4+ years already… Alas

Which rootpy example is that? (rootpy went dormant about 2 years ago when Noel left the field.)

To add a class to a TTree, you needs something addressable. To write the tree then to disk, you need a streamer. The latter can not be generated by rootcling for a Python class, but since its only task is to grab the data of an object and turn them into a bunch of bytes, pickle will do fine. Then to hook this up to a class and give the TTree something addressable, you’d have to have a custom C++ base class for the Python class, one that defines the parts of ClassDef macro and points to a custom streamer that calls pickle. That you can all do, but re-using TObjString seems so much simpler to me.

Sorry, I understand very little, due to lack of programming knowledge, i guess.

But will TObjString objects written in TFile be readable in TBrowser with all the branch/leafs variables without unpickling them?

But I may try once, thanks for the advice.

About rootpy example:
rootpy/examples/tree/model.py

Doesn’t work with python 3.7.4

Return:

ERROR:ROOT.TUnixSystem.DispatchSignals] segmentation violation
ERROR:stack] File "/home/FoxWise/Downloads/test.py", line 77, in <module>
ERROR:stack]   for event in tree:
ERROR:stack] File "/home/FoxWise/Software/Python-3.7.4/lib/python3.7/site-packages/rootpy/tree/tree.py", line 452, in __iter__
ERROR:stack]   super(BaseTree, self).GetEntry(i)

Without a stack trace print, not sure where the problem is there but it’s highly unlikely to be python (or p3 specific) related.

But the same there: what rootpy is doing is creating structs on the fly (using gROOT.ProcessLine; see https://github.com/rootpy/rootpy/blob/master/rootpy/tree/treemodel.py#L137) so that it has something addressable. It then has these custom helpers, such as FloatCol in the example, to fill said structs and place the addresses with the TTree.

If you want to have full integration with things like TBrowser, you’re not going to get away from that.

But then if that’s the way you want to go, I don’t see why not simply write a C++ struct with the addressable data (or use module struct, https://docs.python.org/3/library/struct.html), using rootcling for the normal dictionary generation etc. and then simply dress it up with python behaviors in a python-side derived class?

I guess what I’m missing is the “pythonic” part of the question. As in, I don’t feel that that rootpy code is very pythonic.

alternatively, you could perhaps test uproot, a pure-python implementation of ROOT I/O.
it just added writing support for Trees.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.