29 KFbase::KFbase(
const Settings *settings,
const uint nHelixPar,
const string &fitterName,
const uint nMeas)
30 : TrackFitGeneric(settings, fitterName) {
31 nHelixPar_ = nHelixPar;
44 vector<Stub *> stubs = l1track3D.
stubs();
46 auto orderByLayer = [](
const Stub *
a,
const Stub *
b) {
return bool(
a->layerId() <
b->layerId()); };
47 sort(stubs.begin(), stubs.end(), orderByLayer);
50 const TP *tpa(
nullptr);
58 PrintL1trk() <<
"===============================================================================";
59 std::stringstream
text;
61 text <<
"Input track cand: [phiSec,etaReg]=[" << l1track3D.
iPhiSec() <<
"," << l1track3D.
iEtaReg() <<
"]";
63 <<
") q/pt=" << l1track3D.
qOverPt() <<
" tanL=" << l1track3D.
tanLambda() <<
" z0=" << l1track3D.
z0()
64 <<
" phi0=" << l1track3D.
phi0() <<
" nStubs=" << l1track3D.
numStubs() <<
" d0=" << l1track3D.
d0();
78 if (
cand !=
nullptr) {
103 double chi2rphi_bcon = 0.;
105 fitTrk.setBeamConstr(trackPars_bcon[
QOVERPT], trackPars_bcon[
PHI0], chi2rphi_bcon);
114 fitTrk.digitizeTrack(
"KF4ParamsComb");
116 if (!fitTrk.consistentSector()) {
118 PrintL1trk() <<
"Track rejected by sector consistency test";
129 bool goodTrack = (tpa && tpa->useForAlgEff());
131 int tpin = tpa->index();
132 PrintL1trk() <<
"TRACK LOST: eta=" << l1track3D.
iEtaReg() <<
" pt=" << l1track3D.
pt() <<
" tp=" << tpin;
134 for (
auto stub : stubs) {
136 this->
kalmanLayer(l1track3D.
iEtaReg(), stub->layerIdReduced(), stub->barrel(), stub->r(), stub->z());
137 std::stringstream
text;
139 text <<
" Stub: lay_red=" << stub->layerIdReduced() <<
" KFlay=" << kalmanLay <<
" r=" << stub->r()
140 <<
" z=" << stub->z() <<
" assoc TPs =";
141 for (
const TP *tp_i : stub->assocTPs())
142 text <<
" " << tp_i->index();
144 if (stub->assocTPs().empty())
153 if (tpa && tpa->useForAlgEff()) {
154 PrintL1trk() <<
"TP for eff. missed addr. index : " << tpa <<
" " << tpa->index();
169 map<unsigned int, const KalmanState *, std::greater<unsigned int>>
170 best_state_by_nstubs;
173 TVectorD x0 =
seedX(l1track3D);
174 TMatrixD pxx0 =
seedC(l1track3D);
178 const KalmanState *state0 =
mkState(l1track3D, 0, -1,
nullptr, x0, pxx0, K, dcov,
nullptr, 0, 0);
181 vector<const KalmanState *> new_states;
182 vector<const KalmanState *> prev_states;
183 prev_states.push_back(state0);
190 int etaReg = l1track3D.
iEtaReg();
191 map<int, vector<Stub *>> layerStubs;
193 for (
auto stub : stubs) {
195 int kalmanLay = this->
kalmanLayer(etaReg, stub->layerIdReduced(), stub->barrel(), stub->r(), stub->z());
197 constexpr
unsigned int invalidKFlayer = 7;
198 if (kalmanLay != invalidKFlayer) {
200 layerStubs[kalmanLay].push_back(stub);
203 layerStubs[kalmanLay].back() = stub;
209 constexpr
unsigned int nTypicalLayers = 6;
213 int combinations_per_iteration = 0;
216 unsigned int kalmanMaxSkipLayers =
220 vector<const KalmanState *>::const_iterator i_state = prev_states.begin();
221 for (; i_state != prev_states.end(); i_state++) {
231 unsigned nSkippedDeadLayers = 0;
232 unsigned nSkippedAmbiguousLayers = 0;
233 while (kfDeadLayers.find(
layer) != kfDeadLayers.end() && layerStubs[
layer].empty()) {
235 ++nSkippedDeadLayers;
239 ++nSkippedAmbiguousLayers;
243 vector<const KalmanState *> next_states;
244 vector<const KalmanState *> next_states_skipped;
248 vector<Stub *> thislay_stubs = layerStubs[
layer];
252 vector<Stub *> nextlay_stubs;
256 unsigned nSkippedDeadLayers_nextStubs = 0;
257 unsigned nSkippedAmbiguousLayers_nextStubs = 0;
258 if (nSkipped < kalmanMaxSkipLayers) {
259 if (kfDeadLayers.find(
layer + 1) != kfDeadLayers.end() && layerStubs[
layer + 1].empty()) {
260 nextlay_stubs = layerStubs[
layer + 2];
261 nSkippedDeadLayers_nextStubs++;
263 nextlay_stubs = layerStubs[
layer + 2];
264 nSkippedAmbiguousLayers_nextStubs++;
266 nextlay_stubs = layerStubs[
layer + 1];
273 nextlay_stubs.empty())
275 <<
" : #thislay_stubs=" << thislay_stubs.size() <<
" #nextlay_stubs=" << nextlay_stubs.size()
276 <<
" layer=" <<
layer <<
" eta=" << l1track3D.
iEtaReg();
279 nSkipped += nSkippedDeadLayers;
280 nSkipped += nSkippedAmbiguousLayers;
286 vector<Stub *> temp_thislaystubs;
287 vector<Stub *> temp_nextlaystubs;
288 for (
auto stub : thislay_stubs) {
289 if (stub->psModule())
290 temp_thislaystubs.push_back(stub);
292 for (
auto stub : nextlay_stubs) {
293 if (stub->psModule())
294 temp_nextlaystubs.push_back(stub);
296 thislay_stubs = temp_thislaystubs;
297 nextlay_stubs = temp_nextlaystubs;
300 combinations_per_iteration += thislay_stubs.size() + nextlay_stubs.size();
303 for (
unsigned i = 0;
i < thislay_stubs.size();
i++) {
304 Stub *stub = thislay_stubs[
i];
311 next_states.push_back(new_state);
315 for (
unsigned i = 0;
i < nextlay_stubs.size();
i++) {
316 Stub *stub = nextlay_stubs[
i];
319 kalmanUpdate(nSkipped + 1 + nSkippedDeadLayers_nextStubs + nSkippedAmbiguousLayers_nextStubs,
320 layer + 1 + nSkippedDeadLayers_nextStubs + nSkippedAmbiguousLayers_nextStubs,
326 next_states_skipped.push_back(new_state);
331 return bool(
a->chi2scaled() <
b->chi2scaled());
333 sort(next_states.begin(), next_states.end(), orderByChi2);
334 sort(next_states_skipped.begin(), next_states_skipped.end(), orderByChi2);
336 new_states.insert(new_states.end(), next_states.begin(), next_states.end());
337 new_states.insert(new_states.end(), next_states_skipped.begin(), next_states_skipped.end());
357 return bool((
a->chi2scaled()) * (
a->nSkippedLayers() + 1) < (
b->chi2scaled()) * (
b->nSkippedLayers() + 1));
359 sort(new_states.begin(), new_states.end(), orderByMinSkipChi2);
364 best_state_by_nstubs[nStubs] = new_states[0];
373 prev_states = new_states;
378 if (not best_state_by_nstubs.empty()) {
380 finished_state = best_state_by_nstubs.begin()->second;
382 std::stringstream
text;
384 text <<
"Track found! final state selection: nLay=" << finished_state->
nStubLayers()
386 <<
" phiSec=" << l1track3D.
iPhiSec() <<
" etaReg=" << l1track3D.
iEtaReg() <<
" HT(m,c)=("
389 text <<
" q/pt=" << y[
QOVERPT] <<
" tanL=" << y[
T] <<
" z0=" << y[
Z0] <<
" phi0=" << y[
PHI0];
392 text <<
" chosen from states:";
393 for (
const auto &
p : best_state_by_nstubs)
394 text <<
" " <<
p.second->chi2() <<
"/" <<
p.second->nStubLayers();
403 return finished_state;
420 TVectorD vecX =
state->vectorX();
421 TMatrixD matC =
state->matrixC();
424 PrintL1trk() <<
"STATE BARREL TO ENDCAP BEFORE ";
425 PrintL1trk() <<
"state : " << vecX[0] <<
" " << vecX[1] <<
" " << vecX[2] <<
" " << vecX[3];
430 PrintL1trk() <<
"STATE BARREL TO ENDCAP AFTER ";
431 PrintL1trk() <<
"state : " << vecX[0] <<
" " << vecX[1] <<
" " << vecX[2] <<
" " << vecX[3];
438 TMatrixD matFtrans(TMatrixD::kTransposed, matF);
445 TVectorD vecXref = matF * vecX;
474 TMatrixD matCref = matF * matC * matFtrans + matScat;
486 TMatrixD matRinv =
matrixRinv(matH, matCref, matV);
505 double new_chi2rphi = 0, new_chi2rz = 0;
510 PrintL1trk() <<
"adjusted x = " << new_vecX[0] <<
", " << new_vecX[1] <<
", " << new_vecX[2] <<
", "
513 PrintL1trk() <<
"adjusted x = " << new_vecX[0] <<
", " << new_vecX[1] <<
", " << new_vecX[2] <<
", "
514 << new_vecX[3] <<
", " << new_vecX[4];
517 PrintL1trk() <<
"adjust chi2rphi=" << new_chi2rphi <<
" chi2rz=" << new_chi2rz;
521 state->candidate(), nSkipped,
layer,
state, new_vecX, new_matC, matK, matV, stub, new_chi2rphi, new_chi2rz);
532 const TVectorD &vecX,
533 const TMatrixD &matC,
534 const TMatrixD &matK,
535 const TMatrixD &matV,
539 auto new_state = std::make_unique<const KalmanState>(
540 settings_, candidate, nSkipped,
layer, last_state, vecX, matC, matK, matV, stub, chi2rphi, chi2rz);
550 TMatrixD matHtrans(TMatrixD::kTransposed, matH);
551 return matH * matC * matHtrans;
556 TMatrixD
KFbase::matrixRinv(
const TMatrixD &matH,
const TMatrixD &matCref,
const TMatrixD &matV)
const {
558 TMatrixD matR = matV + matHCHt;
559 TMatrixD matRinv(2, 2);
560 if (matR.Determinant() > 0) {
561 matRinv = TMatrixD(TMatrixD::kInverted, matR);
565 const double big = 9.9e9;
566 matRinv =
big * unitMatrix;
580 TMatrixD matHtrans(TMatrixD::kTransposed, matH);
581 TMatrixD matCrefht = matCref * matHtrans;
582 TMatrixD matK = matCrefht * matRinv;
591 TVectorD hx =
h * vecX;
600 float tanL = vecX[
T];
606 double corr = stub->
r() * inv2R;
611 deltaS = (1. / 6.) * (stub->
r()) *
pow(
corr, 2);
617 float rShift = (stub->
z() -
z0) / tanL - stub->
r();
645 const TMatrixD &matCref,
646 const TVectorD &vecXref,
647 const TMatrixD &matH,
648 const TVectorD &
delta,
650 TMatrixD &new_matC)
const {
651 new_vecX = vecXref + matK *
delta;
653 TMatrixD
tmp = unitMatrix - matK * matH;
654 new_matC =
tmp * matCref;
660 const TMatrixD &matRinv,
661 const TVectorD &
delta,
663 double &chi2rz)
const {
669 PrintL1trk() <<
"delta(chi2rphi)=" << delChi2rphi <<
" delta(chi2rz)= " << delChi2rz;
671 chi2rphi =
state->chi2rphi() + delChi2rphi;
672 chi2rz =
state->chi2rz() + delChi2rz;
683 unsigned int iEtaReg,
unsigned int layerIDreduced,
bool barrel,
float r,
float z)
const {
692 const unsigned int nEta = 16;
693 const unsigned int nGPlayID = 7;
697 <<
"ERROR KFbase::getKalmanLayer hardwired value of nEta differs from NumEtaRegions cfg param";
701 constexpr
unsigned layerMap[
nEta / 2][nGPlayID + 1] = {
702 {7, 0, 1, 5, 4, 3, 7, 2},
703 {7, 0, 1, 5, 4, 3, 7, 2},
704 {7, 0, 1, 5, 4, 3, 7, 2},
705 {7, 0, 1, 5, 4, 3, 7, 2},
706 {7, 0, 1, 5, 4, 3, 7, 2},
707 {7, 0, 1, 3, 4, 2, 6, 2},
708 {7, 0, 1, 1, 2, 3, 4, 5},
709 {7, 0, 7, 1, 2, 3, 4, 5},
712 unsigned int kfEtaReg;
719 unsigned int kalmanLay = layerMap[kfEtaReg][layerIDreduced];
725 if (layerIDreduced == 6) {
730 if (layerIDreduced > 2) {
744 if (layerIDreduced == 3) {
746 }
else if (layerIDreduced == 4) {
748 }
else if (layerIDreduced == 5) {
754 if (layerIDreduced == 5) {
756 }
else if (layerIDreduced == 7) {
807 const unsigned int nEta = 16;
808 const unsigned int nKFlayer = 7;
809 constexpr
bool ambiguityMap[
nEta / 2][nKFlayer] = {
810 {
false,
false,
false,
false,
false,
false,
false},
811 {
false,
false,
false,
false,
false,
false,
false},
812 {
false,
false,
false,
false,
false,
false,
false},
813 {
false,
false,
false,
false,
false,
false,
false},
814 {
false,
false,
false,
false,
false,
false,
false},
815 {
false,
false,
true,
false,
false,
false,
false},
816 {
true,
true,
false,
false,
false,
false,
false},
817 {
true,
false,
false,
false,
false,
false,
false},
820 unsigned int kfEtaReg;
827 bool ambiguous =
false;
829 ambiguous = ambiguityMap[kfEtaReg][kfLayer];
844 set<pair<unsigned, bool>> deadGPlayers;
850 deadGPlayers.insert(pair<unsigned, bool>(4,
true));
853 if (iEtaReg_ <= 7 && iPhiSec_ >= 1 &&
iPhiSec_ <= 5) {
854 deadGPlayers.insert(pair<unsigned, bool>(1,
true));
858 if (iEtaReg_ <= 7 && iPhiSec_ >= 1 &&
iPhiSec_ <= 5) {
859 deadGPlayers.insert(pair<unsigned, bool>(1,
true));
862 deadGPlayers.insert(pair<unsigned, bool>(2,
true));
866 if (iEtaReg_ <= 7 && iPhiSec_ >= 1 &&
iPhiSec_ <= 5) {
867 deadGPlayers.insert(pair<unsigned, bool>(1,
true));
869 if (iEtaReg_ <= 3 && iPhiSec_ >= 1 &&
iPhiSec_ <= 5) {
870 deadGPlayers.insert(pair<unsigned, bool>(3,
false));
876 set<unsigned> kfDeadLayers;
877 for (
const auto &
p : deadGPlayers) {
878 unsigned int layer =
p.first;
883 kfDeadLayers.insert(kalmanLay);
898 TVectorD tpParams(5);
899 bool useForAlgEff(
false);
901 useForAlgEff =
tp->useForAlgEff();
903 tpParams[
PHI0] =
tp->phi0();
904 tpParams[
Z0] =
tp->z0();
905 tpParams[
T] =
tp->tanLambda();
906 tpParams[
D0] =
tp->d0();
908 std::stringstream
text;
911 text <<
" TP index = " <<
tp->index() <<
" useForAlgEff = " << useForAlgEff <<
" ";
912 const string helixNames[5] = {
"qOverPt",
"phi0",
"z0",
"tanL",
"d0"};
913 for (
int i = 0;
i < tpParams.GetNrows();
i++) {
914 text << helixNames[
i] <<
":" << tpParams[
i] <<
", ";
926 std::stringstream
text;
929 text <<
"stub layers = []\n";
931 text <<
"stub layers = [ ";
932 for (
unsigned i = 0;
i < stubs.size();
i++) {
933 text << stubs[
i]->layerId();
934 if (
i != stubs.size() - 1)
938 text <<
"KF stub layers = [ ";
939 for (
unsigned j = 0;
j < stubs.size();
j++) {
940 unsigned int kalmanLay =
943 if (
j != stubs.size() - 1)
954 std::stringstream
text;
957 text <<
"index=" << stub->
index() <<
" ";
959 text <<
"r=" << stub->
r() <<
" ";
960 text <<
"phi=" << stub->
phi() <<
" ";
961 text <<
"z=" << stub->
z() <<
" ";
965 std::set<const TP *> tps = stub->
assocTPs();
967 text <<
tp->index() <<
",";
974 for (
auto &stub : stubs) {