-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mille.cc
228 lines (204 loc) · 7.61 KB
/
Mille.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/** \file
* Create Millepede-II C-binary record.
*
* \author Gero Flucke, University Hamburg, 2006
*
* \copyright
* Copyright (c) 2009 - 2015 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY \n\n
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version. \n\n
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details. \n\n
* You should have received a copy of the GNU Library General Public
* License along with this program (see the file COPYING.LIB for more
* details); if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**
* author : Gero Flucke, University Hamburg, 2006
* date : October 2006
* $Revision: 1.3 $
* $Date: 2007/04/16 17:47:38 $
* (last update by $Author: flucke $)
*/
#include "Mille.h"
#include <fstream>
#include <iostream>
//___________________________________________________________________________
/// Opens outFileName (by default as binary file).
/**
* \param[in] outFileName file name
* \param[in] asBinary flag for binary
* \param[in] writeZero flag for keeping of zeros
*/
Mille::Mille(const char *outFileName, bool asBinary, bool writeZero) :
myOutFile(outFileName, (asBinary ? (std::ios::binary | std::ios::out) : std::ios::out)),
myAsBinary(asBinary), myWriteZero(writeZero), myBufferPos(-1), myHasSpecial(false)
{
// Instead myBufferPos(-1), myHasSpecial(false) and the following two lines
// we could call newSet() and kill()...
myBufferInt[0] = 0;
myBufferFloat[0] = 0.;
if (!myOutFile.is_open()) {
std::cerr << "Mille::Mille: Could not open " << outFileName
<< " as output file." << std::endl;
}
}
//___________________________________________________________________________
/// Closes file.
Mille::~Mille()
{
myOutFile.close();
}
//___________________________________________________________________________
/// Add measurement to buffer.
/**
* \param[in] NLC number of local derivatives
* \param[in] derLc local derivatives
* \param[in] NGL number of global derivatives
* \param[in] derGl global derivatives
* \param[in] label global labels
* \param[in] rMeas measurement (residuum)
* \param[in] sigma error
*/
void Mille::mille(int NLC, const float *derLc,
int NGL, const float *derGl, const int *label,
float rMeas, float sigma)
{
if (sigma <= 0.) return;
if (myBufferPos == -1) this->newSet(); // start, e.g. new track
if (!this->checkBufferSize(NLC, NGL)) return;
// first store measurement
++myBufferPos;
myBufferFloat[myBufferPos] = rMeas;
myBufferInt [myBufferPos] = 0;
// store local derivatives and local 'lables' 1,...,NLC
for (int i = 0; i < NLC; ++i) {
if (derLc[i] || myWriteZero) { // by default store only non-zero derivatives
++myBufferPos;
myBufferFloat[myBufferPos] = derLc[i]; // local derivatives
myBufferInt [myBufferPos] = i+1; // index of local parameter
}
}
// store uncertainty of measurement in between locals and globals
++myBufferPos;
myBufferFloat[myBufferPos] = sigma;
myBufferInt [myBufferPos] = 0;
// store global derivatives and their labels
for (int i = 0; i < NGL; ++i) {
if (derGl[i] || myWriteZero) { // by default store only non-zero derivatives
if ((label[i] > 0 || myWriteZero) && label[i] <= myMaxLabel) { // and for valid labels
++myBufferPos;
myBufferFloat[myBufferPos] = derGl[i]; // global derivatives
myBufferInt [myBufferPos] = label[i]; // index of global parameter
} else {
std::cerr << "Mille::mille: Invalid label " << label[i]
<< " <= 0 or > " << myMaxLabel << std::endl;
}
}
}
}
//___________________________________________________________________________
/// Add special data to buffer.
/**
* \param[in] nSpecial number of floats/ints
* \param[in] floatings floats
* \param[in] integers ints
*/
void Mille::special(int nSpecial, const float *floatings, const int *integers)
{
if (nSpecial == 0) return;
if (myBufferPos == -1) this->newSet(); // start, e.g. new track
if (myHasSpecial) {
std::cerr << "Mille::special: Special values already stored for this record."
<< std::endl;
return;
}
if (!this->checkBufferSize(nSpecial, 0)) return;
myHasSpecial = true; // after newSet() (Note: MILLSP sets to buffer position...)
// myBufferFloat[.] | myBufferInt[.]
// ------------------------------------
// 0.0 | 0
// -float(nSpecial) | 0
// The above indicates special data, following are nSpecial floating and nSpecial integer data.
++myBufferPos; // zero pair
myBufferFloat[myBufferPos] = 0.;
myBufferInt [myBufferPos] = 0;
++myBufferPos; // nSpecial and zero
myBufferFloat[myBufferPos] = -nSpecial; // automatic conversion to float
myBufferInt [myBufferPos] = 0;
for (int i = 0; i < nSpecial; ++i) {
++myBufferPos;
myBufferFloat[myBufferPos] = floatings[i];
myBufferInt [myBufferPos] = integers[i];
}
}
//___________________________________________________________________________
/// Reset buffers, i.e. kill derivatives accumulated for current set.
void Mille::kill()
{
myBufferPos = -1;
}
//___________________________________________________________________________
/// Write buffer (set of derivatives with same local parameters) to file.
void Mille::end()
{
if (myBufferPos > 0) { // only if anything stored...
const int numWordsToWrite = (myBufferPos + 1)*2;
if (myAsBinary) {
myOutFile.write(reinterpret_cast<const char*>(&numWordsToWrite),
sizeof(numWordsToWrite));
myOutFile.write(reinterpret_cast<char*>(myBufferFloat),
(myBufferPos+1) * sizeof(myBufferFloat[0]));
myOutFile.write(reinterpret_cast<char*>(myBufferInt),
(myBufferPos+1) * sizeof(myBufferInt[0]));
} else {
myOutFile << numWordsToWrite << "\n";
for (int i = 0; i < myBufferPos+1; ++i) {
myOutFile << myBufferFloat[i] << " ";
}
myOutFile << "\n";
for (int i = 0; i < myBufferPos+1; ++i) {
myOutFile << myBufferInt[i] << " ";
}
myOutFile << "\n";
}
}
myBufferPos = -1; // reset buffer for next set of derivatives
}
//___________________________________________________________________________
/// Initialize for new set of locals, e.g. new track.
void Mille::newSet()
{
myBufferPos = 0;
myHasSpecial = false;
myBufferFloat[0] = 0.0;
myBufferInt [0] = 0; // position 0 used as error counter
}
//___________________________________________________________________________
/// Enough space for next nLocal + nGlobal derivatives incl. measurement?
/**
* \param[in] nLocal number of local derivatives
* \param[in] nGlobal number of global derivatives
* \return true if sufficient space available (else false)
*/
bool Mille::checkBufferSize(int nLocal, int nGlobal)
{
if (myBufferPos + nLocal + nGlobal + 2 >= myBufferSize) {
++(myBufferInt[0]); // increase error count
std::cerr << "Mille::checkBufferSize: Buffer too short ("
<< myBufferSize << "),"
<< "\n need space for nLocal (" << nLocal<< ")"
<< "/nGlobal (" << nGlobal << ") local/global derivatives, "
<< myBufferPos + 1 << " already stored!"
<< std::endl;
return false;
} else {
return true;
}
}