Hi guys…
I found this strange behavior while trying to fit a TH1 in a simple macro I wrote. I will try to explain…
I have some TH2Ds. I want to do the ProjectionY on these TH2s, fit the projection with a gauss function and fill a TH1D with the mean obtained from the gauss fits. Finally, I need to fit this TH1D with a pol1 (see image).
In the figure, you can see the results of these operations with the code I wrote. The red triangles are the bin contents (with errors) of the TH1D filled with the mean value from the gauss fit.
As you can see, it basically works, but I don’t know why I have those points at x > 3000 (considering that in my macro there is a check on the entries of the ProjectionY, if they are less than 20 the bin is skipped).
My program should not fill those points and in fact, if I stop the cursor over them, the histogram name is not present.
Then the real problem is when I try to fit the red TH1D with a pol1.
It seems that the fit - range defined between 0 and 2800 - is forced to cross the red triangle at y = 0 and x ~ 2700, and this of course makes the fit wrong.
If I change the fit range excluding the last point, the fit is correct but unfortunately this is not a viable option considering what I need.
I don’t know what are these points at y=0… and I don’t know why the fit seems forced to pass over these points.
I also read that bins with error = 0 should be excluded from a fit, so why do I see this behavior?
How could I fix it?
I hope I was clear in the explanation.
Thank you very much for any help
Hi @Dilicus,
thanks for the welcome. I will try with the TGraphErrors. Anyway, this is a small part of my macro with the lines regarding this issue (but the TH2D is in an external root file…)
But I don’t understand why the TGraph (in black) and TH1D (in red) have different errors, they are filled with the same content. I just added the lines:
It is hard to guess without a file but a I have a couple of suggestions
I will use a “<” instead of “<=”, I think you do a projection outside the X range of the histogram, and I do not know how ProjectionY works in this case, maybe is the reason for the point with zero error
below i will try to use SetBinContent instead of Fill with a weight, just to be consistent. Not 100% sure but maybe this affects how the chi2 is computed.
I think you are right. By replacing the Fill instruction with SetBinContent it seems to work as expected.
Anyway, the TGraphErrors is probably the best option, I don’t know why I didn’t think about it.