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