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 uing the macro tree/cernbuild.C
if (fileNotFound) {
TString macroName = filedir + "cernbuild.C";
if (!gInterpreter->IsLoaded(macroName)) gInterpreter->LoadMacro(macroName);
gROOT->ProcessLineFast("cernbuild()");
}
TFile * f = TFile::Open(filename);
if (!f) {
Error("hbars","file cernstaff.root not found");
return 0;
}
TTree *T = (TTree*)f->Get("T");
if (!T) {
Error("hbars","Tree T is not present in file %s",f->GetName() );
return 0;
}
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;
}
Author
Rene Brun

Definition in file hbars.C.

f
#define f(i)
Definition: RSha256.hxx:104
TH1::DrawCopy
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition: TH1.cxx:3097
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:79
gInterpreter
#define gInterpreter
Definition: TInterpreter.h:560
TFile::Open
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:3995
TString
Basic string class.
Definition: TString.h:136
TSystem::AccessPathName
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:1294
h1
TH1F * h1
Definition: legend1.C:5
TH1::Clone
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition: TH1.cxx:2717
TLegend::AddEntry
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:330
gDirectory
#define gDirectory
Definition: TDirectory.h:236
TH1::SetStats
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8756
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
TAttFill::SetFillColor
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
TH1
TH1 is the base class of all histogramm classes in ROOT.
Definition: TH1.h:58
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
gPad
#define gPad
Definition: TVirtualPad.h:287
TLegend::Draw
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:423
TLegend
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
TH1::SetBarWidth
virtual void SetBarWidth(Float_t width=0.5)
Definition: TH1.h:360
TH1::SetBarOffset
virtual void SetBarOffset(Float_t offset=0.25)
Definition: TH1.h:359
gROOT
#define gROOT
Definition: TROOT.h:406
Error
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
c1
return c1
Definition: legend1.C:41