Embed TSpline3 in TF1 in pyroot

_ROOT Version: 6, python2,3
_Platform: Fedora
_Compiler: gcc


I do simple TSpline to TGraph points in pyroot like,

spline = ROOT.TSpline3(“name”, Graph)

and then easily get values for some x:

for x in LIST: spline.Eval(x)

But now I want this TSpline as TF1 object so I can integrate and do other stuff;

The simple

fit = TF1( “fit”, spline, 0.1, 2.95)

Doesn’t work for me; I know the number of parameters is missing in fit, but this wouldn’t mean anything since its a spline not a user-defined function!

Any ideas, thanks


a fit is a procedure with which you extract the “best” value for the parameters of your model as well as their uncertainties. In your case you are trying to fit a spline to your data, without having parameters. In some sense the procedure is not well defined.
What are you really trying to achieve?


import ROOT
import json
from sys import argv, exit

def main():

fdata = ROOT.TFile(“graph_file.root”, “READ”);

PT_DATA = [1,2,3,4,5,6…]
Graph = ROOT.TGraph()
fdata.GetObject(“Graph”, Graph)

#use a cubic spline to smooth the graph

s = ROOT.TSpline3(“name”, Graph)


for i in PT_DATA:

  temp = round(s.Eval(i), 8)


if one runs this in pyroot this works; TSpline fits the TGraph and one can get then any point from Eval; Now, you can not Integrate the TSpline but I need here a TF1 object

Hi @Damir_Devetak

With PyROOT, you can construct TF1 objects from Python callables. In C++, one of the overloads of the TF1 constructor has this signature:
TF1 (const char *name, Double_t(*fcn)(Double_t *, Double_t *), Double_t xmin=0, Double_t xmax=1, Int_t npar=0, Int_t ndim=1, EAddToList addToGlobList=EAddToList::kDefault)

So the function it expects has two parameters which are double arrays. See for instance this example:

and here:

What you could do is wrap your TSpline3 in a python function def mywrapper(x, p): ... that uses the TSpline3.


thanks this should work!

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