CbmRoot
L1AlgoEfficiencyPerformance.h
Go to the documentation of this file.
1 #ifndef L1AlgoEfficiencyPerformance_h
2 #define L1AlgoEfficiencyPerformance_h
3 
11 #include "CbmL1.h"
12 #include "CbmL1Counters.h"
13 #include "CbmL1Def.h"
14 #include "L1Algo/L1Algo.h"
15 #include "L1Algo/L1StsHit.h"
16 
17 #include <iostream>
18 #include <vector>
19 
20 using std::cout;
21 using std::endl;
22 using std::vector;
23 
24 const int MaxNStations = 10;
25 
26 template<int NHits>
27 class L1Tracklet {
28 public:
29  L1Tracklet() : mcTrackId(-1), iStation(-1) {};
30 
31  static bool compare(const L1Tracklet<NHits>& a, const L1Tracklet<NHits>& b) {
32  return (a.mcTrackId < b.mcTrackId)
33  || ((a.mcTrackId == b.mcTrackId) && (a.iStation < b.iStation));
34  }
35 
36  bool operator==(const L1Tracklet<NHits>& a) {
37  return ((a.mcTrackId == mcTrackId) && (a.iStation == iStation));
38  }
39  bool operator!=(const L1Tracklet<NHits>& a) { return !operator==(a); }
40  bool operator<(const L1Tracklet<NHits>& a) { return compare(*this, a); }
41 
42  int mcTrackId;
43  int iStation;
44 };
45 
46 // reconstructed Tracklet
47 template<int NHits>
48 class L1RTracklet : public L1Tracklet<NHits> {
49 public:
51  L1RTracklet(THitI* ih_) : L1Tracklet<NHits>() {
52  for (int iih = 0; iih < NHits; iih++) {
53  i[iih] = ih_[iih];
54  }
55  };
56 
57  bool IsGhost() {
59  }; // is it ghost or reconstructed
60 
61  bool operator!=(const L1Tracklet<NHits>& a) {
63  }
64 
65  bool operator==(const L1RTracklet<NHits>& a) {
66  bool flag = 1;
67  // flag &= L1Tracklet<NHits>::operator==(a);
68  for (int ih = 0; ih < NHits; ih++) {
69  flag &= (a.i[ih] == i[ih]);
70  }
71  return flag;
72  }
73  bool operator!=(const L1RTracklet<NHits>& a) { return !operator==(a); }
74 
75  THitI i[NHits]; // indices of left, middle and right hits
76 };
77 
78 // MC Tracklet
79 template<int NHits>
80 class L1MTracklet : public L1Tracklet<NHits> {
81 public:
83 
84  void AddReconstructed(int recoId) { recoIds.push_back(recoId); };
86 
87  int GetNClones() {
88  return (recoIds.size() > 1) ? recoIds.size() - 1 : 0;
89  }; // number of created clones
90  bool IsReconstructed() { return recoIds.size(); }; // is it reconstructed
92  return isReconstructable;
93  }; // is it possible to reconstruct
94  bool IsPrimary() {
95  return mcMotherTrackId < 0;
96  }; // is it primary or secondary
97 
99  float p; // momentum
100  vector<int> recoIds; // indices of reco tracklets in sorted recoArray
101 
102  vector<int>
103  hitIds[NHits]; // indices of hits in L1::vStsHits. Separated by stations
104 private:
105  bool isReconstructable; // is it reconstructed
106 };
107 
108 // tracklets categories
111  AddCounter("total", "Allset efficiency");
112  AddCounter("fast", "Refset efficiency");
113  AddCounter("fast_prim", "RefPrim efficiency");
114  AddCounter("fast_sec", "RefSec efficiency");
115  AddCounter("slow", "Extra efficiency");
116  AddCounter("slow_prim", "ExtraPrim efficiency");
117  AddCounter("slow_sec", "ExtraSec efficiency");
118  }
119 };
120 
121 
122 template<int NHits = 3>
124 public:
128  void Init();
129 
130  bool AddOne(
131  THitI* ih_); // add one recoTracklets. Return is it reconstructed or not
132  void CalculateEff();
133  void Print(); // Print efficiencies
134  void Print(TString title = "Triplets performance statistic",
135  bool station = 0); // Print efficiencies
136 
137 private:
138  void FillMC(); // collect mcTracklets from mcTracks
139  void MatchTracks();
140 
142 
143  vector<L1RecoTracklet> recoTracklets;
144  vector<L1MCTracklet> mcTracklets;
145 
146  TL1AlgoEfficiencies ntra; // current event efficiencies
147  TL1AlgoEfficiencies NTRA; // efficiencies statistic
148 
149  TL1AlgoEfficiencies ntra_sta[MaxNStations]; // for each station separately
151 };
152 
153 // ===================================================================================
154 
155 template<int NHits>
157  fL1 = CbmL1::Instance();
158  recoTracklets.clear();
159  mcTracklets.clear();
160  ntra = TL1AlgoEfficiencies();
161  for (int iSta = 0; iSta < MaxNStations; iSta++) {
162  ntra_sta[iSta] = ntra;
163  }
164 
165  FillMC();
166 };
167 
168 template<int NHits>
170  for (vector<CbmL1MCTrack>::iterator mtraIt = fL1->vMCTracks.begin();
171  mtraIt != fL1->vMCTracks.end();
172  mtraIt++) {
173  CbmL1MCTrack& mtra = *(mtraIt);
174  // if( ! mtra.IsReconstructable() ) continue;
175 
176  const int NMCPoints = mtra.Points.size();
177  // const int NIter = mtra.NStations()-NHits+1; // number of possible tracklets
178  int lastIterSta = -1;
179  for (int iterOffset = 0; iterOffset < NMCPoints;
180  iterOffset++) { // first mcPoint on the station
181  // const int iterMcId = mtra.Points[iterOffset];
182  int iterSta = fL1->vMCPoints[mtra.Points[iterOffset]].iStation;
183  if (iterSta == lastIterSta) continue; // find offset for next station
184  lastIterSta = iterSta;
185 
186  // read hits
187  // take all points on each station
188  // TODO: Should we create all possible tracklets?
189  L1MCTracklet trlet; // TODO: don't use hits in mcTracklet
190  for (int is = 0, offset = iterOffset;
191  ((offset < NMCPoints) && (is < NHits));
192  offset++) {
193  const int mcId = mtra.Points[offset];
194  CbmL1MCPoint* mcPs = &(fL1->vMCPoints[mcId]);
195  is = mcPs->iStation - iterSta;
196 
197  if (is < NHits) {
198  const int NPointHits = mcPs->hitIds.size();
199  for (int ih = 0; ih < NPointHits; ih++) {
200  trlet.hitIds[is].push_back(mcPs->hitIds[ih]);
201  }
202  }
203  } // for is
204  // check tracklet
205  bool good = 1;
206  for (int is = 0; is < NHits; is++) {
207  good &= trlet.hitIds[is].size();
208  }
209  if (!good) continue; // can't create tracklet
210 
211  trlet.iStation = iterSta;
212  trlet.mcTrackId = mtra.ID;
213  trlet.mcMotherTrackId = mtra.mother_ID;
214  trlet.p = mtra.p;
215  if (mtra.p > fL1->MinRecoMom) trlet.SetAsReconstructable();
216 
217  mcTracklets.push_back(trlet);
218  } // for Iter = stations
219  } // for mcTracks
220 } // void L1AlgoEfficiencyPerformance::FillMC()
221 
222 
223 template<int NHits>
225  L1RecoTracklet trlet(iHits);
226 
227  // --- check is all hits belong to one mcTrack ---
228  vector<int> mcIds[NHits];
229 
230  // obtain mc data
231  for (int iih = 0; iih < NHits; iih++) {
232  int nMC = fL1->vStsHits[iHits[iih]].mcPointIds.size();
233  for (int iMC = 0; iMC < nMC; iMC++) {
234  const int iMCP = fL1->vStsHits[iHits[iih]].mcPointIds[iMC];
235  int mcId = fL1->vMCPoints[iMCP].ID;
236  mcIds[iih].push_back(mcId);
237  } // for mcPoints
238  } // for hits
239 
240  // find all reconstructed track id-s
241  for (int level = 0; level < NHits - 1; level++) {
242  vector<int>& mcs1 = mcIds[level];
243  vector<int>& mcs2 = mcIds[level + 1];
244 
245  // leave only matched ID-s.
246  for (unsigned int i2 = 0; i2 < mcs2.size(); i2++) {
247  int& mc2 = mcs2[i2];
248  bool flag = 0;
249  // is the same ID on prev station?
250  for (unsigned int i1 = 0; i1 < mcs1.size(); i1++) {
251  flag |= (mcs1[i1] == mc2);
252  }
253  if (!flag) mc2 = -2;
254  } // for i2
255  }
256 
257  // use first found // TODO: save all!!!
258  vector<int>& mcsN = mcIds[NHits - 1];
259  for (unsigned int i = 0; i < mcsN.size(); i++) {
260  if (mcsN[i] >= 0) {
261  trlet.mcTrackId = mcsN[i];
262  trlet.iStation =
263  fL1->vMCPoints[fL1->vStsHits[iHits[0]].mcPointIds[0]].iStation;
264  break;
265  }
266  }
267 
268  // --- save ---
269  // if ( std::find(recoTracklets.begin(), recoTracklets.end(), trlet) == recoTracklets.end() )
270  recoTracklets.push_back(trlet);
271 
272  return (trlet.mcTrackId >= 0);
273 }; // inline bool L1AlgoEfficiencyPerformance::AddOne(THitI ihl, THitI ihm, THitI ihr)
274 
275 
276 template<int NHits>
278  MatchTracks() { // TODO: If we create all possible tracklets on NHits station - addapt to it.
279  std::sort(
280  recoTracklets.begin(), recoTracklets.end(), L1Tracklet<NHits>::compare);
281  std::sort(mcTracklets.begin(), mcTracklets.end(), L1Tracklet<NHits>::compare);
282 
283  const int NRecoTrlets = recoTracklets.size();
284  const int NMCTrlets = mcTracklets.size();
285  // cout << NMCTrlets << " " << NRecoTrlets << " " << endl;
286  for (int iReco = 0, iMC = 0; (iReco < NRecoTrlets) && (iMC < NMCTrlets);) {
287  L1MCTracklet& mcTrlet = mcTracklets[iMC];
288  L1RecoTracklet& recoTrlet = recoTracklets[iReco];
289 
290  // if (recoTrlet.mcTrackId >= 0)
291  // cout << iMC << " " << mcTrlet.iStation << " " << mcTrlet.mcTrackId << " " << mcTrlet.p << " | "
292  // << iReco << " " << recoTrlet.iStation << " " << recoTrlet.mcTrackId << " " << endl;
293 
294  if (recoTrlet != mcTrlet) {
295  if (recoTrlet < mcTrlet)
296  iReco++;
297  else
298  iMC++;
299  } else {
300  // cout << iMC << " " << mcTrlet.iStation << " " << mcTrlet.mcTrackId << " " << mcTrlet.p << " | "
301  // << iReco << " " << recoTrlet.iStation << " " << recoTrlet.mcTrackId << " " << endl;
302  // check if same tracklet has been matched, and save it if not.
303  bool flag = 1;
304  const int nReco = mcTrlet.recoIds.size();
305  for (int iR = 0; iR < nReco; iR++) {
306  flag &= (recoTrlet != recoTracklets[mcTrlet.recoIds[iR]]);
307  }
308  if (flag) mcTrlet.AddReconstructed(iReco);
309 
310  iReco++; // suppose we have only one tracklet with certain (Id,Station)
311  }
312  };
313 } // void L1AlgoEfficiencyPerformance::MatchTracks()
314 
315 template<int NHits>
317  MatchTracks();
318 
319  const int NRecoTrlets = recoTracklets.size();
320  for (int iReco = 0; iReco < NRecoTrlets; iReco++) {
321  L1RecoTracklet& reco = recoTracklets[iReco];
322  ntra /*_sta[reco.iStation]*/.ghosts += reco.IsGhost();
323  }
324 
325  const int NMCTrlets = mcTracklets.size();
326  for (int iMC = 0; iMC < NMCTrlets; iMC++) {
327  L1MCTracklet& mtra = mcTracklets[iMC];
328  if (!mtra.IsReconstructable()) continue;
329  int iSta = mtra.iStation;
330 
331  // find used constans
332  const bool reco = mtra.IsReconstructed();
333 
334  // count tracklets
335  ntra_sta[iSta].Inc(reco, "total");
336 
337  if (mtra.p > fL1->MinRefMom) { // reference tracks
338  ntra_sta[iSta].Inc(reco, "fast");
339  if (mtra.IsPrimary()) { // reference primary
340  ntra_sta[iSta].Inc(reco, "fast_prim");
341 
342  // // Debug
343  // if (!reco){
344  // cout << "iMC/N " << iMC << "/" << NMCTrlets << " sta " << mtra.iStation << " trackId " << mtra.mcTrackId << " momentum " << mtra.p << ". MotherTrackId " << mtra.mcMotherTrackId << endl;
345  // for (int iSta2 = 0; iSta2 < NHits; iSta2++){
346  // cout << iSta2 << ": ";
347  // const int NPointHits = mtra.hitIds[iSta2].size();
348  // for (int iH = 0; iH < NPointHits; iH++){
349  // cout << mtra.hitIds[iSta2][iH] << "";
350  // cout << "(" << fL1->vHitStore[mtra.hitIds[iSta2][iH]].x << "\\" << fL1->vHitStore[mtra.hitIds[iSta2][iH]].y << "= " << fL1->vHitStore[mtra.hitIds[iSta2][iH]].x/fL1->vHitStore[mtra.hitIds[iSta2][iH]].y << " ) ";
351  // }
352  // cout << endl;
353  // }
354  // }
355 
356  } else { // reference secondary
357  ntra_sta[iSta].Inc(reco, "fast_sec");
358  }
359  } else { // extra set of tracks
360  ntra_sta[iSta].Inc(reco, "slow");
361  if (mtra.IsPrimary()) { // extra primary
362  ntra_sta[iSta].Inc(reco, "slow_prim");
363  } else {
364  ntra_sta[iSta].Inc(reco, "slow_sec");
365  }
366  } // if extra
367  ntra_sta[iSta].clones += mtra.GetNClones(); // TODO:check if works
368  }; //for mcTracklets
369 
370  for (int iSta = 0; iSta < MaxNStations; iSta++) {
371  ntra += ntra_sta[iSta];
372  NTRA_STA[iSta] += ntra_sta[iSta];
373  ntra_sta[iSta].CalcEff();
374  NTRA_STA[iSta].CalcEff();
375  }
376  NTRA += ntra;
377  ntra.CalcEff();
378  NTRA.CalcEff();
379 } // void L1AlgoEfficiencyPerformance::CalculateEff()
380 
381 
382 template<int NHits>
384  bool stations) {
385  // cout << endl;
386  // cout << "-------- Triplets performance ----------" << endl;
387  // ntra.PrintEff();
388 
389  cout << endl;
390  cout << "-------- " << title << " ----------" << endl;
391  NTRA.PrintEff();
392  cout << endl;
393 
394  if (stations) {
395  for (int iSta = 0; iSta < fL1->NStation - NHits + 1; iSta++) {
396  TString title_sta = title;
397  title_sta += " station ";
398  title_sta += iSta;
399  cout << endl;
400  cout << "-------- " << title_sta << " ----------" << endl;
401  NTRA_STA[iSta].PrintEff();
402  cout << endl;
403  }
404  }
405 };
406 
407 
408 #endif
L1Tracklet::operator!=
bool operator!=(const L1Tracklet< NHits > &a)
Definition: L1AlgoEfficiencyPerformance.h:39
L1Algo.h
L1Tracklet::L1Tracklet
L1Tracklet()
Definition: L1AlgoEfficiencyPerformance.h:29
CbmL1MCPoint
Definition: CbmL1MCPoint.h:23
L1MTracklet::GetNClones
int GetNClones()
Definition: L1AlgoEfficiencyPerformance.h:87
L1RTracklet::operator==
bool operator==(const L1RTracklet< NHits > &a)
Definition: L1AlgoEfficiencyPerformance.h:65
L1MTracklet::SetAsReconstructable
void SetAsReconstructable()
Definition: L1AlgoEfficiencyPerformance.h:85
L1Tracklet::mcTrackId
int mcTrackId
Definition: L1AlgoEfficiencyPerformance.h:42
L1RTracklet::IsGhost
bool IsGhost()
Definition: L1AlgoEfficiencyPerformance.h:57
L1MTracklet::p
float p
Definition: L1AlgoEfficiencyPerformance.h:99
L1MTracklet::IsPrimary
bool IsPrimary()
Definition: L1AlgoEfficiencyPerformance.h:94
L1AlgoEfficiencyPerformance::Init
void Init()
Definition: L1AlgoEfficiencyPerformance.h:156
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
L1Tracklet::operator<
bool operator<(const L1Tracklet< NHits > &a)
Definition: L1AlgoEfficiencyPerformance.h:40
L1AlgoEfficiencyPerformance::recoTracklets
vector< L1RecoTracklet > recoTracklets
Definition: L1AlgoEfficiencyPerformance.h:143
L1MTracklet::IsReconstructable
bool IsReconstructable()
Definition: L1AlgoEfficiencyPerformance.h:91
L1AlgoEfficiencyPerformance::L1AlgoEfficiencyPerformance
L1AlgoEfficiencyPerformance()
Definition: L1AlgoEfficiencyPerformance.h:127
L1AlgoEfficiencyPerformance::ntra_sta
TL1AlgoEfficiencies ntra_sta[MaxNStations]
Definition: L1AlgoEfficiencyPerformance.h:149
L1MTracklet::hitIds
vector< int > hitIds[NHits]
Definition: L1AlgoEfficiencyPerformance.h:103
L1MTracklet
Definition: L1AlgoEfficiencyPerformance.h:80
L1AlgoEfficiencyPerformance::NTRA_STA
TL1AlgoEfficiencies NTRA_STA[MaxNStations]
Definition: L1AlgoEfficiencyPerformance.h:150
L1RTracklet::operator!=
bool operator!=(const L1Tracklet< NHits > &a)
Definition: L1AlgoEfficiencyPerformance.h:61
CbmL1.h
L1AlgoEfficiencyPerformance::L1MCTracklet
L1MTracklet< NHits > L1MCTracklet
Definition: L1AlgoEfficiencyPerformance.h:126
L1Tracklet::compare
static bool compare(const L1Tracklet< NHits > &a, const L1Tracklet< NHits > &b)
Definition: L1AlgoEfficiencyPerformance.h:31
L1AlgoEfficiencyPerformance
Definition: L1AlgoEfficiencyPerformance.h:123
L1MTracklet::AddReconstructed
void AddReconstructed(int recoId)
Definition: L1AlgoEfficiencyPerformance.h:84
L1AlgoEfficiencyPerformance::fL1
CbmL1 * fL1
Definition: L1AlgoEfficiencyPerformance.h:141
L1AlgoEfficiencyPerformance::CalculateEff
void CalculateEff()
Definition: L1AlgoEfficiencyPerformance.h:316
CbmL1Def.h
THitI
unsigned int THitI
Definition: L1StsHit.h:8
L1MTracklet::mcMotherTrackId
int mcMotherTrackId
Definition: L1AlgoEfficiencyPerformance.h:96
L1MTracklet::L1MTracklet
L1MTracklet()
Definition: L1AlgoEfficiencyPerformance.h:82
CbmL1::Instance
static CbmL1 * Instance()
Definition: CbmL1.h:129
L1RTracklet::L1RTracklet
L1RTracklet()
Definition: L1AlgoEfficiencyPerformance.h:50
L1AlgoEfficiencyPerformance::NTRA
TL1AlgoEfficiencies NTRA
Definition: L1AlgoEfficiencyPerformance.h:147
L1RTracklet::L1RTracklet
L1RTracklet(THitI *ih_)
Definition: L1AlgoEfficiencyPerformance.h:51
CbmL1MCTrack::p
double p
Definition: CbmL1MCTrack.h:31
TL1AlgoEfficiencies
Definition: L1AlgoEfficiencyPerformance.h:109
L1Tracklet
Definition: L1AlgoEfficiencyPerformance.h:27
CbmL1MCTrack::Points
vector< int > Points
Definition: CbmL1MCTrack.h:33
CbmL1MCTrack::mother_ID
int mother_ID
Definition: CbmL1MCTrack.h:32
MaxNStations
const int MaxNStations
Definition: L1AlgoEfficiencyPerformance.h:24
CbmL1MCTrack::ID
int ID
Definition: CbmL1MCTrack.h:32
CbmL1MCPoint::iStation
int iStation
Definition: CbmL1MCPoint.h:61
L1AlgoEfficiencyPerformance::L1RecoTracklet
L1RTracklet< NHits > L1RecoTracklet
Definition: L1AlgoEfficiencyPerformance.h:125
L1AlgoEfficiencyPerformance::Print
void Print()
TL1Efficiencies::AddCounter
virtual void AddCounter(TString shortname, TString name)
Definition: CbmL1Counters.h:160
CbmL1MCPoint::hitIds
vector< int > hitIds
Definition: CbmL1MCPoint.h:73
CbmL1Counters.h
L1RTracklet::i
THitI i[NHits]
Definition: L1AlgoEfficiencyPerformance.h:75
L1MTracklet::IsReconstructed
bool IsReconstructed()
Definition: L1AlgoEfficiencyPerformance.h:90
L1AlgoEfficiencyPerformance::mcTracklets
vector< L1MCTracklet > mcTracklets
Definition: L1AlgoEfficiencyPerformance.h:144
L1RTracklet
Definition: L1AlgoEfficiencyPerformance.h:48
L1RTracklet::operator!=
bool operator!=(const L1RTracklet< NHits > &a)
Definition: L1AlgoEfficiencyPerformance.h:73
L1AlgoEfficiencyPerformance::ntra
TL1AlgoEfficiencies ntra
Definition: L1AlgoEfficiencyPerformance.h:146
L1StsHit.h
TL1Efficiencies
Definition: CbmL1Counters.h:117
TL1AlgoEfficiencies::TL1AlgoEfficiencies
TL1AlgoEfficiencies()
Definition: L1AlgoEfficiencyPerformance.h:110
L1AlgoEfficiencyPerformance::FillMC
void FillMC()
Definition: L1AlgoEfficiencyPerformance.h:169
CbmL1MCTrack
Definition: CbmL1MCTrack.h:29
L1MTracklet::isReconstructable
bool isReconstructable
Definition: L1AlgoEfficiencyPerformance.h:105
CbmL1
Definition: CbmL1.h:113
L1AlgoEfficiencyPerformance::MatchTracks
void MatchTracks()
Definition: L1AlgoEfficiencyPerformance.h:278
L1MTracklet::recoIds
vector< int > recoIds
Definition: L1AlgoEfficiencyPerformance.h:100
L1Tracklet::operator==
bool operator==(const L1Tracklet< NHits > &a)
Definition: L1AlgoEfficiencyPerformance.h:36
L1Tracklet::iStation
int iStation
Definition: L1AlgoEfficiencyPerformance.h:43
L1AlgoEfficiencyPerformance::AddOne
bool AddOne(THitI *ih_)
Definition: L1AlgoEfficiencyPerformance.h:224