Project

General

Profile

Some useful ROOT tricks/tips

All the example codes for this talk are under gm2analyses/day1/talk6_root_tricks (Branch = AnalysisWorkshop2017Feb).
All the codes under this folder can be executed using

root exampleN.C

and all the scripts can be executed using

source exampleN.sh

Only the important part of the code is reproduced here. Please refer to the original code for the full package.

Example 1: root -l

Tired of this splash screen?

ROOTScreen.png

Simply put this into your ~/.bash_rc or ~/.profile

alias root='root -l'

or

source example1.sh

All it does is to put the alias line into your ~/.bash_rc file.

Example 2: Quick ROOT file content inspection

The ones having a behaviour which is compatible with the widely known *nix tools like ls, rm, mkdir.
*rootls: list the content of a ROOT file.
*rootcp: copy objects stored in a file to another file.
*rootrm: delete objects contained in a file.
*rootmv: move objects stored in a file to another file.
*rootmkdir: create a "directory" inside a ROOT file.

The ones which automate very common operations performed on widely adopted ROOT classes:
*rootbrowse: open a TBrowser directly on the content of a ROOT file.
*rooteventselector: extract a range of events of a tree contained in a file and put them as a new tree in another file.
*rootprint: plot objects in an image file.

Try example2.sh by

source example2.sh

Example 3: Using TFile to open a ROOT file

TFile *file = new TFile ("gm2ringsim_xtalHits_32M.root");

Example 4: Using gDirectory to navigate in a ROOT file

gDirectory->cd("crystalHit");

Example 5: Using TTree::Draw function and set TH1,TH2 bin size

tree->Draw("t>>h(50)");

Example 6: Using TTree::Draw and dump array of variables

auto var1 = tree->GetV1();

Example 7: Using TTree::Draw and get the handle to the TObjects (TH1, TH2, TGraph)

 TH1D *time = (TH1D*)gDirectory->Get("h");

Example 8: Split TCanvas into multiple TPads

TCanvas *c1 = new TCanvas("c1","c1",800,600);
c1->Divide(2,2);
c1->cd(1); //go into pad #1

Example 9: Using the powerful TString::Form()

This will save you time typing out everything explicitly.

TH1D *h1[9];
h1[i] = (TH1D*)file->Get(Form("crystalHit/calo00xtalHit%.2d",i));

Example 10: Using gStyle to set global title/label sizes

Histogram/graph title tends to get too small if you have multiple TPads. Use gStyle->SetTitleH or gStyle->SetTitleW to change the size globally.

gStyle->SetTitleH(0.1); // Set the TH1D title size    
gStyle->SetLabelSize(0.05,"XY");
gStyle->SetTitleSize(0.05,"XY");

Example 11: Using gStyle to customize Stat/Fit box
If you do not feel like plotting the statistics box, simply

gStyle->SetOptStat(0);

Example 12: Using gStyle to set TH2’s color scale (palette)

gStyle->SetPalette(kRainBow); // set the palette
tree->Draw("x:y","","colz");  // then draw with "colz" option 

Examples given in ROOT's official website

PaletteExamples.png

More information can be found here https://root.cern.ch/doc/master/classTColor.html

Example 13: Manipulating Stat boxes for multiple histograms on the same TPad

gPad->Update();
TPaveStats *st[3][9];
st[iCalo][iXtal] = (TPaveStats*)h1[iCalo][iXtal]->FindObject("stats");
st[iCalo][iXtal]->SetX1NDC(0.7);
st[iCalo][iXtal]->SetX2NDC(0.9);
st[iCalo][iXtal]->SetY1NDC(0.7-0.2*iCalo);
st[iCalo][iXtal]->SetY2NDC(0.9-0.2*iCalo);
st[iCalo][iXtal]->SetLineColor(iCalo+1);
st[iCalo][iXtal]->SetTextColor(iCalo+1);
gPad->Update(); // force the painting so that the StatBox is created

Example 14: Add Legend to your plots

TLegend *leg[9];
leg[iXtal]->AddEntry(h1[iCalo][iXtal],h1[iCalo][iXtal]->GetTitle(),"l");
leg[iXtal]->Draw();

Example 14a: Split the legend into multiple columns

leg[iXtal]->SetNColumns(2);
leg[iXtal]->Draw();

Example 15: Set ranges on x and y-axis (SetRangeUser and SetLimit)

h1[iCalo][iXtal]->GetXaxis()->SetRangeUser(0,2500);

However, if you want to change the range to a value that exceeds what you defined in a histogram, you will need to use this

h1[iCalo][iXtal]->GetXaxis()->SetLimits(0,3500);

Example 16: Create movie using ImageMagick’s convert program

convert -delay 20 -loop 0 InputFiles*.png OutputFile.gif