All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sbndcode/sbndcode/CRT/CRTUtils/CRTCommonUtils.cc
Go to the documentation of this file.
2 
3 namespace sbnd{
4 
5 // Simple distance of closest approach between infinite track and centre of hit
6 double CRTCommonUtils::SimpleDCA(sbn::crt::CRTHit hit, TVector3 start, TVector3 direction){
7 
8  TVector3 pos (hit.x_pos, hit.y_pos, hit.z_pos);
9  TVector3 end = start + direction;
10  double denominator = direction.Mag();
11  double numerator = (pos - start).Cross(pos - end).Mag();
12  return numerator/denominator;
13 
14 }
15 
16 // Minimum distance from infinite track to CRT hit assuming that hit is a 2D square
17 double CRTCommonUtils::DistToCrtHit(sbn::crt::CRTHit hit, TVector3 start, TVector3 end){
18 
19  // Check if track goes inside hit
20  TVector3 min (hit.x_pos - hit.x_err, hit.y_pos - hit.y_err, hit.z_pos - hit.z_err);
21  TVector3 max (hit.x_pos + hit.x_err, hit.y_pos + hit.y_err, hit.z_pos + hit.z_err);
22  if(CubeIntersection(min, max, start, end).first.X() != -99999) return 0;
23 
24  // Calculate the closest distance to each edge of the CRT hit
25  // Assume min error is the fixed position of tagger
26  TVector3 vertex1 (hit.x_pos, hit.y_pos - hit.y_err, hit.z_pos - hit.z_err);
27  TVector3 vertex2 (hit.x_pos, hit.y_pos + hit.y_err, hit.z_pos - hit.z_err);
28  TVector3 vertex3 (hit.x_pos, hit.y_pos - hit.y_err, hit.z_pos + hit.z_err);
29  TVector3 vertex4 (hit.x_pos, hit.y_pos + hit.y_err, hit.z_pos + hit.z_err);
30  if(hit.y_err < hit.x_err && hit.y_err < hit.z_err){
31  vertex1.SetXYZ(hit.x_pos - hit.x_err, hit.y_pos, hit.z_pos - hit.z_err);
32  vertex2.SetXYZ(hit.x_pos + hit.x_err, hit.y_pos, hit.z_pos - hit.z_err);
33  vertex3.SetXYZ(hit.x_pos - hit.x_err, hit.y_pos, hit.z_pos + hit.z_err);
34  vertex4.SetXYZ(hit.x_pos + hit.x_err, hit.y_pos, hit.z_pos + hit.z_err);
35  }
36  if(hit.z_err < hit.x_err && hit.z_err < hit.y_err){
37  vertex1.SetXYZ(hit.x_pos - hit.x_err, hit.y_pos - hit.y_err, hit.z_pos);
38  vertex2.SetXYZ(hit.x_pos + hit.x_err, hit.y_pos - hit.y_err, hit.z_pos);
39  vertex3.SetXYZ(hit.x_pos - hit.x_err, hit.y_pos + hit.y_err, hit.z_pos);
40  vertex4.SetXYZ(hit.x_pos + hit.x_err, hit.y_pos + hit.y_err, hit.z_pos);
41  }
42 
43  double dist1 = LineSegmentDistance(vertex1, vertex2, start, end);
44  double dist2 = LineSegmentDistance(vertex1, vertex3, start, end);
45  double dist3 = LineSegmentDistance(vertex4, vertex2, start, end);
46  double dist4 = LineSegmentDistance(vertex4, vertex3, start, end);
47 
48  return std::min(std::min(dist1, dist2), std::min(dist3, dist4));
49 
50 }
51 
52 
53 // Distance between infinite line (2) and segment (1)
54 // http://geomalgorithms.com/a07-_distance.html
55 double CRTCommonUtils::LineSegmentDistance(TVector3 start1, TVector3 end1, TVector3 start2, TVector3 end2){
56 
57  double smallNum = 0.00001;
58 
59  // 1 is segment
60  TVector3 direction1 = end1 - start1;
61  // 2 is infinite line
62  TVector3 direction2 = end2 - start2;
63 
64  TVector3 u = direction1;
65  TVector3 v = direction2;
66  TVector3 w = start1 - start2;
67 
68  double a = u.Dot(u);
69  double b = u.Dot(v);
70  double c = v.Dot(v);
71  double d = u.Dot(w);
72  double e = v.Dot(w);
73  double D = a * c - b * b;
74  double sc, sN, sD = D; // sc = sN/sD
75  double tc, tN, tD = D; // sc = sN/sD
76 
77  // Compute the line parameters of the two closest points
78  if(D < smallNum){ // Lines are almost parallel
79  sN = 0.0;
80  sD = 1.0;
81  tN = e;
82  tD = c;
83  }
84  else{
85  sN = (b * e - c * d)/D;
86  tN = (a * e - b * d)/D;
87  if(sN < 0.){ // sc < 0, the s = 0 edge is visible
88  sN = 0.;
89  tN = e;
90  tD = c;
91  }
92  else if(sN > sD){ // sc > 1, the s = 1 edge is visible
93  sN = sD;
94  tN = e + b;
95  tD = c;
96  }
97  }
98 
99  sc = (std::abs(sN) < smallNum ? 0.0 : sN / sD);
100  tc = (std::abs(tN) < smallNum ? 0.0 : tN / tD);
101  // Get the difference of the two closest points
102  TVector3 dP = w + (sc * u) - (tc * v);
103 
104  return dP.Mag();
105 
106 }
107 
108 // Intersection between axis-aligned cube and infinite line
109 // (https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-box-intersection)
110 std::pair<TVector3, TVector3> CRTCommonUtils::CubeIntersection(TVector3 min, TVector3 max, TVector3 start, TVector3 end){
111 
112  TVector3 dir = (end - start);
113  TVector3 invDir (1./dir.X(), 1./dir.Y(), 1/dir.Z());
114 
115  double tmin, tmax, tymin, tymax, tzmin, tzmax;
116 
117  TVector3 enter (-99999, -99999, -99999);
118  TVector3 exit (-99999, -99999, -99999);
119 
120  // Find the intersections with the X plane
121  if(invDir.X() >= 0){
122  tmin = (min.X() - start.X()) * invDir.X();
123  tmax = (max.X() - start.X()) * invDir.X();
124  }
125  else{
126  tmin = (max.X() - start.X()) * invDir.X();
127  tmax = (min.X() - start.X()) * invDir.X();
128  }
129 
130  // Find the intersections with the Y plane
131  if(invDir.Y() >= 0){
132  tymin = (min.Y() - start.Y()) * invDir.Y();
133  tymax = (max.Y() - start.Y()) * invDir.Y();
134  }
135  else{
136  tymin = (max.Y() - start.Y()) * invDir.Y();
137  tymax = (min.Y() - start.Y()) * invDir.Y();
138  }
139 
140  // Check that it actually intersects
141  if((tmin > tymax) || (tymin > tmax)) return std::make_pair(enter, exit);
142 
143  // Max of the min points is the actual intersection
144  if(tymin > tmin) tmin = tymin;
145 
146  // Min of the max points is the actual intersection
147  if(tymax < tmax) tmax = tymax;
148 
149  // Find the intersection with the Z plane
150  if(invDir.Z() >= 0){
151  tzmin = (min.Z() - start.Z()) * invDir.Z();
152  tzmax = (max.Z() - start.Z()) * invDir.Z();
153  }
154  else{
155  tzmin = (max.Z() - start.Z()) * invDir.Z();
156  tzmax = (min.Z() - start.Z()) * invDir.Z();
157  }
158 
159  // Check for intersection
160  if((tmin > tzmax) || (tzmin > tmax)) return std::make_pair(enter, exit);
161 
162  // Find final intersection points
163  if(tzmin > tmin) tmin = tzmin;
164 
165  // Find final intersection points
166  if(tzmax < tmax) tmax = tzmax;
167 
168  // Calculate the actual crossing points
169  double xmin = start.X() + tmin * dir.X();
170  double xmax = start.X() + tmax * dir.X();
171  double ymin = start.Y() + tmin * dir.Y();
172  double ymax = start.Y() + tmax * dir.Y();
173  double zmin = start.Z() + tmin * dir.Z();
174  double zmax = start.Z() + tmax * dir.Z();
175 
176  // Return pair of entry and exit points
177  enter.SetXYZ(xmin, ymin, zmin);
178  exit.SetXYZ(xmax, ymax, zmax);
179  return std::make_pair(enter, exit);
180 
181 }
182 
184 
185  if (tagger == "volTaggerBot_0" ) return kCRTBot;
186  else if (tagger == "volTaggerSouth_0" ) return kCRTFaceSouth;
187  else if (tagger == "volTaggerNorth_0" ) return kCRTFaceNorth;
188  else if (tagger == "volTaggerWest_0" ) return kCRTSideWest;
189  else if (tagger == "volTaggerEast_0" ) return kCRTSideEast;
190  else if (tagger == "volTaggerTopLow_0" ) return kCRTTopLow;
191  else if (tagger == "volTaggerTopHigh_0" ) return kCRTTopHigh;
192  else {
193  mf::LogWarning("CRTCommonUtils") << "CRT tagger unkown: " << tagger << std::endl;
194  return kCRTNotDefined;
195  }
196 
197 }
198 }
199 
float z_err
position uncertainty in z-direction (cm).
Definition: CRTHit.hh:43
float x_err
position uncertainty in x-direction (cm).
Definition: CRTHit.hh:39
float y_err
position uncertainty in y-direction (cm).
Definition: CRTHit.hh:41
process_name hit
Definition: cheaterreco.fcl:51
process_name gaushit a
process_name pandoraGausCryo1 vertexChargeCryo1 vertexStubCryo1 xmin
float z_pos
position in z-direction (cm).
Definition: CRTHit.hh:42
double DistToCrtHit(sbn::crt::CRTHit hit, TVector3 start, TVector3 end)
T abs(T value)
enum::sbnd::CRTPlane GetPlaneIndex(std::string tagger)
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
double LineSegmentDistance(TVector3 start1, TVector3 end1, TVector3 start2, TVector3 end2)
tuple dir
Definition: dropbox.py:28
float y_pos
position in y-direction (cm).
Definition: CRTHit.hh:40
double SimpleDCA(sbn::crt::CRTHit hit, TVector3 start, TVector3 direction)
float x_pos
position in x-direction (cm).
Definition: CRTHit.hh:38
do i e
stream1 can override from command line with o or output services user sbnd
std::pair< TVector3, TVector3 > CubeIntersection(TVector3 min, TVector3 max, TVector3 start, TVector3 end)