Difference between fit panel and programmatically fit

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.24.06
Platform: macOS Monterey 12.2.1

I have a histogram of several peaks but only want to fit one of the peaks. For that specific peak, I know to a good degree (but not as exact as a fit would yield) the parameters needed for a Gaussian fit. When use in a program like this

gtest = ROOT.TF1('gtest','gaus(0)')
gtest.SetParameters(height, mean, sigma)

I get

which is not very good given that I am trying to fit the middle peak. However, when I try to fit with the Fit Panel, using the same parameters in the “Set Parameters…” box of Fit Panel, I get a much better fit

Why do I get such different results even though the fit function (“gaus”, in this case) and the fit parameters are the same?

Probably you have a range defined in the fit panel. In any case, see how to fit within a range:

Hi @dastudillo,

That was one of the possibilities I thought of, but I think it’s unlikely. When using the Fit Panel, I made sure to leave the range slider alone. So, does the Fit Panel automatically change the range?

Also, I am aware that it is possible to fit a peak given a range. I am trying to avoid that option and just use the height, mean, and sigma parameters instead. I have many histograms where a peak of interest can shift around horizontally. So, I want to write something that can fit the peak if given the fit parameters without a range.

You need:
hist.Fit(gtest, "", "", xmin, xmax)
hist.Fit(gtest, "", "", mean - 3. * sigma, mean + 3. * sigma)


This works for most of my cases. Thank you! Any particular reason why you pick 3.? Is that what the Fit Panel does?

You can set whatever “xmin” and “xmax” you want.

If you want to set initial parameters when fitting a Gaussian function (“gaus”), you should use the option “B”. I think this is the reason you observe such difference when using the FitPanel.

gtest = ROOT.TF1('gtest','gaus')
gtest.SetParameters(height, mean, sigma)



Thank you!!