Logo ROOT  
Reference Guide
canvas2.C File Reference

Detailed Description

View in nbviewer Open in SWAN 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 CanvasPartition(TCanvas *C,const Int_t Nx = 2,const Int_t Ny = 2,
Float_t lMargin = 0.15, Float_t rMargin = 0.05,
Float_t bMargin = 0.15, Float_t tMargin = 0.05);
void canvas2()
{
TCanvas *C = (TCanvas*) gROOT->FindObject("C");
if (C) delete C;
C = new TCanvas("C","canvas",1024,640);
C->SetFillStyle(4000);
// Number of PADS
const Int_t Nx = 5;
const Int_t Ny = 5;
// Margins
Float_t lMargin = 0.12;
Float_t rMargin = 0.05;
Float_t bMargin = 0.15;
Float_t tMargin = 0.05;
// Canvas setup
CanvasPartition(C,Nx,Ny,lMargin,rMargin,bMargin,tMargin);
// Dummy histogram.
TH1F *h = (TH1F*) gROOT->FindObject("histo");
if (h) delete h;
h = new TH1F("histo","",100,-5.0,5.0);
h->FillRandom("gaus",10000);
h->GetXaxis()->SetTitle("x axis");
h->GetYaxis()->SetTitle("y axis");
TPad *pad[Nx][Ny];
for (Int_t i=0;i<Nx;i++) {
for (Int_t j=0;j<Ny;j++) {
C->cd(0);
// Get the pads previously created.
char pname[16];
sprintf(pname,"pad_%i_%i",i,j);
pad[i][j] = (TPad*) gROOT->FindObject(pname);
pad[i][j]->Draw();
pad[i][j]->SetFillStyle(4000);
pad[i][j]->SetFrameFillStyle(4000);
pad[i][j]->cd();
// Size factors
Float_t xFactor = pad[0][0]->GetAbsWNDC()/pad[i][j]->GetAbsWNDC();
Float_t yFactor = pad[0][0]->GetAbsHNDC()/pad[i][j]->GetAbsHNDC();
char hname[16];
sprintf(hname,"h_%i_%i",i,j);
TH1F *hFrame = (TH1F*) h->Clone(hname);
hFrame->Reset();
hFrame->Draw();
// y axis range
hFrame->GetYaxis()->SetRangeUser(0.0001,1.2*h->GetMaximum());
// Format for y axis
hFrame->GetYaxis()->SetLabelFont(43);
hFrame->GetYaxis()->SetLabelSize(16);
hFrame->GetYaxis()->SetLabelOffset(0.02);
hFrame->GetYaxis()->SetTitleFont(43);
hFrame->GetYaxis()->SetTitleSize(16);
hFrame->GetYaxis()->SetTitleOffset(2);
hFrame->GetYaxis()->CenterTitle();
hFrame->GetYaxis()->SetNdivisions(505);
// TICKS Y Axis
hFrame->GetYaxis()->SetTickLength(xFactor*0.04/yFactor);
// Format for x axis
hFrame->GetXaxis()->SetLabelFont(43);
hFrame->GetXaxis()->SetLabelSize(16);
hFrame->GetXaxis()->SetLabelOffset(0.02);
hFrame->GetXaxis()->SetTitleFont(43);
hFrame->GetXaxis()->SetTitleSize(16);
hFrame->GetXaxis()->SetTitleOffset(1);
hFrame->GetXaxis()->CenterTitle();
hFrame->GetXaxis()->SetNdivisions(505);
// TICKS X Axis
hFrame->GetXaxis()->SetTickLength(yFactor*0.06/xFactor);
h->Draw("same");
}
}
C->cd();
}
void CanvasPartition(TCanvas *C,const Int_t Nx,const Int_t Ny,
Float_t lMargin, Float_t rMargin,
Float_t bMargin, Float_t tMargin)
{
if (!C) return;
// Setup Pad layout:
Float_t vSpacing = 0.0;
Float_t vStep = (1.- bMargin - tMargin - (Ny-1) * vSpacing) / Ny;
Float_t hSpacing = 0.0;
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;
}
C->cd(0);
char name[16];
sprintf(name,"pad_%i_%i",i,j);
TPad *pad = (TPad*) gROOT->FindObject(name);
if (pad) delete pad;
pad = new TPad(name,"",hposl,vposd,hposr,vposu);
pad->SetLeftMargin(hmarl);
pad->SetRightMargin(hmarr);
pad->SetBottomMargin(vmard);
pad->SetTopMargin(vmaru);
pad->SetBorderMode(0);
pad->SetBorderSize(0);
pad->Draw();
}
}
}
Author
Olivier Couet

Definition in file canvas2.C.

TAttPad::SetFrameBorderMode
void SetFrameBorderMode(Int_t mode=1)
Definition: TAttPad.h:79
TAttPad::SetTopMargin
virtual void SetTopMargin(Float_t topmargin)
Set Pad top margin in fraction of the pad height.
Definition: TAttPad.cxx:129
TAttPad::SetFrameFillStyle
void SetFrameFillStyle(Style_t styl=0)
Definition: TAttPad.h:75
TPad
The most important graphics class in the ROOT system.
Definition: TPad.h:26
TAxis::SetRangeUser
virtual void SetRangeUser(Double_t ufirst, Double_t ulast)
Set the viewing range for the axis from ufirst to ulast (in user coordinates, that is,...
Definition: TAxis.cxx:946
TAttAxis::SetLabelOffset
virtual void SetLabelOffset(Float_t offset=0.005)
Set distance between the axis and the labels.
Definition: TAttAxis.cxx:192
Float_t
float Float_t
Definition: RtypesCore.h:57
TPad::GetAbsWNDC
Double_t GetAbsWNDC() const override
Definition: TPad.h:216
Int_t
int Int_t
Definition: RtypesCore.h:45
TAxis::CenterTitle
void CenterTitle(Bool_t center=kTRUE)
Center axis title.
Definition: TAxis.h:184
TAttAxis::SetTitleSize
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title.
Definition: TAttAxis.cxx:303
ROOT::Math::Cephes::C
static double C[]
Definition: SpecFuncCephes.cxx:187
TPad::SetBorderSize
void SetBorderSize(Short_t bordersize) override
Definition: TPad.h:318
TPad::cd
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition: TPad.cxx:604
TAttAxis::SetLabelSize
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels.
Definition: TAttAxis.cxx:203
TPad::SetFillStyle
void SetFillStyle(Style_t fstyle) override
Override TAttFill::FillStyle for TPad because we want to handle style=0 as style 4000.
Definition: TPad.cxx:5921
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TAttPad::SetBottomMargin
virtual void SetBottomMargin(Float_t bottommargin)
Set Pad bottom margin in fraction of the pad height.
Definition: TAttPad.cxx:99
TH1::GetYaxis
TAxis * GetYaxis()
Definition: TH1.h:321
h
#define h(i)
Definition: RSha256.hxx:106
TAttAxis::SetTitleFont
virtual void SetTitleFont(Style_t font=62)
Set the title font.
Definition: TAttAxis.cxx:321
TPad::SetBorderMode
void SetBorderMode(Short_t bordermode) override
Definition: TPad.h:317
TStyle::SetOptStat
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1589
TAttPad::SetLeftMargin
virtual void SetLeftMargin(Float_t leftmargin)
Set Pad left margin in fraction of the pad width.
Definition: TAttPad.cxx:109
TAttPad::SetRightMargin
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:119
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
TAttAxis::SetTickLength
virtual void SetTickLength(Float_t length=0.03)
Set tick mark length.
Definition: TAttAxis.cxx:279
name
char name[80]
Definition: TGX11.cxx:110
TAttAxis::SetNdivisions
virtual void SetNdivisions(Int_t n=510, Bool_t optim=kTRUE)
Set the number of divisions for this axis.
Definition: TAttAxis.cxx:228
TH1F::Reset
virtual void Reset(Option_t *option="")
Reset.
Definition: TH1.cxx:9941
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
TH1::GetXaxis
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:320
TPad::Draw
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1300
TAttAxis::SetLabelFont
virtual void SetLabelFont(Style_t font=62)
Set labels' font.
Definition: TAttAxis.cxx:182
gROOT
#define gROOT
Definition: TROOT.h:406
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3073
TPad::GetAbsHNDC
Double_t GetAbsHNDC() const override
Definition: TPad.h:217