xboa
Bunchcore.h
Go to the documentation of this file.
1 //This file is a part of xboa
2 //
3 //xboa is free software: you can redistribute it and/or modify
4 //it under the terms of the GNU General Public License as published by
5 //the Free Software Foundation, either version 3 of the License, or
6 //(at your option) any later version.
7 //
8 //xboa is distributed in the hope that it will be useful,
9 //but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 //GNU General Public License for more details.
12 //
13 //You should have received a copy of the GNU General Public License
14 //along with xboa in the doc folder. If not, see
15 //<http://www.gnu.org/licenses/>.
16 //
17 
18 
19 /*
20 Bunchcore.h
21 Set of core functions for Bunch python module
22 I want to improve speed - the thing I really worry about is the time taken
23 to calc moments. My optimisation is to use Hitcore.get_x() etc directly
24 rather than requiring the python type lookups etc
25 
26 Bunchcore is a list of Hit PyObjects and corresponding Hitcore objects
27 Nb I store separate arrays of Hitcore and Hit objects for speed reasons
28 but the Hitcore and Hit objects should always correspond!
29 */
30 
31 #ifndef Bunchcore_h
32 #define Bunchcore_h
33 
34 #include <Python.h>
35 #include "Hitcore.h"
36 
37 static PyTypeObject BunchcoreType;
38 static PySequenceMethods Bunchcore_as_seq;
39 
40 //I want to be able to directly access the hitcore without going through Python gumph
41 typedef struct { PyObject* hit; Hitcore* hitcore; } Bunchcore_hit;
42 //Bunchcore data struct is a vector of Bunchcore_hits
43 typedef struct { PyObject_HEAD; vector* vec; } Bunchcore;
44 
45 //deallocate memory from Bunchcore
46 static void Bunchcore_dealloc(Bunchcore * self);
47 //initialise Bunchcore objects
48 static int Bunchcore_init(Bunchcore* self, int length);
49 //reallocate memory (extend)
50 static Bunchcore* bc_realloc(Bunchcore* self, int new_length);
51 
52 static int length(Bunchcore* self) {return self->vec->data_length/self->vec->object_size;}
53 static PyObject* length_python(PyObject* self, PyObject* args);
54 static Py_ssize_t len_python(PyObject* self); //len() function wants Py_ssize_t in return
55 
56 ////// GET METHODS ///////
57 static PyObject* get_item_python(PyObject* self, PyObject* args);
58 
59 ////// SET METHODS ////////
60 static void set_item_bc_hit(Bunchcore* self, PyObject* a_hit, Hitcore* a_hitcore, int pos, int* ierr);
61 static PyObject* set_item_python(PyObject* self, PyObject* args);
62 
63 /////// OTHER STUFF ///////
64 //get moments using char list to index moment type
65 static double get_moment_char (Bunchcore* self, const char** axes, double* means, int n_axes, int* ierr);
66 static void covariance_matrix_char(Bunchcore* self, const char* axes[], double* means, int n_axes, double* target, int* ierr);
67 
68 //get moments using PyObjects
69 //helper function to parse list of strings - USER must FREE const char** afterwards
70 static void make_array_from_char_list (PyObject* py_list, const char*** string_list, int* list_size);
71 static void make_array_from_float_list(PyObject* py_list, double** double_list, int* list_size);
72 //calculate the moment
73 static PyObject* get_moment_python (PyObject* self, PyObject* args);
74 static PyObject* covariance_matrix_python (PyObject* self, PyObject* args);
75 
76 // inner loop for cut (speed optimisation); returns 0 on success, 1 on error
77 static int _cut_double(Bunchcore* self, const char* variable,
78  PyObject* comp, const double cut_value,
79  const int is_local);
80 static PyObject* _cut_double_python(PyObject* self, PyObject* args);
81 
82 void no_warn(void);
83 
84 /////// API ////////
85 
86 //initialise Bunchcore python module
87 PyMODINIT_FUNC initBunchcore (void);
88 
89 //hitcore api stuff
90 void** hitcore_api;
91 void import_hitcore(void);
92 
94 static void (*hc_set_local_weight)(Hitcore* hc, double weight);
95 static void (*hc_set_global_weight)(Hitcore* hc, double weight);
96 
97 //vector (in Hitcore.h)
98 static vector* (*vector_alloc )(size_t object_size, int n_elements);
99 static vector* (*vector_realloc)(vector* vec, int n_elements);
100 static vector* (*vector_init )(void* data, size_t data_length, size_t allocd_length, size_t object_size);
101 static vector* (*vector_free )(vector* vec);
102 static void (*vector_insert )(vector* vec, void* start, void* end, size_t insert_point, int* ierr);
103 static void* (*vector_el )(vector* vec, int element);
104 static void* (*vector_el_bc )(vector* vec, int element);
105 static size_t (*vector_size )(vector* vec);
106 
107 #endif