TObject::DrawClone() does not work in batch mode

Dear ROOT developers and users,

I’d like to draw a graph and its partly magnified image on a sub pad using TGraph::DrawClone().
Following commands work fine in interactive model, but when I try to automize this process by a macro, nothing is drawn on the sub pad.

TGraph *gr; // a graph
TCanvas c1(“c1”);
gr->Draw(“ap”);
TPad p1(“p1”, “”, 0.3, 0.6, 0.65, 0.9);
p1.SetFillStyle(4000);
p1.Draw();
p1.cd();
TGraph *clone = (TGraph *) gr->DrawClone(“ap”);
clone->GetXaxis()->SetRangeUser(0.3, 0.6);

Hello Satoru,
your problem is problem with gROOT->GetSelectedPad() (see in TObject::DrawClone). For example use graph.C from tutorials and modified your macro.C:

{
    TGraph *gr = Graph; // from $ROOTSYS/tutorials/graph.C 
    TCanvas c2("c2");
    gr->Draw("ap");
    TPad p1("p1", "", 0.3, 0.6, 0.65, 0.9);
    p1.SetFillStyle(4000);
    p1.Draw();
    p1.cd();
    Printf("%s", gROOT->GetSelectedPad()->GetName());
    gPad->Update(); // important !!!
    Printf("%s", gROOT->GetSelectedPad()->GetName());
    TGraph *clone = (TGraph *) gr->DrawClone("ap");
    clone->GetXaxis()->SetRangeUser(0.3, 0.6);
}

now simple:

I hope this help,
Jan

Hi Jan,
thank you for the reply.

It sure works with gPad->Update().
It’s never occured to me because I thought that gPad->Update() is not about changing the status of objects but only about painting. It looks like a kind of pitfall …
By the way, the batch mode has another limitation: failure to interpret a one-liner cast such as
((TGraph*)p1.FindObject(“gr”))->GetYaxis()->SetNdivisions(505);

Thanks anyway!

Hello Satoru,

[quote]By the way, the batch mode has another limitation: failure to interpret a one-liner cast such as
((TGraph*)p1.FindObject(“gr”))->GetYaxis()->SetNdivisions(505); [/quote]
name of TGraph object is Graph !

[quote]root [16] p1.GetListOfPrimitives()->ls()
TFrame X1= 0.292600 Y1=1.185789 X2=0.606100 Y2=10.796640
OBJ: TGraph Graph a simple graph : 0 at: 0x9d63f88
OBJ: TPaveText title X1= 0.257331 Y1=11.123450 X2=0.343994 Y2=11.937929[/quote]
so: ((TGraph*)p1.FindObject("Graph"))->GetYaxis()->SetNdivisions(505)
or more clear: clone->SetName("clone_gr"); ((TGraph*)p1.FindObject("clone_gr"))->GetYaxis()->SetNdivisions(505);
Jan

Hi Jan,

You are right.
It was not a problem of cast.
Thank you for your advice.