Adding new method to existing class

Hi rooters,
is there a way to add a new method to an existing class?
More specifically, I would like to define a new Draw method for the TTree class
that would be a sort of optimized for my analysis work.

Thanks in advance!!


ROOT Version: 6.10/08
Platform, compiler: Linux Fedora 4.13.16-100.fc25.x86_64, gcc version 6.4.1


Hi @mmoo,
C++ as a programming language does not allow extension methods (as opposed to e.g. C#).
Depending on what you need, you might be happy with a free function void MyTreeDraw(TTree &t, options...) that you call like MyTreeDraw(tree, opts...) instead of tree.Draw(opts...).
There are more elaborate options (overloading operator| like in the soon-to-be-standardized ranges-v3 or create your own class that inherits from TTree) but they are probably overkill if all you want is a custom TTree::Draw.

If you are using pyROOT instead (ROOT’s python bindings) you are in luck, in python it’s trivial to add methods to classes:

class A:
    pass

def foo(self):
    print "called"

A.method = foo

a = A()
a.method()

Hope this helps!
Enrico

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