Walkthrough: Working with Histograms (25 minutes)

Let’s create a simple histogram:

[] TH1D h1("hist1","Histogram from a gaussian",100,-3,3)

Note

Let’s think about what these arguments mean for a moment (and also look at the description of TH1D on the ROOT web site).

  • The ROOT name of the histogram is hist1.

  • The title displayed when plotting the histogram is “Histogram from a gaussian”.1

  • There are 100 bins in the histogram.

  • The limits of the histogram are from -3 to 3.

Question

What is the width of one bin of this histogram? Type the following to see if your answer is the same as ROOT thinks it is:

[] h1.GetBinWidth(0)

Note that we have to indicate which bin’s width we want (bin 0 in this case), because you can define histograms with varying bin widths.2

If you type

[] h1.Draw()

right now, you won’t see much. That’s because the histogram is empty. Let’s randomly generate 10,000 values according to a distribution and fill the histogram with them:

[] h1.FillRandom("gaus",10000)
[] h1.Draw()

The “gaus” function is pre-defined by ROOT (see the TFormula class on the ROOT web site; there’s also more later in this tutorial). The default gaussian distribution has a width of 1 and a mean of zero.

You’ve probably already noticed the words “Mean” and “StdDev” in the upper right-hand corner of the plot. If you need formal definitions, you can find them in Equations (1) and (2) later in this tutorial.

Question

For those who’ve know statistics: In this histogram, why isn’t the mean exactly 0, nor the width exactly 1?

Add another 10,000 events to histogram h1 with the FillRandom method (use up-arrow to enter the command again). Click on the canvas. Does the histogram update immediately, or do you have to type another Draw command?

Let’s put some error bars on the histogram. Select View->Editor, then click on the histogram. From the Error pop-up menu, select Simple. Try clicking on the Simple Drawing box and see how the plot changes.

Note

With these options, the size of the error bars is equal to the square root of the number of events in that histogram bin. Use the up-arrow key in the ROOT command window and execute the FillRandom method a few more times; draw the canvas again.

Question

Why do the error bars get smaller? Hint: Look at how the y-axis changes.

You will often want to draw histograms with error bars. For future reference, you could have used the following command instead of the Editor:

[] h1.Draw("e")

Let’s create a function of our own:

[] TF1 myfunc("myfunc","gaus",0,3)

The “gaus” (or gaussian) function is actually \(P_{0}e^{- \frac{1}{2}\left( \frac{\left( x - P_{1} \right)}{P_{2}} \right)^{2}}\), where \(P_{0}\), \(P_{1}\), and \(P_{2}\) are “parameters” of the function. Let’s set these three parameters to values that we choose, draw the result, and then create a new histogram from our function:

[] myfunc.SetParameters(10.,1.0,0.5)
[] myfunc.Draw()
[] TH1D h2("hist2","Histogram from my function",100,-3,3)
[] h2.FillRandom("myfunc",10000)
[] h2.Draw()

Note that we could also set the function’s parameters individually:

[] myfunc.SetParameter(1,-1.0)
[] h2.FillRandom("myfunc",10000)

Question

What’s the difference between SetParameters and SetParameter? If you have any doubts, check the description of class TF1 on the ROOT web site.

xkcd sports

Figure 10: https://xkcd.com/904/ by Randall Munroe


1

Did you just ask “What is a ‘gaussian’?” I created a section on statistics just for you!

2

For advanced users: Why would you have varying bin widths? Recall the “too many bins” and “too few bins” examples that I showed in the introduction to the class. In physics, it’s common to see event distributions with long “tails.” There are times when it’s a good idea to have small-width bins in regions with large numbers of events, and large bin widths in regions with only a few events. This can result in having a large number of events in every bin in the histogram, which helps with fitting to functions as discussed below and in the statistics section.