Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist005_TH1_palettecolor.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// Palette coloring for TH1
5///
6/// Palette coloring for histogram is activated thanks to the options `PFC`
7/// (Palette Fill Color), `PLC` (Palette Line Color) and `PMC` (Palette Marker Color).
8/// When one of these options is given to `TH1::Draw` the histogram gets its color
9/// from the current color palette defined by `gStyle->SetPalette(...)`. The color
10/// is determined according to the number of objects having palette coloring in
11/// the current pad.
12///
13/// In this example five histograms are displayed with palette coloring for lines and
14/// and marker. The histograms are drawn with markers and error bars and one can see
15/// the color of each histogram is picked inside the default palette `kBird`.
16///
17/// \macro_image
18/// \macro_code
19///
20/// \date November 2024
21/// \author Olivier Couet
22
24{
25 auto *canvas = new TCanvas();
26
27 // Disable drawing the title of the canvas
29 // Disable drawing the stats box
31
32 auto *h1 = new TH1D("h1", "Histogram drawn with full circles", 100, -4, 4);
33 auto *h2 = new TH1D("h2", "Histogram drawn with full squares", 100, -4, 4);
34 auto *h3 = new TH1D("h3", "Histogram drawn with full triangles up", 100, -4, 4);
35 auto *h4 = new TH1D("h4", "Histogram drawn with full triangles down", 100, -4, 4);
36 auto *h5 = new TH1D("h5", "Histogram drawn with empty circles", 100, -4, 4);
37
38 // Use Mersenne-Twister random number generator
40 for (int i = 0; i < 25000; i++) {
41 // "Rannor" fills the two parameters we pass with RANdom numbers picked from a NORmal distribution.
42 // In this case we ignore the second value.
43 double val, ignored;
44 rng.Rannor(val, ignored);
45 // Fill() called with 2 arguments adds the given value (first arg) with the specified weight (second arg)
46 h1->Fill(val, 10.);
47 h2->Fill(val, 8.);
48 h3->Fill(val, 6.);
49 h4->Fill(val, 4.);
50 h5->Fill(val, 2.);
51 }
52
53 // Set different styles for the various histograms
55 h2->SetMarkerStyle(kFullSquare);
56 h3->SetMarkerStyle(kFullTriangleUp);
57 h4->SetMarkerStyle(kFullTriangleDown);
58 h5->SetMarkerStyle(kOpenCircle);
59
60 // Draw all histograms overlapped in the same canvas (thanks to the "SAME" option)
61 h1->Draw("PLC PMC");
62 h2->Draw("SAME PLC PMC");
63 h3->Draw("SAME PLC PMC");
64 h4->Draw("SAME PLC PMC");
65 h5->Draw("SAME PLC PMC");
66
67 // Build a legend from the objects drawn in the pad, using their description that we specified when constructing them
68 gPad->BuildLegend();
69}
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
@ kFullTriangleDown
Definition TAttMarker.h:59
@ kFullSquare
Definition TAttMarker.h:58
@ kFullTriangleUp
Definition TAttMarker.h:58
@ kFullCircle
Definition TAttMarker.h:58
@ kOpenCircle
Definition TAttMarker.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
#define gPad
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:41
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:693
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3315
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3037
Random number generator class based on M.
Definition TRandom3.h:27
void SetOptTitle(Int_t tit=1)
Definition TStyle.h:338
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1642
TH1F * h1
Definition legend1.C:5