TH1 Xaxis SetLimits

Hi there,

Just a stupid question I hope… I’d like to plot a histogram out of its range (e.g. with 2 extra bins, one on the far left, one on the far right). But I cannot figure out how to do it. I’ve tried several functions (SetRange(), SetRangeUser(), SetLimits()) in vain. The Set() method does approximately what I want but of course shifts everything (see code below). Could anyone gimme a hand ?

Cheers,
Zesp


I have: 
    _
  _| |
 |   |


And I want:
    _
  _| |
_|   |_

{

gROOT->Reset() ; 

Int_t nBins=2 ; 
Double_t binMin=-1., binMax=+1. ; 

TH1F *h1 = new TH1F ("h1","h1",nBins,binMin,binMax) ;  
  h1->SetBinContent(1,1.) ; 
  h1->SetBinContent(2,2.) ; 
  h1->SetMinimum(0.) ; 
        
TH1F *h2 = (TH1F*) h1->Clone("h2") ; 

TCanvas *c1 = new TCanvas ("c1","c1",10,10,800,600) ; 
c1->Divide(1,2) ; 

c1->cd(1) ; 
  h1->Draw() ; 

c1->cd(2) ; 
  h2->GetXaxis()->Set(nBins+2, binMin-h2->GetBinWidth(1), binMax+h2->GetBinWidth(1)) ; 
//h2->SetAxisRange            (binMin-h2->GetBinWidth(1), binMax+h2->GetBinWidth(1)) ; 
//h2->GetXaxis()->SetLimits   (binMin-h2->GetBinWidth(1), binMax+h2->GetBinWidth(1)) ; 
//h2->GetXaxis()->SetRangeUser(binMin-h2->GetBinWidth(1), binMax+h2->GetBinWidth(1)) ; 
//h2->GetXaxis()->SetRange    (binMin-h2->GetBinWidth(1), binMax+h2->GetBinWidth(1)) ; 
  h2->Draw() ;
 
//RebinAxis() ???
//FindNewAxisLimits() ???
//RecomputeAxisLimits() ???
//SameLimitsAndNBins() ???
 
c1->cd() ; 

}

One possible way:

{
   Int_t nBins=2 ;
   Double_t binMin=-1., binMax=+1. ;

   TH1F *frame = new TH1F ("frame","frame",nBins,-2.,2.) ;       
   frame->SetMinimum(0.) ;
   frame->SetMaximum(3.) ;

   TH1F *h1 = new TH1F ("h1","h1",nBins,binMin,binMax) ;
   h1->SetBinContent(1,1.) ;
   h1->SetBinContent(2,2.) ;
   h1->SetMinimum(0.) ;
   h1->SetFillStyle(3004);
   h1->SetFillColor(1);   

   TCanvas *c1 = new TCanvas ("c1","c1",10,10,800,600) ;

   frame->Draw() ;
   h1->Draw("SAME") ;  
}

Yeah, but then I have to draw extra lines to ‘close’ the histogram and so I need to get the two histogram ends relative coordinates.
How can I do that ? What are the other possible ways ?
Thanks in advance,
Zesp

a possible way:

{
   Int_t nBins=2 ;
   Double_t binMin=-1., binMax=+1. ;

   TH1F *frame = new TH1F ("frame","frame",nBins,-2.,2.) ;
   frame->SetMinimum(0.) ;
   frame->SetMaximum(3.) ;

   TH1F *h1 = new TH1F ("h1","h1",nBins,binMin,binMax) ;
   h1->SetBinContent(1,1.) ;
   h1->SetBinContent(2,2.) ;
   h1->SetMinimum(0.) ;
   h1->SetFillColor(20.) ;
   h1->SetLineColor(0.) ;

   TCanvas *c1 = new TCanvas ("c1","c1",10,10,800,600) ;

   frame->Draw() ;
   h1->Draw("SAME"); 
   c1->RedrawAxis();
}

This does not seem to work… I’m using ROOT 5.08 under cygwin. In black’s what I get. In red’s what I’d like to have.
Thanks, Zesp


With the last macro I sent you using root 5.09/01 under cygwin I get the following:


I get the same too. But without the 2 following lines:

h1->SetFillColor(20.) ; 
h1->SetLineColor(0.) ; 

I get the previous figure, namely a plain histogram with no boundaries. And what I’d like to have is the same plain histogram WITH boundaries (i.e. with the 2 red dashed lines on the side).

I have: 
    _ 
  _|  
 .   . 

And I want: 
    _ 
  _| | 
 |   | 

Thx

May be that is not possible in your case but why don’t you just create the histogram a bit larger:

{
   Int_t nBins = 2 ;
   Int_t binmargin = 1;
   Double_t binMin=-1., binMax=+1. ;
   Double_t xmargin = 1;
 
   TH1F *frame = new TH1F ("frame","frame",nBins,-2.,2.) ;
 
   TH1F *h1 = new TH1F ("h1","h1",nBins+2*binmargin,
                                 binMin-xmargin,binMax+xmargin) ;
   h1->SetBinContent(1+binmargin,1.) ;
   h1->SetBinContent(2+binmargin,2.) ;
 
   TCanvas *c1 = new TCanvas ("c1","c1",10,10,800,600) ;
 
   h1->Draw("");    
}

That’s what I’m doing actually but I thought there had to be a nicer to do so (with some SetRange() like functions). Never mind! Thanks for your help. Best Regards, Zesp