CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/Alignment/CommonAlignmentParametrization/src/BeamSpotAlignmentParameters.cc

Go to the documentation of this file.
00001 
00008 #include "FWCore/Utilities/interface/Exception.h"
00009 
00010 #include "Alignment/CommonAlignment/interface/Utilities.h"
00011 #include "Alignment/CommonAlignment/interface/Alignable.h"
00012 #include "Alignment/CommonAlignment/interface/AlignableDetOrUnitPtr.h"
00013 #include "Alignment/CommonAlignmentParametrization/interface/FrameToFrameDerivative.h"
00014 #include "Alignment/CommonAlignmentParametrization/interface/BeamSpotAlignmentDerivatives.h"
00015 #include "Alignment/CommonAlignmentParametrization/interface/AlignmentParametersFactory.h"
00016 #include "CondFormats/Alignment/interface/Definitions.h"
00017 
00018 // This class's header 
00019 #include "Alignment/CommonAlignmentParametrization/interface/BeamSpotAlignmentParameters.h"
00020 
00021 //__________________________________________________________________________________________________
00022 BeamSpotAlignmentParameters::BeamSpotAlignmentParameters(Alignable* ali, bool calcMis) :
00023   AlignmentParameters(ali, displacementFromAlignable(calcMis ? ali : 0),
00024                       AlgebraicSymMatrix(N_PARAM, 0))
00025 {
00026 
00027 }
00028 
00029 //__________________________________________________________________________________________________
00030 BeamSpotAlignmentParameters::BeamSpotAlignmentParameters(Alignable* alignable, 
00031                                                          const AlgebraicVector& parameters, 
00032                                                          const AlgebraicSymMatrix& covMatrix) :
00033   AlignmentParameters( alignable, parameters, covMatrix )
00034 {  
00035   if (parameters.num_row() != N_PARAM) {
00036     throw cms::Exception("BadParameters") << "in BeamSpotAlignmentParameters(): "
00037                                           << parameters.num_row() << " instead of " << N_PARAM 
00038                                           << " parameters.";
00039   }
00040 }
00041 
00042 //__________________________________________________________________________________________________
00043 BeamSpotAlignmentParameters::BeamSpotAlignmentParameters(Alignable* alignable, 
00044                                                          const AlgebraicVector& parameters, 
00045                                                          const AlgebraicSymMatrix& covMatrix,
00046                                                          const std::vector<bool>& selection ) :
00047   AlignmentParameters( alignable, parameters, covMatrix, selection )
00048 {  
00049   if (parameters.num_row() != N_PARAM) {
00050     throw cms::Exception("BadParameters") << "in BeamSpotAlignmentParameters(): "
00051                                           << parameters.num_row() << " instead of " << N_PARAM 
00052                                           << " parameters.";
00053   }
00054 }
00055 
00056 //__________________________________________________________________________________________________
00057 BeamSpotAlignmentParameters::~BeamSpotAlignmentParameters()
00058 {
00059 
00060 } 
00061 
00062 //__________________________________________________________________________________________________
00063 BeamSpotAlignmentParameters* 
00064 BeamSpotAlignmentParameters::clone( const AlgebraicVector& parameters, 
00065                                     const AlgebraicSymMatrix& covMatrix ) const 
00066 {
00067   BeamSpotAlignmentParameters* rbap = 
00068     new BeamSpotAlignmentParameters( alignable(), parameters, covMatrix, selector());
00069 
00070   if (userVariables()) rbap->setUserVariables(userVariables()->clone());
00071   rbap->setValid(isValid());
00072 
00073   return rbap;
00074 }
00075 
00076 //__________________________________________________________________________________________________
00077 BeamSpotAlignmentParameters* 
00078 BeamSpotAlignmentParameters::cloneFromSelected( const AlgebraicVector& parameters,
00079                                                 const AlgebraicSymMatrix& covMatrix ) const
00080 {
00081   BeamSpotAlignmentParameters* rbap = 
00082     new BeamSpotAlignmentParameters(alignable(), expandVector( parameters, selector()),
00083                                     expandSymMatrix(covMatrix, selector()), selector());
00084 
00085   if ( userVariables() ) rbap->setUserVariables(userVariables()->clone());
00086   rbap->setValid(isValid());
00087   
00088   return rbap;
00089 }
00090 
00091 //__________________________________________________________________________________________________
00092 AlgebraicMatrix 
00093 BeamSpotAlignmentParameters::derivatives( const TrajectoryStateOnSurface &tsos,
00094                                           const AlignableDetOrUnitPtr &alidet ) const
00095 {
00096   const Alignable *ali = this->alignable(); // Alignable of these parameters
00097 
00098   if (ali == alidet) { // same alignable => same frame
00099     return BeamSpotAlignmentDerivatives()(tsos);
00100   } else { // different alignable => transform into correct frame
00101     const AlgebraicMatrix deriv = BeamSpotAlignmentDerivatives()(tsos);
00102     FrameToFrameDerivative ftfd;
00103     return ftfd.frameToFrameDerivative(alidet, ali) * deriv;
00104   }
00105 }
00106 
00107 //__________________________________________________________________________________________________
00108 AlgebraicMatrix 
00109 BeamSpotAlignmentParameters::selectedDerivatives( const TrajectoryStateOnSurface& tsos, 
00110                                                   const AlignableDetOrUnitPtr &alignableDet ) const
00111 {
00112   const AlgebraicMatrix dev = this->derivatives( tsos, alignableDet );
00113 
00114   int ncols  = dev.num_col();
00115   int nrows  = dev.num_row();
00116   int nsel   = numSelected();
00117 
00118   AlgebraicMatrix seldev( nsel, ncols );
00119 
00120   int ir2=0;
00121   for ( int irow=0; irow<nrows; ++irow ) {
00122     if (selector()[irow]) {
00123       for ( int icol=0; icol<ncols; ++icol ) seldev[ir2][icol] = dev[irow][icol];
00124       ++ir2;
00125     }
00126   }
00127 
00128   return seldev;
00129 }
00130 
00131 //__________________________________________________________________________________________________
00132 AlgebraicVector BeamSpotAlignmentParameters::translation(void) const
00133 { 
00134   AlgebraicVector shift(3);
00135   for ( int i=0;i<2;++i ) shift[i] = theData->parameters()[i];
00136   shift[2] = 0.0;
00137 
00138   return shift;
00139 }
00140 
00141 //__________________________________________________________________________________________________
00142 AlgebraicVector BeamSpotAlignmentParameters::rotation(void) const
00143 {
00144   AlgebraicVector rot(3);
00145 
00146   double dxdz = theData->parameters()[2];  
00147   double dydz = theData->parameters()[3];  
00148   double angleY = std::atan(dxdz);
00149   double angleX = -std::atan(dydz);
00150 
00151   align::RotationType rotY( std::cos(angleY),  0., -std::sin(angleY), 
00152                             0.,                1.,  0.,
00153                             std::sin(angleY),  0.,  std::cos(angleY) );
00154 
00155   align::RotationType rotX( 1.,  0.,                0.,
00156                             0.,  std::cos(angleX),  std::sin(angleX),
00157                             0., -std::sin(angleX),  std::cos(angleX) );
00158 
00159   align::EulerAngles angles = align::toAngles(rotY * rotX);
00160 
00161   rot[0] = angles(1);
00162   rot[1] = angles(2);
00163   rot[2] = angles(3);
00164 
00165   return rot;
00166 }
00167 
00168 //__________________________________________________________________________________________________
00169 void BeamSpotAlignmentParameters::apply()
00170 {
00171   Alignable *alignable = this->alignable();
00172   if (!alignable) {
00173     throw cms::Exception("BadParameters") 
00174       << "BeamSpotAlignmentParameters::apply: parameters without alignable";
00175   }
00176   
00177   // Translation in local frame
00178   AlgebraicVector shift = this->translation(); // fixme: should be LocalVector
00179   
00180   // Translation local->global
00181   align::GlobalVector gv(shift[0], shift[1], shift[2]);
00182   alignable->move( gv );
00183 
00184   // Rotation in local frame
00185   align::EulerAngles angles = this->rotation();
00186   // original code:
00187   //  alignable->rotateInLocalFrame( align::toMatrix(angles) );
00188   // correct for rounding errors:
00189   align::RotationType rot = alignable->surface().toGlobal( align::toMatrix(angles) );
00190   align::rectify(rot);
00191   alignable->rotateInGlobalFrame(rot);
00192 }
00193 
00194 //__________________________________________________________________________________________________
00195 int BeamSpotAlignmentParameters::type() const
00196 {
00197   return AlignmentParametersFactory::kBeamSpot;
00198 }
00199 
00200 //__________________________________________________________________________________________________
00201 AlgebraicVector BeamSpotAlignmentParameters::globalParameters(void) const
00202 {
00203   AlgebraicVector m_GlobalParameters(N_PARAM, 0);
00204 
00205   const AlgebraicVector shift = translation(); // fixme: should return LocalVector
00206 
00207   const align::GlobalVector dg(shift[0], shift[1], 0);
00208   
00209   m_GlobalParameters[0] = dg.x();
00210   m_GlobalParameters[1] = dg.y();
00211   
00212   align::LocalVector lv(0.0, 0.0, 1.0);
00213   align::GlobalVector gv = theAlignable->surface().toGlobal(lv);
00214   
00215   double dxdz = gv.x()/gv.z();
00216   double dydz = gv.x()/gv.z();
00217   
00218   m_GlobalParameters[2] = dxdz;
00219   m_GlobalParameters[3] = dydz;
00220   
00221   return m_GlobalParameters;
00222 }
00223 
00224 //__________________________________________________________________________________________________
00225 void BeamSpotAlignmentParameters::print(void) const
00226 {
00227 
00228   std::cout << "Contents of BeamSpotAlignmentParameters:"
00229             << "\nParameters: " << theData->parameters()
00230             << "\nCovariance: " << theData->covariance() << std::endl;
00231 }
00232 
00233 //__________________________________________________________________________________________________
00234 AlgebraicVector BeamSpotAlignmentParameters::displacementFromAlignable(const Alignable* ali)
00235 {
00236   AlgebraicVector displacement(N_PARAM);
00237 
00238   if (ali) {
00239     const align::RotationType& dR = ali->rotation();
00240     
00241     const align::LocalVector shifts( ali->globalRotation() * 
00242                                      ( dR.transposed() * ali->displacement().basicVector() ) );
00243     
00244     align::GlobalVector gv(0.0, 0.0, 1.0);
00245     align::LocalVector lv(dR.transposed() * gv.basicVector());
00246     
00247     displacement[0] = shifts.x();
00248     displacement[1] = shifts.y();
00249     displacement[2] = lv.x()/lv.z();
00250     displacement[3] = lv.y()/lv.z();
00251   }
00252 
00253   return displacement;
00254 }