Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 72057 2013-07-04 13:07:29Z 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  G4bool isApplicable = true;
107  G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
108 
109  G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
110 
111  if(iter == dedxMapElements.end()) isApplicable = false;
112 
113  return isApplicable;
114 }
115 
116 // #########################################################################
117 
119  G4int atomicNumberIon, // Atomic number of ion
120  const G4String& matIdentifier // Name or chemical formula of material
121  ) {
122  G4bool isApplicable = true;
123  G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
124 
125  G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
126 
127  if(iter == dedxMapMaterials.end()) isApplicable = false;
128 
129  return isApplicable;
130 }
131 
132 // #########################################################################
133 
135  G4int atomicNumberIon, // Atomic number of ion
136  G4int atomicNumberElem // Atomic number of elemental material
137  ) {
138 
139  G4PhysicsVector* physVector = 0;
140 
141  G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
142 
143  G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
144 
145  if(iter != dedxMapElements.end()) physVector = iter -> second;
146 
147  return physVector;
148 }
149 
150 // #########################################################################
151 
153  G4int atomicNumberIon, // Atomic number of ion
154  const G4String& matIdentifier // Name or chemical formula of material
155  ) {
156 
157  G4PhysicsVector* physVector = 0;
158 
159  G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
160 
161  G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
162 
163  if(iter != dedxMapMaterials.end()) physVector = iter -> second;
164 
165  return physVector;
166 }
167 
168 // #########################################################################
169 
171  G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
172  G4int atomicNumberIon, // Atomic number of ion
173  G4int atomicNumberElem // Atomic number of elemental material
174  ) {
175  G4double dedx = 0;
176 
177  G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
178 
179  G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
180 
181  if( iter != dedxMapElements.end() ) {
182  G4PhysicsVector* physicsVector = iter -> second;
183 
184  G4bool b;
185  dedx = physicsVector -> GetValue( kinEnergyPerNucleon, b );
186  }
187 
188  return dedx;
189 }
190 
191 // #########################################################################
192 
194  G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
195  G4int atomicNumberIon, // Atomic number of ion
196  const G4String& matIdentifier // Name or chemical formula of material
197  ) {
198  G4double dedx = 0;
199 
200  G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
201 
202  G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
203 
204  if(iter != dedxMapMaterials.end()) {
205  G4PhysicsVector* physicsVector = iter -> second;
206 
207  G4bool b;
208  dedx = physicsVector -> GetValue( kinEnergyPerNucleon, b );
209  }
210 
211  return dedx;
212 }
213 
214 // #########################################################################
215 
217  G4PhysicsVector* physicsVector, // Physics vector
218  G4int atomicNumberIon, // Atomic number of ion
219  const G4String& matIdentifier, // Name of elemental material
220  G4int atomicNumberElem // Atomic number of elemental material
221  ) {
222 
223  if(physicsVector == 0) {
224 
225 #ifdef G4VERBOSE
226  G4cout << "G4IonDEDXTable::AddPhysicsVector() Error: Pointer to vector"
227  << " is null-pointer."
228  << G4endl;
229 #endif
230 
231  return false;
232  }
233 
234  if(matIdentifier.empty()) {
235 
236 #ifdef G4VERBOSE
237  G4cout << "G4IonDEDXTable::AddPhysicsVector() Error: "
238  << "Cannot add physics vector. Invalid name."
239  << G4endl;
240 #endif
241 
242  return false;
243  }
244 
245  if(atomicNumberIon <= 2) {
246 
247 #ifdef G4VERBOSE
248  G4cout << "G4IonDEDXTable::AddPhysicsVector() Error: "
249  << "Cannot add physics vector. Illegal atomic number."
250  << G4endl;
251 #endif
252 
253  return false;
254  }
255 
256  if(atomicNumberElem > 0) {
257 
258  G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
259 
260  if(dedxMapElements.count(key) == 1) {
261 
262 #ifdef G4VERBOSE
263  G4cout << "G4IonDEDXTable::AddPhysicsVector() Error: "
264  << "Vector already exists. Remove first before replacing."
265  << G4endl;
266 #endif
267  return false;
268  }
269 
270  dedxMapElements[key] = physicsVector;
271  }
272 
273  G4IonDEDXKeyMat mkey = std::make_pair(atomicNumberIon, matIdentifier);
274 
275  if(dedxMapMaterials.count(mkey) == 1) {
276 
277 #ifdef G4VERBOSE
278  G4cout << "G4IonDEDXTable::AddPhysicsVector() Error: "
279  << "Vector already exists. Remove first before replacing."
280  << G4endl;
281 #endif
282 
283  return false;
284  }
285 
286  dedxMapMaterials[mkey] = physicsVector;
287 
288  return true;
289 }
290 
291 // #########################################################################
292 
294  G4int atomicNumberIon, // Atomic number of ion
295  const G4String& matIdentifier // Name or chemical formula of material
296  ) {
297 
298  G4PhysicsVector* physicsVector = 0;
299 
300  // Deleting key of physics vector from material map
301  G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
302 
303  G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
304 
305  if(iter == dedxMapMaterials.end()) {
306 
307 #ifdef G4VERBOSE
308  G4cout << "G4IonDEDXTable::RemovePhysicsVector() Warning: "
309  << "Cannot remove physics vector. Vector not found."
310  << G4endl;
311 #endif
312 
313  return false;
314  }
315 
316  physicsVector = (*iter).second;
317  dedxMapMaterials.erase(key);
318 
319  // Deleting key of physics vector from elemental material map (if it exists)
320  G4IonDEDXMapElem::iterator it;
321 
322  for(it=dedxMapElements.begin(); it!=dedxMapElements.end(); ++it) {
323 
324  if( (*it).second == physicsVector ) {
325  dedxMapElements.erase(it);
326  break;
327  }
328  }
329 
330  // Deleting physics vector
331  delete physicsVector;
332 
333  return true;
334 }
335 
336 // #########################################################################
337 
339  const G4String& fileName // File name
340  ) {
341  G4bool success = true;
342 
343  std::ofstream ofilestream;
344 
345  ofilestream.open( fileName, std::ios::out );
346 
347  if( !ofilestream ) {
348 
349 #ifdef G4VERBOSE
350  G4cout << "G4ExtDEDXTable::StorePhysicsVector() "
351  << " Cannot open file "<< fileName
352  << G4endl;
353 #endif
354 
355  success = false;
356  }
357  else {
358 
359  size_t nmbMatTables = dedxMapMaterials.size();
360 
361  ofilestream << nmbMatTables << G4endl << G4endl;
362 
363  G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
364  G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
365 
366  for(;iterMat != iterMat_end; iterMat++) {
367  G4IonDEDXKeyMat key = iterMat -> first;
368  G4PhysicsVector* physicsVector = iterMat -> second;
369 
370  G4int atomicNumberIon = key.first;
371  G4String matIdentifier = key.second;
372 
373  G4int atomicNumberElem = FindAtomicNumberElement(physicsVector);
374 
375  if(physicsVector != 0) {
376  ofilestream << atomicNumberIon << " " << matIdentifier;
377 
378  if(atomicNumberElem > 0) ofilestream << " " << atomicNumberElem;
379 
380  ofilestream << " # <Atomic number ion> <Material name> ";
381 
382  if(atomicNumberElem > 0) ofilestream << "<Atomic number element>";
383 
384  ofilestream << G4endl << physicsVector -> GetType() << G4endl;
385 
386  physicsVector -> Store(ofilestream, true);
387 
388  ofilestream << G4endl;
389  }
390  else {
391 
392 #ifdef G4VERBOSE
393  G4cout << "G4ExtDEDXTable::StorePhysicsVector() "
394  << " Cannot store physics vector."
395  << G4endl;
396 #endif
397 
398  }
399  }
400  }
401 
402  ofilestream.close();
403 
404  return success;
405 }
406 
407 // #########################################################################
408 
410 {
411  std::ifstream ifilestream;
412  ifilestream.open( fileName, std::ios::in|std::ios::binary );
413  if( ! ifilestream ) {
414 #ifdef G4VERBOSE
415  G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
416  << " Cannot open file "<< fileName
417  << G4endl;
418 #endif
419  return false;
420  }
421 
422  //std::string::size_type nmbVectors;
423  G4int nmbVectors = 0;
424  ifilestream >> nmbVectors;
425  if( ifilestream.fail() || nmbVectors <= 0) {
426  G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
427  << " File content of " << fileName << " ill-formated."
428  << " Nvectors= " << nmbVectors
429  << G4endl;
430  ifilestream.close();
431  return false;
432  }
433 
434  for(G4int i = 0; i<nmbVectors; ++i) {
435 
436  G4String line = "";
437  while( line.empty() ) {
438 
439  getline( ifilestream, line );
440  if( ifilestream.fail() ) {
441 #ifdef G4VERBOSE
442  G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
443  << " File content of " << fileName << " ill-formated."
444  << G4endl;
445 #endif
446  ifilestream.close();
447  return false;
448  }
449 
450  std::string::size_type pos = line.find_first_of("#");
451  if(pos != std::string::npos && pos > 0) {
452  line = line.substr(0, pos);
453  }
454  }
455 
456  std::istringstream headerstream( line );
457 
458  std::string::size_type atomicNumberIon;
459  headerstream >> atomicNumberIon;
460 
461  G4String materialName;
462  headerstream >> materialName;
463 
464  if( headerstream.fail() || std::string::npos == atomicNumberIon) {
465 
466 #ifdef G4VERBOSE
467  G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
468  << " File content of " << fileName << " ill-formated "
469  << " (vector header)."
470  << G4endl;
471 #endif
472  ifilestream.close();
473  return false;
474  }
475 
476  std::string::size_type atomicNumberMat;
477  headerstream >> atomicNumberMat;
478 
479  if( headerstream.eof() || std::string::npos == atomicNumberMat) {
480  atomicNumberMat = 0;
481  }
482 
483  G4int vectorType;
484  ifilestream >> vectorType;
485 
486  G4PhysicsVector* physicsVector = CreatePhysicsVector(vectorType);
487 
488  if(physicsVector == 0) {
489 #ifdef G4VERBOSE
490  G4cout << "G4ExtDEDXTable::RetrievePhysicsTable "
491  << " illegal physics Vector type " << vectorType
492  << " in " << fileName
493  << G4endl;
494 #endif
495  ifilestream.close();
496  return false;
497  }
498 
499  if( !physicsVector -> Retrieve(ifilestream, true) ) {
500 
501 #ifdef G4VERBOSE
502  G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() "
503  << " File content of " << fileName << " ill-formated."
504  << G4endl;
505 #endif
506  ifilestream.close();
507  return false;
508  }
509 
510  physicsVector -> SetSpline(true);
511 
512  // Retrieved vector is added to material store
513  if( !AddPhysicsVector(physicsVector, (G4int)atomicNumberIon,
514  materialName, (G4int)atomicNumberMat) ) {
515 
516  delete physicsVector;
517  ifilestream.close();
518  return false;
519  }
520  }
521 
522  ifilestream.close();
523 
524  return true;
525 }
526 
527 // #########################################################################
528 
529 G4PhysicsVector* G4ExtDEDXTable::CreatePhysicsVector(G4int vectorType) {
530 
531  G4PhysicsVector* physicsVector = 0;
532 
533  switch (vectorType) {
534 
536  physicsVector = new G4PhysicsLinearVector();
537  break;
538 
539  case T_G4PhysicsLogVector:
540  physicsVector = new G4PhysicsLogVector();
541  break;
542 
543  case T_G4PhysicsLnVector:
544  physicsVector = new G4PhysicsLnVector();
545  break;
546 
547  case T_G4PhysicsFreeVector:
548  physicsVector = new G4PhysicsFreeVector();
549  break;
550 
552  physicsVector = new G4PhysicsOrderedFreeVector();
553  break;
554 
556  physicsVector = new G4LPhysicsFreeVector();
557  break;
558 
559  default:
560  break;
561  }
562  return physicsVector;
563 }
564 
565 // #########################################################################
566 
567 G4int G4ExtDEDXTable::FindAtomicNumberElement(
568  G4PhysicsVector* physicsVector
569  ) {
570 
571  G4int atomicNumber = 0;
572 
573  G4IonDEDXMapElem::iterator iter = dedxMapElements.begin();
574  G4IonDEDXMapElem::iterator iter_end = dedxMapElements.end();
575 
576  for(;iter != iter_end; iter++) {
577 
578  if( (*iter).second == physicsVector ) {
579 
580  G4IonDEDXKeyElem key = (*iter).first;
581  atomicNumber = key.second;
582  }
583  }
584 
585  return atomicNumber;
586 }
587 
588 // #########################################################################
589 
591 
592  G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
593  G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
594 
595  for(;iterMat != iterMat_end; iterMat++) {
596 
597  G4PhysicsVector* vec = iterMat -> second;
598 
599  if(vec != 0) delete vec;
600  }
601 
602  dedxMapElements.clear();
603  dedxMapMaterials.clear();
604 }
605 
606 // #########################################################################
607 
609 
610  G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
611  G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
612 
613  G4cout << std::setw(15) << std::right
614  << "Atomic nmb ion"
615  << std::setw(25) << std::right
616  << "Material name"
617  << std::setw(25) << std::right
618  << "Atomic nmb material"
619  << G4endl;
620 
621  for(;iterMat != iterMat_end; iterMat++) {
622  G4IonDEDXKeyMat key = iterMat -> first;
623  G4PhysicsVector* physicsVector = iterMat -> second;
624 
625  G4int atomicNumberIon = key.first;
626  G4String matIdentifier = key.second;
627 
628  G4int atomicNumberElem = FindAtomicNumberElement(physicsVector);
629 
630  if(physicsVector != 0) {
631  G4cout << std::setw(15) << std::right
632  << atomicNumberIon
633  << std::setw(25) << std::right
634  << matIdentifier
635  << std::setw(25) << std::right;
636 
637  if(atomicNumberElem > 0) G4cout << atomicNumberElem;
638  else G4cout << "N/A";
639 
640  G4cout << G4endl;
641  }
642  }
643 
644 }
645 
646 // #########################################################################
647 
G4double GetDEDX(G4double kinEnergyPerNucleon, G4int atomicNumberIon, G4int atomicNumberElem)
G4bool RetrievePhysicsTable(const G4String &fileName)
G4bool RemovePhysicsVector(G4int atomicNumberIon, const G4String &matIdentifier)
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
G4bool AddPhysicsVector(G4PhysicsVector *physicsVector, G4int atomicNumberIon, const G4String &matIdenfier, G4int atomicNumberElem=0)
G4PhysicsVector * GetPhysicsVector(G4int atomicNumberIon, G4int atomicNumberElem)
G4bool StorePhysicsTable(const G4String &fileName)
G4bool BuildPhysicsVector(G4int ionZ, const G4String &matName)
#define G4endl
Definition: G4ios.hh:61
virtual ~G4ExtDEDXTable()
double G4double
Definition: G4Types.hh:76
G4bool IsApplicable(G4int atomicNumberIon, G4int atomicNumberElem)