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