24 : m_file(filename), m_stream(0), m_isstream(false)
27 HEPMC3_ERROR(
"ReaderAscii: could not open input file: " << filename)
33 : m_stream(&stream), m_isstream(true)
36 HEPMC3_ERROR(
"ReaderAscii: could not open input stream ")
47 const size_t max_buffer_size = 512*512;
48 char buf[max_buffer_size];
49 bool event_context =
false;
50 bool run_info_context =
false;
56 if ( peek ==
'E' ) { event_context =
true; nn--; }
58 if ( !event_context && ( peek ==
'W' || peek ==
'A' || peek ==
'T' ) ) {
60 if (!run_info_context) {
62 run_info_context =
true;
74 if ( event_context && ( peek ==
'V' || peek ==
'P' ) ) event_context=
false;
75 if (nn < 0)
return true;
86 const size_t max_buffer_size = 512*512;
87 char buf[max_buffer_size];
88 bool event_context =
false;
89 bool parsed_weights =
false;
90 bool parsed_particles_or_vertices =
false;
91 bool run_info_context =
false;
92 bool is_parsing_successful =
true;
93 std::pair<int, int> vertices_and_particles(0, 0);
105 if ( strlen(buf) == 0 )
continue;
108 if ( strncmp(buf,
"HepMC", 5) == 0 ) {
109 if ( strncmp(buf,
"HepMC::Version", 14) != 0 && strncmp(buf,
"HepMC::Asciiv3", 14) != 0 )
111 HEPMC3_WARNING(
"ReaderAscii: found unsupported expression in header. Will close the input.")
112 std::cout << buf << std::endl;
116 is_parsing_successful =
true;
125 if (vertices_and_particles.second < 0) {
126 is_parsing_successful =
false;
128 is_parsing_successful =
true;
129 event_context =
true;
130 parsed_weights =
false;
131 parsed_particles_or_vertices =
false;
133 run_info_context =
false;
137 parsed_particles_or_vertices =
true;
141 parsed_particles_or_vertices =
true;
144 if ( event_context ) {
148 if ( !run_info_context ) {
152 run_info_context =
true;
160 if ( event_context ) {
163 if ( !run_info_context ) {
167 run_info_context =
true;
172 if ( event_context ) {
175 if ( !run_info_context ) {
179 run_info_context =
true;
184 HEPMC3_WARNING(
"ReaderAscii: skipping unrecognised prefix: " << buf[0])
185 is_parsing_successful =
true;
189 if ( !is_parsing_successful )
break;
194 if ( event_context && peek ==
'E' )
break;
197 if ( event_context && peek ==
'W' && parsed_weights )
break;
200 if ( event_context && peek ==
'A' && parsed_particles_or_vertices )
break;
203 if ( event_context && peek ==
'T' )
break;
210 if ((
int)evt.
particles().size() > vertices_and_particles.second) {
211 HEPMC3_ERROR(
"ReaderAscii: too many particles were parsed")
212 printf(
"%zu vs %i expected\n", evt.
particles().size(), vertices_and_particles.second);
213 is_parsing_successful =
false;
215 if ((
int)evt.
particles().size() < vertices_and_particles.second) {
216 HEPMC3_ERROR(
"ReaderAscii: too few particles were parsed")
217 printf(
"%zu vs %i expected\n", evt.
particles().size(), vertices_and_particles.second);
218 is_parsing_successful =
false;
221 if ((
int)evt.
vertices().size() > vertices_and_particles.first) {
222 HEPMC3_ERROR(
"ReaderAscii: too many vertices were parsed")
223 printf(
"%zu vs %i expected\n", evt.
vertices().size(), vertices_and_particles.first);
224 is_parsing_successful =
false;
227 if ((
int)evt.
vertices().size() < vertices_and_particles.first) {
228 HEPMC3_ERROR(
"ReaderAscii: too few vertices were parsed")
229 printf(
"%zu vs %i expected\n", evt.
vertices().size(), vertices_and_particles.first);
230 is_parsing_successful =
false;
233 if ( !is_parsing_successful ) {
234 HEPMC3_ERROR(
"ReaderAscii: event parsing failed. Returning empty event")
235 HEPMC3_DEBUG(1,
"Parsing failed at line:" << std::endl << buf)
244 if (p.second == v->id())
245 v->add_particle_out(p.first);
249 std::vector<int> all_ids;
250 std::vector<int> filled_ids;
251 std::vector<int> diff;
252 for (
auto v: evt.
vertices())
if (v->id() != 0) filled_ids.push_back(v->id());
253 for (
int i = -((
long)evt.
vertices().size()); i < 0; i++) all_ids.push_back(i);
254 std::sort(all_ids.begin(), all_ids.end());
255 std::sort(filled_ids.begin(), filled_ids.end());
257 std::set_difference(all_ids.begin(), all_ids.end(), filled_ids.begin(), filled_ids.end(), std::inserter(diff, diff.begin()));
258 auto it = diff.rbegin();
260 for (
auto v: evt.
vertices())
if (v->id() == 0) { v->set_id(*it); it++;}
267 static const std::pair<int, int> err(-1, -1);
268 std::pair<int, int> ret(-1, -1);
269 const char *cursor = buf;
274 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
275 event_no = atoi(cursor);
279 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
280 ret.first = atoi(cursor);
283 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
284 ret.second = atoi(cursor);
287 if ( (cursor = strchr(cursor+1,
'@')) ) {
289 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
290 position.
setX(atof(cursor));
293 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
294 position.
setY(atof(cursor));
297 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
298 position.
setZ(atof(cursor));
301 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
302 position.
setT(atof(cursor));
306 HEPMC3_DEBUG(10,
"ReaderAscii: E: " << event_no <<
" (" <<ret.first <<
"V, " << ret.second <<
"P)")
313 std::istringstream iss(buf + 1);
314 std::vector<double> wts;
316 while (iss >> w) wts.push_back(w);
318 &&
run_info()->weight_names().size() != wts.size() )
319 throw std::logic_error(
"ReaderAscii::parse_weight_values: "
320 "The number of weights ("+std::to_string((
long long int)(wts.size()))+
") does not match "
321 "the number weight names("+std::to_string((
long long int)(
run_info()->weight_names().size()))+
") in the GenRunInfo object");
329 const char *cursor = buf;
332 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
337 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
341 evt.
set_units(momentum_unit, length_unit);
350 GenVertexPtr data = std::make_shared<GenVertex>();
352 const char *cursor = buf;
353 const char *cursor2 =
nullptr;
358 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
362 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
363 data->set_status(atoi(cursor));
366 if ( !(cursor = strchr(cursor+1,
'[')) )
return false;
371 int particle_in = atoi(cursor);
374 if (particle_in > 0) {
376 if (particle_in <= highest_id) {
377 data->add_particle_in(evt.
particles()[particle_in-1]);
385 if ( !(cursor = strchr(cursor+1,
',')) ) {
386 if ( !(cursor = strchr(cursor2+1,
']')) )
return false;
392 if ( (cursor = strchr(cursor+1,
'@')) ) {
394 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
395 position.
setX(atof(cursor));
398 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
399 position.
setY(atof(cursor));
402 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
403 position.
setZ(atof(cursor));
406 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
407 position.
setT(atof(cursor));
408 data->set_position(position);
411 HEPMC3_DEBUG(10,
"ReaderAscii: V: " <<
id <<
" with "<< data->particles_in().size() <<
" particles)")
422 GenParticlePtr data = std::make_shared<GenParticle>();
424 const char *cursor = buf;
428 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
430 if ( atoi(cursor) != (
int)evt.
particles().size() + 1 ) {
437 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
438 mother_id = atoi(cursor);
441 if ( mother_id > 0 && mother_id <= (
int)evt.
particles().size() ) {
442 GenParticlePtr mother = evt.
particles()[ mother_id-1 ];
443 GenVertexPtr vertex = mother->end_vertex();
447 vertex = std::make_shared<GenVertex>();
448 vertex->add_particle_in(mother);
451 vertex->add_particle_out(data);
457 else if ( mother_id < 0 )
461 for (
auto v: evt.
vertices())
if (v->id() == mother_id) {v->add_particle_out(data); found =
true;
break; }
472 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
473 data->set_pid(atoi(cursor));
476 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
477 momentum.
setPx(atof(cursor));
480 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
481 momentum.
setPy(atof(cursor));
484 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
485 momentum.
setPz(atof(cursor));
488 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
489 momentum.
setE(atof(cursor));
490 data->set_momentum(momentum);
493 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
494 data->set_generated_mass(atof(cursor));
497 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
498 data->set_status(atoi(cursor));
502 HEPMC3_DEBUG(10,
"ReaderAscii: P: " << data->id() <<
" ( mother: " << mother_id <<
", pid: " << data->pid() <<
")")
509 const char *cursor = buf;
510 const char *cursor2 = buf;
514 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
517 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
520 if ( !(cursor2 = strchr(cursor,
' ')) )
return false;
521 snprintf(name, 512,
"%.*s", (
int)(cursor2-cursor), cursor);
525 std::shared_ptr<Attribute> att =
534 const char *cursor = buf;
535 const char *cursor2 = buf;
538 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
541 if ( !(cursor2 = strchr(cursor,
' ')) )
return false;
542 snprintf(name, 512,
"%.*s", (
int)(cursor2-cursor), cursor);
546 std::shared_ptr<StringAttribute> att =
549 run_info()->add_attribute(std::string(name), att);
556 const char *cursor = buf;
558 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
561 std::istringstream iss(
unescape(cursor));
562 std::vector<std::string> names;
564 while (iss >> name) names.push_back(name);
566 run_info()->set_weight_names(names);
572 const char *cursor = buf;
574 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
576 std::string line =
unescape(cursor);
578 std::string::size_type pos = line.find(
"\n");
579 tool.
name = line.substr(0, pos);
580 line = line.substr(pos + 1);
581 pos = line.find(
"\n");
582 tool.
version = line.substr(0, pos);
584 run_info()->tools().push_back(tool);
592 ret.reserve(s.length());
593 for ( std::string::const_iterator it = s.begin(); it != s.end(); ++it ) {
610 if ( !
m_file.is_open())
return;
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
#define HEPMC3_DEBUG(LEVEL, MESSAGE)
Macro for printing debug messages with appropriate debug level.
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition of class GenEvent.
Definition of class GenParticle.
Definition of class GenVertex.
Definition of class ReaderAscii.
Definition of class Units.
Stores event-related information.
void add_vertex(GenVertexPtr v)
Add vertex.
void shift_position_to(const FourVector &newpos)
Shift position of all vertices in the event to op.
void add_particle(GenParticlePtr p)
Add particle.
void set_units(Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit)
Change event units Converts event from current units to new ones.
void set_event_number(const int &num)
Set event number.
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
void add_attribute(const std::string &name, const std::shared_ptr< Attribute > &att, const int &id=0)
Add event attribute to event.
const Units::LengthUnit & length_unit() const
Get length unit.
void clear()
Remove contents of this event.
const std::vector< double > & weights() const
Get event weight values as a vector.
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
bool parse_tool(const char *buf)
Parse run-level tool information.
bool m_isstream
toggles usage of m_file or m_stream
bool parse_weight_values(GenEvent &evt, const char *buf)
Parse weight value lines.
std::map< GenParticlePtr, int > m_forward_daughters
Temp storage for prod vertex ids.
bool read_event(GenEvent &evt) override
Load event from file.
std::string unescape(const std::string &s)
Unsecape '\' and ' ' characters in string.
bool failed() override
Return status of the stream.
bool skip(const int) override
skip events
std::ifstream m_file
Input file.
bool parse_units(GenEvent &evt, const char *buf)
Parse units.
bool parse_particle_information(GenEvent &evt, const char *buf)
Parse particle.
std::map< GenVertexPtr, std::set< int > > m_forward_mothers
Temp storage for outgoing particle ids.
bool parse_attribute(GenEvent &evt, const char *buf)
Parse attribute.
void close() override
Close file stream.
bool parse_vertex_information(GenEvent &evt, const char *buf)
Parse vertex.
~ReaderAscii()
Destructor.
bool parse_weight_names(const char *buf)
Parse run-level weight names.
ReaderAscii(const std::string &filename)
Constructor.
std::istream * m_stream
For ctor when reading from stdin.
bool parse_run_attribute(const char *buf)
Parse run-level attribute.
std::pair< int, int > parse_event_information(GenEvent &evt, const char *buf)
Parse event.
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Attribute that holds a string.
static LengthUnit length_unit(const std::string &name)
Get length unit based on its name.
LengthUnit
Position units.
static std::string name(MomentumUnit u)
Get name of momentum unit.
MomentumUnit
Momentum units.
static MomentumUnit momentum_unit(const std::string &name)
Get momentum unit based on its name.