11 #include "GaudiKernel/StatusCode.h"
12 #include "GaudiKernel/MsgStream.h"
14 #include "GaudiKernel/AlgTool.h"
28 #include "CLHEP/Matrix/Matrix.h"
29 #include "CLHEP/Matrix/SymMatrix.h"
30 #include "CLHEP/Matrix/Vector.h"
33 #include <TMatrixDSym.h>
34 #include <TMatrixDSparse.h>
41 #include <TDecompBK.h>
49 #include <boost/io/ios_state.hpp>
53 #include <sys/resource.h>
62 , m_alignModuleTool(
"Trk::AlignModuleTool/AlignModuleTool")
63 , m_bigmatrix(nullptr)
64 , m_bigvector(nullptr)
104 declareInterface<IMatrixTool>(
this);
133 std::vector<std::string> defaultMatInput,defaultVecInput,defaultTFile;
134 defaultMatInput.emplace_back(
"matrix.bin");
135 defaultVecInput.emplace_back(
"vector.bin");
136 defaultTFile.emplace_back(
"AlignmentTFile.root");
164 std::vector<std::string> defaultHitmapInput;
165 defaultMatInput.emplace_back(
"hitmap.bin");
211 return StatusCode::FAILURE;
214 ATH_MSG_INFO(
"Retrieving data from the following files: ");
219 return StatusCode::SUCCESS;
227 return StatusCode::SUCCESS;
234 ATH_MSG_INFO(
"allocating matrix and vector with nDoF = "<<nDoF);
256 return StatusCode::SUCCESS;
269 *
m_logStream<<
"*************************************************************"<<std::endl;
270 *
m_logStream<<
"************** solving using Global method ****************"<<std::endl;
271 *
m_logStream<<
"************** using ROOT ****************"<<std::endl;
272 *
m_logStream<<
"*************************************************************"<<std::endl;
276 clock_t starttime = clock();
286 for (
int i=0;
i<nDoF;
i++)
287 for (
int j=0;j<nDoF;j++)
291 for (
int i=0;
i<nDoF;
i++)
297 double * firstderiv =
new double[
m_aNDoF];
298 for (
int iActive=0;iActive<
m_aNDoF;iActive++) {
300 firstderiv[iActive] = (*m_bigvector)[
i];
301 for (
int jActive=0;jActive<
m_aNDoF;jActive++) {
303 secderiv[iActive*
m_aNDoF+jActive] = (*m_bigmatrix)[
i][j];
323 TMatrixDSym ainv(
c.Invert(
status));
325 TVectorD
r(
b.GetNrows());
330 clock_t stoptime = clock();
331 double totaltime = (stoptime-starttime)/
double(CLOCKS_PER_SEC);
332 ATH_MSG_INFO(
"Time spent in solveROOT: "<<totaltime<<
" s");
335 msg(MSG::ERROR)<<
"ROOT inversion failed"<<
endmsg;
345 for (
int iAdof=0;iAdof<
m_aNDoF;iAdof++) {
348 AlignPar * alignPar=(*alignParList)[idof];
351 double param = -
r[iAdof] *
sigma;
352 double err = std::sqrt(2.*std::fabs(ainv(iAdof,iAdof))) *
sigma;
358 ATH_MSG_DEBUG(
"set param to "<<param<<
" for alignPar "<<alignPar);
369 *
m_logStream<<
"norm of first derivative : "<<sqrt(
b.Norm2Sqr())<<std::endl;
372 double dist = sqrt( (
b - (
a *
r) ).Norm2Sqr() );
373 *
m_logStream<<
"distance to solution : "<<dist<<std::endl;
376 double chi2 =
a.Similarity(
r) * .5;
380 *
m_logStream<<
"time spent in solve : "<<totaltime<<
" s"<<std::endl;
385 delete [] firstderiv;
395 *
m_logStream<<
"*************************************************************"<<std::endl;
396 *
m_logStream<<
"************** solving using Global method ****************"<<std::endl;
397 *
m_logStream<<
"************** using CLHEP ****************"<<std::endl;
398 *
m_logStream<<
"*************************************************************"<<std::endl;
402 clock_t starttime = clock();
414 for (
int i=0;
i<nDoF;
i++)
415 for (
int j=0;j<nDoF;j++)
419 for (
int i=0;
i<nDoF;
i++)
424 CLHEP::HepSymMatrix * d2Chi2 =
new CLHEP::HepSymMatrix(
m_aNDoF,0);
425 CLHEP::HepVector * dChi2 =
new CLHEP::HepVector(
m_aNDoF,0);
426 for (
int iActive=0;iActive<
m_aNDoF;iActive++) {
428 (*dChi2)[iActive] = (*m_bigvector)[
i];
429 for (
int jActive=0;jActive<
m_aNDoF;jActive++) {
431 (*d2Chi2)[iActive][jActive] = (*m_bigmatrix)[
i][j];
439 CLHEP::HepVector delta(
m_aNDoF,0);
440 CLHEP::HepVector deltafull(
m_aNDoF,0);
449 *
m_logStream<<
"Running matrix inversion"<<std::endl;
454 msg(MSG::ERROR)<<
"CLHEP inversion status flag = "<<ierr<<
endmsg;
458 *
m_logStream<<
"CLHEP inversion status flag = "<<ierr<<std::endl;
461 delta =
cov * (*dChi2);
471 CLHEP::HepSymMatrix cov2 = *d2Chi2 * .5;
476 msg(MSG::WARNING)<<
"Second CLHEP inversion status flag = "<<ierr2<<
endmsg;
478 CLHEP::HepVector delta2 = cov2 * (*dChi2) * .5;
479 for (
int i=0;
i<delta.num_row(); ++
i)
480 if ( fabs((delta[
i] - delta2[
i])/delta[
i]) > 1
e-5 ) {
481 msg(MSG::WARNING)<<
"Something's wrong with the matrix inversion: delta["<<
i<<
"] = "<<delta[
i]<<
" delta2["<<
i<<
"] = "<<delta2[
i]<<
endmsg;
487 *
m_logStream<<
"CLHEP inversion status flag for halfed matrix = "<<ierr2<<std::endl;
488 *
m_logStream<<
"Matrix inversion check failed"<<std::endl;
498 *
m_logStream<<
"Running diagonalization"<<std::endl;
500 CLHEP::HepSymMatrix D = *d2Chi2;
501 CLHEP::HepMatrix U = CLHEP::diagonalize( &D );
511 if(D[j][j] < D[
i][
i]) {
526 CLHEP::HepVector eigenvector(
m_aNDoF);
529 *
m_logStream<<
"/------ The Eigenvalue Spectrum -------"<<std::endl;
532 for(
int imode=0; imode<
m_aNDoF; ++imode) {
535 for(
int irow=0; irow<
m_aNDoF; ++irow)
536 eigenvector[irow] = U[irow][imode];
540 double eigenvalue = D[imode][imode];
543 double evdotb =
dot(*dChi2,eigenvector);
544 CLHEP::HepVector thisdelta = evdotb/eigenvalue * eigenvector;
545 deltafull += thisdelta;
550 *
m_logStream<<
"| skipping eigenvalue "<<eigenvalue<<std::endl;
555 *
m_logStream<<
"| skipping eigenvalue "<<eigenvalue<<std::endl;
564 for(
int irow=0; irow<
m_aNDoF; ++irow)
566 cov[irow][
icol] += eigenvector[irow] * eigenvector[
icol] / eigenvalue;
576 *
m_logStream<<
"\\----- End of Eigenvalue Spectrum -----"<<std::endl;
583 clock_t stoptime = clock();
584 double totaltime = (stoptime-starttime)/
double(CLOCKS_PER_SEC);
585 ATH_MSG_INFO(
"Time spent in solveCLHEP: "<<totaltime<<
" s");
590 for (
int iAdof=0;iAdof<
m_aNDoF;iAdof++) {
593 AlignPar * alignPar=(*alignParList)[idof];
596 double param = -delta[iAdof] *
sigma;
597 double err = std::sqrt(2.*std::fabs(
cov[iAdof][iAdof])) *
sigma;
603 ATH_MSG_DEBUG(
"set param to "<<param<<
" for alignPar "<<alignPar);
611 *
m_logStream<<
"norm of first derivative : "<<dChi2->norm()<<std::endl;
614 double dist = ( - (*d2Chi2) * deltafull + (*dChi2) ).
norm();
615 *
m_logStream<<
"distance to solution : "<<dist<<std::endl;
618 double chi2 = d2Chi2->similarity(delta) * .5;
622 *
m_logStream<<
"time spent in solve : "<<totaltime<<
" s"<<std::endl;
637 *
m_logStream<<
"*************************************************************"<<std::endl;
638 *
m_logStream<<
"************** solving using Local method *****************"<<std::endl;
639 *
m_logStream<<
"*************************************************************"<<std::endl;
643 double totalChi2(0.);
647 AlignModuleList::const_iterator imod = alignModules->begin();
648 AlignModuleList::const_iterator imod_end = alignModules->end();
649 for( ; imod!=imod_end; ++imod) {
657 int thisNDoF = alignPars->
size();
659 CLHEP::HepSymMatrix d2Chi2(thisNDoF,0);
660 CLHEP::HepVector dChi2(thisNDoF,0);
661 for (
int i=0;
i<thisNDoF;++
i) {
662 int ipar = alignPars->
at(
i)->index();
663 dChi2[
i] = (*m_bigvector)[ipar];
664 for (
int j=0;j<thisNDoF;++j) {
665 int jpar = alignPars->
at(j)->index();
666 d2Chi2[
i][j] = (*m_bigmatrix)[ipar][jpar];
690 totalNDoF += thisNDoF;
692 CLHEP::HepSymMatrix
cov(d2Chi2);
703 CLHEP::HepVector delta =
cov * dChi2;
711 for (
int idof=0;idof<thisNDoF;++idof) {
715 double param = -delta[idof] *
sigma;
716 double err = std::sqrt(2.*std::fabs(
cov[idof][idof])) *
sigma;
722 ATH_MSG_DEBUG(
"Filling constants obtained using Local method");
724 ATH_MSG_DEBUG(
"set param to "<<param<<
" for alignPar "<<alignPar);
731 *
m_logStream<<
"CLHEP inversion status flag = "<<ierr<<std::endl;
734 double chi2 = d2Chi2.similarity(delta) * .5;
736 *
m_logStream<<
"delta(chi2) of the alignment change : "<<
chi2<<
" / "<<thisNDoF<<std::endl;
741 *
m_logStream<<
"--------------------------------------------------------------------------------"<<std::endl;
742 *
m_logStream<<
"Total delta(chi2) of the alignment change from the local method : "<<totalChi2<<
" / "<<totalNDoF<<std::endl;
757 ATH_MSG_INFO(
"Info to obtained from from Binary files");
768 int nDoF=alignParList->
size();
770 std::map<int,unsigned long long> modIndexMap;
771 float dummyVersion(0.);
772 double totalscale=0.;
777 AlVec newVector(nDoF);
778 std::map<int,unsigned long long> newModIndexMap;
784 if (
sc==StatusCode::FAILURE) {
796 modIndexMap = newModIndexMap;
797 else if (modIndexMap!=newModIndexMap) {
817 int nDoF=modIndexMap.size();
823 if (
sc==StatusCode::SUCCESS)
824 *symBigMatrix += newMatrix;
828 throw std::logic_error(
"Unhandled matrix type");
834 if (
sc==StatusCode::SUCCESS) {
836 *spaBigMatrix += newMatrix;
838 *spaBigMatrix = newMatrix;
842 if (
sc==StatusCode::FAILURE) {
848 ATH_MSG_WARNING(
"matrix not expected format! Changing m_wSqMatrix to "<<!triang);
870 int nDoF = alignParList->
size();
877 double *
val =
new double[nDoF];
878 for (
int i=0;
i<nDoF;
i++) {
879 val[
i] = (*m_bigvector)[
i];
882 TVectorD myTVector(nDoF,
val);
891 double *hitmapA =
new double[
nModules];
892 double *hitmapB =
new double[
nModules];
893 AlignModuleList::const_iterator imod =
moduleList->begin();
894 AlignModuleList::const_iterator imod_end =
moduleList->end();
896 for(; imod != imod_end; ++imod) {
903 TVectorD hitmapHits(
nModules, hitmapA);
904 TVectorD hitmapTracks(
nModules, hitmapB);
912 hitmapHits.Write(
"Hits");
913 hitmapTracks.Write(
"Tracks");
914 myTMatrix->Write(
"Matrix");
915 myTVector.Write(
"Vector");
918 scale.Write(
"Scale");
921 double *moduleInfoA =
new double[nDoF];
922 double *dofInfoA =
new double[nDoF];
924 if (
sizeof(
unsigned long long) !=
sizeof(
double))
925 ATH_MSG_ERROR(
"Module Identifiers will not be saved. sizeof(double)!=sizeof(ulonglong)");
932 uint64_t id = (*alignPars)[
i]->alignModule()->identify().get_compact();
936 uint64_t dof = (*alignPars)[
i]->paramType();
941 TVectorD moduleIDs(nDoF, moduleInfoA) ;
942 TVectorD moduleDoFs(nDoF,dofInfoA);
943 delete [] moduleInfoA;
944 moduleIDs.Write(
"ModuleID");
945 moduleDoFs.Write(
"dof");
960 int nDoF=alignParList->
size();
963 std::map<int,unsigned long long> modIndexMap;
964 std::map<int,unsigned long long> DoFMap;
965 double totalscale=0.;
978 int numberOfReadErrors = 0;
980 struct rusage myusage{};
981 int itworked = getrusage(RUSAGE_SELF,&myusage);
985 long intialMemUse = myusage.ru_maxrss;
995 itworked = getrusage(RUSAGE_SELF,&myusage);
996 ATH_MSG_DEBUG(
"Memory usage [MB], total " << myusage.ru_maxrss/1024 <<
", increase " << (myusage.ru_maxrss-intialMemUse)/1024);
1001 ++numberOfReadErrors;
1006 std::map<int,unsigned long long> newModIndexMap;
1008 TVectorD* myModuleIDs;
1009 myModuleIDs = (TVectorD*)
myFile->Get(
"ModuleID");
1011 ++numberOfReadErrors;
1016 for (
int i(0);
i<myModuleIDs->GetNrows(); ++
i){
1018 double source = (*myModuleIDs)(
i);
1027 std::map<int,unsigned long long> newDoFMap;
1030 myDoFs = (TVectorD*)
myFile->Get(
"dof");
1032 ++numberOfReadErrors;
1037 for (
int i(0);
i<myDoFs->GetNrows(); ++
i){
1050 ++numberOfReadErrors;
1055 double scale=(*Scale)(0);
1056 totalscale +=
scale;
1063 ++numberOfReadErrors;
1089 for (
int i=0;
i<nDoF;
i++) {
1090 (*newVector)[
i] = (*vector)(
i);
1097 }
else if (DoFMap!=newDoFMap) {
1103 modIndexMap = newModIndexMap;
1104 }
else if (modIndexMap!=newModIndexMap) {
1122 ++numberOfReadErrors;
1134 for (
int ii=0;ii<nDoF;ii++) {
1135 const TMatrixTSparseRow_const<double> myRow = (*matrix)[ii];
1136 int i = myRow.GetRowIndex();
1137 for (
int jj=0;jj<myRow.GetNindex();jj++) {
1138 int j = (myRow.GetColPtr())[jj];
1139 const double myElement= (myRow.GetDataPtr())[jj];
1143 i = (myRow.GetColPtr())[jj];
1145 (*accumMatrix)[
i][j] = myElement;
1150 }
else if ( accumMatrix) {
1153 for (
int ii=0;ii<nDoF;ii++) {
1154 const TMatrixTSparseRow_const<double> myRow = (*matrix)[ii];
1155 int i = myRow.GetRowIndex();
1156 for (
int jj=0;jj<myRow.GetNindex();jj++) {
1157 int j = (myRow.GetColPtr())[jj];
1158 const double myElement= (myRow.GetDataPtr())[jj];
1162 i = (myRow.GetColPtr())[jj];
1164 (*accumMatrix)[
i][j] += myElement;
1171 ++numberOfReadErrors;
1184 ++numberOfReadErrors;
1189 tracks = (TVectorD*)
myFile->Get(
"Tracks");
1192 ++numberOfReadErrors;
1197 if(
hits->GetNrows() != TotalHits.GetNrows() ){
1200 ++numberOfReadErrors;
1205 TotalHits += (*hits);
1206 TotalTracks += (*tracks);
1214 itworked = getrusage(RUSAGE_SELF,&myusage);
1215 ATH_MSG_DEBUG(
"Memory usage [MB], total " << myusage.ru_maxrss/1024 <<
", increase " << (myusage.ru_maxrss-intialMemUse)/1024);
1226 for (
int i=0;
i<nDoF;
i++) {
1227 for (
int j=0;j<=
i;j++) {
1228 newMatrix[
i][j] = (*accumMatrix)[
i][j];
1232 *symBigMatrix += newMatrix;
1234 }
else if (spaBigMatrix) {
1236 *spaBigMatrix += *accumMatrix;
1244 AlignModuleList::const_iterator imod =
moduleList->begin();
1245 AlignModuleList::const_iterator imod_end =
moduleList->end();
1248 for(; imod != imod_end; ++imod, ++
index ) {
1252 totalhits += (
int)TotalHits(
index);
1283 double dummyVersion(2.);
1286 std::map<int,unsigned long long> modIndexMap;
1287 std::map<int,std::string> modNameMap;
1290 modIndexMap[
i]=(*alignPars)[
i]->alignModule()->identify().get_compact();
1291 modNameMap [
i]=(*alignPars)[
i]->alignModule()->name();
1298 if (!sc1.isSuccess() || !sc2.isSuccess()) {
1299 msg(MSG::ERROR)<<
"problem writing matrix or vector"<<
endmsg;
1312 if (!sc1.isSuccess() || !sc2.isSuccess()) {
1313 msg(MSG::ERROR)<<
"problem writing matrix or vector"<<
endmsg;
1339 ATH_MSG_DEBUG(
"rescaling the matrix/vector and applying the soft-mode-cut");
1342 int nDoF = alignParList->
size();
1348 for (
int i=0;
i<nDoF;
i++) {
1350 double sigma_i = (*alignParList)[
i]->sigma();
1351 double softCut = 2 *
pow( (*alignParList)[
i]->softCut() , -2 );
1352 (*m_bigvector)[
i] *= sigma_i;
1354 for (
int j=0;j<=
i;j++) {
1356 if ((*chkMatrix)[
i][j] != 0.) {
1357 double sigma_j = (*alignParList)[j]->sigma();
1358 (*m_bigmatrix)[
i][j] *= sigma_i * sigma_j;
1368 (*alignParList)[
i]->setFirstDeriv((*
m_bigvector)[
i]/sigma_i);
1369 (*alignParList)[
i]->setSecndDeriv((*
m_bigmatrix)[
i][
i]/sigma_i/sigma_i);
1376 int i =
p.first.first;
1377 int j =
p.first.second;
1380 double sigma_i = (*alignParList)[
i]->sigma();
1381 double sigma_j = (*alignParList)[j]->sigma();
1383 (*m_bigmatrix)[
i][j] *= sigma_i * sigma_j;
1388 for (
int i=0;
i<nDoF;
i++) {
1390 double sigma_i = (*alignParList)[
i]->sigma();
1391 (*m_bigvector)[
i] *= sigma_i;
1393 double softCut = 2 *
pow( (*alignParList)[
i]->softCut() , -2 );
1397 (*alignParList)[
i]->setFirstDeriv((*
m_bigvector)[
i]/sigma_i);
1398 (*alignParList)[
i]->setSecndDeriv((*
m_bigmatrix)[
i][
i]/sigma_i/sigma_i);
1402 unsigned long long OldPixelIdentifier = 37769216;
1403 unsigned long long IBLIdentifier = 33574912;
1405 unsigned long long SCT_ECA_8_Identifier = 218116096;
1406 std::string SCT_ECA_8_Name =
"SCT/EndcapA/Disk_8";
1411 ATH_MSG_INFO(
"Javi: Printing (*alignParList)[i]->alignModule()->identify32()");
1414 for(
int i=0;
i<nDoF;
i++)
1423 const auto & theParameterList = *alignParList;
1424 const auto & thisIdentifier = theParameterList[
i]->alignModule()->identify32();
1425 const auto & thisName = theParameterList[
i]->alignModule()->name();
1426 const auto & thisParameterType = theParameterList[
i]->paramType();
1427 const bool oldPixel = (thisIdentifier == OldPixelIdentifier);
1428 const bool ibl = (thisIdentifier == IBLIdentifier);
1429 const bool SCTECA8 = (thisIdentifier == SCT_ECA_8_Identifier);
1430 const bool SCTECA8_n = (thisName.find(SCT_ECA_8_Name)!= std::string::npos);
1434 {
ATH_MSG_INFO(
"SCT ECA Last Disk DoF have been skipped in the solving because DeactivateSCT_ECA_LastDisk is set to True");
1437 {
ATH_MSG_INFO(
"SCT ECA Last Disk DoF have been skipped in the solving because DeactivateSCT_ECA_LastDisk is set to True");
1443 {
ATH_MSG_INFO(
"Pixel DoF have been skipped in the solving because AlignIBLbutNotPixel is set to True");
1448 {
ATH_MSG_INFO(
"IBL DoF have been skipped in the solving because AlignPixelbutNotIBL is set to True");
1454 if ( oldPixel and (thisParameterType == 0))
1455 {
ATH_MSG_INFO(
"Pixel Tx DoF has been skipped in the solving because Remove_Pixel_Tx is set to True");
1459 if ( oldPixel and (thisParameterType == 1))
1460 {
ATH_MSG_INFO(
"Pixel Ty DoF has been skipped in the solving because Remove_Pixel_Ty is set to True");
1464 if (oldPixel and (thisParameterType == 2))
1465 {
ATH_MSG_INFO(
"Pixel Tz DoF has been skipped in the solving because Remove_Pixel_Tz is set to True");
1469 if (oldPixel and (thisParameterType == 3))
1470 {
ATH_MSG_INFO(
"Pixel Rx DoF has been skipped in the solving because Remove_Pixel_Rx is set to True");
1474 if (oldPixel and (thisParameterType == 4))
1475 {
ATH_MSG_INFO(
"Pixel Ry DoF has been skipped in the solving because Remove_Pixel_Ry is set to True");
1479 if (oldPixel and (thisParameterType == 5))
1480 {
ATH_MSG_INFO(
"Pixel Rz DoF has been skipped in the solving because Remove_Pixel_Rz is set to True");
1485 if (ibl and (thisParameterType == 0))
1486 {
ATH_MSG_INFO(
"IBL Tx DoF has been skipped in the solving because Remove_IBL_Tx is set to True");
1490 if (ibl and (thisParameterType == 1))
1491 {
ATH_MSG_INFO(
"IBL Ty DoF has been skipped in the solving because Remove_IBL_Ty is set to True");
1495 if (ibl and (thisParameterType == 2))
1496 {
ATH_MSG_INFO(
"IBL Tz DoF has been skipped in the solving because Remove_IBL_Tz is set to True");
1500 if (ibl and (thisParameterType == 3))
1501 {
ATH_MSG_INFO(
"IBL Rx DoF has been skipped in the solving because Remove_IBL_Rx is set to True");
1505 if (ibl and (thisParameterType == 4))
1506 {
ATH_MSG_INFO(
"IBL Ry DoF has been skipped in the solving because Remove_IBL_Ry is set to True");
1510 if (ibl and (thisParameterType == 5))
1511 {
ATH_MSG_INFO(
"IBL Rz DoF has been skipped in the solving because Remove_IBL_Rz is set to True");
1533 ATH_MSG_INFO(
"Spurious removal not implemented at the moment.");
1611 (*m_bigvector)[irow] += firstderiv;
1617 (*m_bigmatrix)[irow][
icol] += secondderiv;
1625 AlignModuleList::const_iterator imod = alignModules->begin();
1626 AlignModuleList::const_iterator imod_end = alignModules->end();
1627 for( ; imod!=imod_end; ++imod) {
1631 int thisNDoF = alignPars->
size();
1634 CLHEP::HepSymMatrix * covsub =
nullptr;
1636 covsub =
new CLHEP::HepSymMatrix(thisNDoF,0);
1637 for (
int i=0;
i<thisNDoF;++
i) {
1638 int ipar = alignPars->
at(
i)->index();
1639 double sigma_i = alignPars->
at(
i)->sigma();
1646 for (
int j=0;j<=
i;++j) {
1647 int jpar = alignPars->
at(j)->index();
1648 double sigma_j = alignPars->
at(j)->sigma();
1655 (*covsub)[
i][j] = (*cov)[iActive][jActive] * sigma_i * sigma_j;
1664 os <<
"--------------------------------------------------------------------------------" << std::endl;
1670 CLHEP::HepSymMatrix *
cov =
nullptr;
1672 int nsize = cov0->GetNrows();
1673 cov =
new CLHEP::HepSymMatrix(nsize,0);
1675 for(
int i=0;
i<nsize;
i++)
1676 for(
int j=0; j<=
i; j++)
1677 (*
cov)[
i][j] = (*cov0)[
i][j];
1708 boost::io::ios_all_saver ias(
os );
1710 os <<
"--------------------------------------------------------------------------------" << std::endl;
1711 os <<
"Alignment parameters for module: " <<
module->name() << std::endl;
1712 os <<
"Number of tracks passing: " <<
module->nTracks() << std::endl;
1714 os <<
"Number of hits too small: "<<
module->nHits()<<
" < "<<
m_minNumHits<<
" Skipping the module"<<std::endl;
1718 os <<
"Number of tracks too small: "<<
module->nTracks()<<
" < "<<
m_minNumTrks<<
" Skipping the module"<<std::endl;
1721 os <<
"Number of hits seen: " <<
module->nHits() << std::endl;
1722 os <<
"Number of tracks seen: " <<
module->nTracks() << std::endl;
1725 int thisNDoF = alignPars->
size();
1727 if(alignPars->
empty())
1728 os <<
"No active parameters" << std::endl;
1733 os.unsetf(std::ios_base::floatfield);
1734 os << std::setiosflags(std::ios_base::left) << std::setprecision(5);
1739 for ( ; ipar != ipar_end; ++ipar) {
1741 os << std::setw(10) <<
par->dumpType()
1742 << std::setw(12) <<
par->par() <<
" +/- " << std::setw(12) <<
par->err()
1748 CLHEP::HepSymMatrix corrsub(thisNDoF,0);
1749 for(
int irow=0; irow<thisNDoF; ++irow)
1752 os <<
"Local correlation matrix: " << corrsub <<
std::flush;
1769 *
m_logStream<<
"*************************************************************"<<std::endl;
1770 *
m_logStream<<
"************** solving using Global method ****************"<<std::endl;
1771 *
m_logStream<<
"************** using LAPACK ****************"<<std::endl;
1772 *
m_logStream<<
"*************************************************************"<<std::endl;
1778 for (
int iActive=0;iActive<
m_aNDoF;iActive++) {
1780 (*aBetterVec)[iActive] = (*m_bigvector)[
i];
1781 for (
int jActive=0;jActive<
m_aNDoF;jActive++) {
1783 (*aBetterMat)[iActive][jActive] = (*m_bigmatrix)[
i][j];
1790 ATH_MSG_WARNING(
"Scaling requested but scale not set. Not scaling matrix and vector.");
1797 ATH_MSG_DEBUG(
"Now Solving alignment using lapack diagonalization routine dspev...");
1800 const double tol = 1.e-20;
1802 double determ = (*aBetterMat).determinant();
1804 if (fabs(determ) < tol)
1811 d2Chi2 =
new AlSymMat(*aBetterMat);
1813 clock_t starttime = clock();
1821 int info = (*aBetterMat).diagonalize(jobz,
w,
z);
1826 clock_t stoptime = clock();
1827 double time_diag = (stoptime-starttime)/
double(CLOCKS_PER_SEC);
1828 ATH_MSG_INFO(
" - time spent diagonalizing the matrix: "<<time_diag<<
" s");
1830 double time_solve = 0.;
1832 starttime = clock();
1835 time_solve = (stoptime-starttime)/
double(CLOCKS_PER_SEC);
1836 ATH_MSG_INFO(
" - time spent solving the system: "<<time_solve<<
" s");
1838 *
m_logStream<<
"time spent for diagonalization: "<<time_diag<<
" s"<<std::endl;
1839 *
m_logStream<<
"time spent for post-solving: "<<time_solve<<
" s"<<std::endl;
1843 ATH_MSG_ERROR(
"Problem in diagonalization. Solving skipped.");
1845 *
m_logStream<<
"time spent for diagonalization: "<<time_diag<<
" s"<<std::endl;
1849 *
m_logStream<<
"total time spent in solve: "<<time_diag+time_solve<<
" s"<<std::endl;
1874 const double tol = 1.e-20;
1877 if (std::fabs(determ) < tol)
1883 if (fillvecmods==0) {
1895 return StatusCode::SUCCESS;
1897 else if (fillvecmods==2)
1898 return StatusCode::FAILURE;
1954 return StatusCode::SUCCESS;
1962 if(
z.ncol() !=
size) {
1963 msg(MSG::ERROR)<<
"Eigenvector matrix has incorrect size : "<<
z.ncol()<<
" != "<<
size<<
endmsg;
1978 ATH_MSG_INFO(
"writing the eigenvectors in a matrix: "<<
z.nrow() <<
"x" <<
z.ncol());
1989 if (
sc!=StatusCode::SUCCESS)
1990 msg(MSG::ERROR)<<
"Problem writing eigenvector matrix"<<
endmsg;
1996 ATH_MSG_INFO(
"writing the eigenvectors in a vector: "<<
w.size());
2000 sc =
w.WriteEigenvalueVec(
"eigenvalues.bin",
true);
2002 if (
sc!=StatusCode::SUCCESS)
2003 msg(MSG::ERROR)<<
"Problem writing eigenvector matrix"<<
endmsg;
2006 sc =
z.Write(
"eigenvectors.txt",
false);
2007 if (
sc!=StatusCode::SUCCESS)
2008 msg(MSG::ERROR)<<
"Problem writing eigenvector matrix to text file"<<
endmsg;
2009 sc =
w.WriteEigenvalueVec(
"eigenvalues.txt",
false);
2010 if (
sc!=StatusCode::SUCCESS)
2011 msg(MSG::ERROR)<<
"Problem writing eigenvalue vector to text file"<<
endmsg;
2017 const double eigenvalue_threshold = 1
e-19;
2022 ATH_MSG_INFO(
" Starting the automatic Weak Mode Removal method");
2029 ATH_MSG_DEBUG(
"AlignPull vector size is: "<< (*AlignPull).size());
2032 bool wm_stop =
false;
2039 (*Align_db)[
i] = (-D[
i]/
w[
i]);
2040 (*Align_error_db)[
i] = sqrt(1.0/
w[
i]/
m_scale);
2042 if (
w[
i]<eigenvalue_threshold) {
2044 <<
" removed as eigenvalue lower than the threshold " << eigenvalue_threshold
2046 (*AlignPull)[
i] = 0.0;
2050 (*AlignPull)[
i] = (*Align_db)[
i] / (*Align_error_db)[
i];
2052 ATH_MSG_DEBUG(
i <<
". AlignPar: " << (*Align_db)[
i] <<
" +- " << (*Align_error_db)[
i]
2053 <<
" (pull: " << (*AlignPull)[
i] <<
") ; w[i]: " <<
w[
i]);
2066 <<
" removed as pull is lower than " <<
m_pullcut <<
": "
2067 << (*AlignPull)[
i]);
2088 <<
" removed as diff between eigenvalues, " <<
w[
i] <<
" and " <<
w[
i+1]
2110 <<
" removed as diff between corrections, " <<
w[
i] <<
" and " <<
w[
i+1]
2111 <<
", is greater than "
2125 delete Align_error_db;
2128 Align_error_db =
nullptr;
2129 AlignPull =
nullptr;
2181 CLHEP::HepSymMatrix *
cov =
nullptr;
2185 cov =
new CLHEP::HepSymMatrix(
size,0);
2188 *
m_logStream<<
"/------ The Eigenvalue Spectrum -------"<<std::endl;
2192 for(
int j=0;j<
size;j++)
2193 thisdelta[j] =
z[
i][j] * (-D[
i]/
w[
i]);
2194 deltafull += thisdelta;
2212 for(
int j=0;j<
size;j++) {
2213 errSq[j] +=
z[
i][j] *
z[
i][j] /
w[
i];
2215 for(
int k=0;
k<=j;
k++)
2223 *
m_logStream<<
"\\----- End of Eigenvalue Spectrum -----"<<std::endl;
2232 double param = delta[
i];
2233 double err = sqrt(2.*std::fabs(errSq[
i]));
2236 AlignPar * alignPar=(*alignParList)[idof];
2252 ATH_MSG_DEBUG(
"set param to "<<param<<
" for alignPar "<<alignPar);
2260 double norm1st = dChi2->norm();
2263 *
m_logStream<<
"norm of first derivative : "<<norm1st<<std::endl;
2267 double dist = ( (*d2Chi2) * deltafull + (*dChi2) ).
norm();
2270 *
m_logStream<<
"distance to solution : "<<dist<<std::endl;
2273 double chi2 = delta * (*d2Chi2) * delta * .5;
2288 *
m_logStream<<
"*************************************************************"<<std::endl;
2289 *
m_logStream<<
"************** solving using Global method ****************"<<std::endl;
2290 *
m_logStream<<
"************** using SparseEigen ****************"<<std::endl;
2291 *
m_logStream<<
"*************************************************************"<<std::endl;
2295 clock_t starttime = clock();
2300 bool isCopy =
false;
2302 ATH_MSG_INFO(
"Converting Matrix Format for fast solving");
2307 ATH_MSG_INFO(
"Matrix format native to the fast solving");
2311 ATH_MSG_ERROR(
"Cannot cast to neither AlSymMat nor AlSpaMat");
2319 const AlSpaMat * chkMatrix = ABetterMat;
2325 for (
int iActive=0;iActive<
m_aNDoF;iActive++) {
2327 (*aBetterVec)[iActive] = (*m_bigvector)[
i];
2328 for (
int jActive=0;jActive<
m_aNDoF;jActive++) {
2331 if ( (*chkMatrix)[iActive][jActive] != 0. )
2332 (*aBetterMat)[iActive][jActive]=(*ABetterMat)[
i][j];
2337 AlVec origVec(*aBetterVec);
2342 int info = (*aBetterMat).SolveWithEigen(*aBetterVec);
2347 *
m_logStream<<
"SolveWithEigen solving OK."<<std::endl;
2352 *
m_logStream<<
"SolveWithEigen error code (0 if OK) = "<<
info<<std::endl;
2357 ABetterMat =
nullptr;
2360 clock_t stoptime = clock();
2361 double totaltime = (stoptime-starttime)/
double(CLOCKS_PER_SEC);
2362 ATH_MSG_INFO(
"Time spent in SolveWithEigen: "<<totaltime<<
" s");
2369 double param = -(*aBetterVec)[
i];
2373 AlignPar * alignPar=(*alignParList)[idof];
2383 ATH_MSG_DEBUG(
"set param to "<<param<<
" for alignPar "<<alignPar);
2388 CLHEP::HepSymMatrix *
cov =
nullptr;
2392 *
m_logStream<<
"norm of first derivative : "<<origVec.
norm()<<std::endl;
2395 double dist = ( (*aBetterMat) * (*aBetterVec) - origVec ).
norm();
2396 *
m_logStream<<
"distance to solution : "<<dist<<std::endl;
2399 double chi2 = (*aBetterVec) * (*aBetterMat) * (*aBetterVec) * .5;
2403 *
m_logStream<<
"time spent in solve : "<<totaltime<<
" s"<<std::endl;
2426 AlignModuleList::const_iterator imod =
moduleList->begin();
2427 AlignModuleList::const_iterator imod_end =
moduleList->end();
2429 for(; imod != imod_end; ++imod) {
2442 if (
sc!=StatusCode::SUCCESS)
2446 sc = hitmap.
Write(
"hitmap.txt",
false, 0);
2447 if (
sc!=StatusCode::SUCCESS)
2448 ATH_MSG_ERROR(
"Problem writing hitmap matrix to text file");
2464 for(
int imap=0;imap<
nFiles; imap++) {
2476 <<nextmap.
nrow()<<
" x "<<nextmap.
ncol()<<
"), should be ("<<
nModules<<
" x 2). Skipping.");
2483 AlignModuleList::const_iterator imod =
moduleList->begin();
2484 AlignModuleList::const_iterator imod_end =
moduleList->end();
2487 for(; imod != imod_end; ++imod) {
2493 totalhits += (
int)hitmap[
index][0];
2501 ATH_MSG_INFO(
"Hitmap accumulated from "<<
nFiles<<
" files with total of "<<totalhits<<
" hits.");