Example of canvas partitioning.
Sometimes the Divide() method is not appropriate to divide a Canvas. Because of the left and right margins, all the pads do not have the same width and height. CanvasPartition does that properly. This example also ensure that the axis labels and titles have the same sizes and that the tick marks length is uniform.
void canvas2()
{
C =
new TCanvas(
"C",
"canvas",1024,640);
CanvasPartition(C,Nx,Ny,lMargin,rMargin,bMargin,tMargin);
h =
new TH1F(
"histo",
"",100,-5.0,5.0);
for (
Int_t i=0;i<Nx;i++) {
for (
Int_t j=0;j<Ny;j++) {
char pname[16];
sprintf(pname,"pad_%i_%i",i,j);
pad[i][j] = (
TPad*)
gROOT->FindObject(pname);
char hname[16];
sprintf(hname,"h_%i_%i",i,j);
}
}
}
{
if (!C) return;
Float_t vStep = (1.- bMargin - tMargin - (Ny-1) * vSpacing) / Ny;
Float_t hStep = (1.- lMargin - rMargin - (Nx-1) * hSpacing) / Nx;
Float_t vposd,vposu,vmard,vmaru,vfactor;
Float_t hposl,hposr,hmarl,hmarr,hfactor;
for (
Int_t i=0;i<Nx;i++) {
if (i==0) {
hposl = 0.0;
hposr = lMargin + hStep;
hfactor = hposr-hposl;
hmarl = lMargin / hfactor;
hmarr = 0.0;
} else if (i == Nx-1) {
hposl = hposr + hSpacing;
hposr = hposl + hStep + rMargin;
hfactor = hposr-hposl;
hmarl = 0.0;
hmarr = rMargin / (hposr-hposl);
} else {
hposl = hposr + hSpacing;
hposr = hposl + hStep;
hfactor = hposr-hposl;
hmarl = 0.0;
hmarr = 0.0;
}
for (
Int_t j=0;j<Ny;j++) {
if (j==0) {
vposd = 0.0;
vposu = bMargin + vStep;
vfactor = vposu-vposd;
vmard = bMargin / vfactor;
vmaru = 0.0;
} else if (j == Ny-1) {
vposd = vposu + vSpacing;
vposu = vposd + vStep + tMargin;
vfactor = vposu-vposd;
vmard = 0.0;
vmaru = tMargin / (vposu-vposd);
} else {
vposd = vposu + vSpacing;
vposu = vposd + vStep;
vfactor = vposu-vposd;
vmard = 0.0;
vmaru = 0.0;
}
char name[16];
sprintf(name,"pad_%i_%i",i,j);
if (pad) delete pad;
pad =
new TPad(name,
"",hposl,vposd,hposr,vposu);
}
}
}
- Author
- Olivier Couet
Definition in file canvas2.C.