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
|
import numpy as np
|
||||||
|
|
||||||
c_ext = setuptools.Extension("creader",
|
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=[
|
include_dirs=[
|
||||||
np.get_include(),"src/"
|
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 "cluster_reader.h"
|
||||||
#include "data_types.h"
|
#include "data_types.h"
|
||||||
|
#include "ClusterReader.h"
|
||||||
|
|
||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
#include <numpy/arrayobject.h>
|
#include <numpy/arrayobject.h>
|
||||||
@ -10,207 +11,187 @@
|
|||||||
#include <stdbool.h>
|
#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);
|
// static PyArray_Descr *cluster_analysis_dt() {
|
||||||
Py_DECREF(dtype_dict);
|
// PyObject *dtype_dict;
|
||||||
return dtype;
|
// 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() {
|
// PyArray_DescrConverter(dtype_dict, &dtype);
|
||||||
PyObject *dtype_dict;
|
// Py_DECREF(dtype_dict);
|
||||||
PyArray_Descr *dtype;
|
// return 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
// // Constructor: sets the fp to NULL then tries to open the file
|
||||||
// raises python exception if something goes wrong
|
// // raises python exception if something goes wrong
|
||||||
// returned object should mean file is open and ready to read
|
// // returned object should mean file is open and ready to read
|
||||||
static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args,
|
// static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args,
|
||||||
PyObject *Py_UNUSED(kwds)) {
|
// PyObject *Py_UNUSED(kwds)) {
|
||||||
|
|
||||||
// Parse file name
|
// // Parse file name
|
||||||
const char *fname = NULL;
|
// const char *fname = NULL;
|
||||||
if (!PyArg_ParseTuple(args, "s", &fname))
|
// if (!PyArg_ParseTuple(args, "s", &fname))
|
||||||
return -1;
|
// return -1;
|
||||||
|
|
||||||
self->fp = fopen(fname, "rb");
|
// self->fp = fopen(fname, "rb");
|
||||||
self->n_left = 0;
|
// self->n_left = 0;
|
||||||
|
|
||||||
// Raise python exception using information from errno
|
// // Raise python exception using information from errno
|
||||||
if (self->fp == NULL) {
|
// if (self->fp == NULL) {
|
||||||
PyErr_SetFromErrnoWithFilename(PyExc_OSError, fname);
|
// PyErr_SetFromErrnoWithFilename(PyExc_OSError, fname);
|
||||||
return -1;
|
// return -1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Success
|
// // Success
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Custom destructor to make sure we close the file
|
// // Custom destructor to make sure we close the file
|
||||||
static void ClusterFileReader_dealloc(ClusterFileReader *self) {
|
// static void ClusterFileReader_dealloc(ClusterFileReader *self) {
|
||||||
if (self->fp) {
|
// if (self->fp) {
|
||||||
#ifdef CR_VERBOSE
|
// #ifdef CR_VERBOSE
|
||||||
printf("Closing file\n");
|
// printf("Closing file\n");
|
||||||
#endif
|
// #endif
|
||||||
fclose(self->fp);
|
// fclose(self->fp);
|
||||||
self->fp = NULL;
|
// self->fp = NULL;
|
||||||
}
|
// }
|
||||||
Py_TYPE(self)->tp_free((PyObject *)self);
|
// Py_TYPE(self)->tp_free((PyObject *)self);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// read method
|
// // read method
|
||||||
static PyObject *ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
|
// static PyObject *ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
|
||||||
PyObject *Py_UNUSED(kwds)) {
|
// PyObject *Py_UNUSED(kwds)) {
|
||||||
|
|
||||||
const int ndim = 1;
|
// const int ndim = 1;
|
||||||
Py_ssize_t size = 0;
|
// Py_ssize_t size = 0;
|
||||||
if (!PyArg_ParseTuple(args, "|n", &size))
|
// if (!PyArg_ParseTuple(args, "|n", &size))
|
||||||
return NULL;
|
// return NULL;
|
||||||
|
|
||||||
npy_intp dims[] = {size};
|
// npy_intp dims[] = {size};
|
||||||
|
|
||||||
// Create an uninitialized numpy array
|
// // Create an uninitialized numpy array
|
||||||
PyArray_Descr *dtype = cluster_dt();
|
// PyArray_Descr *dtype = cluster_dt();
|
||||||
// PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
// // PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
||||||
// PyArray_Descr *descr)
|
// // PyArray_Descr *descr)
|
||||||
PyObject *clusters = PyArray_SimpleNewFromDescr(ndim, dims, dtype);
|
// PyObject *clusters = PyArray_SimpleNewFromDescr(ndim, dims, dtype);
|
||||||
PyArray_FILLWBYTE((PyArrayObject *)clusters,
|
// PyArray_FILLWBYTE((PyArrayObject *)clusters,
|
||||||
0); // zero initialization can be removed later
|
// 0); // zero initialization can be removed later
|
||||||
|
|
||||||
// Get a pointer to the array memory
|
// // Get a pointer to the array memory
|
||||||
void *buf = PyArray_DATA((PyArrayObject *)clusters);
|
// void *buf = PyArray_DATA((PyArrayObject *)clusters);
|
||||||
|
|
||||||
// Call the standalone C code to read clusters from file
|
// // Call the standalone C code to read clusters from file
|
||||||
// Here goes the looping, removing frame numbers etc.
|
// // Here goes the looping, removing frame numbers etc.
|
||||||
int n_read = read_clusters(self->fp, size, buf, &self->n_left);
|
// int n_read = read_clusters(self->fp, size, buf, &self->n_left);
|
||||||
|
|
||||||
if (n_read != size) {
|
// if (n_read != size) {
|
||||||
// resize the array to match the number of read photons
|
// // resize the array to match the number of read photons
|
||||||
// this will reallocate memory
|
// // this will reallocate memory
|
||||||
|
|
||||||
// create a new_shape struct on the stack
|
// // create a new_shape struct on the stack
|
||||||
PyArray_Dims new_shape;
|
// PyArray_Dims new_shape;
|
||||||
|
|
||||||
// reuse dims for the shape
|
// // reuse dims for the shape
|
||||||
dims[0] = n_read;
|
// dims[0] = n_read;
|
||||||
new_shape.ptr = dims;
|
// new_shape.ptr = dims;
|
||||||
new_shape.len = 1;
|
// new_shape.len = 1;
|
||||||
|
|
||||||
// resize the array to match the number of clusters read
|
// // resize the array to match the number of clusters read
|
||||||
PyArray_Resize((PyArrayObject *)clusters, &new_shape, 1, NPY_ANYORDER);
|
// PyArray_Resize((PyArrayObject *)clusters, &new_shape, 1, NPY_ANYORDER);
|
||||||
}
|
// }
|
||||||
|
|
||||||
return clusters;
|
// return clusters;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// read method
|
// // read method
|
||||||
static PyObject *ClusterFileReader_clusterize(ClusterFileReader *self,
|
// static PyObject *ClusterFileReader_clusterize(ClusterFileReader *self,
|
||||||
PyObject *args,
|
// PyObject *args,
|
||||||
PyObject *Py_UNUSED(kwds)) {
|
// PyObject *Py_UNUSED(kwds)) {
|
||||||
|
|
||||||
// Create an uninitialized numpy array
|
// // Create an uninitialized numpy array
|
||||||
PyArray_Descr *dtypeIn = cluster_dt();
|
// PyArray_Descr *dtypeIn = cluster_dt();
|
||||||
PyArray_Descr *dtypeOut = cluster_analysis_dt();
|
// PyArray_Descr *dtypeOut = cluster_analysis_dt();
|
||||||
|
|
||||||
PyObject *c_obj;
|
// PyObject *c_obj;
|
||||||
if (!PyArg_ParseTuple(args, "O", &c_obj))
|
// if (!PyArg_ParseTuple(args, "O", &c_obj))
|
||||||
return NULL;
|
// return NULL;
|
||||||
|
|
||||||
// Create two numpy arrays from the passed objects, if possible numpy will
|
// // Create two numpy arrays from the passed objects, if possible numpy will
|
||||||
// use the underlying buffer, otherwise it will create a copy, for example
|
// // use the underlying buffer, otherwise it will create a copy, for example
|
||||||
// if data type is different or we pass in a list. The
|
// // if data type is different or we pass in a list. The
|
||||||
// NPY_ARRAY_C_CONTIGUOUS flag ensures that we have contiguous memory.
|
// // NPY_ARRAY_C_CONTIGUOUS flag ensures that we have contiguous memory.
|
||||||
PyObject *c_array = PyArray_FromArray((PyArrayObject *)c_obj, dtypeIn,
|
// PyObject *c_array = PyArray_FromArray((PyArrayObject *)c_obj, dtypeIn,
|
||||||
NPY_ARRAY_C_CONTIGUOUS);
|
// NPY_ARRAY_C_CONTIGUOUS);
|
||||||
|
|
||||||
// If parsing of a or b fails we throw an exception in Python
|
// // If parsing of a or b fails we throw an exception in Python
|
||||||
if (c_array == NULL) {
|
// if (c_array == NULL) {
|
||||||
PyErr_SetString(
|
// PyErr_SetString(
|
||||||
PyExc_TypeError,
|
// PyExc_TypeError,
|
||||||
"Could not convert one of the arguments to a numpy array.");
|
// "Could not convert one of the arguments to a numpy array.");
|
||||||
return NULL;
|
// 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];
|
// Py_ssize_t size = dims[0];
|
||||||
// printf("%d size %d %d\n",ndim,size,sizeof(ClusterAnalysis));
|
// // printf("%d size %d %d\n",ndim,size,sizeof(ClusterAnalysis));
|
||||||
// dims[0]=size;
|
// // dims[0]=size;
|
||||||
|
|
||||||
// Cluster *clusters = reinterpret_cast<Cluster *>(
|
// // Cluster *clusters = reinterpret_cast<Cluster *>(
|
||||||
// PyArray_DATA(reinterpret_cast<PyArrayObject *>(c_array)));
|
// // 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,
|
// // PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims,
|
||||||
// PyArray_Descr *descr)
|
// // PyArray_Descr *descr)
|
||||||
PyObject *clustersA = PyArray_SimpleNewFromDescr(ndim, dims, dtypeOut);
|
// PyObject *clustersA = PyArray_SimpleNewFromDescr(ndim, dims, dtypeOut);
|
||||||
// PyArray_FILLWBYTE((PyArrayObject *)clustersA, 0); //zero initialization
|
// // PyArray_FILLWBYTE((PyArrayObject *)clustersA, 0); //zero initialization
|
||||||
// can be removed later
|
// // can be removed later
|
||||||
npy_intp *strides = PyArray_STRIDES(((PyArrayObject *)(clustersA)));
|
// npy_intp *strides = PyArray_STRIDES(((PyArrayObject *)(clustersA)));
|
||||||
// printf("strides %d %d\n", strides[0],sizeof(ClusterAnalysis));
|
// // printf("strides %d %d\n", strides[0],sizeof(ClusterAnalysis));
|
||||||
|
|
||||||
// Get a pointer to the array memory
|
// // Get a pointer to the array memory
|
||||||
ClusterAnalysis *buf = PyArray_DATA((PyArrayObject *)clustersA);
|
// ClusterAnalysis *buf = PyArray_DATA((PyArrayObject *)clustersA);
|
||||||
|
|
||||||
// Call the standalone C code to read clusters from file
|
// // Call the standalone C code to read clusters from file
|
||||||
// Here goes the looping, removing frame numbers etc.
|
// // Here goes the looping, removing frame numbers etc.
|
||||||
int nc = analyze_clusters(size, clusters, buf);
|
// int nc = analyze_clusters(size, clusters, buf);
|
||||||
// printf("%d %d\n",nc,size);
|
// // 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
|
// // List all methods in our ClusterFileReader class
|
||||||
static PyMethodDef ClusterFileReader_methods[] = {
|
// static PyMethodDef ClusterFileReader_methods[] = {
|
||||||
{"read", (PyCFunction)ClusterFileReader_read, METH_VARARGS,
|
// {"read", (PyCFunction)ClusterFileReader_read, METH_VARARGS,
|
||||||
"Read clusters"},
|
// "Read clusters"},
|
||||||
{"clusterize", (PyCFunction)ClusterFileReader_clusterize, METH_VARARGS,
|
// {"clusterize", (PyCFunction)ClusterFileReader_clusterize, METH_VARARGS,
|
||||||
"Analyze clusters"},
|
// "Analyze clusters"},
|
||||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
// {NULL, NULL, 0, NULL} /* Sentinel */
|
||||||
};
|
// };
|
||||||
|
|
||||||
// Class defenition
|
// // Class defenition
|
||||||
static PyTypeObject ClusterFileReaderType = {
|
// static PyTypeObject ClusterFileReaderType = {
|
||||||
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "creader.ClusterFileReader",
|
// PyVarObject_HEAD_INIT(NULL, 0).tp_name = "creader.ClusterFileReader",
|
||||||
.tp_doc = PyDoc_STR("ClusterFileReader implemented in C"),
|
// .tp_doc = PyDoc_STR("ClusterFileReader implemented in C"),
|
||||||
.tp_basicsize = sizeof(ClusterFileReader),
|
// .tp_basicsize = sizeof(ClusterFileReader),
|
||||||
.tp_itemsize = 0,
|
// .tp_itemsize = 0,
|
||||||
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
// .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
||||||
.tp_new = PyType_GenericNew,
|
// .tp_new = PyType_GenericNew,
|
||||||
.tp_dealloc = (destructor)ClusterFileReader_dealloc,
|
// .tp_dealloc = (destructor)ClusterFileReader_dealloc,
|
||||||
.tp_init = (initproc)ClusterFileReader_init,
|
// .tp_init = (initproc)ClusterFileReader_init,
|
||||||
.tp_methods = ClusterFileReader_methods,
|
// .tp_methods = ClusterFileReader_methods,
|
||||||
};
|
// };
|
||||||
|
|
||||||
//------------------------------------------- Module stuff from here
|
//------------------------------------------- Module stuff from here
|
||||||
|
|
||||||
@ -222,7 +203,7 @@ static struct PyModuleDef creader_def = {
|
|||||||
"creader",
|
"creader",
|
||||||
module_docstring,
|
module_docstring,
|
||||||
-1,
|
-1,
|
||||||
ClusterFileReader_methods,
|
NULL, // m_methods
|
||||||
NULL, // m_slots
|
NULL, // m_slots
|
||||||
NULL, // m_traverse
|
NULL, // m_traverse
|
||||||
NULL, // m_clear
|
NULL, // m_clear
|
||||||
@ -231,20 +212,22 @@ static struct PyModuleDef creader_def = {
|
|||||||
|
|
||||||
PyMODINIT_FUNC PyInit_creader(void) {
|
PyMODINIT_FUNC PyInit_creader(void) {
|
||||||
PyObject *m;
|
PyObject *m;
|
||||||
if (PyType_Ready(&ClusterFileReaderType) < 0)
|
// if (PyType_Ready(&ClusterFileReaderType) < 0)
|
||||||
return NULL;
|
// return NULL;
|
||||||
m = PyModule_Create(&creader_def);
|
m = PyModule_Create(&creader_def);
|
||||||
if (m == NULL)
|
if (m == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
// add numpy functionallity
|
||||||
import_array();
|
import_array();
|
||||||
|
init_ClusterFileReader(m);
|
||||||
Py_INCREF(&ClusterFileReaderType);
|
// add the cluster reader
|
||||||
if (PyModule_AddObject(m, "ClusterFileReader",
|
// Py_INCREF(&ClusterFileReaderType);
|
||||||
(PyObject *)&ClusterFileReaderType) < 0) {
|
// if (PyModule_AddObject(m, "ClusterFileReader",
|
||||||
Py_DECREF(&ClusterFileReaderType);
|
// (PyObject *)&ClusterFileReaderType) < 0) {
|
||||||
Py_DECREF(m);
|
// Py_DECREF(&ClusterFileReaderType);
|
||||||
return NULL;
|
// Py_DECREF(m);
|
||||||
}
|
// return NULL;
|
||||||
|
// }
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
10
test.py
10
test.py
@ -1,16 +1,16 @@
|
|||||||
from creader import ClusterFileReader
|
from creader import ClusterFileReader
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
maxph=100000000
|
# maxph=100000000
|
||||||
#maxph=100
|
maxph=100
|
||||||
|
|
||||||
from pathlib import Path
|
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()
|
||||||
|
|
||||||
r = ClusterFileReader(fpath.as_posix())
|
r = ClusterFileReader(fpath.as_posix())
|
||||||
|
|
||||||
a=r.read(maxph)
|
# a=r.read(maxph)
|
||||||
b=r.clusterize(a)
|
# b=r.clusterize(a)
|
||||||
#v=int(maxph/100)
|
#v=int(maxph/100)
|
||||||
#print(a[::v])
|
#print(a[::v])
|
||||||
|
Reference in New Issue
Block a user