TTree constructor question

Hello, if I have

class pTree(TTree):

How can I instantialize a pTree from a root file ?

f=TFile(‘test.root’,‘READ’)
t = f.Get(‘t’) <- t is TTree
p = pTree() <- I would know if I could use a copy contructor here

Cheers, Gabriel

Gabriel,

it’s not quite clear to me what your intention is: the TTree copy constructor is private and not implemented (see TTree.h)?

AFAIK (and I’m not all that familiar with this), to copy a TTree, one has to create a new TTree and copy all the entries. See the examples under tutorials/tree.

Cheers,
Wim

Hello, the CloneTree method returns a TTree. If I want to extend the TTree defining a derived Class pTree, than it seems impossible to initialize one instance from a root file since there is no copy constructor for TTree. I am right?

Cheers, Gabriel

Gabriel,

yes, that seems true. However, I’m still not sure what you mean by “extend”? Do you mean in the normal class inheritance sense, i.e. overloading methods and such? If so: if you want to overload a C++ method with a python one, you’ll need a proxy in between. The code that can do that is experimental and not part of the release (I can give you a hand-written example if you’re interested).

Cheers,
Wim

Wim, by extending I mean adding extra methods and not overriding the existents. Do you know if there is a reason for not existing the copy constructor for trees? Cheers, gabriel

Hi Gabriel,

What is your goal in ‘extending’ TTree?

Instead you should consider creating a new class that simply ‘contain’ a TTree (instead of deriving from it).

Cheers,
Philippe

Gabriel,

there are a couple of options to do what you want, without the need of inheritance. I would follow Philippe’s advice, like so:[code]class MyTTree( object ):
def init( self, tree ):
self._tree = tree

def getattr( self, name ):
return getattr( self._tree, name )[/code]
What this does, is forward all unknown methods (i.e. those that you did not implement in your new class) to the TTree object that the instance holds a reference to.

Alternatively, if you want to change TTree class behavior altogether, you can simply add to the class directly:[code]def MyMethod( self, arg1, arg2 ):
print self, arg1, arg2

TTree.MyMethod = MyMethod
del MyMethod[/code]
which now allows you to call “pTree.MyMethod( 1, 2 )” etc. (with pTree being a TTree). This latter approach is particularly useful if you are changing TTree behavior vis-a-vis builtin types (e.g. implementing mul, iter, etc.).

HTH,
Wim

Thanks Phillipe and Wim for your useful advices. Although, I still dont see why TTree dont provide a copy constructor, since its a standard practice in c++ coding and probably easy to implement. Cheers, Gabriel

What would you expect from a TTree copy constructor?
-copy the TTree header in memory only
-copy also the Tree buffers in the file(s)
-if (yes) where to copy

That’s the reason why we provide a TTree::CloneTree and TTree::CopyTree such that people think a bit the difference between all the options and realize that copying a Ttree is just a bit more than copying the Ttree object in memory.

Rene