![]() |
HepMC Reference DocumentationHepMC |
00001 //-------------------------------------------------------------------------- 00002 // 00003 // GenEventStreamIO.cc 00004 // Author: Lynn Garren 00005 // 00006 // helper functions used by streaming IO 00007 // 00008 // ---------------------------------------------------------------------- 00009 00010 #include <ostream> 00011 #include <istream> 00012 #include <sstream> 00013 00014 #include "HepMC/GenVertex.h" 00015 #include "HepMC/GenParticle.h" 00016 #include "HepMC/StreamHelpers.h" 00017 #include "HepMC/IO_Exception.h" 00018 00019 namespace HepMC { 00020 00021 namespace detail { 00022 00023 std::istream & read_vertex( std::istream & is, 00024 TempParticleMap & particle_to_end_vertex, 00025 GenVertex * v ) 00026 { 00027 // 00028 // make sure the stream is valid 00029 if ( !is ) { 00030 std::cerr << "StreamHelpers::detail::read_vertex setting badbit." << std::endl; 00031 is.clear(std::ios::badbit); 00032 return is; 00033 } 00034 // 00035 // get the vertex line 00036 std::string line; 00037 std::getline(is,line); 00038 std::istringstream iline(line); 00039 std::string firstc; 00040 iline >> firstc; 00041 // 00042 // test to be sure the next entry is of type "V" 00043 if ( firstc != "V" ) { 00044 std::cerr << "StreamHelpers::detail::read_vertex invalid line type: " 00045 << firstc << std::endl; 00046 std::cerr << "StreamHelpers::detail::read_vertex setting badbit." << std::endl; 00047 is.clear(std::ios::badbit); 00048 return is; 00049 } 00050 // read values into temp variables, then create a new GenVertex object 00051 int identifier =0, id =0, num_orphans_in =0, 00052 num_particles_out = 0, weights_size = 0; 00053 double x = 0., y = 0., z = 0., t = 0.; 00054 iline >> identifier ; 00055 if(!iline) detail::find_event_end( is ); 00056 iline >> id ; 00057 if(!iline) detail::find_event_end( is ); 00058 iline >> x ; 00059 if(!iline) detail::find_event_end( is ); 00060 iline >> y ; 00061 if(!iline) detail::find_event_end( is ); 00062 iline >> z ; 00063 if(!iline) detail::find_event_end( is ); 00064 iline >> t; 00065 if(!iline) detail::find_event_end( is ); 00066 iline >> num_orphans_in ; 00067 if(!iline) detail::find_event_end( is ); 00068 iline >> num_particles_out ; 00069 if(!iline) detail::find_event_end( is ); 00070 iline >> weights_size; 00071 if(!iline) detail::find_event_end( is ); 00072 WeightContainer weights(weights_size); 00073 for ( int i1 = 0; i1 < weights_size; ++i1 ) { 00074 iline >> weights[i1]; 00075 if(!iline) detail::find_event_end( is ); 00076 } 00077 v->set_position( FourVector(x,y,z,t) ); 00078 v->set_id( id ); 00079 v->weights() = weights; 00080 v->suggest_barcode( identifier ); 00081 // 00082 // read and create the associated particles. outgoing particles are 00083 // added to their production vertices immediately, while incoming 00084 // particles are added to a map and handled later. 00085 for ( int i2 = 1; i2 <= num_orphans_in; ++i2 ) { 00086 GenParticle* p1 = new GenParticle( ); 00087 detail::read_particle(is,particle_to_end_vertex,p1); 00088 } 00089 for ( int i3 = 1; i3 <= num_particles_out; ++i3 ) { 00090 GenParticle* p2 = new GenParticle( ); 00091 detail::read_particle(is,particle_to_end_vertex,p2); 00092 v->add_particle_out( p2 ); 00093 } 00094 00095 return is; 00096 } 00097 00098 std::istream & find_event_end( std::istream & is ) { 00099 // since there is no end of event flag, 00100 // read one line at time until we find the next event 00101 // or the end of event block 00102 // don't throw until we find the end of the event 00103 std::string line, firstc; 00104 while ( is ) { 00105 is >> firstc; 00106 if( firstc=="E" ) { // next event 00107 is.unget(); 00108 throw IO_Exception("input stream encountered invalid data"); 00109 return is; 00110 } else if( firstc.size() > 1 ) { // no more events in this block 00111 throw IO_Exception("input stream encountered invalid data, now at end of event block"); 00112 return is; 00113 } 00114 std::getline(is,line); 00115 } 00116 // the stream is bad 00117 throw IO_Exception("input stream encountered invalid data, stream is now corrupt"); 00118 return is; 00119 } 00120 00121 } // detail 00122 00123 } // HepMC