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