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)
63 declareInterface<IMatrixTool>(
this);
83 return StatusCode::FAILURE;
86 ATH_MSG_INFO(
"Retrieving data from the following files: ");
91 return StatusCode::SUCCESS;
99 return StatusCode::SUCCESS;
106 ATH_MSG_INFO(
"allocating matrix and vector with nDoF = "<<nDoF);
128 return StatusCode::SUCCESS;
141 *
m_logStream<<
"*************************************************************"<<std::endl;
142 *
m_logStream<<
"************** solving using Global method ****************"<<std::endl;
143 *
m_logStream<<
"************** using ROOT ****************"<<std::endl;
144 *
m_logStream<<
"*************************************************************"<<std::endl;
148 clock_t starttime = clock();
156 if (
msgLvl(MSG::VERBOSE)) {
157 msg(MSG::VERBOSE)<<
"dumping matrix and vector to screen"<<
endmsg;
158 for (
int i=0;i<nDoF;i++)
159 for (
int j=0;j<nDoF;j++)
161 msg(MSG::VERBOSE)<<i<<
", "<<j<<
" : "<<(*m_bigmatrix)[i][j] <<
endmsg;
163 for (
int i=0;i<nDoF;i++)
164 msg(MSG::VERBOSE)<<i <<
" : "<<(*m_bigvector)[i]<<
endmsg;
169 double * firstderiv =
new double[
m_aNDoF];
170 for (
int iActive=0;iActive<
m_aNDoF;iActive++) {
172 firstderiv[iActive] = (*m_bigvector)[i];
173 for (
int jActive=0;jActive<
m_aNDoF;jActive++) {
175 secderiv[iActive*
m_aNDoF+jActive] = (*m_bigmatrix)[i][j];
184 TVectorD b(
m_aNDoF,firstderiv);
187 msg(MSG::DEBUG)<<
"First derivatives:"<<
endmsg;
189 msg(MSG::DEBUG)<<
"Second derivatives:"<<
endmsg;
195 TMatrixDSym ainv(c.Invert(status));
197 TVectorD
r(b.GetNrows());
199 r = c.Solve(b,status);
202 clock_t stoptime = clock();
203 double totaltime = (stoptime-starttime)/
double(CLOCKS_PER_SEC);
204 ATH_MSG_INFO(
"Time spent in solveROOT: "<<totaltime<<
" s");
207 msg(MSG::ERROR)<<
"ROOT inversion failed"<<
endmsg;
217 for (
int iAdof=0;iAdof<
m_aNDoF;iAdof++) {
220 AlignPar * alignPar=(*alignParList)[idof];
222 double sigma = alignPar->
sigma();
223 double param = -
r[iAdof] * sigma;
224 double err = std::sqrt(2.*std::fabs(ainv(iAdof,iAdof))) * sigma;
227 ATH_MSG_DEBUG(
"ainv("<<iAdof<<
")="<<ainv(iAdof,iAdof)<<
", sigma: "<<sigma);
229 alignPar->
setPar(param,err);
230 ATH_MSG_DEBUG(
"set param to "<<param<<
" for alignPar "<<alignPar);
241 *
m_logStream<<
"norm of first derivative : "<<sqrt(b.Norm2Sqr())<<std::endl;
244 double dist = sqrt( ( b - (
a *
r) ).Norm2Sqr() );
245 *
m_logStream<<
"distance to solution : "<<dist<<std::endl;
248 double chi2 =
a.Similarity(
r) * .5;
252 *
m_logStream<<
"time spent in solve : "<<totaltime<<
" s"<<std::endl;
257 delete [] firstderiv;
267 *
m_logStream<<
"*************************************************************"<<std::endl;
268 *
m_logStream<<
"************** solving using Global method ****************"<<std::endl;
269 *
m_logStream<<
"************** using CLHEP ****************"<<std::endl;
270 *
m_logStream<<
"*************************************************************"<<std::endl;
274 clock_t starttime = clock();
278 for (
int i=0;i<(int)alignParList->
size();i++)
285 msg(MSG::DEBUG)<<
"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::DEBUG)<<i<<
", "<<j<<
" : "<<(*m_bigmatrix)[i][j] <<
endmsg;
291 for (
int i=0;i<nDoF;i++)
292 msg(MSG::DEBUG)<<i <<
" : "<<(*m_bigvector)[i]<<
endmsg;
296 CLHEP::HepSymMatrix * d2Chi2 =
new CLHEP::HepSymMatrix(
m_aNDoF,0);
297 CLHEP::HepVector * dChi2 =
new CLHEP::HepVector(
m_aNDoF,0);
298 for (
int iActive=0;iActive<
m_aNDoF;iActive++) {
300 (*dChi2)[iActive] = (*m_bigvector)[i];
301 for (
int jActive=0;jActive<
m_aNDoF;jActive++) {
303 (*d2Chi2)[iActive][jActive] = (*m_bigmatrix)[i][j];
310 CLHEP::HepSymMatrix cov(
m_aNDoF,0);
311 CLHEP::HepVector delta(
m_aNDoF,0);
312 CLHEP::HepVector deltafull(
m_aNDoF,0);
321 *
m_logStream<<
"Running matrix inversion"<<std::endl;
326 msg(MSG::ERROR)<<
"CLHEP inversion status flag = "<<ierr<<
endmsg;
330 *
m_logStream<<
"CLHEP inversion status flag = "<<ierr<<std::endl;
333 delta = cov * (*dChi2);
343 CLHEP::HepSymMatrix cov2 = *d2Chi2 * .5;
348 msg(MSG::WARNING)<<
"Second CLHEP inversion status flag = "<<ierr2<<
endmsg;
350 CLHEP::HepVector delta2 = cov2 * (*dChi2) * .5;
351 for (
int i=0;i<delta.num_row(); ++i)
352 if ( fabs((delta[i] - delta2[i])/delta[i]) > 1e-5 ) {
353 msg(MSG::WARNING)<<
"Something's wrong with the matrix inversion: delta["<<i<<
"] = "<<delta[i]<<
" delta2["<<i<<
"] = "<<delta2[i]<<
endmsg;
359 *
m_logStream<<
"CLHEP inversion status flag for halfed matrix = "<<ierr2<<std::endl;
360 *
m_logStream<<
"Matrix inversion check failed"<<std::endl;
370 *
m_logStream<<
"Running diagonalization"<<std::endl;
372 CLHEP::HepSymMatrix D = *d2Chi2;
373 CLHEP::HepMatrix U = CLHEP::diagonalize( &D );
381 for (
int i=0; i<
m_aNDoF-1; i++)
382 for (
int j=i+1; j<
m_aNDoF; j++)
383 if(D[j][j] < D[i][i]) {
398 CLHEP::HepVector eigenvector(
m_aNDoF);
401 *
m_logStream<<
"/------ The Eigenvalue Spectrum -------"<<std::endl;
404 for(
int imode=0; imode<
m_aNDoF; ++imode) {
407 for(
int irow=0; irow<
m_aNDoF; ++irow)
408 eigenvector[irow] = U[irow][imode];
412 double eigenvalue = D[imode][imode];
415 double evdotb =
dot(*dChi2,eigenvector);
416 CLHEP::HepVector thisdelta = evdotb/eigenvalue * eigenvector;
417 deltafull += thisdelta;
422 *
m_logStream<<
"| skipping eigenvalue "<<eigenvalue<<std::endl;
427 *
m_logStream<<
"| skipping eigenvalue "<<eigenvalue<<std::endl;
436 for(
int irow=0; irow<
m_aNDoF; ++irow)
437 for(
int icol=0; icol<=irow; ++icol)
438 cov[irow][icol] += eigenvector[irow] * eigenvector[icol] / eigenvalue;
448 *
m_logStream<<
"\\----- End of Eigenvalue Spectrum -----"<<std::endl;
455 clock_t stoptime = clock();
456 double totaltime = (stoptime-starttime)/
double(CLOCKS_PER_SEC);
457 ATH_MSG_INFO(
"Time spent in solveCLHEP: "<<totaltime<<
" s");
459 if(ierr==0 && status)
462 for (
int iAdof=0;iAdof<
m_aNDoF;iAdof++) {
465 AlignPar * alignPar=(*alignParList)[idof];
467 double sigma = alignPar->
sigma();
468 double param = -delta[iAdof] * sigma;
469 double err = std::sqrt(2.*std::fabs(cov[iAdof][iAdof])) * sigma;
472 ATH_MSG_DEBUG(
"cov("<<iAdof<<
")="<<cov[iAdof][iAdof]<<
", sigma: "<<sigma);
474 alignPar->
setPar(param,err);
475 ATH_MSG_DEBUG(
"set param to "<<param<<
" for alignPar "<<alignPar);
483 *
m_logStream<<
"norm of first derivative : "<<dChi2->norm()<<std::endl;
486 double dist = ( - (*d2Chi2) * deltafull + (*dChi2) ).norm();
487 *
m_logStream<<
"distance to solution : "<<dist<<std::endl;
490 double chi2 = d2Chi2->similarity(delta) * .5;
494 *
m_logStream<<
"time spent in solve : "<<totaltime<<
" s"<<std::endl;
509 *
m_logStream<<
"*************************************************************"<<std::endl;
510 *
m_logStream<<
"************** solving using Local method *****************"<<std::endl;
511 *
m_logStream<<
"*************************************************************"<<std::endl;
515 double totalChi2(0.);
519 AlignModuleList::const_iterator imod = alignModules->begin();
520 AlignModuleList::const_iterator imod_end = alignModules->end();
521 for( ; imod!=imod_end; ++imod) {
529 int thisNDoF = alignPars->
size();
531 CLHEP::HepSymMatrix d2Chi2(thisNDoF,0);
532 CLHEP::HepVector dChi2(thisNDoF,0);
533 for (
int i=0;i<thisNDoF;++i) {
534 int ipar = alignPars->
at(i)->index();
535 dChi2[i] = (*m_bigvector)[ipar];
536 for (
int j=0;j<thisNDoF;++j) {
537 int jpar = alignPars->
at(j)->index();
538 d2Chi2[i][j] = (*m_bigmatrix)[ipar][jpar];
546 ATH_MSG_INFO(
"Not enough hits in module \'"<<module->name()<<
"\': "
562 totalNDoF += thisNDoF;
564 CLHEP::HepSymMatrix cov(d2Chi2);
575 CLHEP::HepVector delta = cov * dChi2;
583 for (
int idof=0;idof<thisNDoF;++idof) {
586 double sigma = alignPar->
sigma();
587 double param = -delta[idof] * sigma;
588 double err = std::sqrt(2.*std::fabs(cov[idof][idof])) * sigma;
591 ATH_MSG_DEBUG(
"cov("<<idof<<
")="<<cov[idof][idof]<<
", sigma: "<<sigma);
594 ATH_MSG_DEBUG(
"Filling constants obtained using Local method");
595 alignPar->
setPar(param,err);
596 ATH_MSG_DEBUG(
"set param to "<<param<<
" for alignPar "<<alignPar);
603 *
m_logStream<<
"CLHEP inversion status flag = "<<ierr<<std::endl;
606 double chi2 = d2Chi2.similarity(delta) * .5;
608 *
m_logStream<<
"delta(chi2) of the alignment change : "<<
chi2<<
" / "<<thisNDoF<<std::endl;
613 *
m_logStream<<
"--------------------------------------------------------------------------------"<<std::endl;
614 *
m_logStream<<
"Total delta(chi2) of the alignment change from the local method : "<<totalChi2<<
" / "<<totalNDoF<<std::endl;
629 ATH_MSG_INFO(
"Info to obtained from from Binary files");
640 int nDoF=alignParList->
size();
642 std::map<int,unsigned long long> modIndexMap;
643 float dummyVersion(0.);
644 double totalscale=0.;
649 AlVec newVector(nDoF);
650 std::map<int,unsigned long long> newModIndexMap;
656 if (
sc==StatusCode::FAILURE) {
661 msg(MSG::FATAL) <<
"vector wrong size! newVector size "<<newVector.
size()
668 modIndexMap = newModIndexMap;
669 else if (modIndexMap!=newModIndexMap) {
670 msg(MSG::FATAL)<<
"module index maps don't agree!"<<
endmsg;
689 int nDoF=modIndexMap.size();
695 if (
sc==StatusCode::SUCCESS)
696 *symBigMatrix += newMatrix;
700 throw std::logic_error(
"Unhandled matrix type");
706 if (
sc==StatusCode::SUCCESS) {
708 *spaBigMatrix += newMatrix;
710 *spaBigMatrix = newMatrix;
714 if (
sc==StatusCode::FAILURE) {
720 ATH_MSG_WARNING(
"matrix not expected format! Changing m_wSqMatrix to "<<!triang);
742 int nDoF = alignParList->
size();
749 double *val =
new double[nDoF];
750 for (
int i=0;i<nDoF;i++) {
751 val[i] = (*m_bigvector)[i];
754 TVectorD myTVector(nDoF, val);
761 int nModules = moduleList->size();
763 double *hitmapA =
new double[nModules];
764 double *hitmapB =
new double[nModules];
765 AlignModuleList::const_iterator imod = moduleList->begin();
766 AlignModuleList::const_iterator imod_end = moduleList->end();
768 for(; imod != imod_end; ++imod) {
770 hitmapA[
index] = (double)module->nHits();
771 hitmapB[
index] = (double)module->nTracks();
775 TVectorD hitmapHits(nModules, hitmapA);
776 TVectorD hitmapTracks(nModules, hitmapB);
783 TFile myFile(filename,
"recreate");
784 hitmapHits.Write(
"Hits");
785 hitmapTracks.Write(
"Tracks");
786 myTMatrix->Write(
"Matrix");
787 myTVector.Write(
"Vector");
790 scale.Write(
"Scale");
793 double *moduleInfoA =
new double[nDoF];
794 double *dofInfoA =
new double[nDoF];
796 if (
sizeof(
unsigned long long) !=
sizeof(
double))
797 ATH_MSG_ERROR(
"Module Identifiers will not be saved. sizeof(double)!=sizeof(ulonglong)");
801 for (
int i=0;i<(int)alignPars->
size();i++) {
804 uint64_t
id = (*alignPars)[i]->alignModule()->identify().get_compact();
805 memcpy(&target, &
id,
sizeof(target));
806 moduleInfoA[i]=target;
808 uint64_t dof = (*alignPars)[i]->paramType();
809 memcpy(&target, &dof,
sizeof(target));
813 TVectorD moduleIDs(nDoF, moduleInfoA) ;
814 TVectorD moduleDoFs(nDoF,dofInfoA);
815 delete [] moduleInfoA;
816 moduleIDs.Write(
"ModuleID");
817 moduleDoFs.Write(
"dof");
832 int nDoF=alignParList->
size();
835 std::map<int,unsigned long long> modIndexMap;
836 std::map<int,unsigned long long> DoFMap;
837 double totalscale=0.;
845 int nModules = moduleList->size();
847 TVectorD TotalHits(nModules);
848 TVectorD TotalTracks(nModules);
850 int numberOfReadErrors = 0;
852 struct rusage myusage{};
853 int itworked = getrusage(RUSAGE_SELF,&myusage);
857 long intialMemUse = myusage.ru_maxrss;
859 for (
int ifile = 0; ifile < (int)
m_inputTFiles.size(); ifile++) {
867 itworked = getrusage(RUSAGE_SELF,&myusage);
868 ATH_MSG_DEBUG(
"Memory usage [MB], total " << myusage.ru_maxrss/1024 <<
", increase " << (myusage.ru_maxrss-intialMemUse)/1024);
872 if ( myFile->IsZombie() || !(myFile->IsOpen()) ) {
873 ++numberOfReadErrors;
878 std::map<int,unsigned long long> newModIndexMap;
880 TVectorD* myModuleIDs;
881 myModuleIDs = (TVectorD*)myFile->Get(
"ModuleID");
883 ++numberOfReadErrors;
888 for (
int i(0); i<myModuleIDs->GetNrows(); ++i){
890 double source = (*myModuleIDs)(i);
892 memcpy(&target, &source,
sizeof(target));
893 newModIndexMap[i]=target;
899 std::map<int,unsigned long long> newDoFMap;
902 myDoFs = (TVectorD*)myFile->Get(
"dof");
904 ++numberOfReadErrors;
909 for (
int i(0); i<myDoFs->GetNrows(); ++i){
911 double source = (*myDoFs)(i);
913 memcpy(&target, &source,
sizeof(target));
920 Scale = (TVectorD*)myFile->Get(
"Scale");
922 ++numberOfReadErrors;
927 double scale=(*Scale)(0);
933 TVectorD*
vector = (TVectorD*)myFile->Get(
"Vector");
935 ++numberOfReadErrors;
945 msg(MSG::FATAL) <<
"vector wrong size! newVector size " << newVector->
size()
953 msg(MSG::FATAL) <<
"File vector wrong size! File Vector size " <<
vector->GetNrows()
961 for (
int i=0;i<nDoF;i++) {
962 (*newVector)[i] = (*vector)(i);
969 }
else if (DoFMap!=newDoFMap) {
970 msg(MSG::FATAL) <<
"module dofs don't agree!" <<
endmsg;
975 modIndexMap = newModIndexMap;
976 }
else if (modIndexMap!=newModIndexMap) {
977 msg(MSG::FATAL) <<
"module index maps don't agree!" <<
endmsg;
994 ++numberOfReadErrors;
1006 for (
int ii=0;ii<nDoF;ii++) {
1007 const TMatrixTSparseRow_const<double> myRow = (*matrix)[ii];
1008 int i = myRow.GetRowIndex();
1009 for (
int jj=0;jj<myRow.GetNindex();jj++) {
1010 int j = (myRow.GetColPtr())[jj];
1011 const double myElement= (myRow.GetDataPtr())[jj];
1015 i = (myRow.GetColPtr())[jj];
1017 (*accumMatrix)[i][j] = myElement;
1022 }
else if ( accumMatrix) {
1025 for (
int ii=0;ii<nDoF;ii++) {
1026 const TMatrixTSparseRow_const<double> myRow = (*matrix)[ii];
1027 int i = myRow.GetRowIndex();
1028 for (
int jj=0;jj<myRow.GetNindex();jj++) {
1029 int j = (myRow.GetColPtr())[jj];
1030 const double myElement= (myRow.GetDataPtr())[jj];
1034 i = (myRow.GetColPtr())[jj];
1036 (*accumMatrix)[i][j] += myElement;
1043 ++numberOfReadErrors;
1054 hits = (TVectorD*)myFile->Get(
"Hits");
1056 ++numberOfReadErrors;
1061 tracks = (TVectorD*)myFile->Get(
"Tracks");
1064 ++numberOfReadErrors;
1069 if(hits->GetNrows() != TotalHits.GetNrows() ){
1072 ++numberOfReadErrors;
1077 TotalHits += (*hits);
1078 TotalTracks += (*tracks);
1086 itworked = getrusage(RUSAGE_SELF,&myusage);
1087 ATH_MSG_DEBUG(
"Memory usage [MB], total " << myusage.ru_maxrss/1024 <<
", increase " << (myusage.ru_maxrss-intialMemUse)/1024);
1098 for (
int i=0;i<nDoF;i++) {
1099 for (
int j=0;j<=i;j++) {
1100 newMatrix[i][j] = (*accumMatrix)[i][j];
1104 *symBigMatrix += newMatrix;
1106 }
else if (spaBigMatrix) {
1108 *spaBigMatrix += *accumMatrix;
1116 AlignModuleList::const_iterator imod = moduleList->begin();
1117 AlignModuleList::const_iterator imod_end = moduleList->end();
1120 for(; imod != imod_end; ++imod, ++
index ) {
1122 module->setNHits((int)TotalHits(index));
1123 module->setNTracks((int)TotalTracks(index));
1124 totalhits += (int)TotalHits(
index);
1155 double dummyVersion(2.);
1158 std::map<int,unsigned long long> modIndexMap;
1159 std::map<int,std::string> modNameMap;
1161 for (
int i=0;i<(int)alignPars->
size();i++) {
1162 modIndexMap[i]=(*alignPars)[i]->alignModule()->identify().get_compact();
1163 modNameMap [i]=(*alignPars)[i]->alignModule()->name();
1169 StatusCode sc2 =
m_bigvector->WritePartial(
"vector.bin",
true,
m_scale,modIndexMap,dummyVersion);
1170 if (!sc1.isSuccess() || !sc2.isSuccess()) {
1171 msg(MSG::ERROR)<<
"problem writing matrix or vector"<<
endmsg;
1184 if (!sc1.isSuccess() || !sc2.isSuccess()) {
1185 msg(MSG::ERROR)<<
"problem writing matrix or vector"<<
endmsg;
1211 ATH_MSG_DEBUG(
"rescaling the matrix/vector and applying the soft-mode-cut");
1214 int nDoF = alignParList->
size();
1220 for (
int i=0;i<nDoF;i++) {
1222 double sigma_i = (*alignParList)[i]->sigma();
1223 double softCut = 2 *
pow( (*alignParList)[i]->softCut() , -2 );
1224 (*m_bigvector)[i] *= sigma_i;
1226 for (
int j=0;j<=i;j++) {
1228 if ((*chkMatrix)[i][j] != 0.) {
1229 double sigma_j = (*alignParList)[j]->sigma();
1230 (*m_bigmatrix)[i][j] *= sigma_i * sigma_j;
1240 (*alignParList)[i]->setFirstDeriv((*
m_bigvector)[i]/sigma_i);
1241 (*alignParList)[i]->setSecndDeriv((*
m_bigmatrix)[i][i]/sigma_i/sigma_i);
1247 for (
const datamap::value_type& p : *
m_bigmatrix->ptrMap()) {
1248 int i = p.first.first;
1249 int j = p.first.second;
1252 double sigma_i = (*alignParList)[i]->sigma();
1253 double sigma_j = (*alignParList)[j]->sigma();
1255 (*m_bigmatrix)[i][j] *= sigma_i * sigma_j;
1260 for (
int i=0;i<nDoF;i++) {
1262 double sigma_i = (*alignParList)[i]->sigma();
1263 (*m_bigvector)[i] *= sigma_i;
1265 double softCut = 2 *
pow( (*alignParList)[i]->softCut() , -2 );
1269 (*alignParList)[i]->setFirstDeriv((*
m_bigvector)[i]/sigma_i);
1270 (*alignParList)[i]->setSecndDeriv((*
m_bigmatrix)[i][i]/sigma_i/sigma_i);
1274 unsigned long long OldPixelIdentifier = 37769216;
1275 unsigned long long IBLIdentifier = 33574912;
1277 unsigned long long SCT_ECA_8_Identifier = 218116096;
1278 std::string SCT_ECA_8_Name =
"SCT/EndcapA/Disk_8";
1283 ATH_MSG_INFO(
"Javi: Printing (*alignParList)[i]->alignModule()->identify32()");
1286 for(
int i=0;i<nDoF;i++)
1289 ATH_MSG_DEBUG((*alignParList)[i]->alignModule()->identify32());
1295 const auto & theParameterList = *alignParList;
1296 const auto & thisIdentifier = theParameterList[i]->alignModule()->identify32();
1297 const auto & thisName = theParameterList[i]->alignModule()->name();
1298 const auto & thisParameterType = theParameterList[i]->paramType();
1299 const bool oldPixel = (thisIdentifier == OldPixelIdentifier);
1300 const bool ibl = (thisIdentifier == IBLIdentifier);
1301 const bool SCTECA8 = (thisIdentifier == SCT_ECA_8_Identifier);
1302 const bool SCTECA8_n = (thisName.find(SCT_ECA_8_Name)!= std::string::npos);
1306 {
ATH_MSG_INFO(
"SCT ECA Last Disk DoF have been skipped in the solving because DeactivateSCT_ECA_LastDisk is set to True");
1309 {
ATH_MSG_INFO(
"SCT ECA Last Disk DoF have been skipped in the solving because DeactivateSCT_ECA_LastDisk is set to True");
1315 {
ATH_MSG_INFO(
"Pixel DoF have been skipped in the solving because AlignIBLbutNotPixel is set to True");
1320 {
ATH_MSG_INFO(
"IBL DoF have been skipped in the solving because AlignPixelbutNotIBL is set to True");
1326 if ( oldPixel and (thisParameterType == 0))
1327 {
ATH_MSG_INFO(
"Pixel Tx DoF has been skipped in the solving because Remove_Pixel_Tx is set to True");
1331 if ( oldPixel and (thisParameterType == 1))
1332 {
ATH_MSG_INFO(
"Pixel Ty DoF has been skipped in the solving because Remove_Pixel_Ty is set to True");
1336 if (oldPixel and (thisParameterType == 2))
1337 {
ATH_MSG_INFO(
"Pixel Tz DoF has been skipped in the solving because Remove_Pixel_Tz is set to True");
1341 if (oldPixel and (thisParameterType == 3))
1342 {
ATH_MSG_INFO(
"Pixel Rx DoF has been skipped in the solving because Remove_Pixel_Rx is set to True");
1346 if (oldPixel and (thisParameterType == 4))
1347 {
ATH_MSG_INFO(
"Pixel Ry DoF has been skipped in the solving because Remove_Pixel_Ry is set to True");
1351 if (oldPixel and (thisParameterType == 5))
1352 {
ATH_MSG_INFO(
"Pixel Rz DoF has been skipped in the solving because Remove_Pixel_Rz is set to True");
1357 if (ibl and (thisParameterType == 0))
1358 {
ATH_MSG_INFO(
"IBL Tx DoF has been skipped in the solving because Remove_IBL_Tx is set to True");
1362 if (ibl and (thisParameterType == 1))
1363 {
ATH_MSG_INFO(
"IBL Ty DoF has been skipped in the solving because Remove_IBL_Ty is set to True");
1367 if (ibl and (thisParameterType == 2))
1368 {
ATH_MSG_INFO(
"IBL Tz DoF has been skipped in the solving because Remove_IBL_Tz is set to True");
1372 if (ibl and (thisParameterType == 3))
1373 {
ATH_MSG_INFO(
"IBL Rx DoF has been skipped in the solving because Remove_IBL_Rx is set to True");
1377 if (ibl and (thisParameterType == 4))
1378 {
ATH_MSG_INFO(
"IBL Ry DoF has been skipped in the solving because Remove_IBL_Ry is set to True");
1382 if (ibl and (thisParameterType == 5))
1383 {
ATH_MSG_INFO(
"IBL Rz DoF has been skipped in the solving because Remove_IBL_Rz is set to True");
1405 ATH_MSG_INFO(
"Spurious removal not implemented at the moment.");
1483 (*m_bigvector)[irow] += firstderiv;
1489 (*m_bigmatrix)[irow][icol] += secondderiv;
1497 AlignModuleList::const_iterator imod = alignModules->begin();
1498 AlignModuleList::const_iterator imod_end = alignModules->end();
1499 for( ; imod!=imod_end; ++imod) {
1503 int thisNDoF = alignPars->
size();
1506 CLHEP::HepSymMatrix * covsub =
nullptr;
1508 covsub =
new CLHEP::HepSymMatrix(thisNDoF,0);
1509 for (
int i=0;i<thisNDoF;++i) {
1510 int ipar = alignPars->
at(i)->index();
1511 double sigma_i = alignPars->
at(i)->sigma();
1518 for (
int j=0;j<=i;++j) {
1519 int jpar = alignPars->
at(j)->index();
1520 double sigma_j = alignPars->
at(j)->sigma();
1527 (*covsub)[i][j] = (*cov)[iActive][jActive] * sigma_i * sigma_j;
1536 os <<
"--------------------------------------------------------------------------------" << std::endl;
1542 CLHEP::HepSymMatrix * cov =
nullptr;
1544 int nsize = cov0->GetNrows();
1545 cov =
new CLHEP::HepSymMatrix(nsize,0);
1547 for(
int i=0; i<nsize; i++)
1548 for(
int j=0; j<=i; j++)
1549 (*cov)[i][j] = (*cov0)[i][j];
1580 boost::io::ios_all_saver ias( os );
1582 os <<
"--------------------------------------------------------------------------------" << std::endl;
1583 os <<
"Alignment parameters for module: " << module->name() << std::endl;
1584 os <<
"Number of tracks passing: " << module->nTracks() << std::endl;
1586 os <<
"Number of hits too small: "<<module->nHits()<<
" < "<<
m_minNumHits<<
" Skipping the module"<<std::endl;
1590 os <<
"Number of tracks too small: "<<module->nTracks()<<
" < "<<
m_minNumTrks<<
" Skipping the module"<<std::endl;
1593 os <<
"Number of hits seen: " << module->nHits() << std::endl;
1594 os <<
"Number of tracks seen: " << module->nTracks() << std::endl;
1597 int thisNDoF = alignPars->
size();
1599 if(alignPars->
empty())
1600 os <<
"No active parameters" << std::endl;
1605 os.unsetf(std::ios_base::floatfield);
1606 os << std::setiosflags(std::ios_base::left) << std::setprecision(5);
1611 for ( ; ipar != ipar_end; ++ipar) {
1613 os << std::setw(10) << par->dumpType()
1614 << std::setw(12) << par->par() <<
" +/- " << std::setw(12) << par->err()
1620 CLHEP::HepSymMatrix corrsub(thisNDoF,0);
1621 for(
int irow=0; irow<thisNDoF; ++irow)
1622 for(
int icol=0; icol<=irow; ++icol)
1623 corrsub[irow][icol] = (*cov)[irow][icol] / sqrt((*cov)[irow][irow] * (*cov)[icol][icol]);
1624 os <<
"Local correlation matrix: " << corrsub << std::flush;
1641 *
m_logStream<<
"*************************************************************"<<std::endl;
1642 *
m_logStream<<
"************** solving using Global method ****************"<<std::endl;
1643 *
m_logStream<<
"************** using LAPACK ****************"<<std::endl;
1644 *
m_logStream<<
"*************************************************************"<<std::endl;
1650 for (
int iActive=0;iActive<
m_aNDoF;iActive++) {
1652 (*aBetterVec)[iActive] = (*m_bigvector)[i];
1653 for (
int jActive=0;jActive<
m_aNDoF;jActive++) {
1655 (*aBetterMat)[iActive][jActive] = (*m_bigmatrix)[i][j];
1662 ATH_MSG_WARNING(
"Scaling requested but scale not set. Not scaling matrix and vector.");
1669 ATH_MSG_DEBUG(
"Now Solving alignment using lapack diagonalization routine dspev...");
1672 const double tol = 1.e-20;
1674 double determ = (*aBetterMat).determinant();
1676 if (fabs(determ) < tol)
1683 d2Chi2 =
new AlSymMat(*aBetterMat);
1685 clock_t starttime = clock();
1693 int info = (*aBetterMat).diagonalize(jobz,w,
z);
1698 clock_t stoptime = clock();
1699 double time_diag = (stoptime-starttime)/
double(CLOCKS_PER_SEC);
1700 ATH_MSG_INFO(
" - time spent diagonalizing the matrix: "<<time_diag<<
" s");
1702 double time_solve = 0.;
1704 starttime = clock();
1707 time_solve = (stoptime-starttime)/
double(CLOCKS_PER_SEC);
1708 ATH_MSG_INFO(
" - time spent solving the system: "<<time_solve<<
" s");
1710 *
m_logStream<<
"time spent for diagonalization: "<<time_diag<<
" s"<<std::endl;
1711 *
m_logStream<<
"time spent for post-solving: "<<time_solve<<
" s"<<std::endl;
1715 ATH_MSG_ERROR(
"Problem in diagonalization. Solving skipped.");
1717 *
m_logStream<<
"time spent for diagonalization: "<<time_diag<<
" s"<<std::endl;
1721 *
m_logStream<<
"total time spent in solve: "<<time_diag+time_solve<<
" s"<<std::endl;
1746 const double tol = 1.e-20;
1749 if (std::fabs(determ) < tol)
1755 if (fillvecmods==0) {
1760 if (
msgLvl(MSG::DEBUG)) {
1767 return StatusCode::SUCCESS;
1769 else if (fillvecmods==2)
1770 return StatusCode::FAILURE;
1826 return StatusCode::SUCCESS;
1834 if(
z.ncol() != size) {
1835 msg(MSG::ERROR)<<
"Eigenvector matrix has incorrect size : "<<
z.ncol()<<
" != "<<size<<
endmsg;
1850 ATH_MSG_INFO(
"writing the eigenvectors in a matrix: "<<
z.nrow() <<
"x" <<
z.ncol());
1861 if (
sc!=StatusCode::SUCCESS)
1862 msg(MSG::ERROR)<<
"Problem writing eigenvector matrix"<<
endmsg;
1868 ATH_MSG_INFO(
"writing the eigenvectors in a vector: "<< w.size());
1874 if (
sc!=StatusCode::SUCCESS)
1875 msg(MSG::ERROR)<<
"Problem writing eigenvector matrix"<<
endmsg;
1878 sc =
z.Write(
"eigenvectors.txt",
false);
1879 if (
sc!=StatusCode::SUCCESS)
1880 msg(MSG::ERROR)<<
"Problem writing eigenvector matrix to text file"<<
endmsg;
1881 sc = w.WriteEigenvalueVec(
"eigenvalues.txt",
false);
1882 if (
sc!=StatusCode::SUCCESS)
1883 msg(MSG::ERROR)<<
"Problem writing eigenvalue vector to text file"<<
endmsg;
1889 const double eigenvalue_threshold = 1e-19;
1894 ATH_MSG_INFO(
" Starting the automatic Weak Mode Removal method");
1901 ATH_MSG_DEBUG(
"AlignPull vector size is: "<< (*AlignPull).size());
1904 bool wm_stop =
false;
1909 for(
int i=0; i<size; i++) {
1911 (*Align_db)[i] = (-D[i]/w[i]);
1912 (*Align_error_db)[i] = sqrt(1.0/w[i]/
m_scale);
1914 if (w[i]<eigenvalue_threshold) {
1916 <<
" removed as eigenvalue lower than the threshold " << eigenvalue_threshold
1918 (*AlignPull)[i] = 0.0;
1922 (*AlignPull)[i] = (*Align_db)[i] / (*Align_error_db)[i];
1924 ATH_MSG_DEBUG(i <<
". AlignPar: " << (*Align_db)[i] <<
" +- " << (*Align_error_db)[i]
1925 <<
" (pull: " << (*AlignPull)[i] <<
") ; w[i]: " << w[i]);
1933 for(
int i=
m_modcut; (i<size && !wm_stop); i++) {
1938 <<
" removed as pull is lower than " <<
m_pullcut <<
": "
1939 << (*AlignPull)[i]);
1955 for(
int i=
m_modcut; (i<size && !wm_stop); i++) {
1960 <<
" removed as diff between eigenvalues, " << w[i] <<
" and " << w[i+1]
1977 for(
int i=
m_modcut; (i<size && !wm_stop); i++) {
1980 if ( fabs((*Align_db)[i]) >
m_Align_db_step*fabs((*Align_db)[i+1]) ) {
1982 <<
" removed as diff between corrections, " << w[i] <<
" and " << w[i+1]
1983 <<
", is greater than "
1997 delete Align_error_db;
2000 Align_error_db =
nullptr;
2001 AlignPull =
nullptr;
2049 AlVec deltafull(size);
2053 CLHEP::HepSymMatrix * cov =
nullptr;
2057 cov =
new CLHEP::HepSymMatrix(size,0);
2060 *
m_logStream<<
"/------ The Eigenvalue Spectrum -------"<<std::endl;
2062 for (
int i=0;i<size;i++) {
2063 AlVec thisdelta(size);
2064 for(
int j=0;j<size;j++)
2065 thisdelta[j] =
z[i][j] * (-D[i]/w[i]);
2066 deltafull += thisdelta;
2072 *
m_logStream<<
"| skipping eigenvalue "<<w[i]<<std::endl;
2077 *
m_logStream<<
"| skipping eigenvalue "<<w[i]<<std::endl;
2084 for(
int j=0;j<size;j++) {
2085 errSq[j] +=
z[i][j] *
z[i][j] / w[i];
2087 for(
int k=0;k<=j;k++)
2088 (*cov)[j][k] +=
z[i][j] *
z[i][k] / w[i];
2095 *
m_logStream<<
"\\----- End of Eigenvalue Spectrum -----"<<std::endl;
2102 for(
int i=0; i<size; i++) {
2104 double param = delta[i];
2105 double err = sqrt(2.*std::fabs(errSq[i]));
2108 AlignPar * alignPar=(*alignParList)[idof];
2111 double sigma = alignPar->
sigma();
2121 ATH_MSG_DEBUG(
"cov("<<i<<
")="<<errSq[i]<<
", sigma: "<<sigma);
2123 alignPar->
setPar(param, err);
2124 ATH_MSG_DEBUG(
"set param to "<<param<<
" for alignPar "<<alignPar);
2132 double norm1st = dChi2->
norm();
2135 *
m_logStream<<
"norm of first derivative : "<<norm1st<<std::endl;
2139 double dist = ( (*d2Chi2) * deltafull + (*dChi2) ).norm();
2142 *
m_logStream<<
"distance to solution : "<<dist<<std::endl;
2145 double chi2 = delta * (*d2Chi2) * delta * .5;
2148 *
m_logStream<<
"delta(chi2) of the alignment change : "<<
chi2<<
" / "<<size<<std::endl;
2160 *
m_logStream<<
"*************************************************************"<<std::endl;
2161 *
m_logStream<<
"************** solving using Global method ****************"<<std::endl;
2162 *
m_logStream<<
"************** using SparseEigen ****************"<<std::endl;
2163 *
m_logStream<<
"*************************************************************"<<std::endl;
2167 clock_t starttime = clock();
2172 bool isCopy =
false;
2174 ATH_MSG_INFO(
"Converting Matrix Format for fast solving");
2179 ATH_MSG_INFO(
"Matrix format native to the fast solving");
2183 ATH_MSG_ERROR(
"Cannot cast to neither AlSymMat nor AlSpaMat");
2191 const AlSpaMat * chkMatrix = ABetterMat;
2197 for (
int iActive=0;iActive<
m_aNDoF;iActive++) {
2199 (*aBetterVec)[iActive] = (*m_bigvector)[i];
2200 for (
int jActive=0;jActive<
m_aNDoF;jActive++) {
2203 if ( (*chkMatrix)[iActive][jActive] != 0. )
2204 (*aBetterMat)[iActive][jActive]=(*ABetterMat)[i][j];
2209 AlVec origVec(*aBetterVec);
2214 int info = (*aBetterMat).SolveWithEigen(*aBetterVec);
2219 *
m_logStream<<
"SolveWithEigen solving OK."<<std::endl;
2222 ATH_MSG_ERROR(
"SolveWithEigen error code (0 if OK) = "<<info );
2224 *
m_logStream<<
"SolveWithEigen error code (0 if OK) = "<<info<<std::endl;
2229 ABetterMat =
nullptr;
2232 clock_t stoptime = clock();
2233 double totaltime = (stoptime-starttime)/
double(CLOCKS_PER_SEC);
2234 ATH_MSG_INFO(
"Time spent in SolveWithEigen: "<<totaltime<<
" s");
2239 for(
int i=0; i<
m_aNDoF; i++) {
2241 double param = -(*aBetterVec)[i];
2245 AlignPar * alignPar=(*alignParList)[idof];
2248 double sigma = alignPar->
sigma();
2254 alignPar->
setPar(param, err);
2255 ATH_MSG_DEBUG(
"set param to "<<param<<
" for alignPar "<<alignPar);
2260 CLHEP::HepSymMatrix * cov =
nullptr;
2264 *
m_logStream<<
"norm of first derivative : "<<origVec.
norm()<<std::endl;
2267 double dist = ( (*aBetterMat) * (*aBetterVec) - origVec ).norm();
2268 *
m_logStream<<
"distance to solution : "<<dist<<std::endl;
2271 double chi2 = (*aBetterVec) * (*aBetterMat) * (*aBetterVec) * .5;
2275 *
m_logStream<<
"time spent in solve : "<<totaltime<<
" s"<<std::endl;
2295 int nModules = moduleList->size();
2297 AlMat hitmap(nModules,2);
2298 AlignModuleList::const_iterator imod = moduleList->begin();
2299 AlignModuleList::const_iterator imod_end = moduleList->end();
2301 for(; imod != imod_end; ++imod) {
2303 hitmap[
index][0] =
module->nHits();
2304 hitmap[
index][1] =
module->nTracks();
2312 StatusCode
sc = hitmap.
Write(
"hitmap.bin",
true);
2314 if (
sc!=StatusCode::SUCCESS)
2318 sc = hitmap.
Write(
"hitmap.txt",
false, 0);
2319 if (
sc!=StatusCode::SUCCESS)
2320 ATH_MSG_ERROR(
"Problem writing hitmap matrix to text file");
2332 int nModules = moduleList->size();
2334 AlMat hitmap(nModules,2);
2336 for(
int imap=0;imap<nFiles; imap++) {
2337 AlMat nextmap(nModules,2);
2346 if(nextmap.
nrow()!=nModules || nextmap.
ncol()!=2) {
2348 <<nextmap.
nrow()<<
" x "<<nextmap.
ncol()<<
"), should be ("<<nModules<<
" x 2). Skipping.");
2355 AlignModuleList::const_iterator imod = moduleList->begin();
2356 AlignModuleList::const_iterator imod_end = moduleList->end();
2359 for(; imod != imod_end; ++imod) {
2363 module->setNHits((int)hitmap[index][0]);
2364 module->setNTracks((int)hitmap[index][1]);
2365 totalhits += (int)hitmap[
index][0];
2373 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
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)