ROOT/Getting Started/Many Ways to Use ROOT

In this section we'll have a look at the many ways to execute code in ROOT.

The ROOT Command Line
If you start a ROOT session on a terminal, you'll get an interpreter that takes your code and executes it line after line. For example, let's try the “hello world” program:

Well, wasn't that simple? Let's see what happened. As you hit enter, line 0 (indicated by the  at the beginning) was sent to the CINT who executed it immediately. CINT is a C++ interpreter developed by Masaharu Goto (Agilent Technologies), Philippe Canal and Paul Russo (Fermilab) and Leandro Franco, Diego Marcos, and Axel Naumann (CERN). An interpreter is a software that executes code line after line rather than compiling it all at once. This has the advantage that you needn't wait for the compiler and can see / check the result immediately. On the other hand, interpreting code is much slower than executing compiled code.

The interpreter is smart enough to wait for you to finish your command, even if you hit enter before. This is useful when entering loops as we can see in the following example:

Note that I have used the machine independent types  and   here that are granted to be the same on any machine.

You should also have noted that,   and   worked as expected even though we never included the respective header files nor resolved the namespace. This is because the code isn't compiled and CINT knows the standard C++ functions.

(Making and) Correcting Mistakes
An other nice thing about using the ROOT command line is that if you make a mistake, you'll be informed immediately and can correct it easily.

In this example I have forgotten the second  in line 0. The interpreter told me and I could recall the last line by pressing uparrow, correcting the mistake and executing it again.

It should be said that CINT is quite tolerant and allows you to use input that normally wouldn't be valid C++. However, I clearly discourage to use this option since it will make your code look weired and harder to understand, more likely to fail and impossible to compile if wished later. Therefore this “features” will not be presented here.

Auto Completion
An other very useful tool is auto completion. For example, try typing  and then hit Tab. The word will be completed to  which is still ambiguous. If you hit Tab again, you'll be shown the four possibilities the interpreter knows. Those are the random classes,  ,   and. We'll see later what they are good for.

Especially if you want to try something new and are not very sure about the names to use this can be an enormous helpful support.

ROOT Macros
You have now seen how to use the ROOT command line. It is a nice thing, especially if you want to try something out and are not absolutely sure about the syntax. However, if you're solving a more complex problem, you'll probably want to keep your code stored in a file you can edit and execute multiple times. So what would be more convenient than storing the commands in a script file and telling ROOT to execute them all? Here we go:

Create a text file named. In this file you can put any commands you could have been typing during a session. To start simple, let's do the hello world thing. In this case you would write

The script file must contain a  function that is named the same as the file. When ROOT executes your script file, it will call this function. To do so, open a ROOT session in the directory you've saved the script and say

Please note that the code is still interpreted line by line and not compiled. Except that you have to put everything in the  function there is no difference to using the command line. In particular, it is not necessary to resolve namespaces (as e.g. for  in  ).

Passing Parameters
You can pass on parameters to the macro you call. Consider the following script:

You can call it via

It is even possible to overload the function. For example, we could define both  and. Now either of the two calls

and

would be legal.

Compiling Code on the Fly
Macros will do well for almost anything you'll want to do at the beginning. But they have a huge disadvantage: They run slow! (And some people also observed strange behavior under some circumstances&#133;) ROOT however has very sophisticated routines implemented that ensure to make the most out of your machine's resources. To use them you must compile your code.

We just look at the script we've written before. To make it valid C++ we must be aware of namespaces and header files now. (If you were using messy code that was still accepted by CINT—I've warned you—then you'll have to correct it now. Otherwise AcLiC won't be able to compile.) So our modified file should look like this:

Now we can execute it via

The output is of course the same. The “+” at the end told ROOT to compile the code before running it. This was done by a program called ACLiC („The Automatic Compiler of Libraries for CINT“). ACLiC is a smart tool that makes use of your installed compiler and builds up and reuses a library from your compiled code. You can, however, tell ACLiC to build up the library from scratch even if not necessary by adding “++” at the end.

Another good news is that even if you adopted your script in a way that it can be compiled by ACLiC, it is still possible to
 * pass on parameters. Syntax:    
 * interpret it by CINT without compiling.

Building a Stand-Alone Application
The most advanced way to run your ROOT code is to make it a stand alone application. Remember that ROOT basically is a collection of C++ classes. These can be used to build brand new applications that may be compiled and executed without depending on the original ROOT install anymore. I will show how to compile using g++.

To run g++ on our file it must meet the C++ standard. That is in particular, it must contain an  function. One could of course simply rename the former  method but there is a more elegant way to go. If we'd rename the function then the code can be compiled but no longer interpreted. (No function named  is allowed in interpreted code since it would conflict with the internal functions from CINT.) On a happy note, CINT defines the preprocessor variable. Knowing this we can add a  function that will only be visible when g++ processes the code but not when CINT does. Here is an example:

From CINT's point of view, the file didn't change at all. When g++ compiles it, it will make  call the   function—just what CINT otherwise would have done.

To compile the file, say

and then execute it with

without caring about ROOT at all. On compiling time, ROOT will take care for you that all links are set correctly.

However, you might be a little disappointed because what we actually wrote is nothing but a hello world program that doesn't make use of any ROOT resources at all. Okay, here is another simple example that makes use of the build in data type  and the random class.