Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
canvas2.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphics
3/// \notebook
4/// Example of canvas partitioning.
5/// Sometimes the Divide() method is not appropriate to divide a Canvas.
6/// Because of the left and right margins, all the pads do not have the
7/// same width and height. CanvasPartition does that properly. This
8/// example also ensure that the axis labels and titles have the same
9/// sizes and that the tick marks length is uniform.
10/// In addition, XtoPad and YtoPad allow to place graphics objects like
11/// text in the right place in each sub-pads.
12///
13/// \macro_image
14/// \macro_code
15///
16/// \author Olivier Couet
17
18void CanvasPartition(TCanvas *C,const Int_t Nx = 2,const Int_t Ny = 2,
19 Float_t lMargin = 0.15, Float_t rMargin = 0.05,
20 Float_t bMargin = 0.15, Float_t tMargin = 0.05);
21double XtoPad(double x);
22double YtoPad(double x);
23
24void canvas2()
25{
27
28 auto C = (TCanvas*) gROOT->FindObject("C");
29 if (C) delete C;
30 C = new TCanvas("C","canvas",1024,640);
31 C->SetFillStyle(4000);
32
33 // Number of PADS
34 const Int_t Nx = 5;
35 const Int_t Ny = 5;
36
37 // Margins
38 Float_t lMargin = 0.12;
39 Float_t rMargin = 0.05;
40 Float_t bMargin = 0.15;
41 Float_t tMargin = 0.05;
42
43 // Canvas setup
44 CanvasPartition(C,Nx,Ny,lMargin,rMargin,bMargin,tMargin);
45
46 // Dummy histogram.
47 auto h = (TH1F*) gROOT->FindObject("histo");
48 if (h) delete h;
49 h = new TH1F("histo","",100,-5.0,5.0);
50 h->FillRandom("gaus",10000);
51 h->GetXaxis()->SetTitle("x axis");
52 h->GetYaxis()->SetTitle("y axis");
53
54 TPad *pad[Nx][Ny];
55
56 for (Int_t i = 0; i < Nx; i++) {
57 for (Int_t j = 0; j < Ny; j++) {
58 C->cd(0);
59
60 // Get the pads previously created.
61 pad[i][j] = (TPad*) C->FindObject(TString::Format("pad_%d_%d",i,j).Data());
62 pad[i][j]->Draw();
63 pad[i][j]->SetFillStyle(4000);
64 pad[i][j]->SetFrameFillStyle(4000);
65 pad[i][j]->cd();
66
67 // Size factors
68 Float_t xFactor = pad[0][0]->GetAbsWNDC()/pad[i][j]->GetAbsWNDC();
69 Float_t yFactor = pad[0][0]->GetAbsHNDC()/pad[i][j]->GetAbsHNDC();
70
71 TH1F *hFrame = (TH1F*) h->Clone(TString::Format("h_%d_%d",i,j).Data());
72
73 // y axis range
74 hFrame->SetMinimum(0.0001); // do not show 0
75 hFrame->SetMaximum(1.2*h->GetMaximum());
76
77 // Format for y axis
78 hFrame->GetYaxis()->SetLabelFont(43);
79 hFrame->GetYaxis()->SetLabelSize(16);
80 hFrame->GetYaxis()->SetLabelOffset(0.02);
81 hFrame->GetYaxis()->SetTitleFont(43);
82 hFrame->GetYaxis()->SetTitleSize(16);
83 hFrame->GetYaxis()->SetTitleOffset(2);
84
85 hFrame->GetYaxis()->CenterTitle();
86 hFrame->GetYaxis()->SetNdivisions(505);
87
88 // TICKS Y Axis
89 hFrame->GetYaxis()->SetTickLength(xFactor*0.04/yFactor);
90
91 // Format for x axis
92 hFrame->GetXaxis()->SetLabelFont(43);
93 hFrame->GetXaxis()->SetLabelSize(16);
94 hFrame->GetXaxis()->SetLabelOffset(0.02);
95 hFrame->GetXaxis()->SetTitleFont(43);
96 hFrame->GetXaxis()->SetTitleSize(16);
97 hFrame->GetXaxis()->SetTitleOffset(1);
98 hFrame->GetXaxis()->CenterTitle();
99 hFrame->GetXaxis()->SetNdivisions(505);
100
101 // TICKS X Axis
102 hFrame->GetXaxis()->SetTickLength(yFactor*0.06/xFactor);
103
104 // Draw cloned histogram with individual settings
105 hFrame->Draw();
106
107 TText text;
108 text.SetTextAlign(31);
109 text.SetTextFont(43);
110 text.SetTextSize(10);
111 text.DrawTextNDC(XtoPad(0.9), YtoPad(0.8), gPad->GetName());
112 }
113 }
114 C->cd();
115}
116
117
118
119void CanvasPartition(TCanvas *C,const Int_t Nx,const Int_t Ny,
120 Float_t lMargin, Float_t rMargin,
121 Float_t bMargin, Float_t tMargin)
122{
123 if (!C) return;
124
125 // Setup Pad layout:
126 Float_t vSpacing = 0.0;
127 Float_t vStep = (1.- bMargin - tMargin - (Ny-1) * vSpacing) / Ny;
128
129 Float_t hSpacing = 0.0;
130 Float_t hStep = (1.- lMargin - rMargin - (Nx-1) * hSpacing) / Nx;
131
132 Float_t vposd,vposu,vmard,vmaru,vfactor;
133 Float_t hposl,hposr,hmarl,hmarr,hfactor;
134
135 for (Int_t i=0;i<Nx;i++) {
136
137 if (i==0) {
138 hposl = 0.0;
139 hposr = lMargin + hStep;
140 hfactor = hposr-hposl;
141 hmarl = lMargin / hfactor;
142 hmarr = 0.0;
143 } else if (i == Nx-1) {
144 hposl = hposr + hSpacing;
145 hposr = hposl + hStep + rMargin;
146 hfactor = hposr-hposl;
147 hmarl = 0.0;
148 hmarr = rMargin / (hposr-hposl);
149 } else {
150 hposl = hposr + hSpacing;
151 hposr = hposl + hStep;
152 hfactor = hposr-hposl;
153 hmarl = 0.0;
154 hmarr = 0.0;
155 }
156
157 for (Int_t j=0;j<Ny;j++) {
158
159 if (j==0) {
160 vposd = 0.0;
161 vposu = bMargin + vStep;
162 vfactor = vposu-vposd;
163 vmard = bMargin / vfactor;
164 vmaru = 0.0;
165 } else if (j == Ny-1) {
166 vposd = vposu + vSpacing;
167 vposu = vposd + vStep + tMargin;
168 vfactor = vposu-vposd;
169 vmard = 0.0;
170 vmaru = tMargin / (vposu-vposd);
171 } else {
172 vposd = vposu + vSpacing;
173 vposu = vposd + vStep;
174 vfactor = vposu-vposd;
175 vmard = 0.0;
176 vmaru = 0.0;
177 }
178
179 C->cd(0);
180
181 auto name = TString::Format("pad_%d_%d",i,j);
182 auto pad = (TPad*) C->FindObject(name.Data());
183 if (pad) delete pad;
184 pad = new TPad(name.Data(),"",hposl,vposd,hposr,vposu);
185 pad->SetLeftMargin(hmarl);
186 pad->SetRightMargin(hmarr);
187 pad->SetBottomMargin(vmard);
188 pad->SetTopMargin(vmaru);
189
190 pad->SetFrameBorderMode(0);
191 pad->SetBorderMode(0);
192 pad->SetBorderSize(0);
193
194 pad->Draw();
195 }
196 }
197}
198
199double XtoPad(double x)
200{
201 double xl,yl,xu,yu;
202 gPad->GetPadPar(xl,yl,xu,yu);
203 double pw = xu-xl;
204 double lm = gPad->GetLeftMargin();
205 double rm = gPad->GetRightMargin();
206 double fw = pw-pw*lm-pw*rm;
207 return (x*fw+pw*lm)/pw;
208}
209
210double YtoPad(double y)
211{
212 double xl,yl,xu,yu;
213 gPad->GetPadPar(xl,yl,xu,yu);
214 double ph = yu-yl;
215 double tm = gPad->GetTopMargin();
216 double bm = gPad->GetBottomMargin();
217 double fh = ph-ph*bm-ph*tm;
218 return (y*fh+bm*ph)/ph;
219}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
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:430
#define gPad
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels.
Definition TAttAxis.cxx:203
virtual void SetTitleFont(Style_t font=62)
Set the title font.
Definition TAttAxis.cxx:327
virtual void SetLabelOffset(Float_t offset=0.005)
Set distance between the axis and the labels.
Definition TAttAxis.cxx:191
virtual void SetLabelFont(Style_t font=62)
Set labels' font.
Definition TAttAxis.cxx:180
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title.
Definition TAttAxis.cxx:309
virtual void SetTickLength(Float_t length=0.03)
Set tick mark length.
Definition TAttAxis.cxx:284
virtual void SetNdivisions(Int_t n=510, Bool_t optim=kTRUE)
Set the number of divisions for this axis.
Definition TAttAxis.cxx:233
virtual void SetBottomMargin(Float_t bottommargin)
Set Pad bottom margin in fraction of the pad height.
Definition TAttPad.cxx:99
virtual void SetLeftMargin(Float_t leftmargin)
Set Pad left margin in fraction of the pad width.
Definition TAttPad.cxx:109
void SetFrameBorderMode(Int_t mode=1)
Definition TAttPad.h:79
void SetFrameFillStyle(Style_t styl=0)
Definition TAttPad.h:75
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition TAttPad.cxx:119
virtual void SetTopMargin(Float_t topmargin)
Set Pad top margin in fraction of the pad height.
Definition TAttPad.cxx:129
void CenterTitle(Bool_t center=kTRUE)
Center axis title.
Definition TAxis.h:185
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:577
TAxis * GetXaxis()
Definition TH1.h:322
virtual void SetMaximum(Double_t maximum=-1111)
Definition TH1.h:400
TAxis * GetYaxis()
Definition TH1.h:323
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3060
virtual void SetMinimum(Double_t minimum=-1111)
Definition TH1.h:401
The most important graphics class in the ROOT system.
Definition TPad.h:28
void SetBorderSize(Short_t bordersize) override
Definition TPad.h:321
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
Double_t GetAbsWNDC() const override
Definition TPad.h:220
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition TPad.cxx:597
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition TPad.cxx:1268
void SetBorderMode(Short_t bordermode) override
Definition TPad.h:320
Double_t GetAbsHNDC() const override
Definition TPad.h:221
const char * Data() const
Definition TString.h:380
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:2357
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:1630
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