CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/Calibration/Tools/src/matrixSaver.cc

Go to the documentation of this file.
00001 #include <fstream>
00002 #include <iostream> // for any std::cout
00003 #include <iomanip>  // to print nicely formatted
00004 #include <string>
00005 #include <map>
00006 #include <memory>
00007 //#include <iterator>
00008 #include <cassert>
00009 
00010 #include "CLHEP/Matrix/GenMatrix.h"
00011 #include "CLHEP/Matrix/Matrix.h"
00012 #include "CLHEP/Matrix/Vector.h"
00013 
00014 #include "Calibration/Tools/interface/matrixSaver.h"
00015 
00016 matrixSaver::matrixSaver ()
00017 {
00018 //   std::cout << "[matrixSaver][ctor] matrixSaver instance" << std::endl ;
00019 }
00020 
00021 
00022 matrixSaver::~matrixSaver ()
00023 {
00024 //   std::cout << "[matrixSaver][dtor] destroyed" << std::endl ;
00025 }
00026 
00027 
00028 std::ostream &
00029 operator<< (std::ostream& outputFile,
00030             const CLHEP::HepGenMatrix *saveMe)
00031 {
00032 
00033   int numRow = saveMe->num_row () ;
00034   int numCol = saveMe->num_col () ;
00035 
00036   // write out the matrix dimensions
00037   outputFile << numRow << '\t'
00038                  << numCol << '\n' ;
00039 
00040   // write the elements in the file
00041   for (int row=0 ; row<numRow ; ++row)
00042     {
00043             for (int col=0 ; col<numCol ; ++col)
00044         {
00045                                                 assert (row < numRow) ;
00046             assert (col < numCol) ;
00047                   outputFile << (*saveMe)[row][col] << '\t' ;
00048         }
00049             outputFile << '\n' ;
00050     }
00051 
00052    return outputFile ;
00053 }
00054 
00055 
00056 
00057 int
00058 matrixSaver::saveMatrix (std::string outputFileName,
00059                         const CLHEP::HepGenMatrix *saveMe)
00060 {
00061    // open the output file
00062    std::fstream outputFile (outputFileName.c_str (), std::ios::out) ;
00063    assert (outputFile) ;
00064  
00065    int numRow = saveMe->num_row () ;
00066    int numCol = saveMe->num_col () ;
00067 
00068    // write out the matrix dimensions
00069    outputFile << numRow << '\t'
00070                   << numCol << '\n' ;
00071 
00072    outputFile << saveMe ;
00073 
00074    return 0 ;
00075 
00076 }
00077 
00078 
00079 int
00080 matrixSaver::saveMatrixVector (std::string filename,
00081                                const std::vector<CLHEP::HepGenMatrix *> &saveMe)
00082 {
00083      typedef std::vector<CLHEP::HepGenMatrix*>::const_iterator const_iterator ;
00084      // open the output file
00085      std::fstream outputFile (filename.c_str (), std::ios::out) ;
00086      assert (outputFile != NULL) ;
00087 
00088      // save the number of elements of the vector
00089      outputFile << saveMe.size ()       
00090                 << '\n' ;
00091 
00092      // save the matrix sizes
00093      outputFile << (*saveMe.begin ())->num_row ()
00094                 << '\t'
00095                 << (*saveMe.begin ())->num_col ()
00096                 << '\n' ;
00097 
00098      // loop over the vector
00099      for (const_iterator it = saveMe.begin () ;
00100                it != saveMe.end () ;
00101                ++it)
00102        {
00103                outputFile << (*it) ;
00104        } // loop over the vecor
00105 
00106      return 0 ;
00107 }
00108 
00109 
00110 std::istream &
00111 operator>> (std::istream& input, CLHEP::HepGenMatrix &matrix)
00112 {
00113   int numRow = 0 ;
00114   int numCol = 0 ;
00115   
00116   //PG read the matrix dimension
00117   input >> numRow ;
00118   input >> numCol ;
00119   
00120   //PG check whether the matrices have the right dimension
00121   assert ( numRow == matrix.num_row () ) ;
00122   assert ( numCol == matrix.num_col () ) ;
00123 
00124   //PG get the matrix elements from the file
00125   for (int row=0 ; row<numRow ; ++row)
00126     {
00127       for (int col=0 ; col<numCol ; ++col)
00128               {
00129                 input >> matrix[row][col] ;
00130             assert (col*row < numRow*numCol) ;
00131               } 
00132     }   
00133 
00134   return input ;
00135 }
00136 
00137 
00138 int matrixSaver::touch (std::string inputFileName)
00139 {
00140    std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
00141    return (inputFile != NULL) ;
00142 }
00143 
00144 
00145 
00146 CLHEP::HepGenMatrix *
00147 matrixSaver::getMatrix (std::string inputFileName)
00148 {
00149      //PG open the output file
00150      std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
00151      if (inputFile == NULL) std::cerr << "file: " << inputFileName << std::endl ;
00152      assert (inputFile != NULL) ;
00153 
00154      //PG get the matrix dimensions
00155      int numRow = 0 ;
00156      int numCol = 0 ;
00157      inputFile >> numRow ;
00158      inputFile >> numCol ;
00159 
00160      //PG instantiate the matrix
00161      CLHEP::HepGenMatrix * matrix ;
00162      if (numCol > 1)
00163              matrix = new CLHEP::HepMatrix (numRow, numCol, 0) ;
00164      else
00165              matrix = new CLHEP::HepVector (numRow, 0) ;
00166 
00167      inputFile >> *matrix ;
00168 
00169      return matrix ;
00170 }
00171 
00172 
00173 std::vector<CLHEP::HepGenMatrix*> *
00174 matrixSaver::getMatrixVector (std::string inputFileName)
00175 {
00176      // open the output file
00177      std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
00178      assert (inputFile != NULL) ;
00179 
00180      // get the vector length
00181      int numElem = 0 ;
00182      inputFile >> numElem ;
00183      
00184      // get the matrix dimensions
00185      int numRow = 0 ;
00186      int numCol = 0 ;
00187      inputFile >> numRow ;
00188      inputFile >> numCol ;
00189 
00190      //PG prepara il vector
00191      std::vector<CLHEP::HepGenMatrix*>* matrixVector = 
00192        new std::vector<CLHEP::HepGenMatrix*> (numElem) ;
00193           
00194      //PG loop sugli elementi del vettore
00195      for (int i=0 ; i<numElem ; ++i)
00196        {
00197           //PG definisce il puntatore
00198           CLHEP::HepGenMatrix * matrix ;
00199           //PG attribuisce un oggetto concreto
00200 
00201           if (numCol > 1)
00202                   matrix = new CLHEP::HepMatrix (numRow, numCol, 0) ;
00203           else
00204                   matrix = new CLHEP::HepVector (numRow, 0) ;
00205           
00206           //PG scarica su un oggetto concreto
00207           inputFile >> *matrix ;
00208 
00209           //PG riempie il vettore
00210           (*matrixVector)[i] = matrix ;
00211        }
00212 
00213      return matrixVector ;
00214 }
00215 
00216 
00217 std::vector<CLHEP::HepMatrix> 
00218 matrixSaver::getConcreteMatrixVector (std::string inputFileName)
00219 {
00220      // open the output file
00221      std::fstream inputFile (inputFileName.c_str (), std::ios::in) ;
00222      assert (inputFile != NULL) ;
00223 
00224      // get the vector length
00225      int numElem = 0 ;
00226      inputFile >> numElem ;
00227      
00228      // get the matrix dimensions
00229      int numRow = 0 ;
00230      int numCol = 0 ;
00231      inputFile >> numRow ;
00232      inputFile >> numCol ;
00233 
00234      //PG prepara il vector
00235      std::vector<CLHEP::HepMatrix> matrixVector (
00236          numElem,
00237          CLHEP::HepMatrix (numRow,numCol,0)
00238        ) ;
00239      
00240      //PG loop sugli elementi del vettore
00241      for (int i=0 ; i<numElem ; ++i)
00242        {
00243           
00244           //PG scarica su un oggetto concreto
00245           inputFile >> matrixVector[i] ;
00246 
00247        }
00248 
00249      return matrixVector ;
00250 }