![]() |
HepMC Reference DocumentationHepMC |
00001 //-------------------------------------------------------------------------- 00002 #ifndef HEPMC_GEN_VERTEX_H 00003 #define HEPMC_GEN_VERTEX_H 00004 00006 // Matt.Dobbs@Cern.CH, September 1999, refer to: 00007 // M. Dobbs and J.B. Hansen, "The HepMC C++ Monte Carlo Event Record for 00008 // High Energy Physics", Computer Physics Communications (to be published). 00009 // 00010 // GenVertex within an event 00011 // A vertex is indirectly (via particle "edges") linked to other 00012 // vertices ("nodes") to form a composite "graph" 00014 00015 // --> HANDLE COMPILER INCONSISTENCIES 00016 // This pre-compiler directive is included (2002-01-16) to allow compatibility 00017 // with several compilers. 00018 // Mar 27, 2004: HepMC is now standard compliant only. 00019 // I've removed forward_iterator, and it will no longer compile on gcc < 3. 00020 #ifdef __SUNPRO_CC // Solaris CC 5.2 00021 #define NEED_SOLARIS_FRIEND_FEATURE 00022 #endif // Platform 00023 00024 #include "HepMC/WeightContainer.h" 00025 #include "HepMC/SimpleVector.h" 00026 #include <iostream> 00027 #include <iterator> 00028 #include <vector> 00029 #include <set> 00030 #include <algorithm> 00031 00032 namespace HepMC { 00033 00035 enum IteratorRange { parents, children, family, 00036 ancestors, descendants, relatives }; 00037 class GenParticle; 00038 class GenEvent; 00039 00041 00047 class GenVertex { 00048 00050 friend std::ostream& operator<<( std::ostream&, const GenVertex& ); 00051 friend class GenEvent; 00052 00053 #ifdef NEED_SOLARIS_FRIEND_FEATURE 00054 // This bit of ugly code is only for CC-5.2 compiler. 00055 // M.Dobbs 2002/02/19 00056 // It is not needed by linux gcc, nor Windows Visual C++. 00057 public: 00058 class vertex_iterator; 00059 friend class vertex_iterator; 00060 class particle_iterator; 00061 friend class particle_iterator; 00062 #endif // NEED_SOLARIS_FRIEND_FEATURE 00063 00064 public: 00066 GenVertex( const FourVector& position =FourVector(0,0,0,0), 00067 int id = 0, 00068 const WeightContainer& weights = std::vector<double>() ); 00069 GenVertex( const GenVertex& invertex ); 00070 virtual ~GenVertex(); 00071 00072 void swap( GenVertex & other); 00073 GenVertex& operator= ( const GenVertex& invertex ); 00074 bool operator==( const GenVertex& a ) const; 00075 bool operator!=( const GenVertex& a ) const; 00076 void print( std::ostream& ostr = std::cout ) const; 00077 00078 double check_momentum_conservation() const; 00079 00081 void add_particle_in( GenParticle* inparticle ); 00083 void add_particle_out( GenParticle* outparticle ); 00088 GenParticle* remove_particle( GenParticle* particle ); 00089 00090 operator HepMC::FourVector() const; 00091 operator HepMC::ThreeVector() const; 00092 00094 // access methods // 00096 00098 GenEvent* parent_event() const; 00100 ThreeVector point3d() const; 00102 const FourVector & position() const; 00104 void set_position( const FourVector& position = FourVector(0,0,0,0) ); 00107 int id() const; 00108 void set_id( int id ); 00109 00120 int barcode() const; 00121 00123 bool suggest_barcode( int the_bar_code ); 00124 00126 WeightContainer& weights(); 00128 const WeightContainer& weights() const; 00129 00131 // Iterators // users should use prefer to use particle_iterator 00133 00135 typedef std::vector<HepMC::GenParticle*>::const_iterator 00136 particles_in_const_iterator; 00138 typedef std::vector<HepMC::GenParticle*>::const_iterator 00139 particles_out_const_iterator; 00141 particles_in_const_iterator particles_in_const_begin() const; 00143 particles_in_const_iterator particles_in_const_end() const; 00145 particles_out_const_iterator particles_out_const_begin() const; 00147 particles_out_const_iterator particles_out_const_end() const; 00149 int particles_in_size() const; 00151 int particles_out_size() const; 00152 00153 protected: 00154 //static unsigned int counter(); //!< temporary for debugging 00155 00159 void set_parent_event_( GenEvent* evt ); 00160 void set_barcode_( int the_bar_code ); 00161 void change_parent_event_( GenEvent* evt ); 00162 00164 // edge_iterator // (protected - for internal use only) 00166 // If the user wants the functionality of the edge_iterator, he should 00167 // use particle_iterator with IteratorRange = family, parents, children 00168 // 00169 00171 00178 class edge_iterator : 00179 public std::iterator<std::forward_iterator_tag,HepMC::GenParticle*,ptrdiff_t>{ 00180 public: 00181 edge_iterator(); 00183 edge_iterator( const GenVertex& vtx, IteratorRange range =family ); 00185 edge_iterator( const edge_iterator& p ); 00186 virtual ~edge_iterator(); 00188 edge_iterator& operator=( const edge_iterator& p ); 00190 GenParticle* operator*(void) const; 00192 edge_iterator& operator++(void); // Pre-fix increment 00194 edge_iterator operator++(int); // Post-fix increment 00196 bool operator==( const edge_iterator& a ) const; 00198 bool operator!=( const edge_iterator& a ) const; 00200 bool is_parent() const; 00202 bool is_child() const; 00204 const GenVertex* vertex_root() const; 00205 private: 00207 edge_iterator& operator--(void); 00209 edge_iterator operator--(int); 00210 private: 00211 const GenVertex* m_vertex; 00212 IteratorRange m_range; 00213 std::vector<HepMC::GenParticle*>::const_iterator m_set_iter; 00214 bool m_is_inparticle_iter; 00215 bool m_is_past_end; 00216 }; 00217 friend class edge_iterator; 00219 int edges_size( IteratorRange range = family ) const; 00221 edge_iterator edges_begin( IteratorRange range = family) const; 00223 edge_iterator edges_end( IteratorRange /* dummy_range */ ) const; 00224 00225 public: 00227 // vertex_iterator // 00229 00231 00247 class vertex_iterator : 00248 public std::iterator<std::forward_iterator_tag,HepMC::GenVertex*,ptrdiff_t>{ 00249 public: 00250 vertex_iterator(); 00252 vertex_iterator( GenVertex& vtx_root, IteratorRange range ); 00254 vertex_iterator( GenVertex& vtx_root, IteratorRange range, 00255 std::set<const HepMC::GenVertex*>& visited_vertices ); 00257 vertex_iterator( const vertex_iterator& v_iter ); 00258 virtual ~vertex_iterator(); 00260 vertex_iterator& operator=( const vertex_iterator& ); 00262 GenVertex* operator*(void) const; 00264 vertex_iterator& operator++(void); //Pre-fix increment 00266 vertex_iterator operator++(int); //Post-fix increment 00268 bool operator==( const vertex_iterator& ) const; 00270 bool operator!=( const vertex_iterator& ) const; 00272 GenVertex* vertex_root() const; 00274 IteratorRange range() const; 00276 void copy_with_own_set( const vertex_iterator& 00277 v_iter, 00278 std::set<const HepMC::GenVertex*>& 00279 visited_vertices ); 00280 00281 protected: // intended for internal use only 00283 GenVertex* follow_edge_(); 00285 void copy_recursive_iterator_( const vertex_iterator* 00286 recursive_v_iter ); 00287 private: 00289 vertex_iterator& operator--(void); 00291 vertex_iterator operator--(int); 00292 00293 private: 00294 GenVertex* m_vertex; // the vertex associated to this iter 00295 IteratorRange m_range; 00296 std::set<const HepMC::GenVertex*>* m_visited_vertices; 00297 bool m_it_owns_set; // true if it is responsible for 00298 // deleting the visited vertex set 00299 edge_iterator m_edge; // particle edge pointing to return vtx 00300 vertex_iterator* m_recursive_iterator; 00301 }; 00302 friend class vertex_iterator; 00304 vertex_iterator vertices_begin( IteratorRange range = relatives ); 00306 vertex_iterator vertices_end( IteratorRange /* dummy_range */ ); 00307 00308 public: 00310 // particle_iterator // 00312 00314 00323 class particle_iterator : 00324 public std::iterator<std::forward_iterator_tag,GenParticle*,ptrdiff_t>{ 00325 public: 00326 particle_iterator(); 00328 particle_iterator( GenVertex& vertex_root, IteratorRange range ); 00330 particle_iterator( const particle_iterator& ); 00331 virtual ~particle_iterator(); 00333 particle_iterator& operator=( const particle_iterator& ); 00335 GenParticle* operator*(void) const; 00337 particle_iterator& operator++(void); 00339 particle_iterator operator++(int); 00341 bool operator==( const particle_iterator& ) const; 00343 bool operator!=( const particle_iterator& ) const; 00344 protected: 00345 GenParticle* advance_to_first_(); 00346 private: 00347 vertex_iterator m_vertex_iterator; 00348 edge_iterator m_edge; // points to the return 00349 }; 00350 friend class particle_iterator; 00352 particle_iterator particles_begin( IteratorRange range 00353 = relatives ); 00355 particle_iterator particles_end( IteratorRange 00356 /* dummy_range */ ); 00357 00359 protected: 00361 void delete_adopted_particles(); 00363 void remove_particle_in( GenParticle* ); 00365 void remove_particle_out( GenParticle* ); 00368 void convert_position( const double& ); 00369 00370 private: // GenVertex data members 00371 FourVector m_position; //4-vec of vertex [mm] 00372 std::vector<HepMC::GenParticle*> m_particles_in; //all incoming particles 00373 std::vector<HepMC::GenParticle*> m_particles_out; //all outgoing particles 00374 int m_id; 00375 WeightContainer m_weights; // weights for this vtx 00376 GenEvent* m_event; 00377 int m_barcode; // unique identifier in the event 00378 00379 //static unsigned int s_counter; 00380 }; 00381 00383 // INLINES access methods // 00385 00386 inline GenVertex::operator HepMC::FourVector() const { return position(); } 00387 00388 inline GenVertex::operator HepMC::ThreeVector() const { return point3d(); } 00389 00390 inline const FourVector & GenVertex::position() const { return m_position; } 00391 00392 inline GenEvent* GenVertex::parent_event() const { return m_event; } 00393 00394 inline ThreeVector GenVertex::point3d() const { 00395 return ThreeVector(m_position.x(),m_position.y(),m_position.z()); 00396 } 00397 00398 inline int GenVertex::id() const { return m_id; } 00399 00400 inline int GenVertex::barcode() const { return m_barcode; } 00401 inline void GenVertex::set_barcode_( int bc ) { m_barcode = bc; } 00402 00403 inline WeightContainer& GenVertex::weights() { return m_weights; } 00404 00405 inline const WeightContainer& GenVertex::weights() const 00406 { return m_weights; } 00407 00408 inline void GenVertex::set_position( const FourVector& position ) { 00409 m_position = position; 00410 } 00411 00412 inline void GenVertex::set_id( int id ) { m_id = id; } 00413 00415 // INLINES // 00417 00418 inline GenVertex::particles_in_const_iterator 00419 GenVertex::particles_in_const_begin() const { 00420 return m_particles_in.begin(); 00421 } 00422 00423 inline GenVertex::particles_in_const_iterator 00424 GenVertex::particles_in_const_end() const { 00425 return m_particles_in.end(); 00426 } 00427 00428 inline GenVertex::particles_out_const_iterator 00429 GenVertex::particles_out_const_begin() const { 00430 return m_particles_out.begin(); 00431 } 00432 00433 inline GenVertex::particles_out_const_iterator 00434 GenVertex::particles_out_const_end() const { 00435 return m_particles_out.end(); 00436 } 00437 00438 inline int GenVertex::particles_in_size() const { 00439 return m_particles_in.size(); 00440 } 00441 00442 inline int GenVertex::particles_out_size() const { 00443 return m_particles_out.size(); 00444 } 00445 00446 inline bool GenVertex::edge_iterator::operator==( 00447 const edge_iterator& a ) const { 00448 return **this == *a; 00449 } 00450 00451 inline bool GenVertex::edge_iterator::operator!=( 00452 const edge_iterator& a ) const { 00453 return !(**this == *a); 00454 } 00455 00456 inline const GenVertex* GenVertex::edge_iterator::vertex_root() const { 00457 return m_vertex; 00458 } 00459 00460 inline GenVertex::edge_iterator GenVertex::edges_begin( IteratorRange 00461 range ) const { 00462 return GenVertex::edge_iterator(*this, range); 00463 } 00464 00465 inline GenVertex::edge_iterator GenVertex::edges_end( IteratorRange 00466 /* dummy_range */ ) const { 00467 return GenVertex::edge_iterator(); 00468 } 00469 00470 inline bool GenVertex::vertex_iterator::operator==( 00471 const vertex_iterator& a ) const { 00472 return **this == *a; 00473 } 00474 00475 inline bool GenVertex::vertex_iterator::operator!=( 00476 const vertex_iterator& a ) const { 00477 return !(**this == *a); 00478 } 00479 00480 inline GenVertex* GenVertex::vertex_iterator::vertex_root() const { 00481 return m_vertex; 00482 } 00483 00484 inline IteratorRange GenVertex::vertex_iterator::range() const { 00485 return m_range; 00486 } 00487 00488 inline GenVertex::vertex_iterator GenVertex::vertices_begin( 00489 IteratorRange range ){ 00490 // this is not const because the it could return itself 00491 return vertex_iterator( *this, range ); 00492 } 00493 00494 inline GenVertex::vertex_iterator GenVertex::vertices_end( 00495 IteratorRange /* dummy_range */ ) { 00496 return vertex_iterator(); 00497 } 00498 00499 inline bool GenVertex::particle_iterator::operator==( 00500 const particle_iterator& a ) const { 00501 return **this == *a; 00502 } 00503 00504 inline bool GenVertex::particle_iterator::operator!=( 00505 const particle_iterator& a ) const { 00506 return !(**this == *a); 00507 } 00508 00509 inline GenVertex::particle_iterator GenVertex::particles_begin( 00510 IteratorRange range ) { 00511 return particle_iterator( *this, range ); 00512 } 00513 00514 inline GenVertex::particle_iterator GenVertex::particles_end( 00515 IteratorRange /* dummy_range */ ){ 00516 return particle_iterator(); 00517 } 00518 00519 } // HepMC 00520 00521 #endif // HEPMC_GEN_VERTEX_H 00522 //-------------------------------------------------------------------------- 00523 00524 00525 00526