#!/usr/bin/env python
#Author: Sheena C Schier
#Created 01April2017 Created to make trigger efficiency plots
#Intended to run on signal only


import sys, os, subprocess, getopt
import re, time, copy, math, array
import argparse, commands
import ROOT
from ROOT import TString
ROOT.gROOT.SetBatch(True) #Do I want to run in batch mode?

#local imports
from Effhistograms import *
from CMSobservables import *
from renormalize import *


#======================================================================
def baseLeptonEE(event, hc, CutsDict):
    if( CutsDict["2BaseLep"] == True and CutsDict["eeEvent"] == True ):
        hc.fill(event, "skim")
        return True
    else: return False
#======================================================================
def baseLeptonMM(event, hc, CutsDict):
    if( CutsDict["2BaseLep"] == True and CutsDict["mumuEvent"] == True ):
        hc.fill(event, "skim")
        return True
    else: return False
#======================================================================
def baseLeptonEM(event, hc, CutsDict):
    if( CutsDict["2BaseLep"] == True and CutsDict["mumuEvent"] == False and CutsDict["eeEvent"] == False ):
        hc.fill(event, "skim")
        return True
    else: return False
#======================================================================
def signalLeptonEE(event, hc, CutsDict):
    if( CutsDict["2SigLep"] == True and CutsDict["eeEvent"] == True ):
        hc.fill(event, "skim")
        return True
    else: return False
#======================================================================
def signalLeptonMM(event, hc, CutsDict):
    if( CutsDict["2SigLep"] == True and CutsDict["mumuEvent"] == True ):
        hc.fill(event, "skim")
        return True
    else: return False
#======================================================================
def signalLeptonEM(event, hc, CutsDict):
    if( CutsDict["2SigLep"] == True and CutsDict["mumuEvent"] == False and CutsDict["eeEvent"] == False ):
        hc.fill(event, "skim")
        return True
    else: return False
#======================================================================



#======================================================================

def CMSEWsignalEE(event, hc, CutsDict):

    #if( CutsDict["eeEvent"] == True and CutsDict["RunNewTriggers"] == False ): 
    if( CutsDict["eeEvent"] == True ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        if CutsDict["Jet105"] == False:
            return False
        hc.fill(event, "jet105")

        if CutsDict["Jet145"] == False:
            return False
        hc.fill(event, "jet145")

        return True

    
    else: return False
#======================================================================

def CMSEWsignalMM(event, hc, CutsDict):

    #if( CutsDict["mumuEvent"] == True and CutsDict["RunNewTriggers"] == False ): 
    if( CutsDict["mumuEvent"] == True ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        if CutsDict["Jet105"] == False:
            return False
        hc.fill(event, "jet105")

        if CutsDict["Jet145"] == False:
            return False
        hc.fill(event, "jet145")

        return True


    else: return False
#======================================================================

def CMSEWsignalEM(event, hc, CutsDict):

    #if( CutsDict["mumuEvent"] == False and CutsDict["eeEvent"] == False and CutsDict["RunNewTriggers"] == False ): 
    if( CutsDict["mumuEvent"] == False and CutsDict["eeEvent"] == False ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        if CutsDict["Jet105"] == False:
            return False
        hc.fill(event, "jet105")

        if CutsDict["Jet145"] == False:
            return False
        hc.fill(event, "jet145")

        return True

    
    else: return False
#======================================================================

def CMSEWsignalDimuTriggerEE(event, hc, CutsDict):

    if( CutsDict["dimuTrigger"] == False ):
        return False
    #if( CutsDict["eeEvent"] == True and CutsDict["RunNewTriggers"] == False ): 
    if( CutsDict["eeEvent"] == True ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True
    
    else: return False

#======================================================================

def CMSEWsignalDimuTriggerMM(event, hc, CutsDict):

    if CutsDict["dimuTrigger"] == False:
        return False
    #if( CutsDict["mumuEvent"] == True and CutsDict["RunNewTriggers"] == False ): 
    if( CutsDict["mumuEvent"] == True ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True

    
    else: return False

#======================================================================

def CMSEWsignalDimuTriggerEM(event, hc, CutsDict):

    if CutsDict["dimuTrigger"] == False:
        return False
    #if( CutsDict["mumuEvent"] == False and CutsDict["eeEvent"] == False and CutsDict["RunNewTriggers"] == False ): 
    if( CutsDict["mumuEvent"] == False and CutsDict["eeEvent"] == False ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True

    else: return False

#======================================================================

def CMSEWsignalSingleMuTriggerEE(event, hc, CutsDict):

    if CutsDict["muTrigger"] == False:
        return False
    #if( CutsDict["eeEvent"] == True and CutsDict["RunNewTriggers"] == False ): 
    if( CutsDict["eeEvent"] == True ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True

    else: return False
#======================================================================

def CMSEWsignalSingleMuTriggerMM(event, hc, CutsDict):

    if CutsDict["muTrigger"] == False:
        return False
    #if( CutsDict["mumuEvent"] == True and CutsDict["RunNewTriggers"] == False ): 
    if( CutsDict["mumuEvent"] == True ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True

    else: return False
#======================================================================

def CMSEWsignalSingleMuTriggerEM(event, hc, CutsDict):

    if CutsDict["muTrigger"] == False:
        return False
    #if( CutsDict["mumuEvent"] == False and CutsDict["eeEvent"] == False and CutsDict["RunNewTriggers"] == False ): 
    if( CutsDict["mumuEvent"] == False and CutsDict["eeEvent"] == False ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True

    else: return False
#======================================================================

def CMSEWsignalMetTriggerEE(event, hc, CutsDict):

    if CutsDict["metTrigger"] == False:
        return False
    #if( CutsDict["eeEvent"] == True and CutsDict["RunNewTriggers"] == False ): 
    if( CutsDict["eeEvent"] == True ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True

    else: return False
#======================================================================

def CMSEWsignalMetTriggerMM(event, hc, CutsDict):

    if CutsDict["metTrigger"] == False:
        return False
    #if( CutsDict["mumuEvent"] == True and CutsDict["RunNewTriggers"] == False ): 
    if( CutsDict["mumuEvent"] == True ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True
    
    else: return False
#======================================================================

def CMSEWsignalMetTriggerEM(event, hc, CutsDict):

    if CutsDict["metTrigger"] == False:
        return False
    if( CutsDict["mumuEvent"] == False and CutsDict["eeEvent"] == False ): 
    #if( CutsDict["mumuEvent"] == False and CutsDict["eeEvent"] == False and CutsDict["RunNewTriggers"] == False ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True

    else: return False
#======================================================================
#======================================================================

def CMSEWsignalORTriggerEE(event, hc, CutsDict):

    if CutsDict["ORTrigger"] == False:
        return False
    if( CutsDict["eeEvent"] == True ): 
    #if( CutsDict["eeEvent"] == True and CutsDict["RunNewTriggers"] == False ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True

    else: return False
#======================================================================

def CMSEWsignalORTriggerMM(event, hc, CutsDict):

    if CutsDict["ORTrigger"] == False:
        return False
    if( CutsDict["mumuEvent"] == True ): 
    #if( CutsDict["mumuEvent"] == True and CutsDict["RunNewTriggers"] == False ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True

    else: return False
#======================================================================

def CMSEWsignalORTriggerEM(event, hc, CutsDict):

    if CutsDict["ORTrigger"] == False:
        return False
    if( CutsDict["mumuEvent"] == False  and CutsDict["eeEvent"] == False ): 
    #if( CutsDict["mumuEvent"] == False and CutsDict["eeEvent"] == False and CutsDict["RunNewTriggers"] == False ): 

        hc.fill(event, "skim")
        if CutsDict["2SigLep"] == False:
            return False
        if CutsDict["1Jet25"] == False:
            return False
        hc.fill(event,"signal")
        if CutsDict["opSign"] == False:
            return False
        hc.fill(event, "opSign")
        if CutsDict["METoverHt"] == False:
            return False
        if CutsDict["HT100"] == False:
            return False
        if CutsDict["ptll3"] == False:
            return False
        if CutsDict["4mll60"] == False:
            return False
        if CutsDict["lepPt30"] == False:
            return False
        if CutsDict["bVeto"] == False:
            return False
        if CutsDict["70MtLepMET"] == False:
            return False
        if CutsDict["Mtautau"] == False:
            return False
        hc.fill(event, "mtautau")

        return True

    else: return False
#======================================================================
#======================================================================
#
def analyze(infile, weightfile, tree, DSID, data, signal, outfile, debug, region):

    if debug: print "opening input file"

    f=ROOT.TFile(infile, "RO")
    fw=ROOT.TFile(weightfile, "RO")

    if debug: print f.GetName()

    #get sum of weights hist
    #if data: sumWhist = f.Get("weighted__AOD")
    #elif signal: sumWhist = f.Get("weighted__AOD")
    #else: sumWhist = fw.Get("weighted__AOD")
    #print sumWhist.GetName()

    if data:
        t=f.Get("%s" %tree)
        sumWhist = fw.Get("weighted__AOD")
    else:
        t=f.Get("%s_NoSys" % tree) 
        sumWhist = f.Get("weighted__AOD") 


    if debug: print t.GetName()

    t.SetBranchStatus("trigWeight_metTrig", 0)
    t.SetBranchStatus("trigMatch_metTrig", 1)
    t.SetBranchStatus("trigWeight_singleMuonTrig", 0)
    t.SetBranchStatus("trigMatch_singleMuonTrig", 1)
    t.SetBranchStatus("trigWeight_singleElectronTrig", 0)
    t.SetBranchStatus("trigMatch_singleElectronTrig", 1)
    t.SetBranchStatus("HLT_2mu4_j85_xe50_mht_emul", 1)
    t.SetBranchStatus("HLT_mu4_j125_xe90_mht_emul", 1)
    t.SetBranchStatus("mu",1) 
    t.SetBranchStatus("nVtx",1) 
    t.SetBranchStatus("nLep_base",1)
    t.SetBranchStatus("nLep_signal",1) 
    t.SetBranchStatus("lep1Flavor",1) 
    t.SetBranchStatus("lep1Charge",1) 
    t.SetBranchStatus("lep1Pt",1) 
    t.SetBranchStatus("lep1Eta",1) 
    t.SetBranchStatus("lep1Phi",1) 
    t.SetBranchStatus("lep1M",1) 
    t.SetBranchStatus("lep1D0",1) 
    t.SetBranchStatus("lep1D0Sig",1) 
    t.SetBranchStatus("lep1Z0",1) 
    t.SetBranchStatus("lep1Z0SinTheta",1) 
    t.SetBranchStatus("lep1PassOR",1) 
    t.SetBranchStatus("lep1Type",1) 
    t.SetBranchStatus("lep1Origin",1) 
    t.SetBranchStatus("lep1NPix",0) 
    t.SetBranchStatus("lep1PassBL",0) 
    t.SetBranchStatus("lep1VeryLoose",0) 
    t.SetBranchStatus("lep1Loose",0) 
    t.SetBranchStatus("lep1Medium",0) 
    t.SetBranchStatus("lep1Tight",0) 
    t.SetBranchStatus("lep1IsoLoose",0) 
    t.SetBranchStatus("lep1IsoTight",0) 
    t.SetBranchStatus("lep1IsoGradient",0) 
    t.SetBranchStatus("lep1IsoGradientLoose",0) 
    t.SetBranchStatus("lep1IsoLooseTrackOnly",0) 
    t.SetBranchStatus("lep1IsoFixedCutLoose",0) 
    t.SetBranchStatus("lep1IsoFixedCutTight",0) 
    t.SetBranchStatus("lep1IsoFixedCutTightTrackOnly",0) 
    t.SetBranchStatus("lep1Ptcone20",0) 
    t.SetBranchStatus("lep1Ptcone30",0) 
    t.SetBranchStatus("lep1Ptcone40",0) 
    t.SetBranchStatus("lep1Signal",1) 
    t.SetBranchStatus("lep1MatchesTrigger",1) 
    t.SetBranchStatus("lep2Flavor",1) 
    t.SetBranchStatus("lep2Charge",1) 