TrdMCClusterR Fit Classifier
Intersection3D.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <cmath>
5 #include <iostream>
6 
7 #include "TRDConstants.h"
8 #include "Line.h"
9 #include "Point.h"
10 
11 using std::cerr;
12 using std::endl;
13 
14 
15 namespace Intersection3D
16 {
17  //____________________________________________________________________________
19  {
20  double x, y, z;
21  std::string FitPlane;
22 
23  // Constructor to initialize values
24  IntersectionPoint3D(const std::string& str, double xVal, double yVal, double zVal) : x(xVal), y(yVal), z(zVal), FitPlane(str) {}
25  };
26 
27 
28  //____________________________________________________________________________
30  {
31  bool intersects;
33 
34  Intersection2DResult() : intersects(false), point({-99999, -99999}) {}
36  };
37 
38 
39  //____________________________________________________________________________
41  const Line::FitLine2D& lineB,
42  const bool& debugMode = false
43  )
44  {
46  if (fabs(lineA.m - lineB.m) <= 0.01*(lineA.err_m + lineB.err_m))
47  {
48  if (fabs(lineA.c - lineB.c) > (lineA.err_c + lineB.err_c))
49  {
51  if (debugMode)
52  {
53  std::cerr<<"The lines are almost parallel (within given uncertainties) and don't intersect.\n";
54  std::cerr<<"fabs(mA - mB) = "<<fabs(lineA.m - lineB.m)<<endl;
55  std::cerr<<"Err_mA + Err_mB = "<<lineA.err_m + lineB.err_m<<endl;
56  }
57  return Intersection2DResult(false, {});
58  }
59  }
60 
63  double x = (lineB.c - lineA.c) / (lineA.m - lineB.m);
64  double y = lineA.m * x + lineA.c;
65  return Intersection2DResult(true, {x, y});
66  }
67 
68 
69 
70 
71  //____________________________________________________________________________
72  bool areLinesCloseEnough(const std::vector<Line::FitLine2D>& linesA,
73  const std::vector<Line::FitLine2D>& linesB,
74  const bool& debugMode = false)
75  {
76  std::vector<bool> vResults;
77  std::vector<IntersectionPoint3D> vPoints;
78 
79  for (size_t i = 0; i < 3; ++i)
80  {
81  if (debugMode)
82  printf("For %s:\n", vFitPlanes[i].c_str());
83 
84  Intersection2DResult result = findIntersection2D(linesA[i], linesB[i], debugMode);
85  vResults.push_back(result.intersects);
86 
87  if (debugMode)
88  {
89  if (result.intersects == false)
90  printf("----> No intersection found.");
91 
92  else
93  {
94  printf("----> Intersection point found at (%s, %s) = %8.3f, %8.3f\n",
95  components.at(i).first.c_str(), components.at(i).second.c_str(), result.point.x, result.point.y);
96  }
97  }
98 
99  if (i == 0)
100  vPoints.push_back(IntersectionPoint3D(vFitPlanes.at(i), result.point.x, 0, result.point.y)); //XZ
101  else if (i == 1)
102  vPoints.push_back(IntersectionPoint3D(vFitPlanes.at(i), 0, result.point.x, result.point.y)); //YZ
103  else if (i == 2)
104  vPoints.push_back(IntersectionPoint3D(vFitPlanes.at(i), result.point.x, result.point.y, 0)); //XY
105  else {}
106  }
107 
108 
109  // Displaying the points stored in the vector
110  if (debugMode)
111  {
112  std::cout<<"Saved 3D intersection points:\n";
113  printf("%s: %8.3f, %8.3f, %8.3f\n", vPoints[0].FitPlane.c_str(), vPoints[0].x, vPoints[0].y, vPoints[0].z);
114  printf("%s: %8.3f, %8.3f, %8.3f\n", vPoints[1].FitPlane.c_str(), vPoints[1].x, vPoints[1].y, vPoints[1].z);
115  printf("%s: %8.3f, %8.3f, %8.3f\n", vPoints[2].FitPlane.c_str(), vPoints[2].x, vPoints[2].y, vPoints[2].z);
116  }
117 
118 
121  if (VectorUtilities::areAllElementsEqual(vResults) != true)
122  return false;
123 
124 
126 
131  if (fabs(vPoints.at(0).x - vPoints.at(2).x) < INTERSECTION_MAX_TOLERANCE &&
132  fabs(vPoints.at(1).y - vPoints.at(2).y) < INTERSECTION_MAX_TOLERANCE &&
133  fabs(vPoints.at(0).z - vPoints.at(1).z) < INTERSECTION_MAX_TOLERANCE)
134  {
135  if (debugMode)
136  {
137  printf("Checked for following conditions:\n");
138  printf("%s: fabs(%8.3f - %8.3f) < %f cm: TRUE\n", vPoints[0].FitPlane.c_str(), vPoints.at(0).x, vPoints.at(2).x, INTERSECTION_MAX_TOLERANCE);
139  printf("%s: fabs(%8.3f - %8.3f) < %f cm: TRUE\n", vPoints[1].FitPlane.c_str(), vPoints.at(1).y, vPoints.at(2).y, INTERSECTION_MAX_TOLERANCE);
140  printf("%s: fabs(%8.3f - %8.3f) < %f cm: TRUE\n", vPoints[2].FitPlane.c_str(), vPoints.at(0).z, vPoints.at(1).z, INTERSECTION_MAX_TOLERANCE);
141 
142 
144  if (vPoints.at(0).z > 145 && vPoints.at(0).z < 150)
145  printf("\n\n\nFound interaction just above TRD: %8.3f cm\n", vPoints.at(0).z);
146  }
147 
148 
152  if (std::min(vPoints.at(0).z, vPoints.at(1).z) > TRD_ZFITCUTOFF_MAX)
153  {
156  return false;
157  }
158 
159  else
160  return true;
161  }
162 
163  else
164  return false;
165  }
166 
167 
168 
169 
170  //____________________________________________________________________________
171  int GetNIntersectingLinePairs(const std::vector<std::vector<Line::FitLine2D>>& vvFittedTracks, const bool& debugMode = false)
172  {
173  int count = 0;
174  for (unsigned int i = 0; i < vvFittedTracks.size()-1; ++i)
175  {
176  for (unsigned int j = i + 1; j < vvFittedTracks.size(); ++j)
177  {
178  if (debugMode)
179  {
180  printf("\n\nChecking for fitted line pair (%d, %d), g3PID's (%d, %d), trkID's (%d, %d):\n",
181  i, j, vvFittedTracks[i][0].g3PID, vvFittedTracks[j][0].g3PID, vvFittedTracks[i][0].trkID, vvFittedTracks[j][0].trkID);
182  }
183 
184  if (areLinesCloseEnough(vvFittedTracks[i], vvFittedTracks[j], debugMode))
185  {
186  if (debugMode)
187  printf("----> Intersecting!\n");
188  count++;
189  }
190 
191  else
192  {
193  if (debugMode)
194  {
195  printf("!!!!!!!!!!!!!!!!!!!!!!!\n");
196  printf("----> Not intersecting!\n");
197  printf("!!!!!!!!!!!!!!!!!!!!!!!\n");
198  }
199  }
200  }
201  }
202  return count;
203  }
204 }
const std::vector< std::string > vFitPlanes
Strings for fit planes and components.
Definition: TRDConstants.h:23
const double TRD_ZFITCUTOFF_MAX
for intersection cutoffs
Definition: TRDConstants.h:14
const double INTERSECTION_MAX_TOLERANCE
3D track distance for asserting intersection
Definition: TRDConstants.h:42
const std::map< int, std::pair< std::string, std::string > > components
Definition: TRDConstants.h:30
Definition: Intersection3D.h:16
int GetNIntersectingLinePairs(const std::vector< std::vector< Line::FitLine2D >> &vvFittedTracks, const bool &debugMode=false)
Definition: Intersection3D.h:171
Intersection2DResult findIntersection2D(const Line::FitLine2D &lineA, const Line::FitLine2D &lineB, const bool &debugMode=false)
Definition: Intersection3D.h:40
bool areLinesCloseEnough(const std::vector< Line::FitLine2D > &linesA, const std::vector< Line::FitLine2D > &linesB, const bool &debugMode=false)
Definition: Intersection3D.h:72
bool areAllElementsEqual(const std::vector< T > &vec)
Definition: Utilities.h:113
Definition: Intersection3D.h:30
bool intersects
Definition: Intersection3D.h:31
Point::Point2D point
Definition: Intersection3D.h:32
Intersection2DResult()
Definition: Intersection3D.h:34
Intersection2DResult(bool intersects, Point::Point2D point)
Definition: Intersection3D.h:35
Definition: Intersection3D.h:19
double z
Definition: Intersection3D.h:20
double x
Definition: Intersection3D.h:20
std::string FitPlane
Definition: Intersection3D.h:21
IntersectionPoint3D(const std::string &str, double xVal, double yVal, double zVal)
Definition: Intersection3D.h:24
double y
Definition: Intersection3D.h:20
Definition: Line.h:16
double c
Definition: Line.h:18
double err_m
Definition: Line.h:19
double m
Definition: Line.h:17
double err_c
Definition: Line.h:20
Definition: Point.h:9
double y
Definition: Point.h:10
double x
Definition: Point.h:10