#include "TFile.h"
#include "TTree.h"
#include "TH1F.h"
#include "TCanvas.h"
#include "TLorentzVector.h"

#include <vector>
#include <iostream>

using namespace std;

enum LIMITS {
    MAX_TRACKS = 30000,
};

bool tlv_match(const TLorentzVector & a, const TLorentzVector & b) {
    if (fabs(a.E()-b.E())>0.13*3) return false;
    if (fabs(a.Phi()-b.Phi())>0.14*3) return false;
    if (fabs(a.Theta()-b.Theta())>0.08*3) return false;
    return true;
}

void plot(
        const char* input = "reco2_checkout_hist.root"
        ) {
    auto *tf = TFile::Open(input, "read");
    auto *dir = (TDirectoryFile*) tf->Get("wcpselection");
    auto *T_PFDump = (TTree*) dir->Get("T_PFDump");

    int truth_Ntrack;
    int truth_id[MAX_TRACKS];
    int truth_pdg[MAX_TRACKS];
    int truth_process[MAX_TRACKS];
    int truth_mother[MAX_TRACKS];
    float truth_startXYZT[MAX_TRACKS][4];
    float truth_endXYZT[MAX_TRACKS][4];
    float truth_startMomentum[MAX_TRACKS][4];
    float truth_endMomentum[MAX_TRACKS][4];
    std::vector<std::vector<int> > *truth_daughters;

    int reco_Ntrack;
    int reco_id[MAX_TRACKS];
    int reco_pdg[MAX_TRACKS];
    int reco_process[MAX_TRACKS];
    int reco_mother[MAX_TRACKS];
    float reco_startXYZT[MAX_TRACKS][4];
    float reco_endXYZT[MAX_TRACKS][4];
    float reco_startMomentum[MAX_TRACKS][4];
    float reco_endMomentum[MAX_TRACKS][4];
    std::vector<std::vector<int> > *reco_daughters;

    T_PFDump->SetBranchAddress("truth_Ntrack", &truth_Ntrack);
    T_PFDump->SetBranchAddress("truth_pdg", &truth_pdg);
    T_PFDump->SetBranchAddress("truth_mother", &truth_mother);
    T_PFDump->SetBranchAddress("truth_startXYZT", &truth_startXYZT);
    T_PFDump->SetBranchAddress("truth_startMomentum", &truth_startMomentum);

    T_PFDump->SetBranchAddress("reco_Ntrack", &reco_Ntrack);
    T_PFDump->SetBranchAddress("reco_pdg", &reco_pdg);
    T_PFDump->SetBranchAddress("reco_mother", &reco_mother);
    T_PFDump->SetBranchAddress("reco_startXYZT", &reco_startXYZT);
    T_PFDump->SetBranchAddress("reco_startMomentum", &reco_startMomentum);

    const float plot_max_energy = 3.0;

    TH1F* h_truth_energy = new TH1F("h_truth_energy","h_truth_energy",100,0,plot_max_energy);
    TH1F* h_reco_energy = new TH1F("h_reco_energy","h_reco_energy",100,0,plot_max_energy);
    TH1F* h_reco_energy_match = new TH1F("h_reco_energy_match","h_reco_energy_match",100,0,plot_max_energy);

    TH1F* h_reco_m_truth = new TH1F("h_reco_m_truth","h_reco_m_truth",1000,-10,10);
    TH2F* h_reco_v_truth = new TH2F("h_reco_v_truth","h_reco_v_truth",1000,-300,300,1000,-300,300);

    //for (int ientry=0; ientry<T_PFDump->GetEntries(); ++ientry) {
    for (int ientry=0; ientry<100; ++ientry) {
        T_PFDump->GetEntry(ientry);
        cout << "ientry: " << ientry << endl;
        for(int itruth=0; itruth<truth_Ntrack; ++itruth) {
            if(truth_mother[itruth]!=0) continue;
            if(truth_pdg[itruth]!=11) continue;
            TLorentzVector pos_truth(
                    truth_startXYZT[itruth][0],
                    truth_startXYZT[itruth][1],
                    truth_startXYZT[itruth][2],
                    truth_startXYZT[itruth][3]
                    );
            TLorentzVector mom_truth(
                    truth_startMomentum[itruth][0],
                    truth_startMomentum[itruth][1],
                    truth_startMomentum[itruth][2],
                    truth_startMomentum[itruth][3]
                    );
            h_truth_energy->Fill(mom_truth.E());

            for(int ireco=0; ireco<reco_Ntrack; ++ireco) {
                if(reco_mother[ireco]!=0) continue;
                if(reco_pdg[ireco]!=11) continue;
                TLorentzVector pos_reco(
                        reco_startXYZT[ireco][0],
                        reco_startXYZT[ireco][1],
                        reco_startXYZT[ireco][2],
                        reco_startXYZT[ireco][3]
                        );
                TLorentzVector mom_reco(
                        reco_startMomentum[ireco][0],
                        reco_startMomentum[ireco][1],
                        reco_startMomentum[ireco][2],
                        reco_startMomentum[ireco][3]
                        );
                //if(!tlv_match(mom_truth,mom_reco)) continue;
                h_reco_m_truth->Fill(mom_reco.X()-mom_truth.X());
                h_reco_v_truth->Fill(pos_truth.X(),pos_reco.X());
                h_reco_energy_match->Fill(mom_reco.E());
            }
        }

        for(int ireco=0; ireco<truth_Ntrack; ++ireco) {
            if(reco_mother[ireco]!=0) continue;
            if(reco_pdg[ireco]!=11) continue;
            TLorentzVector mom_reco(
                    reco_startMomentum[ireco][0],
                    reco_startMomentum[ireco][1],
                    reco_startMomentum[ireco][2],
                    reco_startMomentum[ireco][3]
                    );
            h_reco_energy->Fill(mom_reco.E());
        }

    }

    const int LINE_WIDTH = 2;

    TCanvas *c0 = new TCanvas("c0","c0");
    h_reco_energy->SetLineColor(kBlue);
    h_reco_energy->SetLineWidth(LINE_WIDTH);
    h_reco_energy->Draw();
    h_truth_energy->SetLineColor(kBlack);
    h_truth_energy->SetLineWidth(LINE_WIDTH);
    h_truth_energy->Draw("same");
    h_reco_energy_match->SetLineColor(kRed);
    h_reco_energy_match->SetLineWidth(LINE_WIDTH);
    h_reco_energy_match->Draw("same");

    TCanvas *c2 = new TCanvas("c2","c2");
    h_reco_v_truth->SetTitle(";X^{truth} [GeV];X^{reco} [GeV]");
    //h_reco_v_truth->SetStats(0);
    h_reco_v_truth->Draw("colz");

    TCanvas *c3 = new TCanvas("c3","c3");
    h_reco_m_truth->SetTitle(";X^{reco}-X^{truth}");
    //h_reco_m_truth->SetStats(0);
    h_reco_m_truth->Draw();
}
