Error rectangles filled incorectly

Hello,
I have a problem with errors in my histogram - I would like to have error rectangles. For that, I need to set filling color to my histogram, as I found somewhere. So I do it, but then when I draw it, it fills not only the error rectangles, but also whole area under the graph. Error bars look just fine when I draw them, but I would like to have the rectangles.
Any ideas how I could settle that? It is probably just something very simple, but I cannot find anything on this topic anywhere.
Thank you very much!

1 Like

Can you post a small piece of code reproducing the problem you encountered ?

Sure. Here it is:

void errorproblem(){
  TFile *tau = TFile::Open("data_out.root"); //just taking the histogram out of file
  TH1D *h = (TH1D*)tau->Get("h");
  h->SetFillColor(kGray);  //setting filling color
 
  TCanvas *C = new TCanvas("C", "h", 1000, 800); 
  h->Draw("same hist E2");
}

An alternative is to use TGraphErrors, which should do what you want; something like:

  TGraphErrors *gr = new TGraphErrors(n, x, y, x_err, y_err);
  gr->SetLineColor(kGray);
  gr->SetLineWidth(2);
  gr->SetFillStyle(3001);
  gr->SetFillColor(kGray);
  gr->Draw("ALP2");

It seems ok for me:

void errorproblem(){
   auto h = new TH1F("h","h",100,0,1);
   h->FillRandom("gaus");
   h->SetFillColor(kGray);  //setting filling color
   h->Draw("  E2");
}

Thank you! The problem was the "hist" option in Draw(). When I omit that, it makes the errors right.
The downside to this is the fact that to have the lines there, I must now draw the histogram twice, and thus define and set everything twice, but that is manageable.

Note also you should not use SAME when you plot the first histogram.
Can you should me how you final plot should looks like ?
I may find an easier solution.

Thanks.
If you find easier solution, that would be perfect! This is how it should look like (and which I am able to achieve, but in quite lengthy manner)
ff_pt_1.pdf (13.6 KB)

The way to do it will be:

void errorproblem(){
   auto h = new TH1F("h","h",100,0,1);
   h->FillRandom("gaus");
   h->SetFillColor(kGray);  //setting filling color
   h->Draw("E2");
   TH1 *hh = h->DrawCopy("HIST SAME");
   hh->SetFillStyle(0);
   hh->SetLineColor(kRed);
   hh->SetLineWidth(3);
}

May be you found the same solution.

1 Like

Thank you, I did not realize there is DrawCopy, I was creating the histogram again from scratch. This will save me a quite a number of lines of code, thanks!

1 Like