| 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 |
| 40 |
~WriterAsciiHepMC2(); |
| 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(); |
| 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 |
/// @brief Return output precision |
| 62 |
int precision() const; |
| 63 |
private: |
| 64 |
|
| 65 |
/// @name Buffer management |
| 66 |
//@{ |
| 67 |
|
| 68 |
/// @brief Attempts to allocate buffer of the chosen size |
| 69 |
/// |
| 70 |
/// This function can be called manually by the user or will be called |
| 71 |
/// before first read/write operation |
| 72 |
/// |
| 73 |
/// @note If buffer size is too large it will be divided by 2 until it is |
| 74 |
/// small enough for system to allocate |
| 75 |
void allocate_buffer(); |
| 76 |
|
| 77 |
/// @brief Set buffer size (in bytes) |
| 78 |
/// |
| 79 |
/// Default is 256kb. Minimum is 256b. |
| 80 |
/// Size can only be changed before first read/write operation. |
| 81 |
void set_buffer_size(const size_t& size ); |
| 82 |
|
| 83 |
/// @brief Escape '\' and '\n' characters in string |
| 84 |
std::string escape(const std::string& s) const; |
| 85 |
|
| 86 |
/// Inline function flushing buffer to output stream when close to buffer capacity |
| 87 |
void flush(); |
| 88 |
|
| 89 |
/// Inline function forcing flush to the output stream |
| 90 |
void forced_flush(); |
| 91 |
|
| 92 |
//@} |
| 93 |
|
| 94 |
|
| 95 |
/// @name Write helpers |
| 96 |
//@{ |
| 97 |
|
| 98 |
/// @brief Inline function for writing strings |
| 99 |
/// |
| 100 |
/// Since strings can be long (maybe even longer than buffer) they have to be dealt |
| 101 |
/// with separately. |
| 102 |
void write_string(const std::string &str ); |
| 103 |
|
| 104 |
/// @brief Write vertex |
| 105 |
/// |
| 106 |
/// Helper routine for writing single vertex to file |
| 107 |
void write_vertex(ConstGenVertexPtr v); |
| 108 |
|
| 109 |
/// @brief Write particle |
| 110 |
/// |
| 111 |
/// Helper routine for writing single particle to file |
| 112 |
void write_particle(ConstGenParticlePtr p, int second_field); |
| 113 |
|
| 114 |
//@} |
| 115 |
|
| 116 |
private: |
| 117 |
|
| 118 |
std::ofstream m_file; //!< Output file |
| 119 |
std::ostream* m_stream; //!< Output stream |
| 120 |
int m_precision; //!< Output precision |
| 121 |
char* m_buffer; //!< Stream buffer |
| 122 |
char* m_cursor; //!< Cursor inside stream buffer |
| 123 |
unsigned long m_buffer_size; //!< Buffer size |
| 124 |
unsigned long m_particle_counter; //!< Used to set bar codes |
| 125 |
|
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
} // namespace HepMC3 |
| 130 |
|
| 131 |
#endif |