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
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
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:
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
I will read them more deeply and maybe I will understand why I cannot set the fit parameters in my code.