#include #include TMatrixT GFTools::getSmoothedPos(GFTrack* trk, unsigned int irep, unsigned int ihit) { TMatrixT smoothed_state; TMatrixT smoothed_cov; TMatrixT pos; if(GFTools::getSmoothedData(trk, irep, ihit, smoothed_state, smoothed_cov)) { TMatrixT H = trk->getHit(ihit)->getHMatrix(trk->getTrackRep(irep)); TMatrixT pos_tmp(H * smoothed_state); pos.ResizeTo(pos_tmp); pos = pos_tmp; } return pos; } TMatrixT GFTools::getBiasedSmoothedPos(GFTrack* trk, unsigned int irep, unsigned int ihit) { TMatrixT smoothed_state; TMatrixT smoothed_cov; TMatrixT pos; if(GFTools::getBiasedSmoothedData(trk, irep, ihit, smoothed_state, smoothed_cov)) { TMatrixT H = trk->getHit(ihit)->getHMatrix(trk->getTrackRep(irep)); H.Print();smoothed_state.Print(); TMatrixT pos_tmp(H * smoothed_state); pos.ResizeTo(pos_tmp); pos = pos_tmp; } return pos; } TMatrixT GFTools::getSmoothedCov(GFTrack* trk, unsigned int irep, unsigned int ihit) { TMatrixT smoothed_state; TMatrixT smoothed_cov; GFDetPlane pl; GFTools::getSmoothedData(trk, irep, ihit, smoothed_state, smoothed_cov, pl); TMatrixT H = trk->getHit(ihit)->getHMatrix(trk->getTrackRep(irep)); TMatrixT cov_tmp(smoothed_cov,TMatrixT::kMultTranspose,H); TMatrixT cov(H,TMatrixT::kMult,cov_tmp); return cov; } TMatrixT GFTools::getBiasedSmoothedCov(GFTrack* trk, unsigned int irep, unsigned int ihit) { TMatrixT smoothed_state; TMatrixT smoothed_cov; GFDetPlane pl; GFTools::getBiasedSmoothedData(trk, irep, ihit, smoothed_state, smoothed_cov, pl); TMatrixT H = trk->getHit(ihit)->getHMatrix(trk->getTrackRep(irep)); TMatrixT cov_tmp(smoothed_cov,TMatrixT::kMultTranspose,H); TMatrixT cov(H,TMatrixT::kMult,cov_tmp); return cov; } bool GFTools::getSmoothedData(GFTrack* trk, unsigned int irep, unsigned int ihit, TMatrixT& smoothed_state, TMatrixT& smoothed_cov) { GFDetPlane pl; TMatrixT auxInfo; return GFTools::getSmoothedData(trk, irep, ihit, smoothed_state, smoothed_cov, pl, auxInfo); } bool GFTools::getBiasedSmoothedData(GFTrack* trk, unsigned int irep, unsigned int ihit, TMatrixT& smoothed_state, TMatrixT& smoothed_cov) { GFDetPlane pl; TMatrixT auxInfo; return GFTools::getBiasedSmoothedData(trk, irep, ihit, smoothed_state, smoothed_cov, pl, auxInfo); } bool GFTools::getSmoothedData(GFTrack* trk, unsigned int irep, unsigned int ihit, TMatrixT& smoothed_state, TMatrixT& smoothed_cov, GFDetPlane& smoothing_plane) { TMatrixT auxInfo; return GFTools::getSmoothedData(trk, irep, ihit, smoothed_state, smoothed_cov, smoothing_plane, auxInfo); } bool GFTools::getBiasedSmoothedData(GFTrack* trk, unsigned int irep, unsigned int ihit, TMatrixT& smoothed_state, TMatrixT& smoothed_cov, GFDetPlane& smoothing_plane) { TMatrixT auxInfo; return GFTools::getBiasedSmoothedData(trk, irep, ihit, smoothed_state, smoothed_cov, smoothing_plane, auxInfo); } bool GFTools::getSmoothedData(GFTrack* trk, unsigned int irep, unsigned int ihit, TMatrixT& smoothed_state, TMatrixT& smoothed_cov, GFDetPlane& smoothing_plane, TMatrixT& auxInfo) { if(!trk->getSmoothing()) { std::cout << "Trying to get smoothed hit coordinates from a track without smoothing! Aborting..." << std::endl; TMatrixT failed(1,1); return false; } if(ihit >= trk->getNumHits()) { std::cout << "Hit number out of bounds while trying to get smoothed hit coordinates! Aborting..." < failed(1,1); failed(0,0) = 0; return false; } TMatrixT fUpSt; TMatrixT fUpCov; TMatrixT fAuxInfo; TMatrixT* fAuxInfoP; TMatrixT bUpSt; TMatrixT bUpCov; TMatrixT bAuxInfo; TMatrixT* bAuxInfoP; GFDetPlane fPl; GFDetPlane bPl; GFAbsTrackRep* rep = trk->getTrackRep(irep)->clone(); if(trk->getTrackRep(irep)->hasAuxInfo()) { trk->getBK(irep)->getMatrix("fAuxInfo",ihit,auxInfo); } if(ihit == 0) { trk->getBK(irep)->getMatrix("bUpSt",ihit+1,bUpSt); trk->getBK(irep)->getMatrix("bUpCov",ihit+1,bUpCov); trk->getBK(irep)->getDetPlane("fPl",ihit,smoothing_plane); trk->getBK(irep)->getDetPlane("bPl",ihit+1,bPl); if(trk->getTrackRep(irep)->hasAuxInfo()) { trk->getBK(irep)->getMatrix("bAuxInfo",ihit+1,bAuxInfo); bAuxInfoP = &bAuxInfo; } else { bAuxInfoP = NULL; } if(bUpSt.GetNrows() == 0) return false; rep->setData(bUpSt,bPl,&bUpCov,bAuxInfoP); rep->extrapolate(smoothing_plane,smoothed_state,smoothed_cov); return true; } if(ihit == trk->getNumHits()-1) { trk->getBK(irep)->getMatrix("fUpSt",ihit-1,fUpSt); trk->getBK(irep)->getMatrix("fUpCov",ihit-1,fUpCov); trk->getBK(irep)->getDetPlane("fPl",ihit-1,fPl); trk->getBK(irep)->getDetPlane("fPl",ihit,smoothing_plane); if(trk->getTrackRep(irep)->hasAuxInfo()) { trk->getBK(irep)->getMatrix("fAuxInfo",ihit-1,fAuxInfo); fAuxInfoP = &fAuxInfo; } else { fAuxInfoP = NULL; } if(fUpSt.GetNrows() == 0) return false; rep->setData(fUpSt,fPl,&fUpCov,fAuxInfoP); rep->extrapolate(smoothing_plane,smoothed_state,smoothed_cov); return true; } trk->getBK(irep)->getMatrix("fUpSt",ihit-1,fUpSt); trk->getBK(irep)->getMatrix("fUpCov",ihit-1,fUpCov); trk->getBK(irep)->getMatrix("bUpSt",ihit+1,bUpSt); trk->getBK(irep)->getMatrix("bUpCov",ihit+1,bUpCov); if(trk->getTrackRep(irep)->hasAuxInfo()) { trk->getBK(irep)->getMatrix("fAuxInfo",ihit-1,fAuxInfo); trk->getBK(irep)->getMatrix("bAuxInfo",ihit+1,bAuxInfo); fAuxInfoP = &fAuxInfo; bAuxInfoP = &bAuxInfo; } else { fAuxInfoP = bAuxInfoP = NULL; } trk->getBK(irep)->getDetPlane("fPl",ihit-1,fPl); trk->getBK(irep)->getDetPlane("fPl",ihit,smoothing_plane); trk->getBK(irep)->getDetPlane("bPl",ihit+1,bPl); TMatrixT fSt; TMatrixT fCov; TMatrixT bSt; TMatrixT bCov; if(fUpSt.GetNrows() == 0 || bUpSt.GetNrows() == 0) return false; rep->setData(fUpSt,fPl,&fUpCov,fAuxInfoP); rep->extrapolate(smoothing_plane,fSt,fCov); rep->setData(bUpSt,bPl,&bUpCov,bAuxInfoP); rep->extrapolate(smoothing_plane,bSt,bCov); delete rep; TMatrixT fCovInvert; TMatrixT bCovInvert; GFTools::invertMatrix(fCov, fCovInvert); GFTools::invertMatrix(bCov, bCovInvert); GFTools::invertMatrix(fCovInvert + bCovInvert, smoothed_cov); smoothed_state.ResizeTo(smoothed_cov * (fCovInvert*fSt + bCovInvert*bSt)); smoothed_state = smoothed_cov * (fCovInvert*fSt + bCovInvert*bSt); return true; } bool GFTools::getBiasedSmoothedData(GFTrack* trk, unsigned int irep, unsigned int ihit, TMatrixT& smoothed_state, TMatrixT& smoothed_cov, GFDetPlane& smoothing_plane, TMatrixT& auxInfo) { if(!trk->getSmoothing()) { std::cout << "Trying to get smoothed hit coordinates from a track without smoothing! Aborting..." << std::endl; TMatrixT failed(1,1); return false; } if(ihit >= trk->getNumHits()) { std::cout << "Hit number out of bounds while trying to get smoothed hit coordinates! Aborting..." < failed(1,1); failed(0,0) = 0; return false; } TMatrixT bUpSt; TMatrixT bUpCov; TMatrixT bAuxInfo; TMatrixT* bAuxInfoP; GFDetPlane bPl; GFAbsTrackRep* rep = trk->getTrackRep(irep)->clone(); if(trk->getTrackRep(irep)->hasAuxInfo()) { trk->getBK(irep)->getMatrix("fAuxInfo",ihit,auxInfo); } if(ihit == 0) { trk->getBK(irep)->getMatrix("bUpSt",ihit,smoothed_state); trk->getBK(irep)->getMatrix("bUpCov",ihit,smoothed_cov); trk->getBK(irep)->getDetPlane("bPl",ihit,smoothing_plane); return true; } if(ihit == trk->getNumHits()-1) { trk->getBK(irep)->getMatrix("fUpSt",ihit,smoothed_state); trk->getBK(irep)->getMatrix("fUpCov",ihit,smoothed_cov); trk->getBK(irep)->getDetPlane("fPl",ihit,smoothing_plane); return true; } TMatrixT fSt; TMatrixT fCov; trk->getBK(irep)->getMatrix("fUpSt",ihit,fSt); trk->getBK(irep)->getMatrix("fUpCov",ihit,fCov); trk->getBK(irep)->getMatrix("bUpSt",ihit+1,bUpSt); trk->getBK(irep)->getMatrix("bUpCov",ihit+1,bUpCov); if(trk->getTrackRep(irep)->hasAuxInfo()) { trk->getBK(irep)->getMatrix("bAuxInfo",ihit+1,bAuxInfo); bAuxInfoP = &bAuxInfo; } else { bAuxInfoP = NULL; } trk->getBK(irep)->getDetPlane("fPl",ihit,smoothing_plane); trk->getBK(irep)->getDetPlane("bPl",ihit+1,bPl); TMatrixT bSt; TMatrixT bCov; if(bUpSt.GetNrows() == 0) return false; rep->setData(bUpSt,bPl,&bUpCov,bAuxInfoP); rep->extrapolate(smoothing_plane,bSt,bCov); delete rep; TMatrixT fCovInvert; TMatrixT bCovInvert; GFTools::invertMatrix(fCov, fCovInvert); GFTools::invertMatrix(bCov, bCovInvert); GFTools::invertMatrix(fCovInvert + bCovInvert, smoothed_cov); smoothed_state.ResizeTo(smoothed_cov * (fCovInvert*fSt + bCovInvert*bSt)); smoothed_state = smoothed_cov * (fCovInvert*fSt + bCovInvert*bSt); return true; } GFDetPlane GFTools::getSmoothingPlane(GFTrack* trk, unsigned int irep, unsigned int ihit) { GFDetPlane pl; trk->getBK(irep)->getDetPlane("fPl",ihit,pl); return pl; } /* void GFTools::invertMatrix(const TMatrixT& mat, TMatrixT& inv){ inv.ResizeTo(mat); // since ROOT has issues with absolute matrix entries that are below 10**-16 we force the 11 element to be one //double factor = 1./mat[0][0]; //inv = (factor*mat); inv = mat; inv.SetTol(1E-150); double det=0.; inv.Invert(&det); if(TMath::IsNaN(det)) { GFException e("GFTools::invertMatrix(): det of matrix is nan",__LINE__,__FILE__); e.setFatal(); throw e; } if(det==0){ GFException e("cannot invert matrix GFTools::invertMatrix() - det=0", __LINE__,__FILE__); e.setFatal(); throw e; } //recover multiplication with factor at the beginning //inv *= factor; } */ void GFTools::invertMatrix(const TMatrixT& mat, TMatrixT& inv){ inv.ResizeTo(mat); bool status = 0; TDecompSVD invertAlgo(mat); // check if numerical limits are reached (i.e at least one entry < 1E-100 and/or at least one entry > 1E100) if (!(mat<1.E100) || !(mat>-1.E100)){ GFException e("cannot invert matrix GFTools::invertMatrix(), entries too big (>1E100)", __LINE__,__FILE__); e.setFatal(); throw e; } invertAlgo.SetTol(1E-50); //this is a hack because a tolerance of 1E-22 does not make any sense for doubles only for floats inv = invertAlgo.Invert(status); if(status == 0){ GFException e("cannot invert matrix GFTools::invertMatrix(), status = 0", __LINE__,__FILE__); e.setFatal(); throw e; } } double GFTools::getSmoothedChiSqu(GFTrack* const trk, unsigned int irep, unsigned int ihit){ TMatrixT smoothed_state; TMatrixT smoothed_cov; GFTools::getBiasedSmoothedData(trk, irep, ihit, smoothed_state, smoothed_cov); TMatrixT H = trk->getHit(ihit)->getHMatrix(trk->getTrackRep(irep)); TMatrixT HT(TMatrixT::kTransposed, H); TMatrixT pos = H * smoothed_state; TMatrixT m = trk->getHit(ihit)->getRawHitCoord(); //measurement of hit TMatrixT V = trk->getHit(ihit)->getRawHitCov(); //covariance matrix of hit TMatrixT res = m - pos; TMatrixT resT(TMatrixT::kTransposed, res); TMatrixT R = V - H*smoothed_cov*HT; TMatrixT invR; invertMatrix(R,invR); TMatrixT smoothedChiSqu = resT*invR*res; return smoothedChiSqu[0][0]; }