Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:07

0001 #include "FWCore/Utilities/interface/Exception.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 
0004 #include "Alignment/CommonAlignment/interface/Alignable.h"
0005 
0006 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
0007 
0008 //__________________________________________________________________________________________________
0009 AlignmentParameters::AlignmentParameters() : theAlignable(nullptr), theUserVariables(nullptr), bValid(true) {}
0010 
0011 //__________________________________________________________________________________________________
0012 AlignmentParameters::AlignmentParameters(Alignable* object, const AlgebraicVector& par, const AlgebraicSymMatrix& cov)
0013     : theAlignable(object),
0014       theData(DataContainer(new AlignmentParametersData(par, cov))),
0015       theUserVariables(nullptr),
0016       bValid(true) {
0017   // is the data consistent?
0018   theData->checkConsistency();
0019 }
0020 
0021 //__________________________________________________________________________________________________
0022 AlignmentParameters::AlignmentParameters(Alignable* object,
0023                                          const AlgebraicVector& par,
0024                                          const AlgebraicSymMatrix& cov,
0025                                          const std::vector<bool>& sel)
0026     : theAlignable(object),
0027       theData(DataContainer(new AlignmentParametersData(par, cov, sel))),
0028       theUserVariables(nullptr),
0029       bValid(true) {
0030   // is the data consistent?
0031   theData->checkConsistency();
0032 }
0033 
0034 //__________________________________________________________________________________________________
0035 AlignmentParameters::AlignmentParameters(Alignable* object, const AlignmentParametersData::DataContainer& data)
0036     : theAlignable(object), theData(data), theUserVariables(nullptr), bValid(true) {
0037   // is the data consistent?
0038   theData->checkConsistency();
0039 }
0040 
0041 //__________________________________________________________________________________________________
0042 AlignmentParameters::~AlignmentParameters() {
0043   if (theUserVariables)
0044     delete theUserVariables;
0045 }
0046 
0047 //__________________________________________________________________________________________________
0048 const std::vector<bool>& AlignmentParameters::selector(void) const { return theData->selector(); }
0049 
0050 //__________________________________________________________________________________________________
0051 int AlignmentParameters::numSelected(void) const { return theData->numSelected(); }
0052 
0053 //__________________________________________________________________________________________________
0054 AlgebraicVector AlignmentParameters::selectedParameters(void) const {
0055   return collapseVector(theData->parameters(), theData->selector());
0056 }
0057 
0058 //__________________________________________________________________________________________________
0059 AlgebraicSymMatrix AlignmentParameters::selectedCovariance(void) const {
0060   return collapseSymMatrix(theData->covariance(), theData->selector());
0061 }
0062 
0063 //__________________________________________________________________________________________________
0064 const AlgebraicVector& AlignmentParameters::parameters(void) const { return theData->parameters(); }
0065 
0066 //__________________________________________________________________________________________________
0067 const AlgebraicSymMatrix& AlignmentParameters::covariance(void) const { return theData->covariance(); }
0068 
0069 //__________________________________________________________________________________________________
0070 AlgebraicMatrix AlignmentParameters::selectedDerivatives(const TrajectoryStateOnSurface& tsos,
0071                                                          const AlignableDetOrUnitPtr& alignableDet) const {
0072   const AlgebraicMatrix dev(this->derivatives(tsos, alignableDet));
0073 
0074   const int ncols = dev.num_col();
0075   const int nrows = dev.num_row();
0076   const int nsel = numSelected();
0077 
0078   AlgebraicMatrix seldev(nsel, ncols);
0079 
0080   int ir2 = 0;
0081   for (int irow = 0; irow < nrows; ++irow) {
0082     if (this->selector()[irow]) {
0083       for (int icol = 0; icol < ncols; ++icol) {
0084         seldev[ir2][icol] = dev[irow][icol];
0085       }
0086       ++ir2;
0087     }
0088   }
0089 
0090   return seldev;
0091 }
0092 
0093 //__________________________________________________________________________________________________
0094 void AlignmentParameters::setUserVariables(AlignmentUserVariables* auv) {
0095   if (theUserVariables)
0096     delete theUserVariables;
0097   theUserVariables = auv;
0098 }
0099 
0100 //__________________________________________________________________________________________________
0101 AlignmentUserVariables* AlignmentParameters::userVariables(void) const { return theUserVariables; }
0102 
0103 //__________________________________________________________________________________________________
0104 Alignable* AlignmentParameters::alignable(void) const { return theAlignable; }
0105 
0106 //__________________________________________________________________________________________________
0107 unsigned int AlignmentParameters::hierarchyLevel() const {
0108   if (!theAlignable) {
0109     edm::LogError("Alignment") << "@SUB=AlignmentParameters::hierarchyLevel"
0110                                << "Called for AlignmentParameters without pointer to Alignable";
0111     return 0;
0112   }
0113 
0114   align::Alignables comps;
0115   theAlignable->firstCompsWithParams(comps);
0116   if (comps.empty())
0117     return 0;
0118 
0119   unsigned int maxLevelOfComp = 0;
0120   for (const auto& iAli : comps) {  // firstCompsWithParams guaranties that alignmentParameters() != 0:
0121     const unsigned int compResult = iAli->alignmentParameters()->hierarchyLevel();
0122     // levels might be different for components, get largest:
0123     if (maxLevelOfComp < compResult)
0124       maxLevelOfComp = compResult;
0125   }
0126 
0127   return maxLevelOfComp + 1;
0128 }
0129 
0130 //__________________________________________________________________________________________________
0131 int AlignmentParameters::size(void) const { return theData->parameters().num_row(); }
0132 
0133 //__________________________________________________________________________________________________
0134 bool AlignmentParameters::isValid(void) const { return bValid; }
0135 
0136 //__________________________________________________________________________________________________
0137 void AlignmentParameters::setValid(bool v) { bValid = v; }
0138 
0139 //__________________________________________________________________________________________________
0140 AlgebraicSymMatrix AlignmentParameters::collapseSymMatrix(const AlgebraicSymMatrix& m,
0141                                                           const std::vector<bool>& sel) const {
0142   int nRows = m.num_row();
0143   int size = sel.size();
0144 
0145   // Check size matching
0146   if (nRows != size)
0147     throw cms::Exception("LogicError") << "Size mismatch in parameters"
0148                                        << " (input symmatrix = " << nRows << ", selection: " << size << ")";
0149 
0150   // If OK, continue
0151   std::vector<int> rowvec;
0152   for (int i = 0; i < nRows; i++)
0153     if (sel[i])
0154       rowvec.push_back(i);
0155 
0156   int nSelectedRows = rowvec.size();
0157   AlgebraicSymMatrix result(nSelectedRows, 0);
0158   for (int i = 0; i < nSelectedRows; i++)
0159     for (int j = 0; j < nSelectedRows; j++)
0160       result[i][j] = m[rowvec[i]][rowvec[j]];
0161 
0162   return result;
0163 }
0164 
0165 //__________________________________________________________________________________________________
0166 AlgebraicVector AlignmentParameters::collapseVector(const AlgebraicVector& m, const std::vector<bool>& sel) const {
0167   int nRows = m.num_row();
0168   int size = sel.size();
0169 
0170   // Check size matching
0171   if (nRows != size)
0172     throw cms::Exception("LogicError") << "Size mismatch in parameters"
0173                                        << " (input vector = " << nRows << ", selection: " << size << ")";
0174 
0175   // If OK, continue
0176   std::vector<int> rowvec;
0177   for (int i = 0; i < nRows; i++)
0178     if (sel[i])
0179       rowvec.push_back(i);
0180 
0181   int nSelectedRows = rowvec.size();
0182   AlgebraicVector result(nSelectedRows, 0);
0183   for (int i = 0; i < nSelectedRows; i++)
0184     result[i] = m[(int)rowvec[i]];
0185 
0186   return result;
0187 }
0188 
0189 //__________________________________________________________________________________________________
0190 AlgebraicSymMatrix AlignmentParameters::expandSymMatrix(const AlgebraicSymMatrix& m,
0191                                                         const std::vector<bool>& sel) const {
0192   int nRows = m.num_row();
0193   int size = sel.size();
0194 
0195   std::vector<int> rowvec;
0196   for (int i = 0; i < size; i++)
0197     if (sel[i])
0198       rowvec.push_back(i);
0199 
0200   // Check size matching
0201   if (nRows != static_cast<int>(rowvec.size()))
0202     throw cms::Exception("LogicError") << "Size mismatch in parameters"
0203                                        << " (input symmatrix = " << nRows << ", selection: " << size << ")";
0204 
0205   // If OK, continue
0206   AlgebraicSymMatrix result(size, 0);
0207   for (int i = 0; i < nRows; i++)
0208     for (int j = 0; j < nRows; j++)
0209       result[rowvec[i]][rowvec[j]] = m[i][j];
0210 
0211   return result;
0212 }
0213 
0214 //__________________________________________________________________________________________________
0215 AlgebraicVector AlignmentParameters::expandVector(const AlgebraicVector& m, const std::vector<bool>& sel) const {
0216   int nRows = m.num_row();
0217   int size = sel.size();
0218 
0219   std::vector<int> rowvec;
0220   for (int i = 0; i < size; i++)
0221     if (sel[i] == true)
0222       rowvec.push_back(i);
0223 
0224   // Check size matching
0225   if (nRows != static_cast<int>(rowvec.size()))
0226     throw cms::Exception("LogicError") << "Size mismatch in parameters"
0227                                        << " (input vector = " << nRows << ", selection: " << size << ")";
0228 
0229   // If OK, continue
0230   AlgebraicVector result(size, 0);
0231   for (int i = 0; i < nRows; i++)
0232     result[rowvec[i]] = m[i];
0233   return result;
0234 }