Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4ExtDEDXTable.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: G4ExtDEDXTable.cc 96794 2016-05-09 10:09:30Z gcosmo $
27 //
28 // ===========================================================================
29 // GEANT4 class source file
30 //
31 // Class: G4ExtDEDXTable
32 //
33 // Base class: G4VIonDEDXTable
34 //
35 // Author: Anton Lechner (Anton.Lechner@cern.ch)
36 //
37 // First implementation: 29. 02. 2009
38 //
39 // Modifications:
40 // 03.11.2009 A. Lechner: Added new methods BuildPhysicsVector according
41 // to interface changes in base class G4VIonDEDXTable.
42 // 25.10.2010 V.Ivanchenko fixed bug in usage of iterators reported by the
43 // Coverity tool
44 // 01.11.2010 V.Ivanchenko fixed remaining bugs reported by Coverity
45 //
46 //
47 // Class description:
48 // Utility class for users to add their own electronic stopping powers
49 // for ions. This class is dedicated for use with G4IonParametrisedLossModel
50 // of the low-energy electromagnetic package.
51 //
52 // Comments:
53 //
54 // ===========================================================================
55 //
56 
57 #include "G4ExtDEDXTable.hh"
58 #include "G4PhysicsVector.hh"
59 #include "G4PhysicsVectorType.hh"
60 #include "G4LPhysicsFreeVector.hh"
61 #include "G4PhysicsLogVector.hh"
62 #include "G4PhysicsFreeVector.hh"
64 #include "G4PhysicsLinearVector.hh"
65 #include "G4PhysicsLnVector.hh"
66 #include <fstream>
67 #include <sstream>
68 #include <iomanip>
69 
70 
71 // #########################################################################
72 
74 
75 }
76 
77 // #########################################################################
78 
80 
81  ClearTable();
82 }
83 
84 // #########################################################################
85 
87 
88  return IsApplicable( ionZ, matZ );
89 }
90 
91 
92 // #########################################################################
93 
95  const G4String& matName) {
96 
97  return IsApplicable( ionZ, matName );
98 }
99 
100 // #########################################################################
101 
103  G4int atomicNumberIon, // Atomic number of ion
104  G4int atomicNumberElem // Atomic number of elemental material
105  )
106 {
107  G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
108 
109  G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
110 
111  return (iter == dedxMapElements.end()) ? false : true;
112 }
113 
114 // #########################################################################
115 
117  G4int atomicNumberIon, // Atomic number of ion
118  const G4String& matIdentifier // Name or chemical formula of material
119  )
120 {
121  G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
122 
123  G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
124 
125  return (iter == dedxMapMaterials.end()) ? false : true;
126 }
127 
128 // #########################################################################
129 
131  G4int atomicNumberIon, // Atomic number of ion
132  G4int atomicNumberElem // Atomic number of elemental material
133  )
134 {
135  G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
136 
137  G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
138 
139  return (iter != dedxMapElements.end()) ? iter->second : nullptr;
140 }
141 
142 // #########################################################################
143 
145  G4int atomicNumberIon, // Atomic number of ion
146  const G4String& matIdentifier // Name or chemical formula of material
147  )
148 {
149  G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
150 
151  G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
152 
153  return (iter != dedxMapMaterials.end()) ? iter->second : nullptr;
154 }
155 
156 // #########################################################################
157 
159  G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
160  G4int atomicNumberIon, // Atomic number of ion
161  G4int atomicNumberElem // Atomic number of elemental material
162  )
163 {
164  G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
165 
166  G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
167 
168  return ( iter != dedxMapElements.end() ) ?
169  (iter->second)->Value( kinEnergyPerNucleon) : 0.0;
170 }
171 
172 // #########################################################################
173 
175  G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
176  G4int atomicNumberIon, // Atomic number of ion
177  const G4String& matIdentifier // Name or chemical formula of material
178  )
179 {
180  G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
181 
182  G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
183 
184  return (iter != dedxMapMaterials.end()) ?
185  (iter->second)->Value( kinEnergyPerNucleon) : 0.0;
186 }
187 
188 // #########################################################################
189 
191  G4PhysicsVector* physicsVector, // Physics vector
192  G4int atomicNumberIon, // Atomic number of ion
193  const G4String& matIdentifier, // Name of elemental material
194  G4int atomicNumberElem // Atomic number of elemental material
195  ) {
196 
197  if(physicsVector == nullptr) {
198  G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
199  "mat037", FatalException,
200  "Pointer to vector is null-pointer.");
201  return false;
202  }
203 
204  if(matIdentifier.empty()) {
205  G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
206  "mat038", FatalException, "Invalid name of the material.");
207  return false;
208  }
209 
210  if(atomicNumberIon <= 2) {
211  G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
212  "mat039", FatalException, "Illegal atomic number.");
213  return false;
214  }
215 
216  if(atomicNumberElem > 0) {
217 
218  G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
219 
220  if(dedxMapElements.count(key) == 1) {
221  G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
222  "mat037", FatalException,
223  "Vector already exist, remove it before replacing.");
224  return false;
225  }
226 
227  dedxMapElements[key] = physicsVector;
228  }
229 
230  G4IonDEDXKeyMat mkey = std::make_pair(atomicNumberIon, matIdentifier);
231 
232  if(dedxMapMaterials.count(mkey) == 1) {
233  G4Exception ("G4ExtDEDXTable::AddPhysicsVector() for material",
234  "mat037", FatalException,
235  "Vector already exist, remove it before replacing.");
236  return false;
237  }
238 
239  dedxMapMaterials[mkey] = physicsVector;
240 
241  return true;
242 }
243 
244 // #########################################################################
245 
247  G4int atomicNumberIon, // Atomic number of ion
248  const G4String& matIdentifier // Name or chemical formula of material
249  ) {
250 
251  G4PhysicsVector* physicsVector = nullptr;
252 
253  // Deleting key of physics vector from material map
254  G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
255 
256  G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
257 
258  if(iter == dedxMapMaterials.end()) {
259  G4Exception ("G4ExtDEDXTable::RemovePhysicsVector() for material",
260  "mat037", FatalException,
261  "Pointer to vector is null-pointer.");
262  return false;
263  }
264 
265  physicsVector = (*iter).second;
266  dedxMapMaterials.erase(key);
267 
268  // Deleting key of physics vector from elemental material map (if it exists)
269  G4IonDEDXMapElem::iterator it;
270 
271  for(it=dedxMapElements.begin(); it!=dedxMapElements.end(); ++it) {
272 
273  if( (*it).second == physicsVector ) {
274  dedxMapElements.erase(it);
275  break;
276  }
277  }
278 
279  // Deleting physics vector
280  delete physicsVector;
281 
282  return true;
283 }
284 
285 // #########################################################################
286 
288  const G4String& fileName // File name
289  ) {
290  G4bool success = true;
291 
292  std::ofstream ofilestream;
293 
294  ofilestream.open( fileName, std::ios::out );
295 
296  if( !ofilestream ) {
298  ed << "Cannot open file " << fileName;
299  G4Exception ("G4IonStoppingData::StorePhysicsTable()",
300  "mat030", FatalException, ed);
301  success = false;
302  }
303  else {
304 
305  size_t nmbMatTables = dedxMapMaterials.size();
306 
307  ofilestream << nmbMatTables << G4endl << G4endl;
308 
309  G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
310  G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
311 
312  for(;iterMat != iterMat_end; iterMat++) {
313  G4IonDEDXKeyMat key = iterMat -> first;
314  G4PhysicsVector* physicsVector = iterMat -> second;
315 
316  G4int atomicNumberIon = key.first;
317  G4String matIdentifier = key.second;
318 
319  G4int atomicNumberElem = FindAtomicNumberElement(physicsVector);
320 
321  if(physicsVector != nullptr) {
322  ofilestream << atomicNumberIon << " " << matIdentifier;
323 
324  if(atomicNumberElem > 0) ofilestream << " " << atomicNumberElem;
325 
326  ofilestream << " # <Atomic number ion> <Material name> ";
327 
328  if(atomicNumberElem > 0) ofilestream << "<Atomic number element>";
329 
330  ofilestream << G4endl << physicsVector -> GetType() << G4endl;
331 
332  physicsVector -> Store(ofilestream, true);
333 
334  ofilestream << G4endl;
335  } else {
336  G4Exception ("G4IonStoppingData::StorePhysicsTable()",
337  "mat030", FatalException,"Cannot store vector.");
338  }
339  }
340  }
341 
342  ofilestream.close();
343 
344  return success;
345 }
346 
347 // #########################################################################
348 
350 {
351  std::ifstream ifilestream;
352  ifilestream.open( fileName, std::ios::in|std::ios::binary );
353  if( ! ifilestream ) {
355  ed << "Cannot open file " << fileName;
356  G4Exception ("G4IonStoppingData::RetrievePhysicsTable()",
357  "mat030", FatalException, ed);
358  return false;
359  }
360 
361  //std::string::size_type nmbVectors;
362  G4int nmbVectors = 0;
363  ifilestream >> nmbVectors;
364  if( ifilestream.fail() || nmbVectors <= 0) {
365  G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
366  << " File content of " << fileName << " ill-formated."
367  << " Nvectors= " << nmbVectors
368  << G4endl;
369  ifilestream.close();
370  return false;
371  }
372 
373  for(G4int i = 0; i<nmbVectors; ++i) {
374 
375  G4String line = "";
376  // Loop checking, 07-Aug-2015, Vladimir Ivanchenko
377  while( line.empty() ) {
378 
379  getline( ifilestream, line );
380  if( ifilestream.fail() ) {
381  G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
382  << " File content of " << fileName << " ill-formated."
383  << G4endl;
384  ifilestream.close();
385  return false;
386  }
387 
388  std::string::size_type pos = line.find_first_of("#");
389  if(pos != std::string::npos && pos > 0) {
390  line = line.substr(0, pos);
391  }
392  }
393 
394  std::istringstream headerstream( line );
395 
396  std::string::size_type atomicNumberIon;
397  headerstream >> atomicNumberIon;
398 
399  G4String materialName;
400  headerstream >> materialName;
401 
402  if( headerstream.fail() || std::string::npos == atomicNumberIon) {
403  G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
404  << " File content of " << fileName << " ill-formated "
405  << " (vector header)."
406  << G4endl;
407  ifilestream.close();
408  return false;
409  }
410 
411  std::string::size_type atomicNumberMat;
412  headerstream >> atomicNumberMat;
413 
414  if( headerstream.eof() || std::string::npos == atomicNumberMat) {
415  atomicNumberMat = 0;
416  }
417 
418  G4int vectorType;
419  ifilestream >> vectorType;
420 
421  G4PhysicsVector* physicsVector = CreatePhysicsVector(vectorType);
422 
423  if(physicsVector == nullptr) {
424  G4cout << "G4ExtDEDXTable::RetrievePhysicsTable "
425  << " illegal physics Vector type " << vectorType
426  << " in " << fileName
427  << G4endl;
428  ifilestream.close();
429  return false;
430  }
431 
432  if( !physicsVector -> Retrieve(ifilestream, true) ) {
433  G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
434  << " File content of " << fileName << " ill-formated."
435  << G4endl;
436  ifilestream.close();
437  return false;
438  }
439 
440  physicsVector -> SetSpline(true);
441 
442  // Retrieved vector is added to material store
443  if( !AddPhysicsVector(physicsVector, (G4int)atomicNumberIon,
444  materialName, (G4int)atomicNumberMat) ) {
445 
446  delete physicsVector;
447  ifilestream.close();
448  return false;
449  }
450  }
451 
452  ifilestream.close();
453 
454  return true;
455 }
456 
457 // #########################################################################
458 
459 G4PhysicsVector* G4ExtDEDXTable::CreatePhysicsVector(G4int vectorType) {
460 
461  G4PhysicsVector* physicsVector = 0;
462 
463  switch (vectorType) {
464 
466  physicsVector = new G4PhysicsLinearVector();
467  break;
468 
469  case T_G4PhysicsLogVector:
470  physicsVector = new G4PhysicsLogVector();
471  break;
472 
473  case T_G4PhysicsLnVector:
474  physicsVector = new G4PhysicsLnVector();
475  break;
476 
477  case T_G4PhysicsFreeVector:
478  physicsVector = new G4PhysicsFreeVector();
479  break;
480 
482  physicsVector = new G4PhysicsOrderedFreeVector();
483  break;
484 
486  physicsVector = new G4LPhysicsFreeVector();
487  break;
488 
489  default:
490  break;
491  }
492  return physicsVector;
493 }
494 
495 // #########################################################################
496 
497 G4int G4ExtDEDXTable::FindAtomicNumberElement(
498  G4PhysicsVector* physicsVector
499  ) {
500 
501  G4int atomicNumber = 0;
502 
503  G4IonDEDXMapElem::iterator iter = dedxMapElements.begin();
504  G4IonDEDXMapElem::iterator iter_end = dedxMapElements.end();
505 
506  for(;iter != iter_end; iter++) {
507 
508  if( (*iter).second == physicsVector ) {
509 
510  G4IonDEDXKeyElem key = (*iter).first;
511  atomicNumber = key.second;
512  }
513  }
514 
515  return atomicNumber;
516 }
517 
518 // #########################################################################
519 
521 
522  G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
523  G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
524 
525  for(;iterMat != iterMat_end; iterMat++) {
526 
527  G4PhysicsVector* vec = iterMat -> second;
528 
529  if(vec != 0) delete vec;
530  }
531 
532  dedxMapElements.clear();
533  dedxMapMaterials.clear();
534 }
535 
536 // #########################################################################
537 
539 
540  G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
541  G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
542 
543  G4cout << std::setw(15) << std::right
544  << "Atomic nmb ion"
545  << std::setw(25) << std::right
546  << "Material name"
547  << std::setw(25) << std::right
548  << "Atomic nmb material"
549  << G4endl;
550 
551  for(;iterMat != iterMat_end; iterMat++) {
552  G4IonDEDXKeyMat key = iterMat -> first;
553  G4PhysicsVector* physicsVector = iterMat -> second;
554 
555  G4int atomicNumberIon = key.first;
556  G4String matIdentifier = key.second;
557 
558  G4int atomicNumberElem = FindAtomicNumberElement(physicsVector);
559 
560  if(physicsVector != 0) {
561  G4cout << std::setw(15) << std::right
562  << atomicNumberIon
563  << std::setw(25) << std::right
564  << matIdentifier
565  << std::setw(25) << std::right;
566 
567  if(atomicNumberElem > 0) G4cout << atomicNumberElem;
568  else G4cout << "N/A";
569 
570  G4cout << G4endl;
571  }
572  }
573 
574 }
575 
576 // #########################################################################
577 
G4double GetDEDX(G4double kinEnergyPerNucleon, G4int atomicNumberIon, G4int atomicNumberElem)
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4bool RetrievePhysicsTable(const G4String &fileName)
static constexpr double second
Definition: G4SIunits.hh:157
G4bool RemovePhysicsVector(G4int atomicNumberIon, const G4String &matIdentifier)
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool AddPhysicsVector(G4PhysicsVector *physicsVector, G4int atomicNumberIon, const G4String &matIdenfier, G4int atomicNumberElem=0)
virtual G4PhysicsVector * GetPhysicsVector(G4int atomicNumberIon, G4int atomicNumberElem)
G4bool StorePhysicsTable(const G4String &fileName)
virtual G4bool BuildPhysicsVector(G4int ionZ, const G4String &matName)
#define G4endl
Definition: G4ios.hh:61
virtual ~G4ExtDEDXTable()
double G4double
Definition: G4Types.hh:76
G4PhysicsLogVector G4PhysicsLnVector
static const G4double pos
virtual G4bool IsApplicable(G4int atomicNumberIon, G4int atomicNumberElem)