CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SurveyResidual.cc
Go to the documentation of this file.
6 
8 
9 using namespace align;
10 
13  bool bias):
14  theMother(0),
15  theSurface( ali.surface() ),
16  theSelector( ali.alignmentParameters()->selector() )
17 {
18 // Find mother matching given type
19 
20  theMother = &ali; // start finding from this alignable
21 
22  while (theMother->alignableObjectId() != type)
23  {
24  theMother = theMother->mother(); // move up a level
25 
26  if (!theMother) return;
27 // {
28 // throw cms::Exception("ConfigError")
29 // << "Alignable (id = " << ali.geomDetId().rawId()
30 // << ") does not belong to a composite of type " << type;
31 // }
32  }
33 
34  if ( !theMother->mother() )
35  {
36  throw cms::Exception("ConfigError")
37  << "The type " << type << " does not have a survey residual defined!\n"
38  << "You have probably set the highest hierarchy. Choose a lower level.";
39  }
40 
41  findSisters(theMother, bias);
42 
43  if (theSisters.size() == 0)
44  {
45  throw cms::Exception("ConfigError")
46  << "You are finding an unbiased residual of an alignable "
47  << " (id = " << ali.geomDetId().rawId()
48  << ") which has no sister. Abort!";
49  }
50 
51  calculate(ali);
52 }
53 
55 {
56  std::vector<Scalar> pars; // selected parameters
57 
58  pars.reserve(AlignParams::kSize);
59 
60 // Find linear displacements.
61 
63 
64  if (theSelector[0]) pars.push_back( deltaR.x() );
65  if (theSelector[1]) pars.push_back( deltaR.y() );
66  if (theSelector[2]) pars.push_back( deltaR.z() );
67 
68 // Match the centers of current and nominal surfaces to find the angular
69 // displacements about the center. Only do this if angular dof are selected.
70 
71  if (theSelector[3] || theSelector[4] || theSelector[5])
72  {
73  GlobalVectors nominalVs = theNominalVs;
74  GlobalVectors currentVs = theCurrentVs;
75 
76  for (unsigned int j = 0; j < nominalVs.size(); ++j)
77  {
78  nominalVs[j] -= theNominalVs[0]; // move to nominal pos
79  currentVs[j] -= theCurrentVs[0]; // move to current pos
80  }
81 
82  RotationType rot = diffRot(nominalVs, currentVs); // frame rotation
83 
84  EulerAngles deltaW = toAngles( theSurface.toLocal(rot) );
85 
86  if (theSelector[3]) pars.push_back( deltaW(1) );
87  if (theSelector[4]) pars.push_back( deltaW(2) );
88  if (theSelector[5]) pars.push_back( deltaW(3) );
89  }
90 
91  AlgebraicVector deltaRW( pars.size() ); // (deltaR, deltaW)
92 
93  for (unsigned int j = 0; j < pars.size(); ++j) deltaRW(j + 1) = pars[j];
94 
95  return deltaRW;
96 }
97 
99 {
100  LocalVectors residuals;
101 
102  unsigned int nPoint = theNominalVs.size();
103 
104  residuals.reserve(nPoint);
105 
106  for (unsigned int j = 0; j < nPoint; ++j)
107  {
108  residuals.push_back( theSurface.toLocal(theCurrentVs[j] - theNominalVs[j]) );
109  }
110 
111  return residuals;
112 }
113 
115 {
116  if (theSelector.size() != ErrorMatrix::kRows)
117  {
118  throw cms::Exception("LogicError")
119  << "Mismatched number of dof between ErrorMatrix and Selector.";
120  }
121 
122  std::vector<unsigned int> indices; // selected indices
123 
124  indices.reserve(ErrorMatrix::kRows);
125 
126  for (unsigned int i = 0; i < ErrorMatrix::kRows; ++i)
127  if (theSelector[i]) indices.push_back(i);
128 
129  AlgebraicSymMatrix invCov( indices.size() );
130 
131  for (unsigned int i = 0; i < indices.size(); ++i)
132  for (unsigned int j = 0; j <= i; ++j)
133  invCov.fast(i + 1, j + 1) = theCovariance(indices[i], indices[j]);
134 
135  int fail(0); invCov.invert(fail);
136 
137  if (fail)
138  {
139  throw cms::Exception("ConfigError")
140  << "Cannot invert survey error " << invCov;
141  }
142 
143  return invCov;
144 }
145 
147  bool bias)
148 {
149  theSisters.clear();
150  theSisters.reserve(1000);
151 
152  const std::vector<Alignable*>& comp = ali->mother()->components();
153 
154  unsigned int nComp = comp.size();
155 
156  for (unsigned int i = 0; i < nComp; ++i)
157  {
158  const Alignable* dau = comp[i];
159 
160  if (dau != ali || bias)
161  theSisters.insert( theSisters.end(), dau->deepComponents().begin(), dau->deepComponents().end() );
162 // if (dau != ali || bias) theSisters.push_back(dau);
163  }
164 }
165 
167 {
168  unsigned int nSister = theSisters.size();
169 
170 // First get sisters' positions
171 
172  std::vector<const PositionType*> nominalSisPos; // nominal sisters' pos
173  std::vector<const PositionType*> currentSisPos; // current sisters' pos
174 
175  nominalSisPos.reserve(nSister);
176  currentSisPos.reserve(nSister);
177 
178  for (unsigned int i = 0; i < nSister; ++i)
179  {
180  const Alignable* sis = theSisters[i];
181  const SurveyDet* survey = sis->survey();
182 
183  if (!survey)
184  {
185  throw cms::Exception("ConfigError")
186  << "No survey info is found for Alignable "
187  << " (id = " << sis->geomDetId().rawId() << "). Abort!";
188  }
189 
190  nominalSisPos.push_back( &survey->position() );
191  currentSisPos.push_back( &sis->globalPosition() );
192  }
193 
194 // Then find mother's position using sisters' positions
195 
196  PositionType nominalMomPos = motherPosition(nominalSisPos);
197  PositionType currentMomPos = motherPosition(currentSisPos);
198 
199 // Now find rotation from nominal mother to current mother
200 
201  GlobalVectors nominalSisVs; // nominal sisters' pos from mother's pos
202  GlobalVectors currentSisVs; // current sisters' pos from mother's pos
203 
204  for (unsigned int i = 0; i < nSister; ++i)
205  {
206  const Alignable* sis = theSisters[i];
207 
208  const GlobalPoints& nominalSisPoints = sis->survey()->globalPoints();
209  const GlobalPoints& currentSisPoints = sis->surface().toGlobal( sis->survey()->localPoints() );
210 
211  for (unsigned int j = 0; j < nominalSisPoints.size(); ++j)
212  {
213  nominalSisVs.push_back(nominalSisPoints[j] - *nominalSisPos[i]);
214  currentSisVs.push_back(currentSisPoints[j] - *currentSisPos[i]);
215 // nominalSisVs.push_back(nominalSisPoints[j] - nominalMomPos);
216 // currentSisVs.push_back(currentSisPoints[j] - currentMomPos);
217  }
218  }
219 
220  RotationType toCurrent = diffRot(currentSisVs, nominalSisVs);
221 
222 // Finally shift and rotate nominal sensor to current sensor
223 
224  const SurveyDet* survey = ali.survey();
225 
226  if (!survey)
227  {
228  throw cms::Exception("ConfigError")
229  << "No survey info is found for Alignable "
230  << " (id = " << ali.geomDetId().rawId() << "). Abort!";
231  }
232 
233  const GlobalPoints& nominalPoints = survey->globalPoints();
234  const GlobalPoints& currentPoints = theSurface.toGlobal( survey->localPoints() );
235 
236  for (unsigned int j = 0; j < nominalPoints.size(); ++j)
237  {
238  align::GlobalVector nv = nominalPoints[j] - nominalMomPos;
239 
240  theNominalVs.push_back( align::GlobalVector( toCurrent * nv.basicVector() ) );
241  theCurrentVs.push_back(currentPoints[j] - currentMomPos);
242  }
243 
244 // Find the covariance
245 
246  const RotationType& currentFrame = ali.globalRotation();
247 
248  for ( const Alignable* a = &ali; a != theMother->mother(); a = a->mother() )
249  {
250  RotationType deltaR = currentFrame * a->survey()->rotation().transposed();
251 
252  math::Matrix<6, 6>::type jac; // 6 by 6 Jacobian init to 0
253 
254  jac(0, 0) = deltaR.xx(); jac(0, 1) = deltaR.xy(); jac(0, 2) = deltaR.xz();
255  jac(1, 0) = deltaR.yx(); jac(1, 1) = deltaR.yy(); jac(1, 2) = deltaR.yz();
256  jac(2, 0) = deltaR.zx(); jac(2, 1) = deltaR.zy(); jac(2, 2) = deltaR.zz();
257  jac(3, 3) = deltaR.xx(); jac(3, 4) = deltaR.xy(); jac(3, 5) = deltaR.xz();
258  jac(4, 3) = deltaR.yx(); jac(4, 4) = deltaR.yy(); jac(4, 5) = deltaR.yz();
259  jac(5, 3) = deltaR.zx(); jac(5, 4) = deltaR.zy(); jac(5, 5) = deltaR.zz();
260 
261  theCovariance += ROOT::Math::Similarity( jac, a->survey()->errors() );
262  }
263 }
264 
265 // AlgebraicMatrix SurveyResidual::errorTransform(const RotationType& initialFrame,
266 // const RotationType& currentFrame) const
267 // {
268 // // align::EulerAngles angles = align::toAngles(r);
269 
270 // // align::Scalar s1 = std::sin(angles[0]), c1 = std::cos(angles[0]);
271 // // align::Scalar s2 = std::sin(angles[1]), c2 = std::cos(angles[1]);
272 // // align::Scalar s3 = std::sin(angles[2]), c3 = std::cos(angles[2]);
273 
274 // AlgebraicMatrix drdw(9, 3, 0); // 9 by 3 Jacobian init to 0
275 
276 // // drdw(1, 1) = 0;
277 // // drdw(1, 2) = -s2 * c3;
278 // // drdw(1, 3) = c2 * -s3;
279 // // drdw(2, 1) = -s1 * s3 + c1 * s2 * c3;
280 // // drdw(2, 2) = s1 * c2 * c3;
281 // // drdw(2, 3) = c1 * c3 - s1 * s2 * s3;
282 // // drdw(3, 1) = c1 * s3 + s1 * s2 * c3;
283 // // drdw(3, 2) = -c1 * c2 * c3;
284 // // drdw(3, 3) = s1 * c3 + c1 * s2 * s3;
285 // // drdw(4, 1) = 0;
286 // // drdw(4, 2) = s2 * s3;
287 // // drdw(4, 3) = -c2 * c3;
288 // // drdw(5, 1) = -s1 * c3 - c1 * s2 * s3;
289 // // drdw(5, 2) = -s1 * c2 * s3;
290 // // drdw(5, 3) = c1 * -s3 - s1 * s2 * c3;
291 // // drdw(6, 1) = c1 * c3 - s1 * s2 * s3;
292 // // drdw(6, 2) = c1 * c2 * s3;
293 // // drdw(6, 3) = s1 * -s3 + c1 * s2 * c3;
294 // // drdw(7, 1) = 0;
295 // // drdw(7, 2) = c2;
296 // // drdw(7, 3) = 0;
297 // // drdw(8, 1) = -c1 * c2;
298 // // drdw(8, 2) = s1 * s2;
299 // // drdw(8, 3) = 0;
300 // // drdw(9, 1) = -s1 * c2;
301 // // drdw(9, 2) = c1 * -s2;
302 // // drdw(9, 3) = 0;
303 // drdw(2, 3) = drdw(6, 1) = drdw(7, 2) = 1;
304 // drdw(3, 2) = drdw(4, 3) = drdw(8, 1) = -1;
305 
306 // align::RotationType deltaR = initialFrame * currentFrame.transposed();
307 
308 // AlgebraicMatrix dRdr(9, 9, 0); // 9 by 9 Jacobian init to 0
309 
310 // dRdr(1, 1) = deltaR.xx(); dRdr(1, 2) = deltaR.yx(); dRdr(1, 3) = deltaR.zx();
311 // dRdr(2, 1) = deltaR.xy(); dRdr(2, 2) = deltaR.yy(); dRdr(2, 3) = deltaR.zy();
312 // dRdr(3, 1) = deltaR.xz(); dRdr(3, 2) = deltaR.yz(); dRdr(3, 3) = deltaR.zz();
313 // dRdr(4, 4) = deltaR.xx(); dRdr(4, 5) = deltaR.yx(); dRdr(4, 6) = deltaR.zx();
314 // dRdr(5, 4) = deltaR.xy(); dRdr(5, 5) = deltaR.yy(); dRdr(5, 6) = deltaR.zy();
315 // dRdr(6, 4) = deltaR.xz(); dRdr(6, 5) = deltaR.yz(); dRdr(6, 6) = deltaR.zz();
316 // dRdr(7, 7) = deltaR.xx(); dRdr(7, 8) = deltaR.yx(); dRdr(7, 9) = deltaR.zx();
317 // dRdr(8, 7) = deltaR.xy(); dRdr(8, 8) = deltaR.yy(); dRdr(8, 9) = deltaR.zy();
318 // dRdr(9, 7) = deltaR.xz(); dRdr(9, 8) = deltaR.yz(); dRdr(9, 9) = deltaR.zz();
319 
320 // // align::RotationType R = r * deltaR;
321 
322 // AlgebraicMatrix dWdR(3, 9, 0); // 3 by 9 Jacobian init to 0
323 
324 // align::Scalar R11 = deltaR.xx(), R21 = deltaR.yx();
325 // align::Scalar R31 = deltaR.zx(), R32 = deltaR.zy(), R33 = deltaR.zz();
326 
327 // align::Scalar den1 = R32 * R32 + R33 * R33;
328 // align::Scalar den3 = R11 * R11 + R21 * R21;
329 
330 // dWdR(1, 8) = -R33 / den1; dWdR(1, 9) = R32 / den1;
331 // dWdR(2, 7) = 1 / std::sqrt(1 - R31 * R31);
332 // dWdR(3, 1) = R21 / den3; dWdR(3, 4) = -R11 / den3;
333 
334 // AlgebraicMatrix dPdp(6, 6, 0);
335 
336 // dPdp(1, 1) = deltaR.xx(); dPdp(1, 2) = deltaR.xy(); dPdp(1, 3) = deltaR.xz();
337 // dPdp(2, 1) = deltaR.yx(); dPdp(2, 2) = deltaR.yy(); dPdp(2, 3) = deltaR.yz();
338 // dPdp(3, 1) = deltaR.zx(); dPdp(3, 2) = deltaR.zy(); dPdp(3, 3) = deltaR.zz();
339 
340 // dPdp.sub(4, 4, dWdR * dRdr * drdw);
341 
342 // return dPdp;
343 // }
T xx() const
type
Definition: HCALResponse.h:22
int i
Definition: DBlmapReader.cc:9
std::vector< const Alignable * > theSisters
ROOT::Math::SMatrix< double, N, M > type
Definition: Matrix.h:10
const SurveyDet * survey() const
Return survey info.
Definition: Alignable.h:205
std::vector< LocalVector > LocalVectors
Definition: Utilities.h:26
T y() const
Definition: PV3DBase.h:62
T yx() const
const RotationType & globalRotation() const
Return the global orientation of the object.
Definition: Alignable.h:132
void calculate(const Alignable &)
Find the nominal and current vectors.
virtual Alignables components() const =0
Return vector of all direct components.
RotationType diffRot(const GlobalVectors &current, const GlobalVectors &nominal)
Definition: Utilities.cc:71
std::vector< GlobalPoint > GlobalPoints
Definition: Utilities.h:23
uint32_t rawId() const
get the raw id
Definition: DetId.h:45
T zx() const
T xy() const
const align::PositionType & position() const
Definition: SurveyDet.h:63
const std::vector< bool > & theSelector
T zz() const
align::GlobalVectors theCurrentVs
align::RotationType toLocal(const align::RotationType &) const
Return in local frame a rotation given in global frame.
align::GlobalVectors theNominalVs
T z() const
Definition: PV3DBase.h:63
SurveyResidual(const Alignable &, align::StructureType, bool bias=false)
virtual StructureType alignableObjectId() const =0
Return the alignable type identifier.
T zy() const
int j
Definition: DBlmapReader.cc:9
EulerAngles toAngles(const RotationType &)
Convert rotation matrix to angles about x-, y-, z-axes (frame rotation).
Definition: Utilities.cc:7
T yy() const
void findSisters(const Alignable *, bool bias)
const AlignableSurface & surface() const
Return the Surface (global position and orientation) of the object.
Definition: Alignable.h:126
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
CLHEP::HepVector AlgebraicVector
AlgebraicVector EulerAngles
Definition: Definitions.h:36
const Alignables & deepComponents() const
Definition: Alignable.h:71
align::LocalVectors pointsResidual() const
AlgebraicSymMatrix inverseCovariance() const
Get inverse of survey covariance wrt given structure type in constructor.
std::vector< GlobalVector > GlobalVectors
Definition: Utilities.h:24
const Alignable * theMother
align::GlobalPoints globalPoints() const
Definition: SurveyDet.h:83
AlgebraicVector sensorResidual() const
double a
Definition: hdecay.h:121
T xz() const
align::GlobalPoints toGlobal(const align::LocalPoints &) const
Return in global coord given a set of local points.
CLHEP::HepSymMatrix AlgebraicSymMatrix
const PositionType & globalPosition() const
Return the global position of the object.
Definition: Alignable.h:129
PositionType motherPosition(const std::vector< const PositionType * > &dauPos)
Find mother&#39;s position from the average of its daughters&#39; positions.
Definition: Utilities.cc:51
const AlignableSurface & theSurface
T x() const
Definition: PV3DBase.h:61
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:56
const align::LocalPoints & localPoints() const
Definition: SurveyDet.h:78
Alignable * mother() const
Return pointer to container alignable (if any)
Definition: Alignable.h:85
T yz() const
const DetId & geomDetId() const
Definition: Alignable.h:177
align::ErrorMatrix theCovariance