Fork me on GitHub

source: svn/trunk/Utilities/HepMC/src/IO_AsciiParticles.cc@ 475

Last change on this file since 475 was 349, checked in by severine ovyn, 16 years ago

first test

File size: 7.8 KB
Line 
1//--------------------------------------------------------------------------
2
3//////////////////////////////////////////////////////////////////////////////
4// Mikhail.Kirsanov@Cern.CH, 2006
5// event input/output in ascii format for eye and machine reading
6//
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//////////////////////////////////////////////////////////////////////////////
10
11#include "IO_AsciiParticles.h"
12#include "GenEvent.h"
13#include "ParticleDataTable.h"
14#include "Version.h"
15
16namespace HepMC {
17
18 IO_AsciiParticles::IO_AsciiParticles( const char* filename, std::ios::openmode mode )
19 : m_precision(2),
20 m_mode(mode), m_finished_first_event_io(0)
21 {
22 if(std::string(filename) == std::string("cout")) {
23 m_outstream = &(std::cout);
24 m_file = 0;
25 } else {
26 m_file = new std::fstream(filename, mode);
27 m_outstream = m_file;
28 if ( (m_mode&std::ios::out && m_mode&std::ios::in) ||
29 (m_mode&std::ios::app && m_mode&std::ios::in) ) {
30 std::cerr << "IO_AsciiParticles::IO_AsciiParticles Error, open of file requested "
31 << "of input AND output type. Not allowed. Closing file."
32 << std::endl;
33 m_file->close();
34 delete m_file;
35 return;
36 }
37 }
38 // precision 16 (# digits following decimal point) is the minimum that
39 // will capture the full information stored in a double
40 // with precision <= 2 the width of output will be < 80 characters
41 m_outstream->precision(m_precision);
42 // we use decimal to store integers, because it is smaller than hex!
43 m_outstream->setf(std::ios::dec,std::ios::basefield);
44 m_outstream->setf(std::ios::scientific,std::ios::floatfield);
45 }
46
47 IO_AsciiParticles::~IO_AsciiParticles() {
48 if(m_file) {
49 m_file->close();
50 delete m_file;
51 }
52 }
53
54 void IO_AsciiParticles::print( std::ostream& ostr ) const {
55 ostr << "IO_AsciiParticles: formated ascii file IO for eye and machine reading.\n"
56 << "\tFile openmode: " << m_mode
57 << " file state: " << m_outstream->rdstate()
58 << " bad:" << (m_outstream->rdstate()&std::ios::badbit)
59 << " eof:" << (m_outstream->rdstate()&std::ios::eofbit)
60 << " fail:" << (m_outstream->rdstate()&std::ios::failbit)
61 << " good:" << (m_outstream->rdstate()&std::ios::goodbit) << std::endl;
62 }
63
64 void IO_AsciiParticles::write_event( const GenEvent* evt ) {
65 // Writes evt to m_outstream. It does NOT delete the event after writing.
66 //
67 // check the state of m_outstream is good, and that it is in output mode
68 if ( !evt || !m_outstream ) return;
69 if ( !(m_mode&std::ios::out) ) {
70 std::cerr << "HepMC::IO_AsciiParticles::write_event "
71 << " attempt to write to input file." << std::endl;
72 return;
73 }
74 //
75 // write event listing key before first event only.
76 if ( !m_finished_first_event_io ) {
77 m_finished_first_event_io = 1;
78 *m_outstream << "0 Run HepMC::IO_AsciiParticles eye-readable events output"
79 << std::endl;
80 *m_outstream << "# HepMC::Version " << versionName() << std::endl;
81 *m_outstream <<
82 " # stat pdg moth1 px py pz energy mass eta"
83 << std::endl;
84 }
85 //
86 // output the event data
87 std::vector<long int> random_states = evt->random_states();
88 *m_outstream << evt->event_number() << " Event" << std::endl;
89#if 0
90 *m_outstream << " " << evt->event_scale();
91 output( evt->alphaQCD() );
92 output( evt->alphaQED() );
93 output( evt->signal_process_id() );
94 output( ( evt->signal_process_vertex() ?
95 evt->signal_process_vertex()->barcode() : 0 ) );
96 output( evt->vertices_size() ); // total number of vertices.
97 output( (int)random_states.size() );
98 for ( std::vector<long int>::iterator rs = random_states.begin();
99 rs != random_states.end(); ++rs ) {
100 output( *rs );
101 }
102 output( (int)evt->weights().size() );
103 for ( WeightContainer::const_iterator w = evt->weights().begin();
104 w != evt->weights().end(); ++w ) {
105 output( *w );
106 }
107 output('\n');
108#endif
109 //
110 int nparticles=0, imoth=0, ip=0, istati;
111 double xmassi, etai;
112 *m_outstream << evt->particles_size() << " particles" << std::endl;
113 GenVertex* orig;
114 for(HepMC::GenEvent::particle_const_iterator part = evt->particles_begin();
115 part != evt->particles_end(); ++part ) {
116 //if( (*part)->status() != 1 ) continue;
117 nparticles++;
118 ip++;
119 istati = (*part)->status();
120 if( (*part)->end_vertex() && istati == 1) {
121 std::cout << "final particle with end vertex!" << std::endl;
122 istati = -100;
123 }
124 imoth=0;
125 orig = (*part)->production_vertex();
126 if(orig) {
127 imoth = 0;
128 bool ifound=false;
129 for(HepMC::GenEvent::particle_const_iterator part1 =
130 evt->particles_begin();
131 part1 != part; part1++ ) {
132 imoth++;
133 if( (*part1)->end_vertex() == orig ) { ifound = true; break; }
134 }
135 if(!ifound) imoth = 0;
136 }
137
138 m_outstream->width(4);
139 *m_outstream << ip << " ";
140
141 m_outstream->width(3);
142 *m_outstream << istati << " ";
143
144 m_outstream->width(5);
145 *m_outstream << (*part)->pdg_id() << " ";
146
147 m_outstream->width(3);
148 *m_outstream << imoth << " ";
149
150 if((*part)->momentum().px() >= 0.) *m_outstream << " ";
151 *m_outstream << (*part)->momentum().px() << " ";
152 if((*part)->momentum().py() >= 0.) *m_outstream << " ";
153 *m_outstream << (*part)->momentum().py() << " ";
154 if((*part)->momentum().pz() >= 0.) *m_outstream << " ";
155 *m_outstream << (*part)->momentum().pz() << " "
156 << (*part)->momentum().e() << " ";
157
158 xmassi = (*part)->generatedMass();
159 if(fabs(xmassi) < 0.0001) xmassi =0.;
160 m_outstream->setf(std::ios::fixed);
161 m_outstream->precision(3);
162 m_outstream->width(8);
163 *m_outstream << xmassi << " ";
164 m_outstream->setf(std::ios::scientific,std::ios::floatfield);
165 m_outstream->precision(m_precision);
166
167 m_outstream->setf(std::ios::fixed);
168 m_outstream->precision(3);
169 m_outstream->width(6);
170 etai = (*part)->momentum().eta();
171 if(etai > 999.)etai = 999.;
172 if(etai < -999.)etai = -999.;
173 *m_outstream << etai << std::endl;
174 m_outstream->setf(std::ios::scientific,std::ios::floatfield);
175 m_outstream->precision(m_precision);
176
177 }
178 }
179
180 bool IO_AsciiParticles::fill_next_event( GenEvent* evt ){
181 //
182 //
183 // test that evt pointer is not null
184 if ( !evt ) {
185 std::cerr
186 << "IO_AsciiParticles::fill_next_event error - passed null event."
187 << std::endl;
188 return false;
189 }
190 // check the state of m_outstream is good, and that it is in input mode
191 if ( !m_file )
192 std::cerr << "HepMC::IO_AsciiParticles::fill_next_event "
193 << " no file for input" << std::endl;
194 if ( !(m_mode&std::ios::in) ) {
195 std::cerr << "HepMC::IO_AsciiParticles::fill_next_event "
196 << " attempt to read from output file" << std::endl;
197 return false;
198 }
199 std::cerr << "IO_AsciiParticles input is not yet implemented" << std::endl;
200 return false;
201 }
202
203 void IO_AsciiParticles::write_comment( const std::string comment ) {
204 // check the state of *m_outstream is good, and that it is in output mode
205 if ( !m_outstream ) return;
206 if ( !(m_mode&std::ios::out) ) {
207 std::cerr << "HepMC::IO_AsciiParticles::write_particle_data_table "
208 << " attempt to write to input file." << std::endl;
209 return;
210 }
211 // write end of event listing key if events have already been written
212 write_end_listing();
213 // insert the comment key before the comment
214 *m_outstream << "\n" << "HepMC::IO_AsciiParticles-COMMENT\n";
215 *m_outstream << comment << std::endl;
216 }
217
218 bool IO_AsciiParticles::write_end_listing() {
219 return false;
220 }
221
222} // HepMC
223
Note: See TracBrowser for help on using the repository browser.