source: trunk/source/particles/management/src/G4ParticleTable.cc@ 834

Last change on this file since 834 was 824, checked in by garnier, 17 years ago

import all except CVS

File size: 12.0 KB
RevLine 
[824]1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27// $Id: G4ParticleTable.cc,v 1.29.2.1 2008/04/25 12:21:52 kurasige Exp $
28// GEANT4 tag $Name: geant4-09-01-patch-02 $
29//
30// class G4ParticleTable
31//
32// Implementation
33//
34// History:
35// modified Apr., 97 H.Kurashige
36// added fParticleMessenger 14 Nov., 97 H.Kurashige
37// added GetParticle() 13 Dec., 97 H.Kurashige
38// added IonTable and ShortLivedTable 27 June, 98 H.Kurashige
39// modified FindIon 02 Aug., 98 H.Kurashige
40// added dictionary for encoding 24 Sep., 98 H.Kurashige
41// fixed bugs in destruction of IonTable 08 Nov.,98 H.Kurashige
42// commented out G4cout/G4cout in the constructor 10 Nov.,98 H.Kurashige
43// --------------------------------
44// modified destructor for STL interface 18 May 1999
45// fixed some improper codings 08 Apr., 99 H.Kurashige
46// modified FindIon/GetIon methods 17 AUg., 99 H.Kurashige
47// implement new version for using STL map instaed of
48// RW PtrHashedDictionary 28 ct., 99 H.Kurashige
49
50
51#include "G4ios.hh"
52#include "globals.hh"
53#include "G4ParticleTable.hh"
54#include "G4UImessenger.hh"
55#include "G4ParticleMessenger.hh"
56#include "G4IonTable.hh"
57#include "G4ShortLivedTable.hh"
58
59////////////////////
60G4ParticleTable::G4ParticleTable()
61 :verboseLevel(0),fParticleMessenger(0),
62 noName(" "),
63 readyToUse(false)
64{
65 fDictionary = new G4PTblDictionary();
66 fIterator = new G4PTblDicIterator( *fDictionary );
67 fEncodingDictionary = new G4PTblEncodingDictionary();
68
69 // Ion Table
70 fIonTable = new G4IonTable();
71
72 // short lived table
73 fShortLivedTable = new G4ShortLivedTable();
74}
75
76
77////////////////////
78G4ParticleTable::~G4ParticleTable()
79{
80
81 // remove all items from G4ParticleTable
82 RemoveAllParticles();
83
84 // delete Short Lived table
85 if (fShortLivedTable!=0) delete fShortLivedTable;
86 fShortLivedTable =0;
87
88
89 //delete Ion Table
90 if (fIonTable!=0) delete fIonTable;
91 fIonTable =0;
92
93 // delete dictionary for encoding
94 if (fEncodingDictionary!=0){
95 fEncodingDictionary -> clear();
96 delete fEncodingDictionary;
97 fEncodingDictionary =0;
98 }
99
100 if(fDictionary){
101 if (fIterator!=0 )delete fIterator;
102 fIterator =0;
103
104 fDictionary->clear();
105 delete fDictionary;
106 fDictionary =0;
107 }
108
109 if (fParticleMessenger!=0) delete fParticleMessenger;
110 fParticleMessenger =0;
111
112 fgParticleTable =0;
113
114}
115
116////////////////////
117G4ParticleTable::G4ParticleTable(const G4ParticleTable &right)
118{
119 G4Exception("G4ParticleTable::G4ParticleTable()",
120 "illegal constructor call", JustWarning,
121 "you call copy constructor of G4ParticleTable");
122 fDictionary = new G4PTblDictionary(*(right.fDictionary));
123 fIterator = new G4PTblDicIterator(*fDictionary);
124}
125
126
127
128// Static class variable: ptr to single instance of class
129G4ParticleTable* G4ParticleTable::fgParticleTable =0;
130
131////////////////////
132G4ParticleTable* G4ParticleTable::GetParticleTable()
133{
134 static G4ParticleTable theParticleTable;
135 if (!fgParticleTable){
136 fgParticleTable = &theParticleTable;
137 }
138 return fgParticleTable;
139}
140
141////////////////////
142G4UImessenger* G4ParticleTable::CreateMessenger()
143{
144 if (fParticleMessenger== 0) {
145 //UI messenger
146 fParticleMessenger = new G4ParticleMessenger(this);
147 }
148 return fParticleMessenger;
149}
150
151////////////////////
152void G4ParticleTable::DeleteMessenger()
153{
154 if (fParticleMessenger!= 0) {
155 //UI messenger
156 delete fParticleMessenger;
157 fParticleMessenger= 0;
158 }
159
160}
161
162////////////////////
163void G4ParticleTable::DeleteAllParticles()
164{
165
166#ifdef G4VERBOSE
167 if (verboseLevel>1){
168 G4cout << "G4ParticleTable::DeleteAllParticles() " << G4endl;
169 }
170#endif
171
172 // delete all particles
173 G4PTblDicIterator *piter = fIterator;
174 piter -> reset();
175 while( (*piter)() ){
176 delete (piter->value());
177 }
178
179 RemoveAllParticles();
180}
181
182////////////////////
183void G4ParticleTable::RemoveAllParticles()
184{
185
186#ifdef G4VERBOSE
187 if (verboseLevel>1){
188 G4cout << "G4ParticleTable::RemoveAllParticles() " << G4endl;
189 }
190#endif
191
192 //remove all contnts in Ion Table
193 if (fIonTable!=0) {
194 fIonTable->clear();
195 }
196
197 // remomve all contents in hort Lived table
198 if (fShortLivedTable!=0) {
199 fShortLivedTable->clear();
200 }
201
202 // clear dictionary for encoding
203 if (fEncodingDictionary) {
204 fEncodingDictionary->clear();
205 }
206
207 // clear dictionary
208 if (fDictionary) {
209 fDictionary->clear();
210 }
211}
212
213////////////////////
214G4ParticleDefinition* G4ParticleTable::Insert(G4ParticleDefinition *particle)
215{
216
217 // check particle name
218 if ((particle == 0) || (GetKey(particle).isNull())) {
219#ifdef G4VERBOSE
220 if (verboseLevel>0){
221 G4cout << "The particle[Addr:" << particle << "] has no name "<< G4endl;
222 }
223#endif
224 return 0;
225
226 }else {
227
228 if (contains(particle)) {
229#ifdef G4VERBOSE
230 if (verboseLevel>0){
231 G4cout << "The particle has same name "<< G4endl;
232 }
233 if (verboseLevel>1){
234 FindParticle(particle) -> DumpTable();
235 }
236#endif
237 return FindParticle(particle);
238
239 } else {
240 G4PTblDictionary *pdic = fDictionary;
241 G4PTblEncodingDictionary *pedic = fEncodingDictionary;
242
243 // insert into Dictionary
244 pdic->insert( std::pair<G4String, G4ParticleDefinition*>(GetKey(particle), particle) );
245
246 // insert into EncodingDictionary
247 G4int code = particle->GetPDGEncoding();
248 if (code !=0 ) {
249 pedic->insert( std::pair<G4int, G4ParticleDefinition*>(code ,particle) );
250 }
251
252 // insert it in IonTable if "nucleus"
253 if (fIonTable->IsIon(particle) ){
254 fIonTable->Insert(particle);
255 }
256
257 // insert it in ShortLivedTable if "shortlived"
258 if (particle->IsShortLived() ){
259 fShortLivedTable->Insert(particle);
260 }
261
262 return particle;
263 }
264 }
265}
266
267
268////////////////////
269G4ParticleDefinition* G4ParticleTable::Remove(G4ParticleDefinition* particle)
270{
271 G4PTblDictionary::iterator it = fDictionary->find(GetKey(particle));
272 if (it != fDictionary->end()) {
273 fDictionary->erase(it);
274 // remove from EncodingDictionary
275 G4int code = particle->GetPDGEncoding();
276 if (code !=0 ) {
277 fEncodingDictionary->erase(fEncodingDictionary->find(code));
278 }
279 } else {
280 return 0;
281 }
282
283 // remove it from IonTable if "nucleus"
284 if (fIonTable->IsIon(particle) ){
285 fIonTable->Remove(particle);
286 }
287
288 // Remove it from ShortLivedTable if "shortlived"
289 if (particle->IsShortLived() ){
290 fShortLivedTable->Remove(particle);
291 }
292
293 return particle;
294}
295
296////////////////////
297G4ParticleDefinition* G4ParticleTable::FindIon(G4int Z, G4int A, G4int , G4int )
298{
299 CheckReadiness();
300 if (Z<=0) return 0;
301 if (A<Z) return 0;
302 return fIonTable->GetIon(Z, A);
303}
304
305////////////////////
306G4ParticleDefinition* G4ParticleTable::GetIon(G4int Z, G4int A, G4double E)
307{
308 CheckReadiness();
309 if (Z<=0) return 0;
310 if (A<Z) return 0;
311 if (E<0.) return 0;
312 return fIonTable->GetIon(Z, A, E);
313}
314
315////////////////////
316G4ParticleDefinition* G4ParticleTable::GetIon(G4int Z, G4int A, G4int L, G4double E)
317{
318 CheckReadiness();
319 if (Z<=0) return 0;
320 if (A-L<Z) return 0;
321 if (L<0) return 0;
322 if (E<0.) return 0;
323 return fIonTable->GetIon(Z, A, L, E);
324}
325
326////////////////////
327G4ParticleDefinition* G4ParticleTable::FindIon(G4int Z, G4int A, G4double E)
328{
329 CheckReadiness();
330 if (Z<=0) return 0;
331 if (A<Z) return 0;
332 if (E<0.) return 0;
333 return fIonTable->FindIon(Z, A, E);
334}
335
336////////////////////
337G4ParticleDefinition* G4ParticleTable::FindIon(G4int Z, G4int A, G4int L, G4double E)
338{
339 CheckReadiness();
340 if (Z<=0) return 0;
341 if (A-L<Z) return 0;
342 if (L<0) return 0;
343 if (E<0.) return 0;
344 return fIonTable->FindIon(Z, A, L, E);
345}
346
347////////////////////
348G4ParticleDefinition* G4ParticleTable::GetParticle(G4int index)
349{
350 CheckReadiness();
351 if ( (index >=0) && (index < entries()) ) {
352 G4PTblDicIterator *piter = fIterator;
353 piter -> reset();
354 G4int counter = 0;
355 while( (*piter)() ){
356 if ( counter == index ) return piter->value();
357 counter++;
358 }
359 }
360#ifdef G4VERBOSE
361 if (verboseLevel>0){
362 G4cout << " G4ParticleTable::GetParticle";
363 G4cout << " invalid index (=" << index << ")" << G4endl;
364 }
365#endif
366 return 0;
367}
368
369////////////////////
370G4ParticleDefinition* G4ParticleTable::FindParticle(const G4ParticleDefinition *particle)
371{
372 CheckReadiness();
373 G4String key = GetKey(particle);
374 return FindParticle(key);
375}
376
377////////////////////
378G4ParticleDefinition* G4ParticleTable::FindParticle(G4int aPDGEncoding )
379{
380 CheckReadiness();
381 // check aPDGEncoding is valid
382 if (aPDGEncoding == 0){
383#ifdef G4VERBOSE
384 if (verboseLevel>0){
385 G4cout << "PDGEncoding [" << aPDGEncoding << "] is not valid " << G4endl;
386 }
387#endif
388 return 0;
389 }
390
391 G4PTblEncodingDictionary *pedic = fEncodingDictionary;
392 G4ParticleDefinition* particle =0;
393
394 G4PTblEncodingDictionary::iterator it = pedic->find(aPDGEncoding );
395 if (it != pedic->end()) {
396 particle = (*it).second;
397 }
398
399#ifdef G4VERBOSE
400 if ((particle == 0) && (verboseLevel>0) ){
401 G4cout << "CODE:" << aPDGEncoding << " does not exist in ParticleTable " << G4endl;
402 }
403#endif
404 return particle;
405}
406
407////////////////////
408void G4ParticleTable::DumpTable(const G4String &particle_name)
409{
410 CheckReadiness();
411 if (( particle_name == "ALL" ) || (particle_name == "all")){
412 // dump all particles
413 G4PTblDicIterator *piter = fIterator;
414 piter -> reset();
415 while( (*piter)() ){
416 (piter->value())->DumpTable();
417 }
418 } else {
419 // dump only particle with name of particle_name
420 G4ParticleDefinition *ptr;
421 ptr = FindParticle(particle_name);
422 if ( ptr != 0) {
423 ptr->DumpTable();
424 } else {
425 G4cout << " G4ParticleTable::DumpTable : "
426 << particle_name << " does not exist in ParticleTable " <<G4endl;
427 }
428 }
429}
430
431void G4ParticleTable::CheckReadiness()
432{
433 if(!readyToUse)
434 {
435 G4String msg;
436 msg = " Access to G4ParticleTable for finding a particle or equivalent\n";
437 msg += "operation occurs before G4VUserPhysicsList is instantiated and\n";
438 msg += "assigned to G4RunManager. Such an access is prohibited by\n";
439 msg += "Geant4 version 8.0. To fix this problem, please make sure that\n";
440 msg += "your main() instantiates G4VUserPhysicsList and set it to\n";
441 msg += "G4RunManager before instantiating other user classes such as\n";
442 msg += "G4VUserPrimaryParticleGeneratorAction.";
443 G4Exception("G4ParticleTable::CheckReadiness()",
444 "PartMan0000",FatalException,msg);
445 }
446}
447
448
449
450
451
452
Note: See TracBrowser for help on using the repository browser.