HepMC event record
build/outputs/include/HepMC/WriterAscii.h
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2015 The HepMC collaboration (see AUTHORS for details)
5 //
6 #ifndef HEPMC_WRITERASCII_H
7 #define HEPMC_WRITERASCII_H
8 ///
9 /// @file WriterAscii.h
10 /// @brief Definition of class \b WriterAscii
11 ///
12 /// @class HepMC::WriterAscii
13 /// @brief GenEvent I/O serialization for structured text files
14 ///
15 /// @ingroup IO
16 ///
17 #include "HepMC/Writer.h"
18 #include "HepMC/GenEvent.h"
19 #include "HepMC/GenRunInfo.h"
20 #include <string>
21 #include <fstream>
22 
23 namespace HepMC {
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  /// Available range is [2,24]. Default is 16.
57  void set_precision( size_t prec ) {
58  if (prec < 2 || prec > 24) return;
59  m_precision = prec;
60  }
61 
62 private:
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( size_t size ) {
81  if (m_buffer) return;
82  if (size < 256) return;
83  m_buffer_size = size;
84  }
85 
86  /// @brief Escape '\' and '\n' characters in string
87  std::string escape(const std::string s);
88 
89  /// Inline function flushing buffer to output stream when close to buffer capacity
90  void flush();
91 
92  /// Inline function forcing flush to the output stream
93  void forced_flush();
94 
95  //@}
96 
97 
98  /// @name Write helpers
99  //@{
100 
101  /// @brief Inline function for writing strings
102  ///
103  /// Since strings can be long (maybe even longer than buffer) they have to be dealt
104  /// with separately.
105  void write_string( const std::string &str );
106 
107  /// @brief Write vertex
108  ///
109  /// Helper routine for writing single vertex to file
110  void write_vertex (const GenVertexPtr &v);
111 
112  /// @brief Write particle
113  ///
114  /// Helper routine for writing single particle to file
115  void write_particle(const GenParticlePtr &p, int second_field);
116 
117  //@}
118 
119 private:
120 
121  std::ofstream m_file; //!< Output file
122  std::ostream* m_stream; //!< Output stream
123  int m_precision; //!< Output precision
124  char* m_buffer; //!< Stream buffer
125  char* m_cursor; //!< Cursor inside stream buffer
126  unsigned long m_buffer_size; //!< Buffer size
127 
128 };
129 
130 
131 } // namespace HepMC
132 
133 #endif
WriterAscii(const std::string &filename, shared_ptr< GenRunInfo > run=shared_ptr< GenRunInfo >())
Constructor.
Definition: WriterAscii.cc:22
void write_event(const GenEvent &evt)
Write event to file.
Definition: WriterAscii.cc:68
void write_vertex(const GenVertexPtr &v)
Write vertex.
Definition: WriterAscii.cc:209
void write_string(const std::string &str)
Inline function for writing strings.
Definition: WriterAscii.cc:333
~WriterAscii()
Destructor.
Definition: WriterAscii.cc:62
std::ostream * m_stream
Output stream.
void set_precision(size_t prec)
Set output precision.
void forced_flush()
Inline function forcing flush to the output stream.
Definition: WriterAscii.cc:258
bool failed()
Return status of the stream.
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
Definition: WriterAscii.cc:177
Stores event-related information.
GenEvent I/O serialization for structured text files.
void write_run_info()
Write the GenRunInfo object to file.
Definition: WriterAscii.cc:265
void write_particle(const GenParticlePtr &p, int second_field)
Write particle.
Definition: WriterAscii.cc:309
Base class for all I/O writers.
char * m_cursor
Cursor inside stream buffer.
unsigned long m_buffer_size
Buffer size.
void set_buffer_size(size_t size)
Set buffer size (in bytes)
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
Definition: WriterAscii.cc:245
void close()
Close file stream.
Definition: WriterAscii.cc:352
Definition of template class SmartPointer.
std::string escape(const std::string s)
Escape &#39;\&#39; and &#39; &#39; characters in string.
Definition: WriterAscii.cc:196