L1Algo.h 26 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
41
42
43
/*********************************************************************************
 ************ TEMPORARY MACROS, SHOULD BE REMOVED (TODO!!, S. Zharko)  ***********
 *                                                                               */
 
44
#define FEATURING_L1ALGO_INIT 1  // If defined, new initialization will be used, if not - the old one
45
46
47
48
49
// Macro: 1 - new track finder loop, other - old track finder loop
#define FEATURING_L1ALGO_CATRACKFINDER_ITERATIONLOOP 1

/*                                                                               *
 *********************************************************************************/
50

51
#include <array>
52
53
#include <iomanip>
#include <iostream>
54
#include <limits>
55
56
#include <map>

Administrator's avatar
Administrator committed
57
#include "L1Branch.h"
58
#include "L1Field.h"
59
#include "L1Grid.h"
Sergey Gorbunov's avatar
Sergey Gorbunov committed
60
#include "L1Hit.h"
61
62
#include "L1HitPoint.h"
#include "L1HitsSortHelper.h"
63
64
#include "L1InitManager.h"
#include "L1Parameters.h"
65
#include "L1Portion.h"
66
67
68
69
#include "L1Station.h"
#include "L1Track.h"
#include "L1TrackPar.h"
#include "L1TrackParFit.h"
Administrator's avatar
Administrator committed
70
#include "L1Triplet.h"
Sergey Gorbunov's avatar
Sergey Gorbunov committed
71
#include "L1Vector.h"
72
73
74
75
76
77
78
79
80
81
82
83

#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
84
85
template<Tindex NHits>
class L1AlgoEfficiencyPerformance;
86
87
#endif
#ifdef DOUB_PERFORMANCE
Administrator's avatar
Administrator committed
88
89
template<Tindex NHits>
class L1AlgoEfficiencyPerformance;
90
91
92
#endif
typedef int Tindex;

93
94
/// Central class of L1 tracking
///
Administrator's avatar
Administrator committed
95
96
class L1Algo {
public:
97
  L1Algo(unsigned int nThreads = 1);
Administrator's avatar
Administrator committed
98

99
100
  L1Algo(const L1Algo&) = delete;
  L1Algo operator=(const L1Algo&) = delete;
Administrator's avatar
Administrator committed
101

102
103
104
105
106
107
108
109
110
111
112
113
  /// 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
114
  // TODO: make fDefaultMass a private member (S.Zh.)
115

116
117
118
119
  /// 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
120
    assert(Station < L1Parameters::kMaxNstations);
sergeizharko's avatar
sergeizharko committed
121
    assert(Thread < L1Parameters::kMaxNthreads);
sergeizharko's avatar
sergeizharko committed
122
    assert(Triplet < L1Parameters::kMaxNtriplets);
123
#endif
sergeizharko's avatar
sergeizharko committed
124
125
    constexpr unsigned int kMoveThread  = L1Parameters::kTripletBits;
    constexpr unsigned int kMoveStation = L1Parameters::kTripletBits + L1Parameters::kThreadBits;
126
127
128
129
130
131
    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
132
    constexpr unsigned int kMoveStation = L1Parameters::kTripletBits + L1Parameters::kThreadBits;
133
134
135
136
137
138
    return ID >> kMoveStation;
  }

  /// unpack the triplet ID to its thread index
  static unsigned int TripletId2Thread(unsigned int ID)
  {
sergeizharko's avatar
sergeizharko committed
139
    constexpr unsigned int kMoveThread = L1Parameters::kTripletBits;
Sergei Zharko's avatar
Sergei Zharko committed
140
    constexpr unsigned int kThreadMask = (1u << L1Parameters::kThreadBits) - 1u;
141
142
143
144
145
146
    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
147
    constexpr unsigned int kTripletMask = (1u << L1Parameters::kTripletBits) - 1u;
148
149
150
151
    return ID & kTripletMask;
  }


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

Sergey Gorbunov's avatar
Sergey Gorbunov committed
155
156
  // 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
157
  L1Vector<L1Branch> fTrackCandidates[L1Parameters::kMaxNthreads] {"L1Algo::fTrackCandidates"};
Administrator's avatar
Administrator committed
158

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

Sergey Gorbunov's avatar
Sergey Gorbunov committed
162
163
164
165
  //
  // Temporary vectors used by the clone merger
  //
  // vectors that are parallel to fTracks
Sergey Gorbunov's avatar
Sergey Gorbunov committed
166
167
168
169
170
171
172
173
174
175
  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
176
  // other vectors
Sergey Gorbunov's avatar
Sergey Gorbunov committed
177
178
  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
179
180


181
#ifdef DRAW
Sergey Gorbunov's avatar
Sergey Gorbunov committed
182
  L1AlgoDraw* draw {nullptr};
Sergey Gorbunov's avatar
Sergey Gorbunov committed
183
  void DrawRecoTracksTime(const L1Vector<CbmL1Track>& tracks);
184
#endif
Administrator's avatar
Administrator committed
185

186
187
188
189
190
191
  enum TrackingMode
  {
    kSts,
    kGlobal,
    kMcbm
  };
Administrator's avatar
Administrator committed
192

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

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

198
199
  void PrintHits();

Administrator's avatar
Administrator committed
200
  /// The main procedure - find tracks.
201
202
  void CATrackFinder();

Administrator's avatar
Administrator committed
203
  /// Track fitting procedures
204
205
  void KFTrackFitter_simple();  // version, which use procedured used during the reconstruction
  void L1KFTrackFitter();       // version from SIMD-KF benchmark
206
207

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

209
210
  float GetMaxInvMom() const { return MaxInvMom[0]; }

Administrator's avatar
Administrator committed
211
212
213
  /// ----- Input data -----
  // filled in CbmL1::ReadEvent();

214
  void SetNThreads(unsigned int n);
Administrator's avatar
Administrator committed
215

sergeizharko's avatar
sergeizharko committed
216
217
218
219
  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
220

Sergei Zharko's avatar
Sergei Zharko committed
221

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

227
228
  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
229
  L1Grid vGrid[L1Parameters::kMaxNstations];  // hits as a combination of front-, backstrips and z-position
sergeizharko's avatar
sergeizharko committed
230
  L1Grid vGridTime[L1Parameters::kMaxNstations];
Administrator's avatar
Administrator committed
231

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

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

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

Sergey Gorbunov's avatar
Sergey Gorbunov committed
239
240
  const THitI* StsHitsStartIndex {nullptr};  // station-bounders in vStsHits array
  const THitI* StsHitsStopIndex {nullptr};   // station-bounders in vStsHits array
Administrator's avatar
Administrator committed
241
242
243


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

Sergey Gorbunov's avatar
Sergey Gorbunov committed
246
247
248
249
250
251
  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
252
253
  L1Vector<L1Track> fTracks_local[L1Parameters::kMaxNthreads] {"L1Algo::fTracks_local"};
  L1Vector<THitI> fRecoHits_local[L1Parameters::kMaxNthreads] {"L1Algo::fRecoHits_local"};
254

Sergey Gorbunov's avatar
Sergey Gorbunov committed
255
256
257
  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
258
259

#ifdef _OPENMP
Sergey Gorbunov's avatar
Sergey Gorbunov committed
260
261
  L1Vector<omp_lock_t> fHitToBestTrackF {"L1Algo::fHitToBestTrackF"};
  L1Vector<omp_lock_t> fHitToBestTrackB {"L1Algo::fHitToBestTrackB"};
Administrator's avatar
Administrator committed
262
263
#endif

Sergey Gorbunov's avatar
Sergey Gorbunov committed
264
265
  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
266

Sergey Gorbunov's avatar
Sergey Gorbunov committed
267
  int fNThreads {0};
268
  bool fUseHitErrors {true};
269
  bool fMissingHits {0};
270
  TrackingMode fTrackingMode {kSts};
271

sergeizharko's avatar
sergeizharko committed
272
273
  fvec EventTime[L1Parameters::kMaxNthreads][L1Parameters::kMaxNthreads] {{0}};
  fvec Err[L1Parameters::kMaxNthreads][L1Parameters::kMaxNthreads] {{0}};
Administrator's avatar
Administrator committed
274
275
276


  /// standard sizes of the arrays
277
278
  enum
  {
Administrator's avatar
Administrator committed
279
280
281
282
283
284
285
286
287
288
289
290
    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
291
292
293
    MaxArrSize = MaxNPortion * MaxPortionDoublets
                 / L1Parameters::
                   kMaxNstations  //200000,  // standart size of big arrays  // mas be 40000 for normal work in cbmroot!
Administrator's avatar
Administrator committed
294
295
296
297
298
  };


  /// --- data used during finding iterations

Sergey Gorbunov's avatar
Sergey Gorbunov committed
299
300
301
302
303
304
305
  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
306
307
308
309
  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
310
311


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


Administrator's avatar
Administrator committed
315
316
317
  //  fvec u_front[Portion/fvecLen], u_back[Portion/fvecLen];
  //  fvec zPos[Portion/fvecLen];
  //  fvec fHitTime[Portion/fvecLen];
318

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

sergeizharko's avatar
sergeizharko committed
321
322
323
  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"};
324

sergeizharko's avatar
sergeizharko committed
325
326
327
328
329
330
331
332
333
  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];
334
335


sergeizharko's avatar
sergeizharko committed
336
337
  //   Tindex NHits_l[L1Parameters::kMaxNstations];
  //   Tindex NHits_l_P[L1Parameters::kMaxNstations];
Administrator's avatar
Administrator committed
338
339
340
341
342
343
344
345
  /// ----- 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
346
  void GetHitCoor(const L1Hit& _h, fscal& _x, fscal& _y, fscal& _z, const L1Station& sta);
347
348
349
350
351

  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);

352
  void dUdV_to_dXdY(const fvec& u, const fvec& v, fvec& _xy, const L1Station& sta);
353

Sergey Gorbunov's avatar
Sergey Gorbunov committed
354
  void GetHitCoor(const L1Hit& _h, fscal& _x, fscal& _y, char iS);
355
356
357
358
359
  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;
360
  L1HitPoint CreateHitPoint(const L1Hit& hit);  // full the hit point by hit information.
Administrator's avatar
Administrator committed
361

362
  void CreateHitPoint(const L1Hit& hit, L1HitPoint& point);
363
364
365
  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
366
  /// -- Flags routines --
367
368
  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
369
  //   bool GetFUsedD  ( unsigned char flag ){ return (flag&0x01)!=0; }
370

sergeizharko's avatar
sergeizharko committed
371
372

  /// Sets L1Algo parameters object
373
374
375
376
377
378
  void SetParameters(const L1Parameters& other) { fParameters = other; }

  /// Gets a pointer to the L1Algo parameters object
  L1Parameters* GetParameters() { return &fParameters; }
  /// Gets a pointer to the L1Algo initialization object
  L1InitManager* GetInitManager() { return &fInitManager; }
sergeizharko's avatar
sergeizharko committed
379

380
381
  fvec GetCbmTargetZ() const { return fCbmTargetZ; }

sergeizharko's avatar
sergeizharko committed
382
private:
383
384
385
  L1Parameters fParameters {};                ///< Object of L1Algo parameters class
  L1InitManager fInitManager {&fParameters};  ///< Object of L1Algo initialization manager class

sergeizharko's avatar
sergeizharko committed
386

Administrator's avatar
Administrator committed
387
388
389
  /// =================================  FUNCTIONAL PART  =================================

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

391
392
  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
393
394
395
396
397
398
399
400
401
                   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.
402
  void BranchFitterFast(const L1Branch& t, L1TrackPar& T, const bool dir, const fvec qp0 = 0.,
Administrator's avatar
Administrator committed
403
404
405
                        const bool initParams = true);

  /// Fit track. more precise than FitterFast
406
  void BranchFitter(const L1Branch& t, L1TrackPar& T, const bool dir, const fvec qp0 = 0.,
Administrator's avatar
Administrator committed
407
408
409
410
411
412
413
                    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
414
  void FindMoreHits(L1Branch& t, L1TrackPar& T, const bool dir, const fvec qp0 = 0.0);
Administrator's avatar
Administrator committed
415
416
417
418
419
420

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

  /// ----- Subroutines used by L1Algo::CAMergeClones() ------
421
422
423
424
  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]);
425
  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
426
                    fvec* chi2);
427
428
  void CAMergeClones();

Administrator's avatar
Administrator committed
429

430
431
432
  inline __attribute__((always_inline)) void PackLocation(unsigned int& location, unsigned int& triplet,
                                                          unsigned int iStation, unsigned int& thread)
  {
Administrator's avatar
Administrator committed
433
434
435
    location = (triplet << 11) | (thread << 3) | iStation;
  }

436
437
  inline __attribute__((always_inline)) void UnPackStation(unsigned int& location, unsigned int& iStation)
  {
Administrator's avatar
Administrator committed
438
439
440
    iStation = location & 0x7;
  }

441
442
  inline __attribute__((always_inline)) void UnPackThread(unsigned int& location, unsigned int& thread)
  {
Administrator's avatar
Administrator committed
443
444
445
    thread = (location >> 3) & 0xFF;
  }

446
447
  inline __attribute__((always_inline)) void UnPackTriplet(unsigned int& location, unsigned int& triplet)
  {
Administrator's avatar
Administrator committed
448
449
450
    triplet = (location >> 11);
  }

451
452
  inline __attribute__((always_inline)) void SetFStation(unsigned char& flag, unsigned int iStation)
  {
Administrator's avatar
Administrator committed
453
454
    flag = iStation * 4 + (flag % 4);
  }
455
  inline __attribute__((always_inline)) void SetFUsed(unsigned char& flag) { flag |= 0x02; }
Administrator's avatar
Administrator committed
456
  //   void SetFUsedD   ( unsigned char &flag ){ flag |= 0x01; }
457
  inline __attribute__((always_inline)) void SetFUnUsed(unsigned char& flag) { flag &= 0xFC; }
Administrator's avatar
Administrator committed
458
459
460
  //   void SetFUnUsedD ( unsigned char &flag ){ flag &= 0xFE; }

  /// Prepare the portion of left hits data
461
  void f10(  // input
462
    Tindex start_lh, Tindex n1_l, L1HitPoint* StsHits_l,
Administrator's avatar
Administrator committed
463
    // output
464
465
    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
466
467

  /// Get the field approximation. Add the target to parameters estimation. Propagate to middle station.
468
  void f11(  // input
469
470
471
    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
472
    // output
473
    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
474
475

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

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

482
#ifdef DOUB_PERFORMANCE
Sergey Gorbunov's avatar
Sergey Gorbunov committed
483
    L1Vector<THitI>& hitsl_2,
Administrator's avatar
Administrator committed
484
#endif  // DOUB_PERFORMANCE
Sergey Gorbunov's avatar
Sergey Gorbunov committed
485
    L1Vector<THitI>& hitsm_2, fvec* Event, L1Vector<char>& lmDuplets);
Administrator's avatar
Administrator committed
486
487
488

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

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

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

Sergey Gorbunov's avatar
Sergey Gorbunov committed
496
    const L1Vector<char>& mrDuplets,
Administrator's avatar
Administrator committed
497
    // output
Sergey Gorbunov's avatar
Sergey Gorbunov committed
498
499
    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
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
    nsL1::vector<fvec>::TSimd& timeER);

  /// Add the right hits to parameters estimation.
507
  void f31(  // input
508
    Tindex n3_V, L1Station& star, nsL1::vector<fvec>::TSimd& u_front_3, nsL1::vector<fvec>::TSimd& u_back_3,
Administrator's avatar
Administrator committed
509
    nsL1::vector<fvec>::TSimd& z_Pos_3,
510
511
    //    nsL1::vector<fvec>::TSimd& dx_,
    //    nsL1::vector<fvec>::TSimd& dy_,
512
    nsL1::vector<fvec>::TSimd& du_, nsL1::vector<fvec>::TSimd& dv_, nsL1::vector<fvec>::TSimd& timeR,
Administrator's avatar
Administrator committed
513
514
515
516
517
518
    nsL1::vector<fvec>::TSimd& timeER,
    // output
    nsL1::vector<L1TrackPar>::TSimd& T_3);

  /// Refit Triplets.
  void f32(  // input
Sergey Gorbunov's avatar
Sergey Gorbunov committed
519
520
    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
521
522

  /// Select triplets. Save them into vTriplets.
523
  void f4(  // input
Sergey Gorbunov's avatar
Sergey Gorbunov committed
524
525
    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
526
    // output
Sergey Gorbunov's avatar
Sergey Gorbunov committed
527
    Tindex& nstaltriplets, L1Vector<THitI>* hitsn_3 = 0, L1Vector<THitI>* hitsr_5 = 0
Administrator's avatar
Administrator committed
528
529
530
531
532
533
534
535

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


  /// Find neighbours of triplets. Calculate level of triplets.
536
  void f5(  // input
Administrator's avatar
Administrator committed
537
538
539
540
541
542
    // output
    int* nlevel);


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

    // output
546
    L1TrackPar* T_1, L1FieldRegion* fld_1, THitI* hitsl_1,
Administrator's avatar
Administrator committed
547

Sergey Gorbunov's avatar
Sergey Gorbunov committed
548
    L1Vector<char>& lmDuplets,
Administrator's avatar
Administrator committed
549
550


Sergey Gorbunov's avatar
Sergey Gorbunov committed
551
    Tindex& n_2, L1Vector<THitI>& i1_2, L1Vector<THitI>& hitsm_2);
Administrator's avatar
Administrator committed
552
553
554

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

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

Sergey Gorbunov's avatar
Sergey Gorbunov committed
559
    const L1Vector<char>& mrDuplets
Administrator's avatar
Administrator committed
560
561
562
563
564
565
566
567
568

    // output


  );


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

569
570
571
  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
572
573

  void FilterFirst(L1TrackPar& track, fvec& x, fvec& y, L1Station& st);
574
575
576
577
  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
578
                   fvec& dxy);
579
  void FilterFirstL(L1TrackParFit& track, fvec& x, fvec& y, fvec& t, fvec& t_er, L1Station& st, fvec& dx, fvec& dy,
Administrator's avatar
Administrator committed
580
581
                    fvec& dxy);

582
#ifdef TBB
583
584
  enum
  {
Administrator's avatar
Administrator committed
585
586
587
    nthreads = 3,  // number of threads
    nblocks  = 1   // number of stations on one thread
  };
588
589
590

  friend class ParalleledDup;
  friend class ParalleledTrip;
Administrator's avatar
Administrator committed
591
#endif  // TBB
592
#ifdef TBB2
Administrator's avatar
Administrator committed
593
public:
594
  Tindex thrId;
Administrator's avatar
Administrator committed
595
596
597
598
599
#endif  // TBB2
private:
  /// =================================  DATA PART  =================================

  /// ----- Different parameters of CATrackFinder -----
600

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

Administrator's avatar
Administrator committed
603
604
  // fNFindIterations - set number of interation for trackfinding
  // itetation of finding:
605
#ifdef FIND_GAPED_TRACKS
606
607
  enum
  {
Administrator's avatar
Administrator committed
608
609
610
611
612
613
614
615
616
617
    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
618
619
  };
#ifdef TRACKS_FROM_TRIPLETS
620
621
  enum
  {
Administrator's avatar
Administrator committed
622
623
    fNFindIterations = TRACKS_FROM_TRIPLETS_ITERATION + 1
  };  // TODO investigate kAllPrimJumpIter & kAllSecJumpIter
624
#else  // not TRACKS_FROM_TRIPLETS
Administrator's avatar
Administrator committed
625

626
627
  enum
  {
628
    fNFindIterations = 4
Administrator's avatar
Administrator committed
629
  };  // TODO investigate kAllPrimJumpIter & kAllSecJumpIter
630

Administrator's avatar
Administrator committed
631

632
633
#endif  // TRACKS_FROM_TRIPLETS
#else  // not FIND_GAPED_TRACKS
634
635
  enum
  {
Administrator's avatar
Administrator committed
636
637
638
639
640
641
642
643
644
    kFastPrimIter = 0,  // primary fast tracks
    kAllPrimIter,       // primary all tracks
    kAllSecIter,        // secondary all tracks
    kFastPrimJumpIter,  // disabled
    kAllPrimJumpIter,   // disabled
    kFastPrimIter2,
    kAllSecJumpIter,
    kAllPrimEIter,
    kAllSecEIter
645
  };
Administrator's avatar
Administrator committed
646
647
#endif  // FIND_GAPED_TRACKS

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

650
  static constexpr float kNaN {std::numeric_limits<float>::signaling_NaN()};
651

Sergey Gorbunov's avatar
Sergey Gorbunov committed
652
653
654
655
656
  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
657

Sergey Gorbunov's avatar
Sergey Gorbunov committed
658
  fvec MaxDZ {
659
    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
660
661
662

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

663
664
665
666
667
668
669
670
671
672
673
674
  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
675
676
  L1FieldValue targB _fvecalignment {};                // field in the target point
  L1XYMeasurementInfo TargetXYInfo _fvecalignment {};  // target constraint  [cm]
Administrator's avatar
Administrator committed
677
678


Sergey Gorbunov's avatar
Sergey Gorbunov committed
679
680
  L1FieldRegion vtxFieldRegion _fvecalignment {};  // really doesn't used
  L1FieldValue vtxFieldValue _fvecalignment {};    // field at teh vertex position.
Administrator's avatar
Administrator committed
681
682
683

  //  int TripNumThread;

Sergey Gorbunov's avatar
Sergey Gorbunov committed
684
685
686
  int fTrackingLevel {0};     // currently not used
  int fGhostSuppression {0};  // currently not used
  float fMomentumCutOff {0};  // currently not used
Administrator's avatar
Administrator committed
687
688

  /// ----- Debug features -----
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#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