19 #include <structmember.h>
41 PyErr_SetString(PyExc_KeyError,
"Failed to allocate memory in Bunchcore");
46 for(i=0; i<
self->vec->data_length; i++)
50 bc_hit_copy->
hit = bc_hit_self->
hit;
52 Py_INCREF(bc_hit_copy->
hit);
59 int i, old_length =
length(
self);
61 for(i=old_length; i<new_length; i++)
75 if(self->vec == NULL)
return -1;
85 if (bc_hit->
hit != NULL)
86 Py_DECREF(bc_hit->
hit);
110 if(
bc_realloc(
self, pos+1) == NULL) {*ierr = -1;
return;}
112 new->hitcore = a_hitcore;
113 if(new->hit != NULL) {
115 Py_DECREF(new->hitcore);
118 if (new->hit != NULL)
119 Py_INCREF(new->hitcore);
129 return Py_BuildValue(
"i", len);
148 if( PyArg_ParseTuple(args,
"OOi", &a_hit, &a_hitcore, &pos) != 0)
150 if(pos < 0) pos = pos +
length(me);
154 PyErr_SetString(PyExc_IndexError,
"Internal error in Bunchcore.set_item - out of memory?");
160 PyErr_SetString(PyExc_KeyError,
"Failed to parse variable in function Bunchcore.set_item");
168 if( PyArg_ParseTuple(args,
"i", &pos) != 0)
170 if(pos < 0) pos +=
length(me);
174 PyErr_SetString(PyExc_IndexError,
"Index out of range in Bunchcore.set_item");
177 if(bc_hit->
hit == NULL) {Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_BuildValue(
"(OO)", Py_None, Py_None);}
178 return Py_BuildValue(
"(OO)", bc_hit->
hit, bc_hit->
hitcore);
180 PyErr_SetString(PyExc_KeyError,
"Failed to parse variable in function Bunchcore.set_item");
191 for(i=0; i<n_axes; i++)
194 if(functions[i] == NULL) {*ierr = -1;
return 0.;}
199 for(i=0; i<
length(
self); i++)
204 for(i=0; i<
length(
self); i++)
207 for(j=0; j<n_axes; j++)
219 int n_hits =
length(
self);
221 for(i=0; i<n_hits; i++)
224 for(i=0; i<n_axes; i++)
227 if(functions[i] == NULL) {*ierr = -1;
return;}
231 for(i=0; i<n_hits; i++)
234 for(j=0; j<n_axes; j++)
235 for(k=0; k<n_axes; k++)
236 target[j+k*n_axes] = 0.;
238 for(i=0; i<n_hits; i++)
240 for(j=0; j<n_axes; j++)
243 for(k=0; k<n_axes; k++)
244 target[j+k*n_axes] += my_m*(functions[k]( hits[i] ) - means[j]);
252 if(!py_list || !PyList_Check(py_list))
254 PyErr_SetString(PyExc_KeyError,
"Failed to parse argument list of strings in Bunchcore");
260 *list_size = PyList_Size(py_list);
261 *string_list = malloc(
sizeof(
const char*[*list_size]));
262 if(*string_list == NULL)
264 PyErr_SetString(PyExc_KeyError,
"Failed to allocate memory in Bunchcore");
267 for(i=0; i<*list_size; i++)
269 PyObject* my_string = PyList_GetItem(py_list, i);
270 if( PyString_Check(my_string) ) (*string_list)[i] = PyString_AsString(my_string);
273 PyErr_SetString(PyExc_KeyError,
"Failed to parse argument list of strings in Bunchcore");
284 if(!py_list || !PyList_Check(py_list))
286 PyErr_SetString(PyExc_KeyError,
"Failed to parse argument list of strings in Bunchcore");
292 *list_size = PyList_Size(py_list);
293 *double_list = malloc(
sizeof(
double[*list_size]));
294 if(*double_list == NULL)
296 PyErr_SetString(PyExc_KeyError,
"Failed to allocate memory in Bunchcore");
300 for(i=0; i<*list_size; i++)
302 PyObject* my_float = PyList_GetItem(py_list, i);
303 if(PyFloat_Check(my_float)) (*double_list)[i] = PyFloat_AsDouble(my_float) ;
306 PyErr_SetString(PyExc_KeyError,
"Failed to parse argument list of floats in Bunchcore");
317 int i=0,j=0,k=0,err=0;
318 PyObject* the_list = PyList_New(list_size);
319 for(i=0; i<list_size; i++)
321 PyObject* a_list = PyList_New(list_size);
322 err = PyList_SetItem(the_list, i, a_list);
323 for(j=0; j<list_size; j++)
325 PyObject* a_float = Py_BuildValue(
"d", values[k]);
326 err = PyList_SetItem(a_list, j, a_float);
336 PyErr_SetString(PyExc_KeyError,
"Failed while calculating moment in Bunchcore::get_moment");
337 PyObject* py_list_axes, *py_list_means;
338 int list_size1, list_size2, i=0;
339 const char*** string_list = malloc(
sizeof(
const char**));
340 double** double_list = malloc(
sizeof(
double*));
341 if(string_list == NULL || double_list == NULL)
343 PyErr_SetString(PyExc_KeyError,
"Failed to allocate memory in Bunchcore");
347 if( PyArg_ParseTuple(args,
"OO", &py_list_axes, &py_list_means) == 0)
return NULL;
349 if(*string_list == NULL) {free(string_list); free(double_list);
return NULL;}
351 if(*double_list == NULL) { free(*string_list); free(string_list); free(double_list);
return NULL;}
352 if(list_size1 != list_size2) { free(*string_list); free(*double_list); free(string_list); free(double_list);
return NULL;}
354 free(*double_list); free(*string_list); free(string_list); free(double_list);
355 if(i < 0)
return NULL;
357 return Py_BuildValue(
"d", moment);
362 PyErr_SetString(PyExc_KeyError,
"Failed while calculating moment in Bunchcore::get_covariance_matrix");
363 PyObject* py_list_axes, *py_list_means;
364 int list_size1, list_size2, i=0;
365 const char*** string_list = malloc(
sizeof(
const char**));
366 double** double_list = malloc(
sizeof(
double*));
367 if(string_list == NULL || double_list == NULL)
369 PyErr_SetString(PyExc_KeyError,
"Failed to allocate memory in Bunchcore");
373 if( PyArg_ParseTuple(args,
"OO", &py_list_axes, &py_list_means) == 0)
return NULL;
375 if(*string_list == NULL) {free(string_list); free(double_list);
return NULL;}
378 if(*double_list == NULL) { free(*string_list); free(string_list); free(double_list);
return NULL;}
379 if(list_size1 != list_size2) { free(*string_list); free(*double_list); free(string_list); free(double_list);
return NULL;}
381 double* target = malloc(
sizeof(
double[list_size1*list_size1]));
384 PyErr_SetString(PyExc_KeyError,
"Failed to allocate memory in Bunchcore");
390 free(*double_list); free(*string_list); free(string_list); free(double_list);
391 if(i < 0)
return NULL;
399 char* get_variable = NULL;
400 PyObject* comp = NULL;
401 double cut_value = 0.;
402 char* weight_variable = NULL;
403 if (PyArg_ParseTuple(args,
"sOds", &get_variable, &comp, &cut_value, &weight_variable) == 0)
405 int is_local = strcmp(
"local_weight", weight_variable);
407 if (strcmp(
"global_weight", weight_variable) == 0) {
408 PyErr_SetString(PyExc_ValueError,
"Attempting to cut with odd set value");
412 PyErr_SetString(PyExc_ValueError,
"Failed to apply cut");
420 PyObject* comp,
const double cut_value,
421 const int is_local) {
423 if (get_var == NULL) {
426 int my_length =
length(
self);
428 for (i = 0; i < my_length; ++i) {
430 double variable = get_var(hit);
431 PyObject* comp_out = PyObject_CallFunction(comp,
"dd", variable, cut_value);
432 if (comp_out == NULL) {
436 int ok = PyArg_Parse(comp_out,
"i", &comp);
461 {
"set_item", (PyCFunction)
set_item_python, METH_VARARGS,
"Set i^th item"},
462 {
"get_item", (PyCFunction)
get_item_python, METH_VARARGS,
"Get i^th item"},
463 {
"length", (PyCFunction)
length_python, METH_VARARGS,
"Report the length of the Bunchcore"},
464 {
"moment", (PyCFunction)
get_moment_python, METH_VARARGS,
"Get moment from a list of strings"},
465 {
"covariance_matrix", (PyCFunction)
covariance_matrix_python, METH_VARARGS,
"Get covariance matrix from a list of strings"},
466 {
"_cut_double", (PyCFunction)
_cut_double_python, METH_VARARGS,
"Apply cut inner loop. Returns NULL on error or if get_variable is an int"},
482 PyObject_HEAD_INIT(NULL)
484 "Bunchcore.Bunchcore",
502 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
527 BunchcoreType.tp_new = PyType_GenericNew;
528 if (PyType_Ready(&BunchcoreType) < 0)
return;
530 PyObject* m = Py_InitModule3(
"Bunchcore", NULL,
"Core module wrapped by Bunch");
531 if (m == NULL)
return;
533 Py_INCREF(&BunchcoreType);
534 PyModule_AddObject(m,
"Bunchcore", (PyObject *)&BunchcoreType);
538 PyObject* hc_module = PyImport_ImportModule(
"xboa.core.Hitcore");
539 if(hc_module == NULL) {
return;}
542 PyObject *module_dict = PyModule_GetDict(hc_module);
543 PyObject *c_api_object = PyDict_GetItemString(module_dict,
"HITCORE_C_API");
544 if (PyCObject_Check(c_api_object))
546 hitcore_api = (
void **)PyCObject_AsVoidPtr(c_api_object);