#include "WCPSst/GeomDataSource.h"

#include "WCPData/SlimMergeGeomCell.h"
#include "WCPData/TPCParams.h"
#include "WCPData/Singleton.h"
#include "WCPData/ToyCTPointCloud.h"

#include "WCPPID/ToyFiducial.h"

#include "WCPPID/CalcPoints.h"
#include "WCPPID/PR3DCluster.h"

#include "WCPPID/ExecMon.h"
#include "WCPPID/ImprovePR3DCluster.h"

#include "TH1.h"
#include "TFile.h"
#include "TTree.h"

using namespace WCP;
using namespace std;

int main(int argc, char *argv[])
{
    if (argc < 4) {
        cerr << "usage: wire-cell-prod-bee /path/to/ChannelWireGeometry.txt /path/to/matching.root #entry -d[data=0, "
                "overlay=1, full mc=2]"
             << endl;
        return 1;
    }
    TH1::AddDirectory(kFALSE);

    bool flag_debug_output = true;  // output
    int datatier = 0;               // data=0, overlay=1, full mc=2

    for (Int_t i = 1; i != argc; i++) {
        switch (argv[i][1]) {
        case 'd':
            datatier = atoi(&argv[i][2]);
            break;
        }
    }

    int flag_data = 1;                                  // data
    if (datatier == 1 || datatier == 2) flag_data = 0;  // overlay, full mc

    WCPPID::ExecMon em("starting");
    cout << em("load geometry") << endl;

    WCPSst::GeomDataSource gds(argv[1]);
    std::vector<double> ex = gds.extent();
    cout << "Extent: "
         << " x:" << ex[0] / units::mm << " mm"
         << " y:" << ex[1] / units::m << " m"
         << " z:" << ex[2] / units::m << " m" << endl;
    cout << "Pitch: " << gds.pitch(WirePlaneType_t(0)) << " " << gds.pitch(WirePlaneType_t(1)) << " "
         << gds.pitch(WirePlaneType_t(2)) << endl;
    cout << "Angle: " << gds.angle(WirePlaneType_t(0)) << " " << gds.angle(WirePlaneType_t(1)) << " "
         << gds.angle(WirePlaneType_t(2)) << endl;
    std::cout << argv[2] << " " << argv[3] << std::endl;
    TString filename = argv[2];
    int entry_no = atoi(argv[3]);

    TFile *file = new TFile(filename);
    TTree *Trun = (TTree *) file->Get("Trun");

    if (entry_no >= Trun->GetEntries()) return 0;

    int run_no, subrun_no, event_no;
    int time_offset;
    int nrebin;
    int frame_length;
    int eve_num;
    float unit_dis;

    std::vector<int> *timesliceId = new std::vector<int>;
    std::vector<std::vector<int>> *timesliceChannel = new std::vector<std::vector<int>>;
    std::vector<std::vector<int>> *raw_charge = new std::vector<std::vector<int>>;
    std::vector<std::vector<int>> *raw_charge_err = new std::vector<std::vector<int>>;

    Trun->SetBranchAddress("eventNo", &event_no);
    Trun->SetBranchAddress("runNo", &run_no);
    Trun->SetBranchAddress("subRunNo", &subrun_no);
    unsigned int triggerbits;
    Trun->SetBranchAddress("triggerBits", &triggerbits);
    Trun->SetBranchAddress("unit_dis", &unit_dis);
    Trun->SetBranchAddress("frame_length", &frame_length);
    Trun->SetBranchAddress("eve_num", &eve_num);
    Trun->SetBranchAddress("nrebin", &nrebin);
    Trun->SetBranchAddress("time_offset", &time_offset);

    Trun->SetBranchAddress("timesliceId", &timesliceId);
    Trun->SetBranchAddress("timesliceChannel", &timesliceChannel);
    Trun->SetBranchAddress("raw_charge", &raw_charge);
    Trun->SetBranchAddress("raw_charge_err", &raw_charge_err);

    Trun->GetEntry(entry_no);
    double lowerwindow = 0;
    double upperwindow = 0;
    if ((triggerbits >> 11) & 1U) {
        lowerwindow = 3.0;
        upperwindow = 5.0;
    }  // bnb
    if ((triggerbits >> 12) & 1U) {
        lowerwindow = 4.9295;
        upperwindow = 16.6483;
    }  // NUMI
    if (((triggerbits >> 9) & 1U) && time_offset != 5) {
        lowerwindow = 3.45;
        upperwindow = 5.45;
    }  // extbnb
    if (((triggerbits >> 9) & 1U) && time_offset == 5) {
        lowerwindow = 5.3045;
        upperwindow = 17.0233;
    }  // EXTNUMI

    // define singleton ...
    TPCParams &mp = Singleton<TPCParams>::Instance();

    double pitch_u = gds.pitch(WirePlaneType_t(0));
    double pitch_v = gds.pitch(WirePlaneType_t(1));
    double pitch_w = gds.pitch(WirePlaneType_t(2));
    double time_slice_width = nrebin * unit_dis * 0.5 * units::mm;

    double angle_u = gds.angle(WirePlaneType_t(0));
    double angle_v = gds.angle(WirePlaneType_t(1));
    double angle_w = gds.angle(WirePlaneType_t(2));

    mp.set_pitch_u(pitch_u);
    mp.set_pitch_v(pitch_v);
    mp.set_pitch_w(pitch_w);
    mp.set_angle_u(angle_u);
    mp.set_angle_v(angle_v);
    mp.set_angle_w(angle_w);
    mp.set_ts_width(time_slice_width);

    // test geometry ...
    const GeomWire *uwire = gds.by_planeindex(WirePlaneType_t(0), 0);
    const GeomWire *vwire = gds.by_planeindex(WirePlaneType_t(1), 0);
    const GeomWire *wwire = gds.by_planeindex(WirePlaneType_t(2), 0);
    double first_u_dis = gds.wire_dist(*uwire);  // first U wire center ...
    double first_v_dis = gds.wire_dist(*vwire);  // first V wire center ...
    double first_w_dis = gds.wire_dist(*wwire);  // first W wire center ...

    mp.set_first_u_dis(first_u_dis);
    mp.set_first_v_dis(first_v_dis);
    mp.set_first_w_dis(first_w_dis);

    std::map<int, std::pair<double, double>> dead_u_index;
    std::map<int, std::pair<double, double>> dead_v_index;
    std::map<int, std::pair<double, double>> dead_w_index;

    TTree *T_flash = (TTree *) file->Get("T_flash");
    Double_t time;
    Double_t low_time, high_time;
    Int_t type;
    Int_t flash_id;
    Int_t temp_run_no, temp_subrun_no, temp_event_no;
    Double_t total_PE;
    Double_t PE[32], PE_err[32];
    // std::vector<int> matched_tpc_ids;

    T_flash->SetBranchAddress("runNo", &temp_run_no);
    T_flash->SetBranchAddress("subRunNo", &temp_subrun_no);
    T_flash->SetBranchAddress("eventNo", &temp_event_no);
    T_flash->SetBranchAddress("time", &time);
    T_flash->SetBranchAddress("type", &type);
    T_flash->SetBranchAddress("flash_id", &flash_id);
    T_flash->SetBranchAddress("low_time", &low_time);
    T_flash->SetBranchAddress("high_time", &high_time);
    T_flash->SetBranchAddress("total_PE", &total_PE);
    T_flash->SetBranchAddress("PE", PE);
    T_flash->SetBranchAddress("PE_err", PE_err);
    std::vector<int> *fired_channels = new std::vector<int>;
    std::vector<double> *l1_fired_time = new std::vector<double>;
    std::vector<double> *l1_fired_pe = new std::vector<double>;
    T_flash->SetBranchAddress("fired_channels", &fired_channels);
    T_flash->SetBranchAddress("l1_fired_time", &l1_fired_time);
    T_flash->SetBranchAddress("l1_fired_pe", &l1_fired_pe);

    TTree *T_match = (TTree *) file->Get("T_match");
    T_match->SetBranchAddress("runNo", &temp_run_no);
    T_match->SetBranchAddress("subRunNo", &temp_subrun_no);
    T_match->SetBranchAddress("eventNo", &temp_event_no);
    Int_t tpc_cluster_id;
    Int_t event_type;
    T_match->SetBranchAddress("tpc_cluster_id", &tpc_cluster_id);  // parent cluster id
    T_match->SetBranchAddress("flash_id", &flash_id);              // flash id
    T_match->SetBranchAddress("event_type", &event_type);

    Double_t strength;
    Double_t pe_pred[32];
    Double_t pe_meas[32];
    Double_t pe_meas_err[32];
    T_match->SetBranchAddress("strength", &strength);
    T_match->SetBranchAddress("pe_pred", pe_pred);
    T_match->SetBranchAddress("pe_meas", pe_meas);
    T_match->SetBranchAddress("pe_meas_err", pe_meas_err);
    T_match->SetBranchAddress("event_type", &event_type);
    bool flag_close_to_PMT;
    bool flag_at_x_boundary;
    double ks_dis;
    double chi2;
    int ndf;
    double cluster_length;

    T_match->SetBranchAddress("ks_dis", &ks_dis);
    T_match->SetBranchAddress("chi2", &chi2);
    T_match->SetBranchAddress("ndf", &ndf);
    T_match->SetBranchAddress("cluster_length", &cluster_length);

    std::map<int, std::pair<int, double>> map_flash_info;
    std::map<int, int> map_flash_tpc_ids;
    std::map<int, int> map_tpc_flash_ids;
    std::map<std::pair<int, int>, int> map_flash_tpc_pair_type;

    for (int i = 0; i != T_flash->GetEntries(); i++) {
        T_flash->GetEntry(i);
        if (temp_run_no != run_no || temp_subrun_no != subrun_no || temp_event_no != event_no) continue;
        map_flash_info[flash_id] = std::make_pair(type, time);
    }
    for (int i = 0; i != T_match->GetEntries(); i++) {
        T_match->GetEntry(i);
        if (temp_run_no != run_no || temp_subrun_no != subrun_no || temp_event_no != event_no) continue;
        if (flash_id == -1) continue;
        map_flash_tpc_ids[flash_id] = tpc_cluster_id;
        map_tpc_flash_ids[tpc_cluster_id] = flash_id;
        map_flash_tpc_pair_type[std::make_pair(flash_id, tpc_cluster_id)] = event_type;
    }

    // load mcell
    TTree *TC = (TTree *) file->Get("TC");
    std::vector<int> *cluster_id_vec = new std::vector<int>;
    std::vector<int> *parent_cluster_id = new std::vector<int>;
    std::vector<int> *time_slice_vec = new std::vector<int>;
    std::vector<double> *q_vec = new std::vector<double>;
    std::vector<double> *uq_vec = new std::vector<double>;
    std::vector<double> *vq_vec = new std::vector<double>;
    std::vector<double> *wq_vec = new std::vector<double>;
    std::vector<double> *udq_vec = new std::vector<double>;
    std::vector<double> *vdq_vec = new std::vector<double>;
    std::vector<double> *wdq_vec = new std::vector<double>;

    std::vector<int> *nwire_u_vec = new std::vector<int>;
    std::vector<int> *nwire_v_vec = new std::vector<int>;
    std::vector<int> *nwire_w_vec = new std::vector<int>;
    std::vector<int> *flag_u_vec = new std::vector<int>;
    std::vector<int> *flag_v_vec = new std::vector<int>;
    std::vector<int> *flag_w_vec = new std::vector<int>;

    std::vector<std::vector<int>> *wire_index_u_vec = new std::vector<std::vector<int>>;
    std::vector<std::vector<int>> *wire_index_v_vec = new std::vector<std::vector<int>>;
    std::vector<std::vector<int>> *wire_index_w_vec = new std::vector<std::vector<int>>;
    std::vector<std::vector<double>> *wire_charge_u_vec = new std::vector<std::vector<double>>;
    std::vector<std::vector<double>> *wire_charge_v_vec = new std::vector<std::vector<double>>;
    std::vector<std::vector<double>> *wire_charge_w_vec = new std::vector<std::vector<double>>;
    std::vector<std::vector<double>> *wire_charge_err_u_vec = new std::vector<std::vector<double>>;
    std::vector<std::vector<double>> *wire_charge_err_v_vec = new std::vector<std::vector<double>>;
    std::vector<std::vector<double>> *wire_charge_err_w_vec = new std::vector<std::vector<double>>;

    TC->SetBranchAddress("cluster_id", &cluster_id_vec);
    TC->SetBranchAddress("parent_cluster_id", &parent_cluster_id);
    TC->SetBranchAddress("time_slice", &time_slice_vec);
    TC->SetBranchAddress("q", &q_vec);
    TC->SetBranchAddress("uq", &uq_vec);
    TC->SetBranchAddress("vq", &vq_vec);
    TC->SetBranchAddress("wq", &wq_vec);
    TC->SetBranchAddress("udq", &udq_vec);
    TC->SetBranchAddress("vdq", &vdq_vec);
    TC->SetBranchAddress("wdq", &wdq_vec);
    TC->SetBranchAddress("nwire_u", &nwire_u_vec);
    TC->SetBranchAddress("nwire_v", &nwire_v_vec);
    TC->SetBranchAddress("nwire_w", &nwire_w_vec);
    TC->SetBranchAddress("flag_u", &flag_u_vec);
    TC->SetBranchAddress("flag_v", &flag_v_vec);
    TC->SetBranchAddress("flag_w", &flag_w_vec);
    TC->SetBranchAddress("wire_index_u", &wire_index_u_vec);
    TC->SetBranchAddress("wire_index_v", &wire_index_v_vec);
    TC->SetBranchAddress("wire_index_w", &wire_index_w_vec);
    TC->SetBranchAddress("wire_charge_u", &wire_charge_u_vec);
    TC->SetBranchAddress("wire_charge_v", &wire_charge_v_vec);
    TC->SetBranchAddress("wire_charge_w", &wire_charge_w_vec);
    TC->SetBranchAddress("wire_charge_err_u", &wire_charge_err_u_vec);
    TC->SetBranchAddress("wire_charge_err_v", &wire_charge_err_v_vec);
    TC->SetBranchAddress("wire_charge_err_w", &wire_charge_err_w_vec);

    // load mcell
    TTree *TDC = (TTree *) file->Get("TDC");
    std::vector<int> *ntime_slice_vec = new std::vector<int>;
    std::vector<std::vector<int>> *time_slices_vec = new std::vector<std::vector<int>>;
    TDC->SetBranchAddress("cluster_id", &cluster_id_vec);
    TDC->SetBranchAddress("ntime_slice", &ntime_slice_vec);
    TDC->SetBranchAddress("time_slice", &time_slices_vec);

    TDC->SetBranchAddress("nwire_u", &nwire_u_vec);
    TDC->SetBranchAddress("nwire_v", &nwire_v_vec);
    TDC->SetBranchAddress("nwire_w", &nwire_w_vec);
    TDC->SetBranchAddress("flag_u", &flag_u_vec);
    TDC->SetBranchAddress("flag_v", &flag_v_vec);
    TDC->SetBranchAddress("flag_w", &flag_w_vec);
    TDC->SetBranchAddress("wire_index_u", &wire_index_u_vec);
    TDC->SetBranchAddress("wire_index_v", &wire_index_v_vec);
    TDC->SetBranchAddress("wire_index_w", &wire_index_w_vec);

    WCPPID::ToyFiducial *fid =
        new WCPPID::ToyFiducial(3, 800, -first_u_dis / pitch_u, -first_v_dis / pitch_v, -first_w_dis / pitch_w,
                                1. / time_slice_width, 1. / pitch_u, 1. / pitch_v, 1. / pitch_w,  // slope
                                angle_u, angle_v, angle_w,                                        // angle
                                3 * units::cm, 117 * units::cm, -116 * units::cm, 0 * units::cm, 1037 * units::cm,
                                0 * units::cm, 256 * units::cm, flag_data);

    // load cells ...
    GeomCellSelection mcells;
    //  CellIndexMap map_mcell_cluster_id;
    WCPPID::PR3DClusterSelection live_clusters;
    WCPPID::PR3DCluster *cluster;
    std::map<WCPPID::PR3DCluster *, int> map_cluster_parent_id;               // cluster to main cluster
    std::map<int, std::vector<WCPPID::PR3DCluster *>> map_parentid_clusters;  // main cluster to clusters

    int prev_cluster_id = -1;
    int ident = 0;
    TC->GetEntry(entry_no);
    for (int i = 0; i != cluster_id_vec->size(); i++) {
        int cluster_id = cluster_id_vec->at(i);
        SlimMergeGeomCell *mcell = new SlimMergeGeomCell(ident);
        int time_slice = time_slice_vec->at(i);
        std::vector<int> wire_index_u = wire_index_u_vec->at(i);
        std::vector<int> wire_index_v = wire_index_v_vec->at(i);
        std::vector<int> wire_index_w = wire_index_w_vec->at(i);
        std::vector<double> wire_charge_u = wire_charge_u_vec->at(i);
        std::vector<double> wire_charge_v = wire_charge_v_vec->at(i);
        std::vector<double> wire_charge_w = wire_charge_w_vec->at(i);
        std::vector<double> wire_charge_err_u = wire_charge_err_u_vec->at(i);
        std::vector<double> wire_charge_err_v = wire_charge_err_v_vec->at(i);
        std::vector<double> wire_charge_err_w = wire_charge_err_w_vec->at(i);

        mcell->SetTimeSlice(time_slice);

        mcell->set_uq(uq_vec->at(i));
        mcell->set_vq(vq_vec->at(i));
        mcell->set_wq(wq_vec->at(i));

        mcell->set_udq(udq_vec->at(i));
        mcell->set_vdq(vdq_vec->at(i));
        mcell->set_wdq(wdq_vec->at(i));

        mcell->set_q(q_vec->at(i));

        double temp_x = (time_slice * nrebin / 2. * unit_dis / 10. - frame_length / 2. * unit_dis / 10.) * units::cm;

        if (flag_u_vec->at(i) == 0) {
            mcell->add_bad_planes(WirePlaneType_t(0));
            for (int j = 0; j != nwire_u_vec->at(i); j++) {
                if (dead_u_index.find(wire_index_u.at(j)) == dead_u_index.end()) {
                    dead_u_index[wire_index_u.at(j)] =
                        std::make_pair(temp_x - 0.1 * units::cm, temp_x + 0.1 * units::cm);
                }
                else {
                    if (temp_x - 0.1 * units::cm < dead_u_index[wire_index_u.at(j)].first) {
                        dead_u_index[wire_index_u.at(j)].first = temp_x - 0.1 * units::cm;
                    }
                    else if (temp_x + 0.1 * units::cm > dead_u_index[wire_index_u.at(j)].second) {
                        dead_u_index[wire_index_u.at(j)].second = temp_x + 0.1 * units::cm;
                    }
                }
            }
        }
        if (flag_v_vec->at(i) == 0) {
            mcell->add_bad_planes(WirePlaneType_t(1));
            for (int j = 0; j != nwire_v_vec->at(i); j++) {
                if (dead_v_index.find(wire_index_v.at(j)) == dead_v_index.end()) {
                    dead_v_index[wire_index_v.at(j)] =
                        std::make_pair(temp_x - 0.1 * units::cm, temp_x + 0.1 * units::cm);
                }
                else {
                    if (temp_x - 0.1 * units::cm < dead_v_index[wire_index_v.at(j)].first) {
                        dead_v_index[wire_index_v.at(j)].first = temp_x - 0.1 * units::cm;
                    }
                    else if (temp_x + 0.1 * units::cm > dead_v_index[wire_index_v.at(j)].second) {
                        dead_v_index[wire_index_v.at(j)].second = temp_x + 0.1 * units::cm;
                    }
                }
            }
        }
        if (flag_w_vec->at(i) == 0) {
            mcell->add_bad_planes(WirePlaneType_t(2));
            for (int j = 0; j != nwire_w_vec->at(i); j++) {
                if (dead_w_index.find(wire_index_w.at(j)) == dead_w_index.end()) {
                    dead_w_index[wire_index_w.at(j)] =
                        std::make_pair(temp_x - 0.1 * units::cm, temp_x + 0.1 * units::cm);
                }
                else {
                    if (temp_x - 0.1 * units::cm < dead_w_index[wire_index_w.at(j)].first) {
                        dead_w_index[wire_index_w.at(j)].first = temp_x - 0.1 * units::cm;
                    }
                    else if (temp_x + 0.1 * units::cm > dead_w_index[wire_index_w.at(j)].second) {
                        dead_w_index[wire_index_w.at(j)].second = temp_x + 0.1 * units::cm;
                    }
                }
            }
        }
        for (int j = 0; j != nwire_u_vec->at(i); j++) {
            const GeomWire *wire = gds.by_planeindex(WirePlaneType_t(0), wire_index_u.at(j));
            mcell->AddWire(wire, WirePlaneType_t(0), wire_charge_u.at(j), wire_charge_err_u.at(j));
        }
        for (int j = 0; j != nwire_v_vec->at(i); j++) {
            const GeomWire *wire = gds.by_planeindex(WirePlaneType_t(1), wire_index_v.at(j));
            mcell->AddWire(wire, WirePlaneType_t(1), wire_charge_v.at(j), wire_charge_err_v.at(j));
        }
        for (int j = 0; j != nwire_w_vec->at(i); j++) {
            const GeomWire *wire = gds.by_planeindex(WirePlaneType_t(2), wire_index_w.at(j));
            mcell->AddWire(wire, WirePlaneType_t(2), wire_charge_w.at(j), wire_charge_err_w.at(j));
        }
        mcells.push_back(mcell);

        if (cluster_id != prev_cluster_id) {
            cluster = new WCPPID::PR3DCluster(cluster_id);
            map_cluster_parent_id[cluster] = parent_cluster_id->at(i);
            live_clusters.push_back(cluster);
            if (map_parentid_clusters.find(parent_cluster_id->at(i)) == map_parentid_clusters.end()) {
                std::vector<WCPPID::PR3DCluster *> temp_clusters;
                temp_clusters.push_back(cluster);
                map_parentid_clusters[parent_cluster_id->at(i)] = temp_clusters;
            }
            else {
                map_parentid_clusters[parent_cluster_id->at(i)].push_back(cluster);
            }
        }
        cluster->AddCell(mcell, time_slice);

        prev_cluster_id = cluster_id;
        ident++;
    }

    prev_cluster_id = -1;
    cluster_id_vec->clear();
    wire_index_u_vec->clear();
    wire_index_v_vec->clear();
    wire_index_w_vec->clear();
    nwire_u_vec->clear();
    nwire_v_vec->clear();
    nwire_w_vec->clear();
    flag_u_vec->clear();
    flag_v_vec->clear();
    flag_w_vec->clear();

    TDC->GetEntry(entry_no);
    for (int i = 0; i != cluster_id_vec->size(); i++) {
        int cluster_id = cluster_id_vec->at(i);
        SlimMergeGeomCell *mcell = new SlimMergeGeomCell(ident);
        std::vector<int> time_slices = time_slices_vec->at(i);
        std::vector<int> wire_index_u = wire_index_u_vec->at(i);
        std::vector<int> wire_index_v = wire_index_v_vec->at(i);
        std::vector<int> wire_index_w = wire_index_w_vec->at(i);

        mcell->SetTimeSlice(time_slices.at(0));

        double temp_x1 =
            (time_slices.front() * nrebin / 2. * unit_dis / 10. - frame_length / 2. * unit_dis / 10.) * units::cm;
        double temp_x2 =
            (time_slices.back() * nrebin / 2. * unit_dis / 10. - frame_length / 2. * unit_dis / 10.) * units::cm;

        if (flag_u_vec->at(i) == 0) {
            mcell->add_bad_planes(WirePlaneType_t(0));
            for (int j = 0; j != nwire_u_vec->at(i); j++) {
                if (dead_u_index.find(wire_index_u.at(j)) == dead_u_index.end()) {
                    dead_u_index[wire_index_u.at(j)] =
                        std::make_pair(temp_x1 - 0.1 * units::cm, temp_x2 + 0.1 * units::cm);
                }
                else {
                    if (temp_x1 - 0.1 * units::cm < dead_u_index[wire_index_u.at(j)].first) {
                        dead_u_index[wire_index_u.at(j)].first = temp_x1 - 0.1 * units::cm;
                    }
                    else if (temp_x2 + 0.1 * units::cm > dead_u_index[wire_index_u.at(j)].second) {
                        dead_u_index[wire_index_u.at(j)].second = temp_x2 + 0.1 * units::cm;
                    }
                }
            }
        }
        if (flag_v_vec->at(i) == 0) {
            mcell->add_bad_planes(WirePlaneType_t(1));
            for (int j = 0; j != nwire_v_vec->at(i); j++) {
                if (dead_v_index.find(wire_index_v.at(j)) == dead_v_index.end()) {
                    dead_v_index[wire_index_v.at(j)] =
                        std::make_pair(temp_x1 - 0.1 * units::cm, temp_x2 + 0.1 * units::cm);
                }
                else {
                    if (temp_x1 - 0.1 * units::cm < dead_v_index[wire_index_v.at(j)].first) {
                        dead_v_index[wire_index_v.at(j)].first = temp_x1 - 0.1 * units::cm;
                    }
                    else if (temp_x2 + 0.1 * units::cm > dead_v_index[wire_index_v.at(j)].second) {
                        dead_v_index[wire_index_v.at(j)].second = temp_x2 + 0.1 * units::cm;
                    }
                }
            }
        }
        if (flag_w_vec->at(i) == 0) {
            mcell->add_bad_planes(WirePlaneType_t(2));
            for (int j = 0; j != nwire_w_vec->at(i); j++) {
                if (dead_w_index.find(wire_index_w.at(j)) == dead_w_index.end()) {
                    dead_w_index[wire_index_w.at(j)] =
                        std::make_pair(temp_x1 - 0.1 * units::cm, temp_x2 + 0.1 * units::cm);
                }
                else {
                    if (temp_x1 - 0.1 * units::cm < dead_w_index[wire_index_w.at(j)].first) {
                        dead_w_index[wire_index_w.at(j)].first = temp_x1 - 0.1 * units::cm;
                    }
                    else if (temp_x2 + 0.1 * units::cm > dead_w_index[wire_index_w.at(j)].second) {
                        dead_w_index[wire_index_w.at(j)].second = temp_x2 + 0.1 * units::cm;
                    }
                }
            }
        }
        for (int j = 0; j != nwire_u_vec->at(i); j++) {
            const GeomWire *wire = gds.by_planeindex(WirePlaneType_t(0), wire_index_u.at(j));
            mcell->AddWire(wire, WirePlaneType_t(0));
        }
        for (int j = 0; j != nwire_v_vec->at(i); j++) {
            const GeomWire *wire = gds.by_planeindex(WirePlaneType_t(1), wire_index_v.at(j));
            mcell->AddWire(wire, WirePlaneType_t(1));
        }
        for (int j = 0; j != nwire_w_vec->at(i); j++) {
            const GeomWire *wire = gds.by_planeindex(WirePlaneType_t(2), wire_index_w.at(j));
            mcell->AddWire(wire, WirePlaneType_t(2));
        }

        // figure out the boundary here ...
        PointVector pcell;
        if (flag_u_vec->at(i) == 0 && flag_v_vec->at(i) == 0) {
            const GeomWire *uwire_1 = gds.by_planeindex(WirePlaneType_t(0), wire_index_u.front());
            const GeomWire *uwire_2 = gds.by_planeindex(WirePlaneType_t(0), wire_index_u.back());
            float dis_u[3];
            float u_pitch = gds.pitch(kUwire);
            dis_u[0] = gds.wire_dist(*uwire_1) - u_pitch / 2.;
            dis_u[1] = gds.wire_dist(*uwire_2) + u_pitch / 2.;

            const GeomWire *vwire_1 = gds.by_planeindex(WirePlaneType_t(1), wire_index_v.front());
            const GeomWire *vwire_2 = gds.by_planeindex(WirePlaneType_t(1), wire_index_v.back());
            float dis_v[3];
            float v_pitch = gds.pitch(kVwire);
            dis_v[0] = gds.wire_dist(*vwire_1) - v_pitch / 2.;
            dis_v[1] = gds.wire_dist(*vwire_2) + v_pitch / 2.;

            std::vector<Vector> pcross(4);
            bool flag1 = gds.crossing_point(dis_u[0], dis_v[0], kUwire, kVwire, pcross[0]);  // check the inner point

            if (flag1) {
                // fill the outer point
                pcell.push_back(pcross[0]);
            }
            else {
                // scan u-wire
                for (int k = 0; k != wire_index_u.size(); k++) {
                    const GeomWire *uwire_3 = gds.by_planeindex(WirePlaneType_t(0), wire_index_u.at(k));
                    dis_u[2] = gds.wire_dist(*uwire_3) - u_pitch / 2.;

                    if (gds.crossing_point(dis_u[2], dis_v[0], kUwire, kVwire, pcross[0])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[0].y, 2) + pow(pcell.at(k1).z - pcross[0].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }

                        if (flag_qx == 1) pcell.push_back(pcross[0]);
                        break;
                    }
                }
                // scan v-wire
                for (int k = 0; k != wire_index_v.size(); k++) {
                    const GeomWire *vwire_3 = gds.by_planeindex(WirePlaneType_t(1), wire_index_v.at(k));
                    dis_v[2] = gds.wire_dist(*vwire_3) - v_pitch / 2.;
                    if (gds.crossing_point(dis_u[0], dis_v[2], kUwire, kVwire, pcross[0])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[0].y, 2) + pow(pcell.at(k1).z - pcross[0].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[0]);
                        break;
                    }
                }
            }

            bool flag2 = gds.crossing_point(dis_u[0], dis_v[1], kUwire, kVwire, pcross[1]);
            if (flag2) {
                pcell.push_back(pcross[1]);
            }
            else {
                // scan u-wire
                for (int k = 0; k != wire_index_u.size(); k++) {
                    const GeomWire *uwire_3 = gds.by_planeindex(WirePlaneType_t(0), wire_index_u.at(k));
                    dis_u[2] = gds.wire_dist(*uwire_3) - u_pitch / 2.;
                    if (gds.crossing_point(dis_u[2], dis_v[1], kUwire, kVwire, pcross[1])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[1].y, 2) + pow(pcell.at(k1).z - pcross[1].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[1]);
                        break;
                    }
                }
                // scan v-wire
                for (int k = 0; k != wire_index_v.size(); k++) {
                    const GeomWire *vwire_3 =
                        gds.by_planeindex(WirePlaneType_t(1), wire_index_v.at(wire_index_v.size() - 1 - k));
                    dis_v[2] = gds.wire_dist(*vwire_3) + v_pitch / 2.;
                    if (gds.crossing_point(dis_u[0], dis_v[2], kUwire, kVwire, pcross[1])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[1].y, 2) + pow(pcell.at(k1).z - pcross[1].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[1]);
                        break;
                    }
                }
            }

            bool flag3 = gds.crossing_point(dis_u[1], dis_v[0], kUwire, kVwire, pcross[2]);
            if (flag3) {
                pcell.push_back(pcross[2]);
            }
            else {
                for (int k = 0; k != wire_index_u.size(); k++) {
                    const GeomWire *uwire_3 =
                        gds.by_planeindex(WirePlaneType_t(0), wire_index_u.at(wire_index_u.size() - 1 - k));
                    dis_u[2] = gds.wire_dist(*uwire_3) + u_pitch / 2.;
                    if (gds.crossing_point(dis_u[2], dis_v[0], kUwire, kVwire, pcross[2])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[2].y, 2) + pow(pcell.at(k1).z - pcross[2].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[2]);
                        break;
                    }
                }
                // scan v-wire
                for (int k = 0; k != wire_index_v.size(); k++) {
                    const GeomWire *vwire_3 = gds.by_planeindex(WirePlaneType_t(1), wire_index_v.at(k));
                    dis_v[2] = gds.wire_dist(*vwire_3) - v_pitch / 2.;
                    if (gds.crossing_point(dis_u[1], dis_v[2], kUwire, kVwire, pcross[2])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[2].y, 2) + pow(pcell.at(k1).z - pcross[2].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[2]);
                        break;
                    }
                }
            }

            bool flag4 = gds.crossing_point(dis_u[1], dis_v[1], kUwire, kVwire, pcross[3]);
            if (flag4) {
                pcell.push_back(pcross[3]);
            }
            else {
                // scan u-wire
                for (int k = 0; k != wire_index_u.size(); k++) {
                    const GeomWire *uwire_3 =
                        gds.by_planeindex(WirePlaneType_t(0), wire_index_u.at(wire_index_u.size() - 1 - k));
                    dis_u[2] = gds.wire_dist(*uwire_3) + u_pitch / 2.;
                    if (gds.crossing_point(dis_u[2], dis_v[1], kUwire, kVwire, pcross[3])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[3].y, 2) + pow(pcell.at(k1).z - pcross[3].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[3]);
                        break;
                    }
                }
                // scan v-wire
                for (int k = 0; k != wire_index_v.size(); k++) {
                    const GeomWire *vwire_3 =
                        gds.by_planeindex(WirePlaneType_t(1), wire_index_v.at(wire_index_v.size() - 1 - k));
                    dis_v[2] = gds.wire_dist(*vwire_3) + v_pitch / 2.;
                    if (gds.crossing_point(dis_u[1], dis_v[2], kUwire, kVwire, pcross[3])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[3].y, 2) + pow(pcell.at(k1).z - pcross[3].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[3]);
                        break;
                    }
                }
            }

            mcell->AddBoundary(pcell);
        }
        else if (flag_u_vec->at(i) == 0 && flag_w_vec->at(i) == 0) {
            const GeomWire *uwire_1 = gds.by_planeindex(WirePlaneType_t(0), wire_index_u.front());
            const GeomWire *uwire_2 = gds.by_planeindex(WirePlaneType_t(0), wire_index_u.back());
            float dis_u[2];
            float u_pitch = gds.pitch(kUwire);
            dis_u[0] = gds.wire_dist(*uwire_1) - u_pitch / 2.;
            dis_u[1] = gds.wire_dist(*uwire_2) + u_pitch / 2.;

            const GeomWire *wwire_1 = gds.by_planeindex(WirePlaneType_t(2), wire_index_w.front());
            const GeomWire *wwire_2 = gds.by_planeindex(WirePlaneType_t(2), wire_index_w.back());
            float dis_w[2];
            float w_pitch = gds.pitch(kYwire);
            dis_w[0] = gds.wire_dist(*wwire_1) - w_pitch / 2.;
            dis_w[1] = gds.wire_dist(*wwire_2) + w_pitch / 2.;

            std::vector<Vector> pcross(4);

            bool flag1 = gds.crossing_point(dis_u[0], dis_w[0], kUwire, kYwire, pcross[0]);
            if (flag1) {
                pcell.push_back(pcross[0]);
            }
            else {
                // scan u-wire
                for (int k = 0; k != wire_index_u.size(); k++) {
                    const GeomWire *uwire_3 = gds.by_planeindex(WirePlaneType_t(0), wire_index_u.at(k));
                    dis_u[2] = gds.wire_dist(*uwire_3) - u_pitch / 2.;
                    if (gds.crossing_point(dis_u[2], dis_w[0], kUwire, kYwire, pcross[0])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[0].y, 2) + pow(pcell.at(k1).z - pcross[0].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }

                        if (flag_qx == 1) pcell.push_back(pcross[0]);
                        break;
                    }
                }
                // scan w-wire
                for (int k = 0; k != wire_index_w.size(); k++) {
                    const GeomWire *wwire_3 = gds.by_planeindex(WirePlaneType_t(2), wire_index_w.at(k));
                    dis_w[2] = gds.wire_dist(*wwire_3) - w_pitch / 2.;
                    if (gds.crossing_point(dis_u[0], dis_w[2], kUwire, kYwire, pcross[0])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[0].y, 2) + pow(pcell.at(k1).z - pcross[0].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[0]);
                        break;
                    }
                }
            }

            bool flag2 = gds.crossing_point(dis_u[0], dis_w[1], kUwire, kYwire, pcross[1]);
            if (flag2) {
                pcell.push_back(pcross[1]);
            }
            else {
                // scan u-wire
                for (int k = 0; k != wire_index_u.size(); k++) {
                    const GeomWire *uwire_3 = gds.by_planeindex(WirePlaneType_t(0), wire_index_u.at(k));
                    dis_u[2] = gds.wire_dist(*uwire_3) - u_pitch / 2.;
                    if (gds.crossing_point(dis_u[2], dis_w[1], kUwire, kYwire, pcross[1])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[1].y, 2) + pow(pcell.at(k1).z - pcross[1].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[1]);
                        break;
                    }
                }
                // scan w-wire
                for (int k = 0; k != wire_index_w.size(); k++) {
                    const GeomWire *wwire_3 =
                        gds.by_planeindex(WirePlaneType_t(2), wire_index_w.at(wire_index_w.size() - 1 - k));
                    dis_w[2] = gds.wire_dist(*wwire_3) + w_pitch / 2.;
                    if (gds.crossing_point(dis_u[0], dis_w[2], kUwire, kYwire, pcross[1])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[1].y, 2) + pow(pcell.at(k1).z - pcross[1].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[1]);
                        break;
                    }
                }
            }

            bool flag3 = gds.crossing_point(dis_u[1], dis_w[0], kUwire, kYwire, pcross[2]);
            if (flag3) {
                pcell.push_back(pcross[2]);
            }
            else {
                for (int k = 0; k != wire_index_u.size(); k++) {
                    const GeomWire *uwire_3 =
                        gds.by_planeindex(WirePlaneType_t(0), wire_index_u.at(wire_index_u.size() - 1 - k));
                    dis_u[2] = gds.wire_dist(*uwire_3) + u_pitch / 2.;
                    if (gds.crossing_point(dis_u[2], dis_w[0], kUwire, kYwire, pcross[2])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[2].y, 2) + pow(pcell.at(k1).z - pcross[2].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[2]);
                        break;
                    }
                }
                // scan w-wire
                for (int k = 0; k != wire_index_w.size(); k++) {
                    const GeomWire *wwire_3 = gds.by_planeindex(WirePlaneType_t(2), wire_index_w.at(k));
                    dis_w[2] = gds.wire_dist(*wwire_3) - w_pitch / 2.;
                    if (gds.crossing_point(dis_u[1], dis_w[2], kUwire, kYwire, pcross[2])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[2].y, 2) + pow(pcell.at(k1).z - pcross[2].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[2]);
                        break;
                    }
                }
            }

            bool flag4 = gds.crossing_point(dis_u[1], dis_w[1], kUwire, kYwire, pcross[3]);
            if (flag4) {
                pcell.push_back(pcross[3]);
            }
            else {
                // scan u-wire
                for (int k = 0; k != wire_index_u.size(); k++) {
                    const GeomWire *uwire_3 =
                        gds.by_planeindex(WirePlaneType_t(0), wire_index_u.at(wire_index_u.size() - 1 - k));
                    dis_u[2] = gds.wire_dist(*uwire_3) + u_pitch / 2.;
                    if (gds.crossing_point(dis_u[2], dis_w[1], kUwire, kYwire, pcross[3])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[3].y, 2) + pow(pcell.at(k1).z - pcross[3].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[3]);
                        break;
                    }
                }
                // scan w-wire
                for (int k = 0; k != wire_index_w.size(); k++) {
                    const GeomWire *wwire_3 =
                        gds.by_planeindex(WirePlaneType_t(2), wire_index_w.at(wire_index_w.size() - 1 - k));
                    dis_w[2] = gds.wire_dist(*wwire_3) + w_pitch / 2.;
                    if (gds.crossing_point(dis_u[1], dis_w[2], kUwire, kYwire, pcross[3])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[3].y, 2) + pow(pcell.at(k1).z - pcross[3].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[3]);
                        break;
                    }
                }
            }
            mcell->AddBoundary(pcell);
        }
        else if (flag_v_vec->at(i) == 0 && flag_w_vec->at(i) == 0) {
            const GeomWire *wwire_1 = gds.by_planeindex(WirePlaneType_t(2), wire_index_w.front());
            const GeomWire *wwire_2 = gds.by_planeindex(WirePlaneType_t(2), wire_index_w.back());
            float dis_w[2];
            float w_pitch = gds.pitch(kYwire);
            dis_w[0] = gds.wire_dist(*wwire_1) - w_pitch / 2.;
            dis_w[1] = gds.wire_dist(*wwire_2) + w_pitch / 2.;

            const GeomWire *vwire_1 = gds.by_planeindex(WirePlaneType_t(1), wire_index_v.front());
            const GeomWire *vwire_2 = gds.by_planeindex(WirePlaneType_t(1), wire_index_v.back());
            float dis_v[2];
            float v_pitch = gds.pitch(kVwire);
            dis_v[0] = gds.wire_dist(*vwire_1) - v_pitch / 2.;
            dis_v[1] = gds.wire_dist(*vwire_2) + v_pitch / 2.;

            std::vector<Vector> pcross(4);

            bool flag1 = gds.crossing_point(dis_w[0], dis_v[0], kYwire, kVwire, pcross[0]);
            if (flag1) {
                pcell.push_back(pcross[0]);
            }
            else {
                // scan u-wire
                for (int k = 0; k != wire_index_w.size(); k++) {
                    const GeomWire *wwire_3 = gds.by_planeindex(WirePlaneType_t(2), wire_index_w.at(k));
                    dis_w[2] = gds.wire_dist(*wwire_3) - w_pitch / 2.;
                    if (gds.crossing_point(dis_w[2], dis_v[0], kYwire, kVwire, pcross[0])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[0].y, 2) + pow(pcell.at(k1).z - pcross[0].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }

                        if (flag_qx == 1) pcell.push_back(pcross[0]);
                        break;
                    }
                }
                // scan v-wire
                for (int k = 0; k != wire_index_v.size(); k++) {
                    const GeomWire *vwire_3 = gds.by_planeindex(WirePlaneType_t(1), wire_index_v.at(k));
                    dis_v[2] = gds.wire_dist(*vwire_3) - v_pitch / 2.;
                    if (gds.crossing_point(dis_w[0], dis_v[2], kYwire, kVwire, pcross[0])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[0].y, 2) + pow(pcell.at(k1).z - pcross[0].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[0]);
                        break;
                    }
                }
            }

            bool flag2 = gds.crossing_point(dis_w[0], dis_v[1], kYwire, kVwire, pcross[1]);
            if (flag2) {
                pcell.push_back(pcross[1]);
            }
            else {
                // scan u-wire
                for (int k = 0; k != wire_index_w.size(); k++) {
                    const GeomWire *wwire_3 = gds.by_planeindex(WirePlaneType_t(2), wire_index_w.at(k));
                    dis_w[2] = gds.wire_dist(*wwire_3) - w_pitch / 2.;
                    if (gds.crossing_point(dis_w[2], dis_v[1], kYwire, kVwire, pcross[1])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[1].y, 2) + pow(pcell.at(k1).z - pcross[1].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[1]);
                        break;
                    }
                }
                // scan v-wire
                for (int k = 0; k != wire_index_v.size(); k++) {
                    const GeomWire *vwire_3 =
                        gds.by_planeindex(WirePlaneType_t(1), wire_index_v.at(wire_index_v.size() - 1 - k));
                    dis_v[2] = gds.wire_dist(*vwire_3) + v_pitch / 2.;
                    if (gds.crossing_point(dis_w[0], dis_v[2], kYwire, kVwire, pcross[1])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[1].y, 2) + pow(pcell.at(k1).z - pcross[1].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[1]);
                        break;
                    }
                }
            }

            bool flag3 = gds.crossing_point(dis_w[1], dis_v[0], kYwire, kVwire, pcross[2]);
            if (flag3) {
                pcell.push_back(pcross[2]);
            }
            else {
                for (int k = 0; k != wire_index_w.size(); k++) {
                    const GeomWire *wwire_3 =
                        gds.by_planeindex(WirePlaneType_t(2), wire_index_w.at(wire_index_w.size() - 1 - k));
                    dis_w[2] = gds.wire_dist(*wwire_3) + w_pitch / 2.;
                    if (gds.crossing_point(dis_w[2], dis_v[0], kYwire, kVwire, pcross[2])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[2].y, 2) + pow(pcell.at(k1).z - pcross[2].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[2]);
                        break;
                    }
                }
                // scan v-wire
                for (int k = 0; k != wire_index_v.size(); k++) {
                    const GeomWire *vwire_3 = gds.by_planeindex(WirePlaneType_t(1), wire_index_v.at(k));
                    dis_v[2] = gds.wire_dist(*vwire_3) - v_pitch / 2.;
                    if (gds.crossing_point(dis_w[1], dis_v[2], kYwire, kVwire, pcross[2])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[2].y, 2) + pow(pcell.at(k1).z - pcross[2].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[2]);
                        break;
                    }
                }
            }

            bool flag4 = gds.crossing_point(dis_w[1], dis_v[1], kYwire, kVwire, pcross[3]);
            if (flag4) {
                pcell.push_back(pcross[3]);
            }
            else {
                // scan u-wire
                for (int k = 0; k != wire_index_w.size(); k++) {
                    const GeomWire *wwire_3 =
                        gds.by_planeindex(WirePlaneType_t(2), wire_index_w.at(wire_index_w.size() - 1 - k));
                    dis_w[2] = gds.wire_dist(*wwire_3) + w_pitch / 2.;
                    if (gds.crossing_point(dis_w[2], dis_v[1], kYwire, kVwire, pcross[3])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[3].y, 2) + pow(pcell.at(k1).z - pcross[3].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[3]);
                        break;
                    }
                }
                // scan v-wire
                for (int k = 0; k != wire_index_v.size(); k++) {
                    const GeomWire *vwire_3 =
                        gds.by_planeindex(WirePlaneType_t(1), wire_index_v.at(wire_index_v.size() - 1 - k));
                    dis_v[2] = gds.wire_dist(*vwire_3) + v_pitch / 2.;
                    if (gds.crossing_point(dis_w[1], dis_v[2], kYwire, kVwire, pcross[3])) {
                        int flag_qx = 1;
                        for (int k1 = 0; k1 != pcell.size(); k1++) {
                            if (sqrt(pow(pcell.at(k1).y - pcross[3].y, 2) + pow(pcell.at(k1).z - pcross[3].z, 2)) <
                                0.5 * units::mm) {
                                flag_qx = 0;
                                break;
                            }
                        }
                        if (flag_qx == 1) pcell.push_back(pcross[3]);
                        break;
                    }
                }
            }
            mcell->AddBoundary(pcell);
        }
        // figure out the boundary ...

        fid->AddDeadRegion(mcell, time_slices);
        ident++;
    }

    // Load T_ch_bad tree ...
    TTree *T_bad_ch = (TTree *) file->Get("T_bad_ch");
    if (T_bad_ch != 0) {
        Int_t chid, plane;
        Int_t start_time, end_time;
        T_bad_ch->SetBranchAddress("chid", &chid);
        T_bad_ch->SetBranchAddress("plane", &plane);
        T_bad_ch->SetBranchAddress("start_time", &start_time);
        T_bad_ch->SetBranchAddress("end_time", &end_time);
        Int_t temp_run_no, temp_subrun_no, temp_event_no;
        T_bad_ch->SetBranchAddress("runNo", &temp_run_no);
        T_bad_ch->SetBranchAddress("subRunNo", &temp_subrun_no);
        T_bad_ch->SetBranchAddress("eventNo", &temp_event_no);

        for (int i = 0; i != T_bad_ch->GetEntries(); i++) {
            T_bad_ch->GetEntry(i);
            if (temp_run_no != run_no || temp_subrun_no != subrun_no || temp_event_no != event_no) continue;

            double temp_x1 = (start_time / 2. * unit_dis / 10. - frame_length / 2. * unit_dis / 10.) * units::cm;
            double temp_x2 = (end_time / 2. * unit_dis / 10. - frame_length / 2. * unit_dis / 10.) * units::cm;
            if (plane == 1) {
                chid -= 2400;
            }
            else if (plane == 2) {
                chid -= 4800;
            }
            if (plane == 0) {
                if (dead_u_index.find(chid) == dead_u_index.end()) {
                    dead_u_index[chid] = std::make_pair(temp_x1 - 0.1 * units::cm, temp_x2 + 0.1 * units::cm);
                }
                else {
                    if (temp_x1 - 0.1 * units::cm < dead_u_index[chid].first) {
                        dead_u_index[chid].first = temp_x1 - 0.1 * units::cm;
                    }
                    else if (temp_x2 + 0.1 * units::cm > dead_u_index[chid].second) {
                        dead_u_index[chid].second = temp_x2 + 0.1 * units::cm;
                    }
                }
            }
            else if (plane == 1) {
                if (dead_v_index.find(chid) == dead_v_index.end()) {
                    dead_v_index[chid] = std::make_pair(temp_x1 - 0.1 * units::cm, temp_x2 + 0.1 * units::cm);
                }
                else {
                    if (temp_x1 - 0.1 * units::cm < dead_v_index[chid].first) {
                        dead_v_index[chid].first = temp_x1 - 0.1 * units::cm;
                    }
                    else if (temp_x2 + 0.1 * units::cm > dead_v_index[chid].second) {
                        dead_v_index[chid].second = temp_x2 + 0.1 * units::cm;
                    }
                }
            }
            else if (plane == 2) {
                if (dead_w_index.find(chid) == dead_w_index.end()) {
                    dead_w_index[chid] = std::make_pair(temp_x1 - 0.1 * units::cm, temp_x2 + 0.1 * units::cm);
                }
                else {
                    if (temp_x1 - 0.1 * units::cm < dead_w_index[chid].first) {
                        dead_w_index[chid].first = temp_x1 - 0.1 * units::cm;
                    }
                    else if (temp_x2 + 0.1 * units::cm > dead_w_index[chid].second) {
                        dead_w_index[chid].second = temp_x2 + 0.1 * units::cm;
                    }
                }
            }
        }
    }
    cout << em("load clusters from file") << endl;

    // form a global map with the current map information
    std::map<int, std::map<const GeomWire *, SMGCSelection>> global_wc_map;
    for (size_t i = 0; i != live_clusters.size(); i++) {
        WCPPID::PR3DCluster *cluster = live_clusters.at(i);
        SMGCSelection &mcells = cluster->get_mcells();
        for (auto it = mcells.begin(); it != mcells.end(); it++) {
            SlimMergeGeomCell *mcell = *it;
            int time_slice = mcell->GetTimeSlice();
            if (global_wc_map.find(time_slice) == global_wc_map.end()) {
                std::map<const GeomWire *, SMGCSelection> temp_wc_map;
                global_wc_map[time_slice] = temp_wc_map;
            }
            std::map<const GeomWire *, SMGCSelection> &timeslice_wc_map = global_wc_map[time_slice];

            GeomWireSelection &uwires = mcell->get_uwires();
            GeomWireSelection &vwires = mcell->get_vwires();
            GeomWireSelection &wwires = mcell->get_wwires();
            std::vector<WirePlaneType_t> bad_planes = mcell->get_bad_planes();
            if (find(bad_planes.begin(), bad_planes.end(), WirePlaneType_t(0)) == bad_planes.end()) {
                for (int j = 0; j != uwires.size(); j++) {
                    const GeomWire *wire = uwires.at(j);
                    if (timeslice_wc_map.find(wire) == timeslice_wc_map.end()) {
                        SMGCSelection temp_mcells;
                        temp_mcells.push_back(mcell);
                        timeslice_wc_map[wire] = temp_mcells;
                    }
                    else {
                        timeslice_wc_map[wire].push_back(mcell);
                    }
                }
            }
            if (find(bad_planes.begin(), bad_planes.end(), WirePlaneType_t(1)) == bad_planes.end()) {
                for (int j = 0; j != vwires.size(); j++) {
                    const GeomWire *wire = vwires.at(j);
                    if (timeslice_wc_map.find(wire) == timeslice_wc_map.end()) {
                        SMGCSelection temp_mcells;
                        temp_mcells.push_back(mcell);
                        timeslice_wc_map[wire] = temp_mcells;
                    }
                    else {
                        timeslice_wc_map[wire].push_back(mcell);
                    }
                }
            }
            if (find(bad_planes.begin(), bad_planes.end(), WirePlaneType_t(2)) == bad_planes.end()) {
                for (int j = 0; j != wwires.size(); j++) {
                    const GeomWire *wire = wwires.at(j);
                    if (timeslice_wc_map.find(wire) == timeslice_wc_map.end()) {
                        SMGCSelection temp_mcells;
                        temp_mcells.push_back(mcell);
                        timeslice_wc_map[wire] = temp_mcells;
                    }
                    else {
                        timeslice_wc_map[wire].push_back(mcell);
                    }
                }
            }
        }
    }

    // replace by the new sampling points ...
    for (size_t i = 0; i != live_clusters.size(); i++) {
        WCPPID::calc_sampling_points(gds, live_clusters.at(i), nrebin, frame_length, unit_dis);
        live_clusters.at(i)->Create_point_cloud();
    }
    cout << em("Add X, Y, Z points") << std::endl;

    TFile *file1 = new TFile(Form("bee_%d_%d_%d.root", run_no, subrun_no, event_no), "RECREATE");
    TTree *Trun1 = new TTree("Trun", "Trun");
    Trun1->SetDirectory(file1);
    Trun1->Branch("eventNo", &event_no, "eventNo/I");
    Trun1->Branch("runNo", &run_no, "runNo/I");
    Trun1->Branch("subRunNo", &subrun_no, "subRunNo/I");

    Trun1->Branch("triggerBits", &triggerbits, "triggerBits/i");
    Trun1->Branch("unit_dis", &unit_dis, "unit_dis/F");
    Trun1->Branch("frame_length", &frame_length, "frame_length/I");
    Trun1->Branch("eve_num", &eve_num, "eve_num/I");
    Trun1->Branch("nrebin", &nrebin, "nrebin/I");
    Trun1->Branch("time_offset", &time_offset, "time_offset/I");
    int detector = 0;  // MicroBooNE
    Trun1->Branch("detector", &detector, "detector/I");
    // Trun1->Branch("timesliceId",&timesliceId);
    // Trun1->Branch("timesliceChannel",&timesliceChannel);
    // Trun1->Branch("raw_charge",&raw_charge);
    // Trun1->Branch("raw_charge_err",&raw_charge_err);
    Trun1->Fill();

    TTree *T_flash1 = new TTree("T_flash", "T_flash");
    T_flash1->SetDirectory(file1);
    T_flash1->Branch("type", &type);
    T_flash1->Branch("flash_id", &flash_id);
    T_flash1->Branch("low_time", &low_time);
    T_flash1->Branch("high_time", &high_time);
    T_flash1->Branch("time", &time);
    T_flash1->Branch("total_PE", &total_PE);
    T_flash1->Branch("PE", PE, "PE[32]/D");
    T_flash1->Branch("PE_err", PE_err, "PE_err[32]/D");
    T_flash1->Branch("fired_channels", &fired_channels);
    T_flash1->Branch("l1_fired_time", &l1_fired_time);
    T_flash1->Branch("l1_fired_pe", &l1_fired_pe);
    for (int i = 0; i != T_flash->GetEntries(); i++) {
        T_flash->GetEntry(i);
        if (temp_run_no != run_no || temp_subrun_no != subrun_no || temp_event_no != event_no) continue;
        T_flash1->Fill();
    }

    TTree *T_match1 = new TTree("T_match", "T_match");
    T_match1->SetDirectory(file1);
    T_match1->Branch("tpc_cluster_id", &tpc_cluster_id, "tpc_cluster_id/I");  // parent cluster id
    T_match1->Branch("flash_id", &flash_id, "flash_id/I");                    // flash id

    T_match1->Branch("strength", &strength, "strength/D");
    T_match1->Branch("pe_pred", pe_pred, "pe_pred[32]/D");
    T_match1->Branch("pe_meas", pe_meas, "pe_meas[32]/D");
    T_match1->Branch("pe_meas_err", pe_meas_err, "pe_meas_err[32]/D");
    T_match1->Branch("event_type", &event_type, "event_type/I");
    T_match1->Branch("ks_dis", &ks_dis, "ks_dis/D");
    T_match1->Branch("chi2", &chi2, "chi2/D");
    T_match1->Branch("ndf", &ndf, "ndf/I");
    T_match1->Branch("cluster_length", &cluster_length, "cluster_length/D");

    for (int i = 0; i != T_match->GetEntries(); i++) {
        T_match->GetEntry(i);
        if (temp_run_no != run_no || temp_subrun_no != subrun_no || temp_event_no != event_no) continue;
        T_match1->Fill();
    }

    TTree *T_cluster;
    Double_t x, y, z, q, nq;
    Int_t ncluster;
    Int_t temp_time_slice, ch_u, ch_v, ch_w;
    T_cluster = new TTree("T_cluster", "T_cluster");
    T_cluster->Branch("cluster_id", &ncluster, "cluster_id/I");
    T_cluster->Branch("x", &x, "x/D");
    T_cluster->Branch("y", &y, "y/D");
    T_cluster->Branch("z", &z, "z/D");
    T_cluster->Branch("q", &q, "q/D");
    T_cluster->Branch("nq", &nq, "nq/D");
    T_cluster->Branch("time_slice", &temp_time_slice, "time_slice/I");
    T_cluster->Branch("ch_u", &ch_u, "ch_u/I");
    T_cluster->Branch("ch_v", &ch_v, "ch_v/I");
    T_cluster->Branch("ch_w", &ch_w, "ch_w/I");
    T_cluster->SetDirectory(file1);

    for (auto it = live_clusters.begin(); it != live_clusters.end(); it++) {
        WCPPID::PR3DCluster *new_cluster = *it;
        ncluster = map_cluster_parent_id[new_cluster];
        ToyPointCloud *pcloud = new_cluster->get_point_cloud();
        if (pcloud != 0) {
            WCP::WCPointCloud<double> &cloud = pcloud->get_cloud();
            for (size_t i = 0; i != cloud.pts.size(); i++) {
                x = cloud.pts[i].x / units::cm;
                y = cloud.pts[i].y / units::cm;
                z = cloud.pts[i].z / units::cm;
                SlimMergeGeomCell *mcell = cloud.pts[i].mcell;
                ch_u = cloud.pts[i].index_u;
                ch_v = cloud.pts[i].index_v;
                ch_w = cloud.pts[i].index_w;

                if (mcell == 0) {
                    temp_time_slice = -1;
                    q = 1;
                    nq = 1;
                }
                else {
                    temp_time_slice = mcell->GetTimeSlice();
                    nq = 1;
                    const GeomWire *uwire = gds.by_planeindex(WirePlaneType_t(0), ch_u);
                    const GeomWire *vwire = gds.by_planeindex(WirePlaneType_t(1), ch_v);
                    const GeomWire *wwire = gds.by_planeindex(WirePlaneType_t(2), ch_w);
                    q = (mcell->Get_Wire_Charge(uwire) + mcell->Get_Wire_Charge(vwire) +
                         mcell->Get_Wire_Charge(wwire)) /
                        3.;
                }
                T_cluster->Fill();
            }
        }
    }

    TTree *t_bad = new TTree("T_bad", "T_bad");
    t_bad->SetDirectory(file1);
    Int_t bad_npoints;
    Double_t bad_y[100], bad_z[100];
    t_bad->Branch("bad_npoints", &bad_npoints, "bad_npoints/I");
    t_bad->Branch("bad_y", bad_y, "bad_y[bad_npoints]/D");
    t_bad->Branch("bad_z", bad_z, "bad_z[bad_npoints]/D");

    WCP::SMGCSelection &dead_mcells = fid->get_Dead_mcells();
    for (auto it = dead_mcells.begin(); it != dead_mcells.end(); it++) {
        const SlimMergeGeomCell *cell = (SlimMergeGeomCell *) (*it);
        PointVector ps = cell->boundary();
        bad_npoints = ps.size();
        //    std::cout << bad_npoints << std::endl;
        for (int j = 0; j != bad_npoints; j++) {
            bad_y[j] = ps.at(j).y / units::cm;
            bad_z[j] = ps.at(j).z / units::cm;
        }
        t_bad->Fill();
    }

    file1->Write();
    file1->Close();

    return 0;
}
