CMS 3D CMS Logo

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