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