Failing retrieving written object

Hello all,

I am trying to test a simple program to write and read a histogram object. I get an error when I try to access the TH1F object which was loaded from my demo.root file.
I looked at the documentation and I tried calling different methods (just in case I was doing something wrong hehehe) but I did not have any success. One thing I tried was (in ROOT environment)

TFile f(“demo.root”)
TBrowser tbrowsenow

Here I was able to see that my THF1 has been properly saved. It is a very simple example… can anyone reproduce the following error? What am I doing wrong?

BTW, I have attached the short silly script…

bash-3.2$ root writeROOT.C 
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version   5.25/02 29 September 2009   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *            *
  *                                         *

ROOT 5.25/02 (trunk@30530, Sep 29 2009, 15:28:19 on linuxx8664gcc)

CINT/ROOT C/C++ Interpreter version 5.17.00, Dec 21, 2008
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] 
Processing writeROOT.C...
Test session started ******************************
Error: illegal pointer to class object h2 0x0 502  writeROOT.C:17:


writeROOT.C (1.35 KB)

You had several problems in your small test. see fixed version below


TFile *file = new TFile(“demo.root”,“recreate”);

TH1F *h = new TH1F(“hwoow”,“test”,1000,-2,2);
delete file;

file = TFile::Open(“demo.root”); //TFile::
TH1F h2=(TH1F) file->Get(“h9585”);

cout << "Test session started ******************************" << endl;
cout << "Name   : " << h2->GetName() << endl;
cout << "Npars  : " << h2->GetNdivisions() << endl;
delete file;