General Remarks on ROOT macros

You know how other books go on and on about programming fundamentals and finally work up to building a complete, working program ? Let's skip all that. In this guide, we will describe macros executed by the ROOT C++ interpreter Cling.

It is relatively easy to compile a macro, either as a pre-compiled library to load into ROOT, or as a stand-alone application, by adding some include statements for header file or some "dressing code" to any macro.

General Remarks on ROOT macros[edit | edit source]

If you have a number of lines which you were able to execute at the ROOT prompt, they can be turned into a ROOT macro by giving them a name which corresponds to the file name without extension. The general structure for a macro stored in file MacroName.C is

void MacroName() {
        <          ...
          your lines of C++ code
                   ...             >
}

The macro is executed by typing

 > root MacroName.C

at the system prompt, or executed using .x

 > root
 root [0] .x MacroName.C

at the ROOT prompt. or it can be loaded into a ROOT session and then be executed by typing

root [0].L MacroName.C
root [1] MacroName();

at the ROOT prompt. Note that more than one macro can be loaded this way, as each macro has a unique name in the ROOT name space. A small set of options can help making your plot nicer.

gROOT->SetStyle("Plain");   // set plain TStyle
gStyle->SetOptStat(111111); // draw statistics on plots,
                            // (0) for no output
gStyle->SetOptFit(1111);    // draw fit results on plot,
                            // (0) for no ouput
gStyle->SetPalette(57);     // set color map
gStyle->SetOptTitle(0);     // suppress title box
   ...

Next, you should create a canvas for graphical output, with size, subdivisions and format suitable to your needs, see documentation of class TCanvas:

TCanvas c1("c1","<Title>",0,0,400,300); // create a canvas, specify position and size in pixels
c1.Divide(2,2); //set subdivisions, called pads
c1.cd(1); //change to pad 1 of canvas c1

These parts of a well-written macro are pretty standard, and you should remember to include pieces of code like in the examples above to make sure your plots always look as you had intended.

Below, in section Interpretation and Compilation, some more code fragments will be shown, allowing you to use the system compiler to compile macros for more efficient execution, or turn macros into stand-alone applications linked against the ROOT libraries.