#ifndef WIRECELLSIGPROC_ROIREFINEMENT
#define WIRECELLSIGPROC_ROIREFINEMENT

#include "SignalROI.h"
#include "ROI_formation.h"
#include "WireCellUtil/Array.h"
#include "WireCellUtil/Waveform.h"
#include "WireCellUtil/Logging.h"

#include <vector>
#include <map>

namespace WireCell{
  namespace SigProc{
    
    
    
    class ROI_refinement{
    public:
      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); 
      ~ROI_refinement();

      void Clear();

      // initialize the ROIs
      void load_data(int plane, const Array::array_xxf& r_data, ROI_formation& roi_form);
      void refine_data(int plane, ROI_formation& roi_form);
      void refine_data_debug_mode(int plane, ROI_formation& roi_form, const std::string& cmd);

      void apply_roi(int plane, Array::array_xxf& r_data);
      
      SignalROIChList& get_u_rois(){return rois_u_loose;};
      SignalROIChList& get_v_rois(){return rois_v_loose;};
      SignalROIChList& get_w_rois(){return rois_w_tight;};
      SignalROIChList& get_rois_by_plane(int plane);
      
    private:
      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;

      //peak finding ones
      int max_npeaks;
      float sigma;
      float th_percent;
      
      void unlink(SignalROI *prev_roi, SignalROI *next_roi);
      void link(SignalROI *prev_roi, SignalROI *next_roi);
      void CleanUpROIs(int plane);
      void generate_merge_ROIs(int plane);
      void CheckROIs(int plane, ROI_formation& roi_form);

      void CleanUpCollectionROIs();
      void CleanUpInductionROIs(int plane);
      void ShrinkROIs(int plane, ROI_formation& roi_form);
      void ShrinkROI(SignalROI *roi, ROI_formation& roi_form);

      void BreakROIs(int plane, ROI_formation& roi_form);
      void BreakROI(SignalROI *roi, float rms);
      void BreakROI1(SignalROI *roi);
      
      void ExtendROIs();

      void TestROIs();
      
      std::map<int,std::vector<std::pair<int,int>>> bad_ch_map;
      
      
      SignalROIChList rois_u_tight;
      SignalROIChList rois_v_tight;
      SignalROIChList rois_w_tight;
    
      SignalROIChList rois_u_loose;
      SignalROIChList rois_v_loose;
   
    
      SignalROIMap front_rois;
      SignalROIMap back_rois;
      SignalROIMap contained_rois;
      
      Log::logptr_t log;
    };
  }
}
#endif
// Local Variables:
// mode: c++
// c-basic-offset: 2
// End:
