ROI_formation.cxx
Go to the documentation of this file.
1 
2 #include "ROI_formation.h"
3 
4 #include <iostream>
5 
6 using namespace WireCell;
7 using namespace WireCell::SigProc;
8 
9 ROI_formation::ROI_formation(Waveform::ChannelMaskMap& cmm,int nwire_u, int nwire_v, int nwire_w, int nbins, float th_factor_ind, float th_factor_col, int pad, float asy, int rebin , double l_factor, double l_max_th, double l_factor1, int l_short_length, int l_jump_one_bin)
10  : nwire_u(nwire_u)
11  , nwire_v(nwire_v)
12  , nwire_w(nwire_w)
13  , nbins(nbins)
14  , th_factor_ind(th_factor_ind)
15  , th_factor_col(th_factor_col)
16  , pad(pad)
17  , asy(asy)
18  , rebin(rebin)
19  , l_factor(l_factor)
20  , l_max_th(l_max_th)
21  , l_factor1(l_factor1)
22  , l_short_length(l_short_length)
23  , l_jump_one_bin(l_jump_one_bin)
24 {
25  self_rois_u.resize(nwire_u);
26  self_rois_v.resize(nwire_v);
27  self_rois_w.resize(nwire_w);
28 
29  loose_rois_u.resize(nwire_u);
30  loose_rois_v.resize(nwire_v);
31  loose_rois_w.resize(nwire_w);
32 
33  uplane_rms.resize(nwire_u);
34  vplane_rms.resize(nwire_v);
35  wplane_rms.resize(nwire_w);
36 
37  for (auto it = cmm["bad"].begin(); it!=cmm["bad"].end(); it++){
38  int ch = it->first;
39  std::vector<std::pair<int,int>> temps;
40  bad_ch_map[ch] = temps;
41  for (size_t ind = 0; ind < it->second.size(); ind++){
42  bad_ch_map[ch].push_back(std::make_pair(it->second.at(ind).first, it->second.at(ind).second));
43  //std::cout << ch << " " << << std::endl;
44  }
45  }
46  //std::cout << bad_ch_map.size() << std::endl;
47 
48 }
49 
50 void ROI_formation::apply_roi(int plane, Array::array_xxf& r_data, int flag){
51 
52  if (plane==0){
53  for (int irow = 0 ; irow != r_data.rows(); irow++){
54  //refresh ...
55  Waveform::realseq_t signal(r_data.cols(),0);
56  // loop ROIs and assign data
57 
58  std::vector<std::pair<int,int> > rois;
59  if (flag==1){ // self ROI
60  rois = self_rois_u.at(irow);
61  }else if (flag==2){ //loose ROI
62  rois = loose_rois_u.at(irow);
63  }
64 
65  for (auto it = rois.begin(); it!= rois.end(); it++){
66  int start_bin = it->first;
67  int end_bin = it->second;
68 
69  float start_content = r_data(irow,start_bin);
70  float end_content = r_data(irow,end_bin);
71  for (int i=start_bin; i<end_bin+1; i++){
72  int content = r_data(irow,i) - ((end_content - start_content)*(i-start_bin)/(end_bin-start_bin) + start_content);
73  signal.at(i) = content;
74  }
75  }
76  // load data back ..
77  for (int icol = 0; icol!=r_data.cols();icol++){
78  r_data(irow,icol) = signal.at(icol);
79  }
80  }
81  }else if (plane==1){
82  for (int irow = 0 ; irow != r_data.rows(); irow++){
83  //refresh ...
84  Waveform::realseq_t signal(r_data.cols(),0);
85  // loop ROIs and assign data
86  std::vector<std::pair<int,int> > rois;
87  if (flag==1){ // self ROI
88  rois = self_rois_v.at(irow);
89  }else if (flag==2){ //loose ROI
90  rois = loose_rois_v.at(irow);
91  }
92 
93 
94  for (auto it = rois.begin(); it!= rois.end(); it++){
95  int start_bin = it->first;
96  int end_bin = it->second;
97 
98  float start_content = r_data(irow,start_bin);
99  float end_content = r_data(irow,end_bin);
100  for (int i=start_bin; i<end_bin+1; i++){
101  int content = r_data(irow,i) - ((end_content - start_content)*(i-start_bin)/(end_bin-start_bin) + start_content);
102  signal.at(i) = content;
103  }
104  }
105  // load data back ..
106  for (int icol = 0; icol!=r_data.cols();icol++){
107  r_data(irow,icol) = signal.at(icol);
108  }
109  }
110  }else{
111  for (int irow = 0 ; irow != r_data.rows(); irow++){
112  //refresh ...
113  Waveform::realseq_t signal(r_data.cols(),0);
114  std::vector<std::pair<int,int> > rois;
115  if (flag==1){ // self ROI
116  rois = self_rois_w.at(irow);
117  }else if (flag==2){ //loose ROI
118  rois = loose_rois_w.at(irow);
119  }
120 
121  // loop ROIs and assign data
122  for (auto it = rois.begin(); it!= rois.end(); it++){
123  int start_bin = it->first;
124  int end_bin = it->second;
125 
126  for (int i=start_bin; i<end_bin+1; i++){
127  int content = r_data(irow,i);// - ((end_content - start_content)*(i-start_bin)/(end_bin-start_bin) + start_content);
128  signal.at(i) = content;
129  }
130  }
131  // load data back ..
132  for (int icol = 0; icol!=r_data.cols();icol++){
133  r_data(irow,icol) = signal.at(icol);
134  }
135  }
136  }
137 }
138 
139 
141 
142 }
143 
145  self_rois_u.clear();
146  self_rois_v.clear();
147  self_rois_w.clear();
148 
149  loose_rois_u.clear();
150  loose_rois_v.clear();
151  loose_rois_w.clear();
152 
153  uplane_rms.clear();
154  vplane_rms.clear();
155  wplane_rms.clear();
156 }
157 
158 
159 
160 
162  if (plane==0){
163  for (size_t i=0;i!=self_rois_u.size();i++){
164  std::vector<std::pair<int,int>> temp_rois;
165  int temp_begin=0, temp_end=0;
166  for (size_t j=0;j!=self_rois_u.at(i).size();j++){
167  temp_begin = self_rois_u.at(i).at(j).first - pad;
168  if (temp_begin < 0 ) temp_begin = 0;
169  temp_end = self_rois_u.at(i).at(j).second + pad;
170  if (temp_end >= nbins) temp_end = nbins - 1;
171  // merge
172  if (temp_rois.size() == 0){
173  temp_rois.push_back(std::make_pair(temp_begin,temp_end));
174  }else{
175  if (temp_begin < temp_rois.back().second){
176  if (temp_end > temp_rois.back().second){
177  temp_rois.back().second = temp_end;
178  }
179  }else{
180  temp_rois.push_back(std::make_pair(temp_begin,temp_end));
181  }
182  }
183  }
184  self_rois_u.at(i) = temp_rois;
185  }
186  }else if (plane==1){
187  for (size_t i=0;i!=self_rois_v.size();i++){
188  std::vector<std::pair<int,int>> temp_rois;
189  int temp_begin=0, temp_end=0;
190  for (size_t j=0;j!=self_rois_v.at(i).size();j++){
191  temp_begin = self_rois_v.at(i).at(j).first - pad;
192  if (temp_begin < 0 ) temp_begin = 0;
193  temp_end = self_rois_v.at(i).at(j).second + pad;
194  if (temp_end >= nbins) temp_end = nbins - 1;
195  // merge
196  if (temp_rois.size() == 0){
197  temp_rois.push_back(std::make_pair(temp_begin,temp_end));
198  }else{
199  if (temp_begin < temp_rois.back().second){
200  if (temp_end > temp_rois.back().second){
201  temp_rois.back().second = temp_end;
202  }
203  }else{
204  temp_rois.push_back(std::make_pair(temp_begin,temp_end));
205  }
206  }
207  }
208  self_rois_v.at(i) = temp_rois;
209  }
210  }else if (plane==2){
211  for (size_t i=0;i!=self_rois_w.size();i++){
212  std::vector<std::pair<int,int>> temp_rois;
213  int temp_begin=0, temp_end=0;
214  for (size_t j=0;j!=self_rois_w.at(i).size();j++){
215  temp_begin = self_rois_w.at(i).at(j).first - pad;
216  if (temp_begin < 0 ) temp_begin = 0;
217  temp_end = self_rois_w.at(i).at(j).second + pad;
218  if (temp_end >= nbins) temp_end = nbins - 1;
219  // merge
220  if (temp_rois.size() == 0){
221  temp_rois.push_back(std::make_pair(temp_begin,temp_end));
222  }else{
223  if (temp_begin < temp_rois.back().second){
224  if (temp_end > temp_rois.back().second){
225  temp_rois.back().second = temp_end;
226  }
227  }else{
228  temp_rois.push_back(std::make_pair(temp_begin,temp_end));
229  }
230  }
231  }
232  self_rois_w.at(i) = temp_rois;
233  }
234  }
235 
236 }
237 
239 
240  if (plane==0){
241  // u
242  for (int i=0;i!=nwire_u-2;i++){
243  for (size_t j=0; j!=self_rois_u.at(i).size();j++){
244  int start1 = self_rois_u.at(i).at(j).first;
245  int end1 = self_rois_u.at(i).at(j).second;
246  int length1 = end1-start1+1;
247  for (size_t k=0; k!=self_rois_u.at(i+2).size();k++){
248  int start2 = self_rois_u.at(i+2).at(k).first;
249  int end2 = self_rois_u.at(i+2).at(k).second;
250  int length2 = end2 - start2 + 1;
251  if ( fabs(length2 - length1) < (length2 + length1) * asy){
252  int start3 = (start1+start2)/2.;
253  int end3 = (end1+end2)/2.;
254  if (start3 < end3 && start3 <= end1 && start3 <=end2 && end3 >= start1 && end3 >=start2){
255  // go through existing ones to make sure there is no overlap
256  int flag = 0;
257  for (size_t i1 = 0; i1!=self_rois_u.at(i+1).size();i1++){
258  int max_start = start3;
259  if (self_rois_u.at(i+1).at(i1).first > max_start)
260  max_start = self_rois_u.at(i+1).at(i1).first;
261  int min_end = end3;
262  if (self_rois_u.at(i+1).at(i1).second < min_end)
263  min_end = self_rois_u.at(i+1).at(i1).second ;
264  if (max_start < min_end){
265  flag = 1;
266  break;
267  }
268  }
269  if (flag == 0)
270  self_rois_u.at(i+1).push_back(std::make_pair(start3,end3));
271  }
272  }
273  }
274  }
275  }
276  }else if (plane==1){
277  // v
278  for (int i=0;i!=nwire_v-2;i++){
279  for (size_t j=0; j!=self_rois_v.at(i).size();j++){
280  int start1 = self_rois_v.at(i).at(j).first;
281  int end1 = self_rois_v.at(i).at(j).second;
282  int length1 = end1-start1+1;
283  for (size_t k=0; k!=self_rois_v.at(i+2).size();k++){
284  int start2 = self_rois_v.at(i+2).at(k).first;
285  int end2 = self_rois_v.at(i+2).at(k).second;
286  int length2 = end2 - start2 + 1;
287  if ( fabs(length2 - length1) < (length2 + length1) * asy){
288  int start3 = (start1+start2)/2.;
289  int end3 = (end1+end2)/2.;
290  if (start3 < end3 && start3 <= end1 && start3 <=end2 && end3 >= start1 && end3 >=start2){
291  // go through existing ones to make sure there is no overlap
292  int flag = 0;
293  for (size_t i1 = 0; i1!=self_rois_v.at(i+1).size();i1++){
294  int max_start = start3;
295  if (self_rois_v.at(i+1).at(i1).first > max_start)
296  max_start = self_rois_v.at(i+1).at(i1).first;
297  int min_end = end3;
298  if (self_rois_v.at(i+1).at(i1).second < min_end)
299  min_end = self_rois_v.at(i+1).at(i1).second ;
300  if (max_start < min_end){
301  flag = 1;
302  break;
303  }
304  }
305  if (flag == 0)
306  self_rois_v.at(i+1).push_back(std::make_pair(start3,end3));
307  }
308  }
309  }
310  }
311  }
312  }else if (plane==2){
313  // w?
314  for (int i=0;i!=nwire_w-2;i++){
315  for (size_t j=0; j!=self_rois_w.at(i).size();j++){
316  int start1 = self_rois_w.at(i).at(j).first;
317  int end1 = self_rois_w.at(i).at(j).second;
318  int length1 = end1-start1+1;
319  for (size_t k=0; k!=self_rois_w.at(i+2).size();k++){
320  int start2 = self_rois_w.at(i+2).at(k).first;
321  int end2 = self_rois_w.at(i+2).at(k).second;
322  int length2 = end2 - start2 + 1;
323  if ( fabs(length2 - length1) < (length2 + length1) * asy){
324  int start3 = (start1+start2)/2.;
325  int end3 = (end1+end2)/2.;
326  if (start3 < end3 && start3 <= end1 && start3 <=end2 && end3 >= start1 && end3 >=start2){
327  // go through existing ones to make sure there is no overlap
328  int flag = 0;
329  for (size_t i1 = 0; i1!=self_rois_w.at(i+1).size();i1++){
330  int max_start = start3;
331  if (self_rois_w.at(i+1).at(i1).first > max_start)
332  max_start = self_rois_w.at(i+1).at(i1).first;
333  int min_end = end3;
334  if (self_rois_w.at(i+1).at(i1).second < min_end)
335  min_end = self_rois_w.at(i+1).at(i1).second ;
336  if (max_start < min_end){
337  flag = 1;
338  break;
339  }
340  }
341  if (flag == 0)
342  self_rois_w.at(i+1).push_back(std::make_pair(start3,end3));
343  }
344  }
345  }
346  }
347  }
348  }
349 }
350 
352  double result = 0;
353  if (signal.size()>0){
354  // do quantile ...
355  float par[3];
356  par[0] = WireCell::Waveform::percentile(signal,0.5 - 0.34);
357  par[1] = WireCell::Waveform::percentile(signal,0.5);
358  par[2] = WireCell::Waveform::percentile(signal,0.5 + 0.34);
359  float rms = sqrt((pow(par[2]-par[1],2)+pow(par[1]-par[0],2))/2.);
360 
361  float rms2 = 0;
362  float rms1 = 0;
363  for(size_t i =0; i!=signal.size();i++){
364  if (fabs(signal.at(i)) < 5.0 * rms){
365  rms1 += pow(signal.at(i),2);
366  rms2 ++;
367  }
368  }
369  if (rms2 >0){
370  result = sqrt(rms1/rms2);
371  }
372  }
373 
374  return result;
375 }
376 
377 void ROI_formation::find_ROI_by_decon_itself(int plane, const Array::array_xxf& r_data, const Array::array_xxf& r_data_tight){
378 
379  int offset=0;
380  if (plane==0){
381  offset = 0;
382  }else if (plane==1){
383  offset = nwire_u;
384  }else if (plane==2){
385  offset = nwire_u + nwire_v;
386  }
387 
388  for (int irow = 0; irow!=r_data.rows(); irow++){
389  // calclulate rms for a row of r_data
390  Waveform::realseq_t signal(nbins);
391  Waveform::realseq_t signal1(nbins);
392  Waveform::realseq_t signal2(nbins);
393 
394  if (bad_ch_map.find(irow+offset)!=bad_ch_map.end()){
395  int ncount = 0;
396  for (int icol=0;icol!=r_data.cols();icol++){
397  bool flag = true;
398  for (size_t i=0; i!=bad_ch_map[irow+offset].size(); i++){
399  if (icol >= bad_ch_map[irow+offset].at(i).first &&
400  icol <= bad_ch_map[irow+offset].at(i).second){
401  flag = false;
402  break;
403  }
404  }
405  if (flag){
406  signal.at(ncount) = r_data(irow,icol);
407  signal1.at(icol) = r_data(irow,icol);
408  signal2.at(icol) = r_data_tight(irow,icol);
409  ncount ++;
410  }else{
411  signal1.at(icol) = 0;
412  signal2.at(icol) = 0;
413  }
414  }
415  signal.resize(ncount);
416  }else{
417  for (int icol = 0; icol!= r_data.cols(); icol++){
418  signal.at(icol) = r_data(irow,icol);
419  signal1.at(icol) = r_data(irow,icol);
420  signal2.at(icol) = r_data_tight(irow,icol);
421  }
422  }
423  // do threshold and fill rms
424  double rms = cal_RMS(signal);
425  double threshold = 0;
426  if (plane==0){
427  threshold = th_factor_ind * rms + 1;
428  uplane_rms.at(irow) = rms;
429  }else if (plane==1){
430  threshold = th_factor_ind * rms + 1;
431  vplane_rms.at(irow) = rms;
432  }else if (plane==2){
433  threshold = th_factor_col * rms + 1;
434  wplane_rms.at(irow) = rms;
435  }
436 
437  // std::cout << plane << " " << signal.size() << " " << irow << " " << rms << std::endl;
438 
439  // create rois
440  int roi_begin=-1;
441  int roi_end=-1;
442 
443  std::vector<std::pair<int,int>> temp_rois;
444  // now find ROI, above five sigma, and pad with +- six time ticks
445  for (int j=0;j<int(signal1.size())-1;j++){
446  double content = signal1.at(j);
447  double content_tight = signal2.at(j);
448 
449  if (content > threshold ||
450  (content_tight > threshold )){
451  roi_begin = j;
452  roi_end = j;
453  for (int k=j+1;k< int(signal1.size());k++){
454  if (signal1.at(k) > threshold ||
455  (signal2.at(k) > threshold)){
456  roi_end = k;
457  }else{
458  break;
459  }
460  }
461  int temp_roi_begin = roi_begin ; // filter_pad;
462  if (temp_roi_begin <0 ) temp_roi_begin = 0;
463  int temp_roi_end = roi_end ; // filter_pad;
464  if (temp_roi_end >int(signal1.size())-1) temp_roi_end = int(signal1.size())-1;
465 
466  // if (abs(irow-1199)<=1&&plane==0) std::cout << "Tight: " << irow << " " << temp_roi_begin << " " << temp_roi_end << std::endl;
467 
468 
469  if (temp_rois.size() == 0){
470  temp_rois.push_back(std::make_pair(temp_roi_begin,temp_roi_end));
471  }else{
472  if (temp_roi_begin <= temp_rois.back().second){
473  temp_rois.back().second = temp_roi_end;
474  }else{
475  temp_rois.push_back(std::make_pair(temp_roi_begin,temp_roi_end));
476  }
477  }
478  j = roi_end + 1;
479  }
480  }
481 
482  // if (plane==2 && irow == 69){
483  // std::cout << "Xin: " << irow << " " << rms << " " << temp_rois.size() << std::endl;
484  // for (size_t i=0;i!=temp_rois.size();i++){
485  // std::cout << "Xin: " << temp_rois.at(i).first << " " << temp_rois.at(i).second << std::endl;
486  // }
487  // }
488 
489 
490  // fill rois ...
491  if (plane==0){
492  self_rois_u.at(irow) = temp_rois;
493  }else if (plane==1){
494  self_rois_v.at(irow) = temp_rois;
495  }else{
496  self_rois_w.at(irow) = temp_rois;
497  }
498  // std::cout << plane << " " << irow << " " << temp_rois.size() << std::endl;
499  }
500 
501  extend_ROI_self(plane);
502 
503  // for (size_t i=0;i!=self_rois_w.at(69).size();i++){
504  // std::cout << "Xin: " << self_rois_w.at(69).at(i).first << " " << self_rois_w.at(69).at(i).second << std::endl;
505  // }
506 
508 }
509 
511  find_ROI_by_decon_itself(plane, r_data,r_data);
512 }
513 
514 
516 
517  if (plane==0){
518  // compare the loose one with tight one
519  for(int i=0;i!=nwire_u;i++){
520  std::vector<std::pair<int,int>> temp_rois;
521  for (size_t j=0;j!=loose_rois_u.at(i).size();j++){
522  int start = loose_rois_u.at(i).at(j).first;
523  int end = loose_rois_u.at(i).at(j).second;
524  for (size_t k=0;k!=self_rois_u.at(i).size();k++){
525  int temp_start = self_rois_u.at(i).at(k).first;
526  int temp_end = self_rois_u.at(i).at(k).second;
527  if (start > temp_start && start < temp_end)
528  start = temp_start;
529  // loop through all the tight one to examine start
530  if (end > temp_start && end < temp_end)
531  end = temp_end;
532  // loop through all the tight one to examine the end
533  }
534  if (temp_rois.size()==0){
535  temp_rois.push_back(std::make_pair(start,end));
536  }else{
537  if (start < temp_rois.back().second){
538  temp_rois.back().second = end;
539  }else{
540  temp_rois.push_back(std::make_pair(start,end));
541  }
542  }
543  }
544  loose_rois_u.at(i) = temp_rois;
545  }
546  }else if (plane==1){
547  for(int i=0;i!=nwire_v;i++){
548  std::vector<std::pair<int,int>> temp_rois;
549  for (size_t j=0;j!=loose_rois_v.at(i).size();j++){
550  int start = loose_rois_v.at(i).at(j).first;
551  int end = loose_rois_v.at(i).at(j).second;
552  for (size_t k=0;k!=self_rois_v.at(i).size();k++){
553  int temp_start = self_rois_v.at(i).at(k).first;
554  int temp_end = self_rois_v.at(i).at(k).second;
555  if (start > temp_start && start < temp_end)
556  start = temp_start;
557  // loop through all the tight one to examine start
558  if (end > temp_start && end < temp_end)
559  end = temp_end;
560  // loop through all the tight one to examine the end
561  }
562  if (temp_rois.size()==0){
563  temp_rois.push_back(std::make_pair(start,end));
564  }else{
565  if (start < temp_rois.back().second){
566  temp_rois.back().second = end;
567  }else{
568  temp_rois.push_back(std::make_pair(start,end));
569  }
570  }
571  }
572  loose_rois_v.at(i) = temp_rois;
573  }
574  }
575 
576 }
577 
578 
580  double sum1 = 0;
581  double sum2 = 0;
582 
583  for (int i=-width;i<width+1;i++){
584  int current_bin = bin + i;
585 
586  while (current_bin <0)
587  current_bin += signal.size();
588  while (current_bin >= int(signal.size()))
589  current_bin -= signal.size();
590 
591  sum1 += signal.at(current_bin);
592  sum2 ++;
593  }
594 
595  if (sum2>0){
596  return sum1/sum2;
597  }else{
598  return 0;
599  }
600 }
601 
602 
603 int ROI_formation::find_ROI_end(Waveform::realseq_t& signal, int bin, double th , int jump_one_bin ){
604  int end = bin;
605  double content = signal.at(end);
606  while(content>th){
607  end ++;
608  if (end >=int(signal.size())){
609  content = signal.at(end-signal.size());
610  }else{
611  content = signal.at(end);
612  }
613  if (end >= int(signal.size())-1) {
614  end = int(signal.size())-1;
615  break;
616  }
617  }
618 
619  while(local_ave(signal,end+1,1) < local_ave(signal,end,1)+25 ||
620  (jump_one_bin && local_ave(signal,end+2,1) < local_ave(signal,end,1)+25 )
621  ){// ||
622  // (local_ave(signal,end+1,1) + local_ave(signal,end+2,1))*0.5 < local_ave(signal,end,1) ){
623  end++;
624  if (end >= int(signal.size())-1) {
625  end = int(signal.size())-1;
626  break;
627  }
628  }
629  return end;
630 
631 }
632 int ROI_formation::find_ROI_begin(Waveform::realseq_t& signal, int bin, double th, int jump_one_bin ){
633  // find the first one before bin and is below threshold ...
634  int begin = bin;
635  double content = signal.at(begin);
636  while(content > th){
637  begin --;
638  if (begin <0){
639  content = signal.at(begin + int(signal.size()));
640  }else{
641  content = signal.at(begin);
642  }
643  if (begin <= 0) {
644  begin = 0;
645  break;
646  }
647  }
648 
649  // calculate the local average
650  // keep going and find the minimum
651  while( local_ave(signal,begin-1,1) < local_ave(signal,begin,1)+25 ||
652  (jump_one_bin && local_ave(signal,begin-2,1) < local_ave(signal,begin,1)+25)
653  ){// ||
654  // (local_ave(signal,begin-2,1) + local_ave(signal,begin-1,1))*0.5 < local_ave(signal,begin,1) ){
655  begin --;
656  if (begin <= 0) {
657  begin = 0;
658  break;
659  }
660  }
661 
662  return begin;
663 }
664 
665 
666 void ROI_formation::find_ROI_loose(int plane, const Array::array_xxf& r_data){
667  int offset=0;
668  if (plane==0){
669  offset = 0;
670  }else if (plane==1){
671  offset = nwire_u;
672  }else if (plane==2){
673  offset = nwire_u + nwire_v;
674  }
675 
676 
677 
678  // form rebinned waveform ...
679  for (int irow =0; irow!=r_data.rows();irow++){
680  Waveform::realseq_t signal(nbins); // remove bad ones
681  Waveform::realseq_t signal1(nbins); // all signal
682  Waveform::realseq_t signal2(int(nbins/rebin)); // rebinned ones
683 
684  //std::cout << "xin1" << std::endl;
685 
686  if (bad_ch_map.find(irow+offset)!=bad_ch_map.end()){
687  int ncount = 0;
688  for (int icol=0;icol!=r_data.cols();icol++){
689  bool flag = true;
690  for (size_t i=0; i!=bad_ch_map[irow+offset].size(); i++){
691  if (icol >= bad_ch_map[irow+offset].at(i).first &&
692  icol <= bad_ch_map[irow+offset].at(i).second){
693  flag = false;
694  break;
695  }
696  }
697  if (flag){
698  signal.at(ncount) = r_data(irow,icol);
699  signal1.at(icol) = r_data(irow,icol);
700  ncount ++;
701  }else{
702  signal1.at(icol) = 0;
703  }
704  }
705  signal.resize(ncount);
706  }else{
707  for (int icol = 0; icol!= r_data.cols(); icol++){
708  signal.at(icol) = r_data(irow,icol);
709  signal1.at(icol) = r_data(irow,icol);
710  }
711  }
712 
713  //std::cout << "xin2" << std::endl;
714 
715  // get rebinned waveform
716  for (size_t i=0;i!=signal2.size();i++){
717  double temp = 0;
718  for (int j=0;j!=rebin;j++){
719  temp += signal1.at(rebin * i + j);
720  }
721  signal2.at(i) = temp;
722  }
723 
724  //std::cout << "xin3" << " " << signal.size() << " " << signal2.size() << std::endl;
725 
726  // calculate rms ...
727  float th = cal_RMS(signal) * rebin * l_factor;
728  //if (irow==1240) std::cout << "a " << l_factor << " " << rebin << " " << th << " " << l_max_th << std::endl;
729  if (th > l_max_th) th = l_max_th;
730 
731 
732  std::vector<std::pair <int,int> > ROIs_1;
733  std::vector<int> max_bins_1;
734  int ntime = signal2.size();
735 
736  // if (irow == 1200 && plane==0){
737  // for (int j=0;j!=ntime;j++){
738  // std::cout << j << " " << signal2.at(j) << " " << th << " " << l_factor1 << " " << std::endl;
739  // }
740  // }
741 
742  for (int j=1; j<ntime-1;j++){
743  double content = signal2.at(j);
744  double prev_content = signal2.at(j-1);
745  double next_content = signal2.at(j+1);
746  int flag_ROI = 0;
747  int begin=0;
748  int end=0;
749  int max_bin=0;
750  if (content > th){
751  begin = find_ROI_begin(signal2,j, th*l_factor1, l_jump_one_bin) ;
752  end = find_ROI_end(signal2,j, th*l_factor1, l_jump_one_bin) ;
753  max_bin = begin;
754  // if (irow==1240) std::cout << "a: " << begin << " " << end << " " << j << std::endl;
755  for (int k=begin;k<=end;k++){
756  //std::cout << begin << " " << end << " " << max_bin << std::endl;
757  if (signal2.at(k) > signal2.at(max_bin)){
758  max_bin = k;
759  }
760  }
761  flag_ROI = 1;
762  }else{
763  if (content > prev_content && content > next_content){
764  begin = find_ROI_begin(signal2,j, prev_content, l_jump_one_bin);
765  end = find_ROI_end(signal2,j, next_content , l_jump_one_bin);
766  max_bin = begin;
767  for (int k=begin;k<=end;k++){
768  if (signal2.at(k) > signal2.at(max_bin)){
769  max_bin = k;
770  }
771  }
772  if (signal2.at(max_bin) - signal2.at(begin) + signal2.at(max_bin) - signal2.at(end) > th * 2){
773  flag_ROI = 1;
774  }
775 
776 
777  int temp_begin = max_bin-l_short_length;
778  if (temp_begin < begin) temp_begin = begin;
779  int temp_end = max_bin + l_short_length;
780  if (temp_end > end) temp_end = end;
781  if ((signal2.at(max_bin) - signal2.at(temp_begin) > th * l_factor1 &&
782  signal2.at(max_bin) - signal2.at(temp_end) > th * l_factor1)){
783  flag_ROI = 1;
784  }
785 
786  // if (irow==1200 && plane==0)
787  // std::cout << j << " " << begin << " " << end << " " << max_bin << " " << signal2.at(max_bin) * 2 - signal2.at(begin) - signal2.at(end) << " " << th*2 << " " << temp_begin << " " << temp_end << " " << signal2.at(max_bin) - signal2.at(temp_begin) << " " << signal2.at(max_bin) - signal2.at(temp_end) << " " << th*l_factor1 << " " << flag_ROI << std::endl;
788 
789  }
790  }
791 
792 
793 
794  if (flag_ROI == 1){
795  // if (irow==1240) {
796  // std::cout << begin << " " << end << " " << j << " " << content << " " << th << " " << prev_content << " " << next_content << std::endl;
797  // for (int kk = begin; kk!=end; kk++){
798  // std::cout << kk << " a " << signal2.at(kk) << " " << th*l_factor1 << " " << local_ave(signal2,kk,1) << std::endl;
799  // for (int kkk=0;kkk!=6;kkk++){
800  // std::cout << "b " << signal.at(kk*6+kkk) << std::endl;
801  // }
802  // }
803  // }
804 
805  if (ROIs_1.size()>0){
806  if (begin <= ROIs_1.back().second){
807  ROIs_1.back().second = end;
808  if (signal2.at(max_bin) > signal2.at(max_bins_1.back()))
809  max_bins_1.back() = max_bin;
810  }else{
811  ROIs_1.push_back(std::make_pair(begin,end));
812  max_bins_1.push_back(max_bin);
813  }
814  }else{
815  ROIs_1.push_back(std::make_pair(begin,end));
816  max_bins_1.push_back(max_bin);
817  }
818 
819  if (end < int(signal2.size())){
820  j = end;
821  }else{
822  j = signal2.size();
823  }
824  }
825  }
826 
827  //std::cout << "xin4" << std::endl;
828 
829 
830  // for (int j = 0; j!=int(ROIs_1.size());j++){
831  // int begin = ROIs_1.at(j).first * rebin;
832  // int end = ROIs_1.at(j).second *rebin + (rebin-1);
833  // if (irow ==1240) std::cout << "b " << begin << " " << end << std::endl;
834  // }
835 
836 
837 
838  if (ROIs_1.size()==1){
839  }else if (ROIs_1.size()>1){
840  int flag_repeat = 0;
841  // cout << "Xin1: " << ROIs_1.size() << endl;;
842  while(flag_repeat){
843  flag_repeat = 1;
844  for (int k=0;k<int(ROIs_1.size()-1);k++){
845  int begin = ROIs_1.at(k).first;
846  int end = ROIs_1.at(k+1).second;
847 
848  double begin_content = signal2.at(begin);
849  double end_content = signal2.at(end);
850 
851  int begin_1 = ROIs_1.at(k).second;
852  int end_1 = ROIs_1.at(k+1).first;
853 
854  int flag_merge = 1;
855  //Double_t sum1 = 0, sum2 = 0;
856  for (int j=begin_1; j<=end_1;j++){
857  double current_content = signal2.at(j);
858  double content = current_content - ((end_content - begin_content)*(j*1.-begin)/(end-begin*1.) + begin_content);
859 
860  if (content < th*l_factor1){
861  flag_merge = 0;
862  break;
863  }
864  // sum1 += content;
865  // sum2 ++;
866  // cout << j << " " << content << endl;
867  }
868  // if (sum2 >0){
869  // if (sum1/sum2 < th*factor1) flag_merge = 0;
870  // }
871 
872  if (flag_merge == 1){
873  ROIs_1.at(k).second = ROIs_1.at(k+1).second;
874  ROIs_1.erase(ROIs_1.begin()+k+1);
875  flag_repeat = 1;
876  break;
877  }
878  }
879  // cout << "Xin2: " << ROIs_1.size() << endl;
880 
881  }
882  }
883 
884  //std::cout << "xin5" << std::endl;
885  // scale back ...
886  for (int j = 0; j!=int(ROIs_1.size());j++){
887  int begin = ROIs_1.at(j).first * rebin;
888  int end = ROIs_1.at(j).second *rebin + (rebin-1);
889 
890  ROIs_1.at(j).first = begin;
891  ROIs_1.at(j).second = end;
892 
893  //if (abs(irow-1199)<=1&& plane==0) std::cout << "Loose: " << irow << " " << ROIs_1.at(j).first << " " << ROIs_1.at(j).second << std::endl;
894  }
895 
896 
897 
898  if (plane==0){
899  loose_rois_u.at(irow) = ROIs_1;
900  }else if (plane==1){
901  loose_rois_v.at(irow) = ROIs_1;
902  }else{
903  loose_rois_w.at(irow) = ROIs_1;
904  }
905  //std::cout << plane << " " << irow << " " << ROIs_1.size() << std::endl;
906  }
907 
908  // std::cout << "xin6" << std::endl;
909 
910  extend_ROI_loose(plane);
911  //std::cout << "xin7" << std::endl;
912 }
std::vector< std::vector< std::pair< int, int > > > self_rois_u
Definition: ROI_formation.h:85
static QCString result
Sequence< real_t > realseq_t
Definition: Waveform.h:31
int find_ROI_end(Waveform::realseq_t &signal, int bin, double th=0, int jump_one_bin=0)
double rms(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:39
std::vector< std::vector< std::pair< int, int > > > loose_rois_w
Definition: ROI_formation.h:91
std::vector< float > uplane_rms
Definition: ROI_formation.h:93
std::vector< std::vector< std::pair< int, int > > > loose_rois_v
Definition: ROI_formation.h:90
Val sum2(const Sequence< Val > &seq)
Return sum of square of all entries in sequence.
Definition: Waveform.h:184
constexpr T pow(T x)
Definition: pow.h:72
std::map< int, std::vector< std::pair< int, int > > > bad_ch_map
Definition: ROI_formation.h:83
std::vector< std::vector< std::pair< int, int > > > loose_rois_u
Definition: ROI_formation.h:89
const double width
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
std::map< std::string, ChannelMasks > ChannelMaskMap
Collect channel masks by some label.
Definition: Waveform.h:59
double cal_RMS(Waveform::realseq_t signal)
std::vector< std::vector< std::pair< int, int > > > self_rois_w
Definition: ROI_formation.h:87
real_t percentile(realseq_t &wave, real_t percentage)
Definition: Waveform.cxx:87
int find_ROI_begin(Waveform::realseq_t &signal, int bin, double th=0, int jump_one_bin=0)
Definition: Main.h:22
void find_ROI_by_decon_itself(int plane, const Array::array_xxf &r_data)
QTextStream & bin(QTextStream &s)
void apply_roi(int plane, Array::array_xxf &r_data, int flag)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:67
double local_ave(Waveform::realseq_t &signal, int bin, int width)
def rebin(a, args)
Definition: arrays.py:2
ROI_formation(Waveform::ChannelMaskMap &cmm, int nwire_u, int nwire_v, int nwire_w, int nbins=9594, float th_factor_ind=3, float th_factor_col=5, int pad=5, float asy=0.1, int rebin=6, double l_factor=3.5, double l_max_th=10000, double l_factor1=0.7, int l_short_length=3, int l_jump_one_bin=0)
std::vector< std::vector< std::pair< int, int > > > self_rois_v
Definition: ROI_formation.h:86
std::vector< float > vplane_rms
Definition: ROI_formation.h:94
std::vector< float > wplane_rms
Definition: ROI_formation.h:95
Eigen::ArrayXXf array_xxf
A real, 2D array.
Definition: Array.h:54
void find_ROI_loose(int plane, const Array::array_xxf &r_data)