23 : m_file(filename), m_stream(0), m_isstream(false)
26 ERROR(
"ReaderAscii: could not open input file: "<<filename )
34 : m_stream(&stream), m_isstream(true)
37 ERROR(
"ReaderAscii: could not open input stream " )
48 if ( (!
m_file.is_open()) && (!m_isstream) )
return false;
52 bool parsed_event_header =
false;
53 bool is_parsing_successful =
true;
54 pair<int,int> vertices_and_particles(0,0);
64 m_isstream ? m_stream->getline(buf,512*512) :
m_file.getline(buf,512*512);
66 if( strlen(buf) == 0 )
continue;
69 if( strncmp(buf,
"HepMC",5) == 0 ) {
70 if( strncmp(buf,
"HepMC::Version",14) != 0 && strncmp(buf,
"HepMC::Asciiv3",14)!=0 )
72 WARNING(
"ReaderAscii: found unsupported expression in header. Will close the input." )
73 std::cout<<buf<<std::endl;
74 m_isstream ? m_stream->clear(ios::eofbit) :
m_file.clear(ios::eofbit);
76 if(parsed_event_header) {
77 is_parsing_successful =
true;
86 if (vertices_and_particles.second < 0) {
87 is_parsing_successful =
false;
89 is_parsing_successful =
true;
90 parsed_event_header =
true;
100 if ( parsed_event_header )
112 if ( parsed_event_header )
118 WARNING(
"ReaderAscii: skipping unrecognised prefix: " << buf[0] )
119 is_parsing_successful =
true;
123 if( !is_parsing_successful )
break;
126 m_isstream ? peek = m_stream->peek() : peek =
m_file.peek();
127 if( parsed_event_header && peek==
'E' )
break;
132 if ((
int)evt.
particles().size() > vertices_and_particles.second ) {
133 ERROR(
"ReaderAscii: too many particles were parsed" )
134 printf(
"%zu vs %i expected\n",evt.
particles().size(),vertices_and_particles.second );
135 is_parsing_successful =
false;
137 if ((
int)evt.
particles().size() < vertices_and_particles.second ) {
138 ERROR(
"ReaderAscii: too few particles were parsed" )
139 printf(
"%zu vs %i expected\n",evt.
particles().size(),vertices_and_particles.second );
140 is_parsing_successful =
false;
143 if ((
int)evt.
vertices().size() > vertices_and_particles.first) {
144 ERROR(
"ReaderAscii: too many vertices were parsed" )
145 printf(
"%zu vs %i expected\n",evt.
vertices().size(),vertices_and_particles.first );
146 is_parsing_successful =
false;
149 if ((
int)evt.
vertices().size() < vertices_and_particles.first) {
150 ERROR(
"ReaderAscii: too few vertices were parsed" )
151 printf(
"%zu vs %i expected\n",evt.
vertices().size(),vertices_and_particles.first );
152 is_parsing_successful =
false;
155 if( !is_parsing_successful ) {
156 ERROR(
"ReaderAscii: event parsing failed. Returning empty event" )
157 DEBUG( 1,
"Parsing failed at line:" << endl << buf )
160 m_isstream ? m_stream->clear(ios::badbit) :
m_file.clear(ios::badbit);
169 static const pair<int,int> err(-1,-1);
170 pair<int,int> ret(-1,-1);
171 const char *cursor = buf;
176 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
177 event_no = atoi(cursor);
181 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
182 ret.first = atoi(cursor);
185 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
186 ret.second = atoi(cursor);
189 if( (cursor = strchr(cursor+1,
'@')) ) {
192 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
193 position.
setX(atof(cursor));
196 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
197 position.
setY(atof(cursor));
200 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
201 position.
setZ(atof(cursor));
204 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
205 position.
setT(atof(cursor));
209 DEBUG( 10,
"ReaderAscii: E: "<<event_no<<
" ("<<ret.first<<
"V, "<<ret.second<<
"P)" )
217 std::istringstream iss(buf + 1);
220 while ( iss >> w ) wts.push_back(w);
222 &&
run_info()->weight_names().size() != wts.size() )
223 throw std::logic_error(
"ReaderAscii::parse_weight_values: "
224 "The number of weights ("+std::to_string((
long long int)(wts.size()))+
") does not match "
225 "the number weight names("+std::to_string((
long long int)(
run_info()->weight_names().size()))+
") in the GenRunInfo object");
233 const char *cursor = buf;
236 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
241 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
245 evt.
set_units(momentum_unit,length_unit);
254 GenVertexPtr data = make_shared<GenVertex>();
256 const char *cursor = buf;
257 const char *cursor2 =
nullptr;
262 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
266 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
267 data->set_status( atoi(cursor) );
270 if( !(cursor = strchr(cursor+1,
'[')) )
return false;
275 int particle_in = atoi(cursor);
278 if( particle_in > 0 && particle_in <= highest_id) {
279 data->add_particle_in( evt.
particles()[particle_in-1] );
285 if (existing_hc->value()!=0&&particle_in > 0 )
287 WARNING(
"ReaderAscii: event has cycles, this vertex might be repeated." )
296 if( !(cursor = strchr(cursor+1,
',')) ) {
297 if( !(cursor = strchr(cursor2+1,
']')) )
return false;
303 if( (cursor = strchr(cursor+1,
'@')) ) {
306 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
307 position.setX(atof(cursor));
310 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
311 position.setY(atof(cursor));
314 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
315 position.setZ(atof(cursor));
318 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
319 position.setT(atof(cursor));
320 data->set_position( position );
324 DEBUG( 10,
"ReaderAscii: V: "<<
id<<
" with "<<data->particles_in().size()<<
" particles)" )
333 GenParticlePtr data = make_shared<GenParticle>();
335 const char *cursor = buf;
339 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
341 if( atoi(cursor) != (int)evt.
particles().size() + 1 ) {
343 ERROR(
"ReaderAscii: particle ID mismatch" )
348 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
349 mother_id = atoi(cursor);
352 if( mother_id > 0 && mother_id <= (
int)evt.
particles().size() ) {
354 GenParticlePtr mother = evt.
particles()[ mother_id-1 ];
355 GenVertexPtr vertex = mother->end_vertex();
359 vertex = make_shared<GenVertex>();
360 vertex->add_particle_in(mother);
363 vertex->add_particle_out(data);
366 else if( mother_id < 0 && -mother_id <= (
int)evt.
vertices().size() ) {
367 evt.
vertices()[ (-mother_id)-1 ]->add_particle_out(data);
371 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
372 data->set_pid( atoi(cursor) );
375 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
376 momentum.setPx(atof(cursor));
379 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
380 momentum.setPy(atof(cursor));
383 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
384 momentum.setPz(atof(cursor));
387 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
388 momentum.setE(atof(cursor));
389 data->set_momentum(momentum);
392 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
393 data->set_generated_mass( atof(cursor) );
396 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
397 data->set_status( atoi(cursor) );
401 DEBUG( 10,
"ReaderAscii: P: "<<data->id()<<
" ( mother: "<<mother_id<<
", pid: "<<data->pid()<<
")" )
408 const char *cursor = buf;
409 const char *cursor2 = buf;
413 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
416 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
419 if( !(cursor2 = strchr(cursor,
' ')) )
return false;
420 sprintf(name,
"%.*s", (
int)(cursor2-cursor), cursor);
424 shared_ptr<Attribute> att =
433 const char *cursor = buf;
434 const char *cursor2 = buf;
437 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
440 if( !(cursor2 = strchr(cursor,
' ')) )
return false;
441 sprintf(name,
"%.*s", (
int)(cursor2-cursor), cursor);
445 shared_ptr<StringAttribute> att =
448 run_info()->add_attribute(
string(name), att);
456 const char *cursor = buf;
458 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
461 istringstream iss(
unescape(cursor));
462 vector<string> names;
464 while ( iss >> name ) names.push_back(name);
466 run_info()->set_weight_names(names);
473 const char *cursor = buf;
475 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
479 string::size_type pos = line.find(
"\n");
480 tool.
name = line.substr(0, pos);
481 line = line.substr(pos + 1);
482 pos = line.find(
"\n");
483 tool.
version = line.substr(0, pos);
485 run_info()->tools().push_back(tool);
494 ret.reserve(s.length());
495 for ( string::const_iterator it = s.begin(); it != s.end(); ++it ) {
511 if( !
m_file.is_open())
return;
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
bool parse_run_attribute(const char *buf)
Parse run-level attribute.
ReaderAscii(const std::string &filename)
Constructor.
bool parse_units(GenEvent &evt, const char *buf)
Parse units.
void add_vertex(GenVertexPtr v)
Add vertex.
static LengthUnit length_unit(const std::string &name)
Get length unit based on its name.
bool parse_tool(const char *buf)
Parse run-level tool information.
#define ERROR(MESSAGE)
Macro for printing error messages.
Definition of class GenParticle.
std::pair< int, int > parse_event_information(GenEvent &evt, const char *buf)
Parse event.
std::ifstream m_file
Input file.
Definition of class GenVertex.
const Units::LengthUnit & length_unit() const
Get length unit.
Attribute that holds a string.
shared_ptr< T > attribute(const string &name, const int &id=0) const
Get attribute of type T.
void add_particle(GenParticlePtr p)
Add particle.
static std::string name(MomentumUnit u)
Get name of momentum unit.
bool failed()
Return status of the stream.
bool parse_weight_names(const char *buf)
Parse run-level weight names.
bool parse_attribute(GenEvent &evt, const char *buf)
Parse attribute.
#define DEBUG(LEVEL, MESSAGE)
Macro for printing debug messages with appropriate debug level.
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
LengthUnit
Position units.
void setY(double yy)
Set y-component of position/displacement.
MomentumUnit
Momentum units.
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
void setZ(double zz)
Set z-component of position/displacement.
Stores event-related information.
void setT(double tt)
Set time component of position/displacement.
bool read_event(GenEvent &evt)
Load event from file.
Definition of class ReaderAscii.
Definition of class Units.
#define WARNING(MESSAGE)
Macro for printing warning messages.
static MomentumUnit momentum_unit(const std::string &name)
Get momentum unit based on its name.
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 add_attribute(const string &name, const shared_ptr< Attribute > &att, const int &id=0)
Add event attribute to event.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
bool parse_vertex_information(GenEvent &evt, const char *buf)
Parse vertex.
const std::vector< double > & weights() const
Get event weight values as a vector.
std::string unescape(const std::string &s)
Unsecape '\' and ' ' characters in string.
Definition of class GenEvent.
bool parse_particle_information(GenEvent &evt, const char *buf)
Parse particle.
void close()
Close file stream.
void setX(double xx)
Set x-component of position/displacement.
~ReaderAscii()
Destructor.
void clear()
Remove contents of this event.
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
Attribute that holds an Integer implemented as an int.
bool parse_weight_values(GenEvent &evt, const char *buf)
Parse weight value lines.
void set_event_number(const int &num)
Set event number.
void shift_position_to(const FourVector &newpos)
Shift position of all vertices in the event to op.