10 #include "HepMC/ReaderAscii.h" 12 #include "HepMC/GenEvent.h" 13 #include "HepMC/GenParticle.h" 14 #include "HepMC/GenVertex.h" 15 #include "HepMC/Units.h" 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(parsed_event_header) {
71 is_parsing_successful =
true;
81 if (vertices_and_particles.second < 0) {
82 is_parsing_successful =
false;
84 is_parsing_successful =
true;
85 parsed_event_header =
true;
95 if ( parsed_event_header )
107 if ( parsed_event_header )
113 WARNING(
"ReaderAscii: skipping unrecognised prefix: " << buf[0] )
114 is_parsing_successful =
true;
118 if( !is_parsing_successful )
break;
121 m_isstream ? peek = m_stream->peek() : peek =
m_file.peek();
122 if( parsed_event_header && peek==
'E' )
break;
127 if ((
int)evt.
particles().size() != vertices_and_particles.second ) {
128 ERROR(
"ReaderAscii: too few or too many particles were parsed" )
129 is_parsing_successful =
false;
132 if ((
int)evt.
vertices().size() != vertices_and_particles.first) {
133 ERROR(
"ReaderAscii: too few or too many vertices were parsed" )
134 is_parsing_successful =
false;
137 if( !is_parsing_successful ) {
138 ERROR(
"ReaderAscii: event parsing failed. Returning empty event" )
139 DEBUG( 1,
"Parsing failed at line:" << endl << buf )
142 m_isstream ? m_stream->clear(ios::badbit) :
m_file.clear(ios::badbit);
152 static const pair<int,int> err(-1,-1);
153 pair<int,int> ret(-1,-1);
154 const char *cursor = buf;
159 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
160 event_no = atoi(cursor);
164 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
165 ret.first = atoi(cursor);
168 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
169 ret.second = atoi(cursor);
172 if( (cursor = strchr(cursor+1,
'@')) ) {
175 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
176 position.
setX(atof(cursor));
179 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
180 position.
setY(atof(cursor));
183 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
184 position.
setZ(atof(cursor));
187 if( !(cursor = strchr(cursor+1,
' ')) )
return err;
188 position.
setT(atof(cursor));
192 DEBUG( 10,
"ReaderAscii: E: "<<event_no<<
" ("<<ret.first<<
"V, "<<ret.second<<
"P)" )
200 std::istringstream iss(buf + 1);
203 while ( iss >> w ) wts.push_back(w);
205 &&
run_info()->weight_names().size() != wts.size() )
206 throw std::logic_error(
"ReaderAscii::parse_weight_values: " 207 "The number of weights does not match " 208 "the weight names in the GenRunInfo object");
216 const char *cursor = buf;
219 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
224 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
228 evt.
set_units(momentum_unit,length_unit);
239 const char *cursor = buf;
240 const char *cursor2 = NULL;
246 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
250 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
251 data->set_status( atoi(cursor) );
254 if( !(cursor = strchr(cursor+1,
'[')) )
return false;
259 particle_in = atoi(cursor);
262 if( particle_in > 0 && particle_in <= highest_id) {
263 data->add_particle_in( evt.
particles()[particle_in-1] );
270 if( !(cursor = strchr(cursor+1,
',')) ) {
271 if( !(cursor = strchr(cursor2+1,
']')) )
return false;
277 if( (cursor = strchr(cursor+1,
'@')) ) {
280 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
281 position.setX(atof(cursor));
284 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
285 position.setY(atof(cursor));
288 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
289 position.setZ(atof(cursor));
292 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
293 position.setT(atof(cursor));
294 data->set_position( position );
298 DEBUG( 10,
"ReaderAscii: V: "<<
id<<
" with "<<data->particles_in().size()<<
" particles)" )
309 const char *cursor = buf;
313 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
315 if( atoi(cursor) != (int)evt.
particles().size() + 1 ) {
317 ERROR(
"ReaderAscii: particle ID mismatch" )
322 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
323 mother_id = atoi(cursor);
326 if( mother_id > 0 && mother_id <= (
int)evt.
particles().size() ) {
333 vertex = make_shared<GenVertex>();
334 vertex->add_particle_in(mother);
337 vertex->add_particle_out(data);
340 else if( mother_id < 0 && -mother_id <= (
int)evt.
vertices().size() ) {
341 evt.
vertices()[ (-mother_id)-1 ]->add_particle_out(data);
345 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
346 data->set_pid( atoi(cursor) );
349 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
350 momentum.setPx(atof(cursor));
353 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
354 momentum.setPy(atof(cursor));
357 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
358 momentum.setPz(atof(cursor));
361 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
362 momentum.setE(atof(cursor));
363 data->set_momentum(momentum);
366 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
367 data->set_generated_mass( atof(cursor) );
370 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
371 data->set_status( atoi(cursor) );
375 DEBUG( 10,
"ReaderAscii: P: "<<data->id()<<
" ( mother: "<<mother_id<<
", pid: "<<data->pid()<<
")" )
382 const char *cursor = buf;
383 const char *cursor2 = buf;
387 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
390 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
393 if( !(cursor2 = strchr(cursor,
' ')) )
return false;
394 sprintf(name,
"%.*s", (
int)(cursor2-cursor), cursor);
398 shared_ptr<Attribute> att =
407 const char *cursor = buf;
408 const char *cursor2 = buf;
411 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
414 if( !(cursor2 = strchr(cursor,
' ')) )
return false;
415 sprintf(name,
"%.*s", (
int)(cursor2-cursor), cursor);
419 shared_ptr<StringAttribute> att =
422 run_info()->add_attribute(
string(name), att);
430 const char *cursor = buf;
432 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
435 istringstream iss(
unescape(cursor));
436 vector<string> names;
438 while ( iss >> name ) names.push_back(name);
440 run_info()->set_weight_names(names);
447 const char *cursor = buf;
449 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
453 string::size_type pos = line.find(
"\n");
454 tool.
name = line.substr(0, pos);
455 line = line.substr(pos + 1);
456 pos = line.find(
"\n");
457 tool.
version = line.substr(0, pos);
459 run_info()->tools().push_back(tool);
468 ret.reserve(s.length());
469 for ( string::const_iterator it = s.begin(); it != s.end(); ++it ) {
485 if( !
m_file.is_open())
return;
const std::vector< GenParticlePtr > & particles() const
Get list of particles (const)
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
bool parse_weight_names(const char *buf)
Parse run-level weight names.
void add_vertex(GenVertexPtr v)
Add vertex.
bool failed()
Return status of the stream.
ReaderAscii(const std::string &filename)
Constructor.
void set_event_number(int num)
Set event number.
static std::string name(MomentumUnit u)
Get name of momentum unit.
bool read_event(GenEvent &evt)
Load event from file.
void set_units(Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit)
Change event units Converts event from current units to new ones.
std::pair< int, int > parse_event_information(GenEvent &evt, const char *buf)
Parse event.
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
void clear()
Remove contents of this event.
~ReaderAscii()
Destructor.
void close()
Close file stream.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Stores event-related information.
static MomentumUnit momentum_unit(const std::string &name)
Get momentum unit based on its name.
void setT(double tt)
Set time component of position/displacement.
void setX(double xx)
Set x-component of position/displacement.
void setZ(double zz)
Set z-component of position/displacement.
void add_attribute(const string &name, const shared_ptr< Attribute > &att, int id=0)
Add event attribute to event.
void add_particle(GenParticlePtr p)
Add particle.
MomentumUnit
Momentum units.
bool parse_run_attribute(const char *buf)
Parse run-level attribute.
void shift_position_to(const FourVector &newpos)
Shift position of all vertices in the event to op.
bool parse_vertex_information(GenEvent &evt, const char *buf)
Parse vertex.
bool parse_attribute(GenEvent &evt, const char *buf)
Parse attribute.
const std::vector< GenVertexPtr > & vertices() const
Get list of vertices (const)
bool parse_units(GenEvent &evt, const char *buf)
Parse units.
bool parse_tool(const char *buf)
Parse run-level tool information.
bool parse_weight_values(GenEvent &evt, const char *buf)
Parse weight value lines.
Definition of template class SmartPointer.
const Units::LengthUnit & length_unit() const
Get length unit.
static LengthUnit length_unit(const std::string &name)
Get length unit based on its name.
void setY(double yy)
Set y-component of position/displacement.
bool parse_particle_information(GenEvent &evt, const char *buf)
Parse particle.
std::ifstream m_file
Input file.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
const std::vector< double > & weights() const
Get event weight values as a vector.
Attribute that holds a string.
std::string unescape(const std::string s)
Unsecape '\' and ' ' characters in string.
LengthUnit
Position units.