HepMC3 event record library: WriterAsciiHepMC2.h Source File

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-2023 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
23namespace HepMC3
24{
25
26 class WriterAsciiHepMC2 : public Writer
27{
28public:
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 /// @brief Constructor from ostream
36 WriterAsciiHepMC2(std::ostream& stream,
37 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
38 /// @brief Constructor from temp ostream
39 WriterAsciiHepMC2(std::shared_ptr<std::ostream> s_stream,
40 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
41
42 /// @brief Destructor
43 ~WriterAsciiHepMC2();
44
45 /// @brief Write event to file
46 ///
47 /// @param[in] evt Event to be serialized
48 void write_event(const GenEvent& evt) override;
49
50 /// @brief Write the GenRunInfo object to file.
51 void write_run_info();
52
53 /// @brief Return status of the stream
54 bool failed() override;
55
56 /// @brief Close file stream
57 void close() override;
58
59 /// @brief Set output precision
60 ///
61 /// Available range is [2,24]. Default is 16.
62 void set_precision( const int& prec );
63
64 /// @brief Return output precision
65 int precision() const;
66private:
67
68 /// @name Buffer management
69 /// @{
70
71 /// @brief Attempts to allocate buffer of the chosen size
72 ///
73 /// This function can be called manually by the user or will be called
74 /// before first read/write operation
75 ///
76 /// @note If buffer size is too large it will be divided by 2 until it is
77 /// small enough for system to allocate
78 void allocate_buffer();
79
80 /// @brief Set buffer size (in bytes)
81 ///
82 /// Default is 256kb. Minimum is 256b.
83 /// Size can only be changed before first read/write operation.
84 void set_buffer_size(const size_t& size );
85
86 /// @brief Escape '\' and '\n' characters in string
87 static 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 ConstGenVertexPtr& v);
111
112 /// @brief Write particle
113 ///
114 /// Helper routine for writing single particle to file
115 void write_particle(const ConstGenParticlePtr& p, int second_field);
116
117 /// @}
118
119private:
120
121 std::ofstream m_file; //!< Output file
122 std::shared_ptr<std::ostream> m_shared_stream;///< Output temp. stream
123 std::ostream* m_stream; //!< Output stream
124 int m_precision = 16; //!< Output precision
125 char* m_buffer = nullptr; //!< Stream buffer
126 char* m_cursor = nullptr; //!< Cursor inside stream buffer
127 unsigned long m_buffer_size = 262144; //!< Buffer size
128 unsigned long m_particle_counter = 0; //!< Used to set bar codes
129 std::string m_float_printf_specifier; //!< the specifier of printf used for floats
130};
131
132
133} // namespace HepMC3
134
135#endif
GenEvent.h
Definition of class GenEvent.
GenRunInfo.h
Definition of class GenRunInfo.
Writer.h
Definition of interface Writer.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::WriterAsciiHepMC2::set_buffer_size
void set_buffer_size(const size_t &size)
Set buffer size (in bytes).
Definition WriterAsciiHepMC2.cc:454
HepMC3::WriterAsciiHepMC2::set_precision
void set_precision(const int &prec)
Set output precision.
Definition WriterAsciiHepMC2.cc:445
HepMC3::WriterAsciiHepMC2::allocate_buffer
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
Definition WriterAsciiHepMC2.cc:248
HepMC3::WriterAsciiHepMC2::m_cursor
char * m_cursor
Cursor inside stream buffer.
Definition WriterAsciiHepMC2.h:126
HepMC3::WriterAsciiHepMC2::failed
bool failed() override
Return status of the stream.
Definition WriterAsciiHepMC2.cc:443
HepMC3::WriterAsciiHepMC2::m_buffer
char * m_buffer
Stream buffer.
Definition WriterAsciiHepMC2.h:125
HepMC3::WriterAsciiHepMC2::m_float_printf_specifier
std::string m_float_printf_specifier
the specifier of printf used for floats
Definition WriterAsciiHepMC2.h:129
HepMC3::WriterAsciiHepMC2::close
void close() override
Close file stream.
Definition WriterAsciiHepMC2.cc:432
HepMC3::WriterAsciiHepMC2::precision
int precision() const
Return output precision.
Definition WriterAsciiHepMC2.cc:450
HepMC3::WriterAsciiHepMC2::m_precision
int m_precision
Output precision.
Definition WriterAsciiHepMC2.h:124
HepMC3::WriterAsciiHepMC2::WriterAsciiHepMC2
WriterAsciiHepMC2(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
Definition WriterAsciiHepMC2.cc:24
HepMC3::WriterAsciiHepMC2::write_particle
void write_particle(const ConstGenParticlePtr &p, int second_field)
Write particle.
Definition WriterAsciiHepMC2.cc:361
HepMC3::WriterAsciiHepMC2::write_vertex
void write_vertex(const ConstGenVertexPtr &v)
Write vertex.
Definition WriterAsciiHepMC2.cc:292
HepMC3::WriterAsciiHepMC2::m_shared_stream
std::shared_ptr< std::ostream > m_shared_stream
Output temp. stream.
Definition WriterAsciiHepMC2.h:122
HepMC3::WriterAsciiHepMC2::m_file
std::ofstream m_file
Output file.
Definition WriterAsciiHepMC2.h:121
HepMC3::WriterAsciiHepMC2::~WriterAsciiHepMC2
~WriterAsciiHepMC2()
Destructor.
Definition WriterAsciiHepMC2.cc:67
HepMC3::WriterAsciiHepMC2::m_particle_counter
unsigned long m_particle_counter
Used to set bar codes.
Definition WriterAsciiHepMC2.h:128
HepMC3::WriterAsciiHepMC2::escape
static std::string escape(const std::string &s)
Escape '\' and ' ' characters in string.
Definition WriterAsciiHepMC2.cc:271
HepMC3::WriterAsciiHepMC2::write_string
void write_string(const std::string &str)
Inline function for writing strings.
Definition WriterAsciiHepMC2.cc:414
HepMC3::WriterAsciiHepMC2::m_buffer_size
unsigned long m_buffer_size
Buffer size.
Definition WriterAsciiHepMC2.h:127
HepMC3::WriterAsciiHepMC2::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition WriterAsciiHepMC2.cc:74
HepMC3::WriterAsciiHepMC2::flush
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
Definition WriterAsciiHepMC2.cc:338
HepMC3::WriterAsciiHepMC2::write_run_info
void write_run_info()
Write the GenRunInfo object to file.
Definition WriterAsciiHepMC2.cc:359
HepMC3::WriterAsciiHepMC2::forced_flush
void forced_flush()
Inline function forcing flush to the output stream.
Definition WriterAsciiHepMC2.cc:351
HepMC3::WriterAsciiHepMC2::m_stream
std::ostream * m_stream
Output stream.
Definition WriterAsciiHepMC2.h:123
HepMC3::Writer::Writer
Writer()
Constructor.
Definition Writer.h:29
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24