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

Last change on this file since 944 was 850, checked in by garnier, 16 years ago

geant4.8.2 beta

File size: 12.6 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.33 2008/06/08 12:55:45 kurasige Exp $
28// GEANT4 tag $Name: HEAD $
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 has same name "<< G4endl;
238      }
239      if (verboseLevel>1){
240        FindParticle(particle) -> DumpTable();
241      }
242#endif
243      return  FindParticle(particle);
244
245    } else {
246      G4PTblDictionary *pdic =  fDictionary;
247      G4PTblEncodingDictionary *pedic =  fEncodingDictionary; 
248
249      // insert into Dictionary
250      pdic->insert( std::pair<G4String, G4ParticleDefinition*>(GetKey(particle), particle) );
251
252      // insert into EncodingDictionary
253      G4int code = particle->GetPDGEncoding();
254      if (code !=0 ) {
255        pedic->insert( std::pair<G4int, G4ParticleDefinition*>(code ,particle) );
256      }       
257
258      // insert it in IonTable if "nucleus"
259      if (fIonTable->IsIon(particle) ){
260        fIonTable->Insert(particle);
261      }
262
263      // insert it in ShortLivedTable if "shortlived"
264      if (particle->IsShortLived() ){
265        fShortLivedTable->Insert(particle);
266      }
267
268      // set Verbose Level same as ParticleTable
269      particle->SetVerboseLevel(verboseLevel);
270
271#ifdef G4VERBOSE
272      if (verboseLevel>3){
273        G4cout << "The particle "<< particle->GetParticleName() 
274               << " is inserted in the ParticleTable " << G4endl;
275      }
276#endif
277
278      return particle;
279    }
280  }
281}
282
283
284////////////////////
285G4ParticleDefinition* G4ParticleTable::Remove(G4ParticleDefinition* particle)
286{
287  G4PTblDictionary::iterator it =  fDictionary->find(GetKey(particle));
288  if (it != fDictionary->end()) {
289    fDictionary->erase(it);
290    // remove from EncodingDictionary
291    G4int code = particle->GetPDGEncoding();
292    if (code !=0 ) {
293      fEncodingDictionary->erase(fEncodingDictionary->find(code)); 
294    }
295  } else {
296    return 0;
297  }
298
299  // remove it from IonTable if "nucleus"
300  if (fIonTable->IsIon(particle) ){
301    fIonTable->Remove(particle);
302  }
303 
304  // Remove it from ShortLivedTable if "shortlived"
305  if (particle->IsShortLived() ){
306    fShortLivedTable->Remove(particle);
307  }
308
309#ifdef G4VERBOSE
310  if (verboseLevel>3){
311    G4cout << "The particle "<< particle->GetParticleName()
312           << " is removed from the ParticleTable " << G4endl;
313  }
314#endif
315
316  return particle;
317}
318
319////////////////////
320G4ParticleDefinition* G4ParticleTable::FindIon(G4int Z, G4int A, G4int , G4int )
321{
322   CheckReadiness();
323   if (Z<=0) return 0;
324   if (A<Z) return 0;
325   return fIonTable->GetIon(Z, A);
326}
327
328////////////////////
329G4ParticleDefinition* G4ParticleTable::GetIon(G4int Z, G4int A, G4double E)
330{
331   CheckReadiness();
332   if (Z<=0) return 0;
333   if (A<Z) return 0;
334   if (E<0.) return 0;
335   return fIonTable->GetIon(Z, A, E);
336}
337
338////////////////////
339G4ParticleDefinition* G4ParticleTable::GetIon(G4int Z, G4int A, G4int L, G4double E)
340{
341   CheckReadiness();
342   if (Z<=0) return 0;
343   if (A-L<Z) return 0;
344   if (L<0) return 0; 
345   if (E<0.) return 0;
346   return fIonTable->GetIon(Z, A, L, E);
347}
348
349////////////////////
350G4ParticleDefinition* G4ParticleTable::FindIon(G4int Z, G4int A, G4double E)
351{
352   CheckReadiness();
353   if (Z<=0) return 0;
354   if (A<Z) return 0;
355   if (E<0.) return 0;
356   return fIonTable->FindIon(Z, A, E);
357}
358
359////////////////////
360G4ParticleDefinition* G4ParticleTable::FindIon(G4int Z, G4int A, G4int L, G4double E)
361{
362   CheckReadiness();
363   if (Z<=0) return 0;
364   if (A-L<Z) return 0;
365   if (L<0) return 0;
366   if (E<0.) return 0;
367   return fIonTable->FindIon(Z, A, L, E);
368}
369
370////////////////////
371G4ParticleDefinition* G4ParticleTable::GetParticle(G4int index)
372{
373   CheckReadiness();
374  if ( (index >=0) && (index < entries()) ) {
375    G4PTblDicIterator *piter = fIterator; 
376    piter -> reset();
377    G4int counter = 0;
378    while( (*piter)() ){
379      if ( counter == index ) return piter->value();
380      counter++;
381    }
382  } 
383#ifdef G4VERBOSE
384  if (verboseLevel>1){
385    G4cout << " G4ParticleTable::GetParticle";
386    G4cout << " invalid index (=" << index << ")" << G4endl;
387  }
388#endif
389  return 0;
390}
391
392////////////////////
393G4ParticleDefinition* G4ParticleTable::FindParticle(const G4ParticleDefinition *particle)
394{
395  CheckReadiness();
396  G4String key = GetKey(particle);
397  return FindParticle(key);
398}
399
400////////////////////
401G4ParticleDefinition* G4ParticleTable::FindParticle(G4int aPDGEncoding )
402{
403   CheckReadiness();
404    // check aPDGEncoding is valid
405    if (aPDGEncoding == 0){ 
406#ifdef G4VERBOSE
407      if (verboseLevel>1){
408        G4cout << "PDGEncoding  [" <<  aPDGEncoding << "] is not valid " << G4endl;
409      }
410#endif
411      return 0;
412    }
413   
414    G4PTblEncodingDictionary *pedic =  fEncodingDictionary;
415    G4ParticleDefinition* particle =0; 
416
417    G4PTblEncodingDictionary::iterator it =  pedic->find(aPDGEncoding );
418    if (it != pedic->end()) {
419      particle = (*it).second;
420    }
421
422#ifdef G4VERBOSE
423    if ((particle == 0) && (verboseLevel>1) ){
424      G4cout << "CODE:" << aPDGEncoding << " does not exist in ParticleTable " << G4endl;
425    }
426#endif
427    return particle;
428}
429
430////////////////////
431void G4ParticleTable::DumpTable(const G4String &particle_name) 
432{
433  CheckReadiness();
434  if (( particle_name == "ALL" ) || (particle_name == "all")){
435    // dump all particles
436    G4PTblDicIterator *piter = fIterator; 
437    piter -> reset();
438    while( (*piter)() ){
439      (piter->value())->DumpTable();
440    }
441  } else {
442    // dump only particle with name of  particle_name
443    G4ParticleDefinition *ptr;
444    ptr = FindParticle(particle_name);
445    if ( ptr != 0) {
446      ptr->DumpTable();
447    } else { 
448      G4cout << " G4ParticleTable::DumpTable : " 
449             << particle_name << " does not exist in ParticleTable " <<G4endl;
450    }
451  }
452}
453
454void G4ParticleTable::CheckReadiness()
455{
456  if(!readyToUse)
457  {
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.