hepmc - Blame information for rev 173

Subversion Repositories:
Rev:
Rev Author Line No. Line
2 garren 1 //--------------------------------------------------------------------------
2 #ifndef HEPMC_GEN_VERTEX_H
3 #define HEPMC_GEN_VERTEX_H
4  
5 //////////////////////////////////////////////////////////////////////////
6 // Matt.Dobbs@Cern.CH, September 1999, refer to:
7 // M. Dobbs and J.B. Hansen, "The HepMC C++ Monte Carlo Event Record for
8 // High Energy Physics", Computer Physics Communications (to be published).
9 //
10 // GenVertex within an event
11 // A vertex is indirectly (via particle "edges") linked to other
12 //   vertices ("nodes") to form a composite "graph"
13 //////////////////////////////////////////////////////////////////////////
14  
15 // --> HANDLE COMPILER INCONSISTENCIES
16 // This pre-compiler directive is included (2002-01-16) to allow compatibility
17 // with several compilers.
18 // Mar 27, 2004: HepMC is now standard compliant only.
19 //   I've removed forward_iterator, and it will no longer compile on gcc < 3.
20 #ifdef __SUNPRO_CC    // Solaris CC 5.2
21 #define NEED_SOLARIS_FRIEND_FEATURE
22 #endif // Platform
23  
24 #include "HepMC/WeightContainer.h"
43 garren 25 #include "HepMC/SimpleVector.h"
2 garren 26 #include <iostream>
27 #include <iterator>
28 #include <vector>
29 #include <set>
30 #include <algorithm>
31  
32 namespace HepMC {
33  
65 garren 34     /// type of iteration
2 garren 35     enum IteratorRange { parents, children, family,
36                          ancestors, descendants, relatives };
37     class GenParticle;
38     class GenEvent;
39  
65 garren 40     //! GenVertex contains information about decay vertices.
41  
42     ///
43     /// \class GenVertex
44     /// HepMC::GenVertex contains the position in space and time of a decay.
45     /// It also contains lists of incoming and outgoing particles.  
46     ///
2 garren 47     class GenVertex {
48  
65 garren 49         /// print vertex information
2 garren 50         friend std::ostream& operator<<( std::ostream&, const GenVertex& );
51         friend class GenEvent;
52  
53 #ifdef NEED_SOLARIS_FRIEND_FEATURE
54         // This bit of ugly code is only for CC-5.2 compiler.
55         // M.Dobbs 2002/02/19
56         // It is not needed by linux gcc, nor Windows Visual C++.
57     public:
58         class vertex_iterator;
59         friend class vertex_iterator;
60         class particle_iterator;
61         friend class particle_iterator;
62 #endif // NEED_SOLARIS_FRIEND_FEATURE
63  
64     public:
65 garren 65         /// default constructor
43 garren 66         GenVertex( const FourVector& position =FourVector(0,0,0,0),
2 garren 67                    int id = 0,
68                    const WeightContainer& weights = std::vector<double>() );
65 garren 69         GenVertex( const GenVertex& invertex );            //!< shallow copy
2 garren 70         virtual    ~GenVertex();
71  
147 garren 72         void swap( GenVertex & other); //!< swap
65 garren 73         GenVertex& operator= ( const GenVertex& invertex ); //!< shallow
74         bool       operator==( const GenVertex& a ) const; //!< equality
75         bool       operator!=( const GenVertex& a ) const; //!< inequality
76         void       print( std::ostream& ostr = std::cout ) const; //!< print vertex information
2 garren 77  
65 garren 78         double     check_momentum_conservation() const;//!< |Sum (mom_in-mom_out)|
2 garren 79  
65 garren 80         /// add incoming particle
2 garren 81         void       add_particle_in( GenParticle* inparticle );
65 garren 82         /// add outgoing particle
2 garren 83         void       add_particle_out( GenParticle* outparticle );
65 garren 84         /// remove_particle finds *particle in the in and/or out list and
85         ///  removes it from these lists ... it DOES NOT DELETE THE PARTICLE
86         ///  or its relations. You could delete the particle too as follows:
87         ///      delete vtx->remove_particle( particle );
88         GenParticle* remove_particle( GenParticle* particle ); //!< remove a particle
2 garren 89  
173 garren 90         operator    HepMC::FourVector() const; //!< conversion operator
91         operator   HepMC::ThreeVector() const; //!< conversion operator
2 garren 92  
93         ////////////////////
94         // access methods //
95         ////////////////////
96  
65 garren 97         /// pointer to the event that owns this vertex
2 garren 98         GenEvent*               parent_event() const;
65 garren 99         /// vertex position
43 garren 100         ThreeVector             point3d() const;
65 garren 101         /// vertex position and time
43 garren 102         FourVector              position() const;
65 garren 103         /// set vertex position and time
43 garren 104         void                    set_position( const FourVector& position = FourVector(0,0,0,0) );
65 garren 105         /// we don't define what you use the id for -- but we imagine,
106         /// for example it might code the meaning of the weights()
107         int                     id() const;  //!< vertex ID
108         void                    set_id( int id );  //!< set vertex ID
2 garren 109  
65 garren 110         ///
111         /// The barcode is the vertex's reference number, every vertex in the
112         ///  event has a unique barcode. Vertex barcodes are negative numbers,
113         ///  particle barcodes are positive numbers.
114         int                     barcode() const; //!< unique identifier
115  
116         /// In general there is no reason to "suggest_barcode"
2 garren 117         bool                    suggest_barcode( int the_bar_code );
118  
65 garren 119         /// direct access to the weights container is allowed.
2 garren 120         WeightContainer&        weights();
65 garren 121         /// const direct access to the weights container
2 garren 122         const WeightContainer&  weights() const;
123  
124         ////////////////////
125         // Iterators      // users should use prefer to use particle_iterator
126         ////////////////////    
127  
65 garren 128         /// const iterator for incoming particles
173 garren 129         typedef std::vector<HepMC::GenParticle*>::const_iterator
2 garren 130         particles_in_const_iterator;
65 garren 131         /// const iterator for outgoing particles
173 garren 132         typedef std::vector<HepMC::GenParticle*>::const_iterator
2 garren 133         particles_out_const_iterator;
65 garren 134         /// begin iteration of incoming particles
2 garren 135         particles_in_const_iterator         particles_in_const_begin() const;
65 garren 136         /// end iteration of incoming particles
2 garren 137         particles_in_const_iterator         particles_in_const_end() const;
65 garren 138         /// begin iteration of outgoing particles
2 garren 139         particles_out_const_iterator        particles_out_const_begin() const;
65 garren 140         /// end iteration of outgoing particles
2 garren 141         particles_out_const_iterator        particles_out_const_end() const;
65 garren 142         /// number of incoming particles
2 garren 143         int                                 particles_in_size() const;
65 garren 144         /// number of outgoing particles
2 garren 145         int                                 particles_out_size() const;
146  
147     protected:
65 garren 148         static unsigned int     counter(); //!< temporary for debugging
2 garren 149  
65 garren 150         /// only the GenEvent (friend) is allowed to set the parent_event,
151         ///  and barcode. It is done automatically anytime you add a
152         ///  vertex to an event
153         void                    set_parent_event_( GenEvent* evt ); //!< set parent event
154         void                    set_barcode_( int the_bar_code ); //!< set identifier
2 garren 155  
156         /////////////////////////////
157         // edge_iterator           // (protected - for internal use only)
158         /////////////////////////////
159         // If the user wants the functionality of the edge_iterator, he should
160         // use particle_iterator with IteratorRange = family, parents, children
161         //
65 garren 162  
163         //!  edge iterator
164  
165         /// \class  edge_iterator
166         /// iterate over the family of edges connected to m_vertex begins
167         /// with parents (incoming particles) then children (outgoing)
168         /// This is not a recursive iterator ... it is a building block
169         /// for the public iterators and is intended for internal use only.
170         /// The acceptable Iterator Ranges are: family, parents, children
2 garren 171         class edge_iterator :
173 garren 172           public std::iterator<std::forward_iterator_tag,HepMC::GenParticle*,ptrdiff_t>{
2 garren 173         public:
174             edge_iterator();
65 garren 175             /// used to set limits on the iteration
2 garren 176             edge_iterator( const GenVertex& vtx, IteratorRange range =family );
65 garren 177             /// copy
2 garren 178             edge_iterator( const edge_iterator& p );
179             virtual        ~edge_iterator();
65 garren 180             /// make a copy
2 garren 181             edge_iterator& operator=( const edge_iterator& p );
65 garren 182             /// return a pointer to a particle
2 garren 183             GenParticle*      operator*(void) const;
65 garren 184             /// Pre-fix increment
2 garren 185             edge_iterator& operator++(void); // Pre-fix increment
65 garren 186             /// Post-fix increment
2 garren 187             edge_iterator  operator++(int);   // Post-fix increment
65 garren 188             /// equality
2 garren 189             bool           operator==( const edge_iterator& a ) const;
65 garren 190             /// inequality
2 garren 191             bool           operator!=( const edge_iterator& a ) const;
65 garren 192             /// true if parent of root vtx
193             bool           is_parent() const;
194             /// true if child of root vtx
195             bool           is_child() const;
196             /// root vertex of this iteration
2 garren 197             const GenVertex*  vertex_root() const;
198         private:
199             const GenVertex*  m_vertex;
200             IteratorRange  m_range;
173 garren 201             std::vector<HepMC::GenParticle*>::const_iterator m_set_iter;
2 garren 202             bool           m_is_inparticle_iter;
203             bool           m_is_past_end;
204         };
205         friend class edge_iterator;
65 garren 206         /// size
2 garren 207         int              edges_size( IteratorRange range = family ) const;
65 garren 208         /// begin range
2 garren 209         edge_iterator    edges_begin( IteratorRange range = family) const;
65 garren 210         /// end range
2 garren 211         edge_iterator    edges_end( IteratorRange /* dummy_range */ ) const;
212  
213     public:
214         ///////////////////////////////
215         // vertex_iterator           //
216         ///////////////////////////////
65 garren 217  
218         //!  vertex iterator
219  
220         /// \class  vertex_iterator
221         /// Iterates over all vertices connected via a graph to this vertex.
222         /// this is made friend to that it can access protected edge
223         /// iterator the range can be IteratorRange= ( parents, children,
224         /// family, ancestors, descendants, relatives )
225         /// example for range=descendants the iterator
226         /// will return all vertices
227         /// which are children (connected by an outgoing particle edge),
228         /// grandchildren, great-grandchildren, etc. of this vertex
229         /// In all cases the iterator always returns this vertex
230         /// (returned last).
231         /// The algorithm is accomplished by converting the graph to a tree
232         /// (by "chopping" the edges connecting to an already visited
233         /// vertex) and returning the vertices in POST ORDER traversal.
234         ///
2 garren 235         class vertex_iterator :
173 garren 236           public std::iterator<std::forward_iterator_tag,HepMC::GenVertex*,ptrdiff_t>{
2 garren 237         public:
238             vertex_iterator();
65 garren 239             /// used to set limits on the iteration
2 garren 240             vertex_iterator( GenVertex& vtx_root, IteratorRange range );
65 garren 241             /// next constructor is intended for internal use only
2 garren 242             vertex_iterator( GenVertex& vtx_root, IteratorRange range,
173 garren 243                              std::set<const HepMC::GenVertex*>& visited_vertices );
65 garren 244             /// copy
2 garren 245             vertex_iterator( const vertex_iterator& v_iter );
246             virtual             ~vertex_iterator();
65 garren 247             /// make a copy
2 garren 248             vertex_iterator&    operator=( const vertex_iterator& );
65 garren 249             /// return a pointer to a vertex
2 garren 250             GenVertex*          operator*(void) const;
65 garren 251             /// Pre-fix increment
2 garren 252             vertex_iterator&    operator++(void);  //Pre-fix increment
65 garren 253             /// Post-fix increment
2 garren 254             vertex_iterator     operator++(int);   //Post-fix increment
65 garren 255             /// equality
2 garren 256             bool                operator==( const vertex_iterator& ) const;
65 garren 257             /// inequality
2 garren 258             bool                operator!=( const vertex_iterator& ) const;
65 garren 259             /// vertex that this iterator begins from
2 garren 260             GenVertex*          vertex_root() const;
65 garren 261             /// iterator range
2 garren 262             IteratorRange       range() const;
65 garren 263             /// intended for internal use only.
2 garren 264             void                copy_with_own_set( const vertex_iterator&
265                                                    v_iter,
173 garren 266                                                    std::set<const HepMC::GenVertex*>&
2 garren 267                                                    visited_vertices );
65 garren 268  
2 garren 269         protected:                  // intended for internal use only
65 garren 270             /// non-null if recursive iter. created
271             GenVertex* follow_edge_();
272             /// copy recursive iterator
2 garren 273             void    copy_recursive_iterator_( const vertex_iterator*
274                                               recursive_v_iter );
275         private:
276             GenVertex*       m_vertex;   // the vertex associated to this iter
277             IteratorRange    m_range;
173 garren 278             std::set<const HepMC::GenVertex*>* m_visited_vertices;
2 garren 279             bool             m_it_owns_set;  // true if it is responsible for
280                                              // deleting the visited vertex set
281             edge_iterator    m_edge; // particle edge pointing to return vtx
282             vertex_iterator* m_recursive_iterator;
283         };     
284         friend class vertex_iterator;
65 garren 285         /// begin vertex range
2 garren 286         vertex_iterator     vertices_begin( IteratorRange range = relatives );
65 garren 287         /// end vertex range
2 garren 288         vertex_iterator     vertices_end( IteratorRange /* dummy_range */ );
289  
290     public:
291         ///////////////////////////////
292         // particle_iterator         //
293         ///////////////////////////////
65 garren 294  
295         //!  particle iterator
296  
297         /// \class  particle_iterator
298         /// Iterates over all particles connected via a graph.
299         /// by iterating through all vertices in the m_range. For each
300         /// vertex it returns orphaned parent particles
301         /// (i.e. parents without production vertices)
302         /// then children ... in this way each particle is associated
303         /// to exactly one vertex and so it is returned exactly once.
304         /// Is made friend so that it can access protected edge iterator
305         class particle_iterator :
2 garren 306           public std::iterator<std::forward_iterator_tag,GenParticle*,ptrdiff_t>{
307         public:
308             particle_iterator();
65 garren 309             /// used to set limits on the iteration
2 garren 310             particle_iterator( GenVertex& vertex_root, IteratorRange range );
65 garren 311             /// copy
2 garren 312             particle_iterator( const particle_iterator& );
313             virtual             ~particle_iterator();
65 garren 314             /// make a copy
2 garren 315             particle_iterator&  operator=( const particle_iterator& );
65 garren 316             /// return a pointer to a particle
2 garren 317             GenParticle*        operator*(void) const;
65 garren 318             /// Pre-fix increment
319             particle_iterator&  operator++(void);
320             /// Post-fix increment
321             particle_iterator   operator++(int);
322             /// equality
2 garren 323             bool                operator==( const particle_iterator& ) const;
65 garren 324             /// inequality
2 garren 325             bool                operator!=( const particle_iterator& ) const;
326         protected:
65 garren 327             GenParticle*        advance_to_first_(); //!< "first" particle
2 garren 328         private:
329             vertex_iterator     m_vertex_iterator;
330             edge_iterator       m_edge;     // points to the return
331         };
332         friend class particle_iterator;
65 garren 333         /// begin particle range
2 garren 334         particle_iterator       particles_begin( IteratorRange range
335                                                  = relatives );
65 garren 336         /// end particle range
2 garren 337         particle_iterator       particles_end( IteratorRange
338                                                /* dummy_range */ );
339  
340         ////////////////////////////////////////////////
65 garren 341     protected:
342         /// for internal use only
2 garren 343         void delete_adopted_particles();
173 garren 344         /// for internal use only - remove particle from incoming list
345         void remove_particle_in( GenParticle* );
346         /// for internal use only - remove particle from outgoing list
347         void remove_particle_out( GenParticle* );
2 garren 348  
349     private: // GenVertex data members
46 garren 350         FourVector              m_position;      //4-vec of vertex [mm]
173 garren 351         std::vector<HepMC::GenParticle*>  m_particles_in;  //all incoming particles
352         std::vector<HepMC::GenParticle*>  m_particles_out; //all outgoing particles
2 garren 353         int                  m_id;
354         WeightContainer      m_weights;       // weights for this vtx
355         GenEvent*            m_event;
356         int                  m_barcode;   // unique identifier in the event
357  
358         static unsigned int  s_counter;
359     };  
360  
361     ////////////////////////////
362     // INLINES access methods //
363     ////////////////////////////
364  
173 garren 365     inline GenVertex::operator HepMC::FourVector() const { return position(); }
2 garren 366  
173 garren 367     inline GenVertex::operator HepMC::ThreeVector() const { return point3d(); }
2 garren 368  
43 garren 369     inline FourVector GenVertex::position() const { return m_position; }
2 garren 370  
371     inline GenEvent* GenVertex::parent_event() const { return m_event; }
372  
43 garren 373     inline ThreeVector GenVertex::point3d() const {
374         return ThreeVector(m_position.x(),m_position.y(),m_position.z());
2 garren 375     }
376  
377     inline int GenVertex::id() const { return m_id; }
378  
379     inline int  GenVertex::barcode() const { return m_barcode; }
380     inline void GenVertex::set_barcode_( int bc ) { m_barcode = bc; }
381  
382     inline WeightContainer& GenVertex::weights() { return m_weights; }
383  
384     inline const WeightContainer& GenVertex::weights() const
385     { return m_weights; }
386  
43 garren 387     inline void GenVertex::set_position( const FourVector& position ) {
2 garren 388         m_position = position;
389     }
390  
391     inline void GenVertex::set_id( int id ) { m_id = id; }
392  
393     //////////////
394     // INLINES  //
395     //////////////
396  
397     inline GenVertex::particles_in_const_iterator
398     GenVertex::particles_in_const_begin() const {
399         return m_particles_in.begin();
400     }
401  
402     inline GenVertex::particles_in_const_iterator
403     GenVertex::particles_in_const_end() const {
404         return m_particles_in.end();
405     }
406  
407     inline GenVertex::particles_out_const_iterator
408     GenVertex::particles_out_const_begin() const {
409         return m_particles_out.begin();
410     }
411  
412     inline GenVertex::particles_out_const_iterator
413     GenVertex::particles_out_const_end() const {       
414         return m_particles_out.end();
415     }
416  
417     inline int GenVertex::particles_in_size() const {
418         return m_particles_in.size();
419     }
420  
421     inline int GenVertex::particles_out_size() const {
422         return m_particles_out.size();
423     }  
424  
425     inline bool GenVertex::edge_iterator::operator==(
426         const edge_iterator& a ) const {
427         return **this == *a;
428     }
429  
430     inline bool GenVertex::edge_iterator::operator!=(
431         const edge_iterator& a ) const {
432         return !(**this == *a);
433     }
434  
435     inline const GenVertex* GenVertex::edge_iterator::vertex_root() const {
436         return m_vertex;
437     }
438  
439     inline GenVertex::edge_iterator GenVertex::edges_begin( IteratorRange
440                                                       range ) const {
441         return GenVertex::edge_iterator(*this, range);
442     }
443  
444     inline GenVertex::edge_iterator GenVertex::edges_end( IteratorRange
445                                                     /* dummy_range */ ) const {
446         return GenVertex::edge_iterator();
447     }
448  
449     inline bool GenVertex::vertex_iterator::operator==(
450         const vertex_iterator& a ) const {
451         return **this == *a;
452     }
453  
454     inline bool GenVertex::vertex_iterator::operator!=(
455         const vertex_iterator& a ) const {
456         return !(**this == *a);
457     }
458  
459     inline GenVertex* GenVertex::vertex_iterator::vertex_root() const {
460         return m_vertex;
461     }
462  
463     inline IteratorRange GenVertex::vertex_iterator::range() const {
464         return m_range;
465     }
466  
467     inline GenVertex::vertex_iterator GenVertex::vertices_begin(
468         IteratorRange range ){
469         // this is not const because the it could return itself
470         return vertex_iterator( *this, range );
471     }
472  
473     inline GenVertex::vertex_iterator GenVertex::vertices_end(
474         IteratorRange /* dummy_range */ ) {
475         return vertex_iterator();
476     }
477  
478     inline bool GenVertex::particle_iterator::operator==(
479         const particle_iterator& a ) const {
480         return **this == *a;
481     }
482  
483     inline bool GenVertex::particle_iterator::operator!=(
484         const particle_iterator& a ) const {
485         return !(**this == *a);
486     }
487  
488     inline GenVertex::particle_iterator GenVertex::particles_begin(
489         IteratorRange range ) {
490         return particle_iterator( *this, range );
491     }
492  
493     inline GenVertex::particle_iterator GenVertex::particles_end(
494         IteratorRange /* dummy_range */ ){
495         return particle_iterator();
496     }
497  
498 } // HepMC
499  
500 #endif  // HEPMC_GEN_VERTEX_H
501 //--------------------------------------------------------------------------
502  
503  
504  
505