SSO Logout

Subversion Repositories hepmc

Rev

Rev 120 | Rev 161 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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