Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4DataVector class implementation << 27 // 26 // 28 // Author: H.Kurashige, 18 September 2001 << 27 // $Id: G4DataVector.cc,v 1.12 2010-11-01 13:55:53 gcosmo Exp $ 29 // ------------------------------------------- << 28 // GEANT4 tag $Name: geant4-09-04-patch-02 $ >> 29 // >> 30 // >> 31 // -------------------------------------------------------------- >> 32 // GEANT 4 class implementation file >> 33 // >> 34 // G4DataVector.cc >> 35 // >> 36 // History: >> 37 // 18 Sep. 2001, H.Kurashige : Structure created based on object model >> 38 // -------------------------------------------------------------- 30 39 31 #include "G4DataVector.hh" 40 #include "G4DataVector.hh" 32 #include <iomanip> 41 #include <iomanip> 33 42 34 // ------------------------------------------- << 43 G4DataVector::G4DataVector() 35 G4DataVector::G4DataVector(std::size_t cap) << 44 : std::vector<G4double>() >> 45 { >> 46 } >> 47 >> 48 G4DataVector::G4DataVector(size_t cap) 36 : std::vector<G4double>(cap, 0.0) 49 : std::vector<G4double>(cap, 0.0) 37 {} << 50 { >> 51 } 38 52 39 // ------------------------------------------- << 53 G4DataVector::G4DataVector(size_t cap, G4double value) 40 G4DataVector::G4DataVector(std::size_t cap, G4 << 41 : std::vector<G4double>(cap, value) 54 : std::vector<G4double>(cap, value) 42 {} << 55 { >> 56 } >> 57 >> 58 G4DataVector::~G4DataVector() >> 59 { >> 60 } 43 61 44 // ------------------------------------------- << 45 G4bool G4DataVector::Store(std::ofstream& fOut 62 G4bool G4DataVector::Store(std::ofstream& fOut, G4bool ascii) 46 { 63 { 47 // Ascii mode 64 // Ascii mode 48 if(ascii) << 65 if (ascii) 49 { 66 { 50 fOut << *this; 67 fOut << *this; 51 return true; 68 return true; 52 } << 69 } 53 70 54 // Binary Mode 71 // Binary Mode 55 auto sizeV = G4int(size()); << 72 size_t sizeV = size(); 56 fOut.write((char*) (&sizeV), sizeof sizeV); << 73 fOut.write((char*)(&sizeV), sizeof sizeV); 57 << 58 auto value = new G4double[sizeV]; << 59 std::size_t i = 0; << 60 for(auto itr = cbegin(); itr != cend(); ++it << 61 { << 62 value[i] = *itr; << 63 } << 64 fOut.write((char*) (value), sizeV * (sizeof( << 65 delete[] value; << 66 74 >> 75 G4double* value = new G4double[sizeV]; >> 76 size_t i=0; >> 77 for (const_iterator itr=begin(); itr!=end(); itr++, i++) >> 78 { >> 79 value[i] = *itr; >> 80 } >> 81 fOut.write((char*)(value), sizeV*(sizeof (G4double)) ); >> 82 delete [] value; >> 83 67 return true; 84 return true; 68 } 85 } 69 86 70 // ------------------------------------------- << 71 G4bool G4DataVector::Retrieve(std::ifstream& f 87 G4bool G4DataVector::Retrieve(std::ifstream& fIn, G4bool ascii) 72 { 88 { 73 clear(); 89 clear(); 74 G4int sizeV = 0; << 90 G4int sizeV=0; 75 << 91 76 // retrieve in ascii mode 92 // retrieve in ascii mode 77 if(ascii) << 93 if (ascii) 78 { 94 { 79 // contents 95 // contents 80 fIn >> sizeV; 96 fIn >> sizeV; 81 if(fIn.fail()) << 97 if (fIn.fail()) { return false; } >> 98 if (sizeV<=0) 82 { 99 { 83 return false; << 100 #ifdef G4VERBOSE 84 } << 85 if(sizeV <= 0) << 86 { << 87 #ifdef G4VERBOSE << 88 G4cerr << "G4DataVector::Retrieve():"; 101 G4cerr << "G4DataVector::Retrieve():"; 89 G4cerr << " Invalid vector size: " << si 102 G4cerr << " Invalid vector size: " << sizeV << G4endl; 90 #endif 103 #endif 91 return false; 104 return false; 92 } 105 } 93 106 94 reserve(sizeV); 107 reserve(sizeV); 95 for(G4int i = 0; i < sizeV; ++i) << 108 for(G4int i = 0; i < sizeV ; i++) 96 { 109 { 97 G4double vData = 0.0; << 110 G4double vData=0.0; 98 fIn >> vData; 111 fIn >> vData; 99 if(fIn.fail()) << 112 if (fIn.fail()) { return false; } 100 { << 101 return false; << 102 } << 103 push_back(vData); 113 push_back(vData); 104 } 114 } 105 return true; << 115 return true ; 106 } 116 } 107 << 117 108 // retrieve in binary mode 118 // retrieve in binary mode 109 fIn.read((char*) (&sizeV), sizeof sizeV); << 119 fIn.read((char*)(&sizeV), sizeof sizeV); 110 << 120 111 auto value = new G4double[sizeV]; << 121 G4double* value = new G4double[sizeV]; 112 fIn.read((char*) (value), sizeV * (sizeof(G4 << 122 fIn.read((char*)(value), sizeV*(sizeof(G4double)) ); 113 if(G4int(fIn.gcount()) != G4int(sizeV * (siz << 123 if (G4int(fIn.gcount()) != G4int(sizeV*(sizeof(G4double))) ) 114 { 124 { 115 delete[] value; << 125 delete [] value; 116 return false; 126 return false; 117 } 127 } 118 128 119 reserve(sizeV); 129 reserve(sizeV); 120 for(G4int i = 0; i < sizeV; ++i) << 130 for(G4int i = 0; i < sizeV; i++) 121 { 131 { 122 push_back(value[i]); 132 push_back(value[i]); 123 } 133 } 124 delete[] value; << 134 delete [] value; 125 return true; 135 return true; 126 } 136 } 127 << 137 128 // ------------------------------------------- << 129 std::ostream& operator<<(std::ostream& out, co 138 std::ostream& operator<<(std::ostream& out, const G4DataVector& pv) 130 { 139 { 131 out << pv.size() << std::setprecision(12) << << 140 out << pv.size() << std::setprecision(12) << G4endl; 132 for(std::size_t i = 0; i < pv.size(); ++i) << 141 for(size_t i = 0; i < pv.size(); i++) 133 { 142 { 134 out << pv[i] << G4endl; 143 out << pv[i] << G4endl; 135 } 144 } 136 out << std::setprecision(6); 145 out << std::setprecision(6); 137 146 138 return out; 147 return out; 139 } 148 } >> 149 140 150