Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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// G4DataVector class implementation
27//
28// Author: H.Kurashige, 18 September 2001
29// --------------------------------------------------------------------
30
31#include "G4DataVector.hh"
32#include <iomanip>
33
34// --------------------------------------------------------------------
36 : std::vector<G4double>()
37{}
38
39// --------------------------------------------------------------------
41 : std::vector<G4double>(cap, 0.0)
42{}
43
44// --------------------------------------------------------------------
45G4DataVector::G4DataVector(std::size_t cap, G4double value)
46 : std::vector<G4double>(cap, value)
47{}
48
49// --------------------------------------------------------------------
51
52// --------------------------------------------------------------------
53G4bool G4DataVector::Store(std::ofstream& fOut, G4bool ascii)
54{
55 // Ascii mode
56 if(ascii)
57 {
58 fOut << *this;
59 return true;
60 }
61
62 // Binary Mode
63 G4int sizeV = G4int(size());
64 fOut.write((char*) (&sizeV), sizeof sizeV);
65
66 G4double* value = new G4double[sizeV];
67 std::size_t i = 0;
68 for(auto itr = cbegin(); itr != cend(); ++itr, ++i)
69 {
70 value[i] = *itr;
71 }
72 fOut.write((char*) (value), sizeV * (sizeof(G4double)));
73 delete[] value;
74
75 return true;
76}
77
78// --------------------------------------------------------------------
79G4bool G4DataVector::Retrieve(std::ifstream& fIn, G4bool ascii)
80{
81 clear();
82 G4int sizeV = 0;
83
84 // retrieve in ascii mode
85 if(ascii)
86 {
87 // contents
88 fIn >> sizeV;
89 if(fIn.fail())
90 {
91 return false;
92 }
93 if(sizeV <= 0)
94 {
95#ifdef G4VERBOSE
96 G4cerr << "G4DataVector::Retrieve():";
97 G4cerr << " Invalid vector size: " << sizeV << G4endl;
98#endif
99 return false;
100 }
101
102 reserve(sizeV);
103 for(G4int i = 0; i < sizeV; ++i)
104 {
105 G4double vData = 0.0;
106 fIn >> vData;
107 if(fIn.fail())
108 {
109 return false;
110 }
111 push_back(vData);
112 }
113 return true;
114 }
115
116 // retrieve in binary mode
117 fIn.read((char*) (&sizeV), sizeof sizeV);
118
119 G4double* value = new G4double[sizeV];
120 fIn.read((char*) (value), sizeV * (sizeof(G4double)));
121 if(G4int(fIn.gcount()) != G4int(sizeV * (sizeof(G4double))))
122 {
123 delete[] value;
124 return false;
125 }
126
127 reserve(sizeV);
128 for(G4int i = 0; i < sizeV; ++i)
129 {
130 push_back(value[i]);
131 }
132 delete[] value;
133 return true;
134}
135
136// --------------------------------------------------------------------
137std::ostream& operator<<(std::ostream& out, const G4DataVector& pv)
138{
139 out << pv.size() << std::setprecision(12) << G4endl;
140 for(std::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}
std::ostream & operator<<(std::ostream &out, const G4DataVector &pv)
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
virtual ~G4DataVector()
Definition: G4DataVector.cc:50
G4bool Store(std::ofstream &fOut, G4bool ascii=false)
Definition: G4DataVector.cc:53
G4bool Retrieve(std::ifstream &fIn, G4bool ascii=false)
Definition: G4DataVector.cc:79