Jpp  17.1.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMonteCarloFileSupportkit.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JMONTECARLOFILESUPPORTKIT__
2 #define __JSUPPORT__JMONTECARLOFILESUPPORTKIT__
3 
7 
9 
11 #include "JLang/JGZFileReader.hh"
14 #include "JLang/JObjectOutput.hh"
15 #include "JLang/JException.hh"
16 
17 #include "JAAnet/JHead.hh"
18 #include "JAAnet/JHeadToolkit.hh"
19 
23 
24 
25 /**
26  * \author mdejong
27  */
28 
29 /**
30  * Read header from input.
31  *
32  * \param in input stream
33  * \param header header
34  * \return input stream
35  */
36 inline std::istream& operator>>(std::istream& in, Head& header)
37 {
38  header.clear();
39 
40  read(header, in);
41 
42  return in;
43 }
44 
45 
46 /**
47  * \cond NEVER
48  * Write header to output.
49  *
50  * \param out output stream
51  * \param header header
52  * \return output stream
53  * \endcond
54  */
55 /*
56 inline std::ostream& operator<<(std::ostream& out, const Head& header)
57 {
58  write(header, out);
59 
60  return out;
61 }
62 */
63 
64 
65 /**
66  * Write multi-header to output.
67  *
68  * \param out output stream
69  * \param header header
70  * \return output stream
71  * \endcond
72  */
73 inline std::ostream& operator<<(std::ostream& out, const MultiHead& header)
74 {
75  for (MultiHead::const_iterator i = header.begin(); i != header.end(); ++i) {
76  write(*i, out);
77  }
78 
79  return out;
80 }
81 
82 
83 /**
84  * Read event from input.
85  *
86  * \param in input stream
87  * \param evt event
88  * \return input stream
89  */
90 inline std::istream& operator>>(std::istream& in, Evt& evt)
91 {
92  evt = Evt();
93 
94  read(evt, in, false);
95 
96  return in;
97 }
98 
99 
100 /**
101  * Write event to output.
102  *
103  * \param out output stream
104  * \param evt event
105  * \return output stream
106  * \endcond
107  */
108 inline std::ostream& operator<<(std::ostream& out, const Evt& evt)
109 {
110  using namespace std;
111 
112  const ios_base::fmtflags flags = out.flags();
113 
114  write(evt, out << fixed);
115 
116  out.flags(flags);
117 
118  return out;
119 }
120 
121 
122 /**
123  * Read hit from input.
124  *
125  * \param in input stream
126  * \param hit hit
127  * \return input stream
128  */
129 inline std::istream& operator>>(std::istream& in, Hit& hit)
130 {
131  hit = Hit();
132 
133  read(hit, in, false);
134 
135  return in;
136 }
137 
138 
139 /**
140  * Write hit to output.
141  *
142  * \param out output stream
143  * \param hit hit
144  * \return output stream
145  * \endcond
146  */
147 inline std::ostream& operator<<(std::ostream& out, const Hit& hit)
148 {
149  using namespace std;
150 
151  const ios_base::fmtflags flags = out.flags();
152 
153  write(hit, out << fixed);
154 
155  out.flags(flags);
156 
157  return out;
158 }
159 
160 
161 namespace JSUPPORT {}
162 namespace JPP { using namespace JSUPPORT; }
163 
164 namespace JSUPPORT {
165 
166  using JAANET::JHead;
168  using JLANG::JGZFileReader;
172  using JLANG::JException;
173 
174 
175  /**
176  * Template definition of Monte Carlo object reader.
177  */
178  template<class T, template<class> class JFileReader_t>
180 
181 
182  /**
183  * Template specialisation of JMonteCarloFileReader for Head.
184  *
185  * This class re-implements the methods open of the JLANG::JAccessible and method hasNext
186  * of the JLANG::JObjectIterator interface so that only one Head is read per file.
187  */
188  template<template<class> class JFileReader_t>
189  class JMonteCarloFileReader<Head, JFileReader_t> :
190  public JFileReader_t<Head>
191  {
192  public:
193  /**
194  * Default constructor.
195  */
197  JFileReader_t<Head>(),
198  do_next(false)
199  {}
200 
201 
202  /**
203  * Open file.
204  *
205  * \param file_name file name
206  */
207  virtual void open(const char* file_name) override
208  {
209  JFileReader_t<Head>::open(file_name);
210 
211  do_next = true;
212  }
213 
214 
215  /**
216  * Check availability of next element.
217  *
218  * \return true if the iteration has more elements; else false
219  */
220  virtual bool hasNext() override
221  {
222  if (do_next) {
223 
224  do_next = false;
225 
226  return JFileReader_t<Head>::hasNext();
227 
228  } else {
229 
230  return false;
231  }
232  }
233 
234  private:
235  bool do_next;
236  };
237 
238 
239  /**
240  * Template specialisation of JMonteCarloFileReader for Event.
241  *
242  * This class re-implements the method open of the JLANG::JAccessible interface
243  * so that the Head is skipped for each file.
244  */
245  template<template<class> class JFileReader_t>
246  class JMonteCarloFileReader<Evt, JFileReader_t> :
247  public JFileReader_t<Evt>
248  {
249  public:
250  /**
251  * Open file.
252  *
253  * \param file_name file name
254  */
255  virtual void open(const char* file_name) override
256  {
257  using namespace std;
258 
259  JFileReader_t<Evt>::open(file_name);
260 
261  if (this->is_open()) {
262 
263  Head buffer;
264 
265  static_cast<istream&>(*this) >> buffer;
266  }
267  }
268  };
269 
270 
271  /**
272  * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e.\ '.evt')
273  */
274  template<>
276  public JMonteCarloFileReader<Head, JASCIIFileReader>
277  {};
278 
279 
280  /**
281  * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e.\ '.evt')
282  */
283  template<>
285  public JMonteCarloFileReader<Evt, JASCIIFileReader>
286  {};
287 
288 
289  /**
290  * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e.\ '.gz')
291  */
292  template<>
294  public JMonteCarloFileReader<Head, JGZFileReader>
295  {};
296 
297 
298  /**
299  * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e.\ '.gz')
300  */
301  template<>
303  public JMonteCarloFileReader<Evt, JGZFileReader>
304  {};
305 
306 
307  /**
308  * Template specialisation of JMonteCarloStreamObjectOutput for Head.
309  *
310  * This class provides for a ASCII formatted stream output without separator.
311  */
312  template<>
314  public JStreamObjectOutput<Head>
315  {
316  protected:
317  /**
318  * Constructor.
319  *
320  * \param out output stream
321  */
322  JMonteCarloStreamObjectOutput(std::ostream& out) :
323  JStreamObjectOutput<Head>(out, "")
324  {}
325  };
326 
327 
328  /**
329  * Template specialisation of JMonteCarloStreamObjectOutput for Evt.
330  *
331  * This class provides for a ASCII formatted stream output without separator.
332  */
333  template<>
335  public JStreamObjectOutput<Evt>
336  {
337  protected:
338  /**
339  * Constructor.
340  *
341  * \param out output stream
342  */
343  JMonteCarloStreamObjectOutput(std::ostream& out) :
344  JStreamObjectOutput<Evt>(out, "")
345  {}
346  };
347 
348 
349  /**
350  * Template specialisation of JMultipleFileScanner for Monte Carlo header.
351  *
352  * This class re-implements the methods rewind and setObject of the JLANG::JRewindableAbstractObjectIterator interface
353  * so that all header objects in the complete file list are read and added.
354  * It provides for the method JSUPPORT::getHeader which returns the sum of all headers.
355  */
356  template<>
358  public virtual JMultipleFileScanner<>,
360  {
361  public:
362  /**
363  * Default constructor.
364  */
368  do_next(true)
369  {}
370 
371 
372  /**
373  * Copy constructor.
374  * The file list is copied.
375  *
376  * \param file_list JMultipleFileScanner
377  */
381  do_next(true)
382  {
383  static_cast<JMultipleFileScanner_t&>(*this) = file_list;
384  }
385 
386 
387  /**
388  * Rewind.
389  */
390  virtual void rewind() override
391  {
392  do_next = true;
393  }
394 
395 
396  /**
397  * Set object.
398  *
399  * \param object reference to object to be set
400  * \return true if set; else false
401  */
402  virtual bool setObject(Head& object) override
403  {
404  if (do_next) {
405 
406  using namespace JLANG;
407 
408  JHead header;
409 
410  do_next = false;
411 
412  unsigned int count = 0;
413 
414  JFileScanner<Head> scanner;
415 
416  for (const_iterator i = this->begin(); i != this->end(); ++i) {
417 
418  scanner.open(i->c_str());
419 
420  if (scanner.hasNext()) {
421 
422  const JHead buffer = *scanner.next();
423 
424  if (count == 0)
425  header = buffer;
426  else if (header.match(buffer))
427  header.add(buffer);
428  else
429  THROW(JException, "JMultipleFileScanner<Head>::setObject(): inconsistent headers.");
430 
431  ++count;
432  }
433 
434  scanner.close();
435  }
436 
437  copy(header, object);
438 
439  if (count != 0 && count != this->size()) {
440  if (!merge) {
441  THROW(JException, "JMultipleFileScanner<Head>::setObject(): missing header(s): " << count << " != " << this->size());
442  }
443  }
444 
445  return count != 0;
446 
447  } else {
448 
449  return false;
450  }
451  }
452 
453 
454  /**
455  * Get Monte Carlo Header.
456  *
457  * \return header
458  */
459  const Head& getHeader()
460  {
461  const Head* p = NULL;
462 
463  if (!this->hasNext() || (p = this->next()) == NULL) {
464  throw JNullPointerException("JMultipleFileScanner<Head>::getHeader(): Missing Header.");
465  }
466 
467  rewind();
468 
469  return *p;
470  }
471 
472  static bool merge; //!< Allow merging of files w/o header.
473 
474  private:
475  bool do_next;
476  };
477 
478 
479  /**
480  * Initialisation of merge option.
481  */
483 
484 
485  /**
486  * Get Monte Carlo header.
487  *
488  * \param file_list file list
489  * \return Monte Carlo header
490  */
491  inline Head getHeader(const JMultipleFileScanner_t& file_list)
492  {
493  return JMultipleFileScanner<Head>(file_list).getHeader();
494  }
495 
496 
497  /**
498  * Get list of files compatible with geven header.
499  *
500  * Note that the option corresponds to that of method JHead::match.
501  *
502  * \param input file list
503  * \param test test function
504  * \return file list
505  */
506  template<class JFunction_t>
508  {
509  using namespace JPP;
510 
512 
513  for (JMultipleFileScanner_t::const_iterator i = input.begin(); i != input.end(); ++i) {
514  if (test(getHeader(*i))) {
515  result.push_back(*i);
516  }
517  }
518 
519  return result;
520  }
521 }
522 
523 #endif
JMultipleFileScanner(const JMultipleFileScanner_t &file_list)
Copy constructor.
General exception.
Definition: JException.hh:23
Object reading from ASCII file.
virtual bool hasNext() override
Check availability of next element.
Exceptions.
std::istream & read(std::istream &in, JTestSummary &summary, const char delimiter= ' ')
Read test summary.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
JHead & add(const JHead &header)
Addition of headers.
Definition: JHead.hh:1445
Template definition of Monte Carlo object reader for gzipped ASCII formatted file (i...
static bool merge
Allow merging of files w/o header.
T * open(const std::string &file_name)
Open file.
Definition: JeepToolkit.hh:346
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
Template definition of Monte Carlo object reader for ASCII formatted file (i.e. &#39;.evt&#39;)
std::ostream & rewind(std::ostream &out)
Rewind character.
Definition: JManip.hh:222
virtual void open(const char *file_name) override
Open file.
Definition: JFileScanner.hh:77
JMonteCarloStreamObjectOutput(std::ostream &out)
Constructor.
Exception for null pointer operation.
Definition: JException.hh:216
return result
Definition: JPolint.hh:743
const Head & getHeader()
Get Monte Carlo Header.
virtual bool setObject(Head &object) override
Set object.
virtual void open(const char *file_name) override
Open file.
Monte Carlo run header.
Definition: JHead.hh:1167
Template specialisation of JMultipleFileScanner for Monte Carlo header.
The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred ...
Definition: Head.hh:65
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Definition: Hit.hh:8
Template definition of Monte Carlo object reader.
Auxiliary base class for list of file names.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1737
std::vector< int > count
Definition: JAlgorithm.hh:180
General purpose class for object reading from a list of file names.
bool write(const Vec &v, std::ostream &os)
Write a Vec(tor) to a stream.
Definition: io_ascii.hh:155
Object reading from gzipped file.
Template implementation of stream output for single data type.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:139
Object reading from file.
Definition: JFileScanner.hh:36
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
virtual void open(const char *file_name) override
Open file.
JMultipleFileScanner_t getAAnetFiles(const JMultipleFileScanner_t &input, JFunction_t test)
Get list of files compatible with geven header.
Abstract object iterator with rewinding.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition: Evt.hh:20
Template definition of Monte Carlo stream output for single data type.