C++ Walkthrough: Running the Analyze macro (10 minutes)

As it stands, the Analyze macro does nothing, but let’s learn how to run it anyway. Quit ROOT, start it again, and enter the following lines:

[] TFile myFile("experiment.root")
[] tree1->Process("Analyze.C")


Get used to these commands. You’ll be executing them over and over again for the next several exercises. Remember, the up-arrow and tab keys are your friends!1

Let’s examine each of those commands:

  • TFile myFile("experiment.root") – tells ROOT to load the file experiment.root into memory. This saves you from creating the TBrowser and double-clicking on the file name every time you start ROOT (and you’ll be restarting it a lot!).

  • tree1->Process("Analyze.C") – load Analyze.C and run its analysis code on the contents of the tree. This means:

    • load your definitions;

    • execute your initializations;

    • execute the loop code for each entry in the tree;

    • execute your wrap-up code.

After the second command, ROOT will pause as it reads through all the events in the tree. Since we haven’t included any analysis code yet, you won’t see anything happen.


Take another look at Analyze.h, also called a “header file.” (Analyze.C is the “implementation file.”) If you scan through it, you’ll see C++ commands that do something with “branches,” “chains,” and loading the variables from a tree. Fancy stuff, but you don’t have to know about any of the nitty-gritty details. Now go back and look at the top of Analyze.C. You’ll see the line

#include "Analyze.h"

This means ROOT will include the contents of Analyze.h when it loads Analyze.C. This takes care of defining the C++ variables for the contents of the tree.


If you’re a real ROOT honcho (and I know you want to be), there’s an even faster way to do this. When I work through the exercises in this course, I start ROOT with this command:

> root experiment.root

This means to run ROOT and to open file experiment.root right away. I can omit the TFile command and get to work.

If you want to be even faster (and who doesn’t want to be?) you can sometimes skip messing with the TBrowser. After you’ve opened a file, try the ROOT interpreter command:

[] .ls

This will list the contents of the file on the terminal.