Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTreeReaderObjectIterator.hh
Go to the documentation of this file.
1 #ifndef __JROOT__JTREEREADEROBJECTITERATOR__
2 #define __JROOT__JTREEREADEROBJECTITERATOR__
3 
5 #include "JROOT/JTreeReader.hh"
7 #include "JROOT/JCounter.hh"
8 
9 
10 /**
11  * \author mdejong
12  */
13 
14 namespace JROOT {}
15 namespace JPP { using namespace JROOT; }
16 
17 namespace JROOT {
18 
21  using JLANG::skip_type;
22 
23 
24  /**
25  * JTreeReader object iterator.
26  *
27  * This class implements the JLANG::JRewindableObjectIterator interface.
28  */
29  template<class T>
31  public virtual JRewindableObjectIterator<T>,
32  public JTreeReader<T>
33  {
34  public:
35 
37 
38 
39  /**
40  * Default constructor.
41  */
44  counter(0)
45  {}
46 
47 
48  /**
49  * Rewind.
50  */
51  virtual void rewind()
52  {
53  counter = 0;
54  }
55 
56 
57  /**
58  * Check availability of next element.
59  *
60  * \return true if the iteration has more elements.
61  */
62  virtual bool hasNext()
63  {
64  if (this->is_valid())
65  return counter < this->get()->GetEntries();
66  else
67  return false;
68  }
69 
70 
71  /**
72  * Get next element.
73  *
74  * \return pointer to element.
75  */
76  virtual const pointer_type& next()
77  {
78  static pointer_type ps;
79 
80  if (hasNext()) {
81 
82  this->get()->GetEvent(counter++);
83 
84  ps.reset(this->getAddress());
85 
86  } else {
87 
88  ps.reset(NULL);
89  }
90 
91  return ps;
92  }
93 
94 
95  /**
96  * Skip items.
97  *
98  * \param ns number of items to skip
99  * \return number of items skipped
100  */
101  virtual skip_type skip(const skip_type ns)
102  {
103  if (this->is_valid()) {
104  return advance(counter, ns, this->get()->GetEntries());
105  }
106 
107  return 0;
108  }
109 
110 
111  /**
112  * Get internal counter.
113  *
114  * \return counter
115  */
117  {
118  return counter;
119  }
120 
121  protected:
123  };
124 
125 
126  /**
127  * JTemplateTreeReader object iterator.
128  *
129  * This class extends the JTreeReaderObjectIterator class and
130  * implements the JLANG::JAccessibleObjectIterator interface.
131  */
132  template<class T>
134  public JAccessibleObjectIterator<T>,
135  public JTreeReaderObjectIterator<T>
136  {
137  public:
138  /**
139  * Default constructor.
140  */
142  {}
143 
144 
145  /**
146  * Check is file is open.
147  *
148  * \return true if open; else false
149  */
150  virtual bool is_open() const
151  {
152  if (this->is_valid()) {
153 
154  const TFile* file = this->get()->GetCurrentFile();
155 
156  return (file != NULL && file->IsOpen());
157  }
158 
159  return false;
160  }
161 
162 
163  /**
164  * Open file.
165  *
166  * \param file_name file name
167  */
168  virtual void open(const char* file_name)
169  {
170  this->load(TFile::Open(file_name));
171  this->rewind();
172  }
173 
174 
175  /**
176  * Close file.
177  */
178  virtual void close()
179  {
180  if (this->is_valid()) {
181 
182  const TFile* file = this->get()->GetCurrentFile();
183 
184  if (file != NULL) {
185  delete file;
186  }
187  }
188 
189  this->reset();
190  }
191  };
192 }
193 
194 #endif
virtual void open(const char *file_name)
Open file.
Interface for object iteration with rewinding.
JTreeReaderObjectIterator()
Default constructor.
unsigned int skip_type
Type definition for number of objects to skip.
virtual skip_type skip(const skip_type ns)
Skip items.
Long64_t counter_type
Type definition for counter.
Definition: JCounter.hh:24
TTree reading for template data type.
bool is_valid() const
Check validity of pointer.
const JTreeParameters & getTreeParameters() const
Get TTree parameters.
virtual void reset()
Reset pointer.
Definition: JPointer.hh:84
counter_type getCounter() const
Get internal counter.
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
Definition: JCounter.hh:35
Auxiliary class for template TTree reading.
Definition: JTreeReader.hh:54
Interface for object iteration with named access.
JRewindableObjectIterator< T >::pointer_type pointer_type
T * getAddress() const
Get address.
Definition: JRootAddress.hh:58
virtual bool hasNext()
Check availability of next element.
Type definition for counter for ROOT TTree and auxiliary methods.
virtual bool is_open() const
Check is file is open.
virtual const pointer_type & next()
Get next element.
virtual bool load(TFile *file)
Load TTree from given file.
Definition: JTreeReader.hh:80