G4PhysicsTable.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 //
00027 // $Id$
00028 //
00029 // 
00030 // ------------------------------------------------------------
00031 //      GEANT 4 class implementation
00032 //
00033 //      G4PhysicsTable
00034 //
00035 // ------------------------------------------------------------
00036 
00037 #include <iostream>
00038 #include <fstream>
00039 #include <iomanip>
00040 
00041 #include "G4PhysicsVector.hh"
00042 #include "G4PhysicsTable.hh"
00043 #include "G4PhysicsVectorType.hh"
00044 #include "G4LPhysicsFreeVector.hh"
00045 #include "G4PhysicsLogVector.hh"
00046 #include "G4PhysicsFreeVector.hh"
00047 #include "G4PhysicsOrderedFreeVector.hh"
00048 #include "G4PhysicsLinearVector.hh"
00049 #include "G4PhysicsLnVector.hh"
00050  
00051 G4PhysicsTable::G4PhysicsTable()
00052   : G4PhysCollection()
00053 {
00054 }
00055 
00056 G4PhysicsTable::G4PhysicsTable(size_t cap)
00057   : G4PhysCollection()
00058 {
00059   reserve(cap);
00060   vecFlag.reserve(cap);
00061 }
00062 
00063 G4PhysicsTable::G4PhysicsTable(const G4PhysicsTable& right)
00064   : G4PhysCollection()
00065 {
00066   *this = right;
00067 }
00068 
00069 G4PhysicsTable& G4PhysicsTable::operator=(const G4PhysicsTable& right)
00070 {
00071   if (this != &right)
00072   {
00073     size_t idx = 0;
00074     for (G4PhysCollection::const_iterator itr=right.begin();
00075          itr!=right.end(); ++itr )
00076     {
00077       G4PhysCollection::push_back(*itr);
00078       vecFlag.push_back(right.GetFlag(idx));
00079       idx +=1;
00080     }
00081   }
00082   return *this;
00083 }
00084 
00085 G4PhysicsTable::~G4PhysicsTable()
00086 {
00087   G4PhysCollection::clear();
00088   vecFlag.clear();
00089 }
00090  
00091 void   G4PhysicsTable::resize(size_t siz, G4PhysicsVector* vec)
00092 {
00093   G4PhysCollection::resize(siz, vec);
00094   vecFlag.resize(siz, true);
00095 }
00096 
00097 G4bool G4PhysicsTable::StorePhysicsTable(const G4String& fileName,
00098                                          G4bool          ascii)
00099 {
00100   std::ofstream fOut;  
00101   
00102   // open output file //
00103   if (!ascii)
00104     { fOut.open(fileName, std::ios::out|std::ios::binary); }
00105   else
00106     { fOut.open(fileName, std::ios::out); }
00107 
00108   // check if the file has been opened successfully 
00109   if (!fOut)
00110   {
00111 #ifdef G4VERBOSE  
00112     G4cerr << "G4PhysicsTable::StorePhysicsTable():";
00113     G4cerr << " Cannot open file: " << fileName << G4endl;
00114 #endif
00115     fOut.close();
00116     return false;
00117   }
00118 
00119   // Number of elements
00120   size_t tableSize = size(); 
00121   if (!ascii)
00122   {
00123     fOut.write( (char*)(&tableSize), sizeof tableSize); 
00124   }
00125   else
00126   {
00127     fOut << tableSize << G4endl;
00128   }
00129 
00130   // Physics Vector
00131   for (G4PhysicsTableIterator itr=begin(); itr!=end(); ++itr)
00132   {
00133     G4int vType = (*itr)->GetType();
00134     if (!ascii)
00135     {
00136       fOut.write( (char*)(&vType), sizeof vType); 
00137     }
00138     else
00139     {
00140       fOut << vType << G4endl;
00141     }
00142     (*itr)->Store(fOut,ascii);
00143   }
00144   fOut.close();
00145   return true;
00146 }
00147 
00148 
00149 G4bool G4PhysicsTable::ExistPhysicsTable(const G4String& fileName) const
00150 {
00151   std::ifstream fIn;  
00152   G4bool value=true;
00153   // open input file
00154   fIn.open(fileName,std::ios::in);
00155 
00156   // check if the file has been opened successfully 
00157   if (!fIn)
00158   {
00159     value = false;
00160   }
00161   fIn.close();
00162   return value;
00163 }
00164     
00165 G4bool G4PhysicsTable::RetrievePhysicsTable(const G4String& fileName,
00166                                             G4bool          ascii)
00167 {
00168   std::ifstream fIn;  
00169   // open input file
00170   if (ascii)
00171     { fIn.open(fileName,std::ios::in|std::ios::binary); }
00172   else
00173     { fIn.open(fileName,std::ios::in);} 
00174 
00175   // check if the file has been opened successfully 
00176   if (!fIn)
00177   {
00178 #ifdef G4VERBOSE  
00179     G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
00180     G4cerr << " Cannot open file: " << fileName << G4endl;
00181 #endif
00182     fIn.close();
00183     return false;
00184   }
00185 
00186   // clear 
00187   clearAndDestroy();
00188   
00189   // Number of elements
00190   size_t tableSize=0; 
00191   if (!ascii)
00192   {
00193     fIn.read((char*)(&tableSize), sizeof tableSize); 
00194   }
00195   else
00196   {
00197     fIn >> tableSize;
00198   }
00199   reserve(tableSize); 
00200   vecFlag.clear();
00201 
00202   // Physics Vector
00203   for (size_t idx=0; idx<tableSize; ++idx)
00204   {
00205     G4int vType=0;
00206     if (!ascii)
00207     {
00208       fIn.read( (char*)(&vType), sizeof vType); 
00209     }
00210     else
00211     {
00212       fIn >>  vType;
00213     }
00214     G4PhysicsVector* pVec = CreatePhysicsVector(vType);
00215     if (pVec==0)
00216     {
00217 #ifdef G4VERBOSE  
00218       G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
00219       G4cerr << " Illegal Physics Vector type: " << vType << " in: ";
00220       G4cerr << fileName << G4endl;
00221 #endif          
00222       fIn.close();
00223       return false;
00224     }
00225 
00226     if (! (pVec->Retrieve(fIn,ascii)) )
00227     {
00228 #ifdef G4VERBOSE  
00229       G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
00230       G4cerr << " Rrror in retreiving " << idx
00231              << "-th Physics Vector from file: ";
00232       G4cerr << fileName << G4endl;
00233 #endif          
00234       fIn.close();
00235       return false;
00236     }
00237 
00238     // add a PhysicsVector to this PhysicsTable
00239     G4PhysCollection::push_back(pVec);
00240     vecFlag.push_back(true);
00241     
00242   } 
00243   fIn.close();
00244   return true;
00245 }
00246 
00247 std::ostream& operator<<(std::ostream& out, 
00248                          G4PhysicsTable& right)
00249 {
00250   // Printout Physics Vector
00251   size_t i=0;
00252   for (G4PhysicsTableIterator itr=right.begin(); itr!=right.end(); ++itr)
00253   {
00254     out << std::setw(8) << i << "-th Vector   ";
00255     out << ": Type    " << G4int((*itr)->GetType()) ;
00256     out << ": Flag    ";
00257     if (right.GetFlag(i))
00258     {
00259       out << " T";
00260     } 
00261     else
00262     {
00263       out << " F";
00264     } 
00265     out << G4endl;
00266     out << *(*itr);
00267     i +=1;
00268   }
00269   out << G4endl;
00270   return out; 
00271 }
00272 
00273 void G4PhysicsTable::ResetFlagArray()
00274 {
00275   size_t tableSize = G4PhysCollection::size(); 
00276   vecFlag.clear();
00277   for (size_t idx=0; idx<tableSize; idx++)
00278   {
00279     vecFlag.push_back(true);
00280   }
00281 }
00282 
00283 G4PhysicsVector* G4PhysicsTable::CreatePhysicsVector(G4int type)
00284 {
00285   G4PhysicsVector* pVector=0;
00286   switch (type)
00287   {
00288   case T_G4PhysicsLinearVector: 
00289     pVector = new G4PhysicsLinearVector();
00290     break;
00291 
00292   case T_G4PhysicsLogVector: 
00293     pVector = new G4PhysicsLogVector();
00294     break;
00295 
00296   case T_G4PhysicsLnVector: 
00297     pVector = new G4PhysicsLnVector();
00298     break;
00299 
00300   case T_G4PhysicsFreeVector: 
00301     pVector = new G4PhysicsFreeVector();
00302     break;
00303 
00304   case T_G4PhysicsOrderedFreeVector: 
00305     pVector = new G4PhysicsOrderedFreeVector();
00306     break;
00307 
00308   case T_G4LPhysicsFreeVector: 
00309     pVector = new G4LPhysicsFreeVector();
00310     break;
00311   
00312   default:
00313     break;
00314   }
00315   return pVector;
00316 }

Generated on Mon May 27 17:49:19 2013 for Geant4 by  doxygen 1.4.7