Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
transpad.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// \notebook
4 /// Example of a canvas showing two histograms with different scales.
5 /// The second histogram is drawn in a transparent pad
6 ///
7 /// \macro_image
8 /// \macro_code
9 ///
10 /// \author Rene Brun
11 
12 void transpad() {
13  TCanvas *c1 = new TCanvas("c1","transparent pad",200,10,700,500);
14  TPad *pad1 = new TPad("pad1","",0,0,1,1);
15  TPad *pad2 = new TPad("pad2","",0,0,1,1);
16  pad2->SetFillStyle(4000); //will be transparent
17  pad1->Draw();
18  pad1->cd();
19 
20  TH1F *h1 = new TH1F("h1","h1",100,-3,3);
21  TH1F *h2 = new TH1F("h2","h2",100,-3,3);
22  TRandom r;
23  for (Int_t i=0;i<100000;i++) {
24  Double_t x1 = r.Gaus(-1,0.5);
25  Double_t x2 = r.Gaus(1,1.5);
26  if (i <1000) h1->Fill(x1);
27  h2->Fill(x2);
28  }
29  h1->Draw();
30  pad1->Update(); //this will force the generation of the "stats" box
31  TPaveStats *ps1 = (TPaveStats*)h1->GetListOfFunctions()->FindObject("stats");
32  ps1->SetX1NDC(0.4); ps1->SetX2NDC(0.6);
33  pad1->Modified();
34  c1->cd();
35 
36  //compute the pad range with suitable margins
37  Double_t ymin = 0;
38  Double_t ymax = 2000;
39  Double_t dy = (ymax-ymin)/0.8; //10 per cent margins top and bottom
40  Double_t xmin = -3;
41  Double_t xmax = 3;
42  Double_t dx = (xmax-xmin)/0.8; //10 per cent margins left and right
43  pad2->Range(xmin-0.1*dx,ymin-0.1*dy,xmax+0.1*dx,ymax+0.1*dy);
44  pad2->Draw();
45  pad2->cd();
46  h2->SetLineColor(kRed);
47  h2->Draw("][sames");
48  pad2->Update();
49  TPaveStats *ps2 = (TPaveStats*)h2->GetListOfFunctions()->FindObject("stats");
50  ps2->SetX1NDC(0.65); ps2->SetX2NDC(0.85);
51  ps2->SetTextColor(kRed);
52 
53  // draw axis on the right side of the pad
54  TGaxis *axis = new TGaxis(xmax,ymin,xmax,ymax,ymin,ymax,50510,"+L");
55  axis->SetLabelColor(kRed);
56  axis->Draw();
57 }
58 
59