TLine not shown

Dear experts,

I used the following macro to draw TLines in each pad, but those lines were not shown in the saved .pdf file. Could you please take a look? Thanks.

void test () {
  THStack *hs = new THStack("hs","Stacked 1D histograms");
  //create three 1-d histograms
  TH1F *h1st = new TH1F("h1st","test hstack",100,-4,4);
  h1st->FillRandom("gaus",20000);
  h1st->SetFillColor(kRed);
  h1st->SetMarkerStyle(21);
  h1st->SetMarkerColor(kRed);
  hs->Add(h1st);
  TH1F *h2st = new TH1F("h2st","test hstack",100,-4,4);
  h2st->FillRandom("gaus",15000);
  h2st->SetFillColor(kBlue);
  h2st->SetMarkerStyle(21);
  h2st->SetMarkerColor(kBlue);
  hs->Add(h2st);
  TH1F *h3st = new TH1F("h3st","test hstack",100,-4,4);
  h3st->FillRandom("gaus",10000);
  h3st->SetFillColor(kGreen);
  h3st->SetMarkerStyle(21);
  h3st->SetMarkerColor(kGreen);
  hs->Add(h3st);

  TCanvas *cst = new TCanvas("cst","stacked hists");
  hs->Draw("pads");
  gPad->Update();

  const int N = hs->GetNhists();
  TLine *ln[N];

  for (int i = 0; i < N; ++i) {
    cst->cd(i+1);
    ln[i] = new TLine(0.,gPad->GetUymin(),0.,gPad->GetUymax());
    ln[i]->SetLineStyle(2);
    ln[i]->Draw("same");
  }

  cst->SaveAs("test.pdf");
}

ROOT Version: 6.24/06
Platform: CentOS
Compiler: GCC 9.3.0


@couet This looks like a bug. The drawn lines disappear as soon as one tries any of the command lines:
cst->SaveAs("test.pdf"); // the last line in the macro
cst->Modified(); cst->Update();
cst->cd(0); gPad->Modified(); gPad->Update();

BTW. @Yeung TLine has no “same” drawing option (just use “ln[i]->Draw();”).

Thanks. I didn’t know that. :sweat_smile:

I see this weird effect. No need to save in a .pdf file. Just resize the canvas and le lines disappear . Looking at it…

@Yeung A temporary brutal fix:

  TCanvas *cst = new TCanvas("cst", "stacked hists");
  hs->Draw("pads");
  cst->Modified(); cst->Update();
  for (int i = 0; i < hs->GetNhists(); i++) {
    cst->cd(i + 1);
    TLine *ln = new TLine(0., gPad->GetUymin(), 0., gPad->GetUymax());
    ln->SetLineStyle(2);
    ((TH1*)(hs->GetHists()->At(i)))->GetListOfFunctions()->Add(ln);
  }
  cst->cd(0);
  cst->Modified(); cst->Update();

The option “pads” is the faulty one. Here is the minimal reproducer.

void test () {
   auto hs = new THStack("hs","");
   auto h1 = new TH1F("h1","h1",100,-4,4);
   h1->FillRandom("gaus",20000); hs->Add(h1);

   auto c = new TCanvas();
   hs->Draw("pads"); // If the option pads is selected the TText Drawn later disappear when the canvas is resized manually
   gPad->Update();

   c->cd(1);
   auto *t = new TText(0.,400.,"Text");
   t->Draw();
}

I am investigating. My guess is that the pads are rebuilt each time the stack is re-paint and what was added in the pads is dropped. I’ll see how we can fix that.

This PR fixes the problem. Thanks to have seen it.

1 Like

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