#include "module.c" #include #include #include #include #include const char *CHANNEL_NAMES[] = {"EVENT_NUM", "EVENT_I", "EVENT_J", "EVENT_CHARGE", "EVENT_ETA_X", "EVENT_ETA_Y", "EVENT_I_INTERP", "EVENT_J_INTERP"}; int initialized = 0; // max number of events per frame #define MAX_NUM_EVENTS 100 #define EVENT_CHANNELS 7 double evt_p[EVENT_CHANNELS][MAX_NUM_EVENTS]; int func_ph_1d_double( double *frame, int i_dim, int j_dim, double *th_m); void setArrayToValue(double array[], int size, double value) { for (int i = 0; i < size; i++) { array[i] = value; } } //Threshold file //const char *THRESHOLD_FILE = "/configuration/user_scripts/lib/Thresh_2D_start600_size800.npy"; const char *THRESHOLD_FILE = "/configuration/user_scripts/lib/threshold_2d_start600_800.txt"; int threshold_num_elements=-1; double *threshold= NULL; const int MAX_LINE_LENGTH = 50000; int initialize(int size_x, int size_y, PyObject *pars){ threshold = (double *)malloc(size_x*size_y*sizeof(double)); int ret = 1; if (THRESHOLD_FILE!=NULL){ FILE *file = fopen(THRESHOLD_FILE, "rb"); if (file == NULL) { printf("Failed to open file.\n"); return -1; } char line[MAX_LINE_LENGTH]; int x = 0; int y = 0; while (fgets(line, sizeof(line), file) != NULL) { char* token = strtok(line, " "); while (token != NULL) { threshold[y * size_x + x] = atof(token); token = strtok(NULL, " "); x++; } y++; if (x != size_x){ printf("Invalid threshold file: wrong number of columns\n"); ret = -2; break; } x = 0; } if (y != size_y){ printf("Invalid threshold file: wrong number of rows\n"); ret = -3; } fclose(file); if (ret<0){ free(threshold); threshold = NULL; } /* FILE *file = fopen(THRESHOLD_FILE, "rb"); if (file == NULL) { printf("Failed to open file.\n"); return -1; } //Py_Initialize(); //import_array(); PyArray_Descr* dtype = PyArray_DescrFromType(NPY_FLOAT32); PyObject* pArray = PyArray_FromFile(file, dtype, NPY_OUT_ARRAY, NULL); fclose(file); if (pArray == NULL) { return -2; } // Access the array data //PyArrayObject* npArray = (PyArrayObject*)pArray; npy_intp* dims = PyArray_DIMS(pArray); int ndims = PyArray_NDIM(pArray); threshold_num_elements = PyArray_SIZE(pArray); return ndims; //if (ndims!=2){ // ret = -3; //} else if (dims[0]!=size_x){ // ret = -4; //} else if (dims[1]!=size_y){ // ret = -5; //}else { if (threshold_num_elements!=size_x*size_y){ return -6; } else { float* data = (float*)PyArray_DATA(pArray); for (npy_intp i = 0; i < threshold_num_elements; ++i) { //threshold [i] = *((float*)PyArray_GETPTR1(npArray, i)) threshold [i] = data[i]; } ret=1; } Py_DECREF(pArray); //Py_Finalize(); */ } else { PyObject* threshold_obj = PyDict_GetItemString(pars, "threshold"); double threshold_val = 60000.0; if (threshold_obj!=NULL){ if (PyFloat_Check(threshold_obj)) { threshold_val = PyFloat_AsDouble(threshold_obj); } else if (PyLong_Check(threshold_obj)) { threshold_val = PyLong_AsDouble(threshold_obj); } } for(int i=0; idescr->elsize; int dims = image->nd; int size_x = image->dimensions[1]; int size_y = image->dimensions[0]; unsigned short* img_data = (unsigned short*)image->data; //Initialization if (initialized==0){ //initialized = initialize(size_x, size_y, pars); initialized = initialize(1800, 800, pars); } int i,j; int i_dim=size_y; int j_dim=size_x; //background (all matrices are indexed in 1d) double *background = malloc(i_dim*j_dim*sizeof(double)); for(i=0; i=MAX_NUM_EVENTS){ break; } for(j=0;j=MAX_NUM_EVENTS){ break; } // 2x2 version charge = frame[i*j_dim+j]+frame[(i+1)*j_dim+j] + frame[i*j_dim+(j+1)]+frame[(i+1)*j_dim+j+1]; //pixel by pixel threshold th = th_m[i*j_dim +j]; //check if charge above threshold if(charge>th) { eta_x = (frame[(i+1)*j_dim + j ]+frame[(i+1)*j_dim + (j+1)])/charge; eta_y = (frame[ i*j_dim + (j+1)]+frame[(i+1)*j_dim + (j+1)])/charge; i_interp = i + (C[0] + C[1]*eta_x+ C[2]*pow(eta_x,2) + C[3]*pow(eta_x,3) + C[4]*pow(eta_x,4)+ C[5]*pow(eta_x,5) + C[6]*pow(eta_x,6) + C[7]*pow(eta_x,7)); j_interp = j + (D[0] + D[1]*eta_y+ D[2]*pow(eta_y,2) + D[3]*pow(eta_y,3) + D[4]*pow(eta_y,4)+ D[5]*pow(eta_y,5) + D[6]*pow(eta_y,6) + D[7]*pow(eta_y,7)); // 1st case: first event if(evt_i==0){ evt_p[0][evt_i] = i; evt_p[1][evt_i] = j; evt_p[2][evt_i] = charge; evt_p[3][evt_i] = eta_x; evt_p[4][evt_i] = eta_y; evt_p[5][evt_i] = i_interp; // evt_p[6][evt_i] = j_interp; // evt_i++; } else { // 2nd case: not 1st event. we check if it is a neighbourg of the previos events and if charge is larger n=0; evt_m_i = evt_i; for(m=0; m