StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StreamHelpers.cc
1 //--------------------------------------------------------------------------
2 //
3 // GenEventStreamIO.cc
4 // Author: Lynn Garren
5 //
6 // helper functions used by streaming IO
7 //
8 // ----------------------------------------------------------------------
9 
10 #include <ostream>
11 #include <istream>
12 #include <sstream>
13 
14 #include "HepMC/GenVertex.h"
15 #include "HepMC/GenParticle.h"
16 #include "HepMC/StreamHelpers.h"
17 #include "HepMC/IO_Exception.h"
18 
19 namespace HepMC {
20 
21 namespace detail {
22 
23 std::istream & read_vertex( std::istream & is,
24  TempParticleMap & particle_to_end_vertex,
25  GenVertex * v )
26 {
27  //
28  // make sure the stream is valid
29  if ( !is ) {
30  std::cerr << "StreamHelpers::detail::read_vertex setting badbit." << std::endl;
31  is.clear(std::ios::badbit);
32  return is;
33  }
34  //
35  // get the vertex line
36  std::string line;
37  std::getline(is,line);
38  std::istringstream iline(line);
39  std::string firstc;
40  iline >> firstc;
41  //
42  // test to be sure the next entry is of type "V"
43  if ( firstc != "V" ) {
44  std::cerr << "StreamHelpers::detail::read_vertex invalid line type: "
45  << firstc << std::endl;
46  std::cerr << "StreamHelpers::detail::read_vertex setting badbit." << std::endl;
47  is.clear(std::ios::badbit);
48  return is;
49  }
50  // read values into temp variables, then create a new GenVertex object
51  int identifier =0, id =0, num_orphans_in =0,
52  num_particles_out = 0, weights_size = 0;
53  double x = 0., y = 0., z = 0., t = 0.;
54  iline >> identifier ;
55  if(!iline) { throw IO_Exception("read_vertex input stream encounterd invalid data"); }
56  iline >> id ;
57  if(!iline) { throw IO_Exception("read_vertex input stream encounterd invalid data"); }
58  iline >> x ;
59  if(!iline) { throw IO_Exception("read_vertex input stream encounterd invalid data"); }
60  iline >> y ;
61  if(!iline) { throw IO_Exception("read_vertex input stream encounterd invalid data"); }
62  iline >> z ;
63  if(!iline) { throw IO_Exception("read_vertex input stream encounterd invalid data"); }
64  iline >> t;
65  if(!iline) { throw IO_Exception("read_vertex input stream encounterd invalid data"); }
66  iline >> num_orphans_in ;
67  if(!iline) { throw IO_Exception("read_vertex input stream encounterd invalid data"); }
68  iline >> num_particles_out ;
69  if(!iline) { throw IO_Exception("read_vertex input stream encounterd invalid data"); }
70  iline >> weights_size;
71  if(!iline) { throw IO_Exception("read_vertex input stream encounterd invalid data"); }
72  WeightContainer weights(weights_size);
73  for ( int i1 = 0; i1 < weights_size; ++i1 ) {
74  iline >> weights[i1];
75  if(!iline) { throw IO_Exception("read_vertex input stream encounterd invalid data"); }
76  }
77  v->set_position( FourVector(x,y,z,t) );
78  v->set_id( id );
79  v->weights() = weights;
80  v->suggest_barcode( identifier );
81  //
82  // read and create the associated particles. outgoing particles are
83  // added to their production vertices immediately, while incoming
84  // particles are added to a map and handled later.
85  for ( int i2 = 1; i2 <= num_orphans_in; ++i2 ) {
86  GenParticle* p1 = new GenParticle( );
87  detail::read_particle(is,particle_to_end_vertex,p1);
88  }
89  for ( int i3 = 1; i3 <= num_particles_out; ++i3 ) {
90  GenParticle* p2 = new GenParticle( );
91  detail::read_particle(is,particle_to_end_vertex,p2);
92  v->add_particle_out( p2 );
93  }
94 
95  return is;
96 }
97 
98 std::istream & find_event_end( std::istream & is ) {
99  // since there is no end of event flag,
100  // read one line at time until we find the next event
101  // or the end of event block
102  // don't throw until we find the end of the event
103  std::string line, firstc;
104  while ( is ) {
105  is >> firstc;
106  if( firstc=="E" ) { // next event
107  is.unget();
108  throw IO_Exception("input stream encountered invalid data");
109  return is;
110  } else if( firstc.size() > 1 ) { // no more events in this block
111  throw IO_Exception("input stream encountered invalid data, now at end of event block");
112  return is;
113  }
114  std::getline(is,line);
115  }
116  // the stream is bad
117  throw IO_Exception("input stream encountered invalid data, stream is now corrupt");
118  return is;
119 }
120 
121 } // detail
122 
123 } // HepMC