ROI_refinement.cxx
Go to the documentation of this file.
1 #include "ROI_refinement.h"
2 #include "PeakFinding.h"
3 #include <iostream>
4 #include <set>
5 
6 using namespace WireCell;
7 using namespace WireCell::SigProc;
8 
9 ROI_refinement::ROI_refinement(Waveform::ChannelMaskMap& cmm,int nwire_u, int nwire_v, int nwire_w, float th_factor, float fake_signal_low_th, float fake_signal_high_th, float fake_signal_low_th_ind_factor, float fake_signal_high_th_ind_factor, int pad, int break_roi_loop, float th_peak, float sep_peak, float low_peak_sep_threshold_pre, int max_npeaks, float sigma, float th_percent)
10  : nwire_u(nwire_u)
11  , nwire_v(nwire_v)
12  , nwire_w(nwire_w)
13  , th_factor(th_factor)
14  , fake_signal_low_th(fake_signal_low_th)
15  , fake_signal_high_th(fake_signal_high_th)
16  , fake_signal_low_th_ind_factor(fake_signal_low_th_ind_factor)
17  , fake_signal_high_th_ind_factor(fake_signal_high_th_ind_factor)
18  , pad(pad)
19  , break_roi_loop(break_roi_loop)
20  , th_peak(th_peak)
21  , sep_peak(sep_peak)
22  , low_peak_sep_threshold_pre(low_peak_sep_threshold_pre)
23  , max_npeaks(max_npeaks)
24  , sigma(sigma)
25  , th_percent(th_percent)
26  , log(Log::logger("sigproc"))
27 {
28  rois_u_tight.resize(nwire_u);
29  rois_u_loose.resize(nwire_u);
30 
31  rois_v_tight.resize(nwire_v);
32  rois_v_loose.resize(nwire_v);
33 
34  rois_w_tight.resize(nwire_w);
35 
36  for (int i=0;i!=nwire_u;i++){
37  SignalROIList temp_rois;
38  rois_u_tight.at(i) = temp_rois;
39  }
40  for (int i=0;i!=nwire_v;i++){
41  SignalROIList temp_rois;
42  rois_v_tight.at(i) = temp_rois;
43  }
44  for (int i=0;i!=nwire_w;i++){
45  SignalROIList temp_rois;
46  rois_w_tight.at(i) = temp_rois;
47  }
48 
49  for (int i=0;i!=nwire_u;i++){
50  SignalROIList temp_rois;
51  rois_u_loose.at(i) = temp_rois;
52  }
53  for (int i=0;i!=nwire_v;i++){
54  SignalROIList temp_rois;
55  rois_v_loose.at(i) = temp_rois;
56  }
57 
58  for (auto it = cmm["bad"].begin(); it!=cmm["bad"].end(); it++){
59  int ch = it->first;
60  std::vector<std::pair<int,int>> temps;
61  bad_ch_map[ch] = temps;
62  for (size_t ind = 0; ind < it->second.size(); ind++){
63  bad_ch_map[ch].push_back(std::make_pair(it->second.at(ind).first, it->second.at(ind).second));
64  //std::cout << ch << " " << << std::endl;
65  }
66  }
67 
68 }
69 
71  Clear();
72 }
73 
75  for (int i=0;i!=nwire_u;i++){
76  for (auto it = rois_u_tight.at(i).begin(); it!=rois_u_tight.at(i).end();it++){
77  delete *it;
78  }
79  rois_u_tight.at(i).clear();
80  for (auto it = rois_u_loose.at(i).begin(); it!=rois_u_loose.at(i).end();it++){
81  delete *it;
82  }
83  rois_u_loose.at(i).clear();
84  }
85 
86  for (int i=0;i!=nwire_v;i++){
87  for (auto it = rois_v_tight.at(i).begin(); it!=rois_v_tight.at(i).end();it++){
88  delete *it;
89  }
90  rois_v_tight.at(i).clear();
91  for (auto it = rois_v_loose.at(i).begin(); it!=rois_v_loose.at(i).end();it++){
92  delete *it;
93  }
94  rois_v_loose.at(i).clear();
95  }
96 
97  for (int i=0;i!=nwire_w;i++){
98  for (auto it = rois_w_tight.at(i).begin(); it!=rois_w_tight.at(i).end();it++){
99  delete *it;
100  }
101  rois_w_tight.at(i).clear();
102  }
103 
104  front_rois.clear();
105  back_rois.clear();
106  contained_rois.clear();
107 }
108 
110  if (plane==0){
111  for (int irow = 0 ; irow != r_data.rows(); irow++){
112  //refresh ...
113  Waveform::realseq_t signal(r_data.cols(),0);
114  // loop ROIs and assign data
115  for (auto it = rois_u_loose.at(irow).begin(); it!= rois_u_loose.at(irow).end(); it++){
116  SignalROI *roi = *it;
117  // int start_bin = roi->get_start_bin();
118  // int end_bin = roi->get_end_bin();
119 
120  int start_bin = roi->get_ext_start_bin();
121  int end_bin = roi->get_ext_end_bin();
122 
123  float start_content = r_data(irow,start_bin);
124  float end_content = r_data(irow,end_bin);
125  for (int i=start_bin; i<end_bin+1; i++){
126  int content = r_data(irow,i) - ((end_content - start_content)*(i-start_bin)/(end_bin-start_bin) + start_content);
127  signal.at(i) = content;
128  // htemp_signal->SetBinContent(i+1,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  }else if (plane==1){
137  for (int irow = 0 ; irow != r_data.rows(); irow++){
138  //refresh ...
139  Waveform::realseq_t signal(r_data.cols(),0);
140  // loop ROIs and assign data
141  for (auto it = rois_v_loose.at(irow).begin(); it!= rois_v_loose.at(irow).end(); it++){
142  SignalROI *roi = *it;
143  //int start_bin = roi->get_start_bin();
144  //int end_bin = roi->get_end_bin();
145 
146  int start_bin = roi->get_ext_start_bin();
147  int end_bin = roi->get_ext_end_bin();
148 
149  float start_content = r_data(irow,start_bin);
150  float end_content = r_data(irow,end_bin);
151  for (int i=start_bin; i<end_bin+1; i++){
152  int content = r_data(irow,i) - ((end_content - start_content)*(i-start_bin)/(end_bin-start_bin) + start_content);
153  signal.at(i) = content;
154  //htemp_signal->SetBinContent(i+1,content);
155  }
156  }
157  // load data back ..
158  for (int icol = 0; icol!=r_data.cols();icol++){
159  r_data(irow,icol) = signal.at(icol);
160  }
161  }
162  }else{
163  for (int irow = 0 ; irow != r_data.rows(); irow++){
164  //refresh ...
165  Waveform::realseq_t signal(r_data.cols(),0);
166 
167  // if (irow==69){
168  // std::cout << "Xin: " << rois_w_tight.at(irow).size() << " " << std::endl;
169  // for (auto it = rois_w_tight.at(irow).begin(); it!= rois_w_tight.at(irow).end(); it++){
170  // SignalROI *roi = *it;
171  // int start_bin = roi->get_ext_start_bin();
172  // int end_bin = roi->get_ext_end_bin();
173  // std::cout << "Xin: " << start_bin << " " << end_bin << std::endl;
174  // }
175  // }
176 
177  // loop ROIs and assign data
178  for (auto it = rois_w_tight.at(irow).begin(); it!= rois_w_tight.at(irow).end(); it++){
179  SignalROI *roi = *it;
180  //int start_bin = roi->get_start_bin();
181  //int end_bin = roi->get_end_bin();
182 
183  int start_bin = roi->get_ext_start_bin();
184  int end_bin = roi->get_ext_end_bin();
185 
186  // float start_content = r_data(irow,start_bin);
187  // float end_content = r_data(irow,end_bin);
188  for (int i=start_bin; i<end_bin+1; i++){
189  int content = r_data(irow,i);// - ((end_content - start_content)*(i-start_bin)/(end_bin-start_bin) + start_content);
190  signal.at(i) = content;
191  }
192  }
193  // load data back ..
194  for (int icol = 0; icol!=r_data.cols();icol++){
195  r_data(irow,icol) = signal.at(icol);
196  }
197  }
198  }
199 }
200 
201 // helper function for getting ROIs
203 {
204  if (plane==0) return get_u_rois();
205  else if (plane==1) return get_v_rois();
206  else return get_w_rois();
207 }
208 
209 void ROI_refinement::unlink(SignalROI* prev_roi, SignalROI* next_roi){
210  if (front_rois.find(prev_roi)!=front_rois.end()){
211  SignalROISelection& temp_rois = front_rois[prev_roi];
212  auto it = find(temp_rois.begin(),temp_rois.end(),next_roi);
213  if (it != temp_rois.end())
214  temp_rois.erase(it);
215  }
216  if (back_rois.find(next_roi)!=back_rois.end()){
217  SignalROISelection& temp_rois = back_rois[next_roi];
218  auto it = find(temp_rois.begin(),temp_rois.end(),prev_roi);
219  if (it != temp_rois.end())
220  temp_rois.erase(it);
221  }
222 }
223 
224 void ROI_refinement::link(SignalROI* prev_roi, SignalROI* next_roi){
225  if (front_rois.find(prev_roi)!=front_rois.end()){
226  SignalROISelection& temp_rois = front_rois[prev_roi];
227  auto it = find(temp_rois.begin(),temp_rois.end(),next_roi);
228  if (it == temp_rois.end())
229  temp_rois.push_back(next_roi);
230  }else{
231  SignalROISelection temp_rois;
232  temp_rois.push_back(next_roi);
233  front_rois[prev_roi] = temp_rois;
234  }
235 
236  if (back_rois.find(next_roi)!=back_rois.end()){
237  SignalROISelection& temp_rois = back_rois[next_roi];
238  auto it = find(temp_rois.begin(),temp_rois.end(),prev_roi);
239  if (it == temp_rois.end())
240  temp_rois.push_back(prev_roi);
241  }else{
242  SignalROISelection temp_rois;
243  temp_rois.push_back(prev_roi);
244  back_rois[next_roi] = temp_rois;
245  }
246 }
247 
248 void ROI_refinement::load_data(int plane, const Array::array_xxf& r_data, ROI_formation& roi_form){
249  // fill RMS
250  std::vector<float> plane_rms;
251  int offset = 0;
252  if (plane==0){
253  offset = 0;
254  plane_rms = roi_form.get_uplane_rms();
255  }else if (plane==1){
256  offset = nwire_u;
257  plane_rms = roi_form.get_vplane_rms();
258  }else if (plane==2){
259  offset = nwire_u+nwire_v;
260  plane_rms = roi_form.get_wplane_rms();
261  }
262 
263  // load data ...
264  for (int irow = 0; irow!=r_data.rows(); irow++){
265  Waveform::realseq_t signal(r_data.cols());
266  if (bad_ch_map.find(irow+offset)!=bad_ch_map.end()){
267  for (int icol=0;icol!=r_data.cols();icol++){
268  bool flag = true;
269  for (size_t i=0; i!=bad_ch_map[irow+offset].size(); i++){
270  if (icol >= bad_ch_map[irow+offset].at(i).first &&
271  icol <= bad_ch_map[irow+offset].at(i).second){
272  flag = false;
273  break;
274  }
275  }
276  if (flag){
277  signal.at(icol) = r_data(irow,icol);
278  }else{
279  signal.at(icol) = 0;
280  }
281  }
282  }else{
283  for (int icol = 0; icol!= r_data.cols(); icol++){
284  signal.at(icol) = r_data(irow,icol);
285  }
286  }
287 
288  int chid = irow+offset;
289  // load tight rois
290  std::vector<std::pair<int,int>>& uboone_rois = roi_form.get_self_rois(irow+offset);
291  for (size_t i=0;i!=uboone_rois.size();i++){
292  SignalROI *tight_roi = new SignalROI(plane,irow+offset, uboone_rois.at(i).first,uboone_rois.at(i).second, signal);
293  float threshold = plane_rms.at(irow) * th_factor;
294  if (tight_roi->get_above_threshold(threshold).size()==0) {
295  delete tight_roi;
296  continue;
297  }
298 
299  if (plane==0){
300  rois_u_tight[chid].push_back(tight_roi);
301  if (chid>0){
302  //form connectivity map
303  for (auto it = rois_u_tight[chid-1].begin();it!=rois_u_tight[chid-1].end();it++){
304  SignalROI *prev_roi = *it;
305  if (tight_roi->overlap(prev_roi)){
306  if (front_rois.find(prev_roi) == front_rois.end()){
307  SignalROISelection temp_rois;
308  temp_rois.push_back(tight_roi);
309  front_rois[prev_roi] = temp_rois;
310  }else{
311  front_rois[prev_roi].push_back(tight_roi);
312  }
313  if (back_rois.find(tight_roi) == back_rois.end()){
314  SignalROISelection temp_rois;
315  temp_rois.push_back(prev_roi);
316  back_rois[tight_roi] = temp_rois;
317  }else{
318  back_rois[tight_roi].push_back(prev_roi);
319  }
320  }
321  }
322  }
323  if (chid < nwire_u-1){
324  // add the code for the next one to be completed
325  for (auto it = rois_u_tight[chid+1].begin();it!=rois_u_tight[chid+1].end();it++){
326  SignalROI *next_roi = *it;
327  if (tight_roi->overlap(next_roi)){
328  if (back_rois.find(next_roi) == back_rois.end()){
329  SignalROISelection temp_rois;
330  temp_rois.push_back(tight_roi);
331  back_rois[next_roi] = temp_rois;
332  }else{
333  back_rois[next_roi].push_back(tight_roi);
334  }
335 
336  if (front_rois.find(tight_roi) == front_rois.end()){
337  SignalROISelection temp_rois;
338  temp_rois.push_back(next_roi);
339  front_rois[tight_roi] = temp_rois;
340  }else{
341  front_rois[tight_roi].push_back(next_roi);
342  }
343  }
344  }
345  }
346  }else if (plane==1){
347  rois_v_tight[chid-nwire_u].push_back(tight_roi);
348  if (chid>nwire_u){
349  //form connectivity map
350  for (auto it = rois_v_tight[chid - nwire_u-1].begin();it!=rois_v_tight[chid-nwire_u-1].end();it++){
351  SignalROI *prev_roi = *it;
352  if (tight_roi->overlap(prev_roi)){
353  if (front_rois.find(prev_roi) == front_rois.end()){
354  SignalROISelection temp_rois;
355  temp_rois.push_back(tight_roi);
356  front_rois[prev_roi] = temp_rois;
357  }else{
358  front_rois[prev_roi].push_back(tight_roi);
359  }
360  if (back_rois.find(tight_roi) == back_rois.end()){
361  SignalROISelection temp_rois;
362  temp_rois.push_back(prev_roi);
363  back_rois[tight_roi] = temp_rois;
364  }else{
365  back_rois[tight_roi].push_back(prev_roi);
366  }
367  }
368  }
369  }
370 
371  if (chid<nwire_u+nwire_v-1){
372  //form connectivity map
373  for (auto it = rois_v_tight[chid - nwire_u+1].begin();it!=rois_v_tight[chid-nwire_u+1].end();it++){
374  SignalROI *next_roi = *it;
375  if (tight_roi->overlap(next_roi)){
376  if (back_rois.find(next_roi) == back_rois.end()){
377  SignalROISelection temp_rois;
378  temp_rois.push_back(tight_roi);
379  back_rois[next_roi] = temp_rois;
380  }else{
381  back_rois[next_roi].push_back(tight_roi);
382  }
383  if (front_rois.find(tight_roi) == front_rois.end()){
384  SignalROISelection temp_rois;
385  temp_rois.push_back(next_roi);
386  front_rois[tight_roi] = temp_rois;
387  }else{
388  front_rois[tight_roi].push_back(next_roi);
389  }
390  }
391  }
392  }
393  }else {
394  rois_w_tight[chid-nwire_u-nwire_v].push_back(tight_roi);
395 
396  if (chid>nwire_u+nwire_v){
397  //form connectivity map
398  for (auto it = rois_w_tight[chid-nwire_u-nwire_v-1].begin();it!=rois_w_tight[chid-nwire_u-nwire_v-1].end();it++){
399  SignalROI *prev_roi = *it;
400  if (tight_roi->overlap(prev_roi)){
401  if (front_rois.find(prev_roi) == front_rois.end()){
402  SignalROISelection temp_rois;
403  temp_rois.push_back(tight_roi);
404  front_rois[prev_roi] = temp_rois;
405  }else{
406  front_rois[prev_roi].push_back(tight_roi);
407  }
408  if (back_rois.find(tight_roi) == back_rois.end()){
409  SignalROISelection temp_rois;
410  temp_rois.push_back(prev_roi);
411  back_rois[tight_roi] = temp_rois;
412  }else{
413  back_rois[tight_roi].push_back(prev_roi);
414  }
415  }
416  }
417  }
418 
419 
420  if (chid<nwire_u+nwire_v+nwire_w-1){
421  //form connectivity map
422  for (auto it = rois_w_tight[chid-nwire_u-nwire_v+1].begin();it!=rois_w_tight[chid-nwire_u-nwire_v+1].end();it++){
423  SignalROI *next_roi = *it;
424  if (tight_roi->overlap(next_roi)){
425  if (back_rois.find(next_roi) == back_rois.end()){
426  SignalROISelection temp_rois;
427  temp_rois.push_back(tight_roi);
428  back_rois[next_roi] = temp_rois;
429  }else{
430  back_rois[next_roi].push_back(tight_roi);
431  }
432  if (front_rois.find(tight_roi) == front_rois.end()){
433  SignalROISelection temp_rois;
434  temp_rois.push_back(next_roi);
435  front_rois[tight_roi] = temp_rois;
436  }else{
437  front_rois[tight_roi].push_back(next_roi);
438  }
439  }
440  }
441  }
442  }
443 
444 
445 
446  }// loop over tight rois ...
447 
448  if (plane!=2){
449  uboone_rois = roi_form.get_loose_rois(chid);
450  for (size_t i = 0; i!=uboone_rois.size();i++){
451  SignalROI *loose_roi = new SignalROI(plane,chid,uboone_rois.at(i).first,uboone_rois.at(i).second,signal);
452  float threshold = plane_rms.at(irow) * th_factor;
453  if (loose_roi->get_above_threshold(threshold).size()==0) {
454  delete loose_roi;
455  continue;
456  }
457  if (plane==0){
458  rois_u_loose[chid].push_back(loose_roi);
459 
460  if (chid>0){
461  //form connectivity map
462  for (auto it=rois_u_loose[chid-1].begin();it!=rois_u_loose[chid-1].end();it++){
463  SignalROI *prev_roi = *it;
464  if (loose_roi->overlap(prev_roi)){
465  if (front_rois.find(prev_roi) == front_rois.end()){
466  SignalROISelection temp_rois;
467  temp_rois.push_back(loose_roi);
468  front_rois[prev_roi] = temp_rois;
469  }else{
470  front_rois[prev_roi].push_back(loose_roi);
471  }
472  if (back_rois.find(loose_roi) == back_rois.end()){
473  SignalROISelection temp_rois;
474  temp_rois.push_back(prev_roi);
475  back_rois[loose_roi] = temp_rois;
476  }else{
477  back_rois[loose_roi].push_back(prev_roi);
478  }
479  }
480  }
481  }
482 
483  if (chid<nwire_u-1){
484  //form connectivity map
485  for (auto it=rois_u_loose[chid+1].begin();it!=rois_u_loose[chid+1].end();it++){
486  SignalROI *next_roi = *it;
487  if (loose_roi->overlap(next_roi)){
488  if (back_rois.find(next_roi) == back_rois.end()){
489  SignalROISelection temp_rois;
490  temp_rois.push_back(loose_roi);
491  back_rois[next_roi] = temp_rois;
492  }else{
493  back_rois[next_roi].push_back(loose_roi);
494  }
495  if (front_rois.find(loose_roi) == front_rois.end()){
496  SignalROISelection temp_rois;
497  temp_rois.push_back(next_roi);
498  front_rois[loose_roi] = temp_rois;
499  }else{
500  front_rois[loose_roi].push_back(next_roi);
501  }
502  }
503  }
504  }
505 
506  //form contained map ...
507  for (auto it=rois_u_tight[chid].begin();it!=rois_u_tight[chid].end();it++){
508  SignalROI *tight_roi = *it;
509  if (tight_roi->overlap(loose_roi)){
510  if (contained_rois.find(loose_roi)==contained_rois.end()){
511  SignalROISelection temp_rois;
512  temp_rois.push_back(tight_roi);
513  contained_rois[loose_roi] = temp_rois;
514  }else{
515  contained_rois[loose_roi].push_back(tight_roi);
516  }
517  // if (tight_roi->get_start_bin() < loose_roi->get_start_bin())
518  // std::cout << tight_roi->get_start_bin() << " " << loose_roi->get_start_bin() << " " << tight_roi->get_chid() << " " << tight_roi->get_plane() << std::endl;
519  }
520 
521 
522  }
523  }else if (plane==1){
524  rois_v_loose[chid-nwire_u].push_back(loose_roi);
525 
526  if (chid>nwire_u){
527  //form connectivity map
528  for (auto it = rois_v_loose[chid-nwire_u-1].begin();it!=rois_v_loose[chid-nwire_u-1].end();it++){
529  SignalROI *prev_roi = *it;
530  if (loose_roi->overlap(prev_roi)){
531  if (front_rois.find(prev_roi) == front_rois.end()){
532  SignalROISelection temp_rois;
533  temp_rois.push_back(loose_roi);
534  front_rois[prev_roi] = temp_rois;
535  }else{
536  front_rois[prev_roi].push_back(loose_roi);
537  }
538  if (back_rois.find(loose_roi) == back_rois.end()){
539  SignalROISelection temp_rois;
540  temp_rois.push_back(prev_roi);
541  back_rois[loose_roi] = temp_rois;
542  }else{
543  back_rois[loose_roi].push_back(prev_roi);
544  }
545  }
546  }
547  }
548 
549  if (chid<nwire_u+nwire_v-1){
550  //form connectivity map
551  for (auto it = rois_v_loose[chid-nwire_u+1].begin();it!=rois_v_loose[chid-nwire_u+1].end();it++){
552  SignalROI *next_roi = *it;
553  if (loose_roi->overlap(next_roi)){
554  if (back_rois.find(next_roi) == back_rois.end()){
555  SignalROISelection temp_rois;
556  temp_rois.push_back(loose_roi);
557  back_rois[next_roi] = temp_rois;
558  }else{
559  back_rois[next_roi].push_back(loose_roi);
560  }
561  if (front_rois.find(loose_roi) == front_rois.end()){
562  SignalROISelection temp_rois;
563  temp_rois.push_back(next_roi);
564  front_rois[loose_roi] = temp_rois;
565  }else{
566  front_rois[loose_roi].push_back(next_roi);
567  }
568  }
569  }
570  }
571 
572  //form contained map ...
573  for (auto it = rois_v_tight[chid-nwire_u].begin();it!=rois_v_tight[chid-nwire_u].end();it++){
574  SignalROI *tight_roi = *it;
575  if (tight_roi->overlap(loose_roi)){
576  if (contained_rois.find(loose_roi)==contained_rois.end()){
577  SignalROISelection temp_rois;
578  temp_rois.push_back(tight_roi);
579  contained_rois[loose_roi] = temp_rois;
580  }else{
581  contained_rois[loose_roi].push_back(tight_roi);
582  }
583  }
584  }
585  }
586  }
587  }
588 
589  } // loop over signal rows
590 
591 }
592 
594 
595  // clean up ROIs
596  std::map<SignalROI*, int> ROIsaved_map;
597 
598  if (plane==0){
599  //int counter = 0;
600  for (size_t i=0;i!=rois_u_loose.size();i++){
601  // counter += rois_u_loose.at(i).size();
602  for (auto it = rois_u_loose.at(i).begin(); it!= rois_u_loose.at(i).end();it++){
603  SignalROI *roi = *it;
604  if (ROIsaved_map.find(roi)==ROIsaved_map.end()){
605  if (contained_rois.find(roi) != contained_rois.end()){
606  // contain good stuff
607  SignalROISelection temp_rois;
608  temp_rois.push_back(roi);
609  ROIsaved_map[roi] = 1;
610 
611  while(temp_rois.size()){
612  SignalROI *temp_roi = temp_rois.back();
613  temp_rois.pop_back();
614  // save all its neighbour into a temporary holder
615  if (front_rois.find(temp_roi)!=front_rois.end()){
616  for (auto it1 = front_rois[temp_roi].begin();it1!=front_rois[temp_roi].end();it1++){
617  if (ROIsaved_map.find(*it1)==ROIsaved_map.end()){
618  temp_rois.push_back(*it1);
619  ROIsaved_map[*it1] = 1;
620  }
621  }
622  }
623  if (back_rois.find(temp_roi)!=back_rois.end()){
624  for (auto it1 = back_rois[temp_roi].begin();it1!=back_rois[temp_roi].end();it1++){
625  if (ROIsaved_map.find(*it1)==ROIsaved_map.end()){
626  temp_rois.push_back(*it1);
627  ROIsaved_map[*it1] = 1;
628  }
629  }
630  }
631  }
632  }
633  }
634  }
635  }
636 
637  //remove the bad ones ...
638  //int counter2 = 0;
639  for (size_t i=0;i!=rois_u_loose.size();i++){
640  SignalROISelection to_be_removed;
641  for (auto it = rois_u_loose.at(i).begin(); it!= rois_u_loose.at(i).end();it++){
642  SignalROI *roi = *it;
643  if (ROIsaved_map.find(roi) == ROIsaved_map.end()){
644  // counter2 ++;
645  to_be_removed.push_back(roi);
646  //it = rois_u_loose.at(i).erase(it);
647  // check contained map
648  if (contained_rois.find(roi)!= contained_rois.end()){
649  std::cout << "Wrong! " << std::endl;
650  }
651  // check front map
652  if (front_rois.find(roi)!=front_rois.end()){
653  for (auto it1 = front_rois[roi].begin(); it1 != front_rois[roi].end(); it1++){
654  auto it2 = find(back_rois[*it1].begin(),back_rois[*it1].end(),roi);
655  back_rois[*it1].erase(it2);
656  }
657  front_rois.erase(roi);
658  }
659  // check back map
660  if (back_rois.find(roi)!=back_rois.end()){
661  for (auto it1 = back_rois[roi].begin(); it1!=back_rois[roi].end(); it1++){
662  auto it2 = find(front_rois[*it1].begin(),front_rois[*it1].end(),roi);
663  front_rois[*it1].erase(it2);
664  }
665  back_rois.erase(roi);
666  }
667  }
668  }
669 
670  for (auto it = to_be_removed.begin(); it!= to_be_removed.end(); it++){
671  auto it1 = find(rois_u_loose.at(i).begin(), rois_u_loose.at(i).end(),*it);
672  rois_u_loose.at(i).erase(it1);
673  delete (*it);
674  }
675  }
676  }else if (plane==1){
677 
678  // int counter = 0;
679  for (size_t i=0;i!=rois_v_loose.size();i++){
680  // counter += rois_v_loose.at(i).size();
681  for (auto it = rois_v_loose.at(i).begin(); it!= rois_v_loose.at(i).end();it++){
682  SignalROI *roi = *it;
683  if (ROIsaved_map.find(roi)==ROIsaved_map.end()){
684  if (contained_rois.find(roi) != contained_rois.end()){
685  // contain good stuff
686  SignalROISelection temp_rois;
687  temp_rois.push_back(roi);
688  ROIsaved_map[roi] = 1;
689 
690  while(temp_rois.size()){
691  SignalROI *temp_roi = temp_rois.back();
692  temp_rois.pop_back();
693  // save all its neighbour into a temporary holder
694  if (front_rois.find(temp_roi)!=front_rois.end()){
695  for (auto it1 = front_rois[temp_roi].begin();it1!=front_rois[temp_roi].end();it1++){
696  if (ROIsaved_map.find(*it1)==ROIsaved_map.end()){
697  temp_rois.push_back(*it1);
698  ROIsaved_map[*it1] = 1;
699  }
700  }
701  }
702  if (back_rois.find(temp_roi)!=back_rois.end()){
703  for (auto it1 = back_rois[temp_roi].begin();it1!=back_rois[temp_roi].end();it1++){
704  if (ROIsaved_map.find(*it1)==ROIsaved_map.end()){
705  temp_rois.push_back(*it1);
706  ROIsaved_map[*it1] = 1;
707  }
708  }
709  }
710  }
711  }
712  }
713  }
714  }
715 
716  //remove the bad ones ...
717  //int counter2 = 0;
718  for (size_t i=0;i!=rois_v_loose.size();i++){
719  SignalROISelection to_be_removed;
720  for (auto it = rois_v_loose.at(i).begin(); it!= rois_v_loose.at(i).end();it++){
721  SignalROI *roi = *it;
722  if (ROIsaved_map.find(roi) == ROIsaved_map.end()){
723  // counter2 ++;
724  to_be_removed.push_back(roi);
725  //it = rois_v_loose.at(i).erase(it);
726  // check contained map
727  if (contained_rois.find(roi)!= contained_rois.end()){
728  std::cout << "Wrong! " << std::endl;
729  }
730  // check front map
731  if (front_rois.find(roi)!=front_rois.end()){
732  for (auto it1 = front_rois[roi].begin(); it1 != front_rois[roi].end(); it1++){
733  auto it2 = find(back_rois[*it1].begin(),back_rois[*it1].end(),roi);
734  back_rois[*it1].erase(it2);
735  }
736  front_rois.erase(roi);
737  }
738  // check back map
739  if (back_rois.find(roi)!=back_rois.end()){
740  for (auto it1 = back_rois[roi].begin(); it1!=back_rois[roi].end(); it1++){
741  auto it2 = find(front_rois[*it1].begin(),front_rois[*it1].end(),roi);
742  front_rois[*it1].erase(it2);
743  }
744  back_rois.erase(roi);
745  }
746  }
747  }
748 
749  for (auto it = to_be_removed.begin(); it!= to_be_removed.end(); it++){
750  auto it1 = find(rois_v_loose.at(i).begin(), rois_v_loose.at(i).end(),*it);
751  rois_v_loose.at(i).erase(it1);
752  delete (*it);
753  }
754  }
755  }
756 
757 
758 
759  // int counter1 = 0;
760  // for (int i=0;i!=rois_v_loose.size();i++){
761  // counter1+=rois_v_loose.at(i).size();
762  // }
763 
764  // std::cout << counter << " " << ROIsaved_map.size() << " " << counter1 << " " << counter2 << std::endl;
765 }
766 
768  // find tight ROIs not contained by the loose ROIs
769  if (plane==0){
770  for (int i = 0;i!=nwire_u;i++){
771  std::map<SignalROI*,int> covered_tight_rois;
772  for (auto it = rois_u_loose.at(i).begin();it!=rois_u_loose.at(i).end();it++){
773  SignalROI *roi = *it;
774  if (contained_rois.find(roi) != contained_rois.end()){
775  for (auto it1 = contained_rois[roi].begin(); it1!= contained_rois[roi].end(); it1++){
776  if (covered_tight_rois.find(*it1)==covered_tight_rois.end()){
777  covered_tight_rois[*it1] =1;
778  }
779  }
780  }
781  }
782  SignalROISelection saved_rois;
783  for (auto it = rois_u_tight.at(i).begin();it!=rois_u_tight.at(i).end();it++){
784  SignalROI *roi = *it;
785  if (covered_tight_rois.find(roi) == covered_tight_rois.end()){
786  saved_rois.push_back(roi);
787  }
788  }
789  // if (i == 1212)
790  // std::cout << saved_rois.size() << " " << saved_rois.at(0)->get_start_bin() << " " << saved_rois.at(0)->get_end_bin() << std::endl;
791 
792  for (auto it = saved_rois.begin(); it!=saved_rois.end();it++){
793  SignalROI *roi = *it;
794  // Duplicate them
795  SignalROI *loose_roi = new SignalROI(roi);
796 
797  rois_u_loose.at(i).push_back(loose_roi);
798 
799  // update all the maps
800  // contained
801  SignalROISelection temp_rois;
802  temp_rois.push_back(roi);
803  contained_rois[loose_roi] = temp_rois;
804  // front map loose ROI
805  if (i < nwire_u-1){
806  for (auto it1 = rois_u_loose.at(i+1).begin(); it1!=rois_u_loose.at(i+1).end(); it1++){
807  SignalROI *next_roi = *it1;
808 
809  if (loose_roi->overlap(next_roi)){
810  if (back_rois.find(next_roi) == back_rois.end()){
811  SignalROISelection temp_rois;
812  temp_rois.push_back(loose_roi);
813  back_rois[next_roi] = temp_rois;
814  }else{
815  back_rois[next_roi].push_back(loose_roi);
816  }
817 
818  if (front_rois.find(loose_roi) == front_rois.end()){
819  SignalROISelection temp_rois;
820  temp_rois.push_back(next_roi);
821  front_rois[loose_roi] = temp_rois;
822  }else{
823  front_rois[loose_roi].push_back(next_roi);
824  }
825  }
826 
827  }
828  }
829  // back map loose ROI
830  if (i > 0){
831  for (auto it1 = rois_u_loose.at(i-1).begin(); it1!=rois_u_loose.at(i-1).end(); it1++){
832  SignalROI *prev_roi = *it1;
833  if (loose_roi->overlap(prev_roi)){
834  if (front_rois.find(prev_roi) == front_rois.end()){
835  SignalROISelection temp_rois;
836  temp_rois.push_back(loose_roi);
837  front_rois[prev_roi] = temp_rois;
838  }else{
839  front_rois[prev_roi].push_back(loose_roi);
840  }
841  if (back_rois.find(loose_roi) == back_rois.end()){
842  SignalROISelection temp_rois;
843  temp_rois.push_back(prev_roi);
844  back_rois[loose_roi] = temp_rois;
845  }else{
846  back_rois[loose_roi].push_back(prev_roi);
847  }
848  }
849  }
850  }
851  }
852  }
853  }else if (plane==1){
854  for (int i = 0;i!=nwire_v;i++){
855  std::map<SignalROI*,int> covered_tight_rois;
856  for (auto it = rois_v_loose.at(i).begin();it!=rois_v_loose.at(i).end();it++){
857  SignalROI *roi = *it;
858  if (contained_rois.find(roi) != contained_rois.end()){
859  for (auto it1 = contained_rois[roi].begin(); it1!= contained_rois[roi].end(); it1++){
860  if (covered_tight_rois.find(*it1)==covered_tight_rois.end()){
861  covered_tight_rois[*it1] =1;
862  }
863  }
864  }
865  }
866  SignalROISelection saved_rois;
867  for (auto it = rois_v_tight.at(i).begin();it!=rois_v_tight.at(i).end();it++){
868  SignalROI *roi = *it;
869  if (covered_tight_rois.find(roi) == covered_tight_rois.end()){
870  saved_rois.push_back(roi);
871  }
872  }
873  //if (i == 3885-2400)
874  // std::cout << saved_rois.size() << std::endl;
875  // std::cout << saved_rois.size() << " " << saved_rois.at(0)->get_start_bin() << " " << saved_rois.at(0)->get_end_bin() << std::endl;
876 
877  for (auto it = saved_rois.begin(); it!=saved_rois.end();it++){
878  SignalROI *roi = *it;
879  // Duplicate them
880  SignalROI *loose_roi = new SignalROI(roi);
881 
882  rois_v_loose.at(i).push_back(loose_roi);
883 
884  // update all the maps
885  // contained
886  SignalROISelection temp_rois;
887  temp_rois.push_back(roi);
888  contained_rois[loose_roi] = temp_rois;
889  // front map loose ROI
890  if (i < nwire_v-1){
891  for (auto it1 = rois_v_loose.at(i+1).begin(); it1!=rois_v_loose.at(i+1).end(); it1++){
892  SignalROI *next_roi = *it1;
893 
894  if (loose_roi->overlap(next_roi)){
895  if (back_rois.find(next_roi) == back_rois.end()){
896  SignalROISelection temp_rois;
897  temp_rois.push_back(loose_roi);
898  back_rois[next_roi] = temp_rois;
899  }else{
900  back_rois[next_roi].push_back(loose_roi);
901  }
902 
903  if (front_rois.find(loose_roi) == front_rois.end()){
904  SignalROISelection temp_rois;
905  temp_rois.push_back(next_roi);
906  front_rois[loose_roi] = temp_rois;
907  }else{
908  front_rois[loose_roi].push_back(next_roi);
909  }
910  }
911 
912  }
913  }
914  // back map loose ROI
915  if (i > 0){
916  for (auto it1 = rois_v_loose.at(i-1).begin(); it1!=rois_v_loose.at(i-1).end(); it1++){
917  SignalROI *prev_roi = *it1;
918  if (loose_roi->overlap(prev_roi)){
919  if (front_rois.find(prev_roi) == front_rois.end()){
920  SignalROISelection temp_rois;
921  temp_rois.push_back(loose_roi);
922  front_rois[prev_roi] = temp_rois;
923  }else{
924  front_rois[prev_roi].push_back(loose_roi);
925  }
926  if (back_rois.find(loose_roi) == back_rois.end()){
927  SignalROISelection temp_rois;
928  temp_rois.push_back(prev_roi);
929  back_rois[loose_roi] = temp_rois;
930  }else{
931  back_rois[loose_roi].push_back(prev_roi);
932  }
933  }
934  }
935  }
936  }
937  }
938  }
939 }
940 
941 void ROI_refinement::CheckROIs(int plane,ROI_formation& roi_form){
942 
943  if (plane==0){
944  std::vector<float>& rms_u = roi_form.get_uplane_rms();
945 
946  // for (int i=0;i!=rois_u_loose.size();i++){
947  // for (auto it = rois_u_loose.at(i).begin(); it!= rois_u_loose.at(i).end();it++){
948  // SignalROI *roi = *it;
949  // int chid = roi->get_chid();
950  // if (chid != i)
951  // std::cout << roi << std::endl;
952 
953  // if (front_rois.find(roi)!=front_rois.end()){
954  // for (auto it1 = front_rois[roi].begin();it1!=front_rois[roi].end();it1++){
955  // SignalROI *roi1 = *it1;
956  // int chid1 = roi1->get_chid();
957  // if (chid1!=i+1)
958  // std::cout << roi1 << " " << chid << " " << chid1 << std::endl;
959  // }
960  // }
961  // }
962  // }
963 
964  //loop over u loose
965  for (size_t i=0;i!=rois_u_loose.size();i++){
966  for (auto it = rois_u_loose.at(i).begin(); it!= rois_u_loose.at(i).end();it++){
967  SignalROI *roi = *it;
968  int chid = roi->get_chid();
969  float th;
970  th = th_factor*rms_u.at(chid);
971 
972  if (front_rois.find(roi)!=front_rois.end()){
973  SignalROISelection temp_rois;
974  for (auto it1 = front_rois[roi].begin();it1!=front_rois[roi].end();it1++){
975  SignalROI *roi1 = *it1;
976  int chid1 = roi1->get_chid();
977  //std::cout << "F " << i << " " << rois_u_loose.size() << " " << roi1 << " " << chid << " " << chid1 << std::endl;
978  float th1;
979  th1 = th_factor*rms_u.at(chid1);
980  if (roi->overlap(roi1,th,th1)){
981  }else{
982  temp_rois.push_back(roi1);
983  //unlink(roi,roi1);
984  }
985  }
986  for (auto it2 = temp_rois.begin(); it2!= temp_rois.end();it2++){
987  unlink(roi,*it2);
988  }
989  }
990 
991  if (back_rois.find(roi)!=back_rois.end()){
992  SignalROISelection temp_rois;
993  for (auto it1 = back_rois[roi].begin();it1!=back_rois[roi].end();it1++){
994  SignalROI *roi1 = *it1;
995  int chid1 = roi1->get_chid();
996  //std::cout << "B " << roi1 << " " << chid << " " << chid1 << std::endl;
997  float th1;
998  th1 = th_factor*rms_u.at(chid1);
999  if (roi->overlap(roi1,th,th1)){
1000  }else{
1001  temp_rois.push_back(roi1);
1002  //unlink(roi,roi1);
1003  }
1004  }
1005  for (auto it2 = temp_rois.begin(); it2!= temp_rois.end();it2++){
1006  unlink(*it2,roi);
1007  }
1008  }
1009 
1010  }
1011  }
1012  }else if (plane==1){
1013  std::vector<float>& rms_v = roi_form.get_vplane_rms();
1014  //loop over v loose
1015  for (size_t i=0;i!=rois_v_loose.size();i++){
1016  for (auto it = rois_v_loose.at(i).begin(); it!= rois_v_loose.at(i).end();it++){
1017  SignalROI *roi = *it;
1018  int chid = roi->get_chid()-nwire_u;
1019  float th;
1020  th = th_factor*rms_v.at(chid);
1021  if (front_rois.find(roi)!=front_rois.end()){
1022  SignalROISelection temp_rois;
1023  for (auto it1 = front_rois[roi].begin();it1!=front_rois[roi].end();it1++){
1024  SignalROI *roi1 = *it1;
1025  int chid1 = roi1->get_chid()-nwire_u;
1026  float th1;
1027  th1 = th_factor*rms_v.at(chid1);
1028  if (roi->overlap(roi1,th,th1)){
1029  }else{
1030  temp_rois.push_back(roi1);
1031  // unlink(roi,roi1);
1032  }
1033  }
1034  for (auto it2 = temp_rois.begin(); it2!= temp_rois.end();it2++){
1035  unlink(roi,*it2);
1036  }
1037  }
1038 
1039  if (back_rois.find(roi)!=back_rois.end()){
1040  SignalROISelection temp_rois;
1041  for (auto it1 = back_rois[roi].begin();it1!=back_rois[roi].end();it1++){
1042  SignalROI *roi1 = *it1;
1043  int chid1 = roi1->get_chid()-nwire_u;
1044  float th1;
1045  th1 = th_factor*rms_v.at(chid1);
1046  if (roi->overlap(roi1,th,th1)){
1047  }else{
1048  temp_rois.push_back(roi1);
1049  // unlink(roi,roi1);
1050  }
1051  }
1052  for (auto it2 = temp_rois.begin(); it2!= temp_rois.end();it2++){
1053  unlink(*it2,roi);
1054  }
1055  }
1056  }
1057  }
1058  }
1059 }
1060 
1062  // deal with tight ROIs,
1063  // scan with all the tight ROIs to look for peaks above certain threshold, put in a temporary set
1064  float mean_threshold = fake_signal_low_th;
1065  float threshold = fake_signal_high_th; //electrons, about 1/2 of MIP per tick ...
1066  std::set<SignalROI*> Good_ROIs;
1067  for (int i=0;i!=nwire_w;i++){
1068  for (auto it = rois_w_tight.at(i).begin();it!=rois_w_tight.at(i).end();it++){
1069  SignalROI* roi = *it;
1070  //std::cout << "Xin: " << roi->get_max_height() << " " << roi->get_average_heights() << std::endl;
1071  if (roi->get_above_threshold(threshold).size()!=0 || roi->get_average_heights() > mean_threshold)
1072  Good_ROIs.insert(roi);
1073  }
1074  }
1075  // for a particular ROI if it is not in, or it is not connected with one in the temporary map, then remove it
1076  std::list<SignalROI*> Bad_ROIs;
1077  for (int i=0;i!=nwire_w;i++){
1078  for (auto it = rois_w_tight.at(i).begin();it!=rois_w_tight.at(i).end();it++){
1079  SignalROI* roi = *it;
1080 
1081  if (Good_ROIs.find(roi)!=Good_ROIs.end()) continue;
1082  if (front_rois.find(roi)!=front_rois.end()){
1083  SignalROISelection next_rois = front_rois[roi];
1084  int flag_qx = 0;
1085  for (size_t i=0;i!=next_rois.size();i++){
1086  SignalROI* roi1 = next_rois.at(i);
1087  if (Good_ROIs.find(roi1)!=Good_ROIs.end()) {
1088  flag_qx = 1;
1089  continue;
1090  }
1091  }
1092  if (flag_qx == 1) continue;
1093  }
1094 
1095  if (back_rois.find(roi)!=back_rois.end()){
1096  SignalROISelection next_rois = back_rois[roi];
1097  int flag_qx = 0;
1098  for (size_t i=0;i!=next_rois.size();i++){
1099  SignalROI* roi1 = next_rois.at(i);
1100  if (Good_ROIs.find(roi1)!=Good_ROIs.end()) {
1101  flag_qx = 1;
1102  continue;
1103  }
1104  }
1105  if (flag_qx == 1) continue;
1106  }
1107 
1108  Bad_ROIs.push_back(roi);
1109  }
1110  }
1111 
1112  // remove the ROI and then update the map
1113 
1114  for (auto it = Bad_ROIs.begin(); it!=Bad_ROIs.end(); it ++){
1115  SignalROI* roi = *it;
1116  int chid = roi->get_chid()-nwire_u-nwire_v;
1117  //std::cout << chid << std::endl;
1118  if (front_rois.find(roi)!=front_rois.end()){
1119  SignalROISelection next_rois = front_rois[roi];
1120  for (size_t i=0;i!=next_rois.size();i++){
1121  //unlink the current roi
1122  unlink(roi,next_rois.at(i));
1123  }
1124  front_rois.erase(roi);
1125  }
1126 
1127  if (back_rois.find(roi)!=back_rois.end()){
1128  SignalROISelection prev_rois = back_rois[roi];
1129  for (size_t i=0;i!=prev_rois.size();i++){
1130  //unlink the current roi
1131  unlink(prev_rois.at(i),roi);
1132  }
1133  back_rois.erase(roi);
1134  }
1135  auto it1 = find(rois_w_tight.at(chid).begin(), rois_w_tight.at(chid).end(),roi);
1136  if (it1 != rois_w_tight.at(chid).end())
1137  rois_w_tight.at(chid).erase(it1);
1138 
1139  delete roi;
1140  }
1141 
1142 }
1143 
1145  // deal with loose ROIs
1146  // focus on the isolated ones first
1147  float mean_threshold = fake_signal_low_th;
1148  float threshold = fake_signal_high_th;
1149  mean_threshold *= fake_signal_low_th_ind_factor;
1150  threshold *= fake_signal_high_th_ind_factor;
1151 
1152  std::list<SignalROI*> Bad_ROIs;
1153  if (plane==0){
1154  for (int i=0;i!=nwire_u;i++){
1155  for (auto it = rois_u_loose.at(i).begin();it!=rois_u_loose.at(i).end();it++){
1156  SignalROI* roi = *it;
1157  //std::cout << "Xin: " << roi->get_max_height() << " " << roi->get_average_heights() << std::endl;
1158  if (front_rois.find(roi)==front_rois.end() && back_rois.find(roi)==back_rois.end()){
1159  if (roi->get_above_threshold(threshold).size()==0 && roi->get_average_heights() < mean_threshold)
1160  Bad_ROIs.push_back(roi);
1161  }
1162  }
1163  }
1164  for (auto it = Bad_ROIs.begin(); it!=Bad_ROIs.end(); it ++){
1165  SignalROI* roi = *it;
1166  int chid = roi->get_chid();
1167  auto it1 = find(rois_u_loose.at(chid).begin(), rois_u_loose.at(chid).end(),roi);
1168  if (it1 != rois_u_loose.at(chid).end())
1169  rois_u_loose.at(chid).erase(it1);
1170 
1171  if (front_rois.find(roi)!=front_rois.end()){
1172  SignalROISelection next_rois = front_rois[roi];
1173  for (size_t i=0;i!=next_rois.size();i++){
1174  //unlink the current roi
1175  unlink(roi,next_rois.at(i));
1176  }
1177  front_rois.erase(roi);
1178  }
1179 
1180  if (back_rois.find(roi)!=back_rois.end()){
1181  SignalROISelection prev_rois = back_rois[roi];
1182  for (size_t i=0;i!=prev_rois.size();i++){
1183  //unlink the current roi
1184  unlink(prev_rois.at(i),roi);
1185  }
1186  back_rois.erase(roi);
1187  }
1188 
1189  delete roi;
1190  }
1191  Bad_ROIs.clear();
1192  }else if (plane==1){
1193  for (int i=0;i!=nwire_v;i++){
1194  for (auto it = rois_v_loose.at(i).begin();it!=rois_v_loose.at(i).end();it++){
1195  SignalROI* roi = *it;
1196  // std::cout << "Xin: " << roi->get_max_height() << " " << roi->get_average_heights() << std::endl;
1197  if (front_rois.find(roi)==front_rois.end() && back_rois.find(roi)==back_rois.end()){
1198  if (roi->get_above_threshold(threshold).size()==0 && roi->get_average_heights() < mean_threshold)
1199  Bad_ROIs.push_back(roi);
1200  }
1201  }
1202  }
1203  for (auto it = Bad_ROIs.begin(); it!=Bad_ROIs.end(); it ++){
1204  SignalROI* roi = *it;
1205  int chid = roi->get_chid()-nwire_u;
1206  auto it1 = find(rois_v_loose.at(chid).begin(), rois_v_loose.at(chid).end(),roi);
1207  if (it1 != rois_v_loose.at(chid).end())
1208  rois_v_loose.at(chid).erase(it1);
1209 
1210  if (front_rois.find(roi)!=front_rois.end()){
1211  SignalROISelection next_rois = front_rois[roi];
1212  for (size_t i=0;i!=next_rois.size();i++){
1213  //unlink the current roi
1214  unlink(roi,next_rois.at(i));
1215  }
1216  front_rois.erase(roi);
1217  }
1218 
1219  if (back_rois.find(roi)!=back_rois.end()){
1220  SignalROISelection prev_rois = back_rois[roi];
1221  for (size_t i=0;i!=prev_rois.size();i++){
1222  //unlink the current roi
1223  unlink(prev_rois.at(i),roi);
1224  }
1225  back_rois.erase(roi);
1226  }
1227 
1228 
1229  delete roi;
1230  }
1231  }
1232 
1233 
1234  // threshold = fake_signal_low_th;
1235  std::set<SignalROI*> Good_ROIs;
1236  if (plane==0){
1237  for (int i=0;i!=nwire_u;i++){
1238  for (auto it = rois_u_loose.at(i).begin();it!=rois_u_loose.at(i).end();it++){
1239  SignalROI* roi = *it;
1240  if (roi->get_above_threshold(threshold).size()!=0 || roi->get_average_heights() > mean_threshold)
1241  Good_ROIs.insert(roi);
1242  }
1243  }
1244  Bad_ROIs.clear();
1245  for (int i=0;i!=nwire_u;i++){
1246  for (auto it = rois_u_loose.at(i).begin();it!=rois_u_loose.at(i).end();it++){
1247  SignalROI* roi = *it;
1248 
1249  if (Good_ROIs.find(roi)!=Good_ROIs.end()) continue;
1250  if (front_rois.find(roi)!=front_rois.end()){
1251  SignalROISelection next_rois = front_rois[roi];
1252  int flag_qx = 0;
1253  for (size_t i=0;i!=next_rois.size();i++){
1254  SignalROI* roi1 = next_rois.at(i);
1255  if (Good_ROIs.find(roi1)!=Good_ROIs.end()) {
1256  flag_qx = 1;
1257  continue;
1258  }
1259  }
1260  if (flag_qx == 1) continue;
1261  }
1262 
1263  if (back_rois.find(roi)!=back_rois.end()){
1264  SignalROISelection next_rois = back_rois[roi];
1265  int flag_qx = 0;
1266  for (size_t i=0;i!=next_rois.size();i++){
1267  SignalROI* roi1 = next_rois.at(i);
1268  if (Good_ROIs.find(roi1)!=Good_ROIs.end()) {
1269  flag_qx = 1;
1270  continue;
1271  }
1272  }
1273  if (flag_qx == 1) continue;
1274  }
1275 
1276  Bad_ROIs.push_back(roi);
1277  }
1278  }
1279  for (auto it = Bad_ROIs.begin(); it!=Bad_ROIs.end(); it ++){
1280  SignalROI* roi = *it;
1281  int chid = roi->get_chid();
1282  //std::cout << chid << std::endl;
1283  if (front_rois.find(roi)!=front_rois.end()){
1284  SignalROISelection next_rois = front_rois[roi];
1285  for (size_t i=0;i!=next_rois.size();i++){
1286  //unlink the current roi
1287  unlink(roi,next_rois.at(i));
1288  }
1289  front_rois.erase(roi);
1290  }
1291 
1292  if (back_rois.find(roi)!=back_rois.end()){
1293  SignalROISelection prev_rois = back_rois[roi];
1294  for (size_t i=0;i!=prev_rois.size();i++){
1295  //unlink the current roi
1296  unlink(prev_rois.at(i),roi);
1297  }
1298  back_rois.erase(roi);
1299  }
1300  auto it1 = find(rois_u_loose.at(chid).begin(), rois_u_loose.at(chid).end(),roi);
1301  if (it1 != rois_u_loose.at(chid).end())
1302  rois_u_loose.at(chid).erase(it1);
1303 
1304  delete roi;
1305  }
1306  }else if (plane==1){
1307 
1308  Good_ROIs.clear();
1309  for (int i=0;i!=nwire_v;i++){
1310  for (auto it = rois_v_loose.at(i).begin();it!=rois_v_loose.at(i).end();it++){
1311  SignalROI* roi = *it;
1312  if (roi->get_above_threshold(threshold).size()!=0)
1313  Good_ROIs.insert(roi);
1314  }
1315  }
1316  Bad_ROIs.clear();
1317  for (int i=0;i!=nwire_v;i++){
1318  for (auto it = rois_v_loose.at(i).begin();it!=rois_v_loose.at(i).end();it++){
1319  SignalROI* roi = *it;
1320 
1321  if (Good_ROIs.find(roi)!=Good_ROIs.end()) continue;
1322  if (front_rois.find(roi)!=front_rois.end()){
1323  SignalROISelection next_rois = front_rois[roi];
1324  int flag_qx = 0;
1325  for (size_t i=0;i!=next_rois.size();i++){
1326  SignalROI* roi1 = next_rois.at(i);
1327  if (Good_ROIs.find(roi1)!=Good_ROIs.end()) {
1328  flag_qx = 1;
1329  continue;
1330  }
1331  }
1332  if (flag_qx == 1) continue;
1333  }
1334 
1335  if (back_rois.find(roi)!=back_rois.end()){
1336  SignalROISelection next_rois = back_rois[roi];
1337  int flag_qx = 0;
1338  for (size_t i=0;i!=next_rois.size();i++){
1339  SignalROI* roi1 = next_rois.at(i);
1340  if (Good_ROIs.find(roi1)!=Good_ROIs.end()) {
1341  flag_qx = 1;
1342  continue;
1343  }
1344  }
1345  if (flag_qx == 1) continue;
1346  }
1347 
1348  Bad_ROIs.push_back(roi);
1349  }
1350  }
1351  for (auto it = Bad_ROIs.begin(); it!=Bad_ROIs.end(); it ++){
1352  SignalROI* roi = *it;
1353  int chid = roi->get_chid()-nwire_u;
1354  //std::cout << chid << std::endl;
1355  if (front_rois.find(roi)!=front_rois.end()){
1356  SignalROISelection next_rois = front_rois[roi];
1357  for (size_t i=0;i!=next_rois.size();i++){
1358  //unlink the current roi
1359  unlink(roi,next_rois.at(i));
1360  }
1361  front_rois.erase(roi);
1362  }
1363 
1364  if (back_rois.find(roi)!=back_rois.end()){
1365  SignalROISelection prev_rois = back_rois[roi];
1366  for (size_t i=0;i!=prev_rois.size();i++){
1367  //unlink the current roi
1368  unlink(prev_rois.at(i),roi);
1369  }
1370  back_rois.erase(roi);
1371  }
1372  auto it1 = find(rois_v_loose.at(chid).begin(), rois_v_loose.at(chid).end(),roi);
1373  if (it1 != rois_v_loose.at(chid).end())
1374  rois_v_loose.at(chid).erase(it1);
1375 
1376  delete roi;
1377  }
1378  }
1379 }
1380 
1382  // get tight ROI as a inner boundary
1383  // get the nearby ROIs with threshold as some sort of boundary
1384  int start_bin = roi->get_start_bin();
1385  int end_bin = roi->get_end_bin();
1386  if (start_bin <0 || end_bin <0) return;
1387 
1388  int chid = roi->get_chid();
1389  int plane = roi->get_plane();
1390  std::vector<float>& contents = roi->get_contents();
1391 
1392  float threshold1=0;
1393  if (plane==0){
1394  threshold1 = roi_form.get_uplane_rms().at(chid) * th_factor;
1395  }else if (plane==1){
1396  threshold1 = roi_form.get_vplane_rms().at(chid-nwire_u)* th_factor;
1397  }
1398 
1399  int channel_save = 1240;
1400  int print_flag = 0;
1401 
1402  // std::cout << "check tight ROIs " << std::endl;
1403  // use to save contents
1404  Waveform::realseq_t temp_signal(end_bin-start_bin+1,0);
1405  // TH1F *htemp = new TH1F("htemp","htemp",end_bin-start_bin+1,start_bin,end_bin+1);
1406 
1407  // check tight ROIs
1408  if (contained_rois.find(roi)!=contained_rois.end()){
1409  for (auto it = contained_rois[roi].begin();it!=contained_rois[roi].end();it++){
1410  SignalROI *tight_roi = *it;
1411  int start_bin1 = tight_roi->get_start_bin();
1412  int end_bin1 = tight_roi->get_end_bin();
1413 
1414  if (chid == channel_save && print_flag)
1415  std::cout << "Tight " " " << start_bin1 << " " << end_bin1 << std::endl;
1416 
1417  for (int i=start_bin1;i<=end_bin1;i++){
1418  if (i-start_bin >=0 && i-start_bin <int(temp_signal.size())){
1419  // htemp->SetBinContent(i-start_bin+1,1);
1420  temp_signal.at(i-start_bin) = 1;
1421  }
1422  }
1423  }
1424  }
1425 
1426  // std::cout << "check front ROIs " << std::endl;
1427 
1428  //check front ROIs
1429  if (front_rois.find(roi)!=front_rois.end()){
1430  for (auto it=front_rois[roi].begin();it!=front_rois[roi].end();it++){
1431  SignalROI *next_roi = *it;
1432  int start_bin1 = next_roi->get_start_bin();
1433  int chid1 = next_roi->get_chid();
1434  int plane1 = next_roi->get_plane();
1435  float threshold=0;
1436  if (plane1==0){
1437  threshold = roi_form.get_uplane_rms().at(chid1) * th_factor;
1438  }else if (plane1==1){
1439  threshold = roi_form.get_vplane_rms().at(chid1-nwire_u) * th_factor;
1440  }
1441  std::vector<std::pair<int,int>> contents_above_threshold = next_roi->get_above_threshold(threshold);
1442  for (size_t i=0;i!=contents_above_threshold.size();i++){
1443  if (chid == channel_save && print_flag)
1444  std::cout << "Front " << chid1 << " " << start_bin1 + contents_above_threshold.at(i).first << " " << start_bin1 + contents_above_threshold.at(i).second << std::endl;
1445 
1446  for (int j=contents_above_threshold.at(i).first;j<=contents_above_threshold.at(i).second;j++){
1447  if (j+start_bin1-start_bin >=0 && j+start_bin1-start_bin < int(temp_signal.size())){
1448  if (contents.at(j+start_bin1-start_bin) > threshold1)
1449  temp_signal.at(j+start_bin1-start_bin) = 1;
1450  // htemp->SetBinContent(j+start_bin1-start_bin+1,1);
1451  }
1452  }
1453  }
1454  }
1455  }
1456 
1457  //std::cout << "check back ROIs " << std::endl;
1458 
1459  //check back ROIs
1460  if (back_rois.find(roi)!=back_rois.end()){
1461  for (auto it=back_rois[roi].begin();it!=back_rois[roi].end();it++){
1462  SignalROI *prev_roi = *it;
1463  int start_bin1 = prev_roi->get_start_bin();
1464  int chid1 = prev_roi->get_chid();
1465  int plane1 = prev_roi->get_plane();
1466  float threshold = 0;
1467  if (plane1==0){
1468  threshold = roi_form.get_uplane_rms().at(chid1) * th_factor;
1469  }else if (plane1==1){
1470  threshold = roi_form.get_vplane_rms().at(chid1-nwire_u)* th_factor;
1471  }
1472  std::vector<std::pair<int,int>> contents_above_threshold = prev_roi->get_above_threshold(threshold);
1473  for (size_t i=0;i!=contents_above_threshold.size();i++){
1474  if (chid == channel_save && print_flag)
1475  std::cout << "Back " << chid1 << " " << start_bin1 + contents_above_threshold.at(i).first << " " << start_bin1 + contents_above_threshold.at(i).second << std::endl;
1476 
1477  for (int j=contents_above_threshold.at(i).first;j<=contents_above_threshold.at(i).second;j++){
1478  if (j+start_bin1-start_bin >=0 && j+start_bin1-start_bin <int(temp_signal.size())){
1479  if (contents.at(j+start_bin1-start_bin) > threshold1)
1480  temp_signal.at(j+start_bin1-start_bin) = 1;
1481  // htemp->SetBinContent(j+start_bin1-start_bin+1,1);
1482  }
1483  }
1484  }
1485  }
1486  }
1487 
1488  //std::cout << "check contents " << std::endl;
1489 
1490  // // consider the 1/2 of the peak as threshold;
1491  // float max = 0;
1492  // for (int i=0;i!=contents.size();i++){
1493  // if (contents.at(i) > max)
1494  // max = contents.at(i);
1495  // }
1496  // for (int i=0;i!=contents.size();i++){
1497  // // if (contents.at(i) > max/2. && contents.at(i) > threshold1*2 ) htemp->SetBinContent(i+1,1);
1498  // }
1499 
1500  // get the first bin, and last bin, add pad
1501  int new_start_bin=start_bin;
1502  int new_end_bin=end_bin;
1503  for (size_t i=0;i!= temp_signal.size(); i++){
1504  if (temp_signal.at(i) >0){
1505  new_start_bin = i + start_bin;
1506  break;
1507  }
1508  }
1509  for (int i = int(temp_signal.size())-1;i>=0;i--){
1510  if (temp_signal.at(i) > 0){
1511  new_end_bin = i + start_bin;
1512  break;
1513  }
1514  }
1515  new_start_bin -= pad;
1516  new_end_bin += pad;
1517  if (new_start_bin < start_bin) new_start_bin = start_bin;
1518  if (new_end_bin > end_bin) new_end_bin = end_bin;
1519 
1520  if (chid == channel_save && print_flag)
1521  std::cout << "check contents " << " " << start_bin << " " << end_bin << " " << new_start_bin << " " << new_end_bin << std::endl;
1522 
1523 
1524  // create a new ROI
1525  Waveform::realseq_t signal(end_bin+1);
1526  // TH1F *h1 = new TH1F("h1","h1",end_bin+1,0,end_bin+1);
1527  for (int i=new_start_bin; i<=new_end_bin;i++){
1528  signal.at(i) = contents.at(i-start_bin);
1529  // h1->SetBinContent(i+1,contents.at(i-start_bin));
1530  }
1531 
1532  SignalROISelection new_rois;
1533  if (new_start_bin >=0 && new_end_bin > new_start_bin){
1534  SignalROI *new_roi = new SignalROI(plane,chid,new_start_bin,new_end_bin,signal);
1535  new_rois.push_back(new_roi);
1536  }
1537 
1538  // std::cout << "update maps " << std::endl;
1539 
1540  // update the list
1541  if (chid < nwire_u){
1542  auto it = std::find(rois_u_loose.at(chid).begin(),rois_u_loose.at(chid).end(),roi);
1543  rois_u_loose.at(chid).erase(it);
1544  for (size_t i=0;i!=new_rois.size();i++){
1545  rois_u_loose.at(chid).push_back(new_rois.at(i));
1546  }
1547  }else if (chid < nwire_u+nwire_v){
1548  auto it = std::find(rois_v_loose.at(chid-nwire_u).begin(),rois_v_loose.at(chid-nwire_u).end(),roi);
1549  rois_v_loose.at(chid-nwire_u).erase(it);
1550  for (size_t i=0;i!=new_rois.size();i++){
1551  rois_v_loose.at(chid-nwire_u).push_back(new_rois.at(i));
1552  }
1553  }
1554 
1555  // update all the maps
1556  // update front map
1557  if (front_rois.find(roi)!=front_rois.end()){
1558  SignalROISelection next_rois = front_rois[roi];
1559  for (size_t i=0;i!=next_rois.size();i++){
1560  //unlink the current roi
1561  unlink(roi,next_rois.at(i));
1562  //loop new rois and link them
1563  for (size_t j=0;j!=new_rois.size();j++){
1564  if (new_rois.at(j)->overlap(next_rois.at(i)))
1565  link(new_rois.at(j),next_rois.at(i));
1566  }
1567  }
1568  front_rois.erase(roi);
1569  }
1570  // update back map
1571  if (back_rois.find(roi)!=back_rois.end()){
1572  SignalROISelection prev_rois = back_rois[roi];
1573  for (size_t i=0;i!=prev_rois.size();i++){
1574  // unlink the current roi
1575  unlink(prev_rois.at(i),roi);
1576  // loop new rois and link them
1577  for (size_t j=0;j!=new_rois.size();j++){
1578  if (new_rois.at(j)->overlap(prev_rois.at(i)))
1579  link(prev_rois.at(i),new_rois.at(j));
1580  }
1581  }
1582  back_rois.erase(roi);
1583  }
1584 
1585  // update contained map
1586  if (contained_rois.find(roi)!=contained_rois.end()){
1587  SignalROISelection tight_rois = contained_rois[roi];
1588  for (size_t i=0;i!=tight_rois.size();i++){
1589  for (size_t j=0;j!=new_rois.size();j++){
1590  if (new_rois.at(j)->overlap(tight_rois.at(i))){
1591  if (contained_rois.find(new_rois.at(j)) == contained_rois.end()){
1592  SignalROISelection temp_rois;
1593  temp_rois.push_back(tight_rois.at(i));
1594  contained_rois[new_rois.at(j)] = temp_rois;
1595  }else{
1596  contained_rois[new_rois.at(j)].push_back(tight_rois.at(i));
1597  }
1598  }
1599  }
1600  }
1601  contained_rois.erase(roi);
1602  }
1603 
1604  // delete the old ROI
1605  delete roi;
1606 
1607  // delete htemp;
1608  // delete h1;
1609 }
1610 
1611 void ROI_refinement::ShrinkROIs(int plane, ROI_formation& roi_form){
1612  // collect all ROIs
1613  SignalROISelection all_rois;
1614  if (plane==0){
1615  for (size_t i=0;i!=rois_u_loose.size();i++){
1616  for (auto it = rois_u_loose.at(i).begin(); it!= rois_u_loose.at(i).end(); it++){
1617  all_rois.push_back(*it);
1618  }
1619  }
1620  }else if (plane==1){
1621  for (size_t i=0;i!=rois_v_loose.size();i++){
1622  for (auto it = rois_v_loose.at(i).begin(); it!= rois_v_loose.at(i).end(); it++){
1623  all_rois.push_back(*it);
1624  }
1625  }
1626  }
1627  for (size_t i=0;i!=all_rois.size();i++){
1628  ShrinkROI(all_rois.at(i),roi_form);
1629  }
1630 }
1631 
1633 
1634  // std::cout << "haha " << std::endl;
1635 
1636  // main algorithm
1637  int start_bin = roi->get_start_bin();
1638  int end_bin = roi->get_end_bin();
1639 
1640  if (start_bin <0 || end_bin <0 ) return;
1641 
1642  // if (roi->get_chid()==1240){
1643  // std::cout << "xin: " << start_bin << " " << end_bin << std::endl;
1644  // }
1645 
1646  Waveform::realseq_t temp_signal(end_bin-start_bin+1,0);
1647  // TH1F *htemp = new TH1F("htemp","htemp",end_bin-start_bin+1,start_bin,end_bin+1);
1648  std::vector<float>& contents = roi->get_contents();
1649  for (size_t i=0;i!=temp_signal.size();i++){
1650  temp_signal.at(i) = contents.at(i);
1651  // htemp->SetBinContent(i+1,contents.at(i));
1652  }
1653 
1654  // float th_peak = 3.0;
1655  // float sep_peak = 6.0;
1656  float low_peak_sep_threshold = low_peak_sep_threshold_pre; // electrons
1657  if (low_peak_sep_threshold < sep_peak * rms)
1658  low_peak_sep_threshold = sep_peak * rms;
1659  std::set<int> saved_boundaries;
1660 
1662  const int nfound = s.find_peak(temp_signal);
1663  // TSpectrum *s = new TSpectrum(200);
1664  // Int_t nfound = s->Search(htemp,2,"nobackground new",0.1);
1665 
1666  if (nfound == max_npeaks) {
1667  log->debug("ROI_refinement: local ch index {} (plane {}), found max peaks {} with threshold={}",
1668  roi->get_chid(), roi->get_plane(), nfound, th_percent);
1669  }
1670 
1671  if (nfound > 1){
1672  int npeaks = s.GetNPeaks();
1673  double *peak_pos = s.GetPositionX();
1674  double *peak_height = s.GetPositionY();
1675  //const int temp_length = max_npeaks + 5;
1676  std::vector<int> order_peak_pos;
1677  int npeaks_threshold = 0;
1678  for (int j=0;j!=npeaks;j++){
1679  order_peak_pos.push_back(*(peak_pos+j) + start_bin);
1680  if (*(peak_height+j)>th_peak*rms){
1681  npeaks_threshold ++;
1682  }
1683  }
1684 
1685 
1686 
1687  if (npeaks_threshold >1){
1688  std::sort(order_peak_pos.begin(),order_peak_pos.end());
1689  float valley_pos[205];
1690  valley_pos[0] = start_bin;
1691 
1692  // find the first real valley
1693  float min = 1e9;
1694  for (int k=0; k< order_peak_pos[0]-start_bin;k++){
1695  if (temp_signal.at(k) < min){
1696  min = temp_signal.at(k);
1697  valley_pos[0] = k+start_bin;
1698  }
1699  }
1700  if (valley_pos[0] != start_bin){
1701  // temporarily push n a value ...
1702  order_peak_pos.push_back(0);
1703  //
1704  for (int j=npeaks-1;j>=0;j--){
1705  order_peak_pos[j+1] = order_peak_pos[j];
1706  }
1707  npeaks ++;
1708  order_peak_pos[0] = start_bin;
1709  for (int j=start_bin; j!=valley_pos[0];j++){
1710  if (temp_signal.at(j-start_bin) > temp_signal.at(order_peak_pos[0]-start_bin))
1711  // htemp->GetBinContent(j-start_bin+1) > htemp->GetBinContent(order_peak_pos[0]-start_bin+1))
1712  order_peak_pos[0] = j;
1713  }
1714  valley_pos[0] = start_bin;
1715  }
1716 
1717  // std::cout << "kaka1 " << npeaks << std::endl;
1718 
1719  for (int j=0;j!=npeaks-1;j++){
1720  float min = 1e9;
1721  valley_pos[j+1] = order_peak_pos[j];
1722 
1723  // std::cout << order_peak_pos[j]-start_bin << " " << order_peak_pos[j+1]-start_bin << std::endl;
1724 
1725  for (int k = order_peak_pos[j]-start_bin; k< order_peak_pos[j+1]-start_bin;k++){
1726  if (temp_signal.at(k) < min){
1727  min = temp_signal.at(k);
1728  valley_pos[j+1] = k+start_bin;
1729  }
1730  }
1731  }
1732 
1733  // std::cout << "kaka1 " << npeaks << std::endl;
1734 
1735  //find the end ...
1736  valley_pos[npeaks] = end_bin;
1737  min = 1e9;
1738  for (int k=order_peak_pos[npeaks-1]-start_bin; k<= end_bin-start_bin;k++){
1739  if (temp_signal.at(k) < min){
1740  min = temp_signal.at(k);
1741  valley_pos[npeaks] = k+start_bin;
1742  }
1743  }
1744 
1745  // std::cout << "kaka1 " << npeaks << std::endl;
1746 
1747  if (valley_pos[npeaks]!=end_bin){
1748  npeaks ++;
1749  valley_pos[npeaks] = end_bin;
1750  // temporarily add an elements ...
1751  order_peak_pos.push_back(end_bin);
1752  // order_peak_pos[npeaks-1] = end_bin;
1753  for (int j=valley_pos[npeaks-1];j!=valley_pos[npeaks];j++){
1754  if (temp_signal.at(j-start_bin) > temp_signal.at(order_peak_pos[npeaks-1] -start_bin))
1755  order_peak_pos[npeaks-1] = j;
1756  }
1757  }
1758 
1759  // std::cout << "kaka1 " << npeaks << std::endl;
1760 
1761  // if (roi->get_chid() == 1195 && roi->get_plane() == WirePlaneType_t(0)){
1762  // for (int j=0;j!=npeaks;j++){
1763  // std::cout << valley_pos[j] << " " << htemp->GetBinContent(valley_pos[j]-start_bin+1)<< " " << order_peak_pos[j] << " " << htemp->GetBinContent( order_peak_pos[j]-start_bin+1) << " " << valley_pos[j+1] << " " << htemp->GetBinContent(valley_pos[j+1] - start_bin+1)<< " " << rms * sep_peak << std::endl ;
1764  // }
1765  // }
1766 
1767 
1768 
1769  // need to organize the peaks and valleys ...
1770  float valley_pos1[205];
1771  float peak_pos1[205];
1772  int npeaks1 = 0;
1773  // fill in the first valley;
1774  valley_pos1[0] = valley_pos[0];
1775  for (int j=0;j<npeaks;j++){
1776  if (npeaks1 >0){
1777  //std::cout << valley_pos[j]-start_bin << " " << valley_pos1[npeaks1]-start_bin << std::endl;
1778  // find the lowest valley except the first peak, except the first one
1779  if (temp_signal.at(valley_pos[j]-start_bin) < temp_signal.at(valley_pos1[npeaks1]-start_bin)){
1780  valley_pos1[npeaks1] = valley_pos[j];
1781  }
1782  }
1783 
1784  // if (roi->get_chid() == 1195 && roi->get_plane() == WirePlaneType_t(0)){
1785  // std::cout << "c: " << order_peak_pos[j] << " " << htemp->GetBinContent(order_peak_pos[j]-start_bin+1) << " " << valley_pos1[npeaks1] << " " << htemp->GetBinContent(valley_pos1[npeaks1]-start_bin+1) << std::endl;
1786  // }
1787 
1788  // find the next peak
1789  if (temp_signal.at(order_peak_pos[j]-start_bin) - temp_signal.at(valley_pos1[npeaks1]-start_bin) > low_peak_sep_threshold){
1790  peak_pos1[npeaks1] = order_peak_pos[j] ;
1791  npeaks1 ++;
1792  int flag1 = 0;
1793 
1794  for (int k=j+1;k!=npeaks+1;k++){
1795  // find the highest peak before ...
1796  if (k<=npeaks){
1797  if (temp_signal.at(order_peak_pos[k-1]-start_bin) > temp_signal.at(peak_pos1[npeaks1-1]-start_bin))
1798  peak_pos1[npeaks1-1] = order_peak_pos[k-1];
1799  }
1800 
1801  if (temp_signal.at(peak_pos1[npeaks1-1]-start_bin) - temp_signal.at(valley_pos[k]-start_bin) > low_peak_sep_threshold){
1802  valley_pos1[npeaks1] = valley_pos[k];
1803  j = k-1;
1804  flag1 = 1;
1805  break;
1806  }
1807  // find the next valley
1808  }
1809  if (flag1 == 0){
1810  valley_pos1[npeaks1] = valley_pos[npeaks];
1811  j = npeaks;
1812  }
1813 
1814  // if (roi->get_chid() == 1240 && roi->get_plane() == 0)
1815  // std::cout << "c: " << npeaks << " " << valley_pos1[npeaks1-1] << " " << peak_pos1[npeaks1-1] << " " << valley_pos1[npeaks1] << " " << rms * sep_peak << std::endl;
1816  }
1817  }
1818  // fill the last valley
1819  valley_pos1[npeaks1] = valley_pos[npeaks];
1820  //std::cout << roi->get_plane() << " " << roi->get_chid() << " " << npeaks << " " << npeaks1 << " " ;
1821  //std::cout << start_bin << " " << end_bin << std::endl;
1822 
1823 
1824  // if (roi->get_chid() == 1240 && roi->get_plane() == 0){
1825  // for (int j=0;j!=npeaks1;j++){
1826  // std::cout << valley_pos1[j] << " " << peak_pos1[j] << " " << valley_pos1[j+1] << std::endl ;
1827  // }
1828  // }
1829 
1830 
1831  // std::cout << "kaka1 " << std::endl;
1832 
1833  for (int j=0;j!=npeaks1;j++){
1834  int start_pos = valley_pos1[j];
1835  int end_pos = valley_pos1[j+1];
1836 
1837  // if (roi->get_chid()==1195)
1838  // std::cout << "b " << start_pos << " " << end_pos << std::endl;
1839 
1840  saved_boundaries.insert(start_pos);
1841  saved_boundaries.insert(end_pos);
1842  }
1843 
1844  Waveform::realseq_t temp1_signal = temp_signal;
1845  temp_signal.clear();
1846  temp_signal.resize(temp1_signal.size(),0);
1847  // htemp->Reset();
1848  for (int j=0;j!=npeaks1;j++){
1849  //int flag = 0;
1850  int start_pos = valley_pos1[j];
1851  double start_content = temp1_signal.at(valley_pos1[j]-start_bin); //htemp1->GetBinContent(valley_pos1[j]-start_bin+1);
1852  int end_pos = valley_pos1[j+1];
1853  double end_content = temp1_signal.at(valley_pos1[j+1]-start_bin);//htemp1->GetBinContent(valley_pos1[j+1]-start_bin+1);
1854 
1855  // std::cout << "a " << start_pos << " " << end_pos << std::endl;
1856 
1857  if (saved_boundaries.find(start_pos) != saved_boundaries.end() ||
1858  saved_boundaries.find(end_pos) != saved_boundaries.end()){
1859  // if (roi->get_chid() == 1240 && roi->get_plane() == 0)
1860  // std::cout << "d: " << start_pos << " " << end_pos << std::endl;
1861 
1862  for (int k = start_pos; k!=end_pos+1;k++){
1863  double temp_content = temp1_signal.at(k-start_bin) - (start_content + (end_content-start_content) * (k-start_pos) / (end_pos - start_pos));
1864  temp_signal.at(k-start_bin) = temp_content;
1865  }
1866  }
1867  }
1868  // delete htemp1;
1869 
1870  //std::cout << "kaka2 " << std::endl;
1871  // loop through the tight ROIs it contains ...
1872  // if nothing in the range, add things back ...
1873  // if (roi->get_chid() == 1151){
1874  // std::cout << "Break: " << roi->get_chid() << " " << start_bin << " " << end_bin << " " << nfound << std::endl;
1875  // std::cout << npeaks1 << std::endl;
1876  // std::cout << contained_rois[roi].size() << std::endl;
1877 
1878  for (auto it = contained_rois[roi].begin(); it!= contained_rois[roi].end(); it++){
1879  SignalROI *temp_roi = *it;
1880  int temp_flag = 0;
1881  for (int i=temp_roi->get_start_bin(); i<= temp_roi->get_end_bin(); i++){
1882  //std::cout << i-roi->get_start_bin() << " " << i << " " << temp_roi->get_start_bin() << " " << temp_roi->get_end_bin() << " " << roi->get_start_bin() << " " << roi->get_end_bin() << " " << roi->get_chid() << " " << roi->get_plane() << std::endl;
1883  if (i-int(roi->get_start_bin())>=0 && i-int(roi->get_start_bin()) < int(temp_signal.size()))
1884  if (temp_signal.at(i-roi->get_start_bin())!=0){
1885  temp_flag = 1;
1886  break;
1887  }
1888  }
1889  // std::cout << temp_flag << std::endl;
1890  if (temp_flag==0){
1891  for (int i=temp_roi->get_start_bin(); i<= temp_roi->get_end_bin(); i++){
1892  // std::cout << i-roi->get_start_bin() << " " << i-temp_roi->get_start_bin() << std::endl;
1893  if (i-int(roi->get_start_bin())>=0 && i-int(roi->get_start_bin()) < int(temp_signal.size()))
1894  temp_signal.at(i-roi->get_start_bin()) = temp_roi->get_contents().at(i-temp_roi->get_start_bin());
1895  // htemp->SetBinContent(i-roi->get_start_bin()+1, );
1896  }
1897  }
1898 
1899  }
1900  // } // if (1151)
1901 
1902  }
1903  }
1904 
1905  //std::cout << "kaka3 " << std::endl;
1906 
1907 
1908  // if (roi->get_chid() == 1151){
1909  // std::cout << "Break: " << roi->get_chid() << " " << start_bin << " " << end_bin << " " << nfound << std::endl;
1910  // for (int i=0;i<htemp->GetNbinsX();i++){
1911  // std::cout << htemp->GetBinContent(i+1) <<std::endl;
1912  // }
1913  // }
1914 
1915  for (int qx = 0; qx!=2; qx++){
1916  // Now we should go through the system again and re-adjust the content
1917  std::vector<std::pair<int,int>> bins;
1918  for (int i=0;i<int(temp_signal.size());i++){
1919  if (temp_signal.at(i) < th_factor*rms){
1920  int start = i;
1921  int end = i;
1922  for (int j=i+1;j< int(temp_signal.size());j++){
1923  if (temp_signal.at(j) < th_factor*rms){
1924  end = j;
1925  }else{
1926  break;
1927  }
1928  }
1929  bins.push_back(std::make_pair(start,end));
1930  // if (roi->get_chid() == 1240)
1931  // std::cout << qx << " " << start+start_bin << " " << end + start_bin << " " << 3*rms << std::endl;
1932  i = end;
1933  }
1934  }
1935 
1936  // std::cout << "kaka4 " << std::endl;
1937 
1938  std::vector<int> saved_b;
1939  for (int i=0;i!=int(bins.size());i++){
1940  int start = bins.at(i).first;
1941  int end = bins.at(i).second;
1942  // find minimum or zero
1943  float min = 1e9;
1944  int bin_min = start;
1945  for (int j=start;j<=end;j++){
1946  if (fabs(temp_signal.at(j)) < 1e-3){
1947  bin_min = j;
1948  break;
1949  }else{
1950  if (temp_signal.at(j) < min){
1951  min = temp_signal.at(j);
1952  bin_min = j;
1953  }
1954  }
1955  }
1956 
1957  // if (roi->get_chid() == 1308)
1958  // std::cout << bin_min+start_bin << std::endl;
1959 
1960  saved_b.push_back(bin_min);
1961  }
1962 
1963  // std::cout << "kaka5 " << std::endl;
1964  // test
1965 
1966  // if (saved_b.size() >=0){
1967  {
1968  Waveform::realseq_t temp1_signal = temp_signal;
1969  temp_signal.clear();
1970  temp_signal.resize(temp1_signal.size(),0);
1971  // htemp->Reset();
1972  // std::cout << saved_b.size() << " " << bins.size() << " " << htemp->GetNbinsX() << std::endl;
1973  for (int j=0;j<int(saved_b.size())-1;j++){
1974  // if (irow==1240)
1975  // std::cout << saved_b.size() << " " << j << " " << saved<< std::endl;
1976 
1977 
1978  //int flag = 0;
1979  int start_pos = saved_b.at(j);
1980  float start_content = temp1_signal.at(start_pos);//htemp1->GetBinContent(start_pos+1);
1981  int end_pos = saved_b.at(j+1);
1982  float end_content = temp1_signal.at(end_pos);//htemp1->GetBinContent(end_pos+1);
1983 
1984  for (int k = start_pos; k!=end_pos+1;k++){
1985  double temp_content = temp1_signal.at(k) - (start_content + (end_content-start_content) * (k-start_pos) / (end_pos - start_pos));
1986  temp_signal.at(k) = temp_content;
1987  }
1988  }
1989  }
1990  }
1991 
1992  // std::cout << "kaka6 " << std::endl;
1993 
1994  // get back to the original content
1995  for (int i=0;i!=int(temp_signal.size());i++){
1996  contents.at(i) = temp_signal.at(i);//htemp->GetBinContent(i+1);
1997  }
1998 
1999  // delete s;
2000  // delete htemp;
2001 
2002 }
2003 
2005  int start_bin = roi->get_start_bin();
2006  int end_bin = roi->get_end_bin();
2007  if (start_bin <0 || end_bin < 0) return;
2008 
2009  Waveform::realseq_t temp_signal(end_bin-start_bin+1,0);
2010  // TH1F *htemp = new TH1F("htemp","htemp",end_bin-start_bin+1,start_bin,end_bin+1);
2011  std::vector<float>& contents = roi->get_contents();
2012  for (size_t i=0;i!=temp_signal.size();i++){
2013  temp_signal.at(i) = contents.at(i);
2014  // htemp->SetBinContent(i+1,contents.at(i));
2015  }
2016 
2017  // now create many new ROIs
2018  std::vector<int> bins;
2019  for (size_t i=0;i!= temp_signal.size(); i++){
2020  if (fabs(temp_signal.at(i))<1e-3)
2021  bins.push_back(i+start_bin);
2022  }
2023  int chid = roi->get_chid();
2024  int plane = roi->get_plane();
2025  SignalROISelection new_rois;
2026 
2027  // if (chid == 1274)
2028  // std::cout << "BreakROI1: " << chid << " " << roi->get_start_bin() << " " << roi->get_end_bin() << " " << bins.size() << " " << htemp->GetBinContent(1) << " " << htemp->GetBinContent(end_bin-start_bin+1) << std::endl;
2029 
2030  Waveform::realseq_t signal(end_bin+1,0);
2031  // TH1F *h1 = new TH1F("h1","h1",end_bin+1,0,end_bin+1);
2032  for (int i=0;i!=int(bins.size())-1;i++){
2033  int start_bin1 = bins.at(i);
2034  int end_bin1 = bins.at(i+1);
2035  // if (chid == 1274)
2036  // std::cout << start_bin1 << " " << end_bin1 << std::endl;
2037  signal.clear();
2038  signal.resize(end_bin+1,0);
2039  // h1->Reset();
2040  for (int j=start_bin1;j<=end_bin1;j++){
2041  signal.at(j) = temp_signal.at(j-start_bin);
2042  // h1->SetBinContent(j+1,htemp->GetBinContent(j-start_bin+1));
2043  }
2044  if (start_bin1 >=0 && end_bin1 >start_bin1){
2045  SignalROI *sub_roi = new SignalROI(plane,chid,start_bin1,end_bin1,signal);
2046  new_rois.push_back(sub_roi);
2047  }
2048  }
2049 
2050  // update the list
2051  if (chid < nwire_u){
2052  auto it = std::find(rois_u_loose.at(chid).begin(),rois_u_loose.at(chid).end(),roi);
2053  rois_u_loose.at(chid).erase(it);
2054  for (size_t i=0;i!=new_rois.size();i++){
2055  rois_u_loose.at(chid).push_back(new_rois.at(i));
2056  }
2057  }else if (chid < nwire_u+nwire_v){
2058  auto it = std::find(rois_v_loose.at(chid-nwire_u).begin(),rois_v_loose.at(chid-nwire_u).end(),roi);
2059  rois_v_loose.at(chid-nwire_u).erase(it);
2060  for (size_t i=0;i!=new_rois.size();i++){
2061  rois_v_loose.at(chid-nwire_u).push_back(new_rois.at(i));
2062  }
2063  }
2064 
2065  // update all the maps
2066  // update front map
2067  if (front_rois.find(roi)!=front_rois.end()){
2068  SignalROISelection next_rois = front_rois[roi];
2069  for (size_t i=0;i!=next_rois.size();i++){
2070  //unlink the current roi
2071  unlink(roi,next_rois.at(i));
2072  //loop new rois and link them
2073  for (size_t j=0;j!=new_rois.size();j++){
2074  if (new_rois.at(j)->overlap(next_rois.at(i)))
2075  link(new_rois.at(j),next_rois.at(i));
2076  }
2077  }
2078  front_rois.erase(roi);
2079  }
2080  // update back map
2081  if (back_rois.find(roi)!=back_rois.end()){
2082  SignalROISelection prev_rois = back_rois[roi];
2083  for (size_t i=0;i!=prev_rois.size();i++){
2084  // unlink the current roi
2085  unlink(prev_rois.at(i),roi);
2086  // loop new rois and link them
2087  for (size_t j=0;j!=new_rois.size();j++){
2088  if (new_rois.at(j)->overlap(prev_rois.at(i)))
2089  link(prev_rois.at(i),new_rois.at(j));
2090  }
2091  }
2092  back_rois.erase(roi);
2093  }
2094 
2095  // update contained map
2096  if (contained_rois.find(roi)!=contained_rois.end()){
2097  SignalROISelection tight_rois = contained_rois[roi];
2098  for (size_t i=0;i!=tight_rois.size();i++){
2099  for (size_t j=0;j!=new_rois.size();j++){
2100  if (new_rois.at(j)->overlap(tight_rois.at(i))){
2101  if (contained_rois.find(new_rois.at(j)) == contained_rois.end()){
2102  SignalROISelection temp_rois;
2103  temp_rois.push_back(tight_rois.at(i));
2104  contained_rois[new_rois.at(j)] = temp_rois;
2105  }else{
2106  contained_rois[new_rois.at(j)].push_back(tight_rois.at(i));
2107  }
2108  }
2109  }
2110  }
2111  contained_rois.erase(roi);
2112  }
2113 
2114  // delete the old ROI
2115  delete roi;
2116  // delete h1;
2117  // delete htemp;
2118 }
2119 
2120 void ROI_refinement::BreakROIs(int plane, ROI_formation& roi_form){
2121  SignalROISelection all_rois;
2122 
2123  if (plane==0){
2124  std::vector<float>& rms_u = roi_form.get_uplane_rms();
2125  for (size_t i=0;i!=rois_u_loose.size();i++){
2126  for (auto it = rois_u_loose.at(i).begin(); it!= rois_u_loose.at(i).end(); it++){
2127 
2128  BreakROI(*it,rms_u.at(i));
2129  all_rois.push_back(*it);
2130 
2131  }
2132  }
2133  }else if (plane==1){
2134  std::vector<float>& rms_v = roi_form.get_vplane_rms();
2135  for (size_t i=0;i!=rois_v_loose.size();i++){
2136  for (auto it = rois_v_loose.at(i).begin(); it!= rois_v_loose.at(i).end(); it++){
2137  BreakROI(*it,rms_v.at(i));
2138  all_rois.push_back(*it);
2139  }
2140  }
2141  }
2142 
2143 
2144  for (size_t i=0;i!=all_rois.size();i++){
2145  // if (all_rois.at(i)->get_chid()==1151){
2146  // std::cout << all_rois.at(i)->get_chid() << " " << all_rois.at(i)->get_start_bin() << " " << all_rois.at(i)->get_end_bin() << std::endl;
2147  // for (int j=0;j!=all_rois.at(i)->get_contents().size();j++){
2148  // std::cout << j << " " << all_rois.at(i)->get_contents().at(j) << std::endl;
2149  // }
2150  // }
2151 
2152  BreakROI1(all_rois.at(i));
2153  }
2154 
2155 
2156 }
2157 
2158 
2159 void ROI_refinement::refine_data(int plane, ROI_formation& roi_form){
2160 
2161  //if (plane==2) std::cout << "Xin: " << rois_w_tight.at(69).size() << " " << std::endl;
2162 
2163  //std::cout << "Clean up loose ROIs" << std::endl;
2164  CleanUpROIs(plane);
2165  //std::cout << "Generate more loose ROIs from isolated good tight ROIs" << std::endl;
2166  generate_merge_ROIs(plane);
2167 
2168  // if (plane==2) std::cout << "Xin: " << rois_w_tight.at(69).size() << " " << std::endl;
2169 
2170  for (int qx = 0; qx!=break_roi_loop; qx++){
2171  // std::cout << "Break loose ROIs" << std::endl;
2172  BreakROIs(plane, roi_form);
2173  // std::cout << "Clean up ROIs 2nd time" << std::endl;
2174  CheckROIs(plane, roi_form);
2175  CleanUpROIs(plane);
2176  }
2177 
2178  // if (plane==2) std::cout << "Xin: " << rois_w_tight.at(69).size() << " " << std::endl;
2179 
2180 
2181  // std::cout << "Shrink ROIs" << std::endl;
2182  ShrinkROIs(plane, roi_form);
2183  // std::cout << "Clean up ROIs 3rd time" << std::endl;
2184  CheckROIs(plane, roi_form);
2185  CleanUpROIs(plane);
2186 
2187  // if (plane==2) std::cout << "Xin: " << rois_w_tight.at(69).size() << " " << std::endl;
2188 
2189  // Further reduce fake hits
2190  // std::cout << "Remove fake hits " << std::endl;
2191  if (plane==2){
2193  }else{
2194  CleanUpInductionROIs(plane);
2195  }
2196 
2197  ExtendROIs();
2198  //TestROIs();
2199  //if (plane==2) std::cout << "Xin: " << rois_w_tight.at(69).size() << " " << std::endl;
2200 }
2201 
2202 // Basically rewrite the refine_data()
2203 // But now do the ROI operation step by step
2205 
2206  if (cmd=="CleanUpROIs") {
2207  // std::cout << "Clean up loose ROIs" << std::endl;
2208  CleanUpROIs(plane);
2209  //std::cout << "Generate more loose ROIs from isolated good tight ROIs" << std::endl;
2210  generate_merge_ROIs(plane);
2211  }
2212 
2213  else if (cmd=="BreakROIs") {
2214  // std::cout << "Break loose ROIs" << std::endl;
2215  BreakROIs(plane, roi_form);
2216  // std::cout << "Clean up ROIs 2nd time" << std::endl;
2217  CheckROIs(plane, roi_form);
2218  CleanUpROIs(plane);
2219  }
2220 
2221  else if (cmd=="ShrinkROIs") {
2222  // std::cout << "Shrink ROIs" << std::endl;
2223  ShrinkROIs(plane, roi_form);
2224  // std::cout << "Clean up ROIs 3rd time" << std::endl;
2225  CheckROIs(plane, roi_form);
2226  CleanUpROIs(plane);
2227  }
2228 
2229  else if (cmd=="ExtendROIs") {
2230  // Further reduce fake hits
2231  // std::cout << "Remove fake hits " << std::endl;
2232  if (plane==2){
2234  }else{
2235  CleanUpInductionROIs(plane);
2236  }
2237 
2238  ExtendROIs();
2239  //TestROIs();
2240  }
2241 
2242  }
2243 
2245 
2246  for (int chid = 0; chid != nwire_u; chid ++){
2247  for (auto it = rois_u_loose.at(chid).begin(); it!= rois_u_loose.at(chid).end();it++){
2248  SignalROI *roi = *it;
2249  //loop through front
2250  for (auto it1 = front_rois[roi].begin(); it1!=front_rois[roi].end(); it1++){
2251  SignalROI *roi1 = *it1;
2252  if (find(rois_u_loose.at(chid+1).begin(), rois_u_loose.at(chid+1).end(), roi1) == rois_u_loose.at(chid+1).end())
2253  std::cout << chid << " u " << +1 << " " << roi << " " << roi1 << std::endl;
2254  }
2255 
2256  // loop through back
2257  for (auto it1 = back_rois[roi].begin(); it1!=back_rois[roi].end(); it1++){
2258  SignalROI *roi1 = *it1;
2259  if (find(rois_u_loose.at(chid-1).begin(), rois_u_loose.at(chid-1).end(), roi1) == rois_u_loose.at(chid-1).end())
2260  std::cout << chid << " u " << -1 << " " << roi << " " << roi1 << std::endl;
2261  }
2262  }
2263  }
2264 
2265 
2266  for (int chid = 0; chid != nwire_v; chid ++){
2267  for (auto it = rois_v_loose.at(chid).begin(); it!= rois_v_loose.at(chid).end();it++){
2268  SignalROI *roi = *it;
2269  //loop through front
2270  for (auto it1 = front_rois[roi].begin(); it1!=front_rois[roi].end(); it1++){
2271  SignalROI *roi1 = *it1;
2272  if (find(rois_v_loose.at(chid+1).begin(), rois_v_loose.at(chid+1).end(), roi1) == rois_v_loose.at(chid+1).end())
2273  std::cout << chid << " v " << +1 << " " << roi << " " << roi1 << std::endl;
2274  }
2275 
2276  // loop through back
2277  for (auto it1 = back_rois[roi].begin(); it1!=back_rois[roi].end(); it1++){
2278  SignalROI *roi1 = *it1;
2279  if (find(rois_v_loose.at(chid-1).begin(), rois_v_loose.at(chid-1).end(), roi1) == rois_v_loose.at(chid-1).end())
2280  std::cout << chid << " v " << -1 << " " << roi << " " << roi1 << std::endl;
2281  }
2282  }
2283  }
2284 
2285  for (int chid = 0; chid != nwire_w; chid ++){
2286  for (auto it = rois_w_tight.at(chid).begin(); it!= rois_w_tight.at(chid).end();it++){
2287  SignalROI *roi = *it;
2288  //loop through front
2289  for (auto it1 = front_rois[roi].begin(); it1!=front_rois[roi].end(); it1++){
2290  SignalROI *roi1 = *it1;
2291  if (find(rois_w_tight.at(chid+1).begin(), rois_w_tight.at(chid+1).end(), roi1) == rois_w_tight.at(chid+1).end())
2292  std::cout << chid << " w " << +1 << " " << roi << " " << roi1 << std::endl;
2293  }
2294 
2295  // loop through back
2296  for (auto it1 = back_rois[roi].begin(); it1!=back_rois[roi].end(); it1++){
2297  SignalROI *roi1 = *it1;
2298  if (find(rois_w_tight.at(chid-1).begin(), rois_w_tight.at(chid-1).end(), roi1) == rois_w_tight.at(chid-1).end())
2299  std::cout << chid << " w " << -1 << " " << roi << " " << roi1 << std::endl;
2300  }
2301  }
2302  }
2303 }
2304 
2305 
2307 
2308  bool flag = true;
2309 
2310  // U plane ...
2311  for (int chid = 0; chid != nwire_u; chid ++){
2312  rois_u_loose.at(chid).sort(CompareRois());
2313  for (auto it = rois_u_loose.at(chid).begin(); it!= rois_u_loose.at(chid).end();it++){
2314  SignalROI *roi = *it;
2315  // initialize the extended bins ...
2316  roi->set_ext_start_bin(roi->get_start_bin());
2317  roi->set_ext_end_bin(roi->get_end_bin());
2318 
2319  if (flag){
2320  //loop through front
2321  for (auto it1 = front_rois[roi].begin(); it1!=front_rois[roi].end(); it1++){
2322  SignalROI *roi1 = *it1;
2323  int ext_start_bin = roi->get_ext_start_bin();
2324  int ext_end_bin = roi->get_ext_end_bin();
2325  if (ext_start_bin > roi1->get_start_bin()) ext_start_bin = roi1->get_start_bin();
2326  if (ext_end_bin < roi1->get_end_bin()) ext_end_bin = roi1->get_end_bin();
2327  roi->set_ext_start_bin(ext_start_bin);
2328  roi->set_ext_end_bin(ext_end_bin);
2329  // std::cout << roi->get_chid() << " " << roi1->get_chid() << " " << roi->get_start_bin() << " " << roi->get_end_bin() << " " << roi1->get_start_bin() << " " << roi1->get_end_bin() << std::endl;
2330  }
2331 
2332  // loop through back
2333  for (auto it1 = back_rois[roi].begin(); it1!=back_rois[roi].end(); it1++){
2334  SignalROI *roi1 = *it1;
2335  int ext_start_bin = roi->get_ext_start_bin();
2336  int ext_end_bin = roi->get_ext_end_bin();
2337  if (ext_start_bin > roi1->get_start_bin()) ext_start_bin = roi1->get_start_bin();
2338  if (ext_end_bin < roi1->get_end_bin()) ext_end_bin = roi1->get_end_bin();
2339  roi->set_ext_start_bin(ext_start_bin);
2340  roi->set_ext_end_bin(ext_end_bin);
2341  // std::cout << roi->get_chid() << " " << roi1->get_chid() << " " << roi->get_start_bin() << " " << roi->get_end_bin() << " " << roi1->get_start_bin() << " " << roi1->get_end_bin() << std::endl;
2342  }
2343  //std::cout << chid << " " << roi->get_start_bin() << " " << roi->get_end_bin() << std::endl;
2344  }
2345  }
2346 
2347  if (flag){
2348  SignalROI *prev_roi = 0;
2349  for (auto it = rois_u_loose.at(chid).begin(); it!= rois_u_loose.at(chid).end();it++){
2350  SignalROI *roi = *it;
2351  if (prev_roi!=0){
2352  if (prev_roi->get_ext_end_bin() > roi->get_ext_start_bin()){
2353  prev_roi->set_ext_end_bin(int(prev_roi->get_end_bin() * 0.5 + roi->get_start_bin()*0.5));
2354  roi->set_ext_start_bin(int(prev_roi->get_end_bin() * 0.5 + roi->get_start_bin()*0.5));
2355  }
2356  }
2357  prev_roi = roi;
2358  }
2359  }
2360  // for (auto it = rois_u_loose.at(chid).begin(); it!= rois_u_loose.at(chid).end();it++){
2361  // SignalROI *roi = *it;
2362  // std::cout << chid << " " << roi->get_ext_start_bin() << " " << roi->get_ext_end_bin() << std::endl;
2363  // }
2364  }
2365  // V plane
2366  for (int chid = 0; chid != nwire_v; chid ++){
2367  rois_v_loose.at(chid).sort(CompareRois());
2368  for (auto it = rois_v_loose.at(chid).begin(); it!= rois_v_loose.at(chid).end();it++){
2369  SignalROI *roi = *it;
2370  // initialize the extended bins ...
2371  roi->set_ext_start_bin(roi->get_start_bin());
2372  roi->set_ext_end_bin(roi->get_end_bin());
2373 
2374  if (flag){
2375  //loop through front
2376  for (auto it1 = front_rois[roi].begin(); it1!=front_rois[roi].end(); it1++){
2377  SignalROI *roi1 = *it1;
2378  int ext_start_bin = roi->get_ext_start_bin();
2379  int ext_end_bin = roi->get_ext_end_bin();
2380  if (ext_start_bin > roi1->get_start_bin()) ext_start_bin = roi1->get_start_bin();
2381  if (ext_end_bin < roi1->get_end_bin()) ext_end_bin = roi1->get_end_bin();
2382  roi->set_ext_start_bin(ext_start_bin);
2383  roi->set_ext_end_bin(ext_end_bin);
2384  // std::cout << roi->get_chid() << " " << roi1->get_chid() << " " << roi->get_start_bin() << " " << roi->get_end_bin() << " " << roi1->get_start_bin() << " " << roi1->get_end_bin() << std::endl;
2385  }
2386 
2387  // loop through back
2388  for (auto it1 = back_rois[roi].begin(); it1!=back_rois[roi].end(); it1++){
2389  SignalROI *roi1 = *it1;
2390  int ext_start_bin = roi->get_ext_start_bin();
2391  int ext_end_bin = roi->get_ext_end_bin();
2392  if (ext_start_bin > roi1->get_start_bin()) ext_start_bin = roi1->get_start_bin();
2393  if (ext_end_bin < roi1->get_end_bin()) ext_end_bin = roi1->get_end_bin();
2394  roi->set_ext_start_bin(ext_start_bin);
2395  roi->set_ext_end_bin(ext_end_bin);
2396  // std::cout << roi->get_chid() << " " << roi1->get_chid() << " " << roi->get_start_bin() << " " << roi->get_end_bin() << " " << roi1->get_start_bin() << " " << roi1->get_end_bin() << std::endl;
2397  }
2398  //std::cout << chid << " " << roi->get_start_bin() << " " << roi->get_end_bin() << std::endl;
2399  }
2400  }
2401 
2402  if (flag){
2403  SignalROI *prev_roi = 0;
2404  for (auto it = rois_v_loose.at(chid).begin(); it!= rois_v_loose.at(chid).end();it++){
2405  SignalROI *roi = *it;
2406  if (prev_roi!=0){
2407  if (prev_roi->get_ext_end_bin() > roi->get_ext_start_bin()){
2408  prev_roi->set_ext_end_bin(int(prev_roi->get_end_bin() * 0.5 + roi->get_start_bin()*0.5));
2409  roi->set_ext_start_bin(int(prev_roi->get_end_bin() * 0.5 + roi->get_start_bin()*0.5));
2410  }
2411  }
2412  prev_roi = roi;
2413  }
2414  }
2415  // for (auto it = rois_v_loose.at(chid).begin(); it!= rois_v_loose.at(chid).end();it++){
2416  // SignalROI *roi = *it;
2417  // std::cout << chid << " " << roi->get_ext_start_bin() << " " << roi->get_ext_end_bin() << std::endl;
2418  // }
2419  }
2420 
2421  // W plane
2422  for (int chid = 0; chid != nwire_w; chid ++){
2423  rois_w_tight.at(chid).sort(CompareRois());
2424  for (auto it = rois_w_tight.at(chid).begin(); it!= rois_w_tight.at(chid).end();it++){
2425  SignalROI *roi = *it;
2426  // initialize the extended bins ...
2427  roi->set_ext_start_bin(roi->get_start_bin());
2428  roi->set_ext_end_bin(roi->get_end_bin());
2429 
2430  if (flag){
2431  //loop through front
2432  for (auto it1 = front_rois[roi].begin(); it1!=front_rois[roi].end(); it1++){
2433  SignalROI *roi1 = *it1;
2434  int ext_start_bin = roi->get_ext_start_bin();
2435  int ext_end_bin = roi->get_ext_end_bin();
2436  if (ext_start_bin > roi1->get_start_bin()) ext_start_bin = roi1->get_start_bin();
2437  if (ext_end_bin < roi1->get_end_bin()) ext_end_bin = roi1->get_end_bin();
2438  roi->set_ext_start_bin(ext_start_bin);
2439  roi->set_ext_end_bin(ext_end_bin);
2440  // std::cout << roi->get_chid() << " " << roi1->get_chid() << " " << roi->get_start_bin() << " " << roi->get_end_bin() << " " << roi1->get_start_bin() << " " << roi1->get_end_bin() << std::endl;
2441  }
2442 
2443  // loop through back
2444  for (auto it1 = back_rois[roi].begin(); it1!=back_rois[roi].end(); it1++){
2445  SignalROI *roi1 = *it1;
2446  int ext_start_bin = roi->get_ext_start_bin();
2447  int ext_end_bin = roi->get_ext_end_bin();
2448  if (ext_start_bin > roi1->get_start_bin()) ext_start_bin = roi1->get_start_bin();
2449  if (ext_end_bin < roi1->get_end_bin()) ext_end_bin = roi1->get_end_bin();
2450  roi->set_ext_start_bin(ext_start_bin);
2451  roi->set_ext_end_bin(ext_end_bin);
2452  // std::cout << roi->get_chid() << " " << roi1->get_chid() << " " << roi->get_start_bin() << " " << roi->get_end_bin() << " " << roi1->get_start_bin() << " " << roi1->get_end_bin() << std::endl;
2453  }
2454  //std::cout << chid << " " << roi->get_start_bin() << " " << roi->get_end_bin() << std::endl;
2455  }
2456 
2457  }
2458  if (flag){
2459  SignalROI *prev_roi = 0;
2460  for (auto it = rois_w_tight.at(chid).begin(); it!= rois_w_tight.at(chid).end();it++){
2461  SignalROI *roi = *it;
2462  if (prev_roi!=0){
2463  if (prev_roi->get_ext_end_bin() > roi->get_ext_start_bin()){
2464  prev_roi->set_ext_end_bin(int(prev_roi->get_end_bin() * 0.5 + roi->get_start_bin()*0.5));
2465  roi->set_ext_start_bin(int(prev_roi->get_end_bin() * 0.5 + roi->get_start_bin()*0.5));
2466  }
2467  }
2468  prev_roi = roi;
2469  }
2470  }
2471  // for (auto it = rois_w_tight.at(chid).begin(); it!= rois_w_tight.at(chid).end();it++){
2472  // SignalROI *roi = *it;
2473  // std::cout << chid << " " << roi->get_ext_start_bin() << " " << roi->get_ext_end_bin() << std::endl;
2474  // }
2475  }
2476 }
2477 
2478 
2479 
2480 
2481 // Local Variables:
2482 // mode: c++
2483 // c-basic-offset: 2
2484 // End:
std::vector< std::pair< int, int > > & get_self_rois(int chid)
Definition: ROI_formation.h:33
Sequence< real_t > realseq_t
Definition: Waveform.h:31
double rms(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:39
constexpr auto end_pos
void link(SignalROI *prev_roi, SignalROI *next_roi)
std::string string
Definition: nybbler.cc:12
ROI_refinement(Waveform::ChannelMaskMap &cmm, int nwire_u, int nwire_v, int nwire_w, float th_factor=3.0, float fake_signal_low_th=500, float fake_signal_high_th=1000, float fake_signal_low_th_ind_factor=1.0, float fake_signal_high_th_ind_factor=1.0, int pad=5, int break_roi_loop=2, float th_peak=3.0, float sep_peak=6.0, float low_peak_sep_threshold_pre=1200, int max_npeaks=200, float sigma=2, float th_percent=0.1)
std::vector< std::pair< int, int > > get_above_threshold(float th)
Definition: SignalROI.cxx:109
void set_ext_start_bin(int a)
Definition: SignalROI.h:25
std::list< SignalROI * > SignalROIList
Definition: SignalROI.h:51
void unlink(SignalROI *prev_roi, SignalROI *next_roi)
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
const double e
Configuration find(Configuration &lst, const std::string &dotpath, const T &val)
Return dictionary in given list if it value at dotpath matches.
std::map< std::string, ChannelMasks > ChannelMaskMap
Collect channel masks by some label.
Definition: Waveform.h:59
void BreakROI(SignalROI *roi, float rms)
std::vector< float > & get_contents()
Definition: SignalROI.h:30
void ShrinkROIs(int plane, ROI_formation &roi_form)
std::vector< float > & get_wplane_rms()
Definition: ROI_formation.h:55
void load_data(int plane, const Array::array_xxf &r_data, ROI_formation &roi_form)
logptr_t logger(std::string name)
Definition: Logging.cxx:71
std::vector< SignalROIList > SignalROIChList
Definition: SignalROI.h:54
bool overlap(SignalROI *roi)
Definition: SignalROI.cxx:80
void apply_roi(int plane, Array::array_xxf &r_data)
Definition: Main.h:22
std::map< int, std::vector< std::pair< int, int > > > bad_ch_map
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
SignalROIChList & get_rois_by_plane(int plane)
int find_peak(Waveform::realseq_t &signal)
Definition: PeakFinding.cxx:38
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
Definition: format.h:1970
std::vector< SignalROI * > SignalROISelection
Definition: SignalROI.h:52
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:67
std::vector< std::pair< int, int > > & get_loose_rois(int chid)
Definition: ROI_formation.h:43
list cmd
Definition: getreco.py:22
std::vector< float > & get_uplane_rms()
Definition: ROI_formation.h:53
void refine_data(int plane, ROI_formation &roi_form)
static QCString * s
Definition: config.cpp:1042
QTextStream & endl(QTextStream &s)
std::vector< float > & get_vplane_rms()
Definition: ROI_formation.h:54
Eigen::ArrayXXf array_xxf
A real, 2D array.
Definition: Array.h:54
void refine_data_debug_mode(int plane, ROI_formation &roi_form, const std::string &cmd)
void CheckROIs(int plane, ROI_formation &roi_form)
void ShrinkROI(SignalROI *roi, ROI_formation &roi_form)
void BreakROIs(int plane, ROI_formation &roi_form)