RE: writing/reading arrays to/from a file

From: Philippe Canal <pcanal_at_fnal.gov>
Date: Wed, 30 Nov 2005 16:48:49 -0600


Hi Chiara,

TObject/TFile::Write and TFile::GetObject only writing/read one top level object. In other it will not work with a C-Style array. Please use a TObjArray (or TClonesArray, or std::vector) instead.

Cheers,
Philippe.

-----Original Message-----
From: owner-roottalk_at_pcroot.cern.ch [mailto:owner-roottalk_at_pcroot.cern.ch] On Behalf Of chiara zampolli
Sent: Saturday, November 26, 2005 10:00 AM To: roottalk_at_pcroot.cern.ch
Subject: [ROOT] writing/reading arrays to/from a file

Dear rooters,

    I have a problem similar to the one I had some days ago. As before, I am trying to write/read an array of objects to/from a file, but in this case the array is not the data member of a class (so that when writing the object of the class, I write the array), but it is exactly what I have to write. Here you find some code lines which explain what I would like to do (see also the attached testchsim.C):

void testchsim()
{

  AliTOFChSim *ch;
  ch = new AliTOFChSim[1000];

  ch[0].SetMaxToT(3);
  ch[0].SetMinToT(4);
  ch[4].SetMaxToT(2);
  ch[4].SetMaxToT(5);

  Float_t max = ch[0].GetMaxToT();
  Float_t min = ch[0].GetMinToT();
  cout << " max 0 = " << max << endl;
  cout << " min 0 = " << min << endl;
  max = ch[4].GetMaxToT();
  min = ch[4].GetMinToT();
  cout << " max 4 = " << max << endl;
  cout << " min 4 = " << min << endl;
  TFile *file = new TFile("ciccio.root","recreate");   ch->Write("ciccio",TObject::kSingleKey);   file->Close();
  delete file;
  TFile *file = new TFile("ciccio.root","read");   AliTOFChSim *ch1; //[1000]
  file->GetObject("ciccio",ch1);
  Float_t max = ch1[0].GetMaxToT();
  Float_t min = ch1[0].GetMinToT();
  cout << " max 0 = " << max << endl;
  cout << " min 0 = " << min << endl;
  max = ch1[4].GetMaxToT();
  min = ch1[4].GetMinToT();
  cout << " max 4 = " << max << endl;
  cout << " min 4 = " << min << endl;
}

where AliTOFChSim inherits from TObject (it is a class I have defined, where the MaxToT and MinToT are two data members - you can find it in the attached files). As you can see from the output of the macro:

root [15] .x testchsim.C
 ch[1] = 0x9f9afe8

 max 0  = 3
 min 0  = 4
 max 4  = 5
 min 4  = 0
 max 0  = 3
 min 0  = 4
 max 4  = -0.0618096
 min 4  = 1.1992e-11
 max 4  = -0.0618096
 min 4  = 1.1992e-11

root [16] .q

when trying to read back my array, its elements seem to be empty (apart from the first one, element zero). What am I doing wrong?

Thank you for your help.
Cheers,

   Chiara Received on Wed Nov 30 2005 - 23:50:57 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:14 MET