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/CommonIO.h"
00015 #include "HepMC/Version.h"
00016 
00017 namespace HepMC {
00018 
00019     IO_ExtendedAscii::IO_ExtendedAscii( const char* filename, std::ios::openmode mode ) 
00020         : m_mode(mode), m_file(filename, mode), m_finished_first_event_io(0),
00021           m_common_io() 
00022     {
00023         std::cout << "-------------------------------------------------------" << std::endl;
00024         std::cout << "Use of HepMC/IO_ExtendedAscii is deprecated" << std::endl;
00025         std::cout << "-------------------------------------------------------" << std::endl;
00026         if ( (m_mode&std::ios::out && m_mode&std::ios::in) ||
00027              (m_mode&std::ios::app && m_mode&std::ios::in) ) {
00028             std::cerr << "IO_ExtendedAscii::IO_ExtendedAscii Error, open of file requested "
00029                       << "of input AND output type. Not allowed. Closing file."
00030                       << std::endl;
00031             m_file.close();
00032             return;
00033         }
00034         // precision 16 (# digits following decimal point) is the minimum that
00035         //  will capture the full information stored in a double
00036         m_file.precision(16);
00037         // we use decimal to store integers, because it is smaller than hex!
00038         m_file.setf(std::ios::dec,std::ios::basefield);
00039         m_file.setf(std::ios::scientific,std::ios::floatfield);
00040     }
00041 
00042     IO_ExtendedAscii::~IO_ExtendedAscii() {
00043         write_end_listing();
00044         m_file.close();
00045     }
00046 
00047     void IO_ExtendedAscii::print( std::ostream& ostr ) const { 
00048         ostr << "IO_ExtendedAscii: unformated ascii file IO for machine reading.\n" 
00049              << "\tFile openmode: " << m_mode 
00050              << " file state: " << m_file.rdstate()
00051              << " bad:" << (m_file.rdstate()&std::ios::badbit)
00052              << " eof:" << (m_file.rdstate()&std::ios::eofbit)
00053              << " fail:" << (m_file.rdstate()&std::ios::failbit)
00054              << " good:" << (m_file.rdstate()&std::ios::goodbit) << std::endl;
00055     }
00056 
00057     void IO_ExtendedAscii::write_event( const GenEvent* evt ) {
00059         //
00060         // check the state of m_file is good, and that it is in output mode
00061         if ( !evt || !m_file ) return;
00062         if ( !m_mode&std::ios::out ) {
00063             std::cerr << "HepMC::IO_ExtendedAscii::write_event "
00064                       << " attempt to write to input file." << std::endl;
00065             return;
00066         }
00067         //
00068         // write event listing key before first event only.
00069         if ( !m_finished_first_event_io ) {
00070             m_finished_first_event_io = 1;
00071             m_file << "\n" << "HepMC::Version " << versionName();
00072             m_file << "\n" ;
00073             m_common_io.write_IO_ExtendedAscii_Key(m_file);
00074         }
00075         //
00076         // output the event data including the number of primary vertices
00077         //  and the total number of vertices
00078         std::vector<long int> random_states = evt->random_states();
00079         m_file << 'E';
00080         output( evt->event_number() );
00081         output( evt->mpi() );
00082         output( evt->event_scale() );
00083         output( evt->alphaQCD() );
00084         output( evt->alphaQED() );
00085         output( evt->signal_process_id() );
00086         output(   ( evt->signal_process_vertex() ?
00087                     evt->signal_process_vertex()->barcode() : 0 )   );
00088         output( evt->vertices_size() ); // total number of vertices.
00089         write_beam_particles( evt->beam_particles() );
00090         output( (int)random_states.size() );
00091         for ( std::vector<long int>::iterator rs = random_states.begin(); 
00092               rs != random_states.end(); ++rs ) {
00093             output( *rs );
00094         }
00095         output( (int)evt->weights().size() );
00096         for ( WeightContainer::const_iterator w = evt->weights().begin(); 
00097               w != evt->weights().end(); ++w ) {
00098             output( *w );
00099         }
00100         output('\n');
00101         write_heavy_ion( evt->heavy_ion() );
00102         write_pdf_info( evt->pdf_info() );
00103         //
00104         // Output all of the vertices - note there is no real order.
00105         for ( GenEvent::vertex_const_iterator v = evt->vertices_begin();
00106               v != evt->vertices_end(); ++v ) {
00107             write_vertex( *v );
00108         }
00109     }
00110 
00111     bool IO_ExtendedAscii::fill_next_event( GenEvent* evt ){
00112         //
00113         //
00114         // test that evt pointer is not null
00115         if ( !evt ) {
00116             std::cerr 
00117                 << "IO_ExtendedAscii::fill_next_event error - passed null event." 
00118                 << std::endl;
00119             return false;
00120         }
00121         // check the state of m_file is good, and that it is in input mode
00122         if ( !m_file ) return false;
00123         if ( !(m_mode&std::ios::in) ) {
00124             std::cerr << "HepMC::IO_ExtendedAscii::fill_next_event "
00125                       << " attempt to read from output file." << std::endl;
00126             return false;
00127         }
00128         //
00129         // search for event listing key before first event only.
00130         //
00131         // skip through the file just after first occurence of the start_key
00132         int iotype;
00133         if ( !m_finished_first_event_io ) {
00134             iotype = m_common_io.find_file_type(m_file);
00135             if( iotype != extascii ) {
00136                 std::cerr << "IO_ExtendedAscii::fill_next_event start key not found "
00137                           << "setting badbit." << std::endl;
00138                 m_file.clear(std::ios::badbit); 
00139                 return false;
00140             }
00141             m_finished_first_event_io = 1;
00142         }
00143         //
00144         // test to be sure the next entry is of type "E" then ignore it
00145         if ( !m_file ) { 
00146                 std::cerr << "IO_ExtendedAscii::fill_next_event end of stream found "
00147                           << "setting badbit." << std::endl;
00148                 m_file.clear(std::ios::badbit); 
00149                 return false;
00150         }
00151         if ( !m_file || m_file.peek()!='E' ) { 
00152             // if the E is not the next entry, then check to see if it is
00153             // the end event listing key - if yes, search for another start key
00154             if ( m_common_io.find_end_key(m_file) ) {
00155                 iotype = m_common_io.find_file_type(m_file);
00156                 if( iotype != extascii ) {
00157                     // this is the only case where we set an EOF state
00158                     m_file.clear(std::ios::eofbit);
00159                     return false;
00160                 }
00161             } else {
00162                 std::cerr << "IO_ExtendedAscii::fill_next_event end key not found "
00163                           << "setting badbit." << std::endl;
00164                 m_file.clear(std::ios::badbit); 
00165                 return false;
00166             }
00167         } 
00168         m_file.ignore();
00169         // call the read method
00170         return m_common_io.read_io_extendedascii(&m_file, evt);
00171     }
00172 
00173     void IO_ExtendedAscii::write_comment( const std::string comment ) {
00174         // check the state of m_file is good, and that it is in output mode
00175         if ( !m_file ) return;
00176         if ( !m_mode&std::ios::out ) {
00177             std::cerr << "HepMC::IO_ExtendedAscii::write_comment "
00178                       << " attempt to write to input file." << std::endl;
00179             return;
00180         }
00181         // write end of event listing key if events have already been written
00182         write_end_listing();
00183         // insert the comment key before the comment
00184         m_file << "\n" << "HepMC::IO_ExtendedAscii-COMMENT\n";
00185         m_file << comment << std::endl;
00186     }
00187 
00188     void IO_ExtendedAscii::write_particle_data_table( const ParticleDataTable* pdt) {
00189         //
00190         // check the state of m_file is good, and that it is in output mode
00191         if ( !m_file ) return;
00192         if ( !m_mode&std::ios::out ) {
00193             std::cerr << "HepMC::IO_ExtendedAscii::write_particle_data_table "
00194                       << " attempt to write to input file." << std::endl;
00195             return;
00196         }
00197         // write end of event listing key if events have already been written
00198         write_end_listing();
00199         //
00200         m_file << "\n" << "HepMC::IO_ExtendedAscii-START_PARTICLE_DATA\n";
00201         for ( ParticleDataTable::const_iterator pd = pdt->begin(); 
00202               pd != pdt->end(); pd++ ) {
00203             write_particle_data( pd->second );
00204         }
00205         m_file << "HepMC::IO_ExtendedAscii-END_PARTICLE_DATA\n" << std::flush;
00206     }
00207 
00208     bool IO_ExtendedAscii::fill_particle_data_table( ParticleDataTable* pdt ) {
00209         //
00210         // test that pdt pointer is not null
00211         if ( !pdt ) {
00212             std::cerr 
00213                 << "IO_ExtendedAscii::fill_particle_data_table - passed null table." 
00214                 << std::endl;
00215             return false;
00216         }
00217         //
00218         // check the state of m_file is good, and that it is in input mode
00219         if ( !m_file ) return false;
00220         if ( !m_mode&std::ios::in ) {
00221             std::cerr << "HepMC::IO_ExtendedAscii::fill_particle_data_table "
00222                       << " attempt to read from output file." << std::endl;
00223             return false;
00224         }
00225         // position to beginning of file
00226         int initial_file_position = m_file.tellg();
00227         std::ios::iostate initial_state = m_file.rdstate();
00228         m_file.seekg( 0 );
00229         // skip through the file just after first occurence of the start_key
00230         int iotype;
00231         iotype = m_common_io.find_file_type(m_file);
00232         if( iotype != extascii_pdt ) {
00233             m_file.seekg( initial_file_position );
00234             std::cerr << "IO_ExtendedAscii::fill_particle_data_table start key not  "
00235                       << "found setting badbit." << std::endl;
00236             m_file.clear(std::ios::badbit); 
00237             return false;
00238         }
00239         //
00240         pdt->set_description("Read with IO_ExtendedAscii");
00241         m_common_io.read_io_particle_data_table( &m_file, pdt );
00242         //
00243         // check for the end event listing key
00244         iotype =  m_common_io.find_end_key(m_file);
00245         if( iotype != extascii_pdt ) {
00246             std::cerr << "IO_ExtendedAscii::fill_particle_data_table end key not  "
00247                       << "found setting badbit." << std::endl;
00248             m_file.clear(std::ios::badbit);
00249         }
00250         // put the file back into its original state and position
00251         m_file.clear( initial_state );
00252         m_file.seekg( initial_file_position );
00253         return true;
00254     }
00255 
00256     void IO_ExtendedAscii::write_vertex( GenVertex* v ) {
00257         // assumes mode has already been checked
00258         if ( !v || !m_file ) {
00259             std::cerr << "IO_ExtendedAscii::write_vertex !v||!m_file, "
00260                       << "v="<< v << " setting badbit" << std::endl;
00261             m_file.clear(std::ios::badbit); 
00262             return;
00263         }
00264         // First collect info we need
00265         // count the number of orphan particles going into v
00266         int num_orphans_in = 0;
00267         for ( GenVertex::particles_in_const_iterator p1
00268                   = v->particles_in_const_begin();
00269               p1 != v->particles_in_const_end(); ++p1 ) {
00270             if ( !(*p1)->production_vertex() ) ++num_orphans_in;
00271         }
00272         //
00273         m_file << 'V';
00274         output( v->barcode() ); // v's unique identifier
00275         output( v->id() );
00276         output( v->position().x() );
00277         output( v->position().y() );
00278         output( v->position().z() );
00279         output( v->position().t() );
00280         output( num_orphans_in );
00281         output( (int)v->particles_out_size() );
00282         output( (int)v->weights().size() );
00283         for ( WeightContainer::iterator w = v->weights().begin(); 
00284               w != v->weights().end(); ++w ) {
00285             output( *w );
00286         }
00287         output('\n');
00288         for ( GenVertex::particles_in_const_iterator p2 
00289                   = v->particles_in_const_begin();
00290               p2 != v->particles_in_const_end(); ++p2 ) {
00291             if ( !(*p2)->production_vertex() ) {
00292                 write_particle( *p2 );
00293             }
00294         }
00295         for ( GenVertex::particles_out_const_iterator p3 
00296                   = v->particles_out_const_begin();
00297               p3 != v->particles_out_const_end(); ++p3 ) {
00298             write_particle( *p3 );
00299         }
00300     }
00301 
00302     void IO_ExtendedAscii::write_beam_particles( 
00303         std::pair<HepMC::GenParticle *,HepMC::GenParticle *> pr ) {
00304         GenParticle* p = pr.first;
00305         //m_file << 'B';
00306         if(!p) {
00307            output( 0 );
00308         } else {
00309            output( p->barcode() );
00310         }
00311         p = pr.second;
00312         if(!p) {
00313            output( 0 );
00314         } else {
00315            output( p->barcode() );
00316         }
00317     }
00318 
00319     void IO_ExtendedAscii::write_heavy_ion( HeavyIon* ion ) {
00320         // assumes mode has already been checked
00321         if ( !m_file ) {
00322             std::cerr << "IO_ExtendedAscii::write_heavy_ion !m_file, "
00323                       << " setting badbit" << std::endl;
00324             m_file.clear(std::ios::badbit); 
00325             return;
00326         }
00327         m_file << 'H';
00328         // HeavyIon* is set to 0 by default
00329         if ( !ion  ) {
00330             output( 0 );
00331             output( 0 );
00332             output( 0 );
00333             output( 0 );
00334             output( 0 );
00335             output( 0 );
00336             output( 0 );
00337             output( 0 );
00338             output( 0 );
00339             output( 0. );
00340             output( 0. );
00341             output( 0. );
00342             output( 0. );
00343             output('\n');
00344             return;
00345         }
00346         //
00347         output( ion->Ncoll_hard() );
00348         output( ion->Npart_proj() );
00349         output( ion->Npart_targ() );
00350         output( ion->Ncoll() );
00351         output( ion->spectator_neutrons() );
00352         output( ion->spectator_protons() );
00353         output( ion->N_Nwounded_collisions() );
00354         output( ion->Nwounded_N_collisions() );
00355         output( ion->Nwounded_Nwounded_collisions() );
00356         output( ion->impact_parameter() );
00357         output( ion->event_plane_angle() );
00358         output( ion->eccentricity() );
00359         output( ion->sigma_inel_NN() );
00360         output('\n');
00361     }
00362 
00363     void IO_ExtendedAscii::write_pdf_info( PdfInfo* pdf ) {
00364         // assumes mode has already been checked
00365         if ( !m_file ) {
00366             std::cerr << "IO_ExtendedAscii::write_pdf_info !m_file, "
00367                       << " setting badbit" << std::endl;
00368             m_file.clear(std::ios::badbit); 
00369             return;
00370         }
00371         m_file << 'F';
00372         // PdfInfo* is set to 0 by default
00373         if ( !pdf ) {
00374             output( 0 );
00375             output( 0 );
00376             output( 0. );
00377             output( 0. );
00378             output( 0. );
00379             output( 0. );
00380             output( 0. );
00381             output('\n');
00382             return;
00383         }
00384         //
00385         output( pdf->id1() );
00386         output( pdf->id2() );
00387         output( pdf->x1() );
00388         output( pdf->x2() );
00389         output( pdf->scalePDF() );
00390         output( pdf->pdf1() );
00391         output( pdf->pdf2() );
00392         output('\n');
00393     }
00394 
00395     void IO_ExtendedAscii::write_particle( GenParticle* p ) {
00396         // assumes mode has already been checked
00397         if ( !p || !m_file ) {
00398             std::cerr << "IO_ExtendedAscii::write_particle !p||!m_file, "
00399                       << "v="<< p << " setting badbit" << std::endl;
00400             m_file.clear(std::ios::badbit); 
00401             return;
00402         }
00403         m_file << 'P';
00404         output( p->barcode() );
00405         output( p->pdg_id() );
00406         output( p->momentum().px() );
00407         output( p->momentum().py() );
00408         output( p->momentum().pz() );
00409         output( p->momentum().e() );
00410         output( p->generated_mass() );
00411         output( p->status() );
00412         output( p->polarization().theta() );
00413         output( p->polarization().phi() );
00414         // since end_vertex is oftentimes null, this CREATES a null vertex
00415         // in the map
00416         output(   ( p->end_vertex() ? p->end_vertex()->barcode() : 0 )  );
00417         m_file << ' ' << p->flow() << "\n";
00418     }
00419 
00420     void IO_ExtendedAscii::write_particle_data( const ParticleData* pdata ) {
00421         // assumes mode has already been checked
00422         if ( !pdata || !m_file ) {
00423             std::cerr << "IO_ExtendedAscii::write_particle_data !pdata||!m_file, "
00424                       << "pdata="<< pdata << " setting badbit" << std::endl;
00425             m_file.clear(std::ios::badbit); 
00426             return;
00427         }
00428         m_file << 'D';
00429         output( pdata->pdg_id() );
00430         output( pdata->charge() );
00431         output( pdata->mass() );
00432         output( pdata->clifetime() );
00433         output( (int)(pdata->spin()*2.+.1) );
00434         // writes the first 21 characters starting with 0
00435         m_file << " " << pdata->name().substr(0,21) << "\n";
00436     }
00437 
00438     bool IO_ExtendedAscii::write_end_listing() {
00439         if ( m_finished_first_event_io && m_mode&std::ios::out ) {
00440             m_common_io.write_IO_ExtendedAscii_End(m_file);
00441             m_file << std::flush;
00442             m_finished_first_event_io = 0;
00443             return true;
00444         }
00445         return false;
00446     }
00447         
00448 } // HepMC

Generated on Wed Mar 12 13:08:09 2008 for HepMC by  doxygen 1.5.1-3