FairRoot/PandaRoot
PndFtsCATracking.cxx
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //-----------------------------------------------------------
3 
4 // Panda Headers ----------------------
5 
6 #include "PndFtsCATracking.h"
7 
8 #include <iostream>
9 #include <cmath>
10 #include <random>
11 
12 #include "TClonesArray.h"
13 #include "TParticlePDG.h"
14 #include "PndMCTrack.h"
15 
16 #include "FairRootManager.h"
17 //#include "FairGeanePro.h"
18 #include "FairRunAna.h"
19 #include "FairRuntimeDb.h"
20 #include "FairField.h"
21 #include "PndTrackCand.h"
22 #include "PndTrack.h"
23 
24 // fts
25 #include "PndFtsPoint.h"
26 #include "PndFtsHit.h"
27 #include "PndFtsTube.h"
28 #include "PndFtsMapCreator.h"
29 #include "PndGeoFtsPar.h"
30 
31 
32 #include "TGeoManager.h"
34 
35 
36 
37 #include <algorithm>
38 #include <vector>
39 using namespace std;
40 
41 // -----------
42 
43 #include <fstream>
44 #include <iostream>
45 #include <iomanip>
46 #include <cstdio>
47 #include <cstring>
48 #include <sstream>
49 #include <map>
50 using namespace std;
51 
52 #include "PndFTSCATrackParam.h"
53 #include "PndFTSCAGBTracker.h"
54 #include "PndFTSCAPerformance.h"
55 #include "TFile.h"
56 
57 
59 
60 vector <int> allFwdTrackIds;
61 
62 
63 bool compareFtsPoints (PndFtsPoint* const a, PndFtsPoint* const b) { return (a->GetTime()<b->GetTime()); }
64 
66  PndPersistencyTask(name, iVerbose), fMCTracks(0), fTracks(0), fDoPerformance(0), fTracker(0), fPerfHistoFile(0) //, fTracksArrayName("FTSCATracks")
67 {
69 
71 
72  //string fP = "settings.data";
73  string fts_geometry_str =
74  "48\
75  -10\
76  0 294.895 0.00044 0.0117 0 1 6\
77  1 295.77 0.00044 0.0117 0 1 6\
78  2 299.39 0.00044 0.0117 0.0871557 1 6\
79  3 300.265 0.00044 0.0117 0.0871557 1 6\
80  4 304.39 0.00044 0.0117 -0.0871557 1 6\
81  5 305.265 0.00044 0.0117 -0.0871557 1 6\
82  6 309.39 0.00044 0.0117 0 1 6\
83  7 310.265 0.00044 0.0117 0 1 6\
84  8 326.895 0.00044 0.0117 0 1 6\
85  9 327.77 0.00044 0.0117 0 1 6\
86  10 331.39 0.00044 0.0117 0.0871557 1 6\
87  11 332.265 0.00044 0.0117 0.0871557 1 6\
88  12 336.39 0.00044 0.0117 -0.0871557 1 6\
89  13 337.265 0.00044 0.0117 -0.0871557 1 6\
90  14 341.39 0.00044 0.0117 0 1 6\
91  15 342.265 0.00044 0.0117 0 1 6\
92  16 393.995 0.00044 0.0117 0 1 6\
93  17 394.87 0.00044 0.0117 0 1 6\
94  18 400.965 0.00044 0.0117 0.0871557 1 6\
95  19 401.84 0.00044 0.0117 0.0871557 1 6\
96  20 415.49 0.00044 0.0117 -0.0871557 1 6\
97  21 416.365 0.00044 0.0117 -0.0871557 1 6\
98  22 422.965 0.00044 0.0117 0 1 6\
99  23 423.84 0.00044 0.0117 0 1 6\
100  24 437.49 0.00044 0.0117 0 1 6\
101  25 438.365 0.00044 0.0117 0 1 6\
102  26 444.965 0.00044 0.0117 0.0871557 1 6\
103  27 445.84 0.00044 0.0117 0.0871557 1 6\
104  28 459.49 0.00044 0.0117 -0.0871557 1 6\
105  29 460.365 0.00044 0.0117 -0.0871557 1 6\
106  30 466.965 0.00044 0.0117 0 1 6\
107  31 467.84 0.00044 0.0117 0 1 6\
108  32 606.995 0.00044 0.0117 0 1 6\
109  33 607.87 0.00044 0.0117 0 1 6\
110  34 611.49 0.00044 0.0117 0.0871557 1 6\
111  35 612.365 0.00044 0.0117 0.0871557 1 6\
112  36 616.49 0.00044 0.0117 -0.0871557 1 6\
113  37 617.365 0.00044 0.0117 -0.0871557 1 6\
114  38 621.49 0.00044 0.0117 0 1 6\
115  39 622.365 0.00044 0.0117 0 1 6\
116  40 638.995 0.00044 0.0117 0 1 6\
117  41 639.87 0.00044 0.0117 0 1 6\
118  42 643.49 0.00044 0.0117 0.0871557 1 6\
119  43 644.365 0.00044 0.0117 0.0871557 1 6\
120  44 648.49 0.00044 0.0117 -0.0871557 1 6\
121  45 649.365 0.00044 0.0117 -0.0871557 1 6\
122  46 653.49 0.00044 0.0117 0 1 6\
123  47 654.365 0.00044 0.0117 0 1 6";
124 
125  std::istringstream settings(fts_geometry_str);
126  fTracker->ReadSettings(settings);
127 
128  SetPersistency(kTRUE);
129 
130  //cout<<"READGEOM \n";
131  //fTracker->ReadSettingsFromFile(fP);
132 
133 #ifdef DO_TPCCATRACKER_EFF_PERFORMANCE
134  fDoPerformance = 1;
135 
136  TFile* curFile = gFile;
137  TDirectory* curDirectory = gDirectory;
138 
139  //static TFile* performanceHistoFile = 0; //[R.K. 9/2018] unused
140  string filePrefix = "./CATrackerData";
141  filePrefix += "/";
142 
143  if( fDoPerformance ){
144  if( !fPerfHistoFile ){
145  fPerfHistoFile = new TFile( (filePrefix + "CATrackerPerformance.root").data(), "RECREATE" );
146  if( !fPerfHistoFile->IsOpen() ){
147  gSystem->Exec( "mkdir ./CATrackerData");
148  fPerfHistoFile = new TFile( (filePrefix + "CATrackerPerformance.root").data(), "RECREATE" );
149  }
150  }
151 
152  fPerformance = &PndFTSCAPerformance::Instance();
153  fPerformance->SetOutputFile(fPerfHistoFile);
154  fPerformance->CreateHistos();
155  }
156 
157 
158  gFile = curFile;
159  gDirectory = curDirectory;
160 #endif
161 }
162 
164 {
165  if(fTracker) delete fTracker;
166 }
167 
169 {
170  //Initialize magnetic field
172 
173  // ---
174  if(fVerbose>3) Info("Init","Start initialisation.");
175 
176  FairRootManager *fManager = FairRootManager::Instance();
177 
178  // Get MC arrays
179  fMCTracks = dynamic_cast<TClonesArray *>(fManager->GetObject("MCTrack"));
180  if ( ! fMCTracks ) {
181  std::cout << "-W- PndFtsTrackerIdeal::Init: No MCTrack array! Needed for MC Truth" << std::endl;
182  return kERROR;
183  }
184  //FTS
185  fMCPoints = dynamic_cast<TClonesArray *> (fManager->GetObject("FTSPoint"));
186  if ( !fMCPoints ) {
187  std::cout << "-W- PndFtsTrackerIdeal::Init: No FTSPoint array!" << std::endl;
188  return kERROR;
189  }
190  fHits = dynamic_cast<TClonesArray *> (fManager->GetObject("FTSHit"));
191  if ( ! fHits ) {
192  std::cout << "-W- PndFtsTrackerIdeal::Init: No FTSHit array!" << std::endl;
193  return kERROR;
194  }
195  //FTS tube geometry
196  FairRuntimeDb* rtdb = FairRunAna::Instance()->GetRuntimeDb();
197  PndGeoFtsPar *ftsParameters = (PndGeoFtsPar*) rtdb->getContainer("PndGeoFtsPar");
198 
199  if(ftsParameters->GetGeometryType() != -1) {
200  PndFtsMapCreator *mapper = new PndFtsMapCreator(ftsParameters);
201  fTubeArrayFts = mapper->FillTubeArray();
202  }
203 
204  fBranchID = FairRootManager::Instance()->GetBranchId("FTSHit");
205 
206  //output track array
207  fTracks = new TClonesArray("PndTrack");
208  fManager->Register("FtsTrack","Fts",fTracks, kTRUE);
209 
210  return kSUCCESS;
211 }
212 
214  FairRuntimeDb* rtdb = FairRunAna::Instance()->GetRuntimeDb();
215  rtdb->getContainer("PndGeoSttPar");
216  rtdb->getContainer("PndGeoFtsPar");
217 
218 }
219 
220 vector <int> ftslabels;
221 vector <PndFTSCALocalMCPoint> ftsmcpoints;
222 vector <PndFTSCAMCTrack> ftsmctracks;
223 
225 {
226  int NHits = fTracker->GetHitsSize();
227  //non-reconstructable due to very few hits
229  return true;
230  //explanation for this upper-limit see in perStationCounts-cycle
231  /*if (fHits.Size()>240)
232  return true;*/
233 
234  vector <int> perStationCounts(6);
235  for (int i=0; i<NHits; i++)
236  {
237  //we've got 6 blocks (each block has 8 sub-stations)
238  for (int j=0; j<6; j++)
239  {
240  if ( ( (( (fTracker->Hit(i)).IRow()+1)/8)<(1+j) ) && ( ( ( (fTracker->Hit(i)).IRow()+1)/8)>(-1+j) ) )
241  {
242  perStationCounts[j]++;
243  break;
244  }
245  }
246  }
247 
248  for (unsigned int j=0; j<perStationCounts.size(); j++)
249  {
250  //cout<<"perStationCounts[j] "<<perStationCounts[j]<<endl;
251 
252  //NHits/Station-limit; 40 assumes 40*6=240 hits/event limit
253  if (perStationCounts[j]>40)
254 // if (perStationCounts[j]>20) // l_r
255  return true;
256  //don't allow sequences of empty station-blocks due
257  //to non-reconstructable circumstances of such events
258  if (j<3)
259  {
260  //first and last block have hits but 2 between them are empty
261  /*if (((perStationCounts[j+1]+perStationCounts[j+2])<4) && ( (perStationCounts[j]>0) && (perStationCounts[j+3]>0)))
262  return true;
263  //3 empty blocks sequences ("0+1+2", "1+2+3" or "2+3+4") and 1 following block with few hits
264  if (((perStationCounts[j]+perStationCounts[j+1]+perStationCounts[j+2])<4)
265  && (perStationCounts[j+3]<4))
266  return true;*/
267  //4 empty blocks sequences("0+1+2+3","1+2+3+4" or "2+3+4+5")
268  if ((perStationCounts[j]+perStationCounts[j+1]+perStationCounts[j+2]+perStationCounts[j+3])<4)
269  return true;
270  }
271  //5 empty block sequences("0+1+2+3+4" or "1+2+3+4+5")
272  if (j<2)
273  {
274  if ((perStationCounts[j]+perStationCounts[j+1]+perStationCounts[j+2]+perStationCounts[j+3]+perStationCounts[j+4])<4)
275  return true;
276  }
277  }
278 
279  //case for cylindrically curved tracks (when mc-track has more than 1 hit/station; or if z_hit_AnywereBefore_last_hit > z_last_hit)
280  PndFTSCAPerformance* perf = &PndFTSCAPerformance::Instance();
281  const int NMCTracks = perf->GetMCTracks()->Size();
282  /*int NMCTracks_reconstructable=0;
283  for(int iT=0; iT<NMCTracks; iT++)
284  {
285  int nMCPoints = (*perf->GetMCTracks())[iT].NMCPoints();
286  if (nMCPoints>PndFTSCAParameters::MinimumHitsForRecoTrack)
287  NMCTracks_reconstructable++;
288  }*/
289  for(int iT=0; iT<NMCTracks; iT++)
290  {
291  int nFirstMC = (*perf->GetMCTracks())[iT].FirstMCPointID();
292  PndFTSCALocalMCPoint *points = &((*perf->GetMCPoints()).Data()[nFirstMC]);
293  int nMCPoints = (*perf->GetMCTracks())[iT].NMCPoints();
294  /*dbg
295  for (int iP1=0; iP1<nMCPoints-1; iP1++)
296  {
297  cout<<"points[iP1].Z() "<<points[iP1].Z()<<endl;
298  }*/
299  if (nMCPoints<PndFTSCAParameters::MinimumHitsForRecoTrack) continue;
300  for (int iP1=0; iP1<nMCPoints-1; iP1++)
301  {
302  for (int iP2=iP1+1; iP2<nMCPoints; iP2++)
303  {
304  /*
305  //skip events which have only a single cylindrical track
306  if ((points[iP1].Z()>points[iP2].Z()) && (NMCTracks_reconstructable==1))
307  return true;
308  //if NMCTracks>1 - mark cylindrical tracks as non-reconstructable and process event further
309  */
310  if (points[iP1].Z()>points[iP2].Z())
311  (*perf->GetMCTracks())[iT].SetIsForwardTrack(false);
312  break;
313  }
314  }
315  }
316  return false;
317 }
318 
319 void PndFtsCATracking::CATrackParToFairTrackParP( FairTrackParP *fairParam, const PndFTSCATrackParam* kfParam )
320 {
321  const double cA = TMath::Cos( kfParam->Angle() );
322  const double sA = -TMath::Sin( kfParam->Angle() );
323 
324  Double_t x = kfParam->X();
325  Double_t y = kfParam->Y();
326  //Double_t z = kfParam->Z();
327 
328  Double_t tx = kfParam->Tx();
329  Double_t ty = kfParam->Ty();
330  Double_t qp = kfParam->QP();
331 
332  Double_t q = kfParam->QP()>0 ? 1 : -1;
333 
334  Double_t cov[15]; //, cov1[15];
335 
336  for(int i=0; i<15; i++)
337  cov[i] = kfParam->Cov(i);
338 
339  /*
340  double mB = 1/TMath::Sqrt(1 + kfParam->DzDs()*kfParam->DzDs());
341  double mA = -kfParam->QPt() * kfParam->DzDs() *mB*mB*mB;
342  double mC = 1/kfParam->GetCosPhi();
343  double mE = mC*mC*mC;
344  double mD = kfParam->DzDs() * kfParam->SinPhi() * mC*mC*mC;
345 
346  cov1[14] = cov[2];
347  cov1[13] = cov[1];
348  cov1[12] = cov[0];
349  cov1[11] = mE *cov[7] + cov[4]* mD;
350  cov1[10] = mE *cov[6] + cov[3]* mD;
351  cov1[ 9] = mE*mE* cov[9] + 2* mE *cov[8]* mD + cov[5] *mD*mD;
352  cov1[ 8] = mC *cov[4];
353  cov1[ 7] = mC *cov[3];
354  cov1[ 6] = mC *(mE* cov[8] + cov[5]* mD);
355  cov1[ 5] = mC*mC* cov[5];
356  cov1[ 4] = mB *cov[11] + mA *cov[7];
357  cov1[ 3] = mB *cov[10] + mA *cov[6];
358  cov1[ 2] = mB *mE *cov[13] + mA* mE* cov[9] + mB* cov[12] *mD + mA* cov[8]* mD;
359  cov1[ 1] = mC *(mB *cov[12] + mA* cov[8]);
360  cov1[ 0] = 2*mA *mB *cov[13] + mB*mB* cov[14] + mA*mA* cov[9];
361  */
362 
363  //TODO not clear whether we should invert the covariance mtx
364  fairParam->SetTrackPar(x, y, tx, ty, qp, cov, TVector3(0,0,0), TVector3(-sA,cA,0), TVector3(cA,sA,0), TVector3(0,0,-1), q);
365 }
366 
367 
368 void PndFtsCATracking::Exec(Option_t* /*opt*/) //[R.K. 9/2018] unused
369 {
370  if (fVerbose>0) std::cout<<"PndFtsCATracking::Exec"<<std::endl;
371 
372  fTracks->Delete();
373 
374  fTracker->StartEvent();
375 
376  static int iEvent = -1;
377  iEvent++;
378 
379  cout << "iEvent " << iEvent << endl;
380 
381  PndFtsHit* ghit = NULL;
382  PndFtsPoint* myPoint=NULL;
383  std::map<Int_t, PndMCTrack*> mctracklist;
384  Int_t nFtsHits=0;
385  Int_t nPoints = 0;
386  //const Int_t nMCTracks = fMCTracks->GetEntriesFast();
387  //Int_t nMCTracks=0;
388  unsigned int nMCPoints=0;
389  //Int_t prevTrackID=-1;
390 
391  //vector <Int_t> TrackIds;
392  map<int, unsigned int> nHitsInMCTrack, nMCPointsInMCTrack, FirstMCPointIDInMCTrack;
393  //PndCATrackFtsMCPointContainer* MCTrackSortedArray = new PndCATrackFtsMCPointContainer[fMCTracks->GetEntriesFast()];
394  vector < vector <PndFtsPoint*> > MCTrackSortedArray(fMCTracks->GetEntriesFast());
395 
396  for (Int_t ih = 0; ih < fHits->GetEntriesFast(); ih++)
397  {
398  ghit = (PndFtsHit*) fHits->At(ih);
399  if(!ghit) continue;
400  nFtsHits++;
401  Int_t mchitid=ghit->GetRefIndex();
402  if(mchitid<0) continue;
403  myPoint = (PndFtsPoint*)(fMCPoints->At(mchitid));
404  if(!myPoint) continue;
405  Int_t trackID = myPoint->GetTrackID();
406  if(trackID<0) continue;
407  nPoints++;
408  //PndMCTrack* mctr = mctracklist[trackID];
409  //if(NULL==mctr)
410  //{
411  // mctr=new PndMCTrack();
413  //}
414  //mctr->AddHit(fBranchID,ih,myPoint->GetTime());
415  //MCTrackSortedArray[myPoint->GetTrackID()].FtsArray.push_back(myPoint);
416  MCTrackSortedArray[myPoint->GetTrackID()].push_back(myPoint);
417  //prevTrackID=myPoint->GetTrackID();
418  //TrackIds.push_back(prevTrackID);
419  //mctracklist[trackID] = mctr;
420  }
421 
422  //31.01 ftsmctracks.resize(fMCTracks->GetEntriesFast());
423  //31.01 ftsmcpoints.resize(fMCPoints->GetEntriesFast());
424 
427 
428  //cout<<"NMCTRACKS: "<<fMCTracks->GetEntriesFast()<<endl;
429  //cout<<"NMCPOINTS: "<<fMCPoints->GetEntriesFast()<<endl;
430  int xNMCTracks=0;
431  for ( int iTr = 0; iTr < fMCTracks->GetEntriesFast(); iTr++ )
432  {
433  //std::sort(MCTrackSortedArray[iTr].FtsArray.begin(), MCTrackSortedArray[iTr].FtsArray.end(), compareFtsPoints);
434  std::sort(MCTrackSortedArray[iTr].begin(), MCTrackSortedArray[iTr].end(), compareFtsPoints);
435  //int curTrID=-1;
436  //bool checker=true;
437  //cout<<"NFTSPOINTS: "<<MCTrackSortedArray[iTr].size()<<endl;
438  for(unsigned int iPFts=0; iPFts < MCTrackSortedArray[iTr].size(); iPFts++)
439  {
440  PndFtsPoint* point = MCTrackSortedArray[iTr][iPFts]; //MCTrackSortedArray[iTr].FtsArray[iPFts];
441  int trackID = point->GetTrackID();
442  Double_t q = 1;
443  { // get charge
444  if ( trackID < fMCTracks->GetEntriesFast() ) {
445  const PndMCTrack* mcTr = (PndMCTrack*) fMCTracks->At(trackID);
446 
447  if ( mcTr ) {
448  TParticlePDG * part = TDatabasePDG::Instance()->GetParticle(mcTr->GetPdgCode());
449  if ( part )
450  {
451  q = part->Charge()/3.f;
452  //cout<<"q/p "<<q/sqrt( point->GetPx()*point->GetPx() + point->GetPy()*point->GetPy() + point->GetPz()*point->GetPz() )<<" tx "<<point->GetPx()/point->GetPz()<<" ty "<<point->GetPy()/point->GetPz()<<" x "<<point->GetX()<<" y "<<point->GetY()<<" z "<<point->GetZ()<<endl;
453  }
454  }
455  }
456  }
457  /*int iSta = point->GetLayerID();
458  Double_t px = point->GetPx();
459  Double_t py = point->GetPy();
460  Double_t pz = point->GetPz();
461  Double_t p = sqrt( px*px + py*py + pz*pz );*/
462 
463  if ( fDoPerformance ){
465  xxx.SetPoint(point, q);
466  ftsmcpoints.push_back(xxx);
467  /*outMCP << point->GetX() << " " << point->GetY() << " " << point->GetZ() << endl;
468  outMCP << px << " " << py << " " << pz << " "
469  << q/p << endl;
470  outMCP << 0 << " " << iSta << " " << trackID << " " << trackID << endl;*/
471  }
472 
473  if ( nMCPointsInMCTrack.find(trackID) != nMCPointsInMCTrack.end() ) {
474  nMCPointsInMCTrack[trackID]++;
475  } else {
476  nMCPointsInMCTrack[trackID] = 1;
477  FirstMCPointIDInMCTrack[trackID] = nMCPoints;
478  }
479  //cout<<"nMCPointsInMCTrack[trackID] "<<nMCPointsInMCTrack[trackID]<<endl;
480  //cout<<"trackID "<<trackID<<endl;
481  nMCPoints++;
482  }
483  const PndMCTrack* mcTr = (PndMCTrack*) fMCTracks->At(iTr);
484 
485  if( fDoPerformance ){
486  if ( !mcTr ) {
487  PndFTSCAMCTrack yyy;
488  PndMCTrack* mcTrempty = new PndMCTrack();
489  yyy.SetMCTrack(mcTrempty, 0,0,0);
490  ftsmctracks.push_back(yyy);
491  }
492  else {
493  Int_t pdg = mcTr->GetPdgCode();
494  Double_t px = mcTr->GetMomentum().X();
495  Double_t py = mcTr->GetMomentum().Y();
496  Double_t pz = mcTr->GetMomentum().Z();
497  Double_t p = sqrt( px*px + py*py + pz*pz );
498  if(TMath::Abs(p)<1.e-6){
499  px = 1.e-6;
500  py = 1.e-6;
501  pz = 1.e-6;
502  p = 1.e-6;
503  }
504  Double_t q = 1;
505  { // get charge
506  TParticlePDG * part = TDatabasePDG::Instance()->GetParticle(pdg);
507  if ( part )
508  q = part->Charge()/3.f;
509  }
510  //Double_t ex,ey,ez,qp;
511  //cout<<"pdg px py pz "<<pdg<<" "<<px<<" "<<py<<" "<<pz<<" ";
512  PndFTSCAMCTrack yyy;
513  unsigned int nmcpimct = nMCPointsInMCTrack[iTr];
514  unsigned int fmcpidimct = FirstMCPointIDInMCTrack[iTr];
515  yyy.SetMCTrack(mcTr, q, nmcpimct, fmcpidimct);
516  /* we do it in the function
517  //check for curved tracks
518  for (int iP1=0; iP1<nmcpimct-1; iP1++)
519  {
520  for (int iP2=iP1+1; iP2<nmcpimct; iP2++)
521  {
522  if (points[iP1].Z()>points[iP2].Z())
523  {
524  yyy.SetIsForwardTrack(false);
525  break;
526  }
527  }
528  }
529  */
530 
531  if (nmcpimct>6)
532 // if (nmcpimct>22) // dbg
533  xNMCTracks++;
534  ftsmctracks.push_back(yyy);
535  /*outMCT << mcTr->GetMotherID() << " " << pdg << endl;
536  outMCT << mcTr->GetStartVertex().X() << " " << mcTr->GetStartVertex().Y() << " " << mcTr->GetStartVertex().Z() << " "
537  << px/fabs(p) << " " << py/fabs(p) << " " << pz/fabs(p) << " " << q/p << endl;
538  outMCT << 0 << " " << 0 << " " << 0 << " " << 0 << " " << 0 << " " << 0 << " " << 0 << endl;
539  outMCT << p << " " << sqrt( px*px + py*py ) << endl;
540  outMCT << nMCPointsInMCTrack[iTr] << " " << nMCPointsInMCTrack[iTr] << " " << FirstMCPointIDInMCTrack[iTr] << endl; //nHitsInMCTrack[iTr]
541  outMCT << 0 << " " << 0 << " " << 1 << endl;*/
542  }
543  }
544  }
545 
546 
547 // if (xNMCTracks!=1)
548 // return;
549 
550 // if (xNMCTracks==1)
551 // return;
552 
553 
554  //cout<<"ftsmcpoints.size() "<<ftsmcpoints.size()<<endl;
555  //cout<<"ftsmctracks.size() "<<ftsmctracks.size()<<endl;
556  std::vector<PndFTSCAGBHit> vHits;
557 
558  int iHit = 0;
559 
560  //Save FTS hits
561  WriteFTSHits( vHits, /*outH, outHL, outMCT, outMCP,*/ iHit, nHitsInMCTrack);
562 
563 
564  //if(MCTrackSortedArray) delete[] MCTrackSortedArray;
565 
566 /* if( fDoPerformance ){
567  outH.close();
568  outHL.close();
569  outMCT.close();
570  outMCP.close();
571  }
572  */
574 
575  const PndFTSCAGBTracker *fTrackerConst = fTracker;
576 
577  //SETHITS AS IN MVD+STT
578  do{
579  int kEvents = iEvent;
580  char buf[6];
581  sprintf( buf, "%d", kEvents );
583  // std::cout << "CA fTracker: Loading Event " << kEvents << "..." << std::endl;
584  fTracker->SetHits( vHits );
585 
586  cout << "NHits " << fTracker->fHits.Size() << endl;
587 
588 // if (fTracker->fHits.Size() > 200) return;
589 
590 /* dbg-cout
591 if (fTracker->fHits.Size() >=8 )
592 {
593  for(unsigned int iH=0; iH<8; iH++)
594  {
595  int iStation = fTracker->fHits[iH].IRow();
596  float X_Hit = fTracker->fHits[iH].X();
597  float Z_Hit = fTracker->fHits[iH].Z();
598  float R_Hit = fTracker->fHits[iH].R();
599  float RSigned = fTracker->fHits[iH].IsLeft();
600 
601 
602 cout << " iStation " << iStation << " X_Hit " << X_Hit << " Z_Hit " << Z_Hit << endl;
603 cout << " R_Hit " << R_Hit << " RSigned " << RSigned << endl;
604 
605 }
606 }*/
607 
608 
609  //std::cout << "Event " << kEvents << " CPU reconstruction..." << std::endl;
610 #ifdef DO_TPCCATRACKER_EFF_PERFORMANCE
611  // cout<<"Filename "<<fileName<<endl;
612  if ( fDoPerformance && fPerformance ) {
613  fPerformance->SetTracker(fTracker);
614  std::cout << "Loading Monte-Carlo Data for Event " << kEvents << "..." << std::endl;
615  if (!fPerformance->ReadData(ftslabels,ftsmcpoints, ftsmctracks)) {
616  cout << "Monte-Carlo Data for Event " << kEvents << " can't be read." << std::endl;
617  break;
618  }
619  //cout<<"here!!!\n";
620  fPerformance->CombineHits();
621 // fPerformance->DivideHitsOnLR();
622  }
623 #endif
624 
625 
626  if ( NonReconstructableEvent() )
627  {
628  ftslabels.clear();
629  ftsmcpoints.clear();
630  ftsmctracks.clear();
631  return;
632  }
633 
634 
635  cout<<"Run CA trackfinder... "<<endl;
636 
637  fTracker->FindTracks();
638 
639  //rewrite reco tracks into global PndTrack-format
640  {
641  int nOutTracks=0;
642  for( int itr=0; itr<fTracker->NTracks(); itr++)
643  {
644  const PndFTSCAGBTrack &tr = fTracker->Track( itr );
645  //cout<<"Output track:"<<endl;
646  PndTrackCand outCand;
647  for( int ih=0; ih<tr.NHits(); ih++ )
648  {
649  int hitIndex = fTracker->TrackHit( tr.FirstHitRef() + ih );
650  const PndFTSCAGBHit &hit = fTracker->Hit( hitIndex );
651  outCand.AddHit( hit.PndDetID(), hit.PndHitID(), hit.IRow() );
652  }
653  outCand.setMcTrackId(-1);
654 
655  FairTrackParP paramFirst;
656  FairTrackParP paramLast;
657 
658  CATrackParToFairTrackParP( &paramFirst, &tr.InnerParam() );
659  CATrackParToFairTrackParP( &paramLast, &tr.OuterParam() );
660 
661  PndTrack *outTrack = new((*fTracks)[nOutTracks]) PndTrack(paramFirst,paramLast,outCand);
662  outTrack->SetRefIndex(nOutTracks);
663  outTrack->SetFlag(0);
664  nOutTracks++;
665  }
666  }
667 
668 #ifdef DO_TPCCATRACKER_EFF_PERFORMANCE
669  if ( fDoPerformance && fPerformance ) {
670  //SG!!! fTracker->SaveTracksInFile(fileName);
671  if (fTrackerConst->NHits() > 0) {
672  fPerformance->InitSubPerformances();
673  fPerformance->ExecPerformance();
674  }
675  else {
676  cout << "Event " << kEvents << " contains 0 hits." << std::endl;
677  }
678  }
679 #endif
680 
681  if (fVerbose>0){
682 
683  const bool ifAvarageTime = 1;
684  if (!ifAvarageTime){
685  std::cout << "Reconstruction Time"
686  << " Real = " << std::setw( 10 ) << (fTrackerConst->SliceTrackerTime() + fTrackerConst->StatTime( 9 )) * 1.e3 << " ms,"
687  << " CPU = " << std::setw( 10 ) << (fTrackerConst->SliceTrackerCpuTime() + fTrackerConst->StatTime( 10 )) * 1.e3 << " ms"
688  << std::endl;
689  }
690  else{
691  const int NTimers = fTrackerConst->NTimers();
692  static int statIEvent = 0;
693  static double *statTime = new double[NTimers];
694  static double statTime_SliceTrackerTime = 0;
695  static double statTime_SliceTrackerCpuTime = 0;
696 
697  if (!statIEvent){
698  for (int i = 0; i < NTimers; i++){
699  statTime[i] = 0;
700  }
701  }
702 
703  statIEvent++;
704  for (int i = 0; i < NTimers; i++){
705  statTime[i] += fTrackerConst->StatTime( i );
706  }
707  statTime_SliceTrackerTime += fTrackerConst->SliceTrackerTime();
708  statTime_SliceTrackerCpuTime += fTrackerConst->SliceTrackerCpuTime();
709 
710  std::cout << "Reconstruction Time"
711  << " Real = " << std::setw( 10 ) << 1./statIEvent*(statTime_SliceTrackerTime+statTime[ 9 ]) * 1.e3 << " ms,"
712  << " CPU = " << std::setw( 10 ) << 1./statIEvent*(statTime_SliceTrackerCpuTime+statTime[ 10 ]) * 1.e3 << " ms,"
713  << std::endl;
714  }
715  } // fVerbose>0
716 
717  } while(0);
718 
719  ftslabels.clear();
720  ftsmcpoints.clear();
721  ftsmctracks.clear();
722 }
723 
725 {
726 #ifdef DO_TPCCATRACKER_EFF_PERFORMANCE
727  if ( fDoPerformance && fPerformance ) {
728  fPerformance->WriteHistos();
729  }
730  //fPerformanceHistoFile->Close();
731 #endif
732 }
733 
734 void PndFtsCATracking::WriteFTSHits( std::vector<PndFTSCAGBHit> &vHits,
735  /*std::fstream &outH, std::fstream &outHL, std::fstream &outMCT, std::fstream &outMCP,*/ int &iHit, map<int, unsigned int> &nHitsInMCTrack)
736 {
737  TClonesArray *hitsArray;
738  hitsArray = fHits;
739  Int_t ftsLinkType = FairRootManager::Instance()->GetBranchId("FTSPoint");
740  //31.01 ftslabels.resize(hitsArray->GetEntriesFast());
741 
742  //outHL << hitsArray->GetEntriesFast() << endl;
743  //outH << hitsArray->GetEntriesFast() << endl;
744 
745  std::map<Int_t,Int_t> tubeMap;
746  std::map<Int_t,Int_t>::iterator mapIt;
747 
748  for(int iH=0; iH<hitsArray->GetEntriesFast(); iH++)
749  {
750  PndFtsHit* currenthit = (PndFtsHit*) hitsArray->At(iH);
751 // cout << " currenthit->GetTimeStamp() " << currenthit->GetTimeStamp() << endl;
752  Int_t tubeID = currenthit->GetTubeID();
753  mapIt = tubeMap.find('b');
754  if( mapIt == tubeMap.end() ){
755  tubeMap.insert( std::pair<Int_t,Int_t>(tubeID,1) );
756  } else {
757  mapIt->second++;
758  if( mapIt->second >3 ) continue; //SG!!!
759  }
760 
761  PndFtsTube *tube = (PndFtsTube *) fTubeArrayFts->At(tubeID);
762  TVector3 wire_direction = tube->GetWireDirection();
763 
764  const Double_t kSS = 1;//.5; // coefficient between size and sigma
765  const Double_t dXY = tube->GetRadIn();
766  const Double_t errXY = dXY/kSS;
767  const Double_t errXY2 = errXY*errXY;
768  const Double_t errZ = tube->GetHalfLength()/kSS;
769 
770  TMatrixT<Double_t> RM = tube->GetRotationMatrix();
771 // cout<<"mtx \n";
772 // for (int row = 0; row<3; row++)
773 // {
774 // for (int col = 0; col<3; col++)
775 // {
776 // cout<<RM[row][col]<<" ";
777 // }
778 // cout<<endl;
779 // }
780 // cout<<endl;
781  TMatrixT<Double_t> C(3,3); // CovMatrix
782  C[0][0] = errXY2;
783  C[1][1] = errXY2;
784  C[2][2] = errZ*errZ;
785  C[0][1] = C[0][2] = C[1][0] = C[1][2] = C[2][0] = C[2][1] = 0;
786 
787  TMatrixT<Double_t> CR = C; // rotated CovMatrix
788 
789  CR = RM*C;
790  CR = CR*RM.Transpose(RM);
791 
792 
793  Double_t A = 0;
794 
795  TVector3 position = tube->GetPosition();
796  //Double_t xx = position.X();
797  //Double_t yy = position.Y();
798  //Double_t zz = position.Z();
799  //cout<<"tube coords "<<xx<<" "<<yy<<" "<<zz<<endl;
800 
801  Double_t x = currenthit->GetX();
802  Double_t y = currenthit->GetY();
803  Double_t z = currenthit->GetZ();
804  //cout<<"hit coords "<<x<<" "<<y<<" "<<z<<endl;
805  //cout<<"NEXT HIT \n";
806 // cout<<"direct coords "<<x<<" "<<y<<" "<<z<<endl;
807 // cout<<"mtx \n";
808 // for (int row = 0; row<3; row++)
809 // {
810 // for (int col = 0; col<3; col++)
811 // {
812 // cout<<RM[row][col]<<" ";
813 // }
814 // cout<<endl;
815 // }
817  int iSta = -1;
818 
819  // get station angle A and station index iSta
820 
821 // A=1234;
822  iSta = currenthit->GetLayerID() - 1;
823 
824 
825  Double_t radius= currenthit->GetIsochrone(); //0;
826  //cout<<"radius "<<radius<<endl;
827  // error calculation according to the curve chosen by flag
828  Double_t closestDistanceError = currenthit->GetIsochroneError();//0;
829 
830  //int pointID = -1; //[R.K. 9/2018] unused
831 
832  int trackID;
833  PndFtsPoint* point=NULL;
834 
835  FairMultiLinkedData links = currenthit->GetLinksWithType(ftsLinkType);
836  if( links.GetNLinks() >0 )
837  {
838  int iPoint = links.GetLink(0).GetIndex();
839  point = (PndFtsPoint*) fMCPoints->At(iPoint);
840  if( !point )
841  {
842  // cout<<"CA tracker: wrong index of Fts point: "<<iPoint<<" of "<<fFtsPointsArray->GetEntriesFast()<<endl;
843  return;
844  }
845  else
846  {
847  trackID = point->GetTrackID();
848  }
849  }
850 
852 
853  const PndMCTrack* mcTr = (PndMCTrack*) fMCTracks->At(trackID);
854  TParticlePDG * part = TDatabasePDG::Instance()->GetParticle(mcTr->GetPdgCode());
855  float q = part->Charge()/3.f;
856  float qp = q/sqrt( point->GetPx()*point->GetPx() + point->GetPy()*point->GetPy() + point->GetPz()*point->GetPz() );
857  //mc-links:begin
858  h.point_X = (float) point->GetX();
859  h.point_Y = (float) point->GetY();
860  h.point_Z = (float) point->GetZ();
861  h.point_Px = (float) point->GetPx();
862  h.point_Py = (float) point->GetPy();
863  h.point_Pz = (float) point->GetPz();
864  h.point_Qp = qp;
865  h.Track_ID = trackID;
866  //mc-links:end
867 
868  h.SetX( x );
869  h.SetY( y );
870  h.SetZ( z );
871  h.SetErr2X(currenthit->GetDx()*currenthit->GetDx());
872  h.SetErr2Y(currenthit->GetDy()*currenthit->GetDy());
873 
874  h.SetXW(x);
875  h.SetYW(y);
876  h.SetZW(z);
877 
878  h.SetEX(tube->GetWireDirection().X());
879  h.SetEY(tube->GetWireDirection().Y());
880  h.SetEZ(tube->GetWireDirection().Z());
881  //cout<<"ex "<<tube->GetWireDirection().X()<<" ey "<<tube->GetWireDirection().Y()<<" ez "<<tube->GetWireDirection().Z()<<endl;
882  h.SetR( radius );
883  h.SetC( CR );
884  h.SetErr2R( closestDistanceError*closestDistanceError );
885  h.SetIRow(iSta);
886 
887  h.SetID( iHit );
888 
889  //h.SetPndHitID( iH );
890  h.SetAngle( -A );
891  h.SetTubeR( tube->GetRadIn() );
892  h.SetTubeHalfLength( tube->GetHalfLength() );
893 
894  h.SetPndDetID( FairRootManager::Instance()->GetBranchId("FTSHit") );
895  h.SetPndHitID( iH );
896 
897  vHits.push_back(h);
898 
899  int trackIDs[3] = {-1, -1, -1};
900  trackIDs[0] = trackID;
901 
902  if( fDoPerformance ){
903  //outH << h;
904  //outHL << trackIDs[0] << " " << trackIDs[1] << " " << trackIDs[2] << endl;
905  ftslabels.push_back(trackIDs[0]);
906  //cout<<"fPerformance trackIDs[0] "<<trackIDs[0]<<endl;
907  }
908 
909  iHit++;
910 
911  if ( nHitsInMCTrack.find(trackIDs[0]) != nHitsInMCTrack.end() ) {
912  nHitsInMCTrack[trackIDs[0]]++;
913  } else {
914  nHitsInMCTrack[trackIDs[0]] = 1;
915  }
916  }
917 }
void SetC(const TMatrixT< Double_t > c)
Double_t CR[11]
TClonesArray * fTracks
Array of event's hits.
void SetRefIndex(TString branch, Int_t i)
Definition: PndTrack.h:41
void SetErr2X(float v)
Definition: PndFTSCAGBHit.h:91
const PndFTSCAGBTrack & Track(int i) const
int NTimers() const
static void CATrackParToFairTrackParP(FairTrackParP *fairParam, const PndFTSCATrackParam *caParam)
int TrackHit(int i) const
Int_t i
Definition: run_full.C:25
TTree * b
PndFTSCAGBTracker * fTracker
void InitMagneticField()
double StatTime(int iTimer) const
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:29
Int_t GetGeometryType()
Definition: PndGeoFtsPar.h:37
Int_t GetPdgCode() const
Definition: PndMCTrack.h:73
void SetPndHitID(int v)
void setMcTrackId(int i)
Definition: PndTrackCand.h:72
static T Sin(const T &x)
Definition: PndCAMath.h:42
bool compareFtsPoints(PndFtsPoint *const a, PndFtsPoint *const b)
int PndDetID() const
void ReadSettings(std::istream &in)
int IRow() const
Definition: PndFTSCAGBHit.h:56
TVector3 GetMomentum() const
Definition: PndMCTrack.h:78
vector< PndFTSCAMCTrack > ftsmctracks
void SetPersistency(Bool_t val=kTRUE)
void SetPndDetID(int v)
double SliceTrackerCpuTime() const
int FirstHitRef() const
void SetPoint(PndFtsPoint *ppp, Double_t qq)
cout<< "POINTs for new FwEndCap == "<< tsim-> GetEntriesFast()
TVector3 GetWireDirection() const
Definition: PndFtsTube.cxx:83
static T Cos(const T &x)
Definition: PndCAMath.h:43
Int_t GetTubeID() const
Definition: PndFtsHit.h:70
Double_t p
Definition: anasim.C:58
Int_t GetLayerID() const
Definition: PndFtsHit.h:74
void SetErr2Y(float v)
Definition: PndFTSCAGBHit.h:92
int GetHitsSize() const
double SliceTrackerTime() const
TClonesArray * fTubeArrayFts
PndFTSResizableArray< PndFTSCAGBHit > fHits
int Pic_FED Eff_lEE C()
static T Abs(const T &x)
Definition: PndCAMath.h:39
const PndFTSCATrackParam & OuterParam() const
void SetY(float v)
Definition: PndFTSCAGBHit.h:89
bool fDoPerformance
Array of found tracks.
vector< int > ftslabels
int NHits() const
virtual void Finish()
PndFtsCATracking(const char *name="FtsCATracking", Int_t iVerbose=0)
const float * Cov() const
Int_t a
Definition: anaLmdDigi.C:126
void AddHit(UInt_t detId, UInt_t hitId, Double_t rho)
void SetIRow(int v)
Definition: PndFTSCAGBHit.h:95
ClassImp(PndFtsCATracking)
Double_t
void SetHits(std::vector< PndFTSCAGBHit > &hits)
PndFTSCAParam & GetParametersNonConst()
int PndHitID() const
TClonesArray * point
Definition: anaLmdDigi.C:29
void SetMCTrack(const PndMCTrack *ttt, Double_t q, unsigned int Nmcpoints, unsigned int FirstmcpointId)
int Size() const
Definition: PndFTSArray.h:374
FairRuntimeDb * rtdb
Definition: hit_dirc.C:66
TMatrixT< Double_t > GetRotationMatrix() const
Definition: PndFtsTube.cxx:71
Double_t z
TVector3 GetPosition() const
Definition: PndFtsTube.cxx:68
TString name
int NTracks() const
const PndFTSCATrackParam & InnerParam() const
Double_t GetHalfLength() const
Definition: PndFtsTube.cxx:80
PndFTSCAPerformance * fPerformance
TClonesArray * fHits
Array of event's points.
Double_t GetIsochrone() const
Definition: PndFtsHit.h:57
int hit(Int_t nEvents=0, TString inFile="sim.root", TString parFile="par.root", TString inDigi="digi.root", TString outFile="hit.root", Int_t timeBased=0)
Definition: hit.C:1
void SetX(float v)
Definition: PndFTSCAGBHit.h:88
Double_t GetRadIn() const
Definition: PndFtsTube.cxx:74
Double_t x
Double_t GetIsochroneError() const
Definition: PndFtsHit.h:58
double Z
Definition: anaLmdDigi.C:68
TClonesArray * FillTubeArray()
this function will be used in PndFtsHitProducesRealFast
virtual void Exec(Option_t *opt)
Int_t iVerbose
void SetZ(float v)
Definition: PndFTSCAGBHit.h:90
return buf
void SetID(int v)
Definition: PndFTSCAGBHit.h:96
vector< PndFTSCALocalMCPoint > ftsmcpoints
Double_t y
void SetAngle(float v)
const PndFTSCAGBHit & Hit(int index) const
virtual InitStatus Init()
vector< int > allFwdTrackIds
void WriteFTSHits(std::vector< PndFTSCAGBHit > &vHits, int &iHit, map< int, unsigned int > &nHitsInMCTrack)
TClonesArray * fMCPoints
Array of PndMCTrack.
double pz[39]
Definition: pipisigmas.h:14
TClonesArray * fMCTracks