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