L1Algo.h 25.7 KB
Newer Older
1
2
/* Copyright (C) 2007-2021 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
   SPDX-License-Identifier: GPL-3.0-only
3
   Authors: Maksym Zyzak, Valentina Akishina, Igor Kulakov [committer] */
4

5
#ifndef L1Algo_h
Sergey Gorbunov's avatar
Sergey Gorbunov committed
6
#define L1Algo_h
7
8
9

// #define TBB // TODO: Doesn't work now. Renew

Administrator's avatar
Administrator committed
10
/// Debug features
11
12
13
// #define PULLS            // triplets pulls
// #define TRIP_PERFORMANCE // triplets efficiencies
// #define DOUB_PERFORMANCE // doublets efficiencies
Sergey Gorbunov's avatar
Sergey Gorbunov committed
14
// #define DRAW             // event display
15
16
17
18
19
20
21
#ifdef DRAW
class L1AlgoDraw;
#include "CbmL1Track.h"
#endif
//#define XXX               // time debug
//#define COUNTERS          // diff counters (hits, doublets, ... )

22
#define MERGE_CLONES
23
24
25
26
27
28
29
// #define TRACKS_FROM_TRIPLETS_ITERATION kAllPrimIter

//#define HitErrors
//#define GLOBAL
//#define mCBM

#define LAST_ITERATION kAllSecIter
Administrator's avatar
Administrator committed
30
#define FIND_GAPED_TRACKS  // use triplets with gaps
31
32
33
34
35
36
#define USE_RL_TABLE
#ifndef TRACKS_FROM_TRIPLETS
#define EXTEND_TRACKS
#endif
//#define USE_EVENT_NUMBER
//#endif
37
//#define MERGE_CLONES
38

Sergei Zharko's avatar
Sergei Zharko committed
39

40
#define FEATURING_L1ALGO_INIT 1  // If defined, new initialization will be used, if not - the old one
41

42
#include <array>
43
44
#include <iomanip>
#include <iostream>
45
#include <limits>
46
47
#include <map>

Administrator's avatar
Administrator committed
48
#include "L1Branch.h"
49
#include "L1Field.h"
50
#include "L1Grid.h"
Sergey Gorbunov's avatar
Sergey Gorbunov committed
51
#include "L1Hit.h"
52
53
#include "L1HitPoint.h"
#include "L1HitsSortHelper.h"
54
55
#include "L1InitManager.h"
#include "L1Parameters.h"
56
#include "L1Portion.h"
57
58
59
60
#include "L1Station.h"
#include "L1Track.h"
#include "L1TrackPar.h"
#include "L1TrackParFit.h"
Administrator's avatar
Administrator committed
61
#include "L1Triplet.h"
Sergey Gorbunov's avatar
Sergey Gorbunov committed
62
#include "L1Vector.h"
63
64
65
66
67
68
69
70
71
72
73
74

#ifdef _OPENMP
#include "omp.h"
#endif

using std::map;

#ifdef PULLS
#define TRIP_PERFORMANCE
class L1AlgoPulls;
#endif
#ifdef TRIP_PERFORMANCE
Administrator's avatar
Administrator committed
75
76
template<Tindex NHits>
class L1AlgoEfficiencyPerformance;
77
78
#endif
#ifdef DOUB_PERFORMANCE
Administrator's avatar
Administrator committed
79
80
template<Tindex NHits>
class L1AlgoEfficiencyPerformance;
81
82
83
84
#endif
typedef int Tindex;


Administrator's avatar
Administrator committed
85
86
class L1Algo {
public:
87
  L1Algo(unsigned int nThreads = 1);
Administrator's avatar
Administrator committed
88

89
90
  L1Algo(const L1Algo&) = delete;
  L1Algo operator=(const L1Algo&) = delete;
Administrator's avatar
Administrator committed
91

92
93
94
95
96
97
98
99
100
101
102
103
  /// set a default particle mass for the track fit
  /// it is used during reconstruction
  /// for the multiple scattering and energy loss estimation
  void SetDefaultParticleMass(float mass) { fDefaultMass = mass; }

  /// get default particle mass
  float GetDefaultParticleMass() const { return fDefaultMass; }

  /// get default particle mass squared
  float GetDefaultParticleMass2() const { return fDefaultMass * fDefaultMass; }

  float fDefaultMass = 0.10565800;  // muon mass
104
  // TODO: make fDefaultMass a private member (S.Zh.)
105

106
107
108
109
  /// pack station, thread and triplet indices to an unique triplet ID
  static unsigned int PackTripletId(unsigned int Station, unsigned int Thread, unsigned int Triplet)
  {
#ifndef FAST_CODE
sergeizharko's avatar
sergeizharko committed
110
    assert(Station < L1Parameters::kMaxNstations);
sergeizharko's avatar
sergeizharko committed
111
    assert(Thread < L1Parameters::kMaxNthreads);
sergeizharko's avatar
sergeizharko committed
112
    assert(Triplet < L1Parameters::kMaxNtriplets);
113
#endif
sergeizharko's avatar
sergeizharko committed
114
115
    constexpr unsigned int kMoveThread  = L1Parameters::kTripletBits;
    constexpr unsigned int kMoveStation = L1Parameters::kTripletBits + L1Parameters::kThreadBits;
116
117
118
119
120
121
    return (Station << kMoveStation) + (Thread << kMoveThread) + Triplet;
  }

  /// unpack the triplet ID to its station index
  static unsigned int TripletId2Station(unsigned int ID)
  {
sergeizharko's avatar
sergeizharko committed
122
    constexpr unsigned int kMoveStation = L1Parameters::kTripletBits + L1Parameters::kThreadBits;
123
124
125
126
127
128
    return ID >> kMoveStation;
  }

  /// unpack the triplet ID to its thread index
  static unsigned int TripletId2Thread(unsigned int ID)
  {
sergeizharko's avatar
sergeizharko committed
129
    constexpr unsigned int kMoveThread = L1Parameters::kTripletBits;
Sergei Zharko's avatar
Sergei Zharko committed
130
    constexpr unsigned int kThreadMask = (1u << L1Parameters::kThreadBits) - 1u;
131
132
133
134
135
136
    return (ID >> kMoveThread) & kThreadMask;
  }

  /// unpack the triplet ID to its triplet index
  static unsigned int TripletId2Triplet(unsigned int ID)
  {
Sergei Zharko's avatar
Sergei Zharko committed
137
    constexpr unsigned int kTripletMask = (1u << L1Parameters::kTripletBits) - 1u;
138
139
140
141
    return ID & kTripletMask;
  }


sergeizharko's avatar
sergeizharko committed
142
  L1Vector<L1Triplet> fTriplets[L1Parameters::kMaxNstations][L1Parameters::kMaxNthreads] {
143
    {"L1Algo::fTriplets"}};  // created triplets at station + thread
144

Sergey Gorbunov's avatar
Sergey Gorbunov committed
145
146
  // Track candidates created out of adjacent triplets before the final track selection.
  // The candidates may share any amount of hits.
sergeizharko's avatar
sergeizharko committed
147
  L1Vector<L1Branch> fTrackCandidates[L1Parameters::kMaxNthreads] {"L1Algo::fTrackCandidates"};
Administrator's avatar
Administrator committed
148

sergeizharko's avatar
sergeizharko committed
149
  Tindex fDupletPortionStopIndex[L1Parameters::kMaxNstations] {0};     // end of the duplet portions for the station
Sergey Gorbunov's avatar
Sergey Gorbunov committed
150
  L1Vector<Tindex> fDupletPortionSize {"L1Algo::fDupletPortionSize"};  // N duplets in a portion
Administrator's avatar
Administrator committed
151

Sergey Gorbunov's avatar
Sergey Gorbunov committed
152
153
154
155
  //
  // Temporary vectors used by the clone merger
  //
  // vectors that are parallel to fTracks
Sergey Gorbunov's avatar
Sergey Gorbunov committed
156
157
158
159
160
161
162
163
164
165
  L1Vector<unsigned short> fMergerTrackFirstStation {"L1Algo::fMergerTrackFirstStation"};  // first station of a track
  L1Vector<unsigned short> fMergerTrackLastStation {"L1Algo::fMergerTrackLastStation"};    // last station of a track
  L1Vector<THitI> fMergerTrackFirstHit {"L1Algo::fMergerTrackFirstHit"};  // index of the first tracks hit
  L1Vector<THitI> fMergerTrackLastHit {"L1Algo::fMergerTrackLastHit"};    // index of the last tracks hit
  L1Vector<unsigned short> fMergerTrackNeighbour {
    "L1Algo::fMergerTrackNeighbour"};                             // track that can be merged with the given track
  L1Vector<float> fMergerTrackChi2 {"L1Algo::fMergerTrackChi2"};  // chi2 of the merge
  L1Vector<char> fMergerTrackIsStored {"L1Algo::fMergerTrackIsStored"};  // is the track already stored to the output
  L1Vector<char> fMergerTrackIsDownstreamNeighbour {
    "L1Algo::fMergerTrackIsDownstreamNeighbour"};  // is the track a downstream neighbor of another track
Sergey Gorbunov's avatar
Sergey Gorbunov committed
166
  // other vectors
Sergey Gorbunov's avatar
Sergey Gorbunov committed
167
168
  L1Vector<L1Track> fMergerTracksNew {"L1Algo::fMergerTracksNew"};    // vector of tracks after the merge
  L1Vector<THitI> fMergerRecoHitsNew {"L1Algo::fMergerRecoHitsNew"};  // vector of track hits after the merge
Administrator's avatar
Administrator committed
169
170


171
#ifdef DRAW
Sergey Gorbunov's avatar
Sergey Gorbunov committed
172
  L1AlgoDraw* draw {nullptr};
Sergey Gorbunov's avatar
Sergey Gorbunov committed
173
  void DrawRecoTracksTime(const L1Vector<CbmL1Track>& tracks);
174
#endif
Administrator's avatar
Administrator committed
175

176
177
178
179
180
181
  enum TrackingMode
  {
    kSts,
    kGlobal,
    kMcbm
  };
Administrator's avatar
Administrator committed
182

183
  void Init(const L1Vector<fscal>& geo, const bool UseHitErrors, const TrackingMode mode, const bool MissingHits);
Administrator's avatar
Administrator committed
184

185
  void SetData(L1Vector<L1Hit>& StsHits_, int nStsStrips_, L1Vector<unsigned char>& SFlag_,
Sergey Gorbunov's avatar
Sergey Gorbunov committed
186
               const THitI* StsHitsStartIndex_, const THitI* StsHitsStopIndex_);
Administrator's avatar
Administrator committed
187

188
189
  void PrintHits();

Administrator's avatar
Administrator committed
190
  /// The main procedure - find tracks.
191
192
  void CATrackFinder();

Administrator's avatar
Administrator committed
193
  /// Track fitting procedures
194
195
  void KFTrackFitter_simple();  // version, which use procedured used during the reconstruction
  void L1KFTrackFitter();       // version from SIMD-KF benchmark
196
197

  void L1KFTrackFitterMuch();
Administrator's avatar
Administrator committed
198

199
200
  float GetMaxInvMom() const { return MaxInvMom[0]; }

Administrator's avatar
Administrator committed
201
202
203
  /// ----- Input data -----
  // filled in CbmL1::ReadEvent();

204
  void SetNThreads(unsigned int n);
Administrator's avatar
Administrator committed
205

sergeizharko's avatar
sergeizharko committed
206
207
208
209
  int NStations {0};        ///< number of all detector stations
  int NMvdStations {0};     ///< number of mvd stations
  int NStsStations {0};     ///< number of sts stations
  int fNfieldStations {0};  ///< number of stations in the field region
Sergey Gorbunov's avatar
Sergey Gorbunov committed
210

Sergei Zharko's avatar
Sergei Zharko committed
211

212
  // TODO: Replace _fvecalignment with C++11 alignas(16) attibute, see vStationsNew (S.Zh.)
sergeizharko's avatar
sergeizharko committed
213
  L1Station vStations[L1Parameters::kMaxNstations] _fvecalignment;  // station info
214
  alignas(16) std::array<L1Station, L1Parameters::kMaxNstations> fStationsNew;
215
  L1Vector<L1Material> fRadThick {"fRadThick"};        // material for each station
216

217
218
  int NStsStrips {0};                         // number of strips
  L1Vector<L1Hit>* vStsHits {nullptr};        // hits as a combination of front-, backstrips and z-position
sergeizharko's avatar
sergeizharko committed
219
  L1Grid vGrid[L1Parameters::kMaxNstations];  // hits as a combination of front-, backstrips and z-position
sergeizharko's avatar
sergeizharko committed
220
  L1Grid vGridTime[L1Parameters::kMaxNstations];
Administrator's avatar
Administrator committed
221

Sergey Gorbunov's avatar
Sergey Gorbunov committed
222
  L1Vector<unsigned char>* fStripFlag {nullptr};  // information of hits station & using hits in tracks;
Administrator's avatar
Administrator committed
223

224
  double fCATime {0.};  // time of track finding
Administrator's avatar
Administrator committed
225

Sergey Gorbunov's avatar
Sergey Gorbunov committed
226
227
  L1Vector<L1Track> fTracks {"L1Algo::fTracks"};    // reconstructed tracks
  L1Vector<THitI> fRecoHits {"L1Algo::fRecoHits"};  // packed hits of reconstructed tracks
Administrator's avatar
Administrator committed
228

Sergey Gorbunov's avatar
Sergey Gorbunov committed
229
230
  const THitI* StsHitsStartIndex {nullptr};  // station-bounders in vStsHits array
  const THitI* StsHitsStopIndex {nullptr};   // station-bounders in vStsHits array
Administrator's avatar
Administrator committed
231
232
233


  //  L1Branch* pointer;
Sergey Gorbunov's avatar
Sergey Gorbunov committed
234
  unsigned int NHitsIsecAll {0};
235

Sergey Gorbunov's avatar
Sergey Gorbunov committed
236
237
238
239
240
241
  L1Vector<L1Hit> vStsDontUsedHits_A {"L1Algo::vStsDontUsedHits_A"};
  L1Vector<L1Hit> vStsDontUsedHits_B {"L1Algo::vStsDontUsedHits_B"};
  L1Vector<L1Hit> vStsDontUsedHits_Buf {"L1Algo::vStsDontUsedHits_Buf"};
  L1Vector<L1HitPoint> vStsDontUsedHitsxy_A {"L1Algo::vStsDontUsedHitsxy_A"};
  L1Vector<L1HitPoint> vStsDontUsedHitsxy_buf {"L1Algo::vStsDontUsedHitsxy_buf"};
  L1Vector<L1HitPoint> vStsDontUsedHitsxy_B {"L1Algo::vStsDontUsedHitsxy_B"};
sergeizharko's avatar
sergeizharko committed
242
243
  L1Vector<L1Track> fTracks_local[L1Parameters::kMaxNthreads] {"L1Algo::fTracks_local"};
  L1Vector<THitI> fRecoHits_local[L1Parameters::kMaxNthreads] {"L1Algo::fRecoHits_local"};
244

Sergey Gorbunov's avatar
Sergey Gorbunov committed
245
246
247
  L1Vector<THitI> RealIHit_v {"L1Algo::RealIHit_v"};
  L1Vector<THitI> RealIHit_v_buf {"L1Algo::RealIHit_v_buf"};
  L1Vector<THitI> RealIHit_v_buf2 {"L1Algo::RealIHit_v_buf2"};
Administrator's avatar
Administrator committed
248
249

#ifdef _OPENMP
Sergey Gorbunov's avatar
Sergey Gorbunov committed
250
251
  L1Vector<omp_lock_t> fHitToBestTrackF {"L1Algo::fHitToBestTrackF"};
  L1Vector<omp_lock_t> fHitToBestTrackB {"L1Algo::fHitToBestTrackB"};
Administrator's avatar
Administrator committed
252
253
#endif

Sergey Gorbunov's avatar
Sergey Gorbunov committed
254
255
  L1Vector<int> fStripToTrack {"L1Algo::fStripToTrack"};    // front strip to track pointers
  L1Vector<int> fStripToTrackB {"L1Algo::fStripToTrackB"};  // back strip to track pointers
Administrator's avatar
Administrator committed
256

Sergey Gorbunov's avatar
Sergey Gorbunov committed
257
  int fNThreads {0};
258
  bool fUseHitErrors {true};
259
  bool fMissingHits {0};
260
  TrackingMode fTrackingMode {kSts};
261

sergeizharko's avatar
sergeizharko committed
262
263
  fvec EventTime[L1Parameters::kMaxNthreads][L1Parameters::kMaxNthreads] {{0}};
  fvec Err[L1Parameters::kMaxNthreads][L1Parameters::kMaxNthreads] {{0}};
Administrator's avatar
Administrator committed
264
265
266


  /// standard sizes of the arrays
267
268
  enum
  {
Administrator's avatar
Administrator committed
269
270
271
272
273
274
275
276
277
278
279
280
    multiCoeff = 1,  // central - 1, mbias


    coeff = 64 / 4,

    Portion = 1024 / coeff,  // portion of left hits

    MaxPortionDoublets = 10000 / 5 * 64 / 2 / coeff /*/ multiCoeff*/ * 1,
    MaxPortionTriplets = 10000 * 5 * 64 / 2 / coeff /*/ multiCoeff*/ * 1,
    MaxNPortion        = 40 * coeff / multiCoeff,


sergeizharko's avatar
sergeizharko committed
281
282
283
    MaxArrSize = MaxNPortion * MaxPortionDoublets
                 / L1Parameters::
                   kMaxNstations  //200000,  // standart size of big arrays  // mas be 40000 for normal work in cbmroot!
Administrator's avatar
Administrator committed
284
285
286
287
288
  };


  /// --- data used during finding iterations

Sergey Gorbunov's avatar
Sergey Gorbunov committed
289
290
291
292
293
294
295
  int isec {0};  // iteration
  L1Vector<L1Hit>* vStsHitsUnused {nullptr};
  L1Vector<THitI>* RealIHitP {nullptr};
  L1Vector<THitI>* RealIHitPBuf {nullptr};
  L1Vector<L1HitPoint>* vStsHitPointsUnused {nullptr};
  THitI* RealIHit {nullptr};  // index in vStsHits indexed by index in vStsHitsUnused

sergeizharko's avatar
sergeizharko committed
296
297
298
299
  THitI StsHitsUnusedStartIndex[L1Parameters::kMaxNstations + 1] {0};
  THitI StsHitsUnusedStopIndex[L1Parameters::kMaxNstations + 1] {0};
  THitI StsHitsUnusedStartIndexEnd[L1Parameters::kMaxNstations + 1] {0};
  THitI StsHitsUnusedStopIndexEnd[L1Parameters::kMaxNstations + 1] {0};
Administrator's avatar
Administrator committed
300
301


sergeizharko's avatar
sergeizharko committed
302
  L1Vector<int> TripForHit[2] {"L1Algo::TripForHit"};  // TODO: what does '2' stand for?
303
304


Administrator's avatar
Administrator committed
305
306
307
  //  fvec u_front[Portion/fvecLen], u_back[Portion/fvecLen];
  //  fvec zPos[Portion/fvecLen];
  //  fvec fHitTime[Portion/fvecLen];
308

sergeizharko's avatar
sergeizharko committed
309
  nsL1::vector<L1TrackPar>::TSimd fT_3[L1Parameters::kMaxNthreads];
310

sergeizharko's avatar
sergeizharko committed
311
312
313
  L1Vector<THitI> fhitsl_3[L1Parameters::kMaxNthreads] {"L1Algo::fhitsl_3"};
  L1Vector<THitI> fhitsm_3[L1Parameters::kMaxNthreads] {"L1Algo::fhitsm_3"};
  L1Vector<THitI> fhitsr_3[L1Parameters::kMaxNthreads] {"L1Algo::fhitsr_3"};
314

sergeizharko's avatar
sergeizharko committed
315
316
317
318
319
320
321
322
323
  nsL1::vector<fvec>::TSimd fu_front3[L1Parameters::kMaxNthreads];
  nsL1::vector<fvec>::TSimd fu_back3[L1Parameters::kMaxNthreads];
  nsL1::vector<fvec>::TSimd fz_pos3[L1Parameters::kMaxNthreads];
  nsL1::vector<fvec>::TSimd fTimeR[L1Parameters::kMaxNthreads];
  nsL1::vector<fvec>::TSimd fTimeER[L1Parameters::kMaxNthreads];
  nsL1::vector<fvec>::TSimd dx[L1Parameters::kMaxNthreads];
  nsL1::vector<fvec>::TSimd dy[L1Parameters::kMaxNthreads];
  nsL1::vector<fvec>::TSimd du[L1Parameters::kMaxNthreads];
  nsL1::vector<fvec>::TSimd dv[L1Parameters::kMaxNthreads];
324
325


sergeizharko's avatar
sergeizharko committed
326
327
  //   Tindex NHits_l[L1Parameters::kMaxNstations];
  //   Tindex NHits_l_P[L1Parameters::kMaxNstations];
Administrator's avatar
Administrator committed
328
329
330
331
332
333
334
335
  /// ----- Output data -----

  friend class CbmL1;

  const L1FieldValue& GetVtxFieldValue() const { return vtxFieldValue; }
  const L1FieldRegion& GetVtxFieldRegion() const { return vtxFieldRegion; }
  /// ----- Hit-point-strips conversion routines ------

Sergey Gorbunov's avatar
Sergey Gorbunov committed
336
  void GetHitCoor(const L1Hit& _h, fscal& _x, fscal& _y, fscal& _z, const L1Station& sta);
337
338
339
340
341

  void dUdV_to_dY(const fvec& u, const fvec& v, fvec& _y, const L1Station& sta);

  void dUdV_to_dX(const fvec& u, const fvec& v, fvec& _x, const L1Station& sta);

342
  void dUdV_to_dXdY(const fvec& u, const fvec& v, fvec& _xy, const L1Station& sta);
343

Sergey Gorbunov's avatar
Sergey Gorbunov committed
344
  void GetHitCoor(const L1Hit& _h, fscal& _x, fscal& _y, char iS);
345
346
347
348
349
  void StripsToCoor(const fscal& u, const fscal& v, fscal& x, fscal& y,
                    const L1Station& sta) const;  // convert strip positions to coordinates
  void StripsToCoor(const fscal& u, const fscal& v, fvec& x, fvec& y,
                    const L1Station& sta) const;  // convert strip positions to coordinates
  void StripsToCoor(const fvec& u, const fvec& v, fvec& x, fvec& y, const L1Station& sta) const;
350
  L1HitPoint CreateHitPoint(const L1Hit& hit);  // full the hit point by hit information.
Administrator's avatar
Administrator committed
351

352
  void CreateHitPoint(const L1Hit& hit, L1HitPoint& point);
353
354
355
  inline int PackIndex(const int& a, const int& b, const int& c);

  inline int UnPackIndex(const int& i, int& a, int& b, int& c);
Administrator's avatar
Administrator committed
356
  /// -- Flags routines --
357
358
  inline __attribute__((always_inline)) static unsigned char GetFStation(unsigned char flag) { return flag / 4; }
  inline __attribute__((always_inline)) static bool GetFUsed(unsigned char flag) { return (flag & 0x02) != 0; }
Administrator's avatar
Administrator committed
359
  //   bool GetFUsedD  ( unsigned char flag ){ return (flag&0x01)!=0; }
360

sergeizharko's avatar
sergeizharko committed
361
362
363

  /// Sets L1Algo parameters object
  void SetL1Parameters(const L1Parameters& other) { fL1Parameters = other; }
sergeizharko's avatar
sergeizharko committed
364
  /// Gets a constant reference to the L1Algo parameters object
sergeizharko's avatar
sergeizharko committed
365
  const L1Parameters& GetL1Parameters() const { return fL1Parameters; }
366
  // TODO: We should think about, where non-constexpr L1Algo parameters can be modified. At the moment we can create a
sergeizharko's avatar
sergeizharko committed
367
  //       L1Parameters object somewhere outside the L1Algo, fill its fields there and then pass it directly to
368
  //       the L1Algo instance. (S.Zh.)
Sergei Zharko's avatar
Sergei Zharko committed
369
  L1InitManager* GetL1InitManager() { return &fInitManager; }
sergeizharko's avatar
sergeizharko committed
370

371
372
  fvec GetCbmTargetZ() const { return fCbmTargetZ; }

sergeizharko's avatar
sergeizharko committed
373
private:
sergeizharko's avatar
sergeizharko committed
374
  /// Object containing L1Parameters. Default consturctor is used
375
  L1Parameters fL1Parameters;  ///< Object of L1Algo parameters class
Sergei Zharko's avatar
Sergei Zharko committed
376
  L1InitManager fInitManager;  ///< Object of L1Algo initialization manager class
sergeizharko's avatar
sergeizharko committed
377

Administrator's avatar
Administrator committed
378
379
380
  /// =================================  FUNCTIONAL PART  =================================

  /// ----- Subroutines used by L1Algo::CATrackFinder() ------
381

382
383
  void CAFindTrack(int ista, L1Branch& best_tr, unsigned char& best_L, fscal& best_chi2, const L1Triplet* curr_trip,
                   L1Branch& curr_tr, unsigned char& curr_L, fscal& curr_chi2, unsigned char min_best_l,
Administrator's avatar
Administrator committed
384
385
386
387
388
389
390
391
392
                   L1Branch* new_tr);


  /// Fit track
  /// t - track with hits
  /// T - track params
  /// dir - 0 - forward, 1 - backward
  /// qp0 - momentum for extrapolation
  /// initialize - should be params ititialized. 1 - yes.
393
  void BranchFitterFast(const L1Branch& t, L1TrackPar& T, const bool dir, const fvec qp0 = 0.,
Administrator's avatar
Administrator committed
394
395
396
                        const bool initParams = true);

  /// Fit track. more precise than FitterFast
397
  void BranchFitter(const L1Branch& t, L1TrackPar& T, const bool dir, const fvec qp0 = 0.,
Administrator's avatar
Administrator committed
398
399
400
401
402
403
404
                    const bool initParams = true);

  /// Find additional hits for existing track
  /// t - track with hits
  /// T - track params
  /// dir - 0 - forward, 1 - backward
  /// qp0 - momentum for extrapolation
405
  void FindMoreHits(L1Branch& t, L1TrackPar& T, const bool dir, const fvec qp0 = 0.0);
Administrator's avatar
Administrator committed
406
407
408
409
410
411

  /// Find additional hits for existing track
  /// return chi2
  fscal BranchExtender(L1Branch& t);

  /// ----- Subroutines used by L1Algo::CAMergeClones() ------
412
413
414
415
  void InvertCholetsky(fvec a[15]);
  void MultiplySS(fvec const C[15], fvec const V[15], fvec K[5][5]);
  void MultiplyMS(fvec const C[5][5], fvec const V[15], fvec K[15]);
  void MultiplySR(fvec const C[15], fvec const r_in[5], fvec r_out[5]);
416
  void FilterTracks(fvec const r[5], fvec const C[15], fvec const m[5], fvec const V[15], fvec R[5], fvec W[15],
Administrator's avatar
Administrator committed
417
                    fvec* chi2);
418
419
  void CAMergeClones();

Administrator's avatar
Administrator committed
420

421
422
423
  inline __attribute__((always_inline)) void PackLocation(unsigned int& location, unsigned int& triplet,
                                                          unsigned int iStation, unsigned int& thread)
  {
Administrator's avatar
Administrator committed
424
425
426
    location = (triplet << 11) | (thread << 3) | iStation;
  }

427
428
  inline __attribute__((always_inline)) void UnPackStation(unsigned int& location, unsigned int& iStation)
  {
Administrator's avatar
Administrator committed
429
430
431
    iStation = location & 0x7;
  }

432
433
  inline __attribute__((always_inline)) void UnPackThread(unsigned int& location, unsigned int& thread)
  {
Administrator's avatar
Administrator committed
434
435
436
    thread = (location >> 3) & 0xFF;
  }

437
438
  inline __attribute__((always_inline)) void UnPackTriplet(unsigned int& location, unsigned int& triplet)
  {
Administrator's avatar
Administrator committed
439
440
441
    triplet = (location >> 11);
  }

442
443
  inline __attribute__((always_inline)) void SetFStation(unsigned char& flag, unsigned int iStation)
  {
Administrator's avatar
Administrator committed
444
445
    flag = iStation * 4 + (flag % 4);
  }
446
  inline __attribute__((always_inline)) void SetFUsed(unsigned char& flag) { flag |= 0x02; }
Administrator's avatar
Administrator committed
447
  //   void SetFUsedD   ( unsigned char &flag ){ flag |= 0x01; }
448
  inline __attribute__((always_inline)) void SetFUnUsed(unsigned char& flag) { flag &= 0xFC; }
Administrator's avatar
Administrator committed
449
450
451
  //   void SetFUnUsedD ( unsigned char &flag ){ flag &= 0xFE; }

  /// Prepare the portion of left hits data
452
  void f10(  // input
453
    Tindex start_lh, Tindex n1_l, L1HitPoint* StsHits_l,
Administrator's avatar
Administrator committed
454
    // output
455
456
    fvec* u_front_l, fvec* u_back_l, fvec* zPos_l, THitI* hitsl, fvec* HitTime_l, fvec* HitTimeEr, fvec* Event_l,
    fvec* d_x, fvec* d_y, fvec* d_xy, fvec* d_u, fvec* d_v);
Administrator's avatar
Administrator committed
457
458

  /// Get the field approximation. Add the target to parameters estimation. Propagate to middle station.
459
  void f11(  // input
460
461
462
    int istal, int istam, Tindex n1_V,

    fvec* u_front_l, fvec* u_back_l, fvec* zPos_l, fvec* HitTime_l, fvec* HitTimeEr,
Administrator's avatar
Administrator committed
463
    // output
464
    L1TrackPar* T_1, L1FieldRegion* fld_1, fvec* d_x, fvec* d_y, fvec* d_xy, fvec* d_u, fvec* d_v);
Administrator's avatar
Administrator committed
465
466

  /// Find the doublets. Reformat data in the portion of doublets.
467
  void f20(  // input
468
    Tindex n1, L1Station& stal, L1Station& stam, L1HitPoint* vStsHits_m, L1TrackPar* T_1, THitI* hitsl_1,
Administrator's avatar
Administrator committed
469
470

    // output
Sergey Gorbunov's avatar
Sergey Gorbunov committed
471
    Tindex& n2, L1Vector<THitI>& i1_2,
Administrator's avatar
Administrator committed
472

473
#ifdef DOUB_PERFORMANCE
Sergey Gorbunov's avatar
Sergey Gorbunov committed
474
    L1Vector<THitI>& hitsl_2,
Administrator's avatar
Administrator committed
475
#endif  // DOUB_PERFORMANCE
Sergey Gorbunov's avatar
Sergey Gorbunov committed
476
    L1Vector<THitI>& hitsm_2, fvec* Event, L1Vector<char>& lmDuplets);
Administrator's avatar
Administrator committed
477
478
479

  /// Add the middle hits to parameters estimation. Propagate to right station.
  /// Find the triplets (right hit). Reformat data in the portion of triplets.
480
  void f30(  // input
481
    L1HitPoint* vStsHits_r, L1Station& stam, L1Station& star,
Administrator's avatar
Administrator committed
482

483
    int istam, int istar, L1HitPoint* vStsHits_m, L1TrackPar* T_1, L1FieldRegion* fld_1, THitI* hitsl_1,
Administrator's avatar
Administrator committed
484

Sergey Gorbunov's avatar
Sergey Gorbunov committed
485
    Tindex n2, L1Vector<THitI>& hitsm_2, L1Vector<THitI>& i1_2,
Administrator's avatar
Administrator committed
486

Sergey Gorbunov's avatar
Sergey Gorbunov committed
487
    const L1Vector<char>& mrDuplets,
Administrator's avatar
Administrator committed
488
    // output
Sergey Gorbunov's avatar
Sergey Gorbunov committed
489
490
    Tindex& n3, nsL1::vector<L1TrackPar>::TSimd& T_3, L1Vector<THitI>& hitsl_3, L1Vector<THitI>& hitsm_3,
    L1Vector<THitI>& hitsr_3, nsL1::vector<fvec>::TSimd& u_front_3, nsL1::vector<fvec>::TSimd& u_back_3,
Administrator's avatar
Administrator committed
491
    nsL1::vector<fvec>::TSimd& z_Pos_3,
492
493
    //    nsL1::vector<fvec>::TSimd& dx_,
    //    nsL1::vector<fvec>::TSimd& dy_,
494
    nsL1::vector<fvec>::TSimd& du_, nsL1::vector<fvec>::TSimd& dv_, nsL1::vector<fvec>::TSimd& timeR,
Administrator's avatar
Administrator committed
495
496
497
    nsL1::vector<fvec>::TSimd& timeER);

  /// Add the right hits to parameters estimation.
498
  void f31(  // input
499
    Tindex n3_V, L1Station& star, nsL1::vector<fvec>::TSimd& u_front_3, nsL1::vector<fvec>::TSimd& u_back_3,
Administrator's avatar
Administrator committed
500
    nsL1::vector<fvec>::TSimd& z_Pos_3,
501
502
    //    nsL1::vector<fvec>::TSimd& dx_,
    //    nsL1::vector<fvec>::TSimd& dy_,
503
    nsL1::vector<fvec>::TSimd& du_, nsL1::vector<fvec>::TSimd& dv_, nsL1::vector<fvec>::TSimd& timeR,
Administrator's avatar
Administrator committed
504
505
506
507
508
509
    nsL1::vector<fvec>::TSimd& timeER,
    // output
    nsL1::vector<L1TrackPar>::TSimd& T_3);

  /// Refit Triplets.
  void f32(  // input
Sergey Gorbunov's avatar
Sergey Gorbunov committed
510
511
    Tindex n3, int istal, nsL1::vector<L1TrackPar>::TSimd& T_3, L1Vector<THitI>& hitsl_3, L1Vector<THitI>& hitsm_3,
    L1Vector<THitI>& hitsr_3, int nIterations = 0);
Administrator's avatar
Administrator committed
512
513

  /// Select triplets. Save them into vTriplets.
514
  void f4(  // input
Sergey Gorbunov's avatar
Sergey Gorbunov committed
515
516
    Tindex n3, int istal, int istam, int istar, nsL1::vector<L1TrackPar>::TSimd& T_3, L1Vector<THitI>& hitsl_3,
    L1Vector<THitI>& hitsm_3, L1Vector<THitI>& hitsr_3,
Administrator's avatar
Administrator committed
517
    // output
Sergey Gorbunov's avatar
Sergey Gorbunov committed
518
    Tindex& nstaltriplets, L1Vector<THitI>* hitsn_3 = 0, L1Vector<THitI>* hitsr_5 = 0
Administrator's avatar
Administrator committed
519
520
521
522
523
524
525
526

    // #ifdef XXX
    //                 ,unsigned int &stat_n_trip
    // #endif
  );


  /// Find neighbours of triplets. Calculate level of triplets.
527
  void f5(  // input
Administrator's avatar
Administrator committed
528
529
530
531
532
533
    // output
    int* nlevel);


  /// Find doublets on station
  void DupletsStaPort(  // input
Sergey Gorbunov's avatar
Sergey Gorbunov committed
534
    int istal, int istam, Tindex ip, L1Vector<Tindex>& n_g, Tindex* portionStopIndex_,
Administrator's avatar
Administrator committed
535
536

    // output
537
    L1TrackPar* T_1, L1FieldRegion* fld_1, THitI* hitsl_1,
Administrator's avatar
Administrator committed
538

Sergey Gorbunov's avatar
Sergey Gorbunov committed
539
    L1Vector<char>& lmDuplets,
Administrator's avatar
Administrator committed
540
541


Sergey Gorbunov's avatar
Sergey Gorbunov committed
542
    Tindex& n_2, L1Vector<THitI>& i1_2, L1Vector<THitI>& hitsm_2);
Administrator's avatar
Administrator committed
543
544
545

  /// Find triplets on station
  void TripletsStaPort(  // input
546
547
    int istal, int istam, int istar, Tindex& nstaltriplets, L1TrackPar* T_1, L1FieldRegion* fld_1, THitI* hitsl_1,

Sergey Gorbunov's avatar
Sergey Gorbunov committed
548
    Tindex& n_2, L1Vector<THitI>& i1_2, L1Vector<THitI>& hitsm_2,
Administrator's avatar
Administrator committed
549

Sergey Gorbunov's avatar
Sergey Gorbunov committed
550
    const L1Vector<char>& mrDuplets
Administrator's avatar
Administrator committed
551
552
553
554
555
556
557
558
559

    // output


  );


  ///  ------ Subroutines used by L1Algo::KFTrackFitter()  ------

560
561
562
  void GuessVec(L1TrackPar& t, fvec* xV, fvec* yV, fvec* zV, fvec* Sy, fvec* wV, int NHits, fvec* zCur = 0);
  void GuessVec(L1TrackParFit& t, fvec* xV, fvec* yV, fvec* zV, fvec* Sy, fvec* wV, int NHits, fvec* zCur = 0,
                fvec* timeV = 0, fvec* w_time = 0);
Administrator's avatar
Administrator committed
563
564

  void FilterFirst(L1TrackPar& track, fvec& x, fvec& y, L1Station& st);
565
566
567
568
  void FilterFirst(L1TrackParFit& track, fvec& x, fvec& y, fvec& t, L1Station& st);
  void FilterFirst(L1TrackParFit& track, fvec& x, fvec& y, fvec& t, fvec& t_er, L1Station& st);

  void FilterFirst(L1TrackParFit& track, fvec& x, fvec& y, fvec& t, fvec& t_er, L1Station& st, fvec& dx, fvec& dy,
Administrator's avatar
Administrator committed
569
                   fvec& dxy);
570
  void FilterFirstL(L1TrackParFit& track, fvec& x, fvec& y, fvec& t, fvec& t_er, L1Station& st, fvec& dx, fvec& dy,
Administrator's avatar
Administrator committed
571
572
                    fvec& dxy);

573
#ifdef TBB
574
575
  enum
  {
Administrator's avatar
Administrator committed
576
577
578
    nthreads = 3,  // number of threads
    nblocks  = 1   // number of stations on one thread
  };
579
580
581

  friend class ParalleledDup;
  friend class ParalleledTrip;
Administrator's avatar
Administrator committed
582
#endif  // TBB
583
#ifdef TBB2
Administrator's avatar
Administrator committed
584
public:
585
  Tindex thrId;
Administrator's avatar
Administrator committed
586
587
588
589
590
#endif  // TBB2
private:
  /// =================================  DATA PART  =================================

  /// ----- Different parameters of CATrackFinder -----
591

Sergey Gorbunov's avatar
Sergey Gorbunov committed
592
  Tindex FIRSTCASTATION {0};  //first station used in CA
593

Administrator's avatar
Administrator committed
594
595
  // fNFindIterations - set number of interation for trackfinding
  // itetation of finding:
596
#ifdef FIND_GAPED_TRACKS
597
598
  enum
  {
Administrator's avatar
Administrator committed
599
600
601
602
603
604
605
606
607
608
    kFastPrimIter,     // primary fast tracks
    kAllPrimIter,      // primary all tracks
    kAllPrimJumpIter,  // primary tracks with jumped triplets
    kAllSecIter,       // secondary all tracks
    kAllPrimEIter,     // primary all electron tracks
    kAllSecEIter,      // secondary all electron tracks

    kFastPrimJumpIter,  // primary fast tracks with jumped triplets
    kFastPrimIter2,
    kAllSecJumpIter  // secondary tracks with jumped triplets
609
610
  };
#ifdef TRACKS_FROM_TRIPLETS
611
612
  enum
  {
Administrator's avatar
Administrator committed
613
614
    fNFindIterations = TRACKS_FROM_TRIPLETS_ITERATION + 1
  };  // TODO investigate kAllPrimJumpIter & kAllSecJumpIter
615
#else  // not TRACKS_FROM_TRIPLETS
Administrator's avatar
Administrator committed
616

617
618
  enum
  {
619
    fNFindIterations = 4
Administrator's avatar
Administrator committed
620
  };  // TODO investigate kAllPrimJumpIter & kAllSecJumpIter
621

Administrator's avatar
Administrator committed
622

623
624
#endif  // TRACKS_FROM_TRIPLETS
#else  // not FIND_GAPED_TRACKS
625
626
  enum
  {
Administrator's avatar
Administrator committed
627
628
629
630
631
632
633
634
635
    kFastPrimIter = 0,  // primary fast tracks
    kAllPrimIter,       // primary all tracks
    kAllSecIter,        // secondary all tracks
    kFastPrimJumpIter,  // disabled
    kAllPrimJumpIter,   // disabled
    kFastPrimIter2,
    kAllSecJumpIter,
    kAllPrimEIter,
    kAllSecEIter
636
  };
Administrator's avatar
Administrator committed
637
638
#endif  // FIND_GAPED_TRACKS

Sergey Gorbunov's avatar
Sergey Gorbunov committed
639
  map<int, int> threadNumberToCpuMap {};
Administrator's avatar
Administrator committed
640

641
  static constexpr float kNaN {std::numeric_limits<float>::signaling_NaN()};
642

Sergey Gorbunov's avatar
Sergey Gorbunov committed
643
644
645
646
647
  float TRACK_CHI2_CUT {10.f};
  float TRIPLET_CHI2_CUT {5.f};  // cut for selecting triplets before collecting tracks.per one DoF
  float DOUBLET_CHI2_CUT {5.f};
  float TIME_CUT1 {0.f};
  float TIME_CUT2 {0.f};
Administrator's avatar
Administrator committed
648

Sergey Gorbunov's avatar
Sergey Gorbunov committed
649
  fvec MaxDZ {
650
    kNaN};  // correction in order to take into account overlaping and iff z. if sort by y then it is max diff between same station's modules (~0.4cm)
Administrator's avatar
Administrator committed
651
652
653

  /// parameters which are different for different iterations. Set in the begin of CAL1TrackFinder

654
655
656
657
658
659
660
661
662
663
664
665
  float Pick_gather {kNaN};    // same for attaching additional hits to track
  float PickNeighbour {kNaN};  // (PickNeighbour < dp/dp_error)  =>  triplets are neighbours
  fvec MaxInvMom {kNaN};       // max considered q/p for tracks
  fvec MaxSlopePV {kNaN};      // max slope (tx\ty) in prim vertex
  float MaxSlope {kNaN};       // max slope (tx\ty) in 3d hit position of a triplet
  fvec fCbmTargetX {kNaN};     // target position
  fvec fCbmTargetY {kNaN};
  fvec fCbmTargetZ {kNaN};
  fvec fTargX {kNaN};  // target position for the current iteration
  fvec fTargY {kNaN};
  fvec fTargZ {kNaN};

Sergey Gorbunov's avatar
Sergey Gorbunov committed
666
667
  L1FieldValue targB _fvecalignment {};                // field in the target point
  L1XYMeasurementInfo TargetXYInfo _fvecalignment {};  // target constraint  [cm]
Administrator's avatar
Administrator committed
668
669


Sergey Gorbunov's avatar
Sergey Gorbunov committed
670
671
  L1FieldRegion vtxFieldRegion _fvecalignment {};  // really doesn't used
  L1FieldValue vtxFieldValue _fvecalignment {};    // field at teh vertex position.
Administrator's avatar
Administrator committed
672
673
674

  //  int TripNumThread;

Sergey Gorbunov's avatar
Sergey Gorbunov committed
675
676
677
  int fTrackingLevel {0};     // currently not used
  int fGhostSuppression {0};  // currently not used
  float fMomentumCutOff {0};  // currently not used
Administrator's avatar
Administrator committed
678
679

  /// ----- Debug features -----
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
#ifdef PULLS
  L1AlgoPulls* fL1Pulls;
#endif
#ifdef TRIP_PERFORMANCE
  L1AlgoEfficiencyPerformance<3>* fL1Eff_triplets;
  L1AlgoEfficiencyPerformance<3>* fL1Eff_triplets2;
#endif
#ifdef DOUB_PERFORMANCE
  L1AlgoEfficiencyPerformance<2>* fL1Eff_doublets;
#endif
#ifdef DRAW
  friend class L1AlgoDraw;
#endif
} _fvecalignment;

#endif