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>
49#include <boost/io/ios_state.hpp>
53#include <sys/resource.h>
59 const IInterface* parent)
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();
284 if (
msgLvl(MSG::VERBOSE)) {
285 msg(MSG::VERBOSE)<<
"dumping matrix and vector to screen"<<
endmsg;
286 for (
int i=0;i<nDoF;i++)
287 for (
int j=0;j<nDoF;j++)
289 msg(MSG::VERBOSE)<<i<<
", "<<j<<
" : "<<(*m_bigmatrix)[i][j] <<
endmsg;
291 for (
int i=0;i<nDoF;i++)
292 msg(MSG::VERBOSE)<<i <<
" : "<<(*m_bigvector)[i]<<
endmsg;
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];
312 TVectorD b(
m_aNDoF,firstderiv);
315 msg(MSG::DEBUG)<<
"First derivatives:"<<
endmsg;
317 msg(MSG::DEBUG)<<
"Second derivatives:"<<
endmsg;
323 TMatrixDSym ainv(c.Invert(status));
325 TVectorD
r(b.GetNrows());
327 r = c.Solve(b,status);
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];
350 double sigma = alignPar->
sigma();
351 double param = -
r[iAdof] * sigma;
352 double err = std::sqrt(2.*std::fabs(ainv(iAdof,iAdof))) * sigma;
355 ATH_MSG_DEBUG(
"ainv("<<iAdof<<
")="<<ainv(iAdof,iAdof)<<
", sigma: "<<sigma);
357 alignPar->
setPar(param,err);
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();
406 for (
int i=0;i<(int)alignParList->
size();i++)
413 msg(MSG::DEBUG)<<
"dumping matrix and vector to screen"<<
endmsg;
414 for (
int i=0;i<nDoF;i++)
415 for (
int j=0;j<nDoF;j++)
417 msg(MSG::DEBUG)<<i<<
", "<<j<<
" : "<<(*m_bigmatrix)[i][j] <<
endmsg;
419 for (
int i=0;i<nDoF;i++)
420 msg(MSG::DEBUG)<<i <<
" : "<<(*m_bigvector)[i]<<
endmsg;
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];
438 CLHEP::HepSymMatrix cov(
m_aNDoF,0);
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]) > 1e-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 );
509 for (
int i=0; i<
m_aNDoF-1; i++)
510 for (
int j=i+1; j<
m_aNDoF; j++)
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)
565 for(
int icol=0; icol<=irow; ++icol)
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");
587 if(ierr==0 && status)
590 for (
int iAdof=0;iAdof<
m_aNDoF;iAdof++) {
593 AlignPar * alignPar=(*alignParList)[idof];
595 double sigma = alignPar->
sigma();
596 double param = -delta[iAdof] * sigma;
597 double err = std::sqrt(2.*std::fabs(cov[iAdof][iAdof])) * sigma;
600 ATH_MSG_DEBUG(
"cov("<<iAdof<<
")="<<cov[iAdof][iAdof]<<
", sigma: "<<sigma);
602 alignPar->
setPar(param,err);
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];
674 ATH_MSG_INFO(
"Not enough hits in module \'"<<module->name()<<
"\': "
690 totalNDoF += thisNDoF;
692 CLHEP::HepSymMatrix cov(d2Chi2);
703 CLHEP::HepVector delta = cov * dChi2;
711 for (
int idof=0;idof<thisNDoF;++idof) {
714 double sigma = alignPar->
sigma();
715 double param = -delta[idof] * sigma;
716 double err = std::sqrt(2.*std::fabs(cov[idof][idof])) * sigma;
719 ATH_MSG_DEBUG(
"cov("<<idof<<
")="<<cov[idof][idof]<<
", sigma: "<<sigma);
722 ATH_MSG_DEBUG(
"Filling constants obtained using Local method");
723 alignPar->
setPar(param,err);
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) {
789 msg(MSG::FATAL) <<
"vector wrong size! newVector size "<<newVector.
size()
796 modIndexMap = newModIndexMap;
797 else if (modIndexMap!=newModIndexMap) {
798 msg(MSG::FATAL)<<
"module index maps don't agree!"<<
endmsg;
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);
889 int nModules = moduleList->size();
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) {
898 hitmapA[
index] = (double)module->nHits();
899 hitmapB[
index] = (double)module->nTracks();
903 TVectorD hitmapHits(nModules, hitmapA);
904 TVectorD hitmapTracks(nModules, hitmapB);
911 TFile myFile(filename,
"recreate");
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)");
929 for (
int i=0;i<(int)alignPars->
size();i++) {
932 uint64_t
id = (*alignPars)[i]->alignModule()->identify().get_compact();
933 memcpy(&target, &
id,
sizeof(target));
934 moduleInfoA[i]=target;
936 uint64_t dof = (*alignPars)[i]->paramType();
937 memcpy(&target, &dof,
sizeof(target));
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.;
973 int nModules = moduleList->size();
975 TVectorD TotalHits(nModules);
976 TVectorD TotalTracks(nModules);
978 int numberOfReadErrors = 0;
980 struct rusage myusage{};
981 int itworked = getrusage(RUSAGE_SELF,&myusage);
985 long intialMemUse = myusage.ru_maxrss;
987 for (
int ifile = 0; ifile < (int)
m_inputTFiles.size(); ifile++) {
995 itworked = getrusage(RUSAGE_SELF,&myusage);
996 ATH_MSG_DEBUG(
"Memory usage [MB], total " << myusage.ru_maxrss/1024 <<
", increase " << (myusage.ru_maxrss-intialMemUse)/1024);
1000 if ( myFile->IsZombie() || !(myFile->IsOpen()) ) {
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);
1020 memcpy(&target, &source,
sizeof(target));
1021 newModIndexMap[i]=target;
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){
1039 double source = (*myDoFs)(i);
1041 memcpy(&target, &source,
sizeof(target));
1042 newDoFMap[i]=target;
1048 Scale = (TVectorD*)myFile->Get(
"Scale");
1050 ++numberOfReadErrors;
1055 double scale=(*Scale)(0);
1056 totalscale += scale;
1061 TVectorD*
vector = (TVectorD*)myFile->Get(
"Vector");
1063 ++numberOfReadErrors;
1073 msg(MSG::FATAL) <<
"vector wrong size! newVector size " << newVector->
size()
1081 msg(MSG::FATAL) <<
"File vector wrong size! File Vector size " <<
vector->GetNrows()
1089 for (
int i=0;i<nDoF;i++) {
1090 (*newVector)[i] = (*vector)(i);
1097 }
else if (DoFMap!=newDoFMap) {
1098 msg(MSG::FATAL) <<
"module dofs don't agree!" <<
endmsg;
1103 modIndexMap = newModIndexMap;
1104 }
else if (modIndexMap!=newModIndexMap) {
1105 msg(MSG::FATAL) <<
"module index maps don't agree!" <<
endmsg;
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;
1182 hits = (TVectorD*)myFile->Get(
"Hits");
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 ) {
1250 module->setNHits((int)TotalHits(index));
1251 module->setNTracks((int)TotalTracks(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;
1289 for (
int i=0;i<(int)alignPars->
size();i++) {
1290 modIndexMap[i]=(*alignPars)[i]->alignModule()->identify().get_compact();
1291 modNameMap [i]=(*alignPars)[i]->alignModule()->name();
1297 StatusCode sc2 =
m_bigvector->WritePartial(
"vector.bin",
true,
m_scale,modIndexMap,dummyVersion);
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);
1375 for (
const datamap::value_type& p : *
m_bigmatrix->ptrMap()) {
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++)
1417 ATH_MSG_DEBUG((*alignParList)[i]->alignModule()->identify32());
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)
1750 for(
int icol=0; icol<=irow; ++icol)
1751 corrsub[irow][icol] = (*cov)[irow][icol] / sqrt((*cov)[irow][irow] * (*cov)[icol][icol]);
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) {
1888 if (
msgLvl(MSG::DEBUG)) {
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());
1987 StatusCode
sc =
z.Write(
"eigenvectors.bin",
true);
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 = 1e-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;
2037 for(
int i=0; i<size; i++) {
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]);
2061 for(
int i=
m_modcut; (i<size && !wm_stop); i++) {
2066 <<
" removed as pull is lower than " <<
m_pullcut <<
": "
2067 << (*AlignPull)[i]);
2083 for(
int i=
m_modcut; (i<size && !wm_stop); i++) {
2088 <<
" removed as diff between eigenvalues, " << w[i] <<
" and " << w[i+1]
2105 for(
int i=
m_modcut; (i<size && !wm_stop); i++) {
2108 if ( fabs((*Align_db)[i]) >
m_Align_db_step*fabs((*Align_db)[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;
2177 AlVec deltafull(size);
2181 CLHEP::HepSymMatrix * cov =
nullptr;
2185 cov =
new CLHEP::HepSymMatrix(size,0);
2188 *
m_logStream<<
"/------ The Eigenvalue Spectrum -------"<<std::endl;
2190 for (
int i=0;i<size;i++) {
2191 AlVec thisdelta(size);
2192 for(
int j=0;j<size;j++)
2193 thisdelta[j] =
z[i][j] * (-D[i]/w[i]);
2194 deltafull += thisdelta;
2200 *
m_logStream<<
"| skipping eigenvalue "<<w[i]<<std::endl;
2205 *
m_logStream<<
"| skipping eigenvalue "<<w[i]<<std::endl;
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++)
2216 (*cov)[j][k] +=
z[i][j] *
z[i][k] / w[i];
2223 *
m_logStream<<
"\\----- End of Eigenvalue Spectrum -----"<<std::endl;
2230 for(
int i=0; i<size; i++) {
2232 double param = delta[i];
2233 double err = sqrt(2.*std::fabs(errSq[i]));
2236 AlignPar * alignPar=(*alignParList)[idof];
2239 double sigma = alignPar->
sigma();
2249 ATH_MSG_DEBUG(
"cov("<<i<<
")="<<errSq[i]<<
", sigma: "<<sigma);
2251 alignPar->
setPar(param, err);
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;
2276 *
m_logStream<<
"delta(chi2) of the alignment change : "<<
chi2<<
" / "<<size<<std::endl;
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;
2350 ATH_MSG_ERROR(
"SolveWithEigen error code (0 if OK) = "<<info );
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");
2367 for(
int i=0; i<
m_aNDoF; i++) {
2369 double param = -(*aBetterVec)[i];
2373 AlignPar * alignPar=(*alignParList)[idof];
2376 double sigma = alignPar->
sigma();
2382 alignPar->
setPar(param, err);
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;
2423 int nModules = moduleList->size();
2425 AlMat hitmap(nModules,2);
2426 AlignModuleList::const_iterator imod = moduleList->begin();
2427 AlignModuleList::const_iterator imod_end = moduleList->end();
2429 for(; imod != imod_end; ++imod) {
2431 hitmap[
index][0] =
module->nHits();
2432 hitmap[
index][1] =
module->nTracks();
2440 StatusCode
sc = hitmap.
Write(
"hitmap.bin",
true);
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");
2460 int nModules = moduleList->size();
2462 AlMat hitmap(nModules,2);
2464 for(
int imap=0;imap<nFiles; imap++) {
2465 AlMat nextmap(nModules,2);
2474 if(nextmap.
nrow()!=nModules || nextmap.
ncol()!=2) {
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) {
2491 module->setNHits((int)hitmap[index][0]);
2492 module->setNTracks((int)hitmap[index][1]);
2493 totalhits += (int)hitmap[
index][0];
2501 ATH_MSG_INFO(
"Hitmap accumulated from "<<nFiles<<
" files with total of "<<totalhits<<
" hits.");
class TMatrixTSparse< double > TMatrixDSparse
#define ATH_MSG_WARNING(x)
constexpr int pow(int base, int exp) noexcept
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
bool msgLvl(const MSG::Level lvl) const
DataModel_detail::const_iterator< DataVector > const_iterator
Standard const_iterator.
const T * at(size_type n) const
Access an element, as an rvalue.
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
bool empty() const noexcept
Returns true if the collection is empty.
contains the implementation of the methods of class AlMat, for handling general NxM matrices
StatusCode ReadScalaPack(const std::string &)
StatusCode Write(const std::string &, bool, unsigned int precision=6)
void SetPathBin(const std::string &)
void SetPathTxt(const std::string &)
contains the implementation for handling sparse matrices
virtual StatusCode Read(const std::string &, int &, bool &, float &) override final
contains the base implementation for handling symmertic matrices
const datamap * ptrMap() const
contains the implementation for handling symmetric matrices in triangular representation
virtual StatusCode Read(const std::string &, int &, bool &, float &) override final
void SetPathBin(const std::string &)
StatusCode ReadPartial(const std::string &, double &, std::map< int, unsigned long long > &, float &)
void SetPathTxt(const std::string &)
double sigma() const
returns sigma
void setPar(double par, double err)
sets final parameter and error
double initPar() const
returns initial parameter and error
double chi2(TH1 *h0, TH1 *h1)
void Scale(TH1 *h, double d=1)
Ensure that the ATLAS eigen extensions are properly loaded.
std::vector< AlignModule * > AlignModuleList
@ z
global position (cartesian)