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-2023 The HepMC collaboration (see AUTHORS for details)
5//
6/**
7 * @file WriterHEPEVT.cc
8 * @brief Implementation of \b class WriterHEPEVT
9 *
10 */
11#include <array>
12#include <cstdio> // sprintf
13#include <sstream>
14
15#include "HepMC3/Print.h"
16#include "HepMC3/WriterHEPEVT.h"
17namespace HepMC3
18{
19
20
21WriterHEPEVT::WriterHEPEVT(const std::string &filename,
22 std::shared_ptr<GenRunInfo> /*run*/): m_file(filename), m_stream(&m_file)
23{
24 HEPMC3_WARNING("WriterHEPEVT::WriterHEPEVT: HEPEVT format is outdated. Please use HepMC3 format instead.")
25 m_hepevt_interface.allocate_internal_storage();
26}
27
28WriterHEPEVT::WriterHEPEVT(std::ostream& stream,
29 std::shared_ptr<GenRunInfo> /*run*/): m_stream(&stream)
30{
31 HEPMC3_WARNING("WriterHEPEVT::WriterHEPEVT: HEPEVT format is outdated. Please use HepMC3 format instead.")
32 m_hepevt_interface.allocate_internal_storage();
33}
34
35WriterHEPEVT::WriterHEPEVT(std::shared_ptr<std::ostream> s_stream,
36 std::shared_ptr<GenRunInfo> /*run*/): m_shared_stream(s_stream), m_stream(s_stream.get())
37{
38 HEPMC3_WARNING("WriterHEPEVT::WriterHEPEVT: HEPEVT format is outdated. Please use HepMC3 format instead.")
39 m_hepevt_interface.allocate_internal_storage();
40}
41
42void WriterHEPEVT::write_hepevt_particle(int index, bool iflong)
43{
44 std::array<char, 512> buf;//Note: the format is fixed, so no reason for complicatied tratment
45 char* cursor = buf.data();
46 cursor += sprintf(cursor, "% 8i% 8i", m_hepevt_interface.status(index), m_hepevt_interface.id(index));
47 if (iflong)
48 {
49 cursor += sprintf(cursor, "% 8i% 8i", m_hepevt_interface.first_parent(index), m_hepevt_interface.last_parent(index));
50 cursor += sprintf(cursor, "% 8i% 8i", m_hepevt_interface.first_child(index), m_hepevt_interface.last_child(index));
51 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));
52 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));
53 }
54 else
55 {
56 cursor += sprintf(cursor, "% 8i% 8i", m_hepevt_interface.first_child(index), m_hepevt_interface.last_child(index));
57 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));
58 }
59 unsigned long length = cursor - buf.data();
60 m_stream->write(buf.data(), length);
61}
62
64{
65 std::array<char, 512> buf;//Note: the format is fixed, so no reason for complicatied tratment
66 char* cursor = buf.data();
67 cursor += sprintf(cursor, "E% 8i %8i\n", m_hepevt_interface.event_number(), m_hepevt_interface.number_entries());
68 unsigned long length = cursor - buf.data();
69 m_stream->write(buf.data(), length);
70}
71
73{
74 m_hepevt_interface.GenEvent_to_HEPEVT(&evt);
75 m_hepevt_interface.fix_daughters();
77 for ( int i = 1; i <= m_hepevt_interface.number_entries(); ++i ) write_hepevt_particle(i, get_vertices_positions_present());
79}
80
82{
83 auto* ofs = dynamic_cast<std::ofstream*>(m_stream);
84 if (ofs && !ofs->is_open()) return;
85 if (ofs) ofs->close();
86}
87
89{
90 return (bool)m_file.rdstate();
91}
92
93void WriterHEPEVT::set_vertices_positions_present(bool iflong) { if (iflong) m_options["vertices_positions_are_absent"] = ""; else m_options.erase("vertices_positions_are_absent"); }
94
95bool WriterHEPEVT::get_vertices_positions_present() const { return (m_options.count("vertices_positions_are_absent") == 0); }
96
97} // namespace HepMC3
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition Errors.h:27
Definition of static class Print.
Definition of class WriterHEPEVT.
Stores event-related information.
Definition GenEvent.h:41
HEPEVT_Wrapper_Template< 100000 > m_hepevt_interface
Templated HEPEVT interface.
bool failed() override
Get stream error state flag.
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...
void set_vertices_positions_present(bool iflong)
set flag if vertex positions are available. Effectively this adds or removes key "vertices_positions_...
virtual void write_hepevt_event_header()
Write event header to file.
int m_events_count
Events count. Needed to generate unique object name.
virtual void write_hepevt_particle(int index, bool iflong=true)
Write particle to file.
void close() override
Close file stream.
std::shared_ptr< std::ostream > m_shared_stream
Output temp. stream.
std::ofstream m_file
Output file.
WriterHEPEVT(const std::string &filename, std::shared_ptr< GenRunInfo > run=nullptr)
Default constructor.
void write_event(const GenEvent &evt) override
Write event to file.
std::ostream * m_stream
Output stream.
std::map< std::string, std::string > m_options
options
Definition Writer.h:59
HepMC3 main namespace.