ROOT logo

From $ROOTSYS/tutorials/hist/hbars.C

// Example of bar charts with 1-d histograms
// Author: Rene Brun
{
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   dir.ReplaceAll("hbars.C","../tree/cernbuild.C");
   dir.ReplaceAll("/./","/");
   if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
   TFile *f = (TFile*)gROOT->ProcessLineFast("cernbuild(1)");
   if (!f) return;
   TTree *T = (TTree*)f->Get("T");
   T->SetFillColor(45);
   TCanvas *c1 = new TCanvas("c1","histograms with bars",700,800);
   c1->SetFillColor(42);
   c1->Divide(1,2);
   
   //horizontal bar chart
   c1->cd(1); gPad->SetGrid(); gPad->SetLogx(); gPad->SetFrameFillColor(33);
   T->Draw("Nation","","hbar2");
   
   //vertical bar chart
   c1->cd(2); gPad->SetGrid(); gPad->SetFrameFillColor(33);
   T->Draw("Division>>hDiv","","goff");
   TH1F *hDiv   = (TH1F*)gDirectory->Get("hDiv");
   hDiv->SetStats(0);
   TH1F *hDivFR = (TH1F*)hDiv->Clone("hDivFR");
   T->Draw("Division>>hDivFR","Nation==\"FR\"","goff");
   hDiv->SetBarWidth(0.45);
   hDiv->SetBarOffset(0.1);
   hDiv->SetFillColor(49);
   TH1 *h1 = hDiv->DrawCopy("bar2");
   hDivFR->SetBarWidth(0.4);
   hDivFR->SetBarOffset(0.55);
   hDivFR->SetFillColor(50);
   TH1 *h2 = hDivFR->DrawCopy("bar2,same");
  
   TLegend *legend = new TLegend(0.55,0.65,0.76,0.82);
   legend->AddEntry(h1,"All nations","f");
   legend->AddEntry(h2,"French only","f");
   legend->Draw();
   
   c1->cd();
   delete f;
   return c1;
}
 hbars.C:1
 hbars.C:2
 hbars.C:3
 hbars.C:4
 hbars.C:5
 hbars.C:6
 hbars.C:7
 hbars.C:8
 hbars.C:9
 hbars.C:10
 hbars.C:11
 hbars.C:12
 hbars.C:13
 hbars.C:14
 hbars.C:15
 hbars.C:16
 hbars.C:17
 hbars.C:18
 hbars.C:19
 hbars.C:20
 hbars.C:21
 hbars.C:22
 hbars.C:23
 hbars.C:24
 hbars.C:25
 hbars.C:26
 hbars.C:27
 hbars.C:28
 hbars.C:29
 hbars.C:30
 hbars.C:31
 hbars.C:32
 hbars.C:33
 hbars.C:34
 hbars.C:35
 hbars.C:36
 hbars.C:37
 hbars.C:38
 hbars.C:39
 hbars.C:40
 hbars.C:41
 hbars.C:42
 hbars.C:43
 hbars.C:44
 hbars.C:45
thumb