Problem on writing/reading FitResults to/from a file (python)

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()
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

May be @moneta and/or @etejedor can help.

It is correct, you cannot store a function, since yhou need the function expression to re-execute it.
What you should do is to save your data object (e.g. histogram) and the fitted parameter value, which are stored in the snapshot of the fit function stored with the histogram.
Afterwards you can re-do a fit using the stored parameter values and you will getg a fit result that you can use to compute confidence intervals


Hi Lorenzo, thank you for reply.

My example shows that there’s no problem to store a function: it is evaluated normally after write & read.
There’s also no problem to store a fit result, I have access to Covarience/Correleation matrices etc.
The only problem that fitresult contains the pointer to my function, for sure it is not valid after save & read cause new function has another allocation in memory.
And I have no rights to change that pointer and no way to save fitf and fitr as one object.

The way you’ve suggested is the one I used before and I just thought that this could be done in a better way.
It resemble me one old joke.

How physicist and mathematician will solve the problem of “Boil the kettle?”
Pour water, light a fire, put the kettle on the fire and heat to 100° C.
And now a new task “Boil the kettle filled with water?”
Physicist: light the fire, put, heat.
Mathematician: pour water out of the kettle than reduce the problem to the previous one.

Nickolai Muchnoi

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