Fork me on GitHub

source: svn/trunk/Utilities/HepMC/interface/ParticleDataTable.h@ 491

Last change on this file since 491 was 349, checked in by severine ovyn, 16 years ago

first test

File size: 9.5 KB
Line 
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 "ParticleData.h"
25
26namespace HepMC {
27
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 ///
35 class ParticleDataTable {
36
37 public:
38 /// constructor with optional description
39 ParticleDataTable( std::string description = std::string() );
40 /// constructor with description
41 ParticleDataTable( const char description );
42 /// copy constructor
43 ParticleDataTable( const ParticleDataTable& );
44 /// Shallow: does not delete ParticleData entries
45 virtual ~ParticleDataTable();
46 /// shallow: does not copy the entries, only makes new pointers
47 ParticleDataTable& operator=( const ParticleDataTable& );
48
49 /// make corresponding anti-particles for all particles in table
50 void make_antiparticles_from_particles();
51 /// merge two tables
52 int merge_table( const ParticleDataTable& );
53
54 /// write the table to ostr
55 void print( std::ostream& ostr = std::cout ) const;
56
57 void delete_all(); //!<delete all ParticleData instances in this table
58 void clear(); //!<clears table without deleting
59
60 /// return pointer to requested ParticleData
61 ParticleData* operator[]( int id ) const;
62 /// return pointer to requested ParticleData
63 ParticleData* find( int id ) const;
64 /// size of table
65 int size() const;
66 /// true if the table is empty
67 bool empty() const;
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
75 typedef std::map<int,HepMC::ParticleData*>::iterator iterator;
76 /// const iterator for ParticleData map
77 typedef std::map<int,HepMC::ParticleData*>::const_iterator const_iterator;
78 /// begin iteration
79 iterator begin();
80 /// end iteration
81 iterator end();
82 /// begin const iteration
83 const_iterator begin() const;
84 /// end const iteration
85 const_iterator end() const;
86
87 ////////////////////
88 // access methods //
89 ////////////////////
90
91 /// table description
92 std::string description() const;
93 /// set table description
94 void set_description( std::string );
95 /// set table description
96 void set_description( const char );
97
98 private:
99 std::string m_description;
100 std::map<int,HepMC::ParticleData*> m_data_table;
101 };
102
103 ///////////////////////////
104 // INLINES //
105 ///////////////////////////
106
107 inline ParticleDataTable::ParticleDataTable( std::string description )
108 : m_description(description) {
109 std::cout << "-------------------------------------------------------" << std::endl;
110 std::cout << "Use of HepMC/ParticleDataTable is deprecated" << std::endl;
111 std::cout << "-------------------------------------------------------" << std::endl;
112 }
113
114 inline ParticleDataTable::ParticleDataTable( const char description ) {
115 m_description = description;
116
117 std::cout << "-------------------------------------------------------" << std::endl;
118 std::cout << "Use of HepMC/ParticleDataTable is deprecated" << std::endl;
119 std::cout << "-------------------------------------------------------" << std::endl;
120 }
121
122 inline ParticleDataTable::ParticleDataTable( const ParticleDataTable& pdt){
123 *this = pdt;
124 }
125
126 inline ParticleDataTable::~ParticleDataTable(){}
127
128 inline ParticleDataTable& ParticleDataTable::operator=( const
129 ParticleDataTable&
130 pdt) {
131 m_description = pdt.m_description;
132 m_data_table = pdt.m_data_table;
133 return *this;
134 }
135
136 inline void ParticleDataTable::make_antiparticles_from_particles() {
137 /// make corresponding anti-particles for all particles in table
138 ParticleDataTable new_data;
139 for ( ParticleDataTable::iterator p = begin(); p != end(); ++p ) {
140 ParticleData* pdata = p->second;
141 if ( pdata->charge() ) {
142 new_data.insert( new ParticleData( pdata->name()+"~",
143 -1*pdata->pdg_id(),
144 -1.*pdata->charge(),
145 pdata->mass(),
146 pdata->clifetime(),
147 pdata->spin() ));
148 }
149 }
150 merge_table( new_data );
151 }
152
153 inline void ParticleDataTable::print( std::ostream& ostr ) const {
154 /// prints a summary of all particle Data currently in memory
155 //
156 ostr << "________________________________________"
157 << "________________________________________\n";
158 ostr << "ParticleData: ***** ParticleDataTable"
159 << " ***** ( " << size()
160 << " entries )\n";
161 ostr << " Description: " << m_description << "\n";
162 ostr << " PDG ID " << " PARTICLE NAME "
163 << "CHARGE" << " MASS "
164 << " C*LIFETIME (CM) " << " SPIN\n";
165 for ( std::map< int,ParticleData* >::const_iterator pd
166 = m_data_table.begin(); pd != m_data_table.end(); pd++ ) {
167 ostr << *(pd->second) << "\n";
168 }
169 ostr << "________________________________________"
170 << "________________________________________" << std::endl;
171 }
172
173 inline ParticleData* ParticleDataTable::find( int id ) const {
174 /// finds a ParticleData pointer corresponding to id IF it exists in
175 /// the table. If not returns NULL
176 std::map<int,ParticleData*>::const_iterator iter
177 = m_data_table.find(id);
178 return ( iter == m_data_table.end() ) ? 0 : iter->second;
179 }
180
181 inline ParticleData* ParticleDataTable::operator[]( int id ) const {
182 return find(id);
183 }
184
185 inline int ParticleDataTable::size() const {
186 return (int)m_data_table.size();
187 }
188
189 inline bool ParticleDataTable::empty() const {
190 return (bool)m_data_table.empty();
191 }
192
193 inline bool ParticleDataTable::insert( ParticleData* pdata ) {
194 /// inserts pdata in the table IFF pdata's id has not already been used.
195 /// It does NOT replace entries with the same id. True if successful.
196 /// If you wish to overwrite another entry, first use erase()
197 if ( m_data_table.count(pdata->pdg_id()) ) return 0;
198 return ( m_data_table[pdata->pdg_id()] = pdata ); // true is success
199 }
200
201 inline bool ParticleDataTable::erase( ParticleData* pdata ) {
202 /// removes from table does not delete
203 /// returns True is an entry pdata existed in the table and was erased
204 return (bool)m_data_table.erase( pdata->pdg_id() );
205 }
206
207
208 inline bool ParticleDataTable::erase( int id ) {
209 /// removes from table does not delete
210 /// returns True is an entry pdata existed in the table and was erased
211 return (bool)m_data_table.erase( id );
212 }
213
214 inline ParticleDataTable::iterator ParticleDataTable::begin() {
215 return m_data_table.begin();
216 }
217
218 inline ParticleDataTable::iterator ParticleDataTable::end() {
219 return m_data_table.end();
220 }
221
222 inline ParticleDataTable::const_iterator ParticleDataTable::begin() const {
223 return m_data_table.begin();
224 }
225
226 inline ParticleDataTable::const_iterator ParticleDataTable::end() const {
227 return m_data_table.end();
228 }
229
230 inline std::string ParticleDataTable::description() const {
231 return m_description;
232 }
233
234 inline void ParticleDataTable::set_description( std::string description ) {
235 m_description = description;
236 }
237
238 inline void ParticleDataTable::set_description( const char description ) {
239 m_description = description;
240 }
241
242 inline void ParticleDataTable::delete_all() {
243 /// deletes all ParticleData instances in this table
244 for ( std::map<int,ParticleData*>::iterator pd = m_data_table.begin();
245 pd != m_data_table.end(); pd++) delete pd->second;
246 clear();
247 }
248
249 inline void ParticleDataTable::clear() { m_data_table.clear(); }
250
251 inline int ParticleDataTable::merge_table( const ParticleDataTable& pdt ) {
252 /// merges pdt into this table
253 /// each entry from pdt is inserted only if this table does not
254 /// already have an entry matching the ParticleData's id
255 /// returns the number of new entries inserted into this table.
256 int count_number_insertions =0;
257 for ( ParticleDataTable::const_iterator p = pdt.begin();
258 p != pdt.end(); ++p ) {
259 if ( insert(p->second) ) ++count_number_insertions;
260 }
261 return count_number_insertions;
262 }
263
264} // HepMC
265
266#endif // HEPMC_PARTICLE_DATA_TABLE_H
267//--------------------------------------------------------------------------
268
269
270
Note: See TracBrowser for help on using the repository browser.