Problem saving TList to a TFile

Hi,

I would be very thankful if someone could give me a hint whats wrong in my program…

TASK
To read TObjects (calibrations) from a TList saved in a TFile, add new calibrations to the list and save the list back to the file again

PROBLEM
When I read the TObjects from the list they are empty

CODE
The interface to the TList and TFile is below, I use the functions:

//To initialise the TFile and TList (only done once)
void ListHandler::Init(TString filename)
{
printf(“Creating TFile %s and TList\n”,filename.Data());
calib_file = new TFile(filename.Data(),“NEW”);
calib_list = new TList();
calib_file->WriteObject(calib_list,“list”,“SingleKey,Overwrite”);
calib_file->Close();

}

//To open the TFile and read in the TList
int ListHandler::ReadList(TString filename)
{
calib_file = new TFile(filename.Data(),“UPDATE”);
calib_list = new TList();
if(calib_file->IsZombie())
return 0;
else{
calib_file->GetObject(“list”,calib_list);
if(calib_list)
return 1;
else
return 0;
}
}

//To write the TList back to the file and close the file
int ListHandler::WriteList()
{
calib_file->WriteObject(calib_list,“list”,“SingleKey,Overwrite”);//,TObject::kOverwrite);
calib_file->Write();
calib_file->Close();

return 0;
}

//To save a TObject (calibevent) in the TList
int ListHandler::SaveEvent(calibEvent *event)
{
if(calib_file->IsOpen() && calib_list){
calib_list->Add(event);
return 1;
}
else
return 0;
}

Petter

I have condensed it down to the following program. It

  • creates a myobject
  • set the myobject flag to 1
  • push the object in a TList
  • write the TList to a TFile and closes the TFile
  • opens it and reads the TList
  • print the flag
    but the flag is 0…

class myobject : public TObject {
private:
Int_t flag;

public:
void SetFlag(){flag=1;}
Int_t GetFlag(){
if(flag==1)
return 1;
else
return 0;
}

};

int wtf()
{
TList *mylist = new TList();
TFile *myfile = new TFile(“wtf.root”,“NEW”);

myobject *mo = new myobject();
mo->SetFlag();
printf(“flag0 %d\n”,mo->GetFlag());

mylist->Add(mo);
printf(“flag1 %d\n”,((myobject*)mylist->First())->GetFlag());
myfile->WriteObject(mylist,“list”,“SingleKey,Overwrite”);

myfile->Write();
myfile->Close();

TFile *newfile = new TFile(“wtf.root”,“READ”);
TList *newlist;// = new TList;
newfile->GetObject(“list”,newlist);

myobject *newmo;// = new myobject();

newmo = (myobject*)newlist->First());

printf(“flag2 %d\n”,newmo->GetFlag());

}

Petter

Hi Petter,
you must compile your code (rootcint generate the dictionary file and myobject::Streamer() ).In your second exam simple add two lines:
#include <TFile.h>
ClassDef(myobject, 1); // very important

[quote]root [0] .L wtf.C+
Info in TUnixSystem::ACLiC: creating shared library /home/mucha/./wtf_C.so
root [1] wtf()
flag0 1
flag1 1
flag2 1
(int)0
root [2][/quote]For more info see UserGuide chapters Input/Output and Adding a Class.
By the way, try too ClassDef(myobject, 0)

I hope this help, Jan

Thanks! The macro now works :slight_smile:

But, the program in the first post (the actual program Im working on) will be compiled with a makefile. When I include the ClassDef in the classes in this case, the compiler complains with

[pamela@localhost testarea]$ make TestList
g++ -c -o TestList.o TestList.cpp -Iroot-config --incdir -I/home/pamela/src/testarea
g++ -c -o ListHandler.o ListHandler.cpp -Iroot-config --incdir -I/home/pamela/src/testarea
g++ -o TestList TestList.o ListHandler.o root-config --libs
ListHandler.o: In function ListHandler::ListHandler()': ListHandler.cpp:(.text+0x4): undefined reference tovtable for ListHandler’
ListHandler.o: In function ListHandler::ListHandler()': ListHandler.cpp:(.text+0x14): undefined reference tovtable for ListHandler’
ListHandler.o: In function ListHandler::~ListHandler()': ListHandler.cpp:(.text+0x24): undefined reference tovtable for ListHandler’
ListHandler.o: In function ListHandler::~ListHandler()': ListHandler.cpp:(.text+0x34): undefined reference tovtable for ListHandler’
ListHandler.o: In function calibEvent::calibEvent(float, float (*) [12], int (*) [12])': ListHandler.cpp:(.text+0x152): undefined reference tovtable for calibEvent’
ListHandler.o: In function calibEvent::calibEvent(float, float (*) [12], int (*) [12])': ListHandler.cpp:(.text+0x23a): undefined reference tovtable for calibEvent’
collect2: ld returned 1 exit status
make: *** [TestList] Error 1
[pamela@localhost testarea]$

Do I have to inlcude any other headerfiles to make this work?!

thanks again
Petter

Please send your TestList.cpp with make file (as attachment files.tar.gz)

Jan

Here they are. Main program TestList.cpp, classes in ListHandler.cpp/.h

thanks
Petter
testlist.tar (1.33 KB)

Hi Petter,
I attach your test files and strongly recommend that you read the Users’ Guide (Adding a Class).

Jan

Edited, things cleared up