HepMC3 event record library: WriterAscii.h Source File

HepMC3 event record library

WriterAscii.h
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#ifndef HEPMC3_WRITERASCII_H
7#define HEPMC3_WRITERASCII_H
8///
9/// @file WriterAscii.h
10/// @brief Definition of class \b WriterAscii
11///
12/// @class HepMC3::WriterAscii
13/// @brief GenEvent I/O serialization for structured text files
14///
15/// @ingroup IO
16///
17#include "HepMC3/Writer.h"
18#include "HepMC3/GenEvent.h"
19#include "HepMC3/GenRunInfo.h"
20#include <string>
21#include <fstream>
22
23namespace HepMC3 {
24
25 class WriterAscii : public Writer {
26public:
27
28 /// @brief Constructor
29 /// @warning If file already exists, it will be cleared before writing
30 WriterAscii(const std::string& filename,
31 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
32
33 /// @brief Constructor from ostream
34 WriterAscii(std::ostream& stream,
35 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
36 /// @brief Constructor from temp ostream
37 WriterAscii(std::shared_ptr<std::ostream> s_stream,
38 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
39
40 /// @brief Destructor
41 ~WriterAscii();
42
43 /// @brief Write event to file
44 ///
45 /// @param[in] evt Event to be serialized
46 void write_event(const GenEvent& evt) override;
47
48 /// @brief Write the GenRunInfo object to file.
49 void write_run_info();
50
51 /// @brief Return status of the stream
52 bool failed() override;
53
54 /// @brief Close file stream
55 void close() override;
56
57 /// @brief Set output precision
58 ///
59 /// So far available range is [2,24]. Default is 16.
60 void set_precision(const int& prec );
61 /// @brief Return output precision
62 int precision() const;
63private:
64
65 /// @name Buffer management
66 /// @{
67
68 /// @brief Attempts to allocate buffer of the chosen size
69 ///
70 /// This function can be called manually by the user or will be called
71 /// before first read/write operation
72 ///
73 /// @note If buffer size is too large it will be divided by 2 until it is
74 /// small enough for system to allocate
75 void allocate_buffer();
76
77 /// @brief Set buffer size (in bytes)
78 ///
79 /// Default is 256kb. Minimum is 256b.
80 /// Size can only be changed before first read/write operation.
81 void set_buffer_size(const size_t& size );
82
83 /// @brief Escape '\' and '\n' characters in string
84 static std::string escape(const std::string& s);
85
86 /// Inline function flushing buffer to output stream when close to buffer capacity
87 void flush();
88
89 /// Inline function forcing flush to the output stream
90 void forced_flush();
91
92 /// @}
93
94
95 /// @name Write helpers
96 /// @{
97
98 /// @brief Inline function for writing strings
99 ///
100 /// Since strings can be long (maybe even longer than buffer) they have to be dealt
101 /// with separately.
102 void write_string( const std::string &str );
103
104 /// @brief Write vertex
105 ///
106 /// Helper routine for writing single vertex to file
107 void write_vertex(const ConstGenVertexPtr& v);
108
109 /// @brief Write particle
110 ///
111 /// Helper routine for writing single particle to file
112 void write_particle(const ConstGenParticlePtr& p, int second_field);
113
114 /// @}
115
116private:
117
118 std::ofstream m_file; //!< Output file
119 std::shared_ptr<std::ostream> m_shared_stream = nullptr;///< Output temp. stream
120 std::ostream* m_stream = nullptr; //!< Output stream
121
122 int m_precision = 16; //!< Output precision
123 char* m_buffer = nullptr; //!< Stream buffer
124 char* m_cursor = nullptr; //!< Cursor inside stream buffer
125 unsigned long m_buffer_size = 262144; //!< Buffer size
126 std::string m_float_printf_specifier; //!< the specifier of printf used for floats
127 std::string m_particle_printf_specifier; //!< the specifier of printf used for floats
128 std::string m_vertex_short_printf_specifier; //!< the specifier of printf used for zero vertices
129 std::string m_vertex_long_printf_specifier; //!< the specifier of printf used for vertices
130};
131
132
133} // namespace HepMC3
134
135#endif
GenEvent.h
Definition of class GenEvent.
GenRunInfo.h
Definition of class GenRunInfo.
Writer.h
Definition of interface Writer.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::WriterAscii::set_buffer_size
void set_buffer_size(const size_t &size)
Set buffer size (in bytes).
Definition WriterAscii.cc:369
HepMC3::WriterAscii::set_precision
void set_precision(const int &prec)
Set output precision.
Definition WriterAscii.cc:360
HepMC3::WriterAscii::allocate_buffer
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
Definition WriterAscii.cc:206
HepMC3::WriterAscii::m_cursor
char * m_cursor
Cursor inside stream buffer.
Definition WriterAscii.h:124
HepMC3::WriterAscii::failed
bool failed() override
Return status of the stream.
Definition WriterAscii.cc:358
HepMC3::WriterAscii::m_buffer
char * m_buffer
Stream buffer.
Definition WriterAscii.h:123
HepMC3::WriterAscii::m_float_printf_specifier
std::string m_float_printf_specifier
the specifier of printf used for floats
Definition WriterAscii.h:126
HepMC3::WriterAscii::~WriterAscii
~WriterAscii()
Destructor.
Definition WriterAscii.cc:86
HepMC3::WriterAscii::close
void close() override
Close file stream.
Definition WriterAscii.cc:348
HepMC3::WriterAscii::precision
int precision() const
Return output precision.
Definition WriterAscii.cc:365
HepMC3::WriterAscii::m_precision
int m_precision
Output precision.
Definition WriterAscii.h:122
HepMC3::WriterAscii::write_particle
void write_particle(const ConstGenParticlePtr &p, int second_field)
Write particle.
Definition WriterAscii.cc:326
HepMC3::WriterAscii::m_vertex_short_printf_specifier
std::string m_vertex_short_printf_specifier
the specifier of printf used for zero vertices
Definition WriterAscii.h:128
HepMC3::WriterAscii::write_vertex
void write_vertex(const ConstGenVertexPtr &v)
Write vertex.
Definition WriterAscii.cc:244
HepMC3::WriterAscii::m_shared_stream
std::shared_ptr< std::ostream > m_shared_stream
Output temp. stream.
Definition WriterAscii.h:119
HepMC3::WriterAscii::m_file
std::ofstream m_file
Output file.
Definition WriterAscii.h:118
HepMC3::WriterAscii::escape
static std::string escape(const std::string &s)
Escape '\' and ' ' characters in string.
Definition WriterAscii.cc:226
HepMC3::WriterAscii::write_string
void write_string(const std::string &str)
Inline function for writing strings.
Definition WriterAscii.cc:333
HepMC3::WriterAscii::m_buffer_size
unsigned long m_buffer_size
Buffer size.
Definition WriterAscii.h:125
HepMC3::WriterAscii::m_particle_printf_specifier
std::string m_particle_printf_specifier
the specifier of printf used for floats
Definition WriterAscii.h:127
HepMC3::WriterAscii::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition WriterAscii.cc:92
HepMC3::WriterAscii::WriterAscii
WriterAscii(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
Definition WriterAscii.cc:25
HepMC3::WriterAscii::m_vertex_long_printf_specifier
std::string m_vertex_long_printf_specifier
the specifier of printf used for vertices
Definition WriterAscii.h:129
HepMC3::WriterAscii::flush
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
Definition WriterAscii.cc:264
HepMC3::WriterAscii::write_run_info
void write_run_info()
Write the GenRunInfo object to file.
Definition WriterAscii.cc:283
HepMC3::WriterAscii::forced_flush
void forced_flush()
Inline function forcing flush to the output stream.
Definition WriterAscii.cc:276
HepMC3::WriterAscii::m_stream
std::ostream * m_stream
Output stream.
Definition WriterAscii.h:120
HepMC3::Writer::Writer
Writer()
Constructor.
Definition Writer.h:29
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24