//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 twoscalestranspad() { //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 here end_html //Author: Rene Brun TCanvas *c1 = new TCanvas("c1","hists with different scales",600,400); // c1->SetLogy(1); // --> output: twoscales_LogScaleFirst.png //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(); c1->Update(); // make a transparent pad TPad *pad2 = new TPad("pad2","",0,0,1,1); pad2->SetFillStyle(0); pad2->SetFillColor(0); pad2->SetFrameFillStyle(0); pad2->SetBorderSize(0); pad2->SetFrameLineWidth(0); pad2->SetFrameBorderMode(0); pad2->Draw(); pad2->cd(); // draw the axis on the transparent pad double mymin = -2, mymax = +2; // TGaxis *axis2 = new TGaxis(gPad->GetUxmin(),gPad->GetUymax(), // gPad->GetUxmax(), gPad->GetUymax(),mymin,mymax,510,"-L"); TGaxis *axis2 = new TGaxis(0.1,0.9,0.9,0.9,mymin,mymax,510,"-L"); axis2->SetLineColor(kBlue); axis2->SetLabelColor(kBlue); axis2->Draw(); pad2->Update(); // go back to original canvas to change RangeUser and SetLog c1->cd(); c1->Update(); h1->GetXaxis()->SetRangeUser(mymin,mymax); c1->SetLogy(1); gStyle->SetGridStyle(3); c1->SetTicks(0,1); c1->SetGrid(1,1); c1->Update(); // Suggestions: // set LogScale Y // set SetRangeUser X // play with the order of doing things // try with TGraph instead of Histogram // redraw axis? }