#ifndef WIRECELLPID_UBOONE_TOYFIDUCIAL_H
#define WIRECELLPID_UBOONE_TOYFIDUCIAL_H

#include "WireCellData/Units.h"
#include "WireCellData/Point.h"
#include "WireCellData/SlimMergeGeomCell.h"
//#include "WireCellData/FlashTPCBundle.h"
#include "WireCellData/ToyCTPointCloud.h"
#include "WireCellData/LMBDT.h"
#include "WireCellData/Opflash.h"
#include "WireCellData/PhotonLibrary.h"

#include "WireCellPID/PR3DCluster.h"

#include "TVector3.h"
#include "TGraph.h"
#include "TFile.h"

#include <vector>
#include <map>

namespace WireCellPID{
  class ToyFiducial{
  public:
    ToyFiducial(int dead_region_ch_ext = 3, double offset_t=800, double offset_u=0, double offset_v=0, double offset_w=0, double slope_t=1./2*units::mm, double slope_u=1./(3*units::mm), double slope_v=1./(3*units::mm), double slope_w=1./(3*units::mm), double angle_u=-1.047198, double angle=1.047198, double angle_w=0,
		double boundary_dis_cut=2*units::cm, double top=117*units::cm, double bottom=-116*units::cm, double upstream=0*units::cm, double downstream=1037*units::cm, double anode = 0*units::cm, double cathode=256*units::cm, int flag_data=1);
    ~ToyFiducial();

  
    
    
    void set_offset_t(double value){offset_t=value;};
    
    // helper functions ...
    bool inside_fiducial_volume(WireCell::Point& p, double offset_x=0);
    bool inside_dead_region(WireCell::Point& p);
    bool check_dead_volume(WireCell::Point& p, TVector3& dir, double step = 1.0*units::cm, double offset_x=0);
    bool check_signal_processing(WireCell::Point& p, TVector3& dir, WireCell::ToyCTPointCloud& ct_point_cloud, double step = 1.0*units::cm, double offset_x=0);

    // TGM tagger ...
    bool check_neutrino_candidate(WireCellPID::PR3DCluster *main_cluster, WireCell::WCPointCloud<double>::WCPoint& wcp1, WireCell::WCPointCloud<double>::WCPoint& wcp2, double offset_x, WireCell::ToyCTPointCloud& ct_point_cloud, bool flag_2view_check = true);
    bool check_tgm(WireCellPID::PR3DCluster* main_cluster,WireCell::Opflash* main_flash, double offset_x, WireCell::ToyCTPointCloud& ct_point_cloud);
    
    // fully contained tagger ...
    bool check_fully_contained(WireCellPID::PR3DCluster* main_cluster, double offset_x, WireCell::ToyCTPointCloud& ct_point_cloud);
    
    // London's new tagger
    std::tuple<int, WireCellPID::PR3DCluster*, WireCell::Opflash*> cosmic_tagger(WireCell::OpflashSelection& flashes, WireCellPID::PR3DCluster* main_cluster, WireCell::Opflash* main_flash, std::tuple<int, double, double, int>& bundle_info, WireCell::Photon_Library *pl, int time_offset, int nrebin, float unit_dis, WireCell::ToyCTPointCloud& ct_point_cloud, int run_no, int subrun_no, int event_no, bool flag_data, bool debug_tagger=false);

    // check STM code ...
    bool check_stm(WireCellPID::PR3DCluster* cluster, double offset_x, double flash_time, WireCell::ToyCTPointCloud& ct_point_cloud, std::map<int,std::map<const WireCell::GeomWire*, WireCell::SMGCSelection > >& global_wc_map, int& event_type);

    bool eval_stm(WireCellPID::PR3DCluster* main_cluster, int kink_num, double peak_range = 40*units::cm, double offset_x = 0*units::cm, double com_range = 35*units::cm);

    bool detect_proton(WireCellPID::PR3DCluster* main_cluster, int kink_num);

    int find_first_kink(WireCellPID::PR3DCluster* main_cluster);

    bool check_full_detector_dead();
    
    void AddDeadRegion(WireCell::SlimMergeGeomCell* mcell, std::vector<int>& time_slices);

    WireCell::SMGCSelection& get_Dead_mcells(){return mcells;};

    
    
  protected:
    // boundary
    double m_top; // top distance
    double m_bottom; // bottom distance
    double m_upstream;
    double m_downstream;
    double m_anode;
    double m_cathode;
    
    // space charge boundary
    double m_sc_bottom_1_x, m_sc_bottom_1_y;
    double m_sc_bottom_2_x, m_sc_bottom_2_y;

    double m_sc_top_1_x, m_sc_top_1_y;
    double m_sc_top_2_x, m_sc_top_2_y;

    double m_sc_upstream_1_x, m_sc_upstream_1_z;
    double m_sc_upstream_2_x, m_sc_upstream_2_z;

    double m_sc_downstream_1_x, m_sc_downstream_1_z;
    double m_sc_downstream_2_x, m_sc_downstream_2_z;

    std::vector<double> boundary_xy_x, boundary_xy_y;
    std::vector<double> boundary_xz_x, boundary_xz_z;
    
    // dead regions ... 
    WireCell::SMGCSelection mcells;
    std::map<WireCell::SlimMergeGeomCell*, std::pair<int,int>> mcell_time_map;
    std::map<int, std::set<WireCell::SlimMergeGeomCell*>> ch_mcell_set_map;
    
    // conversion between positions to the channel and time ???

    // convert time into a position
    // (time_slice - offset_t) / slope_t = position_x 
    double offset_t, slope_t;
    // convert u wire number into a position
    // (u_index -offset_u) / slope_u = position_u
    double offset_u, slope_u;
    // convert v wire number into a position
    double offset_v, slope_v;
    // convert w wire number into a position 
    double offset_w, slope_w;
    double angle_u, angle_v, angle_w;

    int dead_region_ch_ext;

    TFile *file;
    TGraph *g_muon;
    TGraph *g_pion;
    TGraph *g_kaon;
    TGraph *g_proton;
    TGraph *g_electron;
    
  };
}

#endif
