All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Seed.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // \brief 3D seed object for kalman tracking package
4 // and bezier tracking algorithm
5 //
6 // \author Ben Jones, MIT
7 // bjpjones@mit.edu
8 //
9 ////////////////////////////////////////////////////////////////////////////
12 #include "TVector3.h"
13 
14 #include <iostream>
15 #include <stdlib.h>
16 
17 namespace recob{
18 
19 
20  //----------------------------------------------------------------------------
22  {
23  fIsValid=false;
24  }
25 
26 
27  //----------------------------------------------------------------------------
28  Seed::Seed(double* Pt, double* Dir)
29  {
30  for(int i=0; i!=3; i++)
31  {
32  fSeedPoint[i] = Pt[i];
33  fSeedDirection[i] = Dir[i];
34  fSeedPointError[i] = 0;
35  fSeedDirectionError[i] = 0;
36  }
37  fIsValid=true;
38  }
39 
40  //----------------------------------------------------------------------------
41  Seed::Seed(double* Pt, double* Dir, double* PtErr, double* DirErr)
42  {
43  for(int i=0; i!=3; i++)
44  {
45  fSeedPoint[i] = Pt[i];
46  fSeedDirection[i] = Dir[i];
47  fSeedPointError[i] = PtErr[i];
48  fSeedDirectionError[i] = DirErr[i];
49  }
50  fIsValid=true;
51  }
52 
53 
54  //----------------------------------------------------------------------------
55  void Seed::Print() const
56  {
57  std::cout<<"Printing seed contents : "
58  << fSeedPoint[0]<<" "
59  <<fSeedPoint[1]<< " "
60  <<fSeedPoint[2]<<", "
61  << fSeedDirection[0]
62  <<" " <<fSeedDirection[1]
63  <<" " << fSeedDirection[2]
64  <<std::endl;
65 
66  }
67 
68 
69  //----------------------------------------------------------------------------
70  bool Seed::IsValid() const
71  {
72  return fIsValid;
73  }
74 
75 
76  //----------------------------------------------------------------------------
78  {
79  double NewSeedDir[3];
80  for(size_t n=0; n!=3; ++n)
81  {
82  NewSeedDir[n]=-fSeedDirection[n];
83  }
84  return Seed(fSeedPoint, NewSeedDir, fSeedPointError, fSeedDirectionError);
85 
86  }
87 
88 
89  //----------------------------------------------------------------------------
90  void Seed::SetValidity(bool Validity)
91  {
92  fIsValid=Validity;
93  }
94 
95 
96 
97  //----------------------------------------------------------------------------
98  void Seed::GetDirection(double * rDir, double* rDirErr) const
99  {
100  for(int i=0; i!=3; i++)
101  {
102  rDir[i] = fSeedDirection[i];
103  if (rDirErr) rDirErr[i] = fSeedDirectionError[i];
104  }
105  }
106 
107  //----------------------------------------------------------------------------
108  void Seed::GetPoint(double * rPt, double* rPtErr) const
109  {
110  for(int i=0; i!=3; i++)
111  {
112  rPt[i] = fSeedPoint[i];
113  if (rPtErr) rPtErr[i] = fSeedPointError[i];
114  }
115  }
116 
117 
118  //----------------------------------------------------------------------------
119  void Seed::SetDirection( double * Dir)
120  {
121  double Empty[3]={0,0,0};
122  SetDirection(Dir, Empty);
123  }
124 
125  //----------------------------------------------------------------------------
126  void Seed::SetPoint( double * Pt)
127  {
128  double Empty[3]={0,0,0};
129  SetPoint(Pt, Empty);
130  }
131 
132  //----------------------------------------------------------------------------
133  void Seed::SetDirection( double * Dir, double * Err)
134  {
135  for(int i=0; i!=3; i++)
136  {
137  fSeedDirection[i] = Dir[i];
138  fSeedDirectionError[i] = Err[i];
139  }
140  fIsValid=true;
141  }
142 
143  //----------------------------------------------------------------------------
144  void Seed::SetPoint(double* Pt, double* Err)
145  {
146  for(int i=0; i!=3; i++)
147  {
148  fSeedPoint[i] = Pt[i];
149  fSeedPointError[i] = Err[i];
150  }
151  fIsValid=true;
152  }
153 
154  //----------------------------------------------------------------------------
155  double Seed::GetLength() const
156  {
157  return pow(pow(fSeedDirection[0],2)+
158  pow(fSeedDirection[1],2)+
159  pow(fSeedDirection[2],2),0.5);
160  }
161 
162 
163  //----------------------------------------------------------------------------
164  double Seed::GetProjAngleDiscrepancy(Seed const & AnotherSeed) const
165  {
166  double OtherPt[3];
167  double OtherPtErr[3];
168 
169  AnotherSeed.GetPoint( OtherPt, OtherPtErr );
170 
171  TVector3 OtherPtV( OtherPt[0], OtherPt[1], OtherPt[2] );
172  TVector3 ThisDirV( fSeedDirection[0], fSeedDirection[1], fSeedDirection[2] );
173  TVector3 ThisPtV( fSeedPoint[0], fSeedPoint[1], fSeedPoint[2] );
174 
175  return (OtherPtV-ThisPtV).Angle(ThisDirV.Unit());
176  }
177 
178  //----------------------------------------------------------------------------
179  void Seed::GetVectorBetween(Seed const & AnotherSeed, double* xyz) const
180  {
181  double xyzother[3], err[3];
182  AnotherSeed.GetPoint(xyzother,err);
183 
184  xyz[0] = xyzother[0]-fSeedPoint[0];
185  xyz[1] = xyzother[1]-fSeedPoint[1];
186  xyz[2] = xyzother[2]-fSeedPoint[2];
187 
188  }
189 
190 
191  //----------------------------------------------------------------------------
192  double Seed::GetAngle( Seed const & AnotherSeed) const
193  {
194  double OtherDir[3];
195  double OtherDirErr[3];
196  AnotherSeed.GetDirection(OtherDir,OtherDirErr);
197 
198  double OtherMag = AnotherSeed.GetLength();
199  double ThisMag = GetLength();
200 
201  /*
202  std::cout<<"Seed angle calc: " <<
203  OtherMag<< " " << ThisMag << " " <<
204  ( (OtherDir[0]*fSeedDirection[0] +
205  OtherDir[1]*fSeedDirection[1] +
206  OtherDir[2]*fSeedDirection[2] ) / (ThisMag*OtherMag)) <<
207  std::endl;
208  */
209 
210 
211  // Need a tiny offset, as acos(1.0) gives unpredictable results due to
212  // floating point precision
213  double eta = 0.00001;
214 
215  return std::acos( ( OtherDir[0]*fSeedDirection[0] +
216  OtherDir[1]*fSeedDirection[1] +
217  OtherDir[2]*fSeedDirection[2] - eta) / (ThisMag*OtherMag));
218  }
219 
220 
221  //----------------------------------------------------------------------------
222  double Seed::GetProjDiscrepancy(Seed const & AnotherSeed) const
223  {
224  double OtherPt[3];
225  double OtherPtErr[3];
226 
227  AnotherSeed.GetPoint( OtherPt, OtherPtErr );
228 
229  TVector3 OtherPtV( OtherPt[0], OtherPt[1], OtherPt[2] );
230  TVector3 ThisDirV( fSeedDirection[0], fSeedDirection[1], fSeedDirection[2] );
231  TVector3 ThisPtV( fSeedPoint[0], fSeedPoint[1], fSeedPoint[2] );
232 
233 
234  return ((OtherPtV-ThisPtV) - ThisDirV.Unit()*(ThisDirV.Unit().Dot(OtherPtV-ThisPtV))).Mag();
235  }
236 
237 
238  //----------------------------------------------------------------------------
239  double Seed::GetDistance(Seed const & AnotherSeed) const
240  {
241  double OtherPt[3];
242  double OtherPtErr[3];
243 
244  AnotherSeed.GetPoint( OtherPt, OtherPtErr );
245 
246  return pow( pow(OtherPt[0]-fSeedPoint[0],2)+
247  pow(OtherPt[1]-fSeedPoint[1],2)+
248  pow(OtherPt[2]-fSeedPoint[2],2), 0.5);
249  }
250 
251 
252  //----------------------------------------------------------------------------
253  double Seed::GetDistanceFrom(recob::SpacePoint const & SomePoint) const
254  {
255  double SPxyz[3];
256  SPxyz[0]=SomePoint.XYZ()[0];
257  SPxyz[1]=SomePoint.XYZ()[1];
258  SPxyz[2]=SomePoint.XYZ()[2];
259 
260  // std::cout<<"Seed Dir Vec " << fSeedDirection[0]<<" " <<fSeedDirection[1]<< " " << fSeedDirection[2]<<std::endl;
261 
262  double ThisSeedLength =
263  pow( pow(fSeedDirection[0],2) +
264  pow(fSeedDirection[1],2) +
265  pow(fSeedDirection[2],2) , 0.5);
266 
267 
268  double SPProjOnSeed =
269  ( fSeedDirection[0] * ( SPxyz[0] - fSeedPoint[0] ) +
270  fSeedDirection[1] * ( SPxyz[1] - fSeedPoint[1] ) +
271  fSeedDirection[2] * ( SPxyz[2] - fSeedPoint[2] ) )
272  / ThisSeedLength;
273 
274  // std::cout<<"proj : " <<SPProjOnSeed<<std::endl;
275  // std::cout<<"Seed len :" << ThisSeedLength<<std::endl;
276 
277  if(SPProjOnSeed > (ThisSeedLength))
278  {
279  // std::cout<<"Seed over end"<<std::endl;
280  return
281  pow(pow(fSeedPoint[0] + fSeedDirection[0] - SPxyz[0], 2) +
282  pow(fSeedPoint[1] + fSeedDirection[1] - SPxyz[1], 2) +
283  pow(fSeedPoint[2] + fSeedDirection[2] - SPxyz[2], 2), 0.5);
284  }
285  else if(SPProjOnSeed<(0-ThisSeedLength))
286  {
287  // std::cout<<"Seed under end"<<std::endl;
288  return
289  pow(pow(fSeedPoint[0] - fSeedDirection[0] - SPxyz[0], 2) +
290  pow(fSeedPoint[1] - fSeedDirection[1] - SPxyz[1], 2) +
291  pow(fSeedPoint[2] - fSeedDirection[2] - SPxyz[2], 2), 0.5);
292 
293  }
294  else
295  {
296  // std::cout<<"Seed valid region"<<std::endl;
297  double crossprod[3];
298  CrossProd( fSeedPoint[0]+fSeedDirection[0]-SPxyz[0],
299  fSeedPoint[1]+fSeedDirection[1]-SPxyz[1],
300  fSeedPoint[2]+fSeedDirection[2]-SPxyz[2],
301  SPxyz[0]-fSeedPoint[0],
302  SPxyz[1]-fSeedPoint[1],
303  SPxyz[2]-fSeedPoint[2],
304  crossprod[0], crossprod[1], crossprod[2]);
305 
306  return
307  pow( pow(crossprod[0],2) +
308  pow(crossprod[1],2) +
309  pow(crossprod[2],2), 0.5) /
310  pow( pow(fSeedDirection[0],2) +
311  pow(fSeedDirection[1],2) +
312  pow(fSeedDirection[2],2), 0.5);
313 
314  }
315  }
316 
317  //----------------------------------------------------------------------------
318  int Seed::GetPointingSign(Seed const & AnotherSeed) const
319  {
320  double OtherPos[3], OtherErr[3];
321  AnotherSeed.GetPoint(OtherPos,OtherErr);
322  double DotProd =
323  (OtherPos[0]-fSeedPoint[0])*fSeedDirection[0]+
324  (OtherPos[1]-fSeedPoint[1])*fSeedDirection[1]+
325  (OtherPos[2]-fSeedPoint[2])*fSeedDirection[2];
326  return ((DotProd>0)-(DotProd<0));
327  }
328 
329 
330  //----------------------------------------------------------------------------
331  void CrossProd(double x1, double x2, double x3,
332  double y1, double y2, double y3,
333  double& out1, double& out2, double& out3)
334  {
335  out1 = (x2*y3-x3*y2);
336  out2 = (x3*y1-x1*y3);
337  out3 = (x1*y2-x2*y1);
338  }
339 
340  //----------------------------------------------------------------------
341  std::ostream& operator<< (std::ostream& o, Seed const& a)
342  {
343  o << "Printing seed contents : "
344  << a.fSeedPoint[0] << " "
345  << a.fSeedPoint[1] << " "
346  << a.fSeedPoint[2] << ", "
347  << a.fSeedDirection[0] << " "
348  << a.fSeedDirection[1] << " "
349  << a.fSeedDirection[2];
350 
351  return o;
352  }
353 
354  //----------------------------------------------------------------------
355  // < operator.
356  //
357  bool operator < (const Seed & a, const Seed & b)
358  {
359  if (a.fSeedPoint[2] != b.fSeedPoint[2])
360  { return a.fSeedPoint[2] < b.fSeedPoint[2]; }
361  else if (a.fSeedPoint[1] != b.fSeedPoint[1])
362  { return a.fSeedPoint[1] < b.fSeedPoint[1]; }
363 
364  return a.fSeedPoint[0] < b.fSeedPoint[0];
365 
366  }
367 
368 
369 }
double fSeedDirection[3]
Definition: Seed.h:28
double GetDistanceFrom(SpacePoint const &SomePoint) const
Definition: Seed.cxx:253
bool operator<(Cluster const &a, Cluster const &b)
Definition: Cluster.cxx:196
double GetProjAngleDiscrepancy(Seed const &AnotherSeed) const
Definition: Seed.cxx:164
bool IsValid() const
Definition: Seed.cxx:70
bool fIsValid
Definition: Seed.h:31
double GetAngle(Seed const &AnotherSeed) const
Definition: Seed.cxx:192
EResult err(const char *call)
void GetPoint(double *Pt, double *Err) const
Definition: Seed.cxx:108
double GetProjDiscrepancy(Seed const &AnotherSeed) const
Definition: Seed.cxx:222
Seed Reverse()
Definition: Seed.cxx:77
process_name gaushit a
void CrossProd(double x1, double x2, double x3, double y1, double y2, double y3, double &out1, double &out2, double &out3)
Definition: Seed.cxx:331
void SetPoint(double *Pt, double *Err)
Definition: Seed.cxx:144
double fSeedDirectionError[3]
Definition: Seed.h:30
const Double32_t * XYZ() const
Definition: SpacePoint.h:76
double GetDistance(Seed const &AnotherSeed) const
Definition: Seed.cxx:239
int GetPointingSign(Seed const &AnotherSeed) const
Definition: Seed.cxx:318
double DotProd(const Vector3_t &v1, const Vector3_t &v2)
Definition: PFPUtils.h:128
void GetVectorBetween(Seed const &AnotherSeed, double *xyz) const
Definition: Seed.cxx:179
double fSeedPoint[3]
Definition: Seed.h:27
void Print() const
Definition: Seed.cxx:55
void SetValidity(bool Validity)
Definition: Seed.cxx:90
void GetDirection(double *Dir, double *Err) const
Definition: Seed.cxx:98
BEGIN_PROLOG could also be cout
double fSeedPointError[3]
Definition: Seed.h:29
std::ostream & operator<<(std::ostream &o, Cluster const &c)
Definition: Cluster.cxx:173
double GetLength() const
Definition: Seed.cxx:155
void SetDirection(double *Dir, double *Err)
Definition: Seed.cxx:133