Table of Contents

Getting started with ROOTUseful pyROOT snippetsHandling TH1 histograms

Handling TH1 histograms

Here is the class reference for TH1.

Importing ROOT classes

To use TH1D in python, you can either only import a TH1D class:

from ROOT import TH1D
hist = TH1D("hist_name","hist_title",100,0,100)

or all classes:

from ROOT import * # loads all classes, and thus takes longer
hist = TH1D("hist_name","hist_title",100,0,100)

or only the ROOT module:

import ROOT
hist = ROOT.TH1D("hist_name","hist_title",100,0,100)

The examples below also make use of ROOT's gRandom to make some random variables.

Filling a histogram

Simple enough:

hist = TH1D("hist_name","hist title",100,0,100)
for i in xrange(10000):
    hist.Fill(gRandom.Gauss())

With some weight:

weight = gRandom.Gauss(1,0.1)
hist.Fill(gRandom.Gauss(),weight)

Filling a histogram from a tree

The classic way would be with a simple for loop

hist = TH1D("hist_name","hist title",100,0,100)
for event in tree:
    hist.Fill(tree.px)

But the simplest and most powerful way to draw some variable into a histogram, is with the TTree::Draw function:

hist = TH1D("hist_name","hist title",100,0,100)
tree.Draw("px >> hist_name")

Note that the histogram has been saved into ROOT's working memory and is therefore accessible via its name in the Draw function, “hist_name” in this example.

You can also use mathematical expressions, including boolean expressions (which can take the value 0.0 or 1.0):

tree.Draw("2*sqrt(px*px+py*py) >> hist_name")
tree.Draw("abs(eta) >> hist_name")
tree.Draw("pt1*(eta1<2.4) + pt2*(eta2<2.4) >> hist_name") # boolean expressions

This also includes custom functions, which you can load into ROOT with a C file:

from ROOT import gROOT, ...
gROOT.Macro("myfunctions.C+")
...
tree.Draw("deltaR(eta1,phi1,eta2,phi2) >> hist_name")    # deltaR     function defined in "myfunctions.C+"
tree.Draw("pt1 >> hist_name","(pt>30)*weightEta1(eta1)") # weightEta1 function defined in "myfunctions.C+"

You can apply some selections using variable, available in the tree's branches:

tree.Draw("px >> hist_name","pt>20 && E>20")

You can apply some selections and/or a weight, if a weight variable is available in the tree:

tree.Draw("px >> hist_name","weight")
tree.Draw("px >> hist_name","(pt>20 && E>20)*weight")

An equivalent method is creating the histogram in the Draw method. If you need the TH1F object, you can still get it with gDirectory (see here):

from ROOT import gDirectory
...
tree.Draw("pt >> h(100,0,100)")
hist = gDirectory.Get("h") # will be a TH1F object

It's also possible to pass some draw option you would normally use in “TH1::Draw()”. It is passed as the third string.

tree.Draw("px >> h1(100,0,100)","","E2")
tree.Draw("py >> h2(100,0,100)","","E2 SAME")
 
tree.Draw("px >> h1(100,0,100)","abs(eta)>2","E2")
tree.Draw("px >> h2(100,0,100)","abs(eta)<2","E2 SAME")

Protip: This is very useful for quick drawing and comparing, for example in the ROOT command line.

$ root -l tree.root
[1] tree->Draw("px >> h1(100,0,100)","")
[2] tree->Draw("px >> h1(100,0,100)","pt>20")
[3] tree->Draw("px >> h1(100,0,100)","","E2")
[4] tree->Draw("py >> h2(100,0,100)","","E2 SAME") // compare to px