L1Algo.h 26.1 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
// =====>dispatched<===== // #define USE_RL_TABLE
32
33
34
35
36
#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
/*********************************************************************************
 ************ TEMPORARY MACROS, SHOULD BE REMOVED (TODO!!, S. Zharko)  ***********
 *                                                                               */
Sergei Zharko's avatar
Sergei Zharko committed
43

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"
71
#include "L1Utils.h"
Sergey Gorbunov's avatar
Sergey Gorbunov committed
72
#include "L1Vector.h"
73
74
75
76
77
78
79
80
81
82
83
84

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

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

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

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

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

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


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

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

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

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


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

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

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

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

199
200
  void PrintHits();

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

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

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

210
  float GetMaxInvMom() const { return fMaxInvMom[0]; }
211

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

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

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

Sergei Zharko's avatar
Sergei Zharko committed
222

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

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

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

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

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

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


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

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

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

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

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

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

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


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


  /// --- data used during finding iterations

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


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


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

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

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

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


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

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

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

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

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

sergeizharko's avatar
sergeizharko committed
372
373

  /// Sets L1Algo parameters object
374
375
376
377
378
379
  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
380

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

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

sergeizharko's avatar
sergeizharko committed
387

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

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

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

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

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

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

Administrator's avatar
Administrator committed
430

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

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

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

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

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

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

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

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

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

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

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

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

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

Sergey Gorbunov's avatar
Sergey Gorbunov committed
497
    const L1Vector<char>& mrDuplets,
Administrator's avatar
Administrator committed
498
    // output
Sergey Gorbunov's avatar
Sergey Gorbunov committed
499
500
    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
501
    nsL1::vector<fvec>::TSimd& z_Pos_3,
502
503
    //    nsL1::vector<fvec>::TSimd& dx_,
    //    nsL1::vector<fvec>::TSimd& dy_,
504
    nsL1::vector<fvec>::TSimd& du_, nsL1::vector<fvec>::TSimd& dv_, nsL1::vector<fvec>::TSimd& timeR,
Administrator's avatar
Administrator committed
505
506
507
    nsL1::vector<fvec>::TSimd& timeER);

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

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

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

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


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


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

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

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


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

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

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

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

    // output


  );


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

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

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

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

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

  /// ----- Different parameters of CATrackFinder -----
601

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

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

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

Administrator's avatar
Administrator committed
632

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

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

651
652
653
654
655
  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
656

657
  /// 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)
658
  fvec fMaxDZ {L1Utils::kNaN};
Administrator's avatar
Administrator committed
659
660
661

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

662
663
664
665
666
  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
667
  fvec fCbmTargetX {L1Utils::kNaN};      // target position
668
669
670
671
672
673
  fvec fCbmTargetY {L1Utils::kNaN};
  fvec fCbmTargetZ {L1Utils::kNaN};
  fvec fTargX {L1Utils::kNaN};  // target position for the current iteration
  fvec fTargY {L1Utils::kNaN};
  fvec fTargZ {L1Utils::kNaN};

Sergei Zharko's avatar
Sergei Zharko committed
674
  L1FieldValue fTargB _fvecalignment {};               // field in the target point
Sergey Gorbunov's avatar
Sergey Gorbunov committed
675
  L1XYMeasurementInfo TargetXYInfo _fvecalignment {};  // target constraint  [cm]
Administrator's avatar
Administrator committed
676
677


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

  //  int TripNumThread;

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

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