Hello,
I ran into a problem fitting a 2D Gaussian to a histogram today. I roughly followed the “official” c++ example (http://root.cern.ch/root/html/tutorials/fit/fit2.C.html) 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
'exp(-0.5*((y-[4])/[5])**2)'),
-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.Scale(1/10000.0)
h.Fit("peak", "R")
h.Draw('surf1')
I’d much appreciate any help!