added examples, moved implementation to new file
This commit is contained in:
parent
6aa6673696
commit
ae6aa5b58d
7
.clang-format
Normal file
7
.clang-format
Normal file
@ -0,0 +1,7 @@
|
||||
BasedOnStyle: LLVM
|
||||
IndentWidth: 4
|
||||
|
||||
UseTab: Never
|
||||
ColumnLimit: 80
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveMacros: true
|
23
examples/hist.py
Normal file
23
examples/hist.py
Normal file
@ -0,0 +1,23 @@
|
||||
import os, sys
|
||||
from pathlib import Path
|
||||
import boost_histogram as bh
|
||||
import matplotlib.pyplot as plt
|
||||
from creader import ClusterFileReader
|
||||
|
||||
try:
|
||||
base = Path(os.environ['MOENCH_DATA'])
|
||||
except:
|
||||
print('MOENCH_DATA has to be defined to run examples')
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
fname = "Moench_LGAD_SIM_Nov22/moenchLGAD202211/clustW17new/beam_En800eV_-40deg_300V_10us_d0_f5_0.clust"
|
||||
r = ClusterFileReader((base/fname).as_posix())
|
||||
cl = r.read(10000)
|
||||
|
||||
hist1 = bh.Histogram(bh.axis.Regular(40, -2, 2**14))
|
||||
hist1.fill(cl['data'].flat)
|
||||
fig, ax = plt.subplots()
|
||||
ax.bar(hist1.axes[0].centers, hist1.values(), width=hist1.axes[0].widths)
|
||||
ax.set_yscale('log')
|
||||
plt.show()
|
2
setup.py
2
setup.py
@ -4,7 +4,7 @@ import setuptools
|
||||
import numpy as np
|
||||
|
||||
c_ext = setuptools.Extension("creader",
|
||||
sources = ["src/creader_module.c", "src/cluster_reader.c"],
|
||||
sources = ["src/creader_module.c", "src/cluster_reader.c", "src/ClusterReader.c"],
|
||||
include_dirs=[
|
||||
np.get_include(),"src/"
|
||||
],
|
||||
|
219
src/ClusterReader.c
Normal file
219
src/ClusterReader.c
Normal file
@ -0,0 +1,219 @@
|
||||
#include "ClusterReader.h"
|
||||
#include "cluster_reader.h"
|
||||
#include "data_types.h"
|
||||
|
||||
#include <numpy/arrayobject.h>
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
FILE *fp;
|
||||
int n_left;
|
||||
} ClusterFileReader;
|
||||
|
||||
// Create a custom numpy data type that should reflect
|
||||
// our cluster data type.
|
||||
// TODO! Update with the actual cluster data type
|
||||
static PyArray_Descr *cluster_dt() {
|
||||
PyObject *dtype_dict;
|
||||
PyArray_Descr *dtype;
|
||||
dtype_dict = Py_BuildValue("[(s, s),(s, s),(s, s, (i))]", "x", "u2", "y",
|
||||
"u2", "data", "i4", 9);
|
||||
|
||||
PyArray_DescrConverter(dtype_dict, &dtype);
|
||||
Py_DECREF(dtype_dict);
|
||||
return dtype;
|
||||
}
|
||||
|
||||
static PyArray_Descr *cluster_analysis_dt() {
|
||||
PyObject *dtype_dict;
|
||||
PyArray_Descr *dtype;
|
||||
dtype_dict = Py_BuildValue("[(s, s),(s, s),(s, s)]", "tot3", "i4", "tot2",
|
||||
"i4", "corner", "u4");
|
||||
|
||||
PyArray_DescrConverter(dtype_dict, &dtype);
|
||||
Py_DECREF(dtype_dict);
|
||||
return dtype;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Constructor: sets the fp to NULL then tries to open the file
|
||||
// raises python exception if something goes wrong
|
||||
// returned object should mean file is open and ready to read
|
||||
static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args,
|
||||
PyObject *Py_UNUSED(kwds)) {
|
||||
|
||||
// Parse file name
|
||||
const char *fname = NULL;
|
||||
if (!PyArg_ParseTuple(args, "s", &fname))
|
||||
return -1;
|
||||
|
||||
self->fp = fopen(fname, "rb");
|
||||
self->n_left = 0;
|
||||
|
||||
// Raise python exception using information from errno
|
||||
if (self->fp == NULL) {
|
||||
PyErr_SetFromErrnoWithFilename(PyExc_OSError, fname);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Success
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Custom destructor to make sure we close the file
|
||||
static void ClusterFileReader_dealloc(ClusterFileReader *self) {
|
||||
if (self->fp) {
|
||||
#ifdef CR_VERBOSE
|
||||
printf("Closing file\n");
|
||||
#endif
|
||||
fclose(self->fp);
|
||||
self->fp = NULL;
|
||||
}
|
||||
Py_TYPE(self)->tp_free((PyObject *)self);
|
||||
}
|
||||
|
||||
// read method
|
||||
static PyObject *ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
|
||||
PyObject *Py_UNUSED(kwds)) {
|
||||
|
||||
const int ndim = 1;
|
||||
Py_ssize_t size = 0;
|
||||
if (!PyArg_ParseTuple(args, "|n", &size))
|
||||
return NULL;
|
||||
|
||||
npy_intp dims[] = {size};
|
||||
|
||||
// Create an uninitialized numpy array
|
||||
PyArray_Descr *dtype = cluster_dt();
|
||||
// PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
||||
// PyArray_Descr *descr)
|
||||
PyObject *clusters = PyArray_SimpleNewFromDescr(ndim, dims, dtype);
|
||||
PyArray_FILLWBYTE((PyArrayObject *)clusters,
|
||||
0); // zero initialization can be removed later
|
||||
|
||||
// Get a pointer to the array memory
|
||||
void *buf = PyArray_DATA((PyArrayObject *)clusters);
|
||||
|
||||
// Call the standalone C code to read clusters from file
|
||||
// Here goes the looping, removing frame numbers etc.
|
||||
int n_read = read_clusters(self->fp, size, buf, &self->n_left);
|
||||
|
||||
if (n_read != size) {
|
||||
// resize the array to match the number of read photons
|
||||
// this will reallocate memory
|
||||
|
||||
// create a new_shape struct on the stack
|
||||
PyArray_Dims new_shape;
|
||||
|
||||
// reuse dims for the shape
|
||||
dims[0] = n_read;
|
||||
new_shape.ptr = dims;
|
||||
new_shape.len = 1;
|
||||
|
||||
// resize the array to match the number of clusters read
|
||||
PyArray_Resize((PyArrayObject *)clusters, &new_shape, 1, NPY_ANYORDER);
|
||||
}
|
||||
|
||||
return clusters;
|
||||
}
|
||||
|
||||
// read method
|
||||
static PyObject *ClusterFileReader_clusterize(ClusterFileReader *self,
|
||||
PyObject *args,
|
||||
PyObject *Py_UNUSED(kwds)) {
|
||||
|
||||
// Create an uninitialized numpy array
|
||||
PyArray_Descr *dtypeIn = cluster_dt();
|
||||
PyArray_Descr *dtypeOut = cluster_analysis_dt();
|
||||
|
||||
PyObject *c_obj;
|
||||
if (!PyArg_ParseTuple(args, "O", &c_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 *c_array = PyArray_FromArray((PyArrayObject *)c_obj, dtypeIn,
|
||||
NPY_ARRAY_C_CONTIGUOUS);
|
||||
|
||||
// If parsing of a or b fails we throw an exception in Python
|
||||
if (c_array == NULL) {
|
||||
PyErr_SetString(
|
||||
PyExc_TypeError,
|
||||
"Could not convert one of the arguments to a numpy array.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const int ndim = PyArray_NDIM((PyArrayObject *)c_array);
|
||||
|
||||
npy_intp *dims = PyArray_SHAPE((PyArrayObject *)c_array);
|
||||
|
||||
Py_ssize_t size = dims[0];
|
||||
// printf("%d size %d %d\n",ndim,size,sizeof(ClusterAnalysis));
|
||||
// dims[0]=size;
|
||||
|
||||
// Cluster *clusters = reinterpret_cast<Cluster *>(
|
||||
// PyArray_DATA(reinterpret_cast<PyArrayObject *>(c_array)));
|
||||
|
||||
Cluster *clusters = (Cluster *)(PyArray_DATA((PyArrayObject *)(c_array)));
|
||||
|
||||
// PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
||||
// PyArray_Descr *descr)
|
||||
PyObject *clustersA = PyArray_SimpleNewFromDescr(ndim, dims, dtypeOut);
|
||||
// PyArray_FILLWBYTE((PyArrayObject *)clustersA, 0); //zero initialization
|
||||
// can be removed later
|
||||
npy_intp *strides = PyArray_STRIDES(((PyArrayObject *)(clustersA)));
|
||||
// printf("strides %d %d\n", strides[0],sizeof(ClusterAnalysis));
|
||||
|
||||
// Get a pointer to the array memory
|
||||
ClusterAnalysis *buf = PyArray_DATA((PyArrayObject *)clustersA);
|
||||
|
||||
// Call the standalone C code to read clusters from file
|
||||
// Here goes the looping, removing frame numbers etc.
|
||||
int nc = analyze_clusters(size, clusters, buf);
|
||||
// printf("%d %d\n",nc,size);
|
||||
|
||||
if (nc != size) {
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Parsed wrong size array!");
|
||||
}
|
||||
|
||||
return clustersA;
|
||||
}
|
||||
|
||||
// List all methods in our ClusterFileReader class
|
||||
static PyMethodDef ClusterFileReader_methods[] = {
|
||||
{"read", (PyCFunction)ClusterFileReader_read, METH_VARARGS,
|
||||
"Read clusters"},
|
||||
{"clusterize", (PyCFunction)ClusterFileReader_clusterize, METH_VARARGS,
|
||||
"Analyze clusters"},
|
||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
// Class defenition
|
||||
static PyTypeObject ClusterFileReaderType = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "creader.ClusterFileReader",
|
||||
.tp_doc = PyDoc_STR("ClusterFileReader implemented in C"),
|
||||
.tp_basicsize = sizeof(ClusterFileReader),
|
||||
.tp_itemsize = 0,
|
||||
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
||||
.tp_new = PyType_GenericNew,
|
||||
.tp_dealloc = (destructor)ClusterFileReader_dealloc,
|
||||
.tp_init = (initproc)ClusterFileReader_init,
|
||||
.tp_methods = ClusterFileReader_methods,
|
||||
};
|
||||
|
||||
void init_ClusterFileReader(PyObject *m){
|
||||
import_array();
|
||||
if (PyType_Ready(&ClusterFileReaderType) < 0)
|
||||
return NULL;
|
||||
|
||||
Py_INCREF(&ClusterFileReaderType);
|
||||
if (PyModule_AddObject(m, "ClusterFileReader",
|
||||
(PyObject *)&ClusterFileReaderType) < 0) {
|
||||
Py_DECREF(&ClusterFileReaderType);
|
||||
Py_DECREF(m);
|
||||
return NULL;
|
||||
}
|
||||
}
|
5
src/ClusterReader.h
Normal file
5
src/ClusterReader.h
Normal file
@ -0,0 +1,5 @@
|
||||
# pragma once
|
||||
#include <Python.h>
|
||||
|
||||
|
||||
void init_ClusterFileReader(PyObject *m);
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include "cluster_reader.h"
|
||||
#include "data_types.h"
|
||||
#include "ClusterReader.h"
|
||||
|
||||
#include <Python.h>
|
||||
#include <numpy/arrayobject.h>
|
||||
@ -10,207 +11,187 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
// Create a custom numpy data type that should reflect
|
||||
// our cluster data type.
|
||||
// TODO! Update with the actual cluster data type
|
||||
static PyArray_Descr *cluster_dt() {
|
||||
PyObject *dtype_dict;
|
||||
PyArray_Descr *dtype;
|
||||
dtype_dict = Py_BuildValue("[(s, s),(s, s),(s, s, (i))]", "x", "u2", "y",
|
||||
"u2", "data", "i4", 9);
|
||||
|
||||
PyArray_DescrConverter(dtype_dict, &dtype);
|
||||
Py_DECREF(dtype_dict);
|
||||
return dtype;
|
||||
}
|
||||
// static PyArray_Descr *cluster_analysis_dt() {
|
||||
// PyObject *dtype_dict;
|
||||
// PyArray_Descr *dtype;
|
||||
// dtype_dict = Py_BuildValue("[(s, s),(s, s),(s, s)]", "tot3", "i4", "tot2",
|
||||
// "i4", "corner", "u4");
|
||||
|
||||
static PyArray_Descr *cluster_analysis_dt() {
|
||||
PyObject *dtype_dict;
|
||||
PyArray_Descr *dtype;
|
||||
dtype_dict = Py_BuildValue("[(s, s),(s, s),(s, s)]", "tot3", "i4", "tot2",
|
||||
"i4", "corner", "u4");
|
||||
// PyArray_DescrConverter(dtype_dict, &dtype);
|
||||
// Py_DECREF(dtype_dict);
|
||||
// return dtype;
|
||||
// }
|
||||
|
||||
PyArray_DescrConverter(dtype_dict, &dtype);
|
||||
Py_DECREF(dtype_dict);
|
||||
return dtype;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
// Structure holding our cluster reader class
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
FILE *fp;
|
||||
int n_left;
|
||||
} ClusterFileReader;
|
||||
// clang-format on
|
||||
|
||||
// Constructor: sets the fp to NULL then tries to open the file
|
||||
// raises python exception if something goes wrong
|
||||
// returned object should mean file is open and ready to read
|
||||
static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args,
|
||||
PyObject *Py_UNUSED(kwds)) {
|
||||
// // Constructor: sets the fp to NULL then tries to open the file
|
||||
// // raises python exception if something goes wrong
|
||||
// // returned object should mean file is open and ready to read
|
||||
// static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args,
|
||||
// PyObject *Py_UNUSED(kwds)) {
|
||||
|
||||
// Parse file name
|
||||
const char *fname = NULL;
|
||||
if (!PyArg_ParseTuple(args, "s", &fname))
|
||||
return -1;
|
||||
// // Parse file name
|
||||
// const char *fname = NULL;
|
||||
// if (!PyArg_ParseTuple(args, "s", &fname))
|
||||
// return -1;
|
||||
|
||||
self->fp = fopen(fname, "rb");
|
||||
self->n_left = 0;
|
||||
// self->fp = fopen(fname, "rb");
|
||||
// self->n_left = 0;
|
||||
|
||||
// Raise python exception using information from errno
|
||||
if (self->fp == NULL) {
|
||||
PyErr_SetFromErrnoWithFilename(PyExc_OSError, fname);
|
||||
return -1;
|
||||
}
|
||||
// // Raise python exception using information from errno
|
||||
// if (self->fp == NULL) {
|
||||
// PyErr_SetFromErrnoWithFilename(PyExc_OSError, fname);
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
// Success
|
||||
return 0;
|
||||
}
|
||||
// // Success
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// Custom destructor to make sure we close the file
|
||||
static void ClusterFileReader_dealloc(ClusterFileReader *self) {
|
||||
if (self->fp) {
|
||||
#ifdef CR_VERBOSE
|
||||
printf("Closing file\n");
|
||||
#endif
|
||||
fclose(self->fp);
|
||||
self->fp = NULL;
|
||||
}
|
||||
Py_TYPE(self)->tp_free((PyObject *)self);
|
||||
}
|
||||
// // Custom destructor to make sure we close the file
|
||||
// static void ClusterFileReader_dealloc(ClusterFileReader *self) {
|
||||
// if (self->fp) {
|
||||
// #ifdef CR_VERBOSE
|
||||
// printf("Closing file\n");
|
||||
// #endif
|
||||
// fclose(self->fp);
|
||||
// self->fp = NULL;
|
||||
// }
|
||||
// Py_TYPE(self)->tp_free((PyObject *)self);
|
||||
// }
|
||||
|
||||
// read method
|
||||
static PyObject *ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
|
||||
PyObject *Py_UNUSED(kwds)) {
|
||||
// // read method
|
||||
// static PyObject *ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
|
||||
// PyObject *Py_UNUSED(kwds)) {
|
||||
|
||||
const int ndim = 1;
|
||||
Py_ssize_t size = 0;
|
||||
if (!PyArg_ParseTuple(args, "|n", &size))
|
||||
return NULL;
|
||||
// const int ndim = 1;
|
||||
// Py_ssize_t size = 0;
|
||||
// if (!PyArg_ParseTuple(args, "|n", &size))
|
||||
// return NULL;
|
||||
|
||||
npy_intp dims[] = {size};
|
||||
// npy_intp dims[] = {size};
|
||||
|
||||
// Create an uninitialized numpy array
|
||||
PyArray_Descr *dtype = cluster_dt();
|
||||
// PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
||||
// PyArray_Descr *descr)
|
||||
PyObject *clusters = PyArray_SimpleNewFromDescr(ndim, dims, dtype);
|
||||
PyArray_FILLWBYTE((PyArrayObject *)clusters,
|
||||
0); // zero initialization can be removed later
|
||||
// // Create an uninitialized numpy array
|
||||
// PyArray_Descr *dtype = cluster_dt();
|
||||
// // PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
||||
// // PyArray_Descr *descr)
|
||||
// PyObject *clusters = PyArray_SimpleNewFromDescr(ndim, dims, dtype);
|
||||
// PyArray_FILLWBYTE((PyArrayObject *)clusters,
|
||||
// 0); // zero initialization can be removed later
|
||||
|
||||
// Get a pointer to the array memory
|
||||
void *buf = PyArray_DATA((PyArrayObject *)clusters);
|
||||
// // Get a pointer to the array memory
|
||||
// void *buf = PyArray_DATA((PyArrayObject *)clusters);
|
||||
|
||||
// Call the standalone C code to read clusters from file
|
||||
// Here goes the looping, removing frame numbers etc.
|
||||
int n_read = read_clusters(self->fp, size, buf, &self->n_left);
|
||||
// // Call the standalone C code to read clusters from file
|
||||
// // Here goes the looping, removing frame numbers etc.
|
||||
// int n_read = read_clusters(self->fp, size, buf, &self->n_left);
|
||||
|
||||
if (n_read != size) {
|
||||
// resize the array to match the number of read photons
|
||||
// this will reallocate memory
|
||||
// if (n_read != size) {
|
||||
// // resize the array to match the number of read photons
|
||||
// // this will reallocate memory
|
||||
|
||||
// create a new_shape struct on the stack
|
||||
PyArray_Dims new_shape;
|
||||
// // create a new_shape struct on the stack
|
||||
// PyArray_Dims new_shape;
|
||||
|
||||
// reuse dims for the shape
|
||||
dims[0] = n_read;
|
||||
new_shape.ptr = dims;
|
||||
new_shape.len = 1;
|
||||
// // reuse dims for the shape
|
||||
// dims[0] = n_read;
|
||||
// new_shape.ptr = dims;
|
||||
// new_shape.len = 1;
|
||||
|
||||
// resize the array to match the number of clusters read
|
||||
PyArray_Resize((PyArrayObject *)clusters, &new_shape, 1, NPY_ANYORDER);
|
||||
}
|
||||
// // resize the array to match the number of clusters read
|
||||
// PyArray_Resize((PyArrayObject *)clusters, &new_shape, 1, NPY_ANYORDER);
|
||||
// }
|
||||
|
||||
return clusters;
|
||||
}
|
||||
// return clusters;
|
||||
// }
|
||||
|
||||
// read method
|
||||
static PyObject *ClusterFileReader_clusterize(ClusterFileReader *self,
|
||||
PyObject *args,
|
||||
PyObject *Py_UNUSED(kwds)) {
|
||||
// // read method
|
||||
// static PyObject *ClusterFileReader_clusterize(ClusterFileReader *self,
|
||||
// PyObject *args,
|
||||
// PyObject *Py_UNUSED(kwds)) {
|
||||
|
||||
// Create an uninitialized numpy array
|
||||
PyArray_Descr *dtypeIn = cluster_dt();
|
||||
PyArray_Descr *dtypeOut = cluster_analysis_dt();
|
||||
// // Create an uninitialized numpy array
|
||||
// PyArray_Descr *dtypeIn = cluster_dt();
|
||||
// PyArray_Descr *dtypeOut = cluster_analysis_dt();
|
||||
|
||||
PyObject *c_obj;
|
||||
if (!PyArg_ParseTuple(args, "O", &c_obj))
|
||||
return NULL;
|
||||
// PyObject *c_obj;
|
||||
// if (!PyArg_ParseTuple(args, "O", &c_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 *c_array = PyArray_FromArray((PyArrayObject *)c_obj, dtypeIn,
|
||||
NPY_ARRAY_C_CONTIGUOUS);
|
||||
// // 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 *c_array = PyArray_FromArray((PyArrayObject *)c_obj, dtypeIn,
|
||||
// NPY_ARRAY_C_CONTIGUOUS);
|
||||
|
||||
// If parsing of a or b fails we throw an exception in Python
|
||||
if (c_array == NULL) {
|
||||
PyErr_SetString(
|
||||
PyExc_TypeError,
|
||||
"Could not convert one of the arguments to a numpy array.");
|
||||
return NULL;
|
||||
}
|
||||
// // If parsing of a or b fails we throw an exception in Python
|
||||
// if (c_array == NULL) {
|
||||
// PyErr_SetString(
|
||||
// PyExc_TypeError,
|
||||
// "Could not convert one of the arguments to a numpy array.");
|
||||
// return NULL;
|
||||
// }
|
||||
|
||||
const int ndim = PyArray_NDIM((PyArrayObject *)c_array);
|
||||
// const int ndim = PyArray_NDIM((PyArrayObject *)c_array);
|
||||
|
||||
npy_intp *dims = PyArray_SHAPE((PyArrayObject *)c_array);
|
||||
// npy_intp *dims = PyArray_SHAPE((PyArrayObject *)c_array);
|
||||
|
||||
Py_ssize_t size = dims[0];
|
||||
// printf("%d size %d %d\n",ndim,size,sizeof(ClusterAnalysis));
|
||||
// dims[0]=size;
|
||||
// Py_ssize_t size = dims[0];
|
||||
// // printf("%d size %d %d\n",ndim,size,sizeof(ClusterAnalysis));
|
||||
// // dims[0]=size;
|
||||
|
||||
// Cluster *clusters = reinterpret_cast<Cluster *>(
|
||||
// PyArray_DATA(reinterpret_cast<PyArrayObject *>(c_array)));
|
||||
// // Cluster *clusters = reinterpret_cast<Cluster *>(
|
||||
// // PyArray_DATA(reinterpret_cast<PyArrayObject *>(c_array)));
|
||||
|
||||
Cluster *clusters = (Cluster *)(PyArray_DATA((PyArrayObject *)(c_array)));
|
||||
// Cluster *clusters = (Cluster *)(PyArray_DATA((PyArrayObject *)(c_array)));
|
||||
|
||||
// PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
||||
// PyArray_Descr *descr)
|
||||
PyObject *clustersA = PyArray_SimpleNewFromDescr(ndim, dims, dtypeOut);
|
||||
// PyArray_FILLWBYTE((PyArrayObject *)clustersA, 0); //zero initialization
|
||||
// can be removed later
|
||||
npy_intp *strides = PyArray_STRIDES(((PyArrayObject *)(clustersA)));
|
||||
// printf("strides %d %d\n", strides[0],sizeof(ClusterAnalysis));
|
||||
// // PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
||||
// // PyArray_Descr *descr)
|
||||
// PyObject *clustersA = PyArray_SimpleNewFromDescr(ndim, dims, dtypeOut);
|
||||
// // PyArray_FILLWBYTE((PyArrayObject *)clustersA, 0); //zero initialization
|
||||
// // can be removed later
|
||||
// npy_intp *strides = PyArray_STRIDES(((PyArrayObject *)(clustersA)));
|
||||
// // printf("strides %d %d\n", strides[0],sizeof(ClusterAnalysis));
|
||||
|
||||
// Get a pointer to the array memory
|
||||
ClusterAnalysis *buf = PyArray_DATA((PyArrayObject *)clustersA);
|
||||
// // Get a pointer to the array memory
|
||||
// ClusterAnalysis *buf = PyArray_DATA((PyArrayObject *)clustersA);
|
||||
|
||||
// Call the standalone C code to read clusters from file
|
||||
// Here goes the looping, removing frame numbers etc.
|
||||
int nc = analyze_clusters(size, clusters, buf);
|
||||
// printf("%d %d\n",nc,size);
|
||||
// // Call the standalone C code to read clusters from file
|
||||
// // Here goes the looping, removing frame numbers etc.
|
||||
// int nc = analyze_clusters(size, clusters, buf);
|
||||
// // printf("%d %d\n",nc,size);
|
||||
|
||||
if (nc != size) {
|
||||
// if (nc != size) {
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Parsed wrong size array!");
|
||||
}
|
||||
// PyErr_SetString(PyExc_TypeError, "Parsed wrong size array!");
|
||||
// }
|
||||
|
||||
return clustersA;
|
||||
}
|
||||
// return clustersA;
|
||||
// }
|
||||
|
||||
// List all methods in our ClusterFileReader class
|
||||
static PyMethodDef ClusterFileReader_methods[] = {
|
||||
{"read", (PyCFunction)ClusterFileReader_read, METH_VARARGS,
|
||||
"Read clusters"},
|
||||
{"clusterize", (PyCFunction)ClusterFileReader_clusterize, METH_VARARGS,
|
||||
"Analyze clusters"},
|
||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
// // List all methods in our ClusterFileReader class
|
||||
// static PyMethodDef ClusterFileReader_methods[] = {
|
||||
// {"read", (PyCFunction)ClusterFileReader_read, METH_VARARGS,
|
||||
// "Read clusters"},
|
||||
// {"clusterize", (PyCFunction)ClusterFileReader_clusterize, METH_VARARGS,
|
||||
// "Analyze clusters"},
|
||||
// {NULL, NULL, 0, NULL} /* Sentinel */
|
||||
// };
|
||||
|
||||
// Class defenition
|
||||
static PyTypeObject ClusterFileReaderType = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "creader.ClusterFileReader",
|
||||
.tp_doc = PyDoc_STR("ClusterFileReader implemented in C"),
|
||||
.tp_basicsize = sizeof(ClusterFileReader),
|
||||
.tp_itemsize = 0,
|
||||
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
||||
.tp_new = PyType_GenericNew,
|
||||
.tp_dealloc = (destructor)ClusterFileReader_dealloc,
|
||||
.tp_init = (initproc)ClusterFileReader_init,
|
||||
.tp_methods = ClusterFileReader_methods,
|
||||
};
|
||||
// // Class defenition
|
||||
// static PyTypeObject ClusterFileReaderType = {
|
||||
// PyVarObject_HEAD_INIT(NULL, 0).tp_name = "creader.ClusterFileReader",
|
||||
// .tp_doc = PyDoc_STR("ClusterFileReader implemented in C"),
|
||||
// .tp_basicsize = sizeof(ClusterFileReader),
|
||||
// .tp_itemsize = 0,
|
||||
// .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
||||
// .tp_new = PyType_GenericNew,
|
||||
// .tp_dealloc = (destructor)ClusterFileReader_dealloc,
|
||||
// .tp_init = (initproc)ClusterFileReader_init,
|
||||
// .tp_methods = ClusterFileReader_methods,
|
||||
// };
|
||||
|
||||
//------------------------------------------- Module stuff from here
|
||||
|
||||
@ -222,7 +203,7 @@ static struct PyModuleDef creader_def = {
|
||||
"creader",
|
||||
module_docstring,
|
||||
-1,
|
||||
ClusterFileReader_methods,
|
||||
NULL, // m_methods
|
||||
NULL, // m_slots
|
||||
NULL, // m_traverse
|
||||
NULL, // m_clear
|
||||
@ -231,20 +212,22 @@ static struct PyModuleDef creader_def = {
|
||||
|
||||
PyMODINIT_FUNC PyInit_creader(void) {
|
||||
PyObject *m;
|
||||
if (PyType_Ready(&ClusterFileReaderType) < 0)
|
||||
return NULL;
|
||||
// if (PyType_Ready(&ClusterFileReaderType) < 0)
|
||||
// return NULL;
|
||||
m = PyModule_Create(&creader_def);
|
||||
if (m == NULL)
|
||||
return NULL;
|
||||
|
||||
// add numpy functionallity
|
||||
import_array();
|
||||
|
||||
Py_INCREF(&ClusterFileReaderType);
|
||||
if (PyModule_AddObject(m, "ClusterFileReader",
|
||||
(PyObject *)&ClusterFileReaderType) < 0) {
|
||||
Py_DECREF(&ClusterFileReaderType);
|
||||
Py_DECREF(m);
|
||||
return NULL;
|
||||
}
|
||||
init_ClusterFileReader(m);
|
||||
// add the cluster reader
|
||||
// Py_INCREF(&ClusterFileReaderType);
|
||||
// if (PyModule_AddObject(m, "ClusterFileReader",
|
||||
// (PyObject *)&ClusterFileReaderType) < 0) {
|
||||
// Py_DECREF(&ClusterFileReaderType);
|
||||
// Py_DECREF(m);
|
||||
// return NULL;
|
||||
// }
|
||||
return m;
|
||||
}
|
||||
|
10
test.py
10
test.py
@ -1,16 +1,16 @@
|
||||
from creader import ClusterFileReader
|
||||
import numpy as np
|
||||
|
||||
maxph=100000000
|
||||
#maxph=100
|
||||
# maxph=100000000
|
||||
maxph=100
|
||||
|
||||
from pathlib import Path
|
||||
fpath = Path("/mnt/moench_data/Moench_LGAD_SIM_Nov22/moenchLGAD202211/clustW17new/beam_En800eV_-40deg_300V_10us_d0_f5_0.clust")
|
||||
fpath = Path("/mnt/sls_det_storage/moench_data/Moench_LGAD_SIM_Nov22/moenchLGAD202211/clustW17new/beam_En800eV_-40deg_300V_10us_d0_f5_0.clust")
|
||||
# r = ClusterFileReader()
|
||||
|
||||
r = ClusterFileReader(fpath.as_posix())
|
||||
|
||||
a=r.read(maxph)
|
||||
b=r.clusterize(a)
|
||||
# a=r.read(maxph)
|
||||
# b=r.clusterize(a)
|
||||
#v=int(maxph/100)
|
||||
#print(a[::v])
|
||||
|
Reference in New Issue
Block a user