ROOT tips & hacks#

ROOT is the standard analysis software used for physics analysis (and much more) in mainly particle physics and nuclear physics. This page summarizes some quick to use examples and tips.

Some command-line hacks#

  • To iteratively hadd histograms or trees, which are in multiple paths <source-dir-path1>, <source-dir-path2>, etc.

    # Get the file names from one path
    array=($(ls <source-dir-path1>/tree-* | xargs -n 1 basename));
    # Then go to the target directory and do,
    for i in ${array[@]};
            hadd "$i" <source-dir-path1>/"$i" <source-dir-path2>/"$i";

Some example codes#

  • If you wish to plot three overlayed histograms, after hadding them, in the ROOT console.

    TFile *f1=TFile::Open("HistFile_G.root")
    TFile *f2=TFile::Open("HistFile_D.root")
    TFile *f3=TFile::Open("HistFile_T.root")
    TH1F *h1 = f1->Get<TH1F>("RegionHistogram_SR_mBB")
    TH1F *h2 = f2->Get<TH1F>("RegionHistogram_SR_mBB")
    TH1F *h3 = f3->Get<TH1F>("RegionHistogram_SR_mBB")
    TH1F *h4 = f1->Get<TH1F>("RegionHistogram_CRLow_mBB")
    TH1F *h5 = f2->Get<TH1F>("RegionHistogram_CRLow_mBB")
    TH1F *h6 = f3->Get<TH1F>("RegionHistogram_CRLow_mBB")
    TH1F *h7 = f1->Get<TH1F>("RegionHistogram_CRHigh_mBB")
    TH1F *h8 = f2->Get<TH1F>("RegionHistogram_CRHigh_mBB")
    TH1F *h9 = f3->Get<TH1F>("RegionHistogram_CRHigh_mBB")
    h1->Add(h4); h1->Add(h7); h2->Add(h5); h2->Add(h8); h3->Add(h6); h1->Add(h9);
    h1->SetLineColor(2); h2->SetLineColor(9); h3->SetLineColor(8)
    h1->Rebin(2); h2->Rebin(2); h3->Rebin(2)
    h1->Draw("HIST E"); h2->Draw("HIST SAME E"); h3->Draw("HIST SAME E")