1 | //-------------------------------------------------------------------------- |
---|
2 | #ifndef HEPMC_IO_BASECLASS_H |
---|
3 | #define HEPMC_IO_BASECLASS_H |
---|
4 | |
---|
5 | ////////////////////////////////////////////////////////////////////////// |
---|
6 | // Matt.Dobbs@Cern.CH, November 1999, refer to: |
---|
7 | // M. Dobbs and J.B. Hansen, "The HepMC C++ Monte Carlo Event Record for |
---|
8 | // High Energy Physics", Computer Physics Communications (to be published). |
---|
9 | // |
---|
10 | // event input/output base class |
---|
11 | ////////////////////////////////////////////////////////////////////////// |
---|
12 | // |
---|
13 | // class from which all input/output classes shall inherit from. |
---|
14 | // i.e.: if you want to write events to hbook ntuples, |
---|
15 | // then inherit from this class and re-define read_event() |
---|
16 | // and write_event() |
---|
17 | // |
---|
18 | // (Possible extension: Could make this an input iterator) |
---|
19 | // |
---|
20 | |
---|
21 | #include <iostream> |
---|
22 | #include "HepMC/GenEvent.h" |
---|
23 | |
---|
24 | namespace HepMC { |
---|
25 | |
---|
26 | //! all input/output classes inherit from IO_BaseClass |
---|
27 | |
---|
28 | /// |
---|
29 | /// \class IO_BaseClass |
---|
30 | /// If you want to write a new IO class, |
---|
31 | /// then inherit from this class and re-define read_event() |
---|
32 | /// and write_event() |
---|
33 | /// |
---|
34 | class IO_BaseClass { |
---|
35 | public: |
---|
36 | virtual ~IO_BaseClass() {} |
---|
37 | |
---|
38 | /// write this GenEvent |
---|
39 | virtual void write_event( const GenEvent* ) =0; |
---|
40 | /// fill this GenEvent |
---|
41 | virtual bool fill_next_event( GenEvent* ) =0; |
---|
42 | /// write output to ostr |
---|
43 | virtual void print( std::ostream& ostr = std::cout ) const; |
---|
44 | // |
---|
45 | // the read_next_event() differs from |
---|
46 | // the fill_***() methods in that it creates a new event |
---|
47 | // before calling the corresponding fill_*** method |
---|
48 | // (they are not intended to be over-ridden) |
---|
49 | GenEvent* read_next_event(); //!< do not over-ride |
---|
50 | // |
---|
51 | // The overloaded stream operators >>,<< are identical to |
---|
52 | // read_next_event and write_event methods respectively. |
---|
53 | // (or read_particle_data_table and write_particle_data_table) |
---|
54 | // the event argument for the overloaded stream operators is a pointer, |
---|
55 | // which is passed by reference. |
---|
56 | // i.e. GenEvent* evt; |
---|
57 | // io >> evt; |
---|
58 | // will give the expected result. |
---|
59 | // (note: I don't see any reason to have separate const and non-const |
---|
60 | // versions of operator<<, but the pedantic ansi standard insists |
---|
61 | // on it) |
---|
62 | /// the same as read_next_event |
---|
63 | virtual GenEvent*& operator>>( GenEvent*& ); |
---|
64 | /// the same as write_event |
---|
65 | virtual const GenEvent*& operator<<( const GenEvent*& ); |
---|
66 | /// the same as write_event |
---|
67 | virtual GenEvent*& operator<<( GenEvent*& ); |
---|
68 | }; |
---|
69 | |
---|
70 | ////////////// |
---|
71 | // Inlines // |
---|
72 | ////////////// |
---|
73 | |
---|
74 | inline GenEvent* IO_BaseClass::read_next_event() { |
---|
75 | /// creates a new event and fills it by calling |
---|
76 | /// the sister method read_next_event( GenEvent* ) |
---|
77 | // |
---|
78 | // 1. create an empty event container |
---|
79 | GenEvent* evt = new GenEvent(); |
---|
80 | // 2. fill the evt container - if the read is successful, return the |
---|
81 | // pointer, otherwise return null and delete the evt |
---|
82 | if ( fill_next_event( evt ) ) return evt; |
---|
83 | // note: the below delete is only reached if read fails |
---|
84 | // ... thus there is not much overhead in new then delete |
---|
85 | // since this statement is rarely reached |
---|
86 | delete evt; |
---|
87 | return 0; |
---|
88 | } |
---|
89 | |
---|
90 | inline void IO_BaseClass::print( std::ostream& ostr ) const { |
---|
91 | ostr << "IO_BaseClass: abstract parent I/O class. " << std::endl; |
---|
92 | } |
---|
93 | |
---|
94 | inline GenEvent*& IO_BaseClass::operator>>( GenEvent*& evt ){ |
---|
95 | evt = read_next_event(); |
---|
96 | return evt; |
---|
97 | } |
---|
98 | |
---|
99 | inline const GenEvent*& IO_BaseClass::operator<<( |
---|
100 | const GenEvent*& evt ) { |
---|
101 | write_event( evt ); |
---|
102 | return evt; |
---|
103 | } |
---|
104 | |
---|
105 | inline GenEvent*& IO_BaseClass::operator<<( GenEvent*& evt ) { |
---|
106 | write_event( evt ); |
---|
107 | return evt; |
---|
108 | } |
---|
109 | |
---|
110 | } // HepMC |
---|
111 | |
---|
112 | #endif // HEPMC_IO_BASECLASS_H |
---|
113 | //-------------------------------------------------------------------------- |
---|
114 | |
---|
115 | |
---|
116 | |
---|