Using User Defined Fit Functions with FitSlicesY

I am trying to use a user defined fit function in the TH2::FitSlicesY() function.

As a starting point I just copied the fitf() function from myfit.C tutorial macro. This fitf() function is just a gaussian.

I create a TF1 that uses the fitf() function and then use SetParameters() to set the initial fitting paramters for fitf().

Then, after running a TTree::Draw() command that creates a TH2F histogram, I run

However, when I look at the histograms of the fit parameters, they vary only a little from the parameters that I set using SetParameters().

Am I supposed to Set the TF1 parameters before calling FitSlicesY() as you would before you call TH1::Fit()? Or am I doing something else incorrectly?

The code is attached.

Thanks for any help,
Taylor Childers
Univ. of Minnesota
TDCInterthresholdOffsetsInTCD.cxx (9.94 KB)

What you do seems to be correct. The parameter values that you set before calling TH2::FitSlicesY are used to initialize the parameters of the function before fitting each slice.
When you say that the parameters are only slightly different, do you
mean that the answer is correct or far from the expected solution?
Could you post a modification of your script (including a file myth2.root)
that performs only one FitSlicesY on this particulat histo?


After you said, " The parameter values that you set before calling TH2::FitSlicesY are used to initialize the parameters of the function before fitting each slice." clears things up.

In my TH2F histogram, which is attached, there is a linear distribution. In order to fit this I have been using FitSlicesY(). Until recently I had always just used the built-in “gaus” function. I was trying to use slightly different distribution to fit the slices and then it seem to stop working.

However, when I only fit one bin using FitSlicesY(), after useing SetParameters(), it does seem to fit that slice correctly.

So my problem seems to be that before each slice the parameters are reset to the values I set using SetParameters(). This is a problem because as you move away from the first slice the SetParameters() are no longer valid.

I am attaching the histogram in root format.

Thanks for your help,
Taylor Childers
Univ. of Minnesota
myth2.root (242 KB)

In your example, each slice has a peak but also a long background tail.
When fitting with the chisquare technique, the background bins have a
strong contribution. You should use the “L” option (Loglikelihood) instead
or the “W” option (giving all bins an equal weight).
This however will take more time to fit.
There is a better alternative to FitSlicesY in your example.
You should make a ProfileX of your histo and fit teh ProfileX with a straight line.
This ill be much faster and precise.


Both the suggestions you made seem to work: Adding Loglikelihood fit or Profiling the histogram.

I guess my intention was to deal with the tails by using a user defined Maxwell Distribution which also has a tail, however, these other options are easier.

Thanks for all your help,
Taylor Childers