Jpp  19.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/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 << ' '
152  << trk.status << endl;
153  }
154 
155  for (const auto& hit : evt.mc_hits) { write (hit, out, mc_keys::hit_t); }
156  for (const auto& hit : evt.hits) { write (hit, out, mc_keys::hit_raw_t); }
157 
158  out << mc_keys::weights_t; for (const auto& w : evt.w) { out << ' ' << w;} out << endl;
159  out << mc_keys::w2list_t; for (const auto& w : evt.w2list) { out << ' ' << w;} out << endl;
160  out << mc_keys::w3list_t; for (const auto& w : evt.w3list) { out << ' ' << w;} out << endl;
161 
162  out << mc_keys::eventtime_t << ' '
163  << evt.mc_event_time.GetSec() << ' '
164  << evt.mc_event_time.GetNanoSec() / 16 << endl;
165 
166  out << mc_keys::end_event_t << endl;
167 
168  } else {
169 
170  write(evt, out << fixed);
171  }
172 
173  out.flags(flags);
174 
175  return out;
176 }
177 
178 
179 /**
180  * Read hit from input.
181  *
182  * \param in input stream
183  * \param hit hit
184  * \return input stream
185  */
186 inline std::istream& operator>>(std::istream& in, Hit& hit)
187 {
188  hit = Hit();
189 
190  read(hit, in, false);
191 
192  return in;
193 }
194 
195 
196 /**
197  * Write hit to output.
198  *
199  * \param out output stream
200  * \param hit hit
201  * \return output stream
202  */
203 inline std::ostream& operator<<(std::ostream& out, const Hit& hit)
204 {
205  using namespace std;
206 
207  const ios_base::fmtflags flags = out.flags();
208 
209  write(hit, out << fixed);
210 
211  out.flags(flags);
212 
213  return out;
214 }
215 
216 
217 namespace JSUPPORT {}
218 namespace JPP { using namespace JSUPPORT; }
219 
220 namespace JSUPPORT {
221 
222  using JAANET::JHead;
223  using JAANET::JMultiHead;
224 
226  using JLANG::JGZFileReader;
230  using JLANG::JException;
231 
232 
233  /**
234  * Template definition of Monte Carlo object reader.
235  */
236  template<class T, template<class> class JFileReader_t>
238 
239 
240  /**
241  * Template specialisation of JMonteCarloFileReader for Head.
242  *
243  * This class re-implements the methods open of the JLANG::JAccessible and method hasNext
244  * of the JLANG::JObjectIterator interface so that only one Head is read per file.
245  */
246  template<template<class> class JFileReader_t>
247  class JMonteCarloFileReader<Head, JFileReader_t> :
248  public JFileReader_t<Head>
249  {
250  public:
251  /**
252  * Default constructor.
253  */
255  JFileReader_t<Head>(),
256  do_next(false)
257  {}
258 
259 
260  /**
261  * Open file.
262  *
263  * \param file_name file name
264  */
265  virtual void open(const char* file_name) override
266  {
267  JFileReader_t<Head>::open(file_name);
268 
269  do_next = true;
270  }
271 
272 
273  /**
274  * Check availability of next element.
275  *
276  * \return true if the iteration has more elements; else false
277  */
278  virtual bool hasNext() override
279  {
280  if (do_next) {
281 
282  do_next = false;
283 
284  return JFileReader_t<Head>::hasNext();
285 
286  } else {
287 
288  return false;
289  }
290  }
291 
292  private:
293  bool do_next;
294  };
295 
296 
297  /**
298  * Template specialisation of JMonteCarloFileReader for Event.
299  *
300  * This class re-implements the method open of the JLANG::JAccessible interface
301  * so that the Head is skipped for each file.
302  */
303  template<template<class> class JFileReader_t>
304  class JMonteCarloFileReader<Evt, JFileReader_t> :
305  public JFileReader_t<Evt>
306  {
307  public:
308  /**
309  * Open file.
310  *
311  * \param file_name file name
312  */
313  virtual void open(const char* file_name) override
314  {
315  using namespace std;
316 
317  JFileReader_t<Evt>::open(file_name);
318 
319  if (this->is_open()) {
320 
321  Head buffer;
322 
323  static_cast<istream&>(*this) >> buffer;
324  }
325  }
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<Head, JASCIIFileReader>
335  {};
336 
337 
338  /**
339  * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e.\ '.evt')
340  */
341  template<>
343  public JMonteCarloFileReader<Evt, JASCIIFileReader>
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<Head, JGZFileReader>
353  {};
354 
355 
356  /**
357  * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e.\ '.gz')
358  */
359  template<>
361  public JMonteCarloFileReader<Evt, JGZFileReader>
362  {};
363 
364 
365  /**
366  * Template specialisation of JMonteCarloStreamObjectOutput for Head.
367  *
368  * This class provides for a ASCII formatted stream output without separator.
369  */
370  template<>
372  public JStreamObjectOutput<Head>
373  {
374  protected:
375  /**
376  * Constructor.
377  *
378  * \param out output stream
379  */
380  JMonteCarloStreamObjectOutput(std::ostream& out) :
381  JStreamObjectOutput<Head>(out, "")
382  {}
383  };
384 
385 
386  /**
387  * Template specialisation of JMonteCarloStreamObjectOutput for Evt.
388  *
389  * This class provides for a ASCII formatted stream output without separator.
390  */
391  template<>
393  public JStreamObjectOutput<Evt>
394  {
395  protected:
396  /**
397  * Constructor.
398  *
399  * \param out output stream
400  */
401  JMonteCarloStreamObjectOutput(std::ostream& out) :
402  JStreamObjectOutput<Evt>(out, "")
403  {}
404  };
405 
406 
407  /**
408  * Template specialisation of JMultipleFileScanner for Monte Carlo header.
409  *
410  * This class re-implements the methods rewind and setObject of the JLANG::JRewindableAbstractObjectIterator interface
411  * so that all header objects in the complete file list are read and added.
412  * It provides for the method JSUPPORT::getHeader which returns the sum of all headers.
413  */
414  template<>
416  public virtual JMultipleFileScanner<>,
418  {
419  public:
420  /**
421  * Default constructor.
422  */
426  do_next(true)
427  {}
428 
429 
430  /**
431  * Copy constructor.
432  * The file list is copied.
433  *
434  * \param file_list JMultipleFileScanner
435  */
439  do_next(true)
440  {
441  static_cast<JMultipleFileScanner_t&>(*this) = file_list;
442  }
443 
444 
445  /**
446  * Rewind.
447  */
448  virtual void rewind() override
449  {
450  do_next = true;
451  }
452 
453 
454  /**
455  * Set object.
456  *
457  * \param object reference to object to be set
458  * \return true if set; else false
459  */
460  virtual bool setObject(Head& object) override
461  {
462  if (do_next) {
463 
464  using namespace JLANG;
465 
466  JHead header;
467 
468  do_next = false;
469 
470  unsigned int count = 0;
471 
472  JFileScanner<Head> scanner;
473 
474  for (const_iterator i = this->begin(); i != this->end(); ++i) {
475 
476  scanner.open(i->c_str());
477 
478  if (scanner.hasNext()) {
479 
480  const JHead buffer = *scanner.next();
481 
482  if (count == 0)
483  header = buffer;
484  else if (header.match(buffer))
485  header.add(buffer);
486  else
487  THROW(JException, "JMultipleFileScanner<Head>::setObject(): inconsistent headers.");
488 
489  ++count;
490  }
491 
492  scanner.close();
493  }
494 
495  copy(header, object);
496 
497  if (count != 0 && count != this->size()) {
498  if (!merge) {
499  THROW(JException, "JMultipleFileScanner<Head>::setObject(): missing header(s): " << count << " != " << this->size());
500  }
501  }
502 
503  return count != 0;
504 
505  } else {
506 
507  return false;
508  }
509  }
510 
511 
512  /**
513  * Get Monte Carlo Header.
514  *
515  * \return header
516  */
517  const Head& getHeader()
518  {
519  const Head* p = NULL;
520 
521  if (!this->hasNext() || (p = this->next()) == NULL) {
522  THROW(JNullPointerException, "JMultipleFileScanner<Head>::getHeader(): Missing Header.");
523  }
524 
525  rewind();
526 
527  return *p;
528  }
529 
530  static bool merge; //!< Allow merging of files w/o header.
531 
532  private:
533  bool do_next;
534  };
535 
536 
537  /**
538  * Initialisation of merge option.
539  */
541 
542 
543  /**
544  * Get Monte Carlo header.
545  *
546  * \param file_list file list
547  * \return Monte Carlo header
548  */
549  inline Head getHeader(const JMultipleFileScanner_t& file_list)
550  {
551  return JMultipleFileScanner<Head>(file_list).getHeader();
552  }
553 
554 
555  /**
556  * Get common Monte Carlo header.
557  *
558  * \param file_list file list
559  * \return Monte Carlo header
560  */
561  inline Head getCommonHeader(const JMultipleFileScanner_t& file_list)
562  {
563  Head head;
564 
565  if (!file_list.empty()) {
566 
567  JHead buffer = getHeader(file_list[0]);
568 
569  buffer.createUUID();
570 
571  for (size_t i = 1; i != file_list.size(); ++i) {
572  buffer = buffer.getMatch(getHeader(file_list[i]));
573  }
574 
575  copy(buffer, head);
576  }
577 
578  return head;
579  }
580 
581 
582  /**
583  * Get multi-header corresponding to a given file list.
584  *
585  * \param file_list file list
586  * \return Monte Carlo multi header
587  */
589  {
590  using namespace JPP;
591 
592  JMultiHead multiHeader;
593 
594  for (JMultipleFileScanner<MultiHead> in(file_list); in.hasNext(); ) {
595 
596  const MultiHead* p = in.next();
597 
598  for (MultiHead::const_iterator i = p->cbegin(); i != p->cend(); ++i) {
599 
600  JHead header = *i;
601 
602  multiHeader.insert(header);
603  }
604  }
605 
606  multiHeader.merge();
607 
608  return multiHeader;
609  }
610 
611 
612 
613  /**
614  * Get list of files compatible with geven header.
615  *
616  * Note that the option corresponds to that of method JHead::match.
617  *
618  * \param input file list
619  * \param test test function
620  * \return file list
621  */
622  template<class JFunction_t>
624  {
625  using namespace JPP;
626 
628 
629  for (JMultipleFileScanner_t::const_iterator i = input.begin(); i != input.end(); ++i) {
630  if (test(getHeader(*i))) {
631  result.push_back(*i);
632  }
633  }
634 
635  return result;
636  }
637 }
638 
639 #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
Head getCommonHeader(const JMultipleFileScanner_t &file_list)
Get common Monte Carlo header.
#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
void createUUID()
Create UUID if not already set.
Definition: JHead.hh:1301
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.
JHead getMatch(const JHead &header) const
Get matching fields.
Definition: JHead.hh:1410
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:1532
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:64
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:1832
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.