Inheriting from TTree

I would like to write a class in python that extends TTree. Is there any possibility of assigning an existing TTree object (for example returned from TFile::Get()) to such a class? The TTree does not have any constructor that just initializes with the address of an object, nor casting is available in python, so I expect the answer to be “no”. But maybe you could help? :slight_smile:

Hi,

that is possible. At least, assuming I understand what you are asking for, as I can think of at least three interpretations. I think you’re asking for this (pseudo code!):class MyTTree(TTree): def __init__(self, some_tree): TTree.__init__(self, some_tree)
Now, the only reason to extend TTree, rather than encapsulate a TTree as a data member, is when this derived class instance is going to make a trip into C+±land through a pointer to TTree. For that the work (i.e. for the overridden functions on the python side to be called), it needs to have its own vtable.

It is possible to generate some code and let ACLiC compile that, and I’ve made a prototyped metaclass that solves this in principle, but never included it in PyROOT (should become generally available with Cling, where codegen is a lot easier).

Instead, have a look at some of the hand-written examples, such as:$ROOTSYS/bindings/pyroot/src/TPyFitFunction.cxx $ROOTSYS/bindings/pyroot/src/TPySelector.cxx(and their corresponding headers in $ROOTSYS/bindings/pyroot/inc).

Cheers,
Wim