# Problem in fitting (in function!)

**URL:** <https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785>\
**Category:** ROOT\
**Created:** [April 9, 2020, 8:05am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785 "2020-04-09T08:05:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sandy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@Sandy](https://root-forum.cern.ch/u/Sandy)\
**Post date:** [April 9, 2020, 8:05am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/1 "2020-04-09T08:05:03Z")

</div>

Dear Rooters,

I was trying to fit the data from the below attached text file. The macro I was using is also attached (edit: now removed). I could not fit it. Any kind of help is appreciated.

[data.txt](https://root-forum.cern.ch/uploads/short-url/bflAJOny8KLvHuGHPCwZc2mkQWd.txt) (240.2 KB)

---

<div class="post-metadata">

**Author:** ![Sandy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@Sandy](https://root-forum.cern.ch/u/Sandy)\
**Post date:** [April 9, 2020, 10:16am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/4 "2020-04-09T10:16:02Z")

</div>

Dear @Wile_E_Coyote

Sorry about the parameter number. But, even after correcting for those two (as you mentioned), it does not fit. It gives error like:

```auto
Warning in <TCanvas::ResizePad>: Inf/NaN propagated to the pad. Check drawn objects.
Warning in <TCanvas::ResizePad>: Test width changed from 0 to 10
Error in <TPainter3dAlgorithms::FindLevelLines>: number of points for line not equal 2

```

Any idea?

---

<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:** [April 9, 2020, 10:20am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/5 "2020-04-09T10:20:29Z")

</div>

In your “fg” function, you blindly make calculations which may produce “±nan” and “±infinity” values.  
You could try:

```cpp
// if (TMath::IsNaN(valg) || TMath::IsNaN(-valg) || (valg == TMath::Infinity()) || (valg == -TMath::Infinity())) { std::cout << valg << std::endl; }
if (!((valg > -1000.) && (valg < 1000.))) valg = 0.; // a very brutal fix
return valg;

```

---

<div class="post-metadata">

**Author:** ![Sandy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@Sandy](https://root-forum.cern.ch/u/Sandy)\
**Post date:** [April 9, 2020, 10:26am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/6 "2020-04-09T10:26:53Z")

</div>

That helps. Thank you very much.

---

<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:** [April 9, 2020, 11:16am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/7 "2020-04-09T11:16:47Z")

</div>

Looking at your data, I guess you’d better try to use a [TGraph2D](https://root.cern/doc/master/classTGraph2D.html), e.g.:

```cpp
{
  TGraph2D *g = new TGraph2D("data.txt");
  g->SetTitle("some Thing;some X;some Y;some Z");
  g->Draw("surf3"); g->Draw("pcol same");
  gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
#if 0 /* 0 or 1 */
  g->GetXaxis()->SetLimits(50., 200.);
  g->GetYaxis()->SetLimits(80., 280.);
#else /* 0 or 1 */
  g->GetXaxis()->SetRangeUser(50., 200.);
  g->GetYaxis()->SetRangeUser(80., 280.);
#endif /* 0 or 1 */
  gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
}

```

@couet There is a bug in the drawing routines. One cannot use [TAxis::SetLimits](https://root.cern/doc/master/classTAxis.html#aa56ec858687dad0b58ca0cf939e7c79b), if one wants “surf3” and “pcol” together. However, [TAxis::SetRangeUser](https://root.cern/doc/master/classTAxis.html#ac85f8261dedc23bbe68f90afd196cdb8) does work in this case.

Also, it may help if you use:  
[`g->Fit(fit1, "R"); // use the Range specified in the function range`](https://root.cern/doc/master/classTGraph.html#aa978c8ee0162e661eae795f6f3a35589)

---

<div class="post-metadata">

**Author:** ![Sandy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@Sandy](https://root-forum.cern.ch/u/Sandy)\
**Post date:** [April 9, 2020, 12:32pm UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/8 "2020-04-09T12:32:58Z")

</div>

Dear @Wile_E_Coyote

Thanks. But, the macro you have shown regarding TGraph2D, is it plotting my data? I tried but in vain.

---

<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:** [April 9, 2020, 2:15pm UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/9 "2020-04-09T14:15:51Z")

</div>

I modified the macro so that you can now explicitly see the data points, too.

---

<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:** [April 9, 2020, 2:25pm UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/10 "2020-04-09T14:25:13Z")

</div>

The limits I see on the X and Y axis seems those set by SetLimits.

---

<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:** [April 9, 2020, 2:26pm UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/11 "2020-04-09T14:26:52Z")

</div>

@couet The data points drawn by “pcol” are shifted with respect to the “surf3” shapes (if one uses [TAxis::SetLimits](https://root.cern/doc/master/classTAxis.html#aa56ec858687dad0b58ca0cf939e7c79b)).

---

<div class="post-metadata">

**Author:** ![Sandy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@Sandy](https://root-forum.cern.ch/u/Sandy)\
**Post date:** [April 9, 2020, 3:16pm UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/12 "2020-04-09T15:16:36Z")

</div>

but, somehow I am unable to plot it using your modified macro. I am using 5.34.36

---

<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:** [April 9, 2020, 3:32pm UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/13 "2020-04-09T15:32:02Z")

</div>

Sorry, it seems that ROOT 5 has real problems with `g->Draw("surf3");` (maybe because this graph has 150x175 points on a “regular rectangular grid”). Maybe @couet knows any trick to get it working with ROOT 5 (not even “colz” works well but I think it begins with the problem that `g->GetHistogram()` takes ages to complete so I suspect that it’s a “known problem” with the Delaunay triangles).

@Sandy Can you possibly move to ROOT 6.20/04?  
If you cannot, I’m afraid that you may need to stay with a [TH2](https://root.cern/doc/master/classTH2.html), e.g.:

```cpp
{
  // "data.txt" contains a 150x175 "regular rectangular grid" (26250 points)
  TH2D *h = new TH2D("h", "some Thing;some X;some Y;some Z",
                     150, 0., 300., 175, 0., 350.);
  TGraph2D *g = new TGraph2D("data.txt");
  h->FillN(g->GetN(), g->GetX(), g->GetY(), g->GetZ());
  delete g; // no longer needed
  h->Sumw2(kFALSE); // make sure "bin_error = sqrt(bin_content)"
  h->ResetStats(); // reset the statistics including the number of entries
  h->GetXaxis()->SetRangeUser(50., 200.);
  h->GetYaxis()->SetRangeUser(80., 280.);
  gStyle->SetOptStat("neMRiuo"); // underflows and overflows respect ranges
  h->Draw("surf3");
}

```

---

<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:** [April 9, 2020, 6:21pm UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/14 "2020-04-09T18:21:35Z")

</div>

@couet There is also another problem that I found. According to the “[How to set ranges on axis?](https://root.cern.ch/how/how-set-ranges-axis)”, one should be able to use [TAxis::SetLimits](https://root.cern/doc/master/classTAxis.html#aa56ec858687dad0b58ca0cf939e7c79b) BEFORE the graph is drawn. This does not work here at all. I needed the following (note: no “pcol” at all so it’s another problem):

```cpp
g->Draw("surf3"); // it MUST be drawn first
gPad->Modified(); gPad->Update(); // one MUST make sure it's really (re)drawn
g->GetXaxis()->SetLimits(50., 200.); // now one can change it
gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn

```

BTW. [TAxis::SetRangeUser](https://root.cern/doc/master/classTAxis.html#ac85f8261dedc23bbe68f90afd196cdb8) also needed canvas “Update” before it could be used (which is not mentioned in the “[How to set ranges on axis?](https://root.cern.ch/how/how-set-ranges-axis)” at all).

---

<div class="post-metadata">

**Author:** ![Sandy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@Sandy](https://root-forum.cern.ch/u/Sandy)\
**Post date:** [April 10, 2020, 8:52am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/15 "2020-04-10T08:52:07Z")

</div>

Dear @Wile_E_Coyote

I could plot before also and now with your way too. But, I have query about fit. Let me try to make you understand about my query with an already set example in root website.

@couet Is there any way to make the both fits transparent in the sample plot (“From f2”) given under " Drawing a sub-range of a 2D histogram; the [cutg] option" in “[https://root.cern/doc/v612/classTHistPainter.html](https://root.cern/doc/v612/classTHistPainter.html)” ?

---

<div class="post-metadata">

**Author:** ![Sandy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@Sandy](https://root-forum.cern.ch/u/Sandy)\
**Post date:** [April 10, 2020, 12:07pm UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/16 "2020-04-10T12:07:27Z")

</div>

@couet Like the attached figure…is there any way to change palette in such a way that one of the abode two fits would look like this and another fit would be of another colour but again transparent

![trans](https://root-forum.cern.ch/uploads/default/original/3X/9/a/9a1dd68dc9309905d124a88594d1daadfb9f8301.png)

---

<div class="post-metadata">

**Author:** ![Sandy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@Sandy](https://root-forum.cern.ch/u/Sandy)\
**Post date:** [April 13, 2020, 7:14am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/17 "2020-04-13T07:14:20Z")

</div>

Dear @couet,

Any idea on plotting TH2 drawn with option E and the fit surface drawn  
in the same canvas but having transparent color (shown in the figure  
attached before).

---

<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:** [April 13, 2020, 9:25am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/18 "2020-04-13T09:25:20Z")

</div>

You can try to play with this:

```cpp
{
  TGraph2D *g = new TGraph2D("data.txt");
  g->SetTitle("some Thing;some X;some Y;some Z");
#if 1 /* 0 or 1 */
  Double_t zmin = 1.; // e.g. -1. or 0. or 1.
  for (int i = (g->GetN() - 1); i >= 0; i--)
    if (g->GetZ()[i] <= zmin) g->RemovePoint(i); // remove if "z" <= "zmin"
#endif /* 0 or 1 */
  g->SetMarkerStyle(kCircle); g->SetMarkerSize(0.5);
  g->Draw("pcol fb");
  // g->Draw("surf3 fb"); g->Draw("pcol fb same");
  gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
#if 0 /* 0 or 1 */
  g->GetXaxis()->SetLimits(50., 200.);
  g->GetYaxis()->SetLimits(80., 280.);
#else /* 0 or 1 */
  g->GetXaxis()->SetRangeUser(50., 200.);
  g->GetYaxis()->SetRangeUser(80., 280.);
#endif /* 0 or 1 */
  gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
}

```

Then you can use:

```cpp
g->Fit(fit1, "R");
fit1->Draw("surf fb"); g->Draw("pcol fb same");

```

@couet And there’s another bug … the markers are shifted with respect to the function if one uses (due to differences in axes ranges between the graph and the function?):

```cpp
g->Fit(fit1, "R");
g->Draw("pcol fb"); fit1->Draw("surf fb same");

```

---

<div class="post-metadata">

**Author:** ![Sandy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@Sandy](https://root-forum.cern.ch/u/Sandy)\
**Post date:** [April 13, 2020, 10:46am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/20 "2020-04-13T10:46:58Z")

</div>

Dear @Wile_E_Coyote,  
That’s a nice one.

@couet But, how can I make the fit surface of monocolour, and transparent, no net like structure?

---

<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:** [April 13, 2020, 10:55am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/21 "2020-04-13T10:55:22Z")

</div>

Note that you can also play with:  
[`fit1->SetLineColor(...); fit1->SetLineWidth(...); fit1->SetLineStyle(...);`](https://root.cern/doc/master/classTAttLine.html)

---

<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:** [April 14, 2020, 10:20am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/22 "2020-04-14T10:20:15Z")

</div>

Sorry to come back on that only but the last days were holidays here.  
Can you help me and tell me what I should look at now ?

---

<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:** [April 14, 2020, 10:50am UTC](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/23 "2020-04-14T10:50:21Z")

</div>

From my side … there are three bugs reported in my previous posts here:

1. [TAxis::SetLimits does not work](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/7)
2. [`gPad->Update();` is needed for graphs](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/14)
3. [misbehaving axes ranges](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/18)

From @Sandy … I guess she wants to get [a transparent color plot](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785/16)

[Next page](https://root-forum.cern.ch/t/problem-in-fitting-in-function/38785.md?page=2)
