1 #ifndef LARLITE_HITIMAGEMAKER_CXX
2 #define LARLITE_HITIMAGEMAKER_CXX
6 #include "DataFormat/hit.h"
10 { _name=
"HitImageMaker"; _fout=0;
init(); }
21 auto const ev_hit = storage->get_data<event_hit>(
"gaushit");
27 std::vector<int> x_min_v;
28 std::vector<int> x_max_v;
29 std::vector<int> y_min_v;
30 std::vector<int> y_max_v;
31 std::vector<float> q_max_v;
32 for(
auto const& h : *ev_hit) {
34 if(h.Integral()<5.)
continue;
35 size_t plane = h.WireID().Plane;
36 if(plane >= x_min_v.size()) {
38 x_min_v.resize(plane+1,4000);
39 x_max_v.resize(plane+1,0);
40 y_min_v.resize(plane+1,9600);
41 y_max_v.resize(plane+1,0);
42 q_max_v.resize(plane+1,0);
47 int wire = h.WireID().Wire;
48 int time = (int)(h.PeakTime());
49 float q = h.Integral();
50 if( x_min_v[plane] > wire ) x_min_v[plane] = wire;
51 if( x_max_v[plane] < wire ) x_max_v[plane] = wire;
52 if( y_min_v[plane] > time ) y_min_v[plane] = time;
53 if( y_max_v[plane] < time ) y_max_v[plane] = time;
54 if( q_max_v[plane] < q ) q_max_v[plane] = q;
57 for(
size_t plane=0; plane<x_min_v.size(); ++plane) {
58 std::cout<<
"Plane "<<plane<<
" : "
59 <<x_min_v[plane]<<
" => "<<x_max_v[plane]<<
" : "
60 <<y_min_v[plane]<<
" => "<<y_max_v[plane]<< std::endl;
62 ::cv::Mat mat(x_max_v[plane] - x_min_v[plane] + 1,
63 y_max_v[plane] - y_min_v[plane] + 1,
64 CV_8UC1, cvScalar(0.));
67 canny.create(mat.size(),mat.type());
74 for(
auto const& h : *ev_hit) {
76 if(h.Integral()<5.)
continue;
77 int wire = h.WireID().Wire;
78 int time = (int)(h.PeakTime());
79 size_t plane = h.WireID().Plane;
80 int charge = ((256. * h.Integral() / q_max_v[plane]));
81 wire -= x_min_v[plane];
82 time -= y_min_v[plane];
85 charge += mat.at<
unsigned char>(wire,time);
87 if(charge>256) charge = 256;
88 mat.at<
unsigned char>(wire,time) = (
unsigned char)(charge);
92 for(
size_t plane=0; plane<
_mat_v.size(); ++plane) {
103 std::vector<std::vector<cv::Point> > cv_contour_v;
104 std::vector<cv::Vec4i> cv_hierarchy_v;
106 ::cv::findContours(
_canny_v[plane],cv_contour_v,cv_hierarchy_v,
108 CV_CHAIN_APPROX_SIMPLE);
110 std::cout<<
"Found "<<cv_contour_v.size()<<
" contours..."<<std::endl;
113 plane_contour_v.resize( cv_contour_v.size() );
115 for(
size_t c_index=0; c_index<cv_contour_v.size(); ++c_index) {
117 auto& cv_contour = cv_contour_v[c_index];
118 auto& out_contour = plane_contour_v[c_index];
120 out_contour.resize(cv_contour.size());
121 for(
size_t p_index=0; p_index<cv_contour.size(); ++p_index)
122 out_contour.set(p_index, cv_contour[p_index].x, cv_contour[p_index].y);
140 if(plane >=
_mat_v.size()) {
141 print(
msg::kERROR,__FUNCTION__,
"Invalid plane ID requested...");
142 throw std::exception();
152 if(plane >=
_mat_v.size()) {
153 print(
msg::kERROR,__FUNCTION__,
"Invalid plane ID requested...");
154 throw std::exception();
164 if(plane >=
_mat_v.size()) {
165 print(
msg::kERROR,__FUNCTION__,
"Invalid plane ID requested...");
166 throw std::exception();
173 if(plane >=
_mat_v.size()) {
174 print(
msg::kERROR,__FUNCTION__,
"Invalid plane ID requested...");
175 throw std::exception();
179 print(
msg::kERROR,__FUNCTION__,
"Invalid contour ID requested...");
180 throw std::exception();
183 int* dim_data =
new int[2];
185 dim_data[1] = (int)(
_contour_v[plane][contour_index].size());
187 return PyArray_FromDimsAndData( 2, dim_data, NPY_DOUBLE, (
char*) &(
_contour_v[plane][contour_index].raw_data()[0]) );