[349] | 1 | //--------------------------------------------------------------------------
|
---|
| 2 | #ifndef HEPMC_IO_ASCII_H
|
---|
| 3 | #define HEPMC_IO_ASCII_H
|
---|
| 4 |
|
---|
| 5 | //////////////////////////////////////////////////////////////////////////
|
---|
| 6 | // Matt.Dobbs@Cern.CH, January 2000, 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 in ascii format for machine reading
|
---|
| 11 | //////////////////////////////////////////////////////////////////////////
|
---|
| 12 | //
|
---|
| 13 | // Strategy for reading or writing events/particleData as machine readable
|
---|
| 14 | // ascii to a file. When instantiating, the mode of file to be created
|
---|
| 15 | // must be specified. Options are:
|
---|
| 16 | // std::ios::in open file for input
|
---|
| 17 | // std::ios::out open file for output
|
---|
| 18 | // std::ios::trunc erase old file when opening (i.e. ios::out|ios::trunc
|
---|
| 19 | // removes oldfile, and creates a new one for output )
|
---|
| 20 | // std::ios::app append output to end of file
|
---|
| 21 | // for the purposes of this class, simultaneous input and output mode
|
---|
| 22 | // ( std::ios::in | std::ios::out ) is not allowed.
|
---|
| 23 | //
|
---|
| 24 | // Event listings are preceded by the key:
|
---|
| 25 | // "HepMC::IO_Ascii-START_EVENT_LISTING\n"
|
---|
| 26 | // and terminated by the key:
|
---|
| 27 | // "HepMC::IO_Ascii-END_EVENT_LISTING\n"
|
---|
| 28 | // GenParticle Data tables are preceded by the key:
|
---|
| 29 | // "HepMC::IO_Ascii-START_PARTICLE_DATA\n"
|
---|
| 30 | // and terminated by the key:
|
---|
| 31 | // "HepMC::IO_Ascii-END_PARTICLE_DATA\n"
|
---|
| 32 | // Comments are allowed. They need not be preceded by anything, though if
|
---|
| 33 | // a comment is written using write_comment( const string ) then it will be
|
---|
| 34 | // preceded by "HepMC::IO_Ascii-COMMENT\n"
|
---|
| 35 | // Each event, vertex, particle, particle data is preceded by
|
---|
| 36 | // "E ","V ","P ","D " respectively.
|
---|
| 37 | // Comments may appear anywhere in the file -- so long as they do not contain
|
---|
| 38 | // any of the 4 start/stop keys.
|
---|
| 39 | //
|
---|
| 40 |
|
---|
| 41 | #include <fstream>
|
---|
| 42 | #include <string>
|
---|
| 43 | #include <map>
|
---|
| 44 | #include <vector>
|
---|
| 45 | #include "IO_BaseClass.h"
|
---|
| 46 | #include "TempParticleMap.h"
|
---|
| 47 | #include "CommonIO.h"
|
---|
| 48 |
|
---|
| 49 | namespace HepMC {
|
---|
| 50 |
|
---|
| 51 | class GenEvent;
|
---|
| 52 | class GenVertex;
|
---|
| 53 | class GenParticle;
|
---|
| 54 | class ParticleData;
|
---|
| 55 |
|
---|
| 56 | //! IO_Ascii is used to read or write from an ascii file
|
---|
| 57 |
|
---|
| 58 | ///
|
---|
| 59 | /// \class IO_Ascii
|
---|
| 60 | /// Strategy for reading or writing events/particleData as machine readable
|
---|
| 61 | /// ascii to a file. When instantiating, the mode of file to be created
|
---|
| 62 | /// must be specified.
|
---|
| 63 | ///
|
---|
| 64 | class IO_Ascii : public IO_BaseClass {
|
---|
| 65 | public:
|
---|
| 66 | /// constructor requiring a file name and std::ios mode
|
---|
| 67 | IO_Ascii( const char* filename="IO_Ascii.dat",
|
---|
| 68 | std::ios::openmode mode=std::ios::out );
|
---|
| 69 | virtual ~IO_Ascii();
|
---|
| 70 |
|
---|
| 71 | /// write this event
|
---|
| 72 | void write_event( const GenEvent* evt );
|
---|
| 73 | /// get the next event
|
---|
| 74 | bool fill_next_event( GenEvent* evt );
|
---|
| 75 | void write_particle_data_table(const ParticleDataTable*);
|
---|
| 76 | bool fill_particle_data_table( ParticleDataTable* );
|
---|
| 77 | /// insert a comment directly into the output file --- normally you
|
---|
| 78 | /// only want to do this at the beginning or end of the file. All
|
---|
| 79 | /// comments are preceded with "HepMC::IO_Ascii-COMMENT\n"
|
---|
| 80 | void write_comment( const std::string comment );
|
---|
| 81 |
|
---|
| 82 | int rdstate() const; //!< check the state of the IO stream
|
---|
| 83 | void clear(); //!< clear the IO stream
|
---|
| 84 |
|
---|
| 85 | /// write to ostr
|
---|
| 86 | void print( std::ostream& ostr = std::cout ) const;
|
---|
| 87 |
|
---|
| 88 | protected: // for internal use only
|
---|
| 89 | /// write vertex information
|
---|
| 90 | void write_vertex( GenVertex* );
|
---|
| 91 | /// write particle information
|
---|
| 92 | void write_particle( GenParticle* p );
|
---|
| 93 | /// write ParticleDataTable information
|
---|
| 94 | void write_particle_data( const ParticleData* d );
|
---|
| 95 | /// write end tag
|
---|
| 96 | bool write_end_listing();
|
---|
| 97 |
|
---|
| 98 | void output( const double& ); //!< write double
|
---|
| 99 | void output( const int& ); //!< write int
|
---|
| 100 | void output( const long int& ); //!< write long int
|
---|
| 101 | void output( const char& ); //!< write a single character
|
---|
| 102 | private: // use of copy constructor is not allowed
|
---|
| 103 | IO_Ascii( const IO_Ascii& ) : IO_BaseClass() {}
|
---|
| 104 | private: // data members
|
---|
| 105 | std::ios::openmode m_mode;
|
---|
| 106 | std::fstream m_file;
|
---|
| 107 | bool m_finished_first_event_io;
|
---|
| 108 | CommonIO m_common_io;
|
---|
| 109 | };
|
---|
| 110 |
|
---|
| 111 | //////////////
|
---|
| 112 | // Inlines //
|
---|
| 113 | //////////////
|
---|
| 114 |
|
---|
| 115 | inline void IO_Ascii::output( const double& d ) {
|
---|
| 116 | if ( d == 0. ) {
|
---|
| 117 | m_file << ' ' << (int)0;
|
---|
| 118 | } else {
|
---|
| 119 | m_file << ' ' << d;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | inline void IO_Ascii::output( const int& i ) { m_file << ' ' << i; }
|
---|
| 123 | inline void IO_Ascii::output( const long int& i ) { m_file << ' ' << i; }
|
---|
| 124 | inline void IO_Ascii::output( const char& c ) { m_file << c; }
|
---|
| 125 | inline int IO_Ascii::rdstate() const { return (int)m_file.rdstate(); }
|
---|
| 126 | inline void IO_Ascii::clear() { m_file.clear(); }
|
---|
| 127 |
|
---|
| 128 | } // HepMC
|
---|
| 129 |
|
---|
| 130 | #endif // HEPMC_IO_ASCII_H
|
---|
| 131 | //--------------------------------------------------------------------------
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 |
|
---|