All I want is to save the function and its fit results to a file in order to evaluate function and fit confidence interval at certain x later.
The function is not saved along with the TFitResult object and there’s no way to redefine a pointer to the function after write/read TFitResult to a file.
I don’t understand how to overcome the problem
My example with some comments is below:
import ROOT, numpy as np
def usefit(x, fitf, fitr):
x,e = np.array([x]), np.array([0.0])
fitr.GetConfidenceIntervals(1, 1, 1, x, e, 0.683, False)
print x[0], fitf.Eval(x[0]), e[0]
vx, vy = np.array([1.0, 2.0, 3.0]), np.array([0.9, 2.1, 2.9])
ex, ey = np.array([0.1, 0.1, 0.1]), np.array([0.1, 0.1, 0.1])
pnts = ROOT.TGraphErrors(3, vx, vy, ex, ey)
fitf = ROOT.TF1('fitf', '[0] + [1]*x')
fitr = pnts.Fit('fitf', 'SQ')
print fitr # <ROOT.TFitResultPtr object at 0x2dfc880>
usefit(4.0, fitf, fitr) # Here usefit() works
fitr = ROOT.TFitResult(fitr.Get())
print fitr # <ROOT.TFitResult object ("TFitResult") at 0x383f520>
usefit(4.0, fitf, fitr) # Here too
data = ROOT.TList()
data.Add(fitf)
data.Add(fitr)
fp = ROOT.TFile('save.root','RECREATE'); fp.WriteObject(data, 'test'); fp.Close()
fp = ROOT.TFile('save.root'); fp.GetObject('test', data); fp.Close()
fitf = data[0]
fitr = data[1]
print fitr # <ROOT.TFitResult object ("TFitResult") at 0x2943d90>
usefit(4.0, fitf, fitr) # Here usefit() doesn't work
Here is the stdout:
<ROOT.TFitResultPtr object at 0x37a09c0>
4.0 3.98003436242 0.217605401486
<ROOT.TFitResult object ("TFitResult") at 0x3d944b0>
4.0 3.98003436242 0.217605401486
<ROOT.TFitResult object ("TFitResult") at 0x42ef260>
Error in <ROOT::Math::FitResult::GetConfidenceIntervals>: Cannot compute Confidence Intervals without fit model function
4.0 3.98003436242 0.0
ROOT Version: 6.08/06
Platform: Linux Mint 4.4.0-143-generic
Compiler: gcc 5.4.0 20160609