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-2020 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 
37  /// @brief Destructor
38  ~WriterAscii();
39 
40  /// @brief Write event to file
41  ///
42  /// @param[in] evt Event to be serialized
43  void write_event(const GenEvent& evt) override;
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() override;
50 
51  /// @brief Close file stream
52  void close() override;
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;
60 private:
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 
113 private:
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 GenRunInfo.
void write_string(const std::string &str)
Inline function for writing strings.
Definition: WriterAscii.cc:335
void forced_flush()
Inline function forcing flush to the output stream.
Definition: WriterAscii.cc:260
int m_precision
Output precision.
Definition: WriterAscii.h:118
std::ostream * m_stream
Output stream.
Definition: WriterAscii.h:116
void set_buffer_size(const size_t &size)
Set buffer size (in bytes)
Definition: WriterAscii.cc:372
char * m_cursor
Cursor inside stream buffer.
Definition: WriterAscii.h:120
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
Definition: WriterAscii.cc:170
WriterAscii(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
Definition: WriterAscii.cc:22
void write_particle(ConstGenParticlePtr p, int second_field)
Write particle.
Definition: WriterAscii.cc:311
unsigned long m_buffer_size
Buffer size.
Definition: WriterAscii.h:121
Definition: pytypes.h:935
void set_precision(const int &prec)
Set output precision.
Definition: WriterAscii.cc:363
bool failed() override
Return status of the stream.
Definition: WriterAscii.cc:361
Stores event-related information.
Definition: GenEvent.h:41
char * m_buffer
Stream buffer.
Definition: WriterAscii.h:119
void write_vertex(ConstGenVertexPtr v)
Write vertex.
Definition: WriterAscii.cc:208
Definition of interface Writer.
int precision() const
Return output precision.
Definition: WriterAscii.cc:368
~WriterAscii()
Destructor.
Definition: WriterAscii.cc:57
void write_run_info()
Write the GenRunInfo object to file.
Definition: WriterAscii.cc:267
void write_event(const GenEvent &evt) override
Write event to file.
Definition: WriterAscii.cc:63
Base class for all I/O writers.
Definition: Writer.h:25
std::ofstream m_file
Output file.
Definition: WriterAscii.h:115
Definition of class GenEvent.
std::string escape(const std::string &s) const
Escape &#39;\&#39; and &#39; &#39; characters in string.
Definition: WriterAscii.cc:190
void close() override
Close file stream.
Definition: WriterAscii.cc:354
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
Definition: WriterAscii.cc:247
GenEvent I/O serialization for structured text files.
Definition: WriterAscii.h:25