transpad.C

Go to the documentation of this file.
00001 // Example of a canvas showing two histograms with different scales.
00002 // The second histogram is drawn in a transparent pad
00003 //Author: Rene Brun
00004    
00005 void transpad() {
00006    TCanvas *c1 = new TCanvas("c1","transparent pad",200,10,700,500);
00007    TPad *pad1 = new TPad("pad1","",0,0,1,1);
00008    TPad *pad2 = new TPad("pad2","",0,0,1,1);
00009    pad2->SetFillStyle(4000); //will be transparent
00010    pad1->Draw();
00011    pad1->cd();
00012 
00013    TH1F *h1 = new TH1F("h1","h1",100,-3,3);
00014    TH1F *h2 = new TH1F("h2","h2",100,-3,3);
00015    TRandom r;
00016    for (Int_t i=0;i<100000;i++) {
00017       Double_t x1 = r.Gaus(-1,0.5);
00018       Double_t x2 = r.Gaus(1,1.5);
00019       if (i <1000) h1->Fill(x1);
00020       h2->Fill(x2);
00021    }
00022    h1->Draw();
00023    pad1->Update(); //this will force the generation of the "stats" box
00024    TPaveStats *ps1 = (TPaveStats*)h1->GetListOfFunctions()->FindObject("stats");
00025    ps1->SetX1NDC(0.4); ps1->SetX2NDC(0.6);
00026    pad1->Modified();
00027    c1->cd();
00028    
00029    //compute the pad range with suitable margins
00030    Double_t ymin = 0;
00031    Double_t ymax = 2000;
00032    Double_t dy = (ymax-ymin)/0.8; //10 per cent margins top and bottom
00033    Double_t xmin = -3;
00034    Double_t xmax = 3;
00035    Double_t dx = (xmax-xmin)/0.8; //10 per cent margins left and right
00036    pad2->Range(xmin-0.1*dx,ymin-0.1*dy,xmax+0.1*dx,ymax+0.1*dy);
00037    pad2->Draw();
00038    pad2->cd();
00039    h2->SetLineColor(kRed);
00040    h2->Draw("][sames");
00041    pad2->Update();
00042    TPaveStats *ps2 = (TPaveStats*)h2->GetListOfFunctions()->FindObject("stats");
00043    ps2->SetX1NDC(0.65); ps2->SetX2NDC(0.85);
00044    ps2->SetTextColor(kRed);
00045    
00046    // draw axis on the right side of the pad
00047    TGaxis *axis = new TGaxis(xmax,ymin,xmax,ymax,ymin,ymax,50510,"+L");
00048    axis->SetLabelColor(kRed);
00049    axis->Draw();
00050 }
00051    
00052       

Generated on Tue Jul 5 15:44:30 2011 for ROOT_528-00b_version by  doxygen 1.5.1