00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00012
00013
00014 #include "HepMC/GenEvent.h"
00015 #include "HepMC/IO_GenEvent.h"
00016 #include "HepMC/IO_Ascii.h"
00017 #include "HepMC/IO_AsciiParticles.h"
00018
00019
00020 #include "IsGoodEvent.h"
00021
00022 void read_testIOGenEvent();
00023 void read_testAscii();
00024 void read_variousFormats();
00025
00026 double findPiZero( HepMC::GenEvent* );
00027
00028 int main() {
00029 read_testIOGenEvent();
00030 read_testAscii();
00031 read_variousFormats();
00032 return 0;
00033 }
00034
00035 void read_testIOGenEvent()
00036 {
00037 std::cout << std::endl;
00038 std::cout << "basic IO_GenEvent input and output" << std::endl;
00039
00040
00041 HepMC::IO_GenEvent ascii_in("@srcdir@/testIOGenEvent.input",std::ios::in);
00042 ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM );
00043
00044 HepMC::IO_GenEvent ascii_out("testHepMC.out",std::ios::out);
00045
00046 HepMC::IO_AsciiParticles particle_out("testHepMCParticle.out",std::ios::out);
00047
00048 IsGoodEvent is_good_event;
00049
00050 int icount=0;
00051 int num_good_events=0;
00052 HepMC::GenEvent* evt = ascii_in.read_next_event();
00053 while ( evt ) {
00054 icount++;
00055 if ( icount%50==1 ) std::cout << "Processing Event Number " << icount
00056 << " its # " << evt->event_number()
00057 << std::endl;
00058 if ( is_good_event(evt) ) {
00059 ascii_out << evt;
00060 particle_out << evt;
00061 ++num_good_events;
00062 }
00063
00064
00065 delete evt;
00066 ascii_in >> evt;
00067 }
00068
00069 std::cout << num_good_events << " out of " << icount
00070 << " processed events passed the cuts. Finished." << std::endl;
00071 }
00072
00073 void read_testAscii()
00074 {
00075 std::cout << std::endl;
00076 std::cout << "read file written with IO_Ascii" << std::endl;
00077
00078
00079 HepMC::IO_GenEvent ascii_in("@srcdir@/testAscii.input",std::ios::in);
00080 ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM );
00081 if ( ascii_in.rdstate() == std::ios::failbit ) {
00082 std::cerr << "ERROR input file @srcdir@/testAscii.input is needed "
00083 << "and does not exist. Exit." << std::endl;
00084 return;
00085 }
00086
00087 HepMC::IO_Ascii ascii_out("testIOAscii.dat",std::ios::out);
00088 if ( ascii_out.rdstate() == std::ios::failbit ) {
00089 std::cerr << "ERROR opening output file testAscii.dat. Exit."
00090 << std::endl;
00091 return;
00092 }
00093
00094 int icount=0;
00095 HepMC::GenEvent* evt = ascii_in.read_next_event();
00096 while ( evt ) {
00097 icount++;
00098 if ( icount%50==1 ) std::cout << "Processing Event Number " << icount
00099 << " its # " << evt->event_number()
00100 << std::endl;
00101 ascii_out << evt;
00102
00103
00104 delete evt;
00105 ascii_in >> evt;
00106 }
00107
00108 std::cout << icount << " events processed. Finished." << std::endl;
00109 }
00110
00111 void read_variousFormats()
00112 {
00113 std::cout << std::endl;
00114 std::cout << "process varied input" << std::endl;
00115
00116 HepMC::IO_GenEvent ascii_in("@srcdir@/testHepMCVarious.input",std::ios::in);
00117 ascii_in.use_input_units( HepMC::Units::GEV, HepMC::Units::MM );
00118
00119 HepMC::IO_GenEvent ascii_out("testHepMCVarious.out",std::ios::out);
00120
00121 int icount=0;
00122 HepMC::GenEvent* evt = ascii_in.read_next_event();
00123 while ( evt ) {
00124 icount++;
00125 double pim;
00126 std::cout << "Processing Event Number " << icount
00127 << " its # " << evt->event_number()
00128 << std::endl;
00129 ascii_out << evt;
00130
00131 evt->write_units();
00132 pim = findPiZero(evt);
00133 std::cout << " pizero mass: " << pim << std::endl;
00134
00135 evt->use_units(HepMC::Units::GEV, HepMC::Units::MM);
00136 evt->write_units();
00137 pim = findPiZero(evt);
00138 std::cout << " pizero mass: " << pim
00139 << " " << HepMC::Units::name( evt->momentum_unit() ) << std::endl;
00140
00141 evt->use_units(HepMC::Units::MEV, HepMC::Units::MM);
00142 evt->write_units();
00143 pim = findPiZero(evt);
00144 std::cout << " pizero mass: " << pim
00145 << " " << HepMC::Units::name( evt->momentum_unit() ) << std::endl;
00146
00147 delete evt;
00148 ascii_in >> evt;
00149 }
00150
00151 std::cout << icount << " events processed. Finished." << std::endl;
00152 }
00153
00154 double findPiZero( HepMC::GenEvent* evt )
00155 {
00156 for ( HepMC::GenEvent::particle_const_iterator p
00157 = evt->particles_begin(); p != evt->particles_end(); ++p ){
00158 if ( (*p)->pdg_id() == 111 ) {
00159 return (*p)->generated_mass();
00160 }
00161 }
00162 return 0.;
00163 }