Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MilleBinary.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MilleBinary.cc
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 
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 }