RootTalk


ROOT Discussion Forums

Plotting various TH1F's with different x-range  SOLVED

Discuss installing and running ROOT here. Please post bug reports here.

Moderator: rootdev

Plotting various TH1F's with different x-range

Postby suter_a » Fri May 29, 2009 10:54

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

For example
Code: Select all
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.
suter_a
 
Posts: 25
Joined: Wed Jul 07, 2004 7:48
Location: Paul Scherrer Institute, Switzerland

Postby couet » Fri May 29, 2009 11:13

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"
User avatar
couet
 
Posts: 5511
Joined: Tue Sep 02, 2003 8:32
Location: CERN

Postby suter_a » Fri May 29, 2009 17:19

Thanks a lot. Solves all my problems.
suter_a
 
Posts: 25
Joined: Wed Jul 07, 2004 7:48
Location: Paul Scherrer Institute, Switzerland

Postby Zesp » Sun Jan 10, 2010 17:03

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

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: Select all
  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);
    gPad->DrawFrame(1.,0., 10.,5.);
    histo->Draw("sames");
  canvas->cd(2);
    gPad->DrawFrame(4.,0., 10.,5.);
    histo->Draw("sames");

Thanks, Z
Attachments
canvas.png
canvas.png (11.58 KiB) Viewed 1258 times
User avatar
Zesp
 
Posts: 313
Joined: Tue Jul 27, 2004 22:08
Location: IPHC, Strasbourg, France

Postby brun » Sun Jan 10, 2010 21:09

Use a THStack. See examples in tutorials.

Rene
User avatar
brun
 
Posts: 5823
Joined: Wed Aug 27, 2003 9:49
Location: CERN

Postby Zesp » Sun Jan 10, 2010 22:40

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
User avatar
Zesp
 
Posts: 313
Joined: Tue Jul 27, 2004 22:08
Location: IPHC, Strasbourg, France

Postby couet » Mon Jan 11, 2010 11:57

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 ?
User avatar
couet
 
Posts: 5511
Joined: Tue Sep 02, 2003 8:32
Location: CERN

Postby Zesp » Mon Jan 11, 2010 12:34

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.
User avatar
Zesp
 
Posts: 313
Joined: Tue Jul 27, 2004 22:08
Location: IPHC, Strasbourg, France

Postby Zesp » Mon Jan 11, 2010 12:57

may be that should be revised

Yes, please [-o<
User avatar
Zesp
 
Posts: 313
Joined: Tue Jul 27, 2004 22:08
Location: IPHC, Strasbourg, France

Re: Plotting various TH1F's with different x-range

Postby Zesp » Wed May 09, 2012 0:55

Hi there,

The code below generates a similar problem. Same issue, I suppose.
Code: Select all
{
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") ;
}

Any revisions scheduled?

Cheers,
Z
User avatar
Zesp
 
Posts: 313
Joined: Tue Jul 27, 2004 22:08
Location: IPHC, Strasbourg, France

Re: Plotting various TH1F's with different x-range

Postby couet » Wed May 09, 2012 18:08

Hello Mathieu,

I found out that the problem is in TGraphPainter.cxx. In PaintGrapHist in the section:
Code: Select all
  //      Draw a standard Histogram (default)


around line # 1766. If I replace:
Code: Select all
[...]
            if (gxwork[npt-1] >= uxmin-rounding && gxwork[npt] <= uxmax+rounding) npt += 2;
            else xwork[npt-2] = TMath::Min(gxwork[npt], uxmax);
[...]


by:
Code: Select all
[...]
            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;).
User avatar
couet
 
Posts: 5511
Joined: Tue Sep 02, 2003 8:32
Location: CERN

Re: Plotting various TH1F's with different x-range

Postby Zesp » Tue May 15, 2012 9:56

Hi Olivier,
Thanks for the investigation. Any news?
In the meanwhile how about adding a new gStyle method?
Cheers,
Z
User avatar
Zesp
 
Posts: 313
Joined: Tue Jul 27, 2004 22:08
Location: IPHC, Strasbourg, France

Re: Plotting various TH1F's with different x-range

Postby couet » Tue May 15, 2012 10:07

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
User avatar
couet
 
Posts: 5511
Joined: Tue Sep 02, 2003 8:32
Location: CERN

Re: Plotting various TH1F's with different x-range

Postby couet » Fri May 25, 2012 16:20

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.
...
User avatar
couet
 
Posts: 5511
Joined: Tue Sep 02, 2003 8:32
Location: CERN

Re: Plotting various TH1F's with different x-range

Postby Zesp » Fri May 25, 2012 17:07

Ok, thanks Olivier.
Good luck :wink:
Z
User avatar
Zesp
 
Posts: 313
Joined: Tue Jul 27, 2004 22:08
Location: IPHC, Strasbourg, France

Next

Return to ROOT Support

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests