Set parameters for pre-defined functions

I would like to fit an histogram with the pre-defined “gaus” function by setting the parameters. In the user guide it is said that one should use option “B” “[…] to fix one or more parameters [when] the fitting function is a predefined one, like polN , expo , landau , gaus”, but it does not say how to set the parameters.
Of course the syntax
gaus->SetParameters(par);
where “par” is an array, does not work.
How should I proceed?

Many thanks in advance

Davide

ROOT Version: 6.14
Platform: Linux 4.15.0-70-generic #79-Ubuntu SMP Tue Nov 12 10:36:11 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
Compiler: Not Provided


TF1 *f = ((TF1*)(gROOT->GetFunction("gaus")));
f->SetParameters(...);

Thanks for your reply.
Anyway it does not seem to work.
I have an histogram with more (Gaussian) peaks, and I want to fit each peak individually. Therefore first I search for the peaks with the TSpectrum() function, and then I perform the fit. Here is the piece of code that is supposed to do that (vOffset[station] is an array of TH1D histograms):

 // Searching for the peak(s), and fitting them
 peaks = s->Search(vOffset[station], 0.001, "noMarkov", 0.2);
 cout << "peaks: " << peaks << "\n";
 peak_pos = s->GetPositionX();
 for ( Int_t p = 0 ; p < peaks ; p++ ) {
   par[0] = vOffset[station]->GetBinContent(vOffset[station]->GetXaxis()->FindBin(peak_pos[p])); // "height"
   par[1] = peak_pos[p]; // "mean"
   par[2] = 0.02; // "sigma"
   cout << "peak position: " << peak_pos[p] << "\tpeak height: " << vOffset[station]->GetBinContent(vOffset[station]->GetXaxis()->FindBin(peak_pos[p])) << "\n";
   vOffset[station]->Fit("gaus", "B");
   TF1 *f = ((TF1*)(gROOT->GetFunction("gaus")));
   f->SetParameters(par);
 }

According to the output it seems that the fit is actually performed as many times as many peaks are found, but each time the parameters are overridden:
peaks: 2
peak position: 0.31 peak height: 81
FCN=114.609 FROM MIGRAD STATUS=CONVERGED 358 CALLS 359 TOTAL
EDM=3.45044e-09 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 Constant 5.26362e+01 3.57764e+00 1.57355e-02 -1.18895e-05
2 Mean 3.44236e-01 3.80586e-03 1.51409e-05 1.62027e-02
3 Sigma 4.77900e-02 2.13055e-03 7.50300e-06 -5.79584e-02
peak position: 0.39 peak height: 54
FCN=114.609 FROM MIGRAD STATUS=CONVERGED 118 CALLS 119 TOTAL
EDM=9.61076e-07 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 Constant 5.26361e+01 3.57759e+00 1.57454e-02 -4.28919e-05
2 Mean 3.44238e-01 3.80586e-03 1.51260e-05 4.80336e-01
3 Sigma 4.77900e-02 2.13049e-03 7.48970e-06 -5.99565e-01

The output histogram is also attached.

Many thanks for any further help!

Davide

You first fit it and then set the values of parameters :question: :upside_down_face:

Right (I thought that Fit() worked like Draw(), one first instantiates it and then changes the attributes), but even if I set the parameters before the fit the result does not change:

that part of the code:

       TF1 *f = ((TF1*)(gROOT->GetFunction("gaus")));
       f->SetParameters(par);
       vOffset[station]->Fit("gaus", "B");

the results:

peaks: 2
peak position: 0.31	peak height: 82
 FCN=105.475 FROM MIGRAD    STATUS=CONVERGED     111 CALLS         112 TOTAL
                     EDM=1.42505e-08    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  Constant     5.21955e+01   3.51238e+00   1.45538e-02  -2.44286e-05
   2  Mean         3.43834e-01   4.16302e-03   1.63895e-05  -4.57769e-02
   3  Sigma        5.23988e-02   2.65127e-03   9.08220e-06   3.31301e-02
peak position: 0.39	peak height: 49
 FCN=105.475 FROM MIGRAD    STATUS=CONVERGED      83 CALLS          84 TOTAL
                     EDM=4.3694e-09    STRATEGY= 1  ERROR MATRIX UNCERTAINTY   3.6 per cent
  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  Constant     5.21954e+01   3.51408e+00  -2.78515e-03  -3.67083e-05
   2  Mean         3.43834e-01   4.14783e-03  -1.96062e-06   1.83239e-02
   3  Sigma        5.23988e-02   2.64274e-03   2.06310e-06  -3.10724e-02

Thanks for the links, actually I already knew them and I used them to write my code, just in a simpler fashion. It looks like I have to use the full version :wink:
I will read them more deeply and maybe I will understand why I cannot set the fit parameters in my code.

The problem is that your peaks are not well separated so, in the last step, you need to fit them all together.

1 Like

Setting also the fit range helped a lot:

vOffset[station]->Fit("gaus", "B", "", peak_pos[p]-0.04, peak_pos[p]+0.04);

Regards

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