Line data Source code
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 0 : IO_AsciiParticles::IO_AsciiParticles( const char* filename, std::ios::openmode mode )
18 0 : : m_precision(2),
19 0 : m_mode(mode), m_finished_first_event_io(0)
20 0 : {
21 0 : if(std::string(filename) == std::string("cout")) {
22 0 : m_outstream = &(std::cout);
23 0 : m_file = 0;
24 0 : } else {
25 0 : m_file = new std::fstream(filename, mode);
26 0 : m_outstream = m_file;
27 0 : if ( (m_mode&std::ios::out && m_mode&std::ios::in) ||
28 0 : (m_mode&std::ios::app && m_mode&std::ios::in) ) {
29 0 : std::cerr << "IO_AsciiParticles::IO_AsciiParticles Error, open of file requested "
30 0 : << "of input AND output type. Not allowed. Closing file."
31 0 : << std::endl;
32 0 : m_file->close();
33 0 : 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 0 : m_outstream->precision(m_precision);
41 : // we use decimal to store integers, because it is smaller than hex!
42 0 : m_outstream->setf(std::ios::dec,std::ios::basefield);
43 0 : m_outstream->setf(std::ios::scientific,std::ios::floatfield);
44 0 : }
45 :
46 0 : IO_AsciiParticles::~IO_AsciiParticles() {
47 0 : if(m_file) {
48 0 : m_file->close();
49 0 : delete m_file;
50 : }
51 0 : }
52 :
53 : void IO_AsciiParticles::print( std::ostream& ostr ) const {
54 0 : ostr << "IO_AsciiParticles: formated ascii file IO for eye and machine reading.\n"
55 0 : << "\tFile openmode: " << m_mode
56 0 : << " file state: " << m_outstream->rdstate()
57 0 : << " bad:" << (m_outstream->rdstate()&std::ios::badbit)
58 0 : << " eof:" << (m_outstream->rdstate()&std::ios::eofbit)
59 0 : << " fail:" << (m_outstream->rdstate()&std::ios::failbit)
60 0 : << " good:" << (m_outstream->rdstate()&std::ios::goodbit) << std::endl;
61 0 : }
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 0 : if ( !evt || !m_outstream ) return;
68 0 : if ( !(m_mode&std::ios::out) ) {
69 0 : std::cerr << "HepMC::IO_AsciiParticles::write_event "
70 0 : << " attempt to write to input file." << std::endl;
71 0 : return;
72 : }
73 : //
74 : // write event listing key before first event only.
75 0 : if ( !m_finished_first_event_io ) {
76 0 : m_finished_first_event_io = 1;
77 0 : *m_outstream << "0 Run HepMC::IO_AsciiParticles eye-readable events output"
78 0 : << std::endl;
79 0 : *m_outstream << "# HepMC::Version " << versionName() << std::endl;
80 0 : *m_outstream <<
81 : " # stat pdg moth1 px py pz energy mass eta"
82 0 : << std::endl;
83 0 : }
84 : //
85 : // output the event data
86 0 : std::vector<long int> random_states = evt->random_states();
87 0 : *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 0 : *m_outstream << evt->particles_size() << " particles" << std::endl;
112 : GenVertex* orig;
113 0 : for(HepMC::GenEvent::particle_const_iterator part = evt->particles_begin();
114 0 : part != evt->particles_end(); ++part ) {
115 : //if( (*part)->status() != 1 ) continue;
116 0 : nparticles++;
117 0 : ip++;
118 0 : istati = (*part)->status();
119 0 : if( (*part)->end_vertex() && istati == 1) {
120 0 : std::cout << "final particle with end vertex!" << std::endl;
121 : istati = -100;
122 0 : }
123 : imoth=0;
124 0 : orig = (*part)->production_vertex();
125 0 : if(orig) {
126 : imoth = 0;
127 : bool ifound=false;
128 0 : for(HepMC::GenEvent::particle_const_iterator part1 =
129 0 : evt->particles_begin();
130 0 : part1 != part; part1++ ) {
131 0 : imoth++;
132 0 : if( (*part1)->end_vertex() == orig ) { ifound = true; break; }
133 : }
134 0 : if(!ifound) imoth = 0;
135 0 : }
136 :
137 0 : m_outstream->width(4);
138 0 : *m_outstream << ip << " ";
139 :
140 0 : m_outstream->width(3);
141 0 : *m_outstream << istati << " ";
142 :
143 0 : m_outstream->width(5);
144 0 : *m_outstream << (*part)->pdg_id() << " ";
145 :
146 0 : m_outstream->width(3);
147 0 : *m_outstream << imoth << " ";
148 :
149 0 : if((*part)->momentum().px() >= 0.) *m_outstream << " ";
150 0 : *m_outstream << (*part)->momentum().px() << " ";
151 0 : if((*part)->momentum().py() >= 0.) *m_outstream << " ";
152 0 : *m_outstream << (*part)->momentum().py() << " ";
153 0 : if((*part)->momentum().pz() >= 0.) *m_outstream << " ";
154 0 : *m_outstream << (*part)->momentum().pz() << " "
155 0 : << (*part)->momentum().e() << " ";
156 :
157 0 : xmassi = (*part)->generatedMass();
158 0 : if(fabs(xmassi) < 0.0001) xmassi =0.;
159 0 : m_outstream->setf(std::ios::fixed);
160 0 : m_outstream->precision(3);
161 0 : m_outstream->width(8);
162 0 : *m_outstream << xmassi << " ";
163 0 : m_outstream->setf(std::ios::scientific,std::ios::floatfield);
164 0 : m_outstream->precision(m_precision);
165 :
166 0 : m_outstream->setf(std::ios::fixed);
167 0 : m_outstream->precision(3);
168 0 : m_outstream->width(6);
169 0 : etai = (*part)->momentum().eta();
170 0 : if(etai > 999.)etai = 999.;
171 0 : if(etai < -999.)etai = -999.;
172 0 : *m_outstream << etai << std::endl;
173 0 : m_outstream->setf(std::ios::scientific,std::ios::floatfield);
174 0 : m_outstream->precision(m_precision);
175 :
176 : }
177 0 : }
178 :
179 : bool IO_AsciiParticles::fill_next_event( GenEvent* evt ){
180 : //
181 : //
182 : // test that evt pointer is not null
183 0 : if ( !evt ) {
184 : std::cerr
185 0 : << "IO_AsciiParticles::fill_next_event error - passed null event."
186 0 : << std::endl;
187 0 : return false;
188 : }
189 : // check the state of m_outstream is good, and that it is in input mode
190 0 : if ( !m_file )
191 0 : std::cerr << "HepMC::IO_AsciiParticles::fill_next_event "
192 0 : << " no file for input" << std::endl;
193 0 : if ( !(m_mode&std::ios::in) ) {
194 0 : std::cerr << "HepMC::IO_AsciiParticles::fill_next_event "
195 0 : << " attempt to read from output file" << std::endl;
196 0 : return false;
197 : }
198 0 : std::cerr << "IO_AsciiParticles input is not yet implemented" << std::endl;
199 0 : return false;
200 0 : }
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 0 : if ( !m_outstream ) return;
205 0 : if ( !(m_mode&std::ios::out) ) {
206 0 : std::cerr << "HepMC::IO_AsciiParticles::write_particle_data_table "
207 0 : << " attempt to write to input file." << std::endl;
208 0 : return;
209 : }
210 : // write end of event listing key if events have already been written
211 0 : write_end_listing();
212 : // insert the comment key before the comment
213 0 : *m_outstream << "\n" << "HepMC::IO_AsciiParticles-COMMENT\n";
214 0 : *m_outstream << comment << std::endl;
215 0 : }
216 :
217 : bool IO_AsciiParticles::write_end_listing() {
218 0 : return false;
219 : }
220 :
221 : } // HepMC
222 :
|