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