File indexing completed on 2024-04-06 11:56:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "Mille.h"
0011
0012 #include <iostream>
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014
0015
0016
0017 Mille::Mille(const char *outFileName, bool asBinary, bool writeZero)
0018 : fileMode_(asBinary ? (std::ios::binary | std::ios::out) : std::ios::out),
0019 fileName_(outFileName),
0020 outFile_(fileName_, fileMode_),
0021 asBinary_(asBinary),
0022 writeZero_(writeZero),
0023 bufferPos_(-1),
0024 hasSpecial_(false) {
0025
0026
0027
0028
0029 bufferInt_[0] = 0;
0030 bufferFloat_[0] = 0.;
0031
0032 if (!outFile_.is_open()) {
0033 edm::LogError("Alignment") << "Mille::Mille: Could not open " << fileName_ << " as output file.";
0034 }
0035 }
0036
0037
0038
0039 Mille::~Mille() {
0040
0041 outFile_.close();
0042 }
0043
0044
0045
0046 void Mille::mille(int NLC, const float *derLc, int NGL, const float *derGl, const int *label, float rMeas, float sigma) {
0047 if (sigma <= 0.)
0048 return;
0049 if (bufferPos_ == -1)
0050 this->newSet();
0051 if (!this->checkBufferSize(NLC, NGL))
0052 return;
0053
0054
0055 ++bufferPos_;
0056 bufferFloat_[bufferPos_] = rMeas;
0057 bufferInt_[bufferPos_] = 0;
0058
0059
0060 for (int i = 0; i < NLC; ++i) {
0061 if (derLc[i] || writeZero_) {
0062 ++bufferPos_;
0063 bufferFloat_[bufferPos_] = derLc[i];
0064 bufferInt_[bufferPos_] = i + 1;
0065 }
0066 }
0067
0068
0069 ++bufferPos_;
0070 bufferFloat_[bufferPos_] = sigma;
0071 bufferInt_[bufferPos_] = 0;
0072
0073
0074 for (int i = 0; i < NGL; ++i) {
0075 if (derGl[i] || writeZero_) {
0076 if ((label[i] > 0 || writeZero_) && label[i] <= maxLabel_) {
0077 ++bufferPos_;
0078 bufferFloat_[bufferPos_] = derGl[i];
0079 bufferInt_[bufferPos_] = label[i];
0080 } else {
0081 edm::LogError("Alignment") << "Mille::mille: Invalid label " << label[i] << " <= 0 or > " << maxLabel_;
0082 }
0083 }
0084 }
0085 }
0086
0087
0088 void Mille::special(int nSpecial, const float *floatings, const int *integers) {
0089 if (nSpecial == 0)
0090 return;
0091 if (bufferPos_ == -1)
0092 this->newSet();
0093 if (hasSpecial_) {
0094 edm::LogError("Alignment") << "Mille::special: Special values already stored for this record.";
0095 return;
0096 }
0097 if (!this->checkBufferSize(nSpecial, 0))
0098 return;
0099 hasSpecial_ = true;
0100
0101
0102
0103
0104
0105
0106
0107 ++bufferPos_;
0108 bufferFloat_[bufferPos_] = 0.;
0109 bufferInt_[bufferPos_] = 0;
0110
0111 ++bufferPos_;
0112 bufferFloat_[bufferPos_] = -nSpecial;
0113 bufferInt_[bufferPos_] = 0;
0114
0115 for (int i = 0; i < nSpecial; ++i) {
0116 ++bufferPos_;
0117 bufferFloat_[bufferPos_] = floatings[i];
0118 bufferInt_[bufferPos_] = integers[i];
0119 }
0120 }
0121
0122
0123
0124 void Mille::kill() {
0125
0126 bufferPos_ = -1;
0127 }
0128
0129
0130
0131 void Mille::flushOutputFile() {
0132
0133 outFile_.flush();
0134 }
0135
0136
0137
0138 void Mille::resetOutputFile() {
0139
0140 outFile_.close();
0141 outFile_.open(fileName_, fileMode_);
0142 if (!outFile_.is_open()) {
0143 edm::LogError("Alignment") << "Mille::resetOutputFile: Could not reopen " << fileName_ << ".";
0144 }
0145 }
0146
0147
0148
0149 void Mille::end() {
0150
0151 if (bufferPos_ > 0) {
0152 const int numWordsToWrite = (bufferPos_ + 1) * 2;
0153
0154 if (asBinary_) {
0155 outFile_.write(reinterpret_cast<const char *>(&numWordsToWrite), sizeof(numWordsToWrite));
0156 outFile_.write(reinterpret_cast<char *>(bufferFloat_), (bufferPos_ + 1) * sizeof(bufferFloat_[0]));
0157 outFile_.write(reinterpret_cast<char *>(bufferInt_), (bufferPos_ + 1) * sizeof(bufferInt_[0]));
0158 } else {
0159 outFile_ << numWordsToWrite << "\n";
0160 for (int i = 0; i < bufferPos_ + 1; ++i) {
0161 outFile_ << bufferFloat_[i] << " ";
0162 }
0163 outFile_ << "\n";
0164
0165 for (int i = 0; i < bufferPos_ + 1; ++i) {
0166 outFile_ << bufferInt_[i] << " ";
0167 }
0168 outFile_ << "\n";
0169 }
0170 }
0171 bufferPos_ = -1;
0172 }
0173
0174
0175
0176 void Mille::newSet() {
0177
0178 bufferPos_ = 0;
0179 hasSpecial_ = false;
0180 bufferFloat_[0] = 0.0;
0181 bufferInt_[0] = 0;
0182 }
0183
0184
0185
0186 bool Mille::checkBufferSize(int nLocal, int nGlobal) {
0187
0188
0189 if (bufferPos_ + nLocal + nGlobal + 2 >= bufferSize_) {
0190 ++(bufferInt_[0]);
0191 edm::LogError("Alignment") << "Mille::checkBufferSize: Buffer too short (" << bufferSize_ << "),"
0192 << "\n need space for nLocal (" << nLocal << ")"
0193 << "/nGlobal (" << nGlobal << ") local/global derivatives, " << bufferPos_ + 1
0194 << " already stored!";
0195 return false;
0196 } else {
0197 return true;
0198 }
0199 }