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
G4IonStoppingData.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// $Id$
27//
28// ===========================================================================
29// GEANT4 class source file
30//
31// Class: G4IonStoppingData
32//
33// Base class: G4VIonDEDXTable
34//
35// Author: Anton Lechner (Anton.Lechner@cern.ch)
36//
37// First implementation: 03. 11. 2009
38//
39// Modifications:
40// 25.10.2010 V.Ivanchenko fixed warnings reported by the Coverity tool
41// 25.10.2011: new scheme for G4Exception (mma)
42//
43//
44// Class description: Class which can read ion stopping power data from
45// $G4LEDATA/ion_stopping_data
46//
47// Comments:
48//
49// ===========================================================================
50//
51
52#include <fstream>
53#include <sstream>
54#include <iomanip>
55
56#include "G4IonStoppingData.hh"
57#include "G4PhysicsVector.hh"
60#include "G4SystemOfUnits.hh"
61
62// #########################################################################
63
65 subDir( leDirectory ) {
66
67}
68
69// #########################################################################
70
72
73 ClearTable();
74}
75
76// #########################################################################
77
79 G4int atomicNumberIon, // Atomic number of ion
80 G4int atomicNumberElem // Atomic number of elemental material
81 ) {
82 G4bool isApplicable = true;
83 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
84
85 G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
86
87 if(iter == dedxMapElements.end()) isApplicable = false;
88
89 return isApplicable;
90}
91
92// #########################################################################
93
95 G4int atomicNumberIon, // Atomic number of ion
96 const G4String& matIdentifier // Name or chemical formula of material
97 ) {
98 G4bool isApplicable = true;
99 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
100
101 G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
102
103 if(iter == dedxMapMaterials.end()) isApplicable = false;
104
105 return isApplicable;
106}
107
108// #########################################################################
109
111 G4int atomicNumberIon, // Atomic number of ion
112 G4int atomicNumberElem // Atomic number of elemental material
113 ) {
114
115 G4PhysicsVector* physVector = 0;
116
117 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
118
119 G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
120
121 if(iter != dedxMapElements.end()) physVector = iter -> second;
122
123 return physVector;
124}
125
126// #########################################################################
127
129 G4int atomicNumberIon, // Atomic number of ion
130 const G4String& matIdentifier // Name or chemical formula of material
131 ) {
132
133 G4PhysicsVector* physVector = 0;
134
135 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
136
137 G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
138
139 if(iter != dedxMapMaterials.end()) physVector = iter -> second;
140
141 return physVector;
142}
143
144// #########################################################################
145
147 G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
148 G4int atomicNumberIon, // Atomic number of ion
149 G4int atomicNumberElem // Atomic number of elemental material
150 ) {
151 G4double dedx = 0;
152
153 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
154
155 G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
156
157 if( iter != dedxMapElements.end() ) {
158 G4PhysicsVector* physicsVector = iter -> second;
159
160 G4bool b;
161 dedx = physicsVector -> GetValue( kinEnergyPerNucleon, b );
162 }
163
164 return dedx;
165}
166
167// #########################################################################
168
170 G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
171 G4int atomicNumberIon, // Atomic number of ion
172 const G4String& matIdentifier // Name or chemical formula of material
173 ) {
174 G4double dedx = 0;
175
176 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
177
178 G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
179
180 if(iter != dedxMapMaterials.end()) {
181 G4PhysicsVector* physicsVector = iter -> second;
182
183 G4bool b;
184 dedx = physicsVector -> GetValue( kinEnergyPerNucleon, b );
185 }
186
187 return dedx;
188}
189
190// #########################################################################
191
193 G4PhysicsVector* physicsVector, // Physics vector
194 G4int atomicNumberIon, // Atomic number of ion
195 const G4String& matIdentifier // Name of elemental material
196 ) {
197
198 if(physicsVector == 0) {
199
200#ifdef G4VERBOSE
201 G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: Pointer to vector"
202 << " is null-pointer."
203 << G4endl;
204#endif
205
206 return false;
207 }
208
209 if(matIdentifier.empty()) {
210
211#ifdef G4VERBOSE
212 G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
213 << "Cannot add physics vector. Invalid name."
214 << G4endl;
215#endif
216
217 return false;
218 }
219
220 if(atomicNumberIon <= 0) {
221
222#ifdef G4VERBOSE
223 G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
224 << "Cannot add physics vector. Illegal atomic number."
225 << G4endl;
226#endif
227
228 return false;
229 }
230
231 G4IonDEDXKeyMat mkey = std::make_pair(atomicNumberIon, matIdentifier);
232
233 if(dedxMapMaterials.count(mkey) == 1) {
234
235#ifdef G4VERBOSE
236 G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
237 << "Vector with Z1 = " << atomicNumberIon << ", mat = "
238 << matIdentifier
239 << "already exists. Remove first before replacing."
240 << G4endl;
241#endif
242
243 return false;
244 }
245
246 dedxMapMaterials[mkey] = physicsVector;
247
248 return true;
249}
250
251// #########################################################################
252
254 G4PhysicsVector* physicsVector, // Physics vector
255 G4int atomicNumberIon, // Atomic number of ion
256 G4int atomicNumberElem // Atomic number of elemental material
257 ) {
258
259 if(physicsVector == 0) {
260
261#ifdef G4VERBOSE
262 G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
263 << "Pointer to vector is null-pointer."
264 << G4endl;
265#endif
266
267 return false;
268 }
269
270 if(atomicNumberIon <= 0) {
271
272#ifdef G4VERBOSE
273 G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
274 << "Cannot add physics vector. Illegal atomic number."
275 << G4endl;
276#endif
277
278 return false;
279 }
280
281 if(atomicNumberElem <= 0) {
282
283#ifdef G4VERBOSE
284 G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
285 << "Atomic number of element < 0."
286 << G4endl;
287#endif
288 return false;
289 }
290
291 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
292
293 if(dedxMapElements.count(key) == 1) {
294
295#ifdef G4VERBOSE
296 G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
297 << "Vector with Z1 = " << atomicNumberIon << ", Z2 = "
298 << atomicNumberElem
299 << " already exists. Remove first before replacing."
300 << G4endl;
301#endif
302 return false;
303 }
304
305 dedxMapElements[key] = physicsVector;
306
307 return true;
308}
309
310// #########################################################################
311
313 G4int atomicNumberIon, // Atomic number of ion
314 const G4String& matIdentifier // Name or chemical formula of material
315 ) {
316
317 G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
318
319 G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
320
321 if(iter == dedxMapMaterials.end()) {
322
323#ifdef G4VERBOSE
324 G4cerr << "G4IonStoppingData::RemovePhysicsVector() Warning: "
325 << "Cannot remove physics vector. Vector not found."
326 << G4endl;
327#endif
328
329 return false;
330 }
331
332 G4PhysicsVector* physicsVector = (*iter).second;
333
334 // Deleting key of physics vector from material map
335 dedxMapMaterials.erase(key);
336
337 // Deleting physics vector
338 delete physicsVector;
339
340 return true;
341}
342
343// #########################################################################
344
346 G4int atomicNumberIon, // Atomic number of ion
347 G4int atomicNumberElem // Atomic number of elemental material
348 ) {
349 G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
350
351 G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
352
353 if(iter == dedxMapElements.end()) {
354
355#ifdef G4VERBOSE
356 G4cerr << "G4IonStoppingData::RemovePhysicsVector() Warning: "
357 << "Cannot remove physics vector. Vector not found."
358 << G4endl;
359#endif
360
361 return false;
362 }
363
364 G4PhysicsVector* physicsVector = (*iter).second;
365
366 // Deleting key of physics vector from material map
367 dedxMapElements.erase(key);
368
369 // Deleting physics vector
370 delete physicsVector;
371
372 return true;
373}
374
375// #########################################################################
376
378 G4int atomicNumberIon, // Atomic number of ion
379 const G4String& matIdentifier // Name of material
380 ) {
381
382 if( IsApplicable(atomicNumberIon, matIdentifier) ) return true;
383
384 char* path = getenv("G4LEDATA");
385 if ( !path ) {
386 G4Exception("G4IonStoppingData::BuildPhysicsVector()", "mat521",
387 FatalException, "G4LEDATA environment variable not set");
388 return false;
389 }
390
391 std::ostringstream file;
392
393 file << path << "/" << subDir << "/z"
394 << atomicNumberIon << "_" << matIdentifier
395 << ".dat";
396
397 G4String fileName = G4String( file.str().c_str() );
398
399 std::ifstream ifilestream( fileName );
400
401 if ( !ifilestream.is_open() ) return false;
402
403 G4LPhysicsFreeVector* physicsVector = new G4LPhysicsFreeVector();
404
405 if( !physicsVector -> Retrieve(ifilestream, true) ) {
406
407 ifilestream.close();
408 return false;
409 }
410
411 physicsVector -> ScaleVector( MeV, MeV * cm2 /( 0.001 * g) );
412 physicsVector -> SetSpline( true );
413 physicsVector -> FillSecondDerivatives();
414
415 // Retrieved vector is added to material store
416 if( !AddPhysicsVector(physicsVector, atomicNumberIon, matIdentifier) ) {
417 delete physicsVector;
418 ifilestream.close();
419 return false;
420 }
421
422 ifilestream.close();
423 return true;
424}
425
426// #########################################################################
427
429 G4int atomicNumberIon, // Atomic number of ion
430 G4int atomicNumberElem // Atomic number of elemental material
431 ) {
432
433 if( IsApplicable(atomicNumberIon, atomicNumberElem) ) return true;
434
435 char* path = getenv("G4LEDATA");
436 if ( !path ) {
437 G4Exception("G4IonStoppingData::BuildPhysicsVector()", "mat522",
438 FatalException, "G4LEDATA environment variable not set");
439 return false;
440 }
441 std::ostringstream file;
442
443 file << path << "/" << subDir << "/z"
444 << atomicNumberIon << "_" << atomicNumberElem
445 << ".dat";
446
447 G4String fileName = G4String( file.str().c_str() );
448
449 std::ifstream ifilestream( fileName );
450
451 if ( !ifilestream.is_open() ) return false;
452
453 G4LPhysicsFreeVector* physicsVector = new G4LPhysicsFreeVector();
454
455 if( !physicsVector -> Retrieve(ifilestream, true) ) {
456
457 ifilestream.close();
458 return false;
459 }
460
461 physicsVector -> ScaleVector( MeV, MeV * cm2 /( 0.001 * g) );
462 physicsVector -> SetSpline( true );
463 physicsVector -> FillSecondDerivatives();
464
465 // Retrieved vector is added to material store
466 if( !AddPhysicsVector(physicsVector, atomicNumberIon, atomicNumberElem) ) {
467 delete physicsVector;
468 ifilestream.close();
469 return false;
470 }
471
472 ifilestream.close();
473 return true;
474}
475
476// #########################################################################
477
479
480 G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
481 G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
482
483 for(;iterMat != iterMat_end; iterMat++) {
484
485 G4PhysicsVector* vec = iterMat -> second;
486
487 if(vec != 0) delete vec;
488 }
489
490 dedxMapMaterials.clear();
491
492 G4IonDEDXMapElem::iterator iterElem = dedxMapElements.begin();
493 G4IonDEDXMapElem::iterator iterElem_end = dedxMapElements.end();
494
495 for(;iterElem != iterElem_end; iterElem++) {
496
497 G4PhysicsVector* vec = iterElem -> second;
498
499 if(vec != 0) delete vec;
500 }
501
502 dedxMapElements.clear();
503}
504
505// #########################################################################
506
508
509 G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
510 G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
511
512 G4cout << std::setw(15) << std::right
513 << "Atomic nmb ion"
514 << std::setw(25) << std::right
515 << "Material name"
516 << G4endl;
517
518 for(;iterMat != iterMat_end; iterMat++) {
519 G4IonDEDXKeyMat key = iterMat -> first;
520 G4PhysicsVector* physicsVector = iterMat -> second;
521
522 G4int atomicNumberIon = key.first;
523 G4String matIdentifier = key.second;
524
525 if(physicsVector != 0) {
526 G4cout << std::setw(15) << std::right
527 << atomicNumberIon
528 << std::setw(25) << std::right
529 << matIdentifier
530 << G4endl;
531 }
532 }
533
534 G4IonDEDXMapElem::iterator iterElem = dedxMapElements.begin();
535 G4IonDEDXMapElem::iterator iterElem_end = dedxMapElements.end();
536
537 G4cout << std::setw(15) << std::right
538 << "Atomic nmb ion"
539 << std::setw(25) << std::right
540 << "Atomic nmb material"
541 << G4endl;
542
543 for(;iterElem != iterElem_end; iterElem++) {
544 G4IonDEDXKeyElem key = iterElem -> first;
545 G4PhysicsVector* physicsVector = iterElem -> second;
546
547 G4int atomicNumberIon = key.first;
548 G4int atomicNumberElem = key.second;
549
550 if(physicsVector != 0) {
551 G4cout << std::setw(15) << std::right
552 << atomicNumberIon
553 << std::setw(25) << std::right
554 << atomicNumberElem
555 << G4endl;
556 }
557 }
558
559}
560
561// #########################################################################
562
@ FatalException
double G4double
Definition: G4Types.hh:64
int G4int
Definition: G4Types.hh:66
bool G4bool
Definition: G4Types.hh:67
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cerr
G4DLLIMPORT std::ostream G4cout
G4PhysicsVector * GetPhysicsVector(G4int atomicNumberIon, G4int atomicNumberElem)
G4bool IsApplicable(G4int atomicNumberIon, G4int atomicNumberElem)
G4bool RemovePhysicsVector(G4int atomicNumberIon, const G4String &matIdentifier)
G4double GetDEDX(G4double kinEnergyPerNucleon, G4int atomicNumberIon, G4int atomicNumberElem)
G4bool AddPhysicsVector(G4PhysicsVector *physicsVector, G4int atomicNumberIon, const G4String &matIdentifier)
G4bool BuildPhysicsVector(G4int ionZ, const G4String &matName)
G4IonStoppingData(const G4String &leDirectory)
virtual ~G4IonStoppingData()
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41