FairRoot/PandaRoot
PndTrkTools.cxx
Go to the documentation of this file.
1 
2 //
3 // PndTrkTools.cxx
4 //
5 // authors: Lia Lavezzi - INFN Pavia (2012)
6 //
7 
8 #include "PndTrkTools.h"
9 
10 #include "TMath.h"
11 
12 
13 #include <iostream>
14 
15 using namespace std;
16 
19 
20 // given a slope, where this line is tangent
21 // to a given circle? There are two points
22 // where the line is tangent
23 void PndTrkTools::ComputeLinePointOfTangenceOnACircle(double m, double xc, double yc, double R, double &xi1, double &yi1, double &xi2, double &yi2) {
24  // there are two points where the line is tangent
25  xi1 = xc + m * R / TMath::Sqrt(m * m + 1);
26  yi1 = yc - R / TMath::Sqrt(m * m + 1);
27 
28  xi2 = xc - m * R / TMath::Sqrt(m * m + 1);
29  yi2 = yc + R / TMath::Sqrt(m * m + 1);
30 
31 }
32 
33 // if I have a point P(x, y) which is not
34 // exactly on a circle(xc, yc, R) and I need
35 // the point ON the circle closest to P
36 TVector2 PndTrkTools::ComputePocaToPointOnCircle2(double x, double y, double xc, double yc, double R) {
37  // line which pass in P and C(xc, yc)
38  double m = (y - yc)/(x - xc);
39  // its perpendicular --> tangent in the poca I am searching
40  double mperp = -1./m;
41 
42  double xi1, yi1, xi2, yi2;
43  ComputeLinePointOfTangenceOnACircle(mperp, xc, yc, R, xi1, yi1, xi2, yi2);
44 
45  // I want the nearest to P
46  double xp = 999, yp = -999;
47 
48  sqrt((xi1 - x) * (xi1 - x) + (yi1 - y) * (yi1 - y)) < sqrt((xi2 - x) * (xi2 - x) + (yi2 - y) * (yi2 - y)) ? (xp = xi1 , yp = yi1) : (xp = xi2 , yp = yi2);
49  return TVector2(xp, yp);
50 }
51 
52 TVector3 PndTrkTools::ComputePocaToPointOnCircle3(double x, double y, double xc, double yc, double R) {
53  TVector2 vec = ComputePocaToPointOnCircle2(x, y, xc, yc, R);
54  return TVector3(vec.X(), vec.Y(), 0.0);
55 }
56 
57 // I have a circle (xc, yc, R) and a
58 // straight line (y = mx + q) and I
59 // want the point ON the circle closest to the line
60 TVector2 PndTrkTools::ComputePocaToLineOnCircle(double m, double q, double xc, double yc, double R) {
61  // there are two points where the line is tangent
62  // to the circle
63  double xi1, yi1, xi2, yi2;
64  ComputeLinePointOfTangenceOnACircle(m, xc, yc, R, xi1, yi1, xi2, yi2);
65 
66  // I want the nearest to the line
67  double xp = 999, yp = -999;
68  fabs(yi1 - (m * xi1 + q)) < fabs(yi2 - (m * xi2 + q)) ? (xp = xi1, yp = yi1) : (xp = xi2 , yp = yi2);
69 
70  return TVector2(xp, yp);
71 }
72 
73 // I have a circle (xc, yc, R) and a
74 // segment of a straight line (x1, y1) - (x2, y2)
75 // and I want to know whether they are
76 // intersecting and where
77 Int_t PndTrkTools::ComputeSegmentCircleIntersection(TVector2 ex1, TVector2 ex2, double xc, double yc, double R, TVector2 &intersection1, TVector2 &intersection2) {
78 
79  double xa, ya, xb, yb;
80  double delta;
81  // ex1.Print();
82  // ex2.Print();
83  // cout << xc << " " << yc << " " << R << endl;
84 
85  if((ex2.X() - ex1.X()) != 0) {
86  double m = (ex2.Y() - ex1.Y())/(ex2.X() - ex1.X());
87  double p = ex2.Y() - m * ex2.X();;
88 
89  delta = - (m * xc + p - yc) * (m * xc + p - yc) + R * R * (m * m + 1);
90  // cout << "delta " << delta << endl;
91  if(delta < 0 || TMath::IsNaN(delta)) return 0;
92  xa = (-(m * (p - yc) - xc) + sqrt(delta))/(m * m + 1);
93  ya = m * xa + p;
94  xb = (-(m * (p - yc) - xc) - sqrt(delta))/(m * m + 1);
95  yb = m * xb + p;
96  }
97  else {
98  // (x - xc)**2 + (y -yc)**2 = R**2
99  // y**2 - 2yc y + x**2 + xc**2 - 2xc x + yc**2 - R**2 = 0;
100  // y = yc +/- sqrt(R**2 -(x - xc)**2)
101  delta = R * R - (ex1.X() - xc) * (ex1.X() - xc);
102  // cout << "// to y, delta " << delta << endl;
103  if(delta < 0 || TMath::IsNaN(delta)) return 0;
104 
105  xa = ex1.X();
106  ya = yc + sqrt(delta);
107  xb = ex1.X();
108  yb = yc - sqrt(delta);
109  }
110 
111  TVector2 int1(xa, ya);
112  TVector2 int2(xb, yb);
113 
114  TVector2 distance11 = int1 - ex1;
115  TVector2 distance12 = int1 - ex2;
116 
117  TVector2 distance21 = int2 - ex1;
118  TVector2 distance22 = int2 - ex2;
119 
120  TVector2 length = ex1 - ex2;
121  Int_t found = 0;
122  // cout << distance11.Mod() << " " << distance12.Mod() << " " << length.Mod() << endl;
123  // cout << distance21.Mod() << " " << distance22.Mod() << " " << length.Mod() << endl;
124  if(distance11.Mod() < length.Mod() && distance12.Mod() < length.Mod()) {
125  // cout << "intersection 1" << endl;
126  // TMarker *mrka = new TMarker(xa, ya, 20);
127  // mrka->Draw("SAME");
128  found++;
129  intersection1 = int1;
130  }
131 
132  if(distance22.Mod() < length.Mod() && distance21.Mod() < length.Mod()) {
133  // cout << "intersection 2" << endl;
134  // TMarker *mrkb = new TMarker(xb, yb, 22);
135  // mrkb->Draw("SAME");
136  if(found == 1) intersection2 = int2;
137  else intersection1 = int2;
138  found++;
139  }
140 
141  // if delta = 0 we have two identical intersections
142  if(delta == 0) found--; // CHECK
143  return found;
144 }
145 
146 // I have a circle (xc, yc, R) and a
147 // point on the circle.
148 // I want the tangent to the circle in that point
149 TVector2 PndTrkTools::ComputeTangentInPoint(double xc, double yc, TVector2 point) {
150  TVector2 center(xc, yc);
151  TVector2 fromcentertoint = point - center;
152  TVector2 tangent(fromcentertoint.Y(), -fromcentertoint.X()); // CHECK
153  return tangent;
154 }
155 
156 void PndTrkTools::ComputeTangentInPoint(double xc, double yc, TVector2 point, double &m, double &p) {
157  TVector2 tangent = ComputeTangentInPoint(xc, yc, point);
158  m = TMath::Tan(tangent.Phi()); // CHECK this is [0, 2pi] by default
159  p = point.Y() - m * point.X();
160 }
161 
163 
Double_t p
Definition: anasim.C:58
TVector2 ComputePocaToLineOnCircle(double m, double q, double xc, double yc, double R)
Definition: PndTrkTools.cxx:60
__m128 m
Definition: P4_F32vec4.h:28
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:29
static T Sqrt(const T &x)
Definition: PndCAMath.h:37
float Tan(float x)
Definition: PndCAMath.h:165
TVector2 ComputePocaToPointOnCircle2(double x, double y, double xc, double yc, double R)
Definition: PndTrkTools.cxx:36
TVector2 ComputeTangentInPoint(double xc, double yc, TVector2 point)
friend F32vec4 fabs(const F32vec4 &a)
Definition: P4_F32vec4.h:47
TVector3 ComputePocaToPointOnCircle3(double x, double y, double xc, double yc, double R)
Definition: PndTrkTools.cxx:52
void ComputeLinePointOfTangenceOnACircle(double m, double xc, double yc, double R, double &xi1, double &yi1, double &xi2, double &yi2)
Definition: PndTrkTools.cxx:23
Double_t x
Int_t ComputeSegmentCircleIntersection(TVector2 ex1, TVector2 ex2, double xc, double yc, double R, TVector2 &intersection1, TVector2 &intersection2)
Definition: PndTrkTools.cxx:77
ClassImp(PndAnaContFact)
Double_t y
Double_t R
Definition: checkhelixhit.C:61
dble_vec_t vec[12]
Definition: ranlxd.cxx:380
PndSdsMCPoint * point
Definition: anaLmdCluster.C:72