Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JMonteCarloFileSupportkit.hh
Go to the documentation of this file.
1#ifndef __JSUPPORT__JMONTECARLOFILESUPPORTKIT__
2#define __JSUPPORT__JMONTECARLOFILESUPPORTKIT__
3
7
9
15#include "JLang/JException.hh"
16#include "JLang/JManip.hh"
17
18#include "JAAnet/JHead.hh"
19#include "JAAnet/JMultiHead.hh"
23
27
28
29/**
30 * \author mdejong
31 */
32
33/**
34 * Read header from input.
35 *
36 * \param in input stream
37 * \param header header
38 * \return input stream
39 */
40inline std::istream& operator>>(std::istream& in, Head& header)
41{
42 header.clear();
43
44 read(header, in);
45
46 return in;
47}
48
49
50/**
51 * \cond NEVER
52 * Write header to output.
53 *
54 * \param out output stream
55 * \param header header
56 * \return output stream
57 * \endcond
58 */
59/*
60 inline std::ostream& operator<<(std::ostream& out, const Head& header)
61 {
62 write(header, out);
63
64 return out;
65 }
66*/
67
68
69/**
70 * Write multi-header to output.
71 *
72 * \param out output stream
73 * \param header header
74 * \return output stream
75 */
76inline std::ostream& operator<<(std::ostream& out, const MultiHead& header)
77{
78 for (MultiHead::const_iterator i = header.begin(); i != header.end(); ++i) {
79 write(*i, out);
80 }
81
82 return out;
83}
84
85
86/**
87 * Read event from input.
88 *
89 * \param in input stream
90 * \param evt event
91 * \return input stream
92 */
93inline std::istream& operator>>(std::istream& in, Evt& evt)
94{
95 evt = Evt();
96
97 read(evt, in, false);
98
99 return in;
100}
101
102
103/**
104 * Write event to output.
105 *
106 * \param out output stream
107 * \param evt event
108 * \return output stream
109 */
110inline std::ostream& operator<<(std::ostream& out, const Evt& evt)
111{
112 using namespace std;
113 using namespace JPP;
114
115 const ios_base::fmtflags flags = out.flags();
116
117 out << scientific << setprecision(5);
118
119 if (getLongprint(out)) {
120
121 out << mc_keys::start_event_t << ' ' << evt.mc_id << ' ' << 1 << endl;
122
123 for (const auto& trk : evt.mc_trks) {
124
125 out << mc_keys::track_in_t << ' '
126 << setw( 3) << trk.id << ' '
127 << setw(10) << trk.type << ' '
128 << setw( 3) << trk.mother_id << ' '
129 << FIXED(12,3) << trk.pos.x << ' '
130 << FIXED(12,3) << trk.pos.y << ' '
131 << FIXED(12,3) << trk.pos.z << ' '
132 << FIXED( 7,4) << trk.dir.x << ' '
133 << FIXED( 7,4) << trk.dir.y << ' '
134 << FIXED( 7,4) << trk.dir.z << ' '
135 << FIXED( 7,1) << trk.len << ' '
136 << SCIENTIFIC(12,3) << trk.E << ' '
137 << SCIENTIFIC(12,3) << trk.t << ' '
138 << setw( 4) << trk.status << ' '
139 << is_initialstate(trk)
140 << is_finalstate (trk)
141 << endl;
142 }
143
144 for (const auto& trk : evt.trks) {
145
146 out << mc_keys::track_fit_t << ' '
147 << setw(3) << trk.id << ' '
148 << FIXED(12,3) << trk.pos.x << ' '
149 << FIXED(12,3) << trk.pos.y << ' '
150 << FIXED(12,3) << trk.pos.z << ' '
151 << FIXED( 7,4) << trk.dir.x << ' '
152 << FIXED( 7,4) << trk.dir.y << ' '
153 << FIXED( 7,4) << trk.dir.z << ' '
154 << FIXED( 7,1) << trk.len << ' '
155 << SCIENTIFIC(12,3) << trk.E << ' '
156 << FIXED(10,1) << trk.t << ' '
157 << setw(3) << trk.status << endl;
158 }
159
160 for (const auto& hit : evt.mc_hits) { write (hit, out, mc_keys::hit_t); }
161 for (const auto& hit : evt.hits) { write (hit, out, mc_keys::hit_raw_t); }
162
163 out << mc_keys::weights_t; for (const auto& w : evt.w) { out << ' ' << w;} out << endl;
164 out << mc_keys::w2list_t; for (const auto& w : evt.w2list) { out << ' ' << w;} out << endl;
165 out << mc_keys::w3list_t; for (const auto& w : evt.w3list) { out << ' ' << w;} out << endl;
166
167 out << mc_keys::eventtime_t << ' '
168 << evt.mc_event_time.GetSec() << ' '
169 << evt.mc_event_time.GetNanoSec() / 16 << endl;
170
171 out << mc_keys::end_event_t << endl;
172
173 } else {
174
175 write(evt, out << fixed);
176 }
177
178 out.flags(flags);
179
180 return out;
181}
182
183
184/**
185 * Read hit from input.
186 *
187 * \param in input stream
188 * \param hit hit
189 * \return input stream
190 */
191inline std::istream& operator>>(std::istream& in, Hit& hit)
192{
193 hit = Hit();
194
195 read(hit, in, false);
196
197 return in;
198}
199
200
201/**
202 * Write hit to output.
203 *
204 * \param out output stream
205 * \param hit hit
206 * \return output stream
207 */
208inline std::ostream& operator<<(std::ostream& out, const Hit& hit)
209{
210 using namespace std;
211
212 const ios_base::fmtflags flags = out.flags();
213
214 write(hit, out << fixed);
215
216 out.flags(flags);
217
218 return out;
219}
220
221
222namespace JSUPPORT {}
223namespace JPP { using namespace JSUPPORT; }
224
225namespace JSUPPORT {
226
227 using JAANET::JHead;
228 using JAANET::JMultiHead;
230
236 using JLANG::JException;
237
238
239 /**
240 * Template definition of Monte Carlo object reader.
241 */
242 template<class T, template<class> class JFileReader_t>
244
245
246 /**
247 * Template specialisation of JMonteCarloFileReader for Head.
248 *
249 * This class re-implements the methods open of the JLANG::JAccessible and method hasNext
250 * of the JLANG::JObjectIterator interface so that only one Head is read per file.
251 */
252 template<template<class> class JFileReader_t>
253 class JMonteCarloFileReader<Head, JFileReader_t> :
254 public JFileReader_t<Head>
255 {
256 public:
257 /**
258 * Default constructor.
259 */
261 JFileReader_t<Head>(),
262 do_next(false)
263 {}
264
265
266 /**
267 * Open file.
268 *
269 * \param file_name file name
270 */
271 virtual void open(const char* file_name) override
272 {
273 JFileReader_t<Head>::open(file_name);
274
275 do_next = true;
276 }
277
278
279 /**
280 * Check availability of next element.
281 *
282 * \return true if the iteration has more elements; else false
283 */
284 virtual bool hasNext() override
285 {
286 if (do_next) {
287
288 do_next = false;
289
290 return JFileReader_t<Head>::hasNext();
291
292 } else {
293
294 return false;
295 }
296 }
297
298 private:
300 };
301
302
303 /**
304 * Template specialisation of JMonteCarloFileReader for Event.
305 *
306 * This class re-implements the method open of the JLANG::JAccessible interface
307 * so that the Head is skipped for each file.
308 */
309 template<template<class> class JFileReader_t>
310 class JMonteCarloFileReader<Evt, JFileReader_t> :
311 public JFileReader_t<Evt>
312 {
313 public:
314 /**
315 * Open file.
316 *
317 * \param file_name file name
318 */
319 virtual void open(const char* file_name) override
320 {
321 using namespace std;
322
323 JFileReader_t<Evt>::open(file_name);
324
325 if (this->is_open()) {
326
327 Head buffer;
328
329 static_cast<istream&>(*this) >> buffer;
330 }
331 }
332 };
333
334
335 /**
336 * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e.\ '.evt')
337 */
338 template<>
340 public JMonteCarloFileReader<Head, JASCIIFileReader>
341 {};
342
343
344 /**
345 * Template implementation of Monte Carlo object reader for ASCII formatted file (i.e.\ '.evt')
346 */
347 template<>
349 public JMonteCarloFileReader<Evt, JASCIIFileReader>
350 {};
351
352
353 /**
354 * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e.\ '.gz')
355 */
356 template<>
358 public JMonteCarloFileReader<Head, JGZFileReader>
359 {};
360
361
362 /**
363 * Template implementation of Monte Carlo object reader for gzipped ASCII formatted file (i.e.\ '.gz')
364 */
365 template<>
367 public JMonteCarloFileReader<Evt, JGZFileReader>
368 {};
369
370
371 /**
372 * Template specialisation of JMonteCarloStreamObjectOutput for Head.
373 *
374 * This class provides for a ASCII formatted stream output without separator.
375 */
376 template<>
378 public JStreamObjectOutput<Head>
379 {
380 protected:
381 /**
382 * Constructor.
383 *
384 * \param out output stream
385 */
386 JMonteCarloStreamObjectOutput(std::ostream& out) :
387 JStreamObjectOutput<Head>(out, "")
388 {}
389 };
390
391
392 /**
393 * Template specialisation of JMonteCarloStreamObjectOutput for Evt.
394 *
395 * This class provides for a ASCII formatted stream output without separator.
396 */
397 template<>
399 public JStreamObjectOutput<Evt>
400 {
401 protected:
402 /**
403 * Constructor.
404 *
405 * \param out output stream
406 */
407 JMonteCarloStreamObjectOutput(std::ostream& out) :
408 JStreamObjectOutput<Evt>(out, "")
409 {}
410 };
411
412
413 /**
414 * Template specialisation of JMultipleFileScanner for Monte Carlo header.
415 *
416 * This class re-implements the methods rewind and setObject of the JLANG::JRewindableAbstractObjectIterator interface
417 * so that all header objects in the complete file list are read and added.
418 * It provides for the method JSUPPORT::getHeader which returns the sum of all headers.
419 */
420 template<>
422 public virtual JMultipleFileScanner<>,
424 {
425 public:
426 /**
427 * Default constructor.
428 */
434
435
436 /**
437 * Copy constructor.
438 * The file list is copied.
439 *
440 * \param file_list JMultipleFileScanner
441 */
445 do_next(true)
446 {
447 static_cast<JMultipleFileScanner_t&>(*this) = file_list;
448 }
449
450
451 /**
452 * Rewind.
453 */
454 virtual void rewind() override
455 {
456 do_next = true;
457 }
458
459
460 /**
461 * Set object.
462 *
463 * \param object reference to object to be set
464 * \return true if set; else false
465 */
466 virtual bool setObject(Head& object) override
467 {
468 if (do_next) {
469
470 using namespace JLANG;
471
472 JHead header;
473
474 do_next = false;
475
476 unsigned int count = 0;
477
479
480 for (const_iterator i = this->begin(); i != this->end(); ++i) {
481
482 scanner.open(i->c_str());
483
484 if (scanner.hasNext()) {
485
486 const JHead buffer = *scanner.next();
487
488 if (count == 0)
489 header = buffer;
490 else if (header.match(buffer))
491 header.add(buffer);
492 else
493 THROW(JException, "JMultipleFileScanner<Head>::setObject(): inconsistent headers.");
494
495 ++count;
496 }
497
498 scanner.close();
499 }
500
501 copy(header, object);
502
503 if (count != 0 && count != this->size()) {
504 if (!merge) {
505 THROW(JException, "JMultipleFileScanner<Head>::setObject(): missing header(s): " << count << " != " << this->size());
506 }
507 }
508
509 return count != 0;
510
511 } else {
512
513 return false;
514 }
515 }
516
517
518 /**
519 * Get Monte Carlo Header.
520 *
521 * \return header
522 */
524 {
525 const Head* p = NULL;
526
527 if (!this->hasNext() || (p = this->next()) == NULL) {
528 THROW(JNullPointerException, "JMultipleFileScanner<Head>::getHeader(): Missing Header.");
529 }
530
531 rewind();
532
533 return *p;
534 }
535
536 static bool merge; //!< Allow merging of files w/o header.
537
538 private:
540 };
541
542
543 /**
544 * Initialisation of merge option.
545 */
547
548
549 /**
550 * Get Monte Carlo header.
551 *
552 * \param file_list file list
553 * \return Monte Carlo header
554 */
555 inline Head getHeader(const JMultipleFileScanner_t& file_list)
556 {
557 return JMultipleFileScanner<Head>(file_list).getHeader();
558 }
559
560
561 /**
562 * Get common Monte Carlo header.
563 *
564 * \param file_list file list
565 * \return Monte Carlo header
566 */
568 {
569 Head head;
570
571 if (!file_list.empty()) {
572
573 JHead buffer = getHeader(file_list[0]);
574
575 buffer.createUUID();
576
577 for (size_t i = 1; i != file_list.size(); ++i) {
578 buffer = buffer.getMatch(getHeader(file_list[i]));
579 }
580
581 copy(buffer, head);
582 }
583
584 return head;
585 }
586
587
588 /**
589 * Get multi-header corresponding to a given file list.
590 *
591 * \param file_list file list
592 * \return Monte Carlo multi header
593 */
595 {
596 using namespace JPP;
597
598 JMultiHead multiHeader;
599
600 for (JMultipleFileScanner<MultiHead> in(file_list); in.hasNext(); ) {
601
602 const MultiHead* p = in.next();
603
604 for (MultiHead::const_iterator i = p->cbegin(); i != p->cend(); ++i) {
605
606 JHead header = *i;
607
608 multiHeader.insert(header);
609 }
610 }
611
612 multiHeader.merge();
613
614 return multiHeader;
615 }
616
617
618
619 /**
620 * Get list of files compatible with geven header.
621 *
622 * Note that the option corresponds to that of method JHead::match.
623 *
624 * \param input file list
625 * \param test test function
626 * \return file list
627 */
628 template<class JFunction_t>
630 {
631 using namespace JPP;
632
634
635 for (JMultipleFileScanner_t::const_iterator i = input.begin(); i != input.end(); ++i) {
636 if (test(getHeader(*i))) {
637 result.push_back(*i);
638 }
639 }
640
641 return result;
642 }
643
644
645 /**
646 * Get the unique event categories corresponding to a given list of MC files.
647 *
648 * \param file_list list of MC files
649 * \return unique event categories
650 */
652 {
653 using namespace std;
654 using namespace JPP;
655
656 JEvtCategorySet categories;
657
658 for (JMultipleFileScanner_t::const_iterator i = file_list.cbegin(); i != file_list.cend(); ++i) {
659 const JHead& header = getHeader(*i);
660
661 JEvtCategorySet cats = getCategories(header);
662
663 categories.insert(cats.begin(), cats.end());
664 }
665
666 return categories;
667 }
668}
669
670#endif
Definition of hit and track types and auxiliary methods for handling Monte Carlo data.
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
I/O manipulators.
bool getLongprint(std::ostream &out)
Get long print option.
Definition JManip.hh:121
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Monte Carlo run header.
Definition JHead.hh:1236
JHead & add(const JHead &header)
Addition of headers.
Definition JHead.hh:1550
void createUUID()
Create UUID if not already set.
Definition JHead.hh:1301
JHead getMatch(const JHead &header) const
Get matching fields.
Definition JHead.hh:1410
bool match(const JHead &header) const
Test match of headers.
Definition JHead.hh:1492
Object reading from ASCII file.
General exception.
Definition JException.hh:25
Object reading from gzipped file.
Exception for null pointer operation.
Abstract object iterator with rewinding.
Template implementation of stream output for single data type.
Object reading from file.
Template definition of Monte Carlo object reader for ASCII formatted file (i.e. '....
virtual void open(const char *file_name) override
Open file.
virtual bool hasNext() override
Check availability of next element.
virtual void open(const char *file_name) override
Open file.
Template definition of Monte Carlo object reader.
Template definition of Monte Carlo object reader for gzipped ASCII formatted file (i....
Template definition of Monte Carlo stream output for single data type.
const Head & getHeader()
Get Monte Carlo Header.
JMultipleFileScanner(const JMultipleFileScanner_t &file_list)
Copy constructor.
virtual bool setObject(Head &object) override
Set object.
static bool merge
Allow merging of files w/o header.
General purpose class for object reading from a list of file names.
virtual void rewind() override
Rewind.
virtual bool hasNext() override
Check availability of next element.
virtual const pointer_type & next() override
Get next element.
bool write(const Vec &v, std::ostream &os)
Write a Vec(tor) to a stream.
Definition io_ascii.hh:155
bool is_finalstate(const Trk &track)
Test whether given track corresponds to a final state particle.
bool is_initialstate(const Trk &track)
Test whether given track corresponds to an initial state particle.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition JHead.cc:157
std::ostream & operator<<(std::ostream &out, const morphology_type &object)
Write morphology to output stream.
std::istream & operator>>(std::istream &in, morphology_type &object)
Read morphology from input stream.
std::istream & read(std::istream &in, JTestSummary &summary)
Read test summary.
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Support classes and methods for experiment specific I/O.
JMultiHead getMultiHeader(const JMultipleFileScanner_t &file_list)
Get multi-header corresponding to a given file list.
JMultipleFileScanner_t getAAnetFiles(const JMultipleFileScanner_t &input, JFunction_t test)
Get list of files compatible with geven header.
Head getCommonHeader(const JMultipleFileScanner_t &file_list)
Get common Monte Carlo header.
Head getHeader(const JMultipleFileScanner_t &file_list)
Get Monte Carlo header.
JAANET::JEvtCategorySet getCategories(const JMultipleFileScanner_t &file_list)
Get the unique event categories corresponding to a given list of MC files.
return result
Definition JPolint.hh:862
const char *const w3list_t
Definition io_ascii.hh:35
const char *const weights_t
Definition io_ascii.hh:33
const char *const eventtime_t
Definition io_ascii.hh:37
const char *const track_in_t
Definition io_ascii.hh:26
const char *const start_event_t
Definition io_ascii.hh:23
const char *const hit_t
Definition io_ascii.hh:24
const char *const end_event_t
Definition io_ascii.hh:40
const char *const track_fit_t
Definition io_ascii.hh:28
const char *const w2list_t
Definition io_ascii.hh:34
const char *const hit_raw_t
Definition io_ascii.hh:25
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition Evt.hh:21
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
int mc_id
identifier of the MC event (as found in ascii or antcc file).
Definition Evt.hh:24
std::vector< Hit > hits
list of hits
Definition Evt.hh:38
std::vector< Hit > mc_hits
MC: list of MC truth hits.
Definition Evt.hh:48
std::vector< Trk > mc_trks
MC: list of MC truth tracks.
Definition Evt.hh:49
TTimeStamp mc_event_time
MC: true generation time (UTC) of the event, (default: 01 Jan 1970 00:00:00)
Definition Evt.hh:46
std::vector< double > w3list
MC: atmospheric flux information.
Definition Evt.hh:44
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 > trks
list of reconstructed tracks (can be several because of prefits,showers, etc).
Definition Evt.hh:39
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred ...
Definition Head.hh:65
Definition Hit.hh:10
Helper class for event categories.
Container for a set of event categories.
Auxiliary data structure to store multiple headers and bookkeep event-weight normalisations.
Definition JMultiHead.hh:39
bool insert(const JHead &header)
Insert the given header.
Definition JMultiHead.hh:69
void merge(const JHead &header)
Merge the given header into this object.
Definition JMultiHead.hh:56
Auxiliary base class for list of file names.
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488