SetBinLabel, labels disapear if read and write, update TGraph

Hi,
As it’s written in the subject, custom labels sets in TGraphErrors with SetBinLabel disapear if, write and then read TGraph.
After read the TAxis is empty, all the labels disapears and only the one added in current scope stays.

What to do to make them stay?

Here is sample of my code:

TFile file2(somePath,“UPDATE”);

file2.GetObject(“h_tpc_event_recvertex_0”,h_tpc_event_recvertex_0);

Int_t currentEntries= h_tpc_event_recvertex_0->GetN();

//not important, i think
//h_tpc_event_recvertex_0->SetPoint(currentEntries,currentEntries+1,recVertexX[0]);
//h_tpc_event_recvertex_0->SetPointError(currentEntries,0,recVertexX[1]);

Int_t binNumber= h_tpc_event_recvertex_0->GetXaxis()->FindBin(currentEntries+1);
h_tpc_event_recvertex_0->GetXaxis()->SetBinLabel(binNumber,“new Label”);

h_tpc_event_recvertex_0->Write(“h_tpc_event_recvertex_0”,TObject::kOverwrite);

file2.Write();

Can you provide a minimal running example reproducing the issue ?

First I create a TGraph, and i save it to the file.


TFile *file1= new TFile(somepath,“recreate”);

TGraphErrors *h_tpc_event_recvertex_0 = new TGraphErrors();
h_tpc_event_recvertex_0->Write(“h_tpc_event_recvertex_0”);

file1->Close();


Now I Open the TGraph (in the different macro):
And I set in the bin with label ‘1’ new label ‘myLabel1’


TFile file2(somepath,“UPDATE”);

TGraphErrors *h_tpc_event_recvertex_0;
file2.GetObject(“h_tpc_event_recvertex_0”,h_tpc_event_recvertex_0);

h_tpc_event_recvertex_0->SetPoint(0,1,recVertexX[0]);
h_tpc_event_recvertex_0->SetPointError(0,0,recVertexX[1]);

Int_t binNumber= h_tpc_event_recvertex_0->GetXaxis()->FindBin(1);
h_tpc_event_recvertex_0->GetXaxis()->SetBinLabel(binNumber,“myLabel1”);

h_tpc_event_recvertex_0->Write(“h_tpc_event_recvertex_0”,TObject::kOverwrite);

file2.Write();


Now If I Open the TGraph again and, just Open it and Save, the labels I set disapear:


File file2(somepath,“UPDATE”);

TGraphErrors *h_tpc_event_recvertex_0;
file2.GetObject(“h_tpc_event_recvertex_0”,h_tpc_event_recvertex_0);

h_tpc_event_recvertex_0->Write(“h_tpc_event_recvertex_0”,TObject::kOverwrite);

file2.Write();


My prediction is that when openning TGraph it does not open axis to, but I don’t know how to fix it.
Please help and sorry for not precise first post.

The following is fine for me:

void bartosz546()
{
   TFile file2("file2.root","RECREATE");

   TGraphErrors *ge = new TGraphErrors();
   ge->SetName("ge");

   ge->SetPoint(0,1,0.1);
   ge->SetPointError(0,0,0.1);

   Int_t binNumber= ge->GetXaxis()->FindBin(1);
   ge->GetXaxis()->SetBinLabel(binNumber,"myLabel1");
   ge->Draw();

   ge->Write();
}

then :


root [0] .x bartosz546.C
Processing bartosz546.C...
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [1] .q

$ root file2.root
   ----------------------------------------------------------------
  | Welcome to ROOT 6.07/07                    http://root.cern.ch |
  |                                   (c) 1995-2016, The ROOT Team |
  | Built for macosx64                                             |
  | From heads/master@v6-07-06-397-g75f09f9, May 26 2016, 08:35:01 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'     |
   ----------------------------------------------------------------

root [0] 
Attaching file file2.root as _file0...
(TFile *) 0x7ffc534ea860
root [1] _file0->ls()
TFile**		file2.root	
 TFile*		file2.root	
  KEY: TGraphErrors	ge;1	
root [2] ge->Draw()

But what would happen if you add in the second scope some new label in the other bin and then draw()

I do not know. Try to modify my example.