Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:14:24

0001 /** 
0002 */
0003 
0004 #include "Calibration/Tools/interface/BlockSolver.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 int BlockSolver::operator()(const CLHEP::HepMatrix& matrix, const CLHEP::HepVector& vector, CLHEP::HepVector& result) {
0007   double threshold = matrix.trace() / static_cast<double>(matrix.num_row());
0008   std::vector<int> holes;
0009   // loop over the matrix rows
0010   for (int row = 0; row < matrix.num_row(); ++row) {
0011     double sumAbs = 0.;
0012     for (int col = 0; col < matrix.num_col(); ++col)
0013       sumAbs += fabs(matrix[row][col]);
0014     if (sumAbs < threshold)
0015       holes.push_back(row);
0016   }  // loop over the matrix rows
0017 
0018   int dim = matrix.num_col() - holes.size();
0019 
0020   if (holes.empty())  //PG exceptional case!
0021   {
0022     for (int i = 0; i < result.num_row(); ++i)
0023       result[i] = 1.;
0024   } else if (dim > 0) {
0025     CLHEP::HepMatrix solution(dim, dim, 0);
0026     CLHEP::HepVector input(dim, 0);
0027     shrink(matrix, solution, vector, input, holes);
0028     CLHEP::HepVector output = solve(solution, input);
0029     pour(result, output, holes);
0030   }
0031   return holes.size();
0032 }
0033 
0034 // ------------------------------------------------------------
0035 
0036 void BlockSolver::shrink(const CLHEP::HepMatrix& matrix,
0037                          CLHEP::HepMatrix& solution,
0038                          const CLHEP::HepVector& result,
0039                          CLHEP::HepVector& input,
0040                          const std::vector<int>& where) {
0041   int offsetRow = 0;
0042   std::vector<int>::const_iterator whereRows = where.begin();
0043   // loop over rows
0044   for (int row = 0; row < matrix.num_row(); ++row) {
0045     if (row == *whereRows) {
0046       //          std::cerr << "        DEBUG shr hole found " << std::endl ;
0047       ++offsetRow;
0048       ++whereRows;
0049       continue;
0050     }
0051     input[row - offsetRow] = result[row];
0052     int offsetCol = 0;
0053     std::vector<int>::const_iterator whereCols = where.begin();
0054     // loop over columns
0055     for (int col = 0; col < matrix.num_col(); ++col) {
0056       if (col == *whereCols) {
0057         ++offsetCol;
0058         ++whereCols;
0059         continue;
0060       }
0061       solution[row - offsetRow][col - offsetCol] = matrix[row][col];
0062     }
0063   }  // loop over rows
0064   return;
0065 }
0066 
0067 // ------------------------------------------------------------
0068 
0069 void BlockSolver::pour(CLHEP::HepVector& result, const CLHEP::HepVector& output, const std::vector<int>& where) {
0070   std::vector<int>::const_iterator whereCols = where.begin();
0071   int readingIndex = 0;
0072   //PG loop over the output crystals
0073   for (int xtal = 0; xtal < result.num_row(); ++xtal) {
0074     if (xtal == *whereCols) {
0075       result[xtal] = 1.;
0076       ++whereCols;
0077     } else {
0078       result[xtal] = output[readingIndex];
0079       ++readingIndex;
0080     }
0081   }  //PG loop over the output crystals
0082 
0083   return;
0084 }