Millepede-II V04-16-03
Mille.cc
Go to the documentation of this file.
1
32#include "Mille.h"
33
34#include <fstream>
35#include <iostream>
36
37//___________________________________________________________________________
38
40
45Mille::Mille(const char *outFileName, bool asBinary, bool writeZero) :
46 myOutFile(outFileName, (asBinary ? (std::ios::binary | std::ios::out) : std::ios::out)),
47 myAsBinary(asBinary), myWriteZero(writeZero), myBufferPos(-1), myHasSpecial(false)
48{
49 // Instead myBufferPos(-1), myHasSpecial(false) and the following two lines
50 // we could call newSet() and kill()...
51 myBufferInt[0] = 0;
52 myBufferFloat[0] = 0.;
53
54 if (!myOutFile.is_open()) {
55 std::cerr << "Mille::Mille: Could not open " << outFileName
56 << " as output file." << std::endl;
57 }
58}
59
60//___________________________________________________________________________
63{
64 myOutFile.close();
65}
66
67//___________________________________________________________________________
69
78void Mille::mille(int NLC, const float *derLc,
79 int NGL, const float *derGl, const int *label,
80 float rMeas, float sigma)
81{
82 if (sigma <= 0.) return;
83 if (myBufferPos == -1) this->newSet(); // start, e.g. new track
84 if (!this->checkBufferSize(NLC, NGL)) return;
85
86 // first store measurement
90
91 // store local derivatives and local 'lables' 1,...,NLC
92 for (int i = 0; i < NLC; ++i) {
93 if (derLc[i] || myWriteZero) { // by default store only non-zero derivatives
95 myBufferFloat[myBufferPos] = derLc[i]; // local derivatives
96 myBufferInt [myBufferPos] = i+1; // index of local parameter
97 }
98 }
99
100 // store uncertainty of measurement in between locals and globals
101 ++myBufferPos;
104
105 // store global derivatives and their labels
106 for (int i = 0; i < NGL; ++i) {
107 if (derGl[i] || myWriteZero) { // by default store only non-zero derivatives
108 if ((label[i] > 0 || myWriteZero) && label[i] <= myMaxLabel) { // and for valid labels
109 ++myBufferPos;
110 myBufferFloat[myBufferPos] = derGl[i]; // global derivatives
111 myBufferInt [myBufferPos] = label[i]; // index of global parameter
112 } else {
113 std::cerr << "Mille::mille: Invalid label " << label[i]
114 << " <= 0 or > " << myMaxLabel << std::endl;
115 }
116 }
117 }
118}
119
120//___________________________________________________________________________
122
127void Mille::special(int nSpecial, const float *floatings, const int *integers)
128{
129 if (nSpecial == 0) return;
130 if (myBufferPos == -1) this->newSet(); // start, e.g. new track
131 if (myHasSpecial) {
132 std::cerr << "Mille::special: Special values already stored for this record."
133 << std::endl;
134 return;
135 }
136 if (!this->checkBufferSize(nSpecial, 0)) return;
137 myHasSpecial = true; // after newSet() (Note: MILLSP sets to buffer position...)
138
139 // myBufferFloat[.] | myBufferInt[.]
140 // ------------------------------------
141 // 0.0 | 0
142 // -float(nSpecial) | 0
143 // The above indicates special data, following are nSpecial floating and nSpecial integer data.
144
145 ++myBufferPos; // zero pair
148
149 ++myBufferPos; // nSpecial and zero
150 myBufferFloat[myBufferPos] = -nSpecial; // automatic conversion to float
152
153 for (int i = 0; i < nSpecial; ++i) {
154 ++myBufferPos;
155 myBufferFloat[myBufferPos] = floatings[i];
156 myBufferInt [myBufferPos] = integers[i];
157 }
158}
159
160//___________________________________________________________________________
163{
164 myBufferPos = -1;
165}
166
167//___________________________________________________________________________
170{
171 if (myBufferPos > 0) { // only if anything stored...
172 const int numWordsToWrite = (myBufferPos + 1)*2;
173
174 if (myAsBinary) {
175 myOutFile.write(reinterpret_cast<const char*>(&numWordsToWrite),
176 sizeof(numWordsToWrite));
177 myOutFile.write(reinterpret_cast<char*>(myBufferFloat),
178 (myBufferPos+1) * sizeof(myBufferFloat[0]));
179 myOutFile.write(reinterpret_cast<char*>(myBufferInt),
180 (myBufferPos+1) * sizeof(myBufferInt[0]));
181 } else {
182 myOutFile << numWordsToWrite << "\n";
183 for (int i = 0; i < myBufferPos+1; ++i) {
184 myOutFile << myBufferFloat[i] << " ";
185 }
186 myOutFile << "\n";
187
188 for (int i = 0; i < myBufferPos+1; ++i) {
189 myOutFile << myBufferInt[i] << " ";
190 }
191 myOutFile << "\n";
192 }
193 }
194 myBufferPos = -1; // reset buffer for next set of derivatives
195}
196
197//___________________________________________________________________________
200{
201 myBufferPos = 0;
202 myHasSpecial = false;
203 myBufferFloat[0] = 0.0;
204 myBufferInt [0] = 0; // position 0 used as error counter
205}
206
207//___________________________________________________________________________
209
214bool Mille::checkBufferSize(int nLocal, int nGlobal)
215{
216 if (myBufferPos + nLocal + nGlobal + 2 >= myBufferSize) {
217 ++(myBufferInt[0]); // increase error count
218 std::cerr << "Mille::checkBufferSize: Buffer too short ("
219 << myBufferSize << "),"
220 << "\n need space for nLocal (" << nLocal<< ")"
221 << "/nGlobal (" << nGlobal << ") local/global derivatives, "
222 << myBufferPos + 1 << " already stored!"
223 << std::endl;
224 return false;
225 } else {
226 return true;
227 }
228}
Define class Mille.
void end()
Write buffer (set of derivatives with same local parameters) to file.
Definition: Mille.cc:169
int myBufferInt[myBufferSize]
to collect labels etc.
Definition: Mille.h:71
bool myAsBinary
if false output as text
Definition: Mille.h:67
bool checkBufferSize(int nLocal, int nGlobal)
Enough space for next nLocal + nGlobal derivatives incl. measurement?
Definition: Mille.cc:214
void special(int nSpecial, const float *floatings, const int *integers)
Add special data to buffer.
Definition: Mille.cc:127
@ myBufferSize
Definition: Mille.h:70
~Mille()
Closes file.
Definition: Mille.cc:62
std::ofstream myOutFile
C-binary for output.
Definition: Mille.h:66
bool myHasSpecial
if true, special(..) already called for this record
Definition: Mille.h:74
void kill()
Reset buffers, i.e. kill derivatives accumulated for current set.
Definition: Mille.cc:162
float myBufferFloat[myBufferSize]
to collect derivatives etc.
Definition: Mille.h:72
@ myMaxLabel
Definition: Mille.h:76
int myBufferPos
position in buffer
Definition: Mille.h:73
void mille(int NLC, const float *derLc, int NGL, const float *derGl, const int *label, float rMeas, float sigma)
Add measurement to buffer.
Definition: Mille.cc:78
bool myWriteZero
if true also write out derivatives/labels ==0
Definition: Mille.h:68
Mille(const char *outFileName, bool asBinary=true, bool writeZero=false)
author : Gero Flucke, University Hamburg, 2006 date : October 2006
Definition: Mille.cc:45
void newSet()
Initialize for new set of locals, e.g. new track.
Definition: Mille.cc:199
real(mps), dimension(nplan) sigma
measurement sigma (hit)
Definition: mptest1.f90:65