Walkthrough: Calculate our own variables

(10 minutes)

Let’s calculate our own values in an analysis macro, starting with pt, from our Treeviewer exercise. Let’s begin with a fresh analysis skeleton, using the command that suits the macro path you took:

[] tree1->MakeSelector("AnalyzeVariables")

or

> cp ~seligman/root-class/AnalyzeReader.C AnalyzeVariables.C
# This cryptic command changes AnalyzeReader to AnalyzeVariables in the file
> sed -i -e "s/AnalyzeReader/AnalyzeVariables/g" AnalyzeVariables.C

In the Loop section, put in the following line (remember: all the n-tuple variables are pointers):1

double pt = TMath::Sqrt((*px)*(*px) + (*py)*(*py));

What does this mean?

Whenever you create a new variable in C++, you must say what type of thing it is. We’ve already done this in statements like

TF1 func("user","gaus(0)+gaus(3)")

This statement creates a brand-new variable named func, with a type of TF1. In the Loop section of AnalyzeVariables, we’re creating a new variable named pt, and its type is double.

For the purpose of the analyses that you’re likely to do, there are only a few types of numeric variables that you’ll have to know:

  • float is used for real numbers.

  • double is used for double-precision real numbers.

  • int is used for integers.

  • bool is for boolean (true/false) values.

  • long long int specifies 64-bit integers, which you probably won’t need to use.

Most physicists use double precision for their numeric calculations, just in case.

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 TMath::Sqrt() computes the square root of the expression within the parenthesis “()”.2

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


1

For a MakeSelector macro, You also have to put in that GetEntry line. I complained about this earlier.

2

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

#include <cmath>
# ... fetch px and py
pt = std::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 analysis, finding polynomial roots).