Import RooFormulaVar defined from TF1

Dear experts,

I am trying to use spline inside a workspace. This seems to be quite difficult since:

  1. There is no support for spline in RooFit. It should not be a big deal since they can be defined as TF1
  2. The only implementation I know is TSpline and derived class but TSpline is not a TF1, why?

I am running this code

from array import array

class Function:
    def __init__(self, x, y):
        f = ROOT.TSpline3("spline", x, y, len(x))
        self.f = f

    def __call__(self, x, par=None):
        x = x[0]
        y = self.f.Eval(x)
        return y

x = array('d', [1, 3, 5])
y = array('d', [1, 6, 9])
f = ROOT.TF1("f_efficiency", Function(x, y), min(x), max(x), 0)

mass = ROOT.RooRealVar("mass", "mass", min(x), max(x))
fr = ROOT.RooFit.bindFunction(f, mass)

ws = ROOT.RooWorkspace('ws')
getattr(ws, 'import')(f)
getattr(ws, 'import')(fr)
ws.writeToFile('w.root')

With ROOT 5 and 6 it crashed when closing the file. With ROOT 6.04 (but not ROOT 6.02 and not ROOT 5) I am able to reopen the file and use the workspace.

  1. Am I doing something wrong?
  2. Is there an easier way to do it?

Hi,

The problem is that TF1 or functions built on function code cannot be stored in a file. You can however store in the file your original object (e.g. Graph or TSpline) and then re-create afterwards the TF1 that you can bind to a RooAbsReal and then import in the workspace.
Otherwise you could also make a user defined RooAbsPdf and import it in the workspace. But also in this case you would need to load the corresponding code before reading the workspace

Best Regards

Lorenzo