![]() |
HepMC Reference DocumentationHepMC |
00001 //-------------------------------------------------------------------------- 00002 00004 // Mikhail.Kirsanov@Cern.CH, 2006 00005 // event input/output in ascii format for eye and machine reading 00006 // 00007 // for arguments mostly similar to IO_Ascii. Special value of 00008 // argument filename in constructor: if it is "cout" the output is to std::cout 00010 00011 #include "HepMC/IO_AsciiParticles.h" 00012 #include "HepMC/GenEvent.h" 00013 #include "HepMC/ParticleDataTable.h" 00014 #include "HepMC/Version.h" 00015 00016 namespace HepMC { 00017 00018 IO_AsciiParticles::IO_AsciiParticles( const char* filename, std::ios::openmode mode ) 00019 : m_precision(2), 00020 m_mode(mode), m_finished_first_event_io(0) 00021 { 00022 if(std::string(filename) == std::string("cout")) { 00023 m_outstream = &(std::cout); 00024 m_file = 0; 00025 } else { 00026 m_file = new std::fstream(filename, mode); 00027 m_outstream = m_file; 00028 if ( (m_mode&std::ios::out && m_mode&std::ios::in) || 00029 (m_mode&std::ios::app && m_mode&std::ios::in) ) { 00030 std::cerr << "IO_AsciiParticles::IO_AsciiParticles Error, open of file requested " 00031 << "of input AND output type. Not allowed. Closing file." 00032 << std::endl; 00033 m_file->close(); 00034 delete m_file; 00035 return; 00036 } 00037 } 00038 // precision 16 (# digits following decimal point) is the minimum that 00039 // will capture the full information stored in a double 00040 // with precision <= 2 the width of output will be < 80 characters 00041 m_outstream->precision(m_precision); 00042 // we use decimal to store integers, because it is smaller than hex! 00043 m_outstream->setf(std::ios::dec,std::ios::basefield); 00044 m_outstream->setf(std::ios::scientific,std::ios::floatfield); 00045 } 00046 00047 IO_AsciiParticles::~IO_AsciiParticles() { 00048 if(m_file) { 00049 m_file->close(); 00050 delete m_file; 00051 } 00052 } 00053 00054 void IO_AsciiParticles::print( std::ostream& ostr ) const { 00055 ostr << "IO_AsciiParticles: formated ascii file IO for eye and machine reading.\n" 00056 << "\tFile openmode: " << m_mode 00057 << " file state: " << m_outstream->rdstate() 00058 << " bad:" << (m_outstream->rdstate()&std::ios::badbit) 00059 << " eof:" << (m_outstream->rdstate()&std::ios::eofbit) 00060 << " fail:" << (m_outstream->rdstate()&std::ios::failbit) 00061 << " good:" << (m_outstream->rdstate()&std::ios::goodbit) << std::endl; 00062 } 00063 00064 void IO_AsciiParticles::write_event( const GenEvent* evt ) { 00065 // Writes evt to m_outstream. It does NOT delete the event after writing. 00066 // 00067 // check the state of m_outstream is good, and that it is in output mode 00068 if ( !evt || !m_outstream ) return; 00069 if ( !(m_mode&std::ios::out) ) { 00070 std::cerr << "HepMC::IO_AsciiParticles::write_event " 00071 << " attempt to write to input file." << std::endl; 00072 return; 00073 } 00074 // 00075 // write event listing key before first event only. 00076 if ( !m_finished_first_event_io ) { 00077 m_finished_first_event_io = 1; 00078 *m_outstream << "0 Run HepMC::IO_AsciiParticles eye-readable events output" 00079 << std::endl; 00080 *m_outstream << "# HepMC::Version " << versionName() << std::endl; 00081 *m_outstream << 00082 " # stat pdg moth1 px py pz energy mass eta" 00083 << std::endl; 00084 } 00085 // 00086 // output the event data 00087 std::vector<long int> random_states = evt->random_states(); 00088 *m_outstream << evt->event_number() << " Event" << std::endl; 00089 #if 0 00090 *m_outstream << " " << evt->event_scale(); 00091 output( evt->alphaQCD() ); 00092 output( evt->alphaQED() ); 00093 output( evt->signal_process_id() ); 00094 output( ( evt->signal_process_vertex() ? 00095 evt->signal_process_vertex()->barcode() : 0 ) ); 00096 output( evt->vertices_size() ); // total number of vertices. 00097 output( (int)random_states.size() ); 00098 for ( std::vector<long int>::iterator rs = random_states.begin(); 00099 rs != random_states.end(); ++rs ) { 00100 output( *rs ); 00101 } 00102 output( (int)evt->weights().size() ); 00103 for ( WeightContainer::const_iterator w = evt->weights().begin(); 00104 w != evt->weights().end(); ++w ) { 00105 output( *w ); 00106 } 00107 output('\n'); 00108 #endif 00109 // 00110 int nparticles=0, imoth=0, ip=0, istati; 00111 double xmassi, etai; 00112 *m_outstream << evt->particles_size() << " particles" << std::endl; 00113 GenVertex* orig; 00114 for(HepMC::GenEvent::particle_const_iterator part = evt->particles_begin(); 00115 part != evt->particles_end(); ++part ) { 00116 //if( (*part)->status() != 1 ) continue; 00117 nparticles++; 00118 ip++; 00119 istati = (*part)->status(); 00120 if( (*part)->end_vertex() && istati == 1) { 00121 std::cout << "final particle with end vertex!" << std::endl; 00122 istati = -100; 00123 } 00124 imoth=0; 00125 orig = (*part)->production_vertex(); 00126 if(orig) { 00127 imoth = 0; 00128 bool ifound=false; 00129 for(HepMC::GenEvent::particle_const_iterator part1 = 00130 evt->particles_begin(); 00131 part1 != part; part1++ ) { 00132 imoth++; 00133 if( (*part1)->end_vertex() == orig ) { ifound = true; break; } 00134 } 00135 if(!ifound) imoth = 0; 00136 } 00137 00138 m_outstream->width(4); 00139 *m_outstream << ip << " "; 00140 00141 m_outstream->width(3); 00142 *m_outstream << istati << " "; 00143 00144 m_outstream->width(5); 00145 *m_outstream << (*part)->pdg_id() << " "; 00146 00147 m_outstream->width(3); 00148 *m_outstream << imoth << " "; 00149 00150 if((*part)->momentum().px() >= 0.) *m_outstream << " "; 00151 *m_outstream << (*part)->momentum().px() << " "; 00152 if((*part)->momentum().py() >= 0.) *m_outstream << " "; 00153 *m_outstream << (*part)->momentum().py() << " "; 00154 if((*part)->momentum().pz() >= 0.) *m_outstream << " "; 00155 *m_outstream << (*part)->momentum().pz() << " " 00156 << (*part)->momentum().e() << " "; 00157 00158 xmassi = (*part)->generatedMass(); 00159 if(fabs(xmassi) < 0.0001) xmassi =0.; 00160 m_outstream->setf(std::ios::fixed); 00161 m_outstream->precision(3); 00162 m_outstream->width(8); 00163 *m_outstream << xmassi << " "; 00164 m_outstream->setf(std::ios::scientific,std::ios::floatfield); 00165 m_outstream->precision(m_precision); 00166 00167 m_outstream->setf(std::ios::fixed); 00168 m_outstream->precision(3); 00169 m_outstream->width(6); 00170 etai = (*part)->momentum().eta(); 00171 if(etai > 999.)etai = 999.; 00172 if(etai < -999.)etai = -999.; 00173 *m_outstream << etai << std::endl; 00174 m_outstream->setf(std::ios::scientific,std::ios::floatfield); 00175 m_outstream->precision(m_precision); 00176 00177 } 00178 } 00179 00180 bool IO_AsciiParticles::fill_next_event( GenEvent* evt ){ 00181 // 00182 // 00183 // test that evt pointer is not null 00184 if ( !evt ) { 00185 std::cerr 00186 << "IO_AsciiParticles::fill_next_event error - passed null event." 00187 << std::endl; 00188 return false; 00189 } 00190 // check the state of m_outstream is good, and that it is in input mode 00191 if ( !m_file ) 00192 std::cerr << "HepMC::IO_AsciiParticles::fill_next_event " 00193 << " no file for input" << std::endl; 00194 if ( !(m_mode&std::ios::in) ) { 00195 std::cerr << "HepMC::IO_AsciiParticles::fill_next_event " 00196 << " attempt to read from output file" << std::endl; 00197 return false; 00198 } 00199 std::cerr << "IO_AsciiParticles input is not yet implemented" << std::endl; 00200 return false; 00201 } 00202 00203 void IO_AsciiParticles::write_comment( const std::string comment ) { 00204 // check the state of *m_outstream is good, and that it is in output mode 00205 if ( !m_outstream ) return; 00206 if ( !(m_mode&std::ios::out) ) { 00207 std::cerr << "HepMC::IO_AsciiParticles::write_particle_data_table " 00208 << " attempt to write to input file." << std::endl; 00209 return; 00210 } 00211 // write end of event listing key if events have already been written 00212 write_end_listing(); 00213 // insert the comment key before the comment 00214 *m_outstream << "\n" << "HepMC::IO_AsciiParticles-COMMENT\n"; 00215 *m_outstream << comment << std::endl; 00216 } 00217 00218 bool IO_AsciiParticles::write_end_listing() { 00219 return false; 00220 } 00221 00222 } // HepMC 00223