Logo ROOT   6.12/07
Reference Guide
TFileIter.h
Go to the documentation of this file.
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 
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 #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; //! The inner TFidrectory interator;
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);
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);
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 //__________________________________________________________________________
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 //__________________________________________________________________________
155 { return fNestedIterator ? fNestedIterator->GetTDirectory() : fRootFile; }
156 
157 //__________________________________________________________________________
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 //__________________________________________________________________________
204 { SkipObjects(shift); return *this;}
205 //__________________________________________________________________________
207 { return operator+=(-shift);}
208 //__________________________________________________________________________
210 { SkipObjects( 1); return *this;}
211 //__________________________________________________________________________
213 { SkipObjects(-1); return *this;}
214 //__________________________________________________________________________
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
UInt_t fRunNumber
Definition: TFileIter.h:65
TFileIter * fNestedIterator
Definition: TFileIter.h:57
virtual ~TFileIter()
TFileIter dtor.
Definition: TFileIter.cxx:196
virtual Bool_t IsOpen() const
Check whether the associated ROOT TFile was open and TFile object is healthy.
Definition: TFileIter.cxx:226
UInt_t fEventNumber
Definition: TFileIter.h:66
virtual TIterator & operator=(const TIterator &)
The inner TFidrectory interator;.
Definition: TFileIter.h:59
static const char * GetDefaultMapFileName()
Definition: TFileIter.h:138
TObject * ReadObj(const TKey *key) const
Read the next TObject from for the TDirectory by TKey provided.
Definition: TFileIter.cxx:495
Bool_t fOwnTFile
Definition: TFileIter.h:68
const char Option_t
Definition: RtypesCore.h:62
virtual const TFile * GetTFile() const
Definition: TFileIter.h:152
Int_t fCursorPosition
Definition: TFileIter.h:67
TObject * operator()()
Definition: TFileIter.h:235
virtual TObject * GetObject() const
read the object from TFile defined by the current TKey
Definition: TFileIter.cxx:273
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
void SetCursorPosition(Int_t cursorPosition)
Definition: TFileIter.h:172
Basic string class.
Definition: TString.h:125
static const char * GetLocalFileNameKey()
Definition: TFileIter.h:140
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
static const char * GetResourceName()
Definition: TFileIter.h:136
Iterator abstract base class.
Definition: TIterator.h:30
Iterator of linked list.
Definition: TList.h:197
const char * GetKeyName() const
return the name of the current TKey
Definition: TFileIter.cxx:256
#define ClassDef(name, id)
Definition: Rtypes.h:320
TDirectory * fRootFile
Definition: TFileIter.h:63
TString fEventName
Definition: TFileIter.h:64
virtual TObject * NextEventGet(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*")
reads, creates and returns the object by TKey name that matches the "name" .
Definition: TFileIter.cxx:487
virtual Int_t TotalKeys() const
The total number of the TKey keys in the current TDirectory only Usually this means the total number ...
Definition: TFileIter.cxx:294
TKey * GetCurrentKey() const
return the pointer to the current TKey
Definition: TFileIter.cxx:240
Int_t GetObjlen() const
Returns the uncompressed length of the current object.
Definition: TFileIter.cxx:280
static TString MapName(const char *name, const char *localSystemKey=0, const char *mountedFileSystemKey=0)
MapName(const char *name, const char *localSystemKey,const char *mountedFileSystemKey) Substitute th...
Definition: TFileIter.cxx:547
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
TFileIter & operator++()
Definition: TFileIter.h:209
virtual const TDirectory * GetTDirectory() const
Definition: TFileIter.h:154
virtual TFile * GetFile() const
Definition: TDirectory.h:147
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
A doubly linked list.
Definition: TList.h:44
TFileIter & operator--()
Definition: TFileIter.h:212
virtual TObject * Next()
Return next object in the list. Returns 0 when no more objects in list.
Definition: TFileIter.h:158
void Initialize()
to be documented
Definition: TFileIter.cxx:211
int operator==(const char *name) const
Definition: TFileIter.h:227
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void Reset()
Reset the status of the iterator.
Definition: TFileIter.cxx:356
virtual Bool_t operator!=(const TIterator &it) const
This operator compares two TIterator objects.
Definition: TFileIter.h:60
static const char * GetForeignFileSystemKey()
Definition: TFileIter.h:142
Int_t CurrentCursorPosition() const
Definition: TFileIter.h:145
virtual void Rewind()
Definition: TFileIter.h:166
Describe directory structure in memory.
Definition: TDirectory.h:34
virtual TKey * SkipObjects(Int_t nSkip=1)
Returns the TKey pointer to the nSkip TKey object from the current one nSkip = 0; the state of the it...
Definition: TFileIter.cxx:401
TFileIter & operator+=(Int_t shift)
Definition: TFileIter.h:203
Mother of all ROOT objects.
Definition: TObject.h:37
TFileIter & operator-=(Int_t shift)
Definition: TFileIter.h:206
Definition: file.py:1
TFileIter(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Open ROOT TFile by the name provided; This TFile is to be deleted by the TFileIter alone...
Definition: TFileIter.cxx:149
TObject * operator*() const
Return current object or nullptr.
Definition: TFileIter.h:215
Bool_t operator!=(const TIterator &aIter) const
This operator compares two TIterator objects.
Definition: TList.cxx:1166
virtual Int_t GetDepth() const
return the current number of the nested subdirectroies; = 0 - means there is no subdirectories ...
Definition: TFileIter.cxx:248
static void PurgeKeys(TList *listOfKeys)
Remove the TKey duplication, leave the keys with highest cycle number only Sort if first...
Definition: TFileIter.cxx:316
char name[80]
Definition: TGX11.cxx:109
TKey * NextEventKey(UInt_t eventNumber=UInt_t(-1), UInt_t runNumber=UInt_t(-1), const char *name="*")
Definition: TFileIter.cxx:443