Nan in error fitting

Dear Experts,
I am trying to fit my data using double sided crystal ball + Gaussian. The initial parameters looks fine however I got nan in error (see pdf attached). Also, I have tried to extract an uncertainties yields of both signal and background. I tried to use fitNormSum but I face issue with histogram scale. Can someone help me to get a better fit?
I have attached my code and root file.
Thanks inadvance.

fit_Crystalball_inbfall3.pdf (18.3 KB)
attached). ___
Please read tips for efficient and successful posting and posting code

_ROOT Version:6.20/02
_Platform:pyroot
Compiler: Not Provided


test.root (4.3 KB)
fitcrystalGug.py (5.5 KB)

Before “h1.Fit(fit, ...)”, for all parameters, try to “fit.SetParLimits(ipar, parmin, parmax)”.

@couet The descritpion of TF1::SetParLimits is misleading (compare, e.g., with TH1::Fit).

I try to set parameters as:

fit = TF1('fit', DoubleSidedCB_gaus, y_fit_range[0], y_fit_range[1], 10)
fit.SetParLimits(0, 200, 400)
fit.SetParLimits(1, 1, 1.5)
fit.SetParLimits(2, 0.07, 0.4)
fit.SetParLimits(3, 1, 50)
fit.SetParLimits(4, 1, 50)
fit.SetParLimits(5, 1, 50)
fit.SetParLimits(6, 1, 50)
fit.SetParLimits(7, 400, 600)
fit.SetParLimits(8, 0.88, 0.95)

the output I got is:

****************************************
         Invalid FitResult  (status = 3 )
****************************************
Minimizer is Minuit2 / Migrad
MinFCN                    =      42.6912
Chi2                      =      104.048
NDf                       =           57
Edm                       =     0.370082
NCalls                    =         1031
Amp                       =       337.76   +/-   60.2252      	 (limited)
mean                      =      1.34247   +/-   0.150775     	 (limited)
sigma                     =     0.388961   +/-   0.0386304    	 (limited)
a1                        =      2.21922   +/-   5.61055      	 (limited)
n1                        =      44.6916   +/-   37.8344      	 (limited)
a2                        =      1.92991   +/-   0.0706328    	 (limited)
n2                        =      47.6487   +/-   26.8919      	 (limited)
AmpG                      =      520.098   +/-   64.1338      	 (limited)
meanG                     =         0.88   +/-   0.00875941   	 (limited)
sigmaG                    =     0.101333   +/-   0.0640794   

****************************************
         Invalid FitResult  (status = 3 )
****************************************
Minimizer is Minuit2 / Migrad
MinFCN                    =      42.6912
Chi2                      =      104.048
NDf                       =           57
Edm                       =     0.370082
NCalls                    =         1031
Amp                       =       337.76   +/-   60.2252      	 (limited)
mean                      =      1.34247   +/-   0.150775     	 (limited)
sigma                     =     0.388961   +/-   0.0386304    	 (limited)
a1                        =      2.21922   +/-   5.61055      	 (limited)
n1                        =      44.6916   +/-   37.8344      	 (limited)
a2                        =      1.92991   +/-   0.0706328    	 (limited)
n2                        =      47.6487   +/-   26.8919      	 (limited)
AmpG                      =      520.098   +/-   64.1338      	 (limited)
meanG                     =         0.88   +/-   0.00875941   	 (limited)
sigmaG                    =     0.101333   +/-   0.0640794   

Also, I notice an uncertainty of fit Yield is too large (see attachment)

fit_Crystalball_inbfall3.pdf (18.3 KB)

I suggest to ask @moneta about that.

Note that you must always set “reasonable” initial values for all parameters.
Then you can also set some limits, if needed, e.g.:
fit.SetParameter(ipar, parvalue); fit.SetParLimits(ipar, parmin, parmax)

Thanks @Wile_E_Coyote for your help, now it seems look fine and the output I got is

****************************************
Minimizer is Minuit2 / Migrad
MinFCN                    =      38.6353
Chi2                      =      94.2732
NDf                       =           57
Edm                       =  2.88054e-07
NCalls                    =         1397
Amp                       =      540.445   +/-   19.9275     
mean                      =     0.873912   +/-   0.00271174  
sigma                     =     0.103947   +/-   0.00417312  
a1                        =      2.15153   +/-   0.354735    
n1                        =      1.42092   +/-   0.582392    
a2                        =      1.93174   +/-   0.41519     
n2                        =      1.16521   +/-   2.11956     
AmpG                      =      329.005   +/-   20.9276     
meanG                     =      1.35878   +/-   0.0162047   
sigmaG                    =     0.373823   +/-   0.012132    

****************************************
Minimizer is Minuit2 / Migrad
MinFCN                    =      38.6353
Chi2                      =      94.2732
NDf                       =           57
Edm                       =  2.88054e-07
NCalls                    =         1397
Amp                       =      540.445   +/-   19.9275     
mean                      =     0.873912   +/-   0.00271174  
sigma                     =     0.103947   +/-   0.00417312  
a1                        =      2.15153   +/-   0.354735    
n1                        =      1.42092   +/-   0.582392    
a2                        =      1.93174   +/-   0.41519     
n2                        =      1.16521   +/-   2.11956     
AmpG                      =      329.005   +/-   20.9276     
meanG                     =      1.35878   +/-   0.0162047   
sigmaG                    =     0.373823   +/-   0.012132    

Covariance Matrix:

            	         Amp        mean       sigma          a1          n1          a2          n2        AmpG       meanG      sigmaG
Amp         	      397.11      0.0177     0.04091     -4.9281      6.2427     -3.6893     -12.702     -243.57     0.17497    -0.16971
mean        	      0.0177  7.3535e-06  5.6152e-06 -0.00032774  0.00045448 -0.00017455  -0.0022247   -0.030561  6.3442e-06  -1.007e-05
sigma       	     0.04091  5.6152e-06  1.7415e-05 -0.00098725   0.0011948 -0.00066108  -0.0036241   -0.058943  3.5901e-05 -3.8566e-05
a1          	     -4.9281 -0.00032774 -0.00098725     0.12584    -0.18693    0.064764     0.19489      3.8433  -0.0036121   0.0035387
n1          	      6.2427  0.00045448   0.0011948    -0.18693     0.33918   -0.084919     -0.2471     -5.0511   0.0044492  -0.0041918
a2          	     -3.6893 -0.00017455 -0.00066108    0.064764   -0.084919     0.17238    -0.30187       2.217  -0.0047876   0.0013499
n2          	     -12.702  -0.0022247  -0.0036241     0.19489     -0.2471    -0.30187      4.4926      35.174     0.01287    0.011122
AmpG        	     -243.57   -0.030561   -0.058943      3.8433     -5.0511       2.217      35.174      437.96   -0.028331     0.14016
meanG       	     0.17497  6.3442e-06  3.5901e-05  -0.0036121   0.0044492  -0.0047876     0.01287   -0.028331  0.00026259 -0.00010071
sigmaG      	    -0.16971  -1.007e-05 -3.8566e-05   0.0035387  -0.0041918   0.0013499    0.011122     0.14016 -0.00010071  0.00014719

Correlation Matrix:

            	         Amp        mean       sigma          a1          n1          a2          n2        AmpG       meanG      sigmaG
Amp         	           1     0.32754     0.49195    -0.69715     0.53791    -0.44591    -0.30072    -0.58406     0.54184    -0.70196
mean        	     0.32754           1      0.4962     -0.3407     0.28777    -0.15504    -0.38707    -0.53852     0.14437    -0.30608
sigma       	     0.49195      0.4962           1     -0.6669     0.49159    -0.38155    -0.40973    -0.67492     0.53089    -0.76174
a1          	    -0.69715     -0.3407     -0.6669           1    -0.90482     0.43973      0.2592     0.51771    -0.62837     0.82225
n1          	     0.53791     0.28777     0.49159    -0.90482           1    -0.35119    -0.20017    -0.41443     0.47144    -0.59327
a2          	    -0.44591    -0.15504    -0.38155     0.43973    -0.35119           1    -0.34303     0.25515     -0.7116       0.268
n2          	    -0.30072    -0.38707    -0.40973      0.2592    -0.20017    -0.34303           1     0.79296     0.37472     0.43253
AmpG        	    -0.58406    -0.53852    -0.67492     0.51771    -0.41443     0.25515     0.79296           1   -0.083542     0.55205
meanG       	     0.54184     0.14437     0.53089    -0.62837     0.47144     -0.7116     0.37472   -0.083542           1    -0.51228
sigmaG      	    -0.70196    -0.30608    -0.76174     0.82225    -0.59327       0.268     0.43253     0.55205    -0.51228           1

Also an uncertainty of yield looks fine (attached). I am still figure out how to get the uncertainties of both signal and background. I now I should calculate a covariance matrix Could you please help me how I can do that in my code.
Thank you so much for your help.

fit_Crystalball_inbfall3.pdf (30.7 KB)

fitcrystalGug.py (5.5 KB)

@Wile_E_Coyote Thank you for reporting this ! I will open a PR fixing this

For this use case, see the TF1NormSum class and the tutorial explaining its use,
https://root.cern.ch/doc/master/fitNormSum_8C.html

If you want the correct uncertainty in the integral, you need to use it as a parameter of the fit, instead of an arbitrary amplitude as it is in case of gaus(0)+pol2(3). The TF1NormSum does this correctly for you by normalising the function components before summing them.

Lorenzo

Thanks @moneta for your reply. I have tried to use `TF1NormSum following the tutorial. I notice the uncertainties of Amplitude are 2 for both functions (double side crystal ball and Gaussian functions). Is there something wrong in a fitting?
Here is the output of fitting

****************************************
Minimizer is Minuit2 / Migrad
Chi2                      =      62.6924
NDf                       =           53
Edm                       =  6.26505e-06
NCalls                    =         1595
NSig                      =       5470.4   +/-   1.99973     
NBG                       =       9371.8   +/-   1.99973     
Amp                       =          700   +/-   2           
mean                      =     0.874386   +/-   0.00228535  
sigma                     =    -0.106565   +/-   0.00253326  
a1                        =      1.77127   +/-   0.248825    
n1                        =      1.09454   +/-   0.822172    
a2                        =      2.03443   +/-   0.197052    
n2                        =      1.46801   +/-   0.444712    
AmpG                      =          300   +/-   2           
meanG                     =      1.36911   +/-   0.0146358   
sigmaG                    =     0.363652   +/-   0.00741669  

****************************************
Minimizer is Minuit2 / Migrad
Chi2                      =      62.6924
NDf                       =           53
Edm                       =  6.26505e-06
NCalls                    =         1595
NSig                      =       5470.4   +/-   1.99973     
NBG                       =       9371.8   +/-   1.99973     
Amp                       =          700   +/-   2           
mean                      =     0.874386   +/-   0.00228535  
sigma                     =    -0.106565   +/-   0.00253326  
a1                        =      1.77127   +/-   0.248825    
n1                        =      1.09454   +/-   0.822172    
a2                        =      2.03443   +/-   0.197052    
n2                        =      1.46801   +/-   0.444712    
AmpG                      =          300   +/-   2           
meanG                     =      1.36911   +/-   0.0146358   
sigmaG                    =     0.363652   +/-   0.00741669  

Covariance Matrix:

            	        NSig         NBG         Amp        mean       sigma          a1          n1          a2          n2        AmpG       meanG      sigmaG
NSig        	      3.9989   -0.001073 -1.1638e-14  4.4675e-06 -1.0745e-05  -0.0005514  -0.0019527 -0.00068517  0.00089948 -1.8871e-14   3.006e-05 -3.9761e-05
NBG         	   -0.001073      3.9989  8.0583e-15 -2.9541e-06  5.7042e-06  0.00017023   0.0016902  0.00040288 -0.00069198  6.5396e-15  -1.065e-05  2.0209e-05
Amp         	 -1.1638e-14  8.0583e-15           4 -3.6008e-15  7.8042e-15  1.1455e-12  -4.708e-12  7.6693e-13 -1.4346e-12 -2.2312e-16 -8.2327e-14  1.4935e-14
mean        	  4.4675e-06 -2.9541e-06 -3.6008e-15  5.2228e-06 -1.2353e-06 -1.3336e-05  0.00013998 -1.9435e-05  6.3797e-05 -4.2337e-15  2.9738e-06   1.985e-06
sigma       	 -1.0745e-05  5.7042e-06  7.8042e-15 -1.2353e-06  6.4174e-06  9.9342e-05 -0.00077703  4.3885e-05 -2.5054e-05  7.0807e-15 -1.7252e-05   5.667e-06
a1          	  -0.0005514  0.00017023  1.1455e-12 -1.3336e-05  9.9342e-05    0.061914    -0.18118    0.011671   -0.018876   2.296e-12  -0.0023028 -2.0478e-05
n1          	  -0.0019527   0.0016902  -4.708e-12  0.00013998 -0.00077703    -0.18118     0.67597   -0.071659     0.10614 -8.0683e-12    0.010574   -0.001896
a2          	 -0.00068517  0.00040288  7.6693e-13 -1.9435e-05  4.3885e-05    0.011671   -0.071659    0.038829   -0.078614  1.3244e-12  -0.0015686  0.00083156
n2          	  0.00089948 -0.00069198 -1.4346e-12  6.3797e-05 -2.5054e-05   -0.018876     0.10614   -0.078614     0.19777 -2.5581e-12   0.0022737  -0.0010685
AmpG        	 -1.8871e-14  6.5396e-15 -2.2312e-16 -4.2337e-15  7.0807e-15   2.296e-12 -8.0683e-12  1.3244e-12 -2.5581e-12           4 -1.2637e-13  1.8055e-14
meanG       	   3.006e-05  -1.065e-05 -8.2327e-14  2.9738e-06 -1.7252e-05  -0.0023028    0.010574  -0.0015686   0.0022737 -1.2637e-13  0.00021421 -5.1928e-05
sigmaG      	 -3.9761e-05  2.0209e-05  1.4935e-14   1.985e-06   5.667e-06 -2.0478e-05   -0.001896  0.00083156  -0.0010685  1.8055e-14 -5.1928e-05  5.5007e-05

Correlation Matrix:

            	        NSig         NBG         Amp        mean       sigma          a1          n1          a2          n2        AmpG       meanG      sigmaG
NSig        	           1 -0.00026833   -2.91e-15  0.00097756   -0.002121  -0.0011082  -0.0011877  -0.0017388   0.0010114 -4.7184e-15   0.0010271  -0.0026809
NBG         	 -0.00026833           1  2.0149e-15 -0.00064641    0.001126  0.00034212    0.001028   0.0010224 -0.00077811  1.6351e-15 -0.00036389   0.0013626
Amp         	   -2.91e-15  2.0149e-15           1 -7.8781e-13  1.5404e-12  2.3018e-12 -2.8631e-12   1.946e-12  -1.613e-12 -5.5779e-17 -2.8125e-12  1.0068e-12
mean        	  0.00097756 -0.00064641 -7.8781e-13           1    -0.21338   -0.023452    0.074498   -0.043158    0.062772 -9.2626e-13    0.088907     0.11711
sigma       	   -0.002121    0.001126  1.5404e-12    -0.21338           1      0.1576    -0.37307    0.087913   -0.022239  1.3976e-12     -0.4653     0.30162
a1          	  -0.0011082  0.00034212  2.3018e-12   -0.023452      0.1576           1    -0.88565     0.23803    -0.17058  4.6137e-12    -0.63233   -0.011097
n1          	  -0.0011877    0.001028 -2.8631e-12    0.074498    -0.37307    -0.88565           1    -0.44231     0.29029 -4.9067e-12     0.87871    -0.31093
a2          	  -0.0017388   0.0010224   1.946e-12   -0.043158    0.087913     0.23803    -0.44231           1     -0.8971  3.3606e-12    -0.54388     0.56899
n2          	   0.0010114 -0.00077811  -1.613e-12    0.062772   -0.022239    -0.17058     0.29029     -0.8971           1 -2.8761e-12     0.34933    -0.32395
AmpG        	 -4.7184e-15  1.6351e-15 -5.5779e-17 -9.2626e-13  1.3976e-12  4.6137e-12 -4.9067e-12  3.3606e-12 -2.8761e-12           1 -4.3171e-12  1.2172e-12
meanG       	   0.0010271 -0.00036389 -2.8125e-12    0.088907     -0.4653    -0.63233     0.87871    -0.54388     0.34933 -4.3171e-12           1    -0.47838
sigmaG      	  -0.0026809   0.0013626  1.0068e-12     0.11711     0.30162   -0.011097    -0.31093     0.56899    -0.32395  1.2172e-12    -0.47838           1

Also, I got a different integral value of my histogram before I scale it and after (attached). The last thing how I can make my histogram scale match with fitting parameters to show both signal and background functions.
fit_Crystalball_inbfall4.pdf (18.6 KB)

fitcrystalGus_v2.py (4.3 KB)

Hi,
When using TF1NormSum you add two additional parameters, NSig and NBG. These are acting as the amplitudes for the two functions. You need then to fix the previous amplitude parameters, otherwise there will be 1005 correlated.
These two lines before fitting, should make it work:

f_sum.FixParameter(2,1);
f_sum.FixParameter(9,1);

Cheers

Lorenzo

Thanks @moneta for your reply. I would like to understand why sigma is giving me minus value. Also, how I can show both signal and background in the same histogram after I scale it (the blue and magenta curves in attached)
Thank you so much for your help
fit_Crystalball_inbfallPmm27.pdf (18.6 KB)

Hi,

For the sigma you would need to set a limit. In your function definition, sigma and -sigma are both valid solutions.
For drawing components, you would need to set the fitted parameters values and obtain the correct normalisation. The attached script does this.

Best regards

Lorenzo
fitcrystalGus_v2.py (4.9 KB)

Thank you so much @moneta . I really appropriate your help :slight_smile:

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