Meaning of gROOT->GetSelectedPad()

Hi,
if you use “TObject::DrawClone”, sometimes the clone is drawn in a canvas which you don’t expect. E.g:

#include "TH1.h"
#include "TCanvas.h"
#include "TF1.h"

#include <iostream>

using namespace std;

void drawcloneex()
{


  TH1F* hOne = new TH1F("hist1","hist1",40,-10,10);
  TH1F* hTwo = new TH1F("hist2","hist2",40,-10,10);

  // =====  work on canvas 1.... ===========

  TCanvas* c1 = new TCanvas("canvas1","canvas1");
  hOne->FillRandom("pol0");
  hOne->Draw();

  // ... do stuff;

  gPad->Modified();gPad->Update();

  // ===== work on canvas 2.... ============

  TCanvas* c2 = new TCanvas("canvas2","canvas2");
  hTwo->FillRandom("gaus");
  hTwo->Fit( "gaus", "MIL", "", -10,0 );


  //  if ( debug ) 
  //    {
      TH1F* gf = hTwo->GetFunction("gaus");
      gf->DrawClone("same");   // !!!!!!!!! <- Draws on canvas 1 !?!!!
  //    }

  // ... do more stuff;
  
}

The source-code of TObject reveals, that DrawClone draws into gROOT->GetSelectedPad() instead of gPad. Does this mean, that “TObject::DrawClone()” must not be used because it does unpredictable things or is there some documentation about what the result of gROOT->GetSelectedPad() points to?

Cheers, J.
drawcloneex.C (776 Bytes)

see changes and comments in the updated file below:

[code]#include “TH1.h”
#include “TCanvas.h”
#include “TF1.h”

#include

using namespace std;

void drawcloneex()
{

TH1F* hOne = new TH1F(“hist1”,“hist1”,40,-10,10);
TH1F* hTwo = new TH1F(“hist2”,“hist2”,40,-10,10);

// ===== work on canvas 1… ===========

TCanvas* c1 = new TCanvas(“canvas1”,“canvas1”);
hOne->FillRandom(“pol0”);
hOne->Draw();

// … do stuff;

c1->Modified();c1->Update(); //line not required

// ===== work on canvas 2… ============

TCanvas* c2 = new TCanvas(“canvas2”,“canvas2”);
hTwo->FillRandom(“gaus”);
hTwo->Fit( “gaus”, “MIL”, “”, -10,0 );
c2->Modified();c2->Update(); //line not required

// if ( debug )
// {
//TH1F* gf = hTwo->GetFunction(“gaus”); //this WAS WRONG
TF1* gf = hTwo->GetFunction(“gaus”);
gf->DrawClone(“same”); // !!! <- Draws on canvas 1 !!!
// }

// … do more stuff;

}
[/code]

Rene

Hi,

I’m sorry, the line

      //TH1F* gf = hTwo->GetFunction("gaus");  //this WAS WRONG
      TF1* gf = hTwo->GetFunction("gaus"); 

was clearly a type which was introduced when I reduced my “real” problem to this example (although I thought, that I’ve tested the version which I’ve posted).

However, I almost missed your subtle change, which fixed the real problem.
you did replace

  gPad->Modified();gPad->Update();

with

  c1->Modified();c1->Update(); //line not required

In this “minimal” example the line is in fact not necessary. In my “real life” example I do some manipulations on the histogram (abbreviated ad “// … do stuff” in this example). For this reason, I need the c1->Modified(); c1->Update(); pair to make these changes visible.

I attached two (working) versions of my example to this post to demonstrate the difference (look at canvas1 in both cases to see the difference).
Isn’t it safe to use “gPad” in a function? Is there a rule of thumb to avoid problems like this?

Cheers, J.
drawcloneexfixed.C (846 Bytes)
drawcloneexbroken.C (781 Bytes)