Python Walkthrough: Calculating our own variables (10 minutes)


There are other quantities that we may be interested in apart from the ones already present in the n-tuple. One such quantity is \(p_{T}\) which is defined by:

\[p_{T} = \sqrt{ p_{x}^{2} + p_{x}^{2} }\]

This is the transverse momentum of the particle, that is, the component of the particle’s momentum that’s perpendicular to the z-axis.

Let’s calculate our own values in an analysis macro. Load a fresh copy of that script into your notebook:


In the Loop section, put in the following line:

pt = ROOT.TMath.Sqrt(px*px + py*py)


Did that not work? To get at the variables px and py, you have fetch them from the n-tuple with something like mychain.px. You also have to have either import ROOT or from ROOT import TMath.

ROOT comes with a very complete set of math functions. You can browse them all by looking at the TMath class on the ROOT web site, or Chapter 13 in the ROOT User’s Guide. For now, it’s enough to know that ROOT.TMath.Sqrt() computes the square root of the expression within the parenthesis “()”.1

Test the script to make sure it runs. You won’t see any output, so we’ll fix that in the next exercise.

xkcd location_sharing

Figure 34: by Randall Munroe


To be fair, there are Python math packages as well. I could have asked you to do something like this:

import math
# ... fetch px and py
pt = math.sqrt(px*px + py*py)

The reason why I ask you to use ROOT’s math packages is that I want you to get used to looking up and using ROOT’s basic math functions (algebra, trig) in preparation for using its advanced routines (e.g., fourier transforms, multi-variant analysis).