Logo ROOT  
Reference Guide
No Matches
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// Example of bar charts with 1-d histograms.
6/// \macro_image
7/// \macro_code
9/// \author Rene Brun
11TCanvas *hbars()
13 // Try to open first the file cernstaff.root in tutorials/tree directory
14 TString filedir = gROOT->GetTutorialDir();
15 filedir += TString("/tree/");
16 TString filename = "cernstaff.root";
17 bool fileNotFound = gSystem->AccessPathName(filename); // note opposite return code
19 // If file is not found try to generate it using 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 }
26 if (!f) {
27 Error("hbars", "file cernstaff.root not found");
28 return nullptr;
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 nullptr;
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);
40 // Horizontal bar chart
41 c1->cd(1);
42 gPad->SetGrid();
43 gPad->SetLogx();
44 gPad->SetFrameFillColor(33);
45 T->Draw("Nation","","hbar2");
47 // Vertical bar chart
48 c1->cd(2);
49 gPad->SetGrid();
50 gPad->SetFrameFillColor(33);
51 T->Draw("Division>>hDiv","","goff");
52 TH1F *hDiv = (TH1F*)gDirectory->Get("hDiv");
53 hDiv->SetStats(0);
54 TH1F *hDivFR = (TH1F*)hDiv->Clone("hDivFR");
55 T->Draw("Division>>hDivFR","Nation==\"FR\"","goff");
56 hDiv->SetBarWidth(0.45);
57 hDiv->SetBarOffset(0.1);
58 hDiv->SetFillColor(49);
59 TH1 *h1 = hDiv->DrawCopy("bar2");
60 hDivFR->SetBarWidth(0.4);
61 hDivFR->SetBarOffset(0.55);
62 hDivFR->SetFillColor(50);
63 TH1 *h2 = hDivFR->DrawCopy("bar2,same");
65 TLegend *legend = new TLegend(0.55,0.65,0.76,0.82);
66 legend->AddEntry(h1,"All nations","f");
67 legend->AddEntry(h2,"French only","f");
68 legend->Draw();
70 c1->cd();
71 delete f;
72 return c1;
#define f(i)
Definition RSha256.hxx:104
#define gDirectory
Definition TDirectory.h:384
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
#define gInterpreter
#define gROOT
Definition TROOT.h:406
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
#define gPad
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
The Canvas class.
Definition TCanvas.h:23
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
Definition TFile.h:53
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4070
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:578
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
virtual void SetBarOffset(Float_t offset=0.25)
Set the bar offset as fraction of the bin width for drawing mode "B".
Definition TH1.h:362
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition TH1.cxx:3094
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
Definition TH1.cxx:2733
virtual void SetBarWidth(Float_t width=0.5)
Set the width of bars as fraction of the bin width for drawing mode "B".
Definition TH1.h:363
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:8861
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition TLegend.cxx:317
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition TLegend.cxx:422
Basic string class.
Definition TString.h:139
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:1283
A TTree represents a columnar dataset.
Definition TTree.h:79
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5
double T(double x)