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 {
18 // std::cout << "[matrixSaver][ctor] matrixSaver instance" << std::endl ;
19 }
20 
21 
23 {
24 // std::cout << "[matrixSaver][dtor] destroyed" << std::endl ;
25 }
26 
27 
28 std::ostream &
29 operator<< (std::ostream& outputFile,
30  const CLHEP::HepGenMatrix *saveMe)
31 {
32 
33  int numRow = saveMe->num_row () ;
34  int numCol = saveMe->num_col () ;
35 
36  // write out the matrix dimensions
37  outputFile << numRow << '\t'
38  << numCol << '\n' ;
39 
40  // write the elements in the file
41  for (int row=0 ; row<numRow ; ++row)
42  {
43  for (int col=0 ; col<numCol ; ++col)
44  {
45  assert (row < numRow) ;
46  assert (col < numCol) ;
47  outputFile << (*saveMe)[row][col] << '\t' ;
48  }
49  outputFile << '\n' ;
50  }
51 
52  return outputFile ;
53 }
54 
55 
56 
57 int
59  const CLHEP::HepGenMatrix *saveMe)
60 {
61  // open the output file
62  std::fstream outputFile (outputFileName.c_str (), std::ios::out) ;
63  assert (outputFile) ;
64 
65  int numRow = saveMe->num_row () ;
66  int numCol = saveMe->num_col () ;
67 
68  // write out the matrix dimensions
69  outputFile << numRow << '\t'
70  << numCol << '\n' ;
71 
72  outputFile << saveMe ;
73 
74  return 0 ;
75 
76 }
77 
78 
79 int
81  const std::vector<CLHEP::HepGenMatrix *> &saveMe)
82 {
83  typedef std::vector<CLHEP::HepGenMatrix*>::const_iterator const_iterator ;
84  // open the output file
85  std::fstream outputFile (filename.c_str (), std::ios::out) ;
86  assert(outputFile.fail() == false) ;
87 
88  // save the number of elements of the vector
89  outputFile << saveMe.size ()
90  << '\n' ;
91 
92  // save the matrix sizes
93  outputFile << (*saveMe.begin ())->num_row ()
94  << '\t'
95  << (*saveMe.begin ())->num_col ()
96  << '\n' ;
97 
98  // loop over the vector
99  for (const_iterator it = saveMe.begin () ;
100  it != saveMe.end () ;
101  ++it)
102  {
103  outputFile << (*it) ;
104  } // loop over the vecor
105 
106  return 0 ;
107 }
108 
109 
110 std::istream &
111 operator>> (std::istream& input, CLHEP::HepGenMatrix &matrix)
112 {
113  int numRow = 0 ;
114  int numCol = 0 ;
115 
116  //PG read the matrix dimension
117  input >> numRow ;
118  input >> numCol ;
119 
120  //PG check whether the matrices have the right dimension
121  assert ( numRow == matrix.num_row () ) ;
122  assert ( numCol == matrix.num_col () ) ;
123 
124  //PG get the matrix elements from the file
125  for (int row=0 ; row<numRow ; ++row)
126  {
127  for (int col=0 ; col<numCol ; ++col)
128  {
129  input >> matrix[row][col] ;
130  assert (col*row < numRow*numCol) ;
131  }
132  }
133 
134  return input ;
135 }
136 
138 {
139  std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
140  return !inputFile.fail();
141 }
142 
143 
144 
145 CLHEP::HepGenMatrix *
147 {
148  //PG open the output file
149  std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
150  if (inputFile.fail()) std::cerr << "file: " << inputFileName << std::endl ;
151  assert(inputFile.fail() == false);
152 
153  //PG get the matrix dimensions
154  int numRow = 0 ;
155  int numCol = 0 ;
156  inputFile >> numRow ;
157  inputFile >> numCol ;
158 
159  //PG instantiate the matrix
160  CLHEP::HepGenMatrix * matrix ;
161  if (numCol > 1)
162  matrix = new CLHEP::HepMatrix (numRow, numCol, 0) ;
163  else
164  matrix = new CLHEP::HepVector (numRow, 0) ;
165 
166  inputFile >> *matrix ;
167 
168  return matrix ;
169 }
170 
171 
172 std::vector<CLHEP::HepGenMatrix*> *
174 {
175  // open the output file
176  std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
177  assert(inputFile.fail() == false);
178 
179  // get the vector length
180  int numElem = 0 ;
181  inputFile >> numElem ;
182 
183  // get the matrix dimensions
184  int numRow = 0 ;
185  int numCol = 0 ;
186  inputFile >> numRow ;
187  inputFile >> numCol ;
188 
189  //PG prepara il vector
190  std::vector<CLHEP::HepGenMatrix*>* matrixVector =
191  new std::vector<CLHEP::HepGenMatrix*> (numElem) ;
192 
193  //PG loop sugli elementi del vettore
194  for (int i=0 ; i<numElem ; ++i)
195  {
196  //PG definisce il puntatore
197  CLHEP::HepGenMatrix * matrix ;
198  //PG attribuisce un oggetto concreto
199 
200  if (numCol > 1)
201  matrix = new CLHEP::HepMatrix (numRow, numCol, 0) ;
202  else
203  matrix = new CLHEP::HepVector (numRow, 0) ;
204 
205  //PG scarica su un oggetto concreto
206  inputFile >> *matrix ;
207 
208  //PG riempie il vettore
209  (*matrixVector)[i] = matrix ;
210  }
211 
212  return matrixVector ;
213 }
214 
215 
216 std::vector<CLHEP::HepMatrix>
218 {
219  // open the output file
220  std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
221  assert(inputFile.fail() == false);
222 
223  // get the vector length
224  int numElem = 0 ;
225  inputFile >> numElem ;
226 
227  // get the matrix dimensions
228  int numRow = 0 ;
229  int numCol = 0 ;
230  inputFile >> numRow ;
231  inputFile >> numCol ;
232 
233  //PG prepara il vector
234  std::vector<CLHEP::HepMatrix> matrixVector (
235  numElem,
236  CLHEP::HepMatrix (numRow,numCol,0)
237  ) ;
238 
239  //PG loop sugli elementi del vettore
240  for (int i=0 ; i<numElem ; ++i)
241  {
242 
243  //PG scarica su un oggetto concreto
244  inputFile >> matrixVector[i] ;
245 
246  }
247 
248  return matrixVector ;
249 }
int saveMatrix(std::string outputFileName, const CLHEP::HepGenMatrix *saveMe)
Definition: matrixSaver.cc:58
static std::string const input
Definition: EdmProvDump.cc:44
std::vector< CLHEP::HepGenMatrix * > * getMatrixVector(std::string inputFileName)
Definition: matrixSaver.cc:173
bool touch(std::string inputFileName)
Definition: matrixSaver.cc:137
CLHEP::HepGenMatrix * getMatrix(std::string inputFileName)
Definition: matrixSaver.cc:146
std::ostream & operator<<(std::ostream &outputFile, const CLHEP::HepGenMatrix *saveMe)
Definition: matrixSaver.cc:29
std::istream & operator>>(std::istream &input, CLHEP::HepGenMatrix &matrix)
Definition: matrixSaver.cc:111
col
Definition: cuy.py:1008
int saveMatrixVector(std::string outputFileName, const std::vector< CLHEP::HepGenMatrix * > &saveMe)
Definition: matrixSaver.cc:80
std::vector< CLHEP::HepMatrix > getConcreteMatrixVector(std::string inputFileName)
Definition: matrixSaver.cc:217