00001 //-------------------------------------------------------------------------- 00002 #ifndef HEPMC_IO_ASCII_H 00003 #define HEPMC_IO_ASCII_H 00004 00006 // Matt.Dobbs@Cern.CH, January 2000, 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 in ascii format for machine reading 00012 // 00013 // Strategy for reading or writing events/particleData as machine readable 00014 // ascii to a file. When instantiating, the mode of file to be created 00015 // must be specified. Options are: 00016 // std::ios::in open file for input 00017 // std::ios::out open file for output 00018 // std::ios::trunc erase old file when opening (i.e. ios::out|ios::trunc 00019 // removes oldfile, and creates a new one for output ) 00020 // std::ios::app append output to end of file 00021 // for the purposes of this class, simultaneous input and output mode 00022 // ( std::ios::in | std::ios::out ) is not allowed. 00023 // 00024 // Event listings are preceded by the key: 00025 // "HepMC::IO_Ascii-START_EVENT_LISTING\n" 00026 // and terminated by the key: 00027 // "HepMC::IO_Ascii-END_EVENT_LISTING\n" 00028 // GenParticle Data tables are preceded by the key: 00029 // "HepMC::IO_Ascii-START_PARTICLE_DATA\n" 00030 // and terminated by the key: 00031 // "HepMC::IO_Ascii-END_PARTICLE_DATA\n" 00032 // Comments are allowed. They need not be preceded by anything, though if 00033 // a comment is written using write_comment( const string ) then it will be 00034 // preceded by "HepMC::IO_Ascii-COMMENT\n" 00035 // Each event, vertex, particle, particle data is preceded by 00036 // "E ","V ","P ","D " respectively. 00037 // Comments may appear anywhere in the file -- so long as they do not contain 00038 // any of the 4 start/stop keys. 00039 // 00040 00041 #include <fstream> 00042 #include <string> 00043 #include <map> 00044 #include <vector> 00045 #include "HepMC/IO_BaseClass.h" 00046 #include "HepMC/TempParticleMap.h" 00047 #include "HepMC/CommonIO.h" 00048 00049 namespace HepMC { 00050 00051 class GenEvent; 00052 class GenVertex; 00053 class GenParticle; 00054 class ParticleData; 00055 00057 00064 class IO_Ascii : public IO_BaseClass { 00065 public: 00067 IO_Ascii( const char* filename="IO_Ascii.dat", 00068 std::ios::openmode mode=std::ios::out ); 00069 virtual ~IO_Ascii(); 00070 00072 void write_event( const GenEvent* evt ); 00074 bool fill_next_event( GenEvent* evt ); 00075 void write_particle_data_table(const ParticleDataTable*); 00076 bool fill_particle_data_table( ParticleDataTable* ); 00080 void write_comment( const std::string comment ); 00081 00082 int rdstate() const; 00083 void clear(); 00084 00086 void print( std::ostream& ostr = std::cout ) const; 00087 00088 protected: // for internal use only 00090 void write_vertex( GenVertex* ); 00092 void write_particle( GenParticle* p ); 00094 void write_particle_data( const ParticleData* d ); 00096 bool write_end_listing(); 00097 00098 void output( const double& ); 00099 void output( const int& ); 00100 void output( const long int& ); 00101 void output( const char& ); 00102 private: // use of copy constructor is not allowed 00103 IO_Ascii( const IO_Ascii& ) : IO_BaseClass() {} 00104 private: // data members 00105 std::ios::openmode m_mode; 00106 std::fstream m_file; 00107 bool m_finished_first_event_io; 00108 CommonIO m_common_io; 00109 }; 00110 00112 // Inlines // 00114 00115 inline void IO_Ascii::output( const double& d ) { 00116 if ( d == 0. ) { 00117 m_file << ' ' << (int)0; 00118 } else { 00119 m_file << ' ' << d; 00120 } 00121 } 00122 inline void IO_Ascii::output( const int& i ) { m_file << ' ' << i; } 00123 inline void IO_Ascii::output( const long int& i ) { m_file << ' ' << i; } 00124 inline void IO_Ascii::output( const char& c ) { m_file << c; } 00125 inline int IO_Ascii::rdstate() const { return (int)m_file.rdstate(); } 00126 inline void IO_Ascii::clear() { m_file.clear(); } 00127 00128 } // HepMC 00129 00130 #endif // HEPMC_IO_ASCII_H 00131 //-------------------------------------------------------------------------- 00132 00133 00134