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 void ReaderLHEF::init()
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.
36  m_hepr->tags = XMLTag::findXMLTags(m_reader->headerBlock + m_reader->initComments);
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
70 ReaderLHEF::~ReaderLHEF() {close();};
71 
72 bool ReaderLHEF::read_event(GenEvent& ev)
73 {
74  m_failed=!(m_reader->readEvent());
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() )
82  hepe->tags = XMLTag::findXMLTags(m_reader->outsideBlock);
83  hepe->hepeup = m_reader->hepeup;
84  ev.set_event_number(m_neve);
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
123 bool ReaderLHEF::failed() { return m_failed;}
124 
125 /// @brief Close file stream
126 void ReaderLHEF::close() { delete m_reader; };
127 } // namespace HepMC3
128 
129 
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
Stores event-related information.
Definition: GenEvent.h:42
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:209
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition: GenEvent.h:87
string description
Other information about how the tool was used in the run.
Definition: GenRunInfo.h:47
Definition of class ReaderLHEF.
void set_event_number(const int &num)
Set event number.
Definition: GenEvent.h:138