hepmc - Blame information for rev 87

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