G4ExtDEDXTable.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: G4ExtDEDXTable.cc 67044 2013-01-30 08:50:06Z gcosmo $
00027 //
00028 // ===========================================================================
00029 // GEANT4 class source file
00030 //
00031 // Class:                G4ExtDEDXTable
00032 //
00033 // Base class:           G4VIonDEDXTable 
00034 // 
00035 // Author:               Anton Lechner (Anton.Lechner@cern.ch)
00036 //
00037 // First implementation: 29. 02. 2009
00038 //
00039 // Modifications:
00040 // 03.11.2009 A. Lechner:  Added new methods BuildPhysicsVector according
00041 //            to interface changes in base class G4VIonDEDXTable.
00042 // 25.10.2010 V.Ivanchenko fixed bug in usage of iterators reported by the 
00043 //            Coverity tool
00044 // 01.11.2010 V.Ivanchenko fixed remaining bugs reported by Coverity 
00045 //
00046 //
00047 // Class description:
00048 //    Utility class for users to add their own electronic stopping powers
00049 //    for ions. This class is dedicated for use with G4IonParametrisedLossModel
00050 //    of the low-energy electromagnetic package.
00051 //
00052 // Comments:
00053 //
00054 // =========================================================================== 
00055 //
00056 
00057 #include "G4ExtDEDXTable.hh" 
00058 #include "G4PhysicsVector.hh"
00059 #include "G4PhysicsVectorType.hh"
00060 #include "G4LPhysicsFreeVector.hh"
00061 #include "G4PhysicsLogVector.hh"
00062 #include "G4PhysicsFreeVector.hh"
00063 #include "G4PhysicsOrderedFreeVector.hh"
00064 #include "G4PhysicsLinearVector.hh"
00065 #include "G4PhysicsLnVector.hh"
00066 #include <fstream>
00067 #include <sstream>
00068 #include <iomanip>
00069 
00070 
00071 // #########################################################################
00072 
00073 G4ExtDEDXTable::G4ExtDEDXTable() {
00074 
00075 }
00076 
00077 // #########################################################################
00078 
00079 G4ExtDEDXTable::~G4ExtDEDXTable() {
00080 
00081   ClearTable();
00082 }
00083 
00084 // #########################################################################
00085 
00086 G4bool G4ExtDEDXTable::BuildPhysicsVector(G4int ionZ, G4int matZ) {
00087 
00088   return IsApplicable( ionZ, matZ );
00089 }
00090 
00091 
00092 // #########################################################################
00093 
00094 G4bool G4ExtDEDXTable::BuildPhysicsVector(G4int ionZ, 
00095                                           const G4String& matName) {
00096 
00097   return IsApplicable( ionZ, matName );
00098 }
00099 
00100 // #########################################################################
00101 
00102 G4bool G4ExtDEDXTable::IsApplicable(
00103          G4int atomicNumberIon,  // Atomic number of ion
00104          G4int atomicNumberElem  // Atomic number of elemental material
00105                                     ) {
00106   G4bool isApplicable = true; 
00107   G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
00108 
00109   G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
00110 
00111   if(iter == dedxMapElements.end()) isApplicable = false; 
00112 
00113   return isApplicable; 
00114 }
00115 
00116 // #########################################################################
00117 
00118 G4bool G4ExtDEDXTable::IsApplicable(
00119          G4int atomicNumberIon,         // Atomic number of ion
00120          const G4String& matIdentifier  // Name or chemical formula of material
00121                                     ) {
00122   G4bool isApplicable = true; 
00123   G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
00124 
00125   G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
00126 
00127   if(iter == dedxMapMaterials.end()) isApplicable = false; 
00128 
00129   return isApplicable; 
00130 }
00131 
00132 // #########################################################################
00133 
00134 G4PhysicsVector* G4ExtDEDXTable::GetPhysicsVector(
00135          G4int atomicNumberIon,        // Atomic number of ion
00136          G4int atomicNumberElem        // Atomic number of elemental material
00137                                     ) {
00138 
00139   G4PhysicsVector* physVector = 0;
00140 
00141   G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
00142 
00143   G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
00144 
00145   if(iter != dedxMapElements.end()) physVector = iter -> second; 
00146 
00147   return physVector; 
00148 }
00149 
00150 // #########################################################################
00151 
00152 G4PhysicsVector*  G4ExtDEDXTable::GetPhysicsVector(
00153          G4int atomicNumberIon,        // Atomic number of ion
00154          const G4String& matIdentifier // Name or chemical formula of material
00155                                     ) {
00156 
00157   G4PhysicsVector* physVector = 0;
00158 
00159   G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
00160 
00161   G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
00162 
00163   if(iter != dedxMapMaterials.end()) physVector = iter -> second; 
00164 
00165   return physVector; 
00166 }
00167 
00168 // #########################################################################
00169 
00170 G4double G4ExtDEDXTable::GetDEDX(
00171          G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
00172          G4int atomicNumberIon,        // Atomic number of ion
00173          G4int atomicNumberElem        // Atomic number of elemental material
00174                                   ) {
00175   G4double dedx = 0;
00176 
00177   G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
00178 
00179   G4IonDEDXMapElem::iterator iter = dedxMapElements.find(key);
00180 
00181   if( iter != dedxMapElements.end() ) {
00182      G4PhysicsVector* physicsVector = iter -> second; 
00183 
00184      G4bool b;
00185      dedx = physicsVector -> GetValue( kinEnergyPerNucleon, b );   
00186   }
00187 
00188   return dedx; 
00189 }
00190 
00191 // #########################################################################
00192 
00193 G4double G4ExtDEDXTable::GetDEDX(
00194          G4double kinEnergyPerNucleon, // Kinetic energy per nucleon
00195          G4int atomicNumberIon,        // Atomic number of ion
00196          const G4String& matIdentifier // Name or chemical formula of material
00197                                   ) {
00198   G4double dedx = 0;
00199 
00200   G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
00201 
00202   G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
00203 
00204   if(iter != dedxMapMaterials.end()) {
00205      G4PhysicsVector* physicsVector = iter -> second; 
00206 
00207      G4bool b;
00208      dedx = physicsVector -> GetValue( kinEnergyPerNucleon, b );   
00209   }
00210 
00211   return dedx; 
00212 }
00213 
00214 // #########################################################################
00215 
00216 G4bool G4ExtDEDXTable::AddPhysicsVector(
00217         G4PhysicsVector* physicsVector, // Physics vector
00218         G4int atomicNumberIon,          // Atomic number of ion
00219         const G4String& matIdentifier,  // Name of elemental material
00220         G4int atomicNumberElem          // Atomic number of elemental material
00221                                       ) {
00222 
00223   if(physicsVector == 0) {
00224 
00225 #ifdef G4VERBOSE
00226      G4cout << "G4IonDEDXTable::AddPhysicsVector() Error: Pointer to vector"
00227             << " is null-pointer."
00228             << G4endl;
00229 #endif
00230 
00231      return false;
00232   }
00233 
00234   if(matIdentifier.empty()) {
00235 
00236 #ifdef G4VERBOSE
00237      G4cout << "G4IonDEDXTable::AddPhysicsVector() Error: "
00238             << "Cannot add physics vector. Invalid name."
00239             << G4endl;
00240 #endif
00241 
00242      return false;
00243   }
00244 
00245   if(atomicNumberIon <= 2) {
00246 
00247 #ifdef G4VERBOSE
00248      G4cout << "G4IonDEDXTable::AddPhysicsVector() Error: "
00249             << "Cannot add physics vector. Illegal atomic number."
00250             << G4endl;
00251 #endif
00252 
00253      return false;
00254   }
00255 
00256   if(atomicNumberElem > 0) {
00257 
00258      G4IonDEDXKeyElem key = std::make_pair(atomicNumberIon, atomicNumberElem);
00259 
00260      if(dedxMapElements.count(key) == 1) {
00261 
00262 #ifdef G4VERBOSE
00263         G4cout << "G4IonDEDXTable::AddPhysicsVector() Error: "
00264                << "Vector already exists. Remove first before replacing."
00265                << G4endl;
00266 #endif
00267         return false;
00268      }
00269 
00270      dedxMapElements[key] = physicsVector;
00271   }
00272 
00273   G4IonDEDXKeyMat mkey = std::make_pair(atomicNumberIon, matIdentifier);
00274 
00275   if(dedxMapMaterials.count(mkey) == 1) {
00276 
00277 #ifdef G4VERBOSE
00278      G4cout << "G4IonDEDXTable::AddPhysicsVector() Error: "
00279             << "Vector already exists. Remove first before replacing."
00280             << G4endl;
00281 #endif
00282 
00283      return false;
00284   }
00285 
00286   dedxMapMaterials[mkey] = physicsVector;
00287 
00288   return true;
00289 }
00290 
00291 // #########################################################################
00292 
00293 G4bool G4ExtDEDXTable::RemovePhysicsVector(
00294         G4int atomicNumberIon,         // Atomic number of ion
00295         const G4String& matIdentifier  // Name or chemical formula of material
00296                                       ) {
00297 
00298   G4PhysicsVector* physicsVector = 0;
00299 
00300   // Deleting key of physics vector from material map
00301   G4IonDEDXKeyMat key = std::make_pair(atomicNumberIon, matIdentifier);
00302 
00303   G4IonDEDXMapMat::iterator iter = dedxMapMaterials.find(key);
00304 
00305   if(iter == dedxMapMaterials.end()) {
00306 
00307 #ifdef G4VERBOSE
00308     G4cout << "G4IonDEDXTable::RemovePhysicsVector() Warning: "
00309            << "Cannot remove physics vector. Vector not found."
00310            << G4endl;
00311 #endif
00312 
00313     return false;
00314   }
00315 
00316   physicsVector = (*iter).second;
00317   dedxMapMaterials.erase(key);
00318 
00319   // Deleting key of physics vector from elemental material map (if it exists)
00320   G4IonDEDXMapElem::iterator it;
00321   
00322   for(it=dedxMapElements.begin(); it!=dedxMapElements.end(); ++it) {
00323 
00324      if( (*it).second == physicsVector ) {
00325         dedxMapElements.erase(it);
00326         break;
00327      }
00328   }
00329 
00330   // Deleting physics vector
00331   delete physicsVector;
00332 
00333   return true;
00334 }
00335 
00336 // #########################################################################
00337 
00338 G4bool G4ExtDEDXTable::StorePhysicsTable(
00339          const G4String& fileName // File name
00340                                     ) {
00341   G4bool success = true;
00342 
00343   std::ofstream ofilestream;
00344 
00345   ofilestream.open( fileName, std::ios::out );
00346 
00347   if( !ofilestream ) {
00348 
00349 #ifdef G4VERBOSE
00350      G4cout << "G4ExtDEDXTable::StorePhysicsVector() " 
00351             << " Cannot open file "<< fileName 
00352             << G4endl;
00353 #endif
00354       
00355      success = false;
00356   }   
00357   else {
00358 
00359      size_t nmbMatTables = dedxMapMaterials.size();
00360 
00361      ofilestream << nmbMatTables << G4endl << G4endl; 
00362 
00363      G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
00364      G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
00365 
00366      for(;iterMat != iterMat_end; iterMat++) {
00367          G4IonDEDXKeyMat key = iterMat -> first;
00368          G4PhysicsVector* physicsVector = iterMat -> second; 
00369 
00370          G4int atomicNumberIon = key.first;
00371          G4String matIdentifier = key.second;
00372 
00373          G4int atomicNumberElem = FindAtomicNumberElement(physicsVector);
00374 
00375          if(physicsVector != 0) {
00376             ofilestream << atomicNumberIon << "  " << matIdentifier;
00377 
00378             if(atomicNumberElem > 0) ofilestream << "  " << atomicNumberElem;
00379 
00380             ofilestream << "  # <Atomic number ion>  <Material name>  ";
00381 
00382             if(atomicNumberElem > 0) ofilestream << "<Atomic number element>";
00383 
00384             ofilestream << G4endl << physicsVector -> GetType() << G4endl;
00385 
00386             physicsVector -> Store(ofilestream, true);
00387 
00388             ofilestream << G4endl;
00389          }
00390          else {
00391 
00392 #ifdef G4VERBOSE
00393               G4cout << "G4ExtDEDXTable::StorePhysicsVector() " 
00394                      << " Cannot store physics vector." 
00395                      << G4endl;
00396 #endif
00397 
00398          }
00399      }
00400   }
00401 
00402   ofilestream.close();
00403 
00404   return success; 
00405 }
00406 
00407 // #########################################################################
00408 
00409 G4bool G4ExtDEDXTable::RetrievePhysicsTable(const G4String& fileName) 
00410 { 
00411   std::ifstream ifilestream;
00412   ifilestream.open( fileName, std::ios::in|std::ios::binary );
00413   if( ! ifilestream ) {
00414 #ifdef G4VERBOSE
00415     G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() " 
00416            << " Cannot open file "<< fileName 
00417            << G4endl;
00418 #endif
00419     return false;
00420   }   
00421 
00422   //std::string::size_type nmbVectors;
00423   G4int nmbVectors;
00424   ifilestream >> nmbVectors;
00425   if( ifilestream.fail() ) { 
00426     G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() " 
00427            << " File content of " << fileName << " ill-formated." 
00428            << G4endl;     
00429     ifilestream.close(); 
00430     return false; 
00431   }
00432 
00433   //  if(nmbVectors == std::string::npos) {
00434   /*
00435   if(nmbVectors <= 0) {
00436 #ifdef G4VERBOSE
00437     G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() " 
00438            << " The file is corrupted " << G4endl;
00439 #endif
00440     return false;
00441   }  
00442   */
00443   //size_t nm = size_t(nmbVectors);
00444   for(G4int i = 0; i<nmbVectors; ++i) {
00445 
00446     G4String line = "";
00447     while( line.empty() ) {
00448 
00449       getline( ifilestream, line );
00450       if( ifilestream.fail() ) { 
00451 #ifdef G4VERBOSE  
00452         G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() " 
00453                << " File content of " << fileName << " ill-formated." 
00454                << G4endl;
00455 #endif          
00456         ifilestream.close(); 
00457         return false; 
00458       }
00459 
00460       std::string::size_type pos = line.find_first_of("#");
00461       if(pos != std::string::npos && pos > 0) {
00462         line = line.substr(0, pos);
00463       }
00464     }
00465 
00466     std::istringstream headerstream( line );     
00467 
00468     std::string::size_type atomicNumberIon;
00469     headerstream >> atomicNumberIon;
00470 
00471     G4String materialName;
00472     headerstream >> materialName;
00473 
00474     if( headerstream.fail() || std::string::npos == atomicNumberIon) {
00475  
00476 #ifdef G4VERBOSE  
00477       G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() " 
00478              << " File content of " << fileName << " ill-formated "
00479              << " (vector header)." 
00480              << G4endl;
00481 #endif          
00482       ifilestream.close();
00483       return false;
00484     } 
00485 
00486     std::string::size_type atomicNumberMat;
00487     headerstream >> atomicNumberMat;
00488 
00489     if( headerstream.eof() || std::string::npos == atomicNumberMat) { 
00490       atomicNumberMat = 0; 
00491     }
00492 
00493     G4int vectorType;
00494     ifilestream >> vectorType;
00495       
00496     G4PhysicsVector* physicsVector = CreatePhysicsVector(vectorType);
00497 
00498     if(physicsVector == 0) {
00499 #ifdef G4VERBOSE  
00500       G4cout << "G4ExtDEDXTable::RetrievePhysicsTable  "
00501              << " illegal physics Vector type " << vectorType
00502              << " in  " << fileName 
00503              << G4endl;
00504 #endif          
00505       ifilestream.close();
00506       return false;
00507     }
00508 
00509     if( !physicsVector -> Retrieve(ifilestream, true) ) {
00510         
00511 #ifdef G4VERBOSE  
00512       G4cout << "G4ExtDEDXTable::RetrievePhysicsTable() " 
00513              << " File content of " << fileName << " ill-formated." 
00514              << G4endl;
00515 #endif          
00516       ifilestream.close();
00517       return false;
00518     } 
00519 
00520     physicsVector -> SetSpline(true);
00521 
00522     // Retrieved vector is added to material store
00523     if( !AddPhysicsVector(physicsVector, (G4int)atomicNumberIon, 
00524                           materialName, (G4int)atomicNumberMat) ) {
00525 
00526       delete physicsVector;
00527       ifilestream.close();
00528       return false;
00529     }
00530   }
00531 
00532   ifilestream.close();
00533 
00534   return true;
00535 }
00536 
00537 // #########################################################################
00538 
00539 G4PhysicsVector* G4ExtDEDXTable::CreatePhysicsVector(G4int vectorType) {
00540 
00541   G4PhysicsVector* physicsVector = 0;
00542 
00543   switch (vectorType) {
00544 
00545   case T_G4PhysicsLinearVector: 
00546     physicsVector = new G4PhysicsLinearVector();
00547     break;
00548 
00549   case T_G4PhysicsLogVector: 
00550     physicsVector = new G4PhysicsLogVector();
00551     break;
00552 
00553   case T_G4PhysicsLnVector: 
00554     physicsVector = new G4PhysicsLnVector();
00555     break;
00556 
00557   case T_G4PhysicsFreeVector: 
00558     physicsVector = new G4PhysicsFreeVector();
00559     break;
00560 
00561   case T_G4PhysicsOrderedFreeVector: 
00562     physicsVector = new G4PhysicsOrderedFreeVector();
00563     break;
00564 
00565   case T_G4LPhysicsFreeVector: 
00566     physicsVector = new G4LPhysicsFreeVector();
00567     break;
00568   
00569   default:
00570     break;
00571   }
00572   return physicsVector;
00573 }
00574 
00575 // #########################################################################
00576 
00577 G4int G4ExtDEDXTable::FindAtomicNumberElement(
00578                                    G4PhysicsVector* physicsVector
00579                                                    ) {
00580 
00581   G4int atomicNumber = 0;
00582 
00583   G4IonDEDXMapElem::iterator iter = dedxMapElements.begin();
00584   G4IonDEDXMapElem::iterator iter_end = dedxMapElements.end();
00585   
00586   for(;iter != iter_end; iter++) {
00587 
00588      if( (*iter).second == physicsVector ) {
00589 
00590         G4IonDEDXKeyElem key = (*iter).first;
00591         atomicNumber = key.second;
00592      }
00593   }
00594 
00595   return atomicNumber;
00596 }
00597 
00598 // #########################################################################
00599 
00600 void G4ExtDEDXTable::ClearTable() {
00601 
00602   G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
00603   G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
00604 
00605   for(;iterMat != iterMat_end; iterMat++) { 
00606 
00607     G4PhysicsVector* vec = iterMat -> second;
00608 
00609     if(vec != 0) delete vec;
00610   }
00611 
00612   dedxMapElements.clear();
00613   dedxMapMaterials.clear();
00614 }
00615 
00616 // #########################################################################
00617 
00618 void G4ExtDEDXTable::DumpMap() {
00619 
00620   G4IonDEDXMapMat::iterator iterMat = dedxMapMaterials.begin();
00621   G4IonDEDXMapMat::iterator iterMat_end = dedxMapMaterials.end();
00622 
00623   G4cout << std::setw(15) << std::right
00624          << "Atomic nmb ion"
00625          << std::setw(25) << std::right
00626          << "Material name"
00627          << std::setw(25) << std::right
00628          << "Atomic nmb material"
00629          << G4endl;
00630 
00631   for(;iterMat != iterMat_end; iterMat++) {
00632       G4IonDEDXKeyMat key = iterMat -> first;
00633       G4PhysicsVector* physicsVector = iterMat -> second; 
00634 
00635       G4int atomicNumberIon = key.first;
00636       G4String matIdentifier = key.second;
00637 
00638       G4int atomicNumberElem = FindAtomicNumberElement(physicsVector);
00639 
00640       if(physicsVector != 0) {
00641          G4cout << std::setw(15) << std::right
00642                 << atomicNumberIon
00643                 << std::setw(25) << std::right
00644                 << matIdentifier
00645                 << std::setw(25) << std::right;
00646 
00647          if(atomicNumberElem > 0) G4cout << atomicNumberElem;
00648          else G4cout << "N/A";
00649 
00650          G4cout << G4endl;
00651       }
00652   }
00653 
00654 }
00655 
00656 // #########################################################################
00657 

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