FairRoot/PandaRoot
PndLmdTrksFilterTask.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- PndLmdTrksFilterTask source file -----
3 // ----- Created 06/05/13 by A.Karavdina -----
4 // -------------------------------------------------------------------------
5 // libc includes
6 #include <iostream>
7 
8 // Root includes
9 #include <TMatrixDSym.h>
10 #include "TClonesArray.h"
11 #include "TROOT.h"
12 #include "TVector3.h"
13 
14 // framework includes
15 #include "FairHit.h"
16 #include "FairRootManager.h"
17 #include "FairRun.h"
18 #include "FairRuntimeDb.h"
19 #include "PndLmdTrksFilterTask.h"
20 //#include "PndMCTrack.h"
21 // #include "FairBaseParSet.h"
22 // #include "TGeant3.h"
23 // #include "FairTrackParH.h"
24 // #include "FairTrackParP.h"
25 // #include "TDatabasePDG.h"
26 #include "PndTrack.h"
27 
28 // PndSds includes
29 #include "PndSdsHit.h"
30 #include "PndSdsMCPoint.h"
31 #include "PndSdsMergedHit.h"
32 // PndLmd includes
33 //#include "PndLinTrack.h"
34 
35 #include <map>
36 #include <vector>
37 
38 // ----- Default constructor -------------------------------------------
40  : FairTask("Tracks filtering Task for PANDA Lmd"),
41  fEventNr(0),
42  fdX(0),
43  fdY(0) {
44  flSkipKinFilt = false;
45  flBOXKinFilt = false;
46  flXThKinFilt = false;
47  fHitName = "LMDHitsMerged";
48  fMCHitName = "LMDPoint";
49  // fClusterName = clusterBranch;
50  // fDigiName = digiBrunch;
51  fTrkCandName = "LMDTrackCand";
52  fTrkName = "LMDPndTrack";
53  fTrkOutName = "LMDPndTrackFilt";
54 }
55 
56 // ----- Destructor ----------------------------------------------------
58 
59 // ----- Public method Init --------------------------------------------
61  // Get RootManager
62  FairRootManager* ioman = FairRootManager::Instance();
63  if (ioman == 0) {
64  Error("PndLmdTrksFilterTask::Init", "RootManager not instantiated!");
65  return kERROR;
66  }
67 
68  fTrkArray = (TClonesArray*)ioman->GetObject(fTrkName);
69  if (fTrkArray == 0) {
70  Error("PndLmdTrksFilterTask::Init", "track-array not found!");
71  return kERROR;
72  }
73 
74  fTrkCandArray = (TClonesArray*)ioman->GetObject(fTrkCandName);
75  if (fTrkCandArray == 0) {
76  Error("PndLmdTrksFilterTask::Init", "trk-cand--array not found!");
77  return kERROR;
78  }
79 
80  fTrkOutArray = new TClonesArray("PndTrack");
81  ioman->Register("LMDPndTrackFilt", "PndLmd", fTrkOutArray, kTRUE);
82 
83  // htthetatphiTrkFit = new
84  // TNtuple("htthetatphiTrkFit","ntthetatphiTrk","tg_theta:tg_phi");
85  return kSUCCESS;
86 }
87 // -------------------------------------------------------------------------
88 
89 // ----- Public method Exec --------------------------------------------
90 void PndLmdTrksFilterTask::Exec(Option_t*) {
91  fTrkOutArray->Delete();
92  if (fVerbose > 4) {
93  cout << "" << endl;
94  cout << "--------- New Event " << fEventNr << "--------" << endl;
95  }
96 
97  // go through all tracks
98  const unsigned int gll = fTrkArray->GetEntriesFast();
99  int trksH0[gll]; // hits on pl#0
100  int trksH1[gll]; // hits on pl#1
101  int trksH2[gll]; // hits on pl#2
102  int trksH3[gll]; // hits on pl#3
103  vector<unsigned int> trkHn; // number of hits in trk
104  vector<bool> trk_accept;
105  // vector<bool> stopch; //if trks were checked by chi2 there is no need to
106  // check them futher
107  vector<double> vchi2;
108  for (unsigned int i = 0; i < gll; i++) {
109  trksH0[i] = -1;
110  trksH1[i] = -1;
111  trksH2[i] = -1;
112  trksH3[i] = -1;
113  }
114  // fill vectors with trks hits
115  for (unsigned int i = 0; i < gll; i++) {
116  PndTrack* trkpnd = (PndTrack*)(fTrkArray->At(i));
117  bool dirOK = true;
118  // check trk kinematics -----
119  FairTrackParP fFittedTrkP = trkpnd->GetParamFirst();
120 
121  if (!flSkipKinFilt) {
122  TVector3 MomRecLMD(fFittedTrkP.GetPx(), fFittedTrkP.GetPy(),
123  fFittedTrkP.GetPz());
124  MomRecLMD *= 1. / MomRecLMD.Mag();
125  if (fVerbose > 0) cout << "Check Kinematics!" << endl;
126  // cout<<"FLAGS: "<<bool(flSkipKinFilt)<<" "<<bool(flBOXKinFilt)<<"
127  // "<<bool(flXThKinFilt)<<endl;
128  if (flBOXKinFilt) {
129  if (fVerbose > 0) cout << "!BOXThPhFilt!" << endl;
130  double thetaCent = MomRecLMD.Theta() - 0.040;
131  // if(fVerbose<2){
132  if (abs(thetaCent) > 0.011 || abs(MomRecLMD.Phi()) > 0.25)
133  dirOK = false;
134  if (abs(fFittedTrkP.GetX()) > 1000. || abs(fFittedTrkP.GetY()) > 1000)
135  dirOK = false; // misaligned sensors give wierd results
136  // }
137  }
138  // else{
139  if (flXThKinFilt) {
140  // double dist_max = 1.4;
141  // if(fFittedTrkP.GetPz()<2) dist_max = 1.8;
142  if (fVerbose > 0) cout << "!XThFilt!" << endl;
143  double Xref = -19.1 + 1.12 * 1e3 * MomRecLMD.Theta() + fdX;
144  double diffX = abs(fFittedTrkP.GetX() - Xref);
145  if (fVerbose > 0)
146  cout << "fFittedTrkP.GetX() = " << fFittedTrkP.GetX()
147  << " Xref = " << Xref << " diffX = " << diffX << endl;
148  // if(diffX>1.5) dirOK=false;
149  if (diffX > 3.0) dirOK = false;
150  }
151  if (flYPhKinFilt) {
152  // double dist_max = 1.4;
153  // if(fFittedTrkP.GetPz()<2) dist_max = 1.8;
154  if (fVerbose > 0) cout << "!YPhFilt!" << endl;
155  double Yref = -0.00651 + 0.045 * 1e3 * MomRecLMD.Phi() + fdY;
156  double diffY = abs(fFittedTrkP.GetY() - Yref);
157  if (fVerbose > 0)
158  cout << "fFittedTrkP.GetY() = " << fFittedTrkP.GetY()
159  << " Yref = " << Yref << " diffY = " << diffY << endl;
160  // if(diffY>2.1) dirOK=false;
161  if (diffY > 4.0) dirOK = false;
162  }
163 
164  // }
165  }
166 
167  // //--------------------------
168 
169  double chi2 = trkpnd->GetChi2();
170  vchi2.push_back(chi2);
171  trk_accept.push_back(dirOK);
172  // stopch.push_back(false);
173 
174  int candID = trkpnd->GetRefIndex();
175  PndTrackCand* trkcand = (PndTrackCand*)fTrkCandArray->At(candID);
176  const unsigned int Ntrkcandhits = trkcand->GetNHits();
177  trkHn.push_back(Ntrkcandhits);
178  }
179 
180  // compare trks on hit level
181  for (int ittr = 0; ittr < 2;
182  ittr++) { // repeat comparision twice to avoid accepting trk twice due
183  // to different check
184  for (unsigned int i = 0; i < gll; i++) {
185  // if(stopch[i]) continue;
186  if (!trk_accept[i]) continue;
187  for (unsigned int j = i + 1; j < gll; j++) {
188  // if(stopch[j]) continue;
189  if (!trk_accept[j]) continue;
190  int coundduphit = 4; // count dublicate hits
191  if (trksH0[i] != trksH0[j]) coundduphit--;
192  if (trksH1[i] != trksH1[j]) coundduphit--;
193  if (trksH2[i] != trksH2[j]) coundduphit--;
194  if (trksH3[i] != trksH3[j]) coundduphit--;
195 
196  if (coundduphit > 1) { // if 2 and more hits are similar
197  if (trkHn[i] > trkHn[j]) { // take trk with higher number of hits
198  trk_accept[i] = true;
199  trk_accept[j] = false;
200  } else {
201  if (trkHn[i] < trkHn[j]) { // take trk with higher number of hits
202  trk_accept[i] = false;
203  trk_accept[j] = true;
204  } else {
205  if (vchi2[i] > vchi2[j]) {
206  trk_accept[i] = false;
207  trk_accept[j] = true;
208  } else {
209  trk_accept[i] = true;
210  trk_accept[j] = false;
211  }
212  // stopch[i]=true;
213  // stopch[j]=true;
214  }
215  }
216  if (fVerbose > 4) {
217  cout << " trk#" << i << ": has " << trkHn[i] << "hits; trk#" << j
218  << ": has " << trkHn[j] << " hits" << endl;
219  cout << " trk#" << i << " has chi2=" << vchi2[i] << " "
220  << " trk#" << j << " has chi2=" << vchi2[j] << endl;
221  if (trk_accept[i]) cout << " trk#" << i << " accepted" << endl;
222  if (trk_accept[j]) cout << " trk#" << j << " accepted" << endl;
223  }
224  } // if 2 and more hits are similar
225  else {
226  if (coundduphit > 0 && trkHn[i] == 3 &&
227  trkHn[j] == 3) { // if 1 hit is similar
228  if (vchi2[i] > vchi2[j]) {
229  trk_accept[i] = false;
230  trk_accept[j] = true;
231  } else {
232  trk_accept[i] = true;
233  trk_accept[j] = false;
234  }
235  } // if 1 hit is similar
236  }
237  }
238  }
239  }
240 
241  // save good trks
242  int rec_trk = 0;
243  for (unsigned int i = 0; i < gll; i++) {
244  if (trk_accept[i]) {
245  PndTrack* trkpnd = (PndTrack*)(fTrkArray->At(i));
246 
247  new ((*fTrkOutArray)[rec_trk]) PndTrack(*(trkpnd)); // save Track
248  rec_trk++;
249  }
250  }
251  if (fVerbose > 2)
252  cout << "Ev#" << fEventNr << ": " << rec_trk << " trks saved out of " << gll
253  << endl;
254 
255  if (fVerbose > 2) cout << "PndLmdTrksFilterTask::Exec END!" << endl;
256  fEventNr++;
257 }
258 
260 
int fVerbose
Definition: poormantracks.C:24
Int_t i
Definition: run_full.C:25
double diffX
Definition: anaLmdReco.C:76
virtual void Exec(Option_t *opt)
double diffY
Definition: anaLmdReco.C:76
Double_t GetChi2() const
Definition: PndTrack.h:34
UInt_t GetNHits() const
Definition: PndTrackCand.h:59
TClonesArray * fTrkCandArray
Int_t GetRefIndex() const
Definition: PndTrack.h:36
virtual InitStatus Init()
FairTrackParP GetParamFirst()
Definition: PndTrack.h:49
ClassImp(PndLmdTrksFilterTask)