A brief review (5 minutes)

Start up Jupyter if it’s not already started. At Nevis, this means to visit https://notebook.nevis.columbia.edu, type your Nevis account name and password, then select Python 3 from the New pop-up on the upper left. Use File->Rename… to change the name from “Untitled” to anything you want; e.g., “Basic Test”.

What turns Python into pyroot is the inclusion of the ROOT libraries. That’s done with the import command. Cut-and-paste the following into the first cell, then press SHIFT-ENTER.

from ROOT import TH1D, TCanvas
my_canvas = TCanvas()
example = TH1D("example","example histogram",100,-3,3)
xkcd python

Figure 28: https://xkcd.com/353/ by Randall Munroe

If you worked on The C++ Path, you may have noticed that the ROOT C++ interpreter, cling, knows the names of all the ROOT classes.

[] TH1D* example4 = new TH1D("example4","my fourth histogram",100,-3,3);
[] example4->Draw();

In Python, you have to explicitly import the ROOT package, pyroot. There are two ways to do this:

Method 1: Import all of ROOT, and indicate which classes are part of ROOT with a prefix:

In [] import ROOT
In [] example4 = ROOT.TH1D("example4","my fourth histogram",100,-3,3)
In [] example4.Draw()

Method 2: Import the classes you’ll need explicitly so you can omit the prefix:

In [] from ROOT import TH1D
In [] example4 = TH1D("example4","my fourth histogram",100,-3,3)
In [] example4.Draw()

I’m typically going to use the second method in this tutorial, but you can use either one.1 If you use the second method, be aware that if you include a new ROOT class to your Python script (e.g., TCanvas), you’ll have to add it to your import list:

In [] from ROOT import TH1D, TCanvas

At Nevis, all the various Python distributions I’ve installed include pyroot. If you’re working on a different system, you may have to install pyroot yourself if it hasn’t already been installed for you. The exact procedure depends on how your local Python distribution is managed. The following command (or a variation) might work:

pip install --user pyroot
xkcd python_environment

Figure 29: https://xkcd.com/1987/ by Randall Munroe


You may know of a third way: from ROOT import *

Never do this! It’s an extremely bad programming practice that will lead you into disaster someday. In fact, forget I mentioned it. Take some masking tape and use it to cover this footnote.