Set Precision in pyROOT

Hello everyone. I have copied and modified the following code in pyROOT. I keep getting the error message: Fit:0: RuntimeWarning: Abnormal termination of minimization. Invalid FitResult (status = 4 ). Now and I need to SetPrecision of the minimizer to 0.1 to get converged results, but I don’t know how to do it in pyROOT. Thank you

import numpy as np
from ROOT import TCanvas, TGraph
from ROOT import kGreen
from ROOT import TF1

#Data
data = np.loadtxt(‘lep.txt’)
r = data[:, 0]
V = data[:, 1]

#Canvas to plot on and graph
c = TCanvas()
g = TGraph(r.size, r.astype(np.double),V.astype(np.double))

#Draw canvas and graph
c.Draw()
g.Draw()

#Formatting
g.SetLineColor( kGreen)
g.SetLineWidth( 2 )
g.SetMarkerColor( kGreen )
g.SetMarkerStyle( 21 )

#Labels and title
g.SetTitle(‘Lambda-p potential’)
g.GetXaxis().SetTitle(‘r’)
g.GetYaxis().SetTitle(‘V’)
c.SetGrid()
c.Draw()

#Draw (A)xis and §oints
g.Draw(‘AP’)

#Use a custom function
func = TF1(‘func’, ‘[0]*exp(-((x - [1])/[2])**2) + [3]*exp(-((x- [4])/[5])**2) + [6]*exp(-((x - [7])/[8])**2)’, 0, 10)
func.SetParameters(136.3, 0.2616,-0.2762, 168.0,0.0742, 0.1499, 145.3,0.3888,0.1533)
fit = g.Fit(‘func’, ‘S’)
fitter.SetPrecision(1)

c.Draw()
g.Draw(‘AP’)

Dear @Otto_P ,

Welcome to the ROOT forum!

I understand fitter is a TVirtualFitter and you are trying to use this method:

https://root.cern.ch/doc/master/classTVirtualFitter.html#a2bc52342fe1570b1ac8feb6c1516c1b4

Then it should be enough from PyROOT to do fitter.SetPrecision(0.1).

Any other comment @StephanH ?

Yes! My comment is that in the current script there’s no fitter yet, so we have to make one in order to be able to set its configuration. We discussed a similar problem here:

It’s C++, but I hope that it’s straightforward to translate it to Python. Let us know if it doesn’t work. (And also if you get it to work. Maybe somebody else will have the same problem in the future.)

I am not sure that SetPrecision is the right thing to do in this case. You should normally used the default precision value since you are doing a simple fit and the likelihood/chisquare is computed using double precision.
If your fit does not converge, maybe is due to a different problem, e.g. bad initial parameter values

Lorenzo