Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4ParticleTable.cc
이 파일의 문서화 페이지로 가기
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 110257 2018-05-17 14:20:12Z gcosmo $
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 Oct., 99 H.Kurashige
48 // remove G4ShortLivedTable 25 July, 13 H.Kurashige
49 // remove FindIon/GetIon 25 Sep. 14 H.Kurashige
50 // added support for MuonicAtom September, 17 K.L.Genser
51 //
52 
53 #include "globals.hh"
54 #include "G4ios.hh"
55 #include "G4ParticleTable.hh"
56 #include "G4UImessenger.hh"
57 #include "G4ParticleMessenger.hh"
58 #include "G4IonTable.hh"
59 #include "G4StateManager.hh"
60 
61 // These fields should be thread local or thread private. For a singleton
62 // class, we can change any member field as static without any problem
63 // because there is only one instance. Then we are allowed to add
64 // "G4ThreadLocal".
65 //
70 
71 // This field should be thread private. However, we have to keep one copy
72 // of the ion table pointer. So we change all important fields of G4IonTable
73 // to the thread local variable.
74 //
76 
77 
78 // These shadow pointers are used by each worker thread to copy the content
79 // from the master thread.
80 //
85 
86 // Static class variable: ptr to single instance of class
88 
89 #ifdef G4MULTITHREADED
90 // Lock for particle table accesses.
91 //
92 G4Mutex& G4ParticleTable::particleTableMutex()
93 {
94  static G4Mutex _instance = G4MUTEX_INITIALIZER;
95  return _instance;
96 }
97 G4int& G4ParticleTable::lockCount()
98 {
99  static G4int _instance = 0;
100  return _instance;
101 }
102 #endif
103 
106 {
107  static G4ParticleTable theParticleTable;
108  if (!fgParticleTable){
109  fgParticleTable = &theParticleTable;
110  }
111 
112  // Here we initialize all thread private data members.
113  //
115 
116  return fgParticleTable;
117 }
118 
121  :verboseLevel(1),
122  noName(" "),
123  readyToUse(false),
124  genericIon(nullptr),
125  genericMuonicAtom(nullptr)
126 {
128 
129  // Set up the shadow pointer used by worker threads.
130  //
131  if (fDictionaryShadow == 0)
132  {
134  }
135 
137 
138  // Set up the shadow pointer used by worker threads.
139  //
140  if (fIteratorShadow == 0)
141  {
143  }
144 
146  // Set up the shadow pointer used by worker threads.
147  //
148  if (fEncodingDictionaryShadow == 0)
149  {
151  }
152 
153 
154  // Ion Table
155  fIonTable = new G4IonTable();
156 
157 }
158 
159 // This method is similar to the constructor. It is used by each worker
160 // thread to achieve the partial effect as that of the master thread.
161 // Here we initialize all thread private data members.
162 //
164 {
165  G4Exception("G4ParticleTable::SlaveG4ParticleTable()","G4MT0000",FatalException,"Obsolete");
166 }
167 
169 {
170  // The iterator for the shadow particle table is not sharable.
171  //
172 #ifdef G4MULTITHREADED
173  G4MUTEXLOCK(&G4ParticleTable::particleTableMutex());
174  G4ParticleTable::lockCount()++;
175 #endif
176  if(fDictionary == 0) {
177  fDictionary = new G4PTblDictionary();
178  } else {
179  fDictionary->clear();
180  }
181 
182  if(fEncodingDictionary == 0){
184  } else {
185  fEncodingDictionary->clear();
186  }
187 
188  fIteratorShadow->reset(false);
189  while( (*fIteratorShadow)() ) { // Loop checking, 09.08.2015, K.Kurashige
191  fDictionary->insert( std::pair<G4String, G4ParticleDefinition*>(GetKey(particle), particle) );
192  G4int code = particle->GetPDGEncoding();
193  if (code !=0 ) {
194  fEncodingDictionary->insert( std::pair<G4int, G4ParticleDefinition*>(code ,particle) );
195  }
196  }
198 
199 #ifdef G4MULTITHREADED
200  G4MUTEXUNLOCK(&G4ParticleTable::particleTableMutex());
201 #endif
202 
204 
205 }
206 
209 {
210  readyToUse = false;
211 
212  // remove all items from G4ParticleTable
214 
215  //delete Ion Table
216  if (fIonTable!=0) delete fIonTable;
217  fIonTable =0;
218 
219  // delete dictionary for encoding
220  if (fEncodingDictionary!=0){
222  delete fEncodingDictionary;
224  }
225 
226  if(fDictionary){
227  if (fIterator!=0 )delete fIterator;
228  fIterator =0;
229 
230  fDictionary->clear();
231  delete fDictionary;
232  fDictionary =0;
233  }
234 
235  if (fParticleMessenger!=0) delete fParticleMessenger;
237 
238  fgParticleTable =0;
239 
240  G4ParticleDefinition::Clean(); // Delete sub-instance static data
241 }
242 
245 {
246  //delete Ion Table in worker thread
248 
249  // delete dictionary for encoding
250  if (fEncodingDictionary!=0){
252  delete fEncodingDictionary;
254  }
255 
256  if(fDictionary){
257  if (fIterator!=0 )delete fIterator;
258  fIterator =0;
259 
260  fDictionary->clear();
261  delete fDictionary;
262  fDictionary =0;
263  }
264 
265  if (fParticleMessenger!=0) delete fParticleMessenger;
267 }
268 
271  :verboseLevel(1),
272  noName(" "),
273  readyToUse(false)
274 {
275  fParticleMessenger = 0 ;
276 
277  G4Exception("G4ParticleTable::G4ParticleTable()",
278  "PART001", FatalException,
279  "Illegal call of copy constructor for G4ParticleTable");
280  fDictionary = new G4PTblDictionary(*(right.fDictionary));
282 }
283 
286 {
287  if (this != &right) {
288  G4Exception("G4ParticleTable::G4ParticleTable()",
289  "PART001", FatalException,
290  "Illegal call of assignment operator for G4ParticleTable");
291  fDictionary = new G4PTblDictionary(*(right.fDictionary));
293  }
294  return *this;
295 }
296 
299 {
300  if (fParticleMessenger== 0) {
301  //UI messenger
303  }
304  return fParticleMessenger;
305 }
306 
309 {
310  if (fParticleMessenger!= 0) {
311  //UI messenger
312  delete fParticleMessenger;
314  }
315 
316 }
317 
320 {
321  //set readyToUse false
322  readyToUse = false;
323 
324 #ifdef G4VERBOSE
325  if (verboseLevel>1){
326  G4cout << "G4ParticleTable::DeleteAllParticles() " << G4endl;
327  }
328 #endif
329 
330  // delete all particles
331  G4PTblDicIterator *piter = fIterator;
332  piter -> reset(false);
333  while( (*piter)() ){// Loop checking, 09.08.2015, K.Kurashige
334 #ifdef G4VERBOSE
335  if (verboseLevel>2){
336  G4cout << "Delete " << (piter->value())->GetParticleName()
337  << " " << (piter->value()) << G4endl;
338  }
339 #endif
340  delete (piter->value());
341  }
343 }
344 
347 {
348  if (readyToUse) {
349  G4Exception("G4ParticleTable::RemoveAllParticle()",
350  "PART115", JustWarning,
351  "No effects because readyToUse is true.");
352  return;
353  }
354 
355 #ifdef G4VERBOSE
356  if (verboseLevel>1){
357  G4cout << "G4ParticleTable::RemoveAllParticles() " << G4endl;
358  }
359 #endif
360 
361  //remove all contnts in Ion Table
362  if (fIonTable!=0) {
363  fIonTable->clear();
364  }
365 
366  // clear dictionary
367  if (fDictionary) {
368  fDictionary->clear();
369  }
370 }
371 
374 {
375 
376  // check particle name
377  if ((particle == 0) || (GetKey(particle).isNull())) {
378  G4Exception("G4ParticleTable::Insert()",
379  "PART121", FatalException,
380  "Particle witnout name can not be registered.");
381 #ifdef G4VERBOSE
382  if (verboseLevel>1){
383  G4cout << "The particle[Addr:" << particle << "] has no name "<< G4endl;
384  }
385 #endif
386  return 0;
387 
388  }else {
389 
390  if (contains(particle)) {
391 #ifdef G4VERBOSE
392  if (verboseLevel>2){
393  FindParticle(particle) -> DumpTable();
394  }
395 #endif
396  G4String msg = "The particle ";
397  msg += particle->GetParticleName();
398  msg += " has already been registered in the Particle Table ";
399  G4Exception("G4ParticleTable::Insert()",
400  "PART122", FatalException,msg);
402  return particle;
403 
404  } else {
406 
407  // insert into Dictionary
408  pdic->insert( std::pair<G4String, G4ParticleDefinition*>(GetKey(particle), particle) );
409 #ifdef G4MULTITHREADED
411  { fDictionary->insert( std::pair<G4String, G4ParticleDefinition*>(GetKey(particle), particle) ); }
412 #endif
413 
415  // insert into EncodingDictionary
416  G4int code = particle->GetPDGEncoding();
417  if (code !=0 ) {
418  pedic->insert( std::pair<G4int, G4ParticleDefinition*>(code ,particle) );
419 #ifdef G4MULTITHREADED
421  { fEncodingDictionary->insert( std::pair<G4int, G4ParticleDefinition*>(code ,particle) ); }
422 #endif
423  }
424 
425  // insert it in IonTable if "nucleus"
426  if (fIonTable->IsIon(particle) ){
427  fIonTable->Insert(particle);
428  }
429 
430  // set Verbose Level same as ParticleTable
431  particle->SetVerboseLevel(verboseLevel);
432 
433 #ifdef G4VERBOSE
434  if (verboseLevel>3){
435  G4cout << "The particle "<< particle->GetParticleName()
436  << " is inserted in the ParticleTable " << G4endl;
437  }
438 #endif
439 
440  return particle;
441  }
442  }
443 }
444 
447 {
448  if(!particle) return 0;
449 #ifdef G4MULTITHREADED
452  ed << "Request of removing " << particle->GetParticleName()
453  << " is ignored as it is invoked from a worker thread.";
454  G4Exception("G4ParticleTable::Remove()","PART10117",JustWarning,ed);
455  return 0;
456  }
457 #endif
458  if (readyToUse) {
460  G4ApplicationState currentState = pStateManager->GetCurrentState();
461  if (currentState != G4State_PreInit) {
462  G4String msg = "Request of removing ";
463  msg += particle->GetParticleName();
464  msg += " has No effects other than Pre_Init";
465  G4Exception("G4ParticleTable::Remove()",
466  "PART117", JustWarning, msg);
467  return 0;
468  } else {
469 #ifdef G4VERBOSE
470  if (verboseLevel>0){
471  G4cout << particle->GetParticleName()
472  << " will be removed from the ParticleTable " << G4endl;
473  }
474 #endif
475  }
476  }
477 
478  G4PTblDictionary::iterator it = fDictionaryShadow->find(GetKey(particle));
479  if (it != fDictionaryShadow->end()) {
480  fDictionaryShadow->erase(it);
481  // remove from EncodingDictionary
482  G4int code = particle->GetPDGEncoding();
483  if (code !=0 ) {
485  }
486  } else {
487  return 0;
488  }
489 
490  // remove it from IonTable if "nucleus"
491  if (fIonTable->IsIon(particle) ){
492  fIonTable->Remove(particle);
493  }
494 
495 #ifdef G4VERBOSE
496  if (verboseLevel>3){
497  G4cout << "The particle "<< particle->GetParticleName()
498  << " is removed from the ParticleTable " << G4endl;
499  }
500 #endif
501 
502  return particle;
503 }
504 
505 
508 {
509  CheckReadiness();
510  if ( (index >=0) && (index < entries()) ) {
511  G4PTblDicIterator *piter = fIterator;
512  piter -> reset(false);
513  G4int counter = 0;
514  while( (*piter)() ){ // Loop checking, 09.08.2015, K.Kurashige
515  if ( counter == index ) return piter->value();
516  counter++;
517  }
518  }
519 #ifdef G4VERBOSE
520  if (verboseLevel>1){
521  G4cout << " G4ParticleTable::GetParticle"
522  << " invalid index (=" << index << ")" << G4endl;
523  }
524 #endif
525  return 0;
526 }
527 
530 {
531  G4ParticleDefinition* aParticle =GetParticle(index);
532  if (aParticle != 0) {
533  return aParticle->GetParticleName();
534  } else {
535  return noName;
536  }
537 }
538 
541 {
542  G4PTblDictionary::iterator it = fDictionary->find(particle_name);
543  if (it != fDictionary->end()) {
544  return (*it).second;
545  } else {
546 #ifdef G4MULTITHREADED
547  G4ParticleDefinition* ptcl = 0;
549  {
550  G4MUTEXLOCK(&G4ParticleTable::particleTableMutex());
551  G4PTblDictionary::iterator its = fDictionaryShadow->find(particle_name);
552  if(its != fDictionaryShadow->end())
553  {
554  fDictionary->insert(*its);
555  ptcl = (*its).second;
556  G4int code = ptcl->GetPDGEncoding();
557  if(code!=0) fEncodingDictionary->insert(std::pair<G4int, G4ParticleDefinition*>(code,ptcl) );
558  }
559  G4MUTEXUNLOCK(&G4ParticleTable::particleTableMutex());
560  }
561  return ptcl;
562 #else
563  return 0;
564 #endif
565  }
566 }
567 
570 {
571  CheckReadiness();
572  G4String key = GetKey(particle);
573  return FindParticle(key);
574 }
575 
578 {
579  CheckReadiness();
580  // check aPDGEncoding is valid
581  if (aPDGEncoding == 0){
582 #ifdef G4VERBOSE
583  if (verboseLevel>1){
584  G4cout << "PDGEncoding [" << aPDGEncoding << "] is not valid " << G4endl;
585  }
586 #endif
587  return 0;
588  }
589 
591  G4ParticleDefinition* particle =0;
592 
593  G4PTblEncodingDictionary::iterator it = pedic->find(aPDGEncoding );
594  if (it != pedic->end()) {
595  particle = (*it).second;
596  }
597 
598 #ifdef G4MULTITHREADED
599  if(particle == 0 && G4Threading::IsWorkerThread())
600  {
601  G4MUTEXLOCK(&G4ParticleTable::particleTableMutex());
602  G4PTblEncodingDictionary::iterator its = fEncodingDictionaryShadow->find(aPDGEncoding);
603  if(its!=fEncodingDictionaryShadow->end())
604  {
605  particle = (*its).second;
606  fEncodingDictionary->insert(*its);
607  G4String key = GetKey(particle);
608  fDictionary->insert( std::pair<G4String, G4ParticleDefinition*>(key,particle) );
609  }
610  G4MUTEXUNLOCK(&G4ParticleTable::particleTableMutex());
611  }
612 #endif
613 
614 #ifdef G4VERBOSE
615  if ((particle == 0) && (verboseLevel>1) ){
616  G4cout << "CODE:" << aPDGEncoding << " does not exist in ParticleTable " << G4endl;
617  }
618 #endif
619  return particle;
620 }
621 
623 void G4ParticleTable::DumpTable(const G4String &particle_name)
624 {
625  CheckReadiness();
626  if (( particle_name == "ALL" ) || (particle_name == "all")){
627  // dump all particles
628  G4PTblDicIterator *piter = fIterator;
629  piter -> reset();
630  while( (*piter)() ){// Loop checking, 09.08.2015, K.Kurashige
631  (piter->value())->DumpTable();
632  }
633  } else {
634  // dump only particle with name of particle_name
636  ptr = FindParticle(particle_name);
637  if ( ptr != 0) {
638  ptr->DumpTable();
639  } else {
640 #ifdef G4VERBOSE
641  if (verboseLevel>1) {
642  G4cout << " G4ParticleTable::DumpTable : "
643  << particle_name << " does not exist in ParticleTable " <<G4endl;
644  }
645 #endif
646  }
647  }
648 }
649 
651 {
652  if(!readyToUse) {
653  G4String msg;
654  msg = "Illegal use of G4ParticleTable : ";
655  msg += " Access to G4ParticleTable for finding a particle or equivalent\n";
656  msg += "operation occurs before G4VUserPhysicsList is instantiated and\n";
657  msg += "assigned to G4RunManager. Such an access is prohibited by\n";
658  msg += "Geant4 version 8.0. To fix this problem, please make sure that\n";
659  msg += "your main() instantiates G4VUserPhysicsList and set it to\n";
660  msg += "G4RunManager before instantiating other user classes such as\n";
661  msg += "G4VUserPrimaryParticleGeneratorAction.";
662  G4Exception("G4ParticleTable::CheckReadiness()",
663  "PART002",FatalException,msg);
664  }
665 }
666 
667 
668 
670 {
671  return fIonTable;
672 }
673 
675 {
676  return fDictionary;
677 }
678 
680 {
681  return fIterator;
682 }
683 
685 {
686  return fEncodingDictionary;
687 }
688 
689 G4bool G4ParticleTable::contains(const G4String& particle_name) const
690 {
691  G4PTblDictionary::iterator it = fDictionaryShadow->find(particle_name);
692  return (it != fDictionaryShadow->end());
693 }
694 
696 {
697  return fDictionary->size();
698 }
699 
701 {
702  return fDictionary->size();
703 }
704 
705 
G4ParticleDefinition * FindParticle(G4int PDGEncoding)
void DumpTable(const G4String &particle_name="ALL")
G4int size() const
static G4bool IsIon(const G4ParticleDefinition *)
Definition: G4IonTable.cc:1148
void DestroyWorkerG4ParticleTable()
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
static G4ParticleTable * GetParticleTable()
G4bool contains(const G4ParticleDefinition *particle) const
static G4IonTable * fIonTable
#define G4endl
Definition: G4ios.hh:61
static G4ParticleMessenger * fParticleMessengerShadow
G4UImessenger * CreateMessenger()
const G4String & GetParticleName() const
const G4String & GetKey(const G4ParticleDefinition *particle) const
G4IonTable * GetIonTable() const
const G4String noName
#define G4ThreadLocal
Definition: tls.hh:69
const G4PTblDictionary * GetDictionary() const
G4ParticleTableIterator< G4int, G4ParticleDefinition * >::Map G4PTblEncodingDictionary
G4PTblDicIterator * GetIterator() const
void reset(G4bool ifSkipIon=true)
static G4PTblEncodingDictionary * fEncodingDictionaryShadow
static G4ParticleTable * fgParticleTable
G4int entries() const
G4ParticleTableIterator< G4String, G4ParticleDefinition * >::Map G4PTblDictionary
G4ParticleTableIterator< G4String, G4ParticleDefinition * > G4PTblDicIterator
G4ParticleDefinition * GetParticle(G4int index) const
G4ParticleTable & operator=(const G4ParticleTable &)
static G4PTblDictionary * fDictionaryShadow
bool G4bool
Definition: G4Types.hh:79
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:88
static G4ThreadLocal G4ParticleMessenger * fParticleMessenger
G4bool IsWorkerThread()
Definition: G4Threading.cc:129
void DestroyWorkerG4IonTable()
Definition: G4IonTable.cc:218
void clear()
Definition: G4IonTable.cc:1346
#define G4MUTEXUNLOCK(mutex)
Definition: G4Threading.hh:234
G4ParticleDefinition * Remove(G4ParticleDefinition *particle)
Definition: inftrees.h:24
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
int G4int
Definition: G4Types.hh:78
void WorkerG4IonTable()
Definition: G4IonTable.cc:157
G4ApplicationState GetCurrentState() const
void CheckReadiness() const
static G4PTblDicIterator * fIteratorShadow
void WorkerG4ParticleTable()
G4ParticleDefinition * Insert(G4ParticleDefinition *particle)
void Remove(const G4ParticleDefinition *particle)
Definition: G4IonTable.cc:1405
static G4ThreadLocal G4PTblDicIterator * fIterator
#define G4MUTEXLOCK(mutex)
Definition: G4Threading.hh:233
const G4String & GetParticleName(G4int index) const
G4GLOB_DLL std::ostream G4cout
G4ApplicationState
virtual ~G4ParticleTable()
void SetVerboseLevel(G4int value)
static G4ThreadLocal G4PTblEncodingDictionary * fEncodingDictionary
void Insert(const G4ParticleDefinition *particle)
Definition: G4IonTable.cc:1364
static G4ThreadLocal G4PTblDictionary * fDictionary
vec_iX clear()
static G4StateManager * GetStateManager()
const G4PTblEncodingDictionary * GetEncodingDictionary() const
std::mutex G4Mutex
Definition: G4Threading.hh:84