Problem writing TGraph to file (written object stays empty)

Hi,

I am trying to write a TGraphError object to an existing file. It puts something into the
file but the contents seems empty. I can’t figure out what I am doing wrong …

TFile* f = TFile::Open(inFilename.c_str(),“UPDATE”);
f->cd(“to some dir”); // to retrieve histos to have info for the TGraphErrors

TGraphErrors * krms_z_vs_ntrk = new TGraphErrors(… some filled arrays …);
krms_z_vs_ntrk->GetYaxis()->SetTitle(“some title”);
krms_z_vs_ntrk->GetXaxis()->SetTitle(“some label”);
krms_z_vs_ntrk->SetTitle(“some other title”);
krms_z_vs_ntrk->SetName(“SomeUniqueName”);
krms_z_vs_ntrk->GetXaxis()->SetRangeUser(0.,100.); // only relevant for plotting?

krms_z_vs_ntrk->Print(); // looks fine
krms_z_vs_ntrk->Write();
//krms_z_vs_ntrk->Write("", TObject::kOverwrite);

//f->Write(); // not needed (will write all histos in memory?)
f->Close();

I end up with a root file with object “SomeUniqueName” but when I plot it (with e.g. TBrowser) I get an empty canvas. I also add histograms to the root file and there it is no problem. I never plot the TGraph but I know it’s filled. There is also no TApplication involved (in case it matter).

Thanks for any hints.
Cheers
Andi

Hi,

even this simple test does not work:
{
Float_t x[5] = {1, 2, 3, 4, 5};
Float_t y[5] = {3, 4, 5, 2 ,1};
Float_t ex[5] = {1, 1, 1, 1, 1};
Float_t ey[5] = {1, 1, 1, 1 ,1};

TFile f(“test.root”, “RECREATE”);
TGraph *g = new TGraphErrors(5, x, y, ex, ey);
g->Draw(“AP”);
g->SetName(“hello”);
g->Write();
f.Close();
}

Maybe I am opening the resulting file in a wrong way … I am doing:
root test.root
TBrowser bla;
and then double click on “hello”.

This gives an empty canvas. I am using root 5.28.

Cheers
Andi

In the Browser select the draw option “alp”

Hi,

thanks, that was indeed the “problem” …

I wasted about three hours trying to find out why the write() does not work!

well … :wink:

thanks!
Andi

You are right. I have changed the default drawing option for graphs to “alp”.
It makes more sense.