Skip to content

Commit 5d3f3f1

Browse files
committed
ITS: mathutils protect against degeneracy
Signed-off-by: Felix Schlepper <felix.schlepper@cern.ch>
1 parent 31ac10a commit 5d3f3f1

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

Detectors/ITSMFT/ITS/tracking/include/ITStracking/Constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ constexpr float GB = 1024.f * 1024.f * 1024.f;
2525
constexpr bool DoTimeBenchmarks = true;
2626
constexpr bool SaveTimeBenchmarks = false;
2727

28+
constexpr float Tolerance{1e-12}; // numerical tolerance
2829
constexpr int ClustersPerCell{3};
2930
constexpr int UnusedIndex{-1};
3031
constexpr float Resolution{0.0005f};

Detectors/ITSMFT/ITS/tracking/include/ITStracking/MathUtils.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define O2_ITS_TRACKING_MATHUTILS_H_
1818

1919
#include "CommonConstants/MathConstants.h"
20+
#include "ITStracking/Constants.h"
2021
#include "MathUtils/Utils.h"
2122
#include "GPUCommonMath.h"
2223
#include "GPUCommonDef.h"
@@ -42,33 +43,47 @@ GPUhdi() constexpr float getNormalizedPhi(float phi)
4243

4344
GPUhdi() float computeCurvature(float x1, float y1, float x2, float y2, float x3, float y3)
4445
{
46+
// in case the triangle is degenerate we return infinite curvature.
4547
const float d = (x2 - x1) * (y3 - y2) - (x3 - x2) * (y2 - y1);
46-
if (o2::gpu::CAMath::Abs(d) < o2::constants::math::Almost0) {
48+
if (o2::gpu::CAMath::Abs(d) < o2::its::constants::Tolerance) {
4749
return 0.f;
4850
}
4951
const float a =
5052
0.5f * ((y3 - y2) * (y2 * y2 - y1 * y1 + x2 * x2 - x1 * x1) - (y2 - y1) * (y3 * y3 - y2 * y2 + x3 * x3 - x2 * x2));
5153
const float b =
5254
0.5f * ((x2 - x1) * (y3 * y3 - y2 * y2 + x3 * x3 - x2 * x2) - (x3 - x2) * (y2 * y2 - y1 * y1 + x2 * x2 - x1 * x1));
5355
const float den = o2::gpu::CAMath::Hypot(d * x1 - a, d * y1 - b);
56+
if (den < o2::its::constants::Tolerance) {
57+
return 0.f;
58+
}
5459
return -d / den;
5560
}
5661

5762
GPUhdi() float computeCurvatureCentreX(float x1, float y1, float x2, float y2, float x3, float y3)
5863
{
64+
// in case the triangle is degenerate we return set the centre to infinity.
5965
float dx21 = x2 - x1, dx32 = x3 - x2;
60-
if (dx21 == 0.f || dx32 == 0.f) { // add small offset
66+
if (o2::gpu::CAMath::Abs(dx21) < o2::its::constants::Tolerance ||
67+
o2::gpu::CAMath::Abs(dx32) < o2::its::constants::Tolerance) { // add small offset
6168
x2 += 1e-4;
6269
dx21 = x2 - x1;
6370
dx32 = x3 - x2;
6471
}
65-
float k1 = (y2 - y1) / dx21, k2 = (y3 - y2) / dx32;
66-
return (k1 != k2) ? 0.5f * (k1 * k2 * (y1 - y3) + k2 * (x1 + x2) - k1 * (x2 + x3)) / (k2 - k1) : 1e5f;
72+
const float k1 = (y2 - y1) / dx21, k2 = (y3 - y2) / dx32;
73+
if (o2::gpu::CAMath::Abs(k2 - k1) < o2::its::constants::Tolerance) {
74+
return o2::constants::math::VeryBig;
75+
}
76+
return 0.5f * (k1 * k2 * (y1 - y3) + k2 * (x1 + x2) - k1 * (x2 + x3)) / (k2 - k1);
6777
}
6878

6979
GPUhdi() float computeTanDipAngle(float x1, float y1, float x2, float y2, float z1, float z2)
7080
{
71-
return (z1 - z2) / o2::gpu::CAMath::Hypot(x1 - x2, y1 - y2);
81+
// in case the points vertically align we go to pos/neg inifinity.
82+
const float d = o2::gpu::CAMath::Hypot(x1 - x2, y1 - y2);
83+
if (o2::gpu::CAMath::Abs(d) < o2::its::constants::Tolerance) {
84+
return ((z1 > z2) ? -1.f : 1.f) * o2::constants::math::VeryBig;
85+
}
86+
return (z1 - z2) / d;
7287
}
7388

7489
GPUhdi() float smallestAngleDifference(float a, float b)

0 commit comments

Comments
 (0)