Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:14

0001 // -*- C++ -*-
0002 //
0003 // This file is part of HepMC
0004 // Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
0005 //
0006 #ifndef HEPMC3_WRITERASCIIHEPMC2_H
0007 #define HEPMC3_WRITERASCIIHEPMC2_H
0008 ///
0009 /// @file  WriterAsciiHepMC2.h
0010 /// @brief Definition of class \b WriterAsciiHepMC2
0011 ///
0012 /// @class HepMC3::WriterAsciiHepMC2
0013 /// @brief GenEvent I/O serialization for structured text files
0014 ///
0015 /// @ingroup IO
0016 ///
0017 #include "HepMC3/Writer.h"
0018 #include "HepMC3/GenEvent.h"
0019 #include "HepMC3/GenRunInfo.h"
0020 #include <string>
0021 #include <fstream>
0022 
0023 namespace HepMC3
0024 {
0025 
0026 class WriterAsciiHepMC2 : public Writer
0027 {
0028 public:
0029 
0030     /// @brief Constructor
0031     /// @warning If file already exists, it will be cleared before writing
0032     WriterAsciiHepMC2(const std::string& filename,
0033                       std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
0034 
0035     /// @brief Constructor from ostream
0036     WriterAsciiHepMC2(std::ostream& stream,
0037                       std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
0038     /// @brief Constructor from temp ostream
0039     WriterAsciiHepMC2(std::shared_ptr<std::ostream> s_stream,
0040                       std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
0041 
0042     /// @brief Destructor
0043     ~WriterAsciiHepMC2();
0044 
0045     /// @brief Write event to file
0046     ///
0047     /// @param[in] evt Event to be serialized
0048     void write_event(const GenEvent& evt)  override;
0049 
0050     /// @brief Write the GenRunInfo object to file.
0051     void write_run_info();
0052 
0053     /// @brief Return status of the stream
0054     bool failed()  override;
0055 
0056     /// @brief Close file stream
0057     void close()  override;
0058 
0059     /// @brief Set output precision
0060     ///
0061     /// Available range is [2,24]. Default is 16.
0062     void set_precision( const int& prec );
0063 
0064     /// @brief Return output precision
0065     int precision() const;
0066 private:
0067 
0068     /// @name Buffer management
0069     /// @{
0070 
0071     /// @brief Attempts to allocate buffer of the chosen size
0072     ///
0073     /// This function can be called manually by the user or will be called
0074     /// before first read/write operation
0075     ///
0076     /// @note If buffer size is too large it will be divided by 2 until it is
0077     /// small enough for system to allocate
0078     void allocate_buffer();
0079 
0080     /// @brief Set buffer size (in bytes)
0081     ///
0082     /// Default is 256kb. Minimum is 256b.
0083     /// Size can only be changed before first read/write operation.
0084     void set_buffer_size(const size_t& size );
0085 
0086     /// @brief Escape '\' and '\n' characters in string
0087     static std::string escape(const std::string& s);
0088 
0089     /// Inline function flushing buffer to output stream when close to buffer capacity
0090     void flush();
0091 
0092     /// Inline function forcing flush to the output stream
0093     void forced_flush();
0094 
0095     /// @}
0096 
0097 
0098     /// @name Write helpers
0099     /// @{
0100 
0101     /// @brief Inline function for writing strings
0102     ///
0103     /// Since strings can be long (maybe even longer than buffer) they have to be dealt
0104     /// with separately.
0105     void write_string(const std::string &str );
0106 
0107     /// @brief Write vertex
0108     ///
0109     /// Helper routine for writing single vertex to file
0110     void write_vertex(const ConstGenVertexPtr& v);
0111 
0112     /// @brief Write particle
0113     ///
0114     /// Helper routine for writing single particle to file
0115     void write_particle(const ConstGenParticlePtr& p, int second_field);
0116 
0117     /// @}
0118 
0119 private:
0120 
0121     std::ofstream m_file; //!< Output file
0122     std::shared_ptr<std::ostream> m_shared_stream;///< Output temp. stream
0123     std::ostream* m_stream; //!< Output stream
0124     int m_precision = 16; //!< Output precision
0125     char* m_buffer = nullptr;  //!< Stream buffer
0126     char* m_cursor = nullptr;  //!< Cursor inside stream buffer
0127     unsigned long m_buffer_size = 262144; //!< Buffer size
0128     unsigned long m_particle_counter = 0; //!< Used to set bar codes
0129     std::string m_float_printf_specifier; //!< the specifier of printf used for floats
0130 };
0131 
0132 
0133 } // namespace HepMC3
0134 
0135 #endif