HepMC3 event record library: WriterHEPEVT.cc Source File

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_LEVEL(900,"WriterHEPEVT::WriterHEPEVT: HEPEVT format is outdated. Please use HepMC3 format instead.")
25 m_hepevt_interface.allocate_internal_storage();
26}
27
28 WriterHEPEVT::WriterHEPEVT(std::ostream& stream,
29 std::shared_ptr<GenRunInfo> /*run*/): m_stream(&stream)
30{
31 HEPMC3_WARNING_LEVEL(900,"WriterHEPEVT::WriterHEPEVT: HEPEVT format is outdated. Please use HepMC3 format instead.")
32 m_hepevt_interface.allocate_internal_storage();
33}
34
35 WriterHEPEVT::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_LEVEL(900,"WriterHEPEVT::WriterHEPEVT: HEPEVT format is outdated. Please use HepMC3 format instead.")
39 m_hepevt_interface.allocate_internal_storage();
40}
41
42 void 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
63 void WriterHEPEVT::write_hepevt_event_header()
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
72 void WriterHEPEVT::write_event(const GenEvent &evt)
73{
74 m_hepevt_interface.GenEvent_to_HEPEVT(&evt);
75 m_hepevt_interface.fix_daughters();
76 write_hepevt_event_header();
77 for ( int i = 1; i <= m_hepevt_interface.number_entries(); ++i ) write_hepevt_particle(i, get_vertices_positions_present());
78 m_events_count++;
79}
80
81 void WriterHEPEVT::close()
82{
83 auto* ofs = dynamic_cast<std::ofstream*>(m_stream);
84 if (ofs && !ofs->is_open()) return;
85 if (ofs) ofs->close();
86}
87
88 bool WriterHEPEVT::failed()
89{
90 return static_cast<bool>(m_file.rdstate());
91}
92
93 void WriterHEPEVT::set_vertices_positions_present(bool iflong) { if (iflong) m_options["vertices_positions_are_absent"] = ""; else m_options.erase("vertices_positions_are_absent"); }
94
95 bool WriterHEPEVT::get_vertices_positions_present() const { return (m_options.count("vertices_positions_are_absent") == 0); }
96
97} // namespace HepMC3
HEPMC3_WARNING_LEVEL
#define HEPMC3_WARNING_LEVEL(LEVEL, MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition Errors.h:34
Print.h
Definition of static class Print.
WriterHEPEVT.h
Definition of class WriterHEPEVT.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::WriterHEPEVT::m_hepevt_interface
HEPEVT_Wrapper_Template< 100000 > m_hepevt_interface
Templated HEPEVT interface.
Definition WriterHEPEVT.h:89
HepMC3::WriterHEPEVT::failed
bool failed() override
Get stream error state flag.
Definition WriterHEPEVT.cc:88
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:95
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:93
HepMC3::WriterHEPEVT::write_hepevt_event_header
virtual void write_hepevt_event_header()
Write event header to file.
Definition WriterHEPEVT.cc:63
HepMC3::WriterHEPEVT::m_events_count
int m_events_count
Events count. Needed to generate unique object name.
Definition WriterHEPEVT.h:88
HepMC3::WriterHEPEVT::write_hepevt_particle
virtual void write_hepevt_particle(int index, bool iflong=true)
Write particle to file.
Definition WriterHEPEVT.cc:42
HepMC3::WriterHEPEVT::close
void close() override
Close file stream.
Definition WriterHEPEVT.cc:81
HepMC3::WriterHEPEVT::m_shared_stream
std::shared_ptr< std::ostream > m_shared_stream
Output temp. stream.
Definition WriterHEPEVT.h:85
HepMC3::WriterHEPEVT::m_file
std::ofstream m_file
Output file.
Definition WriterHEPEVT.h:84
HepMC3::WriterHEPEVT::WriterHEPEVT
WriterHEPEVT(const std::string &filename, std::shared_ptr< GenRunInfo > run=nullptr)
Default constructor.
Definition WriterHEPEVT.cc:21
HepMC3::WriterHEPEVT::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition WriterHEPEVT.cc:72
HepMC3::WriterHEPEVT::m_stream
std::ostream * m_stream
Output stream.
Definition WriterHEPEVT.h:86
HepMC3::Writer::m_options
std::map< std::string, std::string > m_options
options
Definition Writer.h:59
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24