28        HEPMC3_ERROR(
"ReaderAscii: could not open input file: " << filename)
 
 
   37        HEPMC3_ERROR(
"ReaderAscii: could not open input stream ")
 
 
   47        HEPMC3_ERROR(
"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        if ( strlen(buf.data()) == 0 ) 
continue;
 
  115        if ( strncmp(buf.data(), 
"HepMC", 5) == 0 ) {
 
  116            if ( strncmp(buf.data(), 
"HepMC::Version", 14) != 0 && strncmp(buf.data(), 
"HepMC::Asciiv3", 14) != 0 )
 
  118                HEPMC3_WARNING(
"ReaderAscii: found unsupported expression in header. Will close the input.")
 
  119                std::cout << buf.data() << std::endl;
 
  123                is_parsing_successful = 
true;
 
  132            if (vertices_and_particles.second < 0) {
 
  133                is_parsing_successful = 
false;
 
  135                is_parsing_successful = 
true;
 
  136                event_context   = 
true;
 
  137                parsed_weights = 
false;
 
  138                parsed_particles_or_vertices = 
false;
 
  140            run_info_context   = 
false;
 
  144            parsed_particles_or_vertices =  
true;
 
  148            parsed_particles_or_vertices =  
true;
 
  151            if ( event_context ) {
 
  155                if ( !run_info_context ) {
 
  159                run_info_context = 
true;
 
  164            is_parsing_successful = 
parse_units(evt, buf.data());
 
  167            if ( event_context ) {
 
  170                if ( !run_info_context ) {
 
  174                run_info_context = 
true;
 
  175                is_parsing_successful = 
parse_tool(buf.data());
 
  179            if ( event_context ) {
 
  182                if ( !run_info_context ) {
 
  186                run_info_context = 
true;
 
  191            HEPMC3_WARNING(
"ReaderAscii: skipping unrecognised prefix: " << buf[0])
 
  192            is_parsing_successful = 
true;
 
  196        if ( !is_parsing_successful ) 
break;
 
  201        if ( event_context &&  peek == 
'E' ) 
break;
 
  204        if ( event_context &&  peek == 
'W' && parsed_weights  ) 
break;
 
  207        if ( event_context &&  peek == 
'A' && parsed_particles_or_vertices  ) 
break;
 
  210        if ( event_context &&  peek == 
'T' ) 
break;
 
  217    if ((
int)evt.
particles().size() > vertices_and_particles.second) {
 
  218        HEPMC3_ERROR(
"ReaderAscii: too many particles were parsed")
 
  219        printf(
"%zu  vs  %i expected\n", evt.
particles().size(), vertices_and_particles.second);
 
  220        is_parsing_successful = 
false;
 
  222    if ((
int)evt.
particles().size() < vertices_and_particles.second) {
 
  223        HEPMC3_ERROR(
"ReaderAscii: too few  particles were parsed")
 
  224        printf(
"%zu  vs  %i expected\n", evt.
particles().size(), vertices_and_particles.second);
 
  225        is_parsing_successful = 
false;
 
  228    if ((
int)evt.
vertices().size()  > vertices_and_particles.first) {
 
  229        HEPMC3_ERROR(
"ReaderAscii: too many vertices were parsed")
 
  230        printf(
"%zu  vs  %i expected\n", evt.
vertices().size(), vertices_and_particles.first);
 
  231        is_parsing_successful =  
false;
 
  234    if ((
int)evt.
vertices().size()  < vertices_and_particles.first) {
 
  235        HEPMC3_ERROR(
"ReaderAscii: too few vertices were parsed")
 
  236        printf(
"%zu  vs  %i expected\n", evt.
vertices().size(), vertices_and_particles.first);
 
  237        is_parsing_successful =  
false;
 
  240    if ( !is_parsing_successful ) {
 
  241        HEPMC3_ERROR(
"ReaderAscii: event parsing failed. Returning empty event")
 
  242        HEPMC3_DEBUG(1, 
"Parsing failed at line:" << std::endl << buf.data())
 
  251        for (
const auto& v: evt.
vertices()) {
 
  252            if (p.second == v->id()) {
 
  253                v->add_particle_out(p.first);
 
  257    for ( 
const auto& v : 
m_forward_mothers )  
for (
const auto& idpm : v.second )  v.first->add_particle_in(evt.
particles()[idpm-1]);
 
  260    std::vector<int> all_ids;
 
  261    all_ids.reserve(evt.
vertices().size());
 
  262    std::vector<int> filled_ids;
 
  263    filled_ids.reserve(evt.
vertices().size());
 
  264    std::vector<int> diff;
 
  265    diff.reserve(evt.
vertices().size());
 
  266    for (
const auto& v: evt.
vertices()) 
if (v->id() != 0) filled_ids.emplace_back(v->id());
 
  267    for (
int i = -((
long)evt.
vertices().size()); i < 0; i++) all_ids.emplace_back(i);
 
  268    std::sort(all_ids.begin(), all_ids.end());
 
  269    std::sort(filled_ids.begin(), filled_ids.end());
 
  271    std::set_difference(all_ids.begin(), all_ids.end(), filled_ids.begin(), filled_ids.end(), std::inserter(diff, diff.begin()));
 
  272    auto it = diff.rbegin();
 
  274    for (
const auto& v: evt.
vertices()) 
if (v->id() == 0) { v->set_id(*it); it++;}
 
 
  281    static const std::pair<int, int>  err(-1, -1);
 
  282    std::pair<int, int>               ret(-1, -1);
 
  283    const char                 *cursor   = buf;
 
  288    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return err;
 
  289    event_no = atoi(cursor);
 
  293    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return err;
 
  294    ret.first = atoi(cursor);
 
  297    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return err;
 
  298    ret.second = atoi(cursor);
 
  301    if ( (cursor = strchr(cursor+1, 
'@')) ) {
 
  303        if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return err;
 
  304        position.
setX(atof(cursor));
 
  307        if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return err;
 
  308        position.
setY(atof(cursor));
 
  311        if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return err;
 
  312        position.
setZ(atof(cursor));
 
  315        if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return err;
 
  316        position.
setT(atof(cursor));
 
  320    HEPMC3_DEBUG(10, 
"ReaderAscii: E: " << event_no << 
" (" <<ret.first << 
"V, " << ret.second << 
"P)")
 
 
  327    std::istringstream iss(buf + 1);
 
  328    std::vector<double> wts;
 
  330    while (iss >> w) wts.emplace_back(w);
 
  332            && 
run_info()->weight_names().size() != wts.size() ) {
 
  333        throw std::logic_error(
"ReaderAscii::parse_weight_values: " 
  334                               "The number of weights ("+std::to_string((
long long int)(wts.size()))+
") does not match " 
  335                               "the  number weight names("+std::to_string((
long long int)(
run_info()->weight_names().size()))+
") in the GenRunInfo object");
 
 
  344    const char *cursor = buf;
 
  347    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  352    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  356    evt.
set_units(momentum_unit, length_unit);
 
 
  365    GenVertexPtr  data = std::make_shared<GenVertex>();
 
  367    const char   *cursor          = buf;
 
  368    const char   *cursor2         = 
nullptr;
 
  373    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  377    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  378    data->set_status(atoi(cursor));
 
  381    if ( !(cursor = strchr(cursor+1, 
'[')) ) 
return false;
 
  386        int  particle_in = atoi(cursor);
 
  389        if (particle_in > 0) {
 
  391            if (particle_in <= highest_id) {
 
  392                data->add_particle_in(evt.
particles()[particle_in-1]);
 
  400        if ( !(cursor = strchr(cursor+1, 
',')) ) {
 
  401            if ( !(cursor = strchr(cursor2+1, 
']')) ) 
return false;
 
  407    if ( (cursor = strchr(cursor+1, 
'@')) ) {
 
  409        if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  410        position.
setX(atof(cursor));
 
  413        if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  414        position.
setY(atof(cursor));
 
  417        if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  418        position.
setZ(atof(cursor));
 
  421        if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  422        position.
setT(atof(cursor));
 
  423        data->set_position(position);
 
  426    HEPMC3_DEBUG(10, 
"ReaderAscii: V: " << 
id << 
" with  "<< data->particles_in().size() << 
" particles)")
 
 
  437    GenParticlePtr  data = std::make_shared<GenParticle>();
 
  439    const char     *cursor  = buf;
 
  443    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  445    if ( atoi(cursor) != (
int)evt.
particles().size() + 1 ) {
 
  452    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  453    mother_id = atoi(cursor);
 
  456    if ( mother_id > 0 && mother_id <= (
int)evt.
particles().size() ) {
 
  457        GenParticlePtr mother = evt.
particles()[ mother_id-1 ];
 
  458        GenVertexPtr   vertex = mother->end_vertex();
 
  462            vertex = std::make_shared<GenVertex>();
 
  463            vertex->add_particle_in(mother);
 
  466        vertex->add_particle_out(data);
 
  477            for (
auto v: evt.
vertices()) 
if (v->id() == mother_id) {v->add_particle_out(data); found = 
true; 
break; }
 
  488    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  489    data->set_pid(atoi(cursor));
 
  492    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  493    momentum.
setPx(atof(cursor));
 
  496    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  497    momentum.
setPy(atof(cursor));
 
  500    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  501    momentum.
setPz(atof(cursor));
 
  504    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  505    momentum.
setE(atof(cursor));
 
  506    data->set_momentum(momentum);
 
  509    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  510    data->set_generated_mass(atof(cursor));
 
  513    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  514    data->set_status(atoi(cursor));
 
  518    HEPMC3_DEBUG(10, 
"ReaderAscii: P: " << data->id() << 
" ( mother: " << mother_id << 
", pid: " << data->pid() << 
")")
 
 
  525    const char     *cursor  = buf;
 
  526    const char     *cursor2 = buf;
 
  527    std::array<char, 512> name;
 
  530    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  533    if ( !(cursor  = strchr(cursor+1, 
' ')) ) 
return false;
 
  536    if ( !(cursor2 = strchr(cursor, 
' ')) ) 
return false;
 
  537    snprintf(name.data(), name.size(), 
"%.*s", (
int)(cursor2-cursor), cursor);
 
  541    std::shared_ptr<Attribute> att =
 
 
  550    const char     *cursor  = buf;
 
  551    const char     *cursor2 = buf;
 
  552    std::array<char, 512> name;
 
  554    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  557    if ( !(cursor2 = strchr(cursor, 
' ')) ) 
return false;
 
  558    snprintf(name.data(), name.size(), 
"%.*s", (
int)(cursor2-cursor), cursor);
 
  562    std::shared_ptr<StringAttribute> att =
 
  565    run_info()->add_attribute(std::string(name.data()), att);
 
 
  572    const char     *cursor  = buf;
 
  574    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  577    std::istringstream iss(
unescape(cursor));
 
  578    std::vector<std::string> names;
 
  580    while (iss >> name) names.emplace_back(name);
 
  582    run_info()->set_weight_names(names);
 
 
  588    const char     *cursor  = buf;
 
  590    if ( !(cursor = strchr(cursor+1, 
' ')) ) 
return false;
 
  592    std::string line = 
unescape(cursor);
 
  594    std::string::size_type pos = line.find(
'\n');
 
  595    tool.
name = line.substr(0, pos);
 
  596    line = line.substr(pos + 1);
 
  597    pos = line.find(
'\n');
 
  598    tool.
version = line.substr(0, pos);
 
  600    run_info()->tools().emplace_back(tool);
 
 
  608    ret.reserve(s.length());
 
  609    for ( std::string::const_iterator it = s.begin(); it != s.end(); ++it ) {
 
 
  628    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)
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
const Units::LengthUnit & length_unit() const
Get length unit.
void add_attribute(const std::string &name, const std::shared_ptr< Attribute > &att, const int &id=0)
const std::vector< double > & weights() const
Get event weight values as a vector.
void clear()
Remove contents of this event.
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
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 stream.
std::shared_ptr< std::istream > m_shared_stream
For ctor when reading from temp. stream.
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.
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.
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.