TH2 z axis not redrawn with "colz"

I’m having the problem of drawing a 2D histogram with fractions created from numerator->Divide(denominator), after the numerator in that fraction has already been drawn. The z axis label and numbers are still the values of the numerator, rather than the fraction. Both are drawn with “colz”. This feature doesn’t appear if I don’t draw the numerator first. I attach some code and the root file with histos.

{
    gROOT->Reset();
    gROOT->SetBatch();
    gStyle->SetOptStat(0);

    TFile file("./file.root", "READ");

    TH2F* hnum = (TH2F*)file.Get("SelectedMigratedPtPrimaryTracks_PtEta"); //numerator
    TH2F* hden = (TH2F*)file.Get("SelectedPrimaryTracks_PtEta");  // denominator

    hnum->Rebin2D(4, 16);
    hden->Rebin2D(4, 16);

    TCanvas c("c","c",800,600);
    c.SetRightMargin(0.2);

    //  Draw numerator and denominator
    hnum->SetTitle(";x;y;z");        // UNCOMMENT THESE 
    hnum->Draw("colz");              // LINES AND THE 
    c.SaveAs("numerator.png");       // FRACTION HISTO
    hden->Draw("colz");              // BELOW WILL HAVE ITS
    c.SaveAs("denominator.png");     // Z AXIS  CORRECTLY DRAWN

    // Draw fraction
    TH2F* h_frac = (TH2F*)hnum->Clone("fraction");
    h_frac->GetZaxis()->SetTitle("z frac");
    h_frac->Divide(h_frac, hden, 1., 1., "B");
    cout << "bin content (8,25): " << h_frac->GetBinContent(8, 25) << endl;
    h_frac->Draw("colz");
    c.SaveAs("fraction.png");

}

As written in the code, if the numerator is not drawn first, the fraction comes out fine.

I’m aware that similar problems have been reported in sft.its.cern.ch/jira/si/jira.is … -7168.html, but none of them are helping me. I’m getting this problem with pyROOT as well, and creating a new canvas does not help.
file.root (522 KB)

I think the following is ok:

{
    gROOT->Reset();
    gROOT->SetBatch();
    gStyle->SetOptStat(0);

    TFile file("./file.root", "READ");

    TH2F* hnum = (TH2F*)file.Get("SelectedMigratedPtPrimaryTracks_PtEta"); //numerator
    TH2F* hden = (TH2F*)file.Get("SelectedPrimaryTracks_PtEta");  // denominator

    hnum->Rebin2D(4, 16);
    hden->Rebin2D(4, 16);

    TCanvas c("c","c",800,600);
    c.SetRightMargin(0.2);

   //  Draw numerator and denominator
   hnum->DrawClone("colz");
   gPad->Update();
   c.SaveAs("numerator.png");

   c.Clear();
   hden->DrawClone("colz");
   gPad->Update();
   c.SaveAs("denominator.png");

   // Draw fraction
   c.Clear();
   TH2F* h_frac = (TH2F*)hnum->Clone("fraction");
   h_frac->GetZaxis()->SetTitle("z frac");
   h_frac->Divide(h_frac, hden, 1., 1., "B");
   cout << "bin content (8,25): " << h_frac->GetBinContent(8, 25) << endl;
   h_frac->DrawClone("colz");
   c.SaveAs("fraction.png");
}

OK, this works, and after closer inspection it seems it’s the DrawClone that does it (I could comment out the other new things without altering the result). But this is a simplified example, I want this to work in my plotting scripts generally. In those, I pass histograms to a macro which creates a canvas, plots the histogram and saves the figure. I could get around this temporarily by calling “DrawCopy” inside that macro rather than “Draw” (“DrawClone” produced an empty plot, even after calling gPad->Update() or canvas->Update()). But is this intended to get fixed? It’s clearly not working as you’d expect/want it to.

You can also use different canvas… you draw all you plot on the same, that also create problem … that’s why a Clear was needed also

Lorenzo and I had a closer look to your question. We will come back to it. Basically the command:
hnum->Clone(“fraction”); does copy of the palette also (which is part of the histogram). We need to looks at this closer. instead do:
TH2F* h_frac =new TH2F(*hnum);