Programming in ROOT


There are 4 ways to execute commands in ROOT.


1) Enter each line at the ROOT prompt:

Root[0] {

Root[1] int j = 0;

Root[2] for (int I = 0; I<3;I++)

Root[3] {

Root[4] j=j+1;

Root[5] cout <<" I = "<< I<< , " j =" << j << endl;

Root[6] }

Root[7] }

I = 0, j = 0

I = 1, j = 1

I = 2, j = 3


2) Create an un-named script.

To do this pick you favorite editor and create a file, e.g.:

>xemacs example.C

In the file example.C you can put the exact same thing you saw above

{

int j = 0;

for (int I = 0; I<3;I++)

{

j=j+1;

cout << "I =" << I<< , " j = "<< j << endl;

}

}

then in ROOT type

root[0] .x example.C

I = 0, j = 0

I = 1, j = 1

I = 2, j = 3

The ".x" command will load and execute the un-named script


3) Create a named script.

To do this pick you favorite editor and create a file, e.g.:

>xemacs example2.C

and create a C++ like program

#include <iostream.h>

int main()

{

int j = 0;

for (int I = 0; I<3;I++)

{

j=j+1;

cout << "I =" << I<<," j = "<< j << endl;

}

            return 0;

}

then in ROOT type

root[0] .L example2.C

root[1] main()

I = 0, j = 0

I = 1, j = 1

I = 2, j = 3

The first line will load the script or scripts into memory and the second line will execute the script called main.


4) Create a stand-alone program.

For the above example one could just type

> g++ -o example2 example2.C

>example2

I = 0, j = 0

I = 1, j = 1

I = 2, j = 3

Of course this one is independent of ROOT.  One can make stand-alone programs that create or manipulate ROOT objects.  See examples.

back