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 
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  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
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);
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() { return (bool)m_file.rdstate(); }
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  if (prec < 2 || prec > 24) return;
59  m_precision = prec;
60  }
61  /// @brief Return output precision
62  int precision() const {
63  return m_precision;
64  }
65 private:
66 
67  /// @name Buffer management
68  //@{
69 
70  /// @brief Attempts to allocate buffer of the chosen size
71  ///
72  /// This function can be called manually by the user or will be called
73  /// before first read/write operation
74  ///
75  /// @note If buffer size is too large it will be divided by 2 until it is
76  /// small enough for system to allocate
77  void allocate_buffer();
78 
79  /// @brief Set buffer size (in bytes)
80  ///
81  /// Default is 256kb. Minimum is 256b.
82  /// Size can only be changed before first read/write operation.
83  void set_buffer_size(const size_t& size ) {
84  if (m_buffer) return;
85  if (size < 256) return;
86  m_buffer_size = size;
87  }
88 
89  /// @brief Escape '\' and '\n' characters in string
90  std::string escape(const std::string& s) const;
91 
92  /// Inline function flushing buffer to output stream when close to buffer capacity
93  void flush();
94 
95  /// Inline function forcing flush to the output stream
96  void forced_flush();
97 
98  //@}
99 
100 
101  /// @name Write helpers
102  //@{
103 
104  /// @brief Inline function for writing strings
105  ///
106  /// Since strings can be long (maybe even longer than buffer) they have to be dealt
107  /// with separately.
108  void write_string( const std::string &str );
109 
110  /// @brief Write vertex
111  ///
112  /// Helper routine for writing single vertex to file
113  void write_vertex(ConstGenVertexPtr v);
114 
115  /// @brief Write particle
116  ///
117  /// Helper routine for writing single particle to file
118  void write_particle(ConstGenParticlePtr p, int second_field);
119 
120  //@}
121 
122 private:
123 
124  std::ofstream m_file; //!< Output file
125  std::ostream* m_stream; //!< Output stream
126 
127  int m_precision; //!< Output precision
128  char* m_buffer; //!< Stream buffer
129  char* m_cursor; //!< Cursor inside stream buffer
130  unsigned long m_buffer_size; //!< Buffer size
131 
132 };
133 
134 
135 } // namespace HepMC3
136 
137 #endif
Definition of class GenRunInfo.
void write_string(const std::string &str)
Inline function for writing strings.
Definition: WriterAscii.cc:330
void forced_flush()
Inline function forcing flush to the output stream.
Definition: WriterAscii.cc:255
HepMC3 main namespace.
Definition: WriterDOT.h:19
int m_precision
Output precision.
Definition: WriterAscii.h:127
std::ostream * m_stream
Output stream.
Definition: WriterAscii.h:125
void set_buffer_size(const size_t &size)
Set buffer size (in bytes)
Definition: WriterAscii.h:83
char * m_cursor
Cursor inside stream buffer.
Definition: WriterAscii.h:129
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
Definition: WriterAscii.cc:170
WriterAscii(const std::string &filename, shared_ptr< GenRunInfo > run=shared_ptr< GenRunInfo >())
Constructor.
Definition: WriterAscii.cc:22
bool failed()
Return status of the stream.
Definition: WriterAscii.h:49
void write_particle(ConstGenParticlePtr p, int second_field)
Write particle.
Definition: WriterAscii.cc:306
unsigned long m_buffer_size
Buffer size.
Definition: WriterAscii.h:130
void set_precision(const int &prec)
Set output precision.
Definition: WriterAscii.h:57
Stores event-related information.
Definition: GenEvent.h:42
char * m_buffer
Stream buffer.
Definition: WriterAscii.h:128
void write_vertex(ConstGenVertexPtr v)
Write vertex.
Definition: WriterAscii.cc:203
Definition of interface Writer.
int precision() const
Return output precision.
Definition: WriterAscii.h:62
~WriterAscii()
Destructor.
Definition: WriterAscii.cc:57
void close()
Close file stream.
Definition: WriterAscii.cc:349
void write_run_info()
Write the GenRunInfo object to file.
Definition: WriterAscii.cc:262
Base class for all I/O writers.
Definition: Writer.h:25
std::ofstream m_file
Output file.
Definition: WriterAscii.h:124
Definition of class GenEvent.
void write_event(const GenEvent &evt)
Write event to file.
Definition: WriterAscii.cc:63
std::string escape(const std::string &s) const
Escape '\' and ' ' characters in string.
Definition: WriterAscii.cc:190
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
Definition: WriterAscii.cc:242
GenEvent I/O serialization for structured text files.
Definition: WriterAscii.h:25