ROOT is a powerful software framework designed for High Energy Physics experiments.

Colors, Markers, Styles

Basic

ROOT has its own website, http://root.cern.ch

On this website, the Tutorial would be useful for beginner; User's Guide will help you understand the concept of ROOT. I myself found Class index also useful.

Parameters

By default the parameters for a gaussian fit are:

0 : Constant
1 : Mean
2 : Sigma

Static/Dynamic construction of a TFile

If you draw some histograms while a file is open, then these hists belongs to the directory of the file; if the file is created via static memory alloc other than a dynamic one, then ROOT will automatically delete it when your script ends, so sometimes your plot won't show up.

gSystem and etc:

gSystem->ProcessEvents();

Histograms, Graphs, etc:

To avoid auto saving a hist to memory, use
TH1::AddDirectory(0);

when drawing the hist from ntuple, use Entry$ for entry number

hist Fitting options

Parameters must be initialized before invoking the Fit function.

The setting of the parameter initial values is automatic for the predefined functions : poln, expo, gaus, landau. One can however disable this automatic computation by specifying the option "B".

Note that if a predefined function is defined with an argument, eg, gaus(0), expo(1), you must specify the initial values for the parameters.

You can specify boundary limits for some or all parameters via f1->SetParLimits(p_number, parmin, parmax);

if parmin>=parmax, the parameter is fixed

following are the options:

         option = "W"  Set all errors to 1
                = "I" Use integral of function in bin instead of value at bin center
                = "L" Use Loglikelihood method (default is chisquare method)
                = "LL" Use Loglikelihood method and bin contents are not integers)
                = "U" Use a User specified fitting algorithm (via SetFCN)
                = "Q" Quiet mode (minimum printing)
                = "V" Verbose mode (default is between Q and V)
                = "E" Perform better Errors estimation using Minos technique
                = "B" Use this option when you want to fix one or more parameters
                      and the fitting function is like "gaus","expo","poln","landau".
                = "M" More. Improve fit results
                = "R" Use the Range specified in the function range
                = "N" Do not store the graphics function, do not draw
                = "0" Do not plot the result of the fit. By default the fitted function
                      is drawn unless the option"N" above is specified.
                = "+" Add this new fitted function to the list of fitted functions
                      (by default, any previous function is deleted)
                = "C" In case of linear fitting, don't calculate the chisquare
                      (saves time)
                = "F" If fitting a polN, switch to minuit fitter

Fit while excluding certain range

Double_t fline(Double_t *x, Double_t *par)
{
    if (x[0] > 2.5 && x[0] < 3.5) {
      TF1::RejectPoint();
      return 0;
   }
   return par[0] + par[1]*x[0];
}

void fitExclude() {
   //Create a source function
   TF1 *f1 = new TF1("f1","[0] +[1]*x +gaus(2)",0,5);
   f1->SetParameters(6,-1,5,3,0.2);
   // create an histogram and fill it according to the source function
   TH1F *h = new TH1F("h","background + signal",100,0,5);
   h->FillRandom("f1",2000);
   TF1 *fline = new TF1("fline",fline,0,5,2);
   fline->SetParameters(2,-1);
   //we want to fit only the linear background excluding the signal area
   h->Fit("fline","l");
}

Graph options

  chopt='L' :  A simple polyline between every points is drawn

  chopt='F' :  A fill area is drawn ('CF' draw a smooth fill area)

  chopt='A' :  Axis are drawn around the graph

  chopt='C' :  A smooth Curve is drawn

  chopt='*' :  A Star is plotted at each point

  chopt='P' :  Idem with the current marker

  chopt='B' :  A Bar chart is drawn at each point

  chopt='1' :  ylow=rwymin

  chopt='X+' : The X-axis is drawn on the top side of the plot.

  chopt='Y+' : The Y-axis is drawn on the right side of the plot.

options specific for TGraphErrors:

"Z" option to leave off the small lines at the end of the error bars.

">" an arrow is drawn at the end of the error bars

"|>" a full arrow is drawn at the end of the error bars the size of the arrow is set to 2/3 of the marker size.

[] option is interesting to superimpose systematic errors on top of the graph with the statistical errors. When it is specified, only the end vertical/horizontal lines of the error bars are drawn.

To control the size of the error along x use: gStyle->SetErrorX(dx); Set dx=0 to suppress the error along x. To control the size of the lines at the end of the error bars (when option 1 is chosen) use SetEndErrorSize(np). By default np=1; np represents the number of pixels. gStyle->SetEndErrorSize(np);

a code stub for fitting, drawing with errors:

int
pi0MassRun(const char* rootList = "rootfile.lst")
{
  char rootFile[500];
  ifstream runlist1(rootList);
  int count = 0;
  int temp = 0;
  double run[MAXFILECOUNT], runerr[MAXFILECOUNT];
  double mass[MAXFILECOUNT], width[MAXFILECOUNT], masserr[MAXFILECOUNT], widtherr[MAXFILECOUNT];
  c1 = new TCanvas("c1", "temp");
  while (runlist1.getline(rootFile, 500)) {
    sscanf(rootFile, "batch/%d.root", &temp);
    run[count] = temp;
    runerr[count] = 0;
    //cout<<"file name "<<rootFile<<" run number "<<run<<endl;

    TFile* f = new TFile(rootFile);
    if(f->IsZombie()) {
      cout<<"########## root file "<<rootFile<<" fail to read ########## "<<endl;
    } else {
      if(!T) {
        cout<<"########## cannot get Tree from "<<rootFile<<" ##########"<<endl;
      } else {

        TTree* T = (TTree*)f->Get("T");
        T->Draw("mass>>histM(100, 0, 0.3)","mass<0.3&&pt>1");
        double par[7];
        TH1F* histM = gDirectory->Get("histM");
        TF1* sigback = new TF1("sigback", "pol3(0)+gaus(4)", 0.05, 0.25);
        TF1* sig = new TF1("sig", "gaus", 0.11, 0.17);
        TF1* back = new TF1("back", "pol3", 0.05, 0.25);
        histM->Fit("sig", "QNR");
        histM->Fit("back", "QNR+");
        back->GetParameters(&par[0]);
        sig->GetParameters(&par[4]);
        sigback->SetParameters(par);
        histM->Fit("sigback","QNR+");
        mass[count] = sigback->GetParameter(5);
        width[count] = sigback->GetParameter(6);
        masserr[count] = sigback->GetParError(5);
        widtherr[count] = sigback->GetParError(6);

        cout<<"run# "<<run[count]<<" mass "<<mass[count]<<" width "<<width[count]
            <<" masserr "<<masserr[count]<<" widtherr "<<widtherr[count]
            <<endl;
        count++;
      } // if !T
    } // if file zombie
    f->Close();
  }
  delete c1;
  cout<<" -------- processed "<<count<<" files --------"<<endl;

  c2 = new TCanvas("c2", "Mass vs Run");
  //TGraph* g = new TGraph(count, run, mass);
  TGraphErrors* gr = new TGraphErrors(count, run, mass, runerr, masserr);
  gr->SetTitle("Mass vs Run;Run;Mass(GeV)");
  gr->SetMarkerColor(4);
  gr->SetMarkerStyle(21);
  gr->Draw("AP");

  return 1;
}

save Canvas to a file

  c1 = new TCanvas("c1");
  ...
  // draw something on c1
  ...
  gSystem->ProcessEvents();
  TImage* img = TImage::Create();
  img->FromPad(c1);
  char gifname[255];
  sprintf(gifname, "temp.gif");
  img->WriteImage(gifname);
  cout<<"+++ canvasAll saved to "<<gifname<<" +++"<<endl;
  delete img;
or quickly,
c1->SaveAs();

Trees, Ntuples, etc.

2 examples of how to build a new tree using selected branch or keys from a old tree

http://root.cern.ch/root/html/examples/copytree2.C.html

http://root.cern.ch/root/html/examples/copytree2.C.html

TCollections, TClonesArray, etc.

Drawings

To draw a label, a example
a = new TPaveText(0.1,0.1,0.9,0.9, "NDC");
a->AddText("line1");
a->AddText("line2");
a->Draw();

Misc

macros in ROOT
goto $ROOTSYS/build/misc you will find two scripts, one for emacs to help you writing projects using ROOT; one for m4 so you can include it in your aclocal.m4 to search any installed ROOT distribution.

undefined symbol problem in ROOT

http://root.cern.ch/phpBB2/viewtopic.php?t=4778

to find out the lib to load

symbols=`nm -C ~/build/lib/libgetAUT.so | grep -E ' U T[[:alnum:]_]+::' | sed 's,^.* U \([[:alnum:]_]\+\)::.*$,(\1),'| sort | uniq | tr '\n' '|'`; (for so in $ROOTSYS/lib/*.so; do nm -C -D --defined-only $so | grep -E 'T ('$symbols')::' > /dev/null && echo $so; done )| sort | uniq

writing TCloneArray

TBranch *branch = tree->Branch(branchname,clonesarray, bufsize, splitlevel)

language bindings

Python

as described in the user's guide

Ruby

There is an official one also in the user's guide

probably some others using SWIG

http://alcaraz.web.cern.ch/alcaraz/RubyRoot/

Java

http://sarkar.web.cern.ch/sarkar/jroot/main.html

Perl

the SWIG way : http://www.desy.de/~salvaire/root_wrapper.html

the one that calls the Inline::Python to use PyROOT : http://sarkar.web.cern.ch/sarkar/PerlRoot/index.html

some references http://root.cern.ch/phpBB2/viewtopic.php?p=2403

http://root.cern.ch/phpBB2/viewtopic.php?p=7899

http://root.cern.ch/phpBB2/viewtopic.php?t=2229

SWIG

http://www.swig.org/

http://www.swig.org/papers/Perl98/swigperl.htm

http://www.swig.org/Doc1.1/PDF/SWIGManual.pdf

http://www.swig.org/Doc1.3/SWIGDocumentation.pdf

PyROOT

http://wlav.web.cern.ch/wlav/pyroot/

gErrorIgnoreLevel

use

gErrorIgnoreLevel = 1001
to suppress the message ROOT prints out when calling TCanvas::SaveAs(), etc.

To use it in compiled code, need

#include <TError.h>

Debian/Ubuntu package for ROOT

http://mirror.phy.bnl.gov/debian-root/ubuntu/

http://wiki.debian.org/DebianScience/ROOT

batch mode in Python interface / PyROOT

http://www.pv.infn.it/~bellomo/MyBlogs/EWPA_blog/Entries/2008/4/3_Run_ROOT_in_batch_mode_from_C++_Python_code.html