Exercise 2: Modifying a histogram
(15 minutes)
You’re probably still plotting your histograms as a solid curve. Most
of the time, your supervisor will want to see histograms with errors.
Figure out how to draw one of your histograms (e.g., ebeam
)
with error bars. (Here’s a hint.)

Figure 47: What I get when I plot ebeam
with errors bars turned on.
That was pretty easy once you looked at the hint. We’ll take it up a notch: Let’s be good scientists and set/improve the axis labels; set the x-axis label to “Beam energy [GeV]” and set the y-axis label to “Number of events”.
This is tricky to do “from scratch” without reading the ROOT documentation in detail as described in Exercise 1: Detective work, eventually reaching a description of the TGAxis class. Fortunately, you’ve got a faster way, if you remember an earlier lesson.
Need more help?
Here’s another hint if the above hint is too opaque for you: While
RDataFrame
is a useful tool, sometimes the easiest way to answer
a question is to go back to the basics.
Another thing to remember that if you can use Draw on a histogram, there are other ROOT histogram methods you can use on it.
Also remember that histogram names are arbitrary. Maybe someone else calls a histogram
something like hist__1__1
, but you can call it histebeam
or whatever you want.
One last tip for Python programmers: The ->
used in C++ becomes a simple .
for you.
Are you using the command line?
If you’re using the command line instead of the The Notebook Server, at this point in the tutorial it’s going to be more convenient to start editing an external file and executing it from within your command environment.
root
You may remember this from Walkthrough: Saving and printing your work: Create a file containing your C++ code, preferably with the extension
.C
; e.g.,exercise2.C
. You’ll want to set up your code as a C++ function; e.g.:void exercise2() { // This is not the real code for Exercise 2! cout << "Hello, world!" << endl; }
You can then execute this file from within root with:
.x exercise2.C
ipython
If you’re using interactive Python outside of a notebook, then you’ll want to use the
%run
command. Create a file containing your Python code, preferably with the extension.py
; e.g.,exercise2.py
. You can then execute this file from within ipython with:%run exercise2.py
Tip
If you’re exiting your command-line environment to edit a file, quitting your editor, then starting up your environment again, there’s a simpler way: just start up another Terminal window. Run your command-line environment in one window and edit files in another.1
- 1
I apologize if this tip seems trite to you. But you might be surprised how many students have come from a GUI environment that did not encourage them to open multiple windows at the same time.