Exercise 5: Two histograms at the same time

(15 minutes)

If you make a cut during your analysis, your supervisor will probably want to see your histograms both before and after cuts.

Consider the quantity:

\[p_{tot} = \sqrt{ p_{x}^{2} + p_{y}^{2} + p_{z}^{2} }\]

Start with our original dataframe. Make a histogram of \(p_{tot}\). (Don’t forget the error bars and to label the axes!) Then apply a cut of \(p_{z} < 145GeV\) and make a plot of \(p_{tot}\) after that cut.

To better simulate the kind of work you’d actually have to do, don’t just run your code twice. Put all the code to generate both plots into a single cell (if you’re using a notebook) or a single script (if you’re using the command line).

Hint

I absolutely expect you to copy-and-paste code fragments from earlier pages and Exercises in this tutorial, and then edit them to suit your task.

Warning

Prediction: You’re going to run into trouble when you actually Draw the histograms. You’ll probably only see one histogram plotted, and it may 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.

The simplest way to deal with this: create more than one canvas. Something like this may guide you:1

Listing 21: A sketch of working with multiple canvases (Python)
canvas_a = ROOT.TCanvas()
histogram_a.Draw()
canvas_a.Draw()
canvas_b = ROOT.TCanvas()
histogram_b.Draw()
canvas_b.Draw()

Take a look at your multiple plots and feel proud! Except… look carefully. The x-axis of the two plots is not quite the same. It doesn’t matter much here, but in an actual analysis you’ll want the axes of your different plots to match. Revise your cell or script so that the x-axes of the two plots are the same.

Hint

There’s a clue about how to do this in Walkthrough: Making scatterplots. Check the footnotes!

xkcd selection_bias

Figure 52: https://xkcd.com/2618/ by Randall Munroe


1

I warned you earlier that at some point I’d stop showing examples both C++ and Python, once you’d seen enough of them to convert one from the other. That point has been reached!