BmnRoot
Loading...
Searching...
No Matches
BmnStsMatchTracks.cxx
Go to the documentation of this file.
1// -------------------------------------------------------------------------
2// ----- CbmStsMatchTracks source file -----
3// ----- Created 24/11/05 by V. Friese -----
4// ----- BmnStsMatchTracks source file -----
5// ----- Created 26/12/21 by A. Zinchenko -----
6// -------------------------------------------------------------------------
7#include "BmnStsMatchTracks.h"
8
9#include "CbmStsHit.h"
10#include "CbmStsTrack.h"
11#include "CbmTrackMatch.h"
12#include "FairMCPoint.h"
13#include "FairRootManager.h"
14#include "TClonesArray.h"
15
16#include <iomanip>
17#include <iostream>
18#include <map>
19
20using std::cout;
21using std::endl;
22using std::fixed;
23using std::left;
24using std::map;
25using std::right;
26using std::setprecision;
27using std::setw;
28
29// ----- Default constructor -------------------------------------------
31 : FairTask("STSMatchTracks")
32 , fTracks(NULL)
33 , fGemPoints(NULL)
34 , fSilPoints(NULL)
35 , fHits(NULL)
36 , fMatches(NULL)
37 , fTimer()
38 , fTrBranch("")
39 , // AZ
40 fMatchMap()
41 , fNEvents(0)
42 , fNEventsFailed(0)
43 , fTime(0.)
44 , fNTrackMatches(0.)
45 , fNAllHits(0.)
46 , fNTrueHits(0.)
47{}
48
49// ----- Standard constructor -------------------------------------------
51 : FairTask("STSMatchTracks", iVerbose)
52 , fTracks(NULL)
53 , fGemPoints(NULL)
54 , fSilPoints(NULL)
55 , fHits(NULL)
56 , fMatches(NULL)
57 , fTimer()
58 , fTrBranch("")
59 , // AZ
60 fMatchMap()
61 , fNEvents(0)
62 , fNEventsFailed(0)
63 , fTime(0.)
64 , fNTrackMatches(0.)
65 , fNAllHits(0.)
66 , fNTrueHits(0.)
67{}
68
69// ----- Constructor with task name ------------------------------------
70BmnStsMatchTracks::BmnStsMatchTracks(const char* name, Int_t iVerbose)
71 : FairTask(name, iVerbose)
72 , fTracks(NULL)
73 , fGemPoints(NULL)
74 , fSilPoints(NULL)
75 , fHits(NULL)
76 , fMatches(NULL)
77 , fTimer()
78 , fTrBranch("")
79 , // AZ
80 fMatchMap()
81 , fNEvents(0)
82 , fNEventsFailed(0)
83 , fTime(0.)
84 , fNTrackMatches(0.)
85 , fNAllHits(0.)
86 , fNTrueHits(0.)
87{}
88
89// ----- Destructor ----------------------------------------------------
91{
92 if (fMatches) {
93 fMatches->Delete();
94 delete fMatches;
95 }
96}
97// -------------------------------------------------------------------------
98
99// ----- Virtual public method Exec ------------------------------------
100void BmnStsMatchTracks::Exec(Option_t* opt)
101{
102
103 // Timer
104 fTimer.Start();
105 Bool_t warn = kFALSE;
106
107 // Clear output array
108 // fMatches->Clear();
109 fMatches->Delete();
110
111 // Create some pointers and variables
112 CbmStsTrack* track = NULL;
113 CbmStsHit* hit = NULL;
114 FairMCPoint* point = NULL;
115 Int_t nHits = 0;
116 Int_t nMCTracks = 0;
117 Int_t iPoint = 0;
118 Int_t iMCTrack = 0;
119 Int_t nAll = 0;
120 Int_t nTrue = 0;
121 Int_t nWrong = 0;
122 Int_t nFake = 0;
123 Int_t nHitSum = 0;
124 Int_t nTrueSum = 0;
125 Int_t nWrongSum = 0;
126 Int_t nFakeSum = 0;
127 Int_t nMCTrackSum = 0;
128 map<Int_t, Int_t>::iterator it;
129
130 // Loop over StsTracks
131 Int_t nTracks = fTracks->GetEntriesFast();
132 for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
133 track = (CbmStsTrack*)fTracks->At(iTrack);
134 if (!track) {
135 cout << "-W- BmnStsMatchTracks::Exec: Empty StsTrack at " << iTrack << endl;
136 warn = kTRUE;
137 continue;
138 }
139 nHits = track->GetNStsHits();
140 nAll = nTrue = nWrong = nFake = nMCTracks = 0;
141 fMatchMap.clear();
142 if (fVerbose > 2)
143 cout << endl << "Track " << iTrack << ", Hits " << nHits << endl;
144
145 // Loop over StsHits of track
146 for (Int_t iHit = 0; iHit < nHits; iHit++) {
147 hit = (CbmStsHit*)fHits->At(track->GetStsHitIndex(iHit));
148 if (!hit) {
149 cout << "-E- BmnStsMatchTracks::Exec: "
150 << "No StsHit " << iHit << " for track " << iTrack << endl;
151 warn = kTRUE;
152 continue;
153 }
154 //*AZ
155 iPoint = hit->GetRefIndex();
156 if (iPoint < 0) { // Fake or background hit
157 nFake++;
158 continue;
159 }
160 // point = (FairMCPoint*) fPoints->At(iPoint);
161 if (hit->GetSystemId() == kSILICON || (fSilPoints && hit->GetDx() < 0.01)) { // AZ-280322
162 point = (FairMCPoint*)fSilPoints->At(iPoint);
163 } else if (hit->GetSystemId() == kGEM) {
164 point = (FairMCPoint*)fGemPoints->At(iPoint);
165 }
166 if (!point) {
167 cout << "-E- CbmStsMatchTracks::Exec: "
168 << "Empty MCPoint " << iPoint << " from MapsHit " << iHit << " (track " << iTrack << ")" << endl;
169 warn = kTRUE;
170 continue;
171 }
172 iMCTrack = point->GetTrackID();
173 if (fVerbose > 2)
174 cout << "Track " << iTrack << ", MAPS hit " << track->GetStsHitIndex(iHit) << ", StsPoint " << iPoint
175 << ", MCTrack " << iMCTrack << endl;
176 fMatchMap[iMCTrack]++;
177 //*/
178 /*
179 for (Int_t iLink = 0; iLink < hit->GetNLinks(); ++iLink) {
180 //if (hit->GetRefIndex() == kGEM) {
181 if (hit->GetSystemId() == kGEM) {
182 point = (FairMCPoint*)(fGemPoints->At(hit->GetLink(iLink).GetIndex()));
183 //AZ } else if (hit->GetRefIndex() == kSILICON) {
184 } else if (hit->GetSystemId() == kSILICON) {
185 point = (FairMCPoint*)(fSilPoints->At(hit->GetLink(iLink).GetIndex()));
186 }
187 iMCTrack = point->GetTrackID();
188 if ( fVerbose > 2 ) cout << "Track " << iTrack << ", MAPS hit "
189 << track->GetStsHitIndex(iHit)
190 << ", StsPoint " << iPoint << ", MCTrack "
191 << iMCTrack << endl;
192 fMatchMap[iMCTrack]++;
193 }
194 */
195 }
196
197 // Search for best matching MCTrack
198 iMCTrack = -1;
199 for (it = fMatchMap.begin(); it != fMatchMap.end(); it++) {
200 if (fVerbose > 2)
201 cout << it->second << " common points wth MCtrack " << it->first << endl;
202 nMCTracks++;
203 nAll += it->second;
204 if (it->second > nTrue) {
205 iMCTrack = it->first;
206 nTrue = it->second;
207 }
208 }
209 nWrong = nAll - nTrue;
210 if (fVerbose > 1)
211 cout << "-I- BmnStsMatchTracks: StsTrack " << iTrack << ", MCTrack " << iMCTrack << ", true " << nTrue
212 << ", wrong " << nWrong << ", fake " << nFake << ", #MCTracks " << nMCTracks << endl;
213
214 // Create StsTrackMatch
215 new ((*fMatches)[iTrack]) CbmTrackMatch(iMCTrack, nTrue, nWrong, nFake, nMCTracks);
216
217 // Some statistics
218 nHitSum += nHits;
219 nTrueSum += nTrue;
220 nWrongSum += nWrong;
221 nFakeSum += nFake;
222 nMCTrackSum += nMCTracks;
223
224 } // Track loop
225
226 // Event statistics
227 fTimer.Stop();
228 Double_t qTrue = 0.;
229 if (nHitSum)
230 qTrue = Double_t(nTrueSum) / Double_t(nHitSum) * 100.;
231 if (fVerbose > 1) {
232 Double_t qWrong = Double_t(nWrongSum) / Double_t(nHitSum) * 100.;
233 Double_t qFake = Double_t(nFakeSum) / Double_t(nHitSum) * 100.;
234 Double_t qMC = Double_t(nMCTrackSum) / Double_t(nTracks);
235 cout << endl;
236 cout << "-------------------------------------------------------" << endl;
237 cout << "-I- Sts Track Matching -I-" << endl;
238 cout << "Reconstructed StsTracks : " << nTracks << endl;
239 ;
240 cout << "True hit assignments : " << qTrue << " %" << endl;
241 cout << "Wrong hit assignments : " << qWrong << " %" << endl;
242 cout << "Fake hit assignments : " << qFake << " %" << endl;
243 cout << "MCTracks per StsTrack : " << qMC << endl;
244 cout << "--------------------------------------------------------" << endl;
245 }
246 if (fVerbose == 1) {
247 if (warn)
248 cout << "- ";
249 else
250 cout << "+ ";
251 cout << setw(15) << left << fName << ": " << setprecision(4) << setw(8) << fixed << right << fTimer.RealTime()
252 << " s, matches " << nTracks << ", hit quota " << qTrue << " %" << endl;
253 }
254
255 // Run statistics
256 if (warn)
257 fNEventsFailed++;
258 else {
259 fNEvents++;
260 fTime += fTimer.RealTime();
261 fNTrackMatches += Double_t(nTracks);
262 fNAllHits += Double_t(nHitSum);
263 fNTrueHits += Double_t(nTrueSum);
264 }
265}
266// -------------------------------------------------------------------------
267
268// ----- Virtual private method Init -----------------------------------
270{
271
272 // Get FairRootManager
273 FairRootManager* ioman = FairRootManager::Instance();
274 if (!ioman) {
275 cout << "-E- BmnStsMatchTracks::Init: "
276 << "RootManager not instantised!" << endl;
277 return kFATAL;
278 }
279
280 // Get StsHit Array
281 fHits = (TClonesArray*)ioman->GetObject("StsHit");
282 if (!fHits) {
283 cout << "-W- BmnStsMatchTracks::Init: No StsHit array!" << endl;
284 }
285
286 // Get StsTrack Array
287 // AZ fTracks = (TClonesArray*) ioman->GetObject("StsTrack");
288 if (fTrBranch == "")
289 fTracks = (TClonesArray*)ioman->GetObject("StsTrack"); // AZ
290 else
291 fTracks = (TClonesArray*)ioman->GetObject(fTrBranch); // AZ
292
293 if (!fTracks) {
294 cout << "-E- BmnStsMatchTracks::Init: No StsTrack array!" << endl;
295 return kERROR;
296 }
297
298 // Get StsPoint array
299 fGemPoints = (TClonesArray*)ioman->GetObject("StsPoint");
300 if (!fGemPoints) {
301 // cout << "-E- BmnStsMatchTracks::Init: No StsPoint array!" << endl;
302 return kERROR;
303 }
304
305 fSilPoints = (TClonesArray*)ioman->GetObject("SiliconPoint");
306 if (!fSilPoints) {
307 // cout << "-E- BmnStsMatchTracks::Init: No SiliconPoint array!" << endl;
308 // AZ-170322 return kERROR;
309 }
310
311 // Create and register StsTrackMatch array
312 fMatches = new TClonesArray("CbmTrackMatch", 100);
313 ioman->Register("StsTrackMatch", "STS", fMatches, kTRUE);
314
315 return kSUCCESS;
316}
317// -------------------------------------------------------------------------
318
319// ----- Virtual private method Finish ---------------------------------
321{
322
323 cout << endl;
324 cout << "============================================================" << endl;
325 cout << "===== " << GetName() << ": Run summary " << endl;
326 cout << "===== " << endl;
327 cout << "===== Good events : " << setw(6) << fNEvents << endl;
328 cout << "===== Failed events : " << setw(6) << fNEventsFailed << endl;
329 cout << "===== Average time : " << setprecision(4) << setw(8) << right << fTime / Double_t(fNEvents) << " s"
330 << endl;
331 cout << "===== " << endl;
332 cout << "===== Tracks per event : " << fixed << setprecision(0) << fNTrackMatches / Double_t(fNEvents) << endl;
333 cout << setprecision(2);
334 cout << "===== True hits : " << fixed << setw(6) << right << fNTrueHits / fNAllHits * 100. << " %" << endl;
335 cout << "============================================================" << endl;
336}
337// -------------------------------------------------------------------------
@ kSILICON
@ kGEM
virtual InitStatus Init()
virtual void Exec(Option_t *opt)
Int_t GetSystemId() const
Definition CbmStsHit.h:64
Int_t GetNStsHits() const
Definition CbmStsTrack.h:60
Int_t GetStsHitIndex(Int_t iHit) const
Definition CbmStsTrack.h:62