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{
13 THStack *hs = new THStack("hs","Stacked 1D histograms");
14 //create three 1-d histograms
15 TH1F *h1st = new TH1F("h1st","test hstack",100,-4,4);
16 h1st->FillRandom("gaus",20000);
17 h1st->SetFillColor(kRed);
18 h1st->SetMarkerStyle(21);
19 h1st->SetMarkerColor(kRed);
20 hs->Add(h1st);
21 TH1F *h2st = new TH1F("h2st","test hstack",100,-4,4);
22 h2st->FillRandom("gaus",15000);
23 h2st->SetFillColor(kBlue);
24 h2st->SetMarkerStyle(21);
25 h2st->SetMarkerColor(kBlue);
26 hs->Add(h2st);
27 TH1F *h3st = new TH1F("h3st","test hstack",100,-4,4);
28 h3st->FillRandom("gaus",10000);
29 h3st->SetFillColor(kGreen);
30 h3st->SetMarkerStyle(21);
32 hs->Add(h3st);
33
34 TCanvas *cst = new TCanvas("cst","stacked hists",10,10,700,700);
35 cst->Divide(2,2);
36 // in top left pad, draw the stack with defaults
37 cst->cd(1);
38 hs->Draw();
39 // in top right pad, draw the stack in non-stack mode
40 // and errors option
41 cst->cd(2);
42 gPad->SetGrid();
43 hs->Draw("nostack,e1p");
44 //in bottom left, draw in stack mode with "lego1" option
45 cst->cd(3);
46 gPad->SetFrameFillColor(17);
47 gPad->SetTheta(3.77);
48 gPad->SetPhi(2.9);
49 hs->Draw("lego1");
50
51 cst->cd(4);
52 //create two 2-D histograms and draw them in stack mode
53 gPad->SetFrameFillColor(17);
54 THStack *a = new THStack("a","Stacked 2D histograms");
55 TF2 *f1 = new TF2("f1",
56 "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,
58 3600,-2,0.7,-3,0.3};
59 f1->SetParameters(params1);
60 TH2F *h2sta = new TH2F("h2sta","h2sta",20,-4,4,20,-4,4);
61 h2sta->SetFillColor(38);
62 h2sta->FillRandom("f1",4000);
63 TF2 *f2 = new TF2("f2","xygaus + xygaus(5)",-4,4,-4,4);
64 Double_t params2[] = {100,-1.4,1.9,1.1,2, 80,2,0.7,-2,0.5};
65 f2->SetParameters(params2);
66 TH2F *h2stb = new TH2F("h2stb","h2stb",20,-4,4,20,-4,4);
67 h2stb->SetFillColor(46);
68 h2stb->FillRandom("f2",3000);
69 a->Add(h2sta);
70 a->Add(h2stb);
71 a->Draw();
72 return cst;
73}
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:711
void Draw(Option_t *option="") override
Draw this ellipse with its current attributes.
Definition: TEllipse.cxx:168
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:577
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3513
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:668
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:1153
TF1 * f1
Definition: legend1.C:11
TArc a
Definition: textangle.C:12