Skip to content

Commit ffcef87

Browse files
committed
Change error handling to exceptions
Instead of warnings and boolean return values use exception in case of out of range errors. Two types of exceptions are implemented: - InvalidPixelException dealing with pixels out of range - OutOfActiveAreaException dealing with local (chip) coordinates exceeding the active area of the chip Functions to which these cases might apply are updaged in the way that they throw exceptions in case of errors and do not return any value.
1 parent a607bee commit ffcef87

3 files changed

Lines changed: 130 additions & 16 deletions

File tree

its/Segmentation.h

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#ifndef ALICEO2_ITS_SEGMENTATION_H_
55
#define ALICEO2_ITS_SEGMENTATION_H_
66

7+
#include <exception>
8+
#include <sstream>
9+
710
#include <TObject.h>
811

912
class TF1;
@@ -18,6 +21,116 @@ namespace ITS {
1821
class Segmentation : public TObject {
1922

2023
public:
24+
/// Error handling in case a point in local coordinates
25+
/// exceeds limits in any direction
26+
class OutOfActiveAreaException : public std::exception {
27+
public:
28+
/// Definition of direction in which the boundary is exceeded
29+
enum Direction {
30+
kX = 0, ///< Local X
31+
kZ = 1 ///< Local Z
32+
};
33+
/// Constructor
34+
/// Settting necessary information for the error handling
35+
/// @param dir Direction in which the range exception happened
36+
/// @param val Value of the exception
37+
/// @param lower Lower limit in the direction
38+
/// @param upper Upper limit in the direction
39+
OutOfActiveAreaException(Direction dir, Double_t val, Double_t lower, Double_t upper) :
40+
fErrorMessage(), fDirection(dir), fValue(val), fLower(lower), fUpper(upper)
41+
{
42+
std::stringstream errormessage;
43+
errormessage << "Range exceeded in " << (fDirection == kX ? "x" : "z") << "-direction, value " << fValue << ", limits [" << fLower << "|" << fUpper << "]";
44+
fErrorMessage = errormessage.str();
45+
}
46+
/// Destructor
47+
virtual ~OutOfActiveAreaException() throw() {}
48+
49+
/// Get the value for which the exception was raised
50+
/// @return Value (point in one direction)
51+
Double_t GetValue() const { return fValue; }
52+
/// Get the lower limit in direction for which the exception
53+
/// was raised
54+
/// @return Lower limit of the direction
55+
Double_t GetLowerLimit() const { return fLower; }
56+
/// Get the upper limit in direction for which the exception
57+
/// was raised
58+
/// @return Upper limit of the direction
59+
Double_t GetUpperLimit() const { return fUpper; }
60+
/// Check whether exception was raised in x-directon
61+
/// @return True if exception was raised in x-direction, false otherwise
62+
Bool_t IsX() const { return fDirection == kX; }
63+
/// Check whether exception was raised in z-direction
64+
/// @return True if exception was raised in z-direction, false otherwise
65+
Bool_t IsZ() const { return fDirection == kZ; }
66+
67+
/// Provide error message string containing direction,
68+
/// value of the point, and limits
69+
/// @return Error message
70+
const char *what() const noexcept {
71+
return fErrorMessage.c_str();
72+
}
73+
74+
private:
75+
std::string fErrorMessage; ///< Error message connected to the exception
76+
Direction fDirection; ///< Direction in which the exception was raised
77+
Double_t fValue; ///< Value which exceeds limit
78+
Double_t fLower; ///< Lower limit in direction
79+
Double_t fUpper; ///< Upper limit in direction
80+
};
81+
82+
/// Error handling in case of access to an invalid pixel ID
83+
/// (pixel ID in direction which exceeds the range of valid pixel IDs)
84+
class InvalidPixelException : public std::exception{
85+
public:
86+
/// Definition of direction in which the boundary is exceeded
87+
enum Direction {
88+
kX = 0, ///< Local X
89+
kZ = 1 ///< Local Z
90+
};
91+
/// Constructor
92+
/// Setting necessary information for the error handling
93+
/// @param dir Direction in which the exception occurs
94+
/// @param pixelID Index of the pixel (in direction) which is out of scope
95+
/// @param maxPixelID Maximum amount of pixels in direction
96+
InvalidPixelException(Direction dir, Int_t pixelID, Int_t maxPixelID):
97+
fErrorMessage(), fDirection(dir), fValue(pixelID), fMaxPixelID(maxPixelID)
98+
{
99+
std::stringstream errormessage;
100+
errormessage << "Obtained " << (fDirection == kX ? "row" : "col") << " " << fValue << " is not in range [0:" << fMaxPixelID << ")";
101+
fErrorMessage = errormessage.str();
102+
}
103+
104+
/// Destructor
105+
virtual ~InvalidPixelException() {}
106+
107+
/// Get the ID of the pixel which raised the exception
108+
/// @return ID of the pixel
109+
Int_t GetPixelID() const { return fValue; }
110+
/// Get the maximum number of pixels in a given direction
111+
/// @return Max. number of pixels
112+
Int_t GetMaxNumberOfPixels() const { return fMaxPixelID; }
113+
/// Check whether exception was raised in x-directon
114+
/// @return True if exception was raised in x-direction, false otherwise
115+
Bool_t IsX() const { return fDirection == kX; }
116+
/// Check whether exception was raised in z-direction
117+
/// @return True if exception was raised in z-direction, false otherwise
118+
Bool_t IsZ() const { return fDirection == kZ; }
119+
120+
/// Provide error message string containing direction,
121+
/// index of the pixel out of range, and the maximum pixel ID
122+
const char *what() const noexcept {
123+
return fErrorMessage.c_str();
124+
}
125+
126+
private:
127+
std::string fErrorMessage; ///< Error message connected to this exception
128+
Direction fDirection; ///< Direction in which the pixel index is out of range
129+
Int_t fValue; ///< Value of the pixel ID which is out of range
130+
Int_t fMaxPixelID; ///< Maximum amount of pixels in direction;
131+
};
132+
133+
21134
/// Default constructor
22135
Segmentation();
23136

@@ -101,10 +214,12 @@ class Segmentation : public TObject {
101214

102215
/// Transformation from Geant cm detector center local coordinates
103216
/// to detector segmentation/cell coordiantes starting from (0,0).
104-
virtual Bool_t localToDetector(Float_t, Float_t, Int_t&, Int_t&) const = 0;
217+
/// @throw OutOfActiveAreaException if the point is outside the active area in any of the directions
218+
virtual void localToDetector(Float_t, Float_t, Int_t&, Int_t&) const = 0;
105219

106220
/// Transformation from detector segmentation/cell coordiantes starting
107221
/// from (0,0) to Geant cm detector center local coordinates.
222+
/// @throw InvalidPixelException in case the pixel ID in any direction is out of range
108223
virtual void detectorToLocal(Int_t, Int_t, Float_t&, Float_t&) const = 0;
109224

110225
/// Initialisation

its/UpgradeSegmentationPixel.cxx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,41 +250,38 @@ void UpgradeSegmentationPixel::neighbours(Int_t iX, Int_t iZ, Int_t* nlist, Int_
250250
zlist[7] = iZ - 1;
251251
}
252252

253-
Bool_t UpgradeSegmentationPixel::localToDetector(Float_t x, Float_t z, Int_t& ix, Int_t& iz) const
253+
void UpgradeSegmentationPixel::localToDetector(Float_t x, Float_t z, Int_t& ix, Int_t& iz) const
254254
{
255255
x += 0.5 * dxActive() + mShiftLocalX; // get X,Z wrt bottom/left corner
256256
z += 0.5 * dzActive() + mShiftLocalZ;
257257
ix = iz = -1;
258258
if (x < 0 || x > dxActive()) {
259-
return kFALSE; // outside x range.
259+
throw OutOfActiveAreaException(OutOfActiveAreaException::kX, x, 0, dxActive());
260+
//return kFALSE; // outside x range.
260261
}
261262
if (z < 0 || z > dzActive()) {
262-
return kFALSE; // outside z range.
263+
throw OutOfActiveAreaException(OutOfActiveAreaException::kZ, z, 0, dzActive());
264+
//return kFALSE; // outside z range.
263265
}
264266
ix = int(x / mPitchX);
265267
iz = zToColumn(z);
266-
return kTRUE; // Found ix and iz, return.
268+
//return kTRUE; // Found ix and iz, return.
267269
}
268270

269271
void UpgradeSegmentationPixel::detectorToLocal(Int_t ix, Int_t iz, Float_t& x, Float_t& z) const
270272
{
271273
x = -0.5 * dxActive(); // default value.
272274
z = -0.5 * dzActive(); // default value.
273275
if (ix < 0 || ix >= mNumberOfRows) {
274-
LOG(WARNING) << "Obtained row " << ix << " is not in range [0:" << mNumberOfRows << ")"
275-
<< FairLogger::endl;
276-
return;
276+
throw InvalidPixelException(InvalidPixelException::kX, ix, mNumberOfRows);
277277
} // outside of detector
278278
if (iz < 0 || iz >= mNumberOfColumns) {
279-
LOG(WARNING) << "Obtained col " << ix << " is not in range [0:" << mNumberOfColumns << ")"
280-
<< FairLogger::endl;
281-
return;
279+
throw InvalidPixelException(InvalidPixelException::kZ, iz, mNumberOfColumns);
282280
} // outside of detector
283281
x +=
284282
(ix + 0.5) * mPitchX - mShiftLocalX; // RS: we go to the center of the pad, i.e. + pitch/2, not
285283
// to the boundary as in SPD
286284
z += columnToZ(iz) - mShiftLocalZ;
287-
return; // Found x and z, return.
288285
}
289286

290287
void UpgradeSegmentationPixel::cellBoundries(Int_t ix, Int_t iz, Double_t& xl, Double_t& xu,
@@ -310,16 +307,17 @@ void UpgradeSegmentationPixel::cellBoundries(Int_t ix, Int_t iz, Double_t& xl, D
310307
Int_t UpgradeSegmentationPixel::getChipFromChannel(Int_t, Int_t iz) const
311308
{
312309
if (iz >= mNumberOfColumns || iz < 0) {
313-
LOG(WARNING) << "Bad cell number" << FairLogger::endl;
314-
return -1;
310+
throw InvalidPixelException(InvalidPixelException::kZ, iz, mNumberOfColumns);
315311
}
316312
return iz / mNumberOfColumnsPerChip;
317313
}
318314

319315
Int_t UpgradeSegmentationPixel::getChipFromLocal(Float_t, Float_t zloc) const
320316
{
321317
Int_t ix0, iz;
322-
if (!localToDetector(0, zloc, ix0, iz)) {
318+
try {
319+
localToDetector(0, zloc, ix0, iz);
320+
} catch (OutOfActiveAreaException &e) {
323321
LOG(WARNING) << "Bad local coordinate" << FairLogger::endl;
324322
return -1;
325323
}

its/UpgradeSegmentationPixel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace ITS {
2020
class UpgradeSegmentationPixel : public Segmentation {
2121

2222
public:
23+
2324
UpgradeSegmentationPixel(UInt_t id = 0, int nchips = 0, int ncol = 0, int nrow = 0, float pitchX = 0,
2425
float pitchZ = 0, float thickness = 0, float pitchLftC = -1, float pitchRgtC = -1,
2526
float edgL = 0, float edgR = 0, float edgT = 0, float edgB = 0);
@@ -64,7 +65,7 @@ class UpgradeSegmentationPixel : public Segmentation {
6465
/// the center of the sensitive volulme.
6566
/// \param Int_t ix Detector x cell coordinate. Has the range 0 <= ix < mNumberOfRows
6667
/// \param Int_t iz Detector z cell coordinate. Has the range 0 <= iz < mNumberOfColumns
67-
virtual Bool_t localToDetector(Float_t x, Float_t z, Int_t& ix, Int_t& iz) const;
68+
virtual void localToDetector(Float_t x, Float_t z, Int_t& ix, Int_t& iz) const;
6869

6970
/// Transformation from Detector cell coordiantes to Geant detector centered
7071
/// local coordinates (cm)

0 commit comments

Comments
 (0)