ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hbars.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// Example of bar charts with 1-d histograms.
4 ///
5 /// \macro_image
6 /// \macro_code
7 ///
8 /// \author Rene Brun
9 
10 TCanvas *hbars() {
11  cout << gSystem->DirName(__FILE__) << endl;
12 
13  // Try to open first the file cernstaff.root in tutorials/tree directory
14  TString filedir = gSystem->DirName(__FILE__);
15  filedir += TString("/../tree/");
16  TString filename = "cernstaff.root";
17  bool fileNotFound = gSystem->AccessPathName(filename); // note opposite return code
18 
19  // If file is not found try to generate it uing the macro tree/cernbuild.C
20  if (fileNotFound) {
21  TString macroName = filedir + "cernbuild.C";
22  if (!gInterpreter->IsLoaded(macroName)) gInterpreter->LoadMacro(macroName);
23  gROOT->ProcessLineFast("cernbuild()");
24  }
25  TFile * f = TFile::Open(filename);
26  if (!f) {
27  Error("hbars","file cernstaff.root not found");
28  return 0;
29  }
30  TTree *T = (TTree*)f->Get("T");
31  if (!T) {
32  Error("hbars","Tree T is not present in file %s",f->GetName() );
33  return 0;
34  }
35  T->SetFillColor(45);
36  TCanvas *c1 = new TCanvas("c1","histograms with bars",700,800);
37  c1->SetFillColor(42);
38  c1->Divide(1,2);
39 
40  // Horizontal bar chart
41  c1->cd(1); gPad->SetGrid(); gPad->SetLogx(); gPad->SetFrameFillColor(33);
42  T->Draw("Nation","","hbar2");
43 
44  // Vertical bar chart
45  c1->cd(2); gPad->SetGrid(); gPad->SetFrameFillColor(33);
46  T->Draw("Division>>hDiv","","goff");
47  TH1F *hDiv = (TH1F*)gDirectory->Get("hDiv");
48  hDiv->SetStats(0);
49  TH1F *hDivFR = (TH1F*)hDiv->Clone("hDivFR");
50  T->Draw("Division>>hDivFR","Nation==\"FR\"","goff");
51  hDiv->SetBarWidth(0.45);
52  hDiv->SetBarOffset(0.1);
53  hDiv->SetFillColor(49);
54  TH1 *h1 = hDiv->DrawCopy("bar2");
55  hDivFR->SetBarWidth(0.4);
56  hDivFR->SetBarOffset(0.55);
57  hDivFR->SetFillColor(50);
58  TH1 *h2 = hDivFR->DrawCopy("bar2,same");
59 
60  TLegend *legend = new TLegend(0.55,0.65,0.76,0.82);
61  legend->AddEntry(h1,"All nations","f");
62  legend->AddEntry(h2,"French only","f");
63  legend->Draw();
64 
65  c1->cd();
66  delete f;
67  return c1;
68 }
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:1213
virtual void SetBarOffset(Float_t offset=0.25)
Definition: TH1.h:356
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
TCanvas * c1
Definition: legend1.C:2
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:344
Basic string class.
Definition: TString.h:137
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:980
#define gInterpreter
Definition: TInterpreter.h:502
TFile * f
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition: TH1.cxx:2565
virtual void SetBarWidth(Float_t width=0.5)
Definition: TH1.h:357
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:3851
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition: TH1.cxx:2925
void Error(const char *location, const char *msgfmt,...)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
The Canvas class.
Definition: TCanvas.h:48
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:360
The TH1 histogram class.
Definition: TH1.h:80
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:1073
#define gPad
Definition: TVirtualPad.h:288
A TTree object has a header with a name and a title.
Definition: TTree.h:98
#define gDirectory
Definition: TDirectory.h:221
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8320