ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4DataVector.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4DataVector.cc
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 //
28 //
29 // --------------------------------------------------------------
30 // GEANT 4 class implementation file
31 //
32 // G4DataVector.cc
33 //
34 // History:
35 // 18 Sep. 2001, H.Kurashige : Structure created based on object model
36 // --------------------------------------------------------------
37 
38 #include "G4DataVector.hh"
39 #include <iomanip>
40 
42  : std::vector<G4double>()
43 {
44 }
45 
47  : std::vector<G4double>(cap, 0.0)
48 {
49 }
50 
52  : std::vector<G4double>(cap, value)
53 {
54 }
55 
57 {
58 }
59 
60 G4bool G4DataVector::Store(std::ofstream& fOut, G4bool ascii)
61 {
62  // Ascii mode
63  if (ascii)
64  {
65  fOut << *this;
66  return true;
67  }
68 
69  // Binary Mode
70  G4int sizeV = G4int(size());
71  fOut.write((char*)(&sizeV), sizeof sizeV);
72 
73  G4double* value = new G4double[sizeV];
74  size_t i=0;
75  for (const_iterator itr=begin(); itr!=end(); itr++, i++)
76  {
77  value[i] = *itr;
78  }
79  fOut.write((char*)(value), sizeV*(sizeof (G4double)) );
80  delete [] value;
81 
82  return true;
83 }
84 
85 G4bool G4DataVector::Retrieve(std::ifstream& fIn, G4bool ascii)
86 {
87  clear();
88  G4int sizeV=0;
89 
90  // retrieve in ascii mode
91  if (ascii)
92  {
93  // contents
94  fIn >> sizeV;
95  if (fIn.fail()) { return false; }
96  if (sizeV<=0)
97  {
98 #ifdef G4VERBOSE
99  G4cerr << "G4DataVector::Retrieve():";
100  G4cerr << " Invalid vector size: " << sizeV << G4endl;
101 #endif
102  return false;
103  }
104 
105  reserve(sizeV);
106  for(G4int i = 0; i < sizeV ; i++)
107  {
108  G4double vData=0.0;
109  fIn >> vData;
110  if (fIn.fail()) { return false; }
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 std::ostream& operator<<(std::ostream& out, const G4DataVector& pv)
137 {
138  out << pv.size() << std::setprecision(12) << G4endl;
139  for(size_t i = 0; i < pv.size(); i++)
140  {
141  out << pv[i] << G4endl;
142  }
143  out << std::setprecision(6);
144 
145  return out;
146 }
147