hepmc - Blame information for rev 69

Subversion Repositories:
Rev:
Rev Author Line No. Line
2 garren 1 //--------------------------------------------------------------------------
2 #ifndef HEPMC_PARTICLE_DATA_TABLE_H
3 #define HEPMC_PARTICLE_DATA_TABLE_H
4  
5 //////////////////////////////////////////////////////////////////////////
6 // Matt.Dobbs@Cern.CH, Jan 2000, 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 // Container for GenParticle Data Instances --- basically just an interface
11 //   to STL map -- the same naming conventions are used
12 // A GenParticle may belong to any number of ParticleDataTables.
13 // The ParticleDataTable does not own the ParticleData objects and will NOT
14 //   delete them unless explicity told to do so with the delete_all method.
15 // Each ParticleData entry in the table MUST have a unique pdg_id (otherwise
16 //   an attempt to insert it as a new entry will fail).
17 // Updated 2000.02.08 M.Dobbs added merge_table and
18 //                            make_antiparticles_from_particles
19 //////////////////////////////////////////////////////////////////////////
20  
21 #include <iostream>
22 #include <map>
23 #include <cstdio>       // needed for formatted output using sprintf
24 #include "HepMC/ParticleData.h"
25  
26 namespace HepMC {
27  
65 garren 28     //! an example ParticleDataTable class
29  
30     ///
31     /// \class ParticleDataTable
32     /// Example container for ParticleData instances.
33     /// Basically just an interface to STL map.
34     ///
2 garren 35     class ParticleDataTable {
36  
37     public:
65 garren 38         /// constructor with optional description
2 garren 39         ParticleDataTable( std::string description = std::string() );
65 garren 40         /// constructor with description
2 garren 41         ParticleDataTable( const char description );
65 garren 42         /// copy constructor
2 garren 43         ParticleDataTable( const ParticleDataTable& );
65 garren 44         /// Shallow: does not delete ParticleData entries
45         virtual ~ParticleDataTable();
46         /// shallow: does not copy the entries, only makes new pointers
2 garren 47         ParticleDataTable& operator=( const ParticleDataTable& );
48  
69 garren 49         /// make corresponding anti-particles for all particles in table
2 garren 50         void    make_antiparticles_from_particles();
69 garren 51         /// merge two tables
2 garren 52         int     merge_table( const ParticleDataTable& );
53  
69 garren 54         /// write the table to ostr
2 garren 55         void    print( std::ostream& ostr = std::cout ) const;
56  
65 garren 57         void    delete_all(); //!<delete all ParticleData instances in this table
58         void    clear();      //!<clears table without deleting
2 garren 59  
65 garren 60         /// return pointer to requested ParticleData
2 garren 61         ParticleData*       operator[]( int id ) const;
65 garren 62         /// return pointer to requested ParticleData
2 garren 63         ParticleData*       find( int id ) const;
65 garren 64         /// size of table
2 garren 65         int                 size() const;
65 garren 66         /// true if the table is empty
2 garren 67         bool                empty() const;
65 garren 68         /// true if successful
69         bool                insert( ParticleData* );
70         /// removes from table - does not delete
71         bool                erase( ParticleData* );  
72         /// removes from table - does not delete
73         bool                erase( int id );      
74         /// iterator for ParticleData map
2 garren 75         typedef std::map<int,ParticleData*>::iterator iterator;
65 garren 76         /// const iterator for ParticleData map
2 garren 77         typedef std::map<int,ParticleData*>::const_iterator const_iterator;
65 garren 78         /// begin iteration
2 garren 79         iterator            begin();
65 garren 80         /// end iteration
2 garren 81         iterator            end();
65 garren 82         /// begin const iteration
2 garren 83         const_iterator      begin() const;
65 garren 84         /// end const iteration
2 garren 85         const_iterator      end() const;
86  
87         ////////////////////
88         // access methods //
89         ////////////////////
65 garren 90  
91         /// table description
2 garren 92         std::string description() const;
65 garren 93         /// set table description
2 garren 94         void        set_description( std::string );
65 garren 95         /// set table description
2 garren 96         void        set_description( const char );
97  
98     private:
99         std::string                 m_description;
100         std::map<int,ParticleData*> m_data_table;
101     };
102  
103     ///////////////////////////
104     // INLINES               //
105     ///////////////////////////
106  
107     inline ParticleDataTable::ParticleDataTable( std::string description )
108         : m_description(description) {}
109  
110     inline ParticleDataTable::ParticleDataTable( const char description ) {
111         m_description = description;
112     }
113  
114     inline ParticleDataTable::ParticleDataTable( const ParticleDataTable& pdt){
115         *this = pdt;
116     }
117  
118     inline ParticleDataTable::~ParticleDataTable(){}
119  
120     inline ParticleDataTable& ParticleDataTable::operator=( const
121                                                             ParticleDataTable&
122                                                             pdt) {
123         m_description = pdt.m_description;
124         m_data_table = pdt.m_data_table;
125         return *this;
126     }
127  
128     inline void ParticleDataTable::make_antiparticles_from_particles() {
65 garren 129         /// make corresponding anti-particles for all particles in table
2 garren 130         ParticleDataTable new_data;
131         for ( ParticleDataTable::iterator p = begin(); p != end(); ++p ) {
132             ParticleData* pdata = p->second;
133             if ( pdata->charge() ) {
134                 new_data.insert( new ParticleData( pdata->name()+"~",
135                                                    -1*pdata->pdg_id(),
136                                                    -1.*pdata->charge(),
137                                                    pdata->mass(),
138                                                    pdata->clifetime(),
139                                                    pdata->spin() ));
140             }
141         }
142         merge_table( new_data );
143     }
144  
145     inline void ParticleDataTable::print( std::ostream& ostr ) const {
65 garren 146         /// prints a summary of all particle Data currently in memory
2 garren 147         //
148         ostr << "________________________________________"
149              << "________________________________________\n";
150         ostr << "ParticleData:   *****  ParticleDataTable"
151              << "  *****   ( " << size()
152              << " entries )\n";
153         ostr << " Description: " << m_description << "\n";
154         ostr << "   PDG ID " << "       PARTICLE NAME "
155              << "CHARGE" <<    "     MASS     "
156              << "  C*LIFETIME (CM) " << " SPIN\n";
157         for ( std::map< int,ParticleData* >::const_iterator pd
158                   = m_data_table.begin(); pd != m_data_table.end(); pd++ ) {
159             ostr << *(pd->second) << "\n";
160         }
161         ostr << "________________________________________"
162              << "________________________________________" << std::endl;
163     }      
164  
165     inline ParticleData* ParticleDataTable::find( int id ) const {
65 garren 166         /// finds a ParticleData pointer corresponding to id IF it exists in
167         ///  the table. If not returns NULL
2 garren 168         std::map<int,ParticleData*>::const_iterator iter
169             = m_data_table.find(id);
170         return ( iter == m_data_table.end() ) ? 0 : iter->second;
171     }
172  
173     inline ParticleData* ParticleDataTable::operator[]( int id ) const {
174         return find(id);
175     }
176  
177     inline int ParticleDataTable::size() const {
178         return (int)m_data_table.size();
179     }
180  
181     inline bool ParticleDataTable::empty() const {
182         return (bool)m_data_table.empty();
183     }
184  
185     inline bool ParticleDataTable::insert( ParticleData* pdata ) {
65 garren 186         /// inserts pdata in the table IFF pdata's id has not already been used.
187         /// It does NOT replace entries with the same id. True if successful.
188         /// If you wish to overwrite another entry, first use erase()
2 garren 189         if ( m_data_table.count(pdata->pdg_id()) ) return 0;
190         return ( m_data_table[pdata->pdg_id()] = pdata ); // true is success
191     }
192  
193     inline bool ParticleDataTable::erase( ParticleData* pdata ) {
65 garren 194         /// removes from table does not delete
195         /// returns True is an entry pdata existed in the table and was erased
2 garren 196         return (bool)m_data_table.erase( pdata->pdg_id() );
197     }
198  
199  
200     inline bool ParticleDataTable::erase( int id ) {
65 garren 201         /// removes from table does not delete
202         /// returns True is an entry pdata existed in the table and was erased
2 garren 203         return (bool)m_data_table.erase( id );
204     }
205  
206     inline ParticleDataTable::iterator ParticleDataTable::begin() {
207         return m_data_table.begin();
208     }
209  
210     inline ParticleDataTable::iterator ParticleDataTable::end() {
211         return m_data_table.end();
212     }
213  
214     inline ParticleDataTable::const_iterator ParticleDataTable::begin() const {
215         return m_data_table.begin();
216     }
217  
218     inline ParticleDataTable::const_iterator ParticleDataTable::end() const {
219         return m_data_table.end();
220     }
221  
222     inline std::string ParticleDataTable::description() const {
223         return m_description;
224     }
225  
226     inline void ParticleDataTable::set_description( std::string description ) {
227         m_description = description;
228     }
229  
230     inline void ParticleDataTable::set_description( const char description ) {
231         m_description = description;
232     }
233  
234     inline void ParticleDataTable::delete_all() {
65 garren 235         /// deletes all ParticleData instances in this table
2 garren 236         for ( std::map<int,ParticleData*>::iterator pd = m_data_table.begin();
237               pd != m_data_table.end(); pd++) delete pd->second;
238         clear();
239     }
240  
241     inline void ParticleDataTable::clear() { m_data_table.clear(); }
242  
243     inline int ParticleDataTable::merge_table( const ParticleDataTable& pdt ) {
65 garren 244         /// merges pdt into this table
245         /// each entry from pdt is inserted only if this table does not
246         ///  already have an entry matching the ParticleData's id
247         /// returns the number of new entries inserted into this table.
2 garren 248         int count_number_insertions =0;
249         for ( ParticleDataTable::const_iterator p = pdt.begin();
250               p != pdt.end(); ++p ) {
251             if ( insert(p->second) ) ++count_number_insertions;
252         }
253         return count_number_insertions;
254     }
255  
256 } // HepMC
257  
258 #endif  // HEPMC_PARTICLE_DATA_TABLE_H
259 //--------------------------------------------------------------------------
260  
261  
262