Fitting 2 D histogram example not working

I ran into a problem fitting a 2D Gaussian to a histogram today. I roughly followed the “official” c++ example ( but problems arise when using ranges on the TF2 function and the “R” option in the Fit function. Basically, specifying a limited range in the function leads to the histogram being distorted and shifted in the final draw command. The fit parameters look all right, hence I think it is a problem with the drawing. I cannot believe that I am the first to fit a Gausian in a limited range. Am I missing something here?

Here is the minimal working example. Just change the range in the function definition to see everything go weird…

from ROOT import TH2F, TF2, Double

peak = TF2("peak", ('[0]+[1]*exp(-0.5*((x-[2])/[3])**2)*' +   # xygaus
           -1.0, 1.0, -1.6, 1.6)  # limits

peak.SetParameter('p0', 0.9)
peak.SetParameter('p1', 1)
peak.SetParameter('p2', 0)
peak.SetParameter('p3', 0.7)
peak.SetParameter('p4', 0)
peak.SetParameter('p5', 0.3)
xmin = Double()
xmax = Double()
peak.GetRange(xmin, xmax)
print xmin, xmax
h = TH2F("h2", "from f2", 10, -2.5, 4,
         10, -1.6, 1.6)
h.FillRandom("peak", 1000000)
h.Fit("peak", "R")

I’d much appreciate any help!

Ok, I found the problem: When plotting the fit function and the histogram together (default root behaviour) the histogram has a different coordinate system than the function! This is not obvious in the plots or marked with a a second set of axes nor intuitive at all. Hence, I would qualify it as a bug (though probably not in pyroot but root…). How/where does one post issues like this?


bug reports can be filed as JIRA tickets. See here: (look for ‘issues’ in the left bar).


Issue created here:


and forwarded to Olivier, as it isn’t caused by python and therefore out of my range of expertise …