//main file for the angular analysis of the decay B+->Kst+mumu
//Due to the large number of tests, this file beame obsolete.
//I include it for legacy reasons
//Renata Kopecna
//david gerick

#include "BDTSelection.cpp"
#include "TMVAClassApp.cpp"
#include "MVA.cpp"
#include "MCtruthmatching.cpp"
#include "nTrackWeights.cpp"

#include "GlobalFunctions.hh"
#include "Paths.hpp"
#include "RooExpAndGauss/RooExpAndGauss.hpp"
#include "RooDoubleCB/RooDoubleCB.h"

using namespace std;
using namespace RooFit;



Int_t B2Kstmumu(Int_t Run = 1){

    if(Run != 1 && Run != 2){
        coutERROR("Invalid Run number given: " << Run  << ". Exit program!");
        return 0;
    }

    //////////////////////////////
    /// Pre-Selection
    //////////////////////////////


    ///pre-select data samples
    if(runAllSignalData(Run) == 0){
        coutERROR("Preselection of data sample not successfull. Exit program!");
        return 0;
    }
    else coutInfo("Preselection of data sample: DONE!");


    ///pre-select signal MC
    if(runAllSignalMC(Run) == 0){
        coutERROR("Preselection of MC sample not successfull. Exit program!");
        return 0;
    }
    else coutInfo("Preselection of MC sample: DONE!");


    ///pre-select reference channel MC
    if(Run != 2 && Kst2Kspiplus){
        if(runAllRefMC(Run) == 0){
            coutERROR("Preselection of reference MC sample not successfull. Exit program!");
             return 0;
        }
        else coutInfo("Preselection of reference MC sample: DONE!");
    }
    else{
        if(runAllRefMC(Run) == 0){
            coutERROR("Preselection of reference MC sample not successfull. Exit program!");
             return 0;
        }
        else coutInfo("Preselection of reference MC sample: DONE!");
    }


    ///pre-select phase-space MC
    if(runAllPHSPMC(Run) == 0){
        coutERROR("Preselection of PHSP MC sample not successfull. Exit program!");
        return 0;
    }
    else coutInfo("Preselection of PHSP MC sample: DONE!");


    //////////////////////////////
    /// Truth-Matching of MC samples
    //////////////////////////////


    ///TM of signal decay MC
    if(TruthMatchAll(false, false, Run) == 0){
        coutERROR("Truthmatching of signal MC sample not successfull. Exit program!");
        return 0;
    }
    else coutInfo("Truthmatching of signal channel MC sample: DONE!");


    ///TM of reference channel MC
    if(Run != 2 && Kst2Kspiplus){
        if(TruthMatchAll(true, false, Run) == 0){
            coutERROR("Truthmatching of reference MC sample not successfull. Exit program!");
            return 0;
        }
        else coutInfo("Truthmatching of reference channel MC sample: DONE!");
    }
    else{
        if(TruthMatchAll(true, false, Run) == 0){
            coutERROR("Truthmatching of reference MC sample not successfull. Exit program!");
            return 0;
        }
        else coutInfo("Truthmatching of reference channel MC sample: DONE!");
    }

    ///TM of PHSP MC
    if(TruthMatchAll(false, true, Run) == 0){
        coutERROR("Truthmatching of PHSP MC sample not successfull. Exit program!");
        return 0;
    }
    else coutInfo("Truthmatching of PHSP MC sample: DONE!");


    //////////////////////////////
    /// Weighting of data and re-weighting of MC
    //////////////////////////////


    ///sWeights of data is produced and signal channel MC is re-weighted
    if(WeightAll(true, Run) == 0){
        coutERROR("Weighting the MC sample by nTracks and p_T(B+) of sWeighted data not successfull. Exit program!");
        return 0;
    }
    else coutInfo("Weighting the MC sample by nTracks and p_T(B+) of sWeighted data: DONE!");


    ///re-weight the reference channel MC against sWeighted data
    if(Run != 2 && Kst2Kspiplus){
        if(ReweightReferenceMC(true, Run) == 0){
            coutERROR("Reweighting of Reference MC did not succeed. Exit program!");
            return 0;
        }
        else coutInfo("Reweighting of Reference MC sample: DONE!");
    }
    else{
        if(ReweightReferenceMC(true, Run) == 0){
            coutERROR("Reweighting of Reference MC did not succeed. Exit program!");
            return 0;
        }
        else coutInfo("Reweighting of Reference MC sample: DONE!");

    }

    ///re-weight the phase-space MC against sWeighted data
    if(ReweightPHSPMC(true, Run) == 0){
        coutERROR("Reweighting of PHSP MC did not succeed. Exit program!");
        return 0;
    }


    ///compare all variables (signal channel MC vs. data) used for BDT training
    /* TODO: use python
    if(compareAll(Run) == 0){
        coutERROR("Comparing variables of interest in weighted MC and sWeighted data not successfull. Exit program!");
        return 0;
    }
    else coutInfo("Comparing variables of interest in weighted MC and sWeighted data: DONE!");
    */

    //////////////////////////////
    /// BDT training
    //////////////////////////////
    if (Kst2Kspiplus){
        if (RunDDandLLKshort(Run) ==0){
            coutERROR("BDT training not succesful. Exit program!");
            return 0;
        }
        else  coutInfo("BDT training succesful: DONE!");

    }
    else{
        if (RunKplusPizeroResolved(Run) ==0){
            coutERROR("BDT training not succesful. Exit program!");
            return 0;
        }
        else  coutInfo("BDT training succesful: DONE!");
    }


    //////////////////////////////
    /// BDT evaluation
    //////////////////////////////
    if(TMVAClassApp(Run) == 0){
        coutERROR("TMVAClassApp failed to evaluate BDT response. Exit program!");
        return 0;
    }
    else coutInfo("TMVAClassApp succesfully evaulatued the BDT response: DONE!");

    return 1;

}


Int_t CalculateAllEfficiencies(Int_t Run =1){ //TODO: WIP
    //BDT
    //MC
    //preSelection
    //TruthMatching
    return 1;
}


Int_t main(Int_t argc, char* argv[]){

    if(argc != 2){
        coutERROR("Give exactly 1 argument: (" << argc - 1 << " given): [1] Run number ! For example: ./B2Kstmumu 1");
        coutERROR("Exit program!");
        return 0;
    }


    Int_t Run = atoi(argv[1]);
    std::cout << "\tFor Run " << Run;
    if (smallSample) std::cout << ": small sample only";
    std::cout << "." << endl;

    return B2Kstmumu(Run);
}

