Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "Alignment/CommonAlignment/interface/Alignable.h"

#include "Alignment/CommonAlignment/interface/AlignmentParameters.h"

//__________________________________________________________________________________________________
AlignmentParameters::AlignmentParameters() : theAlignable(nullptr), theUserVariables(nullptr), bValid(true) {}

//__________________________________________________________________________________________________
AlignmentParameters::AlignmentParameters(Alignable* object, const AlgebraicVector& par, const AlgebraicSymMatrix& cov)
    : theAlignable(object),
      theData(DataContainer(new AlignmentParametersData(par, cov))),
      theUserVariables(nullptr),
      bValid(true) {
  // is the data consistent?
  theData->checkConsistency();
}

//__________________________________________________________________________________________________
AlignmentParameters::AlignmentParameters(Alignable* object,
                                         const AlgebraicVector& par,
                                         const AlgebraicSymMatrix& cov,
                                         const std::vector<bool>& sel)
    : theAlignable(object),
      theData(DataContainer(new AlignmentParametersData(par, cov, sel))),
      theUserVariables(nullptr),
      bValid(true) {
  // is the data consistent?
  theData->checkConsistency();
}

//__________________________________________________________________________________________________
AlignmentParameters::AlignmentParameters(Alignable* object, const AlignmentParametersData::DataContainer& data)
    : theAlignable(object), theData(data), theUserVariables(nullptr), bValid(true) {
  // is the data consistent?
  theData->checkConsistency();
}

//__________________________________________________________________________________________________
AlignmentParameters::~AlignmentParameters() {
  if (theUserVariables)
    delete theUserVariables;
}

//__________________________________________________________________________________________________
const std::vector<bool>& AlignmentParameters::selector(void) const { return theData->selector(); }

//__________________________________________________________________________________________________
int AlignmentParameters::numSelected(void) const { return theData->numSelected(); }

//__________________________________________________________________________________________________
AlgebraicVector AlignmentParameters::selectedParameters(void) const {
  return collapseVector(theData->parameters(), theData->selector());
}

//__________________________________________________________________________________________________
AlgebraicSymMatrix AlignmentParameters::selectedCovariance(void) const {
  return collapseSymMatrix(theData->covariance(), theData->selector());
}

//__________________________________________________________________________________________________
const AlgebraicVector& AlignmentParameters::parameters(void) const { return theData->parameters(); }

//__________________________________________________________________________________________________
const AlgebraicSymMatrix& AlignmentParameters::covariance(void) const { return theData->covariance(); }

//__________________________________________________________________________________________________
AlgebraicMatrix AlignmentParameters::selectedDerivatives(const TrajectoryStateOnSurface& tsos,
                                                         const AlignableDetOrUnitPtr& alignableDet) const {
  const AlgebraicMatrix dev(this->derivatives(tsos, alignableDet));

  const int ncols = dev.num_col();
  const int nrows = dev.num_row();
  const int nsel = numSelected();

  AlgebraicMatrix seldev(nsel, ncols);

  int ir2 = 0;
  for (int irow = 0; irow < nrows; ++irow) {
    if (this->selector()[irow]) {
      for (int icol = 0; icol < ncols; ++icol) {
        seldev[ir2][icol] = dev[irow][icol];
      }
      ++ir2;
    }
  }

  return seldev;
}

//__________________________________________________________________________________________________
void AlignmentParameters::setUserVariables(AlignmentUserVariables* auv) {
  if (theUserVariables)
    delete theUserVariables;
  theUserVariables = auv;
}

//__________________________________________________________________________________________________
AlignmentUserVariables* AlignmentParameters::userVariables(void) const { return theUserVariables; }

//__________________________________________________________________________________________________
Alignable* AlignmentParameters::alignable(void) const { return theAlignable; }

//__________________________________________________________________________________________________
unsigned int AlignmentParameters::hierarchyLevel() const {
  if (!theAlignable) {
    edm::LogError("Alignment") << "@SUB=AlignmentParameters::hierarchyLevel"
                               << "Called for AlignmentParameters without pointer to Alignable";
    return 0;
  }

  align::Alignables comps;
  theAlignable->firstCompsWithParams(comps);
  if (comps.empty())
    return 0;

  unsigned int maxLevelOfComp = 0;
  for (const auto& iAli : comps) {  // firstCompsWithParams guaranties that alignmentParameters() != 0:
    const unsigned int compResult = iAli->alignmentParameters()->hierarchyLevel();
    // levels might be different for components, get largest:
    if (maxLevelOfComp < compResult)
      maxLevelOfComp = compResult;
  }

  return maxLevelOfComp + 1;
}

//__________________________________________________________________________________________________
int AlignmentParameters::size(void) const { return theData->parameters().num_row(); }

//__________________________________________________________________________________________________
bool AlignmentParameters::isValid(void) const { return bValid; }

//__________________________________________________________________________________________________
void AlignmentParameters::setValid(bool v) { bValid = v; }

//__________________________________________________________________________________________________
AlgebraicSymMatrix AlignmentParameters::collapseSymMatrix(const AlgebraicSymMatrix& m,
                                                          const std::vector<bool>& sel) const {
  int nRows = m.num_row();
  int size = sel.size();

  // Check size matching
  if (nRows != size)
    throw cms::Exception("LogicError") << "Size mismatch in parameters"
                                       << " (input symmatrix = " << nRows << ", selection: " << size << ")";

  // If OK, continue
  std::vector<int> rowvec;
  for (int i = 0; i < nRows; i++)
    if (sel[i])
      rowvec.push_back(i);

  int nSelectedRows = rowvec.size();
  AlgebraicSymMatrix result(nSelectedRows, 0);
  for (int i = 0; i < nSelectedRows; i++)
    for (int j = 0; j < nSelectedRows; j++)
      result[i][j] = m[rowvec[i]][rowvec[j]];

  return result;
}

//__________________________________________________________________________________________________
AlgebraicVector AlignmentParameters::collapseVector(const AlgebraicVector& m, const std::vector<bool>& sel) const {
  int nRows = m.num_row();
  int size = sel.size();

  // Check size matching
  if (nRows != size)
    throw cms::Exception("LogicError") << "Size mismatch in parameters"
                                       << " (input vector = " << nRows << ", selection: " << size << ")";

  // If OK, continue
  std::vector<int> rowvec;
  for (int i = 0; i < nRows; i++)
    if (sel[i])
      rowvec.push_back(i);

  int nSelectedRows = rowvec.size();
  AlgebraicVector result(nSelectedRows, 0);
  for (int i = 0; i < nSelectedRows; i++)
    result[i] = m[(int)rowvec[i]];

  return result;
}

//__________________________________________________________________________________________________
AlgebraicSymMatrix AlignmentParameters::expandSymMatrix(const AlgebraicSymMatrix& m,
                                                        const std::vector<bool>& sel) const {
  int nRows = m.num_row();
  int size = sel.size();

  std::vector<int> rowvec;
  for (int i = 0; i < size; i++)
    if (sel[i])
      rowvec.push_back(i);

  // Check size matching
  if (nRows != static_cast<int>(rowvec.size()))
    throw cms::Exception("LogicError") << "Size mismatch in parameters"
                                       << " (input symmatrix = " << nRows << ", selection: " << size << ")";

  // If OK, continue
  AlgebraicSymMatrix result(size, 0);
  for (int i = 0; i < nRows; i++)
    for (int j = 0; j < nRows; j++)
      result[rowvec[i]][rowvec[j]] = m[i][j];

  return result;
}

//__________________________________________________________________________________________________
AlgebraicVector AlignmentParameters::expandVector(const AlgebraicVector& m, const std::vector<bool>& sel) const {
  int nRows = m.num_row();
  int size = sel.size();

  std::vector<int> rowvec;
  for (int i = 0; i < size; i++)
    if (sel[i] == true)
      rowvec.push_back(i);

  // Check size matching
  if (nRows != static_cast<int>(rowvec.size()))
    throw cms::Exception("LogicError") << "Size mismatch in parameters"
                                       << " (input vector = " << nRows << ", selection: " << size << ")";

  // If OK, continue
  AlgebraicVector result(size, 0);
  for (int i = 0; i < nRows; i++)
    result[rowvec[i]] = m[i];
  return result;
}