FairRoot/PandaRoot
PndGemTrackFinderIdeal.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- PndGemTrackFinderIdeal source file -----
3 // ----- Created 19.03.2009 by R. Karabowicz -----
4 // ----- according to the PndDchTrackFinderIdeal -----
5 // -------------------------------------------------------------------------
6 
8 
9 // Fair includes
10 #include "FairRootManager.h"
11 #include "FairRunAna.h"
12 #include "FairRuntimeDb.h"
13 #include "FairBaseParSet.h"
14 #include "FairTrackParam.h"
15 #include "FairRootManager.h"
16 
17 // Pnd includes
18 #include "PndGemMCPoint.h"
19 #include "PndDetectorList.h"
20 #include "PndTrack.h"
21 #include "PndTrackCand.h"
22 #include "PndTrackCandHit.h"
23 
24 // ROOT includes
25 #include "TClonesArray.h"
26 #include "TGeoManager.h"
27 #include "TMatrixFSym.h"
28 
29 // C++ includes
30 #include <iostream>
31 #include <map>
32 #include <cmath>
33 using std::cout;
34 using std::endl;
35 using std::map;
36 
37 // ----- Default constructor -------------------------------------------
39  fMCTrackArray = NULL;
40  fMCPointArray = NULL;
41  fNofEvents = 0;
42  fVerbose = 0;
43  fPrimary = 0;
44 }
45 
46 // ----- Destructor ----------------------------------------------------
48 
49 // ----- Init -----------------------------------------------------------
51 
52  // Get and check FairRootManager
53  FairRootManager* ioman = FairRootManager::Instance();
54  if( !ioman ) {
55  cout << "-E- "<< GetName() <<"::Init: "
56  << "RootManager not instantised!" << endl;
57  return;
58  }
59 
60  // Get the pointer to the singleton FairRunAna object
61  FairRunAna* ana = FairRunAna::Instance();
62  if(NULL == ana) {
63  cout << "-E- "<< GetName() <<"::Init :"
64  <<" no FairRunAna object!" << endl;
65  return;
66  }
67  // Get the pointer to run-time data base
68  FairRuntimeDb* rtdb = ana->GetRuntimeDb();
69  if(NULL == rtdb) {
70  cout << "-E- "<< GetName() <<"::Init :"
71  <<" no runtime database!" << endl;
72  return;
73  }
74 
75  // Get MCTrack array
76  fMCTrackArray = (TClonesArray*) ioman->GetObject("MCTrack");
77  if( !fMCTrackArray ) {
78  cout << "-E- "<< GetName() <<"::Init: No MCTrack array!"
79  << endl;
80  return;
81  }
82 
83  // Get PndGemPoint (MCPoint) array
84  fMCPointArray = (TClonesArray*) ioman->GetObject("GEMPoint");
85  if( !fMCPointArray ) {
86  cout << "-E- "<< GetName() <<"::Init: No MCPoint array!"
87  << endl;
88  return;
89  }
90  if ( fVerbose )
91  cout <<"fMCPointArray #: "<< fMCPointArray->GetEntriesFast() << endl;
92 
93  // Geometry loading
94  //TFile *infile = ioman->GetInFile();
95  //TGeoManager *geoMan = (TGeoManager*) infile->Get("FAIRGeom");
96  //fGemStructure = PndGemStructure::Instance(geoMan);
97 
98  std::cout << "-I- "<< GetName() <<": Intialization successfull" << std::endl;
99 }
100 
101 // ----- Public method DoFind ------------------------------------------
102 Int_t PndGemTrackFinderIdeal::DoFind(TClonesArray* hitArray,
103  TClonesArray* trackArray,
104  TClonesArray* trackCandArray) {
105 
106  // Count events
107  fNofEvents++;
108  if ( fVerbose ) {
109  cout << endl << endl<< endl << endl;
110  cout << "=======================================================" << endl;
111  cout << "-I- Event No: " << fNofEvents << endl;
112  cout << "=======================================================" << endl;
113 
114 
115  cout <<"-I- "<< GetName() <<"::DoFind "<< endl;
116  cout << "-------------------------------------------------------" << endl;
117  cout << " ### Start DoFind" << endl;
118  cout << "-------------------------------------------------------" << endl;
119  }
120 
121  // Check pointers
122  if( !fMCTrackArray ) {
123  cout << "-E- PndGemTrackFinderIdeal::DoFind: "
124  << "MCTrack array missing! " << endl;
125  return -1;
126  }
127 
128  if( !fMCPointArray ) {
129  cout << "-E- "<< GetName() <<"::DoFind: "
130  << "MCPoint array missing! " << endl;
131  return -1;
132  }
133 
134  if( !hitArray ) {
135  cout << "-E- "<< GetName() <<"::DoFind: "
136  << "Hit arrays missing! "<< endl;
137  return -1;
138  }
139 
140  // Initialise control counters
141  Int_t nNoMCTrack = 0;
142  Int_t nNoTrack = 0;
143  Int_t nNoGemPoint = 0;
144  Int_t nNoGemHit = 0;
145 
146  // Create pointers to GemHit and GemPoint
147  PndGemHit* gemHit = NULL;
148  FairMCPoint* mcPoint = NULL;
149  PndMCTrack* mcTrack = NULL;
150  PndTrack* gemTrack = NULL;
151  PndTrackCand* gemTrackCand = NULL;
152  PndTrackCandHit tcHit;
153 
154  // Declare variables outside the loop
155  Int_t ptIndex = 0; // MC point index
156  Int_t mcTrackIndex = 0; // MC track index
157  Int_t trackIndex = 0; // Gem track index
158  Int_t relDetID = -1;//3000; //
159 
160  // Create STL map from MCTrack index to number of hits
161  std::map<Int_t, Int_t> hitMap;
162  std::map<Int_t, Double_t> trackPMap;
163  std::map<Int_t, Int_t>::iterator itHitMap;
164 
165  if ( fVerbose ) {
166  // Size of fMCTrackArray
167  cout <<"# MC Tracks: "<< fMCTrackArray->GetEntriesFast() << endl;
168  // Size of fMCTrackArray
169  cout <<"# MC Points: "<< fMCPointArray->GetEntriesFast() << endl;
170  }
171 
172  // Number of Gem hits
173  Int_t nGemHits = hitArray->GetEntriesFast();
174  if(fVerbose > 2) cout <<"# GemHits: "<< nGemHits << endl;
175 
176  for(Int_t iHit = 0; iHit < nGemHits; iHit++){
177  // Get the pointer to Gem hit
178  gemHit = (PndGemHit*) hitArray->At(iHit);
179  if(gemHit->GetDetectorID() > relDetID) {
180  if(NULL == gemHit ) continue;
181 
182  // Get point index
183  ptIndex = gemHit->GetRefIndex();
184 
185  if ( ptIndex == -1 ) continue;
186 
187  // Get pointer to MC point
188  mcPoint = (FairMCPoint*) fMCPointArray->At(ptIndex);
189  if(NULL == mcPoint) continue;
190 
191  // Get MC track index
192  mcTrackIndex = mcPoint->GetTrackID();
193 
194  trackPMap[mcTrackIndex] = TMath::Sqrt(mcPoint->GetPx()*mcPoint->GetPx()+
195  mcPoint->GetPy()*mcPoint->GetPy()+
196  mcPoint->GetPz()*mcPoint->GetPz());
197 
198  // Increment the number of hits
199  hitMap[mcTrackIndex] += 1;
200  }
201  }
202 
203 
204  // Create STL map from MCTrack index to GemTrack index
205  map<Int_t, Int_t> trackMap;
206 
207  // Loop over reconstructable MCTracks and create corresponding GemTrack
208  Int_t nMCacc = 0; // Accepted MC tracks
209  Int_t nTracks = 0; // Reconstructable MC tracks
210  Int_t nMCTracks = fMCTrackArray->GetEntriesFast();
211 
212  if(fVerbose > 2) {
213  cout <<"# of MC Tracks (nMCTracks): " << nMCTracks << endl;
214  }
215 
216  for(Int_t iMCTrack = 0; iMCTrack < nMCTracks; iMCTrack++) {
217  mcTrack = (PndMCTrack*) fMCTrackArray->At(iMCTrack);
218  if( !mcTrack ) continue;
219 
220 
221  // Either Primary or All MC tracks are considered
222  if(fPrimary) {
223  if(mcTrack->GetMotherID() != -1 && fabs((mcTrack->GetStartVertex()).Z())>1.) continue;
224 
225  if(fVerbose > 2) {
226  cout <<"MC track #: "<< iMCTrack
227  << "\t MotherID: "<< mcTrack->GetMotherID()
228  << "\t StartVertex Z: "<< mcTrack->GetStartVertex().Z() << endl;
229  }
230  }
231  //----------------------------------------------------
232  nMCacc++;
233 
234  if(fVerbose > 2) {
235  cout << iMCTrack << ": #GemPoints in MCTrack: "<< mcTrack->GetNPoints(kGEM) << " -> " << hitMap[iMCTrack] << endl;
236  }
237  if ( !hitMap[iMCTrack] ) continue;
238 
239  gemTrackCand = new((*trackCandArray)[nTracks]) PndTrackCand();
240 
241  // Loop over hits. Get corresponding MCPoint and MCTrack index
242  for(Int_t iHit = 0; iHit < nGemHits; iHit++) {
243  gemHit = (PndGemHit*) hitArray->At(iHit);
244  if( !gemHit ) {
245  nNoGemHit++;
246  continue;
247  }
248 
249  ptIndex = gemHit->GetRefIndex();
250  if(ptIndex < 0) continue; // fake or background hit
251  mcPoint = (FairMCPoint*) fMCPointArray->At(ptIndex);
252 
253  if( !mcPoint ) {
254  nNoGemPoint++;
255  continue;
256  }
257 
258  mcTrackIndex = mcPoint->GetTrackID();
259 
260  if(mcTrackIndex < 0 || mcTrackIndex > nMCTracks) {
261  cout << "-E- "<< GetName() <<"::DoFind: "
262  << "MCTrack index out of range. " << mcTrackIndex << " "
263  << nMCTracks << endl;
264  nNoMCTrack++;
265  continue;
266  }
267  if ( mcTrackIndex != iMCTrack ) continue;
268 
269  gemTrackCand->AddHit(FairRootManager::Instance()->GetBranchId("GEMHit"),
270  iHit,gemHit->GetPosition().Mag());
271 
272  if(fVerbose > 3) {
273  cout << "GEM hit " << iHit << " from GEM point "
274  << ptIndex << " (" << mcTrackIndex << ") "
275  << "added to GEM track " << trackIndex << endl;
276  }
277  }
278 
279  gemTrackCand->Sort();
280 
281  // cout << "gemTrack " << nTracks << " has " << gemTrackCand->GetNHits() << " hits" << endl;
282  PndTrackCandHit tch = gemTrackCand->GetSortedHit(0);
283 
284  gemHit = (PndGemHit*) hitArray->At(tch.GetHitId());
285 
286  TVector3 pos;
287  TVector3 mom;
288 
289  ptIndex = gemHit->GetRefIndex();
290  mcPoint = (FairMCPoint*) fMCPointArray->At(ptIndex);
291 
292  gemHit->Position(pos);
293  mcPoint->Momentum(mom);
294 
295  mcTrack = (PndMCTrack*) fMCTrackArray->At(mcPoint->GetTrackID());
296 
297  int pdg = mcTrack->GetPdgCode();
298  double charge = 0.;
299  if(pdg<100000000) charge = TDatabasePDG::Instance()->GetParticle(pdg)->Charge()/3.;
300 
301 // gemTrackCand->setMcTrackId(iMCTrack);
302 
303  FairTrackParP firstPar(pos,mom,
304  TVector3(0.5, 0.5, 0.5),
305  0.1*mom,
306  (Int_t)charge,
307  pos,
308  TVector3(1.,0.,0.),
309  TVector3(0.,1.,0.));
310  FairTrackParP lastPar (pos,mom,
311  TVector3(0.5, 0.5, 0.5),
312  0.1*mom,
313  (Int_t)charge,
314  pos,
315  TVector3(1.,0.,0.),
316  TVector3(0.,1.,0.));
317 
318  gemTrack = new((*trackArray)[nTracks]) PndTrack(firstPar, lastPar, *gemTrackCand);
319 
320  gemTrack->SetRefIndex(iMCTrack);
321 
322  trackMap[iMCTrack] = nTracks;
323 
324  nTracks++;
325  }
326 
327  if(fVerbose) {
328  cout << endl;
329  cout << "-------------------------------------------------------" << endl;
330  cout << "-I- "<< GetName() <<": Event summary -I-" << endl;
331  cout << "-------------------------------------------------------" << endl;
332  cout << "Total Gem hits: " << nGemHits << endl;
333  cout << "MC tracks total: " << nMCTracks << ", accepted: "
334  << nMCacc << ", reconstructable: " << nTracks << endl;
335  if(nNoGemHit) cout << "GemHits not found : " << nNoGemHit << endl;
336  if(nNoGemPoint) cout << "GemPoints not found : " << nNoGemPoint << endl;
337  if(nNoMCTrack) cout << "MCTracks not found : " << nNoMCTrack << endl;
338  if(nNoTrack) cout << "GemTracks not found : " << nNoTrack << endl;
339  cout << "------------------------------------------------------" << endl;
340  cout << endl;
341  } else {
342 // cout << "All: " << nMCTracks
343 // << ", Accepted: " << nMCacc
344 // << ", Reconstructed: " << nTracks << endl;
345  }
346 
347  return nTracks;
348 }
349 
350 
TVector3 pos
void SetRefIndex(TString branch, Int_t i)
Definition: PndTrack.h:41
TClonesArray * trackArray
Definition: NHitsPerEvent.C:14
virtual Int_t DoFind(TClonesArray *hitArray, TClonesArray *trackArray, TClonesArray *trackCandArray)
PndTransMap * map
Definition: sim_emc_apd.C:99
Int_t GetNPoints(DetectorId detId) const
Definition: PndMCTrack.cxx:120
static T Sqrt(const T &x)
Definition: PndCAMath.h:37
Int_t GetPdgCode() const
Definition: PndMCTrack.h:73
PndTrackCandHit GetSortedHit(UInt_t i)
Definition: PndTrackCand.h:54
Double_t mom
Definition: plot_dirc.C:14
Int_t fNofEvents
event counter
void AddHit(UInt_t detId, UInt_t hitId, Double_t rho)
Ideal track finding algorithm.
FairRuntimeDb * rtdb
Definition: hit_dirc.C:66
friend F32vec4 fabs(const F32vec4 &a)
Definition: P4_F32vec4.h:47
TVector3 GetPosition() const
Definition: PndGemHit.h:71
ClassImp(PndAnaContFact)
double Z
Definition: anaLmdDigi.C:68
TVector3 GetStartVertex() const
Definition: PndMCTrack.h:76
Int_t GetMotherID() const
Definition: PndMCTrack.h:74
Int_t GetHitId() const