GetYaxis()->GetXmax() for 1D histograms behaviour

Bonjour,

Wanting to add a second axis at the top of a 1D histogram, I have some difficulties to find the actual range used for the display.

Namely the supposedly straightforward method fails:

h->GetYaxis()->GetXmax() returns 1, whatever the actual value, for example:

[code]{
c1 = new TCanvas();

h = new TH1F("h", "test", 10, 0., 10.);

h->Fill(5, 999);

h->Draw();

h->GetMaximum();
cout << "h->GetMaximum() = " << h->GetMaximum() << endl;
cout << "h->GetYaxis()->GetXmax() = " << h->GetYaxis()->GetXmax() << endl;
cout << "h->GetXaxis()->GetXmax() = " << h->GetXaxis()->GetXmax() << endl;

}[/code]
I could use the h->GetMaximum() to obtain the real top of the frame, but it is strongly environnement dependant: it has either to be multiplied by 1.05 (default value) or used directly if SetMaximum() was previously used.
Is there a reason for GetYaxis()->GetXmax() method to behave is it does ?

Cheers, Vincent.

Hi Vincent,

The example twoscales.C in the $ROOTSYS/tutorials directory does that. Here it is:

#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
      
   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();
}

Cheers, Olivier