# 2D histogram fit draws to wrong scale

**URL:** <https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369>\
**Category:** ROOT\
**Created:** [September 28, 2017, 6:22pm UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369 "2017-09-28T18:22:41Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickes](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/6de8d8/32.png) [@wickes](https://root-forum.cern.ch/u/wickes)\
**Post date:** [September 28, 2017, 6:22pm UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/1 "2017-09-28T18:22:42Z")

</div>

I have a 2D histogram that I’m trying to fit, and it seems to work fine except that when I draw the histogram with the attached fit function (via e.g. `hist2d->Draw("surf0 E")` here), it seems to draw the function to a different scale than the histogram. This is supposed to be a function like `1 + y*([0] + [1]*x + ...)` times a step function for `z>0` so the `y=0` edge should be exactly 1, and it is when I use `Eval` function, but instead it appears scaled up above 1.2 due to the histogram’s error bars going above 1.2.

Am I doing something wrong or is this a bug?

 ![c1](https://root-forum.cern.ch/uploads/default/original/2X/e/efd33c553dfc9613b50a5032ec2a7d089c476ca0.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:** [September 29, 2017, 6:44am UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/2 "2017-09-29T06:44:39Z")

</div>

Can you provide a small script reproducing the problem ?

---

<div class="post-metadata">

**Author:** ![wickes](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/6de8d8/32.png) [@wickes](https://root-forum.cern.ch/u/wickes)\
**Post date:** [October 10, 2017, 11:27pm UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/3 "2017-10-10T23:27:52Z")

</div>

Will provide a script soon.

---

<div class="post-metadata">

**Author:** ![wickes](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/6de8d8/32.png) [@wickes](https://root-forum.cern.ch/u/wickes)\
**Post date:** [October 17, 2017, 11:25pm UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/4 "2017-10-17T23:25:08Z")

</div>

Use attached root file containing 2D histogram “hm1”, and this script.

```auto
$ root -l tmp.root
root [0]
Attaching file tmp.root as _file0...
root [1] TH2F* hm1 = (TH2F*)_file0->Get("hm1")
root [2] TF2* f1 = new TF2("f1", "(1 + y*([0] + [1]*x + [2]*x^2)) * (1 + y*([0] + [1]*x + [2]*x^2) > 0)", 0, 1, 0, 600)
root [3] hm1->Fit(f1)
root [4] hm1->Draw("surf0") // correct behavior
root [5] hm1->Draw("surf0 E") // incorrect behavior

```

[tmp.root](https://root-forum.cern.ch/uploads/default/original/2X/e/e73fa56864575a3825a75fc98c35430d56bc181a.root) (8.3 KB)

For some reason I cannot reproduce the original plot with error bars and the fit function; but the bad behavior is still seen with draw option “surf0 E”, except it doesn’t draw error bars (which also seems like a bug).

I have also found that once I have fit a 2D function to a 2D histogram and draw with error bars option “E” (only), I am unable to use the mouse to rotate the view. It can still rotate when drawn with option “surf0” but not with option “E”. Before fitting the function, it works fine with option “E”. This also seems like a bug?

---

<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:** [October 18, 2017, 6:58am UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/5 "2017-10-18T06:58:21Z")

</div>

Thanks for you input. I am investigating.

---

<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:** [October 18, 2017, 9:34am UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/6 "2017-10-18T09:34:17Z")

</div>

This problem is now fixed in the root master.  
The following macro now works as expected:

```auto
{
   TFile *w = new TFile("wickes.root");
   TH2F* hm1 = (TH2F*)w->Get("hm1");
   TF2* f1 = new TF2("f1", "(1 + y*([0] + [1]*x + [2]*x^2)) * (1 + y*([0] + [1]*x + [2]*x^2) > 0)", 0, 1, 0, 600);
   hm1->Fit(f1);
   hm1->Draw("E");
}

```

---

<div class="post-metadata">

**Author:** ![wickes](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/6de8d8/32.png) [@wickes](https://root-forum.cern.ch/u/wickes)\
**Post date:** [October 18, 2017, 6:12pm UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/7 "2017-10-18T18:12:41Z")

</div>

Great! Will it be possible to draw with error bars and surface plot combined, as in “surf0 E” like my original plot?

---

<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:** [October 19, 2017, 7:00am UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/8 "2017-10-19T07:00:58Z")

</div>

If I use “SURF E” I get the surface and not the errors. Even If I do not fit.  
So the SURF option is taken and E is ignored. But your original plot shows that SURF and E  
can be combined . It might be it was possible with older ROOT version. Which version are you using ?

---

<div class="post-metadata">

**Author:** ![wickes](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/6de8d8/32.png) [@wickes](https://root-forum.cern.ch/u/wickes)\
**Post date:** [October 19, 2017, 4:59pm UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/9 "2017-10-19T16:59:05Z")

</div>

I am on 6.10.06 now, but was on 6.10.04 before. I don’t think it was the version, I think I must have done something strange to make that work before, or maybe I drew it with the “same” option.

Does “E” also draw the fit now? I mainly just want a simple way to draw the fit and error bars at the same time (and be able to rotate the view).

---

<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:** [October 20, 2017, 7:05am UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/10 "2017-10-20T07:05:57Z")

</div>

The following macro now produces the attached picture which can’t be rotated .

```auto
{
   TFile *w = new TFile("wickes.root");
   TH2F* hm1 = (TH2F*)w->Get("hm1");
   TF2* f1 = new TF2("f1", "(1 + y*([0] + [1]*x + [2]*x^2)) * (1 + y*([0] + [1]*x + [2]*x^2) > 0)", 0, 1, 0, 600);
   hm1->Fit(f1);
   hm1->Draw("E ");
   hm1->Draw(" SURF0 SAME");
}

```

 ![54](https://root-forum.cern.ch/uploads/default/original/2X/1/1c4599876d0c155569687daaa44d2abc2fd083f6.png)

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [November 3, 2017, 7:05am UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/11 "2017-11-03T07:05:58Z")

</div>

Closed on November 3, 2017, 7:05am UTC

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

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [April 14, 2018, 3:18am UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/12 "2018-04-14T03:18:33Z")

</div>

Opened on April 14, 2018, 3:18am UTC



---

<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, 2018, 4:00am UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/13 "2018-04-14T04:00:19Z")

</div>

[There is a report](https://root-forum.cern.ch/t/2d-histogram-draw-scale-still-inconsistent/28678) that scales used by “E” and “SURF0” are inconsistent (when drawn together).  
Indeed, it seems to me that this small macro reproduces this problem (with ROOT 6.12/06 and 6.13/02):

```auto
{
  TFile *w = new TFile("wickes.root");
  TH2F* hm1 = (TH2F*)w->Get("hm1");
  hm1->SetStats(kFALSE);
  TCanvas *c = new TCanvas("c", "c");
  TF2* f1 = new TF2("f1", "(1 + y*([0] + [1]*x + [2]*x^2)) * (1 + y*([0] + [1]*x + [2]*x^2) > 0)", 0, 1, 0, 600);
  // hm1->Fit(f1);
  c->Clear();
  c->Divide(2, 2);
  c->cd(1);
  hm1->Draw("E ");
  c->cd(2);
  hm1->Draw("SURF0 ");
  c->cd(3);
  hm1->Draw("E ");
  hm1->Draw("SURF0 SAME");
  c->cd(4);
  hm1->Draw("SURF0 ");
  hm1->Draw("E SAME");
  c->cd(0);
}

```

Additionally, note also that there is another bug visible … when drawing “E SAME” (the bottom-right pad), the error bars are NOT drawn at all.

---

<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 16, 2018, 9:10am UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/14 "2018-04-16T09:10:14Z")

</div>

This macro gives me the following plot. I guess the faulty plot the the bottom left one ?

 ![09](https://root-forum.cern.ch/uploads/default/original/3X/e/c/ec37f0edd582bb4dfa63f7aab9d265dc9998a0d9.png)

---

<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 16, 2018, 9:16am UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/15 "2018-04-16T09:16:20Z")

</div>

Both bottom plots are faulty (wrong scales in both and additionally missing error bars in the right one).

---

<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 17, 2018, 8:36am UTC](https://root-forum.cern.ch/t/2d-histogram-fit-draws-to-wrong-scale/26369/16 "2018-04-17T08:36:09Z")

</div>

fro the bottom left one see [https://root.cern/doc/master/classTHistPainter.html#HP060a](https://root.cern/doc/master/classTHistPainter.html#HP060a)  
For the bottom right one I will check.
