12 #include "HepMC3/Version.h" 29 m_buffer_size( 256*1024 ),
32 WARNING(
"WriterAsciiHepMC2::WriterAsciiHepMC2: HepMC2 format is outdated. Please use HepMC3 format instead." )
37 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 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 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 shared_ptr<IntAttribute> rs=evt.
attribute<
IntAttribute>(
"random_states"+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++)
149 vector<string> names =
run_info()->weight_names();
150 for (
size_t q=0; q<evt.
weights().size(); q++)
169 for (
auto vt2: vt1.second )
173 bool status = vt2.second->to_string(st);
177 WARNING(
"WriterAsciiHepMC2::write_event: problem serializing attribute: "<<vt1.first )
181 if (vt1.first==
"GenPdfInfo")
194 for(ConstGenVertexPtr v: evt.
vertices() )
196 int production_vertex = 0;
197 production_vertex=v->id();
199 for(ConstGenParticlePtr p: v->particles_in())
201 if (!p->production_vertex())
write_particle( p, production_vertex );
204 if (p->production_vertex()->id()==0)
write_particle( p, production_vertex );
207 for(ConstGenParticlePtr p: v->particles_out())
222 }
catch (
const std::bad_alloc& e) {
225 WARNING(
"WriterAsciiHepMC2::allocate_buffer: buffer size too large. Dividing by 2. New size: " <<
m_buffer_size )
231 ERROR(
"WriterAsciiHepMC2::allocate_buffer: could not allocate buffer!" )
242 ret.reserve( s.length()*2 );
243 for ( string::const_iterator it = s.begin(); it != s.end(); ++it )
262 std::vector<double> weights;
263 for (
int i=0;i<100;i++)
265 shared_ptr<DoubleAttribute> rs=v->attribute<
DoubleAttribute>(
"weight"+to_string((
long long unsigned int)i));
267 weights.push_back(rs->value());
273 for(ConstGenParticlePtr p: v->particles_in())
275 if (!p->production_vertex()) orph++;
278 if (p->production_vertex()->id()==0)orph++;
297 m_cursor += sprintf(
m_cursor,
" %i %lu %lu",orph,v->particles_out().size(),weights.size());
351 if (p->end_vertex()->id()!=0)
352 ev=p->end_vertex()->id();
354 shared_ptr<DoubleAttribute> A_theta=p->attribute<
DoubleAttribute>(
"theta");
355 shared_ptr<DoubleAttribute> A_phi=p->attribute<
DoubleAttribute>(
"phi");
363 shared_ptr<IntAttribute> A_flow1=p->attribute<
IntAttribute>(
"flow1");
364 shared_ptr<IntAttribute> A_flow2=p->attribute<
IntAttribute>(
"flow2");
366 if (A_flow1) flowsize++;
367 if (A_flow2) flowsize++;
384 strncpy(
m_cursor,str.data(),str.length());
392 m_stream->write( str.data(), str.length() );
399 std::ofstream* ofs =
dynamic_cast<std::ofstream*
>(
m_stream);
400 if (ofs && !ofs->is_open())
return;
402 (*m_stream) <<
"HepMC::IO_GenEvent-END_EVENT_LISTING" << endl << endl;
403 if (ofs) ofs->close();
int event_number() const
Get event number.
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
const Units::LengthUnit & length_unit() const
Get length unit.
#define ERROR(MESSAGE)
Macro for printing error messages.
Definition of class GenParticle.
std::ofstream m_file
Output file.
const std::vector< ConstGenVertexPtr > & vertices() const
Get list of vertices (const)
void forced_flush()
Inline function forcing flush to the output stream.
Definition of class GenVertex.
void write_vertex(ConstGenVertexPtr v)
Write vertex.
std::string version()
Get the HepMC library version string.
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
static std::string name(MomentumUnit u)
Get name of momentum unit.
char * m_buffer
Stream buffer.
char * m_cursor
Cursor inside stream buffer.
bool is_zero() const
Check if the length of this vertex is zero.
unsigned long m_buffer_size
Buffer size.
double x() const
x-component of position/displacement
const std::vector< double > & weights() const
Get event weight values as a vector.
unsigned long m_particle_counter
Used to set bar codes.
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Stores event-related information.
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
std::map< string, std::map< int, shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
Attribute that holds a real number as a double.
void write_string(const std::string &str)
Inline function for writing strings.
std::string escape(const std::string &s) const
Escape '\' and ' ' characters in string.
double y() const
y-component of position/displacement
double t() const
Time component of position/displacement.
void write_run_info()
Write the GenRunInfo object to file.
void write_particle(ConstGenParticlePtr p, int second_field)
Write particle.
void close()
Close file stream.
Definition of class Units.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition of class WriterAsciiHepMC2.
#define WARNING(MESSAGE)
Macro for printing warning messages.
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
~WriterAsciiHepMC2()
Destructor.
int m_precision
Output precision.
WriterAsciiHepMC2(const std::string &filename, shared_ptr< GenRunInfo > run=shared_ptr< GenRunInfo >())
Constructor.
Definition of class GenEvent.
shared_ptr< T > attribute(const string &name, const int &id=0) const
Get attribute of type T.
void write_event(const GenEvent &evt)
Write event to file.
Attribute that holds an Integer implemented as an int.
std::ostream * m_stream
Output stream.
Stores additional information about cross-section.
double z() const
z-component of position/displacement