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-2020 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/HEPEVT_Wrapper.h"
15 #include "HepMC3/Print.h"
16 namespace HepMC3
17 {
18 
19 
20 WriterHEPEVT::WriterHEPEVT(const std::string &filename): 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.")
23  hepevtbuffer = (char*)(new struct HEPEVT());
25 }
26 
27 WriterHEPEVT::WriterHEPEVT(std::ostream& stream): m_file(), m_stream(&stream), m_events_count(0)
28 {
29  HEPMC3_WARNING("WriterHEPEVT::WriterHEPEVT: HEPEVT format is outdated. Please use HepMC3 format instead.")
30  hepevtbuffer = (char*)(new struct HEPEVT());
32 }
33 
34 void WriterHEPEVT::write_hepevt_particle(int index, bool iflong)
35 {
36  char buf[512];//Note: the format is fixed, so no reason for complicatied tratment
37  char* cursor = &(buf[0]);
38  cursor += sprintf(cursor, "% 8i% 8i", HEPEVT_Wrapper::status(index), HEPEVT_Wrapper::id(index));
39  if (iflong)
40  {
41  cursor += sprintf(cursor, "% 8i% 8i", HEPEVT_Wrapper::first_parent(index), HEPEVT_Wrapper::last_parent(index));
42  cursor += sprintf(cursor, "% 8i% 8i", HEPEVT_Wrapper::first_child(index), HEPEVT_Wrapper::last_child(index));
43  cursor += sprintf(cursor, "% 19.8E% 19.8E% 19.8E% 19.8E% 19.8E\n", HEPEVT_Wrapper::px(index), HEPEVT_Wrapper::py(index), HEPEVT_Wrapper::pz(index), HEPEVT_Wrapper::e(index), HEPEVT_Wrapper::m(index));
44  cursor += sprintf(cursor, "%-48s% 19.8E% 19.8E% 19.8E% 19.8E\n", " ", HEPEVT_Wrapper::x(index), HEPEVT_Wrapper::y(index), HEPEVT_Wrapper::z(index), HEPEVT_Wrapper::t(index));
45  }
46  else
47  {
48  cursor += sprintf(cursor, "% 8i% 8i", HEPEVT_Wrapper::first_child(index), HEPEVT_Wrapper::last_child(index));
49  cursor += sprintf(cursor, "% 19.8E% 19.8E% 19.8E% 19.8E\n", HEPEVT_Wrapper::px(index), HEPEVT_Wrapper::py(index), HEPEVT_Wrapper::pz(index), HEPEVT_Wrapper::m(index));
50  }
51  unsigned long length = cursor - &(buf[0]);
52  m_stream->write(buf, length);
53 }
54 
56 {
57  char buf[512];//Note: the format is fixed, so no reason for complicatied tratment
58  char* cursor = buf;
59  cursor += sprintf(cursor, "E% 8i %8i\n", HEPEVT_Wrapper::event_number(), HEPEVT_Wrapper::number_entries());
60  unsigned long length = cursor - &(buf[0]);
61  m_stream->write(buf, length);
62 }
63 
65 {
71 }
72 
74 {
75  std::ofstream* ofs = dynamic_cast<std::ofstream*>(m_stream);
76  if (ofs && !ofs->is_open()) return;
77  if (ofs) ofs->close();
78 }
79 
81 {
82  return (bool)m_file.rdstate();
83 }
84 
85 void WriterHEPEVT::set_vertices_positions_present(bool iflong) { if (iflong) m_options["vertices_positions_are_absent"] = ""; else m_options.erase("vertices_positions_are_absent"); }
86 
87 bool WriterHEPEVT::get_vertices_positions_present() const { return (m_options.find("vertices_positions_are_absent") == m_options.end()); }
88 
89 } // namespace HepMC3
HepMC3::HEPEVT_Wrapper::fix_daughters
static bool fix_daughters()
Tries to fix list of daughters.
Definition: HEPEVT_Wrapper.h:261
HepMC3::WriterHEPEVT::failed
bool failed() override
Get stream error state flag.
Definition: WriterHEPEVT.cc:80
HepMC3::WriterHEPEVT::set_vertices_positions_present
void set_vertices_positions_present(bool iflong)
set flag if vertex positions are available. Effectively this adds or removes key "vertices_positions_...
Definition: WriterHEPEVT.cc:85
HepMC3::HEPEVT_Wrapper::m
static double m(const int &index)
Get generated mass.
Definition: HEPEVT_Wrapper.h:143
HepMC3::WriterHEPEVT::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition: WriterHEPEVT.cc:64
HepMC3::GenEvent
Stores event-related information.
Definition: GenEvent.h:41
HepMC3::HEPEVT_Wrapper::status
static int status(const int &index)
Get status code.
Definition: HEPEVT_Wrapper.h:133
HepMC3::HEPEVT_Wrapper::x
static double x(const int &index)
Get X Production vertex.
Definition: HEPEVT_Wrapper.h:144
HepMC3
HepMC3 main namespace.
Definition: AnalysisExample.h:19
HEPEVT_Wrapper.h
Definition of class HEPEVT_Wrapper.
HepMC3::HEPEVT_Wrapper::py
static double py(const int &index)
Get Y momentum.
Definition: HEPEVT_Wrapper.h:140
HEPEVT
Fortran common block HEPEVT.
Definition: HEPEVT_Wrapper.h:54
HepMC3::HEPEVT_Wrapper::number_entries
static int number_entries()
Get number of entries.
Definition: HEPEVT_Wrapper.h:132
HepMC3::HEPEVT_Wrapper::z
static double z(const int &index)
Get Z Production vertex.
Definition: HEPEVT_Wrapper.h:146
HepMC3::WriterHEPEVT::hepevtbuffer
char * hepevtbuffer
Pointer to HEPEVT Fortran common block/C struct.
Definition: WriterHEPEVT.h:81
HepMC3::HEPEVT_Wrapper::e
static double e(const int &index)
Get Energy.
Definition: HEPEVT_Wrapper.h:142
HepMC3::Writer::m_options
std::map< std::string, std::string > m_options
options
Definition: Writer.h:68
HepMC3::HEPEVT_Wrapper::last_parent
static int last_parent(const int &index)
Get index of last mother.
Definition: HEPEVT_Wrapper.h:136
WriterHEPEVT.h
Definition of class WriterHEPEVT.
HepMC3::HEPEVT_Wrapper::first_child
static int first_child(const int &index)
Get index of 1st daughter.
Definition: HEPEVT_Wrapper.h:137
HepMC3::HEPEVT_Wrapper::y
static double y(const int &index)
Get Y Production vertex.
Definition: HEPEVT_Wrapper.h:145
HepMC3::HEPEVT_Wrapper::px
static double px(const int &index)
Get X momentum.
Definition: HEPEVT_Wrapper.h:139
HepMC3::WriterHEPEVT::m_events_count
int m_events_count
Events count. Needed to generate unique object name.
Definition: WriterHEPEVT.h:82
Print.h
Definition of static class Print.
HepMC3::WriterHEPEVT::WriterHEPEVT
WriterHEPEVT(const std::string &filename)
Default constructor.
Definition: WriterHEPEVT.cc:20
HepMC3::WriterHEPEVT::write_hepevt_particle
virtual void write_hepevt_particle(int index, bool iflong=true)
Write particle to file.
Definition: WriterHEPEVT.cc:34
HepMC3::HEPEVT_Wrapper::last_child
static int last_child(const int &index)
Get index of last daughter.
Definition: HEPEVT_Wrapper.h:138
HepMC3::HEPEVT_Wrapper::pz
static double pz(const int &index)
Get Z momentum.
Definition: HEPEVT_Wrapper.h:141
HepMC3::WriterHEPEVT::m_file
std::ofstream m_file
Output file.
Definition: WriterHEPEVT.h:79
HepMC3::WriterHEPEVT::get_vertices_positions_present
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:87
HepMC3::HEPEVT_Wrapper::event_number
static int event_number()
Get event number.
Definition: HEPEVT_Wrapper.h:131
HepMC3::HEPEVT_Wrapper::id
static int id(const int &index)
Get PDG particle id.
Definition: HEPEVT_Wrapper.h:134
HepMC3::HEPEVT_Wrapper::set_hepevt_address
static void set_hepevt_address(char *c)
Set Fortran block address.
Definition: HEPEVT_Wrapper.h:129
HEPMC3_WARNING
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition: Errors.h:27
HepMC3::HEPEVT_Wrapper::GenEvent_to_HEPEVT
static bool GenEvent_to_HEPEVT(const GenEvent *evt)
Convert GenEvent to HEPEVT.
Definition: HEPEVT_Wrapper.cc:176
HepMC3::HEPEVT_Wrapper::first_parent
static int first_parent(const int &index)
Get index of 1st mother.
Definition: HEPEVT_Wrapper.h:135
HepMC3::WriterHEPEVT::m_stream
std::ostream * m_stream
Output stream.
Definition: WriterHEPEVT.h:80
HepMC3::WriterHEPEVT::close
void close() override
Close file stream.
Definition: WriterHEPEVT.cc:73
HepMC3::HEPEVT_Wrapper::t
static double t(const int &index)
Get production time.
Definition: HEPEVT_Wrapper.h:147
HepMC3::WriterHEPEVT::write_hepevt_event_header
virtual void write_hepevt_event_header()
Write event header to file.
Definition: WriterHEPEVT.cc:55