# Odd behavior using setRange

Hi all,

I am seeing an odd behavior using setRange and then passing it as argument to fitTo. A minimal working code reproducing the issue is attached here (testfitrange.cpp (2.1 KB) ). The code generates 100 events from a Gaussian PDF and creates a histogram. This histogram is then fit with the same Gaussian PDF used to create the histogram. I attach here as well the result of fitting with setRange (right) and without any range, just for reference (left) (test.pdf (19.4 KB) ). Needless to say, the fit results for the case using setRange is way off.

Am I misunderstanding something about fitting within a range? Any advice is greatly appreciated!

Hi @ilam,

The range should work in theory. Two things:

• The RooDataHist has 25 bins from -10 to 10. The ranges you are giving don’t align with bin edges, so there could be problems deciding which bins go into the fit.
• There could simply be a bug, but I don’t have the time to run your example during the conference. Could you check in the mean time that the bin edges don’t cause the problem?

Hi @StephanH,

I changed the number of bins to 20 but it still gives a similar result. I also tried going to 200 bins but nothing changed.

Hi,

it’s indeed a binning problem. Some bins are not populated, and the minimiser tries to fit the curve into these (close to) empty bins.

Just multiplying the number of events by 10 gives much better results:

The best way probably is to use an unbinned fit. In this way, it doesn’t matter if bins are populated.
Note also that sigma in your model can go negative. I advise to limits its range to something like [0.1, 10.]

A short explanation:
If a bin has zero entries, but the Gaussian distribution in this bin suggests that 0.4 events should be in that bin, it’s best to either set n to zero or shift the mean value to the left, to get the gaussian that’s touching the empty bin as close to zero as possible. If the left part of the data is ignored, there’s no bins to counter that decision.
In unbinned fits, this “this bin is empty, let’s pull the Gaussian down” doesn’t happen.

Hi @StephanH ,

I think I understand what is going on now. I was playing around with various values and they seem to work now. Thanks so much!

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