HepMC Reference Documentation

HepMC

CommonIO.cc

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 //
00003 // CommonIO.cc
00004 // Author:  Lynn Garren
00005 //
00006 // ----------------------------------------------------------------------
00007 
00008 #include "HepMC/CommonIO.h"
00009 #include "HepMC/GenEvent.h"
00010 #include "HepMC/HeavyIon.h"
00011 #include "HepMC/PdfInfo.h"
00012 #include "HepMC/TempParticleMap.h"
00013 #include "HepMC/ParticleDataTable.h"
00014 
00015 namespace HepMC {
00016 
00017 int CommonIO::find_file_type( std::istream& istr )
00018 {
00019     std::string line;
00020     while ( std::getline(istr,line) ) {
00021         //
00022         // search for event listing key before first event only.
00023         //
00024         if( line == m_io_genevent_start ) {
00025             m_io_type = gen;
00026             return gen;
00027         } else if( line == m_io_ascii_start ) {
00028             m_io_type = ascii;
00029             return ascii;
00030         } else if( line == m_io_extendedascii_start ) {
00031             m_io_type = extascii;
00032             return extascii;
00033         } else if( line == m_io_ascii_pdt_start ) {
00034             m_io_type = ascii_pdt;
00035             return ascii_pdt;
00036         } else if( line == m_io_extendedascii_pdt_start ) {
00037             m_io_type = extascii_pdt;
00038             return extascii_pdt;
00039         }
00040     }
00041     return -1;
00042 }
00043 
00044 int CommonIO::find_end_key( std::istream& istr )
00045 {
00046     // peek at the first character before proceeding
00047     if( istr.peek()!='H' ) return false;
00048     //
00049     // we only check the next line
00050     std::string line;
00051     std::getline(istr,line);
00052     //
00053     // check to see if this is an end key
00054     int iotype = 0;
00055     if( line == m_io_genevent_end ) {
00056         m_io_type = gen;
00057     } else if( line == m_io_ascii_end ) {
00058         m_io_type = ascii;
00059     } else if( line == m_io_extendedascii_end ) {
00060         m_io_type = extascii;
00061     } else if( line == m_io_ascii_pdt_end ) {
00062         m_io_type = ascii_pdt;
00063     } else if( line == m_io_extendedascii_pdt_end ) {
00064         m_io_type = extascii_pdt;
00065     }
00066     if( iotype != 0 && m_io_type != iotype ) {
00067         std::cerr << "CommonIO::find_end_key: iotype keys have changed" << std::endl;
00068     } else {
00069         return iotype;
00070     }
00071     //
00072     // if we get here, then something has gotten badly confused
00073     std::cerr << "CommonIO::find_end_key: MALFORMED INPUT" << std::endl;
00074     return -1;
00075 }
00076 
00077 bool CommonIO::read_io_ascii( std::istream* istr, GenEvent* evt )
00078 {
00079         // read values into temp variables, then create a new GenEvent
00080         int event_number = 0, signal_process_id = 0, signal_process_vertex = 0,
00081             num_vertices = 0, random_states_size = 0, weights_size = 0;
00082         double eventScale = 0, alpha_qcd = 0, alpha_qed = 0;
00083         *istr >> event_number >> eventScale >> alpha_qcd >> alpha_qed
00084                >> signal_process_id >> signal_process_vertex
00085                >> num_vertices >> random_states_size;
00086         std::vector<long> random_states(random_states_size);
00087         for ( int i = 0; i < random_states_size; ++i ) {
00088             *istr >> random_states[i];
00089         }
00090         *istr >> weights_size;
00091         WeightContainer weights(weights_size);
00092         for ( int ii = 0; ii < weights_size; ++ii ) *istr >> weights[ii];
00093         istr->ignore(2,'\n');
00094         // 
00095         // fill signal_process_id, event_number, weights, random_states
00096         evt->set_signal_process_id( signal_process_id );
00097         evt->set_event_number( event_number );
00098         evt->weights() = weights;
00099         evt->set_random_states( random_states );
00100         //
00101         // the end vertices of the particles are not connected until
00102         //  after the event is read --- we store the values in a map until then
00103         TempParticleMap particle_to_end_vertex;
00104         //
00105         // read in the vertices
00106         for ( int iii = 1; iii <= num_vertices; ++iii ) {
00107             GenVertex* v = read_vertex(istr,particle_to_end_vertex);
00108             evt->add_vertex( v );
00109         }
00110         // set the signal process vertex
00111         if ( signal_process_vertex ) {
00112             evt->set_signal_process_vertex( 
00113                 evt->barcode_to_vertex(signal_process_vertex) );
00114         }
00115         //
00116         // last connect particles to their end vertices
00117         for ( std::map<int,GenParticle*>::iterator pmap 
00118                   = particle_to_end_vertex.order_begin(); 
00119               pmap != particle_to_end_vertex.order_end(); ++pmap ) {
00120             GenParticle* p =  pmap->second;
00121             int vtx = particle_to_end_vertex.end_vertex( p );
00122             GenVertex* itsDecayVtx = evt->barcode_to_vertex(vtx);
00123             if ( itsDecayVtx ) itsDecayVtx->add_particle_in( p );
00124             else {
00125                 std::cerr << "IO_Ascii::fill_next_event ERROR particle points"
00126                           << "\n to null end vertex. " <<std::endl;
00127             }
00128         }
00129         return true;
00130 }
00131 
00132 bool CommonIO::read_io_extendedascii( std::istream* istr, GenEvent* evt )
00133 {
00134         // read values into temp variables, then create a new GenEvent
00135         int event_number = 0, signal_process_id = 0, signal_process_vertex = 0,
00136             num_vertices = 0, random_states_size = 0, weights_size = 0,
00137             nmpi = 0, bp1 = 0, bp2 = 0;
00138         double eventScale = 0, alpha_qcd = 0, alpha_qed = 0;
00139         *istr >> event_number >> nmpi >> eventScale >> alpha_qcd >> alpha_qed
00140                >> signal_process_id >> signal_process_vertex
00141                >> num_vertices >> bp1 >> bp2 >> random_states_size;
00142         std::vector<long> random_states(random_states_size);
00143         for ( int i = 0; i < random_states_size; ++i ) {
00144             *istr >> random_states[i];
00145         }
00146         *istr >> weights_size;
00147         WeightContainer weights(weights_size);
00148         for ( int ii = 0; ii < weights_size; ++ii ) *istr >> weights[ii];
00149         istr->ignore(2,'\n');
00150         // 
00151         // fill signal_process_id, event_number, weights, random_states
00152         evt->set_signal_process_id( signal_process_id );
00153         evt->set_event_number( event_number );
00154         evt->set_mpi( nmpi );
00155         evt->weights() = weights;
00156         evt->set_random_states( random_states );
00157         evt->set_event_scale( eventScale );
00158         evt->set_alphaQCD( alpha_qcd );
00159         evt->set_alphaQED( alpha_qed );
00160         // get HeavyIon and PdfInfo
00161         HeavyIon* ion = read_heavy_ion(istr);
00162         if(ion) evt->set_heavy_ion( *ion );
00163         PdfInfo* pdf = read_pdf_info(istr);
00164         if(pdf) evt->set_pdf_info( *pdf );
00165         //
00166         // the end vertices of the particles are not connected until
00167         //  after the event is read --- we store the values in a map until then
00168         TempParticleMap particle_to_end_vertex;
00169         //
00170         // read in the vertices
00171         for ( int iii = 1; iii <= num_vertices; ++iii ) {
00172             GenVertex* v = read_vertex(istr,particle_to_end_vertex);
00173             evt->add_vertex( v );
00174         }
00175         // set the signal process vertex
00176         if ( signal_process_vertex ) {
00177             evt->set_signal_process_vertex( 
00178                 evt->barcode_to_vertex(signal_process_vertex) );
00179         }
00180         //
00181         // last connect particles to their end vertices
00182         GenParticle* beam1(0);
00183         GenParticle* beam2(0);
00184         for ( std::map<int,GenParticle*>::iterator pmap 
00185                   = particle_to_end_vertex.order_begin(); 
00186               pmap != particle_to_end_vertex.order_end(); ++pmap ) {
00187             GenParticle* p =  pmap->second;
00188             int vtx = particle_to_end_vertex.end_vertex( p );
00189             GenVertex* itsDecayVtx = evt->barcode_to_vertex(vtx);
00190             if ( itsDecayVtx ) itsDecayVtx->add_particle_in( p );
00191             else {
00192                 std::cerr << "IO_ExtendedAscii::fill_next_event ERROR particle points"
00193                           << "\n to null end vertex. " <<std::endl;
00194             }
00195             // also look for the beam particles
00196             if( p->barcode() == bp1 ) beam1 = p;
00197             if( p->barcode() == bp2 ) beam2 = p;
00198         }
00199         evt->set_beam_particles(beam1,beam2);
00200         return true;
00201 }
00202 
00203 bool CommonIO::read_io_genevent( std::istream* is, GenEvent* evt )
00204 {
00206     //
00207     // assume that we've been handed a valid stream and event pointer
00208     //
00209     // read values into temp variables, then fill GenEvent
00210     int event_number = 0, signal_process_id = 0, signal_process_vertex = 0,
00211         num_vertices = 0, random_states_size = 0, weights_size = 0,
00212         nmpi = 0, bp1 = 0, bp2 = 0;
00213     double eventScale = 0, alpha_qcd = 0, alpha_qed = 0;
00214     *is >> event_number >> nmpi >> eventScale >> alpha_qcd >> alpha_qed
00215            >> signal_process_id >> signal_process_vertex
00216            >> num_vertices >> bp1 >> bp2 >> random_states_size;
00217     std::vector<long> random_states(random_states_size);
00218     for ( int i = 0; i < random_states_size; ++i ) {
00219         *is >> random_states[i];
00220     }
00221     *is >> weights_size;
00222     WeightContainer weights(weights_size);
00223     for ( int ii = 0; ii < weights_size; ++ii ) *is >> weights[ii];
00224     is->ignore(2,'\n');
00225     // 
00226     // fill signal_process_id, event_number, weights, random_states, etc.
00227     evt->set_signal_process_id( signal_process_id );
00228     evt->set_event_number( event_number );
00229     evt->set_mpi( nmpi );
00230     evt->weights() = weights;
00231     evt->set_random_states( random_states );
00232     evt->set_event_scale( eventScale );
00233     evt->set_alphaQCD( alpha_qcd );
00234     evt->set_alphaQED( alpha_qed );
00235     // get HeavyIon and PdfInfo
00236     HeavyIon* ion = read_heavy_ion(is);
00237     if(ion) evt->set_heavy_ion( *ion );
00238     PdfInfo* pdf = read_pdf_info(is);
00239     if(pdf) evt->set_pdf_info( *pdf );
00240     //
00241     // the end vertices of the particles are not connected until
00242     //  after the event is read --- we store the values in a map until then
00243     TempParticleMap particle_to_end_vertex;
00244     //
00245     // read in the vertices
00246     for ( int iii = 1; iii <= num_vertices; ++iii ) {
00247         GenVertex* v = read_vertex(is,particle_to_end_vertex);
00248         evt->add_vertex( v );
00249     }
00250     // set the signal process vertex
00251     if ( signal_process_vertex ) {
00252         evt->set_signal_process_vertex( 
00253             evt->barcode_to_vertex(signal_process_vertex) );
00254     }
00255     //
00256     // last connect particles to their end vertices
00257     GenParticle* beam1(0);
00258     GenParticle* beam2(0);
00259     for ( std::map<int,GenParticle*>::iterator pmap 
00260               = particle_to_end_vertex.order_begin(); 
00261           pmap != particle_to_end_vertex.order_end(); ++pmap ) {
00262         GenParticle* p =  pmap->second;
00263         int vtx = particle_to_end_vertex.end_vertex( p );
00264         GenVertex* itsDecayVtx = evt->barcode_to_vertex(vtx);
00265         if ( itsDecayVtx ) itsDecayVtx->add_particle_in( p );
00266         else {
00267             std::cerr << "read_io_genevent: ERROR particle points"
00268                       << " to null end vertex. " <<std::endl;
00269         }
00270         // also look for the beam particles
00271         if( p->barcode() == bp1 ) beam1 = p;
00272         if( p->barcode() == bp2 ) beam2 = p;
00273     }
00274     evt->set_beam_particles(beam1,beam2);
00275     return true;
00276 }
00277 
00278 bool CommonIO::read_io_particle_data_table( std::istream* is, ParticleDataTable* pdt )
00279 {
00280     // 
00281     // read Individual GenParticle data entries
00282     while ( read_particle_data( is, pdt ) );
00283     return true;
00284 }
00285 
00286 HeavyIon* CommonIO::read_heavy_ion(std::istream* is)
00287 {
00288     // assumes mode has already been checked
00289     //
00290     // test to be sure the next entry is of type "H" then ignore it
00291     if ( !(*is) || is->peek()!='H' ) {
00292         std::cerr << "CommonIO::read_heavy_ion setting badbit." << std::endl;
00293         is->clear(std::ios::badbit); 
00294         return false;
00295     } 
00296     is->ignore();
00297     // read values into temp variables, then create a new HeavyIon object
00298     int nh =0, np =0, nt =0, nc =0, 
00299         neut = 0, prot = 0, nw =0, nwn =0, nwnw =0;
00300     float impact = 0., plane = 0., xcen = 0., inel = 0.; 
00301     *is >> nh >> np >> nt >> nc >> neut >> prot
00302            >> nw >> nwn >> nwnw >> impact >> plane >> xcen >> inel;
00303     is->ignore(2,'\n');
00304     if( nh == 0 ) return false;
00305     HeavyIon* ion = new HeavyIon(nh, np, nt, nc, neut, prot,
00306                                  nw, nwn, nwnw, 
00307                                  impact, plane, xcen, inel );
00308     //
00309     return ion;
00310 }
00311 
00312 PdfInfo* CommonIO::read_pdf_info(std::istream* is)
00313 {
00314     // assumes mode has already been checked
00315     //
00316     // test to be sure the next entry is of type "F" then ignore it
00317     if ( !(*is) || is->peek() !='F') {
00318         std::cerr << "CommonIO::read_pdf_info setting badbit." << std::endl;
00319         is->clear(std::ios::badbit); 
00320         return false;
00321     } 
00322     is->ignore();
00323     // read values into temp variables, then create a new PdfInfo object
00324     int id1 =0, id2 =0;
00325     double  x1 = 0., x2 = 0., scale = 0., pdf1 = 0., pdf2 = 0.; 
00326     *is >> id1 >> id2 >> x1 >> x2 >> scale >> pdf1 >> pdf2;
00327     is->ignore(2,'\n');
00328     if( id1 == 0 ) return false;
00329     PdfInfo* pdf = new PdfInfo( id1, id2, x1, x2, scale, pdf1, pdf2);
00330     //
00331     return pdf;
00332 }
00333 
00334 GenVertex* CommonIO::read_vertex( std::istream* is, TempParticleMap& particle_to_end_vertex )
00335 {
00336     // assumes mode has already been checked
00337     //
00338     // test to be sure the next entry is of type "V" then ignore it
00339     if ( !(*is) || is->peek()!='V' ) {
00340         std::cerr << "CommonIO::read_vertex setting badbit." << std::endl;
00341         is->clear(std::ios::badbit); 
00342         return false;
00343     } 
00344     is->ignore();
00345     // read values into temp variables, then create a new GenVertex object
00346     int identifier =0, id =0, num_orphans_in =0, 
00347         num_particles_out = 0, weights_size = 0;
00348     double x = 0., y = 0., z = 0., t = 0.; 
00349     *is >> identifier >> id >> x >> y >> z >> t
00350            >> num_orphans_in >> num_particles_out >> weights_size;
00351     WeightContainer weights(weights_size);
00352     for ( int i1 = 0; i1 < weights_size; ++i1 ) *is >> weights[i1];
00353     is->ignore(2,'\n');
00354     GenVertex* v = new GenVertex( FourVector(x,y,z,t),
00355                             id, weights);
00356     v->suggest_barcode( identifier );
00357     //
00358     // read and create the associated particles. outgoing particles are
00359     //  added to their production vertices immediately, while incoming
00360     //  particles are added to a map and handles later.
00361     for ( int i2 = 1; i2 <= num_orphans_in; ++i2 ) {
00362         read_particle(is,particle_to_end_vertex);
00363     }
00364     for ( int i3 = 1; i3 <= num_particles_out; ++i3 ) {
00365         v->add_particle_out( read_particle(is,particle_to_end_vertex) );
00366     }
00367     return v;
00368 }
00369 
00370 GenParticle* CommonIO::read_particle(std::istream* is,
00371     TempParticleMap& particle_to_end_vertex ){
00372     // assumes mode has already been checked
00373     //
00374     // test to be sure the next entry is of type "P" then ignore it
00375     if ( !(*is) || is->peek()!='P' ) { 
00376         std::cerr << "CommonIO::read_particle setting badbit." 
00377                   << std::endl;
00378         is->clear(std::ios::badbit); 
00379         return false;
00380     } 
00381     is->ignore();
00382     //
00383     // declare variables to be read in to, and read everything except flow
00384     double px = 0., py = 0., pz = 0., e = 0., m = 0., theta = 0., phi = 0.;
00385     int bar_code = 0, id = 0, status = 0, end_vtx_code = 0, flow_size = 0;
00386     if( m_io_type == ascii ) {
00387         *is >> bar_code >> id >> px >> py >> pz >> e >> status 
00388             >> theta >> phi >> end_vtx_code >> flow_size;
00389     } else {
00390         *is >> bar_code >> id >> px >> py >> pz >> e >> m >> status 
00391             >> theta >> phi >> end_vtx_code >> flow_size;
00392     }
00393     //
00394     // read flow patterns if any exist
00395     Flow flow;
00396     int code_index, code;
00397     for ( int i = 1; i <= flow_size; ++i ) {
00398         *is >> code_index >> code;
00399         flow.set_icode( code_index,code);
00400     }
00401     is->ignore(2,'\n'); // '\n' at end of entry
00402     GenParticle* p = new GenParticle( FourVector(px,py,pz,e), 
00403                                 id, status, flow, 
00404                                 Polarization(theta,phi) );
00405     if( m_io_type != ascii ) {
00406         p->set_generated_mass( m );
00407     }
00408     p->suggest_barcode( bar_code );
00409     //
00410     // all particles are connected to their end vertex separately 
00411     // after all particles and vertices have been created - so we keep
00412     // a map of all particles that have end vertices
00413     if ( end_vtx_code != 0 ) {
00414         particle_to_end_vertex.addEndParticle(p,end_vtx_code);
00415     }
00416     return p;
00417 }
00418 
00419 ParticleData* CommonIO::read_particle_data( std::istream* is, ParticleDataTable* pdt ) {
00420     // assumes mode has already been checked
00421     //
00422     // test to be sure the next entry is of type "D" then ignore it
00423     if ( !(*is) || is->peek()!='D' ) return 0;
00424     is->ignore();
00425     //
00426     // read values into temp variables then create new ParticleData object
00427     char its_name[22];
00428     int its_id = 0, its_spin = 0;  
00429     double its_charge = 0, its_mass = 0, its_clifetime = 0;
00430     *is >> its_id >> its_charge >> its_mass 
00431            >> its_clifetime >> its_spin;
00432     is->ignore(1); // eat the " "
00433     is->getline( its_name, 22, '\n' );
00434     ParticleData* pdata = new ParticleData( its_name, its_id, its_charge, 
00435                                             its_mass, its_clifetime, 
00436                                             double(its_spin)/2.);
00437     pdt->insert(pdata);
00438     return pdata;
00439 }
00440 
00441 }       // end namespace HepMC
00442 

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