StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StiCATpcSeedFinder.cxx
1 #include <assert.h>
2 #include <string.h>
3 #include "TVector3.h"
4 #include "Sti/StiToolkit.h"
5 #include "Sti/StiHit.h"
7 #include "StMessMgr.h"
8 #include "StEvent/StTpcHit.h"
9 #include "Sti/StiKalmanTrack.h"
10 #include "StEvent/StTpcHit.h"
11 
12 #include "StiCATpcTrackerInterface.h"
13 //#define PRINT_SEED_STATISTIC
14 //#define PRINT_FIT_ERR_STATISTIC
15 #ifdef PRINT_FIT_ERR_STATISTIC
16 #include <map>
17 #endif // PRINT_FIT_ERR_STATISTIC
18 //#define EXTRAPOLATION_CUT
19 //#define KINK_REJECTION
20 #define OVERLAP_REJECTION
21 //#define StiCATpcSeedFinderBLOG
22 //________________________________________________________________________________
23 Bool_t StiCATpcSeedFinder::SeedsCompareStatus(const Seed_t a, const Seed_t b)
24 {
25  return (a.total_hits < b.total_hits);
26 }
27 //________________________________________________________________________________
29 {
31  if (!mSeeds || mSeeds->size()==0) {
32  if (mEnded) return 0;
33  caTrackerInt.SetNewEvent();
34  // zero all banks before filling !!!
35  auto *map = &StiToolkit::instance()->getHitContainer()->hits();
36 
37 
38  // Run reconstruction by the CA Tracker
39  caTrackerInt.SetHits(*map);
40  caTrackerInt.Run();
41  mSeeds = &caTrackerInt.GetSeeds();
42  if (!mSeeds->size()) { mEnded = 2; return 0;}
43  sort(mSeeds->begin(), mSeeds->end(),SeedsCompareStatus );
44  }
45  int begEndFail=0;
46  while (mSeeds->size()) {
47 
48  Seed_t &aSeed = mSeeds->back();
49  vector<StiHit*> _seedHits;
50  int nHits = aSeed.vhit.size();
51 // Workaround for bug in CA. Sometimes:
52 // 1. hits badRxyed
53 // 2. same hit is used twice in one track
54 
55  auto myLambda = [](SeedHit_t *a, SeedHit_t *b)
56  {
57  const StiHit *ah = a->hit;
58  const StiHit *bh = b->hit;
59  if (ah==bh) return false; // don't do the math for identical hits
60 
61  // HK, VP, GVB:
62  // Test: a's x^2 + y^2 <>=? b's x^2 + y^2
63  // ...but re-arranging the math lets us avoid doing
64  // squares, and converting to double gives more precision
65  // (A^2 + B^2) - (C^2 + D^2) =
66  // (A^2 - C^2) + (B^2 - D^2) =
67  // (A-C)*(A+C) + (B-D)*(B+D)
68  double ax = ah->x_g(),ay = ah->y_g();
69  double bx = bh->x_g(),by = bh->y_g();
70  return (ax-bx)*(ax+bx) + (ay-by)*(ay+by)>0;
71  };
72 
73  StiHit *preHit = aSeed.vhit[ 0]->hit;
74  StiHit *endHit = aSeed.vhit[nHits-1]->hit;
75  int sortIt = 0;
76 #ifdef StiCATpcSeedFinderBLOG
77  int unsRxy=0,unsPad=0,reUsed=0;
78 #endif //StiCATpcSeedFinderBLOG
79 
80  for (int iHit=0;iHit<nHits-1;iHit++)
81  {
82 
83 #ifdef StiCATpcSeedFinderBLOG
84  StiHit *hit = aSeed.vhit[iHit+1]->hit;
85  StHit *sthit = (StHit*)hit->stHit();
86  double rXYsti = hit->rxy();
87  double rXYste = sthit->position().perp();
88  assert(fabs(rXYsti-rXYste)<1e-3);
89  int padrow0 = ((StTpcHit*)(aSeed.vhit[iHit ]->hit->stHit()))->padrow();
90  int padrow1 = ((StTpcHit*)(aSeed.vhit[iHit+1]->hit->stHit()))->padrow();
91 StiDebug::Count("XlocOfPad",padrow0, hit->x());
92 StiDebug::Count("RxyOfPad",padrow0, rXYste);
93 StiDebug::Count("AngLoc",atan2(hit->y(),hit->x())/3.1415*180);
94  if (padrow0<=padrow1) {
95  unsPad++;
96  double rxy = sqrt(pow(hit->x_g(),2)+pow(hit->y_g(),2));
97  double z = hit->z_g();
98 StiDebug::Count("UnsPadXY",aSeed.vhit[iHit+1]->hit->x_g(), aSeed.vhit[iHit+1]->hit->y_g());
99 StiDebug::Count("UnsPadZR",z, rxy);
100  }
101 #endif //StiCATpcSeedFinderBLOG
102 
103  if (myLambda(aSeed.vhit[iHit],aSeed.vhit[iHit+1])) continue;
104  if (!sortIt) sortIt = 100+iHit;
105 
106 #ifdef StiCATpcSeedFinderBLOG
107  unsRxy++;
108  double rxy = sqrt(pow(hit->x_g(),2)+pow(hit->y_g(),2));
109  double z = hit->z_g();
110 StiDebug::Count("UnsHitXY",aSeed.vhit[iHit+1]->hit->x_g(), aSeed.vhit[iHit+1]->hit->y_g());
111 StiDebug::Count("UnsHitZR",z, rxy);
112 static int printIt = 0;
113  if (!printIt) continue;
114 {
115  for (int iHit=0;iHit<nHits-1;iHit++) {
116  StiHit *hit = aSeed.vhit[iHit]->hit;if (!hit) continue;
117  double rxy = sqrt(pow(hit->x_g(),2)+pow(hit->y_g(),2));
118  double z = hit->z_g();
119  printf( "hit[%d] %p Rxy=%g\t Z=%g\n",iHit,hit,rxy,z);
120 } }
121  continue;
122 #endif //StiCATpcSeedFinderBLOG
123  break;
124  }
125 
126 
127  if (sortIt) {
128  std::sort(aSeed.vhit.begin(), aSeed.vhit.end(), myLambda);
129  if (preHit != aSeed.vhit[ 0]->hit) begEndFail+=1;
130  if (endHit != aSeed.vhit[nHits-1]->hit) begEndFail+=2;
131  }
132  preHit = 0;
133  for (int iHit=0;iHit<nHits;iHit++)
134  {
135  StiHit *hit = aSeed.vhit[iHit]->hit;if (!hit) continue;
136  if (hit->timesUsed() || hit == preHit) {
137 
138 #ifdef StiCATpcSeedFinderBLOG
139  reUsed++;
140  double rxy = sqrt(pow(hit->x_g(),2)+pow(hit->y_g(),2));
141 
142  if (hit == preHit) {StiDebug::Count("SameHitXY",hit->x_g(),hit->y_g());
143  StiDebug::Count("SameHitZR",hit->z_g(),rxy );}
144  else {StiDebug::Count("SkipHitXY",hit->x_g(),hit->y_g());
145  StiDebug::Count("SkipHitZR",hit->z_g(),rxy );}
146 #endif //StiCATpcSeedFinderBLOG
147 
148  if (!iHit || iHit == nHits-1) {
149  begEndFail++;
150 #ifdef StiCATpcSeedFinderBLOG
151  StiDebug::Count("BegEndXY" ,hit->x_g(),hit->y_g());
152  StiDebug::Count("BegEndZR" ,hit->z_g(),rxy );
153 #endif //StiCATpcSeedFinderBLOG
154  }
155  continue;
156  }
157  preHit = hit;
158 assert(!hit->timesUsed());
159  _seedHits.push_back(hit);
160  }
161 
162 #ifdef StiCATpcSeedFinderBLOG
163  if (unsPad) {
164 
165  double pct = unsPad*100./nHits;
166 StiDebug::Count("UnsPad_Pct",unsPad, pct);
167  }
168  if (unsRxy) {
169 
170  double pct = unsRxy*100./nHits;
171 StiDebug::Count("UnsRxy_Pct",unsRxy, pct);
172  }
173  if (reUsed) {
174 
175  double pct = reUsed*100./nHits;
176 StiDebug::Count("Reused_Pct",reUsed, pct);
177  }
178 #endif //StiCATpcSeedFinderBLOG
179 
180  StiKalmanTrack* track = 0;
181  if (_seedHits.size() >=4) {
182 
183  track = static_cast<StiKalmanTrack*>(StiToolkit::instance()->getTrackFactory()->getInstance());
184  if (!begEndFail)
185  track->initialize0(_seedHits, &aSeed.firstNodePars, &aSeed.lastNodePars/*, &aSeed.firstNodeErrs, &aSeed.lastNodeErrs*/ ); // use CATracker parameters. P.S errors should not be copied, they'd be initialized.
186  else
187  track->initialize0(_seedHits);
188  }
189  mSeeds->pop_back(); mEnded = !mSeeds->size();
190  if (!track && !mEnded) continue;
191  return track;
192  }
193  mEnded = 3; return 0;
194 }
Definition of Kalman Track.
virtual StiTrack * findTrack(double rMin=0)
Find the next track.
Definition: StHit.h:125
Abstract definition of a Track.
Definition: StiTrack.h:59
Definition: StiHit.h:51
const Float_t & x() const
Return the local x, y, z values.
Definition: StiHit.h:67
Float_t x_g() const
Return the global x, y, z values.
Definition: StiHit.h:73
virtual Abstract * getInstance()=0
Get a pointer to instance of objects served by this factory.
Definition of Kalman Track.
void Run()
Copy data to CATracker. Run CATracker. Copy tracks in fSeeds.
UInt_t timesUsed() const
Return the number of times this hit was assigned to a track.
Definition: StiHit.h:108
float rxy() const
Return the rxy.
Definition: StiHit.h:77
Abstract interface for a STI toolkit.
const StMeasuredPoint * stHit() const
Definition: StiHit.h:104
static StiCATpcTrackerInterface & Instance()
Instance.