CMS 3D CMS Logo

matrixSaver.cc
Go to the documentation of this file.
1 #include <fstream>
2 #include <iostream> // for any std::cout
3 #include <iomanip> // to print nicely formatted
4 #include <string>
5 #include <map>
6 #include <memory>
7 //#include <iterator>
8 #include <cassert>
9 
10 #include "CLHEP/Matrix/GenMatrix.h"
11 #include "CLHEP/Matrix/Matrix.h"
12 #include "CLHEP/Matrix/Vector.h"
13 
15 
17  // std::cout << "[matrixSaver][ctor] matrixSaver instance" << std::endl ;
18 }
19 
21  // std::cout << "[matrixSaver][dtor] destroyed" << std::endl ;
22 }
23 
24 std::ostream &operator<<(std::ostream &outputFile, const CLHEP::HepGenMatrix *saveMe) {
25  int numRow = saveMe->num_row();
26  int numCol = saveMe->num_col();
27 
28  // write out the matrix dimensions
29  outputFile << numRow << '\t' << numCol << '\n';
30 
31  // write the elements in the file
32  for (int row = 0; row < numRow; ++row) {
33  for (int col = 0; col < numCol; ++col) {
34  assert(row < numRow);
35  assert(col < numCol);
36  outputFile << (*saveMe)[row][col] << '\t';
37  }
38  outputFile << '\n';
39  }
40 
41  return outputFile;
42 }
43 
44 int matrixSaver::saveMatrix(std::string outputFileName, const CLHEP::HepGenMatrix *saveMe) {
45  // open the output file
46  std::fstream outputFile(outputFileName.c_str(), std::ios::out);
48 
49  int numRow = saveMe->num_row();
50  int numCol = saveMe->num_col();
51 
52  // write out the matrix dimensions
53  outputFile << numRow << '\t' << numCol << '\n';
54 
55  outputFile << saveMe;
56 
57  return 0;
58 }
59 
60 int matrixSaver::saveMatrixVector(std::string filename, const std::vector<CLHEP::HepGenMatrix *> &saveMe) {
61  typedef std::vector<CLHEP::HepGenMatrix *>::const_iterator const_iterator;
62  // open the output file
63  std::fstream outputFile(filename.c_str(), std::ios::out);
64  assert(outputFile.fail() == false);
65 
66  // save the number of elements of the vector
67  outputFile << saveMe.size() << '\n';
68 
69  // save the matrix sizes
70  outputFile << (*saveMe.begin())->num_row() << '\t' << (*saveMe.begin())->num_col() << '\n';
71 
72  // loop over the vector
73  for (const_iterator it = saveMe.begin(); it != saveMe.end(); ++it) {
74  outputFile << (*it);
75  } // loop over the vecor
76 
77  return 0;
78 }
79 
80 std::istream &operator>>(std::istream &input, CLHEP::HepGenMatrix &matrix) {
81  int numRow = 0;
82  int numCol = 0;
83 
84  //PG read the matrix dimension
85  input >> numRow;
86  input >> numCol;
87 
88  //PG check whether the matrices have the right dimension
89  assert(numRow == matrix.num_row());
90  assert(numCol == matrix.num_col());
91 
92  //PG get the matrix elements from the file
93  for (int row = 0; row < numRow; ++row) {
94  for (int col = 0; col < numCol; ++col) {
95  input >> matrix[row][col];
96  assert(col * row < numRow * numCol);
97  }
98  }
99 
100  return input;
101 }
102 
104  std::fstream inputFile(inputFileName.c_str(), std::ios::in);
105  return !inputFile.fail();
106 }
107 
109  //PG open the output file
110  std::fstream inputFile(inputFileName.c_str(), std::ios::in);
111  if (inputFile.fail())
112  std::cerr << "file: " << inputFileName << std::endl;
113  assert(inputFile.fail() == false);
114 
115  //PG get the matrix dimensions
116  int numRow = 0;
117  int numCol = 0;
118  inputFile >> numRow;
119  inputFile >> numCol;
120 
121  //PG instantiate the matrix
122  CLHEP::HepGenMatrix *matrix;
123  if (numCol > 1)
124  matrix = new CLHEP::HepMatrix(numRow, numCol, 0);
125  else
126  matrix = new CLHEP::HepVector(numRow, 0);
127 
128  inputFile >> *matrix;
129 
130  return matrix;
131 }
132 
133 std::vector<CLHEP::HepGenMatrix *> *matrixSaver::getMatrixVector(std::string inputFileName) {
134  // open the output file
135  std::fstream inputFile(inputFileName.c_str(), std::ios::in);
136  assert(inputFile.fail() == false);
137 
138  // get the vector length
139  int numElem = 0;
140  inputFile >> numElem;
141 
142  // get the matrix dimensions
143  int numRow = 0;
144  int numCol = 0;
145  inputFile >> numRow;
146  inputFile >> numCol;
147 
148  //PG prepara il vector
149  std::vector<CLHEP::HepGenMatrix *> *matrixVector = new std::vector<CLHEP::HepGenMatrix *>(numElem);
150 
151  //PG loop sugli elementi del vettore
152  for (int i = 0; i < numElem; ++i) {
153  //PG definisce il puntatore
154  CLHEP::HepGenMatrix *matrix;
155  //PG attribuisce un oggetto concreto
156 
157  if (numCol > 1)
158  matrix = new CLHEP::HepMatrix(numRow, numCol, 0);
159  else
160  matrix = new CLHEP::HepVector(numRow, 0);
161 
162  //PG scarica su un oggetto concreto
163  inputFile >> *matrix;
164 
165  //PG riempie il vettore
166  (*matrixVector)[i] = matrix;
167  }
168 
169  return matrixVector;
170 }
171 
173  // open the output file
174  std::fstream inputFile(inputFileName.c_str(), std::ios::in);
175  assert(inputFile.fail() == false);
176 
177  // get the vector length
178  int numElem = 0;
179  inputFile >> numElem;
180 
181  // get the matrix dimensions
182  int numRow = 0;
183  int numCol = 0;
184  inputFile >> numRow;
185  inputFile >> numCol;
186 
187  //PG prepara il vector
188  std::vector<CLHEP::HepMatrix> matrixVector(numElem, CLHEP::HepMatrix(numRow, numCol, 0));
189 
190  //PG loop sugli elementi del vettore
191  for (int i = 0; i < numElem; ++i) {
192  //PG scarica su un oggetto concreto
193  inputFile >> matrixVector[i];
194  }
195 
196  return matrixVector;
197 }
matrixSaver::saveMatrix
int saveMatrix(std::string outputFileName, const CLHEP::HepGenMatrix *saveMe)
Definition: matrixSaver.cc:44
mps_fire.i
i
Definition: mps_fire.py:355
input
static const std::string input
Definition: EdmProvDump.cc:48
makeMuonMisalignmentScenario.matrix
list matrix
Definition: makeMuonMisalignmentScenario.py:141
cuy.col
col
Definition: cuy.py:1010
cms::cuda::assert
assert(be >=bs)
matrixSaver::saveMatrixVector
int saveMatrixVector(std::string outputFileName, const std::vector< CLHEP::HepGenMatrix * > &saveMe)
Definition: matrixSaver.cc:60
download_sqlite_cfg.outputFile
outputFile
Definition: download_sqlite_cfg.py:5
InefficientDoubleROC.inputFileName
inputFileName
Definition: InefficientDoubleROC.py:437
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
reco_skim_cfg_mod.outputFileName
outputFileName
Definition: reco_skim_cfg_mod.py:15
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
matrixSaver::matrixSaver
matrixSaver()
Definition: matrixSaver.cc:16
beam_dqm_sourceclient-live_cfg.cerr
cerr
Definition: beam_dqm_sourceclient-live_cfg.py:17
recoMuon::in
Definition: RecoMuonEnumerators.h:6
matrixSaver.h
dtResolutionTest_cfi.inputFile
inputFile
Definition: dtResolutionTest_cfi.py:14
matrixSaver::touch
bool touch(std::string inputFileName)
Definition: matrixSaver.cc:103
matrixSaver::getMatrix
CLHEP::HepGenMatrix * getMatrix(std::string inputFileName)
Definition: matrixSaver.cc:108
operator>>
std::istream & operator>>(std::istream &input, CLHEP::HepGenMatrix &matrix)
Definition: matrixSaver.cc:80
matrixSaver::~matrixSaver
~matrixSaver()
Definition: matrixSaver.cc:20
operator<<
std::ostream & operator<<(std::ostream &outputFile, const CLHEP::HepGenMatrix *saveMe)
Definition: matrixSaver.cc:24
matrixSaver::getMatrixVector
std::vector< CLHEP::HepGenMatrix * > * getMatrixVector(std::string inputFileName)
Definition: matrixSaver.cc:133
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
matrixSaver::getConcreteMatrixVector
std::vector< CLHEP::HepMatrix > getConcreteMatrixVector(std::string inputFileName)
Definition: matrixSaver.cc:172