Get current canvas

Hi,
I have problem with get pointer to current canvas (exactly GetSelectedPad() ), see exam.C:

void exam()
{
TH1F *h1 = new TH1F(“h1”,"",100,-10,10);
h1->Draw();

TPad current_pad = (TPad)gROOT->GetSelectedPad();
if (current_pad)
current_pad->GetCanvas()->Update();
else
Printf(“where is pointer ?”);
}

root [0] .x exam.C
TCanvas::MakeDefCanvas: created default TCanvas with name c1
where is pointer ?
root [1]

Can you any help ? FC3, gcc 3.4, root_v4.03.02
Thanks, Jan

Hi,
TROOT::GetSelectedPad() returns pointer at TVirtualPad, so replace this line

by
TPad current_pad = (TVirtualPad)gROOT->GetSelectedPad(); [color=red]<- this is a mistake (see tpochep’s post below)[/color]
or simply use
TPad *current_pad = gPad;

Your first line is ill-formed.

  1. TROOT::GetSelectedPad() returns pointer to TVirtualPad, what does you cast from TVirtualPad * to TVirtualPad * mean?
  2. TVirtualPad is a base of TPad, you cannot assign - you need explicit cast

with pointer to TPad is my mistake, sorry.
But TPad current_pad = (TVirtualPad)gROOT->GetSelectedPad() -> current_pad = 0 !!! (why ???)

TPad *current_pad = gPad -> working !!! pointer is OK

remember, all commands are in macro exam.C (in command line, fine working)

Hello, musinsky

My message about ill-formed code was not addressed to you.
kbat wrote ill-formed code

TPad * p = gROOT->GetSelectedPad();// here is an error - even if CINT can interpret it, in C++ you cannot assign

Base * p = new Derived;//standard implicit conversion
Derived * p1 = p; //this is inverse of standard conversion, it requires EXPLICIT cast (you can use static_cast, C-style cast, functional-style cast (with correspondent typedef), even dynamic_cast :slight_smile: ).

  1. It’s a CINT bug, that you can assign TVirtualPad * to TPad *
  2. You get 0 (it’s only IMHO) from GetSelectedPad because after pad creation you have not selected it. But gPad contains correct pointer you need. After all you first code is very strange, what are you going to do with it?

Thanks tpochep, now understand.

Thanks tpochep, I understood my mistake.

Hello, Konstantin.
I guess it was a TYPO in you message.
And CINT’s bug.

And you answer about gPad was correct :slight_smile:

Let’s call it a CINT limitation.
The best practice to avoid such “bugs” is passing
script through “real” compiler

Regards. Valeriy

OK :smiley: