Exercise 11: Stand-alone program (optional) (60 minutes or more if you don’t know C++)

Note

Why would you want to write a stand-alone program instead of using ROOT interactively? Compiled code executes faster; maybe you’ve already learned about the techniques described in chapter 7 of the ROOT User’s Guide. Stand-alone programs are easier to submit to batch systems that run in the background while you do something else. The full capabilities of C++ are available.

I’ll be honest with you: I’m spending all this time to teach you about interactive ROOT, but I never use it. I can develop code faster in a stand-alone program, without restarting ROOT or dealing with a puzzling error message that refers to the wrong line in a macro.

If it’s near the end of the last day of this course, don’t bother to start this exercise. But if you have an hour or more – well, you’re pretty good. This exercise is a bit of a challenge for you.

So far, you’ve used ROOT interactively to perform the exercises. Your task now is to write a stand-alone program that uses ROOT. Start with the macro you created in Exercise 10: you have a ROOT script (a “.C” file) that reads an n-tuple, performs a calculation, and writes a plot to a file. Create, compile, and run a C++ program (a “.cc” file) that does the same thing.

Note

You can’t just take Analyze.C, copy it to Analyze.cc, and hope it will compile. For one thing, Analyze.C does not have a main routine; you will have to write one. Also, C++ doesn’t know about the ROOT classes; you have to find a way to include the classes in your program (here’s a hint).

xkcd blorg

Figure 26: https://xkcd.com/1513/ by Randall Munroe

Note

When you try to compile the program, the following simple attempt won’t work:

> g++ Analyze.cc -o Analyze

You will have to add flags to the g++ command that will refer to the ROOT header files and the ROOT libraries. You can save yourself some time by using the root-config command. Use its --help option to see what it can do:

> root-config --help

Try it:

> root-config --cflags
> root-config --libs

Is there were a way of getting all that text into your compilation command without typing it all over again? This is where the UNIX “backtick” comes in handy: it executes whatever command is between the backticks, and returns the output as a text string. Try:

> g++ Analyze.cc -o Analyze `root-config --cflags`

Be careful as you type this; it’s not the usual single quote (’) but the backtick (`), which is typically located in the upper left-hand corner of a computer keyboard.1

Are things still not working? Maybe I want you to think about adding more than one argument to a single command.

That’s enough hints.

xkcd compiling

Figure 27: https://xkcd.com/303/ by Randall Munroe


1

This is esoteric and you probably don’t need to know this, but you know how much I hate to lie to you:

In the UNIX shells bash, zsh, and ksh, a better approach to capturing a command’s output is to use $(command) instead of `command`; e.g., $(root-config --cflags). But the backtick also works in environments where $() does not, e.g., in tcsh and scripting languages like Perl and Ruby.

On Nevis particle-physics systems, most people are set up with bash or zsh. The electronics-design group uses tcsh (since that’s what the design group at CERN uses). If you’re not at Nevis, I have no idea which shell you’re using, which is why I went with the backticks.