Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist023_THStack_simple.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// Example of stacked histograms: class THStack.
5///
6/// \macro_image
7/// \macro_code
8///
9/// \date December 2022
10/// \author Rene Brun
11
13{
14 auto hs = new THStack("hs", "Stacked 1D histograms");
15 // create three 1-d histograms
16 auto h1st = new TH1F("h1st", "test hstack", 100, -4, 4);
17 h1st->FillRandom("gaus", 20000);
18 h1st->SetFillColor(kRed);
19 h1st->SetMarkerStyle(21);
20 h1st->SetMarkerColor(kRed);
21 hs->Add(h1st);
22 auto h2st = new TH1F("h2st", "test hstack", 100, -4, 4);
23 h2st->FillRandom("gaus", 15000);
24 h2st->SetFillColor(kBlue);
25 h2st->SetMarkerStyle(21);
26 h2st->SetMarkerColor(kBlue);
27 hs->Add(h2st);
28 auto h3st = new TH1F("h3st", "test hstack", 100, -4, 4);
29 h3st->FillRandom("gaus", 10000);
30 h3st->SetFillColor(kGreen);
31 h3st->SetMarkerStyle(21);
32 h3st->SetMarkerColor(kGreen);
33 hs->Add(h3st);
34
35 auto cst = new TCanvas("cst", "stacked hists", 10, 10, 700, 700);
36 cst->Divide(2, 2);
37 // in top left pad, draw the stack with defaults
38 cst->cd(1);
39 hs->Draw();
40 // in top right pad, draw the stack in non-stack mode
41 // and errors option
42 cst->cd(2);
43 gPad->SetGrid();
44 hs->Draw("nostack,e1p");
45 // in bottom left, draw in stack mode with "lego1" option
46 cst->cd(3);
47 gPad->SetFrameFillColor(17);
48 gPad->SetTheta(3.77);
49 gPad->SetPhi(2.9);
50 hs->Draw("lego1");
51
52 cst->cd(4);
53 // create two 2-D histograms and draw them in stack mode
54 gPad->SetFrameFillColor(17);
55 auto a = new THStack("a", "Stacked 2D histograms");
56 auto f1 = new TF2("f1", "xygaus + xygaus(5) + xylandau(10)", -4, 4, -4, 4);
57 Double_t params1[] = {130, -1.4, 1.8, 1.5, 1, 150, 2, 0.5, -2, 0.5, 3600, -2, 0.7, -3, 0.3};
59 auto h2sta = new TH2F("h2sta", "h2sta", 20, -4, 4, 20, -4, 4);
60 h2sta->SetFillColor(38);
61 h2sta->FillRandom("f1", 4000);
62 auto f2 = new TF2("f2", "xygaus + xygaus(5)", -4, 4, -4, 4);
63 Double_t params2[] = {100, -1.4, 1.9, 1.1, 2, 80, 2, 0.7, -2, 0.5};
64 f2->SetParameters(params2);
65 auto h2stb = new TH2F("h2stb", "h2stb", 20, -4, 4, 20, -4, 4);
66 h2stb->SetFillColor(46);
67 h2stb->FillRandom("f2", 3000);
68 a->Add(h2sta);
69 a->Add(h2stb);
70 a->Draw();
71}
#define a(i)
Definition RSha256.hxx:99
double Double_t
Definition RtypesCore.h:59
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gPad
The Canvas class.
Definition TCanvas.h:23
virtual void SetParameters(const Double_t *params)
Definition TF1.h:677
A 2-Dim function with parameters.
Definition TF2.h:29
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:645
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
The Histogram stack class.
Definition THStack.h:40
TF1 * f1
Definition legend1.C:11