Jpp
JMonteCarloFileSupportkit.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JAANETSUPPORTKIT__
2 #define __JSUPPORT__JAANETSUPPORTKIT__
3 
6 
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 
45 /**
46  * \cond NEVER
47  * Write header to output.
48  *
49  * \param out output stream
50  * \param header header
51  * \return output stream
52  */
53 /*
54 inline std::ostream& operator<<(std::ostream& out, const Head& header)
55 {
56  write(header, out);
57 
58  return out;
59 }
60 */
61 
62 /**
63  * Read event from input.
64  *
65  * \param in input stream
66  * \param evt event
67  * \return input stream
68  */
69 inline std::istream& operator>>(std::istream& in, Evt& evt)
70 {
71  evt = Evt();
72 
73  read(evt, in, false);
74 
75  return in;
76 }
77 
78 
79 /**
80  * Write event to output.
81  *
82  * \param out output stream
83  * \param evt event
84  * \return output stream
85  * \endcond
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 /**
96  * Read hit from input.
97  *
98  * \param in input stream
99  * \param hit hit
100  * \return input stream
101  */
102 inline std::istream& operator>>(std::istream& in, Hit& hit)
103 {
104  hit = Hit();
105 
106  read(hit, in, false);
107 
108  return in;
109 }
110 
111 
112 /**
113  * Write hit to output.
114  *
115  * \param out output stream
116  * \param hit hit
117  * \return output stream
118  * \endcond
119  */
120 inline std::ostream& operator<<(std::ostream& out, const Hit& hit)
121 {
122  write(hit, out);
123 
124  return out;
125 }
126 
127 
128 namespace JSUPPORT {}
129 namespace JPP { using namespace JSUPPORT; }
130 
131 namespace JSUPPORT {
132 
133  using JAANET::JHead;
135  using JLANG::JGZFileReader;
139  using JLANG::JException;
140 
141 
142  /**
143  * Template definition of Monte Carlo object reader.
144  */
145  template<class T, template<class> class JFileReader_t>
147 
148 
149  /**
150  * Template specialisation of JMonteCarloFileReader for Head.
151  *
152  * This class re-implements the methods open of the JLANG::JAccessible and method hasNext
153  * of the JLANG::JObjectIterator interface so that only one Head is read per file.
154  */
155  template<template<class> class JFileReader_t>
156  class JMonteCarloFileReader<Head, JFileReader_t> :
157  public JFileReader_t<Head>
158  {
159  public:
160  /**
161  * Default constructor.
162  */
164  JFileReader_t<Head>(),
165  do_next(false)
166  {}
167 
168 
169  /**
170  * Open file.
171  *
172  * \param file_name file name
173  */
174  virtual void open(const char* file_name)
175  {
176  JFileReader_t<Head>::open(file_name);
177 
178  do_next = true;
179  }
180 
181 
182  /**
183  * Check availability of next element.
184  *
185  * \return true if the iteration has more elements; else false
186  */
187  virtual bool hasNext()
188  {
189  if (do_next) {
190 
191  do_next = false;
192 
193  return JFileReader_t<Head>::hasNext();
194 
195  } else {
196 
197  return false;
198  }
199  }
200 
201  private:
202  bool do_next;
203  };
204 
205 
206  /**
207  * Template specialisation of JMonteCarloFileReader for Event.
208  *
209  * This class re-implements the method open of the JLANG::JAccessible interface
210  * so that the Head is skipped for each file.
211  */
212  template<template<class> class JFileReader_t>
213  class JMonteCarloFileReader<Evt, JFileReader_t> :
214  public JFileReader_t<Evt>
215  {
216  public:
217  /**
218  * Open file.
219  *
220  * \param file_name file name
221  */
222  virtual void open(const char* file_name)
223  {
224  using namespace std;
225 
226  JFileReader_t<Evt>::open(file_name);
227 
228  if (this->is_open()) {
229 
230  Head buffer;
231 
232  static_cast<istream&>(*this) >> buffer;
233  }
234  }
235  };
236 
237 
238  /**
239  * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e. '.evt')
240  */
241  template<>
243  public JMonteCarloFileReader<Head, JASCIIFileReader>
244  {};
245 
246 
247  /**
248  * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e. '.evt')
249  */
250  template<>
252  public JMonteCarloFileReader<Evt, JASCIIFileReader>
253  {};
254 
255 
256  /**
257  * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e. '.gz')
258  */
259  template<>
261  public JMonteCarloFileReader<Head, JGZFileReader>
262  {};
263 
264 
265  /**
266  * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e. '.gz')
267  */
268  template<>
270  public JMonteCarloFileReader<Evt, JGZFileReader>
271  {};
272 
273 
274  /**
275  * Template specialisation of JMonteCarloStreamObjectOutput for Head.
276  *
277  * This class provides for a ASCII formatted stream output without separator.
278  */
279  template<>
281  public JStreamObjectOutput<Head>
282  {
283  protected:
284  /**
285  * Constructor.
286  *
287  * \param out output stream
288  */
289  JMonteCarloStreamObjectOutput(std::ostream& out) :
290  JStreamObjectOutput<Head>(out, "")
291  {}
292  };
293 
294 
295  /**
296  * Template specialisation of JMonteCarloStreamObjectOutput for Evt.
297  *
298  * This class provides for a ASCII formatted stream output without separator.
299  */
300  template<>
302  public JStreamObjectOutput<Evt>
303  {
304  protected:
305  /**
306  * Constructor.
307  *
308  * \param out output stream
309  */
310  JMonteCarloStreamObjectOutput(std::ostream& out) :
311  JStreamObjectOutput<Evt>(out, "")
312  {}
313  };
314 
315 
316  /**
317  * Template specialisation of JMultipleFileScanner for Monte Carlo header.
318  *
319  * This class re-implements the methods rewind and setObject of the JLANG::JRewindableAbstractObjectIterator interface
320  * so that all header objects in the complete file list are read and added.
321  * It provides for the method JSUPPORT::getHeader which returns the sum of all headers.
322  */
323  template<>
325  public virtual JMultipleFileScanner<>,
327  {
328  public:
329  /**
330  * Default constructor.
331  */
335  do_next(true)
336  {}
337 
338 
339  /**
340  * Copy constructor.
341  * The file list is copied.
342  *
343  * \param file_list JMultipleFileScanner
344  */
348  do_next(true)
349  {
350  static_cast<JMultipleFileScanner_t&>(*this) = file_list;
351  }
352 
353 
354  /**
355  * Rewind.
356  */
357  virtual void rewind()
358  {
359  do_next = true;
360  }
361 
362 
363  /**
364  * Set object.
365  *
366  * \param object reference to object to be set
367  * \return true if set; else false
368  */
369  virtual bool setObject(Head& object)
370  {
371  if (do_next) {
372 
373  using namespace JLANG;
374 
375  JHead header;
376 
377  do_next = false;
378 
379  unsigned int count = 0;
380 
381  JFileScanner<Head> scanner;
382 
383  for (const_iterator i = this->begin(); i != this->end(); ++i) {
384 
385  scanner.open(i->c_str());
386 
387  if (scanner.hasNext()) {
388 
389  const JHead buffer = *scanner.next();
390 
391  if (count == 0)
392  header = buffer;
393  else if (header.match(buffer))
394  header.add(buffer);
395  else
396  THROW(JException, "JMultipleFileScanner<Head>::setObject(): inconsistent headers.");
397 
398  ++count;
399  }
400 
401  scanner.close();
402  }
403 
404  copy(header, object);
405 
406  if (count != 0 && count != this->size()) {
407  if (!merge) {
408  THROW(JException, "JMultipleFileScanner<Head>::setObject(): missing header(s): " << count << " != " << this->size());
409  }
410  }
411 
412  return count != 0;
413 
414  } else {
415 
416  return false;
417  }
418  }
419 
420 
421  /**
422  * Get Monte Carlo Header.
423  *
424  * \return header
425  */
426  const Head& getHeader()
427  {
428  const Head* p = NULL;
429 
430  if (!this->hasNext() || (p = this->next()) == NULL) {
431  throw JNullPointerException("JMultipleFileScanner<Head>::getHeader(): Missing Header.");
432  }
433 
434  rewind();
435 
436  return *p;
437  }
438 
439  static bool merge; //!< Allow merging of files w/o header.
440 
441  private:
442  bool do_next;
443  };
444 
445 
446  /**
447  * Initialisation of merge option.
448  */
450 
451 
452  /**
453  * Get Monte Carlo header.
454  *
455  * \param file_list file list
456  * \return Monte Carlo header
457  */
458  inline Head getHeader(const JMultipleFileScanner_t& file_list)
459  {
460  return JMultipleFileScanner<Head>(file_list).getHeader();
461  }
462 
463 
464  /**
465  * Get list of files compatible with geven header.
466  *
467  * Note that the option corresponds to that of method JHead::match.
468  *
469  * \param input file list
470  * \param header header
471  * \param option option
472  * \return file list
473  */
475  const JHead& header,
476  const bool option = false)
477  {
478  using namespace JPP;
479 
481 
482  for (JMultipleFileScanner_t::const_iterator i = input.begin(); i != input.end(); ++i) {
483  try {
484  if (JHead(getHeader(*i)).match(header, option)) {
485  result.push_back(*i);
486  }
487  }
488  catch(std::exception&) {}
489  }
490 
491  return result;
492  }
493 }
494 
495 #endif
JException.hh
JAANET::JHead::match
bool match(const JHead &header, const bool option=true) const
Test match of headers.
Definition: JHead.hh:979
Hit
Definition: Hit.hh:7
JSUPPORT::JMultipleFileScanner< Head >::getHeader
const Head & getHeader()
Get Monte Carlo Header.
Definition: JMonteCarloFileSupportkit.hh:426
Head.hh
JSUPPORT::JFileScanner
Object reading from file.
Definition: JFileScanner.hh:34
JSUPPORT::JMultipleFileScanner< Head >::JMultipleFileScanner
JMultipleFileScanner()
Default constructor.
Definition: JMonteCarloFileSupportkit.hh:332
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:442
JSUPPORT::JMonteCarloFileReader< Head, JFileReader_t >::do_next
bool do_next
Definition: JMonteCarloFileSupportkit.hh:202
JGZFileReader.hh
JLANG::JNullPointerException
Exception for null pointer operation.
Definition: JException.hh:216
Evt
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition: Evt.hh:19
Evt.hh
JLANG::JObjectIterator::hasNext
virtual bool hasNext()=0
Check availability of next element.
JSUPPORT::JMonteCarloFileReader< Head, JFileReader_t >::JMonteCarloFileReader
JMonteCarloFileReader()
Default constructor.
Definition: JMonteCarloFileSupportkit.hh:163
write
bool write(const Vec &v, std::ostream &os)
Write a Vec(tor) to a stream.
Definition: io_ascii.hh:154
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:174
JAANET::JHead
Monte Carlo run header.
Definition: JHead.hh:839
JSUPPORT::JMonteCarloStreamObjectOutput< Head >::JMonteCarloStreamObjectOutput
JMonteCarloStreamObjectOutput(std::ostream &out)
Constructor.
Definition: JMonteCarloFileSupportkit.hh:289
JHeadToolkit.hh
JSUPPORT::JMonteCarloFileReader< Head, JFileReader_t >::hasNext
virtual bool hasNext()
Check availability of next element.
Definition: JMonteCarloFileSupportkit.hh:187
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:458
JSUPPORT::JMonteCarloFileReader< Evt, JFileReader_t >::open
virtual void open(const char *file_name)
Open file.
Definition: JMonteCarloFileSupportkit.hh:222
Head
The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred ...
Definition: Head.hh:39
JTOOLS::result
return result
Definition: JPolint.hh:695
operator<<
std::ostream & operator<<(std::ostream &out, const Evt &evt)
Definition: JMonteCarloFileSupportkit.hh:87
JSUPPORT::JMultipleFileScanner< Head >
Template specialisation of JMultipleFileScanner for Monte Carlo header.
Definition: JMonteCarloFileSupportkit.hh:324
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
JSUPPORT::JMonteCarloFileReader
Template definition of Monte Carlo object reader.
Definition: JMonteCarloFileSupportkit.hh:146
JASCIIFileReader.hh
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
read
bool read(Vec &v, std::istream &is)
Read a Vec(tor) from a stream.
Definition: io_ascii.hh:141
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:345
JSUPPORT::JMonteCarloStreamObjectOutput< Evt >::JMonteCarloStreamObjectOutput
JMonteCarloStreamObjectOutput(std::ostream &out)
Constructor.
Definition: JMonteCarloFileSupportkit.hh:310
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:439
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:357
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:369
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:23
JLANG::JASCIIFileReader
Object reading from ASCII file.
Definition: JASCIIFileReader.hh:26
JObjectOutput.hh
io_ascii.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:474