StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IO_BaseClass.h
1 //--------------------------------------------------------------------------
2 #ifndef HEPMC_IO_BASECLASS_H
3 #define HEPMC_IO_BASECLASS_H
4 
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
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 
27 
34  class IO_BaseClass {
35  public:
36  virtual ~IO_BaseClass() {}
37 
39  virtual void write_event( const GenEvent* ) =0;
41  virtual bool fill_next_event( GenEvent* ) =0;
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)
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)
63  virtual GenEvent*& operator>>( GenEvent*& );
65  virtual const GenEvent*& operator<<( const GenEvent*& );
67  virtual GenEvent*& operator<<( GenEvent*& );
68  };
69 
71  // Inlines //
73 
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 
95  evt = read_next_event();
96  return evt;
97  }
98 
100  const GenEvent*& evt ) {
101  write_event( evt );
102  return evt;
103  }
104 
106  write_event( evt );
107  return evt;
108  }
109 
110 } // HepMC
111 
112 #endif // HEPMC_IO_BASECLASS_H
113 //--------------------------------------------------------------------------
114 
115 
116 
virtual GenEvent *& operator>>(GenEvent *&)
the same as read_next_event
Definition: IO_BaseClass.h:94
virtual void write_event(const GenEvent *)=0
write this GenEvent
GenEvent * read_next_event()
do not over-ride
Definition: IO_BaseClass.h:74
The GenEvent class is the core of HepMC.
Definition: GenEvent.h:155
all input/output classes inherit from IO_BaseClass
Definition: IO_BaseClass.h:34
virtual bool fill_next_event(GenEvent *)=0
fill this GenEvent
virtual const GenEvent *& operator<<(const GenEvent *&)
the same as write_event
Definition: IO_BaseClass.h:99
virtual void print(std::ostream &ostr=std::cout) const
write output to ostr
Definition: IO_BaseClass.h:90