StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TFileIter.h
1 // @(#)root/table:$Id$
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_TFileIter
14 #define ROOT_TFileIter
15 
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 // //
46 
47 #include "TString.h"
48 #include "TIterator.h"
49 #include "TList.h"
50 #include "TFile.h"
51 
52 
53 class TFileIter : public TListIter {
54 
55 private:
56 
57  TFileIter *fNestedIterator;
58 
59  virtual TIterator &operator=(const TIterator &) { return *this; }
60  virtual Bool_t operator!=(const TIterator &it) const { return TListIter::operator!=(it);}
61 
62 protected:
63  TDirectory *fRootFile; // TDirectory/TFile to be iterated over
64  TString fEventName; // current key name
65  UInt_t fRunNumber; // current "run number"
66  UInt_t fEventNumber; // current "event number"
67  Int_t fCursorPosition; // the position of the current key in the sorted TKey list
68  Bool_t fOwnTFile; // Bit whether this classs creates TFile on its own to delete
69 
70  void Initialize();
71  TObject *ReadObj(const TKey *key) const;
72  TKey *NextEventKey(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*");
73 
74 public:
75 
76  TFileIter(const char *name, Option_t *option = "",
77  const char *ftitle = "", Int_t compress = 1,
78  Int_t netopt = 0);
79  TFileIter(TFile *file=0);
80  TFileIter(TDirectory *directory);
81  TFileIter(const TFileIter &);
82  virtual ~TFileIter();
83 // --- draft !!! virtual Int_t Copy(TFile *destFile);
84  Int_t CurrentCursorPosition() const;
85  virtual const TFile *GetTFile() const;
86  virtual const TDirectory *GetTDirectory() const;
87  static TString MapName(const char *name, const char *localSystemKey = 0
88  , const char *mountedFileSystemKey = 0);
89  static const char *GetResourceName();
90  static const char *GetDefaultMapFileName();
91  static const char *GetLocalFileNameKey();
92  static const char *GetForeignFileSystemKey();
93  static void PurgeKeys(TList *listOfKeys);
94  virtual Bool_t IsOpen() const;
95  virtual TObject *NextEventGet(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*");
96  virtual Int_t NextEventPut(TObject *obj, UInt_t eventNum, UInt_t runNumber, const char *name=0);
97  void SetCursorPosition(Int_t cursorPosition);
98  void SetCursorPosition(const char *keyNameToFind);
99  Int_t GetObjlen() const;
100  virtual Int_t TotalKeys() const;
101  virtual TKey *SkipObjects(Int_t nSkip=1);
102  virtual TObject *GetObject() const;
103  virtual Int_t GetDepth() const;
104 
105  TKey *GetCurrentKey() const;
106  const char *GetKeyName() const;
107 
108  TFileIter &operator=(Int_t cursorPosition);
109  TFileIter &operator=(const char *keyNameToFind);
110  TFileIter &operator+=(Int_t shift);
111  TFileIter &operator-=(Int_t shift);
112  TFileIter &operator++();
113  TFileIter &operator--();
114 
115  TObject *operator*() const;
116  operator const char *() const;
117  operator const TFile *() const;
118  operator const TDirectory *() const;
119  operator int () const;
120  int operator==(const char *name) const;
121  int operator!=(const char *name) const;
122 
123 public: // abstract TIterator methods implementations:
124 
125  virtual TObject *Next();
126  virtual TObject *Next(Int_t nSkip);
127  virtual void Reset();
128  virtual void Rewind();
129  TObject *operator()(Int_t nSkip);
130  TObject *operator()();
131 
132  ClassDef(TFileIter,0) // TFile class iterator
133 };
134 
135 //__________________________________________________________________________
136 inline const char *TFileIter::GetResourceName() {return "ForeignFileMap";}
137 //__________________________________________________________________________
138 inline const char *TFileIter::GetDefaultMapFileName() {return "io.config";}
139 //__________________________________________________________________________
140 inline const char *TFileIter::GetLocalFileNameKey() {return "LocalFileSystem";}
141 //__________________________________________________________________________
142 inline const char *TFileIter::GetForeignFileSystemKey(){return "MountedFileSystem";}
143 
144 //__________________________________________________________________________
145 inline Int_t TFileIter::CurrentCursorPosition() const
146 {
147  // return the current
148  return fNestedIterator ? fNestedIterator->CurrentCursorPosition() : fCursorPosition;
149 }
150 
151 //__________________________________________________________________________
152 inline const TFile *TFileIter::GetTFile() const { return GetTDirectory()->GetFile(); }
153 //__________________________________________________________________________
154 inline const TDirectory *TFileIter::GetTDirectory() const
155 { return fNestedIterator ? fNestedIterator->GetTDirectory() : fRootFile; }
156 
157 //__________________________________________________________________________
158 inline TObject *TFileIter::Next()
159 {
160  // Make 1 step over the file objects and returns its pointer
161  // or 0, if there is no object left in the container
162  return Next(1);
163 }
164 
165 //__________________________________________________________________________
166 inline void TFileIter::Rewind()
167 {
168  // Alias for "Reset" method
169  Reset();
170 }
171 //__________________________________________________________________________
172 inline void TFileIter::SetCursorPosition(Int_t cursorPosition)
173 {
174  // Make <cursorPosition> steps (>0 - forward) over the file
175  // objects to skip it
176  if (fNestedIterator)
177  fNestedIterator->SetCursorPosition(cursorPosition);
178  else
179  SkipObjects(cursorPosition - fCursorPosition);
180 }
181 
182 //__________________________________________________________________________
183 inline TFileIter &TFileIter::operator=(const char *keyNameToFind)
184 {
185  // Iterate unless the name of the object matches <keyNameToFind>
186  SetCursorPosition(keyNameToFind); return *this;}
187 
188 //__________________________________________________________________________
189 inline TFileIter &TFileIter::operator=(Int_t cursorPosition)
190 {
191  // Iterate over <cursorPosition>
192  SetCursorPosition(cursorPosition);
193  return *this;
194 }
195 //__________________________________________________________________________
196 inline TFileIter::operator const TDirectory *() const
197 { return GetTDirectory(); }
198 
199 //__________________________________________________________________________
200 inline TFileIter::operator const TFile *() const
201 { return GetTFile (); }
202 //__________________________________________________________________________
203 inline TFileIter &TFileIter::operator+=(Int_t shift)
204 { SkipObjects(shift); return *this;}
205 //__________________________________________________________________________
206 inline TFileIter &TFileIter::operator-=(Int_t shift)
207 { return operator+=(-shift);}
208 //__________________________________________________________________________
209 inline TFileIter &TFileIter::operator++()
210 { SkipObjects( 1); return *this;}
211 //__________________________________________________________________________
212 inline TFileIter &TFileIter::operator--()
213 { SkipObjects(-1); return *this;}
214 //__________________________________________________________________________
215 inline TObject *TFileIter::operator*() const
216 { return GetObject();}
217 //__________________________________________________________________________
218 inline TFileIter::operator int () const
219 { return CurrentCursorPosition(); }
220 //__________________________________________________________________________
221 inline TFileIter::operator const char *() const
222 {
223  // return the current key name
224  return GetKeyName();
225 }
226 //__________________________________________________________________________
227 inline int TFileIter::operator==(const char *name) const
228 { return name ? !strcmp(name,GetKeyName()):0;}
229 
230 //__________________________________________________________________________
231 inline int TFileIter::operator!=(const char *name) const
232 { return !(operator==(name)); }
233 
234 //__________________________________________________________________________
235 inline TObject *TFileIter::operator()(){ return Next(); }
236 //__________________________________________________________________________
237 inline TObject *TFileIter::operator()(Int_t nSkip){ return Next(nSkip);}
238 
239 #endif
virtual ~TFileIter()
TFileIter dtor.
Definition: TFileIter.cxx:196
virtual Int_t GetDepth() const
Definition: TFileIter.cxx:248
const char * GetKeyName() const
return the name of the current TKey
Definition: TFileIter.cxx:256
TKey * GetCurrentKey() const
return the pointer to the current TKey
Definition: TFileIter.cxx:240
TObject * ReadObj(const TKey *key) const
Read the next TObject from for the TDirectory by TKey provided.
Definition: TFileIter.cxx:495
virtual TObject * NextEventGet(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*")
Definition: TFileIter.cxx:487
static TString MapName(const char *name, const char *localSystemKey=0, const char *mountedFileSystemKey=0)
Definition: TFileIter.cxx:547
virtual Int_t NextEventPut(TObject *obj, UInt_t eventNum, UInt_t runNumber, const char *name=0)
Create a special TKey name with obj provided and write it out.
Definition: TFileIter.cxx:515
virtual TObject * GetObject() const
Definition: TFileIter.cxx:273
void Initialize()
to be documented
Definition: TFileIter.cxx:211
virtual Bool_t IsOpen() const
Definition: TFileIter.cxx:226
virtual Int_t TotalKeys() const
Definition: TFileIter.cxx:294
virtual void Reset()
Reset the status of the iterator.
Definition: TFileIter.cxx:356
virtual TKey * SkipObjects(Int_t nSkip=1)
Definition: TFileIter.cxx:401
TFileIter(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Definition: TFileIter.cxx:149
static void PurgeKeys(TList *listOfKeys)
Definition: TFileIter.cxx:316
Int_t GetObjlen() const
Returns the uncompressed length of the current object.
Definition: TFileIter.cxx:280