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/ParticleDataTable.h" 00023 #include "HepMC/GenEvent.h" 00024 00025 namespace HepMC { 00026 00028 00035 class IO_BaseClass { 00036 public: 00037 virtual ~IO_BaseClass() {} 00038 00040 virtual void write_event( const GenEvent* ) =0; 00042 virtual bool fill_next_event( GenEvent* ) =0; 00044 virtual void write_particle_data_table( const ParticleDataTable* ) =0; 00046 virtual bool fill_particle_data_table( ParticleDataTable* ) =0; 00048 virtual void print( std::ostream& ostr = std::cout ) const; 00049 // 00050 // the read_next_event() and read_particle_data_table() differ from 00051 // the fill_***() methods in that they create a new event or pdt 00052 // before calling the corresponding fill_*** method 00053 // (they are not intended to be over-ridden) 00054 GenEvent* read_next_event(); 00055 ParticleDataTable* read_particle_data_table(); 00056 // 00057 // The overloaded stream operators >>,<< are identical to 00058 // read_next_event and write_event methods respectively. 00059 // (or read_particle_data_table and write_particle_data_table) 00060 // the event argument for the overloaded stream operators is a pointer, 00061 // which is passed by reference. 00062 // i.e. GenEvent* evt; 00063 // io >> evt; 00064 // will give the expected result. 00065 // (note: I don't see any reason to have separate const and non-const 00066 // versions of operator<<, but the pedantic ansi standard insists 00067 // on it) 00069 virtual GenEvent*& operator>>( GenEvent*& ); 00071 virtual const GenEvent*& operator<<( const GenEvent*& ); 00073 virtual GenEvent*& operator<<( GenEvent*& ); 00075 virtual ParticleDataTable*& operator>>( ParticleDataTable*& ); 00077 virtual const ParticleDataTable*& operator<<( const 00078 ParticleDataTable*& ); 00080 virtual ParticleDataTable*& operator<<( ParticleDataTable*& ); 00081 }; 00082 00084 // Inlines // 00086 00087 inline GenEvent* IO_BaseClass::read_next_event() { 00090 // 00091 // 1. create an empty event container 00092 GenEvent* evt = new GenEvent(); 00093 // 2. fill the evt container - if the read is successful, return the 00094 // pointer, otherwise return null and delete the evt 00095 if ( fill_next_event( evt ) ) return evt; 00096 // note: the below delete is only reached if read fails 00097 // ... thus there is not much overhead in new then delete 00098 // since this statement is rarely reached 00099 delete evt; 00100 return 0; 00101 } 00102 00103 inline ParticleDataTable* IO_BaseClass::read_particle_data_table() { 00106 // 00107 // 1. create an empty pdt 00108 ParticleDataTable* pdt = new ParticleDataTable(); 00109 // 2. fill the pdt container - if the read is successful, return the 00110 // pointer, otherwise return null and delete the evt 00111 if ( fill_particle_data_table( pdt ) ) return pdt; 00112 // next statement is only reached if read fails 00113 delete pdt; 00114 return 0; 00115 } 00116 00117 inline void IO_BaseClass::print( std::ostream& ostr ) const { 00118 ostr << "IO_BaseClass: abstract parent I/O class. " << std::endl; 00119 } 00120 00121 inline GenEvent*& IO_BaseClass::operator>>( GenEvent*& evt ){ 00122 evt = read_next_event(); 00123 return evt; 00124 } 00125 00126 inline const GenEvent*& IO_BaseClass::operator<<( 00127 const GenEvent*& evt ) { 00128 write_event( evt ); 00129 return evt; 00130 } 00131 00132 inline GenEvent*& IO_BaseClass::operator<<( GenEvent*& evt ) { 00133 write_event( evt ); 00134 return evt; 00135 } 00136 00137 inline ParticleDataTable*& IO_BaseClass::operator>>( 00138 ParticleDataTable*& pdt ){ 00139 pdt = read_particle_data_table(); 00140 return pdt; 00141 } 00142 00143 inline const ParticleDataTable*& IO_BaseClass::operator<<( 00144 const ParticleDataTable*& pdt ) { 00145 write_particle_data_table( pdt ); 00146 return pdt; 00147 } 00148 00149 inline ParticleDataTable*& IO_BaseClass::operator<<( 00150 ParticleDataTable*& pdt ) { 00151 write_particle_data_table( pdt ); 00152 return pdt; 00153 } 00154 00155 } // HepMC 00156 00157 #endif // HEPMC_IO_BASECLASS_H 00158 //-------------------------------------------------------------------------- 00159 00160 00161