HepMC3 event record library
WriterAsciiHepMC2.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_WRITERASCIIHEPMC2_H
7 #define HEPMC3_WRITERASCIIHEPMC2_H
8 ///
9 /// @file WriterAsciiHepMC2.h
10 /// @brief Definition of class \b WriterAsciiHepMC2
11 ///
12 /// @class HepMC3::WriterAsciiHepMC2
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 
26 class WriterAsciiHepMC2 : public Writer
27 {
28 public:
29 
30  /// @brief Constructor
31  /// @warning If file already exists, it will be cleared before writing
32  WriterAsciiHepMC2(const std::string& filename,
33  shared_ptr<GenRunInfo> run = shared_ptr<GenRunInfo>());
34 
35  /// @brief Constructor from ostream
36  WriterAsciiHepMC2(std::ostream& stream,
37  shared_ptr<GenRunInfo> run = shared_ptr<GenRunInfo>());
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);
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() { return (bool)m_file.rdstate(); }
52 
53  /// @brief Close file stream
54  void close();
55 
56  /// @brief Set output precision
57  ///
58  /// Available range is [2,24]. Default is 16.
59  void set_precision( const int& prec )
60  {
61  if (prec < 2 || prec > 24) return;
62  m_precision = prec;
63  }
64  /// @brief Return output precision
65  int precision() const {
66  return m_precision;
67  }
68 private:
69 
70  /// @name Buffer management
71  //@{
72 
73  /// @brief Attempts to allocate buffer of the chosen size
74  ///
75  /// This function can be called manually by the user or will be called
76  /// before first read/write operation
77  ///
78  /// @note If buffer size is too large it will be divided by 2 until it is
79  /// small enough for system to allocate
80  void allocate_buffer();
81 
82  /// @brief Set buffer size (in bytes)
83  ///
84  /// Default is 256kb. Minimum is 256b.
85  /// Size can only be changed before first read/write operation.
86  void set_buffer_size(const size_t& size )
87  {
88  if (m_buffer) return;
89  if (size < 256) return;
90  m_buffer_size = size;
91  }
92 
93  /// @brief Escape '\' and '\n' characters in string
94  std::string escape(const std::string& s) const;
95 
96  /// Inline function flushing buffer to output stream when close to buffer capacity
97  void flush();
98 
99  /// Inline function forcing flush to the output stream
100  void forced_flush();
101 
102  //@}
103 
104 
105  /// @name Write helpers
106  //@{
107 
108  /// @brief Inline function for writing strings
109  ///
110  /// Since strings can be long (maybe even longer than buffer) they have to be dealt
111  /// with separately.
112  void write_string(const std::string &str );
113 
114  /// @brief Write vertex
115  ///
116  /// Helper routine for writing single vertex to file
117  void write_vertex(ConstGenVertexPtr v);
118 
119  /// @brief Write particle
120  ///
121  /// Helper routine for writing single particle to file
122  void write_particle(ConstGenParticlePtr p, int second_field);
123 
124  //@}
125 
126 private:
127 
128  std::ofstream m_file; //!< Output file
129  std::ostream* m_stream; //!< Output stream
130  int m_precision; //!< Output precision
131  char* m_buffer; //!< Stream buffer
132  char* m_cursor; //!< Cursor inside stream buffer
133  unsigned long m_buffer_size; //!< Buffer size
134  unsigned long m_particle_counter; //!< Used to set bar codes
135 
136 };
137 
138 
139 } // namespace HepMC3
140 
141 #endif
GenEvent I/O serialization for structured text files.
Definition of class GenRunInfo.
std::string escape(const std::string &s) const
Escape &#39;\&#39; and &#39; &#39; characters in string.
std::ofstream m_file
Output file.
bool failed()
Return status of the stream.
void forced_flush()
Inline function forcing flush to the output stream.
void write_vertex(ConstGenVertexPtr v)
Write vertex.
int precision() const
Return output precision.
char * m_buffer
Stream buffer.
char * m_cursor
Cursor inside stream buffer.
unsigned long m_buffer_size
Buffer size.
unsigned long m_particle_counter
Used to set bar codes.
Stores event-related information.
Definition: GenEvent.h:42
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
void write_string(const std::string &str)
Inline function for writing strings.
Definition of interface Writer.
void write_run_info()
Write the GenRunInfo object to file.
void set_buffer_size(const size_t &size)
Set buffer size (in bytes)
void write_particle(ConstGenParticlePtr p, int second_field)
Write particle.
void close()
Close file stream.
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
Base class for all I/O writers.
Definition: Writer.h:25
int m_precision
Output precision.
WriterAsciiHepMC2(const std::string &filename, shared_ptr< GenRunInfo > run=shared_ptr< GenRunInfo >())
Constructor.
Definition of class GenEvent.
void set_precision(const int &prec)
Set output precision.
void write_event(const GenEvent &evt)
Write event to file.
std::ostream * m_stream
Output stream.