Fork me on GitHub

source: svn/trunk/Utilities/HepMC/src/IO_Ascii.cc@ 444

Last change on this file since 444 was 349, checked in by severine ovyn, 16 years ago

first test

File size: 11.4 KB
RevLine 
[349]1//--------------------------------------------------------------------------
2
3//////////////////////////////////////////////////////////////////////////
4// Matt.Dobbs@Cern.CH, January 2000
5// event input/output in ascii format for machine reading
6//////////////////////////////////////////////////////////////////////////
7
8#include "IO_Ascii.h"
9#include "GenEvent.h"
10#include "ParticleDataTable.h"
11#include "CommonIO.h"
12#include "Version.h"
13
14namespace HepMC {
15
16 IO_Ascii::IO_Ascii( const char* filename, std::ios::openmode mode )
17 : m_mode(mode), m_file(filename, mode), m_finished_first_event_io(0),
18 m_common_io()
19 {
20 std::cout << "-------------------------------------------------------" << std::endl;
21 std::cout << "Use of HepMC/IO_Ascii is deprecated" << std::endl;
22 std::cout << "-------------------------------------------------------" << std::endl;
23 if ( (m_mode&std::ios::out && m_mode&std::ios::in) ||
24 (m_mode&std::ios::app && m_mode&std::ios::in) ) {
25 std::cerr << "IO_Ascii::IO_Ascii Error, open of file requested "
26 << "of input AND output type. Not allowed. Closing file."
27 << std::endl;
28 m_file.close();
29 return;
30 }
31 // precision 16 (# digits following decimal point) is the minimum that
32 // will capture the full information stored in a double
33 m_file.precision(16);
34 // we use decimal to store integers, because it is smaller than hex!
35 m_file.setf(std::ios::dec,std::ios::basefield);
36 m_file.setf(std::ios::scientific,std::ios::floatfield);
37 }
38
39 IO_Ascii::~IO_Ascii() {
40 write_end_listing();
41 m_file.close();
42 }
43
44 void IO_Ascii::print( std::ostream& ostr ) const {
45 ostr << "IO_Ascii: unformated ascii file IO for machine reading.\n"
46 << "\tFile openmode: " << m_mode
47 << " file state: " << m_file.rdstate()
48 << " bad:" << (m_file.rdstate()&std::ios::badbit)
49 << " eof:" << (m_file.rdstate()&std::ios::eofbit)
50 << " fail:" << (m_file.rdstate()&std::ios::failbit)
51 << " good:" << (m_file.rdstate()&std::ios::goodbit) << std::endl;
52 }
53
54 void IO_Ascii::write_event( const GenEvent* evt ) {
55 /// Writes evt to m_file. It does NOT delete the event after writing.
56 //
57 // check the state of m_file is good, and that it is in output mode
58 if ( !evt || !m_file ) return;
59 if ( !(m_mode&std::ios::out) ) {
60 std::cerr << "HepMC::IO_Ascii::write_event "
61 << " attempt to write to input file." << std::endl;
62 return;
63 }
64 //
65 // write event listing key before first event only.
66 if ( !m_finished_first_event_io ) {
67 m_finished_first_event_io = 1;
68 m_file << "\n" << "HepMC::Version " << versionName();
69 m_file << "\n" << "HepMC::IO_Ascii-START_EVENT_LISTING\n";
70 }
71 //
72 // output the event data including the number of primary vertices
73 // and the total number of vertices
74 std::vector<long int> random_states = evt->random_states();
75 m_file << 'E';
76 output( evt->event_number() );
77 output( evt->event_scale() );
78 output( evt->alphaQCD() );
79 output( evt->alphaQED() );
80 output( evt->signal_process_id() );
81 output( ( evt->signal_process_vertex() ?
82 evt->signal_process_vertex()->barcode() : 0 ) );
83 output( evt->vertices_size() ); // total number of vertices.
84 output( (int)random_states.size() );
85 for ( std::vector<long int>::iterator rs = random_states.begin();
86 rs != random_states.end(); ++rs ) {
87 output( *rs );
88 }
89 output( (int)evt->weights().size() );
90 for ( WeightContainer::const_iterator w = evt->weights().begin();
91 w != evt->weights().end(); ++w ) {
92 output( *w );
93 }
94 output('\n');
95 //
96 // Output all of the vertices - note there is no real order.
97 for ( GenEvent::vertex_const_iterator v = evt->vertices_begin();
98 v != evt->vertices_end(); ++v ) {
99 write_vertex( *v );
100 }
101 }
102
103 bool IO_Ascii::fill_next_event( GenEvent* evt ){
104 //
105 //
106 // test that evt pointer is not null
107 if ( !evt ) {
108 std::cerr
109 << "IO_Ascii::fill_next_event error - passed null event."
110 << std::endl;
111 return false;
112 }
113 // check the state of m_file is good, and that it is in input mode
114 if ( !m_file ) return false;
115 if ( !(m_mode&std::ios::in) ) {
116 std::cerr << "HepMC::IO_Ascii::fill_next_event "
117 << " attempt to read from output file." << std::endl;
118 return false;
119 }
120 //
121 // search for event listing key before first event only.
122 //
123 // skip through the file just after first occurence of the start_key
124 int iotype;
125 if ( !m_finished_first_event_io ) {
126 m_file.seekg( 0 ); // go to position zero in the file.
127 iotype = m_common_io.find_file_type(m_file);
128 if( iotype != ascii ) {
129 std::cerr << "IO_Ascii::fill_next_event start key not found "
130 << "setting badbit." << std::endl;
131 m_file.clear(std::ios::badbit);
132 return false;
133 }
134 m_finished_first_event_io = 1;
135 }
136 //
137 // test to be sure the next entry is of type "E" then ignore it
138 if ( !m_file ) {
139 std::cerr << "IO_Ascii::fill_next_event end of stream found "
140 << "setting badbit." << std::endl;
141 m_file.clear(std::ios::badbit);
142 return false;
143 }
144 if ( !m_file || m_file.peek()!='E' ) {
145 // if the E is not the next entry, then check to see if it is
146 // the end event listing key - if yes, search for another start key
147 if ( m_common_io.find_end_key(m_file) ) {
148 iotype = m_common_io.find_file_type(m_file);
149 if( iotype != ascii ) {
150 // this is the only case where we set an EOF state
151 m_file.clear(std::ios::eofbit);
152 return false;
153 }
154 } else {
155 std::cerr << "IO_Ascii::fill_next_event end key not found "
156 << "setting badbit." << std::endl;
157 m_file.clear(std::ios::badbit);
158 return false;
159 }
160 }
161 m_file.ignore();
162 // call the read method
163 return m_common_io.read_io_ascii(&m_file, evt);
164 }
165
166 void IO_Ascii::write_comment( const std::string comment ) {
167 // check the state of m_file is good, and that it is in output mode
168 if ( !m_file ) return;
169 if ( !(m_mode&std::ios::out) ) {
170 std::cerr << "HepMC::IO_Ascii::write_particle_data_table "
171 << " attempt to write to input file." << std::endl;
172 return;
173 }
174 // write end of event listing key if events have already been written
175 write_end_listing();
176 // insert the comment key before the comment
177 m_file << "\n" << "HepMC::IO_Ascii-COMMENT\n";
178 m_file << comment << std::endl;
179 }
180
181 void IO_Ascii::write_particle_data_table( const ParticleDataTable* pdt) {
182 //
183 // check the state of m_file is good, and that it is in output mode
184 if ( !m_file ) return;
185 if ( !(m_mode&std::ios::out) ) {
186 std::cerr << "HepMC::IO_Ascii::write_particle_data_table "
187 << " attempt to write to input file." << std::endl;
188 return;
189 }
190 // write end of event listing key if events have already been written
191 write_end_listing();
192 //
193 m_file << "\n" << "HepMC::IO_Ascii-START_PARTICLE_DATA\n";
194 for ( ParticleDataTable::const_iterator pd = pdt->begin();
195 pd != pdt->end(); pd++ ) {
196 write_particle_data( pd->second );
197 }
198 m_file << "HepMC::IO_Ascii-END_PARTICLE_DATA\n" << std::flush;
199 }
200
201 bool IO_Ascii::fill_particle_data_table( ParticleDataTable* pdt ) {
202 //
203 // test that pdt pointer is not null
204 if ( !pdt ) {
205 std::cerr
206 << "IO_Ascii::fill_particle_data_table - passed null table."
207 << std::endl;
208 return false;
209 }
210 //
211 // check the state of m_file is good, and that it is in input mode
212 if ( !m_file ) return false;
213 if ( !m_mode&std::ios::in ) {
214 std::cerr << "HepMC::IO_Ascii::fill_particle_data_table "
215 << " attempt to read from output file." << std::endl;
216 return false;
217 }
218 // position to beginning of file
219 int initial_file_position = m_file.tellg();
220 std::ios::iostate initial_state = m_file.rdstate();
221 m_file.seekg( 0 );
222 // skip through the file just after first occurence of the start_key
223 int iotype;
224 iotype = m_common_io.find_file_type(m_file);
225 if( iotype != ascii_pdt ) {
226 m_file.seekg( initial_file_position );
227 std::cerr << "IO_Ascii::fill_particle_data_table start key not "
228 << "found setting badbit." << std::endl;
229 m_file.clear(std::ios::badbit);
230 return false;
231 }
232 //
233 pdt->set_description("Read with IO_Ascii");
234 m_common_io.read_io_particle_data_table( &m_file, pdt );
235 //
236 // check for the end event listing key
237 iotype = m_common_io.find_end_key(m_file);
238 if( iotype != ascii_pdt ) {
239 std::cerr << "IO_Ascii::fill_particle_data_table end key not "
240 << "found setting badbit." << std::endl;
241 m_file.clear(std::ios::badbit);
242 }
243 // put the file back into its original state and position
244 m_file.clear( initial_state );
245 m_file.seekg( initial_file_position );
246 return true;
247 }
248
249 void IO_Ascii::write_vertex( GenVertex* v ) {
250 // assumes mode has already been checked
251 if ( !v || !m_file ) {
252 std::cerr << "IO_Ascii::write_vertex !v||!m_file, "
253 << "v="<< v << " setting badbit" << std::endl;
254 m_file.clear(std::ios::badbit);
255 return;
256 }
257 // First collect info we need
258 // count the number of orphan particles going into v
259 int num_orphans_in = 0;
260 for ( GenVertex::particles_in_const_iterator p1
261 = v->particles_in_const_begin();
262 p1 != v->particles_in_const_end(); ++p1 ) {
263 if ( !(*p1)->production_vertex() ) ++num_orphans_in;
264 }
265 //
266 m_file << 'V';
267 output( v->barcode() ); // v's unique identifier
268 output( v->id() );
269 output( v->position().x() );
270 output( v->position().y() );
271 output( v->position().z() );
272 output( v->position().t() );
273 output( num_orphans_in );
274 output( (int)v->particles_out_size() );
275 output( (int)v->weights().size() );
276 for ( WeightContainer::iterator w = v->weights().begin();
277 w != v->weights().end(); ++w ) {
278 output( *w );
279 }
280 output('\n');
281 for ( GenVertex::particles_in_const_iterator p2
282 = v->particles_in_const_begin();
283 p2 != v->particles_in_const_end(); ++p2 ) {
284 if ( !(*p2)->production_vertex() ) {
285 write_particle( *p2 );
286 }
287 }
288 for ( GenVertex::particles_out_const_iterator p3
289 = v->particles_out_const_begin();
290 p3 != v->particles_out_const_end(); ++p3 ) {
291 write_particle( *p3 );
292 }
293 }
294
295 void IO_Ascii::write_particle( GenParticle* p ) {
296 // assumes mode has already been checked
297 if ( !p || !m_file ) {
298 std::cerr << "IO_Ascii::write_particle !p||!m_file, "
299 << "p="<< p << " setting badbit" << std::endl;
300 m_file.clear(std::ios::badbit);
301 return;
302 }
303 m_file << 'P';
304 output( p->barcode() );
305 output( p->pdg_id() );
306 output( p->momentum().px() );
307 output( p->momentum().py() );
308 output( p->momentum().pz() );
309 output( p->momentum().e() );
310 output( p->status() );
311 output( p->polarization().theta() );
312 output( p->polarization().phi() );
313 // since end_vertex is oftentimes null, this CREATES a null vertex
314 // in the map
315 output( ( p->end_vertex() ? p->end_vertex()->barcode() : 0 ) );
316 m_file << ' ' << p->flow() << "\n";
317 }
318
319 void IO_Ascii::write_particle_data( const ParticleData* pdata ) {
320 // assumes mode has already been checked
321 if ( !pdata || !m_file ) {
322 std::cerr << "IO_Ascii::write_vertex !pdata||!m_file, "
323 << "pdata="<< pdata << " setting badbit" << std::endl;
324 m_file.clear(std::ios::badbit);
325 return;
326 }
327 m_file << 'D';
328 output( pdata->pdg_id() );
329 output( pdata->charge() );
330 output( pdata->mass() );
331 output( pdata->clifetime() );
332 output( (int)(pdata->spin()*2.+.1) );
333 // writes the first 21 characters starting with 0
334 m_file << " " << pdata->name().substr(0,21) << "\n";
335 }
336
337 bool IO_Ascii::write_end_listing() {
338 if ( m_finished_first_event_io && m_mode&std::ios::out ) {
339 m_common_io.write_IO_Ascii_End(m_file);
340 m_file << std::flush;
341 m_finished_first_event_io = 0;
342 return true;
343 }
344 return false;
345 }
346
347} // HepMC
348
Note: See TracBrowser for help on using the repository browser.