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++) {
224 unsigned int layer = the_state->
nextLayer();
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},
708 {7, 0, 1, 3, 4, 2, 6, 2},
711 {7, 0, 1, 1, 2, 3, 4, 5},
714 {7, 0, 7, 0, 1, 2, 3, 4},
717 unsigned int kfEtaReg;
724 unsigned int kalmanLay = layerMap[kfEtaReg][layerIDreduced];
731 if (layerIDreduced == 3) {
733 }
else if (layerIDreduced == 4) {
735 }
else if (layerIDreduced == 5) {
741 if (layerIDreduced == 5) {
743 }
else if (layerIDreduced == 7) {
819 bool ambiguous =
false;
833 set<pair<unsigned, bool>> deadGPlayers;
839 deadGPlayers.insert(pair<unsigned, bool>(4,
true));
842 if (iEtaReg_ <= 7 && iPhiSec_ >= 1 &&
iPhiSec_ <= 5) {
843 deadGPlayers.insert(pair<unsigned, bool>(1,
true));
847 if (iEtaReg_ <= 7 && iPhiSec_ >= 1 &&
iPhiSec_ <= 5) {
848 deadGPlayers.insert(pair<unsigned, bool>(1,
true));
851 deadGPlayers.insert(pair<unsigned, bool>(2,
true));
855 if (iEtaReg_ <= 7 && iPhiSec_ >= 1 &&
iPhiSec_ <= 5) {
856 deadGPlayers.insert(pair<unsigned, bool>(1,
true));
858 if (iEtaReg_ <= 3 && iPhiSec_ >= 1 &&
iPhiSec_ <= 5) {
859 deadGPlayers.insert(pair<unsigned, bool>(3,
false));
865 set<unsigned> kfDeadLayers;
866 for (
const auto &
p : deadGPlayers) {
867 unsigned int layer =
p.first;
872 kfDeadLayers.insert(kalmanLay);
887 TVectorD tpParams(5);
888 bool useForAlgEff(
false);
890 useForAlgEff =
tp->useForAlgEff();
892 tpParams[
PHI0] =
tp->phi0();
893 tpParams[
Z0] =
tp->z0();
894 tpParams[
T] =
tp->tanLambda();
895 tpParams[
D0] =
tp->d0();
897 std::stringstream
text;
900 text <<
" TP index = " <<
tp->index() <<
" useForAlgEff = " << useForAlgEff <<
" ";
901 const string helixNames[5] = {
"qOverPt",
"phi0",
"z0",
"tanL",
"d0"};
902 for (
int i = 0;
i < tpParams.GetNrows();
i++) {
903 text << helixNames[
i] <<
":" << tpParams[
i] <<
", ";
915 std::stringstream
text;
918 text <<
"stub layers = []\n";
920 text <<
"stub layers = [ ";
921 for (
unsigned i = 0;
i < stubs.size();
i++) {
922 text << stubs[
i]->layerId();
923 if (
i != stubs.size() - 1)
927 text <<
"KF stub layers = [ ";
928 for (
unsigned j = 0;
j < stubs.size();
j++) {
929 unsigned int kalmanLay =
932 if (
j != stubs.size() - 1)
943 std::stringstream
text;
946 text <<
"index=" << stub->
index() <<
" ";
948 text <<
"r=" << stub->
r() <<
" ";
949 text <<
"phi=" << stub->
phi() <<
" ";
950 text <<
"z=" << stub->
z() <<
" ";
954 std::set<const TP *> tps = stub->
assocTPs();
956 text <<
tp->index() <<
",";
963 for (
auto &stub : stubs) {