Fork me on GitHub

source: svn/trunk/Utilities/HepMC/src/IO_GenEvent.cc@ 680

Last change on this file since 680 was 571, checked in by cp3-support, 13 years ago

upgrade HepMC to version 2.06.05

File size: 5.4 KB
RevLine 
[349]1//--------------------------------------------------------------------------
2
3//////////////////////////////////////////////////////////////////////////
4// garren@fnal.gov, July 2006
5// event input/output in ascii format for machine reading
6// IO_GenEvent format contains HeavyIon and PdfInfo classes
7//////////////////////////////////////////////////////////////////////////
8
9#include "IO_GenEvent.h"
[571]10#include "IO_Exception.h"
[349]11#include "GenEvent.h"
[571]12#include "StreamHelpers.h"
[349]13
14namespace HepMC {
15
16 IO_GenEvent::IO_GenEvent( const std::string& filename, std::ios::openmode mode )
17 : m_mode(mode),
18 m_file(filename.c_str(), mode),
19 m_ostr(0),
20 m_istr(0),
21 m_iostr(0),
22 m_have_file(false),
[571]23 m_error_type(IO_Exception::OK),
24 m_error_message()
[349]25 {
26 if ( (m_mode&std::ios::out && m_mode&std::ios::in) ||
27 (m_mode&std::ios::app && m_mode&std::ios::in) ) {
[571]28 m_error_type = IO_Exception::InputAndOutput;
29 m_error_message ="IO_GenEvent::IO_GenEvent Error, open of file requested of input AND output type. Not allowed. Closing file.";
30 std::cerr << m_error_message << std::endl;
[349]31 m_file.close();
32 return;
33 }
34 // now we set the streams
35 m_iostr = &m_file;
36 if ( m_mode&std::ios::in ) {
37 m_istr = &m_file;
38 m_ostr = NULL;
[571]39 detail::establish_input_stream_info(m_file);
[349]40 }
41 if ( m_mode&std::ios::out ) {
42 m_ostr = &m_file;
43 m_istr = NULL;
[571]44 detail::establish_output_stream_info(m_file);
[349]45 }
46 m_have_file = true;
47 }
48
49
50 IO_GenEvent::IO_GenEvent( std::istream & istr )
51 : m_ostr(0),
52 m_istr(&istr),
53 m_iostr(&istr),
54 m_have_file(false),
[571]55 m_error_type(IO_Exception::OK),
56 m_error_message()
57 {
58 detail::establish_input_stream_info( istr );
59 }
[349]60
61 IO_GenEvent::IO_GenEvent( std::ostream & ostr )
62 : m_ostr(&ostr),
63 m_istr(0),
64 m_iostr(&ostr),
65 m_have_file(false),
[571]66 m_error_type(IO_Exception::OK),
67 m_error_message()
68 {
69 detail::establish_output_stream_info( ostr );
70 }
[349]71
72 IO_GenEvent::~IO_GenEvent() {
[571]73 if ( m_ostr != NULL ) {
74 write_HepMC_IO_block_end(*m_ostr);
75 }
[349]76 if(m_have_file) m_file.close();
77 }
78
[571]79 void IO_GenEvent::use_input_units( Units::MomentumUnit mom,
80 Units::LengthUnit len ) {
81 if( m_istr != NULL ) {
82 set_input_units( *m_istr, mom, len );
83 }
[349]84 }
85
86 void IO_GenEvent::print( std::ostream& ostr ) const {
87 ostr << "IO_GenEvent: unformated ascii file IO for machine reading.\n";
88 if(m_have_file) ostr << "\tFile openmode: " << m_mode ;
89 ostr << " stream state: " << m_ostr->rdstate()
90 << " bad:" << (m_ostr->rdstate()&std::ios::badbit)
91 << " eof:" << (m_ostr->rdstate()&std::ios::eofbit)
92 << " fail:" << (m_ostr->rdstate()&std::ios::failbit)
93 << " good:" << (m_ostr->rdstate()&std::ios::goodbit) << std::endl;
94 }
95
[571]96 void IO_GenEvent::precision( int size ) {
97 if( size > 16 ) {
98 std::cerr << "IO_GenEvent::precision Error, "
99 << "precision is greater than 16. "
100 << "Not allowed. Using default precision of 16."
101 << std::endl;
102 size = 16;
[349]103 }
[571]104 if(m_ostr) {
105 m_ostr->precision(size);
[349]106 }
107 }
[571]108
[349]109 bool IO_GenEvent::fill_next_event( GenEvent* evt ){
110 //
[571]111 // reset error type
112 m_error_type = IO_Exception::OK;
[349]113 //
114 // test that evt pointer is not null
115 if ( !evt ) {
[571]116 m_error_type = IO_Exception::NullEvent;
117 m_error_message = "IO_GenEvent::fill_next_event error - passed null event.";
118 std::cerr << m_error_message << std::endl;
[349]119 return false;
120 }
121 // make sure the stream is good, and that it is in input mode
122 if ( !(*m_istr) ) return false;
123 if ( !m_istr ) {
[571]124 m_error_type = IO_Exception::WrongFileType;
125 m_error_message = "HepMC::IO_GenEvent::fill_next_event attempt to read from output file.";
126 std::cerr << m_error_message << std::endl;
[349]127 return false;
128 }
[571]129 // use streaming input
130 try {
131 *m_istr >> *evt;
132 }
133 catch (IO_Exception& e) {
134 m_error_type = IO_Exception::InvalidData;
135 m_error_message = e.what();
136 evt->clear();
137 return false;
138 }
139 if( evt->is_valid() ) return true;
140 return false;
141 }
142
143 void IO_GenEvent::write_event( const GenEvent* evt ) {
144 /// Writes evt to output stream. It does NOT delete the event after writing.
[349]145 //
[571]146 // make sure the state is good, and that it is in output mode
147 if ( !evt ) return;
148 if ( m_ostr == NULL ) {
149 m_error_type = IO_Exception::WrongFileType;
150 m_error_message = "HepMC::IO_GenEvent::write_event attempt to write to input file.";
151 std::cerr << m_error_message << std::endl;
152 return;
[349]153 }
154 //
[571]155 // write event listing key before first event only.
156 write_HepMC_IO_block_begin(*m_ostr);
157 // explicit cast is necessary
158 GenEvent e = *evt;
159 *m_ostr << e ;
[349]160 }
161
162 void IO_GenEvent::write_comment( const std::string comment ) {
163 // make sure the stream is good, and that it is in output mode
164 if ( !(*m_ostr) ) return;
165 if ( m_ostr == NULL ) {
[571]166 m_error_type = IO_Exception::WrongFileType;
167 m_error_message = "HepMC::IO_GenEvent::write_event attempt to write to input file.";
168 std::cerr << m_error_message << std::endl;
[349]169 return;
170 }
171 // write end of event listing key if events have already been written
[571]172 write_HepMC_IO_block_end(*m_ostr);
[349]173 // insert the comment key before the comment
174 *m_ostr << "\n" << "HepMC::IO_GenEvent-COMMENT\n";
175 *m_ostr << comment << std::endl;
176 }
177
178} // HepMC
Note: See TracBrowser for help on using the repository browser.