Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
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. In addition, XtoPad and YtoPad allow to place graphics objects like text in the right place in each sub-pads.

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);
double XtoPad(double x);
double YtoPad(double x);
void canvas2()
{
auto 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
// Dummy histogram.
auto 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.
pad[i][j] = (TPad*) C->FindObject(TString::Format("pad_%d_%d",i,j).Data());
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();
TH1F *hFrame = (TH1F*) h->Clone(TString::Format("h_%d_%d",i,j).Data());
// y axis range
hFrame->SetMinimum(0.0001); // do not show 0
hFrame->SetMaximum(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);
// Draw cloned histogram with individual settings
hFrame->Draw();
text.SetTextAlign(31);
text.SetTextFont(43);
text.SetTextSize(10);
text.DrawTextNDC(XtoPad(0.9), YtoPad(0.8), gPad->GetName());
}
}
C->cd();
}
void CanvasPartition(TCanvas *C,const Int_t Nx,const Int_t Ny,
{
if (!C) return;
// Setup Pad layout:
Float_t vStep = (1.- bMargin - tMargin - (Ny-1) * vSpacing) / Ny;
Float_t hStep = (1.- lMargin - rMargin - (Nx-1) * hSpacing) / Nx;
for (Int_t i=0;i<Nx;i++) {
if (i==0) {
hposl = 0.0;
hmarr = 0.0;
} else if (i == Nx-1) {
hmarl = 0.0;
} else {
hmarl = 0.0;
hmarr = 0.0;
}
for (Int_t j=0;j<Ny;j++) {
if (j==0) {
vposd = 0.0;
vmaru = 0.0;
} else if (j == Ny-1) {
vmard = 0.0;
} else {
vmard = 0.0;
vmaru = 0.0;
}
C->cd(0);
auto name = TString::Format("pad_%d_%d",i,j);
auto pad = (TPad*) C->FindObject(name.Data());
if (pad) delete pad;
pad = new TPad(name.Data(),"",hposl,vposd,hposr,vposu);
pad->SetLeftMargin(hmarl);
pad->SetRightMargin(hmarr);
pad->SetBottomMargin(vmard);
pad->SetTopMargin(vmaru);
pad->SetFrameBorderMode(0);
pad->SetBorderMode(0);
pad->SetBorderSize(0);
pad->Draw();
}
}
}
double XtoPad(double x)
{
double xl,yl,xu,yu;
gPad->GetPadPar(xl,yl,xu,yu);
double pw = xu-xl;
double lm = gPad->GetLeftMargin();
double rm = gPad->GetRightMargin();
double fw = pw-pw*lm-pw*rm;
return (x*fw+pw*lm)/pw;
}
double YtoPad(double y)
{
double xl,yl,xu,yu;
gPad->GetPadPar(xl,yl,xu,yu);
double ph = yu-yl;
double tm = gPad->GetTopMargin();
double bm = gPad->GetBottomMargin();
double fh = ph-ph*bm-ph*tm;
return (y*fh+bm*ph)/ph;
}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char text
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:406
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
#define gPad
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:622
The most important graphics class in the ROOT system.
Definition TPad.h:28
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2378
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:1640
Base class for several text objects.
Definition TText.h:22
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
constexpr Double_t C()
Velocity of light in .
Definition TMath.h:114
Author
Olivier Couet

Definition in file canvas2.C.