10 #include "HepMC/WriterAscii.h" 12 #include "HepMC/Version.h" 13 #include "HepMC/GenEvent.h" 14 #include "HepMC/GenParticle.h" 15 #include "HepMC/GenVertex.h" 16 #include "HepMC/Units.h" 28 m_buffer_size( 256*1024 )
32 ERROR(
"WriterAscii: could not open output file: "<<filename )
35 m_file <<
"HepMC::IO_GenEvent-START_EVENT_LISTING" << std::endl;
47 m_buffer_size( 256*1024 )
56 (*m_stream) <<
"HepMC::IO_GenEvent-START_EVENT_LISTING" << std::endl;
83 WARNING(
"WriterAscii::write_event: GenEvents contain " 84 "different GenRunInfo objects from - only the " 85 "first such object will be serialized." )
115 FOREACH (
double w, evt.
weights())
122 typedef map< string, map<int, shared_ptr<Attribute> > >::value_type value_type1;
123 typedef map<int, shared_ptr<Attribute> >::value_type value_type2;
124 FOREACH (
const value_type1& vt1, evt.
attributes() ) {
125 FOREACH (
const value_type2& vt2, vt1.second ) {
129 bool status = vt2.second->to_string(st);
132 WARNING(
"WriterAscii::write_event: problem serializing attribute: "<<vt1.first )
136 sprintf(
m_cursor,
"A %i %s ",vt2.first,vt1.first.c_str());
145 int vertices_processed = 0;
146 int lowest_vertex_id = 0;
153 int production_vertex = 0;
158 if ( v->particles_in().size() > 1 || !v->data().is_zero() ) production_vertex = v->id();
159 else if ( v->particles_in().size() == 1 ) production_vertex = v->particles_in()[0]->id();
161 if (production_vertex < lowest_vertex_id) {
165 ++vertices_processed;
166 lowest_vertex_id = v->id();
183 WARNING(
"WriterAscii::allocate_buffer: buffer size too large. Dividing by 2. New size: " <<
m_buffer_size )
188 ERROR(
"WriterAscii::allocate_buffer: could not allocate buffer!" )
198 ret.reserve( s.length()*2 );
199 for ( string::const_iterator it = s.begin(); it != s.end(); ++it ) {
201 case '\\': ret +=
"\\\\";
break;
202 case '\n': ret +=
"\\|";
break;
214 bool printed_first =
false;
218 if ( !printed_first ) {
220 printed_first =
true;
272 vector<string> names =
run_info()->weight_names();
274 if ( !names.empty() ) {
275 string out = names[0];
276 for (
int i = 1, N = names.size(); i < N; ++i )
277 out +=
"\n" + names[i];
284 for (
int i = 0, N =
run_info()->tools().size(); i < N; ++i ) {
285 string out =
"T " +
run_info()->tools()[i].name +
"\n" 286 +
run_info()->tools()[i].version +
"\n" 287 +
run_info()->tools()[i].description;
292 typedef map< std::string, shared_ptr<Attribute> >::value_type value_type;
293 FOREACH( value_type att,
run_info()->attributes() ) {
295 if ( ! att.second->to_string(st) ) {
296 WARNING (
"WriterAscii::write_run_info: problem serializing attribute: "<< att.first )
300 sprintf(
m_cursor,
"A %s ", att.first.c_str());
339 strncpy(
m_cursor,str.data(),str.length());
347 m_stream->write( str.data(), str.length() );
353 std::ofstream* ofs =
dynamic_cast<std::ofstream*
>(
m_stream);
355 if (ofs && !ofs->is_open())
return;
359 (*m_stream) <<
"HepMC::IO_GenEvent-END_EVENT_LISTING" << endl << endl;
362 if (ofs) ofs->close();
WriterAscii(const std::string &filename, shared_ptr< GenRunInfo > run=shared_ptr< GenRunInfo >())
Constructor.
const std::vector< GenParticlePtr > & particles() const
Get list of particles (const)
void write_event(const GenEvent &evt)
Write event to file.
double y() const
y-component of position/displacement
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
void write_vertex(const GenVertexPtr &v)
Write vertex.
void write_string(const std::string &str)
Inline function for writing strings.
~WriterAscii()
Destructor.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
std::ostream * m_stream
Output stream.
int event_number() const
Get event number.
static std::string name(MomentumUnit u)
Get name of momentum unit.
const std::map< string, std::map< int, shared_ptr< Attribute > > > & attributes() const
Get list of attributes.
std::string version()
Get the HepMC library version string.
void forced_flush()
Inline function forcing flush to the output stream.
std::ofstream m_file
Output file.
Smart pointer for HepMC objects.
bool is_zero() const
Check if the length of this vertex is zero.
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
void allocate_buffer()
Attempts to allocate buffer of the chosen size.
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
double x() const
x-component of position/displacement
Stores event-related information.
void write_run_info()
Write the GenRunInfo object to file.
double t() const
Time component of position/displacement.
void write_particle(const GenParticlePtr &p, int second_field)
Write particle.
char * m_cursor
Cursor inside stream buffer.
unsigned long m_buffer_size
Buffer size.
const std::vector< GenVertexPtr > & vertices() const
Get list of vertices (const)
void flush()
Inline function flushing buffer to output stream when close to buffer capacity.
int m_precision
Output precision.
void close()
Close file stream.
char * m_buffer
Stream buffer.
const FourVector & event_pos() const
Vertex representing the overall event position.
Definition of template class SmartPointer.
std::string escape(const std::string s)
Escape '\' and ' ' characters in string.
const Units::LengthUnit & length_unit() const
Get length unit.
double z() const
z-component of position/displacement
const std::vector< double > & weights() const
Get event weight values as a vector.