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