ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hstack.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// Example of stacked histograms: class THStack.
4 ///
5 /// \macro_image
6 /// \macro_code
7 ///
8 /// \author Rene Brun
9 
10 TCanvas *hstack() {
11  THStack *hs = new THStack("hs","Stacked 1D histograms");
12  //create three 1-d histograms
13  TH1F *h1st = new TH1F("h1st","test hstack",100,-4,4);
14  h1st->FillRandom("gaus",20000);
15  h1st->SetFillColor(kRed);
16  h1st->SetMarkerStyle(21);
17  h1st->SetMarkerColor(kRed);
18  hs->Add(h1st);
19  TH1F *h2st = new TH1F("h2st","test hstack",100,-4,4);
20  h2st->FillRandom("gaus",15000);
21  h2st->SetFillColor(kBlue);
22  h2st->SetMarkerStyle(21);
23  h2st->SetMarkerColor(kBlue);
24  hs->Add(h2st);
25  TH1F *h3st = new TH1F("h3st","test hstack",100,-4,4);
26  h3st->FillRandom("gaus",10000);
27  h3st->SetFillColor(kGreen);
28  h3st->SetMarkerStyle(21);
29  h3st->SetMarkerColor(kGreen);
30  hs->Add(h3st);
31 
32  TCanvas *cst = new TCanvas("cst","stacked hists",10,10,700,700);
33  cst->SetFillColor(41);
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 }
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:432
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH2.cxx:592
The Histogram stack class.
Definition: THStack.h:35
Definition: Rtypes.h:61
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
Definition: Rtypes.h:61
virtual void Draw(Option_t *chopt="")
Draw this multihist with its current attributes.
Definition: THStack.cxx:422
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3330
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
A 2-Dim function with parameters.
Definition: TF2.h:33
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
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:336
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1073
#define gPad
Definition: TVirtualPad.h:288
Definition: Rtypes.h:61