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