Walkthrough: Fitting to a Gaussian distribution

(10 minutes)

I created a file with a couple of histograms in it for you to play with. Switch to your UNIX window and copy this file into your directory:1

> cp ~seligman/root-class/histogram.root $PWD

Go back to your TBrowser window. (If you’ve quit ROOT, just start it again and start a new browser.) Click on the folder in the left-hand pane with the same name as your home directory.

Double-click on histogram.root. You can see that I’ve created two histograms with the names hist1 and hist2. Double-click on hist1; you may have to move or switch windows around, or click on the Canvas 1 tab, to see the c1 canvas displayed.

What’s a ‘fit’?

You can guess from the x-axis label that I created this histogram from a Gaussian distribution, but what were the parameters? In physics, to answer this question we typically perform a “fit” on the histogram: you assume a functional form that depends on one or more parameters, and then try to find the value of those parameters that make the function best fit the histogram.

Right-click on the histogram and select FitPanel. Under Fit Function, make sure that Predef-1D is selected. Then make sure gaus is selected in the pop-up menu next to it, and Chi-square is selected in the Fit Settings ‣ Method pop-up menu. Click on Fit at the bottom of the panel. You’ll see two changes: A function is drawn on top of the histogram, and the fit results are printed on the ROOT command window.

What do these options mean?

The Fit Function selects which mathematical function is going to be used to fit the histogram. Predef-1D means that the function is going to come from one of ROOT’s pre-defined one-dimensional math functions; as you will learn in just a bit, you can define functions of your own. Chi-square refers to a fitting method; for any fit that you’re likely to do with a FitPanel, this will be the method you’ll use.

Fit results

Interpreting fit results takes a bit of practice. Recall that a Gaussian has 3 parameters (\(P_0\), \(P_1\), and \(P_2\)); these are labeled “Constant”, “Mean”, and “Sigma” on the fit output. ROOT determined that the best value for the “Mean” was 5.98±0.03, and the best value for the “Sigma” was 2.43±0.02. Compare this with the Mean and RMS printed in the box on the upper right-hand corner of the histogram.

Statistics questions

Why are these values almost the same as the results from the fit?

Why aren’t they identical?

On the canvas, select Options ‣ Fit Parameters. You’ll see the fit parameters displayed on the plot.

Fit parameters

As a general rule, whenever you do a fit, you want to show the fit parameters on the plot. They give you some idea if your “theory” (which is often some function) agrees with the “data” (the points on the plot).

gaussian fit

Figure 12: The resulting plot should look something like this.

As a check, click on landau (which vaguely resembles the plot in Figure 13) on the FitPanel’s Fit Function pop-up menu and click on Fit again; then try expo and fit again.

Working with FitPanel

You may have to click on the Fit button more than once for the button to “pick up” the click.

It looks like of these three choices (Gaussian, landau, exponential), the Gaussian is the best functional form for this histogram. Take a look at the “Chi2 / ndf” value in the statistics box on the histogram (“Chi2 / ndf” is pronounced “kye-squared per [number of] degrees of freedom”). Do the fits again and observe how this number changes. Typically, you know you have a good fit if this ratio is about 1.2

The FitPanel is good for Gaussian distributions and other simple fits. But for fitting large numbers of histograms (as you’d do in the Advanced Exercises and the Expert Exercises) or for more complex functions, you’ll want to learn the following ROOT commands.

To fit hist1 to a Gaussian, type the following command:3

[] hist1->Fit("gaus")

This does the same thing as using the FitPanel. You can close the FitPanel; we won’t be using it anymore.

xkcd mu

Figure 13: http://xkcd.com/815 by Randall Munroe. If your fit function looks like this, something has gone wrong. This would be a poor fit for your data.


1

If you’re going through this class and you can’t login to a system on the Nevis particle-physics Linux cluster, you’ll have to get the files from my web site.

If you want to get all the files from that directory at once, one way is to use this UNIX command:

wget -r -np -nH --cut-dirs=2 -R "index.html*" \
    https://www.nevis.columbia.edu/~seligman/root-class/files/

You may have to install the wget command on your system, since it’s often not installed by default.

Be aware that in that directory there are a lot of work files I created to test things. There’s more in there than just the files I reference in my tutorials.

2

If you’re not familiar with terms like “chi2” or “chi-squared” there’s a brief introduction to statistics in this tutorial.

3

What’s the deal with the arrow “->” instead of the period? It’s because when you read in a histogram from a file, you get a pointer instead of an object. This only matters in C++, not in Python. See the section on pointers for more information.