Problem writing arrays

From: Thomas Hadig (hadig@toots.physik.rwth-aachen.de)
Date: Wed Dec 03 1997 - 16:21:00 MET


Hi,

i have encountered a problem in ROOT 1.03/08.

I have a class containing a static sized array of Int_t's. Writing the class
in a file and rereading it, results in a only partially filled array.
The first element will be filled only.

Below you can find an example code. Running this gives the correct result :
aruba:bos2root 28> \nice ./a.out
0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 4
0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 4
0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 4
TFile Writing Name=Event.root Title=TTree benchmark ROOT file

Rereading the file gives :
root [0] gSystem.Load("libfoo.so");
root [1] TFile *hfile = new TFile("Event.root");
root [2] TTree *tree = (TTree *)hfile->Get("T");
root [3] TBranch *b = tree->GetBranch("Foo");
root [4] gEvent = new Foo();
root [5] b->SetAddress(&gEvent);
root [6] tree->GetEvent(0)
(Int_t)12
root [7] gEvent->Print()
0 -> 0
1 -> -1
2 -> -1
3 -> -1
4 -> -1
root [8] gEv = new Foo();
root [9] gEv->Print()    
0 -> -1
1 -> -1
2 -> -1
3 -> -1
4 -> -1
root [10] 

so ... only the array element 0 is read from file.

Ciao
Thomas

----------------------------------------------------------------------------
Thomas Hadig, Ottensen, Donnerstr. 20, D-22763 Hamburg   (++49 40 39901186)
  office : I. Phys. Institut AAC, Room 1c354, Notkestr. 85, D-22603 Hamburg
           hadig@rwth-aachen.de                      Tel. ++49 40 8998 2312
http://www-h1.desy.de/~hadig/                        FAX  ++49 40 8998 4385
----------------------------------------------------------------------------
In a world without walls and fences, who needs windows and gates ?
----------------------------------------------------------------------------

--------------------------------------------------foo.h--------------------
#include <iostream.h>
#include <TObject.h>

class Foo : public TObject
 { 
  public:
    Int_t    entry[5];
             Foo();
    void     Set();
    void     Print();
  ClassDef (Foo,1)
 };
--------------------------------------------------foo.C--------------------
#include "foo.h"

Foo::Foo() 
 {
  for (Int_t i=0; i<5; i++) entry[i]=-1;
 }

void Foo::Print()
 {
  for (Int_t i=0; i<5; i++) cout << i << " -> " << entry[i] << endl;
 }

void Foo::Set()
 {
  for (Int_t i=0; i<5; i++) entry[i]=i;
 }

ClassImp(Foo) 
--------------------------------------------------foo2.C--------------------
#include <stdlib.h>
#include <iostream.h>
#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>
#include <TBranch.h>
#include "foo.h"
Foo* gEvent;

int main (int argc, char **argv)
 {
  TROOT simple("simple","Example of creation of a tree");

  Int_t eventno=0;
  gEvent = new Foo();
  TFile *hfile = new TFile("/tmp/Event.root","RECREATE","TTree benchmark ROOT file");
  hfile->SetCompressionLevel(1);
  TTree *tree = new TTree("T","An example of a ROOT tree");
  tree->SetAutoSave(1000000000); // autosave when 1 Gbyte written
  Int_t bufsize = 64000;
  Int_t split   = 1;
  TBranch *b = tree->Branch("Foo", "Foo", &gEvent, bufsize,split);
  Int_t nb=0;
  while (eventno<3)
   {
    eventno++;
    gEvent->Set();
    nb+=tree->Fill();
    gEvent->Print();
   }
  hfile->Write();
  hfile->Close();
  return 0;
 }
--------------------------------------------------domake--------------------
make foo.o
rootcint -f foocint.C -c foo.h LinkDef.h
make foocint.o
g++ -Wl,-soname,libfoo.so -shared -g -Wl,-u,__builtin_new -Wl,-u,__builtin_delete -Wl,-u,__nw__FUiPv foo.o foocint.o -o libfoo.so
make foo2.o
g++ -Wl,-u,__builtin_new -Wl,-u,__builtin_delete -Wl,-u,__nw__FUiPv -o a.out foo2.o -L. -lfoo -L/cern/root_gcc/lib -lBase -lCint -lClib -lCont -lFunc -lGraf -lGraf3d -lHist -lHtml -lMeta -lMinuit -lNet -lPostscript -lProof -lTree -lUnix -lZip -lg++ -lm -ldl -L/afs/desy.de/group/h1/lib -lh1util -lfpack -lbos -L/cern/pro/lib -lgrafX11 -lmathlib -lgenlib -lkernlib -lpacklib -lgenlib -L/usr/lib -lftn -lh1ndb
\nice ./a.out
--------------------------------------------------END--------------------



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:26:22 MET