MpdTofMatching.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //------------------------------------------------------------------------------------------------------------------------
  2. #ifndef __MPD_TOF_MATCHING_H
  3. #define __MPD_TOF_MATCHING_H 1
  4. //------------------------------------------------------------------------------------------------------------------------
  5. /// \class MpdTofMatching
  6. ///
  7. /// \brief
  8. /// \author Sergei Lobastov (LHE, JINR, Dubna)
  9. //------------------------------------------------------------------------------------------------------------------------
  10. #include <iostream>
  11. #include <assert.h>
  12. #include <TObject.h>
  13. #include <TVector3.h>
  14. #include <TClonesArray.h>
  15. #include <TGeoMatrix.h>
  16. #include <TStopwatch.h>
  17. #include "MpdTpcKalmanTrack.h"
  18. #include "MpdTofHit.h"
  19. #include "MpdTofMatchingData.h"
  20. #include "FairTask.h"
  21. //------------------------------------------------------------------------------------------------------------------------
  22. struct ltPt
  23. {
  24. bool operator()(const MpdKalmanTrack* p1, const MpdKalmanTrack* p2) const // sorted by Pt descending
  25. {
  26. return (p1->Pt() > p2->Pt());
  27. }
  28. };
  29. class MpdTofPoint;
  30. typedef std::multimap<const MpdKalmanTrack*, Int_t, ltPt> TmPt; // <MpdKalmanTrack*, KfIndex> sorted by Pt descending
  31. typedef std::multimap<Int_t, MpdTofPoint*> TmmT2P; // pair< MCtrackID, MpdTofPoint*>
  32. typedef std::multimap<Int_t, std::pair<MpdTofHit*, Int_t> > TmmD2H; // pair< detUID, pair<MpdTofHit*, hitIndex> >
  33. typedef std::map<Int_t, std::pair<MpdTofHit*, Int_t> > TmS2H; // pair< stripUID, pair<MpdTofHit*, hitIndex> >
  34. typedef std::multimap<Int_t, std::pair<MpdTofHit*, Int_t> > TmmT2H; // pair< trackID, pair<MpdTofHit*, hitIndex> >
  35. //------------------------------------------------------------------------------------------------------------------------
  36. struct mcInfo // one-to-many relationship (one kftrack -> many mcpoints)
  37. {
  38. // track info
  39. Int_t trackIndex, pid;
  40. TVector3 vertMomentum, vertPosition;
  41. // mc points info
  42. std::vector<MpdTofPoint*> vPoints;
  43. bool TofTouch;
  44. mcInfo() : trackIndex(-1), pid(-1), TofTouch(false) {}
  45. // return MpdTofPoint number for same tid (kftrack--->mctid<---mcPoint)
  46. size_t Npoints()const { return vPoints.size();}
  47. // return true, if mcPoint exist for this suid
  48. bool GetPosition(TVector3& pos, Int_t suid)const;
  49. // return pair<stripUID, MCtrackId> of closest mcPoint for detector unique ID
  50. std::pair<Int_t, Int_t> GetClosestPointOnDetector(TVector3& pos, TVector3& mom, const TVector3& from, Int_t duid) const;
  51. // return true, if mcPoint exist for this strip
  52. bool IsSameStrip(Int_t suid)const;
  53. // find mcpoint by tid, if suid != -1, ALSO check suid
  54. const MpdTofPoint* FindPoint(Int_t tid, Int_t suid = -1)const;
  55. };
  56. //------------------------------------------------------------------------------------------------------------------------
  57. class MpdTofMatchingQA;
  58. class LWeightMatrix
  59. {
  60. double *pData = nullptr; // mData[kf trackId, hitId] = weight
  61. size_t fNTracks = 0; // number of kf tracks
  62. size_t fNHits = 0; // number of hits
  63. public:
  64. typedef std::map<long, MpdTofMatchingData> TmCandidate; // pair< hash(kf trackId, hitId), MpdTofMatchingData >
  65. TmCandidate mCandidates;
  66. // debug info
  67. size_t fNCandTofTracks = 0, fNCandTrueMatch = 0, fNFoundTrueMatch = 0, fNFoundMisMatch = 0;
  68. MpdTofMatchingQA *pMatchingQA = nullptr;
  69. std::map<Int_t, mcInfo> fmT2MC; // pair< kf trackIndex, mcInfo>
  70. double SumWeightsForHit(size_t hitId)const; // [0,...N-1]
  71. inline size_t GetTrackID(long uid)const{ return ((uid & 0xFFFF0000) >> 16);}; // 4 bytes used from 8
  72. inline size_t GetHitID(long uid)const{ return (uid & 0x0000FFFF);};
  73. inline long hash(size_t trackId, size_t hitId)const
  74. {
  75. long uid = (trackId<<16) + hitId;
  76. size_t track = GetTrackID(uid);
  77. size_t hit = GetHitID(uid);
  78. assert(track==trackId);
  79. assert(hit==hitId);
  80. return uid;
  81. }
  82. public:
  83. LWeightMatrix(MpdTofMatchingQA*);
  84. ~LWeightMatrix();
  85. void AddWeight(size_t trackId, size_t hitId, double value);
  86. void SetWeight(size_t trackId, size_t hitId, double value);
  87. inline double GetWeight(size_t trackId, size_t hitId)const
  88. {
  89. assert(trackId<fNTracks);
  90. assert(hitId<fNHits);
  91. return *(pData + trackId * fNHits + hitId);
  92. }
  93. inline size_t GetRank(size_t trackId, size_t& hitIndex) const
  94. {
  95. size_t nRank = 0; hitIndex = -1;
  96. for(size_t hitId = 0; hitId<fNHits; hitId++){ if(GetWeight(trackId, hitId) > 0.){ nRank++; hitIndex = hitId; }}
  97. return nRank;
  98. }
  99. inline size_t GetRank(size_t trackId, size_t* hitIndex) const
  100. {
  101. size_t nRank = 0;
  102. for(size_t hitId = 0; hitId<fNHits; hitId++){ if(GetWeight(trackId, hitId) > 0.){ if(nRank<49)hitIndex[nRank] = hitId; nRank++;}}
  103. return nRank;
  104. }
  105. inline void RemoveHit(size_t hitId) { for(size_t trackId = 0; trackId<fNTracks; trackId++) SetWeight(trackId, hitId, 0.); }
  106. void Normalize(bool copyNormWeight = true);
  107. void Process(TmPt tids, const TClonesArray *aKfTracks);
  108. void CountCandidates(const TmPt& tids);
  109. void CountFoundCandidates();
  110. void Boost1thRankCand(double mult);
  111. void Reset(size_t trackSize, size_t hitSize);
  112. MpdTofMatchingData* AddCandidate(const MpdTofMatchingData& data);
  113. mcInfo* saveMCinfo(Int_t trackId, const mcInfo& mc);
  114. const mcInfo* getMCinfo(Int_t trackId)const;
  115. void MoveEntries(TClonesArray *aTofMatchings)const;
  116. const MpdTofMatchingData* FindCandidate(size_t trackId, size_t hitId)const;
  117. std::pair<size_t,size_t> GetMatchFound()const{ return {fNFoundTrueMatch, fNFoundMisMatch}; }
  118. std::pair<size_t,size_t> GetMatchCand()const{ return {fNCandTofTracks, fNCandTrueMatch}; }
  119. void Print(const char* comment=nullptr, const TClonesArray* aKfTracks=nullptr, const TClonesArray* aTofHits=nullptr, const TmmT2H* map=nullptr, std::ostream& os=std::cout) const;
  120. };
  121. //------------------------------------------------------------------------------------------------------------------------
  122. class TRandom2;
  123. class MpdKalmanFilter;
  124. class MpdTofMatchingQA;
  125. class LStrip;
  126. class LRectangle;
  127. class MpdTofMatching : public FairTask
  128. {
  129. TClonesArray *aMcPoints = nullptr; //! <--- MC input
  130. TClonesArray *aMcTracks = nullptr; //! <--- MC input
  131. TClonesArray *aTofHits = nullptr; //! <--- input TOF hits
  132. TClonesArray *aTPCkfTracks = nullptr; //! <--- input KF TPC tracks
  133. TClonesArray *aECTkfTracks = nullptr; //! <--- input KF Ect Tracks
  134. TClonesArray *aTofMatchings = nullptr; //! ---> output
  135. MpdKalmanFilter *pKF = nullptr; //!
  136. LWeightMatrix *fWeights = nullptr; //!
  137. TRandom2 *pRandom = nullptr; //!
  138. MpdTofMatchingQA *pQA = nullptr; //!
  139. TStopwatch *pTimer = nullptr; //!
  140. Bool_t fDoTest, fIsMcRun = false, fDoMCtest;
  141. const Double_t fTofBarrelRadius = 152.; // [cm]
  142. Double_t fThreshZ = 5., fThreshPhi = 5.; // [cm]
  143. size_t fNTofTracksEvent = 0, fNTofTracksRun = 0; // number of Tof touched kf tracks per event & run
  144. // refit kalman track before propagation
  145. MpdTpcKalmanTrack RefitTrack(const MpdTpcKalmanTrack*)const;
  146. // propagate kalman track to cylinder surface.
  147. TVector3 EstTrackOnR(const MpdTpcKalmanTrack *tr)const;
  148. // propagate kalman track to plane surface, return error code, 0 - otherwise.
  149. int EstTrackOnPlate(const MpdTpcKalmanTrack& tr, const TVector3& point, const TVector3& perp, TVector3& pos, Double_t& length, TVector3& P) const;
  150. // propagate kalman track to plane surface with smearing track parameters, return true if ok.
  151. bool EstMarkerTrackOnPlate(size_t mode, const MpdTpcKalmanTrack* tr, const TVector3& point, const TVector3& perp, TVector3& pos, TVector3& P)const;
  152. void FillWeights(const TmPt& tids, const TmmD2H& dets, const TmS2H& mHits, const TmmT2P&);
  153. public:
  154. MpdTofMatching(const char *name = "TOF Matching", Int_t verbose = 1, Bool_t DoTest = false, const char *flnm = "QA.MpdTofMatching.root");
  155. virtual ~MpdTofMatching();
  156. virtual InitStatus Init();
  157. virtual void Exec(Option_t * option);
  158. virtual void Finish();
  159. static mcInfo GetMcInfo(const TmmT2P& p2ts, const MpdKalmanTrack* pKfTrack, TClonesArray* aMcTracks);
  160. // mapping pair<MCtrackID, MpdTofPoint*> and sorting by time
  161. void MapMcT2P(TClonesArray *aMcPoints, TmmT2P& mmMCpoints)const;
  162. void Print(const MpdTpcKalmanTrack*, const char* comment = nullptr, std::ostream& os = std::cout)const;
  163. void Report(Int_t verbose, std::ostream& os = std::cout)const;
  164. ClassDef(MpdTofMatching,4) // MpdTofMatching
  165. };
  166. //------------------------------------------------------------------------------------------------------------------------
  167. #endif