Function returning a TPad

I am trying to build a function returning a TPad.
The idea is to fill the TPad with the objects I need to draw and then plot the TPad into a TCanvas.
The problem I have is that the resulting TCanvas seems not to contain the TPad I wanted to plot.

This is a simple working example of my problem:

TPad *getPad() {
  TPad *pad= new TPad("pad","pad",0.,0.,1.,1.);
  TH1D *h = new TH1D("h","h",20,-5.,5.);
  h->FillRandom("gaus");
  pad->cd();
  h->Draw();
  return pad;
}

int padFunc()
{
  TCanvas *c = new TCanvas("c","c",500,500);
  c->cd();
  TPad *pad =getPad();
  pad->Draw();
  c->Update();
}

try to run it on CINT using .x padFunc.cc

Can anybody suggest me what’s wrong with that?

see required change below. You must draw the pad before drawing anything in it.

Rene

[code]TPad *getPad() {
TPad *pad= new TPad(“pad”,“pad”,0.,0.,1.,1.);
pad->Draw();
TH1D *h = new TH1D(“h”,“h”,20,-5.,5.);
h->FillRandom(“gaus”);
pad->cd();
h->Draw();
return pad;
}

int padFunc()
{
TCanvas *c = new TCanvas(“c”,“c”,500,500);
c->cd();
TPad *pad =getPad();
c->Update();
}
[/code]

Also the following code does it:

TPad *getPad() {
   TPad *pad= new TPad("pad","pad",0.,0.,1.,1.);
   TH1D *h = new TH1D("h","h",20,-5.,5.);
   h->FillRandom("gaus");
   pad->cd();
   h->Draw();
   return pad;
}

void padFunc()
{ 
   TCanvas *c = new TCanvas("c","c",500,500);
   c->cd();
   TPad *pad =getPad();
   c->GetListOfPrimitives()->Add(pad);
}

Thanks Rene.

What if I wanted to sub-divide the pad into two other?

I tried writing

TPad *pad=...
pad->Divide(2);
pad->Draw();
...

but it is not working.

I also tried to call

TPad *pad =....
pad->Divide(2);
pad->GetPad(1)->Draw();
pad->GetPad(2)->Draw();
...

but is not working.

[quote=“puma”]Thanks Rene.

What if I wanted to sub-divide the pad into two other?

I tried writing

TPad *pad=...
pad->Divide(2);
pad->Draw();
...

but it is not working.

I also tried to call

TPad *pad =....
pad->Divide(2);
pad->GetPad(1)->Draw();
pad->GetPad(2)->Draw();
...

but is not working.[/quote]

Sorry if I didn’t check couet answer before.
His method is working even with multiple pads.
Thank you very much!