Walkthrough: Calculating 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:

[] tree1->MakeSelector("AnalyzeVariables")

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

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

Note

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 Process section of AnalyzeVariables, we’re creating a new variable named pt, and its type is Double_t.

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_t is used for real numbers.

  • Double_t is used for double-precision real numbers.

  • Int_t is used for integers.

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

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

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

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 “()”.3

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

You also have to put in that GetEntry line, which I complained about earlier.

2

If you already know C++: the reason why I don’t simply tell you to use the built-in types float, double, int, and bool is discussed in Chapter 2 of the ROOT Users Guide.

However, if you decide to use the simpler names for the numeric types, I won’t mind. You’re not likely to use any of the code in this tutorial in another C++ compiler. But bear in mind that when you start programming “for real”, it’s not certain which compilers you’ll use in the future.

3

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).