Jpp
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 
100  using JAANET::JHead;
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 JSUPPORT::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 
342  JHead header;
343 
344  do_next = false;
345 
346  unsigned int count = 0;
347 
348  JFileScanner<Head> scanner;
349 
350  for (const_iterator i = this->begin(); i != this->end(); ++i) {
351 
352  scanner.open(i->c_str());
353 
354  if (scanner.hasNext()) {
355 
356  const JHead buffer = *scanner.next();
357 
358  if (count == 0)
359  header = buffer;
360  else if (header.match(buffer))
361  header.add(buffer);
362  else
363  THROW(JException, "JMultipleFileScanner<Head>::setObject(): inconsistent headers.");
364 
365  ++count;
366  }
367 
368  scanner.close();
369  }
370 
371  copy(header, object);
372 
373  if (count != 0 && count != this->size()) {
374  if (!merge) {
375  THROW(JException, "JMultipleFileScanner<Head>::setObject(): missing header(s): " << count << " != " << this->size());
376  }
377  }
378 
379  return count != 0;
380 
381  } else {
382 
383  return false;
384  }
385  }
386 
387 
388  /**
389  * Get Monte Carlo Header.
390  *
391  * \return header
392  */
393  const Head& getHeader()
394  {
395  const Head* p = NULL;
396 
397  if (!this->hasNext() || (p = this->next()) == NULL) {
398  throw JNullPointerException("JMultipleFileScanner<Head>::getHeader(): Missing Header.");
399  }
400 
401  rewind();
402 
403  return *p;
404  }
405 
406  static bool merge; //!< Allow merging of files w/o header.
407 
408  private:
409  bool do_next;
410  };
411 
412 
413  /**
414  * Initialisation of merge option.
415  */
417 
418 
419  /**
420  * Get Monte Carlo header.
421  *
422  * \param file_list file list
423  * \return Monte Carlo header
424  */
425  inline Head getHeader(const JMultipleFileScanner_t& file_list)
426  {
427  return JMultipleFileScanner<Head>(file_list).getHeader();
428  }
429 
430 
431  /**
432  * Get list of files compatible with geven header.
433  *
434  * Note that the option corresponds to that of method JHead::match.
435  *
436  * \param input file list
437  * \param header header
438  * \param option option
439  * \return file list
440  */
442  const JHead& header,
443  const bool option = false)
444  {
445  using namespace JPP;
446 
448 
449  for (JMultipleFileScanner_t::const_iterator i = input.begin(); i != input.end(); ++i) {
450  try {
451  if (JHead(getHeader(*i)).match(header, option)) {
452  result.push_back(*i);
453  }
454  }
455  catch(std::exception&) {}
456  }
457 
458  return result;
459  }
460 }
461 
462 #endif
JException.hh
JAANET::JHead::match
bool match(const JHead &header, const bool option=true) const
Test match of headers.
Definition: JHead.hh:979
JSUPPORT::JMultipleFileScanner< Head >::getHeader
const Head & getHeader()
Get Monte Carlo Header.
Definition: JMonteCarloFileSupportkit.hh:393
JSUPPORT::JFileScanner
Object reading from file.
Definition: JFileScanner.hh:34
JSUPPORT::JMultipleFileScanner< Head >::JMultipleFileScanner
JMultipleFileScanner()
Default constructor.
Definition: JMonteCarloFileSupportkit.hh:299
JSUPPORT::JMultipleFileScanner_t
Auxiliary base class for list of file names.
Definition: JMultipleFileScanner.hh:44
JHead.hh
rewind
std::ostream & rewind(std::ostream &out)
Rewind character.
Definition: JPrint.hh:258
JAbstractObjectIterator.hh
JLANG::JRewindableAbstractObjectIterator
Abstract object iterator with rewinding.
Definition: JAbstractObjectIterator.hh:92
operator>>
std::istream & operator>>(std::istream &in, Head &header)
Read header from input.
Definition: JMonteCarloFileSupportkit.hh:35
JSUPPORT::JMultipleFileScanner< Head >::do_next
bool do_next
Definition: JMonteCarloFileSupportkit.hh:409
JSUPPORT::JMonteCarloFileReader< Head, JFileReader_t >::do_next
bool do_next
Definition: JMonteCarloFileSupportkit.hh:169
JGZFileReader.hh
JLANG::JNullPointerException
Exception for null pointer operation.
Definition: JException.hh:216
JLANG::JObjectIterator::hasNext
virtual bool hasNext()=0
Check availability of next element.
JSUPPORT::JMonteCarloFileReader< Head, JFileReader_t >::JMonteCarloFileReader
JMonteCarloFileReader()
Default constructor.
Definition: JMonteCarloFileSupportkit.hh:130
JAANET::copy
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:152
JSUPPORT::JMonteCarloFileReader< Head, JFileReader_t >::open
virtual void open(const char *file_name)
Open file.
Definition: JMonteCarloFileSupportkit.hh:141
JAANET::JHead
Monte Carlo run header.
Definition: JHead.hh:839
JSUPPORT::JMonteCarloStreamObjectOutput< Head >::JMonteCarloStreamObjectOutput
JMonteCarloStreamObjectOutput(std::ostream &out)
Constructor.
Definition: JMonteCarloFileSupportkit.hh:256
JHeadToolkit.hh
JSUPPORT::JMonteCarloFileReader< Head, JFileReader_t >::hasNext
virtual bool hasNext()
Check availability of next element.
Definition: JMonteCarloFileSupportkit.hh:154
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JLANG::JStreamObjectOutput
Template implementation of stream output for single data type.
Definition: JStreamObjectOutput.hh:28
JSUPPORT::getHeader
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
Definition: JMonteCarloFileSupportkit.hh:425
JSUPPORT::JMonteCarloFileReader< Evt, JFileReader_t >::open
virtual void open(const char *file_name)
Open file.
Definition: JMonteCarloFileSupportkit.hh:189
JTOOLS::result
return result
Definition: JPolint.hh:695
JSUPPORT::JMultipleFileScanner< Head >
Template specialisation of JMultipleFileScanner for Monte Carlo header.
Definition: JMonteCarloFileSupportkit.hh:291
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:669
JSUPPORT::JMonteCarloFileReader
Template definition of Monte Carlo object reader.
Definition: JMonteCarloFileSupportkit.hh:113
JASCIIFileReader.hh
operator<<
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Definition: clb_common_header.hh:72
JEEP::open
T * open(const std::string &file_name)
Open file.
Definition: JeepToolkit.hh:302
JMultipleFileScanner.hh
JSUPPORT::JMonteCarloGZFileReader
Template definition of Monte Carlo object reader for gzipped ASCII formatted file (i....
Definition: JMonteCarloFileReader.hh:42
JAANET::JHead::add
JHead & add(const JHead &header)
Addition of headers.
Definition: JHead.hh:1023
JSUPPORT::JMultipleFileScanner< Head >::JMultipleFileScanner
JMultipleFileScanner(const JMultipleFileScanner_t &file_list)
Copy constructor.
Definition: JMonteCarloFileSupportkit.hh:312
JSUPPORT::JMonteCarloStreamObjectOutput< Evt >::JMonteCarloStreamObjectOutput
JMonteCarloStreamObjectOutput(std::ostream &out)
Constructor.
Definition: JMonteCarloFileSupportkit.hh:277
JSUPPORT::JMonteCarloStreamObjectOutput
Template definition of Monte Carlo stream output for single data type.
Definition: JMonteCarloFileWriter.hh:37
JStreamObjectOutput.hh
JSUPPORT::JFileScanner::open
virtual void open(const char *file_name)
Open file.
Definition: JFileScanner.hh:64
JSUPPORT::JMultipleFileScanner
General purpose class for object reading from a list of file names.
Definition: JMultipleFileScanner.hh:167
std
Definition: jaanetDictionary.h:36
JSUPPORT::JMultipleFileScanner< Head >::merge
static bool merge
Allow merging of files w/o header.
Definition: JMonteCarloFileSupportkit.hh:406
JSUPPORT
Support classes and methods for experiment specific I/O.
Definition: JDataWriter.cc:38
JLANG::JAccessible::close
virtual void close()=0
Close device.
JMonteCarloFileWriter.hh
JLANG::JGZFileReader
Object reading from gzipped file.
Definition: JGZFileReader.hh:28
JSUPPORT::JMultipleFileScanner< Head >::rewind
virtual void rewind()
Rewind.
Definition: JMonteCarloFileSupportkit.hh:324
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10
JMonteCarloFileReader.hh
JSUPPORT::JMultipleFileScanner< Head >::setObject
virtual bool setObject(Head &object)
Set object.
Definition: JMonteCarloFileSupportkit.hh:336
JSUPPORT::JMonteCarloASCIIFileReader
Template definition of Monte Carlo object reader for ASCII formatted file (i.e.
Definition: JMonteCarloFileReader.hh:28
JLANG::JObjectIterator::next
virtual const pointer_type & next()=0
Get next element.
JLANG::JException
General exception.
Definition: JException.hh:40
JLANG::JASCIIFileReader
Object reading from ASCII file.
Definition: JASCIIFileReader.hh:26
JObjectOutput.hh
JSUPPORT::getAAnetFiles
JMultipleFileScanner_t getAAnetFiles(const JMultipleFileScanner_t &input, const JHead &header, const bool option=false)
Get list of files compatible with geven header.
Definition: JMonteCarloFileSupportkit.hh:441