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