28 HEPMC3_ERROR_LEVEL(100,
"ReaderAscii: could not open input file: " << filename)
37 HEPMC3_ERROR_LEVEL(100,
"ReaderAscii: could not open input stream ")
47 HEPMC3_ERROR_LEVEL(100,
"ReaderAscii: could not open input stream ")
56 std::array<char, 262144> buf{};
57 bool event_context =
false;
58 bool run_info_context =
false;
64 if ( peek ==
'E' ) { event_context =
true; nn--; }
66 if ( !event_context && ( peek ==
'W' || peek ==
'A' || peek ==
'T' ) ) {
68 if (!run_info_context) {
70 run_info_context =
true;
82 if ( event_context && ( peek ==
'V' || peek ==
'P' ) ) event_context=
false;
83 if (nn < 0)
return true;
94 std::array<char, 262144> buf{};
95 bool event_context =
false;
96 bool parsed_weights =
false;
97 bool parsed_particles_or_vertices =
false;
98 bool run_info_context =
false;
99 bool is_parsing_successful =
true;
100 std::pair<int, int> vertices_and_particles(0, 0);
112 m_data.attribute_id.clear();
113 m_data.attribute_name.clear();
114 m_data.attribute_string.clear();
121 if ( strlen(buf.data()) == 0 )
continue;
124 if ( strncmp(buf.data(),
"HepMC", 5) == 0 ) {
125 if ( strncmp(buf.data(),
"HepMC::Version", 14) != 0 && strncmp(buf.data(),
"HepMC::Asciiv3", 14) != 0 )
127 HEPMC3_WARNING_LEVEL(500,
"ReaderAscii: found unsupported expression in header. Will close the input.")
128 std::cout << buf.data() << std::endl;
132 is_parsing_successful =
true;
141 if (vertices_and_particles.second < 0) {
142 is_parsing_successful =
false;
144 is_parsing_successful =
true;
145 event_context =
true;
146 parsed_weights =
false;
147 parsed_particles_or_vertices =
false;
151 run_info_context =
false;
155 parsed_particles_or_vertices =
true;
159 parsed_particles_or_vertices =
true;
162 if ( event_context ) {
166 if ( !run_info_context ) {
170 run_info_context =
true;
178 if ( event_context ) {
181 if ( !run_info_context ) {
185 run_info_context =
true;
186 is_parsing_successful =
parse_tool(buf.data());
190 if ( event_context ) {
193 if ( !run_info_context ) {
197 run_info_context =
true;
203 is_parsing_successful =
true;
207 if ( !is_parsing_successful )
break;
212 if ( event_context && peek ==
'E' )
break;
215 if ( event_context && peek ==
'W' && parsed_weights )
break;
218 if ( event_context && peek ==
'A' && parsed_particles_or_vertices )
break;
221 if ( event_context && peek ==
'T' )
break;
227 int currid = -
static_cast<int>(
m_data.vertices.size());
230 for (; currid < iofirst; ++currid, ++fir) {
241 for (
const auto& i: io.second.first) {
m_data.links1.push_back(i);
m_data.links2.push_back(io.first); }
242 for (
const auto& o: io.second.second) {
m_data.links1.push_back(io.first);
m_data.links2.push_back(o); }
247 if (
static_cast<int>(evt.
particles().size()) > vertices_and_particles.second) {
249 printf(
"%zu vs %i expected\n", evt.
particles().size(), vertices_and_particles.second);
250 is_parsing_successful =
false;
252 if (
static_cast<int>(evt.
particles().size()) < vertices_and_particles.second) {
254 printf(
"%zu vs %i expected\n", evt.
particles().size(), vertices_and_particles.second);
255 is_parsing_successful =
false;
258 if (
static_cast<int>(evt.
vertices().size()) > vertices_and_particles.first) {
260 printf(
"%zu vs %i expected\n", evt.
vertices().size(), vertices_and_particles.first);
261 is_parsing_successful =
false;
264 if (
static_cast<int>(evt.
vertices().size()) < vertices_and_particles.first) {
266 printf(
"%zu vs %i expected\n", evt.
vertices().size(), vertices_and_particles.first);
267 is_parsing_successful =
false;
270 if ( !is_parsing_successful ) {
272 HEPMC3_DEBUG(1,
"Parsing failed at line:" << std::endl << buf.data())
286 static const std::pair<int, int> err(-1, -1);
287 std::pair<int, int> ret(-1, -1);
288 const char *cursor = buf;
292 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
293 m_data.event_number = atoi(cursor);
296 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
297 ret.first = atoi(cursor);
300 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
301 ret.second = atoi(cursor);
302 m_data.vertices = std::vector<GenVertexData>(ret.first);
303 m_data.particles = std::vector<GenParticleData>(ret.second);
305 m_data.links1.reserve(ret.second*2);
306 m_data.links2.reserve(ret.second*2);
307 m_data.attribute_id.reserve(ret.second + ret.first);
308 m_data.attribute_name.reserve(ret.second + ret.first);
309 m_data.attribute_string.reserve(ret.second + ret.first);
312 if ( (cursor = strchr(cursor+1,
'@')) ) {
314 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
315 position.
setX(atof(cursor));
318 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
319 position.
setY(atof(cursor));
322 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
323 position.
setZ(atof(cursor));
326 if ( !(cursor = strchr(cursor+1,
' ')) )
return err;
327 position.
setT(atof(cursor));
330 HEPMC3_DEBUG(10,
"ReaderAscii: E: " <<
m_data.event_number <<
" (" <<ret.first <<
"V, " << ret.second <<
"P)")
337 std::istringstream iss(buf + 1);
338 std::vector<double> wts;
340 while (iss >> w) wts.emplace_back(w);
342 &&
run_info()->weight_names().size() != wts.size() ) {
343 throw std::logic_error(
"ReaderAscii::parse_weight_values: "
344 "The number of weights ("+std::to_string(
static_cast<long long int>(wts.size()))+
") does not match "
345 "the number weight names("+std::to_string(
static_cast<long long int>(
run_info()->weight_names().size()))+
") in the GenRunInfo object");
354 const char *cursor = buf;
357 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
362 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
373 GenVertexPtr data = std::make_shared<GenVertex>();
374 const char *cursor = buf;
375 const char *cursor2 =
nullptr;
379 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
383 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
384 m_data.vertices[-
id-1].status = atoi(cursor);
388 if ( !(cursor = strchr(cursor+1,
'[')) )
return false;
393 int particle_in = atoi(cursor);
396 if (particle_in > 0) {
402 if ( !(cursor = strchr(cursor+1,
',')) ) {
403 if ( !(cursor = strchr(cursor2+1,
']')) )
return false;
409 if ( (cursor = strchr(cursor+1,
'@')) ) {
411 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
412 position.
setX(atof(cursor));
415 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
416 position.
setY(atof(cursor));
419 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
420 position.
setZ(atof(cursor));
423 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
424 position.
setT(atof(cursor));
432 const char *cursor = buf;
436 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
438 int id = atoi(cursor);
439 if ( id < 1 || id >
static_cast<int>(
m_data.particles.size()) ) {
446 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
447 mother_id = atoi(cursor);
448 if ( mother_id < -
static_cast<int>(
m_data.vertices.size()) || mother_id >
static_cast<int>(
m_data.particles.size()) ) {
449 HEPMC3_ERROR_LEVEL(600,
"ReaderAscii: ID of particle mother is out of expected range.")
453 if ( mother_id > 0) {
464 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
465 m_data.particles[
id-1].pid = atoi(cursor);
468 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
469 momentum.
setPx(atof(cursor));
472 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
473 momentum.
setPy(atof(cursor));
476 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
477 momentum.
setPz(atof(cursor));
480 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
481 momentum.
setE(atof(cursor));
484 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
485 m_data.particles[
id-1].mass = atof(cursor);
486 m_data.particles[
id-1].is_mass_set =
true;
489 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
490 m_data.particles[
id-1].status = atoi(cursor);
497 const char *cursor = buf;
498 const char *cursor2 = buf;
499 std::array<char, 512> name{};
502 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
505 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
508 if ( !(cursor2 = strchr(cursor,
' ')) )
return false;
509 snprintf(name.data(), name.size(),
"%.*s",
static_cast<int>(cursor2-cursor), cursor);
513 m_data.attribute_id.push_back(
id);
514 m_data.attribute_name.emplace_back(name.data());
521 const char *cursor = buf;
522 const char *cursor2 = buf;
523 std::array<char, 512> name{};
525 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
528 if ( !(cursor2 = strchr(cursor,
' ')) )
return false;
529 snprintf(name.data(), name.size(),
"%.*s",
static_cast<int>(cursor2-cursor), cursor);
533 std::shared_ptr<StringAttribute> att =
536 run_info()->add_attribute(std::string(name.data()), att);
543 const char *cursor = buf;
545 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
548 std::istringstream iss(
unescape(cursor));
549 std::vector<std::string> names;
551 while (iss >> name) names.emplace_back(name);
553 run_info()->set_weight_names(names);
559 const char *cursor = buf;
561 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
563 std::string line =
unescape(cursor);
565 std::string::size_type pos = line.find(
'\n');
566 tool.
name = line.substr(0, pos);
567 line = line.substr(pos + 1);
568 pos = line.find(
'\n');
569 tool.
version = line.substr(0, pos);
571 run_info()->tools().emplace_back(tool);
579 ret.reserve(s.length());
580 for ( std::string::const_iterator it = s.begin(); it != s.end(); ++it ) {
599 if ( !
m_file.is_open())
return;
#define HEPMC3_WARNING_LEVEL(LEVEL, MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
#define HEPMC3_ERROR_LEVEL(LEVEL, MESSAGE)
Macro for printing error messages.
#define HEPMC3_DEBUG(LEVEL, MESSAGE)
Macro for printing debug messages with appropriate debug level.
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 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 read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
void clear()
Remove contents of this event.
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
bool parse_weight_values(const char *buf)
Parse weight value lines.
bool parse_tool(const char *buf)
Parse run-level tool information.
bool m_isstream
toggles usage of m_file or m_stream
std::map< int, std::pair< std::set< int >, std::set< int > > > m_io_explicit
Temp storage for sets of incoming/outgoing ids for explicit vertices.
std::pair< int, int > parse_event_information(const char *buf)
Parse event.
bool read_event(GenEvent &evt) override
Load event from file.
static 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.
std::set< int > m_io_explicit_ids
Temp storage to keep the order of explicit vertices.
void close() override
Close file stream.
std::vector< int > m_io_implicit_ids
Temp storage to keep the order of implicit vertices.
bool parse_vertex_information(const char *buf)
Parse vertex.
GenEventData m_data
To hold event information.
bool parse_attribute(const char *buf)
Parse attribute.
~ReaderAscii()
Destructor.
bool parse_weight_names(const char *buf)
Parse run-level weight names.
bool parse_units(const char *buf)
Parse units.
ReaderAscii(const std::string &filename)
Constructor.
std::istream * m_stream
For ctor when reading from stream.
std::shared_ptr< std::istream > m_shared_stream
For ctor when reading from temp. stream.
std::unordered_map< int, std::pair< std::set< int >, std::set< int > > > m_io_implicit
Temp storage for sets of incoming/outgoing ids for implicit vertices.
bool parse_particle_information(const char *buf)
Parse particle.
bool parse_run_attribute(const char *buf)
Parse run-level attribute.
virtual void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
virtual 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.
static std::string name(MomentumUnit u)
Get name of momentum unit.
static MomentumUnit momentum_unit(const std::string &name)
Get momentum unit based on its name.