TF1 fixing parameter not working


I need to fit Gaussian function to a histogram. The histogram is the TH1D. The code goes like this:

cout << "Center of fit range: " << exp_mean << "\tsigma: " << exp_sigma << endl;
TF1 *fitHits = new TF1("fitHits", "gaus(0)");
fitHits->SetParNames("constant", "mean", "sigma");
fitHits->SetParameters(100., exp_mean, exp_sigma);
fitHits->FixParameter(1, exp_mean);
mean = fitHits->GetParameter(1);
sigma = abs(fitHits->GetParameter(2));
cout << "mean: " << mean << "\tsigma: " << sigma << endl;

Problem is that despite the mean parameter is fixed. The actual value differs later. The output looks like this:

Center of fit range: 158        sigma: 12.56980509
 FCN=15129 FROM HESSE     STATUS=FAILED         11 CALLS         101 TOTAL
                     EDM=0    STRATEGY= 1  ERROR MATRIX UNCERTAINTY 100.0 per cent
  EXT PARAMETER                APPROXIMATE        STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  constant    -3.47861e+06   1.41421e+00   0.00000e+00   0.00000e+00
   2  mean         5.06221e+02     fixed
   3  sigma        7.03222e-01   3.04562e+05   0.00000e+00   0.00000e+00
mean: 506.2210254    sigma: 0.7032215815

From here you can see that the value used as the mean is different than the one I provided to the function despite it is fixed. When I looked at the parameters in the ROOT browser, it looked like this:

The mean parameter is fixed as it should be but its value is not from the range, which contains only the fixed value.

Thanks in advance

ROOT Version: 6.05/03
Platform: CentOS 7.3
Compiler: g++ 4.8.5

When I replace the line

TF1 *fitHits = new TF1("fitHits", "gaus(0)");


TF1 *fitHits = new TF1("fitHits", "[0] * exp(-(x-[1])*(x-[1])/2./[2]/[2])");

it starts working. However, according to the documentation, it should be the same. May it be a bug?


It is not a bug but a feature. When fitting pre-defined functions in ROOT (e.g. gaus, expo, pol,…) the initial parameter values are automatically calculated. This explains why when using an expression representing the same function you have a different outcome.
If you want to force your values to be considered, you need to use the fit option “B”




1 Like

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