[ROOT] Using the ReadAll method

From: Michael Gold (gold@higgs.phys.unm.edu)
Date: Fri Jun 07 2002 - 21:32:12 MEST


Dear Rooters:

I am trying to read histograms from a file into local memory.
The first routine below tries the ReadAll method, which fails.
The second method is a modification of the ReadAll method
which works as long as the file stays open.  I guess I am
happy with this.  However, am I doing something wrong with
the ReadAll method?  Do I need to keep the file open?
I guess I am thinking of what I used to do in Paw with HRIN.

-Michael Gold

/****************************************
A routine to get histograms from a file 
    in a sub directory
*****************************************/
void getSub(Option_t* fileName, Option_t* subDir="muTuple" )
{
  gDirectory->cd("Rint:/"); // make sure we are in default directory
  TDirectory *dir = gDirectory; //save pointer to Rint dir
  TFile *f = new TFile(fileName); // open in read mode
  // check that file is opened
  if(f->IsZombie()) {
    cout <<" Sorry, file " << fileName << " not opened !! " << endl;
    return;
  }
  
  f->cd(subDir);
  gDirectory->pwd();
  gDirectory->ls();
  gDirectory->ReadAll();
  dir->ls(); // this lists nothing
  delete f;
}

/****************************************
A routine to get histograms from a file 
*****************************************/
void getHist(Option_t* fileName )
{
  gDirectory->cd("Rint:/"); // make sure we are in default directory
  TDirectory *dir = gDirectory; //save pointer to Rint dir
  TFile *f = new TFile(fileName); // open in read mode
  if(f->IsZombie()) { // check that file is opened
    cout <<" Sorry, file " << fileName << " not opened !! " << endl;
    return;
  }

  // loop over keys
  TKey *key;
  TIter next(f->GetListOfKeys());
  while ((key = (TKey *) next())) {
    //   cout << key->GetClassName() << ": "<< key->GetName() << endl; // this 
works
    TNamed *thing = (TNamed*)gDirectory->GetList()->FindObject(key->GetName());
    if (thing) delete thing; 
    TObject *obj=key->ReadObj();
    dir->Add(obj);
    //    dir->Copy(key->ReadObj());
  }
  dir->pwd();
  dir->ls();  // this works as long as file stays open
  //  delete f; << but only if the file stays open
}



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:56 MET