Python Exercise 3: Two histograms in the same loop (15 minutes)

Revise your script to create, fill, and display an additional histogram of the variable ebeam (with error bars and axis labels, of course).


Take care! Recall that I broke up a typical physics analysis task into three pieces: the Set-up, the Loop, and the Wrap-up; I also marked the locations in the script where you’d put these steps.

What may not be obvious is that all your commands that relate to setting things up must go in the Set-up section, all your commands that are repeated for each event must go in the Loop section, and so on. Don’t try to create two histograms by copying the entire script and pasting it more than once; it may execute, but it will take twice as long (because you’re reading the entire n-tuple twice) and you’ll be left with a single histogram at the end.

Prediction: You’re going to run into trouble when you get to the Wrap-up section and draw the histograms. When you run your code, you’ll probably only see one histogram plotted, and it will be the last one you plot.

The problem is that when you issue the Draw command for a histogram, by default it’s drawn on the most recent canvas you created. Both histograms are being drawn to the same canvas.

Some clues to solve this problem: Look at the examples above to see how a canvas is created. Look up the TCanvas class on the ROOT web site to figure out what the commands do. To figure out how to switch between canvases, look at TCanvas::cd() (that is, the cd() method of the TCanvas class). In Python, the namespace delimiter (“::” in C++) is a period (“.”), so your solution will involve something like Or you might define a canvas, draw in it, define a new canvas, then draw in the newer one.

Is the ebeam histogram empty? Take a look at the lower and upper limits of your histogram. What is the range of ebeam in the n-tuple?

xkcd wisdom_of_the_ancients

Figure 32: by Randall Munroe