33 const std::string header =
"HepMC::Version " +
version() +
"\nHepMC::Asciiv3-START_EVENT_LISTING\n";
34 m_file.write(header.data(), header.length());
53 const std::string header =
"HepMC::Version " +
version() +
"\nHepMC::Asciiv3-START_EVENT_LISTING\n";
54 m_stream->write(header.data(), header.length());
72 const std::string header =
"HepMC::Version " +
version() +
"\nHepMC::Asciiv3-START_EVENT_LISTING\n";
73 m_stream->write(header.data(), header.length());
95 auto float_printf_specifier_option =
m_options.find(
"float_printf_specifier");
96 std::string letter=(float_printf_specifier_option !=
m_options.end())?float_printf_specifier_option->second.substr(0,2):
"e";
97 if (letter !=
"e" && letter !=
"E" && letter !=
"G" && letter !=
"g" && letter !=
"f" && letter !=
"F" ) letter =
"e";
118 HEPMC3_WARNING_LEVEL(600,
"WriterAscii::write_event: GenEvents contain different GenRunInfo objects from - only the first such object will be serialized.")
124 std::string especifier =
"E " + std::to_string(evt.
event_number()) +
" "
125 + std::to_string(evt.
vertices().size()) +
" "
126 + std::to_string(evt.
particles().size());
142 if ( !evt.
weights().empty() ) {
144 for (
const auto& w: evt.
weights())
155 for (
const auto& vt2: vt1.second ) {
157 bool status = vt2.second->to_string(st);
160 HEPMC3_WARNING_LEVEL(300,
"WriterAscii::write_event: problem serializing attribute: " << vt1.first)
176 std::map<int, bool> alreadywritten;
177 for (
const ConstGenParticlePtr& p: evt.
particles()) {
179 ConstGenVertexPtr v = p->production_vertex();
180 int parent_object = 0;
185 if ( v->particles_in().size() > 1 || !v->data().is_zero() ) { parent_object = v->id(); }
189 if ( v->particles_in().size() == 1 ) { parent_object = v->particles_in().front()->id();}
190 else {
if ( v->particles_in().empty() ) {
HEPMC3_DEBUG(30,
"WriterAscii::write_event - found a vertex without incoming particles: " << v->id());}}
193 if (alreadywritten.count(v->id()) == 0 && parent_object < 0)
199 alreadywritten.clear();
211 }
catch (
const std::bad_alloc& e) {
228 ret.reserve(s.length()*2);
229 for ( std::string::const_iterator it = s.begin(); it != s.end(); ++it ) {
247 std::vector<int> pids;
248 pids.reserve(v->particles_in().size());
249 for (
const ConstGenParticlePtr& p: v->particles_in()) pids.emplace_back(p->id());
251 std::sort(pids.begin(), pids.end());
252 for (
const auto& p: pids) vlist.append( std::to_string(p).append(
",") );
253 if ( !pids.empty() ) vlist.pop_back();
289 const std::vector<std::string> names =
run_info()->weight_names();
291 if ( !names.empty() ) {
292 std::string out = names[0];
293 for (
int i = 1, N = names.size(); i < N; ++i ) {
294 out +=
"\n" + names[i];
302 for (
const auto& tool:
run_info()->tools()) {
303 std::string out =
"T " + tool.name +
"\n" + tool.version +
"\n" + tool.description;
309 for (
const auto& att:
run_info()->attributes() ) {
311 if ( !att.second->to_string(st) ) {
312 HEPMC3_WARNING_LEVEL(300,
"WriterAscii::write_run_info: problem serializing attribute: " << att.first)
328 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());
336 strncpy(
m_cursor, str.data(), str.length());
343 m_stream->write(str.data(), str.length());
350 auto* ofs =
dynamic_cast<std::ofstream*
>(
m_stream);
351 if (ofs && !ofs->is_open())
return;
353 const std::string footer(
"HepMC::Asciiv3-END_EVENT_LISTING\n\n");
356 if (ofs) ofs->close();
361 if (prec < 2 || prec > 24)
return;
371 if (size < 1024)
return;
#define HEPMC3_WARNING_LEVEL(LEVEL, MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
#define HEPMC3_ERROR_LEVEL(LEVEL, MESSAGE)
Macro for printing error messages.
#define HEPMC3_DEBUG(LEVEL, MESSAGE)
Macro for printing debug messages with appropriate debug level.
Definition of class GenEvent.
Definition of class GenParticle.
Definition of class GenVertex.
Definition of class Units.
Declaration of the Verrion functions and some macros.
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::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
const Units::LengthUnit & length_unit() const
Get length unit.
const FourVector & event_pos() const
Vertex representing the overall event position.
std::map< std::string, std::map< int, std::shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
const std::vector< double > & weights() const
Get event weight values as a vector.
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
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.
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.
int m_precision
Output precision.
void write_particle(const ConstGenParticlePtr &p, int second_field)
Write particle.
std::string m_vertex_short_printf_specifier
the specifier of printf used for zero vertices
void write_vertex(const ConstGenVertexPtr &v)
Write vertex.
std::shared_ptr< std::ostream > m_shared_stream
Output temp. stream.
std::ofstream m_file
Output file.
static std::string escape(const std::string &s)
Escape '\' and ' ' characters in string.
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.
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.
virtual void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
virtual std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
std::map< std::string, std::string > m_options
options
std::string version()
Get the HepMC library version string.