Incorrect z-axis labels when plotting a 2D histogram with colztext

Dear ROOT experts,

I have been creating a 2D histogram of the ratio of 2x2D histograms (h1 and h2) and I draw all 3 onto separate canvases. The plots for h1 and h2 look healthy; however, the z-axis label on the ratio plot (h1/h2) is not consistent with the bin content. I’ve realised that the z-colour scale is correct, it’s only the labels that are wrong. The z-axis labels seem to be coming from histogram h1.

I’ve attached a root file containing h1 and h2 and the sample script (in pyroot) to reproduce the results I see is:

import ROOT as r
r.gROOT.SetBatch(True)
r.gStyle.SetPaintTextFormat(".0f")

f = r.TFile("inputs.root")
h1 = f.Get("h1")
h2 = f.Get("h2")

c = r.TCanvas()
h1.Draw("colztext")
c.SaveAs("h1.pdf")

h2.Draw("colztext")
c.SaveAs("h2.pdf")

r.gStyle.SetPaintTextFormat(".2f")
h3 = h1.Clone("h3")
h3.Divide(h2)
h3.SetMaximum(2.0)
h3.SetMinimum(0.0)
h3.Draw("colztext")
c.SaveAs("h1_h2.pdf")

inputs.root (30.2 KB)

Note that I am using python version 2.7.6 and root version 6.02.00.

The result plots are:


The z-axis scale goes above 4500 here


Again the z-axis scale goes above 4500 here even though the bin contents do not go near this and I set the maximum to 2.0.

Thanks for any help,
Shane.

Yes the z scale on the last plot does not match the bin content. I will try to understand your code.

With the following macro running with the root master I get the attached picture, which looks correct.

{
   gROOT->SetBatch(1);
   gStyle->SetPaintTextFormat(".0f");

   TFile *f = new TFile("inputs.root");
   TH2D *h1 = (TH2D*)f->Get("h1");
   TH2D *h2 = (TH2D*)f->Get("h2");

   TCanvas *c = new TCanvas();
   h1->Draw("colztext");
   c->SaveAs("h1.pdf");

   h2->Draw("colztext");
   c->SaveAs("h2.pdf");

   gStyle->SetPaintTextFormat(".2f");
   TH2D *h3 = (TH2D*)h1->Clone("h3");
   h3->Divide(h2);
   h3->SetMaximum(2.0);
   h3->SetMinimum(0.0);
   h3->Draw("colztext");
   c->SaveAs("h1_h2.pdf");
}

Thanks for the reply.

I tried running the root macro with the version of ROOT I have and had the same correct plot like you did. It seems like there’s an issue in pyroot?

It seems so… The macro I sent you is a line to line copy of your PyROOT one.

I have managed to workaround the problem so far while sticking to pyroot. Instead of using the following snippet

h3 = h1.Clone("h3")
h3.Divide(h2)

I use this instead

h3 = r.TH2D()
h1.Copy(h3)
h3.Divide(h1,h2)

It seems like the cloning procedure is the culprit

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.