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