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

Generated on Fri Apr 11 17:09:57 2008 for Geant4 by  doxygen 1.4.7