Exercise 3: Drawing two histograms in the same loop
(15 minutes)
Revise your macro to create, fill, and display an additional histogram of
the variable ebeam
(with error bars and axis labels, of course).
Warning
Take care! Recall that I broke a typical physics analysis task into pieces: Definition, Initialization, Loop, and Wrap-up; I also marked the locations in the macro where you’d put these steps.
What may not be obvious is that all your commands that relate to definitions must go in the Definitions 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 program and pasting it more than once; it won’t work.
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 “current” canvas. If there is no canvas, a
default one (our old friend c1
) is created. Both histograms are
being drawn to the same canvas.
The easiest way to solve this problem is to create a new canvas for each
histogram. Look at c1.C to see an example of 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).
Is the ebeam
histogram empty? Take a look at the lower and upper
limit of the x-axis of your histogram. What is the range of ebeam
in
the n-tuple?