TLines on Spawned Canvases/Pads are drawn to the wrong Pad


I have a code designed to show a 2-dimensional histogram. I have a TExec that updates a pad/text to show which bin the user’s mouse is over, and on click creates another canvas with more information. Ideally I would like to draw lines on a pad on the spawned canvas, but they appear in the wrong place (somewhere on the original canvas) despite calls to TPad::cd().

I’ve made a small example which reproduces the bug:

RootDrawingBug.C (3.5 KB)

My original code is a mix of macro and compiled code and I can update the pad/text with mouse position. However, the example is a pure macro and the pad/text for the mouse position doesn’t update until the second canvas is drawn, despite calling TPad::Update() in the same way as my original code.

As far as where the TLines appear, they usually appear in the most recently updated pad (e.g., the pad where I show the text with mouse position information). However, I added the TExec to the pad with some original TLines, and now they appear there.

I’m still curious as to why the pad isn’t updated in the example macro–so if someone identifies why that is, I would like to hear it.

However, I found the solution that solves my original problem, which is that each TLine must be allocated to the heap and drawn individually.

Before I had something like

//Draw vertical lines
TLine line;
for(double d = 0.25; d < 1.0; d += 0.25)


This needed to be

//Same as above, but the lines will be drawn to the gPad
for(double d = 0.25; d < 1.0; d += 0.25)
  TLine* line = new TLine(d, 0.1, d, 0.9);

In your “original” code, try: gROOT->SetSelectedPad(gPad); line.DrawClone();

I tried that and it also works.

TLine::DrawClone() just uses TObject::DrawClone() and that draws to gROOT->GetSelectedPad().
TObject::Draw() calls TObject::AppendPad() and that draws to gPad.

It’s weird that they’re not the same but it is what it is. Thank you for that solution.

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