Jpp  master_rocky-43-ge265d140c
the software that should make you happy
JTreeReaderObjectIterator.hh
Go to the documentation of this file.
1 #ifndef __JROOT__JTREEREADEROBJECTITERATOR__
2 #define __JROOT__JTREEREADEROBJECTITERATOR__
3 
4 #include "TFile.h"
5 #include "TError.h"
6 
8 #include "JROOT/JTreeReader.hh"
10 #include "JROOT/JCounter.hh"
11 
12 
13 /**
14  * \author mdejong
15  */
16 
17 namespace JROOT {}
18 namespace JPP { using namespace JROOT; }
19 
20 namespace JROOT {
21 
24  using JLANG::skip_type;
25 
26 
27  /**
28  * JTreeReader object iterator.
29  *
30  * This class implements the JLANG::JRewindableObjectIterator interface.
31  */
32  template<class T>
34  public virtual JRewindableObjectIterator<T>,
35  public JTreeReader<T>
36  {
37  public:
38 
40 
41 
42  /**
43  * Default constructor.
44  */
47  counter(0)
48  {
49  gErrorIgnoreLevel = kError;
50  }
51 
52 
53  /**
54  * Rewind.
55  */
56  virtual void rewind() override
57  {
58  counter = 0;
59  }
60 
61 
62  /**
63  * Check availability of next element.
64  *
65  * \return true if the iteration has more elements.
66  */
67  virtual bool hasNext() override
68  {
69  if (this->is_valid())
70  return counter < this->get()->GetEntries();
71  else
72  return false;
73  }
74 
75 
76  /**
77  * Get next element.
78  *
79  * \return pointer to element.
80  */
81  virtual const pointer_type& next() override
82  {
83  static pointer_type ps;
84 
85  if (hasNext()) {
86 
87  this->get()->GetEvent(counter++);
88 
89  ps.reset(this->getAddress());
90 
91  } else {
92 
93  ps.reset(NULL);
94  }
95 
96  return ps;
97  }
98 
99 
100  /**
101  * Skip items.
102  *
103  * \param ns number of items to skip
104  * \return number of items skipped
105  */
106  virtual skip_type skip(const skip_type ns) override
107  {
108  if (this->is_valid()) {
109  return advance(counter, ns, this->get()->GetEntries());
110  }
111 
112  return 0;
113  }
114 
115 
116  /**
117  * Get internal counter.
118  *
119  * \return counter
120  */
122  {
123  return counter;
124  }
125 
126  protected:
128  };
129 
130 
131  /**
132  * JTemplateTreeReader object iterator.
133  *
134  * This class extends the JTreeReaderObjectIterator class and
135  * implements the JLANG::JAccessibleObjectIterator interface.
136  */
137  template<class T>
139  public JAccessibleObjectIterator<T>,
140  public JTreeReaderObjectIterator<T>
141  {
142  public:
143  /**
144  * Default constructor.
145  */
147  {}
148 
149 
150  /**
151  * Check is file is open.
152  *
153  * \return true if open; else false
154  */
155  virtual bool is_open() const override
156  {
157  if (this->is_valid()) {
158 
159  const TFile* file = this->get()->GetCurrentFile();
160 
161  return (file != NULL && file->IsOpen());
162  }
163 
164  return false;
165  }
166 
167 
168  /**
169  * Open file.
170  *
171  * \param file_name file name
172  */
173  virtual void open(const char* file_name) override
174  {
175  TFile* file = TFile::Open(file_name);
176 
177  if (file != NULL) {
178 
179  if (!this->load(file)) {
180 
181  if (this->is_valid()) {
182  this->get()->SetDirectory(0);
183  }
184 
185  file->Close();
186 
187  delete file;
188  }
189  }
190 
191  this->rewind();
192  }
193 
194 
195  /**
196  * Close file.
197  */
198  virtual void close() override
199  {
200  if (this->is_valid()) {
201 
202  TFile* file = this->get()->GetCurrentFile();
203 
204  if (file != NULL) {
205 
206  if (this->is_valid()) {
207  this->get()->SetDirectory(0);
208  }
209 
210  file->Close();
211 
212  delete file;
213  }
214  }
215 
216  this->reset();
217  }
218  };
219 }
220 
221 #endif
Type definition for counter for ROOT TTree and auxiliary methods.
TTree reading for template data type.
bool is_valid() const
Check validity of pointer.
Interface for object iteration with named access.
virtual JClass_t * get() const override
Get pointer.
Definition: JPointer.hh:64
virtual void reset() override
Reset pointer.
Definition: JPointer.hh:84
Interface for object iteration with rewinding.
const JTreeParameters & getTreeParameters() const
Get TTree parameters.
virtual void open(const char *file_name) override
Open file.
virtual bool is_open() const override
Check is file is open.
virtual skip_type skip(const skip_type ns) override
Skip items.
virtual bool hasNext() override
Check availability of next element.
JRewindableObjectIterator< T >::pointer_type pointer_type
virtual void rewind() override
Rewind.
virtual const pointer_type & next() override
Get next element.
counter_type getCounter() const
Get internal counter.
Auxiliary class for template TTree reading.
Definition: JTreeReader.hh:61
virtual bool load(TFile *file) override
Load TTree from given file.
Definition: JTreeReader.hh:83
unsigned int skip_type
Type definition for number of objects to skip.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
Long64_t counter_type
Type definition for counter.
std::vector< size_t > ns
T * getAddress() const
Get address.
Definition: JRootAddress.hh:62