00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #include <list>
00013
00014 #include "HepMC/IO_GenEvent.h"
00015 #include "HepMC/IO_AsciiParticles.h"
00016 #include "HepMC/GenEvent.h"
00017 #include "HepMC/GenRanges.h"
00018
00019
00020 #include "IsGoodEvent.h"
00021 #include "testHepMCIteration.h"
00022
00023 bool findW( HepMC::GenEvent* evt, std::ofstream& os);
00024 bool simpleIter ( HepMC::GenEvent* evt, std::ostream& os = std::cout );
00025 bool simpleIter2( HepMC::GenEvent* evt, std::ostream& os = std::cout );
00026 bool simpleIter3( HepMC::GenEvent* evt, std::ostream& os = std::cout );
00027 bool simpleIter4( HepMC::GenEvent* evt, std::ostream& os = std::cout );
00028
00033 class PrintW {
00034 public:
00035 PrintW( std::ostream & os, int num ) : m_out( os ),m_event_num( num ) {}
00036 void operator()( HepMC::GenParticle* p ) {
00037 if ( IsWBoson(p) ) {
00038 m_out << std::endl;
00039 m_out << "A W boson has been found in event: " << m_event_num << std::endl;
00040 p->print( m_out );
00041
00042
00043
00044 m_out << "\t Its parents are: " << std::endl;
00045 if ( p->production_vertex() ) {
00046 std::for_each( p->particles_in(HepMC::parents).begin(),
00047 p->particles_in(HepMC::parents).end(),
00048 PrintParticle(m_out));
00049 }
00050
00051
00052 m_out << "\t\t" << "Its children are: " << std::endl;
00053 if ( p->end_vertex() ) {
00054 std::for_each( p->particles_out(HepMC::children).begin(),
00055 p->particles_out(HepMC::children).end(),
00056 PrintChildren(m_out));
00057 }
00058
00059
00060
00061
00062 m_out << "\t\t Its descendants are: " << std::endl;
00063 if ( p->end_vertex() ) {
00064 std::for_each( p->particles_out(HepMC::descendants).begin(),
00065 p->particles_out(HepMC::descendants).end(),
00066 PrintDescendants(m_out));
00067 }
00068 }
00069 }
00070 private:
00071 std::ostream & m_out;
00072 int m_event_num;
00073 };
00074
00079 class PrintConstW {
00080 public:
00081 PrintConstW( std::ostream & os, int num ) : m_out( os ),m_event_num( num ) {}
00082 void operator()( HepMC::GenParticle* p ) {
00083 if ( IsWBoson(p) ) {
00084 m_out << std::endl;
00085 m_out << "A W boson has been found in event: " << m_event_num << std::endl;
00086 p->print( m_out );
00087
00088
00089
00090 m_out << "\t Its parents are: " << std::endl;
00091 if ( p->production_vertex() ) {
00092 std::for_each( p->particles_in(HepMC::parents).begin(),
00093 p->particles_in(HepMC::parents).end(),
00094 PrintParticle(m_out));
00095 }
00096
00097
00098 m_out << "\t\t" << "Its children are: " << std::endl;
00099 if ( p->end_vertex() ) {
00100 std::for_each( p->particles_out(HepMC::children).begin(),
00101 p->particles_out(HepMC::children).end(),
00102 PrintChildren(m_out));
00103 }
00104
00105
00106
00107
00108 m_out << "\t\t Its descendants are: " << std::endl;
00109 if ( p->end_vertex() ) {
00110 std::for_each( p->particles_out(HepMC::descendants).begin(),
00111 p->particles_out(HepMC::descendants).end(),
00112 PrintDescendants(m_out));
00113 }
00114 }
00115 }
00116 private:
00117 std::ostream & m_out;
00118 int m_event_num;
00119 };
00120
00121 int main() {
00122
00123
00124 HepMC::IO_GenEvent ascii_in("@srcdir@/testIOGenEvent.input",std::ios::in);
00125
00126 IsGoodEvent is_good_event;
00127
00128 std::ofstream os( "testHepMCIteration.out" );
00129 std::ofstream os2( "testHepMCIteration2.out" );
00130 std::ofstream os3( "testHepMCIteration3.out" );
00131
00132 int icount=0;
00133 int num_good_events=0;
00134 HepMC::GenEvent* evt = ascii_in.read_next_event();
00135 HepMC::GenEvent* evcopy;
00136 while ( evt ) {
00137 icount++;
00138 if ( icount%50==1 ) std::cout << "Processing Event Number " << icount
00139 << " its # " << evt->event_number()
00140 << std::endl;
00141
00142 if ( icount==100 ) std::cout << "Processing Event Number " << icount
00143 << " its # " << evt->event_number()
00144 << std::endl;
00145 evcopy = evt;
00146 if ( is_good_event(evcopy) ) {
00147 ++num_good_events;
00148
00149 os << "Event " << evcopy->event_number() << " is good " << std::endl;
00150 simpleIter( evcopy, os );
00151 os2 << "Event " << evcopy->event_number() << " is good " << std::endl;
00152 simpleIter2( evcopy, os2 );
00153 os3 << "Event " << evcopy->event_number() << " is good " << std::endl;
00154 simpleIter2( evcopy, os3 );
00155 std::cout << "Event " << evcopy->event_number() << " is good " << std::endl;
00156 simpleIter3( evcopy );
00157 simpleIter4( evcopy );
00158
00159 findW( evcopy, os );
00160
00161 std::for_each( evt->particles_begin(), evt->particles_end(),
00162 PrintW(os2,evcopy->event_number()));
00163
00164 std::for_each( evt->particles_begin(), evt->particles_end(),
00165 PrintConstW(os3,evcopy->event_number()));
00166 }
00167 evcopy->clear();
00168
00169
00170 delete evt;
00171 evt = ascii_in.read_next_event();
00172 }
00173
00174 std::cout << num_good_events << " out of " << icount
00175 << " processed events passed the cuts. Finished." << std::endl;
00176 }
00177
00178 bool simpleIter( HepMC::GenEvent* evt, std::ostream& os )
00179 {
00180
00181
00182 std::list<HepMC::GenVertex*> allvertices;
00183 for ( HepMC::GenEvent::vertex_iterator v = evt->vertices_begin();
00184 v != evt->vertices_end(); ++v ) {
00185 allvertices.push_back(*v);
00186 }
00187
00188
00189
00190 IsFinalState isfinal;
00191 std::list<HepMC::GenParticle*> finalstateparticles;
00192 for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin();
00193 p != evt->particles_end(); ++p ) {
00194 if ( isfinal(*p) ) finalstateparticles.push_back(*p);
00195 }
00196
00197
00198 os << "photons in event " << evt->event_number() << ":" << std::endl;
00199 for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin();
00200 p != evt->particles_end(); ++p ) {
00201 if ( IsPhoton(*p) ) (*p)->print( os );
00202 }
00203
00204 return true;
00205 }
00206
00207 bool simpleIter2( HepMC::GenEvent* evt, std::ostream& os )
00208 {
00209
00210
00211
00212
00213 std::list<HepMC::GenVertex*> allvertices2;
00214 copy( evt->vertices_begin(), evt->vertices_end(),
00215 back_inserter(allvertices2) );
00216
00217
00218
00219
00220
00221 std::list<HepMC::GenParticle*> finalstateparticles2;
00222 HepMC::copy_if( evt->particles_begin(), evt->particles_end(),
00223 back_inserter(finalstateparticles2), IsFinalState() );
00224
00225
00226
00227 os << "photons in event " << evt->event_number() << ":" << std::endl;
00228 std::for_each(evt->particles_begin(), evt->particles_end(),
00229 PrintPhoton(os));
00230
00231 return true;
00232 }
00233
00234 bool simpleIter3( HepMC::GenEvent* evt, std::ostream& os )
00235 {
00236
00237
00238
00239
00240 std::list<HepMC::GenVertex*> allvertices;
00241 HepMC::GenEventVertexRange vc(*evt);
00242 for ( HepMC::GenEvent::vertex_iterator v = vc.begin(); v != vc.end(); ++v ) {
00243 allvertices.push_back(*v);
00244 }
00245
00246
00247
00248 IsFinalState isfinal;
00249 std::list<HepMC::GenParticle*> finalstateparticles;
00250 HepMC::GenEventParticleRange pc(*evt);
00251 for ( HepMC::GenEvent::particle_iterator p = pc.begin(); p != pc.end(); ++p ) {
00252 if ( isfinal(*p) ) finalstateparticles.push_back(*p);
00253 }
00254
00255
00256 os << "photons in event " << evt->event_number() << ":" << std::endl;
00257 std::for_each(pc.begin(), pc.end(), PrintPhoton(os));
00258
00259 return true;
00260 }
00261
00262 bool simpleIter4( HepMC::GenEvent* evt, std::ostream& os )
00263 {
00264
00265
00266
00267
00268
00269
00270
00271 std::list<HepMC::GenVertex*> allvertices;
00272 for ( HepMC::GenEvent::vertex_iterator v = evt->vertex_range().begin();
00273 v != evt->vertex_range().end(); ++v ) {
00274 allvertices.push_back(*v);
00275 }
00276
00277
00278
00279 IsFinalState isfinal;
00280 std::list<HepMC::GenParticle*> finalstateparticles;
00281 for ( HepMC::GenEvent::particle_iterator p = evt->particle_range().begin();
00282 p != evt->particle_range().end(); ++p ) {
00283 if ( isfinal(*p) ) finalstateparticles.push_back(*p);
00284 }
00285
00286
00287 os << "photons in event " << evt->event_number() << ":" << std::endl;
00288 std::for_each(evt->particle_range().begin(),
00289 evt->particle_range().end(),
00290 PrintPhoton(os));
00291
00292 return true;
00293 }
00294
00295 bool findW( HepMC::GenEvent* evt, std::ofstream& os )
00296 {
00297 int num_W = 0;
00298
00299
00300
00301
00302
00303
00304 for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin();
00305 p != evt->particles_end(); ++p ) {
00306 if ( IsWBoson(*p) ) {
00307 ++num_W;
00308 os << std::endl;
00309 os << "A W boson has been found in event: " << evt->event_number() << std::endl;
00310 (*p)->print( os );
00311
00312
00313
00314 os << "\t Its parents are: " << std::endl;
00315 if ( (*p)->production_vertex() ) {
00316 for ( HepMC::GenVertex::particle_iterator mother
00317 = (*p)->production_vertex()->
00318 particles_begin(HepMC::parents);
00319 mother != (*p)->production_vertex()->
00320 particles_end(HepMC::parents);
00321 ++mother ) {
00322 os << "\t";
00323 (*mother)->print( os );
00324 }
00325 }
00326
00327
00328 os << "\t\t" << "Its children are: " << std::endl;
00329 if ( (*p)->end_vertex() ) {
00330 for ( HepMC::GenVertex::particle_iterator child =
00331 (*p)->end_vertex()->particles_begin(HepMC::children);
00332 child != (*p)->end_vertex()->particles_end(HepMC::children);
00333 ++child ) {
00334
00335 HepMC::GenVertex::particle_iterator cp = child;
00336
00337 os << "\t\t\t (id,barcode,status) "
00338 << (*cp)->pdg_id() << " "
00339 << (*child)->barcode() << " "
00340 << (*cp)->status() << std::endl;
00341 }
00342 }
00343
00344
00345
00346
00347 os << "\t\t Its descendants are: " << std::endl;
00348 if ( (*p)->end_vertex() ) {
00349 for ( HepMC::GenVertex::particle_iterator des
00350 =(*p)->end_vertex()->
00351 particles_begin(HepMC::descendants);
00352 des != (*p)->end_vertex()->
00353 particles_end(HepMC::descendants);
00354 ++des ) {
00355 os << "\t\t";
00356 (*des)->print( os );
00357 }
00358 }
00359 }
00360 }
00361 return true;
00362 }