twoscales.C

Go to the documentation of this file.
00001 //example of macro illustrating how to superimpose two histograms
00002 #include "TCanvas.h"
00003 #include "TStyle.h"
00004 #include "TH1.h"
00005 #include "TGaxis.h"
00006 #include "TRandom.h"
00007    
00008 void twoscales()
00009 {
00010    //example of macro illustrating how to superimpose two histograms
00011    //with different scales in the "same" pad.
00012    // To see the output of this macro, click begin_html <a href="gif/twoscales.gif" >here</a> end_html
00013    //Author: Rene Brun
00014      
00015    TCanvas *c1 = new TCanvas("c1","hists with different scales",600,400);
00016 
00017    //create/fill draw h1
00018    gStyle->SetOptStat(kFALSE);
00019    TH1F *h1 = new TH1F("h1","my histogram",100,-3,3);
00020    Int_t i;
00021    for (i=0;i<10000;i++) h1->Fill(gRandom->Gaus(0,1));
00022    h1->Draw();
00023    c1->Update();  
00024     
00025    //create hint1 filled with the bins integral of h1
00026    TH1F *hint1 = new TH1F("hint1","h1 bins integral",100,-3,3);
00027    Float_t sum = 0;
00028    for (i=1;i<=100;i++) {
00029       sum += h1->GetBinContent(i); 
00030       hint1->SetBinContent(i,sum);
00031    }
00032 
00033    //scale hint1 to the pad coordinates
00034    Float_t rightmax = 1.1*hint1->GetMaximum();
00035    Float_t scale = gPad->GetUymax()/rightmax;
00036    hint1->SetLineColor(kRed);
00037    hint1->Scale(scale);
00038    hint1->Draw("same");
00039    
00040    //draw an axis on the right side
00041    TGaxis *axis = new TGaxis(gPad->GetUxmax(),gPad->GetUymin(),
00042          gPad->GetUxmax(), gPad->GetUymax(),0,rightmax,510,"+L");
00043    axis->SetLineColor(kRed);
00044    axis->SetLabelColor(kRed);
00045    axis->Draw();
00046 }

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