# 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 root [0] 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 Float_t and Int_t here that are granted to be the same on any machine.
You should also have noted that cout, sin and cos 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 TRa and then hit Tab. The word will be completed to TRandom which is still ambiguous. If you hit Tab again, you'll be shown the four possibilities the interpreter knows. Those are the random classes TRandom, TRandom1, TRandom2 and TRandom3. 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.
* Exercise: Trying Interactive ROOT
* On an interactive ROOT session: Fill an array of 100 000 000 64Bit floating point numbers of the machine independent ROOT type Double_t with random numbers. Compute the mean of the array and print it to the console. Note the computation time.
* Hint: You can get random real numbers by creating a pointer to an instance of the TRandom class. (The constructor takes one arbitrary integer as initialization.) Then you can call the method TRandom::Rndm to get a simple pseudo random number uniformly distributed between 0 and 1. For example:
* TRandom *R = new TRandom(time(0)); // create a pointer to a new instance of TRandom in the heap
* cout << R->Rndm << endl;

## 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 helloscript.cc. 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 void 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 void function there is no difference to using the command line. In particular, it is not necessary to resolve namespaces (as e.g. for cout in iostream).

### 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 void hello and void hello(Int_t times). Now either of the two calls
and
would be legal.
* Exercise: Interpreted ROOT Macros
* Consider the problem solved in the previous exercise. Now write a script that does the same thing and execute it as interpreted macro. Does it run faster?
* Modify the script in a way that the user can pass the amount of random numbers to be created as a parameter.
* Overload the macro such that the amount of numbers can still be passed but if no parameter is given, 100 000 000 numbers will be generated.

## 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: .x <script>.cc+(<parameter>)
* interpret it by CINT without compiling.
* Exercise: Compiled ROOT Macros
* Use the script you've written before and modify it so it can be run as compiled macro via ACLiC.
* Make sure that both, calling with and without parameter, will still work.
* Can your script still be interpreted by CINT after you've changed it?
* How is the performance of the compiled macro for 100 000 000 numbers? What is faster for 5 numbers?

## 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 int main function. One could of course simply rename the former void 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 int main is allowed in interpreted code since it would conflict with the internal functions from CINT.) On a happy note, CINT defines the preprocessor variable __CINT__. Knowing this we can add a main 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 main call the void 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 Double_t and the random class TRandom.
* Exercise: A Simple Stand-Alone Application
* Once again, consider the code you should have previously written to compute the mean of an array of random numbers. Now, last but not least, make it a stand-alone application and run it independently of any ROOT session. Make sure that passing parameters will still work. Check the performance of your application. Take also care that your modifications don't disturb interpretation or on-the-fly compilation by CINT or ACLiC.

## References

* ↑ CERN: CINT. http://root.cern.ch/drupal/content/cint