HepMC Reference Documentation

HepMC

IO_ExtendedAscii.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 // extended format contains HeavyIon and PdfInfo classes
00008 
00009 #include "HepMC/IO_ExtendedAscii.h"
00010 #include "HepMC/GenEvent.h"
00011 #include "HepMC/ParticleDataTable.h"
00012 #include "HepMC/HeavyIon.h"
00013 #include "HepMC/PdfInfo.h"
00014 #include "HepMC/Version.h"
00015 
00016 namespace HepMC {
00017 
00018     IO_ExtendedAscii::IO_ExtendedAscii( const char* filename, std::ios::openmode mode ) 
00019         : m_mode(mode), m_file(filename, mode), m_finished_first_event_io(0) 
00020     {
00021         if ( (m_mode&std::ios::out && m_mode&std::ios::in) ||
00022              (m_mode&std::ios::app && m_mode&std::ios::in) ) {
00023             std::cerr << "IO_ExtendedAscii::IO_ExtendedAscii Error, open of file requested "
00024                       << "of input AND output type. Not allowed. Closing file."
00025                       << std::endl;
00026             m_file.close();
00027             return;
00028         }
00029         // precision 16 (# digits following decimal point) is the minimum that
00030         //  will capture the full information stored in a double
00031         m_file.precision(16);
00032         // we use decimal to store integers, because it is smaller than hex!
00033         m_file.setf(std::ios::dec,std::ios::basefield);
00034         m_file.setf(std::ios::scientific,std::ios::floatfield);
00035     }
00036 
00037     IO_ExtendedAscii::~IO_ExtendedAscii() {
00038         write_end_listing();
00039         m_file.close();
00040     }
00041 
00042     void IO_ExtendedAscii::print( std::ostream& ostr ) const { 
00043         ostr << "IO_ExtendedAscii: unformated ascii file IO for machine reading.\n" 
00044              << "\tFile openmode: " << m_mode 
00045              << " file state: " << m_file.rdstate()
00046              << " bad:" << (m_file.rdstate()&std::ios::badbit)
00047              << " eof:" << (m_file.rdstate()&std::ios::eofbit)
00048              << " fail:" << (m_file.rdstate()&std::ios::failbit)
00049              << " good:" << (m_file.rdstate()&std::ios::goodbit) << std::endl;
00050     }
00051 
00052     void IO_ExtendedAscii::write_event( const GenEvent* evt ) {
00054         //
00055         // check the state of m_file is good, and that it is in output mode
00056         if ( !evt || !m_file ) return;
00057         if ( !m_mode&std::ios::out ) {
00058             std::cerr << "HepMC::IO_ExtendedAscii::write_event "
00059                       << " attempt to write to input file." << std::endl;
00060             return;
00061         }
00062         //
00063         // write event listing key before first event only.
00064         if ( !m_finished_first_event_io ) {
00065             m_finished_first_event_io = 1;
00066             m_file << "\n" << "HepMC::Version " << versionName();
00067             m_file << "\n" << "HepMC::IO_ExtendedAscii-START_EVENT_LISTING\n";
00068         }
00069         //
00070         // output the event data including the number of primary vertices
00071         //  and the total number of vertices
00072         std::vector<long int> random_states = evt->random_states();
00073         m_file << 'E';
00074         output( evt->event_number() );
00075         output( evt->mpi() );
00076         output( evt->event_scale() );
00077         output( evt->alphaQCD() );
00078         output( evt->alphaQED() );
00079         output( evt->signal_process_id() );
00080         output(   ( evt->signal_process_vertex() ?
00081                     evt->signal_process_vertex()->barcode() : 0 )   );
00082         output( evt->vertices_size() ); // total number of vertices.
00083         write_beam_particles( evt->beam_particles() );
00084         output( (int)random_states.size() );
00085         for ( std::vector<long int>::iterator rs = random_states.begin(); 
00086               rs != random_states.end(); ++rs ) {
00087             output( *rs );
00088         }
00089         output( (int)evt->weights().size() );
00090         for ( WeightContainer::const_iterator w = evt->weights().begin(); 
00091               w != evt->weights().end(); ++w ) {
00092             output( *w );
00093         }
00094         output('\n');
00095         write_heavy_ion( evt->heavy_ion() );
00096         write_pdf_info( evt->pdf_info() );
00097         //
00098         // Output all of the vertices - note there is no real order.
00099         for ( GenEvent::vertex_const_iterator v = evt->vertices_begin();
00100               v != evt->vertices_end(); ++v ) {
00101             write_vertex( *v );
00102         }
00103     }
00104 
00105     bool IO_ExtendedAscii::fill_next_event( GenEvent* evt ){
00106         //
00107         //
00108         // test that evt pointer is not null
00109         if ( !evt ) {
00110             std::cerr 
00111                 << "IO_ExtendedAscii::fill_next_event error - passed null event." 
00112                 << std::endl;
00113             return 0;
00114         }
00115         // check the state of m_file is good, and that it is in input mode
00116         if ( !m_file ) return 0;
00117         if ( !(m_mode&std::ios::in) ) {
00118             std::cerr << "HepMC::IO_ExtendedAscii::fill_next_event "
00119                       << " attempt to read from output file." << std::endl;
00120             return 0;
00121         }
00122         //
00123         // search for event listing key before first event only.
00124         //
00125         // skip through the file just after first occurence of the start_key
00126         if ( !m_finished_first_event_io ) {
00127             m_file.seekg( 0 ); // go to position zero in the file.
00128             if (!search_for_key_end( m_file,
00129                                      "HepMC::IO_ExtendedAscii-START_EVENT_LISTING\n")){
00130                 std::cerr << "IO_ExtendedAscii::fill_next_event start key not found "
00131                           << "setting badbit." << std::endl;
00132                 m_file.clear(std::ios::badbit); 
00133                 return 0;
00134             }
00135             m_finished_first_event_io = 1;
00136         }
00137         //
00138         // test to be sure the next entry is of type "E" then ignore it
00139         if ( !m_file || m_file.peek()!='E' ) { 
00140             // if the E is not the next entry, then check to see if it is
00141             // the end event listing key - if yes, search for another start key
00142             if ( eat_key(m_file, "HepMC::IO_ExtendedAscii-END_EVENT_LISTING\n") ) {
00143                 bool search_result = search_for_key_end(m_file,
00144                                       "HepMC::IO_ExtendedAscii-START_EVENT_LISTING\n");
00145                 if ( !search_result ) {
00146                     // this is the only case where we set an EOF state
00147                     m_file.clear(std::ios::eofbit);
00148                     return 0;
00149                 }
00150             } else {
00151                 std::cerr << "IO_ExtendedAscii::fill_next_event end key not found "
00152                           << "setting badbit." << std::endl;
00153                 m_file.clear(std::ios::badbit); 
00154                 return 0;
00155             }
00156         } 
00157         m_file.ignore();
00158         // read values into temp variables, then create a new GenEvent
00159         int event_number = 0, signal_process_id = 0, signal_process_vertex = 0,
00160             num_vertices = 0, random_states_size = 0, weights_size = 0,
00161             nmpi = 0, bp1 = 0, bp2 = 0;
00162         double eventScale = 0, alpha_qcd = 0, alpha_qed = 0;
00163         m_file >> event_number >> nmpi >> eventScale >> alpha_qcd >> alpha_qed
00164                >> signal_process_id >> signal_process_vertex
00165                >> num_vertices >> bp1 >> bp2 >> random_states_size;
00166         std::vector<long int> random_states(random_states_size);
00167         for ( int i = 0; i < random_states_size; ++i ) {
00168             m_file >> random_states[i];
00169         }
00170         m_file >> weights_size;
00171         WeightContainer weights(weights_size);
00172         for ( int ii = 0; ii < weights_size; ++ii ) m_file >> weights[ii];
00173         m_file.ignore(2,'\n');
00174         // 
00175         // fill signal_process_id, event_number, weights, random_states
00176         evt->set_signal_process_id( signal_process_id );
00177         evt->set_event_number( event_number );
00178         evt->set_mpi( nmpi );
00179         evt->weights() = weights;
00180         evt->set_random_states( random_states );
00181         evt->set_event_scale( eventScale );
00182         evt->set_alphaQCD( alpha_qcd );
00183         evt->set_alphaQED( alpha_qed );
00184         // get HeavyIon and PdfInfo
00185         HeavyIon* ion = read_heavy_ion();
00186         if(ion) evt->set_heavy_ion( *ion );
00187         PdfInfo* pdf = read_pdf_info();
00188         if(pdf) evt->set_pdf_info( *pdf );
00189         //
00190         // the end vertices of the particles are not connected until
00191         //  after the event is read --- we store the values in a map until then
00192         TempParticleMap particle_to_end_vertex;
00193         //
00194         // read in the vertices
00195         for ( int iii = 1; iii <= num_vertices; ++iii ) {
00196             GenVertex* v = read_vertex(particle_to_end_vertex);
00197             evt->add_vertex( v );
00198         }
00199         // set the signal process vertex
00200         if ( signal_process_vertex ) {
00201             evt->set_signal_process_vertex( 
00202                 evt->barcode_to_vertex(signal_process_vertex) );
00203         }
00204         //
00205         // last connect particles to their end vertices
00206         GenParticle* beam1(0);
00207         GenParticle* beam2(0);
00208         for ( std::map<int,GenParticle*>::iterator pmap 
00209                   = particle_to_end_vertex.order_begin(); 
00210               pmap != particle_to_end_vertex.order_end(); ++pmap ) {
00211             GenParticle* p =  pmap->second;
00212             int vtx = particle_to_end_vertex.end_vertex( p );
00213             GenVertex* itsDecayVtx = evt->barcode_to_vertex(vtx);
00214             if ( itsDecayVtx ) itsDecayVtx->add_particle_in( p );
00215             else {
00216                 std::cerr << "IO_ExtendedAscii::fill_next_event ERROR particle points"
00217                           << "\n to null end vertex. " <<std::endl;
00218             }
00219             // also look for the beam particles
00220             if( p->barcode() == bp1 ) beam1 = p;
00221             if( p->barcode() == bp2 ) beam2 = p;
00222         }
00223         evt->set_beam_particles(beam1,beam2);
00224         return 1;
00225     }
00226 
00227     void IO_ExtendedAscii::write_comment( const std::string comment ) {
00228         // check the state of m_file is good, and that it is in output mode
00229         if ( !m_file ) return;
00230         if ( !m_mode&std::ios::out ) {
00231             std::cerr << "HepMC::IO_ExtendedAscii::write_comment "
00232                       << " attempt to write to input file." << std::endl;
00233             return;
00234         }
00235         // write end of event listing key if events have already been written
00236         write_end_listing();
00237         // insert the comment key before the comment
00238         m_file << "\n" << "HepMC::IO_ExtendedAscii-COMMENT\n";
00239         m_file << comment << std::endl;
00240     }
00241 
00242     void IO_ExtendedAscii::write_particle_data_table( const ParticleDataTable* pdt) {
00243         //
00244         // check the state of m_file is good, and that it is in output mode
00245         if ( !m_file ) return;
00246         if ( !m_mode&std::ios::out ) {
00247             std::cerr << "HepMC::IO_ExtendedAscii::write_particle_data_table "
00248                       << " attempt to write to input file." << std::endl;
00249             return;
00250         }
00251         // write end of event listing key if events have already been written
00252         write_end_listing();
00253         //
00254         m_file << "\n" << "HepMC::IO_ExtendedAscii-START_PARTICLE_DATA\n";
00255         for ( ParticleDataTable::const_iterator pd = pdt->begin(); 
00256               pd != pdt->end(); pd++ ) {
00257             write_particle_data( pd->second );
00258         }
00259         m_file << "HepMC::IO_ExtendedAscii-END_PARTICLE_DATA\n" << std::flush;
00260     }
00261 
00262     bool IO_ExtendedAscii::fill_particle_data_table( ParticleDataTable* pdt ) {
00263         //
00264         // test that pdt pointer is not null
00265         if ( !pdt ) {
00266             std::cerr 
00267                 << "IO_ExtendedAscii::fill_particle_data_table - passed null table." 
00268                 << std::endl;
00269             return 0;
00270         }
00271         //
00272         // check the state of m_file is good, and that it is in input mode
00273         if ( !m_file ) return 0;
00274         if ( !m_mode&std::ios::in ) {
00275             std::cerr << "HepMC::IO_ExtendedAscii::fill_particle_data_table "
00276                       << " attempt to read from output file." << std::endl;
00277             return 0;
00278         }
00279         // position to beginning of file
00280         int initial_file_position = m_file.tellg();
00281         std::ios::iostate initial_state = m_file.rdstate();
00282         m_file.seekg( 0 );
00283         // skip through the file just after first occurence of the start_key
00284         if (!search_for_key_end( m_file,
00285                                  "HepMC::IO_ExtendedAscii-START_PARTICLE_DATA\n")) {
00286             m_file.seekg( initial_file_position );
00287             std::cerr << "IO_ExtendedAscii::fill_particle_data_table start key not  "
00288                       << "found setting badbit." << std::endl;
00289             m_file.clear(std::ios::badbit); 
00290             return 0;
00291         }
00292         //
00293         pdt->set_description("Read with IO_ExtendedAscii");
00294         // 
00295         // read Individual GenParticle data entries
00296         while ( read_particle_data( pdt ) );
00297         //
00298         // eat end_key
00299         if ( !eat_key(m_file,"HepMC::IO_ExtendedAscii-END_PARTICLE_DATA\n") ){
00300             std::cerr << "IO_ExtendedAscii::fill_particle_data_table end key not  "
00301                       << "found setting badbit." << std::endl;
00302             m_file.clear(std::ios::badbit);
00303         }
00304         // put the file back into its original state and position
00305         m_file.clear( initial_state );
00306         m_file.seekg( initial_file_position );
00307         return 1;
00308     }
00309 
00310     void IO_ExtendedAscii::write_vertex( GenVertex* v ) {
00311         // assumes mode has already been checked
00312         if ( !v || !m_file ) {
00313             std::cerr << "IO_ExtendedAscii::write_vertex !v||!m_file, "
00314                       << "v="<< v << " setting badbit" << std::endl;
00315             m_file.clear(std::ios::badbit); 
00316             return;
00317         }
00318         // First collect info we need
00319         // count the number of orphan particles going into v
00320         int num_orphans_in = 0;
00321         for ( GenVertex::particles_in_const_iterator p1
00322                   = v->particles_in_const_begin();
00323               p1 != v->particles_in_const_end(); ++p1 ) {
00324             if ( !(*p1)->production_vertex() ) ++num_orphans_in;
00325         }
00326         //
00327         m_file << 'V';
00328         output( v->barcode() ); // v's unique identifier
00329         output( v->id() );
00330         output( v->position().x() );
00331         output( v->position().y() );
00332         output( v->position().z() );
00333         output( v->position().t() );
00334         output( num_orphans_in );
00335         output( (int)v->particles_out_size() );
00336         output( (int)v->weights().size() );
00337         for ( WeightContainer::iterator w = v->weights().begin(); 
00338               w != v->weights().end(); ++w ) {
00339             output( *w );
00340         }
00341         output('\n');
00342         for ( GenVertex::particles_in_const_iterator p2 
00343                   = v->particles_in_const_begin();
00344               p2 != v->particles_in_const_end(); ++p2 ) {
00345             if ( !(*p2)->production_vertex() ) {
00346                 write_particle( *p2 );
00347             }
00348         }
00349         for ( GenVertex::particles_out_const_iterator p3 
00350                   = v->particles_out_const_begin();
00351               p3 != v->particles_out_const_end(); ++p3 ) {
00352             write_particle( *p3 );
00353         }
00354     }
00355 
00356     void IO_ExtendedAscii::write_beam_particles( 
00357         std::pair<GenParticle *,GenParticle *> pr ) {
00358         GenParticle* p = pr.first;
00359         //m_file << 'B';
00360         if(!p) {
00361            output( 0 );
00362         } else {
00363            output( p->barcode() );
00364         }
00365         p = pr.second;
00366         if(!p) {
00367            output( 0 );
00368         } else {
00369            output( p->barcode() );
00370         }
00371     }
00372 
00373     void IO_ExtendedAscii::write_heavy_ion( HeavyIon* ion ) {
00374         // assumes mode has already been checked
00375         if ( !m_file ) {
00376             std::cerr << "IO_ExtendedAscii::write_heavy_ion !m_file, "
00377                       << " setting badbit" << std::endl;
00378             m_file.clear(std::ios::badbit); 
00379             return;
00380         }
00381         m_file << 'H';
00382         // HeavyIon* is set to 0 by default
00383         if ( !ion  ) {
00384             output( 0 );
00385             output( 0 );
00386             output( 0 );
00387             output( 0 );
00388             output( 0 );
00389             output( 0 );
00390             output( 0 );
00391             output( 0 );
00392             output( 0 );
00393             output( 0. );
00394             output( 0. );
00395             output( 0. );
00396             output( 0. );
00397             output('\n');
00398             return;
00399         }
00400         //
00401         output( ion->Ncoll_hard() );
00402         output( ion->Npart_proj() );
00403         output( ion->Npart_targ() );
00404         output( ion->Ncoll() );
00405         output( ion->spectator_neutrons() );
00406         output( ion->spectator_protons() );
00407         output( ion->N_Nwounded_collisions() );
00408         output( ion->Nwounded_N_collisions() );
00409         output( ion->Nwounded_Nwounded_collisions() );
00410         output( ion->impact_parameter() );
00411         output( ion->event_plane_angle() );
00412         output( ion->eccentricity() );
00413         output( ion->sigma_inel_NN() );
00414         output('\n');
00415     }
00416 
00417     void IO_ExtendedAscii::write_pdf_info( PdfInfo* pdf ) {
00418         // assumes mode has already been checked
00419         if ( !m_file ) {
00420             std::cerr << "IO_ExtendedAscii::write_pdf_info !m_file, "
00421                       << " setting badbit" << std::endl;
00422             m_file.clear(std::ios::badbit); 
00423             return;
00424         }
00425         m_file << 'F';
00426         // PdfInfo* is set to 0 by default
00427         if ( !pdf ) {
00428             output( 0 );
00429             output( 0 );
00430             output( 0. );
00431             output( 0. );
00432             output( 0. );
00433             output( 0. );
00434             output( 0. );
00435             output('\n');
00436             return;
00437         }
00438         //
00439         output( pdf->id1() );
00440         output( pdf->id2() );
00441         output( pdf->x1() );
00442         output( pdf->x2() );
00443         output( pdf->scalePDF() );
00444         output( pdf->pdf1() );
00445         output( pdf->pdf2() );
00446         output('\n');
00447     }
00448 
00449     void IO_ExtendedAscii::write_particle( GenParticle* p ) {
00450         // assumes mode has already been checked
00451         if ( !p || !m_file ) {
00452             std::cerr << "IO_ExtendedAscii::write_particle !p||!m_file, "
00453                       << "v="<< p << " setting badbit" << std::endl;
00454             m_file.clear(std::ios::badbit); 
00455             return;
00456         }
00457         m_file << 'P';
00458         output( p->barcode() );
00459         output( p->pdg_id() );
00460         output( p->momentum().px() );
00461         output( p->momentum().py() );
00462         output( p->momentum().pz() );
00463         output( p->momentum().e() );
00464         output( p->generated_mass() );
00465         output( p->status() );
00466         output( p->polarization().theta() );
00467         output( p->polarization().phi() );
00468         // since end_vertex is oftentimes null, this CREATES a null vertex
00469         // in the map
00470         output(   ( p->end_vertex() ? p->end_vertex()->barcode() : 0 )  );
00471         m_file << ' ' << p->flow() << "\n";
00472     }
00473 
00474     void IO_ExtendedAscii::write_particle_data( const ParticleData* pdata ) {
00475         // assumes mode has already been checked
00476         if ( !pdata || !m_file ) {
00477             std::cerr << "IO_ExtendedAscii::write_particle_data !pdata||!m_file, "
00478                       << "pdata="<< pdata << " setting badbit" << std::endl;
00479             m_file.clear(std::ios::badbit); 
00480             return;
00481         }
00482         m_file << 'D';
00483         output( pdata->pdg_id() );
00484         output( pdata->charge() );
00485         output( pdata->mass() );
00486         output( pdata->clifetime() );
00487         output( (int)(pdata->spin()*2.+.1) );
00488         // writes the first 21 characters starting with 0
00489         m_file << " " << pdata->name().substr(0,21) << "\n";
00490     }
00491 
00492     GenVertex* IO_ExtendedAscii::read_vertex
00493     ( TempParticleMap& particle_to_end_vertex )
00494     {
00495         // assumes mode has already been checked
00496         //
00497         // test to be sure the next entry is of type "V" then ignore it
00498         if ( !m_file || m_file.peek()!='V' ) {
00499             std::cerr << "IO_ExtendedAscii::read_vertex setting badbit." << std::endl;
00500             m_file.clear(std::ios::badbit); 
00501             return 0;
00502         } 
00503         m_file.ignore();
00504         // read values into temp variables, then create a new GenVertex object
00505         int identifier =0, id =0, num_orphans_in =0, 
00506             num_particles_out = 0, weights_size = 0;
00507         double x = 0., y = 0., z = 0., t = 0.; 
00508         m_file >> identifier >> id >> x >> y >> z >> t
00509                >> num_orphans_in >> num_particles_out >> weights_size;
00510         WeightContainer weights(weights_size);
00511         for ( int i1 = 0; i1 < weights_size; ++i1 ) m_file >> weights[i1];
00512         m_file.ignore(2,'\n');
00513         GenVertex* v = new GenVertex( FourVector(x,y,z,t),
00514                                 id, weights);
00515         v->suggest_barcode( identifier );
00516         //
00517         // read and create the associated particles. outgoing particles are
00518         //  added to their production vertices immediately, while incoming
00519         //  particles are added to a map and handles later.
00520         for ( int i2 = 1; i2 <= num_orphans_in; ++i2 ) {
00521             read_particle(particle_to_end_vertex);
00522         }
00523         for ( int i3 = 1; i3 <= num_particles_out; ++i3 ) {
00524             v->add_particle_out( read_particle(particle_to_end_vertex) );
00525         }
00526         return v;
00527     }
00528 
00529     HeavyIon* IO_ExtendedAscii::read_heavy_ion()
00530     {
00531         // assumes mode has already been checked
00532         //
00533         // test to be sure the next entry is of type "H" then ignore it
00534         if ( !m_file || m_file.peek()!='H' ) {
00535             std::cerr << "IO_ExtendedAscii::read_heavy_ion setting badbit." << std::endl;
00536             m_file.clear(std::ios::badbit); 
00537             return 0;
00538         } 
00539         m_file.ignore();
00540         // read values into temp variables, then create a new HeavyIon object
00541         int nh =0, np =0, nt =0, nc =0, 
00542             neut = 0, prot = 0, nw =0, nwn =0, nwnw =0;
00543         float impact = 0., plane = 0., xcen = 0., inel = 0.; 
00544         m_file >> nh >> np >> nt >> nc >> neut >> prot
00545                >> nw >> nwn >> nwnw >> impact >> plane >> xcen >> inel;
00546         m_file.ignore(2,'\n');
00547         if( nh == 0 ) return 0;
00548         HeavyIon* ion = new HeavyIon(nh, np, nt, nc, neut, prot,
00549                                      nw, nwn, nwnw, 
00550                                      impact, plane, xcen, inel );
00551         //
00552         return ion;
00553     }
00554 
00555     PdfInfo* IO_ExtendedAscii::read_pdf_info()
00556     {
00557         // assumes mode has already been checked
00558         //
00559         // test to be sure the next entry is of type "F" then ignore it
00560         if ( !m_file || m_file.peek() !='F') {
00561             std::cerr << "IO_ExtendedAscii::read_pdf_info setting badbit." << std::endl;
00562             m_file.clear(std::ios::badbit); 
00563             return 0;
00564         } 
00565         m_file.ignore();
00566         // read values into temp variables, then create a new PdfInfo object
00567         int id1 =0, id2 =0;
00568         double  x1 = 0., x2 = 0., scale = 0., pdf1 = 0., pdf2 = 0.; 
00569         m_file >> id1 >> id2 >> x1 >> x2 >> scale >> pdf1 >> pdf2;
00570         m_file.ignore(2,'\n');
00571         //std::cout << "read " << id1 << std::endl;
00572         if( id1 == 0 ) return 0;
00573         PdfInfo* pdf = new PdfInfo( id1, id2, x1, x2, scale, pdf1, pdf2);
00574         //
00575         return pdf;
00576     }
00577 
00578     GenParticle* IO_ExtendedAscii::read_particle(
00579         TempParticleMap& particle_to_end_vertex ){
00580         // assumes mode has already been checked
00581         //
00582         // test to be sure the next entry is of type "P" then ignore it
00583         if ( !m_file || m_file.peek()!='P' ) { 
00584             std::cerr << "IO_ExtendedAscii::read_particle setting badbit." 
00585                       << std::endl;
00586             m_file.clear(std::ios::badbit); 
00587             return 0;
00588         } 
00589         m_file.ignore();
00590         //
00591         // declare variables to be read in to, and read everything except flow
00592         double px = 0., py = 0., pz = 0., e = 0., m = 0., theta = 0., phi = 0.;
00593         int bar_code = 0, id = 0, status = 0, end_vtx_code = 0, flow_size = 0;
00594         m_file >> bar_code >> id >> px >> py >> pz >> e >> m >> status 
00595                >> theta >> phi >> end_vtx_code >> flow_size;
00596         //
00597         // read flow patterns if any exist
00598         Flow flow;
00599         int code_index, code;
00600         for ( int i = 1; i <= flow_size; ++i ) {
00601             m_file >> code_index >> code;
00602             flow.set_icode( code_index,code);
00603         }
00604         m_file.ignore(2,'\n'); // '\n' at end of entry
00605         GenParticle* p = new GenParticle( FourVector(px,py,pz,e), 
00606                                     id, status, flow, 
00607                                     Polarization(theta,phi) );
00608         p->set_generated_mass( m );
00609         p->suggest_barcode( bar_code );
00610         //
00611         // all particles are connected to their end vertex separately 
00612         // after all particles and vertices have been created - so we keep
00613         // a map of all particles that have end vertices
00614         if ( end_vtx_code != 0 ) {
00615             particle_to_end_vertex.addEndParticle(p,end_vtx_code);
00616         }
00617         return p;
00618     }
00619 
00620     ParticleData* IO_ExtendedAscii::read_particle_data( ParticleDataTable* pdt ) {
00621         // assumes mode has already been checked
00622         //
00623         // test to be sure the next entry is of type "D" then ignore it
00624         if ( !m_file || m_file.peek()!='D' ) return 0;
00625         m_file.ignore();
00626         //
00627         // read values into temp variables then create new ParticleData object
00628         char its_name[22];
00629         int its_id = 0, its_spin = 0;  
00630         double its_charge = 0, its_mass = 0, its_clifetime = 0;
00631         m_file >> its_id >> its_charge >> its_mass 
00632                >> its_clifetime >> its_spin;
00633         m_file.ignore(1); // eat the " "
00634         m_file.getline( its_name, 22, '\n' );
00635         ParticleData* pdata = new ParticleData( its_name, its_id, its_charge, 
00636                                                 its_mass, its_clifetime, 
00637                                                 double(its_spin)/2.);
00638         pdt->insert(pdata);
00639         return pdata;
00640     }
00641 
00642     bool IO_ExtendedAscii::write_end_listing() {
00643         if ( m_finished_first_event_io && m_mode&std::ios::out ) {
00644             m_file << "HepMC::IO_ExtendedAscii-END_EVENT_LISTING\n" << std::flush;
00645             m_finished_first_event_io = 0;
00646             return 1;
00647         }
00648         return 0;
00649     }
00650 
00651     bool IO_ExtendedAscii::search_for_key_end( std::istream& in, const char* key ) {
00655         // 
00656         char c[1];
00657         unsigned int index = 0;
00658         while ( in.get(c[0]) ) {
00659             if ( c[0] == key[index] ) { 
00660                 ++index;
00661             } else { index = 0; }
00662             if ( index == strlen(key) ) return 1;
00663         }
00664         return 0;
00665     }
00666 
00667     bool IO_ExtendedAscii::search_for_key_beginning( std::istream& in,
00668                                              const char* key ) {
00670         if ( search_for_key_end( in, key) ) {
00671             int i = strlen(key);
00672             while ( i>=0 ) in.putback(key[i--]); 
00673             return 1; 
00674         } else {
00675             in.putback(EOF);
00676             in.clear();
00677             return 0;
00678         }
00679     }
00680 
00681     bool IO_ExtendedAscii::eat_key( std::iostream& in, const char* key ) {
00686         int key_length = strlen(key);
00687         // below is the only way I know of to get a variable length string
00688         //  conforming to ansi standard.
00689         char* c = new char[key_length +1];
00690         int i=0;
00691         // read the stream until get fails (because of EOF), a character
00692         //  doesn't match a character in the string, or all characters in
00693         //  the string have been checked and match.
00694         while ( in.get(c[i]) && c[i]==key[i] && i<key_length ) {
00695             ++i;
00696         }
00697         if ( i == key_length ) {
00698             delete [] c;
00699             return 1;
00700         }
00701         //
00702         // if we get here, then we have eaten the wrong this and we must put it
00703         // back
00704         while ( i>=0 ) in.putback(c[i--]); 
00705         delete c;
00706         return 0;
00707     }
00708 
00709     int IO_ExtendedAscii::find_in_map( const std::map<GenVertex*,int>& m, 
00710                                GenVertex* v ) const {
00711         std::map<GenVertex*,int>::const_iterator iter = m.find(v);
00712         if ( iter == m.end() ) return 0;
00713         return iter->second;
00714     }
00715         
00716 } // HepMC

Generated on Tue Feb 5 13:25:44 2008 for HepMC by  doxygen 1.5.1-3