CbmSttMatchTracks.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // -------------------------------------------------------------------------
  2. // ----- CbmStsMatchTracks source file -----
  3. // ----- Created 24/11/05 by V. Friese -----
  4. // -------------------------------------------------------------------------
  5. #include <iostream>
  6. #include <map>
  7. #include "TClonesArray.h"
  8. #include "FairMCPoint.h"
  9. #include "FairRootManager.h"
  10. #include "CbmSttMatchTracks.h"
  11. #include "CbmSttHit.h"
  12. #include "CbmSttTrack.h"
  13. #include "CbmSttTrackMatch.h"
  14. // ----- Default constructor -------------------------------------------
  15. CbmSttMatchTracks::CbmSttMatchTracks()
  16. : FairTask("STT track match") {
  17. fTracks = NULL;
  18. fMatches = NULL;
  19. fVerbose = 1;
  20. fCollectionsComplete = kFALSE;
  21. }
  22. // -------------------------------------------------------------------------
  23. // ----- Constructor with verbosity level ------------------------------
  24. CbmSttMatchTracks::CbmSttMatchTracks(Int_t verbose)
  25. : FairTask("STT track match") {
  26. fTracks = NULL;
  27. fMatches = NULL;
  28. fVerbose = verbose;
  29. fCollectionsComplete = kFALSE;
  30. }
  31. // -------------------------------------------------------------------------
  32. // ----- Constructor with name, title and verbosity ---------------------
  33. CbmSttMatchTracks::CbmSttMatchTracks(const char* name, const char* title,
  34. Int_t verbose)
  35. : FairTask(name) {
  36. fTracks = NULL;
  37. fMatches = NULL;
  38. fVerbose = verbose;
  39. fCollectionsComplete = kFALSE;
  40. }
  41. // -------------------------------------------------------------------------
  42. // ----- Destructor ----------------------------------------------------
  43. CbmSttMatchTracks::~CbmSttMatchTracks()
  44. {
  45. fHitCollectionNames.clear();
  46. fPointCollectionNames.clear();
  47. }
  48. // -------------------------------------------------------------------------
  49. // ----- Public method Init --------------------------------------------
  50. InitStatus CbmSttMatchTracks::Init() {
  51. // Get FairRootManager
  52. FairRootManager* ioman = FairRootManager::Instance();
  53. if (! ioman) {
  54. cout << "-E- CbmSttMatchTracks::Init: "
  55. << "RootManager not instantised!" << endl;
  56. return kFATAL;
  57. }
  58. // Get SttTrack Array
  59. fTracks = (TClonesArray*) ioman->GetObject("STTTrack");
  60. if ( ! fTracks ) {
  61. cout << "-E- CbmSttMatchTracks::Init: No SttTrack array!" << endl;
  62. return kERROR;
  63. }
  64. // Create and register SttTrackMatch array
  65. fMatches = new TClonesArray("CbmSttTrackMatch",100);
  66. ioman->Register("STTTrackMatch", "STT", fMatches, kTRUE);
  67. return kSUCCESS;
  68. }
  69. // -------------------------------------------------------------------------
  70. // ----- Public method Exec --------------------------------------------
  71. void CbmSttMatchTracks::Exec(Option_t* opt)
  72. {
  73. AddAllCollections();
  74. if (fHitCollectionList.GetEntries() == 0)
  75. {
  76. cout << "-E- CbmSttTrackFinderIdeal::DoFind: "
  77. << "No hit arrays present, call AddHitCollection() first (at least once)! " << endl;
  78. }
  79. if (fPointCollectionList.GetEntries() == 0)
  80. {
  81. cout << "-E- CbmSttTrackFinderIdeal::DoFind: "
  82. << "No point arrays present, call AddHitCollection() first (at least once)! " << endl;
  83. }
  84. // Clear output array
  85. fMatches->Delete();
  86. // Create some pointers and variables
  87. CbmSttTrack* track = NULL;
  88. CbmSttHit* mHit = NULL;
  89. FairMCPoint* point = NULL;
  90. Int_t nHits = 0;
  91. Int_t nMCTracks = 0;
  92. Int_t iPoint = 0;
  93. Int_t iFlag = 0;
  94. Int_t iMCTrack = 0;
  95. Int_t nAll = 0;
  96. Int_t nTrue = 0;
  97. Int_t nWrong = 0;
  98. Int_t nFake = 0;
  99. Int_t nHitSum = 0;
  100. Int_t nTrueSum = 0;
  101. Int_t nWrongSum = 0;
  102. Int_t nFakeSum = 0;
  103. Int_t nMCTrackSum = 0;
  104. // map<Int_t, Int_t>::iterator it = 0;
  105. map<Int_t, Int_t>::iterator it;
  106. // it = 0;
  107. // Loop over SttTracks
  108. Int_t nTracks = fTracks->GetEntriesFast();
  109. for (Int_t iTrack=0; iTrack<nTracks; iTrack++)
  110. {
  111. track = (CbmSttTrack*) fTracks->At(iTrack);
  112. if ( ! track)
  113. {
  114. cout << "-W- CbmSttMatchTracks::Exec: Empty SttTrack at "
  115. << iTrack << endl;
  116. continue;
  117. }
  118. nHits = track->GetNofHits();
  119. nAll = nTrue = nWrong = nFake = nMCTracks = 0;
  120. fMatchMap.clear();
  121. if (fVerbose > 2) cout << endl << "Track " << iTrack << ", Hits "
  122. << nHits << endl;
  123. // Loop over Hits of track
  124. for (Int_t iMHit=0; iMHit<nHits; iMHit++)
  125. {
  126. // alter here
  127. mHit = GetHitFromCollections(track->GetHitIndex(iMHit));
  128. if ( ! mHit )
  129. {
  130. cout << "-E- CbmSttMatchTracks::Exec: "
  131. << "No Hit " << iMHit << " for track " << iTrack << endl;
  132. continue;
  133. }
  134. iPoint = mHit->GetRefIndex();
  135. if ( iPoint < 0 )
  136. {
  137. nFake++;
  138. continue;
  139. }
  140. // alter here
  141. point = GetPointFromCollections(track->GetHitIndex(iMHit));
  142. if ( ! point )
  143. {
  144. cout << "-E- CbmSttMatchTracks::Exec: "
  145. << "Empty MCPoint " << iPoint << " from Hit " << iMHit
  146. << " (track " << iTrack << ")" << endl;
  147. continue;
  148. }
  149. iMCTrack = point->GetTrackID();
  150. if ( fVerbose > 2 ) cout << "Track " << iTrack << ", hit "
  151. << track->GetHitIndex(iMHit)
  152. << ", STTPoint " << iPoint << ", MCTrack "
  153. << iMCTrack << endl;
  154. fMatchMap[iMCTrack]++;
  155. }
  156. // Search for best matching MCTrack
  157. iMCTrack = -1;
  158. for (it=fMatchMap.begin(); it!=fMatchMap.end(); ++it)
  159. {
  160. if (fVerbose > 2) cout << it->second
  161. << " common points wth MCtrack "
  162. << it->first << endl;
  163. nMCTracks++;
  164. nAll += it->second;
  165. if ( it->second > nTrue )
  166. {
  167. iMCTrack = it->first;
  168. nTrue = it->second;
  169. }
  170. }
  171. nWrong = nAll - nTrue;
  172. if (fVerbose>1) cout << "-I- CbmSttMatchTracks: STTTrack " << iTrack
  173. << ", MCTrack " << iMCTrack << ", true "
  174. << nTrue << ", wrong " << nWrong << ", fake "
  175. << nFake << ", #MCTracks " << nMCTracks << endl;
  176. // Create SttTrackMatch
  177. new ((*fMatches)[iTrack]) CbmSttTrackMatch(iMCTrack, nTrue,
  178. nWrong, nFake,
  179. nMCTracks);
  180. // Some statistics
  181. nHitSum += nHits;
  182. nTrueSum += nTrue;
  183. nWrongSum += nWrong;
  184. nFakeSum += nFake;
  185. nMCTrackSum += nMCTracks;
  186. } // Track loop
  187. // Event statistics
  188. Double_t qTrue = 0.;
  189. if ( nHitSum)
  190. qTrue = Double_t(nTrueSum) / Double_t(nHitSum) * 100.;
  191. if (fVerbose)
  192. {
  193. Double_t
  194. qWrong = 0.,
  195. qFake = 0.,
  196. qMC = 0.;
  197. if (nHitSum)
  198. {
  199. qWrong = Double_t(nWrongSum) / Double_t(nHitSum) * 100.;
  200. qFake = Double_t(nFakeSum) / Double_t(nHitSum) * 100.;
  201. }
  202. if (nTracks)
  203. qMC = Double_t(nMCTrackSum) / Double_t(nTracks);
  204. cout << endl;
  205. cout << "-------------------------------------------------------"
  206. << endl;
  207. cout << "-I- STT Track Matching -I-"
  208. << endl;
  209. cout << "Reconstructed STTTracks : " << nTracks << endl;;
  210. cout << "True hit assignments : " << qTrue << " %" << endl;
  211. cout << "Wrong hit assignments : " << qWrong << " %" << endl;
  212. cout << "Fake hit assignments : " << qFake << " %" << endl;
  213. cout << "MCTracks per STTTrack : " << qMC << endl;
  214. cout << "--------------------------------------------------------"
  215. << endl;
  216. }
  217. else cout << "-I- CbmSttMatchTracks: rec. " << nTracks << ", quota "
  218. << qTrue << " % " << endl;
  219. }
  220. // -------------------------------------------------------------------------
  221. // -------------------------------------------------------------------------
  222. void CbmSttMatchTracks::AddHitCollectionName(char *hitCollectionName, char *pointCollectionName)
  223. {
  224. string
  225. newPointName(pointCollectionName),
  226. newHitName(hitCollectionName);
  227. fHitCollectionNames.push_back(newHitName);
  228. fPointCollectionNames.push_back(newPointName);
  229. }
  230. void CbmSttMatchTracks::AddHitCollection(char const *hitCollectionName, char const *pointCollectionName)
  231. {
  232. // Get and check FairRootManager
  233. FairRootManager
  234. *ioman = FairRootManager::Instance();
  235. if (!ioman)
  236. {
  237. cout << "-E- CbmSttFindTracks::AddHitCollection: "
  238. << "RootManager not instantised!" << endl;
  239. }
  240. // Get hit Array
  241. TClonesArray
  242. *fHitArray = (TClonesArray*) ioman->GetObject(hitCollectionName);
  243. if (!fHitArray)
  244. {
  245. cout << "-W- CbmSttFindTracks::AddHitCollection: No " << hitCollectionName << " STT hit array!"
  246. << endl;
  247. }
  248. // Get point Array
  249. TClonesArray
  250. *fPointArray = (TClonesArray*) ioman->GetObject(pointCollectionName);
  251. if (!fPointArray)
  252. {
  253. cout << "-W- CbmSttFindTracks::AddHitCollection: No " << pointCollectionName << " STT hit array!"
  254. << endl;
  255. }
  256. fHitCollectionList.Add(fHitArray);
  257. fPointCollectionList.Add(fPointArray);
  258. }
  259. void CbmSttMatchTracks::AddAllCollections()
  260. {
  261. if (!fCollectionsComplete)
  262. {
  263. for (int counter = 0; counter < fHitCollectionNames.size(); counter++)
  264. {
  265. AddHitCollection(fHitCollectionNames[counter].c_str(), fPointCollectionNames[counter].c_str());
  266. }
  267. fCollectionsComplete = kTRUE;
  268. }
  269. }
  270. CbmSttHit* CbmSttMatchTracks::GetHitFromCollections(Int_t hitCounter)
  271. {
  272. CbmSttHit
  273. *retval = NULL;
  274. Int_t
  275. relativeCounter = hitCounter;
  276. for (Int_t collectionCounter = 0; collectionCounter < fHitCollectionList.GetEntries(); collectionCounter++)
  277. {
  278. Int_t
  279. size = ((TClonesArray *)fHitCollectionList.At(collectionCounter))->GetEntriesFast();
  280. if (relativeCounter < size)
  281. {
  282. retval = (CbmSttHit*) ((TClonesArray *)fHitCollectionList.At(collectionCounter))->At(relativeCounter);
  283. break;
  284. }
  285. else
  286. {
  287. relativeCounter -= size;
  288. }
  289. }
  290. return retval;
  291. }
  292. FairMCPoint* CbmSttMatchTracks::GetPointFromCollections(Int_t hitCounter)
  293. {
  294. FairMCPoint
  295. *retval = NULL;
  296. Int_t
  297. relativeCounter = hitCounter;
  298. for (Int_t collectionCounter = 0; collectionCounter < fHitCollectionList.GetEntries(); collectionCounter++)
  299. {
  300. Int_t
  301. size = ((TClonesArray *)fHitCollectionList.At(collectionCounter))->GetEntriesFast();
  302. if (relativeCounter < size)
  303. {
  304. Int_t
  305. tmpHit = ((CbmSttHit*) ((TClonesArray *)fHitCollectionList.At(collectionCounter))->At(relativeCounter))->GetRefIndex();
  306. retval = (FairMCPoint*) ((TClonesArray *)fPointCollectionList.At(collectionCounter))->At(tmpHit);
  307. break;
  308. }
  309. else
  310. {
  311. relativeCounter -= size;
  312. }
  313. }
  314. return retval;
  315. }
  316. // ----- Public method Finish ------------------------------------------
  317. void CbmSttMatchTracks::Finish() { }
  318. // -------------------------------------------------------------------------
  319. ClassImp(CbmSttMatchTracks)