CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Calibration/Tools/src/BlockSolver.cc

Go to the documentation of this file.
00001 
00008 #include "Calibration/Tools/interface/BlockSolver.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 int 
00011 BlockSolver::operator () (const CLHEP::HepMatrix & matrix, 
00012                           const CLHEP::HepVector & vector,
00013                           CLHEP::HepVector & result) 
00014 {
00015   double threshold = matrix.trace () / 
00016                      static_cast<double> (matrix.num_row ()) ;
00017   std::vector<int> holes ;
00018   // loop over the matrix rows
00019   for (int row = 0 ; row < matrix.num_row () ; ++row)
00020     {
00021       double sumAbs = 0. ;
00022       for (int col = 0 ; col < matrix.num_col () ; ++col)
00023         sumAbs += fabs (matrix[row][col]) ;
00024       if (sumAbs < threshold) holes.push_back (row) ;  
00025     } // loop over the matrix rows
00026 
00027   int dim = matrix.num_col () - holes.size () ;
00028 
00029   if (holes.size () == 0) //PG exceptional case!
00030     {
00031       for (int i = 0 ; i < result.num_row () ; ++i)
00032         result[i] = 1. ;
00033     }    
00034   else if (dim > 0) 
00035     {
00036       CLHEP::HepMatrix solution (dim, dim, 0) ;
00037       CLHEP::HepVector input (dim, 0) ;
00038       shrink (matrix, solution, vector, input, holes) ;
00039       CLHEP::HepVector output = solve (solution,input) ;
00040       pour (result, output, holes) ;
00041     } 
00042   return holes.size () ;
00043 }
00044 
00045 
00046 // ------------------------------------------------------------
00047 
00048 
00049 void 
00050 BlockSolver::shrink (const CLHEP::HepMatrix & matrix,
00051                      CLHEP::HepMatrix & solution,
00052                      const CLHEP::HepVector & result,
00053                      CLHEP::HepVector & input,
00054                      const std::vector<int> & where)
00055 {
00056   
00057   int offsetRow = 0 ;
00058   std::vector<int>::const_iterator whereRows = where.begin () ;
00059   // loop over rows
00060   for (int row = 0 ; row < matrix.num_row () ; ++row)
00061     {
00062       if (row == *whereRows) 
00063         {
00064 //          std::cerr << "        DEBUG shr hole found " << std::endl ;
00065           ++offsetRow ;
00066           ++whereRows ;
00067           continue ;
00068         } 
00069       input[row-offsetRow] = result[row] ;        
00070       int offsetCol = 0 ;
00071       std::vector<int>::const_iterator whereCols = where.begin () ;
00072       // loop over columns
00073       for (int col = 0 ; col < matrix.num_col () ; ++col)
00074         {
00075           if (col == *whereCols) 
00076             {
00077               ++offsetCol ;
00078               ++whereCols ;
00079               continue ;
00080             }
00081           solution[row-offsetRow][col-offsetCol] = matrix[row][col] ;    
00082         }
00083     } // loop over rows
00084   return ;  
00085 }                      
00086 
00087 
00088 // ------------------------------------------------------------
00089 
00090 
00091 void
00092 BlockSolver::pour (CLHEP::HepVector & result,
00093                    const CLHEP::HepVector & output,
00094                    const std::vector<int> & where) 
00095 {
00096   std::vector<int>::const_iterator whereCols = where.begin () ;
00097   int readingIndex = 0 ;
00098   //PG loop over the output crystals
00099   for (int xtal = 0 ; xtal < result.num_row () ; ++xtal)
00100     {
00101       if (xtal == *whereCols)
00102         {
00103           result[xtal] = 1. ;
00104           ++whereCols ;        
00105         }
00106       else
00107         {
00108           result[xtal] = output[readingIndex] ;
00109           ++readingIndex ;
00110         }    
00111     } //PG loop over the output crystals
00112 
00113   return ;  
00114 }                
00115