HepMC3 event record library
ReaderLHEF.cc
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
5//
6/**
7 * @file ReaderLHEF.cc
8 * @brief Implementation of \b class ReaderLHEF
9 *
10 */
11#include "HepMC3/ReaderLHEF.h"
12using namespace LHEF;
13namespace HepMC3
14{
15ReaderLHEF::ReaderLHEF(const std::string& filename)
16{
17 m_reader = new LHEF::Reader(filename);
18 init();
19}
20ReaderLHEF::ReaderLHEF(std::istream & stream)
21{
22 m_reader = new LHEF::Reader(stream);
23 init();
24}
25
27{
28 m_neve=0;
29 m_failed=false;
30 // Create a HEPRUP attribute and initialize it from the reader.
31 m_hepr = make_shared<HEPRUPAttribute>();
32 m_hepr->heprup = m_reader->heprup;
33
34 // There may be some XML tags in the LHE file which are
35 // non-standard, but we can save them as well.
37
38 // Nowwe want to create a GenRunInfo object for the HepMC file, and
39 // we add the LHEF attribute to that.
40 set_run_info(make_shared<GenRunInfo>());
41 run_info()->add_attribute("HEPRUP", m_hepr);
42
43 // This is just a test to make sure we can add other attributes as
44 // well.
45 run_info()->add_attribute("NPRUP",
46 make_shared<FloatAttribute>(m_hepr->heprup.NPRUP));
47
48 // We want to be able to convey the different event weights to
49 // HepMC. In particular we need to add the names of the weights to
50 // the GenRunInfo object.
51 std::vector<std::string> weightnames;
52 weightnames.push_back("0"); // The first weight is always the
53 // default weight with name "0".
54 for ( int i = 0, N = m_hepr->heprup.weightinfo.size(); i < N; ++i )
55 weightnames.push_back(m_hepr->heprup.weightNameHepMC(i));
56 run_info()->set_weight_names(weightnames);
57
58 // We also want to convey the information about which generators was
59 // used.
60 for ( int i = 0, N = m_hepr->heprup.generators.size(); i < N; ++i )
61 {
63 tool.name = m_hepr->heprup.generators[i].name;
64 tool.version = m_hepr->heprup.generators[i].version;
65 tool.description = m_hepr->heprup.generators[i].contents;
66 run_info()->tools().push_back(tool);
67 }
68}
69/// @brief Destructor
71
73{
75 if (m_failed) return m_failed;
76 // To each GenEvent we want to add an attribute corresponding to
77 // the HEPEUP. Also here there may be additional non-standard
78 // information outside the LHEF <event> tags, which we may want to
79 // add.
80 shared_ptr<HEPEUPAttribute> hepe = make_shared<HEPEUPAttribute>();
81 if ( m_reader->outsideBlock.length() )
83 hepe->hepeup = m_reader->hepeup;
85 m_neve++;
86 // This is just a text to check that we can add additional
87 // attributes to each event.
88 ev.add_attribute("HEPEUP", hepe);
89 ev.add_attribute("AlphaQCD",
90 make_shared<DoubleAttribute>(hepe->hepeup.AQCDUP));
91 ev.add_attribute("AlphaEM",
92 make_shared<DoubleAttribute>(hepe->hepeup.AQEDUP));
93 ev.add_attribute("NUP",
94 make_shared<IntAttribute>(hepe->hepeup.NUP));
95 ev.add_attribute("IDPRUP",
96 make_shared<LongAttribute>(hepe->hepeup.IDPRUP));
97
98 // Now add the Particles from the LHE event to HepMC
99 GenParticlePtr p1 = make_shared<GenParticle>(hepe->momentum(0),
100 hepe->hepeup.IDUP[0],
101 hepe->hepeup.ISTUP[0]);
102 GenParticlePtr p2 = make_shared<GenParticle>(hepe->momentum(1),
103 hepe->hepeup.IDUP[1],
104 hepe->hepeup.ISTUP[1]);
105 GenVertexPtr vx = make_shared<GenVertex>();
106 vx->add_particle_in(p1);
107 vx->add_particle_in(p2);
108
109 for ( int i = 2; i < hepe->hepeup.NUP; ++i )
110 vx->add_particle_out(make_shared<GenParticle>
111 (hepe->momentum(i),
112 hepe->hepeup.IDUP[i],
113 hepe->hepeup.ISTUP[i]));
114 ev.add_vertex(vx);
115 // And we also want to add the weights.
116 std::vector<double> wts;
117 for ( int i = 0, N = hepe->hepeup.weights.size(); i < N; ++i )
118 wts.push_back(hepe->hepeup.weights[i].first);
119 ev.weights() = wts;
120 return m_failed;
121}
122/// @brief Return status of the stream
124
125/// @brief Close file stream
126void ReaderLHEF::close() { delete m_reader; };
127} // namespace HepMC3
128
129
Definition of class ReaderLHEF.
Stores event-related information.
Definition GenEvent.h:42
void add_vertex(GenVertexPtr v)
Add vertex.
Definition GenEvent.cc:98
void add_attribute(const string &name, const shared_ptr< Attribute > &att, const int &id=0)
Add event attribute to event.
Definition GenEvent.h:209
void set_event_number(const int &num)
Set event number.
Definition GenEvent.h:138
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition GenEvent.h:87
~ReaderLHEF()
Destructor.
Definition ReaderLHEF.cc:70
void init()
Init helper.
Definition ReaderLHEF.cc:26
LHEF::Reader * m_reader
The actual reader.
Definition ReaderLHEF.h:53
void close()
Close.
bool read_event(GenEvent &ev)
Reading event.
Definition ReaderLHEF.cc:72
shared_ptr< HEPRUPAttribute > m_hepr
Holder of attributes.
Definition ReaderLHEF.h:54
bool m_failed
State of reader.
Definition ReaderLHEF.h:56
bool failed()
State.
ReaderLHEF(std::istream &)
The ctor to read from stream.
Definition ReaderLHEF.cc:20
int m_neve
Event counter.
Definition ReaderLHEF.h:55
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition Reader.h:46
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition Reader.h:39
std::string outsideBlock
Definition LHEF.h:3002
HEPEUP hepeup
Definition LHEF.h:3022
HEPRUP heprup
Definition LHEF.h:3012
bool readEvent()
Definition LHEF.h:2868
std::string headerBlock
Definition LHEF.h:3007
std::string initComments
Definition LHEF.h:3017
HepMC3 main namespace.
Definition ReaderGZ.h:28
Les Houches event file classes.
Definition LHEF.h:39
Interrnal struct for keeping track of tools.
Definition GenRunInfo.h:37
string description
Other information about how the tool was used in the run.
Definition GenRunInfo.h:47
string name
The name of the tool.
Definition GenRunInfo.h:40
string version
The version of the tool.
Definition GenRunInfo.h:43
static std::vector< XMLTag * > findXMLTags(std::string str, std::string *leftover=0)
Definition LHEF.h:198