15 #include "HepMC3/Version.h"
30 m_buffer_size(256*1024)
34 HEPMC3_ERROR(
"WriterAscii: could not open output file: " << filename)
37 m_file <<
"HepMC::Asciiv3-START_EVENT_LISTING" << std::endl;
58 m_buffer_size(256*1024)
61 (*m_stream) <<
"HepMC::Version " <<
version() << std::endl;
62 (*m_stream) <<
"HepMC::Asciiv3-START_EVENT_LISTING" << std::endl;
85 auto float_printf_specifier_option =
m_options.find(
"float_printf_specifier");
86 std::string letter=(float_printf_specifier_option !=
m_options.end())?float_printf_specifier_option->second.substr(0,2):
"e";
87 if (letter !=
"e" && letter !=
"E" && letter !=
"G" && letter !=
"g" && letter !=
"f" && letter !=
"F" ) letter =
"e";
109 "different GenRunInfo objects from - only the "
110 "first such object will be serialized.")
116 std::string especifier =
"E " + std::to_string(evt.
event_number()) +
" "
117 + std::to_string(evt.
vertices().size()) +
" "
118 + std::to_string(evt.
particles().size());
147 for (
auto vt2: vt1.second ) {
149 bool status = vt2.second->to_string(st);
152 HEPMC3_WARNING(
"WriterAscii::write_event: problem serializing attribute: " << vt1.first)
168 std::map<int, bool> alreadywritten;
169 for (ConstGenParticlePtr p: evt.
particles()) {
171 ConstGenVertexPtr v = p->production_vertex();
172 int parent_object = 0;
177 if ( v->particles_in().size() > 1 || !v->data().is_zero() ) parent_object = v->id();
180 else if ( v->particles_in().size() == 1 ) parent_object = v->particles_in()[0]->id();
182 if (alreadywritten.find(v->id()) == alreadywritten.end() && parent_object < 0)
188 alreadywritten.clear();
200 }
catch (
const std::bad_alloc& e) {
203 HEPMC3_WARNING(
"WriterAscii::allocate_buffer:" << e.what() <<
" buffer size too large. Dividing by 2. New size: " <<
m_buffer_size)
208 HEPMC3_ERROR(
"WriterAscii::allocate_buffer: could not allocate buffer!")
217 ret.reserve(s.length()*2);
218 for ( std::string::const_iterator it = s.begin(); it != s.end(); ++it ) {
236 std::vector<int> pids;
237 pids.reserve(v->particles_in().size());
238 for (ConstGenParticlePtr p: v->particles_in()) pids.push_back(p->id());
240 std::sort(pids.begin(), pids.end());
241 for (
auto p: pids) vlist.append( std::to_string(p).append(
",") );
242 if ( pids.size() ) vlist.pop_back();
278 const std::vector<std::string> names =
run_info()->weight_names();
280 if ( !names.empty() ) {
281 std::string out = names[0];
282 for (
int i = 1, N = names.size(); i < N; ++i )
283 out +=
"\n" + names[i];
290 for (
int i = 0, N =
run_info()->tools().size(); i < N; ++i) {
291 std::string out =
"T " +
run_info()->tools()[i].name +
"\n"
292 +
run_info()->tools()[i].version +
"\n"
293 +
run_info()->tools()[i].description;
299 for (
auto att:
run_info()->attributes() ) {
301 if ( !att.second->to_string(st) ) {
302 HEPMC3_WARNING(
"WriterAscii::write_run_info: problem serializing attribute: " << att.first)
318 m_cursor += sprintf(
m_cursor,
m_particle_printf_specifier.c_str(), p->id(), second_field, p->pid(), p->momentum().px(), p->momentum().py(), p->momentum().pz(), p->momentum().e(), p->generated_mass(), p->status());
326 strncpy(
m_cursor, str.data(), str.length());
333 m_stream->write(str.data(), str.length());
339 std::ofstream* ofs =
dynamic_cast<std::ofstream*
>(
m_stream);
340 if (ofs && !ofs->is_open())
return;
342 (*m_stream) <<
"HepMC::Asciiv3-END_EVENT_LISTING" << std::endl << std::endl;
343 if (ofs) ofs->close();
348 if (prec < 2 || prec > 24)
return;
358 if (size < 1024)
return;
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition of class GenEvent.
Definition of class GenParticle.
Definition of class GenVertex.
Definition of class Units.
Definition of class WriterAscii.
double t() const
Time component of position/displacement.
bool is_zero() const
Check if the length of this vertex is zero.
double x() const
x-component of position/displacement
double y() const
y-component of position/displacement
double z() const
z-component of position/displacement
Stores event-related information.
int event_number() const
Get event number.
std::map< std::string, std::map< int, std::shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
const FourVector & event_pos() const
Vertex representing the overall event position.
const Units::LengthUnit & length_unit() const
Get length unit.
const std::vector< double > & weights() const
Get event weight values as a vector.
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
static std::string name(MomentumUnit u)
Get name of momentum unit.
void set_buffer_size(const size_t &size)
Set buffer size (in bytes)
void set_precision(const int &prec)
Set output precision.
std::string escape(const std::string &s) const
Escape '\' and ' ' characters in string.
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
char * m_cursor
Cursor inside stream buffer.
bool failed() override
Return status of the stream.
char * m_buffer
Stream buffer.
std::string m_float_printf_specifier
the specifier of printf used for floats
~WriterAscii()
Destructor.
void close() override
Close file stream.
int precision() const
Return output precision.
void write_particle(ConstGenParticlePtr p, int second_field)
Write particle.
int m_precision
Output precision.
std::string m_vertex_short_printf_specifier
the specifier of printf used for zero vertices
std::ofstream m_file
Output file.
void write_string(const std::string &str)
Inline function for writing strings.
unsigned long m_buffer_size
Buffer size.
std::string m_particle_printf_specifier
the specifier of printf used for floats
void write_event(const GenEvent &evt) override
Write event to file.
void write_vertex(ConstGenVertexPtr v)
Write vertex.
WriterAscii(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
std::string m_vertex_long_printf_specifier
the specifier of printf used for vertices
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
void write_run_info()
Write the GenRunInfo object to file.
void forced_flush()
Inline function forcing flush to the output stream.
std::ostream * m_stream
Output stream.
std::map< std::string, std::string > m_options
options
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
std::string version()
Get the HepMC library version string.