Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist006_TH1_bar_charts.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// Draw 1D histograms as bar charts
5///
6/// \macro_image
7/// \macro_code
8///
9/// \date November 2024
10/// \author Rene Brun
11
12TCanvas *hist006_TH1_bar_charts()
13{
14 // Try to open first the file cernstaff.root in tutorials/tree directory
15 TString filedir = gROOT->GetTutorialDir();
16 filedir += TString("/tree/");
17 TString filename = "cernstaff.root";
18 // Note that `AccessPathName` returns 0 (false) on success!
19 bool fileNotFound = gSystem->AccessPathName(filename);
20
21 // If the file is not found try to generate it using the macro tree/cernbuild.C
22 if (fileNotFound) {
23 TString macroName = filedir + "cernbuild.C";
24 if (!gInterpreter->IsLoaded(macroName)) gInterpreter->LoadMacro(macroName);
25 gROOT->ProcessLineFast("cernbuild()");
26 }
27
28 auto file = std::unique_ptr<TFile>(TFile::Open(filename, "READ"));
29 if (!file) {
30 Error("hbars", "file cernstaff.root not found");
31 return nullptr;
32 }
33
34 // Retrieve the TTree named "T" contained in the file
35 auto tree = file->Get<TTree>("T");
36 if (!tree) {
37 Error("hbars", "Tree T is not present in file %s", file->GetName());
38 return nullptr;
39 }
40 tree->SetFillColor(45);
41
42 // Create the canvas to draw on
43 TCanvas *c1 = new TCanvas("c1","histograms with bars", 700, 800);
44 c1->SetFillColor(42);
45 // Divide it vertically in 2 sections
46 int ndivsX = 1;
47 int ndivsY = 2;
48 c1->Divide(ndivsX, ndivsY);
49
50 // Horizontal bar chart
51 auto *curPad = c1->cd(1); // select top section. Section 1 is the first sub-section.
52 curPad->SetGrid();
53 curPad->SetLogx();
54 curPad->SetFrameFillColor(33);
55 // Use the "hbar2" option to draw the tree as a horizontal bar chart
56 tree->Draw("Nation","","hbar2");
57
58 // Vertical bar chart
59 curPad = c1->cd(2);
60 curPad->SetGrid();
61 curPad->SetFrameFillColor(33);
62 // This line makes the TTree draw its "Division" branch to a new histogram called "hDiv".
63 // We use "goff" because we don't want to really draw it to screen but we are only interested
64 // in generating the histogram from it (which we'll display ourselves later).
65 tree->Draw("Division>>hDiv","","goff");
66 // Retrieve the generated histogram
67 TH1F *hDiv = file->Get<TH1F>("hDiv");
68 hDiv->SetStats(0);
69 // Clone the histogram into a new one called "hDivFR".
70 TH1F *hDivFR = static_cast<TH1F*>(hDiv->Clone("hDivFR"));
71 // Overwrite the contents of the newly-cloned histogram to only keep the entries matching our
72 // selection (second argument of TTree::Draw()).
73 tree->Draw("Division>>hDivFR","Nation==\"FR\"","goff");
74
75 // Now draw both histograms side-by-side ("same" option) as vertical bar charts ("bar2" option)
76 hDiv->SetBarWidth(0.45);
77 hDiv->SetBarOffset(0.1);
78 hDiv->SetFillColor(49);
79 TH1 *h1 = hDiv->DrawCopy("bar2");
80 hDivFR->SetBarWidth(0.4);
81 hDivFR->SetBarOffset(0.55);
82 hDivFR->SetFillColor(50);
83 TH1 *h2 = hDivFR->DrawCopy("bar2,same");
84
85 TLegend *legend = new TLegend(0.55,0.65,0.76,0.82);
86 legend->AddEntry(h1,"All nations","f");
87 legend->AddEntry(h2,"French only","f");
88 legend->Draw();
89
90 c1->cd();
91
92 return c1;
93}
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:561
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:38
The Canvas class.
Definition TCanvas.h:23
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:4086
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:634
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
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:376
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition TH1.cxx:3102
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
Definition TH1.cxx:2741
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:377
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:9010
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:320
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition TLegend.cxx:425
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:1296
A TTree represents a columnar dataset.
Definition TTree.h:79
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5