12 #include "HepMC3/Version.h"
28 m_buffer_size( 256*1024 )
32 HEPMC3_ERROR(
"WriterAscii: could not open output file: "<<filename )
35 m_file <<
"HepMC::Asciiv3-START_EVENT_LISTING" << std::endl;
47 m_buffer_size( 256*1024 )
51 (*m_stream) <<
"HepMC::Version " <<
version() << std::endl;
52 (*m_stream) <<
"HepMC::Asciiv3-START_EVENT_LISTING" << std::endl;
78 "different GenRunInfo objects from - only the "
79 "first such object will be serialized." )
121 for (
auto vt2: vt1.second ) {
124 bool status = vt2.second->to_string(st);
127 HEPMC3_WARNING(
"WriterAscii::write_event: problem serializing attribute: "<<vt1.first )
131 sprintf(
m_cursor,
"A %i %s ",vt2.first,vt1.first.c_str());
142 std::map<ConstGenVertexPtr,bool> alreadywritten;
143 for(ConstGenParticlePtr p: evt.
particles() ) {
146 ConstGenVertexPtr v = p->production_vertex();
147 int parent_object = 0;
152 if ( v->particles_in().size() > 1 || !v->data().is_zero() ) parent_object = v->id();
155 else if ( v->particles_in().size() == 1 ) parent_object = v->particles_in()[0]->id();
157 if (alreadywritten.find(v)==alreadywritten.end()&&parent_object<0)
163 alreadywritten.clear();
175 }
catch (
const std::bad_alloc& e) {
183 HEPMC3_ERROR(
"WriterAscii::allocate_buffer: could not allocate buffer!" )
192 ret.reserve( s.length()*2 );
193 for ( std::string::const_iterator it = s.begin(); it != s.end(); ++it ) {
213 bool printed_first =
false;
214 std::vector<int> pids;
215 for(ConstGenParticlePtr p: v->particles_in() ) pids.push_back(p->id());
217 std::sort(pids.begin(),pids.end());
218 for(
auto pid: pids ) {
220 if ( !printed_first ) {
222 printed_first =
true;
254 m_stream->write( m_buffer, length );
274 std::vector<std::string> names =
run_info()->weight_names();
276 if ( !names.empty() ) {
277 std::string out = names[0];
278 for (
int i = 1, N = names.size(); i < N; ++i )
279 out +=
"\n" + names[i];
286 for (
int i = 0, N =
run_info()->tools().size(); i < N; ++i ) {
287 std::string out =
"T " +
run_info()->tools()[i].name +
"\n"
288 +
run_info()->tools()[i].version +
"\n"
289 +
run_info()->tools()[i].description;
295 for (
auto att:
run_info()->attributes() ) {
297 if ( ! att.second->to_string(st) ) {
298 HEPMC3_WARNING (
"WriterAscii::write_run_info: problem serializing attribute: "<< att.first )
302 sprintf(
m_cursor,
"A %s ", att.first.c_str());
341 strncpy(
m_cursor,str.data(),str.length());
349 m_stream->write( str.data(), str.length() );
355 std::ofstream* ofs =
dynamic_cast<std::ofstream*
>(
m_stream);
356 if (ofs && !ofs->is_open())
return;
358 (*m_stream) <<
"HepMC::Asciiv3-END_EVENT_LISTING" << std::endl << std::endl;
359 if (ofs) ofs->close();
364 if (prec < 2 || prec > 24)
return;
374 if (size < 256)
return;
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
void write_string(const std::string &str)
Inline function for writing strings.
void forced_flush()
Inline function forcing flush to the output stream.
int m_precision
Output precision.
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
std::ostream * m_stream
Output stream.
void set_buffer_size(const size_t &size)
Set buffer size (in bytes)
double t() const
Time component of position/displacement.
Definition of class GenParticle.
Definition of class GenVertex.
bool is_zero() const
Check if the length of this vertex is zero.
char * m_cursor
Cursor inside stream buffer.
Definition of class WriterAscii.
std::string version()
Get the HepMC library version string.
const Units::LengthUnit & length_unit() const
Get length unit.
double z() const
z-component of position/displacement
double x() const
x-component of position/displacement
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
WriterAscii(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
static std::string name(MomentumUnit u)
Get name of momentum unit.
int event_number() const
Get event number.
const FourVector & event_pos() const
Vertex representing the overall event position.
void write_particle(ConstGenParticlePtr p, int second_field)
Write particle.
unsigned long m_buffer_size
Buffer size.
void set_precision(const int &prec)
Set output precision.
bool failed() override
Return status of the stream.
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
Stores event-related information.
char * m_buffer
Stream buffer.
void write_vertex(ConstGenVertexPtr v)
Write vertex.
std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
int precision() const
Return output precision.
Definition of class Units.
~WriterAscii()
Destructor.
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
double y() const
y-component of position/displacement
void write_run_info()
Write the GenRunInfo object to file.
void write_event(const GenEvent &evt) override
Write event to file.
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
const std::vector< double > & weights() const
Get event weight values as a vector.
std::ofstream m_file
Output file.
Definition of class GenEvent.
std::string escape(const std::string &s) const
Escape '\' and ' ' characters in string.
std::map< std::string, std::map< int, std::shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
void close() override
Close file stream.
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.