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