Drawing TLine over a histogram

Dear ROOTers,

I am trying to do something very simple and I am surprised that it doesn’t work… Using ROOT 5.26/00b, I have two histograms which are plotted on the same canvas and I want to draw a horizontal line at y=0 over them. When I do so, the line doesn’t show on top of the histograms, even though it was plotted last (only bits and pieces appears). Anybody know why that is?

I attached the C-dump of the plot, to show you how it looks like.

Thanks in advance,

Andrée
TLine_test.C (14.1 KB)

The line is correctly drawn in the range that you specified, ie between 0 and 1 .
It is likely however that you want to draw a line between xmin=-3 and xmax=8 on your plot.
In this case replace

TLine *line = new TLine(0,0,1,0); by

TLine *line = new TLine(-3,0,8,0);
Rene

Ah interesting, so I guess I should have looked more carefully at the C file before sending it. That changes the focus of the problem slightly, but it unfortunately remains. The command I used to draw the line originally is:

TLine *l=new TLine(c1->GetUxmin(),0.0,c1->GetUxmax(),0.0);

(The C file I sent is produce using the GUI Save command after the macro is executed)
So it seems that GetUxmin() and GetUxmax() don’t do their work…?

I tried to slim my code down to the basics and even there, I get that the line is plotted from 0 to 1:

void test() {
    gROOT->Reset();
    gStyle->SetOptStat(0);

    TCanvas *c1= new TCanvas;

    TH1D* t0_b_l_h = new TH1D("t0_b_l_h","",110,-3.,8.);
    for (int m=0; m<112; ++m) {
        t0_b_l_h->SetBinContent(m,5);
    }
    t0_b_l_h->Draw("e1");
    TLine *l=new TLine(c1->GetUxmin(),3.0,c1->GetUxmax(),3.0);
    l->SetLineColor(kBlue);
    l->Draw();
}

What am I doing wrong?

Andrée

Before calling c1->GetUxmin you must update the canvas to force the drawing of the histogram, otherwise
the canvas range will not be updated and the original values [0,1] will be taken. So do as shown below

[code]void test() {
//gROOT->Reset(); //NEVER call this in a named macro
gStyle->SetOptStat(0);

TCanvas *c1= new TCanvas;

TH1D* t0_b_l_h = new TH1D("t0_b_l_h","",110,-3.,8.);
for (int m=0; m<112; ++m) {
    t0_b_l_h->SetBinContent(m,5);
}
t0_b_l_h->Draw("e1");
c1->Update();
TLine *l=new TLine(c1->GetUxmin(),3.0,c1->GetUxmax(),3.0);
l->SetLineColor(kBlue);
l->Draw();

}
[/code]
Rene

1 Like

Great, that works just fine, thanks a bunch!

Now, out of curiosity, why shouldn’t I call gROOT->Reset() in a named macro?

Andrée

see documentation of TROOT::Reset at root.cern.ch/root/html/TROOT.html#TROOT:Reset

Rene

Thanks for putting the link to the info. That indeed answered my question.

Andrée