Logo ROOT  
Reference Guide
hbars.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Example of bar charts with 1-d histograms.

TCanvas *hbars()
// Try to open first the file cernstaff.root in tutorials/tree directory
TString filedir = gROOT->GetTutorialDir();
filedir += TString("/tree/");
TString filename = "cernstaff.root";
bool fileNotFound = gSystem->AccessPathName(filename); // note opposite return code
// If file is not found try to generate it using the macro tree/cernbuild.C
if (fileNotFound) {
TString macroName = filedir + "cernbuild.C";
if (!gInterpreter->IsLoaded(macroName)) gInterpreter->LoadMacro(macroName);
if (!f) {
Error("hbars", "file cernstaff.root not found");
return nullptr;
TTree *T = (TTree*)f->Get("T");
if (!T) {
Error("hbars", "Tree T is not present in file %s", f->GetName());
return nullptr;
TCanvas *c1 = new TCanvas("c1","histograms with bars",700,800);
// Horizontal bar chart
// Vertical bar chart
TH1F *hDiv = (TH1F*)gDirectory->Get("hDiv");
TH1F *hDivFR = (TH1F*)hDiv->Clone("hDivFR");
TH1 *h1 = hDiv->DrawCopy("bar2");
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");
delete f;
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:188
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
Definition: TInterpreter.h:564
#define gROOT
Definition: TROOT.h:406
R__EXTERN TSystem * gSystem
Definition: TSystem.h:560
#define gPad
Definition: TVirtualPad.h:302
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 a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
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:4053
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:577
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:361
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition: TH1.cxx:3107
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
Definition: TH1.cxx:2727
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:362
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8856
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:136
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:1299
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)
Definition: ChebyshevPol.h:34
Rene Brun

Definition in file hbars.C.