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 
141  // Print particles
142  std::map<ConstGenVertexPtr,bool> alreadywritten;
143  for(ConstGenParticlePtr p: evt.particles() ) {
144 
145  // Check to see if we need to write a vertex first
146  ConstGenVertexPtr v = p->production_vertex();
147  int parent_object = 0;
148 
149  if (v) {
150  // Check if we need this vertex at all
151  //Yes, use vertex as parent object
152  if ( v->particles_in().size() > 1 || !v->data().is_zero() ) parent_object = v->id();
153  //No, use particle as parent object
154  //TODO: add check for attributes of this vertex
155  else if ( v->particles_in().size() == 1 ) parent_object = v->particles_in()[0]->id();
156  //Usage of map instead of simple countewr helps to deal with events with random ids of vertices.
157  if (alreadywritten.find(v)==alreadywritten.end()&&parent_object<0)
158  { write_vertex(v); alreadywritten[v]=true;}
159  }
160 
161  write_particle( p, parent_object );
162  }
163  alreadywritten.clear();
164 
165  // Flush rest of the buffer to file
166  forced_flush();
167 }
168 
169 
171  if ( m_buffer ) return;
172  while( m_buffer==nullptr && m_buffer_size >= 256 ) {
173  try {
174  m_buffer = new char[ m_buffer_size ]();
175  } catch (const std::bad_alloc& e) {
176  delete[] 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  std::vector<int> pids;
210  for(ConstGenParticlePtr p: v->particles_in() ) pids.push_back(p->id());
211 //We order pids to be able to compare ascii files
212  std::sort(pids.begin(),pids.end());
213  for(auto pid: pids ) {
214 
215  if ( !printed_first ) {
216  m_cursor += sprintf(m_cursor,"%i", pid);
217  printed_first = true;
218  }
219  else m_cursor += sprintf(m_cursor,",%i",pid);
220 
221  flush();
222  }
223 
224  const FourVector &pos = v->position();
225  if ( !pos.is_zero() ) {
226  m_cursor += sprintf(m_cursor,"] @ %.*e",m_precision,pos.x());
227  flush();
228  m_cursor += sprintf(m_cursor," %.*e", m_precision,pos.y());
229  flush();
230  m_cursor += sprintf(m_cursor," %.*e", m_precision,pos.z());
231  flush();
232  m_cursor += sprintf(m_cursor," %.*e\n", m_precision,pos.t());
233  flush();
234  }
235  else {
236  m_cursor += sprintf(m_cursor,"]\n");
237  flush();
238  }
239 }
240 
241 
242 inline void WriterAscii::flush() {
243  // The maximum size of single add to the buffer (other than by
244  // using WriterAscii::write) is 32 bytes. This is a safe value as
245  // we will not allow precision larger than 24 anyway
246  unsigned long length = m_cursor - m_buffer;
247  if ( m_buffer_size - length < 32 ) {
248  // m_file.write( m_buffer, length );
249  m_stream->write( m_buffer, length );
250  m_cursor = m_buffer;
251  }
252 }
253 
254 
256  // m_file.write( m_buffer, m_cursor-m_buffer );
257  m_stream->write( m_buffer, m_cursor - m_buffer );
258  m_cursor = m_buffer;
259 }
260 
261 
263 
264  allocate_buffer();
265 
266  // If no run info object set, create a dummy one.
267  if ( !run_info() ) set_run_info(make_shared<GenRunInfo>());
268 
269  vector<string> names = run_info()->weight_names();
270 
271  if ( !names.empty() ) {
272  string out = names[0];
273  for ( int i = 1, N = names.size(); i < N; ++i )
274  out += "\n" + names[i];
275  m_cursor += sprintf(m_cursor, "W ");
276  flush();
277  write_string(escape(out));
278  m_cursor += sprintf(m_cursor, "\n");
279  }
280 
281  for ( int i = 0, N = run_info()->tools().size(); i < N; ++i ) {
282  string out = "T " + run_info()->tools()[i].name + "\n"
283  + run_info()->tools()[i].version + "\n"
284  + run_info()->tools()[i].description;
285  write_string(escape(out));
286  m_cursor += sprintf(m_cursor, "\n");
287  }
288 
289 
290  for ( auto att: run_info()->attributes() ) {
291  string st;
292  if ( ! att.second->to_string(st) ) {
293  WARNING ("WriterAscii::write_run_info: problem serializing attribute: "<< att.first )
294  }
295  else {
296  m_cursor +=
297  sprintf(m_cursor, "A %s ", att.first.c_str());
298  flush();
299  write_string(escape(st));
300  m_cursor += sprintf(m_cursor, "\n");
301  flush();
302  }
303  }
304 }
305 
306 void WriterAscii::write_particle(ConstGenParticlePtr p, int second_field) {
307 
308  m_cursor += sprintf(m_cursor,"P %i",p->id());
309  flush();
310 
311  m_cursor += sprintf(m_cursor," %i", second_field);
312  flush();
313  m_cursor += sprintf(m_cursor," %i", p->pid() );
314  flush();
315  m_cursor += sprintf(m_cursor," %.*e", m_precision,p->momentum().px() );
316  flush();
317  m_cursor += sprintf(m_cursor," %.*e", m_precision,p->momentum().py());
318  flush();
319  m_cursor += sprintf(m_cursor," %.*e", m_precision,p->momentum().pz() );
320  flush();
321  m_cursor += sprintf(m_cursor," %.*e", m_precision,p->momentum().e() );
322  flush();
323  m_cursor += sprintf(m_cursor," %.*e", m_precision,p->generated_mass() );
324  flush();
325  m_cursor += sprintf(m_cursor," %i\n", p->status() );
326  flush();
327 }
328 
329 
330 inline void WriterAscii::write_string( const string &str ) {
331 
332  // First let's check if string will fit into the buffer
333  unsigned long length = m_cursor-m_buffer;
334 
335  if ( m_buffer_size - length > str.length() ) {
336  strncpy(m_cursor,str.data(),str.length());
337  m_cursor += str.length();
338  flush();
339  }
340  // If not, flush the buffer and write the string directly
341  else {
342  forced_flush();
343  // m_file.write( str.data(), str.length() );
344  m_stream->write( str.data(), str.length() );
345  }
346 }
347 
348 
350  std::ofstream* ofs = dynamic_cast<std::ofstream*>(m_stream);
351  if (ofs && !ofs->is_open()) return;
352  forced_flush();
353  (*m_stream) << "HepMC::Asciiv3-END_EVENT_LISTING" << endl << endl;
354  if (ofs) ofs->close();
355 }
356 
357 
358 } // 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:330
void forced_flush()
Inline function forcing flush to the output stream.
Definition: WriterAscii.cc:255
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:170
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:415
void write_particle(ConstGenParticlePtr p, int second_field)
Write particle.
Definition: WriterAscii.cc:306
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:349
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:262
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:242