from compareUltimateSelection import *


'''
Tool for comparing two variables from two datasets.
Yes, writting it in Python3 would be smarter, 
but default is Python2 on all servers so far
'''


if '__main__' == __name__:
    print ""
    parser = ShowArgumentsParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        prog=os.path.basename(sys.argv[0]),
        description=("""Do the comparison for:

        For a full list of arguments, do: 'python {0} -h'
        """).format(os.path.basename(sys.argv[0]))
        )

    ## add the arguments
    parser.add_argument("-v", "--verbose", action="store_true",help="increase output verbosity")

    parser.add_argument("-DataMC", "--DataMC", action="store_true",help="DataMC")
    parser.add_argument("-MCMC", "--MCMC", action="store_true",help="MCMC")
    parser.add_argument("-TwoVars", "--TwoVars", action="store_true",help="TwoVars")
    parser.add_argument("-DataData", "--DataData", action="store_true",help="DataData")
    parser.add_argument("-muIP", "--muIP", action="store_true",help="muIP")
    parser.add_argument("-basic", "--basic", action="store_true",help="Set all parameters for comparison")
    parser.add_argument("-all", "--all", action="store_true",help="Run over all variables")
    
    parser.add_argument("-AllKshort", "--AllKshort", action="store_true",help="AllKshort")
    parser.add_argument("-AllKplus", "--AllKplus", action="store_true",help="AllKplus")
    parser.add_argument('-particle', metavar='<particle>', default = -1, type = int,
                        help="Sets which particle plots should be produced, default: all")

    #add parameters
    parser.add_argument('-variable', metavar='<variable>', default = "", type = str,   help="Sets the variable to be compared")
    parser.add_argument('-variable2', metavar='<variable2>', default = "", type = str, help="Sets the second variable to be compared")
    parser.add_argument('-cut', metavar='<cut>', default = "", type = str,   help="Sets the cut for the variable")
    parser.add_argument('-cut2', metavar='<cut2>', default = "", type = str, help="Sets the cut for the second variable")

    parser.add_argument('-year', metavar='<year>', default = "2011", type = str, help="Sets the year")
    parser.add_argument('-year2', metavar='<year2>', default = "2012", type = str, help="Sets the second year")
    parser.add_argument('-polarity', metavar='<polarity>', default = "both", type = str, help="Sets the mode to be split by polarity, default merge Up and Down, options up, down")
    parser.add_argument('-polarity2', metavar='<polarity2>', default = "both", type = str, help="Sets the mode to be split by polarity2, default merge Up and Down, options up, down")

    #TODO add helps
    parser.add_argument("-Data", "--Data", action="store_true", default = False,help="Data")
    parser.add_argument("-Data2", "--Data2", action="store_true",default = False,help="Data2")
    parser.add_argument("-MC", "--MC", action="store_true",default = False,help="MC")
    parser.add_argument("-MC2", "--MC2", action="store_true",default = False,help="MC2")
    parser.add_argument("-TM", "--TM", action="store_true",default = False,help="TM")
    parser.add_argument("-TM2", "--TM2", action="store_true",default = False,help="TM2")
    parser.add_argument("-RefChan", "--RefChan", action="store_true",default = False,help="ReferenceChannel")
    parser.add_argument("-RefChan2", "--RefChan2", action="store_true",default = False,help="ReferenceChannel2")
    parser.add_argument("-PHSP", "--PHSP", action="store_true",default = False,help="PHSP")
    parser.add_argument("-PHSP2", "--PHSP2", action="store_true",default = False,help="PHSP2")
    parser.add_argument("-Presel", "--Presel", action="store_true",default = False,help="Preselected")
    parser.add_argument("-Presel2", "--Presel2", action="store_true",default = False,help="Preselected2")
    parser.add_argument("-BDTed", "--BDTed", action="store_true",default = False,help="BDTed")
    parser.add_argument("-BDTed2", "--BDTed2", action="store_true",default = False,help="BDTed2")

    parser.add_argument("-sWeighted", "--sWeighted", action="store_true",default = False,help="sWeighted")
    parser.add_argument("-sWeighted2", "--sWeighted2", action="store_true",default = False,help="sWeighted2")
    parser.add_argument("-bWeighted", "--bWeighted", action="store_true",default = False,help="bWeighted")
    parser.add_argument("-bWeighted2", "--bWeighted2", action="store_true",default = False,help="bWeighted2")
    parser.add_argument("-b2Dweighted", "--b2Dweighted", action="store_true",default = False,help="b2Dweighted")
    parser.add_argument("-b2Dweighted2", "--b2Dweighted2", action="store_true",default = False,help="b2Dweighted2")


    parser.add_argument("-KshortDecaysInVelo", "--KshortDecaysInVelo", action="store_true",default = False,help="KshortDecaysInVelo")
    parser.add_argument("-KshortDecaysInVelo2", "--KshortDecaysInVelo2", action="store_true",default = False,help="KshortDecaysInVelo2")

    opts = parser.parse_args()
    if (opts.verbose): print "[DEBUG]\t",opts

    #get year/s
    year_list = []
    if (opts.year == "All"):
        year_list = yearList()
    else: year_list =  re.split(",",opts.year)

    year_list2 = []    
    if (opts.year == "All"):
        year_list2 = yearList()
    else: year_list2 =  re.split(",",opts.year)


    
    for year in year_list: 
        if (opts.basic):
            for year2 in year_list2:   
                if (opts.all):
                    for var in ListOfAllVars():   
                        compareUltimate( variable1 = var, variable2 = var,
                            year1 = int(year), year2 = int(year2),  cut1 = opts.cut, cut2 = opts.cut2,
                            magnet1 = opts.polarity, MC1 = opts.MC, TM1 = opts.TM, ReferenceChannel1 = opts.RefChan, PHSP1 = opts.PHSP, Preselected1 = opts.Presel, BDTed1 = opts.BDTed,
                            magnet2 = opts.polarity2, MC2 = opts.MC2, TM2 = opts.TM2, ReferenceChannel2 = opts.RefChan2, PHSP2 = opts.PHSP2, Preselected2 = opts.Presel2, BDTed2 = opts.BDTed2,
                            sWeighted1 = opts.sWeighted, bWeighted1=opts.bWeighted, b2Dweighted1=opts.b2Dweighted, weightBranch1 = firstMCweight(),
                            sWeighted2 = opts.sWeighted2, bWeighted2=opts.bWeighted2, b2Dweighted2=opts.b2Dweighted2,  weightBranch2 = firstMCweight(),
                            bPrint = False,   KshortDecaysInVelo1 = opts.KshortDecaysInVelo,  KshortDecaysInVelo2 = opts.KshortDecaysInVelo2, verbose = opts.verbose)
                else:
                    compareUltimate( variable1 = opts.variable, variable2 = opts.variable2,
                        year1 = int(year), year2 = int(year2),  cut1 = opts.cut, cut2 = opts.cut2,
                        magnet1 = opts.polarity, MC1 = opts.MC, TM1 = opts.TM, ReferenceChannel1 = opts.RefChan, PHSP1 = opts.PHSP, Preselected1 = opts.Presel, BDTed1 = opts.BDTed,
                        magnet2 = opts.polarity2, MC2 = opts.MC2, TM2 = opts.TM2, ReferenceChannel2 = opts.RefChan2, PHSP2 = opts.PHSP2, Preselected2 = opts.Presel2, BDTed2 = opts.BDTed2,
                        sWeighted1 = opts.sWeighted, bWeighted1=opts.bWeighted, b2Dweighted1=opts.b2Dweighted, weightBranch1 = firstMCweight(),
                        sWeighted2 = opts.sWeighted2, bWeighted2=opts.bWeighted2, b2Dweighted2=opts.b2Dweighted2,  weightBranch2 = firstMCweight(),
                        bPrint = False,   KshortDecaysInVelo1 = opts.KshortDecaysInVelo,  KshortDecaysInVelo2 = opts.KshortDecaysInVelo2, verbose = opts.verbose)

        if (opts.muIP):
            if (opts.Data):
                compareUltimateBchargeAllSamplesTwoVars("log(mu_plus_IPCHI2_OWNPV)", "log(mu_minus_IPCHI2_OWNPV)", 
                year = int(year), magnet = opts.polarity, MC =False, ReferenceChannel=False, PHSP= False, Preselected=True, BDTed=False, 
                sWeighted=opts.sWeighted, bWeighted=False, b2Dweighted=False, KshortDecaysInVelo=False, verbose =opts.verbose)
                compareUltimateBchargeAllSamplesTwoVars("log(mu_minus_IPCHI2_OWNPV)", "log(mu_plus_IPCHI2_OWNPV)", 
                year = int(year), magnet = opts.polarity, MC =False, ReferenceChannel=False, PHSP= False, Preselected=True, BDTed=False, 
                sWeighted=opts.sWeighted, bWeighted=False, b2Dweighted=False, KshortDecaysInVelo=False, verbose =opts.verbose)

                compareUltimateBchargeAllSamplesTwoVars("log(mu_plus_IPCHI2_OWNPV)", "log(mu_plus_IPCHI2_OWNPV)", 
                year = int(year), magnet = opts.polarity, MC =False, ReferenceChannel=False, PHSP= False, Preselected=True, BDTed=False, 
                sWeighted=opts.sWeighted, bWeighted=False, b2Dweighted=False, KshortDecaysInVelo=False, verbose =opts.verbose)

                compareUltimateBchargeAllSamplesTwoVars("log(mu_minus_IPCHI2_OWNPV)", "log(mu_minus_IPCHI2_OWNPV)", 
                year = int(year), magnet = opts.polarity, MC =False, ReferenceChannel=False, PHSP= False, Preselected=True, BDTed=False, 
                sWeighted=opts.sWeighted, bWeighted=False, b2Dweighted=False, KshortDecaysInVelo=False, verbose =opts.verbose) 

                compareUltimateTwoVarsData("log(mu_minus_IPCHI2_OWNPV)", "log(mu_minus_IPCHI2_OWNPV)", year= int(year),  magnet = opts.polarity,
                cut1 = "Polarity=65535", cut2 = "Polarity=1", Preselected= True, BDTed = False, sWeighted = opts.sWeighted, KshortDecaysInVelo = False, verbose = opts.verbose)
                compareUltimateTwoVarsData("log(mu_plus_IPCHI2_OWNPV)", "log(mu_plus_IPCHI2_OWNPV)", year= int(year),  magnet = opts.polarity,
                cut1 = "Polarity=65535", cut2 = "Polarity=1", Preselected= True, BDTed = False, sWeighted = opts.sWeighted, KshortDecaysInVelo = False, verbose = opts.verbose)
    
     
            if (opts.MC):    
                compareUltimateBchargeAllSamplesTwoVars("log(mu_plus_IPCHI2_OWNPV)", "log(mu_minus_IPCHI2_OWNPV)", 
                year = int(year), magnet = opts.polarity, MC =True, ReferenceChannel=opts.RefChan, PHSP= opts.PHSP, Preselected=opts.Presel, BDTed=False, 
                sWeighted=False, bWeighted=opts.bWeighted, b2Dweighted=opts.b2Dweighted, KshortDecaysInVelo=False, verbose =opts.verbose)
                compareUltimateBchargeAllSamplesTwoVars("log(mu_plus_IPCHI2_OWNPV)", "log(mu_plus_IPCHI2_OWNPV)", 
                year = int(year), magnet = opts.polarity, MC =True, ReferenceChannel=opts.RefChan, PHSP= opts.PHSP, Preselected=opts.Presel, BDTed=False, 
                sWeighted=False, bWeighted=opts.bWeighted, b2Dweighted=opts.b2Dweighted, KshortDecaysInVelo=False, verbose =opts.verbose)
                compareUltimateBchargeAllSamplesTwoVars("log(mu_minus_IPCHI2_OWNPV)", "log(mu_minus_IPCHI2_OWNPV)", 
                year = int(year), magnet = opts.polarity, MC =True, ReferenceChannel=opts.RefChan, PHSP= opts.PHSP, Preselected=opts.Presel, BDTed=False, 
                sWeighted=False, bWeighted=opts.bWeighted, b2Dweighted=opts.b2Dweighted, KshortDecaysInVelo=False, verbose =opts.verbose)
                
                compareUltimateTwoVarsMC("log(mu_minus_IPCHI2_OWNPV)", "log(mu_minus_IPCHI2_OWNPV)",year= int(year),  magnet = opts.polarity,
                cut1 = "Polarity=65535", cut2 = "Polarity=1", TM=opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP, 
                Preselected= opts.Presel, BDTed = opts.BDTed, bWeighted = opts.bWeighted, b2Dweighted = opts.b2Dweighted, 
                KshortDecaysInVelo = opts.KshortDecaysInVelo, verbose = opts.verbose)
                
                compareUltimateTwoVarsMC("log(mu_plus_IPCHI2_OWNPV)", "log(mu_plus_IPCHI2_OWNPV)",year= int(year),  magnet = opts.polarity,
                cut1 = "Polarity=65535", cut2 = "Polarity=1", TM=opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP, 
                Preselected= opts.Presel, BDTed = opts.BDTed, bWeighted = opts.bWeighted, b2Dweighted = opts.b2Dweighted, 
                KshortDecaysInVelo = opts.KshortDecaysInVelo, verbose = opts.verbose)

            if (opts.DataMC):
                compareUltimateDataMC(variable = "log(mu_plus_IPCHI2_OWNPV)", year =int(year), cut="", magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)
                compareUltimateDataMC(variable = "log(mu_minus_IPCHI2_OWNPV)", year =int(year), cut="", magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)
                compareUltimateDataMC(variable = "log(mu_plus_IPCHI2_OWNPV)", year =int(year), cut="B_plus_ID = 521", magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)
                compareUltimateDataMC(variable = "log(mu_minus_IPCHI2_OWNPV)", year =int(year), cut="B_plus_ID = 521", magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)
                compareUltimateDataMC(variable = "log(mu_plus_IPCHI2_OWNPV)", year =int(year), cut="B_plus_ID = -521", magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)
                compareUltimateDataMC(variable = "log(mu_minus_IPCHI2_OWNPV)", year =int(year), cut="B_plus_ID = -521", magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)
                compareUltimateDataMC(variable = "log(mu_plus_IPCHI2_OWNPV)", year =int(year), cut="B_plus_ID = 521", magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)
                compareUltimateDataMC(variable = "log(mu_minus_IPCHI2_OWNPV)", year =int(year), cut="B_plus_ID = -521", magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)
                compareUltimateDataMC(variable = "log(mu_plus_IPCHI2_OWNPV)", year =int(year), cut="B_plus_ID = -521", magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)
                compareUltimateDataMC(variable = "log(mu_minus_IPCHI2_OWNPV)", year =int(year), cut="B_plus_ID = +521", magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)

        else:
            if (opts.DataMC):
                if (opts.all):
                    for var in ListOfAllVars():
                        compareUltimateDataMC(variable = var, year =int(year), cut=opts.cut, magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)

                else:    
                    compareUltimateDataMC(variable = opts.variable, year =int(year), cut=opts.cut, magnet=opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP,
                        sWeighted = opts.sWeighted, bWeighted  =  opts.bWeighted, b2Dweighted =  opts.b2Dweighted, 
                        KshortDecaysInVelo =  opts.KshortDecaysInVelo, verbose =  opts.verbose)
            if (opts.MCMC):
                for year2 in year_list2:
                    compareUltimateMCMC(variable = opts.variable, year1 = int(year) , year2 = int(year2) , cut1 = opts.cut, cut2 = opts.cut2, magnet1 = opts.polarity, magnet2 = opts.polarity2,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP, KshortDecaysInVelo = opts.KshortDecaysInVelo, verbose = opts.verbose)
            if (opts.DataData):
                for year2 in year_list2:
                    compareUltimateDataData(variable = opts.variable, year1 = int(year) , year2 = int(year2), cut1 = opts.cut, cut2 = opts.cut2, magnet1 = opts.polarity, magnet2 = opts.polarity2,
                    sWeighted1 = opts.sWeighted, sWeighted2 = opts.sWeighted2, 
                    KshortDecaysInVelo1 =  opts.KshortDecaysInVelo, KshortDecaysInVelo2 =  opts.KshortDecaysInVelo2, verbose = opts.verbose)
            if (opts.TwoVars):
                if (opts.Data):            
                    compareUltimateTwoVarsData(variable1 = opts.variable, variable2 = opts.variable2, 
                        year =int(year) , cut1 = opts.cut, cut2 = opts.cut2, magnet = opts.polarity, Preselected=opts.Presel, BDTed = opts.BDTed, 
                        sWeighted = opts.sWeighted, KshortDecaysInVelo =  opts.KshortDecaysInVelo,  verbose =  opts.verbose)   
                if (opts.MC):     
                    compareUltimateTwoVarsMC(variable1 = opts.variable, variable2 = opts.variable2, 
                        year =int(year) , cut1 = opts.cut, cut2 = opts.cut2, magnet = opts.polarity,
                        TM = opts.TM, ReferenceChannel = opts.RefChan, PHSP = opts.PHSP, Preselected=opts.Presel, BDTed = opts.BDTed, 
                        bWeighted1  =  opts.bWeighted, b2Dweighted1 =  opts.b2Dweighted, bWeighted2  =  opts.bWeighted2, b2Dweighted2 =  opts.b2Dweighted2, 
                        KshortDecaysInVelo2 =  opts.KshortDecaysInVelo, verbose = opts.verbose)
