Root Commands and Examples

Official ROOT homepage
ROOT tips and tricks from the LHBC twiki

Environment Setup
Sample .rootrc and .rootlogon.C files

Accessing ROOT top level information and services : (more at TROOT.h)
TString rvers=gROOT->GetVersion();
Int_t ivers=gROOT->GetVersionInt();

FWLite Analysis      More here
Events->Draw("CaloJets_CaloJetMcone5.obj.eta()","CaloJets_CaloJetMcone5.obj.pt()>10");
draws eta spectrum of jets with a pt cut

Generating Postscript     Click here for a full description
Int_t itype=113; // 113 for Encapsulated PostScript, 100114 for nice paper output
TString psfile="noise_dists_cosmics.eps";
TPostScript ps(psfile,itype);// if no itype given, format is determined by extension
... code goes here...
ps.Close();

or simply call
  c1->Print(psfile);
after canvas is complete

Adding Text     Click here for a full description
TLatex *t = new TLatex();
t->SetNDC();
t->SetTextAlign(22);
t->SetTextFont(63);
t->SetTextSizePixels(22);
t->DrawLatex(xtxt,ytxt,"Z ^{#/}(700 GeV) #rightarrow q #bar{q}");

t->SetTextAlign(13); //align at top left
t->SetTextAlign(12); // left, vertically centered
t->SetTextAlign(22); // centered horizontally and vertically
t->SetTextAlign(11); //default bottom alignment

Adding a Legend     Click here for a full description
Double_t xl1=.05, yl1=0.75, xl2=xl1+.3, yl2=yl1+.125;
TLegend *leg = new TLegend(xl1,yl1,xl2,yl2);
leg->AddEntry(h1,"No Ecal","p");   // h1 and h2 are histogram pointers
leg->AddEntry(h2,"Ecal included","p");
leg->Draw();

Opening of ROOT files
TString dirname = ".";
TString rootname = "hlt1jet_mc_120.root";
rootname = dirname + "/" + rootname;
delete gROOT->GetListOfFiles()->FindObject(rootname); // clear memory of file name

cout << "Rootname:" << rootname << endl;
if( gSystem->AccessPathName(rootname) ) {
  cout << endl << "File: " << rootname << " not there!!!" << endl << endl;
  return;
}
TFile *rootfile = new TFile(rootname);
  or, for files in dcache:
TString rootname = "dcap://cmsgridftp.fnal.gov:24125/pnfs/fnal.gov/usr/cms/...";
TFile *_file0 = new TDCacheFile(rootname);
  or, for files in CASTOR (CERN):
TFile *_file0 = TFile::Open("rfio:/castor/cern.ch/user/initial/username/myFile.root");

Listing of Histograms
rootfile->GetListOfKeys()->Print();
rootfile->ls(); // list everything

Testing if a histogram exists in memory
TString hname="TriggerJets";
TKey *key = file->FindKey(hname);
if (key ==0){
  cout << "!!Histogram does not exist!!" << endl;
  throw 1;
}
TH1F *h =  (TH1F*)file->Get(hname);
Plotting of Histograms
TString hname="TriggerJets"; // From list of keys
TH1F *h = (TH1F*)_file0->Get(hname);
h->UseCurrentStyle(); // important for imported histograms
  Alternatively, call gROOT->ForceStyle(); before importing to set the style for all hists
h->GetXaxis()->SetRangeUser(0.,600.); // Set the range
h->SetFillColor(kYellow); // Fill fill color to yellow
h->SetFillColor(kYellow); // Fill fill color to yellow
h->SetMarkerStyle(20); h->SetMarkerColor(kBlue); h->SetMarkerSize(.6); // Draw histograms with blue markers
h->Draw();

Profile Plots     
TProfile *prof = a_2d_histogram->ProfileX();
prof->Draw();
  to fit with p1:
TF1 *p1fit = new TF1("P1","pol1",0,300);
prof->Fit(p1fit,"RQ")

Number of Events in a Tree     
TTree *srctree = dynamic_cast<TTree*>(_file0->Get("Events")); // TTree name is "Events"
cout << "Number of Events: " << srctree->GetEntries() << endl;

Filling Histograms with Variables from a Root Tree     Click here for more info
TH1F *h = new TH1F("jetPt","Jet Pt",25,0.0,250.);
Events->Draw("recoGenJets_midPointCone5GenJets__R3.obj.pt()>>+jetPt");
h->Draw();

Fitting of Histograms     Click here for more info
double minf=0.0, maxf=100.;
TF1 *gfit = new TF1("Gaussian","gaus",minf,maxf); // Create the fit function
h->Fit("Gaussian","RQ"); // Fit histogram h"
     or h->Fit(gfit,"RQ");

double chisq=gfit->GetChisquare();
double ndf=gfit->GetNDF();
double chisqdf=chisq/ndf;
cout << "Chisquare: " << chisq << "/" << ndf << " : " << chisqdf << endl;

Double_t amp = gfit->GetParameter(0); //value of 0th parameter
Double_t eamp = gfit->GetParError(0); //error on 0th parameter
Double_t mean = gfit->GetParameter(1); //value of 1st parameter
Double_t emean = gfit->GetParError(1); //error on 1st parameter

Addition, subtraction, multiplication and division of histograms     Click here for a full description
n1->Sumw2(); d1->Sumw2(); // store sum of squares of weights (if not already done)
TH1F *rat = d1->Clone(); rat->SetName("Ratio"); // Clone one of the histograms
rat->Divide(n1,d1,1.,1.,"B");

Extracting Histogram Information     Click here for a full description
h->GetMean(1); // For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
h->GetMeanError(1);
h->GetRMS(1); h->GetRMSError(1);
h->GetXaxis()->SetRangeUser(0.,600.); // If range is set, mean is computed over specifed range

Axis names and values     Click here for a full description
TString xaxis="p_{T} (GeV/c)";
h->GetXaxis()->SetTitle(xaxis);
h->GetXaxis()->SetRangeUser(0.,600.);
h->GetYaxis()->SetTitle("Entries");
h->GetXaxis()->SetLabelSize(.03);
h->GetYaxis()->SetLabelSize(.03);

Colors and Palettes
The basic colors: kWhite, kBlack, kRed, kGreen, kBlue, kYellow, kMagenta, kCyan
   (defined in $ROOTSYS/include/Gtypes.h)
gROOT->GetListOfColors()->Print(); // generates a full listing

To define your own color:
TColor *colorname = new TColor(colornum,r,g,b,"colorname"); colornum > 228 and 0 < r,g,b < 1
   e.g. TColor *col = new TColor(229,.3,.5,.6,"");
Existing color numbers can be redefined as follows:
  color=(TColor*)(gROOT->GetListOfColors()->At(colornum));
  color->SetRGB(r,g,b);

Working with the Canvas     Click here for a full description
TCanvas *c1 = new TCanvas("c1","Root Canvas 1"); // Default Canvas
Int_t wtopx,wtopy; UInt_t ww, wh;
c1->GetCanvasPar(wtopx,wtopy,ww,wh); // Gets Canvas Parameters

TCanvas *c1 = new TCanvas("c1","Root Canvas",Int_t wtopx, Int_t wtopy, Int_t ww, Int_t wh);
TCanvas *c1 = new TCanvas("c1","Root Canvas",900,20,540,550); // My Default Canvas