Re: Problems with ROOT files

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Mar 28 2000 - 10:19:46 MEST


Hello Xavier,
In your first macro, you create two objects with the same name on the
file.
In this case, ROOT distinguishes them by assigning a cycle number 1 and
2.
You could retrieve these objects in memory either with:
  TStint *sti1 = (TStint*)f.Get("sti;1");
  TStint *sti2 = (TStint*)f.Get("sti;2");
or by looping on the list of keys in the file (what you do).
However, in this particular case, our convention is to return the
highest cycle first. ROOT behaves like the old VMS file cycle numbers.
What you should do is to assign a unique name to each object in the
file.
Instead of:
  TStint stia(1);
  stia.Write("sti");
  stia.Set(2);
  stia.Write("sti");
you should do:
  TStint stia(1);
  stia.Write("sti1");
  stia.Set(2);
  stia.Write("sti2");

The problem with the second macro is simple.
TKey::ReadObj read objects from the current directory/file.
This is on purpose for performance reasons.
To get the expected behaviour, you should replace:

    stic = (TStint *)key3->ReadObj();
    stid = (TStint *)key4->ReadObj();
by

    f3.cd();
    stic = (TStint *)key3->ReadObj();
    f4.cd();
    stid = (TStint *)key4->ReadObj();

Rene Brun

GENTIT Francois-Xavier DAPNIA wrote:
> 
> Dear Rooters,
> 
> I encounter the following problems using ROOT files : I have created a class
> TStint, which contains just an Int_t. This class TStint derives from
> TObject, so it can be saved into a file. The code for this extremely short
> class is given in the joined files TStint.h and TStint.cpp. I have compiled
> and linked the class Stint and then I have tried the following macro
> testf3.C ;
> 
> {
>   TFile f1("litstat1.root","RECREATE");
>   TStint stia(1);
>   stia.Write("sti");
>   stia.Set(2);
>   stia.Write("sti");
>   f1.Close();
>   Int_t i1;
>   TFile f3("litstat1.root","READ");
>   TIter nextkey3(f3.GetListOfKeys());
>   TKey *key3;
>   TStint *stic;
>   while (key3 = (TKey *)nextkey3()) {
>     stic = (TStint *)key3->ReadObj();
>     i1 = stic->Get();
>     cout << "i1 : " << i1 << endl;
>   }
>   f3.Close();
> }
> 
> Running this macro, one gets :
> 
> root[0]  .x testf3.C
> i1 : 2
> i1 : 1
> root[1]
> 
> The file is read in reverse order. Why ?
> 
> In addition, I have tried the following macro testf4.C, which tries to read
> 2 files at the same time :
> 
> {
>   TFile f1("litstat1.root","RECREATE");
>   TStint stia(1);
>   stia.Write("sti");
>   stia.Set(2);
>   stia.Write("sti");
>   f1.Close();
>   TFile f2("litstat2.root","RECREATE");
>   TStint stib(3);
>   stib.Write("sti");
>   stib.Set(4);
>   stib.Write("sti");
>   f2.Close();
>   Int_t i1,i2;
>   TStint *stic;
>   TStint *stid;
>   TKey *key3;
>   TKey *key4;
>   TFile f3("litstat1.root","READ");
>   TIter nextkey3(f3.GetListOfKeys());
>   TFile f4("litstat2.root","READ");
>   TIter nextkey4(f4.GetListOfKeys());
>   while (key3 = (TKey *)nextkey3()) {
>     key4 = (TKey *)nextkey4();
>     stic = (TStint *)key3->ReadObj();
>     stid = (TStint *)key4->ReadObj();
>     i1 = stic->Get();
>     i2 = stid->Get();
>     cout << "i1 : " << i1 << "   i2 : " << i2 << endl;
>   }
>   f3.Close();
>   f4.Close();
> }
> 
> Here the result is entirely wrong. It gives :
> 
> root[0]  .x testf4.C
> i1 : 4   i2 : 4
> i1 : 3   i2 : 3
> root[1]
> 
> File f4 is read twice and in reverse order, and file f3 is not read ? What
> am I doing wrong ? I use ROOT 2.23/12 on Windows NT.
> Thanks a lot if you can help me.
> 
>           F.X. Gentit
>           DAPNIA/SPP CEN Saclay
>           tel : 01 69 08 30 38     fax : 01 69 08 64 28
>           web : http://home.cern.ch/~gentit/
> 
>  <<TStint.h>>  <<TStint.cpp>>  <<testf3.C>>  <<testf4.C>>
> 
>   ------------------------------------------------------------------------
> 
>    TStint.h   Name: TStint.h
>               Type: unspecified type (application/octet-stream)
> 
>    TStint.cpp   Name: TStint.cpp
>                 Type: unspecified type (application/octet-stream)
> 
>    testf3.C   Name: testf3.C
>               Type: unspecified type (application/octet-stream)
> 
>    testf4.C   Name: testf4.C
>               Type: unspecified type (application/octet-stream)



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:22 MET