Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:18:26

0001 /*
0002  * MilleBinary.cpp
0003  *
0004  *  Created on: Aug 31, 2011
0005  *      Author: kleinwrt
0006  */
0007 
0008 /** \file
0009  *  MilleBinary methods.
0010  *
0011  *  \author Claus Kleinwort, DESY, 2011 (Claus.Kleinwort@desy.de)
0012  *
0013  *  \copyright
0014  *  Copyright (c) 2011 - 2016 Deutsches Elektronen-Synchroton,
0015  *  Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY \n\n
0016  *  This library is free software; you can redistribute it and/or modify
0017  *  it under the terms of the GNU Library General Public License as
0018  *  published by the Free Software Foundation; either version 2 of the
0019  *  License, or (at your option) any later version. \n\n
0020  *  This library is distributed in the hope that it will be useful,
0021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0023  *  GNU Library General Public License for more details. \n\n
0024  *  You should have received a copy of the GNU Library General Public
0025  *  License along with this program (see the file COPYING.LIB for more
0026  *  details); if not, write to the Free Software Foundation, Inc.,
0027  *  675 Mass Ave, Cambridge, MA 02139, USA.
0028  */
0029 
0030 #include "MilleBinary.h"
0031 
0032 //! Namespace for the general broken lines package
0033 namespace gbl {
0034 
0035 /// Create binary file.
0036 /**
0037  * \param [in] fileName File name
0038  * \param [in] doublePrec Flag for storage as double values
0039  * \param [in] aSize Buffer size
0040  */
0041 MilleBinary::MilleBinary(const std::string &fileName, bool doublePrec,
0042         unsigned int aSize) :
0043         binaryFile(fileName.c_str(), std::ios::binary | std::ios::out), intBuffer(), floatBuffer(), doubleBuffer(), doublePrecision(
0044                 doublePrec) {
0045     intBuffer.reserve(aSize);
0046     intBuffer.push_back(0); // first word is error counter
0047     if (doublePrecision) {
0048         doubleBuffer.reserve(aSize);
0049         doubleBuffer.push_back(0.);
0050 
0051     } else {
0052         floatBuffer.reserve(aSize);
0053         floatBuffer.push_back(0.);
0054     }
0055 }
0056 
0057 MilleBinary::~MilleBinary() {
0058     binaryFile.close();
0059 }
0060 
0061 /// Add data block to (end of) record.
0062 /**
0063  * \param [in] aMeas Value
0064  * \param [in] aErr Error
0065  * \param [in] indLocal List of labels of local parameters
0066  * \param [in] derLocal List of derivatives for local parameters
0067  * \param [in] labGlobal List of labels of global parameters
0068  * \param [in] derGlobal List of derivatives for global parameters
0069  */
0070 void MilleBinary::addData(double aMeas, double aErr,
0071         const std::vector<unsigned int> &indLocal,
0072         const std::vector<double> &derLocal, const std::vector<int> &labGlobal,
0073         const std::vector<double> &derGlobal) {
0074 
0075     if (doublePrecision) {
0076         // double values
0077         intBuffer.push_back(0);
0078         doubleBuffer.push_back(aMeas);
0079         for (unsigned int i = 0; i < indLocal.size(); ++i) {
0080             intBuffer.push_back(indLocal[i]);
0081             doubleBuffer.push_back(derLocal[i]);
0082         }
0083         intBuffer.push_back(0);
0084         doubleBuffer.push_back(aErr);
0085         for (unsigned int i = 0; i < labGlobal.size(); ++i) {
0086             if (derGlobal[i]) {
0087                 intBuffer.push_back(labGlobal[i]);
0088                 doubleBuffer.push_back(derGlobal[i]);
0089             }
0090         }
0091     } else {
0092         // float values
0093         intBuffer.push_back(0);
0094         floatBuffer.push_back(aMeas);
0095         for (unsigned int i = 0; i < indLocal.size(); ++i) {
0096             intBuffer.push_back(indLocal[i]);
0097             floatBuffer.push_back(derLocal[i]);
0098         }
0099         intBuffer.push_back(0);
0100         floatBuffer.push_back(aErr);
0101         for (unsigned int i = 0; i < labGlobal.size(); ++i) {
0102             if (derGlobal[i]) {
0103                 intBuffer.push_back(labGlobal[i]);
0104                 floatBuffer.push_back(derGlobal[i]);
0105             }
0106         }
0107     }
0108 }
0109 
0110 /// Write record to file.
0111 void MilleBinary::writeRecord() {
0112 
0113     const int recordLength =
0114             (doublePrecision) ? -intBuffer.size() * 2 : intBuffer.size() * 2;
0115     binaryFile.write(reinterpret_cast<const char*>(&recordLength),
0116             sizeof(recordLength));
0117     if (doublePrecision)
0118         binaryFile.write(reinterpret_cast<char*>(&doubleBuffer[0]),
0119                 doubleBuffer.size() * sizeof(doubleBuffer[0]));
0120     else
0121         binaryFile.write(reinterpret_cast<char*>(&floatBuffer[0]),
0122                 floatBuffer.size() * sizeof(floatBuffer[0]));
0123     binaryFile.write(reinterpret_cast<char*>(&intBuffer[0]),
0124             intBuffer.size() * sizeof(intBuffer[0]));
0125 // start with new record
0126     intBuffer.resize(1);
0127     if (doublePrecision)
0128         doubleBuffer.resize(1);
0129     else
0130         floatBuffer.resize(1);
0131 }
0132 }