Simple way to display histograms with different horizontal (X) error bar lengths on the same canvas?

Is there a way (simple, I hope) to display histograms with different size horizontal (X) error bar lengths on the same canvas? Specifically, I have one TH1 to display with a marker and vertical error bars only (no x-error bar) and another to display on the same canvas (with “SAME” Draw option) with the “E2” option and standard-sized x-error bars. The only way I know of to control x-error bar sizes is by invoking gStyle->SetErrorX(value) - but that is not a histo-dependent setting. I tried toggling this setting between invoking the Draw() commands for the two histos I describe above, but this did not work.

Our graphics expert @couet will be back on Monday, I will let him comment on this question.

gStyle->SetErrorX is a global setting. It cannot be apply on individual histogram. I found a macro in my private examples which might answer your question:

void twohist()
{
   TCanvas *c1 = new TCanvas("c1");
   TH1F *h1 = new TH1F("h1","h1",10,-3,3);
   h1->FillRandom("gaus",10000);
   c1->DrawFrame(h1->GetXaxis()->GetXmin(), h1->GetMinimum(),
                 h1->GetXaxis()->GetXmax(), h1->GetMaximum());
   TExec* ex1 = new TExec("ex1","gStyle->SetErrorX(0.0);");
   ex1->Draw();
   h1->Draw("PE SAME");

   TCanvas *c2 = new TCanvas("c2");
   Double_t x[] = {-3,-2,0,2,3};
   TH1F *h2 = new TH1F("h2","h2",4,x);
   h2->FillRandom("gaus",10000);
   c2->DrawFrame(h2->GetXaxis()->GetXmin(), h2->GetMinimum(),
                 h2->GetXaxis()->GetXmax(), h2->GetMaximum());
   TExec* ex2 = new TExec("ex2","gStyle->SetErrorX(0.5);");
   ex2->Draw();
   h2->Draw("PE SAME");
}
1 Like

Thanks - that works! For the record, I should add that it only works if there is no gStyle->SetErrorX(value) somewhere upstream of running the macro with the TExec commands. ATLAS has a default style macro that gets loaded automatically, and it has a gStyle->SetErrorX(0) thrown in. The TExec command doesn’t seem to be able to toggle SetErrorX to other values in that case.

TExec put an item in the canvas list of primitives which is executed while a picture is painted. The ROOT commands in the TExec are executed when the Exec is encountered. This is a way to change a general setting and reset it back.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.