ROOT logo

From $ROOTSYS/tutorials/hist/twoscales.C

//example of macro illustrating how to superimpose two histograms
#include "TCanvas.h"
#include "TStyle.h"
#include "TH1.h"
#include "TGaxis.h"
#include "TRandom.h"
   
void twoscales()
{
   //example of macro illustrating how to superimpose two histograms
   //with different scales in the "same" pad.
   // To see the output of this macro, click begin_html <a href="gif/twoscales.gif" >here</a> end_html
   //Author: Rene Brun
     
   TCanvas *c1 = new TCanvas("c1","hists with different scales",600,400);

   //create/fill draw h1
   gStyle->SetOptStat(kFALSE);
   TH1F *h1 = new TH1F("h1","my histogram",100,-3,3);
   Int_t i;
   for (i=0;i<10000;i++) h1->Fill(gRandom->Gaus(0,1));
   h1->Draw();
   c1->Update();  
    
   //create hint1 filled with the bins integral of h1
   TH1F *hint1 = new TH1F("hint1","h1 bins integral",100,-3,3);
   Float_t sum = 0;
   for (i=1;i<=100;i++) {
      sum += h1->GetBinContent(i); 
      hint1->SetBinContent(i,sum);
   }

   //scale hint1 to the pad coordinates
   Float_t rightmax = 1.1*hint1->GetMaximum();
   Float_t scale = gPad->GetUymax()/rightmax;
   hint1->SetLineColor(kRed);
   hint1->Scale(scale);
   hint1->Draw("same");
   
   //draw an axis on the right side
   TGaxis *axis = new TGaxis(gPad->GetUxmax(),gPad->GetUymin(),
         gPad->GetUxmax(), gPad->GetUymax(),0,rightmax,510,"+L");
   axis->SetLineColor(kRed);
   axis->SetLabelColor(kRed);
   axis->Draw();
}
 twoscales.C:1
 twoscales.C:2
 twoscales.C:3
 twoscales.C:4
 twoscales.C:5
 twoscales.C:6
 twoscales.C:7
 twoscales.C:8
 twoscales.C:9
 twoscales.C:10
 twoscales.C:11
 twoscales.C:12
 twoscales.C:13
 twoscales.C:14
 twoscales.C:15
 twoscales.C:16
 twoscales.C:17
 twoscales.C:18
 twoscales.C:19
 twoscales.C:20
 twoscales.C:21
 twoscales.C:22
 twoscales.C:23
 twoscales.C:24
 twoscales.C:25
 twoscales.C:26
 twoscales.C:27
 twoscales.C:28
 twoscales.C:29
 twoscales.C:30
 twoscales.C:31
 twoscales.C:32
 twoscales.C:33
 twoscales.C:34
 twoscales.C:35
 twoscales.C:36
 twoscales.C:37
 twoscales.C:38
 twoscales.C:39
 twoscales.C:40
 twoscales.C:41
 twoscales.C:42
 twoscales.C:43
 twoscales.C:44
 twoscales.C:45
 twoscales.C:46
 twoscales.C:47