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