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