56 typedef boost::iostreams::stream<dd4hep_file_source<int> >
in_stream;
70 std::vector<Particle*>& particles)
override;
89 #include <CLHEP/Units/SystemOfUnits.h>
90 #include <CLHEP/Units/PhysicalConstants.h>
133 signal_process_id(0), signal_process_vertex(0),
134 scale(0.0), alpha_qcd(0.0), alpha_qed(0.0), weights(), random() {}
165 EventStream(std::istream& in) : instream(in), mom_unit(0.0), pos_unit(0.0),
166 io_type(0), xsection(0.0), xsection_err(0.0)
167 { use_default_units(); }
173 void set_io(
int typ,
const std::string& k)
174 { io_type = typ;
key = k; }
176 { mom_unit = CLHEP::MeV; pos_unit = CLHEP::mm; }
181 char get_input(std::istream& is, std::istringstream& iline);
185 int read_vertex(EventStream &
info, std::istream& is, std::istringstream & iline);
190 int read_pdf(EventStream &, std::istringstream & input);
206 m_input.open(nam.c_str(), BOOST_IOS::in|BOOST_IOS::binary);
207 if ( not m_input.is_open() ) {
208 except(
"+++ Failed to open input stream: %s Error:%s.", nam.c_str(), ::strerror(errno));
223 if( m_currEvent < event_number && event_number != 0 ) {
224 printout(INFO,
"EventReaderHepMC::moveToEvent",
"Current event:%d Skipping the next %d events",
225 m_currEvent, event_number);
226 while ( m_currEvent < event_number ) {
227 if ( not m_events->read() )
return EVENT_READER_ERROR;
231 printout(DEBUG,
"EventReaderHepMC::moveToEvent",
"Current event number: %d",m_currEvent);
232 return EVENT_READER_OK;
244 vertices.emplace_back( primary_vertex );
245 primary_vertex->
x = 0;
246 primary_vertex->
y = 0;
247 primary_vertex->
z = 0;
249 if ( !m_events->ok() ) {
252 return EVENT_READER_EOF;
254 else if ( m_events->read() ) {
257 Position pos(primary_vertex->
x,primary_vertex->
y,primary_vertex->
z);
259 output.reserve(parts.size());
260 transform(parts.begin(),parts.end(),back_inserter(output),detail::reference2nd(parts));
263 for(Particles::iterator k=output.begin(); k != output.end(); ++k) {
273 for(Particles::const_iterator k=output.begin(); k != output.end(); ++k) {
275 printout(VERBOSE,m_name,
276 "+++ %s ID:%3d status:%08X typ:%9d Mom:(%+.2e,%+.2e,%+.2e)[MeV] "
277 "time: %+.2e [ns] #Dau:%3d #Par:%1d",
278 "",p->id,p->status,p->pdgID,
279 p->psx/CLHEP::MeV,p->psy/CLHEP::MeV,p->psz/CLHEP::MeV,p->time/CLHEP::ns,
285 if ( p->parents.size() == 0 ) {
288 primary_vertex->
in.insert(p->id);
290 primary_vertex->
out.insert(p->id);
294 return EVENT_READER_OK;
298 return EVENT_READER_EOF;
304 EventStream::Particles::iterator i;
305 std::set<int>::const_iterator id, ip;
306 for(i=parts.begin(); i != parts.end(); ++i) {
311 #if defined(DD4HEP_DEBUG_HEP_MC_VERTEX)
312 if ( end_vtx_id == DD4HEP_DEBUG_HEP_MC_VERTEX ) {
313 std::cout <<
"End-vertex:" << end_vtx_id << std::endl;
321 for(
id=
v->out.begin();
id!=
v->out.end();++
id) {
322 EventStream::Particles::iterator ipp = parts.find(*
id);
325 std::cout <<
"ERROR: Invalid daughter particle: " << *
id << std::endl;
332 for(
const auto& iv : verts) {
334 for (
int pout :
v->out) {
335 EventStream::Particles::iterator ipp = parts.find(pout);
336 if ( ipp != parts.end() ) {
338 for (
int d :
v->in) {
346 std::vector<Geant4Particle*> beam;
347 for(
const auto& ipart : parts) {
349 if ( p->
parents.size() == 0 ) {
352 beam.emplace_back(pp);
356 for(
auto* ipp : beam) {
364 EventStream::Vertices::iterator it=
info.vertices().find(i);
365 return (it==
info.vertices().end()) ? 0 : (*it).second;
369 char value = is.peek();
371 std::cerr <<
"StreamHelpers: setting badbit." << std::endl;
372 is.clear(std::ios::badbit);
375 std::string line, firstc;
378 std::cerr <<
"StreamHelpers: setting badbit." << std::endl;
379 is.clear(std::ios::badbit);
384 if ( line.length()>0 && line[1] ==
' ' ) iline >> firstc;
385 return iline ? value : -1;
391 char val = is.peek();
396 if ( !is.good() )
return 0;
404 size_t name_size = 0;
406 info.weights.names.clear();
407 info.weights.weights.clear();
410 WeightContainer namedWeight;
411 std::string::size_type i1 = line.find(
"\""), i2, len = line.size();
412 for(
size_t ii = 0; ii < name_size; ++ii) {
415 std::cout <<
"debug: attempting to read past the end of the named weight line " << std::endl;
416 std::cout <<
"debug: We should never get here" << std::endl;
417 std::cout <<
"debug: Looking for the end of this event" << std::endl;
420 i2 = line.find(
"\"",i1+1);
421 name = line.substr(i1+1,i2-i1-1);
422 if ( namedWeight.names.find(name) == namedWeight.names.end() )
423 namedWeight.names[name] = namedWeight.names.size();
424 namedWeight.weights[ii] =
info.weights.weights[ii];
425 i1 = line.find(
"\"",i2+1);
428 info.weights.weights = namedWeight.weights;
429 info.weights.names = namedWeight.names;
435 float ene = 0., theta = 0., phi = 0;
436 int size = 0, stat=0;
441 p->
id =
info.particles().size();
442 #if defined(DD4HEP_DEBUG_HEP_MC_PARTICLE)
443 if ( p->
id == DD4HEP_DEBUG_HEP_MC_PARTICLE ) {
444 std::cout <<
"Particle id: " << p->
id << std::endl;
451 ene *=
info.mom_unit;
462 input >> stat >> theta >> phi >> p->
secondaries >> size;
485 size = std::min(size,100);
486 for (
int i = 0; i < size; ++i ) {
494 int id=0, dummy = 0, num_orphans_in=0, num_particles_out=0, weights_size=0;
495 std::vector<float> weights;
503 input >>
id >> dummy >>
v->x >>
v->y >>
v->z >>
v->time
504 >> num_orphans_in >> num_particles_out >> weights_size;
509 if ( weights_size < 0 || weights_size > USHRT_MAX ) {
513 #if defined(DD4HEP_DEBUG_HEP_MC_VERTEX)
514 if (
id == DD4HEP_DEBUG_HEP_MC_VERTEX ) {
515 printout(ALWAYS,
"HepMC",
"++ Created Vertex ID=%d",
id);
518 v->x *=
info.pos_unit;
519 v->y *=
info.pos_unit;
520 v->z *=
info.pos_unit;
521 for (
int i1 = 0; i1 < weights_size; ++i1) {
524 weights.emplace_back(value);
530 info.vertices().emplace(
id,
v);
531 for(
char value = is.peek(); value==
'P'; value=is.peek()) {
533 if( !input || value < 0 )
538 printout(ERROR,
"HepMC",
"++ Vertex %d Failed to daughter read particle!",
id);
542 info.particles().emplace(p->
id,p);
546 if ( --num_orphans_in >= 0 ) {
551 #if defined(DD4HEP_DEBUG_HEP_MC_VERTEX)
552 if (
id == DD4HEP_DEBUG_HEP_MC_VERTEX ) {
553 printout(ALWAYS,
"HepMC",
"++ Vertex %d Add INGOING Particle: %d",
id,p->
id);
557 else if ( num_particles_out >= 0 ) {
558 v->out.insert(p->
id);
562 #if defined(DD4HEP_DEBUG_HEP_MC_VERTEX)
563 if (
id == DD4HEP_DEBUG_HEP_MC_VERTEX ) {
564 printout(ALWAYS,
"HepMC",
"++ Vertex %d Add OUTGOING Particle: %d",
id,p->
id);
570 except(
"HepMC",
"Invalid number of particles....");
583 if( input.fail() )
return 0;
586 input >> header.
scale;
593 input >> header.
bp1 >> header.
bp2;
595 printout(DEBUG,
"HepMC",
"++ Event header: %s",input.str().c_str());
600 if( size < 0 || size > USHRT_MAX )
603 for(
int i = 0; i < size; ++i ) {
606 header.
random.emplace_back(val);
607 if( input.fail() )
return 0;
613 if( size < 0 || size > USHRT_MAX )
616 std::vector<float> wgt;
617 for(
int ii = 0; ii < size; ++ii ) {
620 wgt.emplace_back(val);
621 if( input.fail() )
return 0;
624 if( !wgt.empty() ) header.
weights = std::move(wgt);
629 input >>
info.xsection >>
info.xsection_err;
630 return input.fail() ? 0 : 1;
635 std::string mom, pos;
637 if ( !input.fail() ) {
638 if ( mom ==
"KEV" )
info.mom_unit = CLHEP::keV;
639 else if ( mom ==
"MEV" )
info.mom_unit = CLHEP::MeV;
640 else if ( mom ==
"GEV" )
info.mom_unit = CLHEP::GeV;
641 else if ( mom ==
"TEV" )
info.mom_unit = CLHEP::TeV;
643 if ( pos ==
"MM" )
info.pos_unit = CLHEP::mm;
644 else if ( pos ==
"CM" )
info.pos_unit = CLHEP::cm;
645 else if ( pos ==
"M" )
info.pos_unit = CLHEP::m;
648 return input.fail() ? 0 : 1;
653 int nh =0, np =0, nt =0, nc =0,
654 neut = 0, prot = 0, nw =0, nwn =0, nwnw =0;
655 float impact = 0., plane = 0., xcen = 0., inel = 0.;
656 input >> nh >> np >> nt >> nc >> neut >> prot >> nw >> nwn >> nwnw;
657 input >> impact >> plane >> xcen >> inel;
674 return input.fail() ? 0 : 1;
680 double x1 = 0., x2 = 0., scale = 0., pdf1 = 0., pdf2 = 0.;
688 input >> id2 >> x1 >> x2 >> scale >> pdf1 >> pdf2;
703 int pdf_id1=0, pdf_id2=0;
704 input >> pdf_id1 >> pdf_id2;
710 return input.fail() ? 0 : 1;
716 if ( instream.eof() || instream.fail() ) {
717 instream.clear(std::ios::badbit);
724 detail::releaseObjects(m_vertices);
725 detail::releaseObjects(m_particles);
730 bool event_read =
false;
732 detail::releaseObjects(vertices());
733 detail::releaseObjects(particles());
735 while( instream.good() ) {
736 char value = instream.peek();
737 std::istringstream input_line;
738 if ( value ==
'E' && event_read )
740 else if ( instream.eof() && event_read )
742 else if ( instream.eof() )
744 else if ( value==
'#' || ::isspace(value) ) {
751 if( !input_line || value < 0 )
757 std::string key_value;
758 input_line >> key_value;
760 key_value = key_value.substr(0,key_value.find(
'\r'));
761 if ( key_value ==
"H" && (this->io_type ==
gen || this->io_type ==
extascii) ) {
765 else if( key_value ==
"HepMC::IO_GenEvent-START_EVENT_LISTING" )
766 this->set_io(
gen,key_value);
767 else if( key_value ==
"HepMC::IO_Ascii-START_EVENT_LISTING" )
768 this->set_io(
ascii,key_value);
769 else if( key_value ==
"HepMC::IO_ExtendedAscii-START_EVENT_LISTING" )
771 else if( key_value ==
"HepMC::IO_Ascii-START_PARTICLE_DATA" )
773 else if( key_value ==
"HepMC::IO_ExtendedAscii-START_PARTICLE_DATA" )
775 else if( key_value ==
"HepMC::IO_GenEvent-END_EVENT_LISTING" )
777 else if( key_value ==
"HepMC::IO_Ascii-END_EVENT_LISTING" )
779 else if( key_value ==
"HepMC::IO_ExtendedAscii-END_EVENT_LISTING" )
781 else if( key_value ==
"HepMC::IO_Ascii-END_PARTICLE_DATA" )
783 else if( key_value ==
"HepMC::IO_ExtendedAscii-END_PARTICLE_DATA" )
786 if( iotype != 0 && this->io_type != iotype ) {
787 std::cerr <<
"GenEvent::find_end_key: iotype keys have changed. "
788 <<
"MALFORMED INPUT" << std::endl;
789 instream.clear(std::ios::badbit);
792 else if ( iotype != 0 ) {
829 std::cerr <<
"streaming input: found unexpected Particle line." << std::endl;
837 printout(WARNING,
"HepMC::EventStream",
"+++ Skip event with ID: %d",this->header.id);
838 detail::releaseObjects(vertices());
839 detail::releaseObjects(particles());
842 if ( instream.eof() )
return false;
845 if( not instream.good() )
return false;
848 detail::releaseObjects(vertices());