L1Algo.h 27.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
// =====>dispatched<===== // #define MERGE_CLONES
23
24
25
26
27
28
// #define TRACKS_FROM_TRIPLETS_ITERATION kAllPrimIter

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

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

Sergei Zharko's avatar
Sergei Zharko committed
38

39
#include <array>
40
41
#include <iomanip>
#include <iostream>
42
#include <limits>
43
44
#include <map>

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

#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
73
74
template<Tindex NHits>
class L1AlgoEfficiencyPerformance;
75
76
#endif
#ifdef DOUB_PERFORMANCE
Administrator's avatar
Administrator committed
77
78
template<Tindex NHits>
class L1AlgoEfficiencyPerformance;
79
80
81
#endif
typedef int Tindex;

82
83
using L1StationsArray_t = std::array<L1Station, L1Parameters::kMaxNstations>;

84
85
/// Central class of L1 tracking
///
Administrator's avatar
Administrator committed
86
87
class L1Algo {
public:
88
  L1Algo(unsigned int nThreads = 1);
Administrator's avatar
Administrator committed
89

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

93
  /// Sets a default particle mass for the track fit
94
95
  /// it is used during reconstruction
  /// for the multiple scattering and energy loss estimation
96
  /// \param mass Default particle mass
97
98
  void SetDefaultParticleMass(float mass) { fDefaultMass = mass; }

99
100
  /// Gets default particle mass
  /// \return particle mass
101
102
  float GetDefaultParticleMass() const { return fDefaultMass; }

103
104
  /// Gets default particle mass squared
  /// \return particle mass squared
105
106
  float GetDefaultParticleMass2() const { return fDefaultMass * fDefaultMass; }

Sergei Zharko's avatar
Sergei Zharko committed
107
  float fDefaultMass = 0.10565800;  // muon mass
108
  // TODO: make fDefaultMass a private member (S.Zharko)
109

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

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


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

Sergey Gorbunov's avatar
Sergey Gorbunov committed
149
150
  // 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
151
  L1Vector<L1Branch> fTrackCandidates[L1Parameters::kMaxNthreads] {"L1Algo::fTrackCandidates"};
Administrator's avatar
Administrator committed
152

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

Sergei Zharko's avatar
Sergei Zharko committed
156
  /********************************************************************************************/ /**
157
158
159
160
   * Temporary vectors used by the clone merger 
   * TODO: Probably, the subclass L1TrackMerger for clones merger would help to improve 
   *       readability (S.Zharko)
   ***********************************************************************************************/
Sergei Zharko's avatar
Sergei Zharko committed
161

Sergey Gorbunov's avatar
Sergey Gorbunov committed
162
  //
163
  // Vectors that are parallel to fTracks
Sergey Gorbunov's avatar
Sergey Gorbunov committed
164
  //
165
166
167
168
169
170
171
172
173
  /// First station of a track
  L1Vector<unsigned short> fMergerTrackFirstStation {"L1Algo::fMergerTrackFirstStation"};
  /// Last station of a track
  L1Vector<unsigned short> fMergerTrackLastStation {"L1Algo::fMergerTrackLastStation"};
  /// Index of the first hit of a track
  L1Vector<L1HitIndex_t> fMergerTrackFirstHit {"L1Algo::fMergerTrackFirstHit"};
  /// Index of the last hit of a track
  L1Vector<L1HitIndex_t> fMergerTrackLastHit {"L1Algo::fMergerTrackLastHit"};
  /// Index (TODO:??) of a track that can be merge with the given track
Sergei Zharko's avatar
Sergei Zharko committed
174
  L1Vector<unsigned short> fMergerTrackNeighbour {"L1Algo::fMergerTrackNeighbour"};
175
176
177
178
179
180
181
182
183
184
  /// Chi2 value of the track merging procedure
  L1Vector<float> fMergerTrackChi2 {"L1Algo::fMergerTrackChi2"};
  /// Flag: is the given track already stored to the output
  L1Vector<char> fMergerTrackIsStored {"L1Algo::fMergerTrackIsStored"};
  /// Flag: is the track a downstream neighbour of another track
  L1Vector<char> fMergerTrackIsDownstreamNeighbour {"L1Algo::fMergerTrackIsDownstreamNeighbour"};
  //
  // Utility vectors
  //
  /// Tracks after the merging procedure
Sergey Gorbunov's avatar
Sergey Gorbunov committed
185
  L1Vector<L1Track> fMergerTracksNew {"L1Algo::fMergerTracksNew"};    // vector of tracks after the merge
186
  L1Vector<L1HitIndex_t> fMergerRecoHitsNew {"L1Algo::fMergerRecoHitsNew"};  // vector of track hits after the merge
Administrator's avatar
Administrator committed
187
188


189
#ifdef DRAW
Sergey Gorbunov's avatar
Sergey Gorbunov committed
190
  L1AlgoDraw* draw {nullptr};
Sergey Gorbunov's avatar
Sergey Gorbunov committed
191
  void DrawRecoTracksTime(const L1Vector<CbmL1Track>& tracks);
192
#endif
Administrator's avatar
Administrator committed
193

194
195
196
197
198
199
  enum TrackingMode
  {
    kSts,
    kGlobal,
    kMcbm
  };
Administrator's avatar
Administrator committed
200

201
  void Init(const bool UseHitErrors, const TrackingMode mode, const bool MissingHits);
Administrator's avatar
Administrator committed
202

203
  void SetData(L1Vector<L1Hit>& StsHits_, int nStsStrips_, L1Vector<unsigned char>& SFlag_,
204
               const L1HitIndex_t* StsHitsStartIndex_, const L1HitIndex_t* StsHitsStopIndex_);
Administrator's avatar
Administrator committed
205

206
207
  void PrintHits();

Administrator's avatar
Administrator committed
208
  /// The main procedure - find tracks.
209
210
  void CATrackFinder();

Administrator's avatar
Administrator committed
211
  /// Track fitting procedures
212
213
  void KFTrackFitter_simple();  // version, which use procedured used during the reconstruction
  void L1KFTrackFitter();       // version from SIMD-KF benchmark
214
215

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

217
  float GetMaxInvMom() const { return fMaxInvMom[0]; }
218

Administrator's avatar
Administrator committed
219
220
221
  /// ----- Input data -----
  // filled in CbmL1::ReadEvent();

222
  void SetNThreads(unsigned int n);
Administrator's avatar
Administrator committed
223

224
private:
Sergei Zharko's avatar
Sergei Zharko committed
225
226
227
228
  int fNstations {0};                       ///< number of all detector stations
  int fNstationsBeforePipe {0};             ///< number of stations before pipe (MVD stations in CBM)
  int fNfieldStations {0};                  ///< number of stations in the field region
  alignas(16) L1StationsArray_t fStations;  ///< array of L1Station objects
229

230
public:
231
  /// Gets total number of stations used in tracking
Sergei Zharko's avatar
Sergei Zharko committed
232
  int GetNstations() const { return fNstations; }
233
234
235
  /// Gets number of stations before the pipe (MVD stations in CBM)
  int GetNstationsBeforePipe() const { return fNstationsBeforePipe; }
  /// Gets number of stations situated in field region (MVD + STS in CBM)
Sergei Zharko's avatar
Sergei Zharko committed
236
  int GetNfieldStations() const { return fNfieldStations; }
237
238
  /// Gets reference to the stations array
  const L1StationsArray_t& GetStations() const { return fStations; }
239
240

public:
241
  L1Vector<L1Material> fRadThick {"fRadThick"};        // material for each station
242

243
244
245
246
  int NStsStrips {0};                             ///> number of strips
  L1Vector<L1Hit>* vStsHits {nullptr};            ///> hits as a combination of front-, backstrips and z-position
  L1Grid vGrid[L1Parameters::kMaxNstations];      ///> hits as a combination of front-, backstrips and z-position
  L1Grid vGridTime[L1Parameters::kMaxNstations];  ///>
Administrator's avatar
Administrator committed
247

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

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

Sergey Gorbunov's avatar
Sergey Gorbunov committed
252
  L1Vector<L1Track> fTracks {"L1Algo::fTracks"};    // reconstructed tracks
253
  L1Vector<L1HitIndex_t> fRecoHits {"L1Algo::fRecoHits"};  // packed hits of reconstructed tracks
Administrator's avatar
Administrator committed
254

255
256
  const L1HitIndex_t* StsHitsStartIndex {nullptr};  // station-bounders in vStsHits array
  const L1HitIndex_t* StsHitsStopIndex {nullptr};   // station-bounders in vStsHits array
Administrator's avatar
Administrator committed
257
258
259


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

Sergey Gorbunov's avatar
Sergey Gorbunov committed
262
263
264
265
266
267
  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
268
  L1Vector<L1Track> fTracks_local[L1Parameters::kMaxNthreads] {"L1Algo::fTracks_local"};
269
  L1Vector<L1HitIndex_t> fRecoHits_local[L1Parameters::kMaxNthreads] {"L1Algo::fRecoHits_local"};
270

271
272
273
  L1Vector<L1HitIndex_t> RealIHit_v {"L1Algo::RealIHit_v"};
  L1Vector<L1HitIndex_t> RealIHit_v_buf {"L1Algo::RealIHit_v_buf"};
  L1Vector<L1HitIndex_t> RealIHit_v_buf2 {"L1Algo::RealIHit_v_buf2"};
Administrator's avatar
Administrator committed
274
275

#ifdef _OPENMP
Sergey Gorbunov's avatar
Sergey Gorbunov committed
276
277
  L1Vector<omp_lock_t> fHitToBestTrackF {"L1Algo::fHitToBestTrackF"};
  L1Vector<omp_lock_t> fHitToBestTrackB {"L1Algo::fHitToBestTrackB"};
Administrator's avatar
Administrator committed
278
279
#endif

Sergey Gorbunov's avatar
Sergey Gorbunov committed
280
281
  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
282

Sergey Gorbunov's avatar
Sergey Gorbunov committed
283
  int fNThreads {0};
284
  bool fUseHitErrors {true};
Sergei Zharko's avatar
Sergei Zharko committed
285
  bool fMissingHits {0};  ///< TODO ???
286
  TrackingMode fTrackingMode {kSts};
287

sergeizharko's avatar
sergeizharko committed
288
289
  fvec EventTime[L1Parameters::kMaxNthreads][L1Parameters::kMaxNthreads] {{0}};
  fvec Err[L1Parameters::kMaxNthreads][L1Parameters::kMaxNthreads] {{0}};
Administrator's avatar
Administrator committed
290
291
292


  /// standard sizes of the arrays
293
294
  enum
  {
Administrator's avatar
Administrator committed
295
296
297
298
299
300
301
302
303
304
305
306
    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
307
308
309
    MaxArrSize = MaxNPortion * MaxPortionDoublets
                 / L1Parameters::
                   kMaxNstations  //200000,  // standart size of big arrays  // mas be 40000 for normal work in cbmroot!
Administrator's avatar
Administrator committed
310
311
312
313
314
  };


  /// --- data used during finding iterations

Sergey Gorbunov's avatar
Sergey Gorbunov committed
315
316
  int isec {0};  // iteration
  L1Vector<L1Hit>* vStsHitsUnused {nullptr};
317
318
  L1Vector<L1HitIndex_t>* RealIHitP {nullptr};
  L1Vector<L1HitIndex_t>* RealIHitPBuf {nullptr};
Sergey Gorbunov's avatar
Sergey Gorbunov committed
319
  L1Vector<L1HitPoint>* vStsHitPointsUnused {nullptr};
320
  L1HitIndex_t* RealIHit {nullptr};  // index in vStsHits indexed by index in vStsHitsUnused
Sergey Gorbunov's avatar
Sergey Gorbunov committed
321

322
323
324
325
  L1HitIndex_t StsHitsUnusedStartIndex[L1Parameters::kMaxNstations + 1] {0};
  L1HitIndex_t StsHitsUnusedStopIndex[L1Parameters::kMaxNstations + 1] {0};
  L1HitIndex_t StsHitsUnusedStartIndexEnd[L1Parameters::kMaxNstations + 1] {0};
  L1HitIndex_t StsHitsUnusedStopIndexEnd[L1Parameters::kMaxNstations + 1] {0};
Administrator's avatar
Administrator committed
326
327


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


Administrator's avatar
Administrator committed
331
332
333
  //  fvec u_front[Portion/fvecLen], u_back[Portion/fvecLen];
  //  fvec zPos[Portion/fvecLen];
  //  fvec fHitTime[Portion/fvecLen];
334

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

337
338
339
  L1Vector<L1HitIndex_t> fhitsl_3[L1Parameters::kMaxNthreads] {"L1Algo::fhitsl_3"};
  L1Vector<L1HitIndex_t> fhitsm_3[L1Parameters::kMaxNthreads] {"L1Algo::fhitsm_3"};
  L1Vector<L1HitIndex_t> fhitsr_3[L1Parameters::kMaxNthreads] {"L1Algo::fhitsr_3"};
340

sergeizharko's avatar
sergeizharko committed
341
342
343
344
345
346
347
348
349
  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];
350
351


sergeizharko's avatar
sergeizharko committed
352
353
  //   Tindex NHits_l[L1Parameters::kMaxNstations];
  //   Tindex NHits_l_P[L1Parameters::kMaxNstations];
Administrator's avatar
Administrator committed
354
355
356
357
  /// ----- Output data -----

  friend class CbmL1;

358
359
  const L1FieldValue& GetVtxFieldValue() const { return fVtxFieldValue; }
  const L1FieldRegion& GetVtxFieldRegion() const { return fVtxFieldRegion; }
Administrator's avatar
Administrator committed
360
361
  /// ----- Hit-point-strips conversion routines ------

Sergey Gorbunov's avatar
Sergey Gorbunov committed
362
  void GetHitCoor(const L1Hit& _h, fscal& _x, fscal& _y, fscal& _z, const L1Station& sta);
363
364
365
366
367

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

368
  void dUdV_to_dXdY(const fvec& u, const fvec& v, fvec& _xy, const L1Station& sta);
369

Sergey Gorbunov's avatar
Sergey Gorbunov committed
370
  void GetHitCoor(const L1Hit& _h, fscal& _x, fscal& _y, char iS);
371
372
373
374
375
  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;
376
  L1HitPoint CreateHitPoint(const L1Hit& hit);  // full the hit point by hit information.
Administrator's avatar
Administrator committed
377

378
  void CreateHitPoint(const L1Hit& hit, L1HitPoint& point);
379
380
381
  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
382
  /// -- Flags routines --
383
384
  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
385
  //   bool GetFUsedD  ( unsigned char flag ){ return (flag&0x01)!=0; }
386

sergeizharko's avatar
sergeizharko committed
387
388

  /// Sets L1Algo parameters object
389
  /// \param other - reference to the L1Parameters object
390
  void SetParameters(const L1Parameters& other) { fParameters = other; }
391
  // TODO: remove it (S.Zharko)
392
393
394
395
396

  /// 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
397

398
  fvec GetTargetZ() const { return fRealTargetZ; }
399

sergeizharko's avatar
sergeizharko committed
400
private:
401
402
403
  L1Parameters fParameters {};                ///< Object of L1Algo parameters class
  L1InitManager fInitManager {&fParameters};  ///< Object of L1Algo initialization manager class

sergeizharko's avatar
sergeizharko committed
404

Sergei Zharko's avatar
Sergei Zharko committed
405
  /*********************************************************************************************/ /**
406
407
   *                             ------  FUNCTIONAL PART ------
   ************************************************************************************************/
Administrator's avatar
Administrator committed
408
409

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

411
412
  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
413
414
415
                   L1Branch* new_tr);


416
417
418
419
420
421
  /// Fits track
  /// \param t - track with hits
  /// \param T - track parameters
  /// \param dir - false - forward, true - backward
  /// \param qp0 - momentum for extrapolation
  /// \param initParams - should be params ititialized. 1 - yes.
422
  void BranchFitterFast(const L1Branch& t, L1TrackPar& T, const bool dir, const fvec qp0 = 0.,
Administrator's avatar
Administrator committed
423
424
                        const bool initParams = true);

425
426
427
428
429
430
  /// Fits track. more precise than FitterFast
  /// \param t - track with hits
  /// \param T - track parameters
  /// \param dir - false - forward, true - backward
  /// \param qp0 - momentum for extrapolation
  /// \param initParams - should be params ititialized. 1 - yes.
431
  void BranchFitter(const L1Branch& t, L1TrackPar& T, const bool dir, const fvec qp0 = 0.,
Administrator's avatar
Administrator committed
432
433
                    const bool initParams = true);

434
435
436
437
438
  /// Finds additional hits for already found track
  /// \param t - track with hits
  /// \param T - track params
  /// \param dir - 0 - forward, 1 - backward
  /// \param qp0 - momentum for extrapolation
439
  void FindMoreHits(L1Branch& t, L1TrackPar& T, const bool dir, const fvec qp0 = 0.0);
Administrator's avatar
Administrator committed
440
441

  /// Find additional hits for existing track
442
  /// \return chi2
Administrator's avatar
Administrator committed
443
444
445
  fscal BranchExtender(L1Branch& t);

  /// ----- Subroutines used by L1Algo::CAMergeClones() ------
446
447
448
449
  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]);
450
  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
451
                    fvec* chi2);
Sergei Zharko's avatar
Sergei Zharko committed
452
  ///
453
454
  void CAMergeClones();

Administrator's avatar
Administrator committed
455

456
457
458
  inline __attribute__((always_inline)) void PackLocation(unsigned int& location, unsigned int& triplet,
                                                          unsigned int iStation, unsigned int& thread)
  {
Administrator's avatar
Administrator committed
459
460
461
    location = (triplet << 11) | (thread << 3) | iStation;
  }

462
463
  inline __attribute__((always_inline)) void UnPackStation(unsigned int& location, unsigned int& iStation)
  {
Administrator's avatar
Administrator committed
464
465
466
    iStation = location & 0x7;
  }

467
468
  inline __attribute__((always_inline)) void UnPackThread(unsigned int& location, unsigned int& thread)
  {
Administrator's avatar
Administrator committed
469
470
471
    thread = (location >> 3) & 0xFF;
  }

472
473
  inline __attribute__((always_inline)) void UnPackTriplet(unsigned int& location, unsigned int& triplet)
  {
Administrator's avatar
Administrator committed
474
475
476
    triplet = (location >> 11);
  }

477
478
  inline __attribute__((always_inline)) void SetFStation(unsigned char& flag, unsigned int iStation)
  {
Administrator's avatar
Administrator committed
479
480
    flag = iStation * 4 + (flag % 4);
  }
481
  inline __attribute__((always_inline)) void SetFUsed(unsigned char& flag) { flag |= 0x02; }
Administrator's avatar
Administrator committed
482
  //   void SetFUsedD   ( unsigned char &flag ){ flag |= 0x01; }
483
  inline __attribute__((always_inline)) void SetFUnUsed(unsigned char& flag) { flag &= 0xFC; }
Administrator's avatar
Administrator committed
484
485
486
  //   void SetFUnUsedD ( unsigned char &flag ){ flag &= 0xFE; }

  /// Prepare the portion of left hits data
487
  void f10(  // input
488
    Tindex start_lh, Tindex n1_l, L1HitPoint* StsHits_l,
Administrator's avatar
Administrator committed
489
    // output
490
    fvec* u_front_l, fvec* u_back_l, fvec* zPos_l, L1HitIndex_t* hitsl, fvec* HitTime_l, fvec* HitTimeEr, fvec* Event_l,
491
    fvec* d_x, fvec* d_y, fvec* d_xy, fvec* d_u, fvec* d_v);
Administrator's avatar
Administrator committed
492
493

  /// Get the field approximation. Add the target to parameters estimation. Propagate to middle station.
494
  void f11(  // input
495
496
497
    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
498
    // output
499
    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
500
501

  /// Find the doublets. Reformat data in the portion of doublets.
502
  void f20(  // input
503
    Tindex n1, L1Station& stal, L1Station& stam, L1HitPoint* vStsHits_m, L1TrackPar* T_1, L1HitIndex_t* hitsl_1,
Administrator's avatar
Administrator committed
504
505

    // output
506
    Tindex& n2, L1Vector<L1HitIndex_t>& i1_2,
Administrator's avatar
Administrator committed
507

508
#ifdef DOUB_PERFORMANCE
509
    L1Vector<L1HitIndex_t>& hitsl_2,
Administrator's avatar
Administrator committed
510
#endif  // DOUB_PERFORMANCE
511
    L1Vector<L1HitIndex_t>& hitsm_2, fvec* Event, L1Vector<char>& lmDuplets);
Administrator's avatar
Administrator committed
512
513
514

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

518
    int istam, int istar, L1HitPoint* vStsHits_m, L1TrackPar* T_1, L1FieldRegion* fld_1, L1HitIndex_t* hitsl_1,
Administrator's avatar
Administrator committed
519

520
    Tindex n2, L1Vector<L1HitIndex_t>& hitsm_2, L1Vector<L1HitIndex_t>& i1_2,
Administrator's avatar
Administrator committed
521

Sergey Gorbunov's avatar
Sergey Gorbunov committed
522
    const L1Vector<char>& mrDuplets,
Administrator's avatar
Administrator committed
523
    // output
524
525
    Tindex& n3, nsL1::vector<L1TrackPar>::TSimd& T_3, L1Vector<L1HitIndex_t>& hitsl_3, L1Vector<L1HitIndex_t>& hitsm_3,
    L1Vector<L1HitIndex_t>& hitsr_3, nsL1::vector<fvec>::TSimd& u_front_3, nsL1::vector<fvec>::TSimd& u_back_3,
Administrator's avatar
Administrator committed
526
    nsL1::vector<fvec>::TSimd& z_Pos_3,
527
528
    //    nsL1::vector<fvec>::TSimd& dx_,
    //    nsL1::vector<fvec>::TSimd& dy_,
529
    nsL1::vector<fvec>::TSimd& du_, nsL1::vector<fvec>::TSimd& dv_, nsL1::vector<fvec>::TSimd& timeR,
Administrator's avatar
Administrator committed
530
531
532
    nsL1::vector<fvec>::TSimd& timeER);

  /// Add the right hits to parameters estimation.
533
  void f31(  // input
534
    Tindex n3_V, L1Station& star, nsL1::vector<fvec>::TSimd& u_front_3, nsL1::vector<fvec>::TSimd& u_back_3,
Administrator's avatar
Administrator committed
535
    nsL1::vector<fvec>::TSimd& z_Pos_3,
536
537
    //    nsL1::vector<fvec>::TSimd& dx_,
    //    nsL1::vector<fvec>::TSimd& dy_,
538
    nsL1::vector<fvec>::TSimd& du_, nsL1::vector<fvec>::TSimd& dv_, nsL1::vector<fvec>::TSimd& timeR,
Administrator's avatar
Administrator committed
539
540
541
542
543
544
    nsL1::vector<fvec>::TSimd& timeER,
    // output
    nsL1::vector<L1TrackPar>::TSimd& T_3);

  /// Refit Triplets.
  void f32(  // input
Sergei Zharko's avatar
Sergei Zharko committed
545
546
    Tindex n3, int istal, nsL1::vector<L1TrackPar>::TSimd& T_3, L1Vector<L1HitIndex_t>& hitsl_3,
    L1Vector<L1HitIndex_t>& hitsm_3, L1Vector<L1HitIndex_t>& hitsr_3, int nIterations = 0);
Administrator's avatar
Administrator committed
547
548

  /// Select triplets. Save them into vTriplets.
549
  void f4(  // input
550
551
    Tindex n3, int istal, int istam, int istar, nsL1::vector<L1TrackPar>::TSimd& T_3, L1Vector<L1HitIndex_t>& hitsl_3,
    L1Vector<L1HitIndex_t>& hitsm_3, L1Vector<L1HitIndex_t>& hitsr_3,
Administrator's avatar
Administrator committed
552
    // output
553
    Tindex& nstaltriplets, L1Vector<L1HitIndex_t>* hitsn_3 = 0, L1Vector<L1HitIndex_t>* hitsr_5 = 0
Administrator's avatar
Administrator committed
554
555
556
557
558
559
560
561

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


  /// Find neighbours of triplets. Calculate level of triplets.
562
  void f5(  // input
Administrator's avatar
Administrator committed
563
564
565
566
567
568
    // output
    int* nlevel);


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

    // output
572
    L1TrackPar* T_1, L1FieldRegion* fld_1, L1HitIndex_t* hitsl_1,
Administrator's avatar
Administrator committed
573

Sergey Gorbunov's avatar
Sergey Gorbunov committed
574
    L1Vector<char>& lmDuplets,
Administrator's avatar
Administrator committed
575
576


577
    Tindex& n_2, L1Vector<L1HitIndex_t>& i1_2, L1Vector<L1HitIndex_t>& hitsm_2);
Administrator's avatar
Administrator committed
578
579
580

  /// Find triplets on station
  void TripletsStaPort(  // input
Sergei Zharko's avatar
Sergei Zharko committed
581
582
    int istal, int istam, int istar, Tindex& nstaltriplets, L1TrackPar* T_1, L1FieldRegion* fld_1,
    L1HitIndex_t* hitsl_1,
583

584
    Tindex& n_2, L1Vector<L1HitIndex_t>& i1_2, L1Vector<L1HitIndex_t>& hitsm_2,
Administrator's avatar
Administrator committed
585

Sergey Gorbunov's avatar
Sergey Gorbunov committed
586
    const L1Vector<char>& mrDuplets
Administrator's avatar
Administrator committed
587
588
589
590
591
592
593
594
595

    // output


  );


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

596
597
598
  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
599
600

  void FilterFirst(L1TrackPar& track, fvec& x, fvec& y, L1Station& st);
601
602
603
604
  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
605
                   fvec& dxy);
606
  void FilterFirstL(L1TrackParFit& track, fvec& x, fvec& y, fvec& t, fvec& t_er, L1Station& st, fvec& dx, fvec& dy,
Administrator's avatar
Administrator committed
607
608
                    fvec& dxy);

609
#ifdef TBB
610
611
  enum
  {
Administrator's avatar
Administrator committed
612
613
614
    nthreads = 3,  // number of threads
    nblocks  = 1   // number of stations on one thread
  };
615
616
617

  friend class ParalleledDup;
  friend class ParalleledTrip;
Administrator's avatar
Administrator committed
618
#endif  // TBB
619
#ifdef TBB2
Administrator's avatar
Administrator committed
620
public:
621
  Tindex thrId;
Administrator's avatar
Administrator committed
622
623
624
625
626
#endif  // TBB2
private:
  /// =================================  DATA PART  =================================

  /// ----- Different parameters of CATrackFinder -----
627

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

Administrator's avatar
Administrator committed
630
631
  // fNFindIterations - set number of interation for trackfinding
  // itetation of finding:
632
#ifdef FIND_GAPED_TRACKS
633
634
  enum
  {
Administrator's avatar
Administrator committed
635
636
637
638
639
640
641
642
643
644
    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
645
646
  };
#ifdef TRACKS_FROM_TRIPLETS
647
648
  enum
  {
Administrator's avatar
Administrator committed
649
650
    fNFindIterations = TRACKS_FROM_TRIPLETS_ITERATION + 1
  };  // TODO investigate kAllPrimJumpIter & kAllSecJumpIter
651
#else  // not TRACKS_FROM_TRIPLETS
Administrator's avatar
Administrator committed
652

653
654
  enum
  {
655
    fNFindIterations = 4
Administrator's avatar
Administrator committed
656
  };  // TODO investigate kAllPrimJumpIter & kAllSecJumpIter
657

Administrator's avatar
Administrator committed
658

659
660
#endif  // TRACKS_FROM_TRIPLETS
#else  // not FIND_GAPED_TRACKS
661
662
  enum
  {
Administrator's avatar
Administrator committed
663
664
665
666
667
668
669
670
671
    kFastPrimIter = 0,  // primary fast tracks
    kAllPrimIter,       // primary all tracks
    kAllSecIter,        // secondary all tracks
    kFastPrimJumpIter,  // disabled
    kAllPrimJumpIter,   // disabled
    kFastPrimIter2,
    kAllSecJumpIter,
    kAllPrimEIter,
    kAllSecEIter
672
  };
Administrator's avatar
Administrator committed
673
674
#endif  // FIND_GAPED_TRACKS

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

677
678
679
680
681
  float fTrackChi2Cut {10.f};
  float fTripletChi2Cut {5.f};  // cut for selecting triplets before collecting tracks.per one DoF
  float fDoubletChi2Cut {5.f};
  float fTimeCut1 {0.f};  // TODO: please, specify "1" and "2" (S.Zharko)
  float fTimeCut2 {0.f};
Administrator's avatar
Administrator committed
682

683
  /// 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)
684
  fvec fMaxDZ {L1Utils::kNaN};
Administrator's avatar
Administrator committed
685
686
687

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

688
689
690
691
692
  float fPickGather {L1Utils::kNaN};     ///< same for attaching additional hits to track
  float fPickNeighbour {L1Utils::kNaN};  ///< (fPickNeighbour < dp/dp_error)  =>  triplets are neighbours
  fvec fMaxInvMom {L1Utils::kNaN};       ///< max considered q/p for tracks
  fvec fMaxSlopePV {L1Utils::kNaN};      ///< max slope (tx\ty) in prim vertex
  float fMaxSlope {L1Utils::kNaN};       ///< max slope (tx\ty) in 3d hit position of a triplet
Sergei Zharko's avatar
Sergei Zharko committed
693
694
695
  fvec fRealTargetX {L1Utils::kNaN};     ///< real target position x coordinate
  fvec fRealTargetY {L1Utils::kNaN};     ///< real target position y coordinate
  fvec fRealTargetZ {L1Utils::kNaN};     ///< real target position z coordinate
696
697
698
  fvec fTargX {L1Utils::kNaN};           ///< target position x coordinate for the current iteration (modifiable)
  fvec fTargY {L1Utils::kNaN};           ///< target position y coordinate for the current iteration (modifiable)
  fvec fTargZ {L1Utils::kNaN};           ///< target position z coordinate for the current iteration (modifiable)
699

Sergei Zharko's avatar
Sergei Zharko committed
700
  L1FieldValue fTargB _fvecalignment {};               // field in the target point
Sergey Gorbunov's avatar
Sergey Gorbunov committed
701
  L1XYMeasurementInfo TargetXYInfo _fvecalignment {};  // target constraint  [cm]
Administrator's avatar
Administrator committed
702
703


704
705
  L1FieldRegion fVtxFieldRegion _fvecalignment {};  // really doesn't used
  L1FieldValue fVtxFieldValue _fvecalignment {};    // field at teh vertex position.
Administrator's avatar
Administrator committed
706
707
708

  //  int TripNumThread;

Sergei Zharko's avatar
Sergei Zharko committed
709
710
  int fTrackingLevel {2};       // currently not used
  int fGhostSuppression {1};    // currently not used
711
  float fMomentumCutOff {0.2};  // currently not used
Administrator's avatar
Administrator committed
712
713

  /// ----- Debug features -----
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#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