Fitting fails with TH1F::Fit(TF1) but succeeds with the Fit Panel

Dear ROOT community,

I’m trying to fit a large number of Gaussians but find that this is compromised by the fact that the fitting process fails with TH1F::Fit(TF1). However, when I use the very same (“gaus”) fitting by right clicking the TH1F on the TCanvas and proceed to fit from the FitPanel, the fit converges without issue. Obviously, this is too slow for it to be repeatable for large numbers of fits.

I tried fixing reasonable parameters (going even so far as to provide the end parameters of the FitPanel Fit) but the fit still fails to converge. I tried fitting using the same commands as in the code in the root interpreter as well and it failed as well (as it should to be fair). Since I’m using default parameters for everything, I’m not sure what I am doing wrong and how the two fits are different.


TH1F *h1 = new TH1F("h1",";Charge [ADC];#",4096,0,4095);

TF1* f = new TF1("f","gaus",100,600);
for(int i=0;i<nev;i++){

h1->Fit("f","W"); //I tried without this as well but was hoping it might help the convergence
TCanvas *c1 = new TCanvas("c1","c1",1600,900);

f->Draw("SAME"); //I tried drawing the function without the fit as well and it fits indeed

The terminal output:

         Invalid FitResult  (status = 4 )
Minimizer is Minuit / Migrad
Chi2                      =   3.7226e+13
NDf                       =         1966
Edm                       =  6.47756e+14
NCalls                    =          254
Constant                  =  4.12274e+08   +/-   1.10094e+06 
Mean                      =     -6.68952   +/-   137604      
Sigma                     =      2.74433   +/-   8.26948e+07  	 (limited)
 FCN=1.87719e+07 FROM HESSE     STATUS=FAILED         11 CALLS         130 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    -1.14120e+08   1.41421e+00   0.00000e+00   0.00000e+00
   2  Mean         1.37631e+05   1.41421e+00   0.00000e+00   0.00000e+00
   3  Sigma        1.01437e+02   5.28128e+02   0.00000e+00   0.00000e+00

TFitEditor::DoFit - using function f  0x55921ab63a90
 FCN=13582.4 FROM MIGRAD    STATUS=CONVERGED      64 CALLS          65 TOTAL
                     EDM=7.07664e-13    STRATEGY= 1  ERROR MATRIX UNCERTAINTY   2.2 per cent
  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  Constant     4.56636e+03   5.74791e+00   4.70786e-03   3.45339e-07
   2  Mean         3.35125e+02   1.25313e-01  -7.15758e-05  -1.97415e-06
   3  Sigma        9.97904e+01   1.09393e-01   5.38063e-07   4.23371e-03

Both fits operate between 170 and 600 (the purpose being to fit all peaks for multiple files)
pics: at the top, the result after using the code, at the bottom the fit through the FitPanel:

Try: h1->Fit(f, "", "", 170., 600.);

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