Re: [ROOT] help reading data files

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Tue Jul 31 2001 - 18:30:45 MEST


On Tue, 31 Jul 2001 10:43:49 -0400
Christy <skow@holley.physics.brown.edu> wrote
concerning ": [ROOT] help reading data files":
> Hi
> I want to read a very large number of root data files out of a
> directory. Is there any easy way of doing this without having to
> explicitly write the name of each data file in my macro? Thanks very
> much.

Here's a skeleton to get a TObjArray of TObjString containing file
names that match the regular expression "pattern". 

  void *dir = gSystem->OpenDirectory(dirname);
  if (!dir) {
    cerr <<  "couldn't open directory << dirname << endl;
    return 0;
  }
  TObjArray*  fileList = new TObjArray;
  const char* file;
  TString     fileName;
  TRegexp     regexp(pattern);
  while ((file = gSystem->GetDirEntry(dir))) {
    fileName = file;
    if (!fileName.CompareTo(".") || !fileName.CompareTo(".."))
      continue;
    if (fileName.Index(regexp) != kNPOS)
      fileList->Add(new TObjString(fileName));
  }  
  gSystem->FreeDirectory(dir);

You can now loop ever the TObjArray, doing 
 
  TIter       next(fileList); 
  TObjString* fn = 0;
  while((fn = (TObjString*)next())) { 
    TFile* f = TFile::Open(fn.Data()); 
    if (!f) 
      continue; 
    // do what ever 
    f->Close();
  }
  
Hope that's what you want.  

Yours,

Christian  -----------------------------------------------------------
Holm Christensen                             Phone:  (+45) 35 35 96 91 
  Sankt Hansgade 23, 1. th.                  Office: (+45) 353  25 305 
  DK-2200 Copenhagen N                       Web:    www.nbi.dk/~cholm    
  Denmark                                    Email:       cholm@nbi.dk



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:53 MET