Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMonteCarloFileSupportkit.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JAANETSUPPORTKIT__
2 #define __JSUPPORT__JAANETSUPPORTKIT__
3 
4 #include "evt/Head.hh"
5 #include "evt/Evt.hh"
6 
7 #include "evt/io_ascii.hh"
8 
10 #include "JLang/JGZFileReader.hh"
13 #include "JLang/JObjectOutput.hh"
14 #include "JLang/JException.hh"
15 
16 #include "JAAnet/JHead.hh"
17 #include "JAAnet/JHeadToolkit.hh"
18 
22 
23 
24 /**
25  * \author mdejong
26  */
27 
28 /**
29  * Read header from input.
30  *
31  * \param in input stream
32  * \param header header
33  * \return input stream
34  */
35 inline std::istream& operator>>(std::istream& in, Head& header)
36 {
37  header.clear();
38 
39  read(header, in);
40 
41  return in;
42 }
43 
44 #ifdef __NO_HEAD_OSTREAM__
45 /**
46  * Write header to output.
47  *
48  * \param out output stream
49  * \param header header
50  * \return output stream
51  */
52 inline std::ostream& operator<<(std::ostream& out, const Head& header)
53 {
54  write(header, out);
55 
56  return out;
57 }
58 #endif
59 
60 /**
61  * Read event from input.
62  *
63  * \param in input stream
64  * \param evt event
65  * \return input stream
66  */
67 inline std::istream& operator>>(std::istream& in, Evt& evt)
68 {
69  evt = Evt();
70 
71  read(evt, in, false);
72 
73  return in;
74 }
75 
76 
77 /**
78  * \cond NEVER
79  * Write event to output.
80  *
81  * \param out output stream
82  * \param evt event
83  * \return output stream
84  * \endcond
85  */
86 /*
87 inline std::ostream& operator<<(std::ostream& out, const Evt& evt)
88 {
89  write(evt, out);
90 
91  return out;
92 }
93 */
94 
95 namespace JSUPPORT {}
96 namespace JPP { using namespace JSUPPORT; }
97 
98 namespace JSUPPORT {
99 
102  using JLANG::JGZFileReader;
106  using JLANG::JException;
107 
108 
109  /**
110  * Template definition of Monte Carlo object reader.
111  */
112  template<class T, template<class> class JFileReader_t>
114 
115 
116  /**
117  * Template specialisation of JMonteCarloFileReader for Head.
118  *
119  * This class re-implements the methods open of the JLANG::JAccessible and method hasNext
120  * of the JLANG::JObjectIterator interface so that only one Head is read per file.
121  */
122  template<template<class> class JFileReader_t>
123  class JMonteCarloFileReader<Head, JFileReader_t> :
124  public JFileReader_t<Head>
125  {
126  public:
127  /**
128  * Default constructor.
129  */
131  JFileReader_t<Head>(),
132  do_next(false)
133  {}
134 
135 
136  /**
137  * Open file.
138  *
139  * \param file_name file name
140  */
141  virtual void open(const char* file_name)
142  {
143  JFileReader_t<Head>::open(file_name);
144 
145  do_next = true;
146  }
147 
148 
149  /**
150  * Check availability of next element.
151  *
152  * \return true if the iteration has more elements; else false
153  */
154  virtual bool hasNext()
155  {
156  if (do_next) {
157 
158  do_next = false;
159 
160  return JFileReader_t<Head>::hasNext();
161 
162  } else {
163 
164  return false;
165  }
166  }
167 
168  private:
169  bool do_next;
170  };
171 
172 
173  /**
174  * Template specialisation of JMonteCarloFileReader for Event.
175  *
176  * This class re-implements the method open of the JLANG::JAccessible interface
177  * so that the Head is skipped for each file.
178  */
179  template<template<class> class JFileReader_t>
180  class JMonteCarloFileReader<Evt, JFileReader_t> :
181  public JFileReader_t<Evt>
182  {
183  public:
184  /**
185  * Open file.
186  *
187  * \param file_name file name
188  */
189  virtual void open(const char* file_name)
190  {
191  using namespace std;
192 
193  JFileReader_t<Evt>::open(file_name);
194 
195  if (this->is_open()) {
196 
197  Head buffer;
198 
199  static_cast<istream&>(*this) >> buffer;
200  }
201  }
202  };
203 
204 
205  /**
206  * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e. '.evt')
207  */
208  template<>
210  public JMonteCarloFileReader<Head, JASCIIFileReader>
211  {};
212 
213 
214  /**
215  * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e. '.evt')
216  */
217  template<>
219  public JMonteCarloFileReader<Evt, JASCIIFileReader>
220  {};
221 
222 
223  /**
224  * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e. '.gz')
225  */
226  template<>
228  public JMonteCarloFileReader<Head, JGZFileReader>
229  {};
230 
231 
232  /**
233  * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e. '.gz')
234  */
235  template<>
237  public JMonteCarloFileReader<Evt, JGZFileReader>
238  {};
239 
240 
241  /**
242  * Template specialisation of JMonteCarloStreamObjectOutput for Head.
243  *
244  * This class provides for a ASCII formatted stream output without separator.
245  */
246  template<>
248  public JStreamObjectOutput<Head>
249  {
250  protected:
251  /**
252  * Constructor.
253  *
254  * \param out output stream
255  */
256  JMonteCarloStreamObjectOutput(std::ostream& out) :
257  JStreamObjectOutput<Head>(out, "")
258  {}
259  };
260 
261 
262  /**
263  * Template specialisation of JMonteCarloStreamObjectOutput for Evt.
264  *
265  * This class provides for a ASCII formatted stream output without separator.
266  */
267  template<>
269  public JStreamObjectOutput<Evt>
270  {
271  protected:
272  /**
273  * Constructor.
274  *
275  * \param out output stream
276  */
277  JMonteCarloStreamObjectOutput(std::ostream& out) :
278  JStreamObjectOutput<Evt>(out, "")
279  {}
280  };
281 
282 
283  /**
284  * Template specialisation of JMultipleFileScanner for Monte Carlo header.
285  *
286  * This class re-implements the methods rewind and setObject of the JLANG::JRewindableAbstractObjectIterator interface
287  * so that all header objects in the complete file list are read and added.
288  * It provides for the method getHeader which returns the sum of all Headers.
289  */
290  template<>
291  class JMultipleFileScanner<Head> :
292  public virtual JMultipleFileScanner<>,
294  {
295  public:
296  /**
297  * Default constructor.
298  */
302  do_next(true)
303  {}
304 
305 
306  /**
307  * Copy constructor.
308  * The file list is copied.
309  *
310  * \param file_list JMultipleFileScanner
311  */
315  do_next(true)
316  {
317  static_cast<JMultipleFileScanner_t&>(*this) = file_list;
318  }
319 
320 
321  /**
322  * Rewind.
323  */
324  virtual void rewind()
325  {
326  do_next = true;
327  }
328 
329 
330  /**
331  * Set object.
332  *
333  * \param object reference to object to be set
334  * \return true if set; else false
335  */
336  virtual bool setObject(Head& object)
337  {
338  if (do_next) {
339 
340  using namespace JLANG;
341  using JAANET::JHead;
342 
343  JHead header;
344 
345  do_next = false;
346 
347  unsigned int count = 0;
348 
349  JFileScanner<Head> scanner;
350 
351  for (const_iterator i = this->begin(); i != this->end(); ++i) {
352 
353  scanner.open(i->c_str());
354 
355  if (scanner.hasNext()) {
356 
357  const JHead buffer = *scanner.next();
358 
359  if (count == 0)
360  header = buffer;
361  else if (header.equals(buffer))
362  header.add(buffer);
363  else
364  THROW(JException, "JMultipleFileScanner<Head>::setObject(): inconsistent headers.");
365 
366  ++count;
367  }
368 
369  scanner.close();
370  }
371 
372  copy(header, object);
373 
374  if (count != 0 && count != this->size()) {
375  THROW(JException, "JMultipleFileScanner<Head>::setObject(): missing header(s): " << count << " != " << this->size());
376  }
377 
378  return count != 0;
379 
380  } else {
381 
382  return false;
383  }
384  }
385 
386 
387  /**
388  * Get Monte Carlo Header.
389  *
390  * \return header
391  */
392  const Head& getHeader()
393  {
394  const Head* p = NULL;
395 
396  if (!this->hasNext() || (p = this->next()) == NULL) {
397  throw JNullPointerException("JMultipleFileScanner<Head>::getHeader(): Missing Header.");
398  }
399 
400  rewind();
401 
402  return *p;
403  }
404 
405 
406  private:
407  bool do_next;
408  };
409 
410 
411  /**
412  * Get Monte Carlo header.
413  *
414  * \param file_list file list
415  * \return Monte Carlo header
416  */
417  inline Head getHeader(const JMultipleFileScanner_t& file_list)
418  {
419  return JMultipleFileScanner<Head>(file_list).getHeader();
420  }
421 }
422 
423 #endif
JMultipleFileScanner(const JMultipleFileScanner_t &file_list)
Copy constructor.
std::ostream & rewind(std::ostream &out)
Rewind character.
Definition: JPrint.hh:239
General exception.
Definition: JException.hh:40
Object reading from ASCII file.
Exceptions.
virtual void open(const char *file_name)
Open file.
std::istream & operator>>(std::istream &in, JHead &header)
Read header from input.
virtual void open(const char *file_name)
Open file.
Definition: JFileScanner.hh:64
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
Template definition of Monte Carlo object reader for gzipped ASCII formatted file (i...
T * open(const std::string &file_name)
Open file.
Definition: JeepToolkit.hh:275
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
Template definition of Monte Carlo object reader for ASCII formatted file (i.e.
virtual void open(const char *file_name)
Open file.
std::istream & read(std::istream &in, JContainer_t< TString, JAllocator_t > &object, const JBool< false > &option)
Auxiliary method for reading if TString does not exist.
Definition: JParser.hh:720
JMonteCarloStreamObjectOutput(std::ostream &out)
Constructor.
Exception for null pointer operation.
Definition: JException.hh:198
virtual bool setObject(Head &object)
Set object.
const Head & getHeader()
Get Monte Carlo Header.
Monte Carlo run header.
Definition: JHead.hh:727
Template specialisation of JMultipleFileScanner for Monte Carlo header.
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Interface for object iteration with named access.
Template definition of Monte Carlo object reader.
Auxiliary base class for list of file names.
General purpose class for object reading from a list of file names.
Object reading from gzipped file.
Template implementation of stream output for single data type.
virtual bool hasNext()
Check availability of next element.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:40
Object reading from file.
Definition: JFileScanner.hh:34
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Abstract object iterator with rewinding.
Template definition of Monte Carlo stream output for single data type.