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