2D histogram fit draws to wrong scale

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?

Can you provide a small script reproducing the problem ?

Will provide a script soon.

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

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

Thanks for you input. I am investigating.

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

{
   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");
}
1 Like

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

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 ?

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

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

{
   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");
}

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

There is a report 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):

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

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

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

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