Logo ROOT  
Reference Guide
hstack.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/// \author Rene Brun
10
11TCanvas *hstack() {
12 THStack *hs = new THStack("hs","Stacked 1D histograms");
13 //create three 1-d histograms
14 TH1F *h1st = new TH1F("h1st","test hstack",100,-4,4);
15 h1st->FillRandom("gaus",20000);
16 h1st->SetFillColor(kRed);
17 h1st->SetMarkerStyle(21);
18 h1st->SetMarkerColor(kRed);
19 hs->Add(h1st);
20 TH1F *h2st = new TH1F("h2st","test hstack",100,-4,4);
21 h2st->FillRandom("gaus",15000);
22 h2st->SetFillColor(kBlue);
23 h2st->SetMarkerStyle(21);
24 h2st->SetMarkerColor(kBlue);
25 hs->Add(h2st);
26 TH1F *h3st = new TH1F("h3st","test hstack",100,-4,4);
27 h3st->FillRandom("gaus",10000);
28 h3st->SetFillColor(kGreen);
29 h3st->SetMarkerStyle(21);
31 hs->Add(h3st);
32
33 TCanvas *cst = new TCanvas("cst","stacked hists",10,10,700,700);
34 cst->Divide(2,2);
35 // in top left pad, draw the stack with defaults
36 cst->cd(1);
37 hs->Draw();
38 // in top right pad, draw the stack in non-stack mode
39 // and errors option
40 cst->cd(2);
41 gPad->SetGrid();
42 hs->Draw("nostack,e1p");
43 //in bottom left, draw in stack mode with "lego1" option
44 cst->cd(3);
45 gPad->SetFrameFillColor(17);
46 gPad->SetTheta(3.77);
47 gPad->SetPhi(2.9);
48 hs->Draw("lego1");
49
50 cst->cd(4);
51 //create two 2-D histograms and draw them in stack mode
52 gPad->SetFrameFillColor(17);
53 THStack *a = new THStack("a","Stacked 2D histograms");
54 TF2 *f1 = new TF2("f1",
55 "xygaus + xygaus(5) + xylandau(10)",-4,4,-4,4);
56 Double_t params1[] = {130,-1.4,1.8,1.5,1, 150,2,0.5,-2,0.5,
57 3600,-2,0.7,-3,0.3};
58 f1->SetParameters(params1);
59 TH2F *h2sta = new TH2F("h2sta","h2sta",20,-4,4,20,-4,4);
60 h2sta->SetFillColor(38);
61 h2sta->FillRandom("f1",4000);
62 TF2 *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 TH2F *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 return cst;
72}
double Double_t
Definition: RtypesCore.h:59
@ kRed
Definition: Rtypes.h:66
@ kGreen
Definition: Rtypes.h:66
@ kBlue
Definition: Rtypes.h:66
#define gPad
Definition: TVirtualPad.h:288
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
The Canvas class.
Definition: TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition: TCanvas.cxx:706
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:649
A 2-Dim function with parameters.
Definition: TF2.h:29
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:574
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3523
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:257
void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr) override
Fill histogram following distribution in function fname.
Definition: TH2.cxx:677
The Histogram stack class.
Definition: THStack.h:38
virtual void Add(TH1 *h, Option_t *option="")
add a new histogram to the list Only 1-d and 2-d histograms currently supported.
Definition: THStack.cxx:365
void Draw(Option_t *chopt="") override
Draw this multihist with its current attributes.
Definition: THStack.cxx:450
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition: TPad.cxx:1169
TF1 * f1
Definition: legend1.C:11
auto * a
Definition: textangle.C:12