# ROOT.gStyle.SetOptFit(1111)

**URL:** <https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071>\
**Category:** Newbie\
**Created:** [March 16, 2023, 5:02pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071 "2023-03-16T17:02:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![L\_N](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/l_n/32/32251_2.png) [@L\_N](https://root-forum.cern.ch/u/L_N)\
**Post date:** [March 16, 2023, 5:02pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/1 "2023-03-16T17:02:13Z")

</div>

Hello,

I have a question regarding some fit results.  
My problem is that the fit parameter shown in the final canvas, which includes the data, the fit and the fitparameters (for which I used ROOT.gStyle.SetOptFit(1111) and GetPrimitive(“stats”)) is not the same as extracted directly from the fit results via “fitfunction”.Parameter().  
I used SetFitFormat(“5.10g”) since the fit parameter has at least 9 important decimal places.  
The deviation of the two values is visible only at the last position after the decimal point.  
Please let me know in case you have an idea what the problem could be.

Thanks in advance and cheers!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 16, 2023, 8:32pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/2 "2023-03-16T20:32:14Z")

</div>

Try with: `"5.17g"`  
Dump your “extracted” fit results using, e.g.: `printf("%5.17g\n", some_parameter);`

---

<div class="post-metadata">

**Author:** ![L\_N](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/l_n/32/32251_2.png) [@L\_N](https://root-forum.cern.ch/u/L_N)\
**Post date:** [March 17, 2023, 10:41am UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/3 "2023-03-17T10:41:47Z")

</div>

Thanks for your answer and suggestions.  
I’m still getting the same values.  
Do you have an other idea what I could try?  
Thanks again and cheers!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 17, 2023, 10:45am UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/4 "2023-03-17T10:45:35Z")

</div>

Do you have a small reproducer?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 17, 2023, 10:46am UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/5 "2023-03-17T10:46:32Z")

</div>

A possible source could be mixing “`double`”/“`Double_t`” and “`float`”/“`Float_t`” variables.

---

<div class="post-metadata">

**Author:** ![L\_N](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/l_n/32/32251_2.png) [@L\_N](https://root-forum.cern.ch/u/L_N)\
**Post date:** [March 17, 2023, 11:50am UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/6 "2023-03-17T11:50:32Z")

</div>

Hi again,

here is a short overview on the code:

ROOT.gStyle.SetOptTitle(0);  
ROOT.gStyle.SetOptStat(0);  
ROOT.gStyle.SetOptFit(1111);  
ROOT.gStyle.SetStatBorderSize(0);

Datareadin=float(data[j])  
mc = ROOT.TCanvas(“fit”, “fit”, 1024, 200, 700, 500)  
graph = ROOT.TGraphErrors(n, ff, sig, ROOT.nullptr, sigErr)  
rfitfunc = ROOT.TF1(“rfit”, fitfunc, min, max, npars)  
/set fit parameter etc  
fitresults = graph.Fit(rfitfunc, “QERMBS”)  
/save fit results in file via fitresults.Parameter()  
graph.Draw(“APE”)  
mc.Update()

ps = mc.GetPrimitive(“stats”)  
ps.SetFitFormat(“5.15f”)  
ps.SetStatFormat(“6.4f”)  
mc.Modified()  
mc.Update()

* * *

Saved fit results are not the same as the one shown in the canvas.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 17, 2023, 12:26pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/7 "2023-03-17T12:26:40Z")

</div>

So, what exactly do you get “in the canvas” versus “saved in a file” (e.g., do you get 15 decimal digits precision in both cases?).

---

<div class="post-metadata">

**Author:** ![L\_N](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/l_n/32/32251_2.png) [@L\_N](https://root-forum.cern.ch/u/L_N)\
**Post date:** [March 17, 2023, 12:36pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/8 "2023-03-17T12:36:46Z")

</div>

I get i.e. 1.4301894903 (canvas with ps.SetFitFormat(“5.11g”)) and 1.4301894936 in the file.  
And I’m using also an optimizer (ROOT.Math.MinimizerOptions.SetDefaultPrecision(1e-18)) in case that’s important.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 17, 2023, 12:51pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/9 "2023-03-17T12:51:28Z")

</div>

@couet It seems there is a bug in [TPaveStats](https://root.cern/doc/master/classTPaveStats.html) … it internally converts the original “`double`”/“`Double_t`” values into “`float`”/“`Float_t`” (which are then displayed).

Try: [`((float)1.4301894936) - 1.4301894903f`](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/8)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 17, 2023, 12:52pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/10 "2023-03-17T12:52:53Z")

</div>

@Wile_E_Coyote @L_N : do you have a reproducer showing the problem?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 17, 2023, 12:57pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/11 "2023-03-17T12:57:26Z")

</div>

Just “`gStyle->SetFitFormat("5.17g");`”, fit anything, and then:  
`printf("%5.17g %5.17g\n", f->GetParameter(0), ((float)f->GetParameter(0)));`

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 17, 2023, 1:32pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/12 "2023-03-17T13:32:56Z")

</div>

I made this:

```auto
Double_t fitf(Double_t *x, Double_t *par)
{
   return x[0]*x[0]*par[0]+x[0]*par[1];
}

void L_N()
{
   gStyle->SetOptFit(1111) ;

   const Int_t n = 4;
   Double_t x[n]= {0.3156, 1.0012, 1.4146, 8.4996};
   Double_t y[n]= {1.881, 5.899, 8.376, 50.239};

   auto gr = new TGraph(n,x,y);
   gr->Draw("A*");

   TF1 *f = new TF1("f", fitf,0, 9, 2);
   f->SetParameters(-0.01, 5.9);
   gr->Fit(f);
   printf("%5.17g %5.17g\n", f->GetParameter(0), ((float)f->GetParameter(0)));

}

```

 ![Screenshot 2023-03-17 at 14.32.17](https://root-forum.cern.ch/uploads/default/original/3X/2/3/2365f2199d81459b960959e7ddb027f0119a632a.png)

```auto
****************************************
Minimizer is Minuit / Migrad
Chi2 = 0.000799383
NDf = 2
Edm = 3.07171e-24
NCalls = 36
p0 = -0.000278198 +/- 0.00159679  
p1 = 5.91312 +/- 0.0132961   
-0.00027819773415341253 -0.00027819772367365658

```

Does it reproduce what you are complaining about?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 17, 2023, 1:36pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/13 "2023-03-17T13:36:24Z")

</div>

@couet Add: `gStyle->SetFitFormat("5.17g");`

---

<div class="post-metadata">

**Author:** ![L\_N](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/l_n/32/32251_2.png) [@L\_N](https://root-forum.cern.ch/u/L_N)\
**Post date:** [March 17, 2023, 1:51pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/14 "2023-03-17T13:51:40Z")

</div>

Thanks a lot for your efforts.  
Yes, could you please add SetFitFormat(“5.17g”) to check if you values also deviate?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 17, 2023, 1:53pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/15 "2023-03-17T13:53:48Z")

</div>

it seems so:

 ![Screenshot 2023-03-17 at 14.53.03](https://root-forum.cern.ch/uploads/default/original/3X/0/8/0819ff4836a1775584279ae75993feda85885cc6.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 17, 2023, 2:01pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/16 "2023-03-17T14:01:46Z")

</div>

If I remove the float casting I get:

 ![Screenshot 2023-03-17 at 15.01.10](https://root-forum.cern.ch/uploads/default/original/3X/d/6/d6a8c869355f32383f6c2d79f0a3f409b8a54053.png)

---

<div class="post-metadata">

**Author:** ![L\_N](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/l_n/32/32251_2.png) [@L\_N](https://root-forum.cern.ch/u/L_N)\
**Post date:** [March 17, 2023, 2:03pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/17 "2023-03-17T14:03:53Z")

</div>

Ok, what does that mean now exactly, so I guess f-\>GetParameter(0) is wrong and the float casting is needed?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 17, 2023, 2:18pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/18 "2023-03-17T14:18:20Z")

</div>

is the 2nd output right ?

---

<div class="post-metadata">

**Author:** ![L\_N](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/l_n/32/32251_2.png) [@L\_N](https://root-forum.cern.ch/u/L_N)\
**Post date:** [March 17, 2023, 3:08pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/19 "2023-03-17T15:08:06Z")

</div>

Please let me know what you think and how to proceed. Are the values extracted via f-\>GetParameter not trustworthy?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 17, 2023, 3:08pm UTC](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071/20 "2023-03-17T15:08:32Z")

</div>

> [@L\_N](#):
>
> f-\>GetParameter not trustworthy?

Yes they are

[Next page](https://root-forum.cern.ch/t/root-gstyle-setoptfit-1111/54071.md?page=2)
