Walkthrough: Executing the AnalyzeReader macro

(10 minutes)

As it stands, the AnalyzeReader macro does nothing, but let’s learn how to run it anyway. Quit ROOT, then start it again with the name of our macro:

> root AnalyzeReader.C

ROOT will start, then automatically interpret and execute AnalyzeReader.C. If you want to run it again without quitting ROOT, use the .x command:

[] .x AnalyzeReader.C

If you make changes to AnalyzeReader.C then execute it with .x again, ROOT will interpret your changes.1

Since we haven’t included any analysis code yet, you won’t see anything happen. We’ll remedy that in the next section.

Understanding the commands

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!

Let’s review what’s happening:

  • When you use .x (for “eXecute”), you cause ROOT’s C++ intepreter to read your file.

  • If the name of the function in the file is the same as the file’s name (without the `.C), ROOT will automatically run that function.

  • As you saw in Listing 42, the function AnalyzeReader does the following:

    • Uses TFile to create a pointer to the file experiment.root.

    • Create a TTreeReader to access an n-tuple in the file.

    • Create one TTreeReaderValue pointer for each variable we plan to use.

    • Executes the loop code for each entry in the tree, referring to the variables you defined with TTReaderValue as pointers.

    • Executes your wrap-up code.

If you went through the TSelector macro part as well as this one, you’ll notice that this AnalyzeReader.C macro is faster. Yay!

The rest of the C++ Path

The subsequent sections of the tutorial are written with the TSelector approach in mind. Since you’re a C++ programmer, I’ll assume you’ll be able to interpolate to the execution instructions I give above.

For the most part, this won’t be an issue; I’ll refer to necessary program edits as taking place in the “Definition” section, the “Loop” section, or whatever. Any such edits would be same in both approaches.

Just in case, I’ll remind you of a couple of things:

  • When you see something like “You can copy the file AnalyzeExercise5.C from my area,” be mindful that any such file will be a TSelector-style macro, not a TTreeReader-style macro.

  • When you see MakeSelector, that doesn’t apply to you. Just make a copy of AnalyzeReader.C and edit the new copy; e.g.,

    > cp AnalyzeReader.C AnalyzeExercise6.C
    # edit AnalyzeExercise6.C
    > root AnalyzeExercise6.C
    [] .x AnalyzeExercise6.C
    
  • Every time you see a mention of an n-tuple variable that you haven’t used before, you have to define a TTreeReaderValue for it. The users of TSelector have this defined in their file Analyze.C, where they don’t have to look at it.

  • I talk about GetEntry in a few places. You don’t use this in your “Loop” section.

  • The students working with TSelector have to restart ROOT every time they edit their macro. You don’t have to do this.


1

That is, if you’ve used your text editor to save the changes in the file. Every once a while a student tells that ROOT didn’t recognize something that they changed, and it turns out the student forgot to do File ‣ Save.