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///
11/// \macro_image
12/// \macro_code
13///
14/// \author Olivier Couet
15
16void CanvasPartition(TCanvas *C,const Int_t Nx = 2,const Int_t Ny = 2,
17 Float_t lMargin = 0.15, Float_t rMargin = 0.05,
18 Float_t bMargin = 0.15, Float_t tMargin = 0.05);
19
20void canvas2()
21{
22
24
25 TCanvas *C = (TCanvas*) gROOT->FindObject("C");
26 if (C) delete C;
27 C = new TCanvas("C","canvas",1024,640);
28 C->SetFillStyle(4000);
29
30 // Number of PADS
31 const Int_t Nx = 5;
32 const Int_t Ny = 5;
33
34 // Margins
35 Float_t lMargin = 0.12;
36 Float_t rMargin = 0.05;
37 Float_t bMargin = 0.15;
38 Float_t tMargin = 0.05;
39
40 // Canvas setup
41 CanvasPartition(C,Nx,Ny,lMargin,rMargin,bMargin,tMargin);
42
43 // Dummy histogram.
44 TH1F *h = (TH1F*) gROOT->FindObject("histo");
45 if (h) delete h;
46 h = new TH1F("histo","",100,-5.0,5.0);
47 h->FillRandom("gaus",10000);
48 h->GetXaxis()->SetTitle("x axis");
49 h->GetYaxis()->SetTitle("y axis");
50
51 TPad *pad[Nx][Ny];
52
53 for (Int_t i=0;i<Nx;i++) {
54 for (Int_t j=0;j<Ny;j++) {
55 C->cd(0);
56
57 // Get the pads previously created.
58 char pname[16];
59 sprintf(pname,"pad_%i_%i",i,j);
60 pad[i][j] = (TPad*) gROOT->FindObject(pname);
61 pad[i][j]->Draw();
62 pad[i][j]->SetFillStyle(4000);
63 pad[i][j]->SetFrameFillStyle(4000);
64 pad[i][j]->cd();
65
66 // Size factors
67 Float_t xFactor = pad[0][0]->GetAbsWNDC()/pad[i][j]->GetAbsWNDC();
68 Float_t yFactor = pad[0][0]->GetAbsHNDC()/pad[i][j]->GetAbsHNDC();
69
70 char hname[16];
71 sprintf(hname,"h_%i_%i",i,j);
72 TH1F *hFrame = (TH1F*) h->Clone(hname);
73 hFrame->Reset();
74 hFrame->Draw();
75
76 // y axis range
77 hFrame->GetYaxis()->SetRangeUser(0.0001,1.2*h->GetMaximum());
78
79 // Format for y axis
80 hFrame->GetYaxis()->SetLabelFont(43);
81 hFrame->GetYaxis()->SetLabelSize(16);
82 hFrame->GetYaxis()->SetLabelOffset(0.02);
83 hFrame->GetYaxis()->SetTitleFont(43);
84 hFrame->GetYaxis()->SetTitleSize(16);
85 hFrame->GetYaxis()->SetTitleOffset(2);
86
87 hFrame->GetYaxis()->CenterTitle();
88 hFrame->GetYaxis()->SetNdivisions(505);
89
90 // TICKS Y Axis
91 hFrame->GetYaxis()->SetTickLength(xFactor*0.04/yFactor);
92
93 // Format for x axis
94 hFrame->GetXaxis()->SetLabelFont(43);
95 hFrame->GetXaxis()->SetLabelSize(16);
96 hFrame->GetXaxis()->SetLabelOffset(0.02);
97 hFrame->GetXaxis()->SetTitleFont(43);
98 hFrame->GetXaxis()->SetTitleSize(16);
99 hFrame->GetXaxis()->SetTitleOffset(1);
100 hFrame->GetXaxis()->CenterTitle();
101 hFrame->GetXaxis()->SetNdivisions(505);
102
103 // TICKS X Axis
104 hFrame->GetXaxis()->SetTickLength(yFactor*0.06/xFactor);
105
106 h->Draw("same");
107 }
108 }
109 C->cd();
110}
111
112
113
114void CanvasPartition(TCanvas *C,const Int_t Nx,const Int_t Ny,
115 Float_t lMargin, Float_t rMargin,
116 Float_t bMargin, Float_t tMargin)
117{
118 if (!C) return;
119
120 // Setup Pad layout:
121 Float_t vSpacing = 0.0;
122 Float_t vStep = (1.- bMargin - tMargin - (Ny-1) * vSpacing) / Ny;
123
124 Float_t hSpacing = 0.0;
125 Float_t hStep = (1.- lMargin - rMargin - (Nx-1) * hSpacing) / Nx;
126
127 Float_t vposd,vposu,vmard,vmaru,vfactor;
128 Float_t hposl,hposr,hmarl,hmarr,hfactor;
129
130 for (Int_t i=0;i<Nx;i++) {
131
132 if (i==0) {
133 hposl = 0.0;
134 hposr = lMargin + hStep;
135 hfactor = hposr-hposl;
136 hmarl = lMargin / hfactor;
137 hmarr = 0.0;
138 } else if (i == Nx-1) {
139 hposl = hposr + hSpacing;
140 hposr = hposl + hStep + rMargin;
141 hfactor = hposr-hposl;
142 hmarl = 0.0;
143 hmarr = rMargin / (hposr-hposl);
144 } else {
145 hposl = hposr + hSpacing;
146 hposr = hposl + hStep;
147 hfactor = hposr-hposl;
148 hmarl = 0.0;
149 hmarr = 0.0;
150 }
151
152 for (Int_t j=0;j<Ny;j++) {
153
154 if (j==0) {
155 vposd = 0.0;
156 vposu = bMargin + vStep;
157 vfactor = vposu-vposd;
158 vmard = bMargin / vfactor;
159 vmaru = 0.0;
160 } else if (j == Ny-1) {
161 vposd = vposu + vSpacing;
162 vposu = vposd + vStep + tMargin;
163 vfactor = vposu-vposd;
164 vmard = 0.0;
165 vmaru = tMargin / (vposu-vposd);
166 } else {
167 vposd = vposu + vSpacing;
168 vposu = vposd + vStep;
169 vfactor = vposu-vposd;
170 vmard = 0.0;
171 vmaru = 0.0;
172 }
173
174 C->cd(0);
175
176 char name[16];
177 sprintf(name,"pad_%i_%i",i,j);
178 TPad *pad = (TPad*) gROOT->FindObject(name);
179 if (pad) delete pad;
180 pad = new TPad(name,"",hposl,vposd,hposr,vposu);
181 pad->SetLeftMargin(hmarl);
182 pad->SetRightMargin(hmarr);
183 pad->SetBottomMargin(vmard);
184 pad->SetTopMargin(vmaru);
185
186 pad->SetFrameBorderMode(0);
187 pad->SetBorderMode(0);
188 pad->SetBorderSize(0);
189
190 pad->Draw();
191 }
192 }
193}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:404
R__EXTERN TStyle * gStyle
Definition TStyle.h:413
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:302
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels.
Definition TAttAxis.cxx:205
virtual void SetTitleFont(Style_t font=62)
Set the title font.
Definition TAttAxis.cxx:330
virtual void SetLabelOffset(Float_t offset=0.005)
Set distance between the axis and the labels.
Definition TAttAxis.cxx:194
virtual void SetLabelFont(Style_t font=62)
Set labels' font.
Definition TAttAxis.cxx:183
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title.
Definition TAttAxis.cxx:312
virtual void SetTickLength(Float_t length=0.03)
Set tick mark length.
Definition TAttAxis.cxx:288
virtual void SetNdivisions(Int_t n=510, Bool_t optim=kTRUE)
Set the number of divisions for this axis.
Definition TAttAxis.cxx:237
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:184
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
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
virtual void Reset(Option_t *option="")
Reset.
Definition TH1.cxx:9941
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition TH1.h:320
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition TH1.cxx:2741
virtual TObject * FindObject(const char *name) const
Search object named name in the list of functions.
Definition TH1.cxx:3866
TAxis * GetYaxis()
Definition TH1.h:321
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3074
The most important graphics class in the ROOT system.
Definition TPad.h:26
void SetBorderSize(Short_t bordersize) override
Definition TPad.h:317
void SetFillStyle(Style_t fstyle) override
Override TAttFill::FillStyle for TPad because we want to handle style=0 as style 4000.
Definition TPad.cxx:5941
Double_t GetAbsWNDC() const override
Definition TPad.h:216
TObject * FindObject(const char *name) const override
Search if object named name is inside this pad or in pads inside this pad.
Definition TPad.cxx:2628
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition TPad.cxx:604
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition TPad.cxx:1300
void SetBorderMode(Short_t bordermode) override
Definition TPad.h:316
Double_t GetAbsHNDC() const override
Definition TPad.h:217
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
constexpr Double_t C()
Velocity of light in .
Definition TMath.h:117