LArOpenCV  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
LArImageHit.cxx
Go to the documentation of this file.
1 #ifndef __LARLITE_LARIMAGEHIT_CXX__
2 #define __LARLITE_LARIMAGEHIT_CXX__
3 
4 #include "LArImageHit.h"
5 #include "LArUtil/Geometry.h"
6 #include "DataFormat/rawdigit.h"
7 //#include "Utils/NDArrayConverter.h"
8 #include "DataFormat/hit.h"
9 #include "DataFormat/cluster.h"
10 #include "DataFormat/event_ass.h"
11 
12 namespace larlite {
13 
14  void LArImageHit::_Configure_(const ::fcllite::PSet &pset)
15  {
16  _charge_to_gray_scale = pset.get<double>("Q2Gray");
17  }
18 
19  void LArImageHit::_Report_() const
20  {
21  std::cout << " # of clusters per plane ......... ";
22  for(auto const& num : _num_clusters_v) std::cout << Form("%-6zu ",num);
23  std::cout << " ... Average (per-event) ";
24  for(size_t plane=0; plane<_num_clustered_hits_v.size(); ++plane) {
25  double nclusters = _num_clusters_v[plane];
26  std::cout << Form("%g ",nclusters / ((double)(_num_stored)));
27  }
28  std::cout << std::endl;
29 
30  std::cout << " # of clustered hits per plane ... ";
31  for(auto const& num : _num_clustered_hits_v) std::cout << Form("%-6zu ",num);
32  std::cout << " ... Fractions: ";
33  for(size_t plane=0; plane<_num_clustered_hits_v.size(); ++plane) {
34  double clustered = _num_clustered_hits_v[plane];
35  double unclustered = _num_unclustered_hits_v[plane];
36  std::cout << Form("%g%% ",clustered/(clustered+unclustered));
37  }
38  std::cout << std::endl;
39  }
40 
41  void LArImageHit::store_clusters(storage_manager* storage)
42  {
43  ++_num_stored;
44 
45  auto geom = ::larutil::Geometry::GetME();
46  if(_num_clusters_v.empty()){
47  _num_clusters_v.resize(geom->Nplanes(),0);
48  _num_clustered_hits_v.resize(geom->Nplanes(),0);
49  _num_unclustered_hits_v.resize(geom->Nplanes(),0);
50  }
51 
52  auto ev_hit = storage->get_data<event_hit>(producer());
53 
54  if(!ev_hit) throw DataFormatException("Could not locate hit data product!");
55 
56  auto const& alg_mgr_v = algo_manager_set();
57 
58  std::vector<size_t> nclusters_v(alg_mgr_v.size(),0);
59  size_t nclusters_total=0;
60  for(size_t plane=0; plane<alg_mgr_v.size(); ++plane) {
61  nclusters_v[plane] = alg_mgr_v[plane].Clusters().size();
62  _num_clusters_v[plane] += nclusters_v[plane];
63  nclusters_total += nclusters_v[plane];
64  }
65 
66  AssSet_t ass_set;
67  ass_set.resize(nclusters_total);
68  for(auto& ass_unit : ass_set) ass_unit.reserve(100);
69 
70  for(size_t hindex=0; hindex<ev_hit->size(); ++hindex) {
71 
72  auto const& h = (*ev_hit)[hindex];
73 
74  auto const& wid = h.WireID();
75 
76  auto const& alg_mgr = alg_mgr_v[wid.Plane];
77 
78  auto const& cid = alg_mgr.ClusterID(wid.Wire,h.PeakTime());
79 
80  if(cid == ::larcv::kINVALID_CLUSTER_ID) {
81  _num_unclustered_hits_v[wid.Plane] += 1;
82  continue;
83  }
84 
85  _num_clustered_hits_v[wid.Plane] += 1;
86 
87  size_t cindex = cid;
88  for(size_t plane=0; plane<wid.Plane; ++plane) cindex += nclusters_v[plane];
89 
90  ass_set[cindex].push_back(hindex);
91  }
92 
93  auto ev_cluster = storage->get_data<event_cluster>("ImageClusterHit");
94  auto ev_ass = storage->get_data<event_ass>("ImageClusterHit");
95  if(ev_cluster) {
96  ev_cluster->reserve(nclusters_total);
97  for(size_t plane=0; plane<nclusters_v.size(); ++plane) {
98 
99  for(size_t cid=0; cid<nclusters_v[plane]; ++cid) {
100 
101  ::larlite::cluster c;
102  c.set_view(geom->PlaneToView(plane));
103  c.set_planeID(geo::PlaneID(0,0,plane));
104  c.set_id(ev_cluster->size());
105 
106  ev_cluster->push_back(c);
107  }
108  }
109  }
110  if(ev_ass)
111  ev_ass->set_association(ev_cluster->id(),ev_ass->id(),ass_set);
112  }
113 
114  void LArImageHit::extract_image(storage_manager* storage)
115  {
116 
117  auto const& geom = ::larutil::Geometry::GetME();
118  const size_t nplanes = geom->Nplanes();
119 
120  auto ev_hit = storage->get_data<event_hit>(producer());
121 
122  if(ev_hit==nullptr) throw DataFormatException("Could not locate hit data product!");
123 
124  std::vector< std::pair<size_t,size_t> > wire_range_v(nplanes,std::pair<size_t,size_t>(1e12,0));
125  std::vector< std::pair<size_t,size_t> > tick_range_v(nplanes,std::pair<size_t,size_t>(1e12,0));
126 
127  for(auto const& h : *ev_hit) {
128 
129  auto const& wid = h.WireID();
130 
131  auto& wire_range = wire_range_v[wid.Plane];
132  if(wire_range.first > wid.Wire) wire_range.first = wid.Wire;
133  if(wire_range.second < wid.Wire) wire_range.second = wid.Wire;
134 
135  auto& tick_range = tick_range_v[wid.Plane];
136  size_t peak_time = (size_t)(h.PeakTime());
137  if(tick_range.first > peak_time ) tick_range.first = ( bool(peak_time) ? peak_time - 1 : 0 );
138  if(tick_range.second < (peak_time+1)) tick_range.second = peak_time+1;
139  }
140 
141  for(size_t plane=0; plane<nplanes; ++plane) {
142  auto const& wire_range = wire_range_v[plane];
143  auto const& tick_range = tick_range_v[plane];
144  size_t nticks = tick_range.second - tick_range.first + 2;
145  size_t nwires = wire_range.second - wire_range.first + 2;
146  ::larcv::ImageMeta meta((double)nwires,(double)nticks,nwires,nticks,wire_range.first,tick_range.first);
147 
148  //if(!nticks || !nwires)
149  //_img_mgr.push_back(cv::Mat(),meta);
150  //else
151  //_img_mgr.push_back(::cv::Mat(nwires, nticks, CV_32FC1, cvScalar(0.)),meta);
152  _img_mgr.push_back(::cv::Mat(nwires, nticks, CV_8UC1, cvScalar(0.)),meta);
153  //_img_mgr->emplace_back(std::move(mat));
154  /*
155  std::cout << "Creating ... " << wire_range.first << " => " << wire_range.second << " ... " << nwires
156  << " : " << tick_range.first << " => " << tick_range.second << " ... " << nticks
157  << " @ " << plane << std::endl;
158  for(size_t x=0; x<nwires; ++x)
159  for(size_t y=0; y<nticks; ++y) mat.at<unsigned char>(x,y) = (unsigned char)5;
160  */
161  //mat.at<unsigned char>(0,0) = (unsigned char)5;
162  }
163 
164  for(auto const& h : *ev_hit) {
165 
166  auto const& wid = h.WireID();
167 
168  if(wid.Plane >= wire_range_v.size()) continue;
169 
170  auto& mat = _img_mgr.img_at(wid.Plane);
171 
172  //continue;
173  auto const& wire_range = wire_range_v[wid.Plane];
174  auto const& tick_range = tick_range_v[wid.Plane];
175 
176  size_t nticks = tick_range.second - tick_range.first + 2;
177  size_t nwires = wire_range.second - wire_range.first + 2;
178 
179  size_t y = (size_t)(h.PeakTime()+0.5) - tick_range.first;
180  size_t x = wid.Wire - wire_range.first;
181 
182  if(y>=nticks || x>=nwires) throw std::exception();
183 
184  //std::cout<<"Inserting " << x << " " << y << " @ " << wid.Plane << std::endl;
185  double charge = h.Integral() / _charge_to_gray_scale;
186  charge += (double)(mat.at<unsigned char>(x,y));
187 
188  if(charge>=255.) charge=255.;
189  if(charge<0.) charge=0.;
190 
191  mat.at<unsigned char>(x,y) = (unsigned char)((int)charge);
192  }
193 
194  }
195 
196 }
197 #endif