Logo ROOT  
Reference Guide
hstack.C File Reference

Detailed Description

View in nbviewer Open in SWAN Example of stacked histograms: class THStack.

TCanvas *hstack() {
THStack *hs = new THStack("hs","Stacked 1D histograms");
//create three 1-d histograms
TH1F *h1st = new TH1F("h1st","test hstack",100,-4,4);
h1st->FillRandom("gaus",20000);
h1st->SetFillColor(kRed);
h1st->SetMarkerStyle(21);
h1st->SetMarkerColor(kRed);
hs->Add(h1st);
TH1F *h2st = new TH1F("h2st","test hstack",100,-4,4);
h2st->FillRandom("gaus",15000);
h2st->SetFillColor(kBlue);
h2st->SetMarkerStyle(21);
h2st->SetMarkerColor(kBlue);
hs->Add(h2st);
TH1F *h3st = new TH1F("h3st","test hstack",100,-4,4);
h3st->FillRandom("gaus",10000);
h3st->SetFillColor(kGreen);
h3st->SetMarkerStyle(21);
h3st->SetMarkerColor(kGreen);
hs->Add(h3st);
TCanvas *cst = new TCanvas("cst","stacked hists",10,10,700,700);
cst->Divide(2,2);
// in top left pad, draw the stack with defaults
cst->cd(1);
hs->Draw();
// in top right pad, draw the stack in non-stack mode
// and errors option
cst->cd(2);
gPad->SetGrid();
hs->Draw("nostack,e1p");
//in bottom left, draw in stack mode with "lego1" option
cst->cd(3);
gPad->SetFrameFillColor(17);
gPad->SetTheta(3.77);
gPad->SetPhi(2.9);
hs->Draw("lego1");
cst->cd(4);
//create two 2-D histograms and draw them in stack mode
gPad->SetFrameFillColor(17);
THStack *a = new THStack("a","Stacked 2D histograms");
TF2 *f1 = new TF2("f1",
"xygaus + xygaus(5) + xylandau(10)",-4,4,-4,4);
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};
f1->SetParameters(params1);
TH2F *h2sta = new TH2F("h2sta","h2sta",20,-4,4,20,-4,4);
h2sta->SetFillColor(38);
h2sta->FillRandom("f1",4000);
TF2 *f2 = new TF2("f2","xygaus + xygaus(5)",-4,4,-4,4);
Double_t params2[] = {100,-1.4,1.9,1.1,2, 80,2,0.7,-2,0.5};
f2->SetParameters(params2);
TH2F *h2stb = new TH2F("h2stb","h2stb",20,-4,4,20,-4,4);
h2stb->SetFillColor(46);
h2stb->FillRandom("f2",3000);
a->Add(h2sta);
a->Add(h2stb);
a->Draw();
return cst;
}
Author
Rene Brun

Definition in file hstack.C.

kGreen
@ kGreen
Definition: Rtypes.h:66
TH2F
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
THStack
Definition: THStack.h:38
THStack::Add
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:361
TPad::Divide
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:1166
TF1::SetParameters
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:640
TH2::FillRandom
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH2.cxx:599
TCanvas::cd
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:704
a
auto * a
Definition: textangle.C:12
kRed
@ kRed
Definition: Rtypes.h:66
TH1::FillRandom
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3444
TF2
Definition: TF2.h:29
f1
TF1 * f1
Definition: legend1.C:11
Double_t
double Double_t
Definition: RtypesCore.h:59
TCanvas
Definition: TCanvas.h:23
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:572
kBlue
@ kBlue
Definition: Rtypes.h:66
gPad
#define gPad
Definition: TVirtualPad.h:287
THStack::Draw
virtual void Draw(Option_t *chopt="")
Draw this multihist with its current attributes.
Definition: THStack.cxx:447