FairRoot/PandaRoot
PndHypKalmanTask.cxx
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 // File and Version Information:
3 // $Id$
4 //
5 // Description:
6 // Implementation of class PndMvdKalmanTask
7 // see PndMvdKalmanTask.hh for details
8 //
9 // Environment:
10 // Software developed for the PANDA Detector at FAIR.
11 //
12 // Author List:
13 // Sebastian Neubert TUM (original author)
14 // Ralf Kliemt, TU Dresden (Copied for MVD use)
15 //
16 //-----------------------------------------------------------
17 
18 // Panda Headers ----------------------
19 
20 // This Class' Header ------------------
21 #include "PndHypKalmanTask.h"
22 
23 // C/C++ Headers ----------------------
24 #include <algorithm>
25 #include <iostream>
26 #include <assert.h>
27 
28 // Collaborating Class Headers --------
29 #include "FairRootManager.h"
30 #include "TClonesArray.h"
31 #include "FairRunAna.h"
32 #include "GFTrack.h"
33 #include "TDatabasePDG.h"
34 #include "PndHypHit.h"
35 #include "FairMCPoint.h"
36 #include "../../pnddata/SdsData/PndSdsMCPoint.h"
37 #include "../../pnddata/SdsData/PndSdsHit.h"
38 #include "../../GenfitTools/recohits/PndSdsRecoHit.h"
39 //#include "PndHypRecoHit.h"
40 #include "PndHypRecoSPHit.h"
41 #include "PndGeoHandling.h"
42 
43 #include "FairField.h"
44 #include "PndFieldAdaptor.h"
45 #include "GFFieldManager.h"
46 
47 #include "GFRecoHitFactory.h"
48 #include "GFKalman.h"
49 #include "GFException.h"
50 #include "TH1D.h"
51 #include "TFile.h"
52 #include "TGeoTrack.h"
53 #include "TGeoManager.h"
54 #include "TLorentzVector.h"
55 #include "GFDetPlane.h"
56 
57 #include "TDatabasePDG.h"
58 #include "FairTrackParH.h"
59 
60 #include "LSLTrackRep.h"
61 #include "GeaneTrackRep.h"
62 #include "FairGeanePro.h"
63 
64 // Class Member definitions -----------
65 
66 
68  : FairTask("Kalman Filter"), fPersistence(kFALSE)
69 {
70  fTrackBranchName = "HypTrackCand";
71  //PndGeoHandling::Instance();
72 }
73 
74 
76 {
77  if(fPH!=NULL)delete fPH;
78  if(fChi2H!=NULL)delete fChi2H;
79 }
80 
81 InitStatus
83 {
84  //Get ROOT Manager
85  FairRootManager* ioman= FairRootManager::Instance();
86 
87  if(ioman==0)
88  {
89  Error("PndHypKalmanTask::Init","RootManager not instantiated!");
90  return kERROR;
91  }
92 
93  // Get input collection
94  fTrackArray=(TClonesArray*) ioman->GetObject(fTrackBranchName);
95 
96  if(fTrackArray==0)
97  {
98  Error("PndHypKalmanTask::Init","track-array not found!");
99  return kERROR;
100  }
101 
102 
103  // Build hit factory -----------------------------
105 
106  std::map<unsigned int,TString>::iterator iter=fHitBranchMap.begin();
107 
108  while(iter!=fHitBranchMap.end()){
109 
110 
111  TClonesArray* har=(TClonesArray*) ioman->GetObject("HypHit");
112 
113  //cout<<" "<<iter->second<<endl;
114 
115  if(har==0){
116 
117  Error("PndHypKalmanTask::Init","Hit array not found");
118  }
119  else{
120 
121  if(iter->first==2)fTheRecoHitFactory->addProducer(iter->first,new GFRecoHitProducer<PndHypHit,PndHypRecoSPHit>(har));
122 
123  }
124 
125  TClonesArray* sar=(TClonesArray*) ioman->GetObject("MVDHit");
126  if(sar==0){
127 
128  Error("PndHypKalmanTask::Init","Hit array not found");
129  }
130  else{
131 
132  if(iter->first==3)fTheRecoHitFactory->addProducer(iter->first,new GFRecoHitProducer<PndSdsHit,PndSdsRecoHit>(sar));
133  }
134 
135  ++iter;
136  }//end loops over hit types
137 
138 // create and register output array
139  fTrArray = new TClonesArray("GFTrack");
140  ioman->Register("Track","GenFit",fTrArray,fPersistence);
141 
142  //fPro = new FairGeanePro();
143 
144  // setup histograms
145  fPH=new TH1D("pH","p",100,0.4,0.6);
146  fChi2H=new TH1D("chi2H","chi2",100,0,20);
147 
148 
149  return kSUCCESS;
150 }
151 
152 
153 void
155 {
156  std::cout<<"PndHypKalmanTask::Exec"<<std::endl;
157  // Reset output Array
158  if(fTrArray==0) Fatal("Kalman::Exec)","No TrackArray");
159  fTrArray->Delete();
160 
161  Int_t ntracks=fTrackArray->GetEntriesFast();
162  // Detailed output
163 
164 
165 
166  if(ntracks>20){
167  std::cout<<"ntracks="<<ntracks<<" Evil Event! skipping"<<std::endl;
168  return;
169  }
170 
171  // Fitting ---------------- can go to another task!
172  GFKalman fitter;
173 
174  // std::vector<TLorentzVector*> particles;
175  //std::vector<Int_t> signs;
176 
177  for(Int_t itr=0;itr<ntracks;++itr){
178 
179  std::cout<<"starting track"<<itr<<std::endl;
180  GFTrackCand* trcnd = (GFTrackCand*)fTrackArray->At(itr);
181  unsigned int detid=12345, index=12345;
182 
183  // setting the magnetic field properly
184  fField= FairRunAna::Instance()->GetField();
186  GFFieldManager * fb;
187  //fb->getInstance();
188  //fb->init(b);
189 
190  GFAbsTrackRep* rep = 0;
191 
192  std::cout<<trcnd->getNHits()<<std::endl;
193  // Starting values for guessing
194 
195  Int_t PDGCode= trcnd->getPdgCode();//2212;
196  TVector3 StartPos = trcnd->getPosSeed();//TVector3 (1.0,0.0,0.0);//cmn
197  TVector3 StartPosErr = TVector3(0,0,0);
198  TVector3 StartMom = trcnd->getDirSeed();//TVector3 (1.,0.,1.);
199  //StartMom.SetMagThetaPhi(1.05 , 70.*TMath::Pi()/360. , 0.);
200  // StartMom.SetMag(1.1);
201  TVector3 StartMomErr = TVector3(0,0,0);
202  TDatabasePDG *fdbPDG= TDatabasePDG::Instance();
203  TParticlePDG *fParticle= fdbPDG->GetParticle(PDGCode);
204  Double_t fCharge= fParticle->Charge();
205  // calc momentum projections
206  TVector3 dir=StartMom.Unit();
207  double dxdz=dir.X()/dir.Z();
208  double dydz=dir.Y()/dir.Z();
209 
210  rep=new LSLTrackRep(StartPos.Z(),StartPos.X(),StartPos.Y(),dxdz,dydz,trcnd->getQoverPseed(),
211  StartPosErr.X(),StartPosErr.Y(),0.1,0.1,0.1,b);//NULL instead of b field
212 
213  // what to guess here?
214  /* TVector3 U(1.,0.,0.);
215  TVector3 V(0.,1.,0.);
216 
217  GFDetPlane start_pl(StartPos,U,V);
218  GFAbsTrackRep* rep = new GeaneTrackRep(fPro,
219  start_pl,StartMom,
220  StartPosErr,StartMomErr,
221  fCharge,PDGCode);*/
222 
223  //GFTrack* trk= new GFTrack(rep);
224  GFTrack* trk=new((*fTrArray)[fTrArray->GetEntriesFast()]) GFTrack(rep);
225 
226  if(trk!=NULL)trk->setCandidate(*(GFTrackCand*)fTrackArray->At(itr));
227 
228  else {
229  std::cout<<" "<< "caca de vaca "<<std::endl;
230  continue;}
231 
232  //std::cout<<" "<< trk->getTrackRep(0)->Print()<<std::endl;
233  //cout<<" "<<trk->Print()<<endl;
234 
235  //GFTrack* trk=(GFTrack*)fTrackArray->At(itr);
236  // Load RecoHits
237  try {
238  trk->addHitVector(fTheRecoHitFactory->createMany(*trcnd));
239 
240  }
241  catch(GFException& e) {
242  std::cout << e.what();
243  throw e;
244  }
245  // Start Fitter
246  try{
247  std::cout<<"starting fit"<<std::endl;
248  fitter.processTrack(trk);
249  }
250  catch (GFException e){
251  std::cout<<e.what()<<std::endl;
252  }
253 
254  // Print Track Parameters after fit
255  if(trk->getTrackRep(0)->getStatusFlag()==0){
256  //trk->getTrackRep(0)->Print();
257  GFDetPlane plane(TVector3(0,0,0.1),TVector3(1,0,0),TVector3(0,1,0));
258  TVector3 p3=trk->getTrackRep(0)->getMom(plane);
259  Double_t p=trk->getMom().Mag();
260  std::cout<<" oye"<<p<<std::endl;
261 
262  fPH->Fill(p);
263 
264 
265 
266  Double_t chi2=trk->getChiSqu();
267  fChi2H->Fill(chi2);
268 
269  }
270 
271  }
272 
273  std::cout<<"Fitting done"<<std::endl;
274 
275 
276 
277  return;
278 }
279 
280 void
282  TFile* file = new TFile(filename,"UPDATE");
283 // if(file->cd("Kalman")==false) file->mkdir("Kalman");
284 // file->cd("Kalman");
285 
286  if(file->cd("Kalman")) file->Delete("Kalman;*");
287  file->mkdir("Kalman");
288  file->cd("Kalman");
289 
290 
291  fPH->Write();
292  delete fPH;
293  fPH=NULL;
294 
295  fChi2H->Write();
296  delete fChi2H;
297  fChi2H=NULL;
298 
299 
300 
301  file->Close();
302  delete file;
303 }
304 
305 
Base Class for genfit track representations. Defines interface for track parameterizations.
Definition: GFAbsTrackRep.h:80
TClonesArray * fTrArray
Double_t p
Definition: anasim.C:58
void WriteHistograms(const TString &filename)
TTree * b
Detector plane genfit geometry class.
Definition: GFDetPlane.h:59
TFile * file
Track object for genfit. genfit algorithms work on these objects.
Definition: GFTrack.h:60
std::vector< GFAbsRecoHit * > createMany(const GFTrackCand &cand)
Creat a collection of RecoHits.
void addProducer(int detID, GFAbsRecoHitProducer *hitProd)
Register a producer module to the factory.
Generic Kalman Filter implementation.
Definition: GFKalman.h:50
virtual const char * what() const
standard error message handling for exceptions. use like &quot;std::cerr &lt;&lt; e.what();&quot; ...
Definition: GFException.cxx:47
FairField * fField
unsigned int getNHits() const
Definition: GFTrackCand.h:113
Template class for a hit producer module.
void setCandidate(const GFTrackCand &cand, bool doreset=false)
set track candidate
Definition: GFTrack.cxx:148
TVector3 getPosSeed() const
get the seed value for track: pos
Definition: GFTrackCand.h:131
virtual void Exec(Option_t *opt)
Int_t * fParticle
Definition: run_full.C:24
TVector3 getDirSeed() const
get the seed value for track: direction
Definition: GFTrackCand.h:133
Factory object to create RecoHits from digitized and clustered data.
Double_t
double getQoverPseed() const
get the seed value for track: qoverp
Definition: GFTrackCand.h:135
Track candidate – a list of cluster indices.
Definition: GFTrackCand.h:55
void processTrack(GFTrack *trk)
Performs fit on a GFTrack.
Definition: GFKalman.cxx:40
GFTrack * trk
Definition: checkgenfit.C:13
int getPdgCode() const
get the PDG code
Definition: GFTrackCand.h:141
virtual InitStatus Init()
Exception class for error handling in GENFIT (provides storage for diagnostic information) ...
Definition: GFException.h:45
ClassImp(PndAnaContFact)
TFile * fb
TClonesArray * fTrackArray
GFRecoHitFactory * fTheRecoHitFactory
std::map< unsigned int, TString > fHitBranchMap
Singleton which provides access to magnetic field for track representations.
const string filename