HepMC3 event record library
WriterHEPEVT.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2021 The HepMC collaboration (see AUTHORS for details)
5 //
6 /**
7  * @file WriterHEPEVT.cc
8  * @brief Implementation of \b class WriterHEPEVT
9  *
10  */
11 #include <sstream>
12 #include <cstdio> // sprintf
13 #include "HepMC3/WriterHEPEVT.h"
14 #include "HepMC3/Print.h"
15 namespace HepMC3
16 {
17 
18 
19 WriterHEPEVT::WriterHEPEVT(const std::string &filename,
20  std::shared_ptr<GenRunInfo> run): m_file(filename), m_stream(&m_file), m_events_count(0)
21 {
22  HEPMC3_WARNING("WriterHEPEVT::WriterHEPEVT: HEPEVT format is outdated. Please use HepMC3 format instead.")
24 }
25 
26 WriterHEPEVT::WriterHEPEVT(std::ostream& stream,
27  std::shared_ptr<GenRunInfo> run): m_file(), m_stream(&stream), m_events_count(0)
28 {
29  HEPMC3_WARNING("WriterHEPEVT::WriterHEPEVT: HEPEVT format is outdated. Please use HepMC3 format instead.")
31 }
32 
33 WriterHEPEVT::WriterHEPEVT(std::shared_ptr<std::ostream> s_stream,
34  std::shared_ptr<GenRunInfo> run): m_file(), m_shared_stream(s_stream), m_stream(s_stream.get()), m_events_count(0)
35 {
36  HEPMC3_WARNING("WriterHEPEVT::WriterHEPEVT: HEPEVT format is outdated. Please use HepMC3 format instead.")
38 }
39 
40 void WriterHEPEVT::write_hepevt_particle(int index, bool iflong)
41 {
42  char buf[512];//Note: the format is fixed, so no reason for complicatied tratment
43  char* cursor = &(buf[0]);
44  cursor += sprintf(cursor, "% 8i% 8i", m_hepevt_interface.status(index), m_hepevt_interface.id(index));
45  if (iflong)
46  {
47  cursor += sprintf(cursor, "% 8i% 8i", m_hepevt_interface.first_parent(index), m_hepevt_interface.last_parent(index));
48  cursor += sprintf(cursor, "% 8i% 8i", m_hepevt_interface.first_child(index), m_hepevt_interface.last_child(index));
49  cursor += sprintf(cursor, "% 19.8E% 19.8E% 19.8E% 19.8E% 19.8E\n", m_hepevt_interface.px(index), m_hepevt_interface.py(index), m_hepevt_interface.pz(index), m_hepevt_interface.e(index), m_hepevt_interface.m(index));
50  cursor += sprintf(cursor, "%-48s% 19.8E% 19.8E% 19.8E% 19.8E\n", " ", m_hepevt_interface.x(index), m_hepevt_interface.y(index), m_hepevt_interface.z(index), m_hepevt_interface.t(index));
51  }
52  else
53  {
54  cursor += sprintf(cursor, "% 8i% 8i", m_hepevt_interface.first_child(index), m_hepevt_interface.last_child(index));
55  cursor += sprintf(cursor, "% 19.8E% 19.8E% 19.8E% 19.8E\n", m_hepevt_interface.px(index), m_hepevt_interface.py(index), m_hepevt_interface.pz(index), m_hepevt_interface.m(index));
56  }
57  unsigned long length = cursor - &(buf[0]);
58  m_stream->write(buf, length);
59 }
60 
62 {
63  char buf[512];//Note: the format is fixed, so no reason for complicatied tratment
64  char* cursor = buf;
65  cursor += sprintf(cursor, "E% 8i %8i\n", m_hepevt_interface.event_number(), m_hepevt_interface.number_entries());
66  unsigned long length = cursor - &(buf[0]);
67  m_stream->write(buf, length);
68 }
69 
71 {
77 }
78 
80 {
81  std::ofstream* ofs = dynamic_cast<std::ofstream*>(m_stream);
82  if (ofs && !ofs->is_open()) return;
83  if (ofs) ofs->close();
84 }
85 
87 {
88  return (bool)m_file.rdstate();
89 }
90 
91 void WriterHEPEVT::set_vertices_positions_present(bool iflong) { if (iflong) m_options["vertices_positions_are_absent"] = ""; else m_options.erase("vertices_positions_are_absent"); }
92 
93 bool WriterHEPEVT::get_vertices_positions_present() const { return (m_options.find("vertices_positions_are_absent") == m_options.end()); }
94 
95 } // namespace HepMC3
WriterHEPEVT(const std::string &filename, std::shared_ptr< GenRunInfo > run=nullptr)
Default constructor.
Definition: WriterHEPEVT.cc:19
int status(const int index) const
Get status code.
double z(const int index) const
Get Z Production vertex.
Definition of class WriterHEPEVT.
std::ofstream m_file
Output file.
Definition: WriterHEPEVT.h:84
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition: Errors.h:27
void allocate_internal_storage()
Allocates m_internal_storage storage in smart pointer to hold HEPEVT of fixed size.
int m_events_count
Events count. Needed to generate unique object name.
Definition: WriterHEPEVT.h:88
int id(const int index) const
Get PDG particle id.
int event_number() const
Get event number.
virtual void write_hepevt_particle(int index, bool iflong=true)
Write particle to file.
Definition: WriterHEPEVT.cc:40
int last_child(const int index) const
Get index of last daughter.
bool get_vertices_positions_present() const
get flag if vertex positions are available. The flag is deduced from m_options. If the m_options have...
Definition: WriterHEPEVT.cc:93
double x(const int index) const
Get X Production vertex.
std::ostream * m_stream
Output stream.
Definition: WriterHEPEVT.h:86
virtual void write_hepevt_event_header()
Write event header to file.
Definition: WriterHEPEVT.cc:61
Stores event-related information.
Definition: GenEvent.h:41
void close() override
Close file stream.
Definition: WriterHEPEVT.cc:79
double e(const int index) const
Get Energy.
double m(const int index) const
Get generated mass.
void write_event(const GenEvent &evt) override
Write event to file.
Definition: WriterHEPEVT.cc:70
bool GenEvent_to_HEPEVT(const GenEvent *evt)
Convert GenEvent to HEPEVT.
HEPEVT_Wrapper_Template< 100000 > m_hepevt_interface
Templated HEPEVT interface.
Definition: WriterHEPEVT.h:89
double py(const int index) const
Get Y momentum.
double pz(const int index) const
Get Z momentum.
void set_vertices_positions_present(bool iflong)
set flag if vertex positions are available. Effectively this adds or removes key &quot;vertices_positions_...
Definition: WriterHEPEVT.cc:91
int last_parent(const int index) const
Get index of last mother.
bool fix_daughters()
Tries to fix list of daughters.
bool failed() override
Get stream error state flag.
Definition: WriterHEPEVT.cc:86
double y(const int index) const
Get Y Production vertex.
int first_child(const int index) const
Get index of 1st daughter.
int number_entries() const
Get number of entries.
std::map< std::string, std::string > m_options
options
Definition: Writer.h:68
double t(const int index) const
Get production time.
double px(const int index) const
Get X momentum.
int first_parent(const int index) const
Get index of 1st mother.