Changing style of 2-D histogram

I want to change the aesthetic style of an already-created histogram that is retrieved from file.
I think I am supposed to use for example the command gROOT->SetStyle(“Pub”) to change the style to use bold type fonts and lines.
Here is the file I am using that contains the histogram I want to change the style of: hist.root (6.04 KB)

The following code seems like it should work, but it fails to change the style of the color palette axis for the case of a 2-D histogram.

gROOT.SetStyle("Pub");
TCanvas c("c", "c");
TFile f("hist.root", "read");
h->UseCurrentStyle();
h->Draw("colz");

hist_fail.pdf (14.6 KB)

The following DOES WORK though.

gROOT.SetStyle("Pub");
TCanvas c("c", "c");
TFile f("hist.root", "read");
h->UseCurrentStyle();
h->Draw(); // <--- extra Draw command with no arguments.
h->Draw("colz");

hist_success.pdf (14.5 KB)

Shouldn’t the first method work?

gROOT->ForceStyle(1);

Thank you couet.
Can you tell me where I put in your line?
I tried the following, but failed.

gROOT->SetStyle("Pub");
gROOT->ForceStyle(1); // <--- placed here
TCanvas c("c", "c");
TFile f("hist.root", "read");
h->UseCurrentStyle();
h->Draw("colz");

Try: // ... TFile f("hist.root", "read"); TH2 *h; f.GetObject("h", h); // TH2F or TH2D // ...

Thank you Coyote.
I tried the following, but failed.

gROOT->SetStyle("Pub");
gROOT->ForceStyle(1); // <--- Use ForceStyle as suggested by couet
TCanvas c("c", "c");
TFile f("hist.root", "read");
TH2D *h; // <--- tried creating a pointer for histogram first
f.GetObject("h", h);
h->UseCurrentStyle();
h->Draw("colz"); // <--- color palette axis fails to be drawn in "Pub" style

I think you need to specify your ROOT version (I’m afraid I see no such problem with my ROOT 5.34/26).
I tried: { gROOT->SetStyle("Pub"); // gROOT->ForceStyle(1); // uncomment either this line ... TCanvas *c = new TCanvas("c", "c"); // the "hsimple.root" file comes from ${ROOTSYS}/tutorials/ TFile *f = TFile::Open("hsimple.root"); TH2F *hpxpy; f->GetObject("hpxpy", hpxpy); // hpxpy->UseCurrentStyle(); // ... or this line hpxpy->Draw("colz"); }

Thank you Coyote.
The version of ROOT I was using was 5.34/19, which is a bit older than yours.
So I switched to the same version you are using 5.34/26.
I tried your code that you suggested on the same exact file hsimple.root from ${ROOTSYS}/tutorials/ and confirmed your claim that it works.

The funny thing is that it was still not working on the hist.root file I uploaded in my original post.
After some research, I found the cause of the problem.
The problem lies in whether the original histogram was drawn before being saved to file (even at this version 5.34/26).
Here I post two TH2D histograms that were created with the code below.

TH2D *h = new TH2D("h", "h", 3, 0, 1, 3, 0, 1);
h->Fill(0.7,0.7);
h->Draw("colz"); // <--- this line only used when saving in hist2a.root
h.SaveAs("hist2.root");

The two histograms appear identical when opened in ROOT but behave differently when retrieved for plotting.
Coyote’s code does not work for hist2a.root but does work for hist2b.root.

Strange indeed…
hist2a.root (6.19 KB)
hist2b.root (3.61 KB)

I think something needs to be fixed in the future.
Obviously the behavior of a retrieved histogram should not depend on whether or not it was viewed before being saved to file, should it?