Jpp  18.5.2
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  if (getLongprint(out)) {
117 
118  out << mc_keys::start_event_t << ' ' << evt.mc_id << ' ' << 1 << endl;
119 
120  for (const auto& trk : evt.mc_trks) {
121 
122  out << mc_keys::track_in_t << ' '
123  << trk.id << ' '
124  << trk.type << ' '
125  << trk.pos << ' '
126  << trk.dir << ' '
127  << trk.len << ' '
128  << trk.E << ' '
129  << trk.t << ' '
130  << is_initialstate(trk)
131  << is_finalstate (trk)
132  << endl;
133  }
134 
135  for (const auto& trk : evt.trks) {
136 
137  out << mc_keys::track_fit_t << ' '
138  << trk.id << ' '
139  << trk.pos << ' '
140  << trk.dir << ' '
141  << trk.len << ' '
142  << trk.E << ' '
143  << trk.t << endl;
144  }
145 
146  for (const auto& hit : evt.mc_hits) { write (hit, out, mc_keys::hit_t); }
147  for (const auto& hit : evt.hits) { write (hit, out, mc_keys::hit_raw_t); }
148 
149  out << mc_keys::weights_t; for (const auto& w : evt.w) { out << ' ' << w;} out << endl;
150  out << mc_keys::w2list_t; for (const auto& w : evt.w2list) { out << ' ' << w;} out << endl;
151  out << mc_keys::w3list_t; for (const auto& w : evt.w3list) { out << ' ' << w;} out << endl;
152 
153  out << mc_keys::eventtime_t << ' '
154  << evt.mc_event_time.GetSec() << ' '
155  << evt.mc_event_time.GetNanoSec() / 16 << endl;
156 
157  out << mc_keys::end_event_t << endl;
158 
159  } else {
160 
161  write(evt, out << fixed);
162  }
163 
164  out.flags(flags);
165 
166  return out;
167 }
168 
169 
170 /**
171  * Read hit from input.
172  *
173  * \param in input stream
174  * \param hit hit
175  * \return input stream
176  */
177 inline std::istream& operator>>(std::istream& in, Hit& hit)
178 {
179  hit = Hit();
180 
181  read(hit, in, false);
182 
183  return in;
184 }
185 
186 
187 /**
188  * Write hit to output.
189  *
190  * \param out output stream
191  * \param hit hit
192  * \return output stream
193  */
194 inline std::ostream& operator<<(std::ostream& out, const Hit& hit)
195 {
196  using namespace std;
197 
198  const ios_base::fmtflags flags = out.flags();
199 
200  write(hit, out << fixed);
201 
202  out.flags(flags);
203 
204  return out;
205 }
206 
207 
208 namespace JSUPPORT {}
209 namespace JPP { using namespace JSUPPORT; }
210 
211 namespace JSUPPORT {
212 
213  using JAANET::JHead;
214  using JAANET::JMultiHead;
215 
217  using JLANG::JGZFileReader;
221  using JLANG::JException;
222 
223 
224  /**
225  * Template definition of Monte Carlo object reader.
226  */
227  template<class T, template<class> class JFileReader_t>
229 
230 
231  /**
232  * Template specialisation of JMonteCarloFileReader for Head.
233  *
234  * This class re-implements the methods open of the JLANG::JAccessible and method hasNext
235  * of the JLANG::JObjectIterator interface so that only one Head is read per file.
236  */
237  template<template<class> class JFileReader_t>
238  class JMonteCarloFileReader<Head, JFileReader_t> :
239  public JFileReader_t<Head>
240  {
241  public:
242  /**
243  * Default constructor.
244  */
246  JFileReader_t<Head>(),
247  do_next(false)
248  {}
249 
250 
251  /**
252  * Open file.
253  *
254  * \param file_name file name
255  */
256  virtual void open(const char* file_name) override
257  {
258  JFileReader_t<Head>::open(file_name);
259 
260  do_next = true;
261  }
262 
263 
264  /**
265  * Check availability of next element.
266  *
267  * \return true if the iteration has more elements; else false
268  */
269  virtual bool hasNext() override
270  {
271  if (do_next) {
272 
273  do_next = false;
274 
275  return JFileReader_t<Head>::hasNext();
276 
277  } else {
278 
279  return false;
280  }
281  }
282 
283  private:
284  bool do_next;
285  };
286 
287 
288  /**
289  * Template specialisation of JMonteCarloFileReader for Event.
290  *
291  * This class re-implements the method open of the JLANG::JAccessible interface
292  * so that the Head is skipped for each file.
293  */
294  template<template<class> class JFileReader_t>
295  class JMonteCarloFileReader<Evt, JFileReader_t> :
296  public JFileReader_t<Evt>
297  {
298  public:
299  /**
300  * Open file.
301  *
302  * \param file_name file name
303  */
304  virtual void open(const char* file_name) override
305  {
306  using namespace std;
307 
308  JFileReader_t<Evt>::open(file_name);
309 
310  if (this->is_open()) {
311 
312  Head buffer;
313 
314  static_cast<istream&>(*this) >> buffer;
315  }
316  }
317  };
318 
319 
320  /**
321  * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e.\ '.evt')
322  */
323  template<>
325  public JMonteCarloFileReader<Head, JASCIIFileReader>
326  {};
327 
328 
329  /**
330  * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e.\ '.evt')
331  */
332  template<>
334  public JMonteCarloFileReader<Evt, JASCIIFileReader>
335  {};
336 
337 
338  /**
339  * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e.\ '.gz')
340  */
341  template<>
343  public JMonteCarloFileReader<Head, JGZFileReader>
344  {};
345 
346 
347  /**
348  * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e.\ '.gz')
349  */
350  template<>
352  public JMonteCarloFileReader<Evt, JGZFileReader>
353  {};
354 
355 
356  /**
357  * Template specialisation of JMonteCarloStreamObjectOutput for Head.
358  *
359  * This class provides for a ASCII formatted stream output without separator.
360  */
361  template<>
363  public JStreamObjectOutput<Head>
364  {
365  protected:
366  /**
367  * Constructor.
368  *
369  * \param out output stream
370  */
371  JMonteCarloStreamObjectOutput(std::ostream& out) :
372  JStreamObjectOutput<Head>(out, "")
373  {}
374  };
375 
376 
377  /**
378  * Template specialisation of JMonteCarloStreamObjectOutput for Evt.
379  *
380  * This class provides for a ASCII formatted stream output without separator.
381  */
382  template<>
384  public JStreamObjectOutput<Evt>
385  {
386  protected:
387  /**
388  * Constructor.
389  *
390  * \param out output stream
391  */
392  JMonteCarloStreamObjectOutput(std::ostream& out) :
393  JStreamObjectOutput<Evt>(out, "")
394  {}
395  };
396 
397 
398  /**
399  * Template specialisation of JMultipleFileScanner for Monte Carlo header.
400  *
401  * This class re-implements the methods rewind and setObject of the JLANG::JRewindableAbstractObjectIterator interface
402  * so that all header objects in the complete file list are read and added.
403  * It provides for the method JSUPPORT::getHeader which returns the sum of all headers.
404  */
405  template<>
407  public virtual JMultipleFileScanner<>,
409  {
410  public:
411  /**
412  * Default constructor.
413  */
417  do_next(true)
418  {}
419 
420 
421  /**
422  * Copy constructor.
423  * The file list is copied.
424  *
425  * \param file_list JMultipleFileScanner
426  */
430  do_next(true)
431  {
432  static_cast<JMultipleFileScanner_t&>(*this) = file_list;
433  }
434 
435 
436  /**
437  * Rewind.
438  */
439  virtual void rewind() override
440  {
441  do_next = true;
442  }
443 
444 
445  /**
446  * Set object.
447  *
448  * \param object reference to object to be set
449  * \return true if set; else false
450  */
451  virtual bool setObject(Head& object) override
452  {
453  if (do_next) {
454 
455  using namespace JLANG;
456 
457  JHead header;
458 
459  do_next = false;
460 
461  unsigned int count = 0;
462 
463  JFileScanner<Head> scanner;
464 
465  for (const_iterator i = this->begin(); i != this->end(); ++i) {
466 
467  scanner.open(i->c_str());
468 
469  if (scanner.hasNext()) {
470 
471  const JHead buffer = *scanner.next();
472 
473  if (count == 0)
474  header = buffer;
475  else if (header.match(buffer))
476  header.add(buffer);
477  else
478  THROW(JException, "JMultipleFileScanner<Head>::setObject(): inconsistent headers.");
479 
480  ++count;
481  }
482 
483  scanner.close();
484  }
485 
486  copy(header, object);
487 
488  if (count != 0 && count != this->size()) {
489  if (!merge) {
490  THROW(JException, "JMultipleFileScanner<Head>::setObject(): missing header(s): " << count << " != " << this->size());
491  }
492  }
493 
494  return count != 0;
495 
496  } else {
497 
498  return false;
499  }
500  }
501 
502 
503  /**
504  * Get Monte Carlo Header.
505  *
506  * \return header
507  */
508  const Head& getHeader()
509  {
510  const Head* p = NULL;
511 
512  if (!this->hasNext() || (p = this->next()) == NULL) {
513  THROW(JNullPointerException, "JMultipleFileScanner<Head>::getHeader(): Missing Header.");
514  }
515 
516  rewind();
517 
518  return *p;
519  }
520 
521  static bool merge; //!< Allow merging of files w/o header.
522 
523  private:
524  bool do_next;
525  };
526 
527 
528  /**
529  * Initialisation of merge option.
530  */
532 
533 
534  /**
535  * Get Monte Carlo header.
536  *
537  * \param file_list file list
538  * \return Monte Carlo header
539  */
540  inline Head getHeader(const JMultipleFileScanner_t& file_list)
541  {
542  return JMultipleFileScanner<Head>(file_list).getHeader();
543  }
544 
545 
546  /**
547  * Get multi-header corresponding to a given file list.
548  *
549  * \param file_list file list
550  */
552  {
553  using namespace JPP;
554 
555  JMultiHead multiHeader0;
556 
557  for (JMultipleFileScanner<MultiHead> in(file_list); in.hasNext(); ) {
558 
559  const MultiHead& multiHeader1 = *in.next();
560 
561  for (MultiHead::const_iterator i = multiHeader1.cbegin(); i != multiHeader1.cend(); ++i) {
562  JHead header = *i;
563  multiHeader0.insert(header);
564  }
565  }
566 
567  multiHeader0.merge();
568 
569  return multiHeader0;
570  }
571 
572 
573 
574  /**
575  * Get list of files compatible with geven header.
576  *
577  * Note that the option corresponds to that of method JHead::match.
578  *
579  * \param input file list
580  * \param test test function
581  * \return file list
582  */
583  template<class JFunction_t>
585  {
586  using namespace JPP;
587 
589 
590  for (JMultipleFileScanner_t::const_iterator i = input.begin(); i != input.end(); ++i) {
591  if (test(getHeader(*i))) {
592  result.push_back(*i);
593  }
594  }
595 
596  return result;
597  }
598 }
599 
600 #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
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: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: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:1529
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:1829
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 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: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.