CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 != NULL) ;
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 
137 
139 {
140  std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
141  return (inputFile != NULL) ;
142 }
143 
144 
145 
146 CLHEP::HepGenMatrix *
148 {
149  //PG open the output file
150  std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
151  if (inputFile == NULL) std::cerr << "file: " << inputFileName << std::endl ;
152  assert (inputFile != NULL) ;
153 
154  //PG get the matrix dimensions
155  int numRow = 0 ;
156  int numCol = 0 ;
157  inputFile >> numRow ;
158  inputFile >> numCol ;
159 
160  //PG instantiate the matrix
161  CLHEP::HepGenMatrix * matrix ;
162  if (numCol > 1)
163  matrix = new CLHEP::HepMatrix (numRow, numCol, 0) ;
164  else
165  matrix = new CLHEP::HepVector (numRow, 0) ;
166 
167  inputFile >> *matrix ;
168 
169  return matrix ;
170 }
171 
172 
173 std::vector<CLHEP::HepGenMatrix*> *
175 {
176  // open the output file
177  std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
178  assert (inputFile != NULL) ;
179 
180  // get the vector length
181  int numElem = 0 ;
182  inputFile >> numElem ;
183 
184  // get the matrix dimensions
185  int numRow = 0 ;
186  int numCol = 0 ;
187  inputFile >> numRow ;
188  inputFile >> numCol ;
189 
190  //PG prepara il vector
191  std::vector<CLHEP::HepGenMatrix*>* matrixVector =
192  new std::vector<CLHEP::HepGenMatrix*> (numElem) ;
193 
194  //PG loop sugli elementi del vettore
195  for (int i=0 ; i<numElem ; ++i)
196  {
197  //PG definisce il puntatore
198  CLHEP::HepGenMatrix * matrix ;
199  //PG attribuisce un oggetto concreto
200 
201  if (numCol > 1)
202  matrix = new CLHEP::HepMatrix (numRow, numCol, 0) ;
203  else
204  matrix = new CLHEP::HepVector (numRow, 0) ;
205 
206  //PG scarica su un oggetto concreto
207  inputFile >> *matrix ;
208 
209  //PG riempie il vettore
210  (*matrixVector)[i] = matrix ;
211  }
212 
213  return matrixVector ;
214 }
215 
216 
217 std::vector<CLHEP::HepMatrix>
219 {
220  // open the output file
221  std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
222  assert (inputFile != NULL) ;
223 
224  // get the vector length
225  int numElem = 0 ;
226  inputFile >> numElem ;
227 
228  // get the matrix dimensions
229  int numRow = 0 ;
230  int numCol = 0 ;
231  inputFile >> numRow ;
232  inputFile >> numCol ;
233 
234  //PG prepara il vector
235  std::vector<CLHEP::HepMatrix> matrixVector (
236  numElem,
237  CLHEP::HepMatrix (numRow,numCol,0)
238  ) ;
239 
240  //PG loop sugli elementi del vettore
241  for (int i=0 ; i<numElem ; ++i)
242  {
243 
244  //PG scarica su un oggetto concreto
245  inputFile >> matrixVector[i] ;
246 
247  }
248 
249  return matrixVector ;
250 }
int i
Definition: DBlmapReader.cc:9
int saveMatrix(std::string outputFileName, const CLHEP::HepGenMatrix *saveMe)
Definition: matrixSaver.cc:58
#define NULL
Definition: scimark2.h:8
std::ostream & operator<<(std::ostream &out, const ALILine &li)
Definition: ALILine.cc:187
static std::string const input
Definition: EdmProvDump.cc:44
std::vector< CLHEP::HepGenMatrix * > * getMatrixVector(std::string inputFileName)
Definition: matrixSaver.cc:174
int touch(std::string inputFileName)
Definition: matrixSaver.cc:138
CLHEP::HepGenMatrix * getMatrix(std::string inputFileName)
Definition: matrixSaver.cc:147
tuple out
Definition: dbtoconf.py:99
std::istream & operator>>(std::istream &input, CLHEP::HepGenMatrix &matrix)
Definition: matrixSaver.cc:111
tuple filename
Definition: lut2db_cfg.py:20
int 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:218