Jpp  18.0.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 #include "JLang/JManip.hh"
17 
18 #include "JAAnet/JHead.hh"
19 #include "JAAnet/JHeadToolkit.hh"
20 #include "JAAnet/JAAnetToolkit.hh"
21 
25 
26 
27 /**
28  * \author mdejong
29  */
30 
31 /**
32  * Read header from input.
33  *
34  * \param in input stream
35  * \param header header
36  * \return input stream
37  */
38 inline std::istream& operator>>(std::istream& in, Head& header)
39 {
40  header.clear();
41 
42  read(header, in);
43 
44  return in;
45 }
46 
47 
48 /**
49  * \cond NEVER
50  * Write header to output.
51  *
52  * \param out output stream
53  * \param header header
54  * \return output stream
55  * \endcond
56  */
57 /*
58  inline std::ostream& operator<<(std::ostream& out, const Head& header)
59  {
60  write(header, out);
61 
62  return out;
63  }
64 */
65 
66 
67 /**
68  * Write multi-header to output.
69  *
70  * \param out output stream
71  * \param header header
72  * \return output stream
73  */
74 inline std::ostream& operator<<(std::ostream& out, const MultiHead& header)
75 {
76  for (MultiHead::const_iterator i = header.begin(); i != header.end(); ++i) {
77  write(*i, out);
78  }
79 
80  return out;
81 }
82 
83 
84 /**
85  * Read event from input.
86  *
87  * \param in input stream
88  * \param evt event
89  * \return input stream
90  */
91 inline std::istream& operator>>(std::istream& in, Evt& evt)
92 {
93  evt = Evt();
94 
95  read(evt, in, false);
96 
97  return in;
98 }
99 
100 
101 /**
102  * Write event to output.
103  *
104  * \param out output stream
105  * \param evt event
106  * \return output stream
107  */
108 inline std::ostream& operator<<(std::ostream& out, const Evt& evt)
109 {
110  using namespace std;
111  using namespace JPP;
112 
113  const ios_base::fmtflags flags = out.flags();
114 
115  if (getLongprint(out)) {
116 
117  out << mc_keys::start_event_t << ' ' << evt.mc_id << ' ' << 1 << endl;
118 
119  for (const auto& trk : evt.mc_trks) {
120 
121  out << mc_keys::track_in_t << ' '
122  << trk.id << ' '
123  << trk.pos << ' '
124  << trk.dir << ' '
125  << trk.E << ' '
126  << trk.t << ' '
127  << is_initialstate(trk)
128  << is_finalstate (trk)
129  << endl;
130  }
131 
132  for (const auto& trk : evt.trks) {
133 
134  out << mc_keys::track_fit_t << ' '
135  << trk.id << ' '
136  << trk.pos << ' '
137  << trk.dir << ' '
138  << trk.E << ' '
139  << trk.t << endl;
140  }
141 
142  for (const auto& hit : evt.mc_hits) { write (hit, out, mc_keys::hit_t); }
143  for (const auto& hit : evt.hits) { write (hit, out, mc_keys::hit_raw_t); }
144 
145  out << mc_keys::weights_t; for (const auto& w : evt.w) { out << ' ' << w;} out << endl;
146  out << mc_keys::w2list_t; for (const auto& w : evt.w2list) { out << ' ' << w;} out << endl;
147  out << mc_keys::w3list_t; for (const auto& w : evt.w3list) { out << ' ' << w;} out << endl;
148 
149  out << mc_keys::eventtime_t << ' '
150  << evt.mc_event_time.GetSec() << ' '
151  << evt.mc_event_time.GetNanoSec() / 16 << endl;
152 
153  out << mc_keys::end_event_t << endl;
154 
155  } else {
156 
157  write(evt, out << fixed);
158  }
159 
160  out.flags(flags);
161 
162  return out;
163 }
164 
165 
166 /**
167  * Read hit from input.
168  *
169  * \param in input stream
170  * \param hit hit
171  * \return input stream
172  */
173 inline std::istream& operator>>(std::istream& in, Hit& hit)
174 {
175  hit = Hit();
176 
177  read(hit, in, false);
178 
179  return in;
180 }
181 
182 
183 /**
184  * Write hit to output.
185  *
186  * \param out output stream
187  * \param hit hit
188  * \return output stream
189  */
190 inline std::ostream& operator<<(std::ostream& out, const Hit& hit)
191 {
192  using namespace std;
193 
194  const ios_base::fmtflags flags = out.flags();
195 
196  write(hit, out << fixed);
197 
198  out.flags(flags);
199 
200  return out;
201 }
202 
203 
204 namespace JSUPPORT {}
205 namespace JPP { using namespace JSUPPORT; }
206 
207 namespace JSUPPORT {
208 
209  using JAANET::JHead;
211  using JLANG::JGZFileReader;
215  using JLANG::JException;
216 
217 
218  /**
219  * Template definition of Monte Carlo object reader.
220  */
221  template<class T, template<class> class JFileReader_t>
223 
224 
225  /**
226  * Template specialisation of JMonteCarloFileReader for Head.
227  *
228  * This class re-implements the methods open of the JLANG::JAccessible and method hasNext
229  * of the JLANG::JObjectIterator interface so that only one Head is read per file.
230  */
231  template<template<class> class JFileReader_t>
232  class JMonteCarloFileReader<Head, JFileReader_t> :
233  public JFileReader_t<Head>
234  {
235  public:
236  /**
237  * Default constructor.
238  */
240  JFileReader_t<Head>(),
241  do_next(false)
242  {}
243 
244 
245  /**
246  * Open file.
247  *
248  * \param file_name file name
249  */
250  virtual void open(const char* file_name) override
251  {
252  JFileReader_t<Head>::open(file_name);
253 
254  do_next = true;
255  }
256 
257 
258  /**
259  * Check availability of next element.
260  *
261  * \return true if the iteration has more elements; else false
262  */
263  virtual bool hasNext() override
264  {
265  if (do_next) {
266 
267  do_next = false;
268 
269  return JFileReader_t<Head>::hasNext();
270 
271  } else {
272 
273  return false;
274  }
275  }
276 
277  private:
278  bool do_next;
279  };
280 
281 
282  /**
283  * Template specialisation of JMonteCarloFileReader for Event.
284  *
285  * This class re-implements the method open of the JLANG::JAccessible interface
286  * so that the Head is skipped for each file.
287  */
288  template<template<class> class JFileReader_t>
289  class JMonteCarloFileReader<Evt, JFileReader_t> :
290  public JFileReader_t<Evt>
291  {
292  public:
293  /**
294  * Open file.
295  *
296  * \param file_name file name
297  */
298  virtual void open(const char* file_name) override
299  {
300  using namespace std;
301 
302  JFileReader_t<Evt>::open(file_name);
303 
304  if (this->is_open()) {
305 
306  Head buffer;
307 
308  static_cast<istream&>(*this) >> buffer;
309  }
310  }
311  };
312 
313 
314  /**
315  * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e.\ '.evt')
316  */
317  template<>
319  public JMonteCarloFileReader<Head, JASCIIFileReader>
320  {};
321 
322 
323  /**
324  * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e.\ '.evt')
325  */
326  template<>
328  public JMonteCarloFileReader<Evt, JASCIIFileReader>
329  {};
330 
331 
332  /**
333  * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e.\ '.gz')
334  */
335  template<>
337  public JMonteCarloFileReader<Head, JGZFileReader>
338  {};
339 
340 
341  /**
342  * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e.\ '.gz')
343  */
344  template<>
346  public JMonteCarloFileReader<Evt, JGZFileReader>
347  {};
348 
349 
350  /**
351  * Template specialisation of JMonteCarloStreamObjectOutput for Head.
352  *
353  * This class provides for a ASCII formatted stream output without separator.
354  */
355  template<>
357  public JStreamObjectOutput<Head>
358  {
359  protected:
360  /**
361  * Constructor.
362  *
363  * \param out output stream
364  */
365  JMonteCarloStreamObjectOutput(std::ostream& out) :
366  JStreamObjectOutput<Head>(out, "")
367  {}
368  };
369 
370 
371  /**
372  * Template specialisation of JMonteCarloStreamObjectOutput for Evt.
373  *
374  * This class provides for a ASCII formatted stream output without separator.
375  */
376  template<>
378  public JStreamObjectOutput<Evt>
379  {
380  protected:
381  /**
382  * Constructor.
383  *
384  * \param out output stream
385  */
386  JMonteCarloStreamObjectOutput(std::ostream& out) :
387  JStreamObjectOutput<Evt>(out, "")
388  {}
389  };
390 
391 
392  /**
393  * Template specialisation of JMultipleFileScanner for Monte Carlo header.
394  *
395  * This class re-implements the methods rewind and setObject of the JLANG::JRewindableAbstractObjectIterator interface
396  * so that all header objects in the complete file list are read and added.
397  * It provides for the method JSUPPORT::getHeader which returns the sum of all headers.
398  */
399  template<>
401  public virtual JMultipleFileScanner<>,
403  {
404  public:
405  /**
406  * Default constructor.
407  */
411  do_next(true)
412  {}
413 
414 
415  /**
416  * Copy constructor.
417  * The file list is copied.
418  *
419  * \param file_list JMultipleFileScanner
420  */
424  do_next(true)
425  {
426  static_cast<JMultipleFileScanner_t&>(*this) = file_list;
427  }
428 
429 
430  /**
431  * Rewind.
432  */
433  virtual void rewind() override
434  {
435  do_next = true;
436  }
437 
438 
439  /**
440  * Set object.
441  *
442  * \param object reference to object to be set
443  * \return true if set; else false
444  */
445  virtual bool setObject(Head& object) override
446  {
447  if (do_next) {
448 
449  using namespace JLANG;
450 
451  JHead header;
452 
453  do_next = false;
454 
455  unsigned int count = 0;
456 
457  JFileScanner<Head> scanner;
458 
459  for (const_iterator i = this->begin(); i != this->end(); ++i) {
460 
461  scanner.open(i->c_str());
462 
463  if (scanner.hasNext()) {
464 
465  const JHead buffer = *scanner.next();
466 
467  if (count == 0)
468  header = buffer;
469  else if (header.match(buffer))
470  header.add(buffer);
471  else
472  THROW(JException, "JMultipleFileScanner<Head>::setObject(): inconsistent headers.");
473 
474  ++count;
475  }
476 
477  scanner.close();
478  }
479 
480  copy(header, object);
481 
482  if (count != 0 && count != this->size()) {
483  if (!merge) {
484  THROW(JException, "JMultipleFileScanner<Head>::setObject(): missing header(s): " << count << " != " << this->size());
485  }
486  }
487 
488  return count != 0;
489 
490  } else {
491 
492  return false;
493  }
494  }
495 
496 
497  /**
498  * Get Monte Carlo Header.
499  *
500  * \return header
501  */
502  const Head& getHeader()
503  {
504  const Head* p = NULL;
505 
506  if (!this->hasNext() || (p = this->next()) == NULL) {
507  THROW(JNullPointerException, "JMultipleFileScanner<Head>::getHeader(): Missing Header.");
508  }
509 
510  rewind();
511 
512  return *p;
513  }
514 
515  static bool merge; //!< Allow merging of files w/o header.
516 
517  private:
518  bool do_next;
519  };
520 
521 
522  /**
523  * Initialisation of merge option.
524  */
526 
527 
528  /**
529  * Get Monte Carlo header.
530  *
531  * \param file_list file list
532  * \return Monte Carlo header
533  */
534  inline Head getHeader(const JMultipleFileScanner_t& file_list)
535  {
536  return JMultipleFileScanner<Head>(file_list).getHeader();
537  }
538 
539 
540  /**
541  * Get list of files compatible with geven header.
542  *
543  * Note that the option corresponds to that of method JHead::match.
544  *
545  * \param input file list
546  * \param test test function
547  * \return file list
548  */
549  template<class JFunction_t>
551  {
552  using namespace JPP;
553 
555 
556  for (JMultipleFileScanner_t::const_iterator i = input.begin(); i != input.end(); ++i) {
557  if (test(getHeader(*i))) {
558  result.push_back(*i);
559  }
560  }
561 
562  return result;
563  }
564 }
565 
566 #endif
JMultipleFileScanner(const JMultipleFileScanner_t &file_list)
Copy constructor.
General exception.
Definition: JException.hh:23
Object reading from ASCII file.
data_type w[N+1][M+1]
Definition: JPolint.hh:778
virtual bool hasNext() override
Check availability of next element.
Exceptions.
const char *const w2list_t
Definition: io_ascii.hh:34
std::istream & operator>>(std::istream &in, std::vector< int > &object)
Read std::vector&lt;int&gt; from input.
std::istream & read(std::istream &in, JTestSummary &summary, const char delimiter= ' ')
Read test summary.
const char *const weights_t
Definition: io_ascii.hh:33
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
const char *const w3list_t
Definition: io_ascii.hh:35
std::vector< double > w
MC: Weights w[0]=w1, w[1]=w2, w[2]=w3 (see e.g. Tag list or km3net-dataformat/definitions) ...
Definition: Evt.hh:42
const char *const track_fit_t
Definition: io_ascii.hh:28
JHead & add(const JHead &header)
Addition of headers.
Definition: JHead.hh:1515
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.
bool is_finalstate(const Trk &track)
Test whether given track corresponds to a final state particle.
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:78
JMonteCarloStreamObjectOutput(std::ostream &out)
Constructor.
const char *const eventtime_t
Definition: io_ascii.hh:37
Exception for null pointer operation.
Definition: JException.hh:216
int mc_id
identifier of the MC event (as found in ascii or antcc file).
Definition: Evt.hh:24
TTimeStamp mc_event_time
MC: true generation time (UTC) of the event, (default: 01 Jan 1970 00:00:00)
Definition: Evt.hh:46
bool getLongprint(std::ostream &out)
Get long print option.
Definition: JManip.hh:121
Definition of hit and track types and auxiliary methods for handling Monte Carlo data.
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.
bool is_initialstate(const Trk &track)
Test whether given track corresponds to an initial state particle.
Monte Carlo run header.
Definition: JHead.hh:1221
I/O manipulators.
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...
const char *const track_in_t
Definition: io_ascii.hh:26
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:1814
std::ostream & operator<<(std::ostream &out, const std::vector< int > &object)
Write std::vector&lt;int&gt; to output.
std::vector< Trk > trks
list of reconstructed tracks (can be several because of prefits,showers, etc).
Definition: Evt.hh:39
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
std::vector< Hit > mc_hits
MC: list of MC truth hits.
Definition: Evt.hh:48
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:162
Object reading from file.
Definition: JFileScanner.hh:37
const char *const hit_t
Definition: io_ascii.hh:24
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.
const char *const end_event_t
Definition: io_ascii.hh:40
std::vector< Hit > hits
list of hits
Definition: Evt.hh:38
const char *const hit_raw_t
Definition: io_ascii.hh:25
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
const char *const start_event_t
Definition: io_ascii.hh:23
std::vector< double > w2list
MC: factors that make up w[1]=w2 (see e.g. Tag list or km3net-dataformat/definitions) ...
Definition: Evt.hh:43
std::vector< Trk > mc_trks
MC: list of MC truth tracks.
Definition: Evt.hh:49
std::vector< double > w3list
MC: atmospheric flux information.
Definition: Evt.hh:44
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.