Logo ROOT   6.14/05
Reference Guide
hbars.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// \notebook
4 /// Example of bar charts with 1-d histograms.
5 ///
6 /// \macro_image
7 /// \macro_code
8 ///
9 /// \author Rene Brun
10 
11 TCanvas *hbars() {
12  // Try to open first the file cernstaff.root in tutorials/tree directory
13  TString filedir = gROOT->GetTutorialDir();
14  filedir += TString("/tree/");
15  TString filename = "cernstaff.root";
16  bool fileNotFound = gSystem->AccessPathName(filename); // note opposite return code
17 
18  // If file is not found try to generate it uing the macro tree/cernbuild.C
19  if (fileNotFound) {
20  TString macroName = filedir + "cernbuild.C";
21  if (!gInterpreter->IsLoaded(macroName)) gInterpreter->LoadMacro(macroName);
22  gROOT->ProcessLineFast("cernbuild()");
23  }
24  TFile * f = TFile::Open(filename);
25  if (!f) {
26  Error("hbars","file cernstaff.root not found");
27  return 0;
28  }
29  TTree *T = (TTree*)f->Get("T");
30  if (!T) {
31  Error("hbars","Tree T is not present in file %s",f->GetName() );
32  return 0;
33  }
34  T->SetFillColor(45);
35  TCanvas *c1 = new TCanvas("c1","histograms with bars",700,800);
36  c1->SetFillColor(42);
37  c1->Divide(1,2);
38 
39  // Horizontal bar chart
40  c1->cd(1); gPad->SetGrid(); gPad->SetLogx(); gPad->SetFrameFillColor(33);
41  T->Draw("Nation","","hbar2");
42 
43  // Vertical bar chart
44  c1->cd(2); gPad->SetGrid(); gPad->SetFrameFillColor(33);
45  T->Draw("Division>>hDiv","","goff");
46  TH1F *hDiv = (TH1F*)gDirectory->Get("hDiv");
47  hDiv->SetStats(0);
48  TH1F *hDivFR = (TH1F*)hDiv->Clone("hDivFR");
49  T->Draw("Division>>hDivFR","Nation==\"FR\"","goff");
50  hDiv->SetBarWidth(0.45);
51  hDiv->SetBarOffset(0.1);
52  hDiv->SetFillColor(49);
53  TH1 *h1 = hDiv->DrawCopy("bar2");
54  hDivFR->SetBarWidth(0.4);
55  hDivFR->SetBarOffset(0.55);
56  hDivFR->SetFillColor(50);
57  TH1 *h2 = hDivFR->DrawCopy("bar2,same");
58 
59  TLegend *legend = new TLegend(0.55,0.65,0.76,0.82);
60  legend->AddEntry(h1,"All nations","f");
61  legend->AddEntry(h2,"French only","f");
62  legend->Draw();
63 
64  c1->cd();
65  delete f;
66  return c1;
67 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1276
virtual void SetBarOffset(Float_t offset=0.25)
Definition: TH1.h:352
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
return c1
Definition: legend1.C:41
double T(double x)
Definition: ChebyshevPol.h:34
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:423
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:47
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:410
Basic string class.
Definition: TString.h:131
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
#define f(i)
Definition: RSha256.hxx:104
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition: TH1.cxx:3021
#define gInterpreter
Definition: TInterpreter.h:527
virtual void SetBarWidth(Float_t width=0.5)
Definition: TH1.h:353
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3976
TH1F * h1
Definition: legend1.C:5
void Error(const char *location, const char *msgfmt,...)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
The Canvas class.
Definition: TCanvas.h:31
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:330
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:357
The TH1 histogram class.
Definition: TH1.h:56
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1162
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition: TH1.cxx:2657
#define gPad
Definition: TVirtualPad.h:285
A TTree object has a header with a name and a title.
Definition: TTree.h:70
#define gDirectory
Definition: TDirectory.h:213
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8284