Skip to content

Commit cd4c4ca

Browse files
committed
add status codes
1 parent da7b0df commit cd4c4ca

File tree

4 files changed

+167
-46
lines changed

4 files changed

+167
-46
lines changed

compile/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Minimal C bindings for JetReconstruction.jl
44

5-
- [C-header](JetReconstruction.h)
5+
- [C-header](include/JetReconstruction.h)
66
- shared library compiled with [PackageCompiler.jl](https://github.com/JuliaLang/PackageCompiler.jl)
77

88
## Building library
@@ -37,10 +37,14 @@ int main(int argc, char *argv[]) {
3737
jetreconstruction_RecoStrategy strategy = JETRECONSTRUCTION_RECOSTRATEGY_BEST;
3838

3939
jetreconstruction_ClusterSequence cluster_seq;
40-
jetreconstruction_jet_reconstruct(particles, len, algorithm, R, strategy,
41-
&cluster_seq);
42-
43-
/*Use the cluster sequence in your application
40+
int ret = jetreconstruction_jet_reconstruct(particles, len, algorithm, R, strategy,
41+
&cluster_seq);
42+
if (ret != JETRECONSTRUCTION_STATUSCODE_OK){
43+
/*An error occurred check the value or stderr for more information*/
44+
return 1;
45+
}
46+
47+
/*Use the cluster sequence in your application
4448
then free memory allocations done by library*/
4549
jetreconstruction_ClusterSequence_free_members(&cluster_seq);
4650
shutdown_julia(0); /*teardown of julia runtime*/

compile/downstream/jetreconstruction_test.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,28 @@ void printJetsResult(const jetreconstruction_JetsResult *results) {
5252

5353
int main(int argc, char *argv[]) {
5454
clock_t start_time = clock();
55+
int sc = 0;
5556
init_julia(argc, argv);
5657
size_t len = 2;
5758
jetreconstruction_PseudoJet particles[2];
58-
jetreconstruction_PseudoJet_init(&particles[0], 0.0, 1.0, 2.0, 3.0);
59-
jetreconstruction_PseudoJet_init(&particles[1], 1.0, 2.0, 3.0, 4.0);
59+
sc = jetreconstruction_PseudoJet_init(&particles[0], 0.0, 1.0, 2.0, 3.0);
60+
assert(sc == JETRECONSTRUCTION_STATUSCODE_OK);
61+
sc = jetreconstruction_PseudoJet_init(&particles[1], 1.0, 2.0, 3.0, 4.0);
62+
assert(sc == JETRECONSTRUCTION_STATUSCODE_OK);
6063

6164
jetreconstruction_JetAlgorithm algorithm = JETRECONSTRUCTION_JETALGORITHM_CA;
6265
double R = 3.0;
6366
jetreconstruction_RecoStrategy strategy = JETRECONSTRUCTION_RECOSTRATEGY_BEST;
6467

6568
jetreconstruction_ClusterSequence cluster_seq;
66-
jetreconstruction_jet_reconstruct(particles, len, algorithm, R, strategy,
67-
&cluster_seq);
69+
sc = jetreconstruction_jet_reconstruct(particles, len, algorithm, R, strategy,
70+
&cluster_seq);
71+
assert(sc == JETRECONSTRUCTION_STATUSCODE_OK);
6872

6973
printClusterSequence(&cluster_seq);
7074
jetreconstruction_JetsResult result;
71-
jetreconstruction_exclusive_jets_njets(&cluster_seq, 2, &result);
75+
sc = jetreconstruction_exclusive_jets_njets(&cluster_seq, 2, &result);
76+
assert(sc == JETRECONSTRUCTION_STATUSCODE_OK);
7277
printJetsResult(&result);
7378

7479
jetreconstruction_JetsResult_free_members(&result);

compile/include/JetReconstruction.h

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,47 @@
2424
/**Special history state: Cluster recombined with beam" */
2525
#define JETRECONSTRUCTION_BEAMJET -1
2626

27+
/**
28+
* @enum jetreconstruction_StatusCode
29+
* @brief Status codes for Jet Reconstruction operations.
30+
*
31+
* Common status codes for Jet Reconstruction operations. Most of the standard
32+
* Julia
33+
* https://docs.julialang.org/en/v1/manual/control-flow/#Exception-Handling
34+
* exceptions are mapped to corresponding codes.
35+
*/
36+
typedef enum {
37+
JETRECONSTRUCTION_STATUSCODE_OK = 0, /**< The operation succeeded. */
38+
JETRECONSTRUCTION_STATUSCODE_GENERICEXCEPTION =
39+
1, /**< An unspecified error, not covered by other status codes occurred.
40+
*/
41+
JETRECONSTRUCTION_STATUSCODE_ARGUMENTERROR = 2,
42+
JETRECONSTRUCTION_STATUSCODE_BOUNDSERROR = 3,
43+
JETRECONSTRUCTION_STATUSCODE_COMPOSITEEXCEPTION = 4,
44+
JETRECONSTRUCTION_STATUSCODE_DIMENSIONMISMATCH = 5,
45+
JETRECONSTRUCTION_STATUSCODE_DIVIDEERROR = 6,
46+
JETRECONSTRUCTION_STATUSCODE_DOMAINERROR = 7,
47+
JETRECONSTRUCTION_STATUSCODE_EOFERROR = 8,
48+
JETRECONSTRUCTION_STATUSCODE_ERROREXCEPTION = 9,
49+
JETRECONSTRUCTION_STATUSCODE_INEXACTERROR = 10,
50+
JETRECONSTRUCTION_STATUSCODE_INITERROR = 11,
51+
JETRECONSTRUCTION_STATUSCODE_INTERRUPTEXCEPTION = 12,
52+
JETRECONSTRUCTION_STATUSCODE_INVALIDSTATEEXCEPTION = 13,
53+
JETRECONSTRUCTION_STATUSCODE_KEYERROR = 14,
54+
JETRECONSTRUCTION_STATUSCODE_LOADERROR = 15,
55+
JETRECONSTRUCTION_STATUSCODE_OUTOFMEMORYERROR = 16,
56+
JETRECONSTRUCTION_STATUSCODE_READONLYMEMORYERROR = 17,
57+
/*JETRECONSTRUCTION_STATUSCODE_REMOTEEXCEPTION = 18, distributed only */
58+
JETRECONSTRUCTION_STATUSCODE_METHODERROR = 19,
59+
JETRECONSTRUCTION_STATUSCODE_OVERFLOWERROR = 20,
60+
/*JETRECONSTRUCTION_STATUSCODE_PARSEERROR = 21, meta only*/
61+
JETRECONSTRUCTION_STATUSCODE_SYSTEMERROR = 22,
62+
JETRECONSTRUCTION_STATUSCODE_TYPEERROR = 23,
63+
JETRECONSTRUCTION_STATUSCODE_UNDEFREFERROR = 24,
64+
JETRECONSTRUCTION_STATUSCODE_UNDEFVARERROR = 25,
65+
JETRECONSTRUCTION_STATUSCODE_STRINGINDEXERROR = 26
66+
} jetreconstruction_StatusCode;
67+
2768
/**
2869
* @enum jetreconstruction_JetAlgorithm
2970
* @brief Enumeration representing different jet algorithms used in
@@ -81,23 +122,25 @@ typedef struct {
81122
* @param[in] py The y-component of the momentum.
82123
* @param[in] pz The z-component of the momentum.
83124
* @param[in] E The energy component of the momentum.
84-
* @return An integer status code indicating the success or failure.
125+
* @return An integer status code indicating the success or failure. See common
126+
* status codes jetreconstruction_StatusCode.
85127
*/
86-
int jetreconstruction_PseudoJet_init(jetreconstruction_PseudoJet *ptr,
87-
double px, double py, double pz, double E);
128+
jetreconstruction_StatusCode
129+
jetreconstruction_PseudoJet_init(jetreconstruction_PseudoJet *ptr, double px,
130+
double py, double pz, double E);
88131

89132
/**
90133
* @struct jetreconstruction_HistoryElement
91134
* @brief A struct holding a record of jet mergers and finalisations.
92135
*/
93136
typedef struct {
94137
long parent1; /**< Index in history where first parent of this jet was
95-
created (@ref JETRECONSTRUCTION_NONEXISTENTPARENT if this jet is
96-
an original particle) */
138+
created (@ref JETRECONSTRUCTION_NONEXISTENTPARENT if this jet
139+
is an original particle) */
97140
long parent2; /**< Index in history where second parent of this jet was
98-
created (@ref JETRECONSTRUCTION_NONEXISTENTPARENT if this jet is
99-
an original particle); @ref JETRECONSTRUCTION_BEAMJET if this
100-
history entry just labels the fact that the jet has
141+
created (@ref JETRECONSTRUCTION_NONEXISTENTPARENT if this jet
142+
is an original particle); @ref JETRECONSTRUCTION_BEAMJET if
143+
this history entry just labels the fact that the jet has
101144
recombined with the beam */
102145
long child; /**< Index in history where the current jet is recombined with
103146
another jet to form its child. It is Invalid
@@ -106,7 +149,8 @@ typedef struct {
106149
PseudoJet object corresponding to this jet (i.e. the
107150
jet created at this entry of the history). NB: if this
108151
element of the history corresponds to a beam
109-
recombination, then @p jetp_index = @ref JETRECONSTRUCTION_INVALID.
152+
recombination, then @p jetp_index = @ref
153+
JETRECONSTRUCTION_INVALID.
110154
*/
111155
double dij; /**< The distance corresponding to the recombination at this
112156
stage of the clustering. */
@@ -182,9 +226,10 @@ static inline void jetreconstruction_ClusterSequence_free_members(
182226
* @param[in] strategy The jet reconstruction strategy to use.
183227
* @param[out] result A pointer to which a cluster sequence containing the
184228
* reconstructed jets and the merging history will be stored.
185-
* @return An integer status code indicating the success or failure.
229+
* @return An integer status code indicating the success or failure. See common
230+
* status codes jetreconstruction_StatusCode.
186231
*/
187-
int jetreconstruction_jet_reconstruct(
232+
jetreconstruction_StatusCode jetreconstruction_jet_reconstruct(
188233
const jetreconstruction_PseudoJet *particles, size_t particles_length,
189234
jetreconstruction_JetAlgorithm algorithm, double R,
190235
jetreconstruction_RecoStrategy strategy,
@@ -195,8 +240,9 @@ int jetreconstruction_jet_reconstruct(
195240
* @brief A structure to hold the inclusive or exclusive jets.
196241
*/
197242
typedef struct {
198-
jetreconstruction_PseudoJet *data; /**< Pointer to an array of jetreconstruction_PseudoJet. */
199-
size_t length; /**< The length of the @ref data array. */
243+
jetreconstruction_PseudoJet
244+
*data; /**< Pointer to an array of jetreconstruction_PseudoJet. */
245+
size_t length; /**< The length of the @ref data array. */
200246
} jetreconstruction_JetsResult;
201247

202248
/** @private */
@@ -235,9 +281,10 @@ jetreconstruction_JetsResult_free_members(jetreconstruction_JetsResult *ptr) {
235281
* @param[in] dcut The distance parameter used to define the exclusive jets.
236282
* @param[out] result A pointer to the jetreconstruction_JetsResult object where
237283
* the resulting jets will be stored.
238-
* @return An integer status code indicating the success or failure.
284+
* @return An integer status code indicating the success or failure. See common
285+
* status codes jetreconstruction_StatusCode.
239286
*/
240-
int jetreconstruction_exclusive_jets_dcut(
287+
jetreconstruction_StatusCode jetreconstruction_exclusive_jets_dcut(
241288
const jetreconstruction_ClusterSequence *clustersequence, double dcut,
242289
jetreconstruction_JetsResult *result);
243290

@@ -257,9 +304,10 @@ int jetreconstruction_exclusive_jets_dcut(
257304
* @param[in] njets The number of exclusive jets to be calculated.
258305
* @param[out] result A pointer to the jetreconstruction_JetsResult object where
259306
* the resulting jets will be stored.
260-
* @return An integer status code indicating the success or failure.
307+
* @return An integer status code indicating the success or failure. See common
308+
* status codes jetreconstruction_StatusCode.
261309
*/
262-
int jetreconstruction_exclusive_jets_njets(
310+
jetreconstruction_StatusCode jetreconstruction_exclusive_jets_njets(
263311
const jetreconstruction_ClusterSequence *clustersequence, size_t njets,
264312
jetreconstruction_JetsResult *result);
265313

@@ -283,8 +331,9 @@ int jetreconstruction_exclusive_jets_njets(
283331
* inclusive jets.
284332
* @param[out] result A pointer to the jetreconstruction_JetsResult object where
285333
* the resulting jets will be stored.
286-
* @return An integer status code indicating the success or failure.
334+
* @return An integer status code indicating the success or failure. See common
335+
* status codes jetreconstruction_StatusCode.
287336
*/
288-
int jetreconstruction_inclusive_jets(
337+
jetreconstruction_StatusCode jetreconstruction_inclusive_jets(
289338
const jetreconstruction_ClusterSequence *clustersequence, double ptmin,
290339
jetreconstruction_JetsResult *result);

src/C_JetReconstruction/C_JetReconstruction.jl

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,58 @@ module C_JetReconstruction
66
using ..JetReconstruction: JetAlgorithm, RecoStrategy, PseudoJet, ClusterSequence,
77
HistoryElement, jet_reconstruct,
88
exclusive_jets, inclusive_jets
9+
using EnumX
10+
11+
"""
12+
@enumx StatusCode
13+
14+
An enumeration of common status codes used in the JetReconstruction C-bindings.
15+
16+
## Fields
17+
- `OK` - The operation succeeded.
18+
- `GenericException`- An unspecified error, not covered by other status codes occurred.
19+
- The rest of the status codes have corresponding standard Julia exception.
20+
"""
21+
@enumx StatusCode OK=0 GenericException=1 ArgumentError=2 BoundsError=3 CompositeException=4 DimensionMismatch=5 DivideError=6 DomainError=7 EOFError=8 ErrorException=9 InexactError=10 InitError=11 InterruptException=12 InvalidStateException=13 KeyError=14 LoadError=15 OutOfMemoryError=16 ReadOnlyMemoryError=17 RemoteException=18 MethodError=19 OverflowError=20 ParseError=21 SystemError=22 TypeError=23 UndefRefError=24 UndefVarError=25 StringIndexError=26
22+
23+
function handle_exception(exception)::Cint
24+
@error exception
25+
return Cint(exception_to_enum(exception))
26+
end
27+
28+
"""
29+
exception_to_enum(::Any)::Cint
30+
31+
Helper function matching Julia exception with C-style status code.
32+
# Returns
33+
- A numerical representation of the corresponding status code from the `StatusCode.T`.
34+
"""
35+
exception_to_enum(::Any) = Cint(StatusCode.GenericException)
36+
exception_to_enum(::ArgumentError) = Cint(StatusCode.ArgumentError)
37+
exception_to_enum(::BoundsError) = Cint(StatusCode.BoundsError)
38+
exception_to_enum(::CompositeException) = Cint(StatusCode.CompositeException)
39+
exception_to_enum(::DimensionMismatch) = Cint(StatusCode.DimensionMismatch)
40+
exception_to_enum(::DivideError) = Cint(StatusCode.DivideError)
41+
exception_to_enum(::DomainError) = Cint(StatusCode.DomainError)
42+
exception_to_enum(::EOFError) = Cint(StatusCode.EOFError)
43+
exception_to_enum(::ErrorException) = Cint(StatusCode.ErrorException)
44+
exception_to_enum(::InexactError) = Cint(StatusCode.InexactError)
45+
exception_to_enum(::InitError) = Cint(StatusCode.InitError)
46+
exception_to_enum(::InterruptException) = Cint(StatusCode.InterruptException)
47+
exception_to_enum(::InvalidStateException) = Cint(StatusCode.InvalidStateException)
48+
exception_to_enum(::KeyError) = Cint(StatusCode.KeyError)
49+
exception_to_enum(::LoadError) = Cint(StatusCode.LoadError)
50+
exception_to_enum(::OutOfMemoryError) = Cint(StatusCode.OutOfMemoryError)
51+
exception_to_enum(::ReadOnlyMemoryError) = Cint(StatusCode.ReadOnlyMemoryError)
52+
# exception_to_enum(::Distributed.RemoteException) = Cint(StatusCode.RemoteException)
53+
exception_to_enum(::MethodError) = Cint(StatusCode.MethodError)
54+
exception_to_enum(::OverflowError) = Cint(StatusCode.OverflowError)
55+
# exception_to_enum(::Meta.ParseError) = Cint(StatusCode.ParseError)
56+
exception_to_enum(::SystemError) = Cint(StatusCode.SystemError)
57+
exception_to_enum(::TypeError) = Cint(StatusCode.TypeError)
58+
exception_to_enum(::UndefRefError) = Cint(StatusCode.UndefRefError)
59+
exception_to_enum(::UndefVarError) = Cint(StatusCode.UndefVarError)
60+
exception_to_enum(::StringIndexError) = Cint(StatusCode.StringIndexError)
961

1062
"""
1163
unsafe_wrap_c_array(ptr::Ptr{T}, array_length::Csize_t) where {T}
@@ -85,9 +137,13 @@ C-binding for `PseudoJet` initialization.
85137
Base.@ccallable function jetreconstruction_PseudoJet_init(ptr::Ptr{PseudoJet}, px::Cdouble,
86138
py::Cdouble, pz::Cdouble,
87139
E::Cdouble)::Cint
88-
pseudojet = PseudoJet(px, py, pz, E)
89-
unsafe_store!(ptr, pseudojet)
90-
return 0
140+
try
141+
pseudojet = PseudoJet(px, py, pz, E)
142+
unsafe_store!(ptr, pseudojet)
143+
catch e
144+
return handle_exception(e)
145+
end
146+
return Cint(StatusCode.OK)
91147
end
92148

93149
"""
@@ -219,12 +275,16 @@ function c_jet_reconstruct(particles::Ptr{T},
219275
R::Cdouble,
220276
strategy::RecoStrategy.Strategy,
221277
result::Ptr{C_ClusterSequence{U}}) where {T, U}
222-
particles_v = unsafe_wrap_c_array(particles, particles_length)
223-
clusterseq = jet_reconstruct(particles_v; p = nothing, algorithm = algorithm, R = R,
224-
strategy = strategy)
225-
c_clusterseq = C_ClusterSequence{U}(clusterseq)
226-
unsafe_store!(result, c_clusterseq)
227-
return 0
278+
try
279+
particles_v = unsafe_wrap_c_array(particles, particles_length)
280+
clusterseq = jet_reconstruct(particles_v; p = nothing, algorithm = algorithm, R = R,
281+
strategy = strategy)
282+
c_clusterseq = C_ClusterSequence{U}(clusterseq)
283+
unsafe_store!(result, c_clusterseq)
284+
catch e
285+
return handle_exception(e)
286+
end
287+
return Cint(StatusCode.OK)
228288
end
229289

230290
"""
@@ -257,8 +317,7 @@ Base.@ccallable function jetreconstruction_jet_reconstruct(particles::Ptr{Pseudo
257317
R::Cdouble,
258318
strategy::RecoStrategy.Strategy,
259319
result::Ptr{C_ClusterSequence{PseudoJet}})::Cint
260-
c_jet_reconstruct(particles, particles_length, algorithm, R, strategy, result)
261-
return 0
320+
return c_jet_reconstruct(particles, particles_length, algorithm, R, strategy, result)
262321
end
263322

264323
"""
@@ -324,12 +383,16 @@ An internal helper function for calling calling functions selecting jets from a
324383
"""
325384
function jets_selection(selector, clustersequence::Ptr{C_ClusterSequence{T}},
326385
result::Ptr{C_JetsResult{U}}; kwargs...)::Cint where {T, U}
327-
c_clusterseq = unsafe_load(clustersequence)
328-
clusterseq = ClusterSequence{T}(c_clusterseq)
329-
jets_result = selector(clusterseq; T = U, kwargs...)
330-
c_results = C_JetsResult{U}(make_c_array(jets_result)...)
331-
unsafe_store!(result, c_results)
332-
return 0
386+
try
387+
c_clusterseq = unsafe_load(clustersequence)
388+
clusterseq = ClusterSequence{T}(c_clusterseq)
389+
jets_result = selector(clusterseq; T = U, kwargs...)
390+
c_results = C_JetsResult{U}(make_c_array(jets_result)...)
391+
unsafe_store!(result, c_results)
392+
catch e
393+
return handle_exception(e)
394+
end
395+
return Cint(StatusCode.OK)
333396
end
334397

335398
"""

0 commit comments

Comments
 (0)