Two versions of ROOT are available on taurus server. We will use newest. To start the session:

> ssh -XY taurus

> source ~tbold/ROOT/bin/thisroot.sh

> root

> .q

File creation in an interactive session:

f = TFile::Open(“output.root”, “NEW”) // from that moment all histograms created belong to that file

h = TH1F(“h”, “test”, 20, -5, 5)

h.Fill(0)

h.Draw()

f->Write()

f->Close()

Reading histograms file in an interactive session:

> root output.root

h->Draw()

Or

> root

f = TFile::Open(“output.root”, “OLD”)

h->Draw()

2) Anonymous scripts:

 To the above example we want to add code that will generate random numbers and fill the histogram “h” with them (this code needs to be there after histogram “h” creation and before file saving):

g = TF1(“g”, “gaus”)

g.SetParameter(0, 2)

g.SetParameter(1, 1)

g.SetParameter(2, 0.4)

for ( int i=0; i < 10000; ++i ) {

        h->Fill(g.GetRandom());

}

This code should be placed in a file, say: “s.C” and can be run with
To be able to overwrite the output file the NEW has to be replaced by RECREATE.

> root -q s.C

Once the script is execute the file output.root can be opened and the histogram drawn.

3) Named scripts:

The goal is to convert whole script to so called “named script”. That means all the code should be inside the function  “void s.C() {...here...} “. ATTENTION: the script name and function name need to be identical.

Extend the function s with arguments allowing to pass the parameter(s) of the gaussian. Say we do only one which is the width.

void s(double width) { … make use of tha width parameter)

Now execute the function:

> root -q “s.C(4.5)”

And check the output.root

> root -q “s.C(1)”

And check the output.

4) Compiled named scripts:

 While in interactive ROOT session (> root -l )  try the:

.L s.C+

At this stage you need to fix all c++ issues in the script. Typically one has to add all includes.

I.e. missing #include <TH1F.h> for the histogram class.

Try execute it once successfully compiled and loaded.

.x s.C+(0.7)

Try it from command line:

> root -l -q -b “test.C+(0.5)”

 Try other options ++ +g +O

5) External library:

The goal is to create library allowing us to compile the script as a standalone library.

We need to generate a dictionary with rootcint first.

Start with creating s.h containing only the “s” function declaration and then create LinkDef.h file like this one:

#ifdef __CINT__
#include "s.h"
#pragma link C++ function s;  // here we say that s is a function

// if we have a class  “blah” that would be: #pragma link c++ class blah+;
#endif





We can generate the dictionary:

rootcint -f sDict.C -c -p  s.h LinkDef.h


And compile (-shared):

g++ `root-config --cflags --libs` s.C sDict.C -shared -o libs.so

Now we will try to use it in an interactive session.

First we want to load the library:

> root -l

.L libs.so

s(0.6)

And after that we can again check the output.root  file content.