HepMC3 event record library
WriterAscii.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 WriterAscii.cc
8 /// @brief Implementation of \b class WriterAscii
9 ///
10 #include "HepMC3/WriterAscii.h"
11 
12 #include "HepMC3/Version.h"
13 #include "HepMC3/GenEvent.h"
14 #include "HepMC3/GenParticle.h"
15 #include "HepMC3/GenVertex.h"
16 #include "HepMC3/Units.h"
17 #include <cstring>
18 #include <algorithm>//min max for VS2017
19 namespace HepMC3 {
20 
21 
22 WriterAscii::WriterAscii(const std::string &filename, shared_ptr<GenRunInfo> run)
23  : m_file(filename),
24  m_stream(&m_file),
25  m_precision(16),
26  m_buffer(nullptr),
27  m_cursor(nullptr),
28  m_buffer_size( 256*1024 )
29 {
30  set_run_info(run);
31  if ( !m_file.is_open() ) {
32  ERROR( "WriterAscii: could not open output file: "<<filename )
33  } else {
34  m_file << "HepMC::Version " << version() << std::endl;
35  m_file << "HepMC::Asciiv3-START_EVENT_LISTING" << std::endl;
36  if ( run_info() ) write_run_info();
37  }
38 }
39 
40 
41 WriterAscii::WriterAscii(std::ostream &stream, shared_ptr<GenRunInfo> run)
42  : m_file(),
43  m_stream(&stream),
44  m_precision(16),
45  m_buffer(nullptr),
46  m_cursor(nullptr),
47  m_buffer_size( 256*1024 )
48 
49 {
50  set_run_info(run);
51  (*m_stream) << "HepMC::Version " << version() << std::endl;
52  (*m_stream) << "HepMC::Asciiv3-START_EVENT_LISTING" << std::endl;
53  if ( run_info() ) write_run_info();
54 }
55 
56 
58  close();
59  if ( m_buffer ) delete[] m_buffer;
60 }
61 
62 
64 
65  // if ( !m_file.is_open() ) return;
67  if ( !m_buffer ) return;
68 
69  // Make sure nothing was left from previous event
70  flush();
71 
72  if ( !run_info() ) {
73  set_run_info(evt.run_info());
75  } else {
76  if ( evt.run_info() && run_info() != evt.run_info() ) {
77  WARNING( "WriterAscii::write_event: GenEvents contain "
78  "different GenRunInfo objects from - only the "
79  "first such object will be serialized." )
80  }
81  // else {
82  //write_run_info();
83  // }
84  }
85 
86  // Write event info
87  m_cursor += sprintf(m_cursor, "E %d %lu %lu", evt.event_number(), evt.vertices().size(), evt.particles().size());
88  flush();
89 
90  // Write event position if not zero
91  const FourVector &pos = evt.event_pos();
92  if ( !pos.is_zero() ) {
93  m_cursor += sprintf(m_cursor," @ %.*e",m_precision,pos.x());
94  flush();
95  m_cursor += sprintf(m_cursor," %.*e", m_precision,pos.y());
96  flush();
97  m_cursor += sprintf(m_cursor," %.*e", m_precision,pos.z());
98  flush();
99  m_cursor += sprintf(m_cursor," %.*e", m_precision,pos.t());
100  flush();
101  }
102 
103  m_cursor += sprintf(m_cursor,"\n");
104  flush();
105 
106  // Write units
107  m_cursor += sprintf(m_cursor, "U %s %s\n", Units::name(evt.momentum_unit()).c_str(), Units::name(evt.length_unit()).c_str());
108  flush();
109 
110  // Write weight values if present
111  if ( evt.weights().size() ) {
112  m_cursor += sprintf(m_cursor, "W");
113  for (auto w: evt.weights())
114  m_cursor += sprintf(m_cursor, " %.*e",std::min(3*m_precision,22), w);
115  m_cursor += sprintf(m_cursor, "\n");
116  flush();
117  }
118 
119  // Write attributes
120  for ( auto vt1: evt.attributes() ) {
121  for ( auto vt2: vt1.second ) {
122 
123  string st;
124  bool status = vt2.second->to_string(st);
125 
126  if( !status ) {
127  WARNING( "WriterAscii::write_event: problem serializing attribute: "<<vt1.first )
128  }
129  else {
130  m_cursor +=
131  sprintf(m_cursor, "A %i %s ",vt2.first,vt1.first.c_str());
132  flush();
133  write_string(escape(st));
134  m_cursor += sprintf(m_cursor, "\n");
135  flush();
136  }
137  }
138  }
139 
140  int vertices_processed = 0;
141  int lowest_vertex_id = 0;
142 
143  // Print particles
144  for(ConstGenParticlePtr p: evt.particles() ) {
145 
146  // Check to see if we need to write a vertex first
147  ConstGenVertexPtr v = p->production_vertex();
148  int production_vertex = 0;
149 
150  if (v) {
151 
152  // Check if we need this vertex at all
153  if ( v->particles_in().size() > 1 || !v->data().is_zero() ) production_vertex = v->id();
154  else if ( v->particles_in().size() == 1 ) production_vertex = v->particles_in()[0]->id();
155 
156  if (production_vertex < lowest_vertex_id) {
157  write_vertex(v);
158  }
159 
160  ++vertices_processed;
161  lowest_vertex_id = v->id();
162  }
163 
164  write_particle( p, production_vertex );
165  }
166 
167  // Flush rest of the buffer to file
168  forced_flush();
169 }
170 
171 
173  if ( m_buffer ) return;
174  while( !m_buffer && m_buffer_size >= 256 ) {
175  m_buffer = new char[ m_buffer_size ]();
176  if (!m_buffer) {
177  m_buffer_size /= 2;
178  WARNING( "WriterAscii::allocate_buffer: buffer size too large. Dividing by 2. New size: " << m_buffer_size )
179  }
180  }
181 
182  if ( !m_buffer ) {
183  ERROR( "WriterAscii::allocate_buffer: could not allocate buffer!" )
184  return;
185  }
186  m_cursor = m_buffer;
187 }
188 
189 
190 string WriterAscii::escape(const string& s) const {
191  string ret;
192  ret.reserve( s.length()*2 );
193  for ( string::const_iterator it = s.begin(); it != s.end(); ++it ) {
194  switch ( *it ) {
195  case '\\': ret += "\\\\"; break;
196  case '\n': ret += "\\|"; break;
197  default: ret += *it;
198  }
199  }
200  return ret;
201 }
202 
203 void WriterAscii::write_vertex(ConstGenVertexPtr v) {
204 
205  m_cursor += sprintf( m_cursor, "V %i %i [",v->id(),v->status() );
206  flush();
207 
208  bool printed_first = false;
209 
210  for(ConstGenParticlePtr p: v->particles_in() ) {
211 
212  if ( !printed_first ) {
213  m_cursor += sprintf(m_cursor,"%i", p->id());
214  printed_first = true;
215  }
216  else m_cursor += sprintf(m_cursor,",%i",p->id());
217 
218  flush();
219  }
220 
221  const FourVector &pos = v->position();
222  if ( !pos.is_zero() ) {
223  m_cursor += sprintf(m_cursor,"] @ %.*e",m_precision,pos.x());
224  flush();
225  m_cursor += sprintf(m_cursor," %.*e", m_precision,pos.y());
226  flush();
227  m_cursor += sprintf(m_cursor," %.*e", m_precision,pos.z());
228  flush();
229  m_cursor += sprintf(m_cursor," %.*e\n", m_precision,pos.t());
230  flush();
231  }
232  else {
233  m_cursor += sprintf(m_cursor,"]\n");
234  flush();
235  }
236 }
237 
238 
239 inline void WriterAscii::flush() {
240  // The maximum size of single add to the buffer (other than by
241  // using WriterAscii::write) is 32 bytes. This is a safe value as
242  // we will not allow precision larger than 24 anyway
243  unsigned long length = m_cursor - m_buffer;
244  if ( m_buffer_size - length < 32 ) {
245  // m_file.write( m_buffer, length );
246  m_stream->write( m_buffer, length );
247  m_cursor = m_buffer;
248  }
249 }
250 
251 
253  // m_file.write( m_buffer, m_cursor-m_buffer );
254  m_stream->write( m_buffer, m_cursor - m_buffer );
255  m_cursor = m_buffer;
256 }
257 
258 
260 
261  allocate_buffer();
262 
263  // If no run info object set, create a dummy one.
264  if ( !run_info() ) set_run_info(make_shared<GenRunInfo>());
265 
266  vector<string> names = run_info()->weight_names();
267 
268  if ( !names.empty() ) {
269  string out = names[0];
270  for ( int i = 1, N = names.size(); i < N; ++i )
271  out += "\n" + names[i];
272  m_cursor += sprintf(m_cursor, "W ");
273  flush();
274  write_string(escape(out));
275  m_cursor += sprintf(m_cursor, "\n");
276  }
277 
278  for ( int i = 0, N = run_info()->tools().size(); i < N; ++i ) {
279  string out = "T " + run_info()->tools()[i].name + "\n"
280  + run_info()->tools()[i].version + "\n"
281  + run_info()->tools()[i].description;
282  write_string(escape(out));
283  m_cursor += sprintf(m_cursor, "\n");
284  }
285 
286 
287  for ( auto att: run_info()->attributes() ) {
288  string st;
289  if ( ! att.second->to_string(st) ) {
290  WARNING ("WriterAscii::write_run_info: problem serializing attribute: "<< att.first )
291  }
292  else {
293  m_cursor +=
294  sprintf(m_cursor, "A %s ", att.first.c_str());
295  flush();
296  write_string(escape(st));
297  m_cursor += sprintf(m_cursor, "\n");
298  flush();
299  }
300  }
301 }
302 
303 void WriterAscii::write_particle(ConstGenParticlePtr p, int second_field) {
304 
305  m_cursor += sprintf(m_cursor,"P %i",p->id());
306  flush();
307 
308  m_cursor += sprintf(m_cursor," %i", second_field);
309  flush();
310  m_cursor += sprintf(m_cursor," %i", p->pid() );
311  flush();
312  m_cursor += sprintf(m_cursor," %.*e", m_precision,p->momentum().px() );
313  flush();
314  m_cursor += sprintf(m_cursor," %.*e", m_precision,p->momentum().py());
315  flush();
316  m_cursor += sprintf(m_cursor," %.*e", m_precision,p->momentum().pz() );
317  flush();
318  m_cursor += sprintf(m_cursor," %.*e", m_precision,p->momentum().e() );
319  flush();
320  m_cursor += sprintf(m_cursor," %.*e", m_precision,p->generated_mass() );
321  flush();
322  m_cursor += sprintf(m_cursor," %i\n", p->status() );
323  flush();
324 }
325 
326 
327 inline void WriterAscii::write_string( const string &str ) {
328 
329  // First let's check if string will fit into the buffer
330  unsigned long length = m_cursor-m_buffer;
331 
332  if ( m_buffer_size - length > str.length() ) {
333  strncpy(m_cursor,str.data(),str.length());
334  m_cursor += str.length();
335  flush();
336  }
337  // If not, flush the buffer and write the string directly
338  else {
339  forced_flush();
340  // m_file.write( str.data(), str.length() );
341  m_stream->write( str.data(), str.length() );
342  }
343 }
344 
345 
347  std::ofstream* ofs = dynamic_cast<std::ofstream*>(m_stream);
348  if (ofs && !ofs->is_open()) return;
349  forced_flush();
350  (*m_stream) << "HepMC::Asciiv3-END_EVENT_LISTING" << endl << endl;
351  if (ofs) ofs->close();
352 }
353 
354 
355 } // namespace HepMC3
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
Definition: GenEvent.cc:44
void write_string(const std::string &str)
Inline function for writing strings.
Definition: WriterAscii.cc:327
void forced_flush()
Inline function forcing flush to the output stream.
Definition: WriterAscii.cc:252
int m_precision
Output precision.
Definition: WriterAscii.h:127
std::ostream * m_stream
Output stream.
Definition: WriterAscii.h:125
#define ERROR(MESSAGE)
Macro for printing error messages.
Definition: Errors.h:23
double t() const
Time component of position/displacement.
Definition: FourVector.h:76
Definition of class GenParticle.
Definition of class GenVertex.
bool is_zero() const
Check if the length of this vertex is zero.
Definition: FourVector.h:154
char * m_cursor
Cursor inside stream buffer.
Definition: WriterAscii.h:129
Definition of class WriterAscii.
std::string version()
Get the HepMC library version string.
Definition: Version.h:20
const Units::LengthUnit & length_unit() const
Get length unit.
Definition: GenEvent.h:143
double z() const
z-component of position/displacement
Definition: FourVector.h:71
double x() const
x-component of position/displacement
Definition: FourVector.h:61
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
Definition: WriterAscii.cc:172
static std::string name(MomentumUnit u)
Get name of momentum unit.
Definition: Units.h:56
int event_number() const
Get event number.
Definition: GenEvent.h:136
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition: GenEvent.h:125
WriterAscii(const std::string &filename, shared_ptr< GenRunInfo > run=shared_ptr< GenRunInfo >())
Constructor.
Definition: WriterAscii.cc:22
const FourVector & event_pos() const
Vertex representing the overall event position.
Definition: GenEvent.cc:388
void write_particle(ConstGenParticlePtr p, int second_field)
Write particle.
Definition: WriterAscii.cc:303
unsigned long m_buffer_size
Buffer size.
Definition: WriterAscii.h:130
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Writer.h:42
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
Definition: GenEvent.h:141
Stores event-related information.
Definition: GenEvent.h:42
Generic 4-vector.
Definition: FourVector.h:34
char * m_buffer
Stream buffer.
Definition: WriterAscii.h:128
void write_vertex(ConstGenVertexPtr v)
Write vertex.
Definition: WriterAscii.cc:203
Definition of class Units.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Writer.h:37
#define WARNING(MESSAGE)
Macro for printing warning messages.
Definition: Errors.h:26
std::string escape(const std::string &s) const
Escape &#39;\&#39; and &#39; &#39; characters in string.
Definition: WriterAscii.cc:190
std::map< string, std::map< int, shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
Definition: GenEvent.h:229
~WriterAscii()
Destructor.
Definition: WriterAscii.cc:57
void close()
Close file stream.
Definition: WriterAscii.cc:346
double y() const
y-component of position/displacement
Definition: FourVector.h:66
void write_run_info()
Write the GenRunInfo object to file.
Definition: WriterAscii.cc:259
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition: GenEvent.h:87
std::ofstream m_file
Output file.
Definition: WriterAscii.h:124
Definition of class GenEvent.
void write_event(const GenEvent &evt)
Write event to file.
Definition: WriterAscii.cc:63
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
Definition: GenEvent.cc:40
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
Definition: WriterAscii.cc:239