Can't draw Histos in Array or List when loading from file

Hello,
when i store Histogramms in a TList or a TObjArray and that in a .root file and read them back in, i can’t use the Draw() method. Ok, actually I can, but it draws an empty grey pad. I suspect that I might have a Problem with the Painter? Everything else works fine the Histogramms have the correct entrys and I am able to draw them via the TBrowser. So what am I doing wrong in the read Code?

Here the Example for TList. I use TObjArray in exactly the same way.
Its almost the same as in http://root.cern.ch/root/roottalk/roottalk03/1798.html.

int fillhisto(){
  TH1F *myhist;
  TList * hlist= new TList();
  char *histname = new char[10];
  for (Int_t h=0; h<5; h++) {
    sprintf(histname, "h_x_%d",h);
    myhist = new TH1F(histname,"",100,-0.25,0.25);
    //     add it to the list
    hlist->Add(myhist);
      for (Int_t iii=0;iii<100; iii++) {
        myhist ->SetBinContent(iii,iii*2);
      }
  }
  TFile f("histo.root", "RECREATE");
  //  write out as list
  hlist->Write("myhists", TObject::kSingleKey);
  f.Close();
  return 0;
}

int readhisto(){
  TFile f("histo.root");
  TList * hlist = (TList*)f.Get("myhists");
  hlist->Print();
  TH1F * h = (TH1F *)hlist->At(4);
  h->Print();
  h->Draw();         // <--- here is the problem (only grey pad)
  return 0;
}

Thanks in advance,
Stephan

Stephan,

In your readhisto function, before reading the histos from the file, add the following statement;
TH1::AddDirectory(kFALSE);

By default when reading from a file, histograms are referenced from the
gDirectory->GetList(). When you close the file, the objects in this list are deleted.

By calling AddDirectory(kFALSE), you instruct the system to not reference your objects. You are the owner in this case.

Rene