Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist004_TH1_labels.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// 1D histograms with alphanumeric labels.
5///
6/// A TH1 can have named bins that are filled with the method overload TH1::Fill(const char*, double)
7///
8/// \macro_image
9/// \macro_code
10///
11/// \date November 2024
12/// \author Rene Brun
13
15{
16 // Create the histogram
17 const std::array people{"Jean", "Pierre", "Marie", "Odile", "Sebastien", "Fons", "Rene",
18 "Nicolas", "Xavier", "Greg", "Bjarne", "Anton", "Otto", "Eddy",
19 "Peter", "Pasha", "Philippe", "Suzanne", "Jeff", "Valery"};
20 // Start with an arbitrary amount of bins and an arbitrary range, but this will be extended thanks to SetCanExtend().
21 int nBins = 3;
22 double rangeMin = 0.0;
23 double rangeMax = 3.0;
24 auto *h = new TH1D("h", "test", nBins, rangeMin, rangeMax);
25 // Disable the default stats box when drawing this histogram
26 h->SetStats(0);
27 h->SetFillColor(38);
28 // Allow both axes to extend past the initial range we gave in the constructor
29 h->SetCanExtend(TH1::kAllAxes);
30 // Fill the Y axis with arbitrary values, a random amount per bin
32 for (int i = 0; i < 5000; i++) {
33 int r = rng.Rndm() * 20;
34 // `Fill()` called with a const char* as the first argument will add a value to the bin with that name,
35 // creating it if it doesn't exist yet.
36 h->Fill(people[r], 1);
37 }
38 // Remove empty bins
39 h->LabelsDeflate();
40
41 auto *c1 = new TCanvas("c1", "demo bin labels", 10, 10, 900, 500);
42 // Enable the grid in the plot
43 c1->SetGrid();
44 c1->SetTopMargin(0.15);
45
46 // Draw the histogram
47 h->Draw();
48
49 // Draw a boxed text
50 // "brNDC" = coordinates draw bottom-right shadow and pass the coordinates in normalized device coordinates
51 auto *pt = new TPaveText(0.7, 0.85, 0.98, 0.98, "brNDC");
52 pt->SetFillColor(18);
53 pt->SetTextAlign(12);
54 pt->AddText("Use the axis Context Menu LabelsOption");
55 pt->AddText(" \"a\" to sort by alphabetic order");
56 pt->AddText(" \">\" to sort by decreasing values");
57 pt->AddText(" \"<\" to sort by increasing values");
58 pt->Draw();
59
60 return c1;
61}
#define h(i)
Definition RSha256.hxx:106
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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 r
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:38
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:44
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:693
@ kAllAxes
Definition TH1.h:76
A Pave (see TPave) with text, lines or/and boxes inside.
Definition TPaveText.h:21
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
void Draw(Option_t *option="") override
Draw this pavetext with its current attributes.
Random number generator class based on M.
Definition TRandom3.h:27
TPaveText * pt
return c1
Definition legend1.C:41