xboa
Hitcore.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 Hitcore.h
21 Designed to improve speed. My concern here is to provide C get
22 functions so that other code (i.e. Bunchcore) can do the lookup
23 directly rather than going through Python
24 
25 Contains hitcore object, which holds most of the data for the hit
26 get functions
27  get internal data, either directly or using a string, int or python
28  string to reference the internal data
29  two sets of functions, one to return double, one to return int
30  if only I had c++ templates!
31 set functions
32  set internal data, either directly or using a string, int or python
33  string to reference the internal data
34  two sets of functions, one to return double, one to return int
35  if only I had c++ templates!
36 global_weight_table
37  this is a dictionary that maps (spill, event_number, particle_number) to
38  (global_weight)
39 
40 Also I define a vector object which is a variable length array of void*.
41 This is designed to look more-or-less like a std::vector from C++, but much less safe i.e. has:
42  data_length - memory initialised),
43  allocated_length - memory reserved but not necessarily initialised)
44  size_t - size of the objects in the vector
45  data* - pointer to the data
46 Initialisation and allocation via vector_init, vector_alloc
47 Data insertion via vector_insert
48 Element referencing via vector_el and vector_el_bc (with bound checking)
49 
50 
51 */
52 
53 #ifndef Hitcore_h
54 #define Hitcore_h
55 
56 
57 #include <Python.h>
58 
59 //Hitcore data struct
60 typedef struct {
62  double x;
63  double y;
64  double z;
65  double t;
66  double px;
67  double py;
68  double pz;
69  double energy;
70  double weight;
71  int event;
72  double mass;
73  double bx;
74  double by;
75  double bz;
76  double ex;
77  double ey;
78  double ez;
79  double sx;
80  double sy;
81  double sz;
82  double pl;
83  double tau;
84  double e_dep;
85  double charge;
86  int station;
87  int pid;
88  int status;
89  int particle;
90  int spill;
91 } Hitcore;
92 typedef double (*hc_get_dbl_function)(Hitcore* self);
93 typedef void (*hc_set_dbl_function)(Hitcore* self, double value);
94 
95 typedef int (*hc_get_int_function)(Hitcore* self);
96 typedef void (*hc_set_int_function)(Hitcore* self, int value);
97 
98 //vector
99 typedef struct {void* data; size_t data_length; size_t allocd_length; size_t object_size; } vector;
100 
101 
102 #ifdef Hitcore_c
103 //deallocate memory from Hitcore
104 static void hc_Hitcore_dealloc(Hitcore * self);
105 static void hc_Hitcore_free(Hitcore * self);
106 //initialise Hitcore objects
107 static PyObject *hc_Hitcore_alloc(PyTypeObject *self, Py_ssize_t nitems);
108 static PyObject *hc_Hitcore_new(PyTypeObject *self, Py_ssize_t nitems);
109 static int hc_Hitcore_init(PyObject* self, PyObject *args, PyObject *kwds);
110 
111 
112 ////// GET METHODS ///////
113 //specialist get_methods
114 double hc_get_x(Hitcore* self);
115 double hc_get_y(Hitcore* self);
116 double hc_get_z(Hitcore* self);
117 double hc_get_t(Hitcore* self);
118 double hc_get_px(Hitcore* self);
119 double hc_get_py(Hitcore* self);
120 double hc_get_pz(Hitcore* self);
121 double hc_get_energy(Hitcore* self);
122 double hc_get_mass(Hitcore* self);
123 double hc_get_bx(Hitcore* self);
124 double hc_get_by(Hitcore* self);
125 double hc_get_bz(Hitcore* self);
126 double hc_get_ex(Hitcore* self);
127 double hc_get_ey(Hitcore* self);
128 double hc_get_ez(Hitcore* self);
129 double hc_get_sx(Hitcore* self);
130 double hc_get_sy(Hitcore* self);
131 double hc_get_sz(Hitcore* self);
132 double hc_get_pl(Hitcore* self); //pl is path length
133 double hc_get_tau(Hitcore* self); //tau is proper time
134 double hc_get_e_dep(Hitcore* self); //e_dep is energy deposited
135 double hc_get_charge(Hitcore* self);
136 
137 //Get weight
138 double hc_get_total_weight (Hitcore* self);
139 double hc_get_local_weight (Hitcore* self);
140 double hc_get_global_weight(Hitcore* self);
141 // expose dictionary (really only for testing)
142 PyObject* hc_get_global_weight_dict(PyObject* self, PyObject* args);
143 // dictionary key
144 PyObject* weight_dict_key(Hitcore* key);
145 
146 //Integer gets
147 int hc_get_event_number (Hitcore* self);
148 int hc_get_station (Hitcore* self);
149 int hc_get_pid (Hitcore* self);
150 int hc_get_status (Hitcore* self);
152 int hc_get_spill (Hitcore* self);
153 int hc_get_unique_id (Hitcore* self);
154 
155 //mapping of strings to functions
156 //e.g. hc_get_dbl_function_by_int (1) (some_hitcore) - returns some_hitcore.y
157 //e.g. hc_get_dbl_function_by_string("y")(some_hitcore) - returns some_hitcore.y
158 
159 static hc_get_dbl_function hc_get_dbl_function_by_string(const char* variable);
160 
161 static hc_get_int_function hc_get_int_function_by_string(const char* variable);
162 
163 //general get methods
164 //for ints
165 //static int hc_get_int_by_int (Hitcore* self, int index, int* ierr);
166 static int hc_get_int_by_string(Hitcore* self, const char* variable, int* ierr);
167 //for doubles
168 //static double hc_get_dbl_by_int (Hitcore* self, int index, int* ierr);
169 static double hc_get_dbl_by_string(Hitcore* self, const char* variable, int* ierr);
170 static PyObject* hc_get_python(PyObject* self, PyObject* args); //ONLY returns DOUBLES
171 ///////////////////////////
172 
173 ////// SET METHODS ////////
174 //specialist set_methods
175 void hc_set_x(Hitcore* self, double value);
176 void hc_set_y(Hitcore* self, double value);
177 void hc_set_z(Hitcore* self, double value);
178 void hc_set_t(Hitcore* self, double value);
179 void hc_set_px(Hitcore* self, double value);
180 void hc_set_py(Hitcore* self, double value);
181 void hc_set_pz(Hitcore* self, double value);
182 void hc_set_energy(Hitcore* self, double value);
183 void hc_set_mass(Hitcore* self, double value);
184 void hc_set_bx(Hitcore* self, double value);
185 void hc_set_by(Hitcore* self, double value);
186 void hc_set_bz(Hitcore* self, double value);
187 void hc_set_ex(Hitcore* self, double value);
188 void hc_set_ey(Hitcore* self, double value);
189 void hc_set_ez(Hitcore* self, double value);
190 void hc_set_sx(Hitcore* self, double value);
191 void hc_set_sy(Hitcore* self, double value);
192 void hc_set_sz(Hitcore* self, double value);
193 void hc_set_pl(Hitcore* self, double value); //pl is path length
194 void hc_set_tau(Hitcore* self, double value); //tau is proper time
195 void hc_set_e_dep(Hitcore* self, double value); //e_dep is energy deposited
196 void hc_set_charge(Hitcore* self, double value);
197 
198 //Integer sets
199 void hc_set_event_number (Hitcore* self, int value);
200 void hc_set_station (Hitcore* self, int value);
201 void hc_set_pid (Hitcore* self, int value);
202 void hc_set_status (Hitcore* self, int value);
203 void hc_set_particle_number(Hitcore* self, int value);
204 void hc_set_spill (Hitcore* self, int value);
205 
206 // update id
207 void update_unique_id(Hitcore* self);
208 
209 //Set weight
210 void hc_set_local_weight (Hitcore* self, double weight);
211 void hc_set_global_weight(Hitcore* self, double weight);
212 
213 //mapping of strings to functions
214 //e.g. hc_set_dbl_function_by_int (1) (some_hitcore, value) - sets y to value
215 //e.g. hc_set_dbl_function_by_string("y")(some_hitcore, value) - sets y to value
216 //doubles
217 static hc_set_dbl_function hc_set_dbl_function_by_string(const char* variable);
218 //static void hc_set_dbl_by_int(Hitcore* self, int index, double value, int* ierr);
219 static void hc_set_dbl_by_string(Hitcore* self, const char* variable, double value, int* ierr);
220 //ints
221 static hc_set_int_function hc_set_int_function_by_string(const char* variable);
222 static void hc_set_int_by_string (Hitcore* self, const char* variable, double value, int* ierr);
223 
224 //general set functions
225 static PyObject* hc_set_variables_to_python(PyObject* self, PyObject* args);
226 static PyObject* hc_set_python(PyObject* self, PyObject* args);
227 static PyObject* hc_clear_global_weights(PyObject* self, PyObject* args);
228 
229 //static check that data integrity is ok (at compile time)
230 static PyObject* hc_integrity_test(PyObject* self, PyObject* args);
231 //initialise Hitcore python module
232 PyMODINIT_FUNC hc_initHitcore(void);
233 
234 //vector is like an std::vector - but without iterators (assume you'll just use ints)
235 //note no bound checking! be careful...
236 //data is a pointer to the array;
237 //data_length is the length of the data in the array (i.e. sizeof(vector))
238 //allocd length is the allocated length of the array
239 //object_size is the size of objects in the array
240 //typedef above
241 //typedef struct {void* data; size_t data_length; size_t allocd_length; size_t object_size; } vector;
242 //allocate a new vector - object_size must be > 0
243 static vector* vector_alloc (size_t object_size, int n_elements);
244 //realloc the vec
245 static vector* vector_realloc (vector* vec, int n_elements);
246 //build a vector from allocated data - data must be allocated elsewhere
247 static vector* vector_init (void* data, size_t data_length, size_t allocd_length, size_t object_size);
248 //delete the vector
249 static void vector_free (vector* vec);
250 //insert elements found between target_start and target_end into vec at the point vec->data+insert_point
251 //if insert_start == NULL, inserts at end (appends)
252 //fail if runs out of memory
253 static void vector_insert (vector* vec, void* start, void* end, size_t insert_point, int* ierr);
254 //return vector element at point int
255 static void* vector_el (vector* vec, int element);
256 //return vector element at point int - with bound checking, returns NULL on out-of-bounds
257 static void* vector_el_bc (vector* vec, int element);
258 //return number of elements
259 static size_t vector_size (vector* vec)
260 {return vec->data_length/vec->object_size;}
261 
262 //// NOT IMPLEMENTED YET:
263 /*
264 //remove elements found between erase_start and erase_end, keeping vec contiguous
265 static void vector_remove (vector* vec, void* start, void* end);
266 //sort between sort_start and sort_end
267 //if sort_start == NULL use vec_start; if sort_end == NULL use vec_end
268 static void vector_sort (vector* vec, void* start, void* end);
269 //find pointer to the highest value of vec elements that has vec[i] <= find_key
270 //where comparator returns <0 for a < b; 0 for a==b; >0 for a>b
271 static void* vector_find_lower(vector* vec, void* start, void* end, void* find_key, int (*comparator)(void* a, void* b));
272 */
273 
274 // does nothing (compiler warning issue)
275 void no_warn(void);
276 
277 //global weights
278 PyObject* global_weight_table;
279 int last_id;
280 #endif //Hitcore_c
281 
282 #endif //Hitcore_h
283