Set error bars in THStack histogram

Dear experts,

can you please point me an example which show how to draw error bars in a THStack histogram as shown in the file I sent you? actually have a THStack with MC samples and I want to put error bars for the sum of the MC samples.

THErr.pdf (6.04 KB)

It is not clear in your example what is in the stack and what is not. Here is a possible example:

   TH1D *h1 = new TH1D("h1","h1",10,0,10);
   TH1D *h2 = new TH1D("h2","h2",10,0,10);
   THStack h;


   for(int i=0; i<11; i++){

   h.Add(h1, "HIST");

Actually here is a piece of code that I’m using:

  // background
  TH1D *httV,*ht....

  // data
  TH1D *hdata

  // THStack fill with background
  THStack *hs = new THStack("hs","stacked histograms");
  hs->Add(httV); hs->Add(ht); hs->Add(hVV); hs->Add(httjets); hs->Add(hdy); hs->Add(hwjets); hs->Add(hqcd);

  // draw data on THStack histogram
  hdata->Draw("hist e&p");
  hs->Draw("hist same");
  hdata->Draw("hist same&e&p");
  htth->Draw("hist same");

when I do that I have error bars for data but how can I set error boxes for the THStack histogram which contains the MC? I want to draw the MC on top of each other, so the error bars should represent the “error sum” of all the MC sample in the THStack…

Best regards

Look at the code I sent you … you can set the drawing option individually for each histogram in the stack at the “Add” time.


  • I looked at you code but I did not get was I wanted (I should have been more clear), actually it does set the errors but individually for each histogram as you can see in file THerror.png that I append.

  • what I want is a “globar error” for each bin, that I can have on top of the THstack like in this file THErrall.pdf that I append (the gray shade area).

  • Do you know how to do that?

THSerrAll.pdf (6.73 KB)

Ok I understand now. There is no automatic way to compute the global error.
The way do to it would be to create an histogram as the sum of all the histograms in the stack
and you plot it on the stack using the option SAME.

I created a hist_err where I added all the background:

then I drawn it in the same canvas then the THStack histogram using:
herr->Draw(“same e2”);

  1. I got what I wanted. It’s just that it drawn the error bars with “light green” rectangles.
    Is there a way that I can change the error rectangle bars color (or use a kind of grey shade)?

I eventually tired before plotting: herr->SetLineColor(40); without success


Dear Couet,

for the color I guess that I found what is wrong in my code.

Thanks you very much for you help!


@calbet , if you have a script could you please share here ?
I’m looking for the same thing and did not get how you did it.