14#include "HepMC3/Version.h"
30 m_buffer_size(256*1024),
33 HEPMC3_WARNING(
"WriterAsciiHepMC2::WriterAsciiHepMC2: HepMC2 IO_GenEvent format is outdated. Please use HepMC3 Asciiv3 format instead.")
38 HEPMC3_ERROR(
"WriterAsciiHepMC2: could not open output file: " << filename )
42 const std::string header =
"HepMC::Version " +
version() +
"\nHepMC::IO_GenEvent-START_EVENT_LISTING\n";
43 m_file.write(header.data(), header.length());
54 m_buffer_size(256*1024),
57 HEPMC3_WARNING(
"WriterAsciiHepMC2::WriterAsciiHepMC2: HepMC2 IO_GenEvent format is outdated. Please use HepMC3 Asciiv3 format instead.")
60 const std::string header =
"HepMC::Version " +
version() +
"\nHepMC::IO_GenEvent-START_EVENT_LISTING\n";
61 m_stream->write(header.data(), header.length());
67 m_shared_stream(s_stream),
68 m_stream(s_stream.get()),
72 m_buffer_size(256*1024),
75 HEPMC3_WARNING(
"WriterAsciiHepMC2::WriterAsciiHepMC2: HepMC2 IO_GenEvent format is outdated. Please use HepMC3 Asciiv3 format instead.")
78 const std::string header =
"HepMC::Version " +
version() +
"\nHepMC::IO_GenEvent-START_EVENT_LISTING\n";
79 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";
109 std::shared_ptr<IntAttribute> A_signal_process_id = evt.
attribute<
IntAttribute>(
"signal_process_id");
111 std::shared_ptr<IntAttribute> A_signal_process_vertex = evt.
attribute<
IntAttribute>(
"signal_process_vertex");
113 double event_scale = A_event_scale?(A_event_scale->value()):0.0;
114 double alphaQED = A_alphaQED?(A_alphaQED->value()):0.0;
115 double alphaQCD = A_alphaQCD?(A_alphaQCD->value()):0.0;
116 int signal_process_id = A_signal_process_id?(A_signal_process_id->value()):0;
117 int mpi = A_mpi?(A_mpi->value()):0;
118 int signal_process_vertex = A_signal_process_vertex?(A_signal_process_vertex->value()):0;
120 std::vector<long> m_random_states;
122 if (random_states_a) {
123 m_random_states = random_states_a->
value();
125 m_random_states.reserve(100);
126 for (
int i = 0; i < 100; i++)
128 std::shared_ptr<LongAttribute> rs = evt.
attribute<
LongAttribute>(
"random_states"+std::to_string((
long long unsigned int)i));
130 m_random_states.push_back(rs->value());
135 std::vector<int> beams;
138 for (ConstGenVertexPtr v: evt.
vertices())
140 for (ConstGenParticlePtr p: v->particles_in())
142 if (!p->production_vertex()) {
if (p->status() == 4) beams.push_back(idbeam); idbeam++; }
143 else if (p->production_vertex()->id() == 0) {
if (p->status() == 4) beams.push_back(idbeam); idbeam++; }
145 for (ConstGenParticlePtr p: v->particles_out()) {
if (p->status() == 4) beams.push_back(idbeam); idbeam++; }
150 if (beams.size() > 0) idbeam1 += beams[0] + 1;
151 if (beams.size() > 1) idbeam2 += beams[1] + 1;
159 signal_process_vertex,
166 for (
size_t q = 0; q < m_random_states.size(); q++)
175 for (
double w: evt.
weights()) {
182 const std::vector<std::string> names =
run_info()->weight_names();
183 for (
size_t q = 0; q < evt.
weights().size(); q++)
185 if (q < names.size())
210 hi->spectator_neutrons,
211 hi->spectator_protons,
212 hi->N_Nwounded_collisions,
213 hi->Nwounded_N_collisions,
214 hi->Nwounded_Nwounded_collisions,
215 hi->impact_parameter,
216 hi->event_plane_angle,
229 HEPMC3_WARNING(
"WriterAsciiHepMC2::write_event: problem serializing GenPdfInfo attribute")
241 for (ConstGenVertexPtr v: evt.
vertices() )
243 int production_vertex = 0;
244 production_vertex = v->id();
246 for (ConstGenParticlePtr p: v->particles_in())
248 if (!p->production_vertex())
write_particle( p, production_vertex );
251 if (p->production_vertex()->id() == 0)
write_particle( p, production_vertex );
254 for (ConstGenParticlePtr p: v->particles_out())
269 }
catch (
const std::bad_alloc& e) {
272 HEPMC3_WARNING(
"WriterAsciiHepMC2::allocate_buffer:" << e.what() <<
" buffer size too large. Dividing by 2. New size: " <<
m_buffer_size)
278 HEPMC3_ERROR(
"WriterAsciiHepMC2::allocate_buffer: could not allocate buffer!")
289 ret.reserve(s.length()*2);
290 for ( std::string::const_iterator it = s.begin(); it != s.end(); ++it )
309 std::vector<double> weights;
312 weights = weights_a->
value();
314 weights.reserve(100);
315 for (
int i = 0; i < 100; i++)
317 std::shared_ptr<DoubleAttribute> rs = v->attribute<
DoubleAttribute>(
"weight"+std::to_string((
long long unsigned int)i));
319 weights.push_back(rs->value());
325 for (ConstGenParticlePtr p: v->particles_in())
327 if (!p->production_vertex()) orph++;
330 if (p->production_vertex()->id() == 0) orph++;
345 m_cursor += sprintf(
m_cursor,
" %i %zu %zu", orph, v->particles_out().size(), weights.size());
391 if (p->end_vertex()->id() != 0)
392 ev = p->end_vertex()->id();
394 std::shared_ptr<DoubleAttribute> A_theta = p->attribute<
DoubleAttribute>(
"theta");
395 std:: shared_ptr<DoubleAttribute> A_phi = p->attribute<
DoubleAttribute>(
"phi");
402 std::shared_ptr<VectorIntAttribute> A_flows = p->attribute<
VectorIntAttribute>(
"flows");
405 std::vector<int> flowsv = A_flows->
value();
406 std::string flowss =
" " + std::to_string(flowsv.size());
407 for (
size_t k = 0; k < flowsv.size(); k++) { flowss += (
" " + std::to_string(k+1) +
" " + std::to_string(flowsv.at(k))); }
411 std::shared_ptr<IntAttribute> A_flow1 = p->attribute<
IntAttribute>(
"flow1");
412 std::shared_ptr<IntAttribute> A_flow2 = p->attribute<
IntAttribute>(
"flow2");
413 std::shared_ptr<IntAttribute> A_flow3 = p->attribute<
IntAttribute>(
"flow3");
415 if (A_flow1) flowsize++;
416 if (A_flow2) flowsize++;
417 if (A_flow3) flowsize++;
418 std::string flowss =
" " + std::to_string(flowsize);
419 if (A_flow1) flowss += (
" 1 " + std::to_string(A_flow1->value()));
420 if (A_flow2) flowss += (
" 2 " + std::to_string(A_flow2->value()));
421 if (A_flow3) flowss += (
" 3 " + std::to_string(A_flow3->value()));
434 strncpy(
m_cursor, str.data(), str.length());
442 m_stream->write(str.data(), str.length());
449 std::ofstream* ofs =
dynamic_cast<std::ofstream*
>(
m_stream);
450 if (ofs && !ofs->is_open())
return;
452 const std::string footer(
"HepMC::IO_GenEvent-END_EVENT_LISTING\n\n");
454 if (ofs) ofs->close();
459 if (prec < 2 || prec > 24)
return;
469 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 WriterAsciiHepMC2.
Attribute that holds a real number as a double.
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 additional information about cross-section.
Stores event-related information.
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
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 std::vector< double > & weights() const
Get event weight values as a vector.
Stores additional information about Heavy Ion generator.
Stores additional information about PDFs.
bool to_string(std::string &att) const override
Implementation of Attribute::to_string.
Attribute that holds an Integer implemented as an int.
Attribute that holds an Integer implemented as an int.
static std::string name(MomentumUnit u)
Get name of momentum unit.
Attribute that holds a vector of FPs of type double.
std::vector< double > value() const
get the value associated to this Attribute.
Attribute that holds a vector of integers of type int.
std::vector< int > value() const
get the value associated to this Attribute.
Attribute that holds a vector of integers of type int.
std::vector< long int > value() const
get the value associated to this Attribute.
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
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.
WriterAsciiHepMC2(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
std::ofstream m_file
Output file.
~WriterAsciiHepMC2()
Destructor.
unsigned long m_particle_counter
Used to set bar codes.
void write_string(const std::string &str)
Inline function for writing strings.
unsigned long m_buffer_size
Buffer size.
void write_event(const GenEvent &evt) override
Write event to file.
void write_vertex(ConstGenVertexPtr v)
Write vertex.
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::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
std::map< std::string, std::string > m_options
options
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
std::string version()
Get the HepMC library version string.