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