CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
KalmanAlignmentUserVariables.cc
Go to the documentation of this file.
1 
4 
7 
9 
10 using namespace std;
11 
12 // Uncomment to plot the evolution of the alignment
13 // parameters in the local rather than the global frame.
14 //#define USE_LOCAL_PARAMETERS
15 
16 
19 
20 
22  int frequency ) :
23  theParentAlignable( parent ),
24  theNumberOfHits( 0 ),
25  theNumberOfUpdates( 0 ),
26  theUpdateFrequency( frequency ),
27  theFirstUpdate( true ),
28  theAlignmentFlag( false )
29 {
30  if ( parent )
31  {
32  pair< int, int > typeAndLayer = theAlignableId->typeAndLayerFromDetId( parent->geomDetId() );
33 
34  int iType = typeAndLayer.first;
35  int iLayer = typeAndLayer.second;
36  int iId = parent->id();
37 
38  string strName = theObjectId->typeToName( parent->alignableObjectId() ) + string( "_" );
39  string strType = string( "Type" ) + toString( iType ) + string( "_" );
40  string strLayer = string( "Layer" ) + toString( iLayer ) + string( "_" );
41  string strId = string( "Id" ) + toString( iId );
42 
43  theTypeAndLayer = strType + strLayer;
44  theIdentifier = theTypeAndLayer + strName + strId;
45 
46  }
47  else theIdentifier = string( "NoAlignable" );
48 }
49 
50 
51 //KalmanAlignmentUserVariables::~KalmanAlignmentUserVariables( void ) {}
52 
53 
54 void KalmanAlignmentUserVariables::update( bool enforceUpdate )
55 {
56  if ( theParentAlignable )
57  {
59 
60  if ( ( ( theNumberOfUpdates % theUpdateFrequency == 0 ) || enforceUpdate ) )
61  {
62 
63 #ifdef USE_LOCAL_PARAMETERS
64 
67  const vector< bool >& selector = theParentAlignable->alignmentParameters()->selector();
68 
69  AlgebraicVector trueParameters( extractTrueParameters() );
70 
71  const int nParameter = 6;
72  int selected = 0;
73 
74  for ( int i = 0; i < nParameter; ++i )
75  {
76  if ( selector[i] )
77  {
78  string parameterId = selectedParameter( i ) + string( "_" ) + theIdentifier;
79 
80  if ( theFirstUpdate )
81  {
82  KalmanAlignmentDataCollector::fillGraph( string("LocalDelta") + parameterId, 0, -trueParameters[i]/selectedScaling(i) );
83  KalmanAlignmentDataCollector::fillGraph( string("LocalSigma") + parameterId, 0, sqrt(covariance[selected][selected])/selectedScaling(i) );
84  }
85 
86  KalmanAlignmentDataCollector::fillGraph( string("LocalDelta") + parameterId, theNumberOfUpdates/theUpdateFrequency, (parameters[selected]-trueParameters[i])/selectedScaling(i) );
87  KalmanAlignmentDataCollector::fillGraph( string("LocalSigma") + parameterId, theNumberOfUpdates/theUpdateFrequency, sqrt(covariance[selected][selected])/selectedScaling(i) );
88 
89  selected++;
90  }
91  }
92 
93  if ( theFirstUpdate ) theFirstUpdate = false;
94 
95 #else
96 
99 
100  const AlignableSurface& surface = theParentAlignable->surface();
101 
102  // Get global euler angles.
103  align::EulerAngles localEulerAngles( parameters.sub( 4, 6 ) );
104  const align::RotationType localRotation = align::toMatrix( localEulerAngles );
105  const align::RotationType globalRotation = surface.toGlobal( localRotation );
106  align::EulerAngles globalEulerAngles = align::toAngles( globalRotation );
107 
108  // Get global shifts.
109  align::LocalVector localShifts( parameters[0], parameters[1], parameters[2] );
110  align::GlobalVector globalShifts( surface.toGlobal( localShifts ) );
111 
112  const int nParameter = 6;
113  AlgebraicVector globalParameters( nParameter );
114  globalParameters[0] = globalShifts.x();
115  globalParameters[1] = globalShifts.y();
116  globalParameters[2] = globalShifts.z();
117  globalParameters[3] = globalEulerAngles[0];
118  globalParameters[4] = globalEulerAngles[1];
119  globalParameters[5] = globalEulerAngles[2];
120 
121  AlgebraicVector trueParameters( extractTrueParameters() );
122 
123  for ( int i = 0; i < nParameter; ++i )
124  {
125  string parameterId = selectedParameter( i ) + string( "_" ) + theIdentifier;
126 
127  if ( theFirstUpdate )
128  {
129  KalmanAlignmentDataCollector::fillGraph( string("GlobalDelta") + parameterId, 0, -trueParameters[i]/selectedScaling(i) );
130  KalmanAlignmentDataCollector::fillGraph( string("LocalSigma") + parameterId, 0, sqrt(covariance[i][i])/selectedScaling(i) );
131  }
132 
133  KalmanAlignmentDataCollector::fillGraph( string("GlobalDelta") + parameterId, theNumberOfUpdates/theUpdateFrequency, (globalParameters[i]-trueParameters[i])/selectedScaling(i) );
134  KalmanAlignmentDataCollector::fillGraph( string("LocalSigma") + parameterId, theNumberOfUpdates/theUpdateFrequency, sqrt(covariance[i][i])/selectedScaling(i) );
135  }
136 
137  if ( theFirstUpdate ) theFirstUpdate = false;
138 
139 #endif
140 
141  }
142  }
143 }
144 
145 
147 {
148  if ( theParentAlignable )
149  {
151 
153  const AlgebraicSymMatrix& covariance = param->selectedCovariance();
154  const vector< bool >& selector = param->selector();
155 
156  AlgebraicVector trueParameters( extractTrueParameters() );
157 
158  const int nParameter = 6;
159  int selected = 0;
160 
161  for ( int i = 0; i < nParameter; ++i )
162  {
163  if ( selector[i] )
164  {
165  string parameterId = selectedParameter( i ) + string( "_" ) + theIdentifier;
166 
167  KalmanAlignmentDataCollector::fillGraph( string("Delta") + parameterId, theNumberOfUpdates/theUpdateFrequency, (parameters[selected]-trueParameters[i])/selectedScaling(i) );
168  KalmanAlignmentDataCollector::fillGraph( string("Sigma") + parameterId, theNumberOfUpdates/theUpdateFrequency, sqrt(covariance[selected][selected])/selectedScaling(i) );
169 
170  selected++;
171  }
172  }
173 
174  if ( theFirstUpdate ) theFirstUpdate = false;
175  }
176 }
177 
178 
180 {
181  if ( theParentAlignable )
182  {
183 
184 #ifdef USE_LOCAL_PARAMETERS
185 
188  vector< bool > selector = theParentAlignable->alignmentParameters()->selector();
189 
190  AlgebraicVector trueParameters = extractTrueParameters();
191 
192  const int nParameter = 6;
193  int selected = 0;
194 
195  //histoNamePrefix += theTypeAndLayer;
196 
197  for ( int i = 0; i < nParameter; ++i )
198  {
199  if ( selector[i] )
200  {
201  string startHistoName = histoNamePrefix + theTypeAndLayer + string( "_Start" ) + selectedParameter( i );
202  KalmanAlignmentDataCollector::fillHistogram( startHistoName, -trueParameters[i]/selectedScaling(i) );
203 
204  string deltaHistoName = histoNamePrefix + theTypeAndLayer + string( "_Delta" ) + selectedParameter( i );
205  KalmanAlignmentDataCollector::fillHistogram( deltaHistoName, (parameters[selected]-trueParameters[i])/selectedScaling(i) );
206 
207  string pullsHistoName = histoNamePrefix + theTypeAndLayer + string( "_Pulls" ) + selectedParameter( i );
208  KalmanAlignmentDataCollector::fillHistogram( pullsHistoName, (parameters[selected]-trueParameters[i])/sqrt(covariance[selected][selected]) );
209 
210  startHistoName = histoNamePrefix + string( "_Start" ) + selectedParameter( i );
211  KalmanAlignmentDataCollector::fillHistogram( startHistoName, -trueParameters[i]/selectedScaling(i) );
212 
213  deltaHistoName = histoNamePrefix + string( "_Delta" ) + selectedParameter( i );
214  KalmanAlignmentDataCollector::fillHistogram( deltaHistoName, (parameters[selected]-trueParameters[i])/selectedScaling(i) );
215 
216  pullsHistoName = histoNamePrefix + string( "_Pulls" ) + selectedParameter( i );
217  KalmanAlignmentDataCollector::fillHistogram( pullsHistoName, (parameters[selected]-trueParameters[i])/sqrt(covariance[selected][selected]) );
218 
219  selected++;
220  }
221  }
222 
223 #else
224 
226 
227  const AlignableSurface& surface = theParentAlignable->surface();
228 
229  // Get global euler angles.
230  align::EulerAngles localEulerAngles( parameters.sub( 4, 6 ) );
231  const align::RotationType localRotation = align::toMatrix( localEulerAngles );
232  const align::RotationType globalRotation = surface.toGlobal( localRotation );
233  align::EulerAngles globalEulerAngles = align::toAngles( globalRotation );
234 
235  // Get global shifts.
236  align::LocalVector localShifts( parameters[0], parameters[1], parameters[2] );
237  align::GlobalVector globalShifts( surface.toGlobal( localShifts ) );
238 
239  const int nParameter = 6;
240  AlgebraicVector globalParameters( nParameter );
241  globalParameters[0] = globalShifts.x();
242  globalParameters[1] = globalShifts.y();
243  globalParameters[2] = globalShifts.z();
244  globalParameters[3] = globalEulerAngles[0];
245  globalParameters[4] = globalEulerAngles[1];
246  globalParameters[5] = globalEulerAngles[2];
247 
248  AlgebraicVector trueParameters( extractTrueParameters() );
249 
250  KalmanAlignmentDataCollector::fillGraph( "y_vs_dx", theParentAlignable->globalPosition().y(), trueParameters[0]-globalParameters[0] );
251  KalmanAlignmentDataCollector::fillGraph( "r_vs_dx", theParentAlignable->globalPosition().perp(), trueParameters[0]-globalParameters[0] );
252  KalmanAlignmentDataCollector::fillGraph( "y_vs_dx_true", theParentAlignable->globalPosition().y(), trueParameters[0] );
253 
254  for ( int i = 0; i < nParameter; ++i )
255  {
256  string startHistoName = histoNamePrefix + string( "_Start" ) + selectedParameter( i );
257  KalmanAlignmentDataCollector::fillHistogram( startHistoName, -trueParameters[i]/selectedScaling(i) );
258 
259  string deltaHistoName = histoNamePrefix + string( "_Delta" ) + selectedParameter( i );
260  KalmanAlignmentDataCollector::fillHistogram( deltaHistoName, (globalParameters[i]-trueParameters[i])/selectedScaling(i) );
261 
262  string valueHistoName = histoNamePrefix + string( "_Value" ) + selectedParameter( i );
263  KalmanAlignmentDataCollector::fillHistogram( valueHistoName, globalParameters[i]/selectedScaling(i) );
264 
265  startHistoName = histoNamePrefix + theTypeAndLayer + string( "_Start" ) + selectedParameter( i );
266  KalmanAlignmentDataCollector::fillHistogram( startHistoName, -trueParameters[i]/selectedScaling(i) );
267 
268  deltaHistoName = histoNamePrefix + theTypeAndLayer + string( "_Delta" ) + selectedParameter( i );
269  KalmanAlignmentDataCollector::fillHistogram( deltaHistoName, (globalParameters[i]-trueParameters[i])/selectedScaling(i) );
270 
271  valueHistoName = histoNamePrefix + theTypeAndLayer + string( "_Value" ) + selectedParameter( i );
272  KalmanAlignmentDataCollector::fillHistogram( valueHistoName, globalParameters[i]/selectedScaling(i) );
273  }
274 
275 #endif
276 
277  }
278 }
279 
280 
282 {
284  AlgebraicSymMatrix fixedCovariance = 1e-6*oldParameters->covariance();
285  AlignmentParameters* newParameters = oldParameters->clone( oldParameters->parameters(), fixedCovariance );
286  theParentAlignable->setAlignmentParameters( newParameters );
287 }
288 
289 
291 {
293  AlgebraicSymMatrix fixedCovariance = 1e6*oldParameters->covariance();
294  AlignmentParameters* newParameters = oldParameters->clone( oldParameters->parameters(), fixedCovariance );
295  theParentAlignable->setAlignmentParameters( newParameters );
296 }
297 
298 
300 {
301 
302 #ifdef USE_LOCAL_PARAMETERS
303 
304  // get surface of alignable
305  const AlignableSurface& surface = theParentAlignable->surface();
306 
307  // get global rotation
308  const align::RotationType& globalRotation = theParentAlignable->rotation();
309  // get local rotation
310  align::RotationType localRotation = surface.toLocal( globalRotation );
311  // get euler angles (local frame)
312  align::EulerAngles localEulerAngles = align::toAngles( localRotation );
313 
314  // get global shifts
315  align::GlobalVector globalShifts( globalRotation.multiplyInverse( theParentAlignable->displacement().basicVector() ) );
316  // get local shifts
317  align::LocalVector localShifts = surface.toLocal( globalShifts );
318 
319  AlgebraicVector trueParameters( 6 );
320  trueParameters[0] = -localShifts.x();
321  trueParameters[1] = -localShifts.y();
322  trueParameters[2] = -localShifts.z();
323  trueParameters[3] = -localEulerAngles[0];
324  trueParameters[4] = -localEulerAngles[1];
325  trueParameters[5] = -localEulerAngles[2];
326 
327 #else
328 
329  // get global rotation
330  const align::RotationType& globalRotation = theParentAlignable->rotation();
331  // get euler angles (global frame)
332  align::EulerAngles globalEulerAngles = align::toAngles( globalRotation );
333 
334  // get global shifts
335  align::GlobalVector globalShifts( globalRotation.multiplyInverse( theParentAlignable->displacement().basicVector() ) );
336 
337  AlgebraicVector trueParameters( 6 );
338  trueParameters[0] = -globalShifts.x();
339  trueParameters[1] = -globalShifts.y();
340  trueParameters[2] = -globalShifts.z();
341  trueParameters[3] = -globalEulerAngles[0];
342  trueParameters[4] = -globalEulerAngles[1];
343  trueParameters[5] = -globalEulerAngles[2];
344 
345 #endif
346 
347  return trueParameters;
348 }
349 
350 
351 const string KalmanAlignmentUserVariables::selectedParameter( const int& selected ) const
352 {
353  switch ( selected )
354  {
355  case 0:
356  return string( "X" );
357  break;
358  case 1:
359  return string( "Y" );
360  break;
361  case 2:
362  return string( "Z" );
363  break;
364  case 3:
365  return string( "Alpha" );
366  break;
367  case 4:
368  return string( "Beta" );
369  break;
370  case 5:
371  return string( "Gamma" );
372  break;
373  default:
374  throw cms::Exception( "OutOfRange" ) << "[KalmanAlignmentUserVariables::selectedParameter] "
375  << "Index out of range (selector = " << selected << ")";
376  }
377 }
378 
379 
380 const float KalmanAlignmentUserVariables::selectedScaling( const int& selected ) const
381 {
382  const float micron = 1e-4;
383  const float millirad = 1e-3;
384  //const float murad = 1e-6;
385 
386  switch ( selected )
387  {
388  case 0:
389  case 1:
390  case 2:
391  return micron;
392  break;
393  case 3:
394  case 4:
395  case 5:
396  return millirad;
397  //return murad;
398  break;
399  default:
400  throw cms::Exception( "LogicError" ) << "@SUB=KalmanAlignmentUserVariables::selectedScaling"
401  << "Index out of range (selector = " << selected << ")\n";
402  }
403 }
404 
405 
406 const string KalmanAlignmentUserVariables::toString( const int& i ) const
407 {
408  char temp[10];
409  sprintf( temp, "%u", i );
410 
411  return string( temp );
412 }
const std::string selectedParameter(const int &selected) const
align::ID id() const
Return the ID of Alignable, i.e. DetId of &#39;first&#39; component GeomDet(Unit).
Definition: Alignable.h:180
int i
Definition: DBlmapReader.cc:9
dictionary parameters
Definition: Parameters.py:2
void histogramParameters(std::string histoNamePrefix)
Histogram current estimate of the alignment parameters wrt. the true values.
static const TrackerAlignableId * theAlignableId
T perp() const
Definition: PV3DBase.h:66
static void fillHistogram(std::string histo_name, float data)
list parent
Definition: dbtoconf.py:74
AlgebraicVector selectedParameters(void) const
Get selected parameters.
const GlobalVector & displacement() const
Return change of the global position since the creation of the object.
Definition: Alignable.h:135
T y() const
Definition: PV3DBase.h:57
const std::vector< bool > & selector(void) const
Get alignment parameter selector vector.
const std::string & typeToName(align::StructureType type) const
Convert type to name.
const std::string toString(const int &i) const
void update(bool enforceUpdate=false)
Call this function in case the associated Alignable was updated by the alignment algorithm.
AlignmentParameters * alignmentParameters() const
Get the AlignmentParameters.
Definition: Alignable.h:57
const AlgebraicVector & parameters(void) const
Get alignment parameters.
const RotationType & rotation() const
Return change of orientation since the creation of the object.
Definition: Alignable.h:138
align::RotationType toLocal(const align::RotationType &) const
Return in local frame a rotation given in global frame.
void setAlignmentParameters(AlignmentParameters *dap)
Set the AlignmentParameters.
Definition: Alignable.cc:79
T sqrt(T t)
Definition: SSEVec.h:28
T z() const
Definition: PV3DBase.h:58
virtual StructureType alignableObjectId() const =0
Return the alignable type identifier.
Allows conversion between type and name, and vice-versa.
EulerAngles toAngles(const RotationType &)
Convert rotation matrix to angles about x-, y-, z-axes (frame rotation).
Definition: Utilities.cc:7
const AlignableSurface & surface() const
Return the Surface (global position and orientation) of the object.
Definition: Alignable.h:126
AlgebraicSymMatrix selectedCovariance(void) const
Get covariance matrix of selected parameters.
static const AlignableObjectId * theObjectId
CLHEP::HepVector AlgebraicVector
AlgebraicVector EulerAngles
Definition: Definitions.h:36
const AlgebraicVector extractTrueParameters(void) const
const float selectedScaling(const int &selected) const
virtual AlignmentParameters * clone(const AlgebraicVector &par, const AlgebraicSymMatrix &cov) const =0
Enforce clone methods in derived classes.
align::GlobalPoints toGlobal(const align::LocalPoints &) const
Return in global coord given a set of local points.
RotationType toMatrix(const EulerAngles &)
Convert rotation angles about x-, y-, z-axes to matrix.
Definition: Utilities.cc:40
CLHEP::HepSymMatrix AlgebraicSymMatrix
std::pair< int, int > typeAndLayerFromDetId(const DetId &detId) const
const PositionType & globalPosition() const
Return the global position of the object.
Definition: Alignable.h:129
Basic3DVector< T > multiplyInverse(const Basic3DVector< T > &v) const
static void fillGraph(std::string graph_name, float x_data, float y_data)
const AlgebraicSymMatrix & covariance(void) const
Get parameter covariance matrix.
T x() const
Definition: PV3DBase.h:56
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:54
const DetId & geomDetId() const
Definition: Alignable.h:177