hepmc - Diff between revs 2 and 63
Subversion Repositories:
| Rev 2 | Rev 63 | |||
|---|---|---|---|---|
| Line 40... | Line 40... | |||
| 40 | return 0; | 40 | return 0; | |
| 41 | } | 41 | } | |
| 42 | }; | 42 | }; | |
| 43 | 43 | |||
| 44 | int main() { | 44 | int main() { | |
| 45 | - | |||
| 46 | // an event has been prepared in advance for this example, read it | - | ||
| 47 | // into memory using the IO_Ascii input strategy | - | ||
| 48 | HepMC::IO_Ascii ascii_in("example_UsingIterators.txt",std::ios::in); | - | ||
| 49 | if ( ascii_in.rdstate() == std::ios::failbit ) { | - | ||
| 50 | std::cerr << "ERROR input file example_UsingIterators.txt is needed " | - | ||
| 51 | << "and does not exist. " | - | ||
| 52 | << "\n Look for it in HepMC/examples, Exit." << std::endl; | - | ||
| 53 | return 1; | - | ||
| 54 | } | - | ||
| - | 45 | { // begin scope of ascii_in | ||
| - | 46 | // an event has been prepared in advance for this example, read it | ||
| - | 47 | // into memory using the IO_Ascii input strategy | ||
| - | 48 | HepMC::IO_Ascii ascii_in("example_UsingIterators.txt",std::ios::in); | ||
| - | 49 | if ( ascii_in.rdstate() == std::ios::failbit ) { | ||
| - | 50 | std::cerr << "ERROR input file example_UsingIterators.txt is needed " | ||
| - | 51 | << "and does not exist. " | ||
| - | 52 | << "\n Look for it in HepMC/examples, Exit." << std::endl; | ||
| - | 53 | return 1; | ||
| - | 54 | } | ||
| 55 | 55 | |||
| 56 | HepMC::GenEvent* evt = ascii_in.read_next_event(); | - | ||
| - | 56 | HepMC::GenEvent* evt = ascii_in.read_next_event(); | ||
| 57 | 57 | |||
| 58 | // if you wish to have a look at the event, then use evt->print(); | - | ||
| - | 58 | // if you wish to have a look at the event, then use evt->print(); | ||
| 59 | 59 | |||
| 60 | // use GenEvent::vertex_iterator to fill a list of all | - | ||
| 61 | // vertices in the event | - | ||
| 62 | std::list<HepMC::GenVertex*> allvertices; | - | ||
| 63 | for ( HepMC::GenEvent::vertex_iterator v = evt->vertices_begin(); | - | ||
| 64 | v != evt->vertices_end(); ++v ) { | - | ||
| 65 | allvertices.push_back(*v); | - | ||
| 66 | } | - | ||
| - | 60 | // use GenEvent::vertex_iterator to fill a list of all | ||
| - | 61 | // vertices in the event | ||
| - | 62 | std::list<HepMC::GenVertex*> allvertices; | ||
| - | 63 | for ( HepMC::GenEvent::vertex_iterator v = evt->vertices_begin(); | ||
| - | 64 | v != evt->vertices_end(); ++v ) { | ||
| - | 65 | allvertices.push_back(*v); | ||
| - | 66 | } | ||
| 67 | 67 | |||
| 68 | // we could do the same thing with the STL algorithm copy | - | ||
| 69 | std::list<HepMC::GenVertex*> allvertices2; | - | ||
| 70 | copy( evt->vertices_begin(), evt->vertices_end(), | - | ||
| 71 | back_inserter(allvertices2) ); | - | ||
| - | 68 | // we could do the same thing with the STL algorithm copy | ||
| - | 69 | std::list<HepMC::GenVertex*> allvertices2; | ||
| - | 70 | copy( evt->vertices_begin(), evt->vertices_end(), | ||
| - | 71 | back_inserter(allvertices2) ); | ||
| 72 | 72 | |||
| 73 | // fill a list of all final state particles in the event, by requiring | - | ||
| 74 | // that each particle satisfyies the IsFinalState predicate | - | ||
| 75 | IsFinalState isfinal; | - | ||
| 76 | std::list<HepMC::GenParticle*> finalstateparticles; | - | ||
| 77 | for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin(); | - | ||
| 78 | p != evt->particles_end(); ++p ) { | - | ||
| 79 | if ( isfinal(*p) ) finalstateparticles.push_back(*p); | - | ||
| 80 | } | - | ||
| 81 | - | |||
| 82 | // an STL-like algorithm called HepMC::copy_if is provided in the | - | ||
| 83 | // GenEvent.h header to do this sort of operation more easily, | - | ||
| 84 | // you could get the identical results as above by using: | - | ||
| 85 | std::list<HepMC::GenParticle*> finalstateparticles2; | - | ||
| 86 | HepMC::copy_if( evt->particles_begin(), evt->particles_end(), | - | ||
| 87 | back_inserter(finalstateparticles2), IsFinalState() ); | - | ||
| - | 73 | // fill a list of all final state particles in the event, by requiring | ||
| - | 74 | // that each particle satisfyies the IsFinalState predicate | ||
| - | 75 | IsFinalState isfinal; | ||
| - | 76 | std::list<HepMC::GenParticle*> finalstateparticles; | ||
| - | 77 | for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin(); | ||
| - | 78 | p != evt->particles_end(); ++p ) { | ||
| - | 79 | if ( isfinal(*p) ) finalstateparticles.push_back(*p); | ||
| - | 80 | } | ||
| 88 | 81 | |||
| 89 | // lets print all photons in the event that satisfy the IsPhoton criteria | - | ||
| 90 | IsPhoton isphoton; | - | ||
| 91 | for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin(); | - | ||
| 92 | p != evt->particles_end(); ++p ) { | - | ||
| 93 | if ( isphoton(*p) ) (*p)->print(); | - | ||
| 94 | } | - | ||
| - | 82 | // an STL-like algorithm called HepMC::copy_if is provided in the | ||
| - | 83 | // GenEvent.h header to do this sort of operation more easily, | ||
| - | 84 | // you could get the identical results as above by using: | ||
| - | 85 | std::list<HepMC::GenParticle*> finalstateparticles2; | ||
| - | 86 | HepMC::copy_if( evt->particles_begin(), evt->particles_end(), | ||
| - | 87 | back_inserter(finalstateparticles2), IsFinalState() ); | ||
| 95 | 88 | |||
| 96 | // the GenVertex::particle_iterator and GenVertex::vertex_iterator | - | ||
| 97 | // are slightly different from the GenEvent:: versions, in that | - | ||
| 98 | // the iterator starts at the given vertex, and walks through the attached | - | ||
| 99 | // vertex returning particles/vertices. | - | ||
| 100 | // Thus only particles/vertices which are in the same graph as the given | - | ||
| 101 | // vertex will be returned. A range is specified with these iterators, | - | ||
| 102 | // the choices are: | - | ||
| 103 | // parents, children, family, ancestors, descendants, relatives | - | ||
| 104 | // here are some examples. | - | ||
| - | 89 | // lets print all photons in the event that satisfy the IsPhoton criteria | ||
| - | 90 | IsPhoton isphoton; | ||
| - | 91 | for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin(); | ||
| - | 92 | p != evt->particles_end(); ++p ) { | ||
| - | 93 | if ( isphoton(*p) ) (*p)->print(); | ||
| - | 94 | } | ||
| 105 | 95 | |||
| 106 | // use GenEvent::particle_iterator to find all W's in the event, | - | ||
| 107 | // then | - | ||
| 108 | // (1) for each W user the GenVertex::particle_iterator with a range of | - | ||
| 109 | // parents to return and print the immediate mothers of these W's. | - | ||
| 110 | // (2) for each W user the GenVertex::particle_iterator with a range of | - | ||
| 111 | // descendants to return and print all descendants of these W's. | - | ||
| 112 | IsW_Boson isw; | - | ||
| 113 | for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin(); | - | ||
| 114 | p != evt->particles_end(); ++p ) { | - | ||
| 115 | if ( isw(*p) ) { | - | ||
| 116 | std::cout << "A W boson has been found: " << std::endl; | - | ||
| 117 | (*p)->print(); | - | ||
| 118 | // return all parents | - | ||
| 119 | // we do this by pointing to the production vertex of the W | - | ||
| 120 | // particle and asking for all particle parents of that vertex | - | ||
| 121 | std::cout << "\t Its parents are: " << std::endl; | - | ||
| 122 | if ( (*p)->production_vertex() ) { | - | ||
| 123 | for ( HepMC::GenVertex::particle_iterator mother | - | ||
| 124 | = (*p)->production_vertex()-> | - | ||
| 125 | particles_begin(HepMC::parents); | - | ||
| 126 | mother != (*p)->production_vertex()-> | - | ||
| 127 | particles_end(HepMC::parents); | - | ||
| 128 | ++mother ) { | - | ||
| 129 | std::cout << "\t"; | - | ||
| 130 | (*mother)->print(); | - | ||
| - | 96 | // the GenVertex::particle_iterator and GenVertex::vertex_iterator | ||
| - | 97 | // are slightly different from the GenEvent:: versions, in that | ||
| - | 98 | // the iterator starts at the given vertex, and walks through the attached | ||
| - | 99 | // vertex returning particles/vertices. | ||
| - | 100 | // Thus only particles/vertices which are in the same graph as the given | ||
| - | 101 | // vertex will be returned. A range is specified with these iterators, | ||
| - | 102 | // the choices are: | ||
| - | 103 | // parents, children, family, ancestors, descendants, relatives | ||
| - | 104 | // here are some examples. | ||
| - | 105 | |||
| - | 106 | // use GenEvent::particle_iterator to find all W's in the event, | ||
| - | 107 | // then | ||
| - | 108 | // (1) for each W user the GenVertex::particle_iterator with a range of | ||
| - | 109 | // parents to return and print the immediate mothers of these W's. | ||
| - | 110 | // (2) for each W user the GenVertex::particle_iterator with a range of | ||
| - | 111 | // descendants to return and print all descendants of these W's. | ||
| - | 112 | IsW_Boson isw; | ||
| - | 113 | for ( HepMC::GenEvent::particle_iterator p = evt->particles_begin(); | ||
| - | 114 | p != evt->particles_end(); ++p ) { | ||
| - | 115 | if ( isw(*p) ) { | ||
| - | 116 | std::cout << "A W boson has been found: " << std::endl; | ||
| - | 117 | (*p)->print(); | ||
| - | 118 | // return all parents | ||
| - | 119 | // we do this by pointing to the production vertex of the W | ||
| - | 120 | // particle and asking for all particle parents of that vertex | ||
| - | 121 | std::cout << "\t Its parents are: " << std::endl; | ||
| - | 122 | if ( (*p)->production_vertex() ) { | ||
| - | 123 | for ( HepMC::GenVertex::particle_iterator mother | ||
| - | 124 | = (*p)->production_vertex()-> | ||
| - | 125 | particles_begin(HepMC::parents); | ||
| - | 126 | mother != (*p)->production_vertex()-> | ||
| - | 127 | particles_end(HepMC::parents); | ||
| - | 128 | ++mother ) { | ||
| - | 129 | std::cout << "\t"; | ||
| - | 130 | (*mother)->print(); | ||
| - | 131 | } | ||
| 131 | } | 132 | } | |
| 132 | } | - | ||
| 133 | // return all descendants | - | ||
| 134 | // we do this by pointing to the end vertex of the W | - | ||
| 135 | // particle and asking for all particle descendants of that vertex | - | ||
| 136 | std::cout << "\t\t Its descendants are: " << std::endl; | - | ||
| 137 | if ( (*p)->end_vertex() ) { | - | ||
| 138 | for ( HepMC::GenVertex::particle_iterator des | - | ||
| 139 | =(*p)->end_vertex()-> | - | ||
| 140 | particles_begin(HepMC::descendants); | - | ||
| 141 | des != (*p)->end_vertex()-> | - | ||
| 142 | particles_end(HepMC::descendants); | - | ||
| 143 | ++des ) { | - | ||
| 144 | std::cout << "\t\t"; | - | ||
| 145 | (*des)->print(); | - | ||
| - | 133 | // return all descendants | ||
| - | 134 | // we do this by pointing to the end vertex of the W | ||
| - | 135 | // particle and asking for all particle descendants of that vertex | ||
| - | 136 | std::cout << "\t\t Its descendants are: " << std::endl; | ||
| - | 137 | if ( (*p)->end_vertex() ) { | ||
| - | 138 | for ( HepMC::GenVertex::particle_iterator des | ||
| - | 139 | =(*p)->end_vertex()-> | ||
| - | 140 | particles_begin(HepMC::descendants); | ||
| - | 141 | des != (*p)->end_vertex()-> | ||
| - | 142 | particles_end(HepMC::descendants); | ||
| - | 143 | ++des ) { | ||
| - | 144 | std::cout << "\t\t"; | ||
| - | 145 | (*des)->print(); | ||
| - | 146 | } | ||
| 146 | } | 147 | } | |
| 147 | } | 148 | } | |
| 148 | } | 149 | } | |
| 149 | } | - | ||
| 150 | 150 | |||
| 151 | // in analogy to the above, similar use can be made of the | - | ||
| 152 | // HepMC::GenVertex::vertex_iterator, which also accepts a range. | - | ||
| - | 151 | // in analogy to the above, similar use can be made of the | ||
| - | 152 | // HepMC::GenVertex::vertex_iterator, which also accepts a range. | ||
| - | 153 | } // end scope of ascii_in | ||
| 153 | 154 | |||
| 154 | return 0; | 155 | return 0; | |
| 155 | } | 156 | } | |
