Python Exercise 2: Adding error bars to a histogram (5 minutes)

We’re still plotting the chi2 histogram as a solid curve. Most of the time, your supervisor will want to see histograms with errors. Revise the script to draw the histograms with error bars. (Here’s a hint.)


The histogram may not be immediately visible, because all the points are squeezed into the left-hand side of the plot. We’ll investigate the reason why in a subsequent exercise.

chi2 histogram

Figure 31: What I get when I plot chi2 with errors bars turned on.


In case you’re interested, the code below is how I made the above plot. I learned to use gPad to access the temporary histogram from reading the documentation for TTree::Draw(). I learned about SetTitleOffset by reading the TAxis documentation, which led me to the list of TGaxis methods. Yes, it was detective work all over again!

Listing 3: How to modify a plot automatically generated by ROOT
from ROOT import TFile, gROOT, TCanvas, gPad
myFile = TFile("experiment.root")
tree1 = gROOT.FindObject("tree1")
my_canvas = TCanvas()
# Get the temporary histogram used by TTree::Draw()
htemp = gPad.GetPrimitive("htemp")
htemp.GetYaxis().SetTitle("number of events")