test
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>
14 
15 //___________________________________________________________________________
16 
17 Mille::Mille(const char *outFileName, bool asBinary, bool writeZero) :
18  fileMode_(asBinary ? (std::ios::binary | std::ios::out) : std::ios::out),
19  fileName_(outFileName),
20  outFile_(fileName_, fileMode_),
21  asBinary_(asBinary), writeZero_(writeZero), bufferPos_(-1), hasSpecial_(false)
22 {
23  // opens outFileName, by default as binary file
24 
25  // Instead bufferPos_(-1), hasSpecial_(false) and the following two lines
26  // we could call newSet() and kill()...
27  bufferInt_[0] = 0;
28  bufferFloat_[0] = 0.;
29 
30  if (!outFile_.is_open()) {
31  edm::LogError("Alignment")
32  << "Mille::Mille: Could not open " << fileName_ << " as output file.";
33  }
34 }
35 
36 //___________________________________________________________________________
37 
39 {
40  // closes file
41  outFile_.close();
42 }
43 
44 //___________________________________________________________________________
45 
46 void Mille::mille(int NLC, const float *derLc,
47  int NGL, const float *derGl, const int *label,
48  float rMeas, float sigma)
49 {
50  if (sigma <= 0.) return;
51  if (bufferPos_ == -1) this->newSet(); // start, e.g. new track
52  if (!this->checkBufferSize(NLC, NGL)) return;
53 
54  // first store measurement
55  ++bufferPos_;
56  bufferFloat_[bufferPos_] = rMeas;
57  bufferInt_ [bufferPos_] = 0;
58 
59  // store local derivatives and local 'lables' 1,...,NLC
60  for (int i = 0; i < NLC; ++i) {
61  if (derLc[i] || writeZero_) { // by default store only non-zero derivatives
62  ++bufferPos_;
63  bufferFloat_[bufferPos_] = derLc[i]; // local derivatives
64  bufferInt_ [bufferPos_] = i+1; // index of local parameter
65  }
66  }
67 
68  // store uncertainty of measurement in between locals and globals
69  ++bufferPos_;
70  bufferFloat_[bufferPos_] = sigma;
71  bufferInt_ [bufferPos_] = 0;
72 
73  // store global derivatives and their lables
74  for (int i = 0; i < NGL; ++i) {
75  if (derGl[i] || writeZero_) { // by default store only non-zero derivatives
76  if ((label[i] > 0 || writeZero_) && label[i] <= maxLabel_) { // and for valid labels
77  ++bufferPos_;
78  bufferFloat_[bufferPos_] = derGl[i]; // global derivatives
79  bufferInt_ [bufferPos_] = label[i]; // index of global parameter
80  } else {
81  edm::LogError("Alignment")
82  << "Mille::mille: Invalid label " << label[i]
83  << " <= 0 or > " << maxLabel_;
84  }
85  }
86  }
87 }
88 
89 //___________________________________________________________________________
90 void Mille::special(int nSpecial, const float *floatings, const int *integers)
91 {
92  if (nSpecial == 0) return;
93  if (bufferPos_ == -1) this->newSet(); // start, e.g. new track
94  if (hasSpecial_) {
95  edm::LogError("Alignment")
96  << "Mille::special: Special values already stored for this record.";
97  return;
98  }
99  if (!this->checkBufferSize(nSpecial, 0)) return;
100  hasSpecial_ = true; // after newSet() (Note: MILLSP sets to buffer position...)
101 
102  // bufferFloat_[.] | bufferInt_[.]
103  // ------------------------------------
104  // 0.0 | 0
105  // -float(nSpecial) | 0
106  // The above indicates special data, following are nSpecial floating and nSpecial integer data.
107 
108  ++bufferPos_; // zero pair
109  bufferFloat_[bufferPos_] = 0.;
110  bufferInt_ [bufferPos_] = 0;
111 
112  ++bufferPos_; // nSpecial and zero
113  bufferFloat_[bufferPos_] = -nSpecial; // automatic conversion to float
114  bufferInt_ [bufferPos_] = 0;
115 
116  for (int i = 0; i < nSpecial; ++i) {
117  ++bufferPos_;
118  bufferFloat_[bufferPos_] = floatings[i];
119  bufferInt_ [bufferPos_] = integers[i];
120  }
121 }
122 
123 //___________________________________________________________________________
124 
126 {
127  // reset buffers, i.e. kill derivatives accumulated for current set
128  bufferPos_ = -1;
129 }
130 
131 //___________________________________________________________________________
132 
133 
135  // flush output file
136  outFile_.flush();
137 }
138 
139 //___________________________________________________________________________
140 
141 
143  // flush output file
144  outFile_.close();
146  if (!outFile_.is_open()) {
147  edm::LogError("Alignment")
148  << "Mille::resetOutputFile: Could not reopen " << fileName_ << ".";
149  }
150 }
151 
152 //___________________________________________________________________________
153 
155 {
156  // write set of derivatives with same local parameters to file
157  if (bufferPos_ > 0) { // only if anything stored...
158  const int numWordsToWrite = (bufferPos_ + 1)*2;
159 
160  if (asBinary_) {
161  outFile_.write(reinterpret_cast<const char*>(&numWordsToWrite),
162  sizeof(numWordsToWrite));
163  outFile_.write(reinterpret_cast<char*>(bufferFloat_),
164  (bufferPos_+1) * sizeof(bufferFloat_[0]));
165  outFile_.write(reinterpret_cast<char*>(bufferInt_),
166  (bufferPos_+1) * sizeof(bufferInt_[0]));
167  } else {
168  outFile_ << numWordsToWrite << "\n";
169  for (int i = 0; i < bufferPos_+1; ++i) {
170  outFile_ << bufferFloat_[i] << " ";
171  }
172  outFile_ << "\n";
173 
174  for (int i = 0; i < bufferPos_+1; ++i) {
175  outFile_ << bufferInt_[i] << " ";
176  }
177  outFile_ << "\n";
178  }
179  }
180  bufferPos_ = -1; // reset buffer for next set of derivatives
181 }
182 
183 //___________________________________________________________________________
184 
186 {
187  // initilise for new set of locals, e.g. new track
188  bufferPos_ = 0;
189  hasSpecial_ = false;
190  bufferFloat_[0] = 0.0;
191  bufferInt_ [0] = 0; // position 0 used as error counter
192 }
193 
194 //___________________________________________________________________________
195 
196 bool Mille::checkBufferSize(int nLocal, int nGlobal)
197 {
198  // enough space for next nLocal + nGlobal derivatives incl. measurement?
199 
200  if (bufferPos_ + nLocal + nGlobal + 2 >= bufferSize_) {
201  ++(bufferInt_[0]); // increase error count
202  edm::LogError("Alignment")
203  << "Mille::checkBufferSize: Buffer too short ("
204  << bufferSize_ << "),"
205  << "\n need space for nLocal (" << nLocal<< ")"
206  << "/nGlobal (" << nGlobal << ") local/global derivatives, "
207  << bufferPos_ + 1 << " already stored!";
208  return false;
209  } else {
210  return true;
211  }
212 }
int i
Definition: DBlmapReader.cc:9
void newSet()
Definition: Mille.cc:185
bool writeZero_
Definition: Mille.h:48
void resetOutputFile()
Definition: Mille.cc:142
int bufferInt_[bufferSize_]
Definition: Mille.h:51
Mille(const char *outFileName, bool asBinary=true, bool writeZero=false)
Definition: Mille.cc:17
float bufferFloat_[bufferSize_]
Definition: Mille.h:52
void end()
Definition: Mille.cc:154
std::ofstream outFile_
Definition: Mille.h:46
void mille(int NLC, const float *derLc, int NGL, const float *derGl, const int *label, float rMeas, float sigma)
Definition: Mille.cc:46
void flushOutputFile()
Definition: Mille.cc:134
bool checkBufferSize(int nLocal, int nGlobal)
Definition: Mille.cc:196
bool asBinary_
Definition: Mille.h:47
bool hasSpecial_
Definition: Mille.h:54
void kill()
Definition: Mille.cc:125
volatile std::atomic< bool > shutdown_flag false
const std::ios_base::openmode fileMode_
Definition: Mille.h:44
~Mille()
Definition: Mille.cc:38
void special(int nSpecial, const float *floatings, const int *integers)
Definition: Mille.cc:90
int bufferPos_
Definition: Mille.h:53
const std::string fileName_
Definition: Mille.h:45