HepMC Reference Documentation

HepMC

IO_GenEvent.cc

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 
00004 // garren@fnal.gov, July 2006
00005 // event input/output in ascii format for machine reading
00006 // IO_GenEvent format contains HeavyIon and PdfInfo classes
00008 
00009 #include "HepMC/IO_GenEvent.h"
00010 #include "HepMC/IO_Exception.h"
00011 #include "HepMC/GenEvent.h"
00012 #include "HepMC/ParticleDataTable.h"
00013 #include "HepMC/StreamHelpers.h"
00014 
00015 namespace HepMC {
00016 
00017     IO_GenEvent::IO_GenEvent( const std::string& filename, std::ios::openmode mode ) 
00018     : m_mode(mode), 
00019       m_file(filename.c_str(), mode), 
00020       m_ostr(0),
00021       m_istr(0),
00022       m_iostr(0),
00023       m_have_file(false),
00024       m_error_type(IO_Exception::OK),
00025       m_error_message()
00026     {
00027         if ( (m_mode&std::ios::out && m_mode&std::ios::in) ||
00028              (m_mode&std::ios::app && m_mode&std::ios::in) ) {
00029             m_error_type = IO_Exception::InputAndOutput;
00030             m_error_message ="IO_GenEvent::IO_GenEvent Error, open of file requested of input AND output type. Not allowed. Closing file.";
00031             std::cerr << m_error_message << std::endl;
00032             m_file.close();
00033             return;
00034         }
00035         // now we set the streams
00036         m_iostr = &m_file;
00037         if ( m_mode&std::ios::in ) {
00038             m_istr = &m_file;
00039             m_ostr = NULL;
00040             detail::establish_input_stream_info(m_file);
00041         }
00042         if ( m_mode&std::ios::out ) {
00043             m_ostr = &m_file;
00044             m_istr = NULL;
00045             detail::establish_output_stream_info(m_file);
00046         }
00047         m_have_file = true;
00048     }
00049 
00050 
00051     IO_GenEvent::IO_GenEvent( std::istream & istr ) 
00052     : m_ostr(0),
00053       m_istr(&istr),
00054       m_iostr(&istr),
00055       m_have_file(false),
00056       m_error_type(IO_Exception::OK),
00057       m_error_message()
00058     { 
00059         detail::establish_input_stream_info( istr );
00060     }
00061 
00062     IO_GenEvent::IO_GenEvent( std::ostream & ostr )
00063     : m_ostr(&ostr),
00064       m_istr(0),
00065       m_iostr(&ostr),
00066       m_have_file(false),
00067       m_error_type(IO_Exception::OK),
00068       m_error_message()
00069    {
00070         detail::establish_output_stream_info( ostr );
00071    }
00072 
00073     IO_GenEvent::~IO_GenEvent() {
00074         if ( m_ostr != NULL ) {
00075             write_HepMC_IO_block_end(*m_ostr);
00076         }
00077         if(m_have_file) m_file.close();
00078     }
00079 
00080     void IO_GenEvent::use_input_units( Units::MomentumUnit mom, 
00081                                        Units::LengthUnit len ) {
00082         if( m_istr != NULL ) {
00083             set_input_units( *m_istr, mom, len );
00084         }
00085     }
00086 
00087     void IO_GenEvent::print( std::ostream& ostr ) const { 
00088         ostr << "IO_GenEvent: unformated ascii file IO for machine reading.\n"; 
00089         if(m_have_file)    ostr  << "\tFile openmode: " << m_mode ;
00090         ostr << " stream state: " << m_ostr->rdstate()
00091              << " bad:" << (m_ostr->rdstate()&std::ios::badbit)
00092              << " eof:" << (m_ostr->rdstate()&std::ios::eofbit)
00093              << " fail:" << (m_ostr->rdstate()&std::ios::failbit)
00094              << " good:" << (m_ostr->rdstate()&std::ios::goodbit) << std::endl;
00095     }
00096 
00097     void IO_GenEvent::precision( int size )  { 
00098         if( size > 16 ) { 
00099             std::cerr << "IO_GenEvent::precision Error, "
00100                       << "precision is greater than 16. "
00101                       << "Not allowed. Using default precision of 16."
00102                       << std::endl;
00103             size = 16;
00104         }
00105         if(m_ostr) {
00106             m_ostr->precision(size);
00107         }
00108     }
00109         
00110     bool IO_GenEvent::fill_next_event( GenEvent* evt ){
00111         //
00112         // reset error type
00113         m_error_type = IO_Exception::OK;
00114         //
00115         // test that evt pointer is not null
00116         if ( !evt ) {
00117             m_error_type = IO_Exception::NullEvent;
00118             m_error_message = "IO_GenEvent::fill_next_event error - passed null event.";
00119             std::cerr << m_error_message << std::endl;
00120             return false;
00121         }
00122         // make sure the stream is good, and that it is in input mode
00123         if ( !(*m_istr) ) return false;
00124         if ( !m_istr ) {
00125             m_error_type = IO_Exception::WrongFileType;
00126             m_error_message = "HepMC::IO_GenEvent::fill_next_event attempt to read from output file.";
00127             std::cerr << m_error_message << std::endl;
00128             return false;
00129         }
00130         // use streaming input
00131         try {
00132             *m_istr >> *evt;
00133         }
00134         catch (IO_Exception& e) {
00135             m_error_type = IO_Exception::InvalidData;
00136             m_error_message = e.what();
00137             evt->clear();
00138             return false;
00139         }
00140         if( evt->is_valid() ) return true;
00141         return false;
00142     }
00143 
00144     void IO_GenEvent::write_event( const GenEvent* evt ) {
00146         //
00147         // make sure the state is good, and that it is in output mode
00148         if ( !evt  ) return;
00149         if ( m_ostr == NULL ) {
00150             m_error_type = IO_Exception::WrongFileType;
00151             m_error_message = "HepMC::IO_GenEvent::write_event attempt to write to input file.";
00152             std::cerr << m_error_message << std::endl;
00153             return;
00154         }
00155         //
00156         // write event listing key before first event only.
00157         write_HepMC_IO_block_begin(*m_ostr);
00158         // explicit cast is necessary
00159         GenEvent e = *evt;
00160         *m_ostr << e ;
00161     }
00162 
00163     void IO_GenEvent::write_comment( const std::string comment ) {
00164         // make sure the stream is good, and that it is in output mode
00165         if ( !(*m_ostr) ) return;
00166         if ( m_ostr == NULL ) {
00167             m_error_type = IO_Exception::WrongFileType;
00168             m_error_message = "HepMC::IO_GenEvent::write_event attempt to write to input file.";
00169             std::cerr << m_error_message << std::endl;
00170             return;
00171         }
00172         // write end of event listing key if events have already been written
00173         write_HepMC_IO_block_end(*m_ostr);
00174         // insert the comment key before the comment
00175         *m_ostr << "\n" << "HepMC::IO_GenEvent-COMMENT\n";
00176         *m_ostr << comment << std::endl;
00177     }
00178 
00179 bool IO_GenEvent::read_io_particle_data_table( std::istream* is, ParticleDataTable* pdt )
00180 {
00181     // 
00182     // read Individual GenParticle data entries
00183     while ( read_particle_data( is, pdt ) ) ;
00184     return true;
00185 }
00186 
00187 ParticleData* IO_GenEvent::read_particle_data( std::istream* is, ParticleDataTable* pdt ) {
00188     // assumes mode has already been checked
00189     //
00190     // test to be sure the next entry is of type "D" then ignore it
00191     if ( !(*is) || is->peek()!='D' ) return 0;
00192     is->ignore();
00193     //
00194     // read values into temp variables then create new ParticleData object
00195     char its_name[22];
00196     int its_id = 0, its_spin = 0;  
00197     double its_charge = 0, its_mass = 0, its_clifetime = 0;
00198     *is >> its_id >> its_charge >> its_mass 
00199            >> its_clifetime >> its_spin;
00200     is->ignore(1); // eat the " "
00201     is->getline( its_name, 22, '\n' );
00202     ParticleData* pdata = new ParticleData( its_name, its_id, its_charge, 
00203                                             its_mass, its_clifetime, 
00204                                             double(its_spin)/2.);
00205     pdt->insert(pdata);
00206     return pdata;
00207 }
00208         
00209 } // HepMC

Generated on Thu Jan 7 13:10:16 2010 for HepMC by  doxygen 1.4.7