12 #include "HepMC3/Version.h"
29 m_buffer_size( 256*1024 ),
32 HEPMC3_WARNING(
"WriterAsciiHepMC2::WriterAsciiHepMC2: HepMC2 format is outdated. Please use HepMC3 format instead." )
37 HEPMC3_ERROR(
"WriterAsciiHepMC2: could not open output file: "<<filename )
42 m_file <<
"HepMC::IO_GenEvent-START_EVENT_LISTING" << std::endl;
52 m_buffer_size( 256*1024 ),
55 HEPMC3_WARNING(
"WriterAsciiHepMC2::WriterAsciiHepMC2: HepMC2 format is outdated. Please use HepMC3 format instead." )
58 (*m_stream) <<
"HepMC::Version " <<
version() << std::endl;
59 (*m_stream) <<
"HepMC::IO_GenEvent-START_EVENT_LISTING" << std::endl;
87 std::shared_ptr<IntAttribute> A_signal_process_vertex=evt.
attribute<
IntAttribute>(
"signal_process_vertex");
89 double event_scale=A_event_scale?(A_event_scale->value()):0.0;
90 double alphaQED=A_alphaQED?(A_alphaQED->value()):0.0;
91 double alphaQCD=A_alphaQCD?(A_alphaQCD->value()):0.0;
92 int signal_process_id=A_signal_process_id?(A_signal_process_id->value()):0;
93 int mpi=A_mpi?(A_mpi->value()):0;
94 int signal_process_vertex=A_signal_process_vertex?(A_signal_process_vertex->value()):0;
96 std::vector<long> m_random_states;
97 for (
int i=0; i<100; i++)
99 std::shared_ptr<IntAttribute> rs=evt.
attribute<
IntAttribute>(
"random_states"+std::to_string((
long long unsigned int)i));
101 m_random_states.push_back(rs->value());
105 std::vector<int> beams;
107 for(ConstGenVertexPtr v: evt.
vertices() )
109 for(ConstGenParticlePtr p: v->particles_in())
111 if (!p->production_vertex()) {
if (p->status()==4) beams.push_back(idbeam); idbeam++;}
112 else if (p->production_vertex()->id()==0) {
if (p->status()==4) beams.push_back(idbeam); idbeam++;}
114 for( ConstGenParticlePtr p: v->particles_out()) {
if (p->status()==4) beams.push_back(idbeam); idbeam++;}
119 if (beams.size()>0) idbeam1+=beams[0]+1;
120 if (beams.size()>1) idbeam2+=beams[1]+1;
128 signal_process_vertex,
135 for (
size_t q=0; q<m_random_states.size(); q++)
148 std::vector<std::string> names =
run_info()->weight_names();
149 for (
size_t q=0; q<evt.
weights().size(); q++)
170 for (
auto vt2: vt1.second )
174 bool status = vt2.second->to_string(st);
178 HEPMC3_WARNING(
"WriterAsciiHepMC2::write_event: problem serializing attribute: "<<vt1.first )
182 if (vt1.first==
"GenPdfInfo")
195 for(ConstGenVertexPtr v: evt.
vertices() )
197 int production_vertex = 0;
198 production_vertex=v->id();
200 for(ConstGenParticlePtr p: v->particles_in())
202 if (!p->production_vertex())
write_particle( p, production_vertex );
205 if (p->production_vertex()->id()==0)
write_particle( p, production_vertex );
208 for(ConstGenParticlePtr p: v->particles_out())
223 }
catch (
const std::bad_alloc& e) {
226 HEPMC3_WARNING(
"WriterAsciiHepMC2::allocate_buffer:"<<e.what()<<
" buffer size too large. Dividing by 2. New size: " <<
m_buffer_size )
232 HEPMC3_ERROR(
"WriterAsciiHepMC2::allocate_buffer: could not allocate buffer!" )
243 ret.reserve( s.length()*2 );
244 for ( std::string::const_iterator it = s.begin(); it != s.end(); ++it )
263 std::vector<double> weights;
264 for (
int i=0; i<100; i++)
266 std::shared_ptr<DoubleAttribute> rs=v->attribute<
DoubleAttribute>(
"weight"+std::to_string((
long long unsigned int)i));
268 weights.push_back(rs->value());
274 for(ConstGenParticlePtr p: v->particles_in())
276 if (!p->production_vertex()) orph++;
279 if (p->production_vertex()->id()==0)orph++;
298 m_cursor += sprintf(
m_cursor,
" %i %lu %lu",orph,v->particles_out().size(),weights.size());
314 m_stream->write( m_buffer, length );
352 if (p->end_vertex()->id()!=0)
353 ev=p->end_vertex()->id();
355 std::shared_ptr<DoubleAttribute> A_theta=p->attribute<
DoubleAttribute>(
"theta");
356 std:: shared_ptr<DoubleAttribute> A_phi=p->attribute<
DoubleAttribute>(
"phi");
358 else m_cursor += sprintf(m_cursor,
" 0");
360 if (A_phi) m_cursor += sprintf(m_cursor,
" %.*e",
m_precision, A_phi->value());
361 else m_cursor += sprintf(m_cursor,
" 0");
363 m_cursor += sprintf(m_cursor,
" %i", ev );
365 std::shared_ptr<VectorIntAttribute> A_flows=p->attribute<
VectorIntAttribute>(
"flows");
368 std::vector<int> flowsv=A_flows->
value();
369 int flowsize=flowsv.size();
370 m_cursor += sprintf(m_cursor,
" %i", flowsize);
371 for (
size_t k=0; k<flowsv.size(); k++) m_cursor += sprintf(m_cursor,
" %lu %i", k+1, flowsv.at(k));
372 m_cursor += sprintf(m_cursor,
"\n");
375 std::shared_ptr<IntAttribute> A_flow1=p->attribute<
IntAttribute>(
"flow1");
376 std::shared_ptr<IntAttribute> A_flow2=p->attribute<
IntAttribute>(
"flow2");
377 std::shared_ptr<IntAttribute> A_flow3=p->attribute<
IntAttribute>(
"flow3");
379 if (A_flow1) flowsize++;
380 if (A_flow2) flowsize++;
381 if (A_flow3) flowsize++;
382 m_cursor += sprintf(m_cursor,
" %i", flowsize);
383 if (A_flow1) m_cursor += sprintf(m_cursor,
" 1 %i", A_flow1->value());
384 if (A_flow2) m_cursor += sprintf(m_cursor,
" 2 %i", A_flow2->value());
385 if (A_flow3) m_cursor += sprintf(m_cursor,
" 3 %i", A_flow3->value());
386 m_cursor += sprintf(m_cursor,
"\n");
400 strncpy(
m_cursor,str.data(),str.length());
408 m_stream->write( str.data(), str.length() );
415 std::ofstream* ofs =
dynamic_cast<std::ofstream*
>(
m_stream);
416 if (ofs && !ofs->is_open())
return;
418 (*m_stream) <<
"HepMC::IO_GenEvent-END_EVENT_LISTING" << std::endl << std::endl;
419 if (ofs) ofs->close();
424 if (prec < 2 || prec > 24)
return;
434 if (size < 256)
return;
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
double t() const
Time component of position/displacement.
Definition of class GenParticle.
void close() override
Close file stream.
std::ofstream m_file
Output file.
WriterAsciiHepMC2(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor.
void write_event(const GenEvent &evt) override
Write event to file.
void forced_flush()
Inline function forcing flush to the output stream.
Definition of class GenVertex.
void write_vertex(ConstGenVertexPtr v)
Write vertex.
bool is_zero() const
Check if the length of this vertex is zero.
Attribute that holds a vector of integers of type int.
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
static std::string name(MomentumUnit u)
Get name of momentum unit.
int event_number() const
Get event number.
int precision() const
Return output precision.
char * m_buffer
Stream buffer.
char * m_cursor
Cursor inside stream buffer.
std::vector< int > value() const
get the value associated to this Attribute.
unsigned long m_buffer_size
Buffer size.
unsigned long m_particle_counter
Used to set bar codes.
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
Stores event-related information.
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
Attribute that holds a real number as a double.
void write_string(const std::string &str)
Inline function for writing strings.
bool failed() override
Return status of the stream.
std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
void write_run_info()
Write the GenRunInfo object to file.
void set_buffer_size(const size_t &size)
Set buffer size (in bytes)
void write_particle(ConstGenParticlePtr p, int second_field)
Write particle.
Definition of class Units.
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
Definition of class WriterAsciiHepMC2.
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
std::string escape(const std::string &s) const
Escape '\' and ' ' characters in string.
double y() const
y-component of position/displacement
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
const std::vector< double > & weights() const
Get event weight values as a vector.
~WriterAsciiHepMC2()
Destructor.
int m_precision
Output precision.
Definition of class GenEvent.
void set_precision(const int &prec)
Set output precision.
std::map< std::string, std::map< int, std::shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Attribute that holds an Integer implemented as an int.
std::ostream * m_stream
Output stream.
Stores additional information about cross-section.