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.
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.
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?