source: trunk/source/global/management/src/G4OrderedTable.cc @ 1358

Last change on this file since 1358 was 1340, checked in by garnier, 14 years ago

update ti head

File size: 5.5 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// $Id: G4OrderedTable.cc,v 1.8 2010/11/01 13:55:53 gcosmo Exp $
28// GEANT4 tag $Name: global-V09-03-22 $
29//
30//
31// ------------------------------------------------------------
32//      GEANT 4 class implementation
33//
34//      G4OrderedTable
35//
36// ------------------------------------------------------------
37
38#include "G4DataVector.hh"
39#include "G4OrderedTable.hh"
40#include <iostream>
41#include <fstream>
42#include <iomanip>
43
44G4OrderedTable::G4OrderedTable()
45  : std::vector<G4DataVector*>()
46{
47}
48
49G4OrderedTable::G4OrderedTable(size_t cap)
50  : std::vector<G4DataVector*>(cap, (G4DataVector*)(0) )
51{
52}
53
54G4OrderedTable::~G4OrderedTable()
55{
56}
57
58G4bool G4OrderedTable::Store(const G4String& fileName,
59                             G4bool          ascii)
60{
61  std::ofstream fOut; 
62 
63  // open output file //
64  if (!ascii)
65    { fOut.open(fileName, std::ios::out|std::ios::binary); }
66  else
67    { fOut.open(fileName, std::ios::out); }
68
69  // check if the file has been opened successfully
70  if (!fOut)
71  {
72#ifdef G4VERBOSE 
73    G4cerr << "G4OrderedTable::::Store():";
74    G4cerr << " Cannot open file: " << fileName << G4endl;
75#endif
76    fOut.close();
77    return false;
78  }
79
80 // Number of elements
81  size_t tableSize = size(); 
82  if (!ascii)
83  {
84    fOut.write( (char*)(&tableSize), sizeof tableSize); 
85  }
86  else
87  {
88    fOut << tableSize << G4endl;
89  }
90
91  // Data Vector
92  G4int vType = G4DataVector::T_G4DataVector;
93  for (G4OrderedTableIterator itr=begin(); itr!=end(); ++itr)
94  {
95    if (!ascii)
96    {
97      fOut.write( (char*)(&vType), sizeof vType); 
98    }
99    else
100    {
101      fOut << vType << G4endl;
102    }
103    (*itr)->Store(fOut,ascii);
104  }
105  fOut.close();
106  return true;
107}
108
109
110
111G4bool G4OrderedTable::Retrieve(const G4String& fileName,
112                                G4bool          ascii)
113{
114  std::ifstream fIn; 
115  // open input file //
116  if (ascii)
117    { fIn.open(fileName,std::ios::in|std::ios::binary); }
118  else
119    { fIn.open(fileName,std::ios::in); }
120
121  // check if the file has been opened successfully
122  if (!fIn)
123  {
124#ifdef G4VERBOSE 
125    G4cerr << "G4OrderedTable::Retrieve():";
126    G4cerr << " Cannot open file: " << fileName << G4endl;
127#endif
128    fIn.close();
129    return false;
130  }
131
132  // clear
133  clearAndDestroy();
134 
135  // Number of elements
136  G4int tableSize=0; 
137  if (!ascii)
138  {
139    fIn.read((char*)(&tableSize), sizeof tableSize); 
140  }
141  else
142  {
143    fIn >> tableSize;
144  }
145  if (tableSize<=0)
146  {
147#ifdef G4VERBOSE 
148    G4cerr << "G4OrderedTable::Retrieve():";
149    G4cerr << " Invalid table size: " << tableSize << G4endl;
150#endif
151    return false;
152  }
153  reserve(tableSize); 
154
155  // Physics Vector
156  for (G4int idx=0; idx<tableSize; ++idx)
157  {
158    G4int vType=0;
159    if (!ascii)
160    {
161      fIn.read( (char*)(&vType), sizeof vType); 
162    }
163    else
164    {
165      fIn >>  vType;
166    }
167    if (vType != G4DataVector::T_G4DataVector)
168    {
169#ifdef G4VERBOSE 
170      G4cerr << "G4OrderedTable::Retrieve():";
171      G4cerr << " Illegal Data Vector type: " << vType << " in  ";
172      G4cerr << fileName << G4endl;
173#endif         
174      fIn.close();
175      return false;
176    }
177
178    G4DataVector* pVec = new G4DataVector;
179
180    if (! (pVec->Retrieve(fIn,ascii)) )
181    {
182#ifdef G4VERBOSE 
183      G4cerr << "G4OrderedTable::Retrieve(): ";
184      G4cerr << " Rrror in retreiving " << idx
185             << "-th Physics Vector from file: ";
186      G4cerr << fileName << G4endl;
187#endif         
188      fIn.close();
189      delete pVec;
190      return false;
191    }
192
193    // add a PhysicsVector to this OrderedTable
194    push_back(pVec);
195  } 
196  fIn.close();
197  return true;
198}
199
200std::ostream& operator<<(std::ostream& out, 
201                         G4OrderedTable& right)
202{
203  // Printout Data Vector
204  size_t i=0;
205  for (G4OrderedTableIterator itr=right.begin(); itr!=right.end(); ++itr)
206  {
207    out << std::setw(8) << i << "-th Vector   ";
208    out << ": Type    " << G4DataVector::T_G4DataVector << G4endl;
209    out << *(*itr);
210    i +=1;
211  }
212  out << G4endl;
213  return out; 
214}
Note: See TracBrowser for help on using the repository browser.