MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
HepMC3::WriterAscii Class Reference

GenEvent I/O serialization for structured text files. More...

#include <WriterAscii.h>

Inheritance diagram for HepMC3::WriterAscii:
HepMC3::Writer

Public Member Functions

 WriterAscii (const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
 Constructor.
 
 WriterAscii (std::ostream &stream, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
 Constructor from ostream.
 
 WriterAscii (std::shared_ptr< std::ostream > s_stream, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
 Constructor from temp ostream.
 
 ~WriterAscii ()
 Destructor.
 
void close () override
 Close file stream.
 
bool failed () override
 Return status of the stream.
 
int precision () const
 Return output precision.
 
void set_precision (const int &prec)
 Set output precision.
 
void write_event (const GenEvent &evt) override
 Write event to file.
 
void write_run_info ()
 Write the GenRunInfo object to file.
 
- Public Member Functions inherited from HepMC3::Writer
 Writer ()
 Constructor.
 
 Writer (const Writer &)=delete
 deleted copy constructor
 
virtual ~Writer ()
 Virtual destructor.
 
virtual std::map< std::string, std::string > get_options () const
 Set options.
 
Writeroperator= (const Writer &)=delete
 deleted copy assignment operator
 
virtual std::shared_ptr< GenRunInforun_info () const
 Get the global GenRunInfo object.
 
virtual void set_options (const std::map< std::string, std::string > &options)
 Set options.
 
virtual void set_run_info (std::shared_ptr< GenRunInfo > run)
 Set the global GenRunInfo object.
 

Additional Inherited Members

- Protected Attributes inherited from HepMC3::Writer
std::map< std::string, std::string > m_options
 options
 

Detailed Description

GenEvent I/O serialization for structured text files.

Definition at line 25 of file WriterAscii.h.

Constructor & Destructor Documentation

◆ WriterAscii() [1/3]

HepMC3::WriterAscii::WriterAscii ( const std::string & filename,
std::shared_ptr< GenRunInfo > run = std::shared_ptr<GenRunInfo>() )

Constructor.

Warning
If file already exists, it will be cleared before writing

Definition at line 1762 of file marley_hepmc3.cc.

1763 : m_file(filename),
1764 m_stream(&m_file)
1765{
1766 set_run_info(run);
1767 if ( !m_file.is_open() ) {
1768 HEPMC3_ERROR_LEVEL(200,"WriterAscii: could not open output file: " << filename)
1769 } else {
1770 const std::string header = "HepMC::Version " + version() + "\nHepMC::Asciiv3-START_EVENT_LISTING\n";
1771 m_file.write(header.data(), header.length());
1772 if ( run_info() ) write_run_info();
1773 }
1774 m_float_printf_specifier = " %." + std::to_string(m_precision) + "e";
1775 m_particle_printf_specifier = "P %i %i %i"
1776 + m_float_printf_specifier
1777 + m_float_printf_specifier
1778 + m_float_printf_specifier
1779 + m_float_printf_specifier
1780 + m_float_printf_specifier + " %i\n";
1781 m_vertex_short_printf_specifier = "V %i %i [%s]\n";
1782 m_vertex_long_printf_specifier = "V %i %i [%s] @"+ m_float_printf_specifier + m_float_printf_specifier + m_float_printf_specifier + m_float_printf_specifier + "\n";
1783}
void write_run_info()
Write the GenRunInfo object to file.
virtual std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition Writer.h:45
virtual void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition Writer.h:42

References HepMC3::Writer::run_info(), HepMC3::Writer::set_run_info(), and write_run_info().

◆ WriterAscii() [2/3]

HepMC3::WriterAscii::WriterAscii ( std::ostream & stream,
std::shared_ptr< GenRunInfo > run = std::shared_ptr<GenRunInfo>() )

Constructor from ostream.

Definition at line 1786 of file marley_hepmc3.cc.

1787 : m_stream(&stream)
1788{
1789 set_run_info(run);
1790 const std::string header = "HepMC::Version " + version() + "\nHepMC::Asciiv3-START_EVENT_LISTING\n";
1791 m_stream->write(header.data(), header.length());
1792 if ( run_info() ) write_run_info();
1793 m_float_printf_specifier = " %." + std::to_string(m_precision) + "e";
1794 m_particle_printf_specifier = "P %i %i %i"
1795 + m_float_printf_specifier
1796 + m_float_printf_specifier
1797 + m_float_printf_specifier
1798 + m_float_printf_specifier
1799 + m_float_printf_specifier + " %i\n";
1800 m_vertex_short_printf_specifier = "V %i %i [%s]\n";
1801 m_vertex_long_printf_specifier = "V %i %i [%s] @"+ m_float_printf_specifier + m_float_printf_specifier + m_float_printf_specifier + m_float_printf_specifier + "\n";
1802}

References HepMC3::Writer::run_info(), HepMC3::Writer::set_run_info(), and write_run_info().

◆ WriterAscii() [3/3]

HepMC3::WriterAscii::WriterAscii ( std::shared_ptr< std::ostream > s_stream,
std::shared_ptr< GenRunInfo > run = std::shared_ptr<GenRunInfo>() )

Constructor from temp ostream.

Definition at line 1804 of file marley_hepmc3.cc.

1805 : m_shared_stream(s_stream),
1806 m_stream(s_stream.get())
1807{
1808 set_run_info(run);
1809 const std::string header = "HepMC::Version " + version() + "\nHepMC::Asciiv3-START_EVENT_LISTING\n";
1810 m_stream->write(header.data(), header.length());
1811 if ( run_info() ) write_run_info();
1812 m_float_printf_specifier = " %." + std::to_string(m_precision) + "e";
1813 m_particle_printf_specifier = "P %i %i %i"
1814 + m_float_printf_specifier
1815 + m_float_printf_specifier
1816 + m_float_printf_specifier
1817 + m_float_printf_specifier
1818 + m_float_printf_specifier + " %i\n";
1819 m_vertex_short_printf_specifier = "V %i %i [%s]\n";
1820 m_vertex_long_printf_specifier = "V %i %i [%s] @"+ m_float_printf_specifier + m_float_printf_specifier + m_float_printf_specifier + m_float_printf_specifier + "\n";
1821}

References HepMC3::Writer::run_info(), HepMC3::Writer::set_run_info(), and write_run_info().

◆ ~WriterAscii()

HepMC3::WriterAscii::~WriterAscii ( )

Destructor.

Definition at line 1823 of file marley_hepmc3.cc.

1823 {
1824 close();
1825 delete[] m_buffer;
1826}
void close() override
Close file stream.

References close().

Member Function Documentation

◆ close()

void HepMC3::WriterAscii::close ( )
overridevirtual

Close file stream.

Implements HepMC3::Writer.

Definition at line 2085 of file marley_hepmc3.cc.

2085 {
2086 if (!m_stream) return;
2087 auto* ofs = dynamic_cast<std::ofstream*>(m_stream);
2088 if (ofs && !ofs->is_open()) return;
2089 forced_flush();
2090 const std::string footer("HepMC::Asciiv3-END_EVENT_LISTING\n\n");
2091 if (m_stream) m_stream->write(footer.data(),footer.length());
2092 m_stream = nullptr;
2093 if (ofs) ofs->close();
2094}

Referenced by ~WriterAscii().

◆ failed()

bool HepMC3::WriterAscii::failed ( )
overridevirtual

Return status of the stream.

Implements HepMC3::Writer.

Definition at line 2095 of file marley_hepmc3.cc.

2095{ return (bool)m_file.rdstate(); }

◆ precision()

int HepMC3::WriterAscii::precision ( ) const

Return output precision.

Definition at line 2102 of file marley_hepmc3.cc.

2102 {
2103 return m_precision;
2104}

◆ set_precision()

void HepMC3::WriterAscii::set_precision ( const int & prec)

Set output precision.

So far available range is [2,24]. Default is 16.

Definition at line 2097 of file marley_hepmc3.cc.

2097 {
2098 if (prec < 2 || prec > 24) return;
2099 m_precision = prec;
2100}

◆ write_event()

void HepMC3::WriterAscii::write_event ( const GenEvent & evt)
overridevirtual

Write event to file.

Parameters
[in]evtEvent to be serialized

Implements HepMC3::Writer.

Definition at line 1829 of file marley_hepmc3.cc.

1829 {
1830 allocate_buffer();
1831 if ( !m_buffer ) return;
1832 auto float_printf_specifier_option = m_options.find("float_printf_specifier");
1833 std::string letter=(float_printf_specifier_option != m_options.end())?float_printf_specifier_option->second.substr(0,2):"e";
1834 if (letter != "e" && letter != "E" && letter != "G" && letter != "g" && letter != "f" && letter != "F" ) letter = "e";
1835 m_float_printf_specifier = " %." + std::to_string(m_precision) + letter;
1836
1837
1838 m_particle_printf_specifier = "P %i %i %i"
1839 + m_float_printf_specifier
1840 + m_float_printf_specifier
1841 + m_float_printf_specifier
1842 + m_float_printf_specifier
1843 + m_float_printf_specifier + " %i\n";
1844 m_vertex_short_printf_specifier = "V %i %i [%s]\n";
1845 m_vertex_long_printf_specifier = "V %i %i [%s] @"+ m_float_printf_specifier + m_float_printf_specifier + m_float_printf_specifier + m_float_printf_specifier + "\n";
1846
1847 // Make sure nothing was left from previous event
1848 flush();
1849
1850 if ( !run_info() ) {
1851 set_run_info(evt.run_info());
1853 } else {
1854 if ( evt.run_info() && (run_info() != evt.run_info()) ) {
1855 HEPMC3_WARNING_LEVEL(600,"WriterAscii::write_event: GenEvents contain different GenRunInfo objects from - only the first such object will be serialized.")
1856 }
1857 }
1858
1859 // Write event info
1860 flush();
1861 std::string especifier = "E " + std::to_string(evt.event_number()) + " "
1862 + std::to_string(evt.vertices().size()) + " "
1863 + std::to_string(evt.particles().size());
1864 // Write event position if not zero
1865 const FourVector &pos = evt.event_pos();
1866 if ( !pos.is_zero() ) {
1867 especifier += ( " @" + m_float_printf_specifier + m_float_printf_specifier + m_float_printf_specifier + m_float_printf_specifier + "\n" );
1868 m_cursor += sprintf(m_cursor, especifier.c_str(), pos.x(), pos.y(), pos.z(), pos.t());
1869 } else {
1870 m_cursor += sprintf(m_cursor, "%s\n", especifier.c_str());
1871 }
1872 flush();
1873
1874 // Write units
1875 m_cursor += sprintf(m_cursor, "U %s %s\n", Units::name(evt.momentum_unit()).c_str(), Units::name(evt.length_unit()).c_str());
1876 flush();
1877
1878 // Write weight values if present
1879 if ( !evt.weights().empty() ) {
1880 m_cursor += sprintf(m_cursor, "W");
1881 for (const auto& w: evt.weights())
1882 {
1883 m_cursor += sprintf(m_cursor, " %.*e", std::min(3*m_precision, 22), w);
1884 flush();
1885 }
1886 m_cursor += sprintf(m_cursor, "\n");
1887 flush();
1888 }
1889
1890 // Write attributes
1891 for ( const auto& vt1: evt.attributes() ) {
1892 for ( const auto& vt2: vt1.second ) {
1893 std::string st;
1894 bool status = vt2.second->to_string(st);
1895
1896 if ( !status ) {
1897 HEPMC3_WARNING_LEVEL(300,"WriterAscii::write_event: problem serializing attribute: " << vt1.first)
1898 }
1899 else {
1900 m_cursor += sprintf(m_cursor, "A %i ", vt2.first);
1901 write_string(escape(vt1.first));
1902 flush();
1903 m_cursor += sprintf(m_cursor, " ");
1904 write_string(escape(st));
1905 m_cursor += sprintf(m_cursor, "\n");
1906 flush();
1907 }
1908 }
1909 }
1910
1911
1912 // Print particles
1913 std::map<int, bool> alreadywritten;
1914 for (const ConstGenParticlePtr& p: evt.particles()) {
1915 // Check to see if we need to write a vertex first
1916 ConstGenVertexPtr v = p->production_vertex();
1917 int parent_object = 0;
1918
1919 if (v) {
1920 // Check if we need this vertex at all
1921 // Yes, use vertex as parent object
1922 if ( v->particles_in().size() > 1 || !v->data().is_zero() ) { parent_object = v->id(); }
1923 // No, use particle as parent object
1924 // Add check for attributes of this vertex
1925 else {
1926 if ( v->particles_in().size() == 1 ) { parent_object = v->particles_in().front()->id();}
1927 else {if ( v->particles_in().empty() ) {HEPMC3_DEBUG(30, "WriterAscii::write_event - found a vertex without incoming particles: " << v->id());}}
1928 }
1929 // Usage of map instead of simple counter helps to deal with events with random ids of vertices.
1930 if (alreadywritten.count(v->id()) == 0 && parent_object < 0)
1931 { write_vertex(v); alreadywritten[v->id()] = true; }
1932 }
1933
1934 write_particle(p, parent_object);
1935 }
1936 alreadywritten.clear();
1937
1938 // Flush rest of the buffer to file
1939 forced_flush();
1940}
static std::string name(MomentumUnit u)
Get name of momentum unit.
Definition Units.h:56
std::map< std::string, std::string > m_options
options
Definition Writer.h:59

References HepMC3::GenEvent::attributes(), HepMC3::GenEvent::event_number(), HepMC3::GenEvent::event_pos(), HepMC3::FourVector::is_zero(), HepMC3::GenEvent::length_unit(), HepMC3::Writer::m_options, HepMC3::GenEvent::momentum_unit(), HepMC3::Units::name(), HepMC3::GenEvent::particles(), HepMC3::GenEvent::run_info(), HepMC3::Writer::run_info(), HepMC3::Writer::set_run_info(), HepMC3::FourVector::t(), HepMC3::GenEvent::vertices(), HepMC3::GenEvent::weights(), write_run_info(), HepMC3::FourVector::x(), HepMC3::FourVector::y(), and HepMC3::FourVector::z().

◆ write_run_info()

void HepMC3::WriterAscii::write_run_info ( )

Write the GenRunInfo object to file.

Definition at line 2020 of file marley_hepmc3.cc.

2020 {
2021 allocate_buffer();
2022
2023 // If no run info object set, create a dummy one.
2024 if ( !run_info() ) set_run_info(std::make_shared<GenRunInfo>());
2025
2026 const std::vector<std::string> names = run_info()->weight_names();
2027
2028 if ( !names.empty() ) {
2029 std::string out = names[0];
2030 for ( int i = 1, N = names.size(); i < N; ++i ) {
2031 out += "\n" + names[i];
2032 }
2033 m_cursor += sprintf(m_cursor, "W ");
2034 flush();
2035 write_string(escape(out));
2036 m_cursor += sprintf(m_cursor, "\n");
2037 }
2038
2039 for (const auto& tool: run_info()->tools()) {
2040 std::string out = "T " + tool.name + "\n" + tool.version + "\n" + tool.description;
2041 write_string(escape(out));
2042 m_cursor += sprintf(m_cursor, "\n");
2043 }
2044
2045
2046 for ( const auto& att: run_info()->attributes() ) {
2047 std::string st;
2048 if ( !att.second->to_string(st) ) {
2049 HEPMC3_WARNING_LEVEL(300,"WriterAscii::write_run_info: problem serializing attribute: " << att.first)
2050 }
2051 else {
2052 m_cursor += sprintf(m_cursor, "A ");
2053 write_string(att.first);
2054 flush();
2055 m_cursor += sprintf(m_cursor, " ");
2056 write_string(escape(st));
2057 m_cursor += sprintf(m_cursor, "\n");
2058 flush();
2059 }
2060 }
2061}

References HepMC3::Writer::run_info(), and HepMC3::Writer::set_run_info().

Referenced by WriterAscii(), WriterAscii(), WriterAscii(), and write_event().


The documentation for this class was generated from the following files: