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
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.
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();
}