Jpp  16.0.3
the software that should make you happy
 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 
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  this->load(TFile::Open(file_name));
176  this->rewind();
177  }
178 
179 
180  /**
181  * Close file.
182  */
183  virtual void close() override
184  {
185  if (this->is_valid()) {
186 
187  const TFile* file = this->get()->GetCurrentFile();
188 
189  if (file != NULL) {
190  delete file;
191  }
192  }
193 
194  this->reset();
195  }
196  };
197 }
198 
199 #endif
virtual bool is_open() const override
Check is file is open.
Interface for object iteration with rewinding.
then set_variable PMT_FILE set_variable DAQ_FILE set_variable OUTPUT_FILE set_variable DETECTOR else fatal Wrong number of arguments fi set_variable RUNBYRUN file
JTreeReaderObjectIterator()
Default constructor.
virtual const pointer_type & next() override
Get next element.
unsigned int skip_type
Type definition for number of objects to skip.
virtual void open(const char *file_name) override
Open file.
Type definition for counter for ROOT TTree and auxiliary methods.
Long64_t counter_type
Type definition for counter.
TTree reading for template data type.
bool is_valid() const
Check validity of pointer.
const JTreeParameters & getTreeParameters() const
Get TTree parameters.
virtual skip_type skip(const skip_type ns) override
Skip items.
counter_type getCounter() const
Get internal counter.
virtual void rewind() override
Rewind.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
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() override
Check availability of next element.
virtual bool load(TFile *file) override
Load TTree from given file.
Definition: JTreeReader.hh:80
virtual void reset() override
Reset pointer.
Definition: JPointer.hh:84