Decorator for TF1

Hi all, I’m trying to use python’s @decorator syntax to more easily create TF1 objects from python functions. It’s not working like I expect, but maybe it’s because I don’t understand decorators enough.

Here is what I normally do to create a TF1 from a python function:

def f(x):
    return x

class f_from_TF1:
    def __call__(self,x):
        return f(x[0])
start,end = 0,1
nparams = 0
f_TF1 = ROOT.TF1(f.func_name,f_from_TF1(),start,end,nparams)

Since this is pretty generic, to avoid writing the code over and over again, I wish to have a @TF1ify decorator that could work like this:

def f(x):
    return x

Does anyone already have one of these? Seems like it might be useful, if there is a good version existing, could it somehow be included in PyROOT?


Here is something that works for parameter-less functions, but it’s obviously not done. It might also fail for non-1D functions.

[code]import ROOT
def TF1ifier(start,end,params=[]):
def TF1ify(func):
class func_TF1:
def call(self,x):
return func(*x)
theTF1 = ROOT.TF1(func.func_name,func_TF1(),start,end,len(params))
for i in range(len(params)):
return theTF1
return TF1ify

def f(x):
return x
type(f) # Returns ROOT.TF1, and you can call it and everything.