6 #include "CLHEP/Matrix/Matrix.h"
7 #include "CLHEP/Matrix/SymMatrix.h"
8 #include "CLHEP/Matrix/DiagMatrix.h"
10 #include "GaudiKernel/StatusCode.h"
11 #include "GaudiKernel/MsgStream.h"
12 #include "GaudiKernel/AlgTool.h"
45 , m_matrixTool(
"Trk::MatrixTool",this)
46 , m_alignModuleTool(
"Trk::AlignModuleTool/AlignModuleTool")
68 , m_perigee_x{}, m_perigee_y{}, m_perigee_z{}
71 , m_bremFitSuccessful{}
74 , m_hardScatterOrKink{}
78 declareInterface<IAlignTool>(
this);
80 declareProperty(
"MatrixTool", m_matrixTool,
"tool for storing and inverting matrix");
81 declareProperty(
"AlignModuleTool", m_alignModuleTool);
83 declareProperty(
"SecondDerivativeCut", m_secondDerivativeCut = -1e5);
85 declareProperty(
"DoTree", m_doTree =
false);
86 declareProperty(
"WriteActualSecDeriv", m_writeActualSecDeriv =
false);
88 declareProperty(
"StoreLocalDerivOnly", m_storeLocalDerivOnly =
false);
90 m_logStream =
nullptr;
115 return StatusCode::SUCCESS;
128 ATH_MSG_DEBUG(
"in GlobalGhi2AlignTool::firstEventInitialize()");
131 ATH_MSG_DEBUG(
"allocating matrix with "<<nDoF<<
" degrees of freedom");
135 return StatusCode::FAILURE;
139 return StatusCode::SUCCESS;
152 m_tree=
new TTree(
"globalChi2Deriv",
"globalChi2Deriv");
183 bool fullVertex =
false;
187 const std::vector<AlignModuleVertexDerivatives> * ptrX =
nullptr;
191 ptrPosition = ptrVertex->
position();
192 ptrCovariance = ptrVertex->covariance();
194 vtxType = ptrVertex->
type();
198 msg(MSG::ERROR)<<
"something missing from alignVertex!"<<
endmsg;
199 if (!ptrPosition)
msg(MSG::ERROR)<<
"no fitted position!"<<
endmsg;
200 if (!ptrCovariance)
msg(MSG::ERROR)<<
"no covariance!"<<
endmsg;
201 if (!ptrX)
msg(MSG::ERROR)<<
"no link to the X object!"<<
endmsg;
217 const std::vector<AlignModuleDerivatives> * ptrDerivs = alignTrack->
derivatives();
220 if (!ptrWeights || !ptrWeightsFD || !ptrResiduals || !ptrDerivs) {
221 msg(MSG::ERROR)<<
"something missing from alignTrack!"<<
endmsg;
222 if (!ptrWeights)
msg(MSG::ERROR)<<
"no weights!"<<
endmsg;
223 if (!ptrWeightsFD)
msg(MSG::ERROR)<<
"no weights for first deriv!"<<
endmsg;
224 if (!ptrResiduals)
msg(MSG::ERROR)<<
"no residuals!"<<
endmsg;
225 if (!ptrDerivs)
msg(MSG::ERROR)<<
"no derivatives!"<<
endmsg;
237 std::vector<AlignModuleDerivatives> derivatives = *ptrDerivs;
238 const std::vector<AlignModuleDerivatives>* derivativeErr = alignTrack->
derivativeErr();
239 const std::vector<std::pair<AlignModule*,std::vector<double> > >* secDerivs = alignTrack->
actualSecondDerivatives();
243 std::vector<AlignPar*> allAlignPars;
244 std::vector<Amg::VectorX*> allDerivatives;
245 std::vector<const Amg::VectorX*> allDerivativeErr;
246 std::vector<double> allActualSecDeriv;
252 msg(
MSG::DEBUG) <<
"accumulate: The derivative vector size is " << derivatives.size() <<
endmsg;
256 std::vector<AlignModuleDerivatives>::const_iterator derivErrIt;
257 std::vector<std::pair<AlignModule*,std::vector<double> > >::const_iterator secDerivIt;
258 if (derivativeErr) derivErrIt=derivativeErr->begin();
259 if (secDerivs) secDerivIt=secDerivs->begin();
260 for ( ; derivIt!=derivIt_end ; ++derivIt) {
267 ATH_MSG_DEBUG(
"add track to module "<<
module->identify().get_identifier32().get_compact());
279 std::vector<Amg::VectorX>& deriv_vec = derivIt->second;
280 ATH_MSG_DEBUG(
"accumulate: The deriv_vec size is " << deriv_vec.size() );
282 int nModPars = alignPars->
size();
283 if (nModPars != (
int)deriv_vec.size() && (nModPars+3) != (
int)deriv_vec.size()) {
287 for (
int i=0;
i<3;
i++) {
288 for (
int j=0;j<WSize;j++) {
289 matrix_F(
i,j) += deriv_vec[nModPars+
i][j];
294 for (
int i=0;
i<nModPars;
i++) {
296 allAlignPars.push_back((*alignPars)[
i]);
297 allDerivatives.push_back(&deriv_vec[
i]);
299 allDerivativeErr.push_back(&(derivErrIt->second)[
i]);
301 allActualSecDeriv.push_back((secDerivIt->second)[
i]);
303 if (derivativeErr) ++derivErrIt;
304 if (secDerivs) ++secDerivIt;
310 for (; iatsos != iatsos_last; ++iatsos)
311 if ((*iatsos)->module())
312 (*iatsos)->module()->addHit();
315 int nAllPars = allAlignPars.size();
317 for (
int ipar=0;ipar<nAllPars;ipar++) {
320 Amg::MatrixX derivativesT = (*allDerivatives[ipar]).transpose();
321 ATH_MSG_DEBUG(
"derivativesT (size "<<derivativesT.cols()<<
"): "<<derivativesT);
327 int index1 = allAlignPars[ipar]->index();
331 if (firstderivM(0,0)==0.) {
332 ATH_MSG_DEBUG(
"firstderivM[0][0] is zero : "<<firstderivM(0,0));
343 ATH_MSG_DEBUG(
"alActualSecDeriv size: "<<allActualSecDeriv.size());
350 int imodule1 = allAlignPars[ipar]->alignModule()->identifyHash();
353 bool goodSecondDeriv =
true;
354 for (
int jpar=ipar;jpar<nAllPars;jpar++) {
357 int index2=allAlignPars[jpar]->index();
360 int imodule2 = allAlignPars[jpar]->alignModule()->identifyHash();
365 if (imodule1 != imodule2)
373 if (jpar==ipar && secondderivM(0,0)<0.) {
379 goodSecondDeriv =
false;
393 if (goodSecondDeriv) {
407 ATH_MSG_DEBUG(
"accumulate: Contribution from the fullVTX will be added " );
409 Amg::MatrixX RHM = (*ptrCovariance) * (matrix_F) * (weightsFirstDeriv * residuals);
411 std::vector<AlignPar*> vtxAlignPars;
412 std::vector<const Amg::VectorX*> vtxDerivatives;
415 msg(
MSG::DEBUG) <<
"accumulate: The Vertex derivative vector size is " << ptrX->size() <<
endmsg;
416 std::vector<AlignModuleVertexDerivatives>::const_iterator XIt = ptrX->begin();
417 std::vector<AlignModuleVertexDerivatives>::const_iterator XIt_end = ptrX->end();
419 for ( ; XIt!=XIt_end ; ++XIt) {
425 const std::vector<Amg::VectorX>& X_vec = XIt->second;
428 int nModPars = alignPars->
size();
429 if (nModPars != (
int)X_vec.size()) {
435 for (
int i=0;
i<nModPars;
i++) {
437 vtxAlignPars.push_back((*alignPars)[
i]);
438 vtxDerivatives.push_back(&X_vec[
i]);
453 ptrVertex->Qmatrix()->computeInverseAndDetWithCheck(Qinv,determinant,invertible);
456 std::cout <<
"accumulate: Q inversion failed. CLHEP status flag = "<<ierr<< std::endl;
466 int nvtxPars = vtxAlignPars.size();
467 for (
int ipar=0;ipar<nvtxPars;ipar++) {
470 Amg::MatrixX derivativesT = (*vtxDerivatives[ipar]).transpose();
475 firstderivM += 2.* derivativesT * ((*ptrCovariance) * Qinv) * vtemp;
479 int index1 = vtxAlignPars[ipar]->index();
485 ATH_MSG_DEBUG(
"accumulate: Filling second derivative for this vertex... ");
486 for (
int jpar=0;jpar<nvtxPars;jpar++) {
489 Amg::MatrixX secondderivM = -1.* derivativesT * (*ptrCovariance) * (*vtxDerivatives[jpar]);
492 if( ptrVertex->
Constrained() && ptrVertex->Qmatrix()->determinant() > 1.0e-24 ) {
493 secondderivM += 2.* derivativesT * ((*ptrCovariance) * Qinv * (*ptrCovariance)) * (*vtxDerivatives[jpar]);
497 int index2=vtxAlignPars[jpar]->index();
524 if (
sc==StatusCode::SUCCESS) {
538 m_momentum=std::fabs(
pT)*std::sqrt(1.+sinh(eta)*sinh(eta));
602 *
m_logStream<<
"*************************************************************"<<std::endl;
603 *
m_logStream<<
"****** Global Chi2 alignment solving ******"<<std::endl;
618 ATH_MSG_INFO(
">>>> -----------------------------------------------");
620 ATH_MSG_INFO(
">>>> -----------------------------------------------");
624 return StatusCode::SUCCESS;
626 return StatusCode::FAILURE;
636 success =
m_tree->Write();
638 return success>0 ? StatusCode::SUCCESS : StatusCode::FAILURE;
645 return StatusCode::SUCCESS;
652 double materialOnTrack(0.);
665 for ( ; tsit!=tsit_end ; ++tsit ) {
667 if (materialEffects) {
679 return materialOnTrack;