G4OrderedTable.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 //      G4OrderedTable
00034 //
00035 // ------------------------------------------------------------
00036 
00037 #include "G4DataVector.hh"
00038 #include "G4OrderedTable.hh"
00039 #include <iostream>
00040 #include <fstream>
00041 #include <iomanip>
00042 
00043 G4OrderedTable::G4OrderedTable()
00044   : std::vector<G4DataVector*>()
00045 {
00046 }
00047 
00048 G4OrderedTable::G4OrderedTable(size_t cap)
00049   : std::vector<G4DataVector*>(cap, (G4DataVector*)(0) )
00050 {
00051 }
00052 
00053 G4OrderedTable::~G4OrderedTable()
00054 {
00055 }
00056 
00057 G4bool G4OrderedTable::Store(const G4String& fileName,
00058                              G4bool          ascii)
00059 {
00060   std::ofstream fOut;  
00061   
00062   // open output file //
00063   if (!ascii)
00064     { fOut.open(fileName, std::ios::out|std::ios::binary); }
00065   else
00066     { fOut.open(fileName, std::ios::out); }
00067 
00068   // check if the file has been opened successfully 
00069   if (!fOut)
00070   {
00071 #ifdef G4VERBOSE  
00072     G4cerr << "G4OrderedTable::::Store():";
00073     G4cerr << " Cannot open file: " << fileName << G4endl;
00074 #endif
00075     fOut.close();
00076     return false;
00077   }
00078 
00079  // Number of elements
00080   size_t tableSize = size(); 
00081   if (!ascii)
00082   {
00083     fOut.write( (char*)(&tableSize), sizeof tableSize); 
00084   }
00085   else
00086   {
00087     fOut << tableSize << G4endl;
00088   }
00089 
00090   // Data Vector
00091   G4int vType = G4DataVector::T_G4DataVector;
00092   for (G4OrderedTableIterator itr=begin(); itr!=end(); ++itr)
00093   {
00094     if (!ascii)
00095     {
00096       fOut.write( (char*)(&vType), sizeof vType); 
00097     }
00098     else
00099     {
00100       fOut << vType << G4endl;
00101     }
00102     (*itr)->Store(fOut,ascii);
00103   }
00104   fOut.close();
00105   return true;
00106 }
00107 
00108 
00109 
00110 G4bool G4OrderedTable::Retrieve(const G4String& fileName,
00111                                 G4bool          ascii)
00112 {
00113   std::ifstream fIn;  
00114   // open input file //
00115   if (ascii)
00116     { fIn.open(fileName,std::ios::in|std::ios::binary); }
00117   else
00118     { fIn.open(fileName,std::ios::in); }
00119 
00120   // check if the file has been opened successfully 
00121   if (!fIn)
00122   {
00123 #ifdef G4VERBOSE  
00124     G4cerr << "G4OrderedTable::Retrieve():";
00125     G4cerr << " Cannot open file: " << fileName << G4endl;
00126 #endif
00127     fIn.close();
00128     return false;
00129   }
00130 
00131   // clear 
00132   clearAndDestroy();
00133   
00134   // Number of elements
00135   G4int tableSize=0; 
00136   if (!ascii)
00137   {
00138     fIn.read((char*)(&tableSize), sizeof tableSize); 
00139   }
00140   else
00141   {
00142     fIn >> tableSize;
00143   }
00144   if (tableSize<=0)
00145   {
00146 #ifdef G4VERBOSE  
00147     G4cerr << "G4OrderedTable::Retrieve():";
00148     G4cerr << " Invalid table size: " << tableSize << G4endl;
00149 #endif
00150     return false;
00151   }
00152   reserve(tableSize); 
00153 
00154   // Physics Vector
00155   for (G4int idx=0; idx<tableSize; ++idx)
00156   {
00157     G4int vType=0;
00158     if (!ascii)
00159     {
00160       fIn.read( (char*)(&vType), sizeof vType); 
00161     }
00162     else
00163     {
00164       fIn >>  vType;
00165     }
00166     if (vType != G4DataVector::T_G4DataVector)
00167     {
00168 #ifdef G4VERBOSE  
00169       G4cerr << "G4OrderedTable::Retrieve():";
00170       G4cerr << " Illegal Data Vector type: " << vType << " in  ";
00171       G4cerr << fileName << G4endl;
00172 #endif          
00173       fIn.close();
00174       return false;
00175     }
00176 
00177     G4DataVector* pVec = new G4DataVector;
00178 
00179     if (! (pVec->Retrieve(fIn,ascii)) )
00180     {
00181 #ifdef G4VERBOSE  
00182       G4cerr << "G4OrderedTable::Retrieve(): ";
00183       G4cerr << " Rrror in retreiving " << idx
00184              << "-th Physics Vector from file: ";
00185       G4cerr << fileName << G4endl;
00186 #endif          
00187       fIn.close();
00188       delete pVec;
00189       return false;
00190     }
00191 
00192     // add a PhysicsVector to this OrderedTable
00193     push_back(pVec);
00194   } 
00195   fIn.close();
00196   return true;
00197 }
00198 
00199 std::ostream& operator<<(std::ostream& out, 
00200                          G4OrderedTable& right)
00201 {
00202   // Printout Data Vector
00203   size_t i=0;
00204   for (G4OrderedTableIterator itr=right.begin(); itr!=right.end(); ++itr)
00205   {
00206     out << std::setw(8) << i << "-th Vector   ";
00207     out << ": Type    " << G4DataVector::T_G4DataVector << G4endl;
00208     out << *(*itr);
00209     i +=1;
00210   }
00211   out << G4endl;
00212   return out; 
00213 }

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