FairRoot/PandaRoot
PndMvdRiemannVertexFinderTask.cxx
Go to the documentation of this file.
2 
3 #include <iostream>
4 #include <math.h>
5 
6 // Root includes
7 #include "TROOT.h"
8 #include "TString.h"
9 #include "TClonesArray.h"
10 #include "TParticlePDG.h"
11 #include "TVector3.h"
12 #include "TH1F.h"
13 
14 // framework includes
15 #include "FairRootManager.h"
16 #include "FairRun.h"
17 #include "FairRuntimeDb.h"
18 #include "PndMCTrack.h"
19 
20 
21 // PndMvd includes
22 // #include "PndMvdTrackCand.h"
23 #include "PndSdsHit.h"
24 #include "PndSdsMCPoint.h"
25 #include "PndSdsCluster.h"
26 #include "PndSdsDigi.h"
27 //#include "PndRiemannTrackFinder.h"
28 #include "PndRiemannHit.h"
29 #include "PndRiemannTrack.h"
30 #include "PndDetectorList.h"
31 #include "PndSdsMCPoint.h"
32 
33 
34 PndMvdRiemannVertexFinderTask::PndMvdRiemannVertexFinderTask() : FairTask("MVD Riemann VERTEX Finder"),
35  delta(NULL),
36  wrongV(NULL),
37  eff(),
38  ghosts(),
39  fHitBranch("MVDHitsPixel"),
40  fHitBranch2("MVDHitsStrip"),
41  fTrackBranch("MVDRiemannTrackCand"),
42  fIdealTrackCandBranch("MVDIdealTrackCand"),
43  fMCTrackBranch("MCTrack"),
44  fEventNr(0),
45  fVerbose(0),
46  fVertexCut(0.),
47  fHitArray(NULL),
48  fHitArray2(NULL),
49  fTrackCandArray(NULL),
50  fTrackArray(NULL),
51  fIdealTrackCandArray(NULL),
52  fMCTrackArray(NULL),
53  fVertex(NULL),
54  fMCVertex(NULL)
55 {
56 }
57 
59 {
60 }
61 
63 {
64  // Get Base Container
65 /*
66  FairRun* ana = FairRun::Instance();
67  FairRuntimeDb* rtdb=ana->GetRuntimeDb();
68  fGeoPar = (PndMvdGeoPar*)(rtdb->getContainer("PndMvdGeoPar"));
69 */
70 }
71 
73 {
74 
75  InitStatus stat=kSUCCESS;
76  return stat;
77 
78  /*
79  FairRun* ana = FairRun::Instance();
80  FairRuntimeDb* rtdb=ana->GetRuntimeDb();
81  fGeoPar=(PndMvdGeoPar*)(rtdb->getContainer("PndMvdGeoPar"));
82 
83  return kSUCCESS;
84  */
85 }
86 
87 // ----- Public method Init --------------------------------------------
89 {
90 
91  FairRootManager* ioman = FairRootManager::Instance();
92 
93  if ( ! ioman )
94  {
95  std::cout << "-E- PndMvdRiemannVertexFinderTask::Init: "
96  << "RootManager not instantiated!" << std::endl;
97  return kFATAL;
98  }
99 
100  // Get input array
101  fHitArray = (TClonesArray*) ioman->GetObject(fHitBranch);
102  if ( !fHitArray){
103  std::cout << "-W- PndMvdRiemannVertexFinderTask::Init: " << "No hitArray!" << std::endl;
104  return kERROR;
105  }
106 
107  fHitArray2 = (TClonesArray*) ioman->GetObject(fHitBranch2);
108  if ( !fHitArray2){
109  std::cout << "-W- PndMvdRiemannVertexFinderTask::Init: " << "No hitArray2!" << std::endl;
110  return kERROR;
111  }
112 
113 
114  fTrackCandArray = (TClonesArray*) ioman->GetObject(fTrackBranch);
115  if ( !fTrackCandArray){
116  std::cout << "-W- PndMvdRiemannVertexFinderTask::Init: " << "No tracks!" << std::endl;
117  return kERROR;
118  }
119 
120  fIdealTrackCandArray= (TClonesArray*) ioman->GetObject(fIdealTrackCandBranch);
121  if ( !fIdealTrackCandArray){
122  std::cout << "-W- PndMvdRiemannVertexFinderTask::Init: " << "No ideal tracks!" << std::endl;
123  return kERROR;
124  }
125 
126 
127  fMCTrackArray = (TClonesArray*) ioman->GetObject(fMCTrackBranch);
128  if ( !fMCTrackArray){
129  std::cout << "-W- PndMvdRiemannVertexFinderTask::Init: " << "No MC tracks!" << std::endl;
130  return kERROR;
131  }
132 
133  fTrackArray = new TClonesArray("PndRiemannTrack");
134 
135  delta = new TH1F("delta","delta",1000,0,10);
136  wrongV= new TH1F("wrong","wrong",1000,0,10);
137 
138  fVertex = new TClonesArray("PndSdsMCPoint");
139  ioman->Register("Vertex", "MVD", fVertex, kTRUE);
140 
141  fMCVertex = new TClonesArray("PndSdsMCPoint");
142  ioman->Register("MCVertex", "MVD", fMCVertex, kTRUE);
143 
144 // fRiemannTrackArray = new TClonesArray("PndRiemannTrack");
145 // ioman->Register("MVDRiemannTrack","MVD",fRiemannTrackArray, kTRUE);
146 
147  std::cout << "-I- PndMvdRiemannVertexFinderTask: Initialisation successfull" << std::endl;
148  return kSUCCESS;
149 }
150 
151 // ----- Public method Exec --------------------------------------------
153 {
154  // Reset output array
155  if ( ! fTrackCandArray )
156  Fatal("Exec", "No trackCandArray");
157  std::vector<int> CheckedCand;
158  refit(CheckedCand);
159 
160 
161 
162  std::vector< std::pair <int,int> > PairCand;
163  std::vector< std::pair <int,int> > TrueMCCand;
164  std::vector< std::pair <int,int> > FalseMCCand;
165  std::vector< std::pair <int,int> > MCCand;
166  int MaxIndex=0;
167  FindVertex(CheckedCand,PairCand,TrueMCCand,FalseMCCand,MCCand ,MaxIndex);
168  CalcEfficiency(TrueMCCand, FalseMCCand, MCCand);
169 
170  std::cout << "Done event : " << fEventNr++ << std::endl;
171 }
172 void PndMvdRiemannVertexFinderTask::CalcEfficiency(std::vector< std::pair <int,int> > TrueMCCand,
173  std::vector< std::pair <int,int> > FalseMCCand,std::vector< std::pair <int,int> > MCCand)
174 {
175  int counter=0;
176  for(unsigned int i=0;i<MCCand.size();i++){
177  int MCfirst=MCCand[i].first;
178  int MCsecond=MCCand[i].second;
179  for(unsigned int j=0;j<TrueMCCand.size();j++){
180  int first=TrueMCCand[j].first;
181  int second=TrueMCCand[j].second;
182  if ((MCfirst==first && MCsecond==second) or (MCfirst==second && MCsecond==first)){
183  counter++;
184  break;
185  }
186  }
187  }
188  eff.first+=counter;
189  eff.second+=MCCand.size();
190  ghosts.first+=FalseMCCand.size();
191  ghosts.second+=MCCand.size();
192 // std::cout<<"eff= "<<((double)counter)/((double)MCCand.size())<<std::endl;
193 // std::cout<<"ghosts= "<<((double)FalseMCCand.size())/((double)MCCand.size())<<std::endl;
194 
195 }
196 
197 void PndMvdRiemannVertexFinderTask::FindVertex(std::vector<int> CheckedCand ,std::vector< std::pair <int,int> >& PairCand,std::vector< std::pair <int,int> >& TrueMCCand,
198  std::vector< std::pair <int,int> >& FalseMCCand, std::vector< std::pair <int,int> >& MCCand , int& MaxIndex)
199 {
200  for(int i=0;i<fTrackArray->GetEntriesFast();i++){
202  track1.SetVerbose(-1);
203  for(int j=i+1;j<fTrackArray->GetEntriesFast();j++){
205  track2.SetVerbose(-1);
206  TVector3 p1,p2,av;
207  if ((track2.calcIntersection(track1,p1,p2)==1) && CheckTwoCands(CheckedCand[i],CheckedCand[j] ) ){
208  // std::cout<<"Vertex: "<<i<<" : "<<j<<std::endl;
209  pair <int,int> Pair(i,j);
210  PairCand.push_back(Pair);
211  if (j>MaxIndex) MaxIndex=j;
212  av=p1+p2;
213  av*=0.5;
214  PndSdsMCPoint* Vertex1 = new PndSdsMCPoint(0,0,-1,p1,p1,p1,p1,0,0,0);
215  PndSdsMCPoint* Vertex2 = new PndSdsMCPoint(0,0,-1,p2,p2,p2,p2,0,0,0);
216  new((*fVertex)[fVertex->GetEntriesFast()])PndSdsMCPoint(*Vertex1);
217  new((*fVertex)[fVertex->GetEntriesFast()])PndSdsMCPoint(*Vertex2);
218 
219  int c1=FoundCandInMCCands(CheckedCand[i]);
220  int c2=FoundCandInMCCands(CheckedCand[j]);
221  if (c1>-1 && c2>-1){
222  // std::cout<<c1<<" "<<c2<<std::endl;
224  PndMCTrack* myTrack1 = (PndMCTrack*)fMCTrackArray->At(Cand1->getMcTrackId());
226  PndMCTrack* myTrack2 = (PndMCTrack*)fMCTrackArray->At(Cand2->getMcTrackId());
227  if (((myTrack1->GetPdgCode()==myTrack2->GetPdgCode()) or ((myTrack1->GetPdgCode()*myTrack2->GetPdgCode()<0) && (fabs(myTrack1->GetPdgCode())!=fabs(myTrack2->GetPdgCode()))))){
228  delta->Fill((myTrack1->GetStartVertex()-p1).Mag());
229  delta->Fill((myTrack1->GetStartVertex()-p2).Mag());
230  pair <int,int> Pair2(c1,c2);
231  TrueMCCand.push_back(Pair2);
232  }
233  else{
234  wrongV->Fill((myTrack1->GetStartVertex()-p1).Mag());
235  wrongV->Fill((myTrack1->GetStartVertex()-p2).Mag());
236  pair <int,int> Pair3(c1,c2);
237  FalseMCCand.push_back(Pair3);
238  }
239 
240  }
241  }
242  }
243  }
244 
245  for(int i=0;i<fIdealTrackCandArray->GetEntriesFast();i++){
247  PndMCTrack* myTrack1 = (PndMCTrack*)fMCTrackArray->At(Cand1->getMcTrackId());
248  for(int j=i+1;j<fIdealTrackCandArray->GetEntriesFast();j++){
250  PndMCTrack* myTrack2 = (PndMCTrack*)fMCTrackArray->At(Cand2->getMcTrackId());
251  if (((myTrack1->GetPdgCode()==myTrack2->GetPdgCode()) or ((myTrack1->GetPdgCode()*myTrack2->GetPdgCode()<0) && (fabs(myTrack1->GetPdgCode())!=fabs(myTrack2->GetPdgCode()))))){
252  if(CheckRecoTrack(Cand1,myTrack1) && CheckRecoTrack(Cand2,myTrack2) ){
253  pair <int,int> Pair(i,j);
254  MCCand.push_back(Pair);
255  // std::cout<<"IdealVertex: "<<i<<" : "<<j<<std::endl;
256  }
257  }
258 
259  }
260 
261  }
262 
263 
264 }
265 
266 
267 void PndMvdRiemannVertexFinderTask::refit(std::vector<int>& CheckedCand)
268 {
269  for(int i=0;i<fTrackCandArray->GetEntriesFast();i++){
271  unsigned int detId,hitId;
273  for(unsigned int j=0;j<Cand->GetNHits();j++){
274  detId=Cand->GetSortedHit(j).GetDetId();
275  hitId=Cand->GetSortedHit(j).GetHitId();
276  PndSdsHit* point = new PndSdsHit();
277  // std::cout<<"detId= "<<detId<<" "<<"hitId= "<<hitId<<std::endl;
278  if ((int)detId==FairRootManager::Instance()->GetBranchId(fHitBranch))
279  point=(PndSdsHit*)fHitArray->At(hitId);
280  else if ((int)detId==FairRootManager::Instance()->GetBranchId(fHitBranch2))
281  point=(PndSdsHit*)fHitArray2->At(hitId);
282  else point=0;
283  if (point!=0){
285  hit.setXYZ(point->GetX(),point->GetY(),point->GetZ());
286  hit.setDXYZ(point->GetDx(),point->GetDy(),point->GetDz());
287  track.addHit(hit);
288  }
289  }
290  track.refit(true);
291  track.szFit(true);
292  track.SetVertexCut(fVertexCut);
293  int size=fTrackArray->GetEntriesFast();
294  new ((*fTrackArray)[size])PndRiemannTrack(track);
295  CheckedCand.push_back(i);
296  }
297  std::cout<<"--------------------------------------------------"<<fTrackCandArray->GetEntriesFast()<<std::endl;
298 }
299 
300 
301 
303 {
304  PndTrackCand* FoundCand=(PndTrackCand*)fTrackCandArray->At(candN);
305  for(int k=0;k<fIdealTrackCandArray->GetEntriesFast();k++){
307  unsigned int counter=0;
308  bool ZeroPos=false;
309  for(unsigned int i=0;i<FoundCand->GetNHits();i++){
310  unsigned int d1,h1;
311  d1=FoundCand->GetSortedHit(i).GetDetId();
312  h1=FoundCand->GetSortedHit(i).GetHitId();
313  if (d1==0){
314  ZeroPos=true;
315  continue;
316  }
317  for(unsigned int j=0;j<TestCand->GetNHits();j++){
318  unsigned int d2,h2;
319  d2=TestCand->GetSortedHit(j).GetDetId();
320  h2=TestCand->GetSortedHit(j).GetHitId();
321  if(d1==d2 && h1==h2){
322  counter++;
323  }
324  }
325  }
326  if ((counter==FoundCand->GetNHits() && !ZeroPos) or (counter==FoundCand->GetNHits()-1 && ZeroPos)){
327  return k;
328  }
329  }
330  return -1;
331 }
332 
333 
334 
335 bool PndMvdRiemannVertexFinderTask::CheckVertex(std::vector<int> Combination, std::vector< std::pair<int,int> > PairCand)
336 {
337  int count1=0;
338  for(unsigned int i=0;i<PairCand.size();i++){
339  int ind1=PairCand[i].first,ind2=PairCand[i].second;
340  int count2=0;
341  for(unsigned int j=0;j<Combination.size();j++){
342  if ((ind1==Combination[j]) or (ind2==Combination[j]))
343  count2++;
344  }
345  if (count2==2)
346  count1++;
347  }
348 
349  if (count1==3)
350  return true;
351  else return false;
352 }
354 {
355  PndTrackCand* cand1=(PndTrackCand*)fTrackCandArray->At(first);
356  PndTrackCand* cand2=(PndTrackCand*)fTrackCandArray->At(second);
357  int counter=0;
358  for(unsigned int i=0;i<cand1->GetNHits();i++){
359  unsigned int d1,h1;
360  d1=cand1->GetSortedHit(i).GetDetId();
361  h1=cand1->GetSortedHit(i).GetHitId();
362  for(unsigned int j=0;j<cand2->GetNHits();j++){
363  unsigned int d2,h2;
364  d2=cand2->GetSortedHit(j).GetDetId();
365  h2=cand2->GetSortedHit(j).GetHitId();
366  if(d1==d2 && h1==h2){
367  counter++;
368  }
369  }
370  }
371  if (counter<3)
372  return true;
373  else
374  return false;
375 }
376 
378 {
379  if (/*myTrack->GetPdgCode()==211 &&*/ (cand->GetNHits()>2) && (myTrack->GetMotherID()==-1)){
380  int count=0;
381  unsigned int detIDi, hitIDi;
382  unsigned int detIDj, hitIDj;
383  for(unsigned int i=0;i<cand->GetNHits();i++){
384  detIDi=cand->GetSortedHit(i).GetDetId();
385  hitIDi=cand->GetSortedHit(i).GetHitId();
386  PndSdsHit *pointI;
387  if ((int)detIDi == FairRootManager::Instance()->GetBranchId(fHitBranch))
388  pointI = (PndSdsHit*)fHitArray->At(hitIDi);
389  else if ((int)detIDi == FairRootManager::Instance()->GetBranchId(fHitBranch2))
390  pointI = (PndSdsHit*)fHitArray2->At(hitIDi);
391  else pointI = 0;
392 
393  for(unsigned int j=0;j<cand->GetNHits();j++){
394  detIDj=cand->GetSortedHit(j).GetDetId();
395  hitIDj=cand->GetSortedHit(j).GetHitId();
396  PndSdsHit *pointJ;
397  if ((int)detIDj == FairRootManager::Instance()->GetBranchId(fHitBranch))
398  pointJ = (PndSdsHit*)fHitArray->At(hitIDj);
399  else if ((int)detIDj == FairRootManager::Instance()->GetBranchId(fHitBranch2))
400  pointJ = (PndSdsHit*)fHitArray2->At(hitIDj);
401  else pointJ = 0;
402 
403  if ((pointI!=0) && (pointJ!=0) && (i!=j)){
404  TVector3 a=pointI->GetPosition() - pointJ->GetPosition();
405  if (a.Mag()<1){
406  count++;
407  }
408  }
409  }
410  }
411  if ((cand->GetNHits()-count/2)<3){
412 // std::cout <<"Less then 3 base points in RecoTrack"<<" "<<cand->getNHits()<<" "<<count <<std::endl;
413  return false;
414  }
415  else return true;
416 
417 
418  }
419  else
420  return false;
421 }
422 
424 {
425  fTrackCandArray->Clear();
426  fIdealTrackCandArray->Clear();
427  fTrackArray->Clear();
428 
429  fVertex->Clear();
430  fMCVertex->Clear();
431 }
432 
434 
int fVerbose
Definition: poormantracks.C:24
int getMcTrackId() const
Definition: PndTrackCand.h:60
Int_t i
Definition: run_full.C:25
TVector3 GetPosition() const
Definition: PndSdsHit.h:93
void refit(std::vector< int > &CheckedCand)
bool CheckRecoTrack(PndTrackCand *cand, PndMCTrack *myTrack)
Int_t GetPdgCode() const
Definition: PndMCTrack.h:73
PndTrackCandHit GetSortedHit(UInt_t i)
Definition: PndTrackCand.h:54
void FindVertex(std::vector< int > CheckedCand, std::vector< std::pair< int, int > > &PairCand, std::vector< std::pair< int, int > > &TrueMCCand, std::vector< std::pair< int, int > > &FalseMCCand, std::vector< std::pair< int, int > > &MCCand, int &MaxIndex)
c2
Definition: plot_dirc.C:39
void setDXYZ(double dx, double dy, double dz)
TVector3 fVertex(0., 0., 0.)
Int_t a
Definition: anaLmdDigi.C:126
int counter
Definition: ZeeAnalysis.C:59
void SetVertexCut(double cut)
void setXYZ(double x, double y, double z)
PndMCTrack * track
Definition: anaLmdCluster.C:89
void refit(bool withErrorCalc=true)
friend F32vec4 fabs(const F32vec4 &a)
Definition: P4_F32vec4.h:47
void SetVerbose(int i)
c1
Definition: plot_dirc.C:35
TPad * p2
Definition: hist-t7.C:117
void CalcEfficiency(std::vector< std::pair< int, int > > TrueMCCand, std::vector< std::pair< int, int > > FalseMCCand, std::vector< std::pair< int, int > > MCCand)
int calcIntersection(PndRiemannTrack &track, TVector3 &p1, TVector3 &p2)
UInt_t GetNHits() const
Definition: PndTrackCand.h:59
ClassImp(PndAnaContFact)
TPad * p1
Definition: hist-t7.C:116
void szFit(bool withErrorCalc=true)
void addHit(PndRiemannHit &hit)
int count
PndSdsMCPoint * hit
Definition: anasim.C:70
TVector3 GetStartVertex() const
Definition: PndMCTrack.h:76
Int_t GetMotherID() const
Definition: PndMCTrack.h:74
Int_t GetHitId() const
Int_t GetDetId() const
bool CheckVertex(std::vector< int > Combination, std::vector< std::pair< int, int > > PairCand)
PndSdsMCPoint * point
Definition: anaLmdCluster.C:72