Multiple Stacked Histograms Tutorial


Ported to notebook by: Theis Hansen

The Histograms

To begin with, we create a stacked histogram graph using the THStack class onto which we will plot two histograms defined by two functions.


In [1]:
THStack theStack("theStack","Stacked 2D histograms")


(THStack &) Name: theStack Title: Stacked 2D histograms

We then create our two histograms with their corresponding functions


In [2]:
TF2 f1("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("h2sta","h2sta",20,-4,4,20,-4,4);
h2sta.SetFillColor(38);
h2sta.FillRandom("f1",4000);

TF2 f2("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("h2stb","h2stb",20,-4,4,20,-4,4);
h2stb.SetFillColor(46);
h2stb.FillRandom("f2",3000);

We then add them onto our stacked histogram graph, and draw it.


In [3]:
theStack.Add(&h2sta);
theStack.Add(&h2stb);
TCanvas c;
theStack.Draw();
c.Draw();