Python Walkthrough: Simple analysis using the Draw command (10 minutes)

Note

It may be that all the analysis tasks that your supervisor will ask you to do can be performed using the Draw command, the Treeviewer, the FitPanel and other simple techniques discussed in the early chapters of the ROOT Users Guide.

However, it’s more likely that these simple commands will only be useful when you get started; for example, you can draw a histogram of just one variable to see what the histogram limits might be. Let’s start with the same tasks you did with Treeviewer.1\(^,\)2

If you didn’t copy the example n-tuple file in The Basics, do so now:

> cp ~seligman/root-class/experiment.root $PWD

Open the sample ROOT TTree in the notebook with the following:

from ROOT import TFile, gROOT
myFile = TFile("experiment.root")
tree1 = gROOT.FindObject("tree1")

Note

The first command imports specific ROOT classes into Python (see the previous page).

That third command means: Look through everything we’ve read in (the “everything” is gROOT) and find the object whose name is tree1.

If you’ve done The C++ Path, note that in Python we have to read in the n-tuple explicitly.

In a notebook, you can’t use the Scan method to look at the contents of the tree, but you can display the names of the variables and the size of the TTree:

tree1.Print()

You can see that the variables stored in the TTree are event, ebeam, px, py, pz, zv, and chi2.

Create a histogram of one of the variables. For example:

    from ROOT import TCanvas
    my_canvas = TCanvas()
    tree1.Draw("ebeam")
    my_canvas.Draw()

While we have to explicitly Draw a canvas, we can re-use a previously-defined canvas (the same way command-line ROOT keeps re-using c1).

Using the Draw commands, make histograms of the other variables.


1

I duplicate some of the descriptions from the Treeviewer discussion, in case you decided to rush into programming and skip the simple tools.

2

If you’re experienced with Python, you may ask why I’m not including NumPy, SciPy, and Matplotlib in this tutorial. I want to focus on the ROOT toolkit, even though many tasks (especially in the Advanced Exercises and Expert Exercises) can be more easily accomplished using those additional packages. I wrestled with this issue for a while, before deciding that there are hundreds of web pages on these standard Python tools but few sites on ROOT. But I may change my mind next year!