Logo ROOT   6.14/05
Reference Guide
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 
16 void 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 
20 void canvas2()
21 {
22 
23  gStyle->SetOptStat(0);
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(5);
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(5);
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 
114 void 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 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:294
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition: TH1.cxx:7844
float Float_t
Definition: RtypesCore.h:53
void SetFrameBorderMode(Int_t mode=1)
Definition: TAttPad.h:79
R__EXTERN TStyle * gStyle
Definition: TStyle.h:406
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:285
virtual void SetBorderMode(Short_t bordermode)
Definition: TPad.h:317
virtual void SetNdivisions(Int_t n=510, Bool_t optim=kTRUE)
Set the number of divisions for this axis.
Definition: TAttAxis.cxx:229
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
#define gROOT
Definition: TROOT.h:410
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
virtual void SetTitleFont(Style_t font=62)
Set the title font.
Definition: TAttAxis.cxx:322
int Int_t
Definition: RtypesCore.h:41
void CenterTitle(Bool_t center=kTRUE)
Center axis title.
Definition: TAxis.h:184
virtual void SetLabelOffset(Float_t offset=0.005)
Set distance between the axis and the labels The distance is expressed in per cent of the pad width...
Definition: TAttAxis.cxx:193
virtual void SetTopMargin(Float_t topmargin)
Set Pad top margin in fraction of the pad height.
Definition: TAttPad.cxx:130
virtual void Reset(Option_t *option="")
Reset.
Definition: TH1.cxx:9398
void SetFrameFillStyle(Style_t styl=0)
Definition: TAttPad.h:75
virtual void SetLabelFont(Style_t font=62)
Set labels&#39; font.
Definition: TAttAxis.cxx:183
virtual void SetRangeUser(Double_t ufirst, Double_t ulast)
Set the viewing range for the axis from ufirst to ulast (in user coordinates).
Definition: TAxis.cxx:928
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:594
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1281
static double C[]
virtual void SetBottomMargin(Float_t bottommargin)
Set Pad bottom margin in fraction of the pad height.
Definition: TAttPad.cxx:100
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3421
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2974
Double_t GetAbsWNDC() const
Definition: TPad.h:217
The most important graphics class in the ROOT system.
Definition: TPad.h:29
TAxis * GetYaxis()
Definition: TH1.h:316
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels The size is expressed in per cent of the pad width.
Definition: TAttAxis.cxx:204
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title The size is expressed in per cent of the pad width.
Definition: TAttAxis.cxx:304
#define h(i)
Definition: RSha256.hxx:106
The Canvas class.
Definition: TCanvas.h:31
virtual void SetFillStyle(Style_t fstyle)
Override TAttFill::FillStyle for TPad because we want to handle style=0 as style 4000.
Definition: TPad.cxx:5790
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:120
Double_t GetAbsHNDC() const
Definition: TPad.h:218
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition: TH1.cxx:2657
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:1444
virtual void SetTickLength(Float_t length=0.03)
Set tick mark length The length is expressed in per cent of the pad width.
Definition: TAttAxis.cxx:280
virtual void SetBorderSize(Short_t bordersize)
Definition: TPad.h:318
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
char name[80]
Definition: TGX11.cxx:109
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:315
virtual void SetLeftMargin(Float_t leftmargin)
Set Pad left margin in fraction of the pad width.
Definition: TAttPad.cxx:110