15 #include "SQLamarr/custom_sql_functions.h"
16 #include "SQLamarr/GlobalPRNG.h"
22 void sqlamarr_create_sql_functions (sqlite3 *db)
26 sqlite3_create_function(db,
28 SQLITE_UTF8, NULL, &_sqlamarr_sql_log, NULL, NULL
31 sqlite3_create_function(db,
33 SQLITE_UTF8, NULL, &_sqlamarr_sql_norm2, NULL, NULL
36 sqlite3_create_function(db,
38 SQLITE_UTF8, NULL, &_sqlamarr_sql_pseudorapidity, NULL, NULL
41 sqlite3_create_function(db,
43 SQLITE_UTF8, NULL, &_sqlamarr_sql_azimuthal, NULL, NULL
46 sqlite3_create_function(db,
48 SQLITE_UTF8, NULL, &_sqlamarr_sql_polar, NULL, NULL
51 sqlite3_create_function(db,
52 "propagation_charge", 1,
53 SQLITE_UTF8, NULL, &_sqlamarr_sql_propagation_charge, NULL, NULL
56 sqlite3_create_function(db,
57 "slopes_to_cartesian", 4,
58 SQLITE_UTF8, NULL, &_sqlamarr_sql_slopes_to_cartesian, NULL, NULL
61 sqlite3_create_function(db,
63 SQLITE_UTF8, NULL, &_sqlamarr_sql_random_uniform, NULL, NULL
66 sqlite3_create_function(db,
68 SQLITE_UTF8, NULL, &_sqlamarr_sql_random_normal, NULL, NULL
71 for (nPars = 1; nPars < 10; ++nPars)
72 sqlite3_create_function(db,
73 "random_category", nPars,
74 SQLITE_UTF8, NULL, &_sqlamarr_sql_random_category, NULL, NULL
77 sqlite3_create_function(db,
78 "z_closest_to_beam", 5,
79 SQLITE_UTF8, NULL, &_sqlamarr_sql_z_closest_to_beam, NULL, NULL
87 void _sqlamarr_sql_log (
88 sqlite3_context *context,
94 res = log(sqlite3_value_double(argv[0]));
95 sqlite3_result_double(context, res);
101 void _sqlamarr_sql_norm2 (
102 sqlite3_context *context,
111 for (iPar = 0; iPar < argc; ++iPar)
113 buf = sqlite3_value_double(argv[iPar]);
118 sqlite3_result_double(context, res);
124 void _sqlamarr_sql_z_closest_to_beam (
125 sqlite3_context *context,
132 sqlite3_result_null(context);
137 const double x0 = sqlite3_value_double(argv[i++]);
138 const double y0 = sqlite3_value_double(argv[i++]);
139 const double z0 = sqlite3_value_double(argv[i++]);
140 const double tx = sqlite3_value_double(argv[i++]);
141 const double ty = sqlite3_value_double(argv[i++]);
143 const double ctb_z = (tx*tx*z0 - tx*x0 + ty*ty*z0 - ty*y0)/(tx*tx + ty*ty);
145 sqlite3_result_double(context, ctb_z);
152 void _sqlamarr_sql_pseudorapidity (
153 sqlite3_context *context,
158 double x, y, z, theta, eta;
162 sqlite3_result_null(context);
166 x = sqlite3_value_double(argv[0]);
167 y = sqlite3_value_double(argv[1]);
168 z = sqlite3_value_double(argv[2]);
170 theta = atan2(sqrt(x*x + y*y), z);
171 eta = -log(tan(0.5*theta));
173 sqlite3_result_double(context, eta);
179 void _sqlamarr_sql_azimuthal (
180 sqlite3_context *context,
189 sqlite3_result_null(context);
193 x = sqlite3_value_double(argv[0]);
194 y = sqlite3_value_double(argv[1]);
199 sqlite3_result_double(context, atan2(y, x));
205 void _sqlamarr_sql_polar (
206 sqlite3_context *context,
215 sqlite3_result_null(context);
219 x = sqlite3_value_double(argv[0]);
220 y = sqlite3_value_double(argv[1]);
221 z = sqlite3_value_double(argv[2]);
223 sqlite3_result_double(context, atan2(x*x + y*y, z));
229 void _sqlamarr_sql_propagation_charge (
230 sqlite3_context *context,
239 sqlite3_result_null(context);
243 pid = sqlite3_value_int(argv[0]);
246 if (abs(pid) == 11 || abs(pid) == 13 || abs(pid) == 15)
247 sqlite3_result_int(context, (pid > 0) ? -1 : +1);
250 else if (abs(pid) == 211 || abs(pid) == 321 || abs(pid) == 2212)
251 sqlite3_result_int(context, (pid > 0) ? +1 : -1);
254 else if (abs(pid) == 22 || abs(pid) == 2112)
255 sqlite3_result_int(context, 0);
258 sqlite3_result_null(context);
264 void _sqlamarr_sql_slopes_to_cartesian (
265 sqlite3_context *context,
276 sqlite3_result_null(context);
280 coord = sqlite3_value_int(argv[0]);
281 if (coord < 0 or coord > 2)
283 sqlite3_result_error(context,
"Invalid coord. Choose 012 for xyz.", -1);
287 norm = sqlite3_value_double(argv[1]);
290 sqlite3_result_error(context,
"Negative norm", -1);
294 tx = sqlite3_value_double(argv[2]);
295 ty = sqlite3_value_double(argv[3]);
297 ret[2] = norm/sqrt(1 + tx*tx + ty*ty);
301 sqlite3_result_double(context, ret[coord]);
308 void _sqlamarr_sql_random_uniform (
309 sqlite3_context *context,
315 std::uniform_real_distribution<double> uniform;
317 sqlite3_result_double(context, uniform(*generator));
323 void _sqlamarr_sql_random_normal (
324 sqlite3_context *context,
330 std::normal_distribution<double> gaussian;
332 sqlite3_result_double(context, gaussian(*generator));
338 void _sqlamarr_sql_random_category (
339 sqlite3_context *context,
345 std::uniform_real_distribution<float> uniform;
347 float r = uniform(*generator);
352 for (iArg = 0; iArg < argc; ++iArg)
353 if (sqlite3_value_double(argv[iArg]) < 0)
355 sqlite3_result_error(context,
"Negative probability", -1);
360 for (iArg = 0; iArg < argc; ++iArg)
362 sum +=
static_cast<float>(sqlite3_value_double(argv[iArg]));
363 if (r < sum && ret == argc) ret = iArg;
367 sqlite3_result_error(context,
"Sum of probabilities larger than 1", -1);
369 sqlite3_result_int(context, ret);
static PRNG * get_or_create(const sqlite3_context *db, uint64_t seed=no_seed)
Return a pointer to an initialized generator.