Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JRootFileReader.hh
Go to the documentation of this file.
1#ifndef __JROOTFILEREADER__
2#define __JROOTFILEREADER__
3
4#include <string>
5#include <limits>
6
7#include "TFile.h"
8#include "TString.h"
9
11#include "JLang/JException.hh"
12#include "JLang/JType.hh"
15#include "JROOT/JRootFile.hh"
17
18
19/**
20 * \author mdejong
21 */
22
23namespace JROOT {}
24namespace JPP { using namespace JROOT; }
25
26namespace JROOT {
27
30 using JLANG::JType;
31 using JLANG::skip_type;
32
33
34 /**
35 * Read object from ROOT file.
36 *
37 * \param file pointer to file
38 * \param key key of object to be read
39 * \param ps pointer to object
40 */
41 template<class T>
42 inline void getObject(TFile* file, const TString& key, T*& ps)
43 {
44 if (file != NULL) {
45 file->GetObject(key, ps);
46 }
47 }
48
49
50 /**
51 * ROOT file reader.
52 */
53 template<class T, bool = JTreeParametersAvailable<T>::result>
55
56
57 /**
58 * Tempate specialisation of JRootFileReader for TTree incompatible iteration.
59 *
60 * The methods JROOT::actionAtFileOpen and JROOT::actionAtFileRead are called
61 * at opening of each file and reading of each object, respectively.
62 *
63 * This class implements the JLANG::JAccessibleObjectIterator interface.
64 */
65 template<class T>
66 class JRootFileReader<T,false> :
69 public JRootInputFile
70 {
71 public:
72
74
75
76 /**
77 * Default constructor.
78 */
80 cycleOld(0),
81 cycleNew(1),
82 __p (NULL),
83 name(getName(JType<T>()))
84 {}
85
86
87 /**
88 * Constructor.
89 *
90 * \param file_name file name
91 * \param key key of object to be read
92 */
93 JRootFileReader(const char* file_name,
94 const char* key = getName(JType<T>())) :
95 cycleOld(0),
96 cycleNew(1),
97 __p (NULL),
98 name(key)
99 {
100 open(file_name);
101 }
102
103
104 /**
105 * Rewind.
106 */
107 virtual void rewind() override
108 {
109 cycleOld = 0;
110 cycleNew = 1;
111 }
112
113
114 /**
115 * Open file.
116 *
117 * \param file_name file name
118 */
119 virtual void open(const char* file_name) override
120 {
121 JRootInputFile::open(file_name);
122
123 actionAtFileOpen<T>(this->getFile());
124
125 rewind();
126 }
127
128
129 /**
130 * Check availability of next element.
131 *
132 * \return true if the iteration has more elements; else false
133 */
134 virtual bool hasNext() override
135 {
136 if (is_open()) {
137
138 if (cycleOld != cycleNew) {
139
140 TString key(name);
141
142 key += ';';
143 key += cycleNew;
144
145 cycleOld = cycleNew;
146 __p = NULL;
147
148 getObject(getFile(), key, __p);
149
151 }
152
153 return (cycleOld == cycleNew && __p != NULL);
154 }
155
156 return false;
157 }
158
159
160 /**
161 * Get next element.
162 *
163 * \return pointer to element.
164 */
165 virtual const pointer_type& next() override
166 {
167 static pointer_type ps;
168
169 if (hasNext()) {
170
171 ++cycleNew;
172
173 ps.reset(__p);
174
175 } else {
176
177 ps.reset(NULL);
178 }
179
180 return ps;
181 }
182
183
184 /**
185 * Skip items.
186 *
187 * \param ns number of items to skip
188 * \return number of items skipped
189 */
190 virtual skip_type skip(const skip_type ns) override
191 {
192 using namespace std;
193
194 if (ns < (skip_type) cycleNew + (skip_type) numeric_limits<Short_t>::max())
195 cycleNew += (Short_t) ns;
196 else
197 cycleNew = numeric_limits<Short_t>::max();
198
199 return cycleNew - cycleOld;
200 }
201
202
203 /**
204 * Get cycle number of last read object.
205 *
206 * \return cycle number
207 */
208 Short_t getCycle() const
209 {
210 return cycleOld;
211 }
212
213 protected:
214 Short_t cycleOld;
215 Short_t cycleNew;
216 T* __p;
217 TString name;
218 };
219
220
221 /**
222 * Tempate specialisation of JRootFileReader for TTree compatible iteration.
223 *
224 * The method JROOT::actionAtFileOpen is called at opening of each file.
225 *
226 * This class implements the JLANG::JAccessibleObjectIterator interface.
227 */
228 template<class T>
229 class JRootFileReader<T,true> :
231 {
232 /**
233 * Open file.
234 *
235 * \param file_name file name
236 */
237 virtual void open(const char* file_name) override
238 {
240
241 if (this->get() != NULL) {
242 actionAtFileOpen<T>(this->get()->GetCurrentFile());
243 }
244 }
245 };
246}
247
248#endif
Exceptions.
std::ostream & rewind(std::ostream &out)
Rewind character.
Definition JManip.hh:222
Data type dependent action methods for customised ROOT version management.
Interface for object iteration with named access.
virtual void reset() override
Reset pointer.
Definition JPointer.hh:84
Interface for object iteration with rewinding.
virtual bool hasNext() override
Check availability of next element.
JRootFileReader()
Default constructor.
JAccessibleObjectIterator< T >::pointer_type pointer_type
virtual void open(const char *file_name) override
Open file.
virtual const pointer_type & next() override
Get next element.
virtual skip_type skip(const skip_type ns) override
Skip items.
Short_t getCycle() const
Get cycle number of last read object.
virtual void rewind() override
Rewind.
JRootFileReader(const char *file_name, const char *key=getName(JType< T >()))
Constructor.
virtual void open(const char *file_name) override
Open file.
ROOT input file.
Definition JRootFile.hh:97
virtual void open(const char *file_name) override
Open file.
Definition JRootFile.hh:136
virtual void open(const char *file_name) override
Open 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.
void actionAtFileRead(T *object)
General action method at file read.
void getObject(TFile *file, const TString &key, T *&ps)
Read object from ROOT file.
void actionAtFileOpen(TFile *file)
General action method at file open.
const char * getName()
Get ROOT name of given data type.
Auxiliary class for a type holder.
Definition JType.hh:19