]>
Commit | Line | Data |
---|---|---|
0ca57c2f | 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 "HepMC/IO_AsciiParticles.h" | |
12 | #include "HepMC/GenEvent.h" | |
13 | #include "HepMC/Version.h" | |
14 | ||
15 | namespace HepMC { | |
16 | ||
17 | IO_AsciiParticles::IO_AsciiParticles( const char* filename, std::ios::openmode mode ) | |
18 | : m_precision(2), | |
19 | m_mode(mode), m_finished_first_event_io(0) | |
20 | { | |
21 | if(std::string(filename) == std::string("cout")) { | |
22 | m_outstream = &(std::cout); | |
23 | m_file = 0; | |
24 | } else { | |
25 | m_file = new std::fstream(filename, mode); | |
26 | m_outstream = m_file; | |
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." | |
31 | << std::endl; | |
32 | m_file->close(); | |
33 | delete m_file; | |
34 | return; | |
35 | } | |
36 | } | |
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); | |
44 | } | |
45 | ||
46 | IO_AsciiParticles::~IO_AsciiParticles() { | |
47 | if(m_file) { | |
48 | m_file->close(); | |
49 | delete m_file; | |
50 | } | |
51 | } | |
52 | ||
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; | |
61 | } | |
62 | ||
63 | void IO_AsciiParticles::write_event( const GenEvent* evt ) { | |
64 | // Writes evt to m_outstream. It does NOT delete the event after writing. | |
65 | // | |
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; | |
71 | return; | |
72 | } | |
73 | // | |
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" | |
78 | << std::endl; | |
79 | *m_outstream << "# HepMC::Version " << versionName() << std::endl; | |
80 | *m_outstream << | |
81 | " # stat pdg moth1 px py pz energy mass eta" | |
82 | << std::endl; | |
83 | } | |
84 | // | |
85 | // output the event data | |
86 | std::vector<long int> random_states = evt->random_states(); | |
87 | *m_outstream << evt->event_number() << " Event" << std::endl; | |
88 | #if 0 | |
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 ) { | |
99 | output( *rs ); | |
100 | } | |
101 | output( (int)evt->weights().size() ); | |
102 | for ( WeightContainer::const_iterator w = evt->weights().begin(); | |
103 | w != evt->weights().end(); ++w ) { | |
104 | output( *w ); | |
105 | } | |
106 | output('\n'); | |
107 | #endif | |
108 | // | |
109 | int nparticles=0, imoth=0, ip=0, istati; | |
110 | double xmassi, etai; | |
111 | *m_outstream << evt->particles_size() << " particles" << std::endl; | |
112 | GenVertex* orig; | |
113 | for(HepMC::GenEvent::particle_const_iterator part = evt->particles_begin(); | |
114 | part != evt->particles_end(); ++part ) { | |
115 | //if( (*part)->status() != 1 ) continue; | |
116 | nparticles++; | |
117 | ip++; | |
118 | istati = (*part)->status(); | |
119 | if( (*part)->end_vertex() && istati == 1) { | |
120 | std::cout << "final particle with end vertex!" << std::endl; | |
121 | istati = -100; | |
122 | } | |
123 | imoth=0; | |
124 | orig = (*part)->production_vertex(); | |
125 | if(orig) { | |
126 | imoth = 0; | |
127 | bool ifound=false; | |
128 | for(HepMC::GenEvent::particle_const_iterator part1 = | |
129 | evt->particles_begin(); | |
130 | part1 != part; part1++ ) { | |
131 | imoth++; | |
132 | if( (*part1)->end_vertex() == orig ) { ifound = true; break; } | |
133 | } | |
134 | if(!ifound) imoth = 0; | |
135 | } | |
136 | ||
137 | m_outstream->width(4); | |
138 | *m_outstream << ip << " "; | |
139 | ||
140 | m_outstream->width(3); | |
141 | *m_outstream << istati << " "; | |
142 | ||
143 | m_outstream->width(5); | |
144 | *m_outstream << (*part)->pdg_id() << " "; | |
145 | ||
146 | m_outstream->width(3); | |
147 | *m_outstream << imoth << " "; | |
148 | ||
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() << " "; | |
156 | ||
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); | |
165 | ||
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); | |
175 | ||
176 | } | |
177 | } | |
178 | ||
179 | bool IO_AsciiParticles::fill_next_event( GenEvent* evt ){ | |
180 | // | |
181 | // | |
182 | // test that evt pointer is not null | |
183 | if ( !evt ) { | |
184 | std::cerr | |
185 | << "IO_AsciiParticles::fill_next_event error - passed null event." | |
186 | << std::endl; | |
187 | return false; | |
188 | } | |
189 | // check the state of m_outstream is good, and that it is in input mode | |
190 | if ( !m_file ) | |
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; | |
196 | return false; | |
197 | } | |
198 | std::cerr << "IO_AsciiParticles input is not yet implemented" << std::endl; | |
199 | return false; | |
200 | } | |
201 | ||
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; | |
208 | return; | |
209 | } | |
210 | // write end of event listing key if events have already been written | |
211 | write_end_listing(); | |
212 | // insert the comment key before the comment | |
213 | *m_outstream << "\n" << "HepMC::IO_AsciiParticles-COMMENT\n"; | |
214 | *m_outstream << comment << std::endl; | |
215 | } | |
216 | ||
217 | bool IO_AsciiParticles::write_end_listing() { | |
218 | return false; | |
219 | } | |
220 | ||
221 | } // HepMC | |
222 |