1 // @(#)root/table:$Name: $:$Id: StFileIter.h,v 1.7 2012/06/11 15:08:41 fisyak Exp $ 2 // Author: Valery Fine(fine@bnl.gov) 01/03/2001 3 4 /************************************************************************* 5 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. * 6 * Copyright (C) 2001 [BNL] Brookhaven National Laboratory. * 7 * All rights reserved. * 8 * * 9 * For the licensing terms see $ROOTSYS/LICENSE. * 10 * For the list of contributors see $ROOTSYS/README/CREDITS. * 11 *************************************************************************/ 12 13 #ifndef ROOT_StFileIter 14 #define ROOT_StFileIter 15 16 /////////////////////////////////////////////////////////////////////////// 17 // // 18 // Class to iterate (read / write ) the events written to /from TFile. // 19 // // 20 // - set the current internal cursor directly by different means // 21 // - set the current cursor to the "next" position if available // 22 // - gain extra information of the TKey object at the current position // 23 // - read TObject object from the TFile defined by TKey at the current // 24 // position // 25 // // 26 // - Read "next" object from the file // 27 // - n-th object from the file // 28 // - object that is in n object on the file // 29 // - read current object // 30 // - return the name of the key of the current object // 31 // - return the current position // 32 // - set the current position by the absolute position number // 33 // - set the current position by relative position number // 34 // - get the number of keys in the file // 35 // // 36 // The event is supposed to assign an unique ID in form of // 37 // // 38 // TKey name ::= event Id ::= eventName "." run_number "." event_number // 39 // // 40 // // 41 // and stored as the TKey name of the object written // 42 // // 43 // author Valeri Fine // 44 // // 45 /////////////////////////////////////////////////////////////////////////// 46 47 #ifndef ROOT_TString 48 #include "TString.h" 49 #endif 50 #ifndef ROOT_TIterator 51 #include "TIterator.h" 52 #endif 53 #ifndef ROOT_TList 54 #include "TList.h" 55 #endif 56 #ifndef ROOT_TFile 57 #include "TFile.h" 58 #endif 59 60 61 class StFileIter : public TListIter { 62 63 private: 64 65 TFile *fFileBackUp; //! temporary data-members 66 TDirectory *fDirectoryBackUp; //! to save/restore TFile/TDirectory global scope 67 StFileIter *fNestedIterator; //! The inner TFidrectory interator; 68 69 bool operator!=(const TIterator&) const{return true;} // silence the hidden warning 70 TIterator& operator=(const TIterator&){ return *this;} // silence the hidden warning 71 72 protected: 73 TDirectory *fRootFile; // TDirectory/TFile to be iterated over 74 TString fEventName; // current key name 75 UInt_t fRunNumber; // current "run number" 76 UInt_t fEventNumber; // current "event number" 77 Int_t fCursorPosition; // the position of the current key in the sorted TKey list 78 Bool_t fOwnTFile; // Bit whether this classs creates TFile on its own to delete 79 80 void Initialize(); 81 TObject *ReadObj(const TKey *key) const; 82 void RestoreFileScope(); 83 void SaveFileScope(); 84 85 public: 86 87 StFileIter(const char *name, Option_t *option = "", 88 const char *ftitle = "", Int_t compress = 1, 89 Int_t netopt = 0); 90 StFileIter(TFile *file=0); 91 StFileIter(TDirectory *directory); 92 StFileIter(const StFileIter &); 93 virtual ~StFileIter(); 94 // --- draft !!! virtual Int_t Copy(TFile *destFile); 95 Int_t CurrentCursorPosition() const; 96 virtual const TFile *GetTFile() const; 97 virtual const TDirectory *GetTDirectory() const; 98 static TString MapName(const char *name, const char *localSystemKey = 0 99 , const char *mountedFileSystemKey = 0); 100 static const char *GetResourceName(); 101 static const char *GetDefaultMapFileName(); 102 static const char *GetLocalFileNameKey(); 103 static const char *GetForeignFileSystemKey(); 104 static void PurgeKeys(TList *listOfKeys); 105 virtual Bool_t IsOpen() const; 106 virtual TObject *NextEventGet(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*"); 107 virtual Int_t NextEventPut(TObject *obj, UInt_t eventNum, UInt_t runNumber, const char *name=0); 108 TKey *NextEventKey(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*"); 109 void SetCursorPosition(Int_t cursorPosition); 110 void SetCursorPosition(const char *keyNameToFind); 111 Int_t GetObjlen() const; 112 virtual Int_t TotalKeys() const; 113 virtual TKey *SkipObjects(Int_t nSkip=1); 114 virtual TObject *GetObject() const; 115 virtual Int_t GetDepth() const; 116 117 TKey *GetCurrentKey() const; 118 const char *GetKeyName() const; 119 120 StFileIter &operator=(Int_t cursorPosition); 121 StFileIter &operator=(const char *keyNameToFind); 122 StFileIter &operator+=(Int_t shift); 123 StFileIter &operator-=(Int_t shift); 124 StFileIter &operator++(); 125 StFileIter &operator--(); 126 127 TObject *operator*() const; 128 operator const char *() const; 129 operator const TFile *() const; 130 operator const TDirectory *() const; 131 operator int () const; 132 int operator==(const char *name) const; 133 int operator!=(const char *name) const; 134 135 public: // abstract TIterator methods implementations: 136 137 virtual TObject *Next(); 138 virtual TObject *Next(Int_t nSkip); 139 virtual void Reset(); 140 virtual void Rewind(); 141 TObject *operator()(Int_t nSkip); 142 TObject *operator()(); 143 144 ClassDef(StFileIter,0) // TFile class iterator 145 }; 146 147 //__________________________________________________________________________ 148 inline const char *StFileIter::GetResourceName() {return "ForeignFileMap";} 149 //__________________________________________________________________________ 150 inline const char *StFileIter::GetDefaultMapFileName() {return "io.config";} 151 //__________________________________________________________________________ 152 inline const char *StFileIter::GetLocalFileNameKey() {return "LocalFileSystem";} 153 //__________________________________________________________________________ 154 inline const char *StFileIter::GetForeignFileSystemKey(){return "MountedFileSystem";} 155 156 //__________________________________________________________________________ 157 inline Int_t StFileIter::CurrentCursorPosition() const 158 { 159 // return the current 160 return fNestedIterator ? fNestedIterator->CurrentCursorPosition() : fCursorPosition; 161 } 162 163 //__________________________________________________________________________ 164 inline const TFile *StFileIter::GetTFile() const { return GetTDirectory()->GetFile(); } 165 //__________________________________________________________________________ 166 inline const TDirectory *StFileIter::GetTDirectory() const 167 { return fNestedIterator ? fNestedIterator->GetTDirectory() : fRootFile; } 168 169 //__________________________________________________________________________ 170 inline TObject *StFileIter::Next() 171 { 172 // Make 1 step over the file objects and returns its pointer 173 // or 0, if there is no object left in the container 174 return Next(1); 175 } 176 177 //__________________________________________________________________________ 178 inline void StFileIter::Rewind() 179 { 180 // Alias ffor Reset method 181 Reset(); 182 } 183 //__________________________________________________________________________ 184 inline void StFileIter::SetCursorPosition(Int_t cursorPosition) 185 { 186 // Make <cursorPosition> steps (>0 - forward) over the file 187 // objects to skip it 188 if (fNestedIterator) 189 fNestedIterator->SetCursorPosition(cursorPosition); 190 else 191 SkipObjects(cursorPosition - fCursorPosition); 192 } 193 194 //__________________________________________________________________________ 195 inline StFileIter &StFileIter::operator=(const char *keyNameToFind) 196 { 197 // Iterate unless the name of the object matches <keyNameToFind> 198 SetCursorPosition(keyNameToFind); return *this;} 199 200 //__________________________________________________________________________ 201 inline StFileIter &StFileIter::operator=(Int_t cursorPosition) 202 { 203 // Iterate over <cursorPosition> 204 SetCursorPosition(cursorPosition); 205 return *this; 206 } 207 //__________________________________________________________________________ 208 inline StFileIter::operator const TDirectory *() const 209 { return GetTDirectory(); } 210 211 //__________________________________________________________________________ 212 inline StFileIter::operator const TFile *() const 213 { return GetTFile (); } 214 //__________________________________________________________________________ 215 inline StFileIter &StFileIter::operator+=(Int_t shift) 216 { SkipObjects(shift); return *this;} 217 //__________________________________________________________________________ 218 inline StFileIter &StFileIter::operator-=(Int_t shift) 219 { return operator+=(-shift);} 220 //__________________________________________________________________________ 221 inline StFileIter &StFileIter::operator++() 222 { SkipObjects( 1); return *this;} 223 //__________________________________________________________________________ 224 inline StFileIter &StFileIter::operator--() 225 { SkipObjects(-1); return *this;} 226 //__________________________________________________________________________ 227 inline TObject *StFileIter::operator*() const 228 { return GetObject();} 229 //__________________________________________________________________________ 230 inline StFileIter::operator int () const 231 { return CurrentCursorPosition(); } 232 //__________________________________________________________________________ 233 inline StFileIter::operator const char *() const 234 { 235 // return the current key name 236 return GetKeyName(); 237 } 238 //__________________________________________________________________________ 239 inline int StFileIter::operator==(const char *name) const 240 { return name ? !strcmp(name,GetKeyName()):0;} 241 242 //__________________________________________________________________________ 243 inline int StFileIter::operator!=(const char *name) const 244 { return !(operator==(name)); } 245 246 //__________________________________________________________________________ 247 inline TObject *StFileIter::operator()(){ return Next(); } 248 //__________________________________________________________________________ 249 inline TObject *StFileIter::operator()(Int_t nSkip){ return Next(nSkip);} 250 251 //__________________________________________________________________________ 252 inline void StFileIter::SaveFileScope() 253 { fFileBackUp = gFile; fDirectoryBackUp = gDirectory; } 254 255 //__________________________________________________________________________ 256 inline void StFileIter::RestoreFileScope() 257 {//YF gFile = fFileBackUp; 258 gDirectory = fDirectoryBackUp; } 259 260 #endif 261