![]() |
HepMC Reference DocumentationHepMC |
00001 00002 // testStreamIO.cc.in 00003 // 00004 // garren@fnal.gov, March 2006 00005 // 00006 // The same as testHepMC, but using the IO stream directly 00008 // 00009 00010 #include <fstream> 00011 00012 #include "HepMC/GenEvent.h" 00013 #include "HepMC/IO_AsciiParticles.h" 00014 #ifdef HEPMC_HAS_IO_GENEVENT 00015 #include "HepMC/IO_GenEvent.h" 00016 #endif 00017 #include "HepMC/Version.h" 00018 #include "HepMC/IO_Exception.h" 00019 00020 // define methods and classes used by this test 00021 #include "IsGoodEvent.h" 00022 #include "testHepMCMethods.h" 00023 00024 void read_testIOGenEvent(); 00025 void read_variousFormats(); 00026 void write_to_stream(); 00027 void write_to_stream3(); 00028 void read_from_stream4(); 00029 00030 int main() { 00031 write_to_stream(); 00032 read_testIOGenEvent(); 00033 read_variousFormats(); 00034 write_to_stream3(); 00035 read_from_stream4(); 00036 return 0; 00037 } 00038 00039 void write_to_stream() 00040 { 00041 std::cout << std::endl; 00042 std::cout << "basic IO_GenEvent input with streaming output" << std::endl; 00043 // declare an input strategy to read the data produced with the 00044 // example_MyPythia - units are GeV and mm 00045 HepMC::IO_GenEvent ascii_in("@srcdir@/testIOGenEvent.input",std::ios::in); 00046 ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM ); 00047 // declare an output stream 00048 const char outfile[] = "testStreamIO.out"; 00049 std::ofstream ascii_out( outfile ); 00050 if( !ascii_out ) { 00051 std::cerr << "cannot open " << outfile << std::endl; 00052 exit(-1); 00053 } 00054 ascii_out.precision(16); 00055 HepMC::write_HepMC_IO_block_begin( ascii_out ); 00056 // declare an instance of the event selection predicate 00057 IsGoodEvent is_good_event; 00058 //........................................EVENT LOOP 00059 int icount=0; 00060 int num_good_events=0; 00061 HepMC::GenEvent* evt = ascii_in.read_next_event(); 00062 while ( evt ) { 00063 icount++; 00064 if ( icount%50==1 ) std::cout << "Processing Event Number " << icount 00065 << " its # " << evt->event_number() 00066 << std::endl; 00067 if ( is_good_event(evt) ) { 00068 ++num_good_events; 00069 particleTypes( evt ); 00070 ascii_out << (*evt); 00071 } 00072 00073 // clean up and get next event 00074 delete evt; 00075 ascii_in >> evt; 00076 } 00077 HepMC::write_HepMC_IO_block_end( ascii_out ); 00078 //........................................PRINT RESULT 00079 std::cout << num_good_events << " out of " << icount 00080 << " processed events passed the cuts. Finished." << std::endl; 00081 } 00082 00083 void read_testIOGenEvent() 00084 { 00085 std::cout << std::endl; 00086 std::cout << "streaming input and output" << std::endl; 00087 // input units are GeV and mm 00088 const char infile[] = "@srcdir@/testIOGenEvent.input"; 00089 std::ifstream is( infile ); 00090 if( !is ) { 00091 std::cerr << "cannot open " << infile << std::endl; 00092 exit(-1); 00093 } 00094 // declare an output stream 00095 const char outfile[] = "testStreamIO2.out"; 00096 std::ofstream ascii_out( outfile ); 00097 if( !ascii_out ) { 00098 std::cerr << "cannot open " << outfile << std::endl; 00099 exit(-1); 00100 } 00101 ascii_out.precision(16); 00102 HepMC::write_HepMC_IO_block_begin( ascii_out ); 00103 // declare another output stream to test precision 00104 const char poutfile[] = "testStreamIOprecision.out"; 00105 std::ofstream pout( poutfile ); 00106 if( !pout ) { 00107 std::cerr << "cannot open " << poutfile << std::endl; 00108 exit(-1); 00109 } 00110 pout.precision(10); 00111 // declare an IO_AsciiParticle for output 00112 HepMC::IO_AsciiParticles particle_out("testStreamIOParticle.out",std::ios::out); 00113 // declare an instance of the event selection predicate 00114 IsGoodEvent is_good_event; 00115 //........................................EVENT LOOP 00116 int icount=0; 00117 int num_good_events=0; 00118 HepMC::GenEvent evt; 00119 while ( is ) { 00120 // WARNING - we are not using pointers, so this could be an empty event 00121 is >> evt; 00122 // make sure this is a valid event 00123 if( evt.is_valid() ) { 00124 ++icount; 00125 if ( icount%50==1 ) std::cout << "Processing Event Number " << icount 00126 << " its # " << evt.event_number() 00127 << std::endl; 00128 if ( is_good_event( &evt ) ) { 00129 ++num_good_events; 00130 particleTypes(&evt); 00131 ascii_out << evt; 00132 pout << evt; 00133 // We must explicitly create the pointer if we want to use this event 00134 // with any IO strategy (e.g., IO_AsciiParticles) 00135 HepMC::GenEvent* pevt= &evt; 00136 particle_out << pevt; 00137 } 00138 } 00139 } 00140 HepMC::write_HepMC_IO_block_end( ascii_out ); 00141 //........................................PRINT RESULT 00142 std::cout << num_good_events << " out of " << icount 00143 << " processed events passed the cuts. Finished." << std::endl; 00144 } 00145 00146 void read_variousFormats() 00147 { 00148 std::cout << std::endl; 00149 std::cout << "process varied input" << std::endl; 00150 // declare an input stream 00151 const char infile[] = "@srcdir@/testHepMCVarious.input"; 00152 std::ifstream is( infile ); 00153 if( !is ) { 00154 std::cerr << "cannot open " << infile << std::endl; 00155 exit(-1); 00156 } 00157 // set input units 00158 HepMC::set_input_units( is, HepMC::Units::GEV, HepMC::Units::MM ); 00159 // declare an output stream 00160 const char outfile[] = "testStreamIOVarious.out"; 00161 std::ofstream ascii_out( outfile ); 00162 if( !ascii_out ) { 00163 std::cerr << "cannot open " << outfile << std::endl; 00164 exit(-1); 00165 } 00166 ascii_out.precision(16); 00167 HepMC::write_HepMC_IO_block_begin( ascii_out ); 00168 //........................................EVENT LOOP 00169 int icount=0, ibad=0; 00170 HepMC::GenEvent evt; 00171 while ( is ) { 00172 // we have to do our own try/catch blocks 00173 try { 00174 is >> evt; 00175 } 00176 catch (HepMC::IO_Exception& e) { 00177 evt.clear(); 00178 ++ibad; 00179 } 00180 // WARNING - we are not using pointers, so this could be an empty event 00181 // make sure this is a valid event 00182 if( evt.is_valid() ) { 00183 icount++; 00184 double pim; 00185 std::cout << "Processing Event Number " << icount 00186 << " its # " << evt.event_number() 00187 << std::endl; 00188 ascii_out << evt; 00189 // units should be unknown 00190 evt.write_units(); 00191 pim = findPiZero(&evt); 00192 std::cout << " pizero mass: " << pim << std::endl; 00193 // set units to GeV and mm 00194 evt.use_units(HepMC::Units::GEV, HepMC::Units::MM); 00195 evt.write_units(); 00196 pim = findPiZero(&evt); 00197 std::cout << " pizero mass: " << pim 00198 << " " << HepMC::Units::name( evt.momentum_unit() ) << std::endl; 00199 // convert units to MeV 00200 evt.use_units(HepMC::Units::MEV, HepMC::Units::MM); 00201 evt.write_units(); 00202 pim = findPiZero(&evt); 00203 std::cout << " pizero mass: " << pim 00204 << " " << HepMC::Units::name( evt.momentum_unit() ) << std::endl; 00205 } 00206 } 00207 HepMC::write_HepMC_IO_block_end( ascii_out ); 00208 //........................................PRINT RESULT 00209 std::cout << icount << " valid events processed. " ; 00210 std::cout << ibad << " invalid events processed. Finished." << std::endl; 00211 } 00212 00213 void write_to_stream3() 00214 { 00215 std::cout << std::endl; 00216 std::cout << "basic IO_GenEvent input with streaming output using member function" << std::endl; 00217 // declare an input strategy to read the data produced with the 00218 // example_MyPythia - units are GeV and mm 00219 HepMC::IO_GenEvent ascii_in("@srcdir@/testIOGenEvent.input",std::ios::in); 00220 ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM ); 00221 // declare an output stream 00222 const char outfile[] = "testStreamIO3.out"; 00223 std::ofstream ascii_out( outfile ); 00224 if( !ascii_out ) { 00225 std::cerr << "cannot open " << outfile << std::endl; 00226 exit(-1); 00227 } 00228 ascii_out.precision(16); 00229 HepMC::write_HepMC_IO_block_begin( ascii_out ); 00230 // declare an instance of the event selection predicate 00231 IsGoodEvent is_good_event; 00232 //........................................EVENT LOOP 00233 int icount=0; 00234 int num_good_events=0; 00235 HepMC::GenEvent* evt = ascii_in.read_next_event(); 00236 while ( evt ) { 00237 icount++; 00238 if ( icount%50==1 ) std::cout << "Processing Event Number " << icount 00239 << " its # " << evt->event_number() 00240 << std::endl; 00241 if ( is_good_event(evt) ) { 00242 ++num_good_events; 00243 particleTypes( evt ); 00244 evt->write(ascii_out); 00245 } 00246 00247 // clean up and get next event 00248 delete evt; 00249 ascii_in >> evt; 00250 } 00251 HepMC::write_HepMC_IO_block_end( ascii_out ); 00252 //........................................PRINT RESULT 00253 std::cout << num_good_events << " out of " << icount 00254 << " processed events passed the cuts. Finished." << std::endl; 00255 } 00256 00257 void read_from_stream4() 00258 { 00259 std::cout << std::endl; 00260 std::cout << "streaming input and output using member functions" << std::endl; 00261 // input units are GeV and mm 00262 const char infile[] = "@srcdir@/testIOGenEvent.input"; 00263 std::ifstream is( infile ); 00264 if( !is ) { 00265 std::cerr << "cannot open " << infile << std::endl; 00266 exit(-1); 00267 } 00268 // declare an output stream 00269 const char outfile[] = "testStreamIO4.out"; 00270 std::ofstream ascii_out( outfile ); 00271 if( !ascii_out ) { 00272 std::cerr << "cannot open " << outfile << std::endl; 00273 exit(-1); 00274 } 00275 ascii_out.precision(16); 00276 HepMC::write_HepMC_IO_block_begin( ascii_out ); 00277 // declare an instance of the event selection predicate 00278 IsGoodEvent is_good_event; 00279 //........................................EVENT LOOP 00280 int icount=0; 00281 int num_good_events=0; 00282 HepMC::GenEvent evt; 00283 while ( is ) { 00284 // WARNING - we are not using pointers, so this could be an empty event 00285 evt.read(is); 00286 // make sure this is a valid event 00287 if( evt.is_valid() ) { 00288 ++icount; 00289 if ( icount%50==1 ) std::cout << "Processing Event Number " << icount 00290 << " its # " << evt.event_number() 00291 << std::endl; 00292 if ( is_good_event( &evt ) ) { 00293 ++num_good_events; 00294 particleTypes(&evt); 00295 evt.write(ascii_out); 00296 } 00297 } 00298 } 00299 HepMC::write_HepMC_IO_block_end( ascii_out ); 00300 //........................................PRINT RESULT 00301 std::cout << num_good_events << " out of " << icount 00302 << " processed events passed the cuts. Finished." << std::endl; 00303 }