Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist004_TH1_labels.C File Reference

Detailed Description

View in nbviewer Open in SWAN
1D histograms with alphanumeric labels.

A TH1 can have named bins that are filled with the method overload TH1::Fill(const char*, double)

{
// Create the histogram
const std::array people{"Jean", "Pierre", "Marie", "Odile", "Sebastien", "Fons", "Rene",
"Nicolas", "Xavier", "Greg", "Bjarne", "Anton", "Otto", "Eddy",
"Peter", "Pasha", "Philippe", "Suzanne", "Jeff", "Valery"};
// Start with an arbitrary amount of bins and an arbitrary range, but this will be extended thanks to SetCanExtend().
int nBins = 3;
double rangeMin = 0.0;
double rangeMax = 3.0;
auto *h = new TH1D("h", "test", nBins, rangeMin, rangeMax);
// Disable the default stats box when drawing this histogram
h->SetStats(0);
h->SetFillColor(38);
// Allow both axes to extend past the initial range we gave in the constructor
h->SetCanExtend(TH1::kAllAxes);
// Fill the Y axis with arbitrary values, a random amount per bin
for (int i = 0; i < 5000; i++) {
int r = rng.Rndm() * 20;
// `Fill()` called with a const char* as the first argument will add a value to the bin with that name,
// creating it if it doesn't exist yet.
h->Fill(people[r], 1);
}
// Remove empty bins
h->LabelsDeflate();
auto *c1 = new TCanvas("c1", "demo bin labels", 10, 10, 900, 500);
// Enable the grid in the plot
c1->SetGrid();
c1->SetTopMargin(0.15);
// Draw the histogram
h->Draw();
// Draw a boxed text
// "brNDC" = coordinates draw bottom-right shadow and pass the coordinates in normalized device coordinates
auto *pt = new TPaveText(0.7, 0.85, 0.98, 0.98, "brNDC");
pt->AddText("Use the axis Context Menu LabelsOption");
pt->AddText(" \"a\" to sort by alphabetic order");
pt->AddText(" \">\" to sort by decreasing values");
pt->AddText(" \"<\" to sort by increasing values");
pt->Draw();
return c1;
}
#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
Date
November 2024
Author
Rene Brun

Definition in file hist004_TH1_labels.C.