28 HEPMC3_WARNING_LEVEL(900,
"WriterAsciiHepMC2::WriterAsciiHepMC2: HepMC2 IO_GenEvent format is outdated. Please use HepMC3 Asciiv3 format instead.")
37 const std::string header =
"HepMC::Version " +
version() +
"\nHepMC::IO_GenEvent-START_EVENT_LISTING\n";
38 m_file.write(header.data(), header.length());
46 HEPMC3_WARNING_LEVEL(900,
"WriterAsciiHepMC2::WriterAsciiHepMC2: HepMC2 IO_GenEvent format is outdated. Please use HepMC3 Asciiv3 format instead.")
49 const std::string header =
"HepMC::Version " +
version() +
"\nHepMC::IO_GenEvent-START_EVENT_LISTING\n";
50 m_stream->write(header.data(), header.length());
58 HEPMC3_WARNING_LEVEL(900,
"WriterAsciiHepMC2::WriterAsciiHepMC2: HepMC2 IO_GenEvent format is outdated. Please use HepMC3 Asciiv3 format instead.")
61 const std::string header =
"HepMC::Version " +
version() +
"\nHepMC::IO_GenEvent-START_EVENT_LISTING\n";
62 m_stream->write(header.data(), header.length());
78 auto float_printf_specifier_option =
m_options.find(
"float_printf_specifier");
79 std::string letter=(float_printf_specifier_option !=
m_options.end())?float_printf_specifier_option->second.substr(0,2):
"e";
80 if (letter !=
"e" && letter !=
"E" && letter !=
"G" && letter !=
"g" && letter !=
"f" && letter !=
"F" ) letter =
"e";
94 std::shared_ptr<IntAttribute> A_signal_process_vertex = evt.
attribute<
IntAttribute>(
"signal_process_vertex");
96 double event_scale = A_event_scale?(A_event_scale->value()):0.0;
97 double alphaQED = A_alphaQED?(A_alphaQED->value()):0.0;
98 double alphaQCD = A_alphaQCD?(A_alphaQCD->value()):0.0;
99 int signal_process_id = A_signal_process_id?(A_signal_process_id->value()):0;
100 int mpi = A_mpi?(A_mpi->value()):0;
101 int signal_process_vertex = A_signal_process_vertex?(A_signal_process_vertex->value()):0;
103 std::vector<long> m_random_states;
105 if (random_states_a) {
106 m_random_states = random_states_a->value();
108 m_random_states.reserve(100);
109 for (
int i = 0; i < 100; i++)
111 std::shared_ptr<LongAttribute> rs = evt.
attribute<
LongAttribute>(
"random_states"+std::to_string(
static_cast<long long unsigned int>(i)));
113 m_random_states.emplace_back(rs->value());
118 std::vector<int> beams;
121 for (
const ConstGenVertexPtr& v: evt.
vertices())
123 for (
const ConstGenParticlePtr& p: v->particles_in())
125 if (!p->production_vertex()) {
if (p->status() == 4) beams.emplace_back(idbeam); idbeam++; }
126 else {
if (p->production_vertex()->id() == 0) {
if (p->status() == 4) beams.emplace_back(idbeam); idbeam++; }}
128 for (
const ConstGenParticlePtr& p: v->particles_out()) {
if (p->status() == 4) beams.push_back(idbeam); idbeam++; }
133 if (!beams.empty()) idbeam1 += beams[0] + 1;
134 if (beams.size() > 1) idbeam2 += beams[1] + 1;
142 signal_process_vertex,
149 for (
size_t q = 0; q < m_random_states.size(); q++)
158 for (
const double& w: evt.
weights()) {
165 const std::vector<std::string> names =
run_info()->weight_names();
166 for (
size_t q = 0; q < evt.
weights().size(); q++)
168 if (q < names.size()) {
194 hi->spectator_neutrons,
195 hi->spectator_protons,
196 hi->N_Nwounded_collisions,
197 hi->Nwounded_N_collisions,
198 hi->Nwounded_Nwounded_collisions,
199 hi->impact_parameter,
200 hi->event_plane_angle,
210 bool status = pi->to_string(st);
213 HEPMC3_WARNING_LEVEL(300,
"WriterAsciiHepMC2::write_event: problem serializing GenPdfInfo attribute")
225 for (
const ConstGenVertexPtr& v: evt.
vertices() )
227 int production_vertex = 0;
228 production_vertex = v->id();
230 for (
const ConstGenParticlePtr& p: v->particles_in())
232 if (!p->production_vertex()) {
write_particle( p, production_vertex ); }
235 if (p->production_vertex()->id() == 0)
write_particle( p, production_vertex );
238 for (
const ConstGenParticlePtr& p: v->particles_out()) {
254 }
catch (
const std::bad_alloc& e) {
263 HEPMC3_ERROR_LEVEL(200,
"WriterAsciiHepMC2::allocate_buffer: could not allocate buffer!")
274 ret.reserve(s.length()*2);
275 for ( std::string::const_iterator it = s.begin(); it != s.end(); ++it )
294 std::vector<double> weights;
297 weights = weights_a->value();
299 weights.reserve(100);
300 for (
int i = 0; i < 100; i++)
302 std::shared_ptr<DoubleAttribute> rs = v->attribute<
DoubleAttribute>(
"weight"+std::to_string(
static_cast<long long unsigned int>(i)));
304 weights.emplace_back(rs->value());
310 for (
const ConstGenParticlePtr& p: v->particles_in())
312 if (!p->production_vertex()) { orph++;}
315 if (p->production_vertex()->id() == 0) orph++;
330 m_cursor += sprintf(
m_cursor,
" %i %zu %zu", orph, v->particles_out().size(), weights.size());
375 if (p->end_vertex()) {
376 if (p->end_vertex()->id() != 0)
377 { ev = p->end_vertex()->id(); }
379 std::shared_ptr<DoubleAttribute> A_theta = p->attribute<
DoubleAttribute>(
"theta");
380 std:: shared_ptr<DoubleAttribute> A_phi = p->attribute<
DoubleAttribute>(
"phi");
387 std::shared_ptr<VectorIntAttribute> A_flows = p->attribute<
VectorIntAttribute>(
"flows");
390 std::vector<int> flowsv = A_flows->value();
391 std::string flowss =
" " + std::to_string(flowsv.size());
392 for (
size_t k = 0; k < flowsv.size(); k++) { flowss += (
" " + std::to_string(k+1) +
" " + std::to_string(flowsv.at(k))); }
396 std::shared_ptr<IntAttribute> A_flow1 = p->attribute<
IntAttribute>(
"flow1");
397 std::shared_ptr<IntAttribute> A_flow2 = p->attribute<
IntAttribute>(
"flow2");
398 std::shared_ptr<IntAttribute> A_flow3 = p->attribute<
IntAttribute>(
"flow3");
400 if (A_flow1) flowsize++;
401 if (A_flow2) flowsize++;
402 if (A_flow3) flowsize++;
403 std::string flowss =
" " + std::to_string(flowsize);
404 if (A_flow1) flowss += (
" 1 " + std::to_string(A_flow1->value()));
405 if (A_flow2) flowss += (
" 2 " + std::to_string(A_flow2->value()));
406 if (A_flow3) flowss += (
" 3 " + std::to_string(A_flow3->value()));
419 strncpy(
m_cursor, str.data(), str.length());
427 m_stream->write(str.data(), str.length());
435 auto* ofs =
dynamic_cast<std::ofstream*
>(
m_stream);
436 if (ofs && !ofs->is_open())
return;
438 const std::string footer(
"HepMC::IO_GenEvent-END_EVENT_LISTING\n\n");
441 if (ofs) ofs->close();
446 if (prec < 2 || prec > 24)
return;
456 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.
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 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.
Attribute that holds an Integer implemented as an int.
Attribute that holds an Integer implemented as a long int.
static std::string name(MomentumUnit u)
Get name of momentum unit.
Attribute that holds a vector of real numbers of type double.
Attribute that holds a vector of integers of type int.
Attribute that holds a vector of integers of type long int.
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
void close() override
Close file stream.
int precision() const
Return output precision.
int m_precision
Output precision.
WriterAsciiHepMC2(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
void write_particle(const ConstGenParticlePtr &p, int second_field)
Write particle.
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.
~WriterAsciiHepMC2()
Destructor.
unsigned long m_particle_counter
Used to set bar codes.
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.
void write_event(const GenEvent &evt) override
Write event to file.
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.