Python 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 is to use the if statement. This is described in every introductory Python 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 Analyze script:


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 Set-up section:

pzCount = 0

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

    if ( pz < 145 ):
        pzCount = pzCount + 1


Be careful: Remember that indentation is important. The next statement after pzCount=pzCount+1 must not be indented the same amount, or it will be considered part of the if statement.

Now we have to display the value. Include the following statement in your Wrap-up section:1

print ("The number of events with pz < 145 is", pzCount)


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.


If you’re using Python 2, you may have to omit the parentheses around the arguments of the print statement:

print "The number of events with pz < 145 is", pzCount