StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFgtLHTracking.cxx
1 /***************************************************************************
2  *
3  * $Id: StFgtLHTracking.cxx,v 1.4 2012/04/13 15:08:58 sgliske Exp $
4  * Author: S. Gliske, March 2012
5  *
6  ***************************************************************************
7  *
8  * Description: see header.
9  *
10  ***************************************************************************
11  *
12  * $Log: StFgtLHTracking.cxx,v $
13  * Revision 1.4 2012/04/13 15:08:58 sgliske
14  * updates
15  *
16  * Revision 1.3 2012/04/11 22:13:30 sgliske
17  * update
18  *
19  * Revision 1.2 2012/04/09 21:08:24 sgliske
20  * many bugs fixed--seems to be working
21  *
22  * Revision 1.1 2012/03/16 21:51:22 sgliske
23  * creation
24  *
25  *
26  **************************************************************************/
27 
28 #include "StFgtLHTracking.h"
29 #include "StRoot/StFgtUtil/geometry/StFgtGeom.h"
30 
31 //#define DEBUG
32 
33 #include "StMuDSTMaker/COMMON/StMuDstMaker.h"
34 #include "StMuDSTMaker/COMMON/StMuDst.h"
35 #include "StMuDSTMaker/COMMON/StMuEvent.h"
36 #include "StarClassLibrary/StThreeVectorF.hh"
37 #include <TVector3.h>
38 
39 // constructor
40 StFgtLHTracking::StFgtLHTracking( const Char_t* name ) : StFgtTracking( name ), mPoints( 3 ), mFitThres( 1 ), mIncludeThres( 0.5 ), mNumAgreeThres( mPoints-1 ), mUseVertex(0) { /* */ };
41 
42 // deconstructor
43 StFgtLHTracking::~StFgtLHTracking(){ /* */ };
44 
45 void StFgtLHTracking::Clear( const Option_t *opt ){
46  StFgtTracking::Clear();
47  mLineVec.clear();
48  mTrackVec.clear();
49 };
50 
51 // find the tracks
52 Int_t StFgtLHTracking::findTracks(){
53 
54  // Make lines out of triplets of points. Points must come from different discs.
55  StFgtTrPointVec tempPoints;
56  Int_t startDiscIdx = 0;
57  UShort_t discBitArray = 0;
58  TVector3 vertex;
59  Bool_t vertexValid = 0;
60 
61  if( mUseVertex ){
62  StMuDstMaker* muDstMkr = static_cast< StMuDstMaker* >( GetMaker( "MuDst" ) );
63  if( muDstMkr ){
64  StMuEvent *event = muDstMkr->muDst()->event();
65 
66  if( event ){
67  const StThreeVectorF& v = event->primaryVertexPosition();
68  tempPoints.push_back( StFgtTrPoint( v.x(), v.y(), v.z() ) );
69  vertex.SetXYZ( v.x(), v.y(), v.z() );
70  vertexValid = 1;
71  } else {
72  LOG_ERROR << "ERROR finding vertex from StMuEvent" << endl;
73  };
74  };
75  };
76 
77  makePointTuples( tempPoints, startDiscIdx, discBitArray );
78 
79  Int_t nLines = mLineVec.size();
80 
81 #ifdef DEBUG
82  if( nLines ){
83  LOG_INFO << "------------------------------------------------" << endm;
84  LOG_INFO << "Event number " << GetEventNumber() << " has " << mPointsTot << " points and " << nLines << " line(s)" << endm;
85 
86 // StFgtLHLineVec::iterator lineIter;
87 // for( lineIter = mLineVec.begin(); lineIter != mLineVec.end(); ++lineIter ){
88 // LOG_INFO << "Line params "
89 // << lineIter->mx << ' ' << lineIter->bx << ' '
90 // << lineIter->my << ' ' << lineIter->by << ' '
91 // << lineIter->vertZ << ' ' << lineIter->res << ' ' << lineIter->res/sqrt(mPoints) << endm;
92 // };
93  };
94 #endif
95 
96  if( nLines ){
97  // copy lines to track containers
98  mTrackVec.clear();
99  StFgtLHLineVec::iterator lineIter;
100  for( lineIter = mLineVec.begin(); lineIter != mLineVec.end(); ++lineIter )
101  mTrackVec.push_back( StFgtLHTrack( *lineIter ) );
102 
103  // prep stuff
104  Double_t includeThresSq = mIncludeThres * mIncludeThres;
105  StFgtLHTrackVec::iterator trackIter1, trackIter2;
106  StFgtTrPointVec::const_iterator pointIter;
107 
108  // iterate over lines to collect the nearby (or nearest) point
109  Int_t trackIdx = 0;
110  for( trackIter1 = mTrackVec.begin(); trackIter1 != mTrackVec.end(); ++trackIter1, ++trackIdx ){
111  for( Int_t disc = 0; disc < kFgtNumDiscs; ++disc ){
112  StFgtTrPointVec &pointVec = mPointVecPerDisc[ disc ];
113 
114  Int_t pointIdx = 0;
115  for( pointIter = pointVec.begin(); pointIter != pointVec.end(); ++pointIter, ++pointIdx ){
116  Double_t thisResSq = perpDistSqLineToPoint( trackIter1->line, pointIter->pos );
117 
118  if( thisResSq < includeThresSq && thisResSq < trackIter1->resSqPerDisc[disc] ){
119  trackIter1->resSqPerDisc[disc] = thisResSq;
120  trackIter1->pointPerDisc[disc] = pointIdx;
121  };
122  };
123  };
124  };
125 
126 #ifdef DEBUG2
127  Int_t nTracks = mTrackVec.size();
128  LOG_INFO << "Before prunning, event number " << GetEventNumber() << " has " << nTracks << " tracks" << endm;
129 
130  trackIdx = 0;
131  for( trackIter1 = mTrackVec.begin(); trackIter1 != mTrackVec.end(); ++trackIter1, ++trackIdx ){
132  cout << "\tPoints on track " << trackIdx << ": ";
133  for( Int_t i = 0; i<kFgtNumDiscs; ++i )
134  cout << trackIter1->pointPerDisc[i] << ' ';
135  cout << endl;
136  };
137 
138  cout << "mInclude Thres is " << mIncludeThres << endl;
139 #endif
140 
141  // now combine tracks that include almost the same points
142  for( trackIter1 = mTrackVec.begin(); trackIter1 != mTrackVec.end(); ++trackIter1 ){
143  if( trackIter1->pointPerDisc[0] > -2 ){
144  trackIter1->effResSq = 0;
145  Int_t nDiscs = 0;
146 
147  if( vertexValid ){
148  ++nDiscs;
149  trackIter1->effResSq += perpDistSqLineToPoint( trackIter1->line, vertex );
150  };
151 
152  for( Int_t disc = 0; disc < kFgtNumDiscs; ++disc )
153  if( trackIter1->pointPerDisc[disc] > -1 ){
154  trackIter1->effResSq += trackIter1->resSqPerDisc[disc];
155  ++nDiscs;
156  };
157  trackIter1->effResSq /= (nDiscs*nDiscs);
158 
159  if( nDiscs < mPoints ){
160  trackIter1->pointPerDisc[0] = -10;
161  continue;
162  };
163 
164  for( trackIter2 = mTrackVec.begin(); trackIter2 != trackIter1; ++trackIter2 ){
165  if( trackIter2->pointPerDisc[0] > -2 ){
166  Int_t nAgree = 0;
167  for( Int_t i = 0; i<kFgtNumDiscs; ++i )
168  nAgree += ( trackIter1->pointPerDisc[i] == trackIter2->pointPerDisc[i] && trackIter1->pointPerDisc[i] > -1 ) ;
169 
170  //cout << "\tNum Agree " << nAgree << ' ' << trackIter1->effResSq << ' ' << trackIter2->effResSq << endl;
171  if( nAgree >= mNumAgreeThres ){
172  ( trackIter1->effResSq < trackIter2->effResSq ? trackIter2 : trackIter1 )->pointPerDisc[0] = -10; // flag to delete
173  };
174  };
175  };
176  };
177  };
178 
179  // prune the removed lines
180  StFgtLHTrackVec copyVec;
181  for( trackIter1 = mTrackVec.begin(); trackIter1 != mTrackVec.end(); ++trackIter1 )
182  if( trackIter1->pointPerDisc[0] > -2 )
183  copyVec.push_back( *trackIter1 );
184  copyVec.swap( mTrackVec );
185 
186 #ifdef DEBUG
187  Int_t nTracks = mTrackVec.size();
188  LOG_INFO << "After prunning, event number " << GetEventNumber() << " has " << nTracks << " tracks" << endm;
189 
190  trackIdx = 0;
191  for( trackIter1 = mTrackVec.begin(); trackIter1 != mTrackVec.end(); ++trackIter1, ++trackIdx ){
192  cout << "\tPoints on track " << trackIdx << ": ";
193  for( Int_t i = 0; i<kFgtNumDiscs; ++i )
194  cout << trackIter1->pointPerDisc[i] << ' ';
195  cout << ", old vertZ " << trackIter1->line.vertZ << ", effRes " << sqrt( trackIter1->effResSq ) << endl;
196  };
197 #endif
198 
199  // refit the line
200  mLineVec.clear();
201  StFgtTrPointVec points;
202  StFgtTrPoint vertex2( vertex.X(), vertex.Y(), vertex.Z() );
203 
204  for( trackIter1 = mTrackVec.begin(); trackIter1 != mTrackVec.end(); ++trackIter1, ++trackIdx ){
205  points.clear();
206  UShort_t bitArray = 0;
207 
208  if( vertexValid )
209  points.push_back( vertex2 );
210 
211  for( Int_t i = 0; i<kFgtNumDiscs; ++i )
212  if( trackIter1->pointPerDisc[i] > -1 ){
213  points.push_back( mPointVecPerDisc[i][trackIter1->pointPerDisc[i]] );
214  bitArray |= (1<<i);
215  };
216 
217  // compute the new line
218  makeLine( points, bitArray, &trackIter1->line );
219 
220  // copy the results
221  trackIter1->effResSq = trackIter1->line.res / points.size();
222  trackIter1->effResSq *= trackIter1->effResSq;
223 
224 #ifdef DEBUG
225  Double_t perp2 = trackIter1->line.bx*trackIter1->line.bx + trackIter1->line.by*trackIter1->line.by;
226  Double_t x = trackIter1->line.bx + trackIter1->line.mx*120;
227  Double_t y = trackIter1->line.by + trackIter1->line.my*120;
228  Double_t perpB = sqrt(x*x+y*y);
229  cout << "\tEvent " << GetEventNumber() << " Track new vertZ " << trackIter1->line.vertZ << ", effRes " << sqrt( trackIter1->effResSq )
230  << ", bitVec 0x" << std::hex << bitArray << std::dec << ", perp at z=0 " << sqrt(perp2) << " perp at z=120 " << perpB;
231 
232  {
233  StMuDstMaker* muDstMkr = static_cast< StMuDstMaker* >( GetMaker( "MuDst" ) );
234  if( muDstMkr ){
235  StMuEvent *event = muDstMkr->muDst()->event();
236 
237  if( event ){
238  const StThreeVectorF& v = event->primaryVertexPosition();
239  cout << " delta vertZ = " << trackIter1->line.vertZ - v.z();
240  };
241  };
242  };
243  cout << endl;
244 #endif
245  };
246 
247 #ifdef DEBUG
248  StMuDstMaker* muDstMkr = static_cast< StMuDstMaker* >( GetMaker( "MuDst" ) );
249  if( muDstMkr ){
250  StMuEvent *event = muDstMkr->muDst()->event();
251 
252  if( event ){
253  const StThreeVectorF& v = event->primaryVertexPosition();
254  cout << "-----> Real vertex at " << v.x() << ' ' << v.y() << ' ' << v.z() << endl;
255  };
256  };
257 #endif
258 
259 // if( nTracks ){
260 // StFgtLHTrackData *data = new StFgtLHTrackData( "LHTracks", mTrackVec );
261 // AddData( data );
262 // cout << "Event " << GetEventNumber() << " added data is at "
263 // << GetData( "LHTracks" ) << endl;
264 // };
265  };
266 
267  return kStOk;
268 };
269 
270 
271 // recursive algo to get all combinations of mPoints points, each point beging on a different disc
272 void StFgtLHTracking::makePointTuples( StFgtTrPointVec& points, Int_t startDiscIdx, UShort_t discBitArray ){
273  if( startDiscIdx < kFgtNumDiscs ){
274  StFgtTrPointVec::const_iterator iter;
275 
276  //LOG_INFO << "Event Number " << GetEventNumber() << " ---------> Number of points " << points.size() << ", start disc is " << startDiscIdx+1 << endm;
277 
278  for( Int_t disc = startDiscIdx; disc < kFgtNumDiscs; ++disc ){
279  StFgtTrPointVec &pointVec = mPointVecPerDisc[ disc ];
280  UShort_t discBit = (1<<disc);
281 
282  for( iter = pointVec.begin(); iter != pointVec.end(); ++iter ){
283  points.push_back( *iter );
284  //cout << std::dec << GetEventNumber() << " 0x" << std::hex << (discBitArray | discBit) << std::dec << ' ' << points.size() << endl;
285  ( (Int_t)points.size() < mPoints ) ? makePointTuples( points, disc+1, discBitArray | discBit ) : makeLine( points, discBitArray | discBit );
286  points.pop_back();
287  };
288  };
289  };
290 };
291 
292 void StFgtLHTracking::makeLine( StFgtTrPointVec& points, UShort_t discBitArray, StFgtLHLine *linePtr ){
293  StFgtTrPointVec::const_iterator iter = points.begin();
294  Double_t A = 0, B = 0, Cx = 0, Cy = 0, D = 0, Ex = 0, Ey = 0;
295 
296  //LOG_INFO << " --------------------------> calling makeLine with " << points.size() << " 0x" << std::hex << discBitArray << std::dec << endm;
297 
298  for( ; iter != points.end(); ++iter ){
299  Double_t x = iter->pos.X();
300  Double_t y = iter->pos.Y();
301  Double_t z = iter->pos.Z();
302 
303  A += z*z;
304  B += z;
305  Cx += x*z;
306  Cy += y*z;
307  Ex += x;
308  Ey += y;
309 
310  //cout << "*** Point located at " << x << ' ' << y << ' ' << z << " <-> " << iter->pos.Perp() << ' ' << iter->pos.Phi() << endl;
311  };
312  D = points.size();
313  //cout << "*** Consts " << A << ' ' << B << ' ' << Cx << ' ' << Cy << ' ' << D << ' ' << Ex << ' ' << Ey << endl;
314 
315  Double_t denom = D*A - B*B;
316  if( denom ){
317  Double_t bx = (-B*Cx + A*Ex)/denom;
318  Double_t by = (-B*Cy + A*Ey)/denom;
319  Double_t mx = ( D*Cx - B*Ex)/denom;
320  Double_t my = ( D*Cy - B*Ey)/denom;
321 
322  mLineVec.push_back( StFgtLHLine( discBitArray, mx, my, bx, by ) );
323  StFgtLHLine& line = mLineVec.back();
324 
325  //cout << "*** Line params " << mx << ' ' << bx << ' ' << my << ' ' << by << endl;
326 
327 // for( iter = points.begin(); iter != points.end(); ++iter ){
328 // cout << "--- Location at each disc, "
329 // << "X: " << mx*iter->pos.Z()+bx << " vs " << iter->pos.X() << ' '
330 // << "Y: " << my*iter->pos.Z()+by << " vs " << iter->pos.Y() << endl;
331 // };
332 
333  // push back points and compute dist
334  Double_t dist = 0;
335  for( iter = points.begin(); iter != points.end(); ++iter ){
336  line.pointIdx.push_back( iter->ptIdx );
337  dist += perpDistSqLineToPoint( line, iter->pos );
338  //cout << "*** DistSq " << dist << endl;
339  };
340  line.res = sqrt(dist);
341 
342  // copy to pointer, if one provided
343  if( linePtr )
344  *linePtr = line;
345 
346  // #ifdef DEBUG
347  // cout << "*** Event " << GetEventNumber() << " final distSQ " << dist << endl;
348  // #endif
349 
350  // add to vector, if thes OK
351  if( line.res > mFitThres )
352  mLineVec.pop_back();
353  };
354 };
355 
356 ClassImp( StFgtLHTracking );
StMuDst * muDst()
Definition: StMuDstMaker.h:425
static StMuEvent * event()
returns pointer to current StMuEvent (class holding the event wise information, e.g. event number, run number)
Definition: StMuDst.h:320
Definition: Stypes.h:41