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