34 const std::string &
name,
37 , m_alignModuleTool(
"Trk::AlignModuleTool/AlignModuleTool")
39 , m_measTypeIdHelper(nullptr)
41 , m_residualTypeSet(false)
42 , m_storeDerivatives(false)
44 declareInterface<IDerivCalcTool>(
this);
71 return StatusCode::FAILURE;
77 return StatusCode::FAILURE;
81 return StatusCode::SUCCESS;
88 return StatusCode::SUCCESS;
100 std::vector<bool> hitModules(
nModules,
false);
104 std::vector<AlignModule *> alignModules;
106 for (; atsosItr != alignTrack->
lastAtsos(); ++atsosItr) {
113 if (!(*atsosItr)->isValid() || !
module)
117 if(!hitModules[
module->identifyHash()]) {
118 hitModules[
module->identifyHash()] =
true;
119 alignModules.push_back(
module);
124 std::vector<AlignModuleDerivatives> * derivatives =
new std::vector<AlignModuleDerivatives>;
126 for ( ; moduleIt!=alignModules.end(); ++moduleIt) {
127 std::vector<Amg::VectorX> deriv_vec =
getDerivatives(alignTrack,*moduleIt);
128 derivatives->push_back(make_pair(*moduleIt,deriv_vec));
143 static std::once_flag
flag;
144 std::call_once(
flag, [&]() {
146 *
m_logStream<<
"*************************************************************"<<std::endl;
147 *
m_logStream<<
"*************************************************************"<<std::endl;
151 *
m_logStream<<
"*** Running full LOCAL Chi2 method *****"<<std::endl;
153 *
m_logStream<<
"*** Running full GLOBAL Chi2 method *****"<<std::endl;
155 *
m_logStream<<
"*** using analytical derivatives *****"<<std::endl;
158 std::string resType =
"HitOnly ";
160 resType =
"Unbiased";
161 *
m_logStream<<
"*** with residual type: "<<resType<<
" *****"<<std::endl;
165 *
m_logStream<<
"*************************************************************"<<std::endl;
172 ATH_MSG_WARNING(
"Inverse local error matrix is invalid, skipping the track");
177 for (
int i=0;
i<Vinv->rows();
i++)
msg()<<(*Vinv)(
i,
i)<<
" ";
185 ATH_MSG_DEBUG(
"setting Residual covariance matrix for local method");
196 ATH_MSG_DEBUG(
"Ignoring measured errors for Pixel clusters, using intrinsic errors");
198 ATH_MSG_DEBUG(
"Ignoring measured errors for SCT clusters, using intrinsic errors");
200 ATH_MSG_DEBUG(
"Ignoring measured errors for TRT clusters, using intrinsic errors");
205 for ( ; itAtsos != itAtsos_end; ++itAtsos) {
223 ATH_MSG_DEBUG(
"setting local weight matrix W ( "<<
W->rows()<<
" x "<<
W->cols()<<
" ) (diagonal only):");
225 for (
int i=0;
i<
W->rows();
i++)
msg()<<(*W)(
i,
i)<<
" ";
242 ATH_MSG_DEBUG(
"V ( "<<V->rows()<<
" x "<<V->cols()<<
" ) (diagonal only):");
244 for (
int i=0;
i<V->rows();
i++)
msg()<<(*V)(
i,
i)<<
" ";
267 *
W = ((*Vinv) * R * (*Vinv));
270 ATH_MSG_DEBUG(
"Weight matrix is invalid, skipping the track");
280 ATH_MSG_DEBUG(
"setting weight for 1st derivatives (diagonal only): ");
282 for (
int i=0;
i<W1st->rows();
i++)
msg()<<(*W1st)(
i,
i)<<
" ";
303 if( H0==
nullptr || C==
nullptr) {
304 ATH_MSG_ERROR(
"no derivative matrix or cov matrix stored on AlignTrack!"
305 <<
"This should have been done in AlignTrackPreProcessor!"
316 int Csize(C->rows());
331 for(
int ii=0; ii<Csize; ++ii ) {
332 for(
int jj=ii; jj<Csize; ++jj ) {
333 CA->elemr(ii,jj) = (*C)(ii,jj);
340 ATH_MSG_ERROR(
"First inversion of matrix CA failed with LAPACK status flag " << ierr);
345 if( !(alignTrack->
refitD0()) ) {
346 for(
int ii=0; ii<(Csize); ++ii)
CA->elemr(0,ii)=0.0;
349 if( !(alignTrack->
refitZ0()) ) {
350 for(
int ii=0; ii<(Csize); ++ii)
CA->elemr(1,ii)=0.0;
354 for(
int ii=0; ii<(Csize); ++ii)
CA->elemr(2,ii)=0.0;
358 for(
int ii=0; ii<(Csize); ++ii)
CA->elemr(3,ii)=0.0;
362 for(
int ii=0; ii<(Csize); ++ii)
CA->elemr(4,ii)=0.0;
370 ATH_MSG_ERROR(
"Second inversion of matrix CA failed with LAPACK status flag " << ierr);
375 for(
int ii=0; ii<Csize; ++ii ) {
376 for(
int jj=ii; jj<Csize; ++jj ) {
377 CC(ii,jj) =
CA->elemc(ii,jj);
382 if( !(alignTrack->
refitD0()) )
for(
int ii=0; ii<(Csize); ++ii){
CC(0,ii)=0.0;
CC(ii,0)=0.0; };
383 if( !(alignTrack->
refitZ0()) )
for(
int ii=0; ii<(Csize); ++ii){
CC(1,ii)=0.0;
CC(ii,1)=0.0; };
384 if( !(alignTrack->
refitPhi()) )
for(
int ii=0; ii<(Csize); ++ii){
CC(2,ii)=0.0;
CC(ii,2)=0.0; };
385 if( !(alignTrack->
refitTheta()) )
for(
int ii=0; ii<(Csize); ++ii){
CC(3,ii)=0.0;
CC(ii,3)=0.0; };
386 if( !(alignTrack->
refitQovP()) )
for(
int ii=0; ii<(Csize); ++ii){
CC(4,ii)=0.0;
CC(ii,4)=0.0; };
397 int nMeas = H0->rows();
403 HCH =
CC.similarity( *H0 );
405 ATH_MSG_DEBUG(
"HCH ( "<<HCH.rows()<<
" x "<<HCH.cols()<<
" )");
412 std::vector<int> matrixIndices(nMeas);
437 matrixIndices[imeas]=-1;
444 for (
const AlignTSOS* atsos : *alignTSOSCollection) {
446 if (!atsos->isValid())
450 ATH_MSG_ERROR(
"can't use scatterers on AlignTrack yet for analytical derivatives!");
456 if (atsos_rio->
identify()==tsosId) {
457 matrixIndices[imeas]=iameas;
461 if (atsos->nResDim()>1)
465 matrixIndices[imeas]=iameas;
471 else if(atsos->nResDim()>1)
481 matrixIndices[imeas]=-1;
482 ATH_MSG_DEBUG(
"matrixIndices["<<imeas<<
"]="<<matrixIndices[imeas]);
492 for (
int k=0;
k<nMeas;
k++) {
494 int iameas=matrixIndices[
k];
498 for (
int l=0;
l<nMeas;
l++) {
499 int jameas=matrixIndices[
l];
503 Q(iameas,jameas) = HCH(
k,
l);
508 ATH_MSG_DEBUG(
"before check Q ( "<<Q.rows()<<
" x "<<Q.cols()<<
" )");
512 ATH_MSG_DEBUG(
"Matrix Q = HCH is invalid, skipping the track");
525 for (; itAtsos != alignTrack->
lastAtsos(); ++itAtsos) {
527 std::vector<Residual>::const_iterator itRes=(**itAtsos).firstResidual();
528 for (; itRes!=(**itAtsos).lastResidual(); ++itRes,
index++) {
546 const double epsilon=1
e-10;
547 for(
int irow=0; irow<R.rows(); ++irow) {
549 Risvalid = Risvalid && R(irow,irow)>0;
551 if( !(R(irow,irow)>0) )
552 msg(
MSG::DEBUG) <<
"matrix invalid: (" << irow <<
"," << irow<<
") = " << R(irow,irow) <<
endmsg;
559 double Rcorr = R(irow,
icol)/sqrt(R(irow,irow)*R(
icol,
icol));
560 if( Rcorr+epsilon<-1 || Rcorr-epsilon>1 )
564 ATH_MSG_DEBUG(
"matrix corr invalid for (" << irow <<
"," <<
icol <<
") Rcorr = " << Rcorr);
585 globalFrameToAlignFrame(0,1)<<
" "<<
586 globalFrameToAlignFrame(0,2));
588 globalFrameToAlignFrame(1,1)<<
" "<<
589 globalFrameToAlignFrame(1,2));
591 globalFrameToAlignFrame(2,1)<<
" "<<
592 globalFrameToAlignFrame(2,2));
597 globalToAlignFrameRotation(0,1)<<
" "<<
598 globalToAlignFrameRotation(0,2));
600 globalToAlignFrameRotation(1,1)<<
" "<<
601 globalToAlignFrameRotation(1,2));
603 globalToAlignFrameRotation(2,1)<<
" "<<
604 globalToAlignFrameRotation(2,2));
607 const int nAlignPar = alignPars->
size();
611 for(
int i(0);
i<nAlignPar+3; ++
i) derivatives[
i].setZero();
615 for (; iatsos != alignTrack->
lastAtsos(); ++iatsos) {
622 int nResDim = alignTSOS->
nResDim();
629 std::vector<Amg::VectorX> * atsosDerivs =
nullptr;
630 std::vector<Amg::VectorX> * atsosDerVtx =
nullptr;
632 atsosDerivs =
new std::vector<Amg::VectorX>(nResDim,
Amg::VectorX(nAlignPar));
633 atsosDerVtx =
new std::vector<Amg::VectorX>(nResDim,
Amg::VectorX(3));
634 ATH_MSG_DEBUG(
"nResDim = "<<nResDim<<
" vector size is "<<atsosDerivs->size());
635 ATH_MSG_DEBUG(
"nAlignPar = "<<nAlignPar<<
" CLHEP::HepVector size is "<<atsosDerivs->at(0).rows());
642 if (!mtp || !(mtp->covariance()) )
continue;
648 localToGlobalRotation(0,1) <<
" " <<
649 localToGlobalRotation(0,2));
651 localToGlobalRotation(1,1) <<
" " <<
652 localToGlobalRotation(1,2));
654 localToGlobalRotation(2,1) <<
" " <<
655 localToGlobalRotation(2,2));
657 if(
double alphastrip=alignTSOS->
alphaStrip()) {
658 ATH_MSG_DEBUG(
"applying fanout rotation : " << alphastrip );
662 localToGlobalRotation(0,1) <<
" " <<
663 localToGlobalRotation(0,2));
665 localToGlobalRotation(1,1) <<
" " <<
666 localToGlobalRotation(1,2));
668 localToGlobalRotation(2,1) <<
" " <<
669 localToGlobalRotation(2,2));
706 ATH_MSG_DEBUG(
"trackdir " << trackdir[0] <<
" " << trackdir[1] <<
" " << trackdir[2]);
710 double cotphi_x = trackdir.x() / trackdir.z();
715 cotphi_x = trackdir.y() / trackdir.z();
718 double Rxx = R(0,0) - cotphi_x * R(0,2);
719 double Ryx = R(1,0) - cotphi_x * R(1,2);
720 double Rzx = R(2,0) - cotphi_x * R(2,2);
721 ATH_MSG_DEBUG(
"Rxx/Ryx/Rzx: " << Rxx <<
"/" << Ryx <<
"/" << Rzx);
757 const double z0z0 = 366.5*366.5;
764 Amg::Vector3D RxGlob=-1.0 * (globalToAlignFrameRotation.inverse() * RxLoc);
766 for (
int ipar=0; ipar<nAlignPar; ipar++) {
767 const AlignPar * alignPar = (*alignPars)[ipar];
771 derivatives[ipar][imeas] = projR[paramType];
773 (*atsosDerivs)[0][ipar] = projR[paramType];
776 for (
int ipar=0; ipar<3; ipar++) {
777 derivatives[nAlignPar+ipar][imeas] = RxGlob[ipar];
781 for (
int i=0;
i<nAlignPar+3;
i++)
782 ATH_MSG_DEBUG(
"derivatives["<<
i<<
"]["<<imeas<<
"]="<<derivatives[
i][imeas]);
789 double cotphi_y = trackdir.y() / trackdir.z() ;
790 double Rxy = R(0,1) - cotphi_y * R(0,2) ;
791 double Ryy = R(1,1) - cotphi_y * R(1,2) ;
792 double Rzy = R(2,1) - cotphi_y * R(2,2) ;
793 ATH_MSG_DEBUG(
"Rxy/Ryy/Rzy: " << Rxy <<
"/" << Ryy <<
"/" << Rzy);
810 Amg::Vector3D RyGlob=-1.0 * (globalToAlignFrameRotation.inverse() * RyLoc);
812 for (
int ipar=0; ipar<nAlignPar; ipar++) {
813 const AlignPar * alignPar = (*alignPars)[ipar];
815 ATH_MSG_DEBUG(
"2nd dim, ipar="<<ipar<<
", paramType="<<paramType);
817 derivatives[ipar][imeas] = projR[paramType];
819 (*atsosDerivs)[1][ipar] = projR[paramType];
823 for (
int ipar=0; ipar<3; ipar++) {
824 derivatives[nAlignPar+ipar][imeas] = RyGlob[ipar];
828 for (
int i=0;
i<nAlignPar+3;
i++)
829 ATH_MSG_DEBUG(
"2nd dim: derivatives["<<
i<<
"]["<<imeas<<
"]="<<derivatives[
i][imeas]);