EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PndRecoKalmanFit.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PndRecoKalmanFit.cxx
1 //-----------------------------------------------------------
2 // File and Version Information:
3 // $Id$
4 //
5 // Description:
6 // Implementation of class PndRecoKalmanFit
7 // see PndRecoKalmanFit.h for details
8 //
9 // Environment:
10 // Software developed for the PANDA Detector at FAIR.
11 //
12 // Author List:
13 // Stefano Spataro, UNI Torino
14 //
15 //-----------------------------------------------------------
16 
17 // Panda Headers ----------------------
18 
19 // This Class' Header ------------------
20 #include "PndRecoKalmanFit.h"
21 
22 // C/C++ Headers ----------------------
23 #include <algorithm>
24 #include <iostream>
25 #include <assert.h>
26 #include <cmath>
27 
28 // Collaborating Class Headers --------
29 #include "FairRootManager.h"
30 #include "FairRuntimeDb.h"
31 #include "FairRunAna.h"
32 #include "TClonesArray.h"
33 
34 #include "GFTrack.h"
35 //#include "TDatabasePDG.h"
36 
37 #include "PndGenfitAdapters.h"
38 #include "PndTrack.h"
39 #include "PndTrackCand.h"
40 #include "PndDetectorList.h"
41 #include "PndGeoHandling.h"
42 #include "GFRecoHitFactory.h"
43 #include "GFKalman.h"
44 #include "GFException.h"
45 #include "TLorentzVector.h"
46 
47 #include "FairTrackParH.h"
48 
49 #include "GeaneTrackRep.h"
50 #include "RKTrackRep.h"
51 #include "GFFieldManager.h"
52 #include "PndGenfitField.h"
53 #include "FairGeanePro.h"
54 
55 // Class Member definitions -----------
56 
57 
58 PndRecoKalmanFit::PndRecoKalmanFit(): TNamed("Genfit", "Fit Tracks"),
59  fMvdBranchName(""), fCentralTrackerBranchName(""),
60  fUseGeane(kTRUE), fPropagateToIP(kTRUE), fPerpPlane(kFALSE), fNumIt(1), fVerbose(0), fTrackRep(0)
61 {
63 }
64 
66 {
67  //Get ROOT Manager
69  if(ioman==0)
70  {
71  Error("PndRecoKalmanFit::Init","RootManager not instantiated!");
72  return kFALSE;
73  }
74 
75  // STT map loading
76  //FairRuntimeDb* rtdb = FairRunAna::Instance()->GetRuntimeDb();
77 
78  //fVerbose = 2;
79 
80  // Build hit factory -----------------------------
82  if (fVerbose<2) GFException::quiet(true);
83 
84  if (fUseGeane)
85  {
86  fPro = new FairGeanePro();
87  //if (fVerbose==0) fPro->SetPrintErrors(kFALSE);
88  }
89  else
90  {
91  Error("PndRecoKalmanFit::Init","Only GEANE Propagatio available!!!");
92  return kFALSE;
93  }
94 
96 
98 
99  std::cout << "===PndRecoKalmanFit::Init() finished ===================================================" << std::endl;
100 
101  return kTRUE;
102 }
103 
104 
106 
107 #include <FairHit.h>
108 #include <EicMoCaPoint.h>
109 #include <GFTools.h>
110 
111 PndTrack* PndRecoKalmanFit::Fit(PndTrack *tBefore, Int_t PDG, bool store_track_parameterization)
112 {
113  PndTrack* tAfter = NULL;
114  if (fVerbose>0) std::cout<<"PndRecoKalmanFit::Fit"<<std::endl;
115  if (fabs(tBefore->GetParamFirst().GetPz())<1e-9)
116  {
117  tAfter = tBefore;
118  tAfter->SetFlag(-10);
119  return tAfter; // flag -10 : pz==0
120  }
121 
122  Int_t fCharge= tBefore->GetParamFirst().GetQ();
123  Int_t PDGCode= PDG;
124  TVector3 StartPos(tBefore->GetParamFirst().GetX(),tBefore->GetParamFirst().GetY(),tBefore->GetParamFirst().GetZ());
125  TVector3 StartMom(tBefore->GetParamFirst().GetPx(),tBefore->GetParamFirst().GetPy(),tBefore->GetParamFirst().GetPz());
126  TVector3 StartPosErr(tBefore->GetParamFirst().GetDX(),tBefore->GetParamFirst().GetDY(),tBefore->GetParamFirst().GetDZ());
127  TVector3 StartMomErr(tBefore->GetParamFirst().GetDPx(),tBefore->GetParamFirst().GetDPy(),tBefore->GetParamFirst().GetDPz());
128 
129  GFAbsTrackRep* rep = 0;
130 
131  if (fPropagateToIP)
132  {
133  // Calculating params at PCA to Origin
134  FairTrackParP par = tBefore->GetParamFirst();
135  Int_t ierr = 0;
136  FairTrackParH *helix = new FairTrackParH(&par, ierr);
137  FairGeanePro *fPro0 = new FairGeanePro();
138  //if (fVerbose==0) fPro0->SetPrintErrors(kFALSE);
139  FairTrackParH *fRes= new FairTrackParH();
140  fPro0->SetPoint(TVector3(0,0,0));
141  fPro0->PropagateToPCA(1, -1);
142  Bool_t rc = fPro0->Propagate(helix, fRes, PDGCode);
143  if (rc)
144  {
145  StartPos.SetXYZ(fRes->GetX(), fRes->GetY(), fRes->GetZ());
146  StartMom.SetXYZ(fRes->GetPx(), fRes->GetPy(), fRes->GetPz());
147  StartPosErr.SetXYZ(fRes->GetDX(), fRes->GetDY(), fRes->GetDZ());
148  StartMomErr.SetXYZ(fRes->GetDPx(), fRes->GetDPy(), fRes->GetDPz());
149  }
150  }
151 
152  TVector3 plane_v1, plane_v2;
153  if (fPerpPlane)
154  {
155  plane_v1 = StartMom.Orthogonal();
156  plane_v2 = StartPos.Cross(plane_v1);
157  }
158  else
159  {
160  plane_v1.SetXYZ(1.,0.,0.);
161  plane_v2.SetXYZ(0.,1.,0.);
162  }
163  GFDetPlane start_pl(StartPos, plane_v1, plane_v2);
164  GFTrack* trk;
165  if (fTrackRep==0)
166  {
167  GeaneTrackRep *grep = new GeaneTrackRep(fPro,
168  start_pl,StartMom,
169  StartPosErr,StartMomErr,
170  fCharge,PDGCode);
171  grep->setPropDir(1);
172  rep = grep;
173  }
174  else if (fTrackRep==1)
175  {
176  RKTrackRep *grep = new RKTrackRep(StartPos, StartMom,
177  StartPosErr, StartMomErr,
178  PDGCode);
179  rep = grep;
180  }
181  else
182  {
183  std::cout << "*** PndRecoKalmanFit::Exec" << "\t" << "Not existing Track Representation " << fTrackRep << std::endl;
184  return NULL; // any smarted ideas?
185  }
186 
187  trk= new GFTrack(rep);
188 
189  PndTrackCand trackCand = tBefore->GetTrackCand();
190  trk->setCandidate(*PndTrackCand2GenfitTrackCand(&trackCand));
191 
192  // Load RecoHits
193  try
194  {
196  if (fVerbose>0) std::cout<<trk->getNumHits()<<" hits in track " << std::endl;
197  }
198  catch(GFException& e)
199  {
200  std::cout << "*** PndRecoKalmanFit::Exec" << "\t" << "Genfit Exception: trk->addHitVector " << e.what() << std::endl;
201  //throw e;
202  }
203  // Start Fitter
204  try
205  {
206  // NB: want fast smoothing turned on;
207  if (store_track_parameterization) trk->setSmoothing(true, true);
208 
210  }
211  catch (GFException e)
212  {
213  std::cout<<"*** FITTER EXCEPTION ***"<<std::endl;
214  std::cout<<e.what()<<std::endl;
215  }
216  if (fVerbose>0) std::cout<<"SUCCESSFULL FIT!"<<std::endl;
217 
218  try
219  {
220  tAfter = (PndTrack*)GenfitTrack2PndTrack(trk);
221  }
222  catch (GFException e)
223  {
224  std::cout<<"*** PndGenfitAdapters EXCEPTION ***"<<std::endl;
225  std::cout<<e.what()<<std::endl;
226  tAfter = tBefore;
227  tAfter->SetFlag(-2); // flag -2: conversion failed
228  }
229 
230  if (fVerbose>0) std::cout<<"Fitting done"<<std::endl;
231  if (store_track_parameterization) {
232  PndTrackCand *cand = tAfter->GetTrackCandPtr();
233  std::vector<PndTrackCandHit> &ptchits = cand->_GetSortedHits();
234 
235  //printf("%2d hits total\n", trk->getNumHits());
236  for(unsigned ih=0; ih<trk->getNumHits(); ih++) {
237  bool ret;
238 
239  TVector3 rcpos = GFTools::getBiasedSmoothedPosXYZ(trk, 0, ih, &ret);
240  if (ret) {
241  TVector3 rcmom = GFTools::getBiasedSmoothedMomXYZ(trk, 0, ih, &ret);
242 
243  if (ret) {
244  NaiveTrackParameterization param(true);
245  param.SetRecoPosition(rcpos); param.SetRecoMomentum(rcmom);
246 
247  // Pull out the original {pos,mom} values at this location;
248  {
249  PndTrackCandHit *ptchit = &ptchits[ih];
250 
251  TClonesArray *dghits = FairRootManager::Instance()->GetDigiLookup(ptchit->GetDetId());
252  FairHit *dghit = (FairHit*) dghits->At(ptchit->GetHitId()); assert(dghit);
253 
254  TClonesArray *mchits = FairRootManager::Instance()->GetMoCaLookup(ptchit->GetDetId());
255  EicMoCaPoint *mchit = (EicMoCaPoint*) mchits->At(dghit->GetRefIndex()); assert(mchit);
256  TVector3 mcpos = mchit->GetPosAvg(), mcmom = mchit->GetMomAvg();
257 
258  param.SetMoCaPosition(mcpos); param.SetMoCaMomentum(mcmom);
259 
260  // Perform some massaging, per default; have no idea why momentum
261  // direction gets flipped at a fraction of nodes; have no desire to
262  // debug this s*it;
263  if (mcmom.Dot(rcmom) < 0.0) param.SetRecoMomentum(-rcmom);
264 
265  // Eventually push back;
266  tAfter->mParameterizations.push_back(param);
267  }
268  } //if
269  } //if
270  } //for ih
271  } //if
272 
273  return tAfter;
274 }
275