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/// \preview 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, Float_t lMargin = 0.15, Float_t rMargin = 0.05,
19 Float_t bMargin = 0.15, Float_t tMargin = 0.05);
20double XtoPad(double x);
21double YtoPad(double x);
22
23void canvas2()
24{
26
27 auto C = (TCanvas *)gROOT->FindObject("C");
28 if (C)
29 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
45
46 // Dummy histogram.
47 auto h = (TH1F *)gROOT->FindObject("histo");
48 if (h)
49 delete h;
50 h = new TH1F("histo", "", 100, -5.0, 5.0);
51 h->FillRandom("gaus", 10000);
52 h->GetXaxis()->SetTitle("x axis");
53 h->GetYaxis()->SetTitle("y axis");
54
55 TPad *pad[Nx][Ny];
56
57 for (Int_t i = 0; i < Nx; i++) {
58 for (Int_t j = 0; j < Ny; j++) {
59 C->cd(0);
60
61 // Get the pads previously created.
62 pad[i][j] = (TPad *)C->FindObject(TString::Format("pad_%d_%d", i, j).Data());
63 pad[i][j]->Draw();
64 pad[i][j]->SetFillStyle(4000);
65 pad[i][j]->SetFrameFillStyle(4000);
66 pad[i][j]->cd();
67
68 // Size factors
69 Float_t xFactor = pad[0][0]->GetAbsWNDC() / pad[i][j]->GetAbsWNDC();
70 Float_t yFactor = pad[0][0]->GetAbsHNDC() / pad[i][j]->GetAbsHNDC();
71
72 TH1F *hFrame = (TH1F *)h->Clone(TString::Format("h_%d_%d", i, j).Data());
73
74 // y axis range
75 hFrame->SetMinimum(0.0001); // do not show 0
76 hFrame->SetMaximum(1.2 * h->GetMaximum());
77
78 // Format for y axis
79 hFrame->GetYaxis()->SetLabelFont(43);
80 hFrame->GetYaxis()->SetLabelSize(16);
81 hFrame->GetYaxis()->SetLabelOffset(0.02);
82 hFrame->GetYaxis()->SetTitleFont(43);
83 hFrame->GetYaxis()->SetTitleSize(16);
84 hFrame->GetYaxis()->SetTitleOffset(2);
85
86 hFrame->GetYaxis()->CenterTitle();
87 hFrame->GetYaxis()->SetNdivisions(505);
88
89 // TICKS Y Axis
90 hFrame->GetYaxis()->SetTickLength(xFactor * 0.04 / yFactor);
91
92 // Format for x axis
93 hFrame->GetXaxis()->SetLabelFont(43);
94 hFrame->GetXaxis()->SetLabelSize(16);
95 hFrame->GetXaxis()->SetLabelOffset(0.02);
96 hFrame->GetXaxis()->SetTitleFont(43);
97 hFrame->GetXaxis()->SetTitleSize(16);
98 hFrame->GetXaxis()->SetTitleOffset(1);
99 hFrame->GetXaxis()->CenterTitle();
100 hFrame->GetXaxis()->SetNdivisions(505);
101
102 // TICKS X Axis
103 hFrame->GetXaxis()->SetTickLength(yFactor * 0.06 / xFactor);
104
105 // Draw cloned histogram with individual settings
106 hFrame->Draw();
107
108 TText text;
109 text.SetTextAlign(31);
110 text.SetTextFont(43);
111 text.SetTextSize(10);
112 text.DrawTextNDC(XtoPad(0.9), YtoPad(0.8), gPad->GetName());
113 }
114 }
115 C->cd();
116}
117
120{
121 if (!C)
122 return;
123
124 // Setup Pad layout:
125 Float_t vSpacing = 0.0;
126 Float_t vStep = (1. - bMargin - tMargin - (Ny - 1) * vSpacing) / Ny;
127
128 Float_t hSpacing = 0.0;
129 Float_t hStep = (1. - lMargin - rMargin - (Nx - 1) * hSpacing) / Nx;
130
133
134 for (Int_t i = 0; i < Nx; i++) {
135
136 if (i == 0) {
137 hposl = 0.0;
138 hposr = lMargin + hStep;
139 hfactor = hposr - hposl;
141 hmarr = 0.0;
142 } else if (i == Nx - 1) {
143 hposl = hposr + hSpacing;
144 hposr = hposl + hStep + rMargin;
145 hfactor = hposr - hposl;
146 hmarl = 0.0;
147 hmarr = rMargin / (hposr - hposl);
148 } else {
149 hposl = hposr + hSpacing;
150 hposr = hposl + hStep;
151 hfactor = hposr - hposl;
152 hmarl = 0.0;
153 hmarr = 0.0;
154 }
155
156 for (Int_t j = 0; j < Ny; j++) {
157
158 if (j == 0) {
159 vposd = 0.0;
160 vposu = bMargin + vStep;
161 vfactor = vposu - vposd;
163 vmaru = 0.0;
164 } else if (j == Ny - 1) {
165 vposd = vposu + vSpacing;
166 vposu = vposd + vStep + tMargin;
167 vfactor = vposu - vposd;
168 vmard = 0.0;
169 vmaru = tMargin / (vposu - vposd);
170 } else {
171 vposd = vposu + vSpacing;
172 vposu = vposd + vStep;
173 vfactor = vposu - vposd;
174 vmard = 0.0;
175 vmaru = 0.0;
176 }
177
178 C->cd(0);
179
180 auto name = TString::Format("pad_%d_%d", i, j);
181 auto pad = (TPad *)C->FindObject(name.Data());
182 if (pad)
183 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
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:442
#define gPad
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:647
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:1642
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