GeneralBrokenLines V03-01-03
using EIGEN
MilleBinary.cpp
Go to the documentation of this file.
1/*
2 * MilleBinary.cpp
3 *
4 * Created on: Aug 31, 2011
5 * Author: kleinwrt
6 */
7
30#include "MilleBinary.h"
31
33namespace gbl {
34
36
42MilleBinary::MilleBinary(const std::string& fileName, bool doublePrec,
43 bool keepZeros, unsigned int aSize) :
44 binaryFile(fileName.c_str(), std::ios::binary | std::ios::out), intBuffer(), floatBuffer(), doubleBuffer(), doublePrecision(
45 doublePrec), globalDerKeepZeros(keepZeros) {
46
47 intBuffer.reserve(aSize);
48 intBuffer.push_back(0); // first word is error counter
49 if (doublePrecision) {
50 doubleBuffer.reserve(aSize);
51 doubleBuffer.push_back(0.);
52
53 } else {
54 floatBuffer.reserve(aSize);
55 floatBuffer.push_back(0.);
56 }
57}
58
60 binaryFile.close();
61}
62
64
73void MilleBinary::addData(double aMeas, double aErr, unsigned int numLocal,
74 unsigned int* indLocal, double* derLocal,
75 const std::vector<int> &labGlobal,
76 const std::vector<double> &derGlobal) {
77
78 if (doublePrecision) {
79 // double values
80 intBuffer.push_back(0);
81 doubleBuffer.push_back(aMeas);
82 for (unsigned int i = 0; i < numLocal; ++i) {
83 intBuffer.push_back(indLocal[i]);
84 doubleBuffer.push_back(derLocal[i]);
85 }
86 intBuffer.push_back(0);
87 doubleBuffer.push_back(aErr);
88 for (unsigned int i = 0; i < labGlobal.size(); ++i) {
89 if (derGlobal[i] or globalDerKeepZeros) {
90 intBuffer.push_back(labGlobal[i]);
91 doubleBuffer.push_back(derGlobal[i]);
92 }
93 }
94 } else {
95 // float values
96 intBuffer.push_back(0);
97 floatBuffer.push_back(aMeas);
98 for (unsigned int i = 0; i < numLocal; ++i) {
99 intBuffer.push_back(indLocal[i]);
100 floatBuffer.push_back(derLocal[i]);
101 }
102 intBuffer.push_back(0);
103 floatBuffer.push_back(aErr);
104 for (unsigned int i = 0; i < labGlobal.size(); ++i) {
105 if (derGlobal[i] or globalDerKeepZeros) {
106 intBuffer.push_back(labGlobal[i]);
107 floatBuffer.push_back(derGlobal[i]);
108 }
109 }
110 }
111}
112
115
116 const int recordLength =
117 (doublePrecision) ? -intBuffer.size() * 2 : intBuffer.size() * 2;
118 binaryFile.write(reinterpret_cast<const char*>(&recordLength),
119 sizeof(recordLength));
120 if (doublePrecision)
121 binaryFile.write(reinterpret_cast<char*>(&doubleBuffer[0]),
122 doubleBuffer.size() * sizeof(doubleBuffer[0]));
123 else
124 binaryFile.write(reinterpret_cast<char*>(&floatBuffer[0]),
125 floatBuffer.size() * sizeof(floatBuffer[0]));
126 binaryFile.write(reinterpret_cast<char*>(&intBuffer[0]),
127 intBuffer.size() * sizeof(intBuffer[0]));
128 // start with new record
129 intBuffer.resize(1);
130 if (doublePrecision)
131 doubleBuffer.resize(1);
132 else
133 floatBuffer.resize(1);
134}
135}
MilleBinary definition.
MilleBinary(const std::string &fileName="milleBinaryISN.dat", bool doublePrec=false, bool keepZeros=false, unsigned int aSize=2000)
Create binary file.
Definition: MilleBinary.cpp:42
std::vector< float > floatBuffer
Float buffer.
Definition: MilleBinary.h:96
virtual ~MilleBinary()
Definition: MilleBinary.cpp:59
std::vector< double > doubleBuffer
Double buffer.
Definition: MilleBinary.h:97
void writeRecord()
Write record to file.
bool doublePrecision
Flag for storage in as double values.
Definition: MilleBinary.h:98
bool globalDerKeepZeros
Flag for keeping global derivatives with value zero.
Definition: MilleBinary.h:99
std::vector< int > intBuffer
Integer buffer.
Definition: MilleBinary.h:95
void addData(double aMeas, double aErr, unsigned int numLocal, unsigned int *indLocal, double *derLocal, const std::vector< int > &labGlobal, const std::vector< double > &derGlobal)
Add data block to (end of) record.
Definition: MilleBinary.cpp:73
std::ofstream binaryFile
Binary File.
Definition: MilleBinary.h:94
Namespace for the general broken lines package.