TFileIter.h

Go to the documentation of this file.
00001 // @(#)root/table:$Id: TFileIter.h 23736 2008-05-08 04:53:29Z brun $
00002 // Author: Valery Fine(fine@bnl.gov)   01/03/2001
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
00006  * Copyright (C) 2001 [BNL] Brookhaven National Laboratory.              *
00007  * All rights reserved.                                                  *
00008  *                                                                       *
00009  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00010  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00011  *************************************************************************/
00012 
00013 #ifndef ROOT_TFileIter
00014 #define ROOT_TFileIter
00015 
00016 ///////////////////////////////////////////////////////////////////////////
00017 //                                                                       //
00018 // Class to iterate (read / write ) the events written to /from TFile.   //
00019 //                                                                       //
00020 //  - set the current internal cursor directly by different means        //
00021 //  - set the current cursor to the "next" position if available         //
00022 //  - gain extra information of the TKey object at the current position  //
00023 //  - read TObject object from the TFile defined by TKey at the current  //
00024 //         position                                                      //
00025 //                                                                       //
00026 //  - Read "next" object from the file                                   //
00027 //  - n-th object from the file                                          //
00028 //  - object that is in n object on the file                             //
00029 //  - read current object                                                //
00030 //  - return the name of the key of the current object                   //
00031 //  - return the current position                                        //
00032 //  - set the current position by the absolute position number           //
00033 //  - set the current position by relative position number               //
00034 //  - get the number of keys in the file                                 //
00035 //                                                                       //
00036 // The event is supposed to assign an unique ID in form of               //
00037 //                                                                       //
00038 // TKey name ::= event Id ::= eventName "." run_number "." event_number  //
00039 //                                                                       //
00040 //                                                                       //
00041 // and stored as the TKey name of the object written                     //
00042 //                                                                       //
00043 //  author Valeri Fine                                                   //
00044 //                                                                       //
00045 ///////////////////////////////////////////////////////////////////////////
00046 
00047 #ifndef ROOT_TString
00048 #include "TString.h"
00049 #endif
00050 #ifndef ROOT_TIterator
00051 #include "TIterator.h"
00052 #endif
00053 #ifndef ROOT_TList
00054 #include "TList.h"
00055 #endif
00056 #ifndef ROOT_TFile
00057 #include "TFile.h"
00058 #endif
00059 
00060 
00061 class TFileIter : public TListIter {
00062 
00063 private:
00064 
00065    TFile      *fFileBackUp;       //! temporary data-members
00066    TDirectory *fDirectoryBackUp;  //! to save/restore TFile/TDirectory global scope
00067    TFileIter *fNestedIterator;   //! The inner TFidrectory interator;
00068 
00069    virtual TIterator &operator=(const TIterator &) { return *this; }
00070    virtual bool operator!=(const TIterator &it) const { return TListIter::operator!=(it);}
00071    
00072 protected:
00073    TDirectory   *fRootFile;       // TDirectory/TFile to be iterated over
00074    TString  fEventName;           // current key name
00075    UInt_t   fRunNumber;           // current "run number"
00076    UInt_t   fEventNumber;         // current "event number"
00077    Int_t    fCursorPosition;      // the position of the current key in the sorted TKey list
00078    Bool_t   fOwnTFile;            // Bit whether this classs creates TFile on its own to delete
00079 
00080    void     Initialize();
00081    TObject *ReadObj(const TKey *key) const;
00082    void     RestoreFileScope();
00083    void     SaveFileScope();
00084    TKey    *NextEventKey(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*");
00085 
00086 public:
00087 
00088    TFileIter(const char *name, Option_t *option = "",
00089              const char *ftitle = "", Int_t compress = 1,
00090              Int_t netopt = 0);
00091    TFileIter(TFile *file=0);
00092    TFileIter(TDirectory *directory);
00093    TFileIter(const TFileIter &);
00094    virtual ~TFileIter();
00095 // --- draft !!!     virtual Int_t Copy(TFile *destFile);
00096    Int_t   CurrentCursorPosition() const;
00097    virtual const TFile *GetTFile() const;
00098    virtual const TDirectory *GetTDirectory() const;
00099    static  TString MapName(const char *name, const char *localSystemKey = 0
00100                                             , const char *mountedFileSystemKey = 0);
00101    static  const char *GetResourceName();
00102    static  const char *GetDefaultMapFileName();
00103    static  const char *GetLocalFileNameKey();
00104    static  const char *GetForeignFileSystemKey();
00105    static  void  PurgeKeys(TList *listOfKeys);
00106    virtual Bool_t      IsOpen() const;
00107    virtual TObject    *NextEventGet(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*");
00108    virtual Int_t       NextEventPut(TObject *obj, UInt_t eventNum, UInt_t runNumber, const char *name=0);
00109    void                SetCursorPosition(Int_t cursorPosition);
00110    void                SetCursorPosition(const char *keyNameToFind);
00111    Int_t               GetObjlen() const;
00112    virtual Int_t       TotalKeys() const;
00113    virtual TKey       *SkipObjects(Int_t  nSkip=1);
00114    virtual TObject    *GetObject() const;
00115    virtual Int_t       GetDepth() const;
00116 
00117    TKey               *GetCurrentKey() const;
00118    const char         *GetKeyName() const;
00119 
00120    TFileIter &operator=(Int_t cursorPosition);
00121    TFileIter &operator=(const char *keyNameToFind);
00122    TFileIter &operator+=(Int_t shift);
00123    TFileIter &operator-=(Int_t shift);
00124    TFileIter &operator++();
00125    TFileIter &operator--();
00126 
00127    TObject *operator*() const;
00128    operator const char *() const;
00129    operator const TFile *() const;
00130    operator const TDirectory *() const;
00131    operator int () const;
00132    int operator==(const char *name) const;
00133    int operator!=(const char *name) const;
00134 
00135 public:  // abstract TIterator methods implementations:
00136 
00137    virtual TObject *Next();
00138    virtual TObject *Next(Int_t  nSkip);
00139    virtual void Reset();
00140    virtual void Rewind();
00141    TObject *operator()(Int_t  nSkip);
00142    TObject *operator()();
00143 
00144    ClassDef(TFileIter,0) // TFile class iterator
00145 };
00146 
00147 //__________________________________________________________________________
00148 inline const char *TFileIter::GetResourceName()        {return "ForeignFileMap";}
00149 //__________________________________________________________________________
00150 inline const char *TFileIter::GetDefaultMapFileName()  {return "io.config";}
00151 //__________________________________________________________________________
00152 inline const char *TFileIter::GetLocalFileNameKey()    {return "LocalFileSystem";}
00153 //__________________________________________________________________________
00154 inline const char *TFileIter::GetForeignFileSystemKey(){return "MountedFileSystem";}
00155 
00156 //__________________________________________________________________________
00157 inline Int_t TFileIter::CurrentCursorPosition() const
00158 {
00159    // return the current
00160    return fNestedIterator ? fNestedIterator->CurrentCursorPosition() : fCursorPosition;
00161 }
00162 
00163 //__________________________________________________________________________
00164 inline const TFile *TFileIter::GetTFile() const { return GetTDirectory()->GetFile(); }
00165 //__________________________________________________________________________
00166 inline const TDirectory *TFileIter::GetTDirectory() const
00167 { return fNestedIterator ? fNestedIterator->GetTDirectory() : fRootFile; }
00168 
00169 //__________________________________________________________________________
00170 inline TObject *TFileIter::Next()
00171 {
00172    // Make 1 step over the file objects and returns its pointer
00173    // or 0, if there is no object left in the container
00174    return Next(1);
00175 }
00176 
00177 //__________________________________________________________________________
00178 inline void TFileIter::Rewind() 
00179 {
00180    // Alias for "Reset" method
00181    Reset();
00182 }
00183 //__________________________________________________________________________
00184 inline void  TFileIter::SetCursorPosition(Int_t cursorPosition)
00185 {
00186    // Make <cursorPosition> steps (>0 - forward) over the file
00187    // objects to skip it
00188    if (fNestedIterator) 
00189       fNestedIterator->SetCursorPosition(cursorPosition);
00190    else 
00191       SkipObjects(cursorPosition - fCursorPosition);
00192 }
00193 
00194 //__________________________________________________________________________
00195 inline TFileIter &TFileIter::operator=(const char *keyNameToFind)
00196 {
00197    // Iterate unless the name of the object matches <keyNameToFind>
00198    SetCursorPosition(keyNameToFind); return *this;}
00199 
00200 //__________________________________________________________________________
00201 inline TFileIter &TFileIter::operator=(Int_t cursorPosition)
00202 {
00203   // Iterate over <cursorPosition>
00204   SetCursorPosition(cursorPosition);
00205   return *this;
00206 }
00207 //__________________________________________________________________________
00208 inline TFileIter::operator const TDirectory *() const
00209 { return GetTDirectory();  }
00210 
00211 //__________________________________________________________________________
00212 inline TFileIter::operator const TFile *() const
00213 { return GetTFile (); }
00214 //__________________________________________________________________________
00215 inline TFileIter &TFileIter::operator+=(Int_t shift)
00216 { SkipObjects(shift); return *this;}
00217 //__________________________________________________________________________
00218 inline TFileIter &TFileIter::operator-=(Int_t shift)
00219 { return operator+=(-shift);}
00220 //__________________________________________________________________________
00221 inline TFileIter &TFileIter::operator++()
00222 { SkipObjects( 1); return *this;}
00223 //__________________________________________________________________________
00224 inline TFileIter &TFileIter::operator--()
00225 { SkipObjects(-1); return *this;}
00226 //__________________________________________________________________________
00227 inline TObject *TFileIter::operator*() const
00228 { return GetObject();}
00229 //__________________________________________________________________________
00230 inline TFileIter::operator int () const
00231 { return CurrentCursorPosition(); }
00232 //__________________________________________________________________________
00233 inline TFileIter::operator const char *() const
00234 {
00235    // return the current key name
00236    return GetKeyName();
00237 }
00238 //__________________________________________________________________________
00239 inline int TFileIter::operator==(const char *name) const
00240 { return name ? !strcmp(name,GetKeyName()):0;}
00241 
00242 //__________________________________________________________________________
00243 inline int TFileIter::operator!=(const char *name) const
00244 { return !(operator==(name)); }
00245 
00246 //__________________________________________________________________________
00247 inline TObject *TFileIter::operator()(){ return Next(); }
00248 //__________________________________________________________________________
00249 inline TObject *TFileIter::operator()(Int_t  nSkip){ return Next(nSkip);}
00250 
00251 //__________________________________________________________________________
00252 inline void  TFileIter::SaveFileScope()
00253 { fFileBackUp = gFile; fDirectoryBackUp = gDirectory; }
00254 
00255 //__________________________________________________________________________
00256 inline void TFileIter::RestoreFileScope()
00257 {  gFile = fFileBackUp; gDirectory = fDirectoryBackUp; }
00258 
00259 #endif

Generated on Tue Jul 5 14:26:49 2011 for ROOT_528-00b_version by  doxygen 1.5.1