def sample() :
  if isCmssm(switches()["model"]) :
    if switches()["model"] == "tanB10":return mSUGRA_m0_20to2000_m12_20to760_tanb_10andA0_0_7TeV_Pythia6Z_Summer11_PU_S4_START42_V11_FastSim_v1
    if switches()["model"] == "tanB40":return mSUGRA_m0_20to2000_m12_20to760_tanb_40andA0_m500_7TeV_Pythia6Z_Summer11_PU_S4_START42_V11_FSIM_v1
  elif isSms(switches()["model"]) :
    if switches()["model"] == "T2tt": return SMS_T2tt_Mstop_225to1200_mLSP_50to1025_7TeV_Pythia6Z_Summer11_PU_START42_V11_FastSim_v1_V15_03_18_scan_T2tt
    if switches()["model"] == "T2bb": return SMS_T2bb_Msbottom_100to1200_mLSP_50to1150_7TeV_Pythia6Z_Summer11_PU_START42_V11_FastSim_v1_V15_03_24_scan_T2bb
    if switches()["model"] == "T2": return SMS_T2_Mgluino_100to1200_mLSP_50to1150_7TeV_Pythia6Z_Summer11_PU_START42_V11_FastSim_v1_V15_03_14_02
    if switches()["model"] == "T1": return SMS_T1_Mgluino_100to1200_mLSP_50to1150_7TeV_Pythia6Z_Summer11_PU_START42_V11_FastSim_v1_V15_03_14_01
  else :         return None
def sample() :
  if isCmssm(switches()["model"]) :
    if switches()["model"] == "tanB10":return mSUGRA_m0_220to3000_m12_100to1000_tanb_10andA0_0_7TeV_Pythia6Z_StoreResults_PU_START42_V11_FastSim_v5_V15_04_02_scan_msugra_Winter11_jetCorrections_L1FastJet_L2Relative_L3Absolute_jetCollections_ak5calo_ak5pf_hbheNoiseFilterDefaultIsoReq_1 
    if switches()["model"] == "tanB40":return mSUGRA_m0_20to2000_m12_20to760_tanb_40andA0_m500_7TeV_Pythia6Z_Summer11_PU_S4_START42_V11_FSIM_v1
  elif isSms(switches()["model"]) :
    if switches()["model"] == "T2tt": return SMS_T2tt_Mstop_225to1200_mLSP_50to1025_7TeV_Pythia6Z_Summer11_PU_START42_V11_FastSim_v1_V15_03_18_scan_T2tt
    if switches()["model"] == "T2bb": return SMS_T2bb_Msbottom_100to1200_mLSP_50to1150_7TeV_Pythia6Z_Summer11_PU_START42_V11_FastSim_v1_V15_03_25_jetCorrections_L1FastJet_L2Relative_L3Absolute_jetCollections_ak5calo_ak5pf_hbheNoiseFilterDefaultIsoReq_1_scan_T2bb 
    if switches()["model"] == "T1bbbb": return SMS_T1bbbb_Mgluino_100to1200_mLSP_50to1150_7TeV_Pythia6Z_Summer11_PU_START42_V11_FastSim_v3_V15_04_02_scan_T1bbbb_jetCorrections_L1FastJet_L2Relative_L3Absolute_jetCollections_ak5calo_ak5pf_hbheNoiseFilterDefaultIsoReq_1
    if switches()["model"] == "T2": return SMS_T2_Mgluino_100to1200_mLSP_50to1150_7TeV_Pythia6Z_Summer11_PU_START42_V11_FastSim_v1_V15_03_14_02
    if switches()["model"] == "T1": return SMS_T1_Mgluino_100to1200_mLSP_50to1150_7TeV_Pythia6Z_Summer11_PU_START42_V11_FastSim_v1_V15_03_14_01
    if switches()["model"] == "T1tttt": return SMS_T1tttt_Mgluino_450to1200_mLSP_50to800_7TeV_Pythia6Z_Summer11_PU_START42_V11_FSIM_v2_V15_04_02_scan_T1tttt_jetCorrections_L1FastJet_L2Relative_L3Absolute_jetCollections_ak5calo_ak5pf_hbheNoiseFilterDefaultIsoReq_1 
  else :         return None
def cutFlow(cutTreeMC, model) :
  out = []

  cutTreeMC.Attach(count_total)
  if isCmssm(model) :
    cutTreeMC.TAttach(count_total,selection)
    out.append( tripleScale(model = switches()["model"], cutTree = cutTreeMC, cut = count_total, label = "before") )
  elif isSms(model) :
  #  cutTreeMC.TAttach(count_total,smsFilter)
    out.append( smsOps(model = switches()["model"], cutTree = cutTreeMC, cut =
    count_total, label = "before") )
    cutTreeMC.TAttach(count_total,selection)

  if switches()["selection"]!="muon" :
    cutTreeMC.TAttach(selection,oddElectron)
    cutTreeMC.TAttach(oddElectron,oddPhoton)
    cutTreeMC.TAttach(oddPhoton,numComLeptons)
    cutTreeMC.TAttach(numComLeptons,numComPhotons)
    cutTreeMC.TAttach(numComPhotons,LeadingJetEta)
    cutTreeMC.TAttach(LeadingJetEta,badMuonInJet)
    cutTreeMC.TAttach(badMuonInJet,oddJet)
    cutTreeMC.TAttach(oddJet,numComJetsGeq2)
    cutTreeMC.TAttach(numComJetsGeq2,secondJetET)
    cutTreeMC.TAttach(secondJetET,deadECAL_MC)
    cutTreeMC.TAttach(deadECAL_MC,MHToverMET)
  else :
    cutTreeMC.TAttach(selection,oddElectron)
    cutTreeMC.TAttach(oddElectron,oddPhoton)
    cutTreeMC.TAttach(oddPhoton,numComElectrons)
    cutTreeMC.TAttach(numComElectrons,numComMuons)
    cutTreeMC.TAttach(numComMuons,minDRMuonJetCut)
    cutTreeMC.TAttach(minDRMuonJetCut,ZMassCut)
    cutTreeMC.TAttach(ZMassCut,CommonMHTovHT)
    cutTreeMC.TAttach(CommonMHTovHT,PFMTCut30)
    cutTreeMC.TAttach(PFMTCut30,numComPhotons)
    cutTreeMC.TAttach(numComPhotons,LeadingJetEta)
    cutTreeMC.TAttach(LeadingJetEta,badMuonInJet)
    cutTreeMC.TAttach(badMuonInJet,oddJet)
    cutTreeMC.TAttach(oddJet,numComJetsGeq2)
    cutTreeMC.TAttach(numComJetsGeq2,secondJetET)
    cutTreeMC.TAttach(secondJetET,deadECAL_MC)
    cutTreeMC.TAttach(deadECAL_MC,MHToverMET)
  

  alphaTSlices = [(55,None),(52,53),(53,55)]
  for slice in alphaTSlices:
     print slice
     aTlow = OP_HadAlphaTCut(slice[0]/100.)
     out.append(aTlow)
     if slice[1] is not None: 
       aThigh = OP_HadAlphaTCut(slice[1]/100.)
       out.append(aThigh)
       cutTreeMC.TAttach(MHToverMET,aThigh)
       cutTreeMC.FAttach(aThigh,aTlow)
     else:
       cutTreeMC.TAttach(MHToverMET,aTlow)
     #Need to do some btagging as well!
     out.append( addBinnedStuff(model = switches()["model"],
                            cutTree = cutTreeMC,
                            cut = aTlow,
                            htBins = [275, 325] + [375+100*i for i in range(6)],
                            label2 = "AlphaT%d_%s"%(int(slice[0]), "" if slice[1] is None else "%d_"%int(slice[1])),extra = MChiCut))


     for btags in [("==",0),("==",1),("==",2),(">",2)]:
         if slice[0]!= 55: continue
         btag = OP_NumCommonBtagJets(btags[0],5,btags[1],0.679)
         out.append(btag)
         cutTreeMC.TAttach(aTlow,btag)
         out.append( addBinnedStuff(model = switches()["model"],
                                cutTree = cutTreeMC,
                                cut = btag,
                                htBins = [275, 325] + [375+100*i for i in range(6)],
                                label2 = "btag_%s_%i_AlphaT%d_%s"%(btags[0],btags[1],int(slice[0]), "" if slice[1] is None else "%d_"%int(slice[1])),extra = MChiCut))



  if switches()["selection"]=="muon" :
    for btags in [("==",0),("==",1),("==",2),(">",2),(">=",1)]:
        BtagNoAlphaT = OP_NumCommonBtagJets(btags[0],5,btags[1],0.679)
        out.append(BtagNoAlphaT)    
        MuonEta = OP_AditionalMuonCuts(10.,2.1)
        MuonPt = OP_LeadingMuonCut(45.)
        cutTreeMC.TAttach(MHToverMET,MuonPt)   
        cutTreeMC.TAttach(MuonPt,MuonEta)
        cutTreeMC.TAttach(MuonEta,BtagNoAlphaT)
        out.append(MuonEta)
        out.append(MuonPt)
        out.append( addBinnedStuff(model = switches()["model"],
                            cutTree = cutTreeMC,
                            cut = BtagNoAlphaT,
                            htBins = [275, 325] + [375+100*i for i in range(6)],
                            label2 = "btag_%s_%i_NoAlphaT_"%(btags[0],btags[1]),extra = MChiCut))

  return out
def checkSwitches(d) :
  assert d["model"] in ["T1","T2","T2bb","T2tt","tanB3","tanB10","tanB40","tanB50"]
  assert d["jes"] in ["","+ve","-ve"]
  assert isCmssm(d["model"]) or isSms(d["model"])



from ra1objectid.vbtfElectronId_cff import *
from ra1objectid.vbtfMuonId_cff import *
from ra1objectid.ra3PhotonId_cff import *
vbtfElectronIdFilter = Electron_IDFilter( vbtfelectronidWP95ps.ps() )
#vbtfElectronIdFilter = Electron_IDFilter( vbtfelectronidWP90ps.ps() )
ra3PhotonIdFilter  = Photon_IDFilter( ra3photonidps.ps() )

muonfilt = CustomVBTFMuID(mu_id.ps()) if switches()["selection"]=="muon" else Muon_IDFilter( vbtfmuonidps.ps() )
# muonfilt = Muon_IDFilter(vbtfmuonidps.ps())
import os
susydir = os.environ['SUSY_WORKING_SW_DIR'] + '/'
if isCmssm(switches()["model"]) :
    if switches()["model"] == "tanB10": LOweights = SignalScanLOCrossSectionWeighting(xsToPSet( readLOXS(susydir+"SUSYSignalScan/textfiles/goodModelNames_10_0_1.txt") ).ps())
    if switches()["model"] == "tanB40": LOweights = SignalScanLOCrossSectionWeighting(xsToPSet( readLOXS(susydir+"SUSYSignalScan/textfiles/goodModelNames_40_m500_1.txt") ).ps())

def addCutFlowMC(a,cutTreeMC) :
  if "tan" in switches()["model"]:a.AddWeightFilter("Weight",LOweights)
  if switches()["jes"] != "":
    a.AddJetFilter("PreCC",JESUncert)
  a.AddMuonFilter("PreCC",muonfilt); print "WARNING: should this be PreCC?"
  a.AddPhotonFilter("PreCC",ra3PhotonIdFilter)
  a.AddElectronFilter("PreCC",vbtfElectronIdFilter)
  a+=cutTreeMC

#*********************************************************************
#---------------------------------Main Program------------------------
#*********************************************************************
def cutFlow(cutTreeMC, model) :
  out = []

  cutTreeMC.Attach(count_total)
  if isCmssm(model) :
    cutTreeMC.TAttach(count_total,selection)
    out.append( tripleScale(model = switches()["model"], cutTree = cutTreeMC, cut = count_total, label = "before") )
  elif isSms(model) :
  #  cutTreeMC.TAttach(count_total,smsFilter)
    out.append( smsOps(model = switches()["model"], cutTree = cutTreeMC, cut =
    count_total, label = "before") )
    cutTreeMC.TAttach(count_total,selection)

  if switches()["selection"]!="muon" :
    cutTreeMC.TAttach(selection,oddMuon)
    cutTreeMC.TAttach(oddMuon,oddElectron)
    cutTreeMC.TAttach(oddElectron,oddPhoton)
    cutTreeMC.TAttach(oddPhoton,numComLeptons)
    cutTreeMC.TAttach(numComLeptons,numComPhotons)
    cutTreeMC.TAttach(numComPhotons,LeadingJetEta)
    cutTreeMC.TAttach(LeadingJetEta,badMuonInJet)
    cutTreeMC.TAttach(badMuonInJet,oddJet)
    cutTreeMC.TAttach(oddJet,numComJetsGeq2)
    cutTreeMC.TAttach(numComJetsGeq2,secondJetET)
    cutTreeMC.TAttach(secondJetET,deadECAL_MC)
    cutTreeMC.TAttach(deadECAL_MC,MHToverMET)
    cutTreeMC.TAttach(MHToverMET,alphaT)
    cutTreeMC.TAttach(MHToverMET,alphaT70)
    cutTreeMC.FAttach(alphaT70,alphaT55)
    cutTreeMC.FAttach(alphaT55,alphaT53)
  else :
    cutTreeMC.TAttach(selection,oddMuon)
    cutTreeMC.TAttach(oddMuon,oddElectron)
    cutTreeMC.TAttach(oddElectron,oddPhoton)
    cutTreeMC.TAttach(oddPhoton,numComElectrons)
    cutTreeMC.TAttach(numComElectrons,numComMuons)
    cutTreeMC.TAttach(numComMuons,minDRMuonJetCut)
    cutTreeMC.TAttach(minDRMuonJetCut,ZMassCut)
    cutTreeMC.TAttach(ZMassCut,CommonMHTovHT)
    cutTreeMC.TAttach(CommonMHTovHT,PFMTCut30)
    cutTreeMC.TAttach(PFMTCut30,numComPhotons)
    cutTreeMC.TAttach(numComPhotons,LeadingJetEta)
    cutTreeMC.TAttach(LeadingJetEta,badMuonInJet)
    cutTreeMC.TAttach(badMuonInJet,oddJet)
    cutTreeMC.TAttach(oddJet,numComJetsGeq2)
    cutTreeMC.TAttach(numComJetsGeq2,secondJetET)
    cutTreeMC.TAttach(secondJetET,deadECAL_MC)
    cutTreeMC.TAttach(deadECAL_MC,MHToverMET)
    cutTreeMC.TAttach(MHToverMET,alphaT)
    cutTreeMC.TAttach(MHToverMET,alphaT70)
    cutTreeMC.FAttach(alphaT70,alphaT55)
    cutTreeMC.FAttach(alphaT55,alphaT53)
  # out.append( addBinnedStuff(model = switches()["model"],
  #                           cutTree = cutTreeMC,
  #                           cut = alphaT,
  #                           htBins = [250, 300, 350, 450],
  #                           label2 = "") )
  out.append( addBinnedStuff(model = switches()["model"],
                            cutTree = cutTreeMC,
                            cut = alphaT,
                            htBins = [275, 325] + [375+100*i for i in range(6)],
                            label2 = ""),extra = MChiCut)

  # out.append( addBinnedStuff(model = switches()["model"],
  #                           cutTree = cutTreeMC,
  #                           cut = alphaT53,
  #                           htBins = [250, 300, 350, 450],
  #                           label2 = "AlphaT53_55_") )

  out.append( addBinnedStuff(model = switches()["model"],
                            cutTree = cutTreeMC,
                            cut = alphaT53,
                            htBins = [275, 325] + [375+100*i for i in range(6)],
                            label2 = "AlphaT53_55_"),extra = MChiCut)

  # out.append( addBinnedStuff(model = switches()["model"],
  #                           cutTree = cutTreeMC,
  #                           cut = alphaT55,
  #                           htBins = [250, 300, 350, 450],
  #                           label2 = "AlphaT55_70_") )

  out.append( addBinnedStuff(model = switches()["model"],
                            cutTree = cutTreeMC,
                            cut = alphaT55,
                            htBins = [275, 325] + [375+100*i for i in range(6)],
                            label2 = "AlphaT55_70_"),extra = MChiCut)

  # out.append( addBinnedStuff(model = switches()["model"],
  #                           cutTree = cutTreeMC,
  #                           cut = alphaT70,
  #                           htBins = [250, 300, 350, 450],
  #                           label2 = "AlphaT70_inf") )

  out.append( addBinnedStuff(model = switches()["model"],
                            cutTree = cutTreeMC,
                            cut = alphaT70,
                            htBins = [275, 325] + [375+100*i for i in range(6)],
                            label2 = "AlphaT70_inf_"),extra = MChiCut)
  return out
def sample() :
  if isCmssm(switches()["model"]) :
    if switches()["model"] == "tanB10":return mSUGRA_m0_20to2000_m12_20to760_tanb_10andA0_0_7TeV_Pythia6Z_Summer11_PU_S4_START42_V11_FastSim_v1
    if switches()["model"] == "tanB40":return mSUGRA_m0_20to2000_m12_20to760_tanb_40andA0_m500_7TeV_Pythia6Z_Summer11_PU_S4_START42_V11_FSIM_v1
  elif isSms(switches()["model"]) : return SMS_T2tt_Mstop_225to1200_mLSP_50to1025_7TeV_Pythia6Z_Summer11_PU_START42_V11_FastSim_v1_V15_03_18_scan_T2tt
  else :         return None