Register | Login | Visitors: 180 / 24 h

Fitting

Creating Random Data

First, we create some random data. For that purpose, we create a new file called fit.C and fill in the following content:
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

In general, we have 2 options for this. The easiest method is to click on "Tools" and then on "Fit Panel". In the opening fit manager, you can press the button "Fit" and even without setting any further parameters, the fit converges successfully. In the terminal. In the terminal, the following message should appear:
 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-03
These 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

Now we want to automize the fitting procedure and directly implement this procedure into our code. There are again several ways to do that. The easiest solution is just to place
 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.