Fork me on GitHub

source: svn/trunk/Utilities/HepMC/src/CommonIO.cc@ 555

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

first test

File size: 16.7 KB
Line 
1//--------------------------------------------------------------------------
2//
3// CommonIO.cc
4// Author: Lynn Garren
5//
6// ----------------------------------------------------------------------
7
8#include <string>
9#include <sstream>
10
11#include "CommonIO.h"
12#include "GenEvent.h"
13#include "HeavyIon.h"
14#include "PdfInfo.h"
15#include "TempParticleMap.h"
16#include "ParticleDataTable.h"
17
18namespace HepMC {
19
20int CommonIO::find_file_type( std::istream& istr )
21{
22 std::string line;
23 while ( std::getline(istr,line) ) {
24 //
25 // search for event listing key before first event only.
26 //
27 if( line == m_io_genevent_start ) {
28 m_io_type = gen;
29 return gen;
30 } else if( line == m_io_ascii_start ) {
31 m_io_type = ascii;
32 return ascii;
33 } else if( line == m_io_extendedascii_start ) {
34 m_io_type = extascii;
35 return extascii;
36 } else if( line == m_io_ascii_pdt_start ) {
37 m_io_type = ascii_pdt;
38 return ascii_pdt;
39 } else if( line == m_io_extendedascii_pdt_start ) {
40 m_io_type = extascii_pdt;
41 return extascii_pdt;
42 }
43 }
44 return -1;
45}
46
47int CommonIO::find_end_key( std::istream& istr )
48{
49 // peek at the first character before proceeding
50 if( istr.peek()!='H' ) return false;
51 //
52 // we only check the next line
53 std::string line;
54 std::getline(istr,line);
55 //
56 // check to see if this is an end key
57 int iotype = 0;
58 if( line == m_io_genevent_end ) {
59 m_io_type = gen;
60 } else if( line == m_io_ascii_end ) {
61 m_io_type = ascii;
62 } else if( line == m_io_extendedascii_end ) {
63 m_io_type = extascii;
64 } else if( line == m_io_ascii_pdt_end ) {
65 m_io_type = ascii_pdt;
66 } else if( line == m_io_extendedascii_pdt_end ) {
67 m_io_type = extascii_pdt;
68 }
69 if( iotype != 0 && m_io_type != iotype ) {
70 std::cerr << "CommonIO::find_end_key: iotype keys have changed" << std::endl;
71 } else {
72 return iotype;
73 }
74 //
75 // if we get here, then something has gotten badly confused
76 std::cerr << "CommonIO::find_end_key: MALFORMED INPUT" << std::endl;
77 return -1;
78}
79
80void CommonIO::use_input_units( Units::MomentumUnit mom, Units::LengthUnit len )
81{
82 m_io_momentum_unit = mom;
83 m_io_position_unit = len;
84}
85
86bool CommonIO::read_io_ascii( std::istream* istr, GenEvent* evt )
87{
88 // read values into temp variables, then create a new GenEvent
89 int event_number = 0, signal_process_id = 0, signal_process_vertex = 0,
90 num_vertices = 0, random_states_size = 0, weights_size = 0;
91 double eventScale = 0, alpha_qcd = 0, alpha_qed = 0;
92 *istr >> event_number >> eventScale >> alpha_qcd >> alpha_qed
93 >> signal_process_id >> signal_process_vertex
94 >> num_vertices >> random_states_size;
95 std::vector<long> random_states(random_states_size);
96 for ( int i = 0; i < random_states_size; ++i ) {
97 *istr >> random_states[i];
98 }
99 *istr >> weights_size;
100 WeightContainer weights(weights_size);
101 for ( int ii = 0; ii < weights_size; ++ii ) *istr >> weights[ii];
102 istr->ignore(2,'\n');
103 //
104 // fill signal_process_id, event_number, weights, random_states
105 evt->set_signal_process_id( signal_process_id );
106 evt->set_event_number( event_number );
107 evt->weights() = weights;
108 evt->set_random_states( random_states );
109 // no unit information, so just set it
110 // this is done before the event has any vertices, so it is safe
111 evt->use_units( m_io_momentum_unit, m_io_position_unit );
112 //
113 // the end vertices of the particles are not connected until
114 // after the event is read --- we store the values in a map until then
115 TempParticleMap particle_to_end_vertex;
116 //
117 // read in the vertices
118 for ( int iii = 1; iii <= num_vertices; ++iii ) {
119 GenVertex* v = read_vertex(istr,particle_to_end_vertex);
120 evt->add_vertex( v );
121 }
122 // set the signal process vertex
123 if ( signal_process_vertex ) {
124 evt->set_signal_process_vertex(
125 evt->barcode_to_vertex(signal_process_vertex) );
126 }
127 //
128 // last connect particles to their end vertices
129 for ( std::map<int,GenParticle*>::iterator pmap
130 = particle_to_end_vertex.order_begin();
131 pmap != particle_to_end_vertex.order_end(); ++pmap ) {
132 GenParticle* p = pmap->second;
133 int vtx = particle_to_end_vertex.end_vertex( p );
134 GenVertex* itsDecayVtx = evt->barcode_to_vertex(vtx);
135 if ( itsDecayVtx ) itsDecayVtx->add_particle_in( p );
136 else {
137 std::cerr << "IO_Ascii::fill_next_event ERROR particle points"
138 << "\n to null end vertex. " <<std::endl;
139 }
140 }
141 return true;
142}
143
144bool CommonIO::read_io_extendedascii( std::istream* istr, GenEvent* evt )
145{
146 // read values into temp variables, then create a new GenEvent
147 int event_number = 0, signal_process_id = 0, signal_process_vertex = 0,
148 num_vertices = 0, random_states_size = 0, weights_size = 0,
149 nmpi = 0, bp1 = 0, bp2 = 0;
150 double eventScale = 0, alpha_qcd = 0, alpha_qed = 0;
151 *istr >> event_number >> nmpi >> eventScale >> alpha_qcd >> alpha_qed
152 >> signal_process_id >> signal_process_vertex
153 >> num_vertices >> bp1 >> bp2 >> random_states_size;
154 std::vector<long> random_states(random_states_size);
155 for ( int i = 0; i < random_states_size; ++i ) {
156 *istr >> random_states[i];
157 }
158 *istr >> weights_size;
159 WeightContainer weights(weights_size);
160 for ( int ii = 0; ii < weights_size; ++ii ) *istr >> weights[ii];
161 istr->ignore(2,'\n');
162 //
163 // fill signal_process_id, event_number, weights, random_states
164 evt->set_signal_process_id( signal_process_id );
165 evt->set_event_number( event_number );
166 evt->set_mpi( nmpi );
167 evt->weights() = weights;
168 evt->set_random_states( random_states );
169 evt->set_event_scale( eventScale );
170 evt->set_alphaQCD( alpha_qcd );
171 evt->set_alphaQED( alpha_qed );
172 // get HeavyIon and PdfInfo
173 HeavyIon* ion = read_heavy_ion(istr);
174 if(ion) evt->set_heavy_ion( *ion );
175 PdfInfo* pdf = read_pdf_info(istr);
176 if(pdf) evt->set_pdf_info( *pdf );
177 // no unit information, so just set it
178 // this is done before the event has any vertices, so it is safe
179 evt->use_units( m_io_momentum_unit, m_io_position_unit );
180 //
181 // the end vertices of the particles are not connected until
182 // after the event is read --- we store the values in a map until then
183 TempParticleMap particle_to_end_vertex;
184 //
185 // read in the vertices
186 for ( int iii = 1; iii <= num_vertices; ++iii ) {
187 GenVertex* v = read_vertex(istr,particle_to_end_vertex);
188 evt->add_vertex( v );
189 }
190 // set the signal process vertex
191 if ( signal_process_vertex ) {
192 evt->set_signal_process_vertex(
193 evt->barcode_to_vertex(signal_process_vertex) );
194 }
195 //
196 // last connect particles to their end vertices
197 GenParticle* beam1(0);
198 GenParticle* beam2(0);
199 for ( std::map<int,GenParticle*>::iterator pmap
200 = particle_to_end_vertex.order_begin();
201 pmap != particle_to_end_vertex.order_end(); ++pmap ) {
202 GenParticle* p = pmap->second;
203 int vtx = particle_to_end_vertex.end_vertex( p );
204 GenVertex* itsDecayVtx = evt->barcode_to_vertex(vtx);
205 if ( itsDecayVtx ) itsDecayVtx->add_particle_in( p );
206 else {
207 std::cerr << "read_io_extendedascii: ERROR particle points"
208 << "\n to null end vertex. " <<std::endl;
209 }
210 // also look for the beam particles
211 if( p->barcode() == bp1 ) beam1 = p;
212 if( p->barcode() == bp2 ) beam2 = p;
213 }
214 evt->set_beam_particles(beam1,beam2);
215 return true;
216}
217
218bool CommonIO::read_io_genevent( std::istream* is, GenEvent* evt )
219{
220 /// this method ONLY works if called from fill_next_event
221 //
222 // assume that we've been handed a valid stream and event pointer
223 //
224 // read values into temp variables, then fill GenEvent
225 int event_number = 0, signal_process_id = 0, signal_process_vertex = 0,
226 num_vertices = 0, random_states_size = 0, weights_size = 0,
227 nmpi = 0, bp1 = 0, bp2 = 0;
228 double eventScale = 0, alpha_qcd = 0, alpha_qed = 0;
229 *is >> event_number >> nmpi >> eventScale >> alpha_qcd >> alpha_qed
230 >> signal_process_id >> signal_process_vertex
231 >> num_vertices >> bp1 >> bp2 >> random_states_size;
232 std::vector<long> random_states(random_states_size);
233 for ( int i = 0; i < random_states_size; ++i ) {
234 *is >> random_states[i];
235 }
236 *is >> weights_size;
237 WeightContainer weights(weights_size);
238 for ( int ii = 0; ii < weights_size; ++ii ) *is >> weights[ii];
239 is->ignore(2,'\n');
240 //
241 // fill signal_process_id, event_number, weights, random_states, etc.
242 evt->set_signal_process_id( signal_process_id );
243 evt->set_event_number( event_number );
244 evt->set_mpi( nmpi );
245 evt->weights() = weights;
246 evt->set_random_states( random_states );
247 evt->set_event_scale( eventScale );
248 evt->set_alphaQCD( alpha_qcd );
249 evt->set_alphaQED( alpha_qed );
250 // get unit information if it exists
251 read_units( is, evt );
252 // get HeavyIon and PdfInfo
253 HeavyIon* ion = read_heavy_ion(is);
254 if(ion) evt->set_heavy_ion( *ion );
255 PdfInfo* pdf = read_pdf_info(is);
256 if(pdf) evt->set_pdf_info( *pdf );
257 //
258 // the end vertices of the particles are not connected until
259 // after the event is read --- we store the values in a map until then
260 TempParticleMap particle_to_end_vertex;
261 //
262 // read in the vertices
263 for ( int iii = 1; iii <= num_vertices; ++iii ) {
264 GenVertex* v = read_vertex(is,particle_to_end_vertex);
265 evt->add_vertex( v );
266 }
267 // set the signal process vertex
268 if ( signal_process_vertex ) {
269 evt->set_signal_process_vertex(
270 evt->barcode_to_vertex(signal_process_vertex) );
271 }
272 //
273 // last connect particles to their end vertices
274 GenParticle* beam1(0);
275 GenParticle* beam2(0);
276 for ( std::map<int,GenParticle*>::iterator pmap
277 = particle_to_end_vertex.order_begin();
278 pmap != particle_to_end_vertex.order_end(); ++pmap ) {
279 GenParticle* p = pmap->second;
280 int vtx = particle_to_end_vertex.end_vertex( p );
281 GenVertex* itsDecayVtx = evt->barcode_to_vertex(vtx);
282 if ( itsDecayVtx ) itsDecayVtx->add_particle_in( p );
283 else {
284 std::cerr << "read_io_genevent: ERROR particle points"
285 << " to null end vertex. " <<std::endl;
286 }
287 // also look for the beam particles
288 if( p->barcode() == bp1 ) beam1 = p;
289 if( p->barcode() == bp2 ) beam2 = p;
290 }
291 evt->set_beam_particles(beam1,beam2);
292 return true;
293}
294
295bool CommonIO::read_io_particle_data_table( std::istream* is, ParticleDataTable* pdt )
296{
297 //
298 // read Individual GenParticle data entries
299 while ( read_particle_data( is, pdt ) ) ;
300 return true;
301}
302
303HeavyIon* CommonIO::read_heavy_ion(std::istream* is)
304{
305 // assumes mode has already been checked
306 //
307 // test to be sure the next entry is of type "H" then ignore it
308 if ( !(*is) || is->peek()!='H' ) {
309 std::cerr << "CommonIO::read_heavy_ion setting badbit." << std::endl;
310 is->clear(std::ios::badbit);
311 return false;
312 }
313 is->ignore();
314 // read values into temp variables, then create a new HeavyIon object
315 int nh =0, np =0, nt =0, nc =0,
316 neut = 0, prot = 0, nw =0, nwn =0, nwnw =0;
317 float impact = 0., plane = 0., xcen = 0., inel = 0.;
318 *is >> nh >> np >> nt >> nc >> neut >> prot
319 >> nw >> nwn >> nwnw >> impact >> plane >> xcen >> inel;
320 is->ignore(2,'\n');
321 if( nh == 0 ) return false;
322 HeavyIon* ion = new HeavyIon(nh, np, nt, nc, neut, prot,
323 nw, nwn, nwnw,
324 impact, plane, xcen, inel );
325 //
326 return ion;
327}
328
329PdfInfo* CommonIO::read_pdf_info(std::istream* is)
330{
331 // assumes mode has already been checked
332 //
333 // test to be sure the next entry is of type "F" then ignore it
334 if ( !(*is) || is->peek() !='F') {
335 std::cerr << "CommonIO::read_pdf_info setting badbit." << std::endl;
336 is->clear(std::ios::badbit);
337 return false;
338 }
339 is->ignore();
340 // read values into temp variables, then create a new PdfInfo object
341 int id1 =0, id2 =0, pdf_id1=0, pdf_id2=0;
342 double x1 = 0., x2 = 0., scale = 0., pdf1 = 0., pdf2 = 0.;
343 *is >> id1 >> id2 >> x1 >> x2 >> scale >> pdf1 >> pdf2;
344 // check to see if we are at the end of the line
345 if( is->peek() != 10 ) {
346 *is >> pdf_id1 >> pdf_id2;
347 }
348 is->ignore(2,'\n');
349 if( id1 == 0 ) return false;
350 PdfInfo* pdf = new PdfInfo( id1, id2, x1, x2, scale, pdf1, pdf2,
351 pdf_id1, pdf_id2 );
352 //
353 return pdf;
354}
355
356GenVertex* CommonIO::read_vertex( std::istream* is, TempParticleMap& particle_to_end_vertex )
357{
358 // assumes mode has already been checked
359 //
360 // test to be sure the next entry is of type "V" then ignore it
361 if ( !(*is) || is->peek()!='V' ) {
362 std::cerr << "CommonIO::read_vertex setting badbit." << std::endl;
363 std::string line;
364 *is >> line;
365 std::cerr << "attempting to read " << line << std::endl;
366 is->clear(std::ios::badbit);
367 return false;
368 }
369 is->ignore();
370 // read values into temp variables, then create a new GenVertex object
371 int identifier =0, id =0, num_orphans_in =0,
372 num_particles_out = 0, weights_size = 0;
373 double x = 0., y = 0., z = 0., t = 0.;
374 *is >> identifier >> id >> x >> y >> z >> t
375 >> num_orphans_in >> num_particles_out >> weights_size;
376 WeightContainer weights(weights_size);
377 for ( int i1 = 0; i1 < weights_size; ++i1 ) *is >> weights[i1];
378 is->ignore(2,'\n');
379 GenVertex* v = new GenVertex( FourVector(x,y,z,t),
380 id, weights);
381 v->suggest_barcode( identifier );
382 //
383 // read and create the associated particles. outgoing particles are
384 // added to their production vertices immediately, while incoming
385 // particles are added to a map and handles later.
386 for ( int i2 = 1; i2 <= num_orphans_in; ++i2 ) {
387 read_particle(is,particle_to_end_vertex);
388 }
389 for ( int i3 = 1; i3 <= num_particles_out; ++i3 ) {
390 v->add_particle_out( read_particle(is,particle_to_end_vertex) );
391 }
392 return v;
393}
394
395GenParticle* CommonIO::read_particle(std::istream* is,
396 TempParticleMap& particle_to_end_vertex ){
397 // assumes mode has already been checked
398 //
399 // test to be sure the next entry is of type "P" then ignore it
400 if ( !(*is) || is->peek()!='P' ) {
401 std::cerr << "CommonIO::read_particle setting badbit."
402 << std::endl;
403 is->clear(std::ios::badbit);
404 return false;
405 }
406 is->ignore();
407 //
408 // declare variables to be read in to, and read everything except flow
409 double px = 0., py = 0., pz = 0., e = 0., m = 0., theta = 0., phi = 0.;
410 int bar_code = 0, id = 0, status = 0, end_vtx_code = 0, flow_size = 0;
411 if( m_io_type == ascii ) {
412 *is >> bar_code >> id >> px >> py >> pz >> e >> status
413 >> theta >> phi >> end_vtx_code >> flow_size;
414 } else {
415 *is >> bar_code >> id >> px >> py >> pz >> e >> m >> status
416 >> theta >> phi >> end_vtx_code >> flow_size;
417 }
418 //
419 // read flow patterns if any exist
420 Flow flow;
421 int code_index, code;
422 for ( int i = 1; i <= flow_size; ++i ) {
423 *is >> code_index >> code;
424 flow.set_icode( code_index,code);
425 }
426 is->ignore(2,'\n'); // '\n' at end of entry
427 GenParticle* p = new GenParticle( FourVector(px,py,pz,e),
428 id, status, flow,
429 Polarization(theta,phi) );
430 if( m_io_type != ascii ) {
431 p->set_generated_mass( m );
432 }
433 p->suggest_barcode( bar_code );
434 //
435 // all particles are connected to their end vertex separately
436 // after all particles and vertices have been created - so we keep
437 // a map of all particles that have end vertices
438 if ( end_vtx_code != 0 ) {
439 particle_to_end_vertex.addEndParticle(p,end_vtx_code);
440 }
441 return p;
442}
443
444ParticleData* CommonIO::read_particle_data( std::istream* is, ParticleDataTable* pdt ) {
445 // assumes mode has already been checked
446 //
447 // test to be sure the next entry is of type "D" then ignore it
448 if ( !(*is) || is->peek()!='D' ) return 0;
449 is->ignore();
450 //
451 // read values into temp variables then create new ParticleData object
452 char its_name[22];
453 int its_id = 0, its_spin = 0;
454 double its_charge = 0, its_mass = 0, its_clifetime = 0;
455 *is >> its_id >> its_charge >> its_mass
456 >> its_clifetime >> its_spin;
457 is->ignore(1); // eat the " "
458 is->getline( its_name, 22, '\n' );
459 ParticleData* pdata = new ParticleData( its_name, its_id, its_charge,
460 its_mass, its_clifetime,
461 double(its_spin)/2.);
462 pdt->insert(pdata);
463 return pdata;
464}
465
466bool CommonIO::read_units( std::istream* is, GenEvent* evt ) {
467 // assumes mode has already been checked
468 //
469 // test to be sure the next entry is of type "F" then ignore it
470 if ( !(*is) ) {
471 std::cerr << "CommonIO::read_units setting badbit." << std::endl;
472 is->clear(std::ios::badbit);
473 return false;
474 }
475 // have no units, but this is not an error
476 // releases prior to 2.04.00 did not write unit information
477 if ( is->peek() !='U') {
478 evt->use_units( m_io_momentum_unit, m_io_position_unit );
479 return true;
480 }
481 is->ignore(); // ignore the first character in the line
482 std::string mom, pos;
483 *is >> mom >> pos;
484 is->ignore(1); // eat the extra whitespace
485 evt->use_units(mom,pos);
486 //
487 return true;
488}
489
490} // end namespace HepMC
491
Note: See TracBrowser for help on using the repository browser.