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 )
43 m_file <<
"HepMC::IO_GenEvent-START_EVENT_LISTING" << std::endl;
54 m_buffer_size(256*1024),
57 HEPMC3_WARNING(
"WriterAsciiHepMC2::WriterAsciiHepMC2: HepMC2 IO_GenEvent format is outdated. Please use HepMC3 Asciiv3 format instead.")
60 (*m_stream) <<
"HepMC::Version " <<
version() << std::endl;
61 (*m_stream) <<
"HepMC::IO_GenEvent-START_EVENT_LISTING" << std::endl;
77 auto float_printf_specifier_option =
m_options.find(
"float_printf_specifier");
78 std::string letter=(float_printf_specifier_option !=
m_options.end())?float_printf_specifier_option->second.substr(0,2):
"e";
79 if (letter !=
"e" && letter !=
"E" && letter !=
"G" && letter !=
"g" && letter !=
"f" && letter !=
"F" ) letter =
"e";
93 std::shared_ptr<IntAttribute> A_signal_process_vertex = evt.
attribute<
IntAttribute>(
"signal_process_vertex");
95 double event_scale = A_event_scale?(A_event_scale->value()):0.0;
96 double alphaQED = A_alphaQED?(A_alphaQED->value()):0.0;
97 double alphaQCD = A_alphaQCD?(A_alphaQCD->value()):0.0;
98 int signal_process_id = A_signal_process_id?(A_signal_process_id->value()):0;
99 int mpi = A_mpi?(A_mpi->value()):0;
100 int signal_process_vertex = A_signal_process_vertex?(A_signal_process_vertex->value()):0;
102 std::vector<long> m_random_states;
104 if (random_states_a) {
105 m_random_states = random_states_a->
value();
107 m_random_states.reserve(100);
108 for (
int i = 0; i < 100; i++)
110 std::shared_ptr<LongAttribute> rs = evt.
attribute<
LongAttribute>(
"random_states"+std::to_string((
long long unsigned int)i));
112 m_random_states.push_back(rs->value());
117 std::vector<int> beams;
120 for (ConstGenVertexPtr v: evt.
vertices())
122 for (ConstGenParticlePtr p: v->particles_in())
124 if (!p->production_vertex()) {
if (p->status() == 4) beams.push_back(idbeam); idbeam++; }
125 else if (p->production_vertex()->id() == 0) {
if (p->status() == 4) beams.push_back(idbeam); idbeam++; }
127 for (ConstGenParticlePtr p: v->particles_out()) {
if (p->status() == 4) beams.push_back(idbeam); idbeam++; }
132 if (beams.size() > 0) idbeam1 += beams[0] + 1;
133 if (beams.size() > 1) idbeam2 += beams[1] + 1;
141 signal_process_vertex,
148 for (
size_t q = 0; q < m_random_states.size(); q++)
157 for (
double w: evt.
weights()) {
164 const std::vector<std::string> names =
run_info()->weight_names();
165 for (
size_t q = 0; q < evt.
weights().size(); q++)
167 if (q < names.size())
192 hi->spectator_neutrons,
193 hi->spectator_protons,
194 hi->N_Nwounded_collisions,
195 hi->Nwounded_N_collisions,
196 hi->Nwounded_Nwounded_collisions,
197 hi->impact_parameter,
198 hi->event_plane_angle,
211 HEPMC3_WARNING(
"WriterAsciiHepMC2::write_event: problem serializing GenPdfInfo attribute")
223 for (ConstGenVertexPtr v: evt.
vertices() )
225 int production_vertex = 0;
226 production_vertex = v->id();
228 for (ConstGenParticlePtr p: v->particles_in())
230 if (!p->production_vertex())
write_particle( p, production_vertex );
233 if (p->production_vertex()->id() == 0)
write_particle( p, production_vertex );
236 for (ConstGenParticlePtr p: v->particles_out())
251 }
catch (
const std::bad_alloc& e) {
254 HEPMC3_WARNING(
"WriterAsciiHepMC2::allocate_buffer:" << e.what() <<
" buffer size too large. Dividing by 2. New size: " <<
m_buffer_size)
260 HEPMC3_ERROR(
"WriterAsciiHepMC2::allocate_buffer: could not allocate buffer!")
271 ret.reserve(s.length()*2);
272 for ( std::string::const_iterator it = s.begin(); it != s.end(); ++it )
291 std::vector<double> weights;
294 weights = weights_a->
value();
296 weights.reserve(100);
297 for (
int i = 0; i < 100; i++)
299 std::shared_ptr<DoubleAttribute> rs = v->attribute<
DoubleAttribute>(
"weight"+std::to_string((
long long unsigned int)i));
301 weights.push_back(rs->value());
307 for (ConstGenParticlePtr p: v->particles_in())
309 if (!p->production_vertex()) orph++;
312 if (p->production_vertex()->id() == 0) orph++;
327 m_cursor += sprintf(
m_cursor,
" %i %zu %zu", orph, v->particles_out().size(), weights.size());
373 if (p->end_vertex()->id() != 0)
374 ev = p->end_vertex()->id();
376 std::shared_ptr<DoubleAttribute> A_theta = p->attribute<
DoubleAttribute>(
"theta");
377 std:: shared_ptr<DoubleAttribute> A_phi = p->attribute<
DoubleAttribute>(
"phi");
384 std::shared_ptr<VectorIntAttribute> A_flows = p->attribute<
VectorIntAttribute>(
"flows");
387 std::vector<int> flowsv = A_flows->
value();
388 std::string flowss =
" " + std::to_string(flowsv.size());
389 for (
size_t k = 0; k < flowsv.size(); k++) { flowss += (
" " + std::to_string(k+1) +
" " + std::to_string(flowsv.at(k))); }
393 std::shared_ptr<IntAttribute> A_flow1 = p->attribute<
IntAttribute>(
"flow1");
394 std::shared_ptr<IntAttribute> A_flow2 = p->attribute<
IntAttribute>(
"flow2");
395 std::shared_ptr<IntAttribute> A_flow3 = p->attribute<
IntAttribute>(
"flow3");
397 if (A_flow1) flowsize++;
398 if (A_flow2) flowsize++;
399 if (A_flow3) flowsize++;
400 std::string flowss =
" " + std::to_string(flowsize);
401 if (A_flow1) flowss += (
" 1 " + std::to_string(A_flow1->value()));
402 if (A_flow2) flowss += (
" 2 " + std::to_string(A_flow2->value()));
403 if (A_flow3) flowss += (
" 3 " + std::to_string(A_flow3->value()));
416 strncpy(
m_cursor, str.data(), str.length());
424 m_stream->write(str.data(), str.length());
431 std::ofstream* ofs =
dynamic_cast<std::ofstream*
>(
m_stream);
432 if (ofs && !ofs->is_open())
return;
434 (*m_stream) <<
"HepMC::IO_GenEvent-END_EVENT_LISTING" << std::endl << std::endl;
435 if (ofs) ofs->close();
440 if (prec < 2 || prec > 24)
return;
450 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.