1 //--------------------------------------------------------------------------
3 //////////////////////////////////////////////////////////////////////////////
4 // Mikhail.Kirsanov@Cern.CH, 2006
5 // event input/output in ascii format for eye and machine reading
7 // for arguments mostly similar to IO_Ascii. Special value of
8 // argument filename in constructor: if it is "cout" the output is to std::cout
9 //////////////////////////////////////////////////////////////////////////////
11 #include "HepMC/IO_AsciiParticles.h"
12 #include "HepMC/GenEvent.h"
13 #include "HepMC/Version.h"
17 IO_AsciiParticles::IO_AsciiParticles( const char* filename, std::ios::openmode mode )
19 m_mode(mode), m_finished_first_event_io(0)
21 if(std::string(filename) == std::string("cout")) {
22 m_outstream = &(std::cout);
25 m_file = new std::fstream(filename, mode);
27 if ( (m_mode&std::ios::out && m_mode&std::ios::in) ||
28 (m_mode&std::ios::app && m_mode&std::ios::in) ) {
29 std::cerr << "IO_AsciiParticles::IO_AsciiParticles Error, open of file requested "
30 << "of input AND output type. Not allowed. Closing file."
37 // precision 16 (# digits following decimal point) is the minimum that
38 // will capture the full information stored in a double
39 // with precision <= 2 the width of output will be < 80 characters
40 m_outstream->precision(m_precision);
41 // we use decimal to store integers, because it is smaller than hex!
42 m_outstream->setf(std::ios::dec,std::ios::basefield);
43 m_outstream->setf(std::ios::scientific,std::ios::floatfield);
46 IO_AsciiParticles::~IO_AsciiParticles() {
53 void IO_AsciiParticles::print( std::ostream& ostr ) const {
54 ostr << "IO_AsciiParticles: formated ascii file IO for eye and machine reading.\n"
55 << "\tFile openmode: " << m_mode
56 << " file state: " << m_outstream->rdstate()
57 << " bad:" << (m_outstream->rdstate()&std::ios::badbit)
58 << " eof:" << (m_outstream->rdstate()&std::ios::eofbit)
59 << " fail:" << (m_outstream->rdstate()&std::ios::failbit)
60 << " good:" << (m_outstream->rdstate()&std::ios::goodbit) << std::endl;
63 void IO_AsciiParticles::write_event( const GenEvent* evt ) {
64 // Writes evt to m_outstream. It does NOT delete the event after writing.
66 // check the state of m_outstream is good, and that it is in output mode
67 if ( !evt || !m_outstream ) return;
68 if ( !(m_mode&std::ios::out) ) {
69 std::cerr << "HepMC::IO_AsciiParticles::write_event "
70 << " attempt to write to input file." << std::endl;
74 // write event listing key before first event only.
75 if ( !m_finished_first_event_io ) {
76 m_finished_first_event_io = 1;
77 *m_outstream << "0 Run HepMC::IO_AsciiParticles eye-readable events output"
79 *m_outstream << "# HepMC::Version " << versionName() << std::endl;
81 " # stat pdg moth1 px py pz energy mass eta"
85 // output the event data
86 std::vector<long int> random_states = evt->random_states();
87 *m_outstream << evt->event_number() << " Event" << std::endl;
89 *m_outstream << " " << evt->event_scale();
90 output( evt->alphaQCD() );
91 output( evt->alphaQED() );
92 output( evt->signal_process_id() );
93 output( ( evt->signal_process_vertex() ?
94 evt->signal_process_vertex()->barcode() : 0 ) );
95 output( evt->vertices_size() ); // total number of vertices.
96 output( (int)random_states.size() );
97 for ( std::vector<long int>::iterator rs = random_states.begin();
98 rs != random_states.end(); ++rs ) {
101 output( (int)evt->weights().size() );
102 for ( WeightContainer::const_iterator w = evt->weights().begin();
103 w != evt->weights().end(); ++w ) {
109 int nparticles=0, imoth=0, ip=0, istati;
111 *m_outstream << evt->particles_size() << " particles" << std::endl;
113 for(HepMC::GenEvent::particle_const_iterator part = evt->particles_begin();
114 part != evt->particles_end(); ++part ) {
115 //if( (*part)->status() != 1 ) continue;
118 istati = (*part)->status();
119 if( (*part)->end_vertex() && istati == 1) {
120 std::cout << "final particle with end vertex!" << std::endl;
124 orig = (*part)->production_vertex();
128 for(HepMC::GenEvent::particle_const_iterator part1 =
129 evt->particles_begin();
130 part1 != part; part1++ ) {
132 if( (*part1)->end_vertex() == orig ) { ifound = true; break; }
134 if(!ifound) imoth = 0;
137 m_outstream->width(4);
138 *m_outstream << ip << " ";
140 m_outstream->width(3);
141 *m_outstream << istati << " ";
143 m_outstream->width(5);
144 *m_outstream << (*part)->pdg_id() << " ";
146 m_outstream->width(3);
147 *m_outstream << imoth << " ";
149 if((*part)->momentum().px() >= 0.) *m_outstream << " ";
150 *m_outstream << (*part)->momentum().px() << " ";
151 if((*part)->momentum().py() >= 0.) *m_outstream << " ";
152 *m_outstream << (*part)->momentum().py() << " ";
153 if((*part)->momentum().pz() >= 0.) *m_outstream << " ";
154 *m_outstream << (*part)->momentum().pz() << " "
155 << (*part)->momentum().e() << " ";
157 xmassi = (*part)->generatedMass();
158 if(fabs(xmassi) < 0.0001) xmassi =0.;
159 m_outstream->setf(std::ios::fixed);
160 m_outstream->precision(3);
161 m_outstream->width(8);
162 *m_outstream << xmassi << " ";
163 m_outstream->setf(std::ios::scientific,std::ios::floatfield);
164 m_outstream->precision(m_precision);
166 m_outstream->setf(std::ios::fixed);
167 m_outstream->precision(3);
168 m_outstream->width(6);
169 etai = (*part)->momentum().eta();
170 if(etai > 999.)etai = 999.;
171 if(etai < -999.)etai = -999.;
172 *m_outstream << etai << std::endl;
173 m_outstream->setf(std::ios::scientific,std::ios::floatfield);
174 m_outstream->precision(m_precision);
179 bool IO_AsciiParticles::fill_next_event( GenEvent* evt ){
182 // test that evt pointer is not null
185 << "IO_AsciiParticles::fill_next_event error - passed null event."
189 // check the state of m_outstream is good, and that it is in input mode
191 std::cerr << "HepMC::IO_AsciiParticles::fill_next_event "
192 << " no file for input" << std::endl;
193 if ( !(m_mode&std::ios::in) ) {
194 std::cerr << "HepMC::IO_AsciiParticles::fill_next_event "
195 << " attempt to read from output file" << std::endl;
198 std::cerr << "IO_AsciiParticles input is not yet implemented" << std::endl;
202 void IO_AsciiParticles::write_comment( const std::string comment ) {
203 // check the state of *m_outstream is good, and that it is in output mode
204 if ( !m_outstream ) return;
205 if ( !(m_mode&std::ios::out) ) {
206 std::cerr << "HepMC::IO_AsciiParticles::write_particle_data_table "
207 << " attempt to write to input file." << std::endl;
210 // write end of event listing key if events have already been written
212 // insert the comment key before the comment
213 *m_outstream << "\n" << "HepMC::IO_AsciiParticles-COMMENT\n";
214 *m_outstream << comment << std::endl;
217 bool IO_AsciiParticles::write_end_listing() {