Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
G4ParticleTable.cc
Go to the documentation of this file.
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$
28//
29// class G4ParticleTable
30//
31// Implementation
32//
33// History:
34// modified Apr., 97 H.Kurashige
35// added fParticleMessenger 14 Nov., 97 H.Kurashige
36// added GetParticle() 13 Dec., 97 H.Kurashige
37// added IonTable and ShortLivedTable 27 June, 98 H.Kurashige
38// modified FindIon 02 Aug., 98 H.Kurashige
39// added dictionary for encoding 24 Sep., 98 H.Kurashige
40// fixed bugs in destruction of IonTable 08 Nov.,98 H.Kurashige
41// commented out G4cout/G4cout in the constructor 10 Nov.,98 H.Kurashige
42// --------------------------------
43// modified destructor for STL interface 18 May 1999
44// fixed some improper codings 08 Apr., 99 H.Kurashige
45// modified FindIon/GetIon methods 17 AUg., 99 H.Kurashige
46// implement new version for using STL map instaed of
47// RW PtrHashedDictionary 28 ct., 99 H.Kurashige
48
49
50#include "G4ios.hh"
51#include "globals.hh"
52#include "G4ParticleTable.hh"
53#include "G4UImessenger.hh"
55#include "G4IonTable.hh"
56#include "G4ShortLivedTable.hh"
57#include "G4StateManager.hh"
58
59// Static class variable: ptr to single instance of class
60G4ParticleTable* G4ParticleTable::fgParticleTable =0;
61
62////////////////////
64{
65 static G4ParticleTable theParticleTable;
66 if (!fgParticleTable){
67 fgParticleTable = &theParticleTable;
68 }
69 return fgParticleTable;
70}
71
72////////////////////
74 :verboseLevel(1),fParticleMessenger(0),
75 noName(" "),
76 readyToUse(false)
77{
78 fDictionary = new G4PTblDictionary();
79 fIterator = new G4PTblDicIterator( *fDictionary );
80 fEncodingDictionary = new G4PTblEncodingDictionary();
81
82 // Ion Table
83 fIonTable = new G4IonTable();
84
85 // short lived table
86 fShortLivedTable = new G4ShortLivedTable();
87}
88
89////////////////////
91{
92 readyToUse = false;
93
94 // remove all items from G4ParticleTable
96
97 // delete Short Lived table
98 if (fShortLivedTable!=0) delete fShortLivedTable;
99 fShortLivedTable =0;
100
101
102 //delete Ion Table
103 if (fIonTable!=0) delete fIonTable;
104 fIonTable =0;
105
106 // delete dictionary for encoding
107 if (fEncodingDictionary!=0){
108 fEncodingDictionary -> clear();
109 delete fEncodingDictionary;
110 fEncodingDictionary =0;
111 }
112
113 if(fDictionary){
114 if (fIterator!=0 )delete fIterator;
115 fIterator =0;
116
117 fDictionary->clear();
118 delete fDictionary;
119 fDictionary =0;
120 }
121
122 if (fParticleMessenger!=0) delete fParticleMessenger;
123 fParticleMessenger =0;
124
125 fgParticleTable =0;
126
127}
128
129////////////////////
131 :verboseLevel(1),fParticleMessenger(0),
132 noName(" "),
133 readyToUse(false)
134{
135 G4Exception("G4ParticleTable::G4ParticleTable()",
136 "PART001", FatalException,
137 "Illegal call of copy constructor for G4ParticleTable");
138 fDictionary = new G4PTblDictionary(*(right.fDictionary));
139 fIterator = new G4PTblDicIterator(*fDictionary);
140}
141
142////////////////////
144{
145 if (this != &right) {
146 G4Exception("G4ParticleTable::G4ParticleTable()",
147 "PART001", FatalException,
148 "Illegal call of assignment operator for G4ParticleTable");
149 fDictionary = new G4PTblDictionary(*(right.fDictionary));
150 fIterator = new G4PTblDicIterator(*fDictionary);
151 }
152 return *this;
153}
154
155////////////////////
157{
158 if (fParticleMessenger== 0) {
159 //UI messenger
160 fParticleMessenger = new G4ParticleMessenger(this);
161 }
162 return fParticleMessenger;
163}
164
165////////////////////
167{
168 if (fParticleMessenger!= 0) {
169 //UI messenger
170 delete fParticleMessenger;
171 fParticleMessenger= 0;
172 }
173
174}
175
176////////////////////
178{
179 //set readyToUse false
180 readyToUse = false;
181
182 //G4StateManager* pStateManager = G4StateManager::GetStateManager();
183 //G4ApplicationState currentState = pStateManager->GetCurrentState();
184 //if (currentState != G4State_Quit) {
185 // G4Exception("G4ParticleTable::DeleteAllParticle()",
186 // "PART114", JustWarning,
187 // "Try to delete particles in state other than State_Quit");
188 //}
189
190#ifdef G4VERBOSE
191 if (verboseLevel>1){
192 G4cout << "G4ParticleTable::DeleteAllParticles() " << G4endl;
193 }
194#endif
195
196 // delete all particles
197 G4PTblDicIterator *piter = fIterator;
198 piter -> reset();
199 while( (*piter)() ){
200#ifdef G4VERBOSE
201 if (verboseLevel>2){
202 G4cout << "Delete " << (piter->value())->GetParticleName()
203 << " " << (piter->value()) << G4endl;
204 }
205#endif
206 delete (piter->value());
207 }
209}
210
211////////////////////
213{
214 if (readyToUse) {
215 G4Exception("G4ParticleTable::RemoveAllParticle()",
216 "PART115", JustWarning,
217 "No effects because readyToUse is true.");
218 return;
219 }
220
221#ifdef G4VERBOSE
222 if (verboseLevel>1){
223 G4cout << "G4ParticleTable::RemoveAllParticles() " << G4endl;
224 }
225#endif
226
227 //remove all contnts in Ion Table
228 if (fIonTable!=0) {
229 fIonTable->clear();
230 }
231
232 // remomve all contents in hort Lived table
233 if (fShortLivedTable!=0) {
234 fShortLivedTable->clear();
235 }
236
237 // clear dictionary for encoding
238 if (fEncodingDictionary) {
239 fEncodingDictionary->clear();
240 }
241
242 // clear dictionary
243 if (fDictionary) {
244 fDictionary->clear();
245 }
246}
247
248////////////////////
250{
251
252 // check particle name
253 if ((particle == 0) || (GetKey(particle).isNull())) {
254#ifdef G4VERBOSE
255 if (verboseLevel>0){
256 G4cerr << "The particle[Addr:" << particle << "] has no name "<< G4endl;
257 }
258#endif
259 return 0;
260
261 }else {
262
263 if (contains(particle)) {
264#ifdef G4VERBOSE
265 if (verboseLevel>0){
266 G4cerr << "The particle " << particle->GetParticleName()
267 << "has been already registered in the Particle Table "<< G4endl;
268 }
269 if (verboseLevel>1){
270 FindParticle(particle) -> DumpTable();
271 }
272#endif
273 return FindParticle(particle);
274
275 } else {
276 G4PTblDictionary *pdic = fDictionary;
277 G4PTblEncodingDictionary *pedic = fEncodingDictionary;
278
279 // insert into Dictionary
280 pdic->insert( std::pair<G4String, G4ParticleDefinition*>(GetKey(particle), particle) );
281
282 // insert into EncodingDictionary
283 G4int code = particle->GetPDGEncoding();
284 if (code !=0 ) {
285 pedic->insert( std::pair<G4int, G4ParticleDefinition*>(code ,particle) );
286 }
287
288 // insert it in IonTable if "nucleus"
289 if (fIonTable->IsIon(particle) ){
290 fIonTable->Insert(particle);
291 }
292
293 // insert it in ShortLivedTable if "shortlived"
294 if (particle->IsShortLived() ){
295 fShortLivedTable->Insert(particle);
296 }
297
298 // set Verbose Level same as ParticleTable
299 particle->SetVerboseLevel(verboseLevel);
300
301#ifdef G4VERBOSE
302 if (verboseLevel>3){
303 G4cout << "The particle "<< particle->GetParticleName()
304 << " is inserted in the ParticleTable " << G4endl;
305 }
306#endif
307
308 return particle;
309 }
310 }
311}
312
313////////////////////
315{
316 if (readyToUse) {
318 G4ApplicationState currentState = pStateManager->GetCurrentState();
319 if (currentState != G4State_PreInit) {
320 G4String msg = "Request of removing ";
321 msg += particle->GetParticleName();
322 msg += " has No effects other than Pre_Init";
323 G4Exception("G4ParticleTable::Remove()",
324 "PART117", JustWarning, msg);
325 return 0;
326 } else {
327#ifdef G4VERBOSE
328 if (verboseLevel>0){
329 G4cout << particle->GetParticleName()
330 << " will be removed from the ParticleTable " << G4endl;
331 }
332#endif
333 }
334 }
335
336 G4PTblDictionary::iterator it = fDictionary->find(GetKey(particle));
337 if (it != fDictionary->end()) {
338 fDictionary->erase(it);
339 // remove from EncodingDictionary
340 G4int code = particle->GetPDGEncoding();
341 if (code !=0 ) {
342 fEncodingDictionary->erase(fEncodingDictionary->find(code));
343 }
344 } else {
345 return 0;
346 }
347
348 // remove it from IonTable if "nucleus"
349 if (fIonTable->IsIon(particle) ){
350 fIonTable->Remove(particle);
351 }
352
353 // Remove it from ShortLivedTable if "shortlived"
354 if (particle->IsShortLived() ){
355 fShortLivedTable->Remove(particle);
356 }
357
358#ifdef G4VERBOSE
359 if (verboseLevel>3){
360 G4cout << "The particle "<< particle->GetParticleName()
361 << " is removed from the ParticleTable " << G4endl;
362 }
363#endif
364
365 return particle;
366}
367
368////////////////////
370{
371 CheckReadiness();
372 if (Z<=0) return 0;
373 if (A<Z) return 0;
374 return fIonTable->GetIon(Z, A);
375}
376
377////////////////////
379{
380 CheckReadiness();
381 if (Z<=0) return 0;
382 if (A<Z) return 0;
383 if (E<0.) return 0;
384 return fIonTable->GetIon(Z, A, E);
385}
386
387////////////////////
389{
390 CheckReadiness();
391 if (Z<=0) return 0;
392 if (A-L<Z) return 0;
393 if (L<0) return 0;
394 if (E<0.) return 0;
395 return fIonTable->GetIon(Z, A, L, E);
396}
397
398////////////////////
400{
401 CheckReadiness();
402 if (Z<=0) return 0;
403 if (A<Z) return 0;
404 if (E<0.) return 0;
405 return fIonTable->FindIon(Z, A, E);
406}
407
408////////////////////
410{
411 CheckReadiness();
412 if (Z<=0) return 0;
413 if (A-L<Z) return 0;
414 if (L<0) return 0;
415 if (E<0.) return 0;
416 return fIonTable->FindIon(Z, A, L, E);
417}
418
419////////////////////
421{
422 CheckReadiness();
423 if ( (index >=0) && (index < entries()) ) {
424 G4PTblDicIterator *piter = fIterator;
425 piter -> reset();
426 G4int counter = 0;
427 while( (*piter)() ){
428 if ( counter == index ) return piter->value();
429 counter++;
430 }
431 }
432#ifdef G4VERBOSE
433 if (verboseLevel>1){
434 G4cerr << " G4ParticleTable::GetParticle"
435 << " invalid index (=" << index << ")" << G4endl;
436 }
437#endif
438 return 0;
439}
440
441////////////////////
443{
444 G4ParticleDefinition* aParticle =GetParticle(index);
445 if (aParticle != 0) {
446 return aParticle->GetParticleName();
447 } else {
448 return noName;
449 }
450}
451
452////////////////////
454{
455 G4PTblDictionary::iterator it = fDictionary->find(particle_name);
456 if (it != fDictionary->end()) {
457 return (*it).second;
458 } else {
459 return 0;
460 }
461}
462
463////////////////////
465{
466 CheckReadiness();
467 G4String key = GetKey(particle);
468 return FindParticle(key);
469}
470
471////////////////////
473{
474 CheckReadiness();
475 // check aPDGEncoding is valid
476 if (aPDGEncoding == 0){
477#ifdef G4VERBOSE
478 if (verboseLevel>1){
479 G4cerr << "PDGEncoding [" << aPDGEncoding << "] is not valid " << G4endl;
480 }
481#endif
482 return 0;
483 }
484
485 G4PTblEncodingDictionary *pedic = fEncodingDictionary;
486 G4ParticleDefinition* particle =0;
487
488 G4PTblEncodingDictionary::iterator it = pedic->find(aPDGEncoding );
489 if (it != pedic->end()) {
490 particle = (*it).second;
491 }
492
493#ifdef G4VERBOSE
494 if ((particle == 0) && (verboseLevel>1) ){
495 G4cerr << "CODE:" << aPDGEncoding << " does not exist in ParticleTable " << G4endl;
496 }
497#endif
498 return particle;
499}
500
501////////////////////
502void G4ParticleTable::DumpTable(const G4String &particle_name)
503{
504 CheckReadiness();
505 if (( particle_name == "ALL" ) || (particle_name == "all")){
506 // dump all particles
507 G4PTblDicIterator *piter = fIterator;
508 piter -> reset();
509 while( (*piter)() ){
510 (piter->value())->DumpTable();
511 }
512 } else {
513 // dump only particle with name of particle_name
515 ptr = FindParticle(particle_name);
516 if ( ptr != 0) {
517 ptr->DumpTable();
518 } else {
519 G4cerr << " G4ParticleTable::DumpTable : "
520 << particle_name << " does not exist in ParticleTable " <<G4endl;
521 }
522 }
523}
524
525void G4ParticleTable::CheckReadiness()
526{
527 if(!readyToUse) {
528 G4String msg;
529 msg = "Illegal use of G4ParticleTable : ";
530 msg += " Access to G4ParticleTable for finding a particle or equivalent\n";
531 msg += "operation occurs before G4VUserPhysicsList is instantiated and\n";
532 msg += "assigned to G4RunManager. Such an access is prohibited by\n";
533 msg += "Geant4 version 8.0. To fix this problem, please make sure that\n";
534 msg += "your main() instantiates G4VUserPhysicsList and set it to\n";
535 msg += "G4RunManager before instantiating other user classes such as\n";
536 msg += "G4VUserPrimaryParticleGeneratorAction.";
537 G4Exception("G4ParticleTable::CheckReadiness()",
538 "PART002",FatalException,msg);
539 }
540}
541
542
543
G4ApplicationState
@ G4State_PreInit
@ JustWarning
@ FatalException
double G4double
Definition: G4Types.hh:64
int G4int
Definition: G4Types.hh:66
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cerr
G4DLLIMPORT std::ostream G4cout
void Remove(const G4ParticleDefinition *particle)
Definition: G4IonTable.cc:817
void clear()
Definition: G4IonTable.cc:784
static G4bool IsIon(const G4ParticleDefinition *)
Definition: G4IonTable.cc:594
G4ParticleDefinition * FindIon(G4int Z, G4int A, G4double E, G4int J=0)
Definition: G4IonTable.cc:353
void Insert(const G4ParticleDefinition *particle)
Definition: G4IonTable.cc:802
G4ParticleDefinition * GetIon(G4int Z, G4int A, G4int J=0)
Definition: G4IonTable.cc:267
void SetVerboseLevel(G4int value)
const G4String & GetParticleName() const
G4ParticleTableIterator< G4String, G4ParticleDefinition * > G4PTblDicIterator
G4int entries() const
const G4String & GetParticleName(G4int index)
G4ParticleTable & operator=(const G4ParticleTable &)
G4ParticleTableIterator< G4String, G4ParticleDefinition * >::Map G4PTblDictionary
G4ParticleDefinition * FindIon(G4int atomicNumber, G4int atomicMass, G4double excitationEnergy)
G4ParticleDefinition * FindParticle(G4int PDGEncoding)
static G4ParticleTable * GetParticleTable()
G4ParticleDefinition * Insert(G4ParticleDefinition *particle)
G4bool contains(const G4ParticleDefinition *particle)
G4ParticleDefinition * Remove(G4ParticleDefinition *particle)
G4UImessenger * CreateMessenger()
virtual ~G4ParticleTable()
G4ParticleDefinition * GetParticle(G4int index)
const G4String & GetKey(const G4ParticleDefinition *particle) const
G4ParticleTableIterator< G4int, G4ParticleDefinition * >::Map G4PTblEncodingDictionary
G4ParticleDefinition * GetIon(G4int atomicNumber, G4int atomicMass, G4double excitationEnergy)
void DumpTable(const G4String &particle_name="ALL")
void Remove(const G4ParticleDefinition *particle)
void Insert(const G4ParticleDefinition *particle)
G4ApplicationState GetCurrentState() const
static G4StateManager * GetStateManager()
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41