Simple_ROOTbook_py.ipynb Open in SWAN Download

Simple ROOTbook (Python)


This simple ROOTbook shows how to create a [histogram](https://root.cern.ch/doc/master/classTH1F.html), [fill it](https://root.cern.ch/doc/master/classTH1.html#a77e71290a82517d317ea8d05e96b6c4a) and [draw it](https://root.cern.ch/doc/master/classTH1.html#aa53a024a9e94d5ec91e3ef49e49563da).

Let's start importing the ROOT module, which gives us access to all ROOT classes.

In [2]:
import ROOT
Welcome to JupyROOT 6.07/07

In order to activate the interactive visualisation we can use the JSROOT magic:

In [3]:
%jsroot on

Now we will create a histogram specifying its title and axes titles:

In [5]:
h = ROOT.TH1F("myHisto","My Histo;X axis;Y axis",64, -4, 4)

Time to create a random generator and fill our histogram:

In [6]:
rndmGenerator = ROOT.TRandom3()
for i in xrange(1000):
    rndm = rndmGenerator.Gaus()
    h.Fill(rndm)

We can now draw the histogram. We will at first create a canvas, the entity which in ROOT holds graphics primitives. Note that thanks to JSROOT, this is not a static plot but an interactive visualisation. Try to play with it and save it as image when you are satisfied!

In [7]:
c = ROOT.TCanvas()
h.Draw()
c.Draw()

We'll try now to beautify the plot a bit, for example filling the histogram with a colour and setting a grid on the canvas.

In [9]:
h.SetFillColor(ROOT.kBlue-10)
c.SetGrid()
h.Draw()
c.Draw()

Alright: we are done with our first step into the ROOTbooks world!