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

Last change on this file since 1257 was 1196, checked in by garnier, 16 years ago

update CVS release candidate geant4.9.3.01

File size: 12.7 KB
Line 
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.35 2009/08/17 14:52:19 kurasige Exp $
28// GEANT4 tag $Name: geant4-09-03-cand-01 $
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(1),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#ifdef G4VERBOSE
177 if (verboseLevel>2){
178 G4cout << "Delete " << (piter->value())->GetParticleName()
179 << " " << (piter->value()) << G4endl;
180 }
181#endif
182 delete (piter->value());
183 }
184
185 RemoveAllParticles();
186}
187
188////////////////////
189void G4ParticleTable::RemoveAllParticles()
190{
191
192#ifdef G4VERBOSE
193 if (verboseLevel>1){
194 G4cout << "G4ParticleTable::RemoveAllParticles() " << G4endl;
195 }
196#endif
197
198 //remove all contnts in Ion Table
199 if (fIonTable!=0) {
200 fIonTable->clear();
201 }
202
203 // remomve all contents in hort Lived table
204 if (fShortLivedTable!=0) {
205 fShortLivedTable->clear();
206 }
207
208 // clear dictionary for encoding
209 if (fEncodingDictionary) {
210 fEncodingDictionary->clear();
211 }
212
213 // clear dictionary
214 if (fDictionary) {
215 fDictionary->clear();
216 }
217}
218
219////////////////////
220G4ParticleDefinition* G4ParticleTable::Insert(G4ParticleDefinition *particle)
221{
222
223 // check particle name
224 if ((particle == 0) || (GetKey(particle).isNull())) {
225#ifdef G4VERBOSE
226 if (verboseLevel>0){
227 G4cout << "The particle[Addr:" << particle << "] has no name "<< G4endl;
228 }
229#endif
230 return 0;
231
232 }else {
233
234 if (contains(particle)) {
235#ifdef G4VERBOSE
236 if (verboseLevel>0){
237 G4cout << "The particle " << particle->GetParticleName()
238 << "has been already registered in the Particle Table "<< G4endl;
239 }
240 if (verboseLevel>1){
241 FindParticle(particle) -> DumpTable();
242 }
243#endif
244 return FindParticle(particle);
245
246 } else {
247 G4PTblDictionary *pdic = fDictionary;
248 G4PTblEncodingDictionary *pedic = fEncodingDictionary;
249
250 // insert into Dictionary
251 pdic->insert( std::pair<G4String, G4ParticleDefinition*>(GetKey(particle), particle) );
252
253 // insert into EncodingDictionary
254 G4int code = particle->GetPDGEncoding();
255 if (code !=0 ) {
256 pedic->insert( std::pair<G4int, G4ParticleDefinition*>(code ,particle) );
257 }
258
259 // insert it in IonTable if "nucleus"
260 if (fIonTable->IsIon(particle) ){
261 fIonTable->Insert(particle);
262 }
263
264 // insert it in ShortLivedTable if "shortlived"
265 if (particle->IsShortLived() ){
266 fShortLivedTable->Insert(particle);
267 }
268
269 // set Verbose Level same as ParticleTable
270 particle->SetVerboseLevel(verboseLevel);
271
272#ifdef G4VERBOSE
273 if (verboseLevel>3){
274 G4cout << "The particle "<< particle->GetParticleName()
275 << " is inserted in the ParticleTable " << G4endl;
276 }
277#endif
278
279 return particle;
280 }
281 }
282}
283
284
285////////////////////
286G4ParticleDefinition* G4ParticleTable::Remove(G4ParticleDefinition* particle)
287{
288 G4PTblDictionary::iterator it = fDictionary->find(GetKey(particle));
289 if (it != fDictionary->end()) {
290 fDictionary->erase(it);
291 // remove from EncodingDictionary
292 G4int code = particle->GetPDGEncoding();
293 if (code !=0 ) {
294 fEncodingDictionary->erase(fEncodingDictionary->find(code));
295 }
296 } else {
297 return 0;
298 }
299
300 // remove it from IonTable if "nucleus"
301 if (fIonTable->IsIon(particle) ){
302 fIonTable->Remove(particle);
303 }
304
305 // Remove it from ShortLivedTable if "shortlived"
306 if (particle->IsShortLived() ){
307 fShortLivedTable->Remove(particle);
308 }
309
310#ifdef G4VERBOSE
311 if (verboseLevel>3){
312 G4cout << "The particle "<< particle->GetParticleName()
313 << " is removed from the ParticleTable " << G4endl;
314 }
315#endif
316
317 return particle;
318}
319
320////////////////////
321G4ParticleDefinition* G4ParticleTable::FindIon(G4int Z, G4int A, G4int , G4int )
322{
323 CheckReadiness();
324 if (Z<=0) return 0;
325 if (A<Z) return 0;
326 return fIonTable->GetIon(Z, A);
327}
328
329////////////////////
330G4ParticleDefinition* G4ParticleTable::GetIon(G4int Z, G4int A, G4double E)
331{
332 CheckReadiness();
333 if (Z<=0) return 0;
334 if (A<Z) return 0;
335 if (E<0.) return 0;
336 return fIonTable->GetIon(Z, A, E);
337}
338
339////////////////////
340G4ParticleDefinition* G4ParticleTable::GetIon(G4int Z, G4int A, G4int L, G4double E)
341{
342 CheckReadiness();
343 if (Z<=0) return 0;
344 if (A-L<Z) return 0;
345 if (L<0) return 0;
346 if (E<0.) return 0;
347 return fIonTable->GetIon(Z, A, L, E);
348}
349
350////////////////////
351G4ParticleDefinition* G4ParticleTable::FindIon(G4int Z, G4int A, G4double E)
352{
353 CheckReadiness();
354 if (Z<=0) return 0;
355 if (A<Z) return 0;
356 if (E<0.) return 0;
357 return fIonTable->FindIon(Z, A, E);
358}
359
360////////////////////
361G4ParticleDefinition* G4ParticleTable::FindIon(G4int Z, G4int A, G4int L, G4double E)
362{
363 CheckReadiness();
364 if (Z<=0) return 0;
365 if (A-L<Z) return 0;
366 if (L<0) return 0;
367 if (E<0.) return 0;
368 return fIonTable->FindIon(Z, A, L, E);
369}
370
371////////////////////
372G4ParticleDefinition* G4ParticleTable::GetParticle(G4int index)
373{
374 CheckReadiness();
375 if ( (index >=0) && (index < entries()) ) {
376 G4PTblDicIterator *piter = fIterator;
377 piter -> reset();
378 G4int counter = 0;
379 while( (*piter)() ){
380 if ( counter == index ) return piter->value();
381 counter++;
382 }
383 }
384#ifdef G4VERBOSE
385 if (verboseLevel>1){
386 G4cout << " G4ParticleTable::GetParticle";
387 G4cout << " invalid index (=" << index << ")" << G4endl;
388 }
389#endif
390 return 0;
391}
392
393////////////////////
394G4ParticleDefinition* G4ParticleTable::FindParticle(const G4ParticleDefinition *particle)
395{
396 CheckReadiness();
397 G4String key = GetKey(particle);
398 return FindParticle(key);
399}
400
401////////////////////
402G4ParticleDefinition* G4ParticleTable::FindParticle(G4int aPDGEncoding )
403{
404 CheckReadiness();
405 // check aPDGEncoding is valid
406 if (aPDGEncoding == 0){
407#ifdef G4VERBOSE
408 if (verboseLevel>1){
409 G4cout << "PDGEncoding [" << aPDGEncoding << "] is not valid " << G4endl;
410 }
411#endif
412 return 0;
413 }
414
415 G4PTblEncodingDictionary *pedic = fEncodingDictionary;
416 G4ParticleDefinition* particle =0;
417
418 G4PTblEncodingDictionary::iterator it = pedic->find(aPDGEncoding );
419 if (it != pedic->end()) {
420 particle = (*it).second;
421 }
422
423#ifdef G4VERBOSE
424 if ((particle == 0) && (verboseLevel>1) ){
425 G4cout << "CODE:" << aPDGEncoding << " does not exist in ParticleTable " << G4endl;
426 }
427#endif
428 return particle;
429}
430
431////////////////////
432void G4ParticleTable::DumpTable(const G4String &particle_name)
433{
434 CheckReadiness();
435 if (( particle_name == "ALL" ) || (particle_name == "all")){
436 // dump all particles
437 G4PTblDicIterator *piter = fIterator;
438 piter -> reset();
439 while( (*piter)() ){
440 (piter->value())->DumpTable();
441 }
442 } else {
443 // dump only particle with name of particle_name
444 G4ParticleDefinition *ptr;
445 ptr = FindParticle(particle_name);
446 if ( ptr != 0) {
447 ptr->DumpTable();
448 } else {
449 G4cout << " G4ParticleTable::DumpTable : "
450 << particle_name << " does not exist in ParticleTable " <<G4endl;
451 }
452 }
453}
454
455void G4ParticleTable::CheckReadiness()
456{
457 if(!readyToUse) {
458 G4String msg;
459 msg = " Access to G4ParticleTable for finding a particle or equivalent\n";
460 msg += "operation occurs before G4VUserPhysicsList is instantiated and\n";
461 msg += "assigned to G4RunManager. Such an access is prohibited by\n";
462 msg += "Geant4 version 8.0. To fix this problem, please make sure that\n";
463 msg += "your main() instantiates G4VUserPhysicsList and set it to\n";
464 msg += "G4RunManager before instantiating other user classes such as\n";
465 msg += "G4VUserPrimaryParticleGeneratorAction.";
466 G4Exception("G4ParticleTable::CheckReadiness()",
467 "PartMan0000",FatalException,msg);
468 }
469}
470
471
472
473
474
475
476
Note: See TracBrowser for help on using the repository browser.