15 #include "HepMC/GenEvent.h"
16 #include "HepMC/GenCrossSection.h"
17 #include "HepMC/StreamInfo.h"
18 #include "HepMC/StreamHelpers.h"
19 #include "HepMC/Version.h"
20 #include "HepMC/IO_Exception.h"
32 if(i!=0 && e!= std::ios_base::erase_event)
return;
40 if(hd) std::cerr <<
"deleted StreamInfo " << hd->
stream_id() <<
"\n";
53 if(iost.iword(0) == 0)
64 std::cerr <<
"created StreamInfo " << ((
StreamInfo*)iost.pword(0))->stream_id() <<
"\n";
85 os.setf(std::ios::dec,std::ios::basefield);
86 os.setf(std::ios::scientific,std::ios::floatfield);
96 detail::output( os,
mpi() );
106 detail::output( os, (
int)m_random_states.size() );
107 for ( std::vector<long>::iterator rs = m_random_states.begin();
108 rs != m_random_states.end(); ++rs ) {
109 detail::output( os, *rs );
115 for ( WeightContainer::const_map_iterator w =
weights().map_begin();
116 w !=
weights().map_end(); ++w ) {
117 detail::output( os, m_weights[w->second] );
119 detail::output( os,
'\n');
125 for ( WeightContainer::const_map_iterator w =
weights().map_begin();
126 w !=
weights().map_end(); ++w ) {
127 detail::output( os,
'"');
129 detail::output( os,
'"');
130 detail::output( os,
' ');
132 detail::output( os,
'\n');
138 detail::output( os,
'\n');
141 if( m_cross_section ) m_cross_section->
write(os);
150 write_vertex(os, *v);
171 std::cerr <<
"streaming input: end of stream found "
172 <<
"setting badbit." << std::endl;
173 is.clear(std::ios::badbit);
179 if ( is.peek()!=
'E' ) {
183 find_end_key(is,ioendtype);
184 if ( ioendtype == info.
io_type() ) {
188 }
else if ( ioendtype > 0 ) {
189 std::cerr <<
"streaming input: end key does not match start key "
190 <<
"setting badbit." << std::endl;
191 is.clear(std::ios::badbit);
193 }
else if ( !info.
has_key() ) {
198 std::cerr <<
"streaming input: end key not found "
199 <<
"setting badbit." << std::endl;
200 is.clear(std::ios::badbit);
206 int num_vertices = 0, bp1 = 0, bp2 = 0;
207 bool units_line =
false;
215 process_event_line( is, num_vertices, bp1, bp2, signal_process_vertex );
219 read_weight_names( is );
238 detail::find_event_end( is );
254 detail::find_event_end( is );
271 detail::find_event_end( is );
285 std::cerr <<
"streaming input: found unexpected line P" << std::endl;
304 for (
int iii = 1; iii <= num_vertices; ++iii ) {
307 detail::read_vertex(is,particle_to_end_vertex,v);
310 for( TempParticleMap::orderIterator it = particle_to_end_vertex.order_begin();
311 it != particle_to_end_vertex.order_end(); ++it ) {
321 detail::find_event_end( is );
326 if ( signal_process_vertex ) {
334 for ( TempParticleMap::orderIterator pmap
335 = particle_to_end_vertex.order_begin();
336 pmap != particle_to_end_vertex.order_end(); ++pmap ) {
338 int vtx = particle_to_end_vertex.end_vertex( p );
342 std::cerr <<
"read_io_genevent: ERROR particle points"
343 <<
" to null end vertex. " <<std::endl;
346 if( p->
barcode() == bp1 ) beam1 = p;
347 if( p->
barcode() == bp2 ) beam2 = p;
355 std::ostream & operator << (std::ostream & os,
GenEvent & evt)
362 std::istream & operator >> (std::istream & is,
GenEvent & evt)
371 Units::MomentumUnit mom,
372 Units::LengthUnit len )
407 std::istream & GenEvent::process_event_line( std::istream & is,
409 int & bp1,
int & bp2,
410 int & signal_process_vertex )
414 std::cerr <<
"GenEvent::process_event_line setting badbit." << std::endl;
415 is.clear(std::ios::badbit);
421 std::getline(is,line);
422 std::istringstream iline(line);
428 random_states_size = 0, nmpi = -1;
429 double eventScale = 0, alpha_qcd = 0, alpha_qed = 0;
431 if(!iline) detail::find_event_end( is );
432 if( info.io_type() == gen || info.io_type() == extascii ) {
434 if(!iline) detail::find_event_end( is );
437 iline >> eventScale ;
438 if(!iline) detail::find_event_end( is );
440 if(!iline) detail::find_event_end( is );
442 if(!iline) detail::find_event_end( is );
444 if(!iline) detail::find_event_end( is );
446 if(!iline) detail::find_event_end( is );
447 iline >> num_vertices;
448 if(!iline) detail::find_event_end( is );
449 if( info.io_type() == gen || info.io_type() == extascii ) {
451 if(!iline) detail::find_event_end( is );
453 if(!iline) detail::find_event_end( is );
455 iline >> random_states_size;
456 if(!iline) detail::find_event_end( is );
458 for (
int i = 0; i < random_states_size; ++i ) {
460 if(!iline) detail::find_event_end( is );
463 iline >> weights_size;
464 if(!iline) detail::find_event_end( is );
465 std::vector<double> wgt(weights_size);
468 if(!iline) detail::find_event_end( is );
471 if( weights_size > 0 ) m_weights = wgt;
484 std::istream & GenEvent::read_weight_names( std::istream & is )
488 std::cerr <<
"GenEvent::read_weight_names setting badbit." << std::endl;
489 is.clear(std::ios::badbit);
495 if ( is.peek() !=
'N') {
500 std::getline(is,line);
501 std::istringstream wline(line);
504 wline >> firstc >> name_size;
505 if(!wline) detail::find_event_end( is );
507 std::cout <<
"debug: first character of named weights is " << firstc << std::endl;
508 std::cout <<
"debug: We should never get here" << std::endl;
509 is.clear(std::ios::badbit);
512 if( m_weights.
size() != name_size ) {
513 std::cout <<
"debug: weight sizes do not match "<< std::endl;
514 std::cout <<
"debug: weight vector size is " << m_weights.
size() << std::endl;
515 std::cout <<
"debug: weight name size is " << name_size << std::endl;
516 is.clear(std::ios::badbit);
520 std::string::size_type i1 = line.find(
"\"");
521 std::string::size_type i2;
522 std::string::size_type len = line.size();
523 WeightContainer namedWeight;
527 std::cout <<
"debug: attempting to read past the end of the named weight line " << std::endl;
528 std::cout <<
"debug: We should never get here" << std::endl;
529 std::cout <<
"debug: Looking for the end of this event" << std::endl;
530 detail::find_event_end( is );
532 i2 = line.find(
"\"",i1+1);
533 name = line.substr(i1+1,i2-i1-1);
534 namedWeight[name] = m_weights[ii];
535 i1 = line.find(
"\"",i2+1);
537 m_weights = namedWeight;
541 std::istream & GenEvent::read_units( std::istream & is )
545 std::cerr <<
"GenEvent::read_units setting badbit." << std::endl;
546 is.
clear(std::ios::badbit);
554 if ( is.peek() !=
'U') {
556 info.io_position_unit() );
560 std::string mom, pos;
568 std::istream & GenEvent::find_file_type( std::istream & istr )
572 if ( !istr )
return istr;
579 if ( istr.peek()==
'E' ) {
580 info.set_io_type( gen );
581 info.set_has_key(
false);
586 while ( std::getline(istr,line) ) {
590 if( line == info.IO_GenEvent_Key() ) {
591 info.set_io_type( gen );
592 info.set_has_key(
true);
594 }
else if( line == info.IO_Ascii_Key() ) {
595 info.set_io_type( ascii );
596 info.set_has_key(
true);
598 }
else if( line == info.IO_ExtendedAscii_Key() ) {
599 info.set_io_type( extascii );
600 info.set_has_key(
true);
602 }
else if( line == info.IO_Ascii_PDT_Key() ) {
603 info.set_io_type( ascii_pdt );
604 info.set_has_key(
true);
606 }
else if( line == info.IO_ExtendedAscii_PDT_Key() ) {
607 info.set_io_type( extascii_pdt );
608 info.set_has_key(
true);
612 info.set_io_type( 0 );
613 info.set_has_key(
false);
617 std::istream & GenEvent::find_end_key( std::istream & istr,
int & iotype )
621 if( istr.peek()!=
'H' )
return istr;
625 std::getline(istr,line);
630 if( line == info.IO_GenEvent_End() ) {
632 }
else if( line == info.IO_Ascii_End() ) {
634 }
else if( line == info.IO_ExtendedAscii_End() ) {
636 }
else if( line == info.IO_Ascii_PDT_End() ) {
638 }
else if( line == info.IO_ExtendedAscii_PDT_End() ) {
639 iotype = extascii_pdt;
641 if( iotype != 0 && info.io_type() != iotype ) {
642 std::cerr <<
"GenEvent::find_end_key: iotype keys have changed" << std::endl;
648 std::cerr <<
"GenEvent::find_end_key: MALFORMED INPUT" << std::endl;
649 istr.clear(std::ios::badbit);
653 std::ostream & establish_output_stream_info( std::ostream & os )
656 if ( !info.finished_first_event() ) {
661 os.setf(std::ios::dec,std::ios::basefield);
662 os.setf(std::ios::scientific,std::ios::floatfield);
667 std::istream & establish_input_stream_info( std::istream & is )
670 if ( !info.finished_first_event() ) {
675 is.setf(std::ios::dec,std::ios::basefield);
676 is.setf(std::ios::scientific,std::ios::floatfield);
688 std::istream & read_particle( std::istream & is,
689 TempParticleMap & particle_to_end_vertex,
694 std::getline(is,line);
695 std::istringstream iline(line);
698 if( firstc !=
"P" ) {
699 std::cerr <<
"StreamHelpers::detail::read_particle invalid line type: "
700 << firstc << std::endl;
701 std::cerr <<
"StreamHelpers::detail::read_particle setting badbit."
703 is.clear(std::ios::badbit);
710 double px = 0., py = 0., pz = 0., e = 0., m = 0., theta = 0., phi = 0.;
711 int bar_code = 0,
id = 0, status = 0, end_vtx_code = 0, flow_size = 0;
714 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
716 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
718 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
720 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
722 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
724 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
725 if( info.io_type() != ascii ) {
727 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
730 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
732 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
734 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
735 iline >> end_vtx_code ;
736 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
738 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
742 int code_index, code;
743 for (
int i = 1; i <= flow_size; ++i ) {
744 iline >> code_index >> code;
745 if(!iline) {
delete p;
throw IO_Exception(
"read_particle input stream encounterd invalid data"); }
746 flow.set_icode( code_index,code);
748 p->set_momentum( FourVector(px,py,pz,e) );
750 p->set_status( status );
752 p->set_polarization( Polarization(theta,phi) );
753 if( info.io_type() == ascii ) {
754 p->set_generated_mass( p->momentum().m() );
756 p->set_generated_mass( m );
758 p->suggest_barcode( bar_code );
763 if ( end_vtx_code != 0 ) {
764 particle_to_end_vertex.addEndParticle(p,end_vtx_code);
769 std::ostream & establish_output_stream_info( std::ostream & os )
772 if ( !info.finished_first_event() ) {
777 os.setf(std::ios::dec,std::ios::basefield);
778 os.setf(std::ios::scientific,std::ios::floatfield);
783 std::istream & establish_input_stream_info( std::istream & is )
786 if ( !info.finished_first_event() ) {
791 is.setf(std::ios::dec,std::ios::basefield);
792 is.setf(std::ios::scientific,std::ios::floatfield);
std::string IO_GenEvent_End() const
IO_GenEvent end event block key.
bool finished_first_event() const
Special information is processed the first time we use the IO.
void set_pdf_info(const PdfInfo &p)
provide a pointer to the PdfInfo container
GenVertex * barcode_to_vertex(int barCode) const
assign a barcode to a vertex
void set_finished_first_event(bool b)
Special information is processed the first time we use the IO.
Units::LengthUnit io_position_unit() const
get the I/O length units
std::istream & read(std::istream &)
HeavyIon const * heavy_ion() const
access the HeavyIon container if it exists
The GenCrossSection class stores the generated cross section.
void set_signal_process_vertex(GenVertex *)
set pointer to the vertex containing the signal process
void use_input_units(Units::MomentumUnit, Units::LengthUnit)
void add_particle_in(GenParticle *inparticle)
add incoming particle
int barcode() const
particle barcode
void HepMCStreamCallback(std::ios_base::event e, std::ios_base &b, int i)
std::istream & read(std::istream &)
read from an input stream
void set_event_scale(double scale)
set energy scale
std::istream & set_input_units(std::istream &, Units::MomentumUnit, Units::LengthUnit)
set the units for this input stream
void set_cross_section(const GenCrossSection &)
provide a pointer to the GenCrossSection container
int io_type() const
get IO type
std::pair< HepMC::GenParticle *, HepMC::GenParticle * > beam_particles() const
pair of pointers to the two incoming beam particles
void set_event_number(int eventno)
set event number
bool is_valid() const
verify that the instance contains non-zero information
GenVertex * production_vertex() const
pointer to the production vertex
GenVertex contains information about decay vertices.
The GenEvent class is the core of HepMC.
TempParticleMap is a temporary GenParticle* container used during input.
StreamInfo & get_stream_info(IO &iost)
GenVertex * signal_process_vertex() const
pointer to the vertex containing the signal process
Units::MomentumUnit io_momentum_unit() const
get the I/O momentum units
Units::MomentumUnit momentum_unit() const
Units used by the GenParticle momentum FourVector.
void set_random_states(const std::vector< long > &randomstates)
provide random state information
void set_heavy_ion(const HeavyIon &ion)
provide a pointer to the HeavyIon container
bool add_vertex(GenVertex *vtx)
adds to evt and adopts
std::string versionName()
return HepMC version
bool reading_event_header()
vertex_const_iterator vertices_end() const
end vertex iteration
void set_mpi(int)
set number of multi parton interactions
WeightContainer & weights()
direct access to WeightContainer
int vertices_size() const
how many vertex barcodes exist?
void set_alphaQCD(double a)
set QCD coupling
void set_alphaQED(double a)
set QED coupling
std::ostream & write(std::ostream &) const
write to an output stream
bool set_beam_particles(GenParticle *, GenParticle *)
set incoming beam particles
std::ostream & write(std::ostream &)
std::string IO_GenEvent_Key() const
IO_GenEvent begin event block key.
void clear()
empties the entire event
std::ostream & write_HepMC_IO_block_begin(std::ostream &)
Explicitly write the begin block lines that IO_GenEvent uses.
std::size_t size_type
defining the size type used by vector and map
StreamInfo contains extra information needed when using streaming IO.
void set_reading_event_header(bool)
set the reading_event_header flag
vertex_const_iterator vertices_begin() const
begin vertex iteration
GenVertex * end_vertex() const
pointer to the decay vertex
int mpi() const
number of multi parton interactions
int event_number() const
event number
bool is_set() const
True if the cross section has been set. False by default.
int signal_process_id() const
unique signal process id
The HeavyIon class stores information about heavy ions.
void use_units(Units::MomentumUnit, Units::LengthUnit)
void set_signal_process_id(int id)
set unique signal process id
PdfInfo const * pdf_info() const
access the PdfInfo container if it exists
std::ostream & write_HepMC_IO_block_end(std::ostream &)
Explicitly write the end block line that IO_GenEvent uses.
const std::vector< long > & random_states() const
vector of integers containing information about the random state
bool is_valid() const
verify that the instance contains non-zero information
double event_scale() const
energy scale, see hep-ph/0109068
size_type size() const
size of weight container
The GenParticle class contains information about generated particles.
double alphaQCD() const
QCD coupling, see hep-ph/0109068.
The PdfInfo class stores PDF information.
Units::LengthUnit length_unit() const
Units used by the GenVertex position FourVector.
void clear()
clear the weight container