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