# Set Precision in pyROOT

**URL:** <https://root-forum.cern.ch/t/set-precision-in-pyroot/35283>\
**Category:** Newbie\
**Created:** [July 20, 2019, 2:45pm UTC](https://root-forum.cern.ch/t/set-precision-in-pyroot/35283 "2019-07-20T14:45:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Otto\_P](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/otto_p/32/19371_2.png) [@Otto\_P](https://root-forum.cern.ch/u/Otto_P)\
**Post date:** [July 20, 2019, 2:45pm UTC](https://root-forum.cern.ch/t/set-precision-in-pyroot/35283/1 "2019-07-20T14:45:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [July 22, 2019, 7:26am UTC](https://root-forum.cern.ch/t/set-precision-in-pyroot/35283/2 "2019-07-22T07:26:02Z")

</div>

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](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 ?

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [July 22, 2019, 9:05am UTC](https://root-forum.cern.ch/t/set-precision-in-pyroot/35283/3 "2019-07-22T09:05:30Z")

</div>

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:

> [@Why do I keep getting a zero matrix from TFitResult for some dataset while not the others?](https://root-forum.cern.ch/t/why-do-i-keep-getting-a-zero-matrix-from-tfitresult-for-some-dataset-while-not-the-others/35107/5):
>
> Dear StephanH @StephanH : Thanks. Good news: after using the verbose mode “V”, it returns to me some EXTERNAL ERROR MATRIX, which seems to be the covariance matrix that I always want (see the terminal report below). But still the MIGRAD thing didn’t converge, I’m not sure how that happened when everything “seems” to be fine (the fit result seems to agree with my data; and it actually get some reasonable covariance matrix, and just doesn’t want to share it with me, when I print it.). Now I’m…

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

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 23, 2019, 1:20pm UTC](https://root-forum.cern.ch/t/set-precision-in-pyroot/35283/4 "2019-07-23T13:20:50Z")

</div>

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
