Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4ParticleTable class implementation 26 // G4ParticleTable class implementation 27 // 27 // 28 // Authors: G.Cosmo, 2 December 1995 - Design, 28 // Authors: G.Cosmo, 2 December 1995 - Design, based on object model 29 // H.Kurashige, 27 June 1996 - First 29 // H.Kurashige, 27 June 1996 - First implementation 30 // History: 30 // History: 31 // - 14 Nov 1997, H.Kurashige - Added messenge 31 // - 14 Nov 1997, H.Kurashige - Added messenger 32 // - 24 Sep 1998, H.Kurashige - Added dictiona 32 // - 24 Sep 1998, H.Kurashige - Added dictionary for encoding 33 // - 28 Oct 1999, H.Kurashige - Migration to S 33 // - 28 Oct 1999, H.Kurashige - Migration to STL maps 34 // - 15 Sep 2017, K.L.Genser - Added support f 34 // - 15 Sep 2017, K.L.Genser - Added support for MuonicAtom 35 // ------------------------------------------- 35 // -------------------------------------------------------------------- 36 36 >> 37 #include "globals.hh" >> 38 #include "G4ios.hh" 37 #include "G4ParticleTable.hh" 39 #include "G4ParticleTable.hh" 38 << 40 #include "G4UImessenger.hh" 39 #include "G4IonTable.hh" << 40 #include "G4ParticleMessenger.hh" 41 #include "G4ParticleMessenger.hh" >> 42 #include "G4IonTable.hh" 41 #include "G4StateManager.hh" 43 #include "G4StateManager.hh" 42 #include "G4UImessenger.hh" << 43 #include "G4ios.hh" << 44 #include "globals.hh" << 45 44 46 // These fields should be thread local or thre 45 // These fields should be thread local or thread private. For a singleton 47 // class, we can change any member field as st 46 // class, we can change any member field as static without any problem 48 // because there is only one instance. Then we << 47 // because there is only one instance. Then we are allowed to add 49 // "G4ThreadLocal" 48 // "G4ThreadLocal" 50 // 49 // 51 G4ThreadLocal G4ParticleTable::G4PTblDictionar << 50 G4ThreadLocal G4ParticleTable::G4PTblDictionary* 52 G4ThreadLocal G4ParticleTable::G4PTblDicIterat << 51 G4ParticleTable::fDictionary = nullptr; 53 G4ThreadLocal G4ParticleTable::G4PTblEncodingD << 52 G4ThreadLocal G4ParticleTable::G4PTblDicIterator* 54 nullptr; << 53 G4ParticleTable::fIterator = nullptr; >> 54 G4ThreadLocal G4ParticleTable::G4PTblEncodingDictionary* >> 55 G4ParticleTable::fEncodingDictionary = nullptr; 55 56 56 // These shadow pointers are used by each work 57 // These shadow pointers are used by each worker thread to copy the content 57 // from the master thread 58 // from the master thread 58 // 59 // 59 G4ParticleTable::G4PTblDictionary* G4ParticleT << 60 G4ParticleTable::G4PTblDictionary* 60 G4ParticleTable::G4PTblDicIterator* G4Particle << 61 G4ParticleTable::fDictionaryShadow = nullptr; 61 G4ParticleTable::G4PTblEncodingDictionary* G4P << 62 G4ParticleTable::G4PTblDicIterator* >> 63 G4ParticleTable::fIteratorShadow = nullptr; >> 64 G4ParticleTable::G4PTblEncodingDictionary* >> 65 G4ParticleTable::fEncodingDictionaryShadow = nullptr; 62 66 63 // Static class variable: pointer to single in 67 // Static class variable: pointer to single instance of class 64 // 68 // 65 G4ParticleTable* G4ParticleTable::fgParticleTa 69 G4ParticleTable* G4ParticleTable::fgParticleTable = nullptr; 66 70 67 #ifdef G4MULTITHREADED 71 #ifdef G4MULTITHREADED 68 // Lock for particle table accesses. 72 // Lock for particle table accesses. 69 // 73 // 70 G4Mutex& G4ParticleTable::particleTableMutex() 74 G4Mutex& G4ParticleTable::particleTableMutex() 71 { 75 { 72 static G4Mutex _instance = G4MUTEX_INITIALIZ 76 static G4Mutex _instance = G4MUTEX_INITIALIZER; 73 return _instance; 77 return _instance; 74 } 78 } 75 G4int& G4ParticleTable::lockCount() 79 G4int& G4ParticleTable::lockCount() 76 { 80 { 77 static G4int _instance = 0; 81 static G4int _instance = 0; 78 return _instance; 82 return _instance; 79 } 83 } 80 #endif << 84 #endif 81 85 >> 86 // -------------------------------------------------------------------- 82 G4ParticleTable* G4ParticleTable::GetParticleT 87 G4ParticleTable* G4ParticleTable::GetParticleTable() 83 { 88 { 84 if (fgParticleTable == nullptr) { << 89 if ( fgParticleTable == nullptr ) >> 90 { 85 static G4ParticleTable theParticleTable; 91 static G4ParticleTable theParticleTable; 86 fgParticleTable = &theParticleTable; 92 fgParticleTable = &theParticleTable; 87 } 93 } 88 94 89 // Here we initialize all thread private dat 95 // Here we initialize all thread private data members. 90 // 96 // 91 if (fDictionary == nullptr) fgParticleTable- 97 if (fDictionary == nullptr) fgParticleTable->WorkerG4ParticleTable(); 92 98 93 return fgParticleTable; 99 return fgParticleTable; 94 } 100 } 95 101 >> 102 // -------------------------------------------------------------------- 96 G4ParticleTable::G4ParticleTable() 103 G4ParticleTable::G4ParticleTable() 97 { 104 { 98 fDictionary = new G4PTblDictionary(); 105 fDictionary = new G4PTblDictionary(); 99 106 100 // Set up the shadow pointer used by worker 107 // Set up the shadow pointer used by worker threads 101 // 108 // 102 if (fDictionaryShadow == nullptr) { << 109 if (fDictionaryShadow == nullptr) >> 110 { 103 fDictionaryShadow = fDictionary; 111 fDictionaryShadow = fDictionary; 104 } 112 } 105 113 106 fIterator = new G4PTblDicIterator(*fDictiona << 114 fIterator = new G4PTblDicIterator( *fDictionary ); 107 115 108 // Set up the shadow pointer used by worker 116 // Set up the shadow pointer used by worker threads 109 // 117 // 110 if (fIteratorShadow == nullptr) { << 118 if (fIteratorShadow == nullptr) >> 119 { 111 fIteratorShadow = fIterator; 120 fIteratorShadow = fIterator; 112 } 121 } 113 << 122 114 fEncodingDictionary = new G4PTblEncodingDict 123 fEncodingDictionary = new G4PTblEncodingDictionary(); 115 // Set up the shadow pointer used by worker 124 // Set up the shadow pointer used by worker threads 116 // 125 // 117 if (fEncodingDictionaryShadow == nullptr) { << 126 if (fEncodingDictionaryShadow == nullptr) >> 127 { 118 fEncodingDictionaryShadow = fEncodingDicti 128 fEncodingDictionaryShadow = fEncodingDictionary; 119 } 129 } 120 130 121 // Ion Table 131 // Ion Table 122 // 132 // 123 fIonTable = new G4IonTable(); 133 fIonTable = new G4IonTable(); 124 fParticleMessenger = nullptr; 134 fParticleMessenger = nullptr; 125 } 135 } 126 136 >> 137 // -------------------------------------------------------------------- 127 // This method is similar to the constructor. 138 // This method is similar to the constructor. It is used by each worker 128 // thread to achieve the partial effect as tha 139 // thread to achieve the partial effect as that of the master thread. 129 // Here we initialize all thread private data 140 // Here we initialize all thread private data members >> 141 // 130 void G4ParticleTable::WorkerG4ParticleTable() 142 void G4ParticleTable::WorkerG4ParticleTable() 131 { 143 { 132 // The iterator for the shadow particle tabl 144 // The iterator for the shadow particle table is not sharable. 133 // 145 // 134 #ifdef G4MULTITHREADED 146 #ifdef G4MULTITHREADED 135 G4MUTEXLOCK(&G4ParticleTable::particleTableM 147 G4MUTEXLOCK(&G4ParticleTable::particleTableMutex()); 136 G4ParticleTable::lockCount()++; 148 G4ParticleTable::lockCount()++; 137 #endif 149 #endif 138 if (fDictionary == nullptr) { << 150 if(fDictionary == nullptr) 139 fDictionary = new G4PTblDictionary(); << 151 { >> 152 fDictionary = new G4PTblDictionary(); 140 } 153 } 141 else { << 154 else 142 fDictionary->clear(); << 155 { >> 156 fDictionary->clear(); 143 } 157 } 144 158 145 if (fEncodingDictionary == nullptr) { << 159 if(fEncodingDictionary == nullptr) 146 fEncodingDictionary = new G4PTblEncodingDi << 160 { >> 161 fEncodingDictionary = new G4PTblEncodingDictionary(); 147 } 162 } 148 else { << 163 else 149 fEncodingDictionary->clear(); << 164 { >> 165 fEncodingDictionary->clear(); 150 } 166 } 151 167 152 fIteratorShadow->reset(false); 168 fIteratorShadow->reset(false); 153 while ((*fIteratorShadow)()) // Loop checki << 169 while( (*fIteratorShadow)() ) // Loop checking, 09.08.2015, K.Kurashige 154 { 170 { 155 G4ParticleDefinition* particle = fIterator 171 G4ParticleDefinition* particle = fIteratorShadow->value(); 156 fDictionary->insert(std::pair<G4String, G4 << 172 fDictionary->insert( std::pair<G4String, >> 173 G4ParticleDefinition*>(GetKey(particle), particle) ); 157 G4int code = particle->GetPDGEncoding(); 174 G4int code = particle->GetPDGEncoding(); 158 if (code != 0) { << 175 if (code !=0 ) 159 fEncodingDictionary->insert(std::pair<G4 << 176 { >> 177 fEncodingDictionary->insert( std::pair<G4int, >> 178 G4ParticleDefinition*>(code ,particle) ); 160 } 179 } 161 } << 180 } 162 fIterator = new G4PTblDicIterator(*fDictiona << 181 fIterator = new G4PTblDicIterator( *fDictionary ); 163 182 164 #ifdef G4MULTITHREADED 183 #ifdef G4MULTITHREADED 165 G4MUTEXUNLOCK(&G4ParticleTable::particleTabl 184 G4MUTEXUNLOCK(&G4ParticleTable::particleTableMutex()); 166 #endif 185 #endif 167 186 168 fIonTable->WorkerG4IonTable(); 187 fIonTable->WorkerG4IonTable(); 169 } 188 } 170 189 >> 190 // -------------------------------------------------------------------- 171 G4ParticleTable::~G4ParticleTable() 191 G4ParticleTable::~G4ParticleTable() 172 { 192 { 173 readyToUse = false; 193 readyToUse = false; 174 << 194 175 // remove all items from G4ParticleTable 195 // remove all items from G4ParticleTable 176 RemoveAllParticles(); 196 RemoveAllParticles(); 177 197 178 // delete Ion Table << 198 // delete Ion Table 179 delete fIonTable; << 199 if (fIonTable != nullptr) delete fIonTable; 180 fIonTable = nullptr; 200 fIonTable = nullptr; 181 201 182 // delete dictionary for encoding 202 // delete dictionary for encoding 183 if (fEncodingDictionary != nullptr) { << 203 if (fEncodingDictionary != nullptr) >> 204 { 184 fEncodingDictionary->clear(); 205 fEncodingDictionary->clear(); 185 delete fEncodingDictionary; 206 delete fEncodingDictionary; 186 fEncodingDictionary = nullptr; 207 fEncodingDictionary = nullptr; 187 } 208 } 188 209 189 if (fDictionary != nullptr) { << 210 if( fDictionary != nullptr) 190 delete fIterator; << 211 { >> 212 if (fIterator != nullptr ) delete fIterator; 191 fIterator = nullptr; 213 fIterator = nullptr; 192 214 193 fDictionary->clear(); 215 fDictionary->clear(); 194 delete fDictionary; 216 delete fDictionary; 195 fDictionary = nullptr; << 217 fDictionary =nullptr; 196 } 218 } 197 219 198 delete fParticleMessenger; << 220 if ( fParticleMessenger != nullptr) delete fParticleMessenger; 199 fParticleMessenger = nullptr; 221 fParticleMessenger = nullptr; 200 222 201 fgParticleTable = nullptr; 223 fgParticleTable = nullptr; 202 224 203 G4ParticleDefinition::Clean(); // Delete su 225 G4ParticleDefinition::Clean(); // Delete sub-instance static data 204 } 226 } 205 227 >> 228 // -------------------------------------------------------------------- 206 void G4ParticleTable::DestroyWorkerG4ParticleT 229 void G4ParticleTable::DestroyWorkerG4ParticleTable() 207 { 230 { 208 // delete Ion Table in worker thread 231 // delete Ion Table in worker thread 209 if (fIonTable != nullptr) fIonTable->Destroy 232 if (fIonTable != nullptr) fIonTable->DestroyWorkerG4IonTable(); 210 233 211 // delete dictionary for encoding 234 // delete dictionary for encoding 212 if (fEncodingDictionary != nullptr) { << 235 if (fEncodingDictionary != nullptr) >> 236 { 213 fEncodingDictionary->clear(); 237 fEncodingDictionary->clear(); 214 delete fEncodingDictionary; 238 delete fEncodingDictionary; 215 fEncodingDictionary = nullptr; 239 fEncodingDictionary = nullptr; 216 } 240 } 217 241 218 if (fDictionary != nullptr) { << 242 if( fDictionary != nullptr) 219 delete fIterator; << 243 { >> 244 if (fIterator != nullptr ) delete fIterator; 220 fIterator = nullptr; 245 fIterator = nullptr; 221 246 222 fDictionary->clear(); 247 fDictionary->clear(); 223 delete fDictionary; 248 delete fDictionary; 224 fDictionary = nullptr; 249 fDictionary = nullptr; 225 } 250 } 226 } 251 } 227 252 >> 253 // -------------------------------------------------------------------- 228 G4UImessenger* G4ParticleTable::CreateMessenge 254 G4UImessenger* G4ParticleTable::CreateMessenger() 229 { 255 { 230 if (fParticleMessenger == nullptr) { << 256 if (fParticleMessenger == nullptr) >> 257 { 231 // UI messenger 258 // UI messenger 232 fParticleMessenger = new G4ParticleMesseng 259 fParticleMessenger = new G4ParticleMessenger(this); 233 } 260 } 234 return fParticleMessenger; 261 return fParticleMessenger; 235 } 262 } 236 263 >> 264 // -------------------------------------------------------------------- 237 void G4ParticleTable::DeleteAllParticles() 265 void G4ParticleTable::DeleteAllParticles() 238 { 266 { 239 // set readyToUse false << 267 //set readyToUse false 240 readyToUse = false; 268 readyToUse = false; 241 269 242 #ifdef G4VERBOSE 270 #ifdef G4VERBOSE 243 if (verboseLevel > 1) { << 271 if (verboseLevel>1) >> 272 { 244 G4cout << "G4ParticleTable::DeleteAllParti 273 G4cout << "G4ParticleTable::DeleteAllParticles() " << G4endl; 245 } 274 } 246 #endif 275 #endif 247 276 248 // delete all particles << 277 // delete all particles 249 G4PTblDicIterator* piter = fIterator; << 278 G4PTblDicIterator* piter = fIterator; 250 piter->reset(false); 279 piter->reset(false); 251 while ((*piter)()) // Loop checking, 09.08. << 280 while( (*piter)() ) // Loop checking, 09.08.2015, K.Kurashige 252 { 281 { 253 #ifdef G4VERBOSE 282 #ifdef G4VERBOSE 254 if (verboseLevel > 2) { << 283 if (verboseLevel>2) 255 G4cout << "Delete " << (piter->value())- << 284 { 256 << G4endl; << 285 G4cout << "Delete " << (piter->value())->GetParticleName() >> 286 << " " << (piter->value()) << G4endl; 257 } 287 } 258 #endif 288 #endif 259 delete (piter->value()); 289 delete (piter->value()); 260 } 290 } 261 RemoveAllParticles(); 291 RemoveAllParticles(); 262 } 292 } 263 293 >> 294 // -------------------------------------------------------------------- 264 void G4ParticleTable::RemoveAllParticles() 295 void G4ParticleTable::RemoveAllParticles() 265 { 296 { 266 if (readyToUse) { << 297 if (readyToUse) 267 G4Exception("G4ParticleTable::RemoveAllPar << 298 { 268 "No effects because readyToUse << 299 G4Exception("G4ParticleTable::RemoveAllParticle()", >> 300 "PART115", JustWarning, >> 301 "No effects because readyToUse is true."); 269 return; 302 return; 270 } 303 } 271 << 304 272 #ifdef G4VERBOSE 305 #ifdef G4VERBOSE 273 if (verboseLevel > 1) { << 306 if (verboseLevel>1) >> 307 { 274 G4cout << "G4ParticleTable::RemoveAllParti 308 G4cout << "G4ParticleTable::RemoveAllParticles() " << G4endl; 275 } 309 } 276 #endif 310 #endif 277 311 278 // remove all contents in Ion Table 312 // remove all contents in Ion Table 279 if (fIonTable != nullptr) { << 313 if (fIonTable != nullptr) >> 314 { 280 fIonTable->clear(); 315 fIonTable->clear(); 281 } 316 } 282 317 283 // clear dictionary 318 // clear dictionary 284 if (fDictionary != nullptr) { << 319 if (fDictionary != nullptr) >> 320 { 285 fDictionary->clear(); 321 fDictionary->clear(); 286 } 322 } 287 } 323 } 288 324 >> 325 // -------------------------------------------------------------------- 289 G4ParticleDefinition* G4ParticleTable::Insert( 326 G4ParticleDefinition* G4ParticleTable::Insert(G4ParticleDefinition* particle) 290 { 327 { 291 // check particle name 328 // check particle name 292 if ((particle == nullptr) || (GetKey(particl << 329 if ((particle == nullptr) || (GetKey(particle).empty())) 293 G4Exception("G4ParticleTable::Insert()", " << 330 { 294 "Particle witnout name can not << 331 G4Exception("G4ParticleTable::Insert()", >> 332 "PART121", FatalException, >> 333 "Particle witnout name can not be registered."); 295 #ifdef G4VERBOSE 334 #ifdef G4VERBOSE 296 if (verboseLevel > 1) { << 335 if (verboseLevel>1) 297 G4cout << "The particle[Addr:" << partic << 336 { >> 337 G4cout << "The particle[Addr:" << particle << "] has no name "<< G4endl; 298 } 338 } 299 #endif 339 #endif 300 return nullptr; 340 return nullptr; 301 } 341 } 302 << 342 else 303 if (contains(particle)) { << 343 { 304 #ifdef G4VERBOSE << 344 if (contains(particle)) 305 if (verboseLevel > 2) { << 345 { 306 FindParticle(particle)->DumpTable(); << 346 #ifdef G4VERBOSE 307 } << 347 if (verboseLevel>2) >> 348 { >> 349 FindParticle(particle) -> DumpTable(); >> 350 } 308 #endif 351 #endif 309 G4String msg = "The particle "; << 352 G4String msg = "The particle "; 310 msg += particle->GetParticleName(); << 353 msg += particle->GetParticleName(); 311 msg += " has already been registered in t << 354 msg += " has already been registered in the Particle Table "; 312 G4Exception("G4ParticleTable::Insert()", " << 355 G4Exception("G4ParticleTable::Insert()", "PART122", FatalException, msg); 313 << 314 return particle; << 315 } << 316 356 317 G4PTblDictionary* pdic = fDictionaryShadow; << 357 return particle; >> 358 } >> 359 else >> 360 { >> 361 G4PTblDictionary* pdic = fDictionaryShadow; 318 362 319 // insert into Dictionary << 363 // insert into Dictionary 320 pdic->insert(std::pair<G4String, G4ParticleD << 364 pdic->insert( std::pair<G4String, >> 365 G4ParticleDefinition*>(GetKey(particle), particle) ); 321 #ifdef G4MULTITHREADED 366 #ifdef G4MULTITHREADED 322 if (G4Threading::IsWorkerThread()) { << 367 if(G4Threading::IsWorkerThread()) 323 fDictionary->insert(std::pair<G4String, G4 << 368 { 324 } << 369 fDictionary->insert( std::pair<G4String, >> 370 G4ParticleDefinition*>(GetKey(particle), particle) ); >> 371 } 325 #endif 372 #endif 326 373 327 G4PTblEncodingDictionary* pedic = fEncodingD << 374 G4PTblEncodingDictionary* pedic = fEncodingDictionaryShadow; 328 375 329 // insert into EncodingDictionary << 376 // insert into EncodingDictionary 330 G4int code = particle->GetPDGEncoding(); << 377 G4int code = particle->GetPDGEncoding(); 331 if (code != 0) { << 378 if (code != 0 ) 332 pedic->insert(std::pair<G4int, G4ParticleD << 379 { >> 380 pedic->insert( std::pair<G4int, >> 381 G4ParticleDefinition*>(code ,particle) ); 333 #ifdef G4MULTITHREADED 382 #ifdef G4MULTITHREADED 334 if (G4Threading::IsWorkerThread()) { << 383 if(G4Threading::IsWorkerThread()) 335 fEncodingDictionary->insert(std::pair<G4 << 384 { 336 } << 385 fEncodingDictionary->insert( std::pair<G4int, 337 #endif << 386 G4ParticleDefinition*>(code ,particle) ); 338 } << 387 } 339 << 388 #endif 340 // insert it in IonTable if "nucleus" << 389 } 341 if (fIonTable->IsIon(particle)) { << 390 342 fIonTable->Insert(particle); << 391 // insert it in IonTable if "nucleus" 343 } << 392 if (fIonTable->IsIon(particle) ) >> 393 { >> 394 fIonTable->Insert(particle); >> 395 } 344 396 345 // set Verbose Level same as ParticleTable << 397 // set Verbose Level same as ParticleTable 346 particle->SetVerboseLevel(verboseLevel); << 398 particle->SetVerboseLevel(verboseLevel); 347 399 348 #ifdef G4VERBOSE 400 #ifdef G4VERBOSE 349 if (verboseLevel > 3) { << 401 if (verboseLevel>3) 350 G4cout << "The particle " << particle->Get << 402 { 351 << G4endl; << 403 G4cout << "The particle "<< particle->GetParticleName() 352 } << 404 << " is inserted in the ParticleTable " << G4endl; >> 405 } 353 #endif 406 #endif 354 return particle; << 407 return particle; >> 408 } >> 409 } 355 } 410 } 356 411 >> 412 // -------------------------------------------------------------------- 357 G4ParticleDefinition* G4ParticleTable::Remove( 413 G4ParticleDefinition* G4ParticleTable::Remove(G4ParticleDefinition* particle) 358 { 414 { 359 if (particle == nullptr) return nullptr; << 415 if( particle == nullptr) return nullptr; 360 #ifdef G4MULTITHREADED 416 #ifdef G4MULTITHREADED 361 if (G4Threading::IsWorkerThread()) { << 417 if(G4Threading::IsWorkerThread()) >> 418 { 362 G4ExceptionDescription ed; 419 G4ExceptionDescription ed; 363 ed << "Request of removing " << particle-> 420 ed << "Request of removing " << particle->GetParticleName() 364 << " is ignored as it is invoked from a 421 << " is ignored as it is invoked from a worker thread."; 365 G4Exception("G4ParticleTable::Remove()", " 422 G4Exception("G4ParticleTable::Remove()", "PART10117", JustWarning, ed); 366 return nullptr; 423 return nullptr; 367 } 424 } 368 #endif 425 #endif 369 if (readyToUse) { << 426 if (readyToUse) >> 427 { 370 G4StateManager* pStateManager = G4StateMan 428 G4StateManager* pStateManager = G4StateManager::GetStateManager(); 371 G4ApplicationState currentState = pStateMa 429 G4ApplicationState currentState = pStateManager->GetCurrentState(); 372 if (currentState != G4State_PreInit) { << 430 if (currentState != G4State_PreInit) >> 431 { 373 G4String msg = "Request of removing "; 432 G4String msg = "Request of removing "; 374 msg += particle->GetParticleName(); << 433 msg += particle->GetParticleName(); 375 msg += " has No effects other than Pre_I 434 msg += " has No effects other than Pre_Init"; 376 G4Exception("G4ParticleTable::Remove()", 435 G4Exception("G4ParticleTable::Remove()", "PART117", JustWarning, msg); 377 return nullptr; 436 return nullptr; 378 } 437 } 379 << 438 else >> 439 { 380 #ifdef G4VERBOSE 440 #ifdef G4VERBOSE 381 if (verboseLevel > 0) { << 441 if (verboseLevel>0) 382 G4cout << particle->GetParticleName() << << 442 { 383 } << 443 G4cout << particle->GetParticleName() >> 444 << " will be removed from the ParticleTable " << G4endl; >> 445 } 384 #endif 446 #endif >> 447 } 385 } 448 } 386 << 449 387 auto it = fDictionaryShadow->find(GetKey(par << 450 G4PTblDictionary::iterator it = fDictionaryShadow->find(GetKey(particle)); 388 if (it != fDictionaryShadow->end()) { << 451 if (it != fDictionaryShadow->end()) >> 452 { 389 fDictionaryShadow->erase(it); 453 fDictionaryShadow->erase(it); 390 // remove from EncodingDictionary 454 // remove from EncodingDictionary 391 G4int code = particle->GetPDGEncoding(); 455 G4int code = particle->GetPDGEncoding(); 392 if (code != 0) { << 456 if (code != 0 ) 393 fEncodingDictionaryShadow->erase(fEncodi << 457 { >> 458 fEncodingDictionaryShadow->erase(fEncodingDictionaryShadow->find(code)); 394 } 459 } 395 } 460 } 396 else { << 461 else >> 462 { 397 return nullptr; 463 return nullptr; 398 } 464 } 399 465 400 // remove it from IonTable if "nucleus" 466 // remove it from IonTable if "nucleus" 401 if (fIonTable->IsIon(particle)) { << 467 if (fIonTable->IsIon(particle) ) >> 468 { 402 fIonTable->Remove(particle); 469 fIonTable->Remove(particle); 403 } 470 } 404 << 471 405 #ifdef G4VERBOSE 472 #ifdef G4VERBOSE 406 if (verboseLevel > 3) { << 473 if (verboseLevel>3) 407 G4cout << "The particle " << particle->Get << 474 { >> 475 G4cout << "The particle "<< particle->GetParticleName() 408 << " is removed from the ParticleTa 476 << " is removed from the ParticleTable " << G4endl; 409 } 477 } 410 #endif 478 #endif 411 479 412 return particle; 480 return particle; 413 } 481 } 414 482 >> 483 // -------------------------------------------------------------------- 415 G4ParticleDefinition* G4ParticleTable::GetPart 484 G4ParticleDefinition* G4ParticleTable::GetParticle(G4int index) const 416 { 485 { 417 CheckReadiness(); 486 CheckReadiness(); 418 if ((index >= 0) && (index < entries())) { << 487 if ( (index >=0) && (index < entries()) ) 419 G4PTblDicIterator* piter = fIterator; << 488 { 420 piter->reset(false); << 489 G4PTblDicIterator* piter = fIterator; >> 490 piter -> reset(false); 421 G4int counter = 0; 491 G4int counter = 0; 422 while ((*piter)()) // Loop checking, 09.0 << 492 while( (*piter)() ) // Loop checking, 09.08.2015, K.Kurashige 423 { 493 { 424 if (counter == index) return piter->valu << 494 if ( counter == index ) return piter->value(); 425 ++counter; 495 ++counter; 426 } 496 } 427 } << 497 } 428 #ifdef G4VERBOSE 498 #ifdef G4VERBOSE 429 if (verboseLevel > 1) { << 499 if (verboseLevel>1) >> 500 { 430 G4cout << " G4ParticleTable::GetParticle" 501 G4cout << " G4ParticleTable::GetParticle" 431 << " invalid index (=" << index << 502 << " invalid index (=" << index << ")" << G4endl; 432 } 503 } 433 #endif 504 #endif 434 return nullptr; 505 return nullptr; 435 } 506 } 436 507 >> 508 // -------------------------------------------------------------------- 437 const G4String& G4ParticleTable::GetParticleNa 509 const G4String& G4ParticleTable::GetParticleName(G4int index) const 438 { 510 { 439 G4ParticleDefinition* aParticle = GetParticl << 511 G4ParticleDefinition* aParticle =GetParticle(index); 440 if (aParticle != nullptr) { << 512 if (aParticle != nullptr) >> 513 { 441 return aParticle->GetParticleName(); 514 return aParticle->GetParticleName(); 442 } 515 } 443 << 516 else 444 return noName; << 517 { >> 518 return noName; >> 519 } 445 } 520 } 446 521 447 G4ParticleDefinition* G4ParticleTable::FindPar << 522 // -------------------------------------------------------------------- >> 523 G4ParticleDefinition* >> 524 G4ParticleTable::FindParticle(const G4String& particle_name) 448 { 525 { 449 auto it = fDictionary->find(particle_name); << 526 G4PTblDictionary::iterator it = fDictionary->find(particle_name); 450 if (it != fDictionary->end()) { << 527 if (it != fDictionary->end()) >> 528 { 451 return (*it).second; 529 return (*it).second; 452 } 530 } 453 << 531 else >> 532 { 454 #ifdef G4MULTITHREADED 533 #ifdef G4MULTITHREADED 455 G4ParticleDefinition* ptcl = nullptr; << 534 G4ParticleDefinition* ptcl = nullptr; 456 if (G4Threading::IsWorkerThread()) { << 535 if(G4Threading::IsWorkerThread()) 457 G4MUTEXLOCK(&G4ParticleTable::particleTabl << 536 { 458 auto its = fDictionaryShadow->find(particl << 537 G4MUTEXLOCK(&G4ParticleTable::particleTableMutex()); 459 if (its != fDictionaryShadow->end()) { << 538 G4PTblDictionary::iterator its = fDictionaryShadow->find(particle_name); 460 fDictionary->insert(*its); << 539 if(its != fDictionaryShadow->end()) 461 ptcl = (*its).second; << 540 { 462 G4int code = ptcl->GetPDGEncoding(); << 541 fDictionary->insert(*its); 463 if (code != 0) << 542 ptcl = (*its).second; 464 fEncodingDictionary->insert(std::pair< << 543 G4int code = ptcl->GetPDGEncoding(); >> 544 if(code!=0) fEncodingDictionary->insert(std::pair<G4int, >> 545 G4ParticleDefinition*>(code,ptcl) ); >> 546 } >> 547 G4MUTEXUNLOCK(&G4ParticleTable::particleTableMutex()); 465 } 548 } 466 G4MUTEXUNLOCK(&G4ParticleTable::particleTa << 549 return ptcl; 467 } << 468 return ptcl; << 469 #else 550 #else 470 return nullptr; << 551 return nullptr; 471 #endif 552 #endif >> 553 } 472 } 554 } 473 555 >> 556 // -------------------------------------------------------------------- 474 void G4ParticleTable::SelectParticle(const G4S 557 void G4ParticleTable::SelectParticle(const G4String& name) 475 { 558 { 476 if (name != selectedName) { << 559 if(name != selectedName) >> 560 { 477 const G4ParticleDefinition* part = FindPar 561 const G4ParticleDefinition* part = FindParticle(name); 478 if (part != nullptr) { << 562 if(part) >> 563 { 479 #ifdef G4MULTITHREADED 564 #ifdef G4MULTITHREADED 480 G4MUTEXLOCK(&G4ParticleTable::particleTa 565 G4MUTEXLOCK(&G4ParticleTable::particleTableMutex()); 481 #endif 566 #endif 482 selectedParticle = part; 567 selectedParticle = part; 483 selectedName = name; 568 selectedName = name; 484 #ifdef G4MULTITHREADED 569 #ifdef G4MULTITHREADED 485 G4MUTEXUNLOCK(&G4ParticleTable::particle 570 G4MUTEXUNLOCK(&G4ParticleTable::particleTableMutex()); 486 #endif 571 #endif 487 } 572 } 488 } 573 } 489 } 574 } 490 575 491 G4ParticleDefinition* G4ParticleTable::FindPar << 576 // -------------------------------------------------------------------- >> 577 G4ParticleDefinition* >> 578 G4ParticleTable::FindParticle(const G4ParticleDefinition* particle ) 492 { 579 { 493 CheckReadiness(); 580 CheckReadiness(); 494 G4String key = GetKey(particle); 581 G4String key = GetKey(particle); 495 return FindParticle(key); 582 return FindParticle(key); 496 } 583 } 497 584 498 G4ParticleDefinition* G4ParticleTable::FindPar << 585 // -------------------------------------------------------------------- >> 586 G4ParticleDefinition* G4ParticleTable::FindParticle(G4int aPDGEncoding ) 499 { 587 { 500 CheckReadiness(); 588 CheckReadiness(); 501 // check aPDGEncoding is valid 589 // check aPDGEncoding is valid 502 if (aPDGEncoding == 0) { << 590 if (aPDGEncoding == 0) >> 591 { 503 #ifdef G4VERBOSE 592 #ifdef G4VERBOSE 504 if (verboseLevel > 1) { << 593 if (verboseLevel>1) 505 G4cout << "PDGEncoding [" << aPDGEncodi << 594 { >> 595 G4cout << "PDGEncoding [" << aPDGEncoding << "] is not valid " >> 596 << G4endl; 506 } 597 } 507 #endif 598 #endif 508 return nullptr; << 599 return nullptr; 509 } << 600 } 510 << 601 511 G4PTblEncodingDictionary* pedic = fEncodingD << 602 G4PTblEncodingDictionary* pedic = fEncodingDictionary; 512 G4ParticleDefinition* particle = nullptr; << 603 G4ParticleDefinition* particle = nullptr; 513 604 514 if (pedic != nullptr) { << 605 if (pedic) 515 auto it = pedic->find(aPDGEncoding); << 606 { 516 if (it != pedic->end()) { << 607 G4PTblEncodingDictionary::iterator it = pedic->find(aPDGEncoding ); 517 particle = (*it).second; << 608 if (it != pedic->end()) >> 609 { >> 610 particle = (*it).second; >> 611 } 518 } 612 } 519 } << 520 613 521 #ifdef G4MULTITHREADED 614 #ifdef G4MULTITHREADED 522 if (particle == nullptr && G4Threading::IsWo << 615 if(particle == nullptr && G4Threading::IsWorkerThread()) 523 G4MUTEXLOCK(&G4ParticleTable::particleTabl << 616 { 524 auto its = fEncodingDictionaryShadow->find << 617 G4MUTEXLOCK(&G4ParticleTable::particleTableMutex()); 525 if (its != fEncodingDictionaryShadow->end( << 618 G4PTblEncodingDictionary::iterator its 526 particle = (*its).second; << 619 = fEncodingDictionaryShadow->find(aPDGEncoding); 527 fEncodingDictionary->insert(*its); << 620 if(its!=fEncodingDictionaryShadow->end()) 528 G4String key = GetKey(particle); << 621 { 529 fDictionary->insert(std::pair<G4String, << 622 particle = (*its).second; >> 623 fEncodingDictionary->insert(*its); >> 624 G4String key = GetKey(particle); >> 625 fDictionary->insert( std::pair<G4String, >> 626 G4ParticleDefinition*>(key,particle) ); >> 627 } >> 628 G4MUTEXUNLOCK(&G4ParticleTable::particleTableMutex()); 530 } 629 } 531 G4MUTEXUNLOCK(&G4ParticleTable::particleTa << 532 } << 533 #endif 630 #endif 534 631 535 #ifdef G4VERBOSE 632 #ifdef G4VERBOSE 536 if ((particle == nullptr) && (verboseLevel > << 633 if ((particle == nullptr) && (verboseLevel>1) ) 537 G4cout << "CODE:" << aPDGEncoding << " doe << 634 { 538 } << 635 G4cout << "CODE:" << aPDGEncoding << " does not exist in ParticleTable " >> 636 << G4endl; >> 637 } 539 #endif 638 #endif 540 return particle; << 639 return particle; 541 } 640 } 542 641 543 void G4ParticleTable::DumpTable(const G4String << 642 // -------------------------------------------------------------------- >> 643 void G4ParticleTable::DumpTable(const G4String& particle_name) 544 { 644 { 545 CheckReadiness(); 645 CheckReadiness(); 546 if ((particle_name == "ALL") || (particle_na << 646 if (( particle_name == "ALL" ) || (particle_name == "all")) 547 // dump all particles << 647 { 548 G4PTblDicIterator* piter = fIterator; << 648 // dump all particles 549 piter->reset(); << 649 G4PTblDicIterator *piter = fIterator; 550 while ((*piter)()) // Loop checking, 09.0 << 650 piter -> reset(); >> 651 while( (*piter)() ) // Loop checking, 09.08.2015, K.Kurashige 551 { 652 { 552 (piter->value())->DumpTable(); 653 (piter->value())->DumpTable(); 553 } 654 } 554 } 655 } 555 else { << 656 else >> 657 { 556 // dump only particle with name of particl 658 // dump only particle with name of particle_name 557 G4ParticleDefinition* ptr = FindParticle(p << 659 G4ParticleDefinition *ptr = FindParticle(particle_name); 558 if (ptr != nullptr) { << 660 if ( ptr != nullptr) >> 661 { 559 ptr->DumpTable(); 662 ptr->DumpTable(); 560 } 663 } 561 else { << 664 else >> 665 { 562 #ifdef G4VERBOSE 666 #ifdef G4VERBOSE 563 if (verboseLevel > 1) { << 667 if (verboseLevel>1) 564 G4cout << " G4ParticleTable::DumpTable << 668 { 565 << " does not exist in Particle << 669 G4cout << " G4ParticleTable::DumpTable : " >> 670 << particle_name << " does not exist in ParticleTable " >> 671 << G4endl; 566 } 672 } 567 #endif 673 #endif 568 } 674 } 569 } 675 } 570 } 676 } 571 677 >> 678 // -------------------------------------------------------------------- 572 void G4ParticleTable::CheckReadiness() const 679 void G4ParticleTable::CheckReadiness() const 573 { 680 { 574 if (!readyToUse) { << 681 if(!readyToUse) >> 682 { 575 G4String msg; 683 G4String msg; 576 msg = "Illegal use of G4ParticleTable :\n" 684 msg = "Illegal use of G4ParticleTable :\n"; 577 msg += "Access to G4ParticleTable for find 685 msg += "Access to G4ParticleTable for finding a particle or equivalent\n"; 578 msg += "operation occurs before G4VUserPhy 686 msg += "operation occurs before G4VUserPhysicsList is instantiated and\n"; 579 msg += "assigned to G4RunManager. Such an 687 msg += "assigned to G4RunManager. Such an access is prohibited since\n"; 580 msg += "Geant4 version 8.0. To fix this pr 688 msg += "Geant4 version 8.0. To fix this problem, please make sure that\n"; 581 msg += "your main() instantiates G4VUserPh 689 msg += "your main() instantiates G4VUserPhysicsList and set it to\n"; 582 msg += "G4RunManager before instantiating 690 msg += "G4RunManager before instantiating other user classes such as\n"; 583 msg += "G4VUserPrimaryParticleGeneratorAct 691 msg += "G4VUserPrimaryParticleGeneratorAction."; 584 G4Exception("G4ParticleTable::CheckReadine << 692 G4Exception("G4ParticleTable::CheckReadiness()", >> 693 "PART002", FatalException, msg); 585 } 694 } 586 } 695 } 587 696 588 G4IonTable* G4ParticleTable::GetIonTable() con << 697 // -------------------------------------------------------------------- >> 698 G4IonTable* G4ParticleTable::GetIonTable() const 589 { 699 { 590 return fIonTable; 700 return fIonTable; 591 } 701 } 592 << 702 >> 703 // -------------------------------------------------------------------- 593 const G4ParticleTable::G4PTblDictionary* G4Par 704 const G4ParticleTable::G4PTblDictionary* G4ParticleTable::GetDictionary() const 594 { 705 { 595 return fDictionary; 706 return fDictionary; 596 } 707 } 597 708 >> 709 // -------------------------------------------------------------------- 598 G4ParticleTable::G4PTblDicIterator* G4Particle 710 G4ParticleTable::G4PTblDicIterator* G4ParticleTable::GetIterator() const 599 { 711 { 600 return fIterator; 712 return fIterator; 601 } 713 } 602 714 603 const G4ParticleTable::G4PTblEncodingDictionar << 715 // -------------------------------------------------------------------- >> 716 const G4ParticleTable::G4PTblEncodingDictionary* >> 717 G4ParticleTable::GetEncodingDictionary() const 604 { 718 { 605 return fEncodingDictionary; 719 return fEncodingDictionary; 606 } 720 } 607 721 608 G4bool G4ParticleTable::contains(const G4Strin << 722 // -------------------------------------------------------------------- >> 723 G4bool G4ParticleTable::contains(const G4String& particle_name) const 609 { 724 { 610 auto it = fDictionaryShadow->find(particle_n << 725 G4PTblDictionary::iterator it = fDictionaryShadow->find(particle_name); 611 return (it != fDictionaryShadow->cend()); 726 return (it != fDictionaryShadow->cend()); 612 } 727 } 613 728 >> 729 // -------------------------------------------------------------------- 614 G4int G4ParticleTable::entries() const 730 G4int G4ParticleTable::entries() const 615 { 731 { 616 return (G4int)fDictionary->size(); 732 return (G4int)fDictionary->size(); 617 } 733 } 618 734 >> 735 // -------------------------------------------------------------------- 619 G4int G4ParticleTable::size() const 736 G4int G4ParticleTable::size() const 620 { 737 { 621 return (G4int)fDictionary->size(); 738 return (G4int)fDictionary->size(); 622 } 739 } 623 740