Fork me on GitHub

source: svn/trunk/Utilities/HepMC/src/IO_PDG_ParticleDataTable.cc@ 553

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

first test

File size: 6.7 KB
Line 
1//--------------------------------------------------------------------------
2
3//////////////////////////////////////////////////////////////////////////
4// Matt.Dobbs@Cern.CH, January 2000
5// Reads a particle data table from file supplied in PDG format
6// For the most recent table see: http://pdg.lbl.gov/computer_read.html
7//////////////////////////////////////////////////////////////////////////
8
9#include <ctype.h> // for isspace() etc.
10#include <string>
11#include <vector>
12#include <cstdlib> // needed for abort()
13#include <cstring> // for strlen()
14#include "IO_PDG_ParticleDataTable.h"
15
16namespace HepMC {
17
18 IO_PDG_ParticleDataTable::IO_PDG_ParticleDataTable( const char* filename )
19 : m_filename(filename), m_file(filename) {
20 std::cout << "-------------------------------------------------------" << std::endl;
21 std::cout << "Use of HepMC/IO_PDG_ParticleDataTable is deprecated" << std::endl;
22 std::cout << "Use of HepMC/ParticleDataTable is deprecated" << std::endl;
23 std::cout << "-------------------------------------------------------" << std::endl;
24 }
25
26 IO_PDG_ParticleDataTable::~IO_PDG_ParticleDataTable() {
27 m_file.close();
28 }
29
30 bool IO_PDG_ParticleDataTable::fill_particle_data_table(
31 ParticleDataTable* pdt)
32 {
33 //
34 // test that pdt pointer is not null
35 if ( !pdt ) {
36 std::cerr
37 << "IO_Ascii::fill_particle_data_table - passed null table."
38 << std::endl;
39 return false;
40 }
41 //
42 // test the file is good and advance to keyword
43 if (!m_file ) {
44 // return false;
45 std::cerr << "IO_PDG_ParticleDataTable::fill_particle_data_table "
46 << " ERROR, file " << m_filename << " does not exist, "
47 << " ABORT." << std::endl;
48 abort();
49 }
50 m_file.seekg( 0 );
51 if ( !search_for_key_end(m_file,"\nM") ) {
52 std::cerr << "IO_PDG_ParticleDataTable: "
53 << "Error advancing to first entry."
54 << std::endl;
55 return false;
56 }
57 // set description
58 std::string description = "Read from " + m_filename
59 + " by IO_PDG_ParticleDataTable";
60 pdt->set_description( description );
61 m_file.putback('M');
62 // read in each entry one by one
63 while ( m_file.rdstate() == 0 ) {
64 read_entry( pdt );
65 }
66 return true;
67 }
68
69 void IO_PDG_ParticleDataTable::read_entry( ParticleDataTable* pdt ) {
70 int position = m_file.tellg();
71 // variables for input
72 char code;
73 int id[4] = { 0,0,0,0 };
74 int charge[4] = { 999, 999, 999, 999 };
75 double value;
76 std::string name;
77
78 code = m_file.get();
79 if ( code == EOF ) {
80 m_file.clear( std::ios::eofbit );
81 return;
82 }
83
84 if ( code == '\n' ) return; // ignore empty lines.
85
86 if ( code!='M' && code!='W' ) {
87 std::cerr << "IO_PDG_ParticleDataTable::read_entry cannot "
88 << "understand syntax, setting badbit."
89 << std::endl;
90 m_file.putback( code );
91 m_file.clear( std::ios::badbit );
92 return;
93 }
94 // read the particle ID's -- here I assume there is atleast one
95 // white space b/t the 4 id columns.
96 for ( int id_i1 = 0; id_i1 < 4; ++id_i1 ) {
97 m_file.seekg( position+1+(id_i1*8) );
98 for ( int i = 0; i < 8; ++i ) {
99 if ( !isspace(m_file.peek()) ) {
100 //m_file.width(8-i); // has no effect
101 // how do I limit to reading only 8-i characters?
102 // setw(8-i) has no effect either.
103 m_file >> id[id_i1];
104 break;
105 }
106 m_file.ignore();
107 }
108 }
109 //m_file.width(0);
110 m_file.seekg(position+33);
111 m_file >> value;
112 m_file.seekg(position+66);
113 m_file >> name;
114 // loop over all remaining columns of the name section, where the
115 // charge is stored --- until \n is reached
116 // Here I assume there is atleast one white space between the name
117 // and the charge. This is true for the 1998 PDG table, and I expect
118 // it will always be true.
119 int id_i2 = 0;
120 while ( m_file.rdstate() == 0 ) {
121 char c = m_file.get();
122 if ( c == '\n' || c == EOF ) break;
123 if ( isspace(c) || c==',' ) {
124 } else if ( c=='-' ) {
125 charge[id_i2] = -1;
126 ++id_i2;
127 } else if ( c=='0' ) {
128 charge[id_i2] = 0;
129 ++id_i2;
130 } else if ( c=='+' ) {
131 charge[id_i2] = 1;
132 if ( m_file.peek() == '+' ) {
133 charge[id_i2] = 2;
134 m_file.ignore();
135 }
136 ++id_i2;
137 }
138 }
139 // add the new information to the particle data table.
140 for ( int id_i3 = 0; id_i3 < 4; ++id_i3 ) {
141 if ( id[id_i3] == 0 ) continue;
142 std::string scharge;
143 if ( charge[id_i3] == -1 ) {
144 scharge = "-";
145 } else if ( charge[id_i3] == 1 ) {
146 scharge = "+";
147 } else if ( charge[id_i3] == 2 ) {
148 scharge = "++";
149 }
150 ParticleData* pdata = pdt->find( id[id_i3] );
151 if ( pdata ) {
152 if ( code == 'M' ) {
153 pdata->set_mass( value );
154 } else if ( code == 'W' ) {
155 pdata->set_clifetime( clifetime_from_width(value) );
156 }
157 } else if ( code == 'M' ) {
158 pdata = new ParticleData( name+scharge,
159 id[id_i3], (double)charge[id_i3],
160 value );
161 pdt->insert(pdata);
162 } else if ( code == 'W' ) {
163 pdata = new ParticleData( name+scharge,
164 id[id_i3], (double)charge[id_i3],
165 0, clifetime_from_width(value) );
166 pdt->insert(pdata);
167 }
168 }
169 }
170
171 void IO_PDG_ParticleDataTable::add_quarks_to_table(
172 ParticleDataTable& pdt ) {
173 /// since quarks aren't included in PDG table, this method adds them
174 // in
175 std::vector<std::string> name(6);
176 name[0] = "d";
177 name[1] = "u";
178 name[2] = "s";
179 name[3] = "c";
180 name[4] = "b";
181 name[5] = "t";
182 int id[6] = { 1, 2, 3, 4, 5, 6 };
183 double charge[6] = { -1./3., 2./3., -1./3., 2./3., -1./3., 2./3. };
184 // take central values from PDG July 1998 for masses
185 double mass[6] = { 0.006, 0.00325, 0.115, 1.25, 4.25, 173.8 };
186 for ( int i = 0; i<6; ++i ) {
187 // build the particle
188 ParticleData* pdata = pdt.find(id[i]);
189 if ( pdata ) {
190 mass[i] = pdata->mass();
191 pdt.erase(pdata);
192 delete pdata;
193 }
194 pdata = new ParticleData( name[i], id[i], charge[i], mass[i], -1.,
195 .5 );
196 pdt.insert(pdata);
197 // build the antiparticle
198 pdata = pdt.find(-1*id[i]);
199 if ( pdata ) {
200 pdt.erase(pdata);
201 delete pdata;
202 }
203 pdata = new ParticleData( name[i]+"~", -1*id[i], -1*charge[i],
204 mass[i], -1., .5 );
205 pdt.insert(pdata);
206 }
207 }
208
209 bool IO_PDG_ParticleDataTable::search_for_key_end( std::istream& in,
210 const char* key ) {
211 /// (this method borrowed from IO_Ascii class)
212 /// reads characters from in until the string of characters matching
213 /// key is found (success) or EOF is reached (failure).
214 /// It stops immediately thereafter. Returns T/F for success/fail
215 //
216 char c[1];
217 unsigned int index = 0;
218 while ( in.get(c[0]) ) {
219 if ( c[0] == key[index] ) {
220 ++index;
221 } else { index = 0; }
222 if ( index == strlen(key) ) return true;
223 }
224 return false;
225 }
226
227} // HepMC
228
229
230
231
232
233
234
235
236
Note: See TracBrowser for help on using the repository browser.