|
HepMC Reference DocumentationHepMC |
00001 //-------------------------------------------------------------------------- 00002 #ifndef HEPMC_IO_BASECLASS_H 00003 #define HEPMC_IO_BASECLASS_H 00004 00006 // Matt.Dobbs@Cern.CH, November 1999, refer to: 00007 // M. Dobbs and J.B. Hansen, "The HepMC C++ Monte Carlo Event Record for 00008 // High Energy Physics", Computer Physics Communications (to be published). 00009 // 00010 // event input/output base class 00012 // 00013 // class from which all input/output classes shall inherit from. 00014 // i.e.: if you want to write events to hbook ntuples, 00015 // then inherit from this class and re-define read_event() 00016 // and write_event() 00017 // 00018 // (Possible extension: Could make this an input iterator) 00019 // 00020 00021 #include <iostream> 00022 #include "HepMC/GenEvent.h" 00023 00024 namespace HepMC { 00025 00027 00034 class IO_BaseClass { 00035 public: 00036 virtual ~IO_BaseClass() {} 00037 00039 virtual void write_event( const GenEvent* ) =0; 00041 virtual bool fill_next_event( GenEvent* ) =0; 00043 virtual void print( std::ostream& ostr = std::cout ) const; 00044 // 00045 // the read_next_event() differs from 00046 // the fill_***() methods in that it creates a new event 00047 // before calling the corresponding fill_*** method 00048 // (they are not intended to be over-ridden) 00049 GenEvent* read_next_event(); 00050 // 00051 // The overloaded stream operators >>,<< are identical to 00052 // read_next_event and write_event methods respectively. 00053 // (or read_particle_data_table and write_particle_data_table) 00054 // the event argument for the overloaded stream operators is a pointer, 00055 // which is passed by reference. 00056 // i.e. GenEvent* evt; 00057 // io >> evt; 00058 // will give the expected result. 00059 // (note: I don't see any reason to have separate const and non-const 00060 // versions of operator<<, but the pedantic ansi standard insists 00061 // on it) 00063 virtual GenEvent*& operator>>( GenEvent*& ); 00065 virtual const GenEvent*& operator<<( const GenEvent*& ); 00067 virtual GenEvent*& operator<<( GenEvent*& ); 00068 }; 00069 00071 // Inlines // 00073 00074 inline GenEvent* IO_BaseClass::read_next_event() { 00077 // 00078 // 1. create an empty event container 00079 GenEvent* evt = new GenEvent(); 00080 // 2. fill the evt container - if the read is successful, return the 00081 // pointer, otherwise return null and delete the evt 00082 if ( fill_next_event( evt ) ) return evt; 00083 // note: the below delete is only reached if read fails 00084 // ... thus there is not much overhead in new then delete 00085 // since this statement is rarely reached 00086 delete evt; 00087 return 0; 00088 } 00089 00090 inline void IO_BaseClass::print( std::ostream& ostr ) const { 00091 ostr << "IO_BaseClass: abstract parent I/O class. " << std::endl; 00092 } 00093 00094 inline GenEvent*& IO_BaseClass::operator>>( GenEvent*& evt ){ 00095 evt = read_next_event(); 00096 return evt; 00097 } 00098 00099 inline const GenEvent*& IO_BaseClass::operator<<( 00100 const GenEvent*& evt ) { 00101 write_event( evt ); 00102 return evt; 00103 } 00104 00105 inline GenEvent*& IO_BaseClass::operator<<( GenEvent*& evt ) { 00106 write_event( evt ); 00107 return evt; 00108 } 00109 00110 } // HepMC 00111 00112 #endif // HEPMC_IO_BASECLASS_H 00113 //-------------------------------------------------------------------------- 00114 00115 00116
1.4.7