Table of Contents

Using and Troubleshooting THStack

Basic Code

  TH1F *h;
  THStack *hs = new THStack("hs","Stacked 1D histograms");
  //create three 1-d histograms
  for (int i = 0; i < 3; i++) {
    h = new TH1F("h"+i,"test hstack",100,-4,4);
    h->FillRandom("gaus",20000-i*5000);
    h->SetFillColor(i+2);
    h->SetMarkerColor(i+2);
    hs->Add(h);
  }
  TCanvas *cst = new TCanvas("cst","stacked hists",10,10,700,700);
  cst->SetFillColor(41);
  hs->Draw()

Troubleshooting

Y Axis Range

see Set Display Range of Axis

Scaling/Normalising Histograms

Do not use SetNormFactor(normFactor)!

If SetNormFactor on a histogram has been called, this histogram will not be stacked, but is painted seperately (as with nostack). Instead use the following code to normalise histogram h

h->Scale(normFactor/h->Integral());