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-2019 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
25class 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#ifndef HEPMC3_PYTHON_BINDINGS
34 /// @brief Constructor from ostream
35 WriterAscii(std::ostream& stream,
36 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
37#endif
38
39 /// @brief Destructor
41
42 /// @brief Write event to file
43 ///
44 /// @param[in] evt Event to be serialized
45 void write_event(const GenEvent& evt) override;
46
47 /// @brief Write the GenRunInfo object to file.
48 void write_run_info();
49
50 /// @brief Return status of the stream
51 bool failed() override;
52
53 /// @brief Close file stream
54 void close() override;
55
56 /// @brief Set output precision
57 ///
58 /// So far available range is [2,24]. Default is 16.
59 void set_precision(const int& prec );
60 /// @brief Return output precision
61 int precision() const;
62private:
63
64 /// @name Buffer management
65 //@{
66
67 /// @brief Attempts to allocate buffer of the chosen size
68 ///
69 /// This function can be called manually by the user or will be called
70 /// before first read/write operation
71 ///
72 /// @note If buffer size is too large it will be divided by 2 until it is
73 /// small enough for system to allocate
74 void allocate_buffer();
75
76 /// @brief Set buffer size (in bytes)
77 ///
78 /// Default is 256kb. Minimum is 256b.
79 /// Size can only be changed before first read/write operation.
80 void set_buffer_size(const size_t& size );
81
82 /// @brief Escape '\' and '\n' characters in string
83 std::string escape(const std::string& s) const;
84
85 /// Inline function flushing buffer to output stream when close to buffer capacity
86 void flush();
87
88 /// Inline function forcing flush to the output stream
89 void forced_flush();
90
91 //@}
92
93
94 /// @name Write helpers
95 //@{
96
97 /// @brief Inline function for writing strings
98 ///
99 /// Since strings can be long (maybe even longer than buffer) they have to be dealt
100 /// with separately.
101 void write_string( const std::string &str );
102
103 /// @brief Write vertex
104 ///
105 /// Helper routine for writing single vertex to file
106 void write_vertex(ConstGenVertexPtr v);
107
108 /// @brief Write particle
109 ///
110 /// Helper routine for writing single particle to file
111 void write_particle(ConstGenParticlePtr p, int second_field);
112
113 //@}
114
115private:
116
117 std::ofstream m_file; //!< Output file
118 std::ostream* m_stream; //!< Output stream
119
120 int m_precision; //!< Output precision
121 char* m_buffer; //!< Stream buffer
122 char* m_cursor; //!< Cursor inside stream buffer
123 unsigned long m_buffer_size; //!< Buffer size
124
125};
126
127
128} // namespace HepMC3
129
130#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:372
void set_precision(const int &prec)
Set output precision.
Definition: WriterAscii.cc:363
std::string escape(const std::string &s) const
Escape '\' and ' ' characters in string.
Definition: WriterAscii.cc:190
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
Definition: WriterAscii.cc:170
char * m_cursor
Cursor inside stream buffer.
Definition: WriterAscii.h:122
bool failed() override
Return status of the stream.
Definition: WriterAscii.cc:361
char * m_buffer
Stream buffer.
Definition: WriterAscii.h:121
~WriterAscii()
Destructor.
Definition: WriterAscii.cc:57
void close() override
Close file stream.
Definition: WriterAscii.cc:354
int precision() const
Return output precision.
Definition: WriterAscii.cc:368
void write_particle(ConstGenParticlePtr p, int second_field)
Write particle.
Definition: WriterAscii.cc:311
int m_precision
Output precision.
Definition: WriterAscii.h:120
std::ofstream m_file
Output file.
Definition: WriterAscii.h:117
void write_string(const std::string &str)
Inline function for writing strings.
Definition: WriterAscii.cc:335
unsigned long m_buffer_size
Buffer size.
Definition: WriterAscii.h:123
void write_event(const GenEvent &evt) override
Write event to file.
Definition: WriterAscii.cc:63
void write_vertex(ConstGenVertexPtr v)
Write vertex.
Definition: WriterAscii.cc:208
WriterAscii(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
Definition: WriterAscii.cc:22
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
Definition: WriterAscii.cc:247
void write_run_info()
Write the GenRunInfo object to file.
Definition: WriterAscii.cc:267
void forced_flush()
Inline function forcing flush to the output stream.
Definition: WriterAscii.cc:260
std::ostream * m_stream
Output stream.
Definition: WriterAscii.h:118
Base class for all I/O writers.
Definition: Writer.h:25
HepMC3 main namespace.