Belle II Software  release-08-01-10
MilleBinary.cc
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 
33 namespace gbl {
34 
36 
41 MilleBinary::MilleBinary(const std::string &fileName, bool doublePrec,
42  unsigned int aSize) :
43  binaryFile(fileName.c_str(), std::ios::binary | std::ios::out), intBuffer(), floatBuffer(), doubleBuffer(), doublePrecision(
44  doublePrec) {
45  intBuffer.reserve(aSize);
46  intBuffer.push_back(0); // first word is error counter
47  if (doublePrecision) {
48  doubleBuffer.reserve(aSize);
49  doubleBuffer.push_back(0.);
50 
51  } else {
52  floatBuffer.reserve(aSize);
53  floatBuffer.push_back(0.);
54  }
55 }
56 
57 MilleBinary::~MilleBinary() {
58  binaryFile.close();
59 }
60 
62 
70 void MilleBinary::addData(double aMeas, double aErr,
71  const std::vector<unsigned int> &indLocal,
72  const std::vector<double> &derLocal, const std::vector<int> &labGlobal,
73  const std::vector<double> &derGlobal) {
74 
75  if (doublePrecision) {
76  // double values
77  intBuffer.push_back(0);
78  doubleBuffer.push_back(aMeas);
79  for (unsigned int i = 0; i < indLocal.size(); ++i) {
80  intBuffer.push_back(indLocal[i]);
81  doubleBuffer.push_back(derLocal[i]);
82  }
83  intBuffer.push_back(0);
84  doubleBuffer.push_back(aErr);
85  for (unsigned int i = 0; i < labGlobal.size(); ++i) {
86  if (derGlobal[i]) {
87  intBuffer.push_back(labGlobal[i]);
88  doubleBuffer.push_back(derGlobal[i]);
89  }
90  }
91  } else {
92  // float values
93  intBuffer.push_back(0);
94  floatBuffer.push_back(aMeas);
95  for (unsigned int i = 0; i < indLocal.size(); ++i) {
96  intBuffer.push_back(indLocal[i]);
97  floatBuffer.push_back(derLocal[i]);
98  }
99  intBuffer.push_back(0);
100  floatBuffer.push_back(aErr);
101  for (unsigned int i = 0; i < labGlobal.size(); ++i) {
102  if (derGlobal[i]) {
103  intBuffer.push_back(labGlobal[i]);
104  floatBuffer.push_back(derGlobal[i]);
105  }
106  }
107  }
108 }
109 
112 
113  const int recordLength =
114  (doublePrecision) ? -intBuffer.size() * 2 : intBuffer.size() * 2;
115  binaryFile.write(reinterpret_cast<const char*>(&recordLength),
116  sizeof(recordLength));
117  if (doublePrecision)
118  binaryFile.write(reinterpret_cast<char*>(&doubleBuffer[0]),
119  doubleBuffer.size() * sizeof(doubleBuffer[0]));
120  else
121  binaryFile.write(reinterpret_cast<char*>(&floatBuffer[0]),
122  floatBuffer.size() * sizeof(floatBuffer[0]));
123  binaryFile.write(reinterpret_cast<char*>(&intBuffer[0]),
124  intBuffer.size() * sizeof(intBuffer[0]));
125 // start with new record
126  intBuffer.resize(1);
127  if (doublePrecision)
128  doubleBuffer.resize(1);
129  else
130  floatBuffer.resize(1);
131 }
132 }
MilleBinary definition.
void writeRecord()
Write record to file.
Definition: MilleBinary.cc:111
std::vector< double > doubleBuffer
Double buffer.
Definition: MilleBinary.h:84
bool doublePrecision
Flag for storage in as double values.
Definition: MilleBinary.h:85
MilleBinary(const std::string &fileName="milleBinaryISN.dat", bool doublePrec=false, unsigned int aSize=2000)
Create binary file.
Definition: MilleBinary.cc:41
std::vector< int > intBuffer
Integer buffer.
Definition: MilleBinary.h:82
std::ofstream binaryFile
Binary File.
Definition: MilleBinary.h:81
std::vector< float > floatBuffer
Float buffer.
Definition: MilleBinary.h:83
void addData(double aMeas, double aPrec, const std::vector< unsigned int > &indLocal, const std::vector< double > &derLocal, const std::vector< int > &labGlobal, const std::vector< double > &derGlobal)
Add data block to (end of) record.
Definition: MilleBinary.cc:70
Namespace for the general broken lines package.