Belle II Software development
LineHelper.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <cmath>
12#include <vector>
13
14namespace Belle2 {
29 class DedxPoint {
30
31 public:
32
34 DedxPoint() : m_x(0), m_y(0), m_valid(true) {}
35
37 DedxPoint(double x, double y) : m_x(x), m_y(y), m_valid(true) {}
38
40 double getX() const { return m_x; }
41
43 double getY() const { return m_y; }
44
46 void setPoint(double x, double y)
47 {
48 m_x = x;
49 m_y = y;
50 }
51
53 double length(const DedxPoint& p)
54 {
55 // horizontal line
56 if (m_y == p.getY()) return std::abs(m_x - p.getX());
57 // vertical line
58 else if (m_x == p.getX()) return std::abs(m_y - p.getY());
59 // else use Pythagorean Theorem
60 else return std::sqrt((m_x - p.getX()) * (m_x - p.getX()) +
61 (m_y - p.getY()) * (m_y - p.getY()));
62 }
63
65 bool isValid() const { return m_valid; }
66
68 void setInvalid() { m_valid = false; }
69
70 private:
71
72 double m_x;
73 double m_y;
74 bool m_valid;
76 };
77
83 class DedxLine {
84
85 public:
86
92 DedxLine(): m_m(0), m_vertical(false) {}
93
95 DedxLine(const DedxPoint& p, const double slope) : m_p1(p), m_p2(p), m_m(slope), m_vertical(false) {}
96
98 DedxLine(const DedxPoint& p1, const DedxPoint& p2) : m_p1(p1), m_p2(p2), m_vertical(false)
99 {
100 //check if the line is vertical (set vertical flag if it is)
101 if (p1.getX() == p2.getX()) {
102 m_m = -10000.0;
103 m_vertical = true;
104 } else
105 m_m = (p1.getY() - p2.getY()) / (p1.getX() - p2.getX());
106 }
107
109 double getSlope() const { return m_m; }
110
113 {
114 double x, y;
115 DedxPoint intP;
116
117 // First check if the lines will intersect.
118 // Also check for special cases of horizontal or
119 // vertical lines.
120 if (m_m == l.m_m) {
121 B2WARNING("Lines are parallel, will not intersect...");
122 intP.setInvalid();
123 intP.setPoint(0, 0);
124 return intP;
125 } else if (m_m == 0.0) {
126 y = m_p1.getY();
127 x = (y - l.m_p1.getY()) / l.m_m + l.m_p1.getX();
128 } else if (l.m_m == 0.0) {
129 y = l.m_p1.getY();
130 x = (y - m_p1.getY()) / m_m + m_p1.getX();
131 } else if (m_vertical) {
132 x = m_p1.getX();
133 y = l.m_p1.getY() + l.m_m * (x - l.m_p1.getX());
134 } else if (l.isVertical()) {
135 x = l.m_p1.getX();
136 y = m_p1.getY() + m_m * (x - m_p1.getX());
137 } else {
138 x = ((l.m_p1.getY() - l.m_m * l.m_p1.getX()) - (m_p1.getY() - m_m * m_p1.getX())) / (m_m - l.m_m);
139 y = m_p1.getY() + m_m * (x - m_p1.getX());
140 }
141
142 // This is the point of intersection
143 intP.setPoint(x, y);
144
145 // Check if the point falls outside the endpoints of the line.
146 // This is useful for calculating the track length (dx).
147 if ((x > m_p1.getX() && x > m_p2.getX()) ||
148 (x < m_p1.getX() && x < m_p2.getX()))
149 intP.setInvalid();
150 if ((y > m_p1.getY() && y > m_p2.getY()) ||
151 (y < m_p1.getY() && y < m_p2.getY()))
152 intP.setInvalid();
153
154 // Check if the DedxLine was made from a point and make sure the
155 // point is not outside the cell
156 if (l.m_p1.getX() == l.m_p2.getX() && l.m_p1.getY() == l.m_p2.getY() &&
157 ((l.m_p1.getX() > m_p1.getX() && l.m_p1.getX() > m_p2.getX()) ||
158 (l.m_p1.getX() < m_p1.getX() && l.m_p1.getX() < m_p2.getX())) &&
159 ((l.m_p1.getY() > m_p1.getY() && l.m_p1.getY() > m_p2.getY()) ||
160 (l.m_p1.getY() < m_p1.getY() && l.m_p1.getY() < m_p2.getY())))
161 intP.setInvalid();
162
163 return intP;
164 }
165
167 bool isVertical() const { return m_vertical; }
168
170 void setVertical() { m_vertical = true; }
171
172 private:
173
176 double m_m;
179 };
180
187
188 public:
189
191 DedxDriftCell(const DedxLine& left, const DedxLine& top, const DedxLine& right, const DedxLine& bot) : m_Left(left), m_Top(top),
192 m_Right(right), m_Bot(bot), m_isValid(true) {}
193
195 DedxDriftCell(const DedxPoint& tl, const DedxPoint& tr, const DedxPoint& br, const DedxPoint& bl) : m_Left(bl, tl), m_Top(tl, tr),
196 m_Right(tr, br), m_Bot(br, bl), m_isValid(true) {}
197
199 bool isValid() { return m_isValid; }
200
203 double dx(const DedxPoint& poca, double entAng)
204 {
205 // The path length (dx) is the length of the track in this cell.
206 double Dx = 0;
207
208 // The DedxPoint Of Closest Approach (poca) is useful for a reference point
209 // to construct a line that represents the track.
210 DedxLine track = DedxLine(poca, std::tan(M_PI_2 - entAng));
211
212 // Find the points of intersection with each cell boundary
213 DedxPoint intLeft = m_Left.intersection(track);
214 DedxPoint intTop = m_Top.intersection(track);
215 DedxPoint intRight = m_Right.intersection(track);
216 DedxPoint intBot = m_Bot.intersection(track);
217
218 std::vector<DedxPoint> endpoints;
219 if (intLeft.isValid())
220 endpoints.push_back(intLeft);
221 if (intTop.isValid())
222 endpoints.push_back(intTop);
223 if (intRight.isValid())
224 endpoints.push_back(intRight);
225 if (intBot.isValid())
226 endpoints.push_back(intBot);
227
228 // Make sure we only get two intersections!
229 if (endpoints.size() == 2) {
230 Dx = endpoints[0].length(endpoints[1]);
231 if (Dx == 0)
232 m_isValid = false;
233 } else
234 m_isValid = false;
235
236 return Dx;
237 }
238
241 double dx(double doca, double entAng)
242 {
243 // The path length (dx) is the length of the track in this cell.
244 double Dx = 0;
245
246 // The DedxPoint Of Closest Approach (poca) is useful for a reference point
247 // to construct a line that represents the track.
248 const DedxPoint poca = DedxPoint(doca * std::abs(std::cos(entAng)), -1.0 * doca * std::sin(entAng));
249 DedxLine track = DedxLine(poca, std::tan(M_PI_2 - entAng));
250
251 // Find the points of intersection with each cell boundary
252 DedxPoint intLeft = m_Left.intersection(track);
253 DedxPoint intTop = m_Top.intersection(track);
254 DedxPoint intRight = m_Right.intersection(track);
255 DedxPoint intBot = m_Bot.intersection(track);
256
257 std::vector< DedxPoint > endpoints;
258 if (intLeft.isValid())
259 endpoints.push_back(intLeft);
260 if (intTop.isValid())
261 endpoints.push_back(intTop);
262 if (intRight.isValid())
263 endpoints.push_back(intRight);
264 if (intBot.isValid())
265 endpoints.push_back(intBot);
266
267 // Make sure we only get two intersections!
268 if (endpoints.size() == 2) {
269 Dx = endpoints[0].length(endpoints[1]);
270 if (Dx == 0)
271 m_isValid = false;
272 } else
273 m_isValid = false;
274
275 return Dx;
276 }
277
278 private:
279
285 };
287} // Belle2 namespace
A class to hold the geometry of a cell.
Definition: LineHelper.h:186
DedxLine m_Right
the left boundary of the cell
Definition: LineHelper.h:282
DedxLine m_Top
the left boundary of the cell
Definition: LineHelper.h:281
double dx(const DedxPoint &poca, double entAng)
Calculate the path length through this cell for a track with a given DedxPoint Of Closest Approach (p...
Definition: LineHelper.h:203
DedxDriftCell(const DedxPoint &tl, const DedxPoint &tr, const DedxPoint &br, const DedxPoint &bl)
Construct a DedxDriftCell from four different DedxPoints (corners)
Definition: LineHelper.h:195
DedxLine m_Left
the left boundary of the cell
Definition: LineHelper.h:280
double dx(double doca, double entAng)
Calculate the path length through this cell for a track with a given Distance Of Closest Approach (do...
Definition: LineHelper.h:241
DedxDriftCell(const DedxLine &left, const DedxLine &top, const DedxLine &right, const DedxLine &bot)
Construct a DedxDriftCell from four different DedxLines (sides)
Definition: LineHelper.h:191
DedxLine m_Bot
the left boundary of the cell
Definition: LineHelper.h:283
bool m_isValid
does the hit land in this cell
Definition: LineHelper.h:284
bool isValid()
Check if this is a valid calculation (number of intersections = 2)
Definition: LineHelper.h:199
A class to hold the endpoints and slope of a line.
Definition: LineHelper.h:83
int m_vertical
If the line is vertical, this is set to true.
Definition: LineHelper.h:177
DedxPoint m_p2
Second endpoint.
Definition: LineHelper.h:175
DedxLine(const DedxPoint &p1, const DedxPoint &p2)
Construct a DedxLine from two DedxPoints.
Definition: LineHelper.h:98
void setVertical()
Mark this DedxLine as being vertical.
Definition: LineHelper.h:170
DedxPoint intersection(const DedxLine &l)
Find the intersection of this and another line.
Definition: LineHelper.h:112
double getSlope() const
Helper function to return the slope.
Definition: LineHelper.h:109
bool isVertical() const
The m_vertical flag is used for the special case of a vertical line.
Definition: LineHelper.h:167
DedxLine(const DedxPoint &p, const double slope)
Construct a DedxLine from the given DedxPoint and slope.
Definition: LineHelper.h:95
DedxLine()
A line is definied either by a point and a slope, or by endpoints.
Definition: LineHelper.h:92
double m_m
Slope of the line.
Definition: LineHelper.h:176
DedxPoint m_p1
First endpoint.
Definition: LineHelper.h:174
A collection of classes that are useful for making a simple path length correction to the dE/dx measu...
Definition: LineHelper.h:29
void setInvalid()
Mark this point as invalid (exists outside a DedxDriftCell)
Definition: LineHelper.h:68
double getX() const
Helper function to return the x-coordinates of a DedxPoint.
Definition: LineHelper.h:40
bool isValid() const
Check whether this point lies within the endpoints of a line.
Definition: LineHelper.h:65
double m_x
the x-coordinate of the DedxPoint
Definition: LineHelper.h:72
DedxPoint()
The default constructor sets the coordinates to zero.
Definition: LineHelper.h:34
bool m_valid
used to check if the point lies inside a boundary
Definition: LineHelper.h:74
void setPoint(double x, double y)
Set the x and y coordinates of a DedxPoint.
Definition: LineHelper.h:46
double getY() const
Helper function to return the y-coordinates of a DedxPoint.
Definition: LineHelper.h:43
double length(const DedxPoint &p)
Calculate the distance between this and another point.
Definition: LineHelper.h:53
double m_y
the y-coordinate of the DedxPoint
Definition: LineHelper.h:73
DedxPoint(double x, double y)
Construct a point DedxPoint object from x and y coordinates.
Definition: LineHelper.h:37
Abstract base class for different kinds of events.