Geant4  10.03
G4DataVector.cc
Go to the documentation of this file.
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: G4DataVector.cc 67970 2013-03-13 10:10:06Z gcosmo $
28 //
29 //
30 // --------------------------------------------------------------
31 // GEANT 4 class implementation file
32 //
33 // G4DataVector.cc
34 //
35 // History:
36 // 18 Sep. 2001, H.Kurashige : Structure created based on object model
37 // --------------------------------------------------------------
38 
39 #include "G4DataVector.hh"
40 #include <iomanip>
41 
43  : std::vector<G4double>()
44 {
45 }
46 
48  : std::vector<G4double>(cap, 0.0)
49 {
50 }
51 
53  : std::vector<G4double>(cap, value)
54 {
55 }
56 
58 {
59 }
60 
61 G4bool G4DataVector::Store(std::ofstream& fOut, G4bool ascii)
62 {
63  // Ascii mode
64  if (ascii)
65  {
66  fOut << *this;
67  return true;
68  }
69 
70  // Binary Mode
71  size_t sizeV = size();
72  fOut.write((char*)(&sizeV), sizeof sizeV);
73 
74  G4double* value = new G4double[sizeV];
75  size_t i=0;
76  for (const_iterator itr=begin(); itr!=end(); itr++, i++)
77  {
78  value[i] = *itr;
79  }
80  fOut.write((char*)(value), sizeV*(sizeof (G4double)) );
81  delete [] value;
82 
83  return true;
84 }
85 
86 G4bool G4DataVector::Retrieve(std::ifstream& fIn, G4bool ascii)
87 {
88  clear();
89  G4int sizeV=0;
90 
91  // retrieve in ascii mode
92  if (ascii)
93  {
94  // contents
95  fIn >> sizeV;
96  if (fIn.fail()) { return false; }
97  if (sizeV<=0)
98  {
99 #ifdef G4VERBOSE
100  G4cerr << "G4DataVector::Retrieve():";
101  G4cerr << " Invalid vector size: " << sizeV << G4endl;
102 #endif
103  return false;
104  }
105 
106  reserve(sizeV);
107  for(G4int i = 0; i < sizeV ; i++)
108  {
109  G4double vData=0.0;
110  fIn >> vData;
111  if (fIn.fail()) { return false; }
112  push_back(vData);
113  }
114  return true ;
115  }
116 
117  // retrieve in binary mode
118  fIn.read((char*)(&sizeV), sizeof sizeV);
119 
120  G4double* value = new G4double[sizeV];
121  fIn.read((char*)(value), sizeV*(sizeof(G4double)) );
122  if (G4int(fIn.gcount()) != G4int(sizeV*(sizeof(G4double))) )
123  {
124  delete [] value;
125  return false;
126  }
127 
128  reserve(sizeV);
129  for(G4int i = 0; i < sizeV; i++)
130  {
131  push_back(value[i]);
132  }
133  delete [] value;
134  return true;
135 }
136 
137 std::ostream& operator<<(std::ostream& out, const G4DataVector& pv)
138 {
139  out << pv.size() << std::setprecision(12) << G4endl;
140  for(size_t i = 0; i < pv.size(); i++)
141  {
142  out << pv[i] << G4endl;
143  }
144  out << std::setprecision(6);
145 
146  return out;
147 }
148 
int G4int
Definition: G4Types.hh:78
G4bool Retrieve(std::ifstream &fIn, G4bool ascii=false)
Definition: G4DataVector.cc:86
bool G4bool
Definition: G4Types.hh:79
virtual ~G4DataVector()
Definition: G4DataVector.cc:57
G4bool Store(std::ofstream &fOut, G4bool ascii=false)
Definition: G4DataVector.cc:61
std::ostream & operator<<(std::ostream &out, const G4DataVector &pv)
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
G4GLOB_DLL std::ostream G4cerr