C++ Walkthrough: Applying a cut (10 minutes)


The last “trick” you need to learn is how to apply a cut in an analysis macro. Once you’ve absorbed this, you’ll know enough about ROOT to start using it for a real physics analysis.

The simplest way to apply a cut in C++ is to use the if statement. This is described in every introductory C and C++ text, and I won’t go into detail here. Instead, I’ll provide an example to get you started.

Once again, let’s start with a fresh macro:

[] tree1->MakeSelector("AnalyzeCuts")

Our goal is to count the number of events for which pz is less than 145 GeV. Since we’re going to count the events, we’re going to need a counter. Put the following in the Definition section of AnalyzeCuts.C:

Int_t pzCount = 0;


Why Int_t and not Long64_t? I find that Int_t is easier to remember. I could even “cheat” and just use int, which will work for this example. You would only have to use the type Long64_t if you were counting more than \(2^{31}\) entries. I promise you that there aren’t that many entries in this file!1

For every event that passes the cut, we want to add one to the count. Put the following in the Process section:

if ( (*pz) < 145 )
    pzCount = pzCount + 1; // you could use "pzCount++;" instead


Be careful: it’s important that you surround the logical expression (*pz) < 145 with parentheses “()”, but the “if-clause” must use curly brackets “{}”.

Now we have to display the value. Again, I’m going to defer a complete description of formatting text output to a C++ textbook, and simply supply the following statement for your Wrap-up section:

std::cout << "The number of events with pz < 145 is "
    << pzCount << std::endl;


When I run this macro, I get the following output:

The number of events with pz < 145 is 14962

Hopefully you’ll get the same answer.


Recall that in the lecture I gave at the start of the class, I mentioned that other commonly used data-analysis programs couldn’t handle a large number of events. Can you picture an Excel spreadsheet with more than \(2^{31}\) rows? ROOT can handle datasets with up to \(2^{63}\) entries!

Having trouble visualizing powers of 2? Remember that \(2^{10} \approx 10^{3}\), so \(2^{63} = 2^{3} \times (2^{60}) = 2^{3} \times \left(2^{10}\right)^{6} \approx 2^{3} \times \left(10^{3}\right)^{6} = 8*10^{18}\) or about eight quintillion, roughly the number of grains of sand in the world. My claim “ROOT can handle datasets with up to \(2^{63}\) entries” is theoretical rather than practical.