Fitting
Table of Contents
Creating Random Data
void fit() { TCanvas *c1 = new TCanvas(); TH1F *hist = new TH1F("hist", "Histogram", 100, -5, 5); hist->FillRandom("gaus", 10000); hist->Draw(); }This results in a histogram filled with 10000 entries following a Gaussian distribution. Now we are ready to fit a Gaussian to it.
Using Fit Panel
FCN=57.7629 FROM MIGRAD STATUS=CONVERGED 60 CALLS 61 TOTAL EDM=3.29508e-09 STRATEGY= 1 ERROR MATRIX ACCURATE EXT PARAMETER STEP FIRST NO. NAME VALUE ERROR SIZE DERIVATIVE 1 Constant 3.97891e+02 4.93046e+00 1.49364e-02 3.65173e-06 2 Mean 1.13884e-02 1.00492e-02 3.75730e-05 7.94991e-03 3 Sigma 9.97455e-01 7.30906e-03 7.36429e-06 3.98611e-03These are the 3 parameters of a Gaussian which is defined as $$f(x) = \frac{1}{2\pi\sqrt{\sigma}}e^{\frac{(x-\mu)^2}{2\sigma^2}}$$ The first one represents the amplitude which is around 400 in this case as shown in the column VALUE. It can easily be verified by taking a look at the histogram. The second parameter is the mean value $\mu$. In this example, it is close to 0, because the data is centered around 0. The last row gives out the standard deviation $\sigma$ which is by definition 1. The values in the ERROR column show the fit error for all 3 values. In this example, the errors are in the order of a few percent which is perfectly sufficient. If the errors are too large, the fit might not be suitable for the underlying data. Another important output is the first line showing the status of the fit. In this case, it says converged after in total 60 calls. Sometimes however this does not work and the fit does not converge. Then the values for the fit parameters cannot be used of course.
Code Implementation
hist->Fit("gaus");below the draw line. After running the code, the output looks similar to the one we obtained before. Sometimes it is however useful to get access to the fit parameters to do calculations with it later. For that purpose, we have to get access to the function using the following line:
TF1* fit = (TF1*)hist->GetFunction("gaus");TF1 is a class used for functions in ROOT and it equals the return value of the method GetFunction() which is part of the class TH1F. After getting access to the fit function, we can now store the value in a variable:
double mean = fit->GetParameter(1);GetParameter(0) would return the amplitude and GetParameter(2) the standard deviation. After that, we can print out the mean in the terminal.
cout << "The mean value is given as: " << mean << endl;For our example, the output value should be around 0.0113884, depending on which seed you are using. Our full code looks as follows:
void fit() { TCanvas *c1 = new TCanvas(); TH1F *hist = new TH1F("hist", "Histogram", 100, -5, 5); hist->FillRandom("gaus", 10000); hist->Draw(); hist->Fit("gaus"); TF1* fit = (TF1*)hist->GetFunction("gaus"); double mean = fit->GetParameter(1); cout << "The mean value is given as: " << mean << endl; }
This page contains 655 words and 4320 characters.
Last modified: 2022-10-01 18:30:50 by mustafa