Contour plot

Hi all,

As already pointed out at
and, “cont” option of TH2::Draw() draws a wrong plot.
“cont4” does the correct job, but the plot cannot be superimposed on another histogram with wider ranges.
I have long regreted that I cannot utilize the abundant functionality of ROOT for practical applications only for this small, but serious flaw.
I am wondering if this problem would be fixed in the near future or there is any clue to get around the problem.

Many thanks,

In the two posts you mention the user requests were answered.
So you request in to draw a cont4 on a wider range. It is true that (because of the technique use to draw cont4) the superposition is not straight forward. I will try to make a macro providing a work around.

By the way, as you certainly know, CONT4 draws filled contours, so if you draw it on top of an other histogram it will hide completely what is behind… is that really what you want ? Which option are you using to draw the 'background" histogram ?

Thank you very much for your prompt reply.

Typically I want to superimpose a contour plot on a blank histogram drawn by TPad::DrawFrame(xmin, xmax, ymin, ymax) for comparing two or more plots with the same range.
Of course I do not need to use the superposition if the drawing ranges of the cont4 plot can be expanded without any artifact.


I can propose you the following solution:

   c1 = new TCanvas("c1","h2smooth",200,10,700,500);

   TH2F *h2  = new TH2F("h2","h2",40,-4,4,40,-4,4);   

   Float_t px, py, pz;
   for (Int_t i = 0; i < 25000; i++) {

   TH2F *h2r;
   h2r = rescale(h2,-10,10,-20,20);
TH2F* rescale(TH2 *h, Double_t xminr, Double_t xmaxr, 
                      Double_t yminr, Double_t ymaxr)
   Double_t bwx  = h->GetXaxis()->GetBinWidth(1);
   Double_t bwy  = h->GetYaxis()->GetBinWidth(1);
   Double_t bwx2 = bwx/2.;
   Double_t bwy2 = bwy/2.;
   Int_t nx      = h->GetXaxis()->GetNbins();
   Int_t ny      = h->GetYaxis()->GetNbins();

   Int_t nxr = (xmaxr-xminr)/bwx;
   Int_t nyr = (ymaxr-yminr)/bwx;

   TH2F *hr  = new TH2F("hr","hr",nxr,xminr,xmaxr,nyr,yminr,ymaxr);

   Double_t w,x,y;

   for (Int_t i=1; i<=nx; i++) {
      for (Int_t j=1; j<=ny; j++) {
         w = h->GetBinContent(i,j);
         x = h->GetXaxis()->GetBinLowEdge(i)+bwx2;
         y = h->GetYaxis()->GetBinLowEdge(j)+bwy2;

   return hr;

It gives the following output:

Sorry for my late reply.

I did not notice there is this simple solution.
Thank you very much!

But I still have a little problem …
Whenever I draw lines or markers etc. over the cont4 plot, I need to convert the histogram coordinate to NDC, which is quite troublesome.
So I tried to define the user coordinate equivalent to the histogram coordinate by the following script:

void SetUserCoord(const TH2 *h2, TVirtualPad *pad = gPad)
  // set user coord equivalent to TH2 coord

  const TAxis *ax = h2->GetXaxis();
  const TAxis *ay = h2->GetYaxis();

  double frameWidth = ax->GetXmax() - ax->GetXmin();
  double frameHeight = ay->GetXmax() - ay->GetXmin();

  double frameWidthNdc = 1. - pad->GetLeftMargin() - pad->GetRightMargin();
  double frameHeightNdc = 1. - pad->GetTopMargin() - pad->GetBottomMargin();

  const double HistPerNdcX = frameWidth / frameWidthNdc;
  const double HistPerNdcY = frameHeight / frameHeightNdc;

  double x1 = ax->GetXmin() - pad->GetLeftMargin() * HistPerNdcX;
  double x2 = ax->GetXmax() + pad->GetRightMargin() * HistPerNdcX;
  double y1 = ay->GetXmin() - pad->GetBottomMargin() * HistPerNdcY;
  double y2 = ay->GetXmax() + pad->GetTopMargin() * HistPerNdcY;

  pad->Range(x1, y1, x2, y2);

But this does not work. The methods such as TLine::Draw() still seem to be using NDC.
Is there any way to draw an graphic object on the cont4 plot using user coordinate?

Many Thanks,

Yes, CONT4 has its own coordinate system. It is in fact a SURF3 seen from top.