G4IonStoppingData.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id: G4IonStoppingData.cc 67044 2013-01-30 08:50:06Z gcosmo $
00027 //
00028 // ===========================================================================
00029 // GEANT4 class source file
00030 //
00031 // Class:                G4IonStoppingData
00032 //
00033 // Base class:           G4VIonDEDXTable 
00034 // 
00035 // Author:               Anton Lechner (Anton.Lechner@cern.ch)
00036 //
00037 // First implementation: 03. 11. 2009
00038 //
00039 // Modifications:
00040 // 25.10.2010 V.Ivanchenko fixed warnings reported by the Coverity tool
00041 // 25.10.2011: new scheme for G4Exception  (mma)
00042 //
00043 //
00044 // Class description: Class which can read ion stopping power data from
00045 //                    $G4LEDATA/ion_stopping_data
00046 //
00047 // Comments:
00048 //
00049 // =========================================================================== 
00050 //
00051 
00052 #include <fstream>
00053 #include <sstream>
00054 #include <iomanip>
00055 
00056 #include "G4IonStoppingData.hh" 
00057 #include "G4PhysicsVector.hh"
00058 #include "G4LPhysicsFreeVector.hh"
00059 #include "G4PhysicalConstants.hh"
00060 #include "G4SystemOfUnits.hh"
00061 
00062 // #########################################################################
00063 
00064 G4IonStoppingData::G4IonStoppingData(const G4String& leDirectory) :
00065   subDir( leDirectory ) {
00066 
00067 }
00068 
00069 // #########################################################################
00070 
00071 G4IonStoppingData::~G4IonStoppingData() {
00072 
00073   ClearTable();
00074 }
00075 
00076 // #########################################################################
00077 
00078 G4bool G4IonStoppingData::IsApplicable(
00079          G4int atomicNumberIon,  // Atomic number of ion
00080          G4int atomicNumberElem  // Atomic number of elemental material
00081                                     ) {
00082   G4bool isApplicable = true; 
00083   G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
00084 
00085   G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
00086 
00087   if(iter == dedxMapElements.end()) isApplicable = false; 
00088 
00089   return isApplicable; 
00090 }
00091 
00092 // #########################################################################
00093 
00094 G4bool G4IonStoppingData::IsApplicable(
00095          G4int atomicNumberIon,         // Atomic number of ion
00096          const G4String& matIdentifier  // Name or chemical formula of material
00097                                     ) {
00098   G4bool isApplicable = true; 
00099   G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
00100 
00101   G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
00102 
00103   if(iter == dedxMapMaterials.end()) isApplicable = false; 
00104 
00105   return isApplicable; 
00106 }
00107 
00108 // #########################################################################
00109 
00110 G4PhysicsVector* G4IonStoppingData::GetPhysicsVector(
00111          G4int atomicNumberIon,        // Atomic number of ion
00112          G4int atomicNumberElem        // Atomic number of elemental material
00113                                     ) {
00114 
00115   G4PhysicsVector* physVector = 0;
00116 
00117   G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
00118 
00119   G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
00120 
00121   if(iter != dedxMapElements.end()) physVector = iter -> second; 
00122 
00123   return physVector; 
00124 }
00125 
00126 // #########################################################################
00127 
00128 G4PhysicsVector*  G4IonStoppingData::GetPhysicsVector(
00129          G4int atomicNumberIon,        // Atomic number of ion
00130          const G4String& matIdentifier // Name or chemical formula of material
00131                                     ) {
00132 
00133   G4PhysicsVector* physVector = 0;
00134 
00135   G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
00136 
00137   G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
00138 
00139   if(iter != dedxMapMaterials.end()) physVector = iter -> second; 
00140 
00141   return physVector; 
00142 }
00143 
00144 // #########################################################################
00145 
00146 G4double G4IonStoppingData::GetDEDX(
00147          G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
00148          G4int atomicNumberIon,        // Atomic number of ion
00149          G4int atomicNumberElem        // Atomic number of elemental material
00150                                   ) {
00151   G4double dedx = 0;
00152 
00153   G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
00154 
00155   G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
00156 
00157   if( iter != dedxMapElements.end() ) {
00158      G4PhysicsVector* physicsVector = iter -> second; 
00159 
00160      G4bool b;
00161      dedx = physicsVector -> GetValue( kinEnergyPerNucleon, b );   
00162   }
00163 
00164   return dedx; 
00165 }
00166 
00167 // #########################################################################
00168 
00169 G4double G4IonStoppingData::GetDEDX(
00170          G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
00171          G4int atomicNumberIon,        // Atomic number of ion
00172          const G4String& matIdentifier // Name or chemical formula of material
00173                                   ) {
00174   G4double dedx = 0;
00175 
00176   G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
00177 
00178   G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
00179 
00180   if(iter != dedxMapMaterials.end()) {
00181      G4PhysicsVector* physicsVector = iter -> second; 
00182 
00183      G4bool b;
00184      dedx = physicsVector -> GetValue( kinEnergyPerNucleon, b );   
00185   }
00186 
00187   return dedx; 
00188 }
00189 
00190 // #########################################################################
00191 
00192 G4bool G4IonStoppingData::AddPhysicsVector(
00193         G4PhysicsVector* physicsVector, // Physics vector
00194         G4int atomicNumberIon,          // Atomic number of ion
00195         const G4String& matIdentifier   // Name of elemental material
00196                                       ) {
00197 
00198   if(physicsVector == 0) {
00199 
00200 #ifdef G4VERBOSE
00201      G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: Pointer to vector"
00202             << " is null-pointer."
00203             << G4endl;
00204 #endif
00205 
00206      return false;
00207   }
00208 
00209   if(matIdentifier.empty()) {
00210 
00211 #ifdef G4VERBOSE
00212      G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
00213             << "Cannot add physics vector. Invalid name."
00214             << G4endl;
00215 #endif
00216 
00217      return false;
00218   }
00219 
00220   if(atomicNumberIon <= 0) {
00221 
00222 #ifdef G4VERBOSE
00223      G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
00224             << "Cannot add physics vector. Illegal atomic number."
00225             << G4endl;
00226 #endif
00227 
00228      return false;
00229   }
00230 
00231   G4IonDEDXKeyMat mkey = std::make_pair(atomicNumberIon, matIdentifier);
00232 
00233   if(dedxMapMaterials.count(mkey) == 1) {
00234 
00235 #ifdef G4VERBOSE
00236      G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
00237             << "Vector with Z1 = " << atomicNumberIon << ", mat = " 
00238             << matIdentifier
00239             << "already exists. Remove first before replacing."
00240             << G4endl;
00241 #endif
00242 
00243      return false;
00244   }
00245 
00246   dedxMapMaterials[mkey] = physicsVector;
00247 
00248   return true;
00249 }
00250 
00251 // #########################################################################
00252 
00253 G4bool G4IonStoppingData::AddPhysicsVector(
00254         G4PhysicsVector* physicsVector, // Physics vector
00255         G4int atomicNumberIon,          // Atomic number of ion
00256         G4int atomicNumberElem          // Atomic number of elemental material
00257                                       ) {
00258 
00259   if(physicsVector == 0) {
00260 
00261 #ifdef G4VERBOSE
00262      G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
00263             << "Pointer to vector is null-pointer."
00264             << G4endl;
00265 #endif
00266 
00267      return false;
00268   }
00269 
00270   if(atomicNumberIon <= 0) {
00271 
00272 #ifdef G4VERBOSE
00273      G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
00274             << "Cannot add physics vector. Illegal atomic number."
00275             << G4endl;
00276 #endif
00277 
00278      return false;
00279   }
00280 
00281   if(atomicNumberElem <= 0) {
00282 
00283 #ifdef G4VERBOSE
00284         G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
00285                << "Atomic number of element < 0."
00286                << G4endl;
00287 #endif
00288         return false;
00289   }
00290 
00291   G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
00292 
00293   if(dedxMapElements.count(key) == 1) {
00294 
00295 #ifdef G4VERBOSE
00296      G4cerr << "G4IonStoppingData::AddPhysicsVector() Error: "
00297             << "Vector with Z1 = " << atomicNumberIon << ", Z2 = " 
00298             << atomicNumberElem
00299             << " already exists. Remove first before replacing."
00300             << G4endl;
00301 #endif
00302       return false;
00303   }
00304 
00305   dedxMapElements[key] = physicsVector;
00306 
00307   return true;
00308 }
00309 
00310 // #########################################################################
00311 
00312 G4bool G4IonStoppingData::RemovePhysicsVector(
00313         G4int atomicNumberIon,         // Atomic number of ion
00314         const G4String& matIdentifier  // Name or chemical formula of material
00315                                       ) {
00316 
00317   G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
00318 
00319   G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
00320 
00321   if(iter == dedxMapMaterials.end()) {
00322 
00323 #ifdef G4VERBOSE
00324      G4cerr << "G4IonStoppingData::RemovePhysicsVector() Warning: "
00325             << "Cannot remove physics vector. Vector not found."
00326             << G4endl;
00327 #endif
00328 
00329      return false;
00330   }
00331 
00332   G4PhysicsVector* physicsVector = (*iter).second;
00333 
00334   // Deleting key of physics vector from material map
00335   dedxMapMaterials.erase(key);
00336 
00337   // Deleting physics vector
00338   delete physicsVector;
00339 
00340   return true;
00341 }
00342 
00343 // #########################################################################
00344 
00345 G4bool G4IonStoppingData::RemovePhysicsVector(
00346         G4int atomicNumberIon,         // Atomic number of ion
00347         G4int atomicNumberElem         // Atomic number of elemental material
00348                                       ) {
00349   G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
00350 
00351   G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
00352 
00353   if(iter == dedxMapElements.end()) {
00354 
00355 #ifdef G4VERBOSE
00356      G4cerr << "G4IonStoppingData::RemovePhysicsVector() Warning: "
00357             << "Cannot remove physics vector. Vector not found."
00358             << G4endl;
00359 #endif
00360 
00361      return false;
00362   }
00363 
00364   G4PhysicsVector* physicsVector = (*iter).second;
00365 
00366   // Deleting key of physics vector from material map
00367   dedxMapElements.erase(key);
00368 
00369   // Deleting physics vector
00370   delete physicsVector;
00371 
00372   return true;
00373 }
00374 
00375 // #########################################################################
00376 
00377 G4bool G4IonStoppingData::BuildPhysicsVector(
00378         G4int atomicNumberIon,          // Atomic number of ion
00379         const G4String& matIdentifier   // Name of material
00380                                                      ) {
00381 
00382   if( IsApplicable(atomicNumberIon, matIdentifier) ) return true;
00383 
00384   char* path = getenv("G4LEDATA");
00385   if ( !path ) {
00386     G4Exception("G4IonStoppingData::BuildPhysicsVector()", "mat521",
00387                 FatalException, "G4LEDATA environment variable not set");
00388     return false;
00389   }
00390   
00391   std::ostringstream file;
00392  
00393   file << path << "/" << subDir << "/z" 
00394        << atomicNumberIon << "_" << matIdentifier 
00395        << ".dat";
00396                       
00397   G4String fileName = G4String( file.str().c_str() );
00398 
00399   std::ifstream ifilestream( fileName );
00400 
00401   if ( !ifilestream.is_open() ) return false;
00402   
00403   G4LPhysicsFreeVector* physicsVector = new G4LPhysicsFreeVector(); 
00404 
00405   if( !physicsVector -> Retrieve(ifilestream, true) ) {
00406  
00407      ifilestream.close();
00408      return false;
00409   }
00410 
00411   physicsVector -> ScaleVector( MeV, MeV * cm2 /( 0.001 * g) ); 
00412   physicsVector -> SetSpline( true );
00413   physicsVector -> FillSecondDerivatives();
00414 
00415   // Retrieved vector is added to material store
00416   if( !AddPhysicsVector(physicsVector, atomicNumberIon, matIdentifier) ) {
00417      delete physicsVector;
00418      ifilestream.close();
00419      return false;
00420   }
00421 
00422   ifilestream.close();
00423   return true;
00424 }
00425 
00426 // #########################################################################
00427 
00428 G4bool G4IonStoppingData::BuildPhysicsVector(
00429         G4int atomicNumberIon,          // Atomic number of ion
00430         G4int atomicNumberElem          // Atomic number of elemental material
00431                                                      ) {
00432 
00433   if( IsApplicable(atomicNumberIon, atomicNumberElem) ) return true;
00434 
00435   char* path = getenv("G4LEDATA");
00436   if ( !path ) {
00437     G4Exception("G4IonStoppingData::BuildPhysicsVector()", "mat522",
00438                 FatalException, "G4LEDATA environment variable not set");
00439     return false;
00440   }
00441   std::ostringstream file;
00442  
00443   file << path << "/" << subDir << "/z" 
00444        << atomicNumberIon << "_" << atomicNumberElem
00445        << ".dat";
00446                       
00447   G4String fileName = G4String( file.str().c_str() );
00448 
00449   std::ifstream ifilestream( fileName );
00450 
00451   if ( !ifilestream.is_open() ) return false;
00452   
00453   G4LPhysicsFreeVector* physicsVector = new G4LPhysicsFreeVector(); 
00454 
00455   if( !physicsVector -> Retrieve(ifilestream, true) ) {
00456  
00457      ifilestream.close();
00458      return false;
00459   }
00460 
00461   physicsVector -> ScaleVector( MeV, MeV * cm2 /( 0.001 * g) ); 
00462   physicsVector -> SetSpline( true );
00463   physicsVector -> FillSecondDerivatives();
00464 
00465   // Retrieved vector is added to material store
00466   if( !AddPhysicsVector(physicsVector, atomicNumberIon, atomicNumberElem) ) {
00467      delete physicsVector;
00468      ifilestream.close();
00469      return false;
00470   }
00471 
00472   ifilestream.close();
00473   return true;
00474 }
00475 
00476 // #########################################################################
00477 
00478 void G4IonStoppingData::ClearTable() {
00479 
00480   G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
00481   G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
00482 
00483   for(;iterMat != iterMat_end; iterMat++) { 
00484 
00485     G4PhysicsVector* vec = iterMat -> second;
00486 
00487     if(vec != 0) delete vec;
00488   }
00489 
00490   dedxMapMaterials.clear();
00491 
00492   G4IonDEDXMapElem::iterator iterElem = dedxMapElements.begin();
00493   G4IonDEDXMapElem::iterator iterElem_end = dedxMapElements.end();
00494 
00495   for(;iterElem != iterElem_end; iterElem++) { 
00496 
00497     G4PhysicsVector* vec = iterElem -> second;
00498 
00499     if(vec != 0) delete vec;
00500   }
00501 
00502   dedxMapElements.clear();
00503 }
00504 
00505 // #########################################################################
00506 
00507 void G4IonStoppingData::DumpMap() {
00508 
00509   G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
00510   G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
00511 
00512   G4cout << std::setw(15) << std::right
00513          << "Atomic nmb ion"
00514          << std::setw(25) << std::right
00515          << "Material name"
00516          << G4endl;
00517 
00518   for(;iterMat != iterMat_end; iterMat++) {
00519       G4IonDEDXKeyMat key = iterMat -> first;
00520       G4PhysicsVector* physicsVector = iterMat -> second; 
00521 
00522       G4int atomicNumberIon = key.first;
00523       G4String matIdentifier = key.second;
00524 
00525       if(physicsVector != 0) {
00526          G4cout << std::setw(15) << std::right
00527                 << atomicNumberIon
00528                 << std::setw(25) << std::right
00529                 << matIdentifier
00530                 << G4endl;
00531       }
00532   }
00533 
00534   G4IonDEDXMapElem::iterator iterElem = dedxMapElements.begin();
00535   G4IonDEDXMapElem::iterator iterElem_end = dedxMapElements.end();
00536 
00537   G4cout << std::setw(15) << std::right
00538          << "Atomic nmb ion"
00539          << std::setw(25) << std::right
00540          << "Atomic nmb material"
00541          << G4endl;
00542 
00543   for(;iterElem != iterElem_end; iterElem++) { 
00544       G4IonDEDXKeyElem key = iterElem -> first;
00545       G4PhysicsVector* physicsVector = iterElem -> second; 
00546 
00547       G4int atomicNumberIon = key.first;
00548       G4int atomicNumberElem = key.second;
00549 
00550       if(physicsVector != 0) {
00551          G4cout << std::setw(15) << std::right
00552                 << atomicNumberIon
00553                 << std::setw(25) << std::right
00554                 << atomicNumberElem
00555                 << G4endl;
00556       }
00557   }
00558 
00559 }
00560 
00561 // #########################################################################
00562 

Generated on Mon May 27 17:48:40 2013 for Geant4 by  doxygen 1.4.7