import ROOT

ROOT.gROOT.SetBatch(True)

#import sys
#sys.path.insert(0, '../../')
import ROOTHelp.FancyROOTStyle

from optparse import OptionParser
p = OptionParser()
p.add_option('--inputData',  type = 'string', dest = 'inFileData', help = 'intput File' )
p.add_option('--inputMC',  type = 'string', dest = 'inFileMC', help = 'intput File' )
p.add_option('--output', type = 'string', default = "jetLevelPlots", dest = 'outDir', help = 'output dir' )
p.add_option('--cmsText', type = 'string', default = "Work in Progress",  help = '' )
p.add_option('--lumiText', default = "",  help = '' )
(o,a) = p.parse_args()


#from rocCurveUtils            import drawWaterMarks
#import rebinning
#from Rebinning import rebinningDB

inFileData  = ROOT.TFile(o.inFileData,  "READ")
inFileMC    = ROOT.TFile(o.inFileMC,  "READ")

import os
if not os.path.exists(o.outDir):
    os.makedirs(o.outDir)


from JetLevelPlotUtils import makeEff, drawComp, drawCompRatio, getHist, drawStackCompRatio, makeStack, makeInverseTurnOn, make2DComp, makeInverseTurnOnAll,plotRatio

# plotRatio("nPV",    "Events",inFileData, "Data", inFileMC, "MC",xTitle="nPV", outDir=o.outDir,binning=1)


#
#  Offline Turnon curves:
#
effBinning=5
eff_CaloDeepCSV_Data = makeEff("DeepCSV_l" ,    ["offCaloJets_matchedCaloDeepcsvTag","offCaloJets_matched"],inFileData,binning=effBinning)
eff_CaloDeepCSV_MC   = makeEff("DeepCSV_l" ,    ["offCaloJets_matchedCaloDeepcsvTag","offCaloJets_matched"],inFileMC  ,binning=effBinning)

eff_CaloDeepCSVvsDeepJet_Data = makeEff("DeepJet_l" ,    ["offCaloJets_matchedCaloDeepcsvTag","offCaloJets_matched"],inFileData,binning=effBinning)
eff_CaloDeepCSVvsDeepJet_MC   = makeEff("DeepJet_l" ,    ["offCaloJets_matchedCaloDeepcsvTag","offCaloJets_matched"],inFileMC  ,binning=effBinning)
eff_PFDeepCSVvsDeepJet_Data   = makeEff("DeepJet_l" ,    ["offJets_matchedPFDeepcsvTag",  "offJets_matched"]  ,inFileData,binning=effBinning)
eff_PFDeepCSVvsDeepJet_MC     = makeEff("DeepJet_l" ,    ["offJets_matchedPFDeepcsvTag",  "offJets_matched"]  ,inFileMC  ,binning=effBinning)

eff_PFDeepJetvsDeepCSV_MC     = makeEff("DeepCSV_l" ,    ["offJets_matchedPFDeepjetTag",  "offJets_matched"]  ,inFileMC  ,binning=effBinning)

eff_PFDeepCSV_Data   = makeEff("DeepCSV_l" ,    ["offJets_matchedPFDeepcsvTag",  "offJets_matched"]  ,inFileData,binning=effBinning)
eff_PFDeepCSV_MC     = makeEff("DeepCSV_l" ,    ["offJets_matchedPFDeepcsvTag",  "offJets_matched"]  ,inFileMC  ,binning=effBinning)
eff_PFDeepJet_Data   = makeEff("DeepJet_l" ,    ["offJets_matchedPFDeepjetTag",  "offJets_matched"]  ,inFileData,binning=effBinning)
eff_PFDeepJet_MC     = makeEff("DeepJet_l" ,    ["offJets_matchedPFDeepjetTag",  "offJets_matched"]  ,inFileMC  ,binning=effBinning)


reveff_DeepJet_MC = {}
reveff_DeepJet_Data = {}
reveff_DeepCSV_MC = {}
reveff_DeepCSV_Data = {}
for jetType in ["PF"]:
    reveff_DeepJet_MC[jetType] = {}
    reveff_DeepJet_Data[jetType] = {}
    reveff_DeepCSV_MC[jetType] = {}
    reveff_DeepCSV_Data[jetType] = {}
    for op in ["Loose","Medium","Tight"]:
        jetTypeDen = jetType if jetType == "Puppi" else ""

        reveff_DeepJet_MC  [jetType][op]   = makeEff("DeepJet_l" ,    ["offJets"+op+"_matched"+jetType+"Jet",  "offJets_matched"+jetTypeDen+"Jet"]  ,inFileMC    ,binning=effBinning)
        reveff_DeepJet_Data[jetType][op]   = makeEff("DeepJet_l" ,    ["offJets"+op+"_matched"+jetType+"Jet",  "offJets_matched"+jetTypeDen+"Jet"]  ,inFileData  ,binning=effBinning)

        reveff_DeepCSV_MC  [jetType][op]   = makeEff("DeepCSV_l" ,    ["offJets"+op+"_matched"+jetType+"Jet",  "offJets_matched"+jetTypeDen+"Jet"]  ,inFileMC    ,binning=effBinning)
        reveff_DeepCSV_Data[jetType][op]   = makeEff("DeepCSV_l" ,    ["offJets"+op+"_matched"+jetType+"Jet",  "offJets_matched"+jetTypeDen+"Jet"]  ,inFileData  ,binning=effBinning)


    drawComp("RevEff_"+jetType+"DeepCSV_MC",[(reveff_DeepCSV_MC[jetType]["Loose"], "Loose",ROOT.kBlue),
                                         (reveff_DeepCSV_MC[jetType]["Medium"],"Medium",ROOT.kGreen),
                                         (reveff_DeepCSV_MC[jetType]["Tight"], "Tight",ROOT.kRed),]
             ,yTitle="Efficiency",xTitle="DeepCSV Value of Jets", otherText=""+jetType+" Jets (MC)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)

    drawComp("RevEff_"+jetType+"DeepJet_MC",[(reveff_DeepJet_MC[jetType]["Loose"], "Loose",ROOT.kBlue),
                                         (reveff_DeepJet_MC[jetType]["Medium"],"Medium",ROOT.kGreen),
                                         (reveff_DeepJet_MC[jetType]["Tight"], "Tight",ROOT.kRed),]
             ,yTitle="Efficiency",xTitle="DeepJet Value of Jets", otherText=""+jetType+" Jets (MC)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)

    drawComp("RevEff_"+jetType+"DeepCSV_Data",[(reveff_DeepCSV_Data[jetType]["Loose"], "Loose",ROOT.kBlue),
                                           (reveff_DeepCSV_Data[jetType]["Medium"],"Medium",ROOT.kGreen),
                                           (reveff_DeepCSV_Data[jetType]["Tight"], "Tight",ROOT.kRed),]
             ,yTitle="Efficiency",xTitle="DeepCSV Value of Jets", otherText=""+jetType+" Jets (Data)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)


    drawComp("RevEff_"+jetType+"DeepJet_All",[(reveff_DeepJet_Data[jetType]["Loose"], "Loose (Data)",ROOT.kBlue),
                                          (reveff_DeepJet_MC[jetType]["Loose"], "Loose (MC)",ROOT.kBlue,24),
                                          (reveff_DeepJet_Data[jetType]["Medium"],"Medium (Data)",ROOT.kGreen+1),
                                          (reveff_DeepJet_MC[jetType]["Medium"],"Medium (MC)",ROOT.kGreen+1,24),
                                          (reveff_DeepJet_Data[jetType]["Tight"], "Tight (Data)",ROOT.kRed),
                                          (reveff_DeepJet_MC[jetType]["Tight"], "Tight (MC)",ROOT.kRed,24),
                                          ]
             ,yTitle="Efficiency",xTitle="Online ("+jetType+"-Jet) CSV Value", otherText="",outDir=o.outDir,yMax=1.2,leg="special",cmsText=o.cmsText,lumiText=o.lumiText)

    drawComp("RevEff_"+jetType+"DeepCSV_All",[(reveff_DeepCSV_Data[jetType]["Loose"], "Loose (Data)",ROOT.kBlue),
                                              (reveff_DeepCSV_MC[jetType]["Loose"], "Loose (MC)",ROOT.kBlue,24),
                                              (reveff_DeepCSV_Data[jetType]["Medium"],"Medium (Data)",ROOT.kGreen+1),
                                              (reveff_DeepCSV_MC[jetType]["Medium"],"Medium (MC)",ROOT.kGreen+1,24),
                                              (reveff_DeepCSV_Data[jetType]["Tight"], "Tight (Data)",ROOT.kRed),
                                              (reveff_DeepCSV_MC[jetType]["Tight"], "Tight (MC)",ROOT.kRed,24),
                                              ]
             ,yTitle="Efficiency",xTitle="Online ("+jetType+"-Jet) DeepCSV Value", otherText="",outDir=o.outDir,yMax=1.2,leg="special",cmsText=o.cmsText,lumiText=o.lumiText)




#   Data vs MC
drawComp("Eff_CaloDeepCSV_DataVSMC",[(eff_CaloDeepCSV_Data,"Data",ROOT.kBlue),(eff_CaloDeepCSV_MC,"MC",ROOT.kRed)]
         ,yTitle="Efficiency",xTitle="CSV Value of Jets", otherText="Online WP: DeepCSV > 0.17",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)
drawComp("Eff_PFDeepCSV_DataVSMC",[(eff_PFDeepCSV_Data,"Data",ROOT.kBlue),(eff_PFDeepCSV_MC,"MC",ROOT.kRed)]
         ,yTitle="Efficiency",xTitle="CSV Value of Jets", otherText="Online WP: DeepCSV > 0.17",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)

drawComp("Eff_CaloDeepCSVvsDeepJet_DataVSMC",[(eff_CaloDeepCSVvsDeepJet_Data,"Data",ROOT.kBlue),(eff_CaloDeepCSVvsDeepJet_MC,"MC",ROOT.kRed)]
         ,yTitle="Efficiency",xTitle="DeepJet Value of Jets", otherText="Online WP: DeepCSV > 0.17",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)
drawComp("Eff_PFDeepCSVvsDeepJet_DataVSMC",[(eff_PFDeepCSVvsDeepJet_Data,"Data",ROOT.kBlue),(eff_PFDeepCSVvsDeepJet_MC,"MC",ROOT.kRed)]
         ,yTitle="Efficiency",xTitle="DeepJet Value of Jets", otherText="Online WP: CSV > 0.17",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)

drawComp("Eff_PFDeepCSV_DataVSMC",[(eff_PFDeepCSV_Data,"Data",ROOT.kBlue),(eff_PFDeepCSV_MC,"MC",ROOT.kRed)]
         ,yTitle="Efficiency",xTitle="DeepCSV Value of Jets", otherText="Online WP: DeepCSV > 0.17",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)


# Calo,PF vs DeepCSV and DeepJet MC

drawComp("Eff_CaloAndPFDeepCSV_PFDeepJet_MC_vsDeepJet",[
    (eff_CaloDeepCSVvsDeepJet_MC,"Data",ROOT.kBlue),
    (eff_PFDeepCSVvsDeepJet_MC,"MC",ROOT.kRed),
    (eff_PFDeepJet_MC,"MC",ROOT.kGreen),
    ]
         ,yTitle="Efficiency",xTitle="Offline DeepJet Value of Jets", otherText="Online WPs: > 0.17",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)

drawComp("Eff_CaloAndPFDeepCSV_PFDeepJet_MC_vsDeepCSV",[
    (eff_CaloDeepCSV_MC,"Data",ROOT.kBlue),
    (eff_PFDeepCSV_MC,"MC",ROOT.kRed),
    (eff_PFDeepJetvsDeepCSV_MC,"MC",ROOT.kGreen),
    ]
         ,yTitle="Efficiency",xTitle="Offline DeepCSV Value of Jets", otherText="Online WPs: > 0.17",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)

#
#   Calo vs PF
#
# drawComp("Eff_CaloVsPF_DeepCSV_Data",[(eff_CaloDeepCSV_Data,"Calo (Data)",ROOT.kBlue),(eff_PFDeepCSV_Data,"PF (Data)",ROOT.kRed)]
#          ,yTitle="Efficiency",xTitle="DeepCSV Value of Jets", otherText=["Online Calo WP: DeepCSV > 0.17","Online PF WP: DeepCSV > 0.17"],outDir=o.outDir,xStartOther=0.3,cmsText=o.cmsText,lumiText=o.lumiText)
# drawComp("Eff_CaloVsPF_CSV_MC",[(eff_CaloCSV_MC,"Calo (MC)",ROOT.kBlue),(eff_PFCSV_MC,"PF (MC)",ROOT.kRed)]
#          ,yTitle="Efficiency",xTitle="CSV Value of Jets", otherText=["Online Calo WP: CSV > 0.17","Online PF WP: CSV > 0.17"],outDir=o.outDir,xStartOther=0.3,cmsText=o.cmsText,lumiText=o.lumiText)
#
drawComp("Eff_CaloVsPF_DeepCSVvsDeepJet_Data",[(eff_CaloDeepCSVvsDeepJet_Data,"Calo (Data)",ROOT.kBlue),(eff_PFDeepCSVvsDeepJet_Data,"PF (Data)",ROOT.kRed)]
         ,yTitle="Efficiency",xTitle="DeepJet Value of Jets", otherText=["Online Calo WP: DeepCSV > 0.17","Online PF WP: DeepCSV > 0.17"],outDir=o.outDir,xStartOther=0.3,cmsText=o.cmsText,lumiText=o.lumiText)
drawComp("Eff_CaloVsPF_DeepCSVvsDeepJet_MC",[(eff_CaloDeepCSVvsDeepJet_MC,"Calo (MC)",ROOT.kBlue),(eff_PFDeepCSVvsDeepJet_MC,"PF (MC)",ROOT.kRed)]
         ,yTitle="Efficiency",xTitle="DeepJet Value of Jets", otherText=["Online Calo WP: DeepCSV > 0.17","Online PF WP: DeepCSV > 0.17"],outDir=o.outDir,xStartOther=0.3,cmsText=o.cmsText,lumiText=o.lumiText)
#
#
drawComp("Eff_CaloVsPF_DeepCSV_Data",[(eff_CaloDeepCSV_Data,"Calo (Data)",ROOT.kBlue),(eff_PFDeepCSV_Data,"PF (Data)",ROOT.kRed)]
         ,yTitle="Efficiency",xTitle="DeepCSV Value of Jets", otherText=["Online Calo WP: DeepCSV > 0.17","Online PF WP: DeepCSV > 0.17"],outDir=o.outDir,xStartOther=0.4,yStartOther=0.15,cmsText=o.cmsText,lumiText=o.lumiText)
drawComp("Eff_CaloVsPF_DeepCSV_MC",[(eff_CaloDeepCSV_MC,"Calo (MC)",ROOT.kBlue),(eff_PFDeepCSV_MC,"PF (MC)",ROOT.kRed)]
         ,yTitle="Efficiency",xTitle="DeepCSV Value of Jets", otherText=["Online Calo WP: DeepCSV > 0.17","Online PF WP: DeepCSV > 0.17"],outDir=o.outDir,xStartOther=0.4,yStartOther=0.15,cmsText=o.cmsText,lumiText=o.lumiText)


#   (Calo vs PF) x (Data vs MC )
drawComp("Eff_CaloVsPF_DeepCSV_DataVsMC",
         [(eff_CaloDeepCSV_Data,"Calo-Jet (Data)",ROOT.kBlue),
          (eff_CaloDeepCSV_MC,  "Calo-Jet (MC)  ",ROOT.kBlue, 24),
          (eff_PFDeepCSV_Data,  "PF-Jet   (Data)",ROOT.kRed),
          (eff_PFDeepCSV_MC,    "PF-Jet   (MC)  ",ROOT.kRed, 24),
          ]
         ,yTitle="Efficiency",xTitle="Offline DeepCSV Value", otherText=["Online Calo WP: DeepCSV > 0.17","Online PF WP: DeepCSV > 0.17"],outDir=o.outDir,
         xStartOther=0.3,cmsText=o.cmsText,lumiText=o.lumiText,yLeg=0.8)
#
#
drawComp("Eff_CaloVsPF_DeepCSVvsDeepJet_DataVsMC",
         [(eff_CaloDeepCSVvsDeepJet_Data,"Calo-Jet (Data)",ROOT.kBlue),
          (eff_CaloDeepCSVvsDeepJet_MC,  "Calo-Jet (MC)  ",ROOT.kBlue, 24),
          (eff_PFDeepCSVvsDeepJet_Data,  "PF-Jet   (Data)",ROOT.kRed),
          (eff_PFDeepCSVvsDeepJet_MC,    "PF-Jet   (MC)  ",ROOT.kRed, 24),
          ]
         ,yTitle="Efficiency",xTitle="Offline DeepJet Value", otherText=["Online Calo WP: DeepCSV > 0.17","Online PF WP: DeepCSV > 0.17"],outDir=o.outDir,
         xStartOther=0.4,yStartOther=0.15,cmsText=o.cmsText,lumiText=o.lumiText,yLeg=0.8)
#
drawComp("Eff_CaloVsPF_DeepCSVvsDeepCSV_DataVsMC",
         [(eff_CaloDeepCSV_Data,"Calo-Jet (Data)",ROOT.kBlue),
          (eff_CaloDeepCSV_MC,  "Calo-Jet (MC)  ",ROOT.kBlue, 24),
          (eff_PFDeepCSV_Data,  "PF-Jet   (Data)",ROOT.kRed),
          (eff_PFDeepCSV_MC,    "PF-Jet   (MC)  ",ROOT.kRed, 24),
          ]
         ,yTitle="Efficiency",xTitle="Offline DeepCSV Value", otherText=["Online Calo WP: DeepCSV > 0.17","Online PF WP: DeepCSV > 0.17"],
         outDir=o.outDir,xStartOther=0.4,yStartOther=0.15,cmsText=o.cmsText,lumiText=o.lumiText,yLeg=0.8)



#
# Offline vs Online
#
csvBinning=3
# Deep CSV
offDeepCSV_PF      = getHist(inFileData,"offJets_matched",     "DeepCSV_l",binning=csvBinning,norm=1)
pfDeepCSV          = getHist(inFileData,"offJets_matchedJet",  "DeepCSV_l",binning=csvBinning,norm=1)
offDeepCSV_PF_MC   = getHist(inFileMC,  "offJets_matched",     "DeepCSV_l",binning=csvBinning,norm=1)
pfDeepCSV_MC       = getHist(inFileMC,  "offJets_matchedJet",  "DeepCSV_l",binning=csvBinning,norm=1)

# CSV
offDeepCSV_Calo    = getHist(inFileData,"offCaloJets_matched",     "DeepCSV_l",binning=csvBinning,norm=1)
caloDeepCSV        = getHist(inFileData,"offCaloJets_matchedJet",  "DeepCSV_l",binning=csvBinning,norm=1)
offDeepCSV_Calo_MC = getHist(inFileMC,  "offCaloJets_matched",     "DeepCSV_l",binning=csvBinning,norm=1)
caloDeepCSV_MC     = getHist(inFileMC,  "offCaloJets_matchedJet",  "DeepCSV_l",binning=csvBinning,norm=1)

offDeepJet_PF      = getHist(inFileData,"offJets_matched",       "DeepJet_l",binning=csvBinning,norm=1)
pfDeepJet          = getHist(inFileData,"offJets_matchedJet",    "DeepJet_l",binning=csvBinning,norm=1)
offDeepJet_PF_MC   = getHist(inFileMC,  "offJets_matched",       "DeepJet_l",binning=csvBinning,norm=1)
pfDeepJet_MC       = getHist(inFileMC,  "offJets_matchedJet",    "DeepJet_l",binning=csvBinning,norm=1)

###
#### Off Vs HLT
drawCompRatio("DeepCSV_PFVsOff",[(pfDeepCSV,"PF Jets (Data)"),(offDeepCSV_PF,"Offline Jets (Data)")]
             ,yTitle="Normalized",xTitle="DeepCSV Value of Jets",rTitle="PF/Offline",outDir=o.outDir,setLogy=0)
drawCompRatio("DeepCSV_PFVsOff_MC",[(pfDeepCSV_MC,"PF Jets (MC)"),(offDeepCSV_PF_MC,"Offline Jets (MC)")]
             ,yTitle="Normalized",xTitle="DeepCSV Value of Jets",rTitle="PF/Offline",outDir=o.outDir,setLogy=0)
#
## Data Vs MC
#drawCompRatio("CSV_Calo_DataVsMC",[(caloCSV,"Data"),(caloCSV_MC,"MC")]
#              ,yTitle="Normalized",xTitle="Calo CSV",rTitle="Data/MC",setLogy=0)
#drawCompRatio("DeepCSV_Calo_DataVsMC",[(caloDeepCSV,"Data"),(caloDeepCSV_MC,"MC")]
#              ,yTitle="Normalized",xTitle="Calo DeepCSV",rTitle="Data/MC",setLogy=0)
#drawCompRatio("CSV_Off_DataVsMC",[(offCSV_PF,"Data"),(offCSV_PF_MC,"MC")]
#              ,yTitle="Normalized",xTitle="Offline CSV",rTitle="Data/MC",setLogy=0)
#drawCompRatio("DeepCSV_Off_DataVsMC",[(offDeepCSV_PF,"Data"),(offDeepCSV_PF_MC,"MC")]
#              ,yTitle="Normalized",xTitle="Offline DeepCSV",rTitle="Data/MC",setLogy=0)
#drawCompRatio("CSV_PF_DataVsMC",[(pfCSV,"Data"),(pfCSV_MC,"MC")]
#              ,yTitle="Normalized",xTitle="PF CSV",rTitle="Data/MC",setLogy=0)
#drawCompRatio("DeepCSV_PF_DataVsMC",[(pfDeepCSV,"Data"),(pfDeepCSV_MC,"MC")]
#              ,yTitle="Normalized",xTitle="PF DeepCSV",rTitle="Data/MC",setLogy=0)


offDeepCSV_B_PF_MC  = getHist(inFileMC,"offJets_matched_B",     "DeepCSV_l",binning=csvBinning,norm=0)
offDeepCSV_C_PF_MC  = getHist(inFileMC,"offJets_matched_C",     "DeepCSV_l",binning=csvBinning,norm=0)
offDeepCSV_L_PF_MC  = getHist(inFileMC,"offJets_matched_L",     "DeepCSV_l",binning=csvBinning,norm=0)

offDeepJet_B_PF_MC  = getHist(inFileMC,"offJets_matched_B",     "DeepJet_l",binning=csvBinning,norm=0)
offDeepJet_C_PF_MC  = getHist(inFileMC,"offJets_matched_C",     "DeepJet_l",binning=csvBinning,norm=0)
offDeepJet_L_PF_MC  = getHist(inFileMC,"offJets_matched_L",     "DeepJet_l",binning=csvBinning,norm=0)

caloDeepCSV_B_MC    = getHist(inFileMC,"offCaloJets_matchedJet_B",     "DeepCSV_l",binning=csvBinning,norm=0)
caloDeepCSV_C_MC    = getHist(inFileMC,"offCaloJets_matchedJet_C",     "DeepCSV_l",binning=csvBinning,norm=0)
caloDeepCSV_L_MC    = getHist(inFileMC,"offCaloJets_matchedJet_L",     "DeepCSV_l",binning=csvBinning,norm=0)

pfDeepCSV_B_MC    = getHist(inFileMC,"offJets_matchedJet_B",     "DeepCSV_l",binning=csvBinning,norm=0)
pfDeepCSV_C_MC    = getHist(inFileMC,"offJets_matchedJet_C",     "DeepCSV_l",binning=csvBinning,norm=0)
pfDeepCSV_L_MC    = getHist(inFileMC,"offJets_matchedJet_L",     "DeepCSV_l",binning=csvBinning,norm=0)

pfDeepJet_B_MC    = getHist(inFileMC,"offJets_matchedJet_B",     "DeepJet_l",binning=csvBinning,norm=0)
pfDeepJet_C_MC    = getHist(inFileMC,"offJets_matchedJet_C",     "DeepJet_l",binning=csvBinning,norm=0)
pfDeepJet_L_MC    = getHist(inFileMC,"offJets_matchedJet_L",     "DeepJet_l",binning=csvBinning,norm=0)


drawStackCompRatio("Calo_DeepCSV_FlavorComp",(caloDeepCSV,"Data"),
                   [(caloDeepCSV_L_MC,"Light Flavor",ROOT.kAzure-9),
                    (caloDeepCSV_C_MC,"Charm Jets",ROOT.kGreen+1),
                    (caloDeepCSV_B_MC,"B Jets",ROOT.kYellow)]
                   ,yTitle="Normalized",xTitle="Online (Calo-Jet) DeepCSV",rTitle="Data/MC",setLogy=0,outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)

drawStackCompRatio("PF_DeepJet_FlavorComp",(pfDeepJet,"Data"),
                   [(pfDeepJet_L_MC,"Light Flavor",ROOT.kAzure-9),
                    (pfDeepJet_C_MC,"Charm Jets",ROOT.kGreen+1),
                    (pfDeepJet_B_MC,"B Jets",ROOT.kYellow)]
                   ,yTitle="Normalized",xTitle="Online (PF-Jet) DeepJet",rTitle="Data/MC",setLogy=0,outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)

drawStackCompRatio("PF_DeepCSV_FlavorComp",(pfDeepCSV,"Data"),
                   [(pfDeepCSV_L_MC,"Light Flavor",ROOT.kAzure-9),
                    (pfDeepCSV_C_MC,"Charm Jets",ROOT.kGreen+1),
                    (pfDeepCSV_B_MC,"B Jets",ROOT.kYellow)]
                   ,yTitle="Normalized",xTitle="Online (PF-Jet) DeepCSV",rTitle="Data/MC",setLogy=0,outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)


drawStackCompRatio("Off_DeepJet_FlavorComp",(offDeepJet_PF,"Data"),
                   [(offDeepJet_L_PF_MC,"Light Flavor",ROOT.kAzure-9),
                    (offDeepJet_C_PF_MC,"Charm Jets",ROOT.kGreen+1),
                    (offDeepJet_B_PF_MC,"B Jets",ROOT.kYellow)]
                   ,yTitle="Normalized",xTitle="Offline DeepJet",rTitle="Data/MC",setLogy=0,outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)

drawStackCompRatio("Off_DeepCSV_FlavorComp",(offDeepCSV_PF,"Data"),
                   [(offDeepCSV_L_PF_MC,"Light Flavor",ROOT.kAzure-9),
                    (offDeepCSV_C_PF_MC,"Charm Jets",ROOT.kGreen+1),
                    (offDeepCSV_B_PF_MC,"B Jets",ROOT.kYellow)]
                   ,yTitle="Normalized",xTitle="Offline DeepCSV",rTitle="Data/MC",setLogy=0,outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)




makeStack("OffJet_Pt",     "pt_m",     "offJets_matched_X",binning=2,xTitle="Offline Jet Pt", rTitle="Data/MC",logy=1,inFileData=inFileData,inFileMC=inFileMC,outDir=o.outDir,min=20,cmsText=o.cmsText,lumiText=o.lumiText)
makeStack("OffJet_Eta",    "eta",      "offJets_matched_X",binning=1,xTitle="Offline Jet Eta",rTitle="Data/MC",logy=0,inFileData=inFileData,inFileMC=inFileMC,outDir=o.outDir,x_min=-3,x_max=3,cmsText=o.cmsText,lumiText=o.lumiText)
makeStack("OffJet_Phi",    "phi",      "offJets_matched_X",binning=2,xTitle="Offline Jet Phi",rTitle="Data/MC",logy=0,inFileData=inFileData,inFileMC=inFileMC,outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)
makeStack("OffJet_DeepCSV","DeepCSV_l","offJets_matched_X",binning=csvBinning,xTitle="Offline Jet DeepCSV",rTitle="Data/MC",logy=0,inFileData=inFileData,inFileMC=inFileMC,outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)
makeStack("OffJet_DeepJet","DeepJet_l","offJets_matched_X",binning=csvBinning,xTitle="Offline Jet DeepJet",rTitle="Data/MC",logy=0,inFileData=inFileData,inFileMC=inFileMC,outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText)



ROOT.gStyle.SetPalette(1)
oldMargin =  ROOT.gStyle.GetPadRightMargin()
ROOT.gStyle.SetPadRightMargin(0.2)



# make2DComp("PFCompDeepCSV_Data",inFileData,"offJets_matchedJet","deepcsv_vs_matched_deepcsv",xTitle="PF Jet Deep CSV",yTitle="Offline Jet Deep CSV", outDir=o.outDir)
make2DComp("PFCompDeepCSV_Data",inFileData,"offJets_matched","deepcsv_vs_matched_deepcsv",xTitle="PF Jet Deep CSV",yTitle="Offline Jet Deep CSV", outDir=o.outDir)
make2DComp("PFCompDeepCSV_MC",  inFileMC  ,"offJets_matched","deepcsv_vs_matched_deepcsv",xTitle="PF Jet Deep CSV",yTitle="Offline Jet Deep CSV", outDir=o.outDir)

make2DComp("PFCompDeepJetvsDeepCSV_Data",inFileData,"offJets_matched","deepjet_vs_matched_deepcsv",xTitle="Online (PF-Jet) DeepJet",yTitle="Offline DeepCSV",outDir=o.outDir)
make2DComp("PFCompDeepJetvsDeepCSV_MC",  inFileMC,  "offJets_matched","deepjet_vs_matched_deepcsv",xTitle="Online (PF-Jet) DeepJet",yTitle="Offline DeepCSV",outDir=o.outDir)

make2DComp("PFCompDeepCSVvsDeepJet_Data",inFileData,"offJets_matched","deepcsv_vs_matched_deepjet",xTitle="Online (PF-Jet) DeepCSV",yTitle="Offline DeepJet",outDir=o.outDir)
make2DComp("PFCompDeepCSVvsDeepJet_MC",  inFileMC,  "offJets_matched","deepcsv_vs_matched_deepjet",xTitle="Online (PF-Jet) DeepCSV",yTitle="Offline DeepJet",outDir=o.outDir)
#
make2DComp("PFCompDeepJet_Data",inFileData,"offJets_matched","deepjet_vs_matched_deepjet",xTitle="Online (PF-Jet) DeepJet",yTitle="Offline DeepJet",outDir=o.outDir)
make2DComp("PFCompDeepJet_MC",  inFileMC,  "offJets_matched","deepjet_vs_matched_deepjet",xTitle="Online (PF-Jet) DeepJet",yTitle="Offline DeepJet",outDir=o.outDir)
#
#
make2DComp("CaloCompDeepCSV_Data",inFileData,"offCaloJets_matched","deepcsv_vs_matched_deepcsv",xTitle="Calo Jet Deep CSV",yTitle="Offline Jet Deep CSV", outDir=o.outDir)
make2DComp("CaloCompDeepCSV_MC",  inFileMC,  "offCaloJets_matched","deepcsv_vs_matched_deepcsv",xTitle="Calo Jet Deep CSV",yTitle="Offline Jet Deep CSV", outDir=o.outDir)


ROOT.gStyle.SetPadRightMargin(oldMargin)


makeInverseTurnOn("CaloDeepCSVEffwrtOff_MC",   "DeepCSV_l","offCaloJetsWORKINGPOINT_matchedCaloJet", inFileMC,  binning=1, otherText="Online Calo DeepCSV Eff wrt Offline (MC)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)
makeInverseTurnOn("CaloDeepCSVEffwrtOffDeepJet_MC",   "DeepJet_l","offCaloJetsWORKINGPOINTDeepFlavour_matchedCaloJet", inFileMC,  binning=1, otherText="Online Calo DeepCSV Eff wrt Offline (MC)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)
makeInverseTurnOn("CaloDeepCSVEffwrtOff_Data", "DeepCSV_l","offCaloJetsWORKINGPOINT_matchedCaloJet", inFileData,binning=1, otherText="Online Calo DeepCSV Eff wrt Offline (Data)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)
makeInverseTurnOnAll("CaloDeepCSVEffwrtOff_All", "DeepCSV_l","offCaloJetsWORKINGPOINT_matchedCaloJet", inFileData,"Data",inFileMC,"MC",binning=1, otherText="Online Calo CSV Eff wrt Offline",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)


makeInverseTurnOn("PFDeepCSVEffwrtOff_MC",   "DeepCSV_l","offJetsWORKINGPOINT_matchedPFJet", inFileMC,  binning=1, otherText="Online PF DeepCSV Eff wrt Offline (MC)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)
makeInverseTurnOn("PFDeepCSVEffwrtOffDeepJet_MC",   "DeepCSV_l","offJetsWORKINGPOINTDeepFlavour_matchedPFJet", inFileMC,  binning=1, otherText="Online PF DeepCSV Eff wrt Offline (MC)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)
makeInverseTurnOn("PFDeepCSVEffwrtOff_Data", "DeepCSV_l","offJetsWORKINGPOINT_matchedPFJet", inFileData,binning=1, otherText="Online PF DeepCSV Eff wrt Offline (Data)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)
makeInverseTurnOnAll("PFDeepCSVEffwrtOff_All", "DeepCSV_l","offJetsWORKINGPOINT_matchedPFJet", inFileData,"Data",inFileMC,"MC",binning=1, outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)

makeInverseTurnOn("PFDeepJetEffwrtOff_MC",   "DeepJet_l","offJetsWORKINGPOINT_matchedPFJet", inFileMC,  binning=1, otherText="Online PF DeepJet Eff wrt Offline (MC)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)
makeInverseTurnOn("PFDeepJetEffwrtOffDeepJet_MC",   "DeepJet_l","offJetsWORKINGPOINTDeepFlavour_matchedPFJet", inFileMC,  binning=1, otherText="Online PF DeepJet Eff wrt Offline (MC)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)
makeInverseTurnOn("PFDeepJetEffwrtOff_Data", "DeepJet_l","offJetsWORKINGPOINT_matchedPFJet", inFileData,binning=1, otherText="Online PF DeepJet Eff wrt Offline (Data)",outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)
makeInverseTurnOnAll("PFDeepJetEffwrtOff_All", "DeepJet_l","offJetsWORKINGPOINT_matchedPFJet", inFileData,"Data",inFileMC,"MC",binning=1, outDir=o.outDir,cmsText=o.cmsText,lumiText=o.lumiText,wpLine=0.24,wpTextY=0.525)


for op in ["Loose","Medium","Tight"]:
    makeInverseTurnOnAll("CaloDeepCSVEffwrtOff_"+op, "DeepCSV_l","offCaloJetsWORKINGPOINT_matchedCaloJet", inFileData,"Data",inFileMC,"MC",binning=1, otherText="Online Calo DeepCSV Eff wrt Offline",outDir=o.outDir,
                         wps=[op],cmsText=o.cmsText,lumiText=o.lumiText)

    makeInverseTurnOnAll("PFDeepCSVEffwrtOff_"+op, "DeepCSV_l","offJetsWORKINGPOINT_matchedPFJet", inFileData,"Data",inFileMC,"MC",binning=1, otherText="Online PF DeepCSV Eff wrt Offline",outDir=o.outDir,
                         wps=[op],cmsText=o.cmsText,lumiText=o.lumiText)

    makeInverseTurnOnAll("PFDeepJetEffwrtOff_"+op, "DeepJet_l","offJetsWORKINGPOINT_matchedPFJet", inFileData,"Data",inFileMC,"MC",binning=1, otherText="Online PF DeepJet Eff wrt Offline",outDir=o.outDir,
                         wps=[op],cmsText=o.cmsText,lumiText=o.lumiText)
