Axis label disappears on saving

Hey there,

I have been wasting a lot of time trying to change the labels on the histograms I create and save to an ntuple, but I always failed. Upon checking this simple root script, I realized, that even this simple script fails:

root [0] TH1D a("a", "a", 100, 0, 100)
(TH1D &) Name: a Title: a NbinsX: 100
root [1] a.GetXaxis() -> ChangeLabel(1, -1, 0.025, -1, -1, -1, "alma");
root [2] a.SaveAs("a.C")
Info in <TH1D::SaveAs>: C++ Macro file: a.C has been generated
root [3] a.GetXaxis() -> GetBinLabel(1)
(const char *) ""
root [4] a.Draw("") // Only this shows the correct label
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1

More importantly, this fails too:

root [0] TH1D a("a", "a", 100, 0, 100)
(TH1D &) Name: a Title: a NbinsX: 100
root [1] a.GetXaxis() -> ChangeLabel(1, -1, 0.025, -1, -1, -1, "alma");
root [2] TFile::Open("alma.root", "RECREATE")
(TFile *) 0x2eb6a40
root [3] a.Write()
(Int_t) 301

I want to save my histograms with the correct labels, but the SaveAs() seem to fail achieving this. What am I doing wrong?
My root version is ROOT 6.08/02.

Cheers,
Hunyadi Adam

1 Like

I think it is simply not implemented when you save the pad a a .C file. Other formats are ok.

I don’t really want to save a histogram as .C. However, I need to write histograms with modified labels to an Ntuple. How can I do it properly?

As I said some implementation is missing. That’s a new feature and It might be that something are missing. I will look at it.

The following macro illustrate the problem. On the left the original histogram with changed label and the number of division also changed. On the right the histogram retrieved from file . I am investigating.

void changelabelsave(){
   auto a = new TH1D("a", "a", 100, 0, 1);
   a->GetXaxis()->SetNdivisions(5);
   a->GetXaxis()->ChangeLabel(1, -1, 0.025, -1, -1, -1, "Changed Label");

   auto c= new TCanvas();
   c->Divide(2,1);
   c->cd(1);
   a->Draw();

   TFile *f1 = new TFile("a.root","RECREATE");
   a->Write();
   f1->Close();

   TFile *f2 = new TFile("a.root","READ");
   auto b = (TH1D*)f2->Get("a");
   c->cd(2);
   b->Draw();
}

Problem now fixed in ROOT master. Thanks to have seen it.

2 Likes

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