CbmRoot
CbmMuchToTrdVectors.cxx
Go to the documentation of this file.
1 
5 #include "CbmMuchToTrdVectors.h"
6 //#include "CbmDefs.h"
7 //#include "CbmSetup.h"
8 #include "CbmMuchTrack.h"
9 
10 #include "FairLogger.h"
11 #include "FairRootManager.h"
12 #include "FairTrackParam.h"
13 
14 #include <TClonesArray.h>
15 //#include <TGeoManager.h>
16 #include <TMath.h>
17 #include <TMatrixF.h>
18 #include <TMatrixFSym.h>
19 
20 #include <iostream>
21 
22 using std::cout;
23 using std::endl;
24 using std::multimap;
25 using std::pair;
26 
27 // ----- Default constructor -------------------------------------------
29  : FairTask("MuchToTrdVectors")
30  , fTrackArray(NULL)
31  , fNofTracks(0)
32  , fMuchTracks(NULL)
33  , fTrdTracks(NULL)
34  , fZ0(-1) {}
35 // -------------------------------------------------------------------------
36 
37 // ----- Destructor ----------------------------------------------------
39 // -------------------------------------------------------------------------
40 
41 // ----- Public method Init (abstract in base class) --------------------
43 
44  // Get and check FairRootManager
45  FairRootManager* ioman = FairRootManager::Instance();
46  if (ioman == NULL) Fatal("Init", "RootManager not instantiated!");
47 
48  // Create and register GlobalVector array
49  fTrackArray = new TClonesArray("CbmMuchTrack", 100);
50  ioman->Register("GlobalVector", "Much", fTrackArray, kTRUE);
51  fMuchTracks = static_cast<TClonesArray*>(ioman->GetObject("MuchVectorTrack"));
52  fTrdTracks = static_cast<TClonesArray*>(ioman->GetObject("TrdVector"));
53 
54  return kSUCCESS;
55 }
56 // -------------------------------------------------------------------------
57 
58 // ----- SetParContainers -------------------------------------------------
60  /*
61  fDigiPar = (CbmTrdDigiPar*) FairRunAna::Instance()->GetRuntimeDb()->getContainer("CbmTrdDigiPar");
62  cout << " ******* digiPar ******** " << fDigiPar << endl;
63  exit(0);
64  */
65 }
66 // -------------------------------------------------------------------------
67 
68 // ----- Public method Exec --------------------------------------------
69 void CbmMuchToTrdVectors::Exec(Option_t* opt) {
70 
71  fTrackArray->Delete();
72 
73  // Do all processing
74  Int_t nTrd = fTrdTracks->GetEntriesFast();
75  if (nTrd == 0) return;
76 
78 
79  // Merge vectors
80  MergeVectors();
81 
82  // Remove clones
83  RemoveClones();
84 }
85 // -------------------------------------------------------------------------
86 
87 // ----- Public method Finish ------------------------------------------
89 // -------------------------------------------------------------------------
90 
91 // ----- Private method GetMuchVectors ---------------------------------
93  // Get MUCH vectors, propagate to TRD
94 
95  const Int_t nMinSeg = 5;
96  Int_t nMuch = fMuchTracks->GetEntriesFast();
97  fXmap.clear();
98 
99  /*
100  if (fZ0 < 0) {
101  // Get TRD first layer position
102  CbmMuchTrack *tr = (CbmMuchTrack*) fTrdTracks->UncheckedAt(0);
103  fZ0 = tr->GetParamFirst()->GetZ();
104  }
105  */
106 
107  TMatrixF matr = TMatrixF(5, 5);
108  TMatrixF unit(TMatrixF::kUnit, matr);
109 
110  for (Int_t itr = 0; itr < nMuch; ++itr) {
111  CbmMuchTrack* tr = (CbmMuchTrack*) fMuchTracks->UncheckedAt(itr);
112  Int_t nvecTr = tr->GetNofHits();
113  if (nvecTr < nMinSeg) continue; // track is too short
114 
115  if (fZ0 < 0) {
116  // Get MUCH first layer position of the last station
117  fZ0 = tr->GetParamFirst()->GetZ();
118  }
119 
120  // Propagate to TRD
121  FairTrackParam parFirst = *tr->GetParamFirst();
122  /*
123  Double_t zbeg = parFirst.GetZ();
124  Double_t dz = fZ0 - zbeg;
125  // Propagate params
126  parFirst.SetX(parFirst.GetX() + dz * parFirst.GetTx());
127  parFirst.SetY(parFirst.GetY() + dz * parFirst.GetTy());
128  parFirst.SetZ(parFirst.GetZ() + dz);
129  TMatrixFSym cov(5);
130  parFirst.CovMatrix(cov);
131  cov(4,4) = 1.0;
132  //cov.Print();
133  TMatrixF ff = unit;
134  //ff.Print();
135  //ff(0,2) = ff(1,3) = dz;
136  ff(2,0) = ff(3,1) = dz;
137  //cout << " Cov: " << dz << " " << cov(0,0) << " " << cov(1,1) << endl;
138  TMatrixF cf(cov,TMatrixF::kMult,ff);
139  TMatrixF fcf(ff,TMatrixF::kTransposeMult,cf);
140  cov.SetMatrixArray(fcf.GetMatrixArray());
141  //cout << " Cov1: " << dz << " " << cov(0,0) << " " << cov(1,1) << endl;
142  cov.Invert(); // weight matrix
143  parFirst.SetCovMatrix(cov);
144  */
145  tr->SetParamLast(&parFirst);
146  fXmap.insert(pair<Double_t, Int_t>(parFirst.GetX(), itr));
147  }
148 }
149 // -------------------------------------------------------------------------
150 
151 // ----- Private method MergeVectors -----------------------------------
153  // Merge MUCH and TRD tracks
154 
155  //const Double_t window = 25.0, chi2max = 24;
156  const Double_t window = 35.0, chi2max = 24;
157  multimap<Double_t, Int_t>::iterator mit, mitb, mite;
158  FairTrackParam parOk;
159 
160  TMatrixF matr = TMatrixF(5, 5);
161  TMatrixF unit(TMatrixF::kUnit, matr);
162 
163  Int_t nTrd = fTrdTracks->GetEntriesFast();
164 
165  for (Int_t itr = 0; itr < nTrd; ++itr) {
166  CbmMuchTrack* tr = (CbmMuchTrack*) fTrdTracks->UncheckedAt(itr);
167  //const FairTrackParam *par1 = tr->GetParamFirst();
168  FairTrackParam par1 = *tr->GetParamFirst();
169  // AZ
170  Double_t zbeg = par1.GetZ();
171  Double_t dz = fZ0 - zbeg;
172  // Propagate params
173  par1.SetX(par1.GetX() + dz * par1.GetTx());
174  par1.SetY(par1.GetY() + dz * par1.GetTy());
175  par1.SetZ(par1.GetZ() + dz);
176  TMatrixFSym cov(5);
177  par1.CovMatrix(cov);
178  cov(4, 4) = 1.0;
179  //cov.Print();
180  TMatrixF ff = unit;
181  //ff.Print();
182  //ff(0,2) = ff(1,3) = dz;
183  ff(2, 0) = ff(3, 1) = dz;
184  //cout << " Cov: " << dz << " " << cov(0,0) << " " << cov(1,1) << endl;
185  TMatrixF cf(cov, TMatrixF::kMult, ff);
186  TMatrixF fcf(ff, TMatrixF::kTransposeMult, cf);
187  cov.SetMatrixArray(fcf.GetMatrixArray());
188  //cout << " Cov1: " << dz << " " << cov(0,0) << " " << cov(1,1) << endl;
189  cov.Invert(); // weight matrix
190  par1.SetCovMatrix(cov);
191  //AZ
192  //Double_t xv = par1->GetX();
193  //Double_t yv = par1->GetY();
194  Double_t xv = par1.GetX();
195  Double_t yv = par1.GetY();
196  mitb = fXmap.lower_bound(xv - window); // lower X-window edge
197  mite = fXmap.upper_bound(xv + window); // upper X-window edge
198  TMatrixFSym w1(5);
199  //par1->CovMatrix(w1);
200  //Float_t pars1[5] = {(Float_t)par1->GetX(), (Float_t)par1->GetY(), (Float_t)par1->GetTx(), (Float_t)par1->GetTy(), 1.0};
201  par1.CovMatrix(w1);
202  Float_t pars1[5] = {(Float_t) par1.GetX(),
203  (Float_t) par1.GetY(),
204  (Float_t) par1.GetTx(),
205  (Float_t) par1.GetTy(),
206  1.0};
207  TMatrixF p1(5, 1, pars1);
208  TMatrixF wp1(w1, TMatrixF::kTransposeMult, p1);
209  //Double_t x0 = parOk.GetX(), y0 = parOk.GetY();
210 
211  for (mit = mitb; mit != mite; ++mit) {
212  Int_t indx = mit->second;
213  CbmMuchTrack* muchTr = (CbmMuchTrack*) fMuchTracks->UncheckedAt(indx);
214  const FairTrackParam* par2 = muchTr->GetParamLast();
215  if (par2->GetY() < yv - window || par2->GetY() > yv + window) continue;
216 
217  TMatrixFSym w2(5);
218  par2->CovMatrix(w2);
219  TMatrixFSym w20 = w2;
220  Float_t pars2[5] = {(Float_t) par2->GetX(),
221  (Float_t) par2->GetY(),
222  (Float_t) par2->GetTx(),
223  (Float_t) par2->GetTy(),
224  1.0};
225  TMatrixF p2(5, 1, pars2);
226  TMatrixF wp2(w2, TMatrixF::kTransposeMult, p2);
227  wp2 += wp1;
228  w2 += w1;
229  w2.Invert();
230  TMatrixF p12(w2, TMatrixF::kTransposeMult, wp2);
231  //p12.Print();
232 
233  // Compute Chi2
234  TMatrixF p122 = p12;
235  TMatrixF pMerge = p12;
236  p12 -= p1;
237  TMatrixF wDp1(w1, TMatrixF::kMult, p12);
238  TMatrixF chi21(p12, TMatrixF::kTransposeMult, wDp1);
239  p122 -= p2;
240  TMatrixF wDp2(w20, TMatrixF::kMult, p122);
241  TMatrixF chi22(p122, TMatrixF::kTransposeMult, wDp2);
242  Double_t c2 = chi21(0, 0) + chi22(0, 0);
243  //cout << " Chi2: " << chi21(0,0) << " " << chi22(0,0) << " " << c2 << endl;
244  if (c2 < 0 || c2 > chi2max) continue;
245 
246  // Merged track parameters
247  parOk.SetX(pMerge(0, 0));
248  parOk.SetY(pMerge(1, 0));
249  parOk.SetZ(par2->GetZ());
250  parOk.SetTx(pMerge(2, 0));
251  parOk.SetTy(pMerge(3, 0));
252  parOk.SetCovMatrix(w2);
253  AddTrack(muchTr, tr, indx, itr, parOk, c2); // add track
254  }
255  }
256 }
257 // -------------------------------------------------------------------------
258 
259 // ----- Private method AddTrack ---------------------------------------
261  CbmMuchTrack* tr2,
262  Int_t indx1,
263  Int_t indx2,
264  FairTrackParam& parOk,
265  Double_t c2) {
266  // Store merged vector (CbmMuchTracks) into TClonesArray
267 
268  Int_t ntrs = fTrackArray->GetEntriesFast();
269 
270  CbmMuchTrack* track = new ((*fTrackArray)[ntrs]) CbmMuchTrack();
271  track->SetParamFirst(&parOk);
272  track->SetChiSq(c2 + tr1->GetChiSq() + tr2->GetChiSq());
273  track->SetNDF(4 + tr1->GetNDF() + tr2->GetNDF());
274 
275  track->SetPreviousTrackId(indx1); // MUCH track index
276  track->AddHit(indx2, kMUCHSTRAWHIT); // TRD vector index
277 
278  if (tr1->GetFlag() == tr2->GetFlag())
279  track->SetFlag(tr1->GetFlag());
280  else
281  track->SetFlag(-1);
282 
283  gLogger->Info(MESSAGE_ORIGIN,
284  "CbmMuchToTrdVectors::AddTrack: trID1=%i, trID2=%i, chi2=%f",
285  tr1->GetFlag(),
286  tr2->GetFlag(),
287  track->GetChiSq());
288 }
289 // -------------------------------------------------------------------------
290 
291 // ----- Private method RemoveClones -----------------------------------
293  // Remove clone tracks (having the same MUCH track)
294 
295  Int_t nvec = fTrackArray->GetEntriesFast();
296 
297  // Do sorting according to "quality"
298  multimap<Double_t, Int_t> qMap;
299  multimap<Double_t, Int_t>::iterator it, it1;
300 
301  for (Int_t i = 0; i < nvec; ++i) {
302  CbmMuchTrack* vec = (CbmMuchTrack*) fTrackArray->UncheckedAt(i);
303  //Double_t qual = vec->GetNofHits() + (99 - TMath::Min(vec->GetChiSq(),99.0)) / 100;
304  Double_t qual = vec->GetChiSq() / vec->GetNDF();
305  qMap.insert(pair<Double_t, Int_t>(qual, i));
306  }
307 
308  for (it = qMap.begin(); it != qMap.end(); ++it) {
309  CbmMuchTrack* vec = (CbmMuchTrack*) fTrackArray->UncheckedAt(it->second);
310  if (vec == NULL) continue;
311  Int_t prevIndx = vec->GetPreviousTrackId();
312 
313  it1 = it;
314  for (++it1; it1 != qMap.end(); ++it1) {
315  CbmMuchTrack* vec1 =
316  (CbmMuchTrack*) fTrackArray->UncheckedAt(it1->second);
317  if (vec1 == NULL) continue;
318 
319  if (vec1->GetPreviousTrackId() == prevIndx) {
320  fTrackArray->RemoveAt(it1->second);
321  continue;
322  }
323  }
324  } // for (it = qMap.begin();
325 
326  fTrackArray->Compress();
327  fNofTracks = fTrackArray->GetEntriesFast();
328 
329  cout << " Global vectors after clones removed: " << nvec << " " << fNofTracks
330  << endl;
331 }
332 // -------------------------------------------------------------------------
333 
CbmTrack::GetChiSq
Double_t GetChiSq() const
Definition: CbmTrack.h:58
CbmTrack::GetParamLast
const FairTrackParam * GetParamLast() const
Definition: CbmTrack.h:62
CbmMuchToTrdVectors
Definition: CbmMuchToTrdVectors.h:24
CbmMuchToTrdVectors::SetParContainers
virtual void SetParContainers()
Definition: CbmMuchToTrdVectors.cxx:59
CbmTrack::GetNofHits
virtual Int_t GetNofHits() const
Definition: CbmTrack.h:53
CbmTrack::GetFlag
Int_t GetFlag() const
Definition: CbmTrack.h:57
CbmMuchToTrdVectors::MergeVectors
void MergeVectors()
Definition: CbmMuchToTrdVectors.cxx:152
CbmMuchToTrdVectors::~CbmMuchToTrdVectors
virtual ~CbmMuchToTrdVectors()
Definition: CbmMuchToTrdVectors.cxx:38
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmMuchToTrdVectors::CbmMuchToTrdVectors
CbmMuchToTrdVectors()
Definition: CbmMuchToTrdVectors.cxx:28
CbmTrack::SetPreviousTrackId
void SetPreviousTrackId(Int_t previousTrackId)
Definition: CbmTrack.h:72
CbmMuchToTrdVectors::Exec
virtual void Exec(Option_t *opt)
Definition: CbmMuchToTrdVectors.cxx:69
CbmTrack::SetParamLast
void SetParamLast(const FairTrackParam *par)
Definition: CbmTrack.h:76
CbmMuchToTrdVectors::Finish
virtual void Finish()
Definition: CbmMuchToTrdVectors.cxx:88
CbmTrack::SetNDF
void SetNDF(Int_t ndf)
Definition: CbmTrack.h:71
CbmMuchTrack
Definition: CbmMuchTrack.h:16
CbmMuchToTrdVectors::Init
virtual InitStatus Init()
Definition: CbmMuchToTrdVectors.cxx:42
CbmTrack::SetFlag
void SetFlag(Int_t flag)
Definition: CbmTrack.h:69
CbmMuchToTrdVectors.h
CbmMuchTrack.h
CbmMuchToTrdVectors::fTrdTracks
TClonesArray * fTrdTracks
Definition: CbmMuchToTrdVectors.h:55
CbmTrack::SetChiSq
void SetChiSq(Double_t chiSq)
Definition: CbmTrack.h:70
CbmTrack::GetNDF
Int_t GetNDF() const
Definition: CbmTrack.h:59
CbmTrack::GetPreviousTrackId
Int_t GetPreviousTrackId() const
Definition: CbmTrack.h:60
CbmMuchToTrdVectors::fXmap
std::multimap< Double_t, Int_t > fXmap
Definition: CbmMuchToTrdVectors.h:58
CbmTrack::GetParamFirst
const FairTrackParam * GetParamFirst() const
Definition: CbmTrack.h:61
CbmMuchToTrdVectors::fMuchTracks
TClonesArray * fMuchTracks
Definition: CbmMuchToTrdVectors.h:54
ClassImp
ClassImp(CbmMuchToTrdVectors)
CbmMuchToTrdVectors::RemoveClones
void RemoveClones()
Definition: CbmMuchToTrdVectors.cxx:292
CbmMuchToTrdVectors::fZ0
Double_t fZ0
Definition: CbmMuchToTrdVectors.h:57
CbmMuchToTrdVectors::AddTrack
void AddTrack(CbmMuchTrack *tr1, CbmMuchTrack *tr2, Int_t indx1, Int_t indx2, FairTrackParam &parOk, Double_t c2)
Definition: CbmMuchToTrdVectors.cxx:260
CbmMuchToTrdVectors::fNofTracks
Int_t fNofTracks
Definition: CbmMuchToTrdVectors.h:53
kMUCHSTRAWHIT
@ kMUCHSTRAWHIT
Definition: CbmHit.h:24
CbmTrack::SetParamFirst
void SetParamFirst(const FairTrackParam *par)
Definition: CbmTrack.h:75
CbmMuchToTrdVectors::GetMuchVectors
void GetMuchVectors()
Definition: CbmMuchToTrdVectors.cxx:92
CbmMuchToTrdVectors::fTrackArray
TClonesArray * fTrackArray
Definition: CbmMuchToTrdVectors.h:45
CbmTrack::AddHit
void AddHit(Int_t index, HitType type)
Definition: CbmTrack.cxx:75