HepMC3 event record library
WriterHEPEVT.cc
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 /**
7  * @file WriterHEPEVT.cc
8  * @brief Implementation of \b class WriterHEPEVT
9  *
10  */
11 #include <sstream>
12 #include <cstdio> // sprintf
13 #include "HepMC3/WriterHEPEVT.h"
14 #include "HepMC3/HEPEVT_Wrapper.h"
15 #include "HepMC3/Print.h"
16 namespace HepMC3
17 {
18 
19 
20 WriterHEPEVT::WriterHEPEVT(const std::string &filename): m_events_count(0),m_vertices_positions_present(true), m_file(filename), m_stream(&m_file)
21 {
22  hepevtbuffer=(char*)(new struct HEPEVT());
23  HEPEVT_Wrapper::set_hepevt_address(hepevtbuffer);
24 }
25 
26 WriterHEPEVT::WriterHEPEVT(std::ostream& stream): m_events_count(0),m_vertices_positions_present(true), m_file(), m_stream(&stream)
27 {
28  hepevtbuffer=(char*)(new struct HEPEVT());
29  HEPEVT_Wrapper::set_hepevt_address(hepevtbuffer);
30 }
31 
32 void WriterHEPEVT::write_hepevt_particle( int index, bool iflong )
33 {
34  char buf[512];//Note: the format is fixed, so no reason for complicatied tratment
35  char* cursor=&(buf[0]);
36  cursor +=sprintf(cursor, "% 8i% 8i",HEPEVT_Wrapper::status(index), HEPEVT_Wrapper::id(index));
37  if (iflong)
38  {
39  cursor +=sprintf(cursor,"% 8i% 8i",HEPEVT_Wrapper::first_parent(index),HEPEVT_Wrapper::last_parent(index));
40  cursor +=sprintf(cursor,"% 8i% 8i",HEPEVT_Wrapper::first_child(index),HEPEVT_Wrapper::last_child(index));
41  cursor +=sprintf(cursor,"% 19.8E% 19.8E% 19.8E% 19.8E% 19.8E\n",HEPEVT_Wrapper::px(index),HEPEVT_Wrapper::py(index),HEPEVT_Wrapper::pz(index),HEPEVT_Wrapper::e(index),HEPEVT_Wrapper::m(index));
42  cursor +=sprintf(cursor, "%-48s% 19.8E% 19.8E% 19.8E% 19.8E\n"," ",HEPEVT_Wrapper::x(index),HEPEVT_Wrapper::y(index),HEPEVT_Wrapper::z(index),HEPEVT_Wrapper::t(index));
43  }
44  else
45  {
46  cursor +=sprintf(cursor, "% 8i% 8i",HEPEVT_Wrapper::first_child(index),HEPEVT_Wrapper::last_child(index));
47  cursor +=sprintf(cursor, "% 19.8E% 19.8E% 19.8E% 19.8E\n",HEPEVT_Wrapper::px(index),HEPEVT_Wrapper::py(index),HEPEVT_Wrapper::pz(index),HEPEVT_Wrapper::m(index));
48  }
49  unsigned long length = cursor - &(buf[0]);
50  m_stream->write( buf, length );
51 }
52 
54 {
55  char buf[512];//Note: the format is fixed, so no reason for complicatied tratment
56  char* cursor=buf;
57  cursor +=sprintf(cursor,"E% 8i %8i\n",HEPEVT_Wrapper::event_number(),HEPEVT_Wrapper::number_entries());
58  unsigned long length = cursor - &(buf[0]);
59  m_stream->write( buf, length );
60 }
61 
63 {
67  for( int i=1; i<=HEPEVT_Wrapper::number_entries(); ++i ) write_hepevt_particle(i,m_vertices_positions_present);
69 }
70 
72 {
73  std::ofstream* ofs = dynamic_cast<std::ofstream*>(m_stream);
74  if (ofs && !ofs->is_open()) return;
75  if (ofs) ofs->close();
76 }
77 
79 {
80  return (bool)m_file.rdstate();
81 }
82 
84 
86 
87 } // namespace HepMC3
HepMC3::HEPEVT_Wrapper::fix_daughters
static bool fix_daughters()
Tries to fix list of daughters.
Definition: HEPEVT_Wrapper.h:240
HepMC3::WriterHEPEVT::set_vertices_positions_present
void set_vertices_positions_present(bool iflong)
set flag if vertex positions are available
Definition: WriterHEPEVT.cc:83
HepMC3::WriterHEPEVT::failed
bool failed()
Get stream error state flag.
Definition: WriterHEPEVT.cc:78
HepMC3::GenEvent
Stores event-related information.
Definition: GenEvent.h:42
HepMC3
HepMC3 main namespace.
Definition: ReaderGZ.h:28
HEPEVT_Wrapper.h
Definition of class HEPEVT_Wrapper.
HEPEVT
C structure representing Fortran common block HEPEVT T. Sjöstrand et al., "A proposed standard event ...
Definition: HEPEVT_Wrapper.h:53
HepMC3::WriterHEPEVT::hepevtbuffer
char * hepevtbuffer
Pointer to HEPEVT Fortran common block/C struct.
Definition: WriterHEPEVT.h:78
WriterHEPEVT.h
Definition of class WriterHEPEVT.
HepMC3::WriterHEPEVT::m_vertices_positions_present
bool m_vertices_positions_present
true if vertex positions are available
Definition: WriterHEPEVT.h:80
HepMC3::WriterHEPEVT::m_events_count
int m_events_count
Events count. Needed to generate unique object name.
Definition: WriterHEPEVT.h:79
Print.h
Definition of static class Print.
HepMC3::WriterHEPEVT::WriterHEPEVT
WriterHEPEVT(const std::string &filename)
Default constructor.
Definition: WriterHEPEVT.cc:20
HepMC3::WriterHEPEVT::write_hepevt_particle
virtual void write_hepevt_particle(int index, bool iflong=true)
Write particle to file.
Definition: WriterHEPEVT.cc:32
HepMC3::WriterHEPEVT::write_event
void write_event(const GenEvent &evt)
Write event to file.
Definition: WriterHEPEVT.cc:62
HepMC3::WriterHEPEVT::m_file
std::ofstream m_file
Output file.
Definition: WriterHEPEVT.h:76
HepMC3::WriterHEPEVT::get_vertices_positions_present
bool get_vertices_positions_present() const
get flag if vertex positions are available
Definition: WriterHEPEVT.cc:85
HepMC3::HEPEVT_Wrapper::GenEvent_to_HEPEVT
static bool GenEvent_to_HEPEVT(const GenEvent *evt)
Convert GenEvent to HEPEVT.
Definition: HEPEVT_Wrapper.cc:176
HepMC3::WriterHEPEVT::m_stream
std::ostream * m_stream
Output stream.
Definition: WriterHEPEVT.h:77
HepMC3::WriterHEPEVT::write_hepevt_event_header
virtual void write_hepevt_event_header()
Write event header to file.
Definition: WriterHEPEVT.cc:53
HepMC3::WriterHEPEVT::close
void close()
Close file stream.
Definition: WriterHEPEVT.cc:71