Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:43:56

0001 #include <fstream>
0002 #include <iostream>  // for any std::cout
0003 #include <iomanip>   // to print nicely formatted
0004 #include <string>
0005 #include <map>
0006 #include <memory>
0007 //#include <iterator>
0008 #include <cassert>
0009 
0010 #include "CLHEP/Matrix/GenMatrix.h"
0011 #include "CLHEP/Matrix/Matrix.h"
0012 #include "CLHEP/Matrix/Vector.h"
0013 
0014 #include "Calibration/Tools/interface/matrixSaver.h"
0015 
0016 matrixSaver::matrixSaver() {
0017   //   std::cout << "[matrixSaver][ctor] matrixSaver instance" << std::endl ;
0018 }
0019 
0020 matrixSaver::~matrixSaver() {
0021   //   std::cout << "[matrixSaver][dtor] destroyed" << std::endl ;
0022 }
0023 
0024 std::ostream &operator<<(std::ostream &outputFile, const CLHEP::HepGenMatrix *saveMe) {
0025   int numRow = saveMe->num_row();
0026   int numCol = saveMe->num_col();
0027 
0028   // write out the matrix dimensions
0029   outputFile << numRow << '\t' << numCol << '\n';
0030 
0031   // write the elements in the file
0032   for (int row = 0; row < numRow; ++row) {
0033     for (int col = 0; col < numCol; ++col) {
0034       assert(row < numRow);
0035       assert(col < numCol);
0036       outputFile << (*saveMe)[row][col] << '\t';
0037     }
0038     outputFile << '\n';
0039   }
0040 
0041   return outputFile;
0042 }
0043 
0044 int matrixSaver::saveMatrix(std::string outputFileName, const CLHEP::HepGenMatrix *saveMe) {
0045   // open the output file
0046   std::fstream outputFile(outputFileName.c_str(), std::ios::out);
0047   assert(outputFile);
0048 
0049   int numRow = saveMe->num_row();
0050   int numCol = saveMe->num_col();
0051 
0052   // write out the matrix dimensions
0053   outputFile << numRow << '\t' << numCol << '\n';
0054 
0055   outputFile << saveMe;
0056 
0057   return 0;
0058 }
0059 
0060 int matrixSaver::saveMatrixVector(std::string filename, const std::vector<CLHEP::HepGenMatrix *> &saveMe) {
0061   typedef std::vector<CLHEP::HepGenMatrix *>::const_iterator const_iterator;
0062   // open the output file
0063   std::fstream outputFile(filename.c_str(), std::ios::out);
0064   assert(outputFile.fail() == false);
0065 
0066   // save the number of elements of the vector
0067   outputFile << saveMe.size() << '\n';
0068 
0069   // save the matrix sizes
0070   outputFile << (*saveMe.begin())->num_row() << '\t' << (*saveMe.begin())->num_col() << '\n';
0071 
0072   // loop over the vector
0073   for (const_iterator it = saveMe.begin(); it != saveMe.end(); ++it) {
0074     outputFile << (*it);
0075   }  // loop over the vecor
0076 
0077   return 0;
0078 }
0079 
0080 std::istream &operator>>(std::istream &input, CLHEP::HepGenMatrix &matrix) {
0081   int numRow = 0;
0082   int numCol = 0;
0083 
0084   //PG read the matrix dimension
0085   input >> numRow;
0086   input >> numCol;
0087 
0088   //PG check whether the matrices have the right dimension
0089   assert(numRow == matrix.num_row());
0090   assert(numCol == matrix.num_col());
0091 
0092   //PG get the matrix elements from the file
0093   for (int row = 0; row < numRow; ++row) {
0094     for (int col = 0; col < numCol; ++col) {
0095       input >> matrix[row][col];
0096       assert(col * row < numRow * numCol);
0097     }
0098   }
0099 
0100   return input;
0101 }
0102 
0103 bool matrixSaver::touch(std::string inputFileName) {
0104   std::fstream inputFile(inputFileName.c_str(), std::ios::in);
0105   return !inputFile.fail();
0106 }
0107 
0108 CLHEP::HepGenMatrix *matrixSaver::getMatrix(std::string inputFileName) {
0109   //PG open the output file
0110   std::fstream inputFile(inputFileName.c_str(), std::ios::in);
0111   if (inputFile.fail())
0112     std::cerr << "file: " << inputFileName << std::endl;
0113   assert(inputFile.fail() == false);
0114 
0115   //PG get the matrix dimensions
0116   int numRow = 0;
0117   int numCol = 0;
0118   inputFile >> numRow;
0119   inputFile >> numCol;
0120 
0121   //PG instantiate the matrix
0122   CLHEP::HepGenMatrix *matrix;
0123   if (numCol > 1)
0124     matrix = new CLHEP::HepMatrix(numRow, numCol, 0);
0125   else
0126     matrix = new CLHEP::HepVector(numRow, 0);
0127 
0128   inputFile >> *matrix;
0129 
0130   return matrix;
0131 }
0132 
0133 std::vector<CLHEP::HepGenMatrix *> *matrixSaver::getMatrixVector(std::string inputFileName) {
0134   // open the output file
0135   std::fstream inputFile(inputFileName.c_str(), std::ios::in);
0136   assert(inputFile.fail() == false);
0137 
0138   // get the vector length
0139   int numElem = 0;
0140   inputFile >> numElem;
0141 
0142   // get the matrix dimensions
0143   int numRow = 0;
0144   int numCol = 0;
0145   inputFile >> numRow;
0146   inputFile >> numCol;
0147 
0148   //PG prepara il vector
0149   std::vector<CLHEP::HepGenMatrix *> *matrixVector = new std::vector<CLHEP::HepGenMatrix *>(numElem);
0150 
0151   //PG loop sugli elementi del vettore
0152   for (int i = 0; i < numElem; ++i) {
0153     //PG definisce il puntatore
0154     CLHEP::HepGenMatrix *matrix;
0155     //PG attribuisce un oggetto concreto
0156 
0157     if (numCol > 1)
0158       matrix = new CLHEP::HepMatrix(numRow, numCol, 0);
0159     else
0160       matrix = new CLHEP::HepVector(numRow, 0);
0161 
0162     //PG scarica su un oggetto concreto
0163     inputFile >> *matrix;
0164 
0165     //PG riempie il vettore
0166     (*matrixVector)[i] = matrix;
0167   }
0168 
0169   return matrixVector;
0170 }
0171 
0172 std::vector<CLHEP::HepMatrix> matrixSaver::getConcreteMatrixVector(std::string inputFileName) {
0173   // open the output file
0174   std::fstream inputFile(inputFileName.c_str(), std::ios::in);
0175   assert(inputFile.fail() == false);
0176 
0177   // get the vector length
0178   int numElem = 0;
0179   inputFile >> numElem;
0180 
0181   // get the matrix dimensions
0182   int numRow = 0;
0183   int numCol = 0;
0184   inputFile >> numRow;
0185   inputFile >> numCol;
0186 
0187   //PG prepara il vector
0188   std::vector<CLHEP::HepMatrix> matrixVector(numElem, CLHEP::HepMatrix(numRow, numCol, 0));
0189 
0190   //PG loop sugli elementi del vettore
0191   for (int i = 0; i < numElem; ++i) {
0192     //PG scarica su un oggetto concreto
0193     inputFile >> matrixVector[i];
0194   }
0195 
0196   return matrixVector;
0197 }