# Plotting various TH1F's with different x-range

Is there an elegant way to plot various TH1F-Objects with different x-ranges into a single graph?

For example

``````TH1F *h1 = new TH1F("h1", "h1", 1000, -0.01, 10.01);
TH1F *h2 = new TH1F("h2", "h2", 1000, -2.01, 12.01);``````

How to plot these two histograms in a single plot ranging from -0.01 to 12.01, or whatever x-range I would like to?

h1->GetXaxis()->SetRangeUser(-1.0, 13.0) does not do the job (though on the y-axis there is no problem).

I hope there is another way then to switch to TMultiGraphs or to redefine the histogram size.

2 ways:

1. Draw the frame using TCanvas::DrawFrame() and then plot on top the 2 histograms using the option SAME.

2. group the 2 histograms into a THStack. And plot it using the option “nostack”

Thanks a lot. Solves all my problems.

If xmin_usedInDrawFrame > histo->GetBinLowEdge(1), the first bin does not display,
which can lead to bad intrepretation or non relevant a posteriori cuts
How to cope with that?

[code] Double_t xAxis[4] = {3., 5., 7., 9.};
TH1D *histo = new TH1D(“histo”,"",3, xAxis);
histo->SetBinContent(1,2.);
histo->SetBinContent(2,4.);
histo->SetBinContent(3,3.);

TCanvas *canvas = new TCanvas(“canvas”, “canvas”,4,23,1250,772);
canvas->Divide(1,2) ;
canvas->cd(1);
histo->Draw(“sames”);
canvas->cd(2);
histo->Draw(“sames”);[/code]
Thanks, Z

Use a THStack. See examples in tutorials.

Rene

I’d tried.
From what I’ve experienced with THStack, you will get your histogram displayed depending on the other histograms’ range : from the smallest xmin to the biggest xmax.
What I’d like to plot is a histogram defined between xmin=3 and xmax=9 but displayed from x=4.
Cheers, Z

The limit you imposed is in a middle of a bin. Only complete bin are displayed in that particular case. may be that should be revised but why don’t you start at 3 instead of 4 ?

This was a dummy example.
I have several histograms with pretty different binnings and ranges (in log scale).
I’d like to set the focus on a particular energy domain.

Hi there,

The code below generates a similar problem. Same issue, I suppose.

[code]{
TH1D * histo1 = new TH1D (“histo1”,“histo1”,100,0.,100.) ;
histo1->SetBinContent(51,80.) ;

TH1D * histo2 = new TH1D (“histo2”,“histo2”,100,49.9,51.1) ; /// not ok
// TH1D * histo2 = new TH1D (“histo2”,“histo2”,100,49.5,51.5) ; /// not ok
// TH1D * histo2 = new TH1D (“histo2”,“histo2”,100,49.1,51.9) ; /// not ok
// TH1D * histo2 = new TH1D (“histo2”,“histo2”,100,49.0,52.0) ; /// ok
histo2->SetMinimum(0.) ; histo2->SetMaximum(100.) ;

histo2->Draw() ; /// histo2 is just drawn to specify histo1 graphical xmin & xmax (See http://root.cern.ch/phpBB3//viewtopic.php?t=2972)
histo1->Draw(“same”) ;
}
[/code]
Any revisions scheduled?

Cheers,
Z

Hello Mathieu,

I found out that the problem is in TGraphPainter.cxx. In PaintGrapHist in the section:

``  //      Draw a standard Histogram (default)``

around line # 1766. If I replace:

``````[...]
if (gxwork[npt-1] >= uxmin-rounding && gxwork[npt] <= uxmax+rounding) npt += 2;
else xwork[npt-2] = TMath::Min(gxwork[npt], uxmax);
[...]``````

by:

``````[...]
npt += 2;
[...]``````

Then you it works as expected. As you can imagine making a change like that sounds a bit dangerous as it may break something else. I tried to go back as far as I could in the code history but I could not find the reason why the faulty code (for your case) was introduced. More investigation are needed. It looks like the original fortran code was equivalent to the simple version ( npt += 2;).

Hi Olivier,
Thanks for the investigation. Any news?
Cheers,
Z

Hi Mathieu,

Nothing right now.
I am very busy preparing a 3 days course on ROOT
for next week (Monday Tuesday and Wednesday). I am being late on that and really need to work on it.

Best
Olivier

I started again looking at this.
I need to find why a such complex code is needed whereas it seems the simple line “npt += 2;” works better.

Ok, thanks Olivier.
Good luck
Z

For info, my change fixes also the initial example you submitted.
(still trying to find out why it is so complex in the current code).

This problem is now fixed in the SVN trunk.
Thanks for reporting.

Thanks Olivier
See you around
Z