How to draw vertical error bars only in TLegend

Hi,

I have been requested for a paper to draw the data points with vertical error bars only. Browsing the ROOT pages I have found [1] that

TLegendEntry* AddEntry(const char* name, const char* label = “”, Option_t* option = “lpf”)

should do the job. (Well, after another look at the ROOT pages it looks like “ep” does not work for any of the two AddEntry methods [2].) It does not work for me, as you can see with the simple ROOT macro that I include in this post.

#include "TCanvas.h"
#include "TF1.h"
#include "TH1.h"
#include "TLegend.h"
#include "TStyle.h"


void testLegendOption()
{
  gStyle->SetOptStat(0);
  

  Double_t mhmin = 100;
  Double_t mhmax = 109;

  TF1* f1 = new TF1("f1", "gaus(0)", mhmin, mhmax);

  f1->SetParameters(1, 0.5*(mhmax+mhmin), 0.2*(mhmax-mhmin));

  TH1F* h1 = new TH1F("h1", "h1", 10*Int_t(mhmax-mhmin), mhmin, mhmax);

  h1->FillRandom("f1", 10000);


  // Draw
  //----------------------------------------------------------------------------
  TCanvas* c1 = new TCanvas();

  c1->cd();
  
  h1->SetMarkerStyle(kFullCircle);
  h1->SetTitle("");

  h1->Draw("ep");

  TLegend* legend = new TLegend(0.68, 0.72, 0.88, 0.89);

  legend->AddEntry("h1", " data", "ep");

  legend->SetBorderSize( 0);
  legend->SetFillColor ( 0);
  legend->SetTextFont  (42);

  legend->Draw("same");
}

Does anyone know how to fix this issue?

Thanks,
Jonatan

[1] root.cern.ch/root/html522/src/TL … html#QKf7K
[2] root.cern.ch/root/html/TLegend.h … d:AddEntry

It looks like your macro does that. See what I get with the latest ROOT version on Mac.


Note the colour difference of error bars, though. The histogram has them blue, the legend black.

One more thing … the “EP” combination works in ROOT 5.34 and newer (maybe also in 5.32, but I’m not sure).

Thank you both.

In ROOT 5.30 the “EP” combination does not work. My patch has been drawing a vertical line by hand :confused:

Cheers,
Jonatan

Just two additional notes.

  1. I believe you wanted to draw your histogram using: h1->Draw(“ex0p”);
  2. You can simply say (don’t use “SAME” if it’s not really necessary - I remember cases in which it produced unwanted side effects, probably being interpreted as 4 independent flags): legend->Draw();
    See also http://root.cern.ch/root/html/THistPainter.html

Hi Pepe,

  1. I wanted to draw the histogram simply using: h1->Draw(“ep”), with both x and y error bars. It’s the Legend that I wanted only with y error bar.

  2. Thank you.

Cheers,
Jonatan

Fixed

In summary, use the “ep” option with TLegend::AddEntry. This works in root v5.34 and does not work in versions 5.32, 5.30, and earlier versions.

Hello,

[quote=“couet”][quote]
Note the colour difference of error bars, though. The histogram has them blue, the legend black.
[/quote]

Fixed[/quote]

The color of the line is still different between the histogram marker in the marker in the legend in root 5.34/00. Did you mean that one can fix this manually? If so, how? Or did you mean that it was fixed in some particular version of root? If so, in what version was that fixed?

Thanks,
Martin

According to: root.cern.ch/drupal/content/root … ease-notes
it was fixed in 5.34/11 … so /00 has the bug.