CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Mille.cc
Go to the documentation of this file.
1 
10 #include "Mille.h"
11 
12 #include <iostream>
13 
14 //___________________________________________________________________________
15 
16 Mille::Mille(const char *outFileName, bool asBinary, bool writeZero) :
17  myOutFile(outFileName, (asBinary ? (std::ios::binary | std::ios::out) : std::ios::out)),
18  myAsBinary(asBinary), myWriteZero(writeZero), myBufferPos(-1), myHasSpecial(false)
19 {
20  // opens outFileName, by default as binary file
21 
22  // Instead myBufferPos(-1), myHasSpecial(false) and the following two lines
23  // we could call newSet() and kill()...
24  myBufferInt[0] = 0;
25  myBufferFloat[0] = 0.;
26 
27  if (!myOutFile.is_open()) {
28  std::cerr << "Mille::Mille: Could not open " << outFileName
29  << " as output file." << std::endl;
30  }
31 }
32 
33 //___________________________________________________________________________
34 
36 {
37  // closes file
38  myOutFile.close();
39 }
40 
41 //___________________________________________________________________________
42 
43 void Mille::mille(int NLC, const float *derLc,
44  int NGL, const float *derGl, const int *label,
45  float rMeas, float sigma)
46 {
47  if (sigma <= 0.) return;
48  if (myBufferPos == -1) this->newSet(); // start, e.g. new track
49  if (!this->checkBufferSize(NLC, NGL)) return;
50 
51  // first store measurement
52  ++myBufferPos;
53  myBufferFloat[myBufferPos] = rMeas;
55 
56  // store local derivatives and local 'lables' 1,...,NLC
57  for (int i = 0; i < NLC; ++i) {
58  if (derLc[i] || myWriteZero) { // by default store only non-zero derivatives
59  ++myBufferPos;
60  myBufferFloat[myBufferPos] = derLc[i]; // local derivatives
61  myBufferInt [myBufferPos] = i+1; // index of local parameter
62  }
63  }
64 
65  // store uncertainty of measurement in between locals and globals
66  ++myBufferPos;
67  myBufferFloat[myBufferPos] = sigma;
69 
70  // store global derivatives and their lables
71  for (int i = 0; i < NGL; ++i) {
72  if (derGl[i] || myWriteZero) { // by default store only non-zero derivatives
73  if ((label[i] > 0 || myWriteZero) && label[i] <= myMaxLabel) { // and for valid labels
74  ++myBufferPos;
75  myBufferFloat[myBufferPos] = derGl[i]; // global derivatives
76  myBufferInt [myBufferPos] = label[i]; // index of global parameter
77  } else {
78  std::cerr << "Mille::mille: Invalid label " << label[i]
79  << " <= 0 or > " << myMaxLabel << std::endl;
80  }
81  }
82  }
83 }
84 
85 //___________________________________________________________________________
86 void Mille::special(int nSpecial, const float *floatings, const int *integers)
87 {
88  if (nSpecial == 0) return;
89  if (myBufferPos == -1) this->newSet(); // start, e.g. new track
90  if (myHasSpecial) {
91  std::cerr << "Mille::special: Special values already stored for this record."
92  << std::endl;
93  return;
94  }
95  if (!this->checkBufferSize(nSpecial, 0)) return;
96  myHasSpecial = true; // after newSet() (Note: MILLSP sets to buffer position...)
97 
98  // myBufferFloat[.] | myBufferInt[.]
99  // ------------------------------------
100  // 0.0 | 0
101  // -float(nSpecial) | 0
102  // The above indicates special data, following are nSpecial floating and nSpecial integer data.
103 
104  ++myBufferPos; // zero pair
106  myBufferInt [myBufferPos] = 0;
107 
108  ++myBufferPos; // nSpecial and zero
109  myBufferFloat[myBufferPos] = -nSpecial; // automatic conversion to float
110  myBufferInt [myBufferPos] = 0;
111 
112  for (int i = 0; i < nSpecial; ++i) {
113  ++myBufferPos;
114  myBufferFloat[myBufferPos] = floatings[i];
115  myBufferInt [myBufferPos] = integers[i];
116  }
117 }
118 
119 //___________________________________________________________________________
120 
122 {
123  // reset buffers, i.e. kill derivatives accumulated for current set
124  myBufferPos = -1;
125 }
126 
127 //___________________________________________________________________________
128 
130 {
131  // write set of derivatives with same local parameters to file
132  if (myBufferPos > 0) { // only if anything stored...
133  const int numWordsToWrite = (myBufferPos + 1)*2;
134 
135  if (myAsBinary) {
136  myOutFile.write(reinterpret_cast<const char*>(&numWordsToWrite),
137  sizeof(numWordsToWrite));
138  myOutFile.write(reinterpret_cast<char*>(myBufferFloat),
139  (myBufferPos+1) * sizeof(myBufferFloat[0]));
140  myOutFile.write(reinterpret_cast<char*>(myBufferInt),
141  (myBufferPos+1) * sizeof(myBufferInt[0]));
142  } else {
143  myOutFile << numWordsToWrite << "\n";
144  for (int i = 0; i < myBufferPos+1; ++i) {
145  myOutFile << myBufferFloat[i] << " ";
146  }
147  myOutFile << "\n";
148 
149  for (int i = 0; i < myBufferPos+1; ++i) {
150  myOutFile << myBufferInt[i] << " ";
151  }
152  myOutFile << "\n";
153  }
154  }
155  myBufferPos = -1; // reset buffer for next set of derivatives
156 }
157 
158 //___________________________________________________________________________
159 
161 {
162  // initilise for new set of locals, e.g. new track
163  myBufferPos = 0;
164  myHasSpecial = false;
165  myBufferFloat[0] = 0.0;
166  myBufferInt [0] = 0; // position 0 used as error counter
167 }
168 
169 //___________________________________________________________________________
170 
171 bool Mille::checkBufferSize(int nLocal, int nGlobal)
172 {
173  // enough space for next nLocal + nGlobal derivatives incl. measurement?
174 
175  if (myBufferPos + nLocal + nGlobal + 2 >= myBufferSize) {
176  ++(myBufferInt[0]); // increase error count
177  std::cerr << "Mille::checkBufferSize: Buffer too short ("
178  << myBufferSize << "),"
179  << "\n need space for nLocal (" << nLocal<< ")"
180  << "/nGlobal (" << nGlobal << ") local/global derivatives, "
181  << myBufferPos + 1 << " already stored!"
182  << std::endl;
183  return false;
184  } else {
185  return true;
186  }
187 }
int i
Definition: DBlmapReader.cc:9
void newSet()
Definition: Mille.cc:160
bool myAsBinary
Definition: Mille.h:43
const std::string & label
Definition: MVAComputer.cc:186
int myBufferInt[myBufferSize]
Definition: Mille.h:47
Mille(const char *outFileName, bool asBinary=true, bool writeZero=false)
Definition: Mille.cc:16
int myBufferPos
Definition: Mille.h:49
std::ofstream myOutFile
Definition: Mille.h:42
void end()
Definition: Mille.cc:129
bool myWriteZero
Definition: Mille.h:44
void mille(int NLC, const float *derLc, int NGL, const float *derGl, const int *label, float rMeas, float sigma)
Definition: Mille.cc:43
tuple out
Definition: dbtoconf.py:99
bool checkBufferSize(int nLocal, int nGlobal)
Definition: Mille.cc:171
float myBufferFloat[myBufferSize]
Definition: Mille.h:48
void kill()
Definition: Mille.cc:121
~Mille()
Definition: Mille.cc:35
void special(int nSpecial, const float *floatings, const int *integers)
Definition: Mille.cc:86
bool myHasSpecial
Definition: Mille.h:50