Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
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
10#include "JROOT/JCounter.hh"
11
12
13/**
14 * \author mdejong
15 */
16
17namespace JROOT {}
18namespace JPP { using namespace JROOT; }
19
20namespace 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>
141 {
142 public:
143 /**
144 * Default constructor.
145 */
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 file->Close();
182
183 delete file;
184 }
185 }
186
187 this->rewind();
188 }
189
190
191 /**
192 * Close file.
193 */
194 virtual void close() override
195 {
196 if (this->is_valid()) {
197
198 TFile* file = this->get()->GetCurrentFile();
199
200 if (file != NULL) {
201
202 file->Close();
203
204 delete file;
205 }
206 }
207
208 this->reset();
209 }
210 };
211}
212
213#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 void reset() override
Reset pointer.
Definition JPointer.hh:84
virtual JClass_t * get() const override
Get pointer.
Definition JPointer.hh:64
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 const pointer_type & next() override
Get next element.
virtual skip_type skip(const skip_type ns) override
Skip items.
virtual bool hasNext() override
Check availability of next element.
virtual void rewind() override
Rewind.
JRewindableObjectIterator< T >::pointer_type pointer_type
counter_type getCounter() const
Get internal counter.
Auxiliary class for template TTree reading.
virtual bool load(TFile *file) override
Load TTree from given file.
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.
T * getAddress() const
Get address.