better implementation
This commit is contained in:
@ -1,2 +1,5 @@
|
||||
|
||||
|
||||
|
||||
test: main.cpp cluster_reader.cpp cluster_reader.h
|
||||
g++ -o test main.cpp cluster_reader.cpp -std=c++11
|
||||
|
@ -1,13 +1,14 @@
|
||||
|
||||
#include "cluster_reader.h"
|
||||
#include <iostream>
|
||||
#include "single_photon_hit.h"
|
||||
#include <vector>
|
||||
|
||||
FILE *f;
|
||||
int ph_left;
|
||||
|
||||
void cpp_open_cluster_file(char *fname, int &ok){
|
||||
f=fopen(fname,"r");
|
||||
ph_left=0;
|
||||
if (f)
|
||||
ok=1;
|
||||
else
|
||||
@ -17,50 +18,50 @@ void cpp_open_cluster_file(char *fname, int &ok){
|
||||
void cpp_close_cluster_file(int &ok){
|
||||
if (f){
|
||||
fclose(f);
|
||||
ph_left=0;
|
||||
f=NULL;
|
||||
ok=1;
|
||||
} else
|
||||
ok=0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void cpp_read_clusters(int &iiph, std::vector<struct cluster> &v, int &ok, int maxframes) {
|
||||
void cpp_read_clusters(struct cluster *clust, int &iiph, int &nframes, int &ok, int maxph){
|
||||
iiph=0;
|
||||
int iframe, iff, nph;
|
||||
//std::vector<struct cluster> v={};
|
||||
struct cluster *clust;
|
||||
single_photon_hit cl;
|
||||
ph_left=0;
|
||||
|
||||
ph_left=0;
|
||||
iff=0;
|
||||
ok=0;
|
||||
iiph=0;
|
||||
int nn;
|
||||
if (f) {
|
||||
ok=1;
|
||||
while (fread((void*)&iframe, 1, sizeof(int), f)) {
|
||||
if (fread((void*)&nph, 1, sizeof(int), f)) {
|
||||
ph_lft=nph;
|
||||
ph_left=nph;
|
||||
if (nph>0) {
|
||||
for (int iph=0; iph<nph; iph++){
|
||||
ph_lft--;
|
||||
if (cl.read(f)) {
|
||||
clust=new cluster;
|
||||
clust->x=cl.x;
|
||||
clust->y=cl.y;
|
||||
for (int ix=0; ix<3; ix++)
|
||||
for (int iy=0; iy<3; iy++)
|
||||
clust->data[iy*3+ix]=cl.data[iy*3+ix];
|
||||
v.push_back(*clust);
|
||||
iiph++;
|
||||
// for (int iph=0; iph<nph; iph++){
|
||||
std::cout << iframe << " " << nph << std::endl;
|
||||
if ((maxph-iiph)>nph) nn=nph;
|
||||
else nn=(maxph-iiph);
|
||||
|
||||
if ( fread( (void*)(clust+iiph), sizeof(struct cluster), nn, f) ) {
|
||||
ph_left=nph-nn;
|
||||
// if ( fread( (void*)&x, sizeof(int16_t), 1, myFile) ) { //reads x
|
||||
// if ( fread( (void*)&y, sizeof(int16_t), 1, myFile ) ) //reads y
|
||||
// return fread( (void*)data, sizeof(int), dx*dy, myFile );
|
||||
|
||||
|
||||
|
||||
iiph+=nn;
|
||||
nframes=iff;
|
||||
if (iiph>=maxph)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
iff++;
|
||||
if (maxframes>0 && iff>maxframes)
|
||||
break;
|
||||
}
|
||||
}
|
||||
//return v;
|
||||
}
|
||||
//return v;
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
#ifndef CLUSTER_READER_H
|
||||
#define CLUSTER_READER_H
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
#include <cstdio>
|
||||
|
||||
#define MAXPH 100000000
|
||||
|
||||
struct cluster {
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
@ -11,7 +12,7 @@ struct cluster {
|
||||
int32_t data[9];
|
||||
} ;
|
||||
|
||||
void cpp_read_clusters(int &iiph, std::vector<struct cluster> &v, int &ok, int maxframes=-1);
|
||||
void cpp_read_clusters(struct cluster *clust, int &iiph, int &nframes, int &ok, int maxph=MAXPH);
|
||||
|
||||
|
||||
void cpp_open_cluster_file(char *fname, int &ok);
|
||||
|
@ -1,5 +1,5 @@
|
||||
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
||||
#include "function.h" //this is the function that we want to call
|
||||
#include "cluster_reader.h" //this is the function that we want to call
|
||||
#include <Python.h>
|
||||
#include <numpy/arrayobject.h>
|
||||
#include<iostream>
|
||||
@ -44,13 +44,15 @@ PyDoc_STRVAR(close_cluster_file_doc, "Read cluster file.\n\n"
|
||||
|
||||
// Declare functions that should go into the module
|
||||
static PyObject *read_clusters(PyObject *self, PyObject *args);
|
||||
static PyObject *open_cluster_file(PyObject *self, PyObject *args);
|
||||
static PyObject *close_cluster_file(PyObject *self, PyObject *args);
|
||||
|
||||
// This is the module itself
|
||||
static PyMethodDef module_methods[] = {{"open_cluster_file", (PyCFunction)open_cluster_file, METH_VARARGS, open_cluster_file_doc},{"close_cluster_file", (PyCFunction)close_cluster_file, METH_VARARGS, close_cluster_file_doc},{"read_clusters", (PyCFunction)read_clusters, METH_VARARGS, read_clusters_doc},
|
||||
{NULL, NULL, 0, NULL}};
|
||||
|
||||
// Don't touch except the name
|
||||
static struct PyModuleDef mymod_def = {PyModuleDef_HEAD_INIT, "cluster_reader_mod",
|
||||
static struct PyModuleDef cluster_reader_module_def = {PyModuleDef_HEAD_INIT, "cluster_reader_module",
|
||||
module_docstring, -1, module_methods};
|
||||
|
||||
// Don't touch
|
||||
@ -63,13 +65,13 @@ PyMODINIT_FUNC PyInit_cluster_reader_cpp(void) {
|
||||
return m;
|
||||
}
|
||||
|
||||
static int open_cluster_file(PyObject *self, PyObject *args) {
|
||||
static PyObject *open_cluster_file(PyObject *self, PyObject *args) {
|
||||
|
||||
// cout << "Hello there" << endl; // General Kenobi
|
||||
// Python offers a lot of flexibility so the first thing we need to do
|
||||
// is to parse the arguments and make sure we are called with correct
|
||||
// parameters
|
||||
char *fname;
|
||||
char *fname="";
|
||||
if (!PyArg_ParseTuple(args, "s", fname))
|
||||
return NULL;
|
||||
|
||||
@ -77,10 +79,10 @@ static int open_cluster_file(PyObject *self, PyObject *args) {
|
||||
int ok;
|
||||
cpp_open_cluster_file(fname,ok);
|
||||
|
||||
return ok;
|
||||
return Py_BuildValue("i",ok);
|
||||
}
|
||||
|
||||
static int close_cluster_file(PyObject *self, PyObject *args) {
|
||||
static PyObject *close_cluster_file(PyObject *self, PyObject *args) {
|
||||
|
||||
// cout << "Hello there" << endl; // General Kenobi
|
||||
// Python offers a lot of flexibility so the first thing we need to do
|
||||
@ -91,7 +93,7 @@ static int close_cluster_file(PyObject *self, PyObject *args) {
|
||||
int ok;
|
||||
cpp_close_cluster_file(ok);
|
||||
|
||||
return ok;
|
||||
return Py_BuildValue("i",ok);
|
||||
}
|
||||
|
||||
|
||||
@ -101,54 +103,45 @@ static PyObject *read_clusters(PyObject *self, PyObject *args) {
|
||||
// Python offers a lot of flexibility so the first thing we need to do
|
||||
// is to parse the arguments and make sure we are called with correct
|
||||
// parameters
|
||||
PyObject *fname_obj;
|
||||
PyObject *maxframes_obj;
|
||||
if (!PyArg_ParseTuple(args, "OO", &_obj, &b_obj))
|
||||
PyObject *clust_obj;
|
||||
//PyObject *maxframes_obj;
|
||||
if (!PyArg_ParseTuple(args, "O", &clust_obj))
|
||||
return NULL;
|
||||
|
||||
// Create two numpy arrays from the passed objects, if possible numpy will
|
||||
// use the underlying buffer, otherwise it will create a copy, for example
|
||||
// if data type is different or we pass in a list. The
|
||||
// NPY_ARRAY_C_CONTIGUOUS flag ensures that we have contiguous memory.
|
||||
PyObject *a_array =
|
||||
PyArray_FROM_OTF(a_obj, NPY_UINT16, NPY_ARRAY_C_CONTIGUOUS);
|
||||
PyObject *d_array =
|
||||
PyArray_FROM_OTF(b_obj, NPY_UINT64, NPY_ARRAY_C_CONTIGUOUS);
|
||||
|
||||
// If parsing of a or b fails we throw an exception in Python
|
||||
if (a_array == NULL || d_array == NULL) {
|
||||
PyObject *clust_array =
|
||||
PyArray_FROM_OTF(clust_obj, NPY_OBJECT, NPY_ARRAY_C_CONTIGUOUS);
|
||||
// npy_intp ind=0;
|
||||
// void* clust_ptr = PyArray_GetPtr((PyArrayObject*)clust_obj, &ind);
|
||||
|
||||
// If parsing of a or b fails we throw an exception in Python
|
||||
if (clust_array == NULL) {
|
||||
PyErr_SetString(
|
||||
PyExc_TypeError,
|
||||
"Could not convert one of the arguments to a numpy array.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int N=MAXPH;
|
||||
// int N = (int)PyArray_DIM(clust_array, 0);
|
||||
//npy_intp dims[2] = { 400, 400 };
|
||||
|
||||
// Create array for return values
|
||||
PyObject *cluster_array = PyArray_SimpleNew(1, dims, NPY_UINT16);
|
||||
|
||||
|
||||
// For the C++ function call we need pointers (or another C++ type/data
|
||||
// structure)
|
||||
uint16_t *a = reinterpret_cast<uint16_t *>(
|
||||
PyArray_DATA(reinterpret_cast<PyArrayObject *>(a_array)));
|
||||
uint64_t*b = reinterpret_cast<uint64_t *>(
|
||||
PyArray_DATA(reinterpret_cast<PyArrayObject *>(d_array)));
|
||||
uint16_t *result_analog = reinterpret_cast<uint16_t *>(
|
||||
PyArray_DATA(reinterpret_cast<PyArrayObject *>(result_analog_array)));
|
||||
uint8_t *result_digital = reinterpret_cast<uint8_t *>(
|
||||
PyArray_DATA(reinterpret_cast<PyArrayObject *>(result_digital_array)));
|
||||
struct cluster *clust = reinterpret_cast<struct cluster *>(
|
||||
PyArray_DATA(reinterpret_cast<PyArrayObject *>(clust_array)));
|
||||
|
||||
int nph, nframes,ok;
|
||||
// Now call add wih pointers and size
|
||||
cpp_decode(a, b, result_analog, result_digital);
|
||||
cpp_read_clusters(clust,nph,nframes,ok,N);
|
||||
|
||||
// Clean up
|
||||
Py_DECREF(a_array);
|
||||
Py_DECREF(d_array);
|
||||
Py_DECREF(clust_array);
|
||||
|
||||
PyObject *result_tuple = PyTuple_New(2);
|
||||
PyTuple_SetItem(result_tuple, 0, result_analog_array);
|
||||
PyTuple_SetItem(result_tuple, 1, result_digital_array);
|
||||
// return result_analog_array;
|
||||
return result_tuple;
|
||||
// return result;
|
||||
return Py_BuildValue("iii",nph,nframes,ok);
|
||||
}
|
||||
|
35
src/main.cpp
Normal file
35
src/main.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include "cluster_reader.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
int nph, nframes;
|
||||
int ok;
|
||||
struct cluster *clust=new struct cluster[MAXPH];
|
||||
cout << "Clusters allocated " << endl;
|
||||
cpp_open_cluster_file("/mnt/moench_data/Moench_LGAD_SIM_Nov22/moenchLGAD202211/clustW17new/beam_En800eV_-40deg_300V_10us_d0_f5_0.clust",ok);
|
||||
cout << "File opened " << endl;
|
||||
cpp_read_clusters(clust, nph,nframes,ok);
|
||||
cout << "Clusters read " << endl;
|
||||
cpp_close_cluster_file(ok);
|
||||
cout << "File closed " << endl;
|
||||
int iph=0;
|
||||
// for (int iph=0; iph<nph; iph++){
|
||||
// int tot=0;
|
||||
// for (int iy=0; iy<3; iy++) {
|
||||
// for (int ix=0; ix<3; ix++)
|
||||
// tot+=(clust+iph)->data[iy*3+ix];
|
||||
// //std::cout << cl.data[iy*3+ix] << "\t";
|
||||
// // std::cout << endl;
|
||||
// }
|
||||
|
||||
// // if (tot>500)
|
||||
// // std::cout << iph << "\t" << (clust+iph)->x << "\t" << (clust+iph)->y << "\t" << tot << endl;
|
||||
|
||||
// }
|
||||
cout << nph << "\t"<< nframes<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user