Re: TCanvas::Divide

Rene Brun (Rene.Brun@cern.ch)
Wed, 04 Jun 1997 08:22:58 +0200


Pasha Murat wrote:
>
> When canvas "page" is divided into several regions (pads),
> the pads are defined as a set of simple variables with their names being
> "page_1", "page_2" etc. A typical task which uses division of a canvas
> consists in filling a set(usually - array) of histograms plotting them.
>
> Suppose one has filled an array of histograms: TH1F* hist[8];
> and has divided a canvas into 8 pads : page->Divide(2,4);
>
> Is there a simple way to plot histograms in a loop with each histogram placed
> on its own pad? To the moment it seems to me to be kind of cumbersome
> operation.
>
> Alternatively one could think of creating an array of pads, for example
> "page_pad[2][4]" or "page_pad[8]" when dividing a canvas. This would
> allow to loop over the pads.
>

One of the possible solutions is shown in this macro:
{
TCanvas *page = new TCanvas("page");
page->Divide(2,4);
TH1F *h[8];
char padname[20];
for (Int_t i=0;i<8;i++) {
sprintf(padname,"page_%d",i+1);
TPad *pad = (TPad*)page->GetPrimitive(padname);
pad->cd();
h[i]->Draw();
}
}

We could also implement functions like:
TPad::cd(Int_t i)
TPad::cd(Int_t i, Int_t j)
Using the first function, the example above will become:

}
TCanvas *page = new TCanvas("page");
page->Divide(2,4);
TH1F *h[8];
for (Int_t i=0;i<8;i++) {
page->cd(i+1);
h[i]->Draw();
}
}

Rene Brun