-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMkFinder.cc
1751 lines (1457 loc) · 60.9 KB
/
MkFinder.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "MkFinder.h"
#include "CandCloner.h"
#include "HitStructures.h"
#include "IterationConfig.h"
#include "FindingFoos.h"
#include "KalmanUtilsMPlex.h"
#include "MatriplexPackers.h"
//#define DEBUG
#include "Debug.h"
//#ifdef DEBUG_BACKWARD_FIT
//#include "Event.h"
//#endif
#ifdef DUMPHITWINDOW
#include "Event.h"
#endif
namespace mkfit {
void MkFinder::Setup(const IterationParams &ip, const IterationLayerConfig &ilc, const std::vector<bool> *ihm)
{
m_iteration_params = &ip;
m_iteration_layer_config = &ilc;
m_iteration_hit_mask = ihm;
}
void MkFinder::Release()
{
m_iteration_params = nullptr;
m_iteration_layer_config = nullptr;
m_iteration_hit_mask = nullptr;
}
//==============================================================================
// Input / Output TracksAndHitIdx
//==============================================================================
void MkFinder::InputTracksAndHitIdx(const std::vector<Track>& tracks,
int beg, int end, bool inputProp)
{
// Assign track parameters to initial state and copy hit values in.
// This might not be true for the last chunk!
// assert(end - beg == NN);
const int iI = inputProp ? iP : iC;
for (int i = beg, imp = 0; i < end; ++i, ++imp)
{
copy_in(tracks[i], imp, iI);
}
}
void MkFinder::InputTracksAndHitIdx(const std::vector<Track>& tracks,
const std::vector<int> & idxs,
int beg, int end, bool inputProp, int mp_offset)
{
// Assign track parameters to initial state and copy hit values in.
// This might not be true for the last chunk!
// assert(end - beg == NN);
const int iI = inputProp ? iP : iC;
for (int i = beg, imp = mp_offset; i < end; ++i, ++imp)
{
copy_in(tracks[idxs[i]], imp, iI);
}
}
void MkFinder::InputTracksAndHitIdx(const std::vector<CombCandidate> & tracks,
const std::vector<std::pair<int,int>>& idxs,
int beg, int end, bool inputProp)
{
// Assign track parameters to initial state and copy hit values in.
// This might not be true for the last chunk!
// assert(end - beg == NN);
const int iI = inputProp ? iP : iC;
for (int i = beg, imp = 0; i < end; ++i, ++imp)
{
const TrackCand &trk = tracks[idxs[i].first][idxs[i].second];
copy_in(trk, imp, iI);
#ifdef DUMPHITWINDOW
SeedAlgo(imp, 0, 0) = tracks[idxs[i].first].m_seed_algo;
SeedLabel(imp, 0, 0) = tracks[idxs[i].first].m_seed_label;
#endif
SeedIdx(imp, 0, 0) = idxs[i].first;
CandIdx(imp, 0, 0) = idxs[i].second;
}
}
void MkFinder::InputTracksAndHitIdx(const std::vector<CombCandidate> & tracks,
const std::vector<std::pair<int,IdxChi2List>>& idxs,
int beg, int end, bool inputProp)
{
// Assign track parameters to initial state and copy hit values in.
// This might not be true for the last chunk!
// assert(end - beg == NN);
const int iI = inputProp ? iP : iC;
for (int i = beg, imp = 0; i < end; ++i, ++imp)
{
const TrackCand &trk = tracks[idxs[i].first][idxs[i].second.trkIdx];
copy_in(trk, imp, iI);
#ifdef DUMPHITWINDOW
SeedAlgo(imp, 0, 0) = tracks[idxs[i].first].m_seed_algo;
SeedLabel(imp, 0, 0) = tracks[idxs[i].first].m_seed_label;
#endif
SeedIdx(imp, 0, 0) = idxs[i].first;
CandIdx(imp, 0, 0) = idxs[i].second.trkIdx;
}
}
void MkFinder::OutputTracksAndHitIdx(std::vector<Track>& tracks,
int beg, int end, bool outputProp) const
{
// Copies requested track parameters into Track objects.
// The tracks vector should be resized to allow direct copying.
const int iO = outputProp ? iP : iC;
for (int i = beg, imp = 0; i < end; ++i, ++imp)
{
copy_out(tracks[i], imp, iO);
}
}
void MkFinder::OutputTracksAndHitIdx(std::vector<Track>& tracks,
const std::vector<int>& idxs,
int beg, int end, bool outputProp) const
{
// Copies requested track parameters into Track objects.
// The tracks vector should be resized to allow direct copying.
const int iO = outputProp ? iP : iC;
for (int i = beg, imp = 0; i < end; ++i, ++imp)
{
copy_out(tracks[idxs[i]], imp, iO);
}
}
//==============================================================================
// getHitSelDynamicWindows
//==============================================================================
// From HitSelectionWindows.h: track-related config on hit selection windows
void MkFinder::getHitSelDynamicWindows(const float invpt, const float theta, float &min_dq, float &max_dq, float &min_dphi, float &max_dphi)
{
const IterationLayerConfig &ILC = *m_iteration_layer_config;
float max_invpt=invpt;
if(invpt>10.0) max_invpt=10.0; // => pT>0.1 GeV
// dq hit selection window
float this_dq = (ILC.c_dq_0)*max_invpt+(ILC.c_dq_1)*theta+(ILC.c_dq_2);
// In case layer is missing (e.g., seeding layers, or too low stats for training), leave original limits
if((ILC.c_dq_sf)*this_dq>0.f){
min_dq = (ILC.c_dq_sf)*this_dq;
max_dq = 2.0f*min_dq;
}
// dphi hit selection window
float this_dphi = (ILC.c_dp_0)*max_invpt+(ILC.c_dp_1)*theta+(ILC.c_dp_2);
// In case layer is missing (e.g., seeding layers, or too low stats for training), leave original limits
if((ILC.c_dp_sf)*this_dphi>min_dphi){
min_dphi = (ILC.c_dp_sf)*this_dphi;
max_dphi = 2.0f*min_dphi;
}
//// For future optimization: for layer & iteration dependend hit chi2 cut
//float this_c2 = (ILC.c_c2_0)*invpt+(ILC.c_c2_1)*theta+(ILC.c_c2_2);
//// In case layer is missing (e.g., seeding layers, or too low stats for training), leave original limits
//if(this_c2>0.f){
// max_c2 = (ILC.c_c2_sf)*this_c2;
//}
}
//==============================================================================
// getHitSelDynamicChi2Cut
//==============================================================================
// From HitSelectionWindows.h: track-related config on hit selection windows
inline float MkFinder::getHitSelDynamicChi2Cut(const int itrk, const int ipar)
{
const IterationLayerConfig &ILC = *m_iteration_layer_config;
const float minChi2Cut = m_iteration_params->chi2Cut_min;
const float invpt = Par[ipar].At(itrk,3,0);
const float theta = std::abs(Par[ipar].At(itrk,5,0) - Config::PIOver2);
float max_invpt = invpt;
if (invpt>10.0) max_invpt=10.0;
float this_c2 = ILC.c_c2_0*max_invpt + ILC.c_c2_1*theta + ILC.c_c2_2;
// In case layer is missing (e.g., seeding layers, or too low stats for training), leave original limits
if ((ILC.c_c2_sf)*this_c2 > minChi2Cut)
return ILC.c_c2_sf*this_c2;
else
return minChi2Cut;
}
//==============================================================================
// SelectHitIndices
//==============================================================================
void MkFinder::SelectHitIndices(const LayerOfHits &layer_of_hits,
const int N_proc)
{
// bool debug = true;
const LayerOfHits &L = layer_of_hits;
const IterationLayerConfig &ILC = *m_iteration_layer_config;
const int iI = iP;
const float nSigmaPhi = 3;
const float nSigmaZ = 3;
const float nSigmaR = 3;
dprintf("LayerOfHits::SelectHitIndices %s layer=%d N_proc=%d\n",
L.is_barrel() ? "barrel" : "endcap", L.layer_id(), N_proc);
float dqv[NN], dphiv[NN], qv[NN], phiv[NN];
int qb1v[NN], qb2v[NN], qbv[NN], pb1v[NN], pb2v[NN];
const auto assignbins = [&](int itrack, float q, float dq, float phi, float dphi, float min_dq, float max_dq, float min_dphi, float max_dphi){
dphi = clamp(std::abs(dphi), min_dphi, max_dphi);
dq = clamp(dq, min_dq, max_dq);
//
qv[itrack] = q;
phiv[itrack] = phi;
dphiv[itrack] = dphi;
dqv[itrack] = dq;
//
qbv [itrack] = L.GetQBinChecked(q);
qb1v[itrack] = L.GetQBinChecked(q - dq);
qb2v[itrack] = L.GetQBinChecked(q + dq) + 1;
pb1v[itrack] = L.GetPhiBin(phi - dphi);
pb2v[itrack] = L.GetPhiBin(phi + dphi) + 1;
};
const auto calcdphi2 = [&](int itrack, float dphidx, float dphidy) {
return dphidx * dphidx * Err[iI].ConstAt(itrack, 0, 0) +
dphidy * dphidy * Err[iI].ConstAt(itrack, 1, 1) +
2 * dphidx * dphidy * Err[iI].ConstAt(itrack, 0, 1);
};
const auto calcdphi = [&](float dphi2, float min_dphi) {
return std::max(nSigmaPhi * std::sqrt(std::abs(dphi2)), min_dphi);
};
float min_dq = ILC.min_dq();
float max_dq = ILC.max_dq();
float min_dphi = ILC.min_dphi();
float max_dphi = ILC.max_dphi();
if (L.is_barrel())
{
// Pull out the part of the loop that vectorizes
//#pragma ivdep
#pragma omp simd
for (int itrack = 0; itrack < NN; ++itrack)
{
XHitSize[itrack] = 0;
min_dq = ILC.min_dq();
max_dq = ILC.max_dq();
min_dphi = ILC.min_dphi();
max_dphi = ILC.max_dphi();
const float invpt = Par[iI].At(itrack,3,0);
const float theta = std::fabs(Par[iI].At(itrack,5,0)-Config::PIOver2);
getHitSelDynamicWindows(invpt, theta, min_dq, max_dq, min_dphi, max_dphi);
const float x = Par[iI].ConstAt(itrack, 0, 0);
const float y = Par[iI].ConstAt(itrack, 1, 0);
const float r2 = x*x + y*y;
const float dphidx = -y/r2, dphidy = x/r2;
const float dphi2 = calcdphi2(itrack, dphidx, dphidy);
#ifdef HARD_CHECK
assert(dphi2 >= 0);
#endif
const float phi = getPhi(x, y);
float dphi = calcdphi(dphi2, min_dphi);
const float z = Par[iI].ConstAt(itrack, 2, 0);
const float dz = std::abs(nSigmaZ * std::sqrt(Err[iI].ConstAt(itrack, 2, 2)));
const float edgeCorr = std::abs(0.5f*(L.m_layer_info->m_rout-L.m_layer_info->m_rin)/std::tan(Par[iI].ConstAt(itrack, 5, 0)));
// XXX-NUM-ERR above, Err(2,2) gets negative!
////// Disable correction
//if (Config::useCMSGeom) // should be Config::finding_requires_propagation_to_hit_pos
//{
// //now correct for bending and for layer thickness unsing linear approximation
// //fixme! using constant value, to be taken from layer properties
// //XXXXMT4GC should we also increase dz?
// //XXXXMT4GC an we just take half od layer dR?
// const float deltaR = Config::cmsDeltaRad;
// const float r = std::sqrt(r2);
// //here alpha is the difference between posPhi and momPhi
// const float alpha = phi - Par[iP].ConstAt(itrack, 4, 0);
// float cosA, sinA;
// if (Config::useTrigApprox) {
// sincos4(alpha, sinA, cosA);
// } else {
// cosA = std::cos(alpha);
// sinA = std::sin(alpha);
// }
// //take abs so that we always inflate the window
// const float dist = std::abs(deltaR*sinA/cosA);
// dphi += dist / r;
//}
XWsrResult[itrack] = L.is_within_z_sensitive_region(z, std::sqrt(dz*dz + edgeCorr*edgeCorr) );
assignbins(itrack, z, dz, phi, dphi, min_dq, max_dq, min_dphi, max_dphi);
}
}
else // endcap
{
// Pull out the part of the loop that vectorizes
//#pragma ivdep
#pragma omp simd
for (int itrack = 0; itrack < NN; ++itrack)
{
XHitSize[itrack] = 0;
min_dq = ILC.min_dq();
max_dq = ILC.max_dq();
min_dphi = ILC.min_dphi();
max_dphi = ILC.max_dphi();
const float invpt = Par[iI].At(itrack,3,0);
const float theta = std::fabs(Par[iI].At(itrack,5,0)-Config::PIOver2);
getHitSelDynamicWindows(invpt, theta, min_dq, max_dq, min_dphi, max_dphi);
const float x = Par[iI].ConstAt(itrack, 0, 0);
const float y = Par[iI].ConstAt(itrack, 1, 0);
const float r2 = x*x + y*y;
const float dphidx = -y/r2, dphidy = x/r2;
const float dphi2 = calcdphi2(itrack, dphidx, dphidy);
#ifdef HARD_CHECK
assert(dphi2 >= 0);
#endif
const float phi = getPhi(x, y);
float dphi = calcdphi(dphi2, min_dphi);
const float r = std::sqrt(r2);
const float dr = nSigmaR*std::sqrt(std::abs(x*x*Err[iI].ConstAt(itrack, 0, 0) + y*y*Err[iI].ConstAt(itrack, 1, 1) + 2*x*y*Err[iI].ConstAt(itrack, 0, 1)) / r2);
const float edgeCorr = std::abs(0.5f*(L.m_layer_info->m_zmax-L.m_layer_info->m_zmin)*std::tan(Par[iI].ConstAt(itrack, 5, 0)));
////// Disable correction
//if (Config::useCMSGeom) // should be Config::finding_requires_propagation_to_hit_pos
//{
// //now correct for bending and for layer thickness unsing linear approximation
// //fixme! using constant value, to be taken from layer properties
// //XXXXMT4GC should we also increase dr?
// //XXXXMT4GC can we just take half of layer dz?
// const float deltaZ = 5;
// float cosT = std::cos(Par[iI].ConstAt(itrack, 5, 0));
// float sinT = std::sin(Par[iI].ConstAt(itrack, 5, 0));
// //here alpha is the helix angular path corresponding to deltaZ
// const float k = Chg.ConstAt(itrack, 0, 0) * 100.f / (-Config::sol*Config::Bfield);
// const float alpha = deltaZ*sinT*Par[iI].ConstAt(itrack, 3, 0)/(cosT*k);
// dphi += std::abs(alpha);
//}
XWsrResult[itrack] = L.is_within_r_sensitive_region(r, std::sqrt(dr*dr + edgeCorr*edgeCorr) );
assignbins(itrack, r, dr, phi, dphi, min_dq, max_dq, min_dphi, max_dphi);
}
}
// Vectorizing this makes it run slower!
//#pragma ivdep
//#pragma omp simd
for (int itrack = 0; itrack < N_proc; ++itrack)
{
if (XWsrResult[itrack].m_wsr == WSR_Outside)
{
XHitSize[itrack] = -1;
continue;
}
const int qb = qbv [itrack];
const int qb1 = qb1v[itrack];
const int qb2 = qb2v[itrack];
const int pb1 = pb1v[itrack];
const int pb2 = pb2v[itrack];
// Used only by usePhiQArrays
const float q = qv[itrack];
const float phi = phiv[itrack];
const float dphi = dphiv[itrack];
const float dq = dqv[itrack];
dprintf(" %2d/%2d: %6.3f %6.3f %6.6f %7.5f %3d %3d %4d %4d\n",
L.layer_id(), itrack, q, phi, dq, dphi, qb1, qb2, pb1, pb2);
// MT: One could iterate in "spiral" order, to pick hits close to the center.
// http://stackoverflow.com/questions/398299/looping-in-a-spiral
// This would then work best with relatively small bin sizes.
// Or, set them up so I can always take 3x3 array around the intersection.
#ifdef DUMPHITWINDOW
{
int thisseedmcid=-999999;
int seedlabel = SeedLabel(itrack, 0, 0);
TrackVec & seedtracks = m_event->seedTracks_;
int thisidx = -999999;
for (int i = 0; i < seedtracks.size(); ++i){
auto & thisseed = seedtracks[i];
if(thisseed.label()==seedlabel){
thisidx = i;
break;
}
}
if(thisidx>-999999){
auto & seedtrack = m_event->seedTracks_[thisidx];
std::vector<int> thismcHitIDs;
seedtrack.mcHitIDsVec(m_event->layerHits_,m_event->simHitsInfo_,thismcHitIDs);
if ( std::adjacent_find( thismcHitIDs.begin(), thismcHitIDs.end(), std::not_equal_to<>() ) == thismcHitIDs.end() ){
thisseedmcid=thismcHitIDs.at(0);
}
}
}
#endif
for (int qi = qb1; qi < qb2; ++qi)
{
for (int pi = pb1; pi < pb2; ++pi)
{
const int pb = pi & L.m_phi_mask;
// Limit to central Q-bin
if (qi == qb && L.m_phi_bin_deads[qi][pb] == true)
{
//std::cout << "dead module for track in layer=" << L.layer_id() << " qb=" << qi << " pb=" << pb << " q=" << q << " phi=" << phi<< std::endl;
XWsrResult[itrack].m_in_gap = true;
}
// MT: The following line is the biggest hog (4% total run time).
// This comes from cache misses, I presume.
// It might make sense to make first loop to extract bin indices
// and issue prefetches at the same time.
// Then enter vectorized loop to actually collect the hits in proper order.
//SK: ~20x1024 bin sizes give mostly 1 hit per bin. Commented out for 128 bins or less
// #pragma nounroll
for (uint16_t hi = L.m_phi_bin_infos[qi][pb].first; hi < L.m_phi_bin_infos[qi][pb].second; ++hi)
{
// MT: Access into m_hit_zs and m_hit_phis is 1% run-time each.
int hi_orig = L.GetOriginalHitIndex(hi);
if (m_iteration_hit_mask && (*m_iteration_hit_mask)[hi_orig])
{
// printf("Yay, denying masked hit on layer %d, hi %d, orig idx %d\n",
// L.m_layer_info->m_layer_id, hi, hi_orig);
continue;
}
if (Config::usePhiQArrays)
{
if (XHitSize[itrack] >= MPlexHitIdxMax)
break;
const float ddq = std::abs(q - L.m_hit_qs[hi]);
const float ddphi = cdist(std::abs(phi - L.m_hit_phis[hi]));
#ifdef DUMPHITWINDOW
{
const MCHitInfo &mchinfo = m_event->simHitsInfo_[L.GetHit(hi).mcHitID()];
int mchid = mchinfo.mcTrackID();
int st_isfindable=0;
int st_label=-999999;
int st_prodtype=0;
int st_nhits=-1;
int st_charge=0;
float st_r = -999.;
float st_z = -999.;
float st_pt =-999.;
float st_eta=-999.;
float st_phi=-999.;
if (mchid >=0){
Track simtrack = m_event->simTracks_[mchid];
st_isfindable = (int) simtrack.isFindable();
st_label = simtrack.label();
st_prodtype = (int) simtrack.prodType();
st_pt = simtrack.pT();
st_eta = simtrack.momEta();
st_phi = simtrack.momPhi();
st_nhits = simtrack.nTotalHits();
st_charge = simtrack.charge();
st_r = simtrack.posR();
st_z = simtrack.z();
}
const Hit &thishit=L.GetHit(hi);
msErr.CopyIn(itrack, thishit.errArray());
msPar.CopyIn(itrack, thishit.posArray());
MPlexQF thisOutChi2;
MPlexLV tmpPropPar;
const FindingFoos &fnd_foos = FindingFoos::get_finding_foos(L.is_barrel());
(*fnd_foos.m_compute_chi2_foo)(Err[iI], Par[iI], Chg, msErr, msPar,
thisOutChi2, tmpPropPar, N_proc, Config::finding_intra_layer_pflags);
float hx = thishit.x();
float hy = thishit.y();
float hz = thishit.z();
float hr = std::hypot(hx, hy);
float hphi = std::atan2(hy, hx);
float hex = std::sqrt(thishit.exx());
if(std::isnan(hex))
hex = -999.;
float hey = std::sqrt(thishit.eyy());
if(std::isnan(hey))
hey = -999.;
float hez = std::sqrt(thishit.ezz());
if(std::isnan(hez))
hez = -999.;
float her = std::sqrt((hx*hx*thishit.exx() + hy*hy*thishit.eyy() + 2.0f*hx*hy*msErr.At(itrack,0,1)) / (hr*hr));
if(std::isnan(her))
her = -999.;
float hephi = std::sqrt(thishit.ephi());
if(std::isnan(hephi))
hephi = -999.;
float hchi2 = thisOutChi2[itrack];
if(std::isnan(hchi2))
hchi2 = -999.;
float tx = Par[iI].At(itrack,0,0);
float ty = Par[iI].At(itrack,1,0);
float tz = Par[iI].At(itrack,2,0);
float tr = std::hypot(tx, ty);
float tphi = std::atan2(ty, tx);
float tchi2 = Chi2(itrack, 0, 0);
if(std::isnan(tchi2))
tchi2 = -999.;
float tex = std::sqrt(Err[iI].At(itrack,0,0));
if(std::isnan(tex))
tex = -999.;
float tey = std::sqrt(Err[iI].At(itrack,1,1));
if(std::isnan(tey))
tey = -999.;
float tez = std::sqrt(Err[iI].At(itrack,2,2));
if(std::isnan(tez))
tez = -999.;
float ter = std::sqrt((tx*tx*tex*tex + ty*ty*tey*tey + 2.0f*tx*ty*Err[iI].At(itrack,0,1)) / (tr*tr));
if(std::isnan(ter))
ter = -999.;
float tephi = std::sqrt((ty*ty*tex*tex + tx*tx*tey*tey - 2.0f*tx*ty*Err[iI].At(itrack,0,1))/(tr*tr*tr*tr));
if(std::isnan(tephi))
tephi = -999.;
float ht_dxy= std::hypot(hx-tx, hy-ty);
float ht_dz = hz-tz;
float ht_dphi= cdist(std::abs(hphi - tphi));
static bool first = true;
if (first)
{
printf("HITWINDOWSEL "
"evt_id/I:"
"lyr_id/I:lyr_isbrl/I:hit_idx/I:"
"trk_cnt/I:trk_idx/I:trk_label/I:"
"trk_pt/F:trk_eta/F:trk_mphi/F:trk_chi2/F:"
"nhits/I:"
"seed_idx/I:seed_label/I:seed_algo/I:seed_mcid/I:"
"hit_mcid/I:"
"st_isfindable/I:st_prodtype/I:st_label/I:"
"st_pt/F:st_eta/F:st_phi/F:"
"st_nhits/I:st_charge/I:st_r/F:st_z/F:"
"trk_q/F:hit_q/F:dq_trkhit/F:dq_cut/F:trk_phi/F:hit_phi/F:dphi_trkhit/F:dphi_cut/F:"
"t_x/F:t_y/F:t_r/F:t_phi/F:t_z/F:"
"t_ex/F:t_ey/F:t_er/F:t_ephi/F:t_ez/F:"
"h_x/F:h_y/F:h_r/F:h_phi/F:h_z/F:"
"h_ex/F:h_ey/F:h_er/F:h_ephi/F:h_ez/F:"
"ht_dxy/F:ht_dz/F:ht_dphi/F:"
"h_chi2/F"
"\n");
first = false;
}
if(!(std::isnan(phi)) && !(std::isnan(getEta(Par[iI].At(itrack,5,0)))))
{
//|| std::isnan(ter) || std::isnan(her) || std::isnan(Chi2(itrack, 0, 0)) || std::isnan(hchi2)))
printf("HITWINDOWSEL "
"%d "
"%d %d %d "
"%d %d %d "
"%6.3f %6.3f %6.3f %6.3f "
"%d "
"%d %d %d %d "
"%d "
"%d %d %d "
"%6.3f %6.3f %6.3f "
"%d %d %6.3f %6.3f "
"%6.3f %6.3f %6.3f %6.3f %6.3f %6.3f %6.3f %6.3f "
"%6.3f %6.3f %6.3f %6.3f %6.3f "
"%6.6f %6.6f %6.6f %6.6f %6.6f "
"%6.3f %6.3f %6.3f %6.3f %6.3f "
"%6.6f %6.6f %6.6f %6.6f %6.6f "
"%6.3f %6.3f %6.3f "
"%6.3f"
"\n",
m_event->evtID(),
L.layer_id(), L.is_barrel(), L.GetOriginalHitIndex(hi),
itrack, CandIdx(itrack, 0, 0), Label(itrack, 0, 0),
1.0f/Par[iI].At(itrack,3,0), getEta(Par[iI].At(itrack,5,0)), Par[iI].At(itrack,4,0), Chi2(itrack, 0, 0),
NFoundHits(itrack, 0, 0),
SeedIdx(itrack, 0, 0), SeedLabel(itrack, 0, 0), SeedAlgo(itrack, 0, 0), thisseedmcid,
mchid,
st_isfindable, st_prodtype, st_label,
st_pt, st_eta, st_phi,
st_nhits, st_charge, st_r, st_z,
q, L.m_hit_qs[hi], ddq, dq, phi, L.m_hit_phis[hi], ddphi, dphi,
tx, ty, tr, tphi, tz,
tex, tey, ter, tephi, tez,
hx, hy, hr, hphi, hz,
hex, hey, her, hephi, hez,
ht_dxy, ht_dz, ht_dphi,
hchi2);
}
}
#endif
if (ddq >= dq)
continue;
if (ddphi >= dphi)
continue;
// dprintf(" SHI %3d %4d %4d %5d %6.3f %6.3f %6.4f %7.5f %s\n",
// qi, pi, pb, hi,
// L.m_hit_qs[hi], L.m_hit_phis[hi], ddq, ddphi,
// (ddq < dq && ddphi < dphi) ? "PASS" : "FAIL");
// MT: Removing extra check gives full efficiency ...
// and means our error estimations are wrong!
// Avi says we should have *minimal* search windows per layer.
// Also ... if bins are sufficiently small, we do not need the extra
// checks, see above.
XHitArr.At(itrack, XHitSize[itrack]++, 0) = hi_orig;
}
else
{
// MT: The following check alone makes more sense with spiral traversal,
// we'd be taking in closest hits first.
// Hmmh -- there used to be some more checks here.
// Or, at least, the phi binning was much smaller and no further checks were done.
assert(false && "this code has not been used in a while -- see comments in code");
if (XHitSize[itrack] < MPlexHitIdxMax)
{
XHitArr.At(itrack, XHitSize[itrack]++, 0) = hi_orig;
}
}
} //hi
} //pi
} //qi
} //itrack
}
//==============================================================================
// AddBestHit - Best Hit Track Finding
//==============================================================================
void MkFinder::AddBestHit(const LayerOfHits &layer_of_hits, const int N_proc,
const FindingFoos &fnd_foos)
{
// debug = true;
MatriplexHitPacker mhp(* layer_of_hits.GetHitArray());
float minChi2[NN];
int bestHit[NN];
// MT: fill_n gave me crap on MIC, NN=8,16, doing in maxSize search below.
// Must be a compiler issue.
// std::fill_n(minChi2, NN, m_iteration_params->chi2Cut_min);
// std::fill_n(bestHit, NN, -1);
int maxSize = 0;
// Determine maximum number of hits for tracks in the collection.
for (int it = 0; it < NN; ++it)
{
if (it < N_proc)
{
if (XHitSize[it] > 0)
{
maxSize = std::max(maxSize, XHitSize[it]);
}
}
bestHit[it] = -1;
minChi2[it] = getHitSelDynamicChi2Cut(it, iP);
}
for (int hit_cnt = 0; hit_cnt < maxSize; ++hit_cnt)
{
//fixme what if size is zero???
mhp.Reset();
#pragma omp simd
for (int itrack = 0; itrack < N_proc; ++itrack)
{
if (hit_cnt < XHitSize[itrack])
{
mhp.AddInputAt(itrack, layer_of_hits.GetHit(XHitArr.At(itrack, hit_cnt, 0)));
}
}
mhp.Pack(msErr, msPar);
//now compute the chi2 of track state vs hit
MPlexQF outChi2;
MPlexLV tmpPropPar;
(*fnd_foos.m_compute_chi2_foo)(Err[iP], Par[iP], Chg, msErr, msPar,
outChi2, tmpPropPar, N_proc, Config::finding_intra_layer_pflags);
//update best hit in case chi2<minChi2
#pragma omp simd
for (int itrack = 0; itrack < N_proc; ++itrack)
{
if (hit_cnt < XHitSize[itrack])
{
const float chi2 = std::abs(outChi2[itrack]);//fixme negative chi2 sometimes...
dprint("chi2=" << chi2 << " minChi2[itrack]=" << minChi2[itrack]);
if (chi2 < minChi2[itrack])
{
minChi2[itrack] = chi2;
bestHit[itrack] = XHitArr.At(itrack, hit_cnt, 0);
}
}
}
} // end loop over hits
//#pragma omp simd
for (int itrack = 0; itrack < N_proc; ++itrack)
{
if (XWsrResult[itrack].m_wsr == WSR_Outside)
{
// Why am I doing this?
msErr.SetDiagonal3x3(itrack, 666);
msPar(itrack,0,0) = Par[iP](itrack,0,0);
msPar(itrack,1,0) = Par[iP](itrack,1,0);
msPar(itrack,2,0) = Par[iP](itrack,2,0);
// XXXX If not in gap, should get back the old track params. But they are gone ...
// Would actually have to do it right after SelectHitIndices where updated params are still ok.
// Here they got screwed during hit matching.
// So, I'd store them there (into propagated params) and retrieve them here.
// Or we decide not to care ...
continue;
}
//fixme decide what to do in case no hit found
if (bestHit[itrack] >= 0)
{
const Hit &hit = layer_of_hits.GetHit( bestHit[itrack] );
const float chi2 = minChi2[itrack];
dprint("ADD BEST HIT FOR TRACK #" << itrack << std::endl
<< "prop x=" << Par[iP].ConstAt(itrack, 0, 0) << " y=" << Par[iP].ConstAt(itrack, 1, 0) << std::endl
<< "copy in hit #" << bestHit[itrack] << " x=" << hit.position()[0] << " y=" << hit.position()[1]);
msErr.CopyIn(itrack, hit.errArray());
msPar.CopyIn(itrack, hit.posArray());
Chi2(itrack, 0, 0) += chi2;
add_hit(itrack, bestHit[itrack], layer_of_hits.layer_id());
}
else
{
int fake_hit_idx = -1;
if (XWsrResult[itrack].m_wsr == WSR_Edge)
{
// YYYYYY Config::store_missed_layers
fake_hit_idx = -3;
}
else if (num_all_minus_one_hits(itrack))
{
fake_hit_idx = -2;
}
dprint("ADD FAKE HIT FOR TRACK #" << itrack << " withinBounds=" << (fake_hit_idx != -3) << " r=" << std::hypot(Par[iP](itrack,0,0), Par[iP](itrack,1,0)));
msErr.SetDiagonal3x3(itrack, 666);
msPar(itrack,0,0) = Par[iP](itrack,0,0);
msPar(itrack,1,0) = Par[iP](itrack,1,0);
msPar(itrack,2,0) = Par[iP](itrack,2,0);
// Don't update chi2
add_hit(itrack, fake_hit_idx, layer_of_hits.layer_id());
}
}
// Update the track parameters with this hit. (Note that some calculations
// are already done when computing chi2. Not sure it's worth caching them?)
dprint("update parameters");
(*fnd_foos.m_update_param_foo)(Err[iP], Par[iP], Chg, msErr, msPar,
Err[iC], Par[iC], N_proc, Config::finding_intra_layer_pflags);
//std::cout << "Par[iP](0,0,0)=" << Par[iP](0,0,0) << " Par[iC](0,0,0)=" << Par[iC](0,0,0)<< std::endl;
}
//=======================================================
// isStripQCompatible : check if prop is consistent with the barrel/endcap strip length
//=======================================================
bool isStripQCompatible(int itrack, bool isBarrel, const MPlexLS &pErr, const MPlexLV &pPar,
const MPlexHS &msErr, const MPlexHV &msPar)
{
//check module compatibility via long strip side = L/sqrt(12)
if (isBarrel) {//check z direction only
const float res = std::abs(msPar.ConstAt(itrack,2,0) - pPar.ConstAt(itrack,2,0));
const float hitHL = sqrt(msErr.ConstAt(itrack,2,2)*3.f);//half-length
const float qErr = sqrt(pErr.ConstAt(itrack,2,2));
dprint("qCompat "<<hitHL <<" + "<<3.f*qErr<<" vs "<<res);
return hitHL + std::max(3.f * qErr, 0.5f) > res;
} else {//project on xy, assuming the strip Length >> Width
const float res[2] {msPar.ConstAt(itrack,0,0) - pPar.ConstAt(itrack,0,0),
msPar.ConstAt(itrack,1,0) - pPar.ConstAt(itrack,1,0)};
const float hitT2 = msErr.ConstAt(itrack,0,0) + msErr.ConstAt(itrack,1,1);
const float hitT2inv = 1.f/hitT2;
const float proj[3] = {msErr.ConstAt(itrack,0,0)*hitT2inv, msErr.ConstAt(itrack,0,1)*hitT2inv,
msErr.ConstAt(itrack,1,1)*hitT2inv};
const float qErr = sqrt(std::abs(pErr.ConstAt(itrack,0,0)*proj[0] + 2.f*pErr.ConstAt(itrack,0,1)*proj[1]
+ pErr.ConstAt(itrack,1,1)*proj[2]));//take abs to avoid non-pos-def cases
const float resProj = sqrt(res[0]*proj[0]*res[0] + 2.f*res[1]*proj[1]*res[0] + res[1]*proj[2]*res[1]);
dprint("qCompat "<< sqrt(hitT2*3.f) <<" + "<<3.f*qErr<<" vs "<<resProj);
return sqrt(hitT2*3.f) + std::max(3.f*qErr, 0.5f) > resProj;
}
}
//=======================================================
// passStripChargePCMfromTrack : apply the slope correction to charge per cm and cut using hit err matrix
// the raw pcm = charge/L_normal
// the corrected qCorr = charge/L_path = charge/(L_normal*p/p_zLocal) = pcm*p_zLocal/p
//=======================================================
bool passStripChargePCMfromTrack(int itrack, bool isBarrel, unsigned int pcm, unsigned int pcmMin,
const MPlexLV &pPar, const MPlexHS &msErr)
{
//skip the overflow case
if (pcm >= Hit::maxChargePerCM())
return true;
float qSF;
if (isBarrel) {//project in x,y, assuming zero-error direction is in this plane
const float hitT2 = msErr.ConstAt(itrack,0,0) + msErr.ConstAt(itrack,1,1);
const float hitT2inv = 1.f/hitT2;
const float proj[3] = {msErr.ConstAt(itrack,0,0)*hitT2inv, msErr.ConstAt(itrack,0,1)*hitT2inv,
msErr.ConstAt(itrack,1,1)*hitT2inv};
const bool detXY_OK = std::abs(proj[0]*proj[2] - proj[1]*proj[1]) < 0.1f;//check that zero-direction is close
const float cosP = cos(pPar.ConstAt(itrack,4,0));
const float sinP = sin(pPar.ConstAt(itrack,4,0));
const float sinT = std::abs(sin(pPar.ConstAt(itrack,5,0)));
//qSF = sqrt[(px,py)*(1-proj)*(px,py)]/p = sinT*sqrt[(cosP,sinP)*(1-proj)*(cosP,sinP)].
qSF = detXY_OK ? sinT*std::sqrt(std::abs(1.f + cosP*cosP*proj[0] + sinP*sinP*proj[2] - 2.f*cosP*sinP*proj[1]) )
: 1.f;
} else {//project on z
// p_zLocal/p = p_z/p = cosT
qSF = std::abs(cos(pPar.ConstAt(itrack,5,0)));
}
const float qCorr = pcm*qSF;
dprint("pcm "<<pcm <<" * "<<qSF<<" = "<<qCorr<<" vs "<<pcmMin);
return qCorr > pcmMin;
}
//==============================================================================
// FindCandidates - Standard Track Finding
//==============================================================================
void MkFinder::FindCandidates(const LayerOfHits &layer_of_hits,
std::vector<std::vector<TrackCand>> &tmp_candidates,
const int offset, const int N_proc,
const FindingFoos &fnd_foos)
{
// bool debug = true;
MatriplexHitPacker mhp(* layer_of_hits.GetHitArray());
int maxSize = 0;
// Determine maximum number of hits for tracks in the collection.
for (int it = 0; it < NN; ++it)
{
if (it < N_proc)
{
if (XHitSize[it] > 0)
{
maxSize = std::max(maxSize, XHitSize[it]);
}
}
}
dprintf("FindCandidates max hits to process=%d\n", maxSize);
int nHitsAdded[NN] {};
for (int hit_cnt = 0; hit_cnt < maxSize; ++hit_cnt)
{
mhp.Reset();
int charge_pcm[NN];
#pragma omp simd
for (int itrack = 0; itrack < N_proc; ++itrack)
{
if (hit_cnt < XHitSize[itrack])
{
const auto& hit = layer_of_hits.GetHit( XHitArr.At(itrack, hit_cnt, 0) );
mhp.AddInputAt(itrack, hit);
charge_pcm[itrack] = hit.chargePerCM();
}
}
mhp.Pack(msErr, msPar);
//now compute the chi2 of track state vs hit
MPlexQF outChi2;
MPlexLV propPar;
(*fnd_foos.m_compute_chi2_foo)(Err[iP], Par[iP], Chg, msErr, msPar,
outChi2, propPar, N_proc, Config::finding_intra_layer_pflags);
// Now update the track parameters with this hit (note that some
// calculations are already done when computing chi2, to be optimized).
// 1. This is not needed for candidates the hit is not added to, but it's
// vectorized so doing it serially below should take the same time.
// 2. Still it's a waste of time in case the hit is not added to any of the
// candidates, so check beforehand that at least one cand needs update.
bool oneCandPassCut = false;
for (int itrack = 0; itrack < N_proc; ++itrack)
{
float max_c2 = getHitSelDynamicChi2Cut(itrack, iP);
if (hit_cnt < XHitSize[itrack])
{
const float chi2 = std::abs(outChi2[itrack]);//fixme negative chi2 sometimes...
dprint("chi2=" << chi2);
if (chi2 < max_c2)
{
bool isCompatible = true;
if (!layer_of_hits.is_pix_lyr()) {
//check module compatibility via long strip side = L/sqrt(12)
isCompatible = isStripQCompatible(itrack, layer_of_hits.is_barrel(), Err[iP], propPar, msErr, msPar);
//rescale strip charge to track parameters and reapply the cut
isCompatible &= passStripChargePCMfromTrack(itrack, layer_of_hits.is_barrel(),
charge_pcm[itrack], Hit::minChargePerCM(),
propPar, msErr);
}
if (isCompatible)
{
oneCandPassCut = true;
break;
}
}
}
}
if (oneCandPassCut)
{
MPlexQI tmpChg = Chg;
(*fnd_foos.m_update_param_foo)(Err[iP], Par[iP], tmpChg, msErr, msPar,
Err[iC], Par[iC], N_proc, Config::finding_intra_layer_pflags);
dprint("update parameters" << std::endl
<< "propagated track parameters x=" << Par[iP].ConstAt(0, 0, 0) << " y=" << Par[iP].ConstAt(0, 1, 0) << std::endl
<< " hit position x=" << msPar.ConstAt(0, 0, 0) << " y=" << msPar.ConstAt(0, 1, 0) << std::endl
<< " updated track parameters x=" << Par[iC].ConstAt(0, 0, 0) << " y=" << Par[iC].ConstAt(0, 1, 0));
//create candidate with hit in case chi2 < m_iteration_params->chi2Cut_min