Problem with drawing TLine

Hi!

So, I’ve got a problem with drawing a vertical line on a plot using a code below:

#include <iostream>
#include <TCanvas.h>
#include <TH1F.h>
#include <TF1.h>
#include <TLine.h>

void draw_canvas(TH1* h, string name, string xlabel, string ylabel) {
        TCanvas* c = new TCanvas(Form("c_%s",name.c_str()),Form("c_%s",name.c_str()),800,600);
        c->cd();
        h->GetXaxis()->SetTitle(xlabel.c_str());
        h->GetYaxis()->SetTitle(ylabel.c_str());
        h->DrawClone("");
        //c->Update();
        TLine* tline = new TLine();
        tline->SetLineWidth(1);
        tline->SetLineColor(kBlack);
        tline->SetLineStyle(2);
        tline->DrawLine(6.,0.,6.,gPad->GetUymax());
        c->SaveAs(Form("line_problem_%s.png",name.c_str()));
}

void line_problem(){
                auto f1 = new TF1("f1","x*gaus(0) + abs(sin(x)/x)",0,10);
                auto f2 = new TF1("f2","x*exp(-x) + abs(sin(x)/x)",0,10);
                auto h1 = new TH1F("h1","Test1",200,0,10);
                h1->FillRandom("f1",10000);
                auto h2 = new TH1F("h2","Test2",200,0,10);
                h2->FillRandom("f2",10000);
                draw_canvas(h1, "name1", "title1", "entries");
                draw_canvas(h2, "name2", "title2", "entries");
}

When I uncomment the line with `c->Update()’ the line is being drawn but I get the contents of both canvases plotted on the first one. And when I comment this line out I don’t get any lines at all. I’ll appreciate any suggestions!

(using ROOT 6.18/04, executing with “root -x line_problem.C++”)

Best,
Maciej

I do not see the problem, on Mac with the latest ROOT version. With or without the update I get the following:

Thanks for the reply!
Do you run it with root -x macro.C++ or some other way?
I’ll install the latest ROOT version and I’ll let you know if the problem persist.

I ran it this way and also without Aclic.

I updated my ROOT version and the problem persists (ROOT Version: 6.26/10, Ubuntu 20.04.5 LTS, compiled from sources).

This is what I’m getting without updating the canvas:

And this is what I get when I include c->Update():

I remember having similar problems in the past (also with using TPad, etc), so it would be really great if there was a way to solve this.

Thanks for your help!

So the line does not show at all in your case. I see you generate png files too. Are they also wrong ?

otherwise I can suggestthe following:

#include <iostream>
#include <TCanvas.h>
#include <TH1F.h>
#include <TF1.h>
#include <TLine.h>

void draw_canvas(TH1* h, string name, string xlabel, string ylabel) {
   auto c = new TCanvas(Form("c_%s",name.c_str()),Form("c_%s",name.c_str()),800,600);
   c->cd();
   h->GetXaxis()->SetTitle(xlabel.c_str());
   h->GetYaxis()->SetTitle(ylabel.c_str());
   h->DrawClone("");
   auto tline = new TLine();
   tline->SetLineWidth(1);
   tline->SetLineColor(kBlack);
   tline->SetLineStyle(2);
   tline->DrawLine(6.,0.,6.,gPad->GetUymax());
   c->Modified();
   c->Update();
   c->SaveAs(Form("line_problem_%s.png",name.c_str()));
}

void line_problem(){
   auto f1 = new TF1("f1","x*gaus(0) + abs(sin(x)/x)",0,10);
   auto f2 = new TF1("f2","x*exp(-x) + abs(sin(x)/x)",0,10);
   auto h1 = new TH1F("h1","Test1",200,0,10);
   h1->FillRandom("f1",10000);
   auto h2 = new TH1F("h2","Test2",200,0,10);
   h2->FillRandom("f2",10000);
   draw_canvas(h1, "name1", "title1", "entries");
   draw_canvas(h2, "name2", "title2", "entries");
}

Either: gROOT->SetSelectedPad(c); h->DrawClone("");
or: h->DrawCopy("");

The following is also fine g=for me:

void draw_canvas(TH1* h, string name, string xlabel, string ylabel) {
   auto c = new TCanvas(Form("c_%s",name.c_str()),Form("c_%s",name.c_str()),800,600);
   c->cd();
   h->GetXaxis()->SetTitle(xlabel.c_str());
   h->GetYaxis()->SetTitle(ylabel.c_str());
   h->Draw();
   c->Update();
   auto tline = new TLine();
   tline->SetLineWidth(1);
   tline->SetLineColor(kBlack);
   tline->SetLineStyle(2);
   tline->DrawLine(6.,0.,6.,gPad->GetUymax());
   c->Modified();
   c->Update();
   c->SaveAs(Form("line_problem_%s.png",name.c_str()));
}

Thanks to your suggestions, I got it working with:

void draw_canvas(TH1* h, string name, string xlabel, string ylabel) {
   auto c = new TCanvas(Form("c_%s",name.c_str()),Form("c_%s",name.c_str()),800,600);
   c->cd();
   h->GetXaxis()->SetTitle(xlabel.c_str());
   h->GetYaxis()->SetTitle(ylabel.c_str());
   h->DrawCopy("");
   c->Modified();
   c->Update();
   auto tline = new TLine();
   tline->SetLineWidth(1);
   tline->SetLineColor(kBlack);
   tline->SetLineStyle(2);
   tline->DrawLine(6.,0.,6.,gPad->GetUymax());
   c->SaveAs(Form("line_problem_%s.png",name.c_str()));
}

Although, I don’t understand the problem… I do get that I should call c->Modified() and c->Update() for the gPad->GetUymax() to work properly (it won’t work in the batch mode, for example, right?). But I don’t understand why there is such a different behaviour for DrawClone() and DrawCopy() - and this is the modification that really helps here.

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