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-2021 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 
23 namespace HepMC3 {
24 
25 class WriterAscii : public Writer {
26 public:
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;
63 private:
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  std::string escape(const std::string& s) const;
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(ConstGenVertexPtr v);
108 
109  /// @brief Write particle
110  ///
111  /// Helper routine for writing single particle to file
112  void write_particle(ConstGenParticlePtr p, int second_field);
113 
114  /// @}
115 
116 private:
117 
118  std::ofstream m_file; //!< Output file
119  std::shared_ptr<std::ostream> m_shared_stream;///< Output temp. stream
120  std::ostream* m_stream; //!< Output stream
121 
122  int m_precision; //!< Output precision
123  char* m_buffer; //!< Stream buffer
124  char* m_cursor; //!< Cursor inside stream buffer
125  unsigned long m_buffer_size; //!< 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
Definition of class GenEvent.
Definition of class GenRunInfo.
Definition of interface Writer.
Stores event-related information.
Definition: GenEvent.h:41
GenEvent I/O serialization for structured text files.
Definition: WriterAscii.h:25
void set_buffer_size(const size_t &size)
Set buffer size (in bytes)
Definition: WriterAscii.cc:381
void set_precision(const int &prec)
Set output precision.
Definition: WriterAscii.cc:372
std::string escape(const std::string &s) const
Escape '\' and ' ' characters in string.
Definition: WriterAscii.cc:239
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
Definition: WriterAscii.cc:219
char * m_cursor
Cursor inside stream buffer.
Definition: WriterAscii.h:124
bool failed() override
Return status of the stream.
Definition: WriterAscii.cc:370
char * m_buffer
Stream buffer.
Definition: WriterAscii.h:123
std::string m_float_printf_specifier
the specifier of printf used for floats
Definition: WriterAscii.h:126
~WriterAscii()
Destructor.
Definition: WriterAscii.cc:99
void close() override
Close file stream.
Definition: WriterAscii.cc:362
int precision() const
Return output precision.
Definition: WriterAscii.cc:377
void write_particle(ConstGenParticlePtr p, int second_field)
Write particle.
Definition: WriterAscii.cc:340
int m_precision
Output precision.
Definition: WriterAscii.h:122
std::string m_vertex_short_printf_specifier
the specifier of printf used for zero vertices
Definition: WriterAscii.h:128
std::shared_ptr< std::ostream > m_shared_stream
Output temp. stream.
Definition: WriterAscii.h:119
std::ofstream m_file
Output file.
Definition: WriterAscii.h:118
void write_string(const std::string &str)
Inline function for writing strings.
Definition: WriterAscii.cc:347
unsigned long m_buffer_size
Buffer size.
Definition: WriterAscii.h:125
std::string m_particle_printf_specifier
the specifier of printf used for floats
Definition: WriterAscii.h:127
void write_event(const GenEvent &evt) override
Write event to file.
Definition: WriterAscii.cc:105
void write_vertex(ConstGenVertexPtr v)
Write vertex.
Definition: WriterAscii.cc:257
WriterAscii(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
Definition: WriterAscii.cc:24
std::string m_vertex_long_printf_specifier
the specifier of printf used for vertices
Definition: WriterAscii.h:129
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
Definition: WriterAscii.cc:277
void write_run_info()
Write the GenRunInfo object to file.
Definition: WriterAscii.cc:296
void forced_flush()
Inline function forcing flush to the output stream.
Definition: WriterAscii.cc:289
std::ostream * m_stream
Output stream.
Definition: WriterAscii.h:120
Base class for all I/O writers.
Definition: Writer.h:25
HepMC3 main namespace.