Jpp  18.2.1
the software that should make you happy
 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  * 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  */
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> :
67  public JAccessibleObjectIterator<T>,
68  public JRewindableObjectIterator<T>,
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 
150  actionAtFileRead<T>(__p);
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
virtual skip_type skip(const skip_type ns) override
Skip items.
Exceptions.
TObject * getObject(const JRootObjectID &id)
Get first TObject with given identifier.
Interface for object iteration with rewinding.
then usage $script[< detector identifier >< run range >]< QA/QCfile > nExample script to produce data quality plots nWhen a detector identifier and run range are data are downloaded from the database nand subsequently stored in the given QA QC file
Definition: JDataQuality.sh:19
virtual const pointer_type & next() override
Get next element.
JRootFileReader()
Default constructor.
unsigned int skip_type
Type definition for number of objects to skip.
std::vector< size_t > ns
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
JRootFileReader(const char *file_name, const char *key=getName(JType< T >()))
Constructor.
virtual void open(const char *file_name) override
Open file.
virtual bool hasNext() override
Check availability of next element.
Auxiliary class for a type holder.
Definition: JType.hh:19
T * open(const std::string &file_name)
Open file.
Definition: JeepToolkit.hh:346
std::ostream & rewind(std::ostream &out)
Rewind character.
Definition: JManip.hh:222
virtual void open(const char *file_name) override
Open file.
Definition: JRootFile.hh:136
TFile * getFile(const std::string &file_name, const std::string &option="exist")
Get TFile pointer corresponding to give file name.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Data type dependent action methods for customised ROOT version management.
virtual void rewind() override
Rewind.
ROOT input file.
Definition: JRootFile.hh:95
virtual void open(const char *file_name) override
Open file.
Interface for object iteration with named access.
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:60
ROOT file reader.
virtual void open(const char *file_name) override
Open file.
JAccessibleObjectIterator< T >::pointer_type pointer_type
virtual void reset() override
Reset pointer.
Definition: JPointer.hh:84
Short_t getCycle() const
Get cycle number of last read object.