def calculation(arg): mode, cat, w = arg if mode == "2016": TTG = TTG_16 lumi_scale = 35.92 elif mode == "2017": TTG = TTG_17 lumi_scale = 41.53 elif mode == "2018": TTG = TTG_18 lumi_scale = 59.74 selCut = args.selection selCuts = [cutInterpreter.cutString("-".join([selCut, cat]))] filterCutMc = getFilterCut(int(mode), isData=False, skipBadChargedCandidate=True, skipVertexFilter=False) tr = TriggerSelector(int(mode)) triggerCutMc = tr.getSelection() # tr = TriggerSelector( int(mode), singleLepton=args.selection.count("nLepTight1") ) # triggerCutMc = tr.getSelection( "MC" ) selCuts += [triggerCutMc] selCuts += [filterCutMc] selCuts += ["pTStitching==1"] # if overlapcut: selCuts += ["overlapRemoval==1"] preSelectionSR = "&&".join(selCuts) if not args.useCorrectedIsoVeto: preSelectionSR = preSelectionSR.replace("nLeptonVetoIsoCorr", "nLeptonVeto") if not "hoton" in selCut and cat != noCat_sel: yields[mode][cat][sel] = -1 return # print TTG.getEventList( preSelectionSR ).GetN() yields[mode][cat][w] = TTG.getYieldFromDraw(selectionString=preSelectionSR, weightString=w.replace( "luminosity", str(lumi_scale)))['val']
def selection(self, dataMC, dileptonic=None, invertLepIso=None, addMisIDSF=None, nJet=None, nBTag=None, nPhoton=None, MET=None, zWindow=None, m3Window=None, leptonEta=None, leptonPt=None, photonIso=None, processCut=None, channel="all"): """Define full selection dataMC: "Data" or "MC" or "MCptincl channel: all, e or mu, eetight, mumutight, SFtight zWindow: offZeg, onZeg, onZSFllTight, onZSFllgTight, onZSFlloffZSFllg or all m3Window: offM3, onM3 or all photonIso: highSieie, highChgIso, highChgIsohighSieie """ if not dileptonic: dileptonic = self.parameters["dileptonic"] if not invertLepIso: invertLepIso = self.parameters["invertLepIso"] if not addMisIDSF: addMisIDSF = self.parameters["addMisIDSF"] if not nJet: nJet = self.parameters["nJet"] if not nBTag: nBTag = self.parameters["nBTag"] if not nPhoton: nPhoton = self.parameters["nPhoton"] if not MET: MET = self.parameters["MET"] if not zWindow: zWindow = self.parameters["zWindow"] if not leptonEta: leptonEta = self.parameters["leptonEta"] if not leptonPt: leptonPt = self.parameters["leptonPt"] if not m3Window: m3Window = self.parameters["m3Window"] if not photonIso: photonIso = self.parameters["photonIso"] #Consistency checks assert dataMC in ["Data", "MC", "DataMC", "MCpTincl" ], "dataMC = Data or MC or DataMC, got %r." % dataMC assert channel in allChannels, "channel must be one of " + ",".join( allChannels) + ". Got %r." % channel assert zWindow in [ "offZeg", "onZeg", "onZSFllTight", "onZSFllgTight", "onZSFlloffZSFllg", "all" ], "zWindow must be one of onZeg, offZeg, onZSFllTight, onZSFllgTight, all. Got %r" % zWindow assert m3Window in [ "offM3", "onM3", "all" ], "m3Window must be one of onM3, offM3, all. Got %r" % m3Window assert photonIso in [ None, "highSieieNoChgIso", "lowSieieNoChgIso", "noSieie", "highSieie", "lowChgIsoNoSieie", "highChgIsoNoSieie", "noChgIso", "highChgIso", "noChgIsoNoSieie", "highChgIsohighSieie" ], "PhotonIso must be one of highSieie, highChgIso, highChgIsohighSieie. Got %r" % photonIso assert processCut in [ None, "cat0", "cat1", "cat2", "cat3", "cat13", "cat02", "cat134", "cat4" ], "Process specific cut must be one of cat0, cat2, cat13, cat4. Got %r" % processCut if self.sys['selectionModifier']: assert self.sys[ 'selectionModifier'] in jmeVariations + metVariations + eVariations + muVariations, "Don't know about systematic variation %r, take one of %s" % ( self.sys['selectionModifier'], ",".join(jmeVariations + metVariations + eVariations + muVariations)) res = {"cuts": [], "prefixes": []} # default lepton selections tightLepton = "nLepTight1" vetoLepton = "nLepVeto1" # vetoLepton = "nLepNoCorrVeto1" jetCutVar = "nJetGood" jetPrefix = "nJet" btagCutVar = "nBTagGood" btagPrefix = "nBTag" photonCutVar = "nPhotonGood" photonPrefix = "nPhoton" photonCatVar = "PhotonGood0_photonCatMagic" photonCatPrefix = "photoncat" photonVetoCutVar = "nPhotonNoChgIsoNoSieie" photonVetoPrefix = "nHadPhoton" # photonVetoCutVar = None # photonVetoPrefix = None if channel in ["e", "eetight"]: leptonEtaCutVar = "abs(LeptonTight0_eta+LeptonTight0_deltaEtaSC)" leptonEtaPrefix = "etascl" else: leptonEtaCutVar = "abs(LeptonTight0_eta)" leptonEtaPrefix = "etal" leptonPtCutVar = "LeptonTight0_pt" leptonPtPrefix = "ptl" if dileptonic: tightLepton = "nLepTight2-OStight" vetoLepton = "nLepVeto2" if photonIso: zWindow += "SB" if invertLepIso: # invert leptonIso in lepton cuts channel += "Inv" zWindow += "Inv" tightLepton = "nInvLepTight1" vetoLepton = "nLepVeto1" # vetoLepton = "nLepNoCorrVeto1" jetCutVar = "nJetGoodInvLepIso" jetPrefix = "nInvLJet" btagCutVar = "nBTagGoodInvLepIso" btagPrefix = "nInvLBTag" photonCutVar = "nPhotonGoodInvLepIso" photonPrefix = "nInvLPhoton" photonCatVar = "PhotonGoodInvLepIso0_photonCatMagic" photonCatPrefix = "invLphotoncat" # photonVetoCutVar = None # photonVetoPrefix = None photonVetoCutVar = "nPhotonNoChgIsoNoSieieInvLepIso" photonVetoPrefix = "nInvLHadPhoton" if channel in ["eInv", "eetightInv"]: leptonEtaCutVar = "abs(LeptonTightInvIso0_eta+LeptonTightInvIso0_deltaEtaSC)" leptonEtaPrefix = "etainvscl" else: leptonEtaCutVar = "abs(LeptonTightInvIso0_eta)" leptonEtaPrefix = "etainvl" leptonPtCutVar = "LeptonTightInvIso0_pt" leptonPtPrefix = "ptinvl" #photon cut photonSel = nPhoton and not (nPhoton[0] == 0 and nPhoton[1] <= 0) if photonSel and photonIso: if invertLepIso: jetCutVar = "nJetGoodNoChgIsoNoSieieInvLepIso" jetPrefix = "nInvLNoChgIsoNoSieieJet" btagCutVar = "nBTagGoodNoChgIsoNoSieieInvLepIso" btagPrefix = "nInvLNoChgIsoNoSieieBTag" photonCutVar = "nPhotonNoChgIsoNoSieieInvLepIso" photonPrefix = "nInvLHadPhoton" photonVetoCutVar = "nPhotonNoChgIsoNoSieieInvLepIso" photonVetoPrefix = "nInvLHadPhoton" photonCatVar = "PhotonNoChgIsoNoSieieInvLepIso0_photonCatMagic" photonCatPrefix = "invLNoChgIsoNoSieiephotoncat" photonIso += "InvL" else: jetCutVar = "nJetGoodNoChgIsoNoSieie" jetPrefix = "nNoChgIsoNoSieieJet" btagCutVar = "nBTagGoodNoChgIsoNoSieie" btagPrefix = "nNoChgIsoNoSieieBTag" photonCutVar = "nPhotonNoChgIsoNoSieie" photonPrefix = "nHadPhoton" photonVetoCutVar = "nPhotonNoChgIsoNoSieie" photonVetoPrefix = "nHadPhoton" photonCatVar = "PhotonNoChgIsoNoSieie0_photonCatMagic" photonCatPrefix = "noChgIsoNoSieiephotoncat" if not photonSel and not dileptonic: # remove default zwindow cut in qcd estimation for non photon regions zWindow = "all" #Postfix for variables (only for MC and if we have a jme variation) jSysStr = "" if ( dataMC == "MC" or dataMC == "MCpTincl" ) and self.sys['selectionModifier'] in jmeVariations + metVariations: jSysStr = "_" + self.sys['selectionModifier'] pSysStr = "" lSysStr = "" isEVar = False isMuVar = False if (dataMC == "MC" or dataMC == "MCpTincl" ) and self.sys['selectionModifier'] in muVariations: lSysStr = "_" + self.sys['selectionModifier'] # leptonPtCutVar += "_totalUp" if "up" in self.sys['selectionModifier'].lower() else "_totalDown" isMuVar = True if (dataMC == "MC" or dataMC == "MCpTincl" ) and self.sys['selectionModifier'] in eVariations: lSysStr = "_" + self.sys['selectionModifier'] pSysStr = "_" + self.sys['selectionModifier'] isEVar = True # leptonPtCutVar += "_totalUp" if "up" in self.sys['selectionModifier'].lower() else "_totalDown" if photonSel and photonIso: res["prefixes"].append(photonIso) preselphotonIso = cutInterpreter.cutString(photonIso) if dataMC == "MC": res["cuts"].append( preselphotonIso.replace("0_", "0" + pSysStr + "_")) else: res["cuts"].append(preselphotonIso) #leptons or inv. iso leptons res["prefixes"].append(tightLepton) lepSel = cutInterpreter.cutString(tightLepton) if lSysStr: lepSel = lepSel.replace("==", "%s==" % lSysStr) res["cuts"].append(lepSel) #lepton channel or inv. iso lepton channel res["prefixes"].append(channel) chStr = cutInterpreter.cutString(channel) if lSysStr and ((channel in ["e", "eetight"] and isEVar) or (channel in ["mu", "mumutight"] and isMuVar)): chStr = chStr.replace("==", "%s==" % lSysStr) res["cuts"].append(chStr) #lepton veto or no Iso lepton veto res["prefixes"].append(vetoLepton) chVetoStr = cutInterpreter.cutString(vetoLepton) if lSysStr: chVetoStr = chVetoStr.replace("==", "%s==" % lSysStr) res["cuts"].append(chVetoStr) if nJet and not (nJet[0] == 0 and nJet[1] < 0): assert nJet[0] >= 0 and (nJet[1] >= nJet[0] or nJet[1] < 0 ), "Not a good nJet selection: %r" % nJet njetsstr = jetCutVar + jSysStr + ">=" + str(nJet[0]) prefix = jetPrefix + str(nJet[0]) if nJet[1] >= 0: njetsstr += "&&" + jetCutVar + jSysStr + "<=" + str(nJet[1]) if nJet[1] != nJet[0]: prefix += "To" + str(nJet[1]) else: prefix += "p" res["cuts"].append(njetsstr) res["prefixes"].append(prefix) if nBTag and not (nBTag[0] == 0 and nBTag[1] < 0): assert nBTag[0] >= 0 and ( nBTag[1] >= nBTag[0] or nBTag[1] < 0), "Not a good nBTag selection: %r" % nBTag if jSysStr: nbtstr = btagCutVar + jSysStr + ">=" + str(nBTag[0]) else: nbtstr = btagCutVar + jSysStr + ">=" + str(nBTag[0]) prefix = btagPrefix + str(nBTag[0]) if nBTag[1] >= 0: if jSysStr: nbtstr += "&&" + btagCutVar + jSysStr + "<=" + str( nBTag[1]) else: nbtstr += "&&" + btagCutVar + jSysStr + "<=" + str( nBTag[1]) if nBTag[1] != nBTag[0]: prefix += "To" + str(nBTag[1]) else: prefix += "p" res["cuts"].append(nbtstr) res["prefixes"].append(prefix) if photonSel: #photon cut assert nPhoton[0] >= 0 and ( nPhoton[1] >= nPhoton[0] or nPhoton[1] < 0), "Not a good nPhoton selection: %r" % nPhoton nphotonsstr = photonCutVar + pSysStr + ">=" + str(nPhoton[0]) prefix = photonPrefix + str(nPhoton[0]) if nPhoton[1] >= 0: nphotonsstr += "&&" + photonCutVar + pSysStr + "<=" + str( nPhoton[1]) if nPhoton[1] != nPhoton[0]: prefix += "To" + str(nPhoton[1]) else: prefix += "p" res["cuts"].append(nphotonsstr) res["prefixes"].append(prefix) if photonVetoCutVar: nphotonVetosstr = photonVetoCutVar + pSysStr + ">=" + str( nPhoton[0]) prefix = photonVetoPrefix + str(nPhoton[0]) if nPhoton[1] >= 0: nphotonVetosstr += "&&" + photonVetoCutVar + pSysStr + "<=" + str( nPhoton[1]) if nPhoton[1] != nPhoton[0]: prefix += "To" + str(nPhoton[1]) else: prefix += "p" res["cuts"].append(nphotonVetosstr) res["prefixes"].append(prefix) else: addMisIDSF = False res["cuts"].append(photonCutVar + pSysStr + "==0") res["prefixes"].append(photonPrefix + "0") if photonVetoCutVar: res["cuts"].append(photonVetoCutVar + pSysStr + "==0") res["prefixes"].append(photonVetoPrefix + "0") #leptonEta cut if leptonEta and not (leptonEta[0] == 0 and leptonEta[1] < 0): assert leptonEta[0] >= 0 and ( leptonEta[1] >= leptonEta[0] or leptonEta[1] < 0) and leptonEta[1] != leptonEta[ 0], "Not a good leptonEta selection: %r" % leptonEta leptonEtastr = leptonEtaCutVar + ">=" + str(leptonEta[0]) prefix = leptonEtaPrefix + str(leptonEta[0]) if leptonEta[1] >= 0: leptonEtastr += "&&" + leptonEtaCutVar + "<" + str( leptonEta[1]) prefix += "To" + str(leptonEta[1]) res["cuts"].append(leptonEtastr) res["prefixes"].append(prefix) #leptonPt cut if leptonPt and not (leptonPt[0] == 0 and leptonPt[1] < 0): assert leptonPt[0] >= 0 and ( leptonPt[1] >= leptonPt[0] or leptonPt[1] < 0) and leptonPt[1] != leptonPt[ 0], "Not a good leptonPt selection: %r" % leptonPt leptonPtstr = leptonPtCutVar + ">=" + str(leptonPt[0]) prefix = leptonPtPrefix + str(leptonPt[0]) if leptonPt[1] >= 0: leptonPtstr += "&&" + leptonPtCutVar + "<" + str(leptonPt[1]) prefix += "To" + str(leptonPt[1]) res["cuts"].append(leptonPtstr) res["prefixes"].append(prefix) #MET cut if MET and not (MET[0] == 0 and MET[1] < 0): assert MET[0] >= 0 and (MET[1] >= MET[0] or MET[1] < 0 ), "Not a good MET selection: %r" % MET metsstr = "MET_pt" + jSysStr + ">=" + str(MET[0]) prefix = "met" + str(MET[0]) if MET[1] >= 0: metsstr += "&&" + "MET_pt" + jSysStr + "<" + str(MET[1]) if MET[1] != MET[0]: prefix += "To" + str(MET[1]) res["cuts"].append(metsstr) res["prefixes"].append(prefix) #Z window if not "all" in zWindow: res["prefixes"].append(zWindow) preselZWindow = cutInterpreter.cutString(zWindow) if pSysStr: for s in systematicReplacements[self.sys['selectionModifier']]: if s in preselZWindow: preselZWindow = preselZWindow.replace(s, s + pSysStr) res["cuts"].append(preselZWindow) #M3 window if m3Window != "all": res["prefixes"].append(m3Window) preselM3Window = cutInterpreter.cutString(m3Window) res["cuts"].append(preselM3Window) if processCut: catPrefix = photonCatPrefix + processCut.replace("cat", "") res["prefixes"].append(catPrefix) catCut = cutInterpreter.cutString(catPrefix) res["cuts"].append(catCut.replace("0_", "0" + pSysStr + "_")) #badEEVeto # if self.year == 2017: # res["prefixes"].append("BadEEJetVeto") # badEEStr = cutInterpreter.cutString( "BadEEJetVeto" ) # res["cuts"].append( badEEStr ) if invertLepIso: res["cuts"].append("triggeredInvIso==1") elif lSysStr: res["cuts"].append("triggered" + lSysStr + "==1") else: res["cuts"].append("triggered==1") res["cuts"].append("reweightHEM>0") if dataMC == "MC": res["cuts"].append("overlapRemoval==1") res["cuts"].append("pTStitching==1") if dataMC == "MCpTincl": res["cuts"].append("overlapRemoval==1") if dataMC != "DataMC": res["cuts"].append( getFilterCut(isData=(dataMC == "Data"), year=self.year, skipBadChargedCandidate=True)) res["cuts"].extend(self.externalCuts) catVar = photonCatVar.split("0")[0] + "0" if pSysStr: catVar += pSysStr return { "cut": "&&".join(res["cuts"]), "prefix": "-".join(res["prefixes"]), "weightStr": self.weightString(dataMC, photon=catVar if addMisIDSF else None, addMisIDSF=addMisIDSF and self.isPhotonSelection) }
# change the style of the MC sample (filled histograms) # the color is defined where mc samples are defined sample.style = styles.fillStyle(sample.color) # add the predefined weight to the samples sample.weight = mcWeight # if you want to check your plots you can run only on a sub-set of events, we reduce the number of events here # this gives you a wrong plot, but you can check if everything looks ok and works if args.small: for sample in stack.samples: sample.normalization = 1. sample.reduceFiles(factor=20) sample.scale /= sample.normalization # define your plot selection via the python option --selection preSelection = cutInterpreter.cutString(args.selection + "-" + args.mode) # set default settings for your plots (weight, selection, do you want an overflow bin?) Plot.setDefaults(stack=stack, weight=staticmethod(weight), selectionString=preSelection, addOverFlowBin="upper") # define a list of plots here plotList = [] plotList.append( Plot( name='PhotonGood0_pt_coarse', # name of the plot file texX='p_{T}(#gamma_{0}) (GeV)', # x axis label texY='Number of Events', # y axis label attribute=lambda event, sample: event.
elif args.year == 2017: import TTGammaEFT.Samples.nanoTuples_Fall17_private_diMuGamma_postProcessed as mc_samples from TTGammaEFT.Samples.nanoTuples_Run2017_14Dec2018_diMuGamma_postProcessed import Run2017 as data_sample elif args.year == 2018: import TTGammaEFT.Samples.nanoTuples_Autumn18_private_diMuGamma_postProcessed as mc_samples from TTGammaEFT.Samples.nanoTuples_Run2018_14Dec2018_diMuGamma_postProcessed import Run2018 as data_sample mc = [ mc_samples.TT_pow, mc_samples.DY_LO, mc_samples.WJets ] #if args.useZG: mc += [mc_samples.ZG] if args.useZG: mc += [mc_samples.ZG_lowpt] mc_samples.ZG_lowpt.setWeightString("0.8") lumi_scale = data_sample.lumi * 0.001 selection = cutInterpreter.cutString( args.selection ) selection += "&&reweightPU<2&&jsonPassed==1" if args.useZG: selection += "&&overlapRemoval==1" weightString = "%f*weight*reweightPU"%lumi_scale dataHist = data_sample.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection, addOverFlowBin="upper" ) for s in mc: s.hist = s.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection, weightString=weightString, addOverFlowBin="upper" ) s.hist.style = styles.fillStyle( s.color ) s.hist.legendText = s.texName dataHist.style = styles.errorStyle( ROOT.kBlack ) dataHist.legendText = "data (#mu#mu#gamma)" replaceLabel = {
dirDB = MergingDirDB(cache_dir) if not dirDB: raise # Sample definition os.environ["gammaSkim"]="False" #always false for QCD estimat from TTGammaEFT.Samples.nanoTuples_Run2016_14Dec2018_semilep_postProcessed import * data2016 = Run2016 from TTGammaEFT.Samples.nanoTuples_Run2017_14Dec2018_semilep_postProcessed import * data2017 = Run2017 from TTGammaEFT.Samples.nanoTuples_Run2018_14Dec2018_semilep_postProcessed import * data2018 = Run2018 lumi_scale = (data2016.lumi + data2017.lumi + data2018.lumi) * 0.001 filterCutData = getFilterCut( 2016, isData=True, skipBadChargedCandidate=True ) data2016.setSelectionString( [filterCutData, "reweightHEM>0", cutInterpreter.cutString( args.mode )] ) data2016.setWeightString( "weight" ) filterCutData = getFilterCut( 2017, isData=True, skipBadChargedCandidate=True ) data2017.setSelectionString( [filterCutData, "reweightHEM>0", cutInterpreter.cutString( args.mode )] ) data2017.setWeightString( "weight" ) filterCutData = getFilterCut( 2018, isData=True, skipBadChargedCandidate=True ) data2018.setSelectionString( [filterCutData, "reweightHEM>0", cutInterpreter.cutString( args.mode )] ) data2018.setWeightString( "weight" ) setup = Setup( year=2016, photonSelection=False, checkOnly=False, runOnLxPlus=False ) #photonselection always false for qcd estimate setup = setup.sysClone( parameters=allRegions[args.selection]["parameters"] ) selection = setup.selection( "MC", channel="all", **setup.defaultParameters() )["prefix"] selection = cutInterpreter.cutString( selection )
} if len(args.selection.split("-")) == 1 and args.selection in allRegions.keys(): print("Plotting region from SetupHelpers: %s" % args.selection) setup = Setup( year=args.year, photonSelection=False, checkOnly=False, runOnLxPlus=False) #photonselection always false for qcd estimate setup = setup.sysClone(parameters=allRegions[args.selection]["parameters"]) selection = setup.selection("MC", channel=args.mode, **setup.defaultParameters())["prefix"] selection = cutInterpreter.cutString(selection) selection += "&&triggered==1" if args.addCut: selection += "&&" + cutInterpreter.cutString(args.addCut) print("Using selection string: %s" % args.selection) preSelection = setup.selection( "MC", channel=args.mode, **setup.defaultParameters(update=QCD_updates))["prefix"] preSelection = cutInterpreter.cutString(preSelection) preSelection += "&&triggeredInvIso==1" if args.addCut: addSel = cutInterpreter.cutString(args.addCut) for iso, invIso in replaceSelection.iteritems(): addSel = addSel.replace(iso, invIso) preSelection += "&&" + addSel
"nJetGoodNoLepSieie": "nJetGoodNoLepSieieInvLepIso", "nBTagGoodNoLepSieie": "nBTagGoodNoLepSieieInvLepIso", "nPhotonGood": "nPhotonGoodInvLepIso", "LeptonTightNoSieie0": "LeptonTightInvIsoNoSieie0", } filterCutData = getFilterCut(args.year, isData=True, skipBadChargedCandidate=True) filterCutMc = getFilterCut(args.year, isData=False, skipBadChargedCandidate=True) blinding = [] if args.year != 2016 and args.blind: blinding += [cutInterpreter.cutString("highSieieLep")] data_sample.setSelectionString([filterCutData, "reweightHEM>0"] + blinding) data_sample.setWeightString("weight") if args.small: data_sample.normalization = 1. data_sample.reduceFiles(factor=5) data_sample.setWeightString("weight*%f" % (1. / data_sample.normalization)) for s in mc: s.setSelectionString([filterCutMc, "pTStitching==1", "overlapRemoval==1"]) s.read_variables = read_variables_MC sampleWeight = "1" if args.small: s.normalization = 1. s.reduceFiles(factor=100)
"mTinv/F", ] lumi_scale = data_sample.lumi * 0.001 filterCutData = getFilterCut(args.year, isData=True, skipBadChargedCandidate=True) filterCutMc = getFilterCut(args.year, isData=False, skipBadChargedCandidate=True) blinding = [] if args.year != 2016: if "lowSieieNoChgIso" in args.addCut: blinding += [cutInterpreter.cutString("lowSieieHighChgIso")] if "lowChgIsoNoSieie" in args.addCut: blinding += [cutInterpreter.cutString("lowChgIsoHighSieie")] data_sample.setSelectionString([filterCutData, "reweightHEM>0"] + blinding) data_sample.setWeightString("weight") if args.small: data_sample.normalization = 1. data_sample.reduceFiles(factor=5) data_sample.setWeightString("weight*%f" % (1. / data_sample.normalization)) print data_sample.selectionString for s in mc: s.setSelectionString([filterCutMc, "pTStitching==1", "overlapRemoval==1"]) # s.setSelectionString( [ filterCutMc, triggerCutMc, "pTStitching==1", "overlapRemoval==1" ] )
if args.small: sample.normalization=1. sample.reduceFiles( factor=20 ) norm = 1./sample.normalization read_variables = [ "weight/F", "nPhotonGood/I", "nJetGood/I", "nBTagGood/I", "nLeptonTight/I", "nLeptonVetoIsoCorr/I", "PhotonGood0_pt/F", "GenPhotonATLASUnfold0_pt/F", "GenPhotonCMSUnfold0_pt/F", "GenPhotonATLASUnfold0_eta/F", "GenPhotonCMSUnfold0_eta/F", "nGenLeptonATLASUnfold/I", "nGenPhotonATLASUnfold/I", "nGenBJetATLASUnfold/I", "nGenJetsATLASUnfold/I", "nGenLeptonCMSUnfold/I", "nGenPhotonCMSUnfold/I", "nGenBJetCMSUnfold/I", "nGenJetsCMSUnfold/I", ] weight_ = lambda event, sample: event.weight * norm * lumi_scale Plot.setDefaults( stack=Stack( [sample] ), weight=staticmethod( weight_ ), selectionString=cutInterpreter.cutString( args.genSelection ) ) plots = [] plots.append( Plot( name = "GenPhotonCMSUnfold0_pt", texX = "p^{gen}_{T}(#gamma_{0})", attribute = lambda event, sample: event.GenPhotonCMSUnfold0_pt, binning = [20, 20, 220], ) ) plots.append( Plot( name = "GenPhotonCMSUnfold0_eta", texX = "p^{gen}_{T}(#gamma_{0})", attribute = lambda event, sample: event.GenPhotonCMSUnfold0_eta, binning = [10, -2, 2],
("genJetCMSGammadR", [0, 0.4, 0.8, 1.2, 1.6, 2.0, 2.4, 2.8, 3.2], "#DeltaR(jet,#gamma)"), ("genLCMStight0GammadR", [0, 0.4, 0.8, 1.2, 1.6, 2.0, 2.4, 2.8, 3.2], "#DeltaR(l,#gamma)"), ("nGenBJetCMSUnfold", [0, 1, 2, 3], "N_{b-jet}"), ("nGenJetsCMSUnfold", [0, 1, 2, 3, 4, 5, 6, 7], "N_{jet}"), ] #sel = "nGenLepCMS1-nGenJetCMS3p-nGenBTagCMS1p-nGenPhotonCMS1" sel = "nGenLepCMS1" #sel = "nGenPhotonCMS1" #sel = "nGenLepCMS1-nGenPhotonCMS1" #sel = "nGenLepCMS1-nGenJetCMS3p-nGenPhotonCMS1" #sel = "nGenLepCMS1-nGenJetCMS3p-nGenBTagCMS1p-nGenPhotonCMS1" #sel = "nGenLepCMS1-nGenJetCMS4p-nGenBTagCMS1p-nGenPhotonCMS1" gen_sel = cutInterpreter.cutString(sel) + "&&overlapRemoval==1" #gen_weight = "weight*(35.92)" gen_weight = "weight*(41.53)" plot_directory_ = os.path.join(plot_directory, "herwig", "ppv49_v1_h7", sel) copyIndexPHP(plot_directory_) print gen_sel #data_dir = "/eos/vbc/user/lukas.lechner/TTGammaEFT/nanoTuples/postprocessed/TTGammaEFT_PP_2016_TTG_private_v49/inclusive" data_dir = "/eos/vbc/user/lukas.lechner/TTGammaEFT/nanoTuples/postprocessed/TTGammaEFT_PP_2017_TTG_private_v49/inclusive" ttg0l_pyt = Sample.fromDirectory( "ttg0l_pyt", directory=[
sample.scale = lumi_scale sample.weight = lambda event, sample: event.reweightTrigger * event.reweightL1Prefire * event.reweightPU * event.reweightLeptonTightSF * event.reweightLeptonTrackingTightSF * event.reweightPhotonSF * event.reweightPhotonElectronVetoSF * event.reweightBTag_SF stackSamples = [[data_sample], [mc_fit], [mc_sb]] stack = Stack(*stackSamples) if args.small: for sample in stack.samples: sample.normalization = 1. sample.reduceFiles(factor=20) sample.scale /= sample.normalization weight_ = lambda event, sample: event.weight # Use some defaults (set defaults before you create/import list of Plots!!) sel = cutInterpreter.cutString(args.selection) Plot.setDefaults(stack=stack, weight=staticmethod(weight_), selectionString=sel) # plotList addPlots = [] isoBinning = Binning.fromThresholds([0, isoThresh, 20]) chgIsoBinning = Binning.fromThresholds([0, chgIsoThresh, 20]) sieieBinning = Binning.fromThresholds( [0, lowSieieThresh, highSieieThresh, 0.025]) if args.sideband == "chgIso": addPlots.append( Plot( name='%s_sieie_%s_coarse' %
ws16 = "+(%s*(PhotonNoChgIsoNoSieie0_photonCatMagic==2)*(%f-1)*(year==2016))" % ( ws, misIDSF_val[2016].val) ws17 = "+(%s*(PhotonNoChgIsoNoSieie0_photonCatMagic==2)*(%f-1)*(year==2017))" % ( ws, misIDSF_val[2017].val) ws18 = "+(%s*(PhotonNoChgIsoNoSieie0_photonCatMagic==2)*(%f-1)*(year==2018))" % ( ws, misIDSF_val[2018].val) weightString = ws + ws16 + ws17 + ws18 for sample in mc: # + signals: sample.read_variables = read_variables_MC sample.scale = lumi_scale sample.style = styles.fillStyle(sample.color) sample.setWeightString(weightString) weight_ = lambda event, sample: event.weight * event.reweightHEM preSelection = cutInterpreter.cutString(args.selection) filterCutMc = getFilterCut(args.year, isData=False, skipBadChargedCandidate=True) triggerCut = "triggered==1" print preSelection Plot.setDefaults(stack=stack, weight=staticmethod(weight_), selectionString=preSelection, addOverFlowBin=None) # plotList addPlots = [] addPlots.append(
elif "4" in args.selection and not "4p" in args.selection: misIDSF_val = misID4SF_val elif "5" in args.selection: misIDSF_val = misID5SF_val elif "2p" in args.selection: misIDSF_val = misID2pSF_val elif "3p" in args.selection: misIDSF_val = misID3pSF_val elif "4p" in args.selection: misIDSF_val = misID4pSF_val filterCutMC = getFilterCut( 2016, isData=False, skipBadChargedCandidate=True ) weightString = "%f*weight*reweightHEM*reweightTrigger*reweightL1Prefire*reweightPU*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightBTag_SF"%lumi_scale weightStringAR = "((%s)+(%s*%f*((nPhotonNoChgIsoNoSieie>0)*(PhotonNoChgIsoNoSieie0_photonCatMagic==2))))"%(weightString,weightString,(misIDSF_val[2016].val-1)) mc16.setSelectionString( [filterCutMC, "pTStitching==1", "reweightHEM>0", cutInterpreter.cutString( args.mode )] ) mc16.setWeightString( weightStringAR ) filterCutMC = getFilterCut( 2017, isData=False, skipBadChargedCandidate=True ) weightString = "%f*weight*reweightHEM*reweightTrigger*reweightL1Prefire*reweightPU*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightBTag_SF"%lumi_scale weightStringAR = "((%s)+(%s*%f*((nPhotonNoChgIsoNoSieie>0)*(PhotonNoChgIsoNoSieie0_photonCatMagic==2))))"%(weightString,weightString,(misIDSF_val[2017].val-1)) mc17.setSelectionString( [filterCutMC, "pTStitching==1", "reweightHEM>0", cutInterpreter.cutString( args.mode )] ) mc17.setWeightString( weightStringAR ) filterCutMC = getFilterCut( 2018, isData=False, skipBadChargedCandidate=True ) weightString = "%f*weight*reweightHEM*reweightTrigger*reweightL1Prefire*reweightPU*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightBTag_SF"%lumi_scale weightStringAR = "((%s)+(%s*%f*((nPhotonNoChgIsoNoSieie>0)*(PhotonNoChgIsoNoSieie0_photonCatMagic==2))))"%(weightString,weightString,(misIDSF_val[2018].val-1)) mc18.setSelectionString( [filterCutMC, "pTStitching==1", "reweightHEM>0", cutInterpreter.cutString( args.mode )] ) mc18.setWeightString( weightStringAR ) setup = Setup( year=2016, photonSelection=False, checkOnly=False, runOnLxPlus=False ) #photonselection always false for qcd estimate
def genSelection(self, dataMC, dileptonic=None, invertLepIso=None, addMisIDSF=None, nJet=None, nBTag=None, nPhoton=None, MET=None, zWindow=None, m3Window=None, leptonEta=None, leptonPt=None, photonIso=None, processCut=None, channel="all"): # define input similar to selection() eventhough not used """Define full selection channel: all, e or mu, eetight, mumutight, SFtight zWindow: offZeg, onZeg, onZSFllTight, onZSFllgTight or all m3Window: offM3, onM3 or all """ if not nJet: nJet = self.parameters["nJet"] if not nBTag: nBTag = self.parameters["nBTag"] if not nPhoton: nPhoton = self.parameters["nPhoton"] if not MET: MET = self.parameters["MET"] if not zWindow: zWindow = self.parameters["zWindow"] if not leptonEta: leptonEta = self.parameters["leptonEta"] if not leptonPt: leptonPt = self.parameters["leptonPt"] if not m3Window: m3Window = self.parameters["m3Window"] #Consistency checks assert channel in allChannels, "channel must be one of " + ",".join( allChannels) + ". Got %r." % channel assert zWindow in [ "offZeg", "onZeg", "onZSFllTight", "onZSFllgTight", "all" ], "zWindow must be one of onZeg, offZeg, onZSFllTight, onZSFllgTight, all. Got %r" % zWindow assert m3Window in [ "offM3", "onM3", "all" ], "m3Window must be one of onM3, offM3, all. Got %r" % m3Window res = {"cuts": [], "prefixes": []} # default lepton selections tightLepton = "nLepTight1" jetCutVar = "nJetGood" jetPrefix = "nJet" btagCutVar = "nBTagGood" btagPrefix = "nBTag" photonCutVar = "nPhotonGood" photonPrefix = "nPhoton" leptonEtaCutVar = "abs(LeptonTight0_eta)" leptonEtaPrefix = "etal" leptonPtCutVar = "LeptonTight0_pt" leptonPtPrefix = "ptl" #leptons or inv. iso leptons res["prefixes"].append(tightLepton) lepSel = cutInterpreter.cutString(tightLepton) res["cuts"].append(lepSel) #lepton channel or inv. iso lepton channel res["prefixes"].append(channel) chStr = cutInterpreter.cutString(channel) res["cuts"].append(chStr) if nJet and not (nJet[0] == 0 and nJet[1] < 0): assert nJet[0] >= 0 and (nJet[1] >= nJet[0] or nJet[1] < 0 ), "Not a good nJet selection: %r" % nJet njetsstr = jetCutVar + ">=" + str(nJet[0]) prefix = jetPrefix + str(nJet[0]) if nJet[1] >= 0: njetsstr += "&&" + jetCutVar + "<=" + str(nJet[1]) if nJet[1] != nJet[0]: prefix += str(nJet[1]) else: prefix += "p" res["cuts"].append(njetsstr) res["prefixes"].append(prefix) if nBTag and not (nBTag[0] == 0 and nBTag[1] < 0): assert nBTag[0] >= 0 and ( nBTag[1] >= nBTag[0] or nBTag[1] < 0), "Not a good nBTag selection: %r" % nBTag nbtstr = btagCutVar + ">=" + str(nBTag[0]) prefix = btagPrefix + str(nBTag[0]) if nBTag[1] >= 0: nbtstr += "&&" + btagCutVar + "<=" + str(nBTag[1]) if nBTag[1] != nBTag[0]: prefix += str(nBTag[1]) else: prefix += "p" res["cuts"].append(nbtstr) res["prefixes"].append(prefix) photonSel = nPhoton and not (nPhoton[0] == 0 and nPhoton[1] <= 0) if photonSel: #photon cut assert nPhoton[0] >= 0 and ( nPhoton[1] >= nPhoton[0] or nPhoton[1] < 0), "Not a good nPhoton selection: %r" % nPhoton nphotonsstr = photonCutVar + ">=" + str(nPhoton[0]) prefix = photonPrefix + str(nPhoton[0]) if nPhoton[1] >= 0: nphotonsstr += "&&" + photonCutVar + "<=" + str(nPhoton[1]) if nPhoton[1] != nPhoton[0]: prefix += "To" + str(nPhoton[1]) else: prefix += "p" res["cuts"].append(nphotonsstr) res["prefixes"].append(prefix) else: res["cuts"].append(photonCutVar + "==0") res["prefixes"].append(photonPrefix + "0") #leptonEta cut if leptonEta and not (leptonEta[0] == 0 and leptonEta[1] < 0): assert leptonEta[0] >= 0 and ( leptonEta[1] >= leptonEta[0] or leptonEta[1] < 0) and leptonEta[1] != leptonEta[ 0], "Not a good leptonEta selection: %r" % leptonEta leptonEtastr = leptonEtaCutVar + ">=" + str(leptonEta[0]) prefix = leptonEtaPrefix + str(leptonEta[0]) if leptonEta[1] >= 0: leptonEtastr += "&&" + leptonEtaCutVar + "<" + str( leptonEta[1]) prefix += "To" + str(leptonEta[1]) res["cuts"].append(leptonEtastr) res["prefixes"].append(prefix) #leptonPt cut if leptonPt and not (leptonPt[0] == 0 and leptonPt[1] < 0): assert leptonPt[0] >= 0 and ( leptonPt[1] >= leptonPt[0] or leptonPt[1] < 0) and leptonPt[1] != leptonPt[ 0], "Not a good leptonPt selection: %r" % leptonPt leptonPtstr = leptonPtCutVar + ">=" + str(leptonPt[0]) prefix = leptonPtPrefix + str(leptonPt[0]) if leptonPt[1] >= 0: leptonPtstr += "&&" + leptonPtCutVar + "<" + str(leptonPt[1]) prefix += "To" + str(leptonPt[1]) res["cuts"].append(leptonPtstr) res["prefixes"].append(prefix) #MET cut if MET and not (MET[0] == 0 and MET[1] < 0): assert MET[0] >= 0 and (MET[1] >= MET[0] or MET[1] < 0 ), "Not a good MET selection: %r" % MET metsstr = "MET_pt" + ">=" + str(MET[0]) prefix = "met" + str(MET[0]) if MET[1] >= 0: metsstr += "&&" + "MET_pt" + "<" + str(MET[1]) if MET[1] != MET[0]: prefix += "To" + str(MET[1]) res["cuts"].append(metsstr) res["prefixes"].append(prefix) #Z window if not "all" in zWindow: res["prefixes"].append(zWindow) preselZWindow = cutInterpreter.cutString(zWindow) res["cuts"].append(preselZWindow) #M3 window if m3Window != "all": res["prefixes"].append(m3Window) preselM3Window = cutInterpreter.cutString(m3Window) res["cuts"].append(preselM3Window) return { "cut": "&&".join(res["cuts"]), "prefix": "-".join(res["prefixes"]) }
filterCutData = getFilterCut(args.year, isData=True, skipBadChargedCandidate=True) data_sample_2 = copy.deepcopy(data_sample) data_sample_3 = copy.deepcopy(data_sample) data_sample_4p = copy.deepcopy(data_sample) setup = Setup( year=args.year, photonSelection=False, checkOnly=False, runOnLxPlus=False) #photonselection always false for qcd estimate setup = setup.sysClone(parameters=allRegions[args.selection]["parameters"]) selection = setup.selection("MC", channel="all", **setup.defaultParameters())["prefix"] selection = cutInterpreter.cutString(selection.replace("4p", "2")) selection += "&&triggered==1" if args.addCut: selection += "&&" + cutInterpreter.cutString(args.addCut) print("Using selection string: %s" % selection) data_sample_2.setSelectionString([ filterCutData, "reweightHEM>0", cutInterpreter.cutString(args.mode), selection ]) data_sample_2.setWeightString("weight") setup = Setup( year=args.year, photonSelection=False, checkOnly=False, runOnLxPlus=False) #photonselection always false for qcd estimate setup = setup.sysClone(parameters=allRegions[args.selection]["parameters"])
if args.variable == "pt": variable = "GenPhotonCMSUnfold0_pt" binning = array.array('d', [20, 35, 50, 80, 120, 160, 200, 260, 320]) elif args.variable == "eta": variable = "abs(GenPhotonCMSUnfold0_eta)" binning = array.array('d', [0, 0.3, 0.6, 0.9, 1.2]) elif args.variable == "dR": variable = "sqrt((GenPhotonCMSUnfold0_eta-GenLeptonCMSUnfold0_eta)**2+acos(cos(GenPhotonCMSUnfold0_phi-GenLeptonCMSUnfold0_phi))**2)" binning = array.array('d', [0.4, 0.8, 1.2, 1.6, 2.0, 2.4, 2.8]) #weight = "weight" weight = "weight*reweightHEM*reweightTrigger*reweightL1Prefire*reweightPU*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightBTag_SF*(35.92*(year==2016)+41.53*(year==2017)+59.74*(year==2018))" selection = cutInterpreter.cutString( "nGenLepCMS1-nGenJetCMS3p-nGenBTagCMS1p-nGenPhotonCMS1") selection += "&&nLeptonTight==1&&nLeptonVetoIsoCorr==1&&nJetGood>=3&&nBTagGood>=1&&nPhotonGood>=1&&nPhotonGood<=1&&nPhotonNoChgIsoNoSieie>=1&&nPhotonNoChgIsoNoSieie<=1&&triggered==1&&reweightHEM>0&&Flag_goodVertices&&Flag_globalSuperTightHalo2016Filter&&Flag_HBHENoiseFilter&&Flag_HBHENoiseIsoFilter&&Flag_EcalDeadCellTriggerPrimitiveFilter&&Flag_BadPFMuonFilter&&PV_ndof>4&&sqrt(PV_x*PV_x+PV_y*PV_y)<=2&&abs(PV_z)<=24" scale_indices = [0, 1, 3, 5, 7, 8] #4 central? pdf_indices = range(100) aS_variations16 = ["abs(LHEPdfWeight[100])", "abs(LHEPdfWeight[101])"] aS_variations = ["abs(LHEPdfWeight[101])", "abs(LHEPdfWeight[102])"] scale_variations = ["abs(LHEScaleWeight[%i])" % i for i in scale_indices] PDF_variations = ["abs(LHEPdfWeight[%i])" % i for i in pdf_indices] # central hist central = sample.get1DHistoFromDraw(variable, binning=binning, selectionString=selection, weightString=weight, addOverFlowBin="upper",
#selectionString="nLepTight1-nLepVeto1-nJet4p-nBTag1p-nPhoton1p-mlgamma85to105-e" #selectionString="nLepTight1-nLepVeto1-nJet4p-nBTag1p-nPhoton1p-mlgamma85to105-mu" #selectionString="nLepTight1-nLepVeto1-nJet4p-nBTag1p-nPhoton1p-mlgamma85to105" #selectionString="nLepTight1-nLepVeto1-nJet3p-nBTag0-nPhoton1p-mlgamma85to105-e" #selectionString="nLepTight1-nLepVeto1-nJet3p-nBTag0-nPhoton1p-mlgamma85to105-mu" #selectionString="nLepTight1-nLepVeto1-nJet3p-nBTag0-nPhoton1p-mlgamma85to105" #selectionString="nLepTight1-nLepVeto1-nJet3p-nPhoton1p-mlgamma85to105-e" selectionString = "nLepTight1-nLepVeto1-nJet1-nBTag0-nPhoton1p-noGenMatch" #selectionString="nLepTight1-nLepVeto1-nJet3p-nPhoton1p-mlgamma85to105" # Define a reader r = sample.treeReader( \ variables = [ TreeVariable.fromString("event/l"), TreeVariable.fromString('run/i'), TreeVariable.fromString("luminosityBlock/i") ], selectionString = cutInterpreter.cutString(selectionString), ) r.start() #eList = sample.getEventList( selectionString=cutInterpreter.cutString(selectionString) ) #sample.chain.SetEventList(eList) with open("dat/" + selectionString + ".dat", "w") as f: # for i in range(eList.GetN()): # sample.chain.GetEntry(i) # f.write(str(sample.chain.run) + ":" + str(sample.chain.luminosityBlock) + ":" + str(sample.chain.event) + "\n") # r.activateAllBranches() # event_list = ttg.getEventList( ttg.selectionString ) # r.SetEventList( event_list )
elif args.year == 2017: from TTGammaEFT.Samples.nanoTuples_Run2017_14Dec2018_semilep_postProcessed import Run2017 as sample elif args.year == 2018: from TTGammaEFT.Samples.nanoTuples_Run2018_14Dec2018_semilep_postProcessed import Run2018 as sample elif args.year == "RunII": from TTGammaEFT.Samples.nanoTuples_RunII_postProcessed import RunII as sample if True: filterCut = getFilterCut(args.year, isData=True, skipBadChargedCandidate=True, skipVertexFilter=False) selection = "&&".join( [cutInterpreter.cutString(args.selection), filterCut, "triggered==1"]) print selection # Define a reader r = sample.treeReader( \ variables = [ TreeVariable.fromString("event/l"), TreeVariable.fromString('run/i'), TreeVariable.fromString('luminosityBlock/i') ], selectionString = selection, ) r.start() selection = args.selection allEvents = [] while r.run():
[0.4, 0.8, 1.2, 1.6, 2.0, 2.4, 2.8, 3.4], "#DeltaR(l, #gamma)^{fid.}"), ("GenLeptonCMSUnfold0_pt", "GenLeptonCMSUnfold0_pt", [35, 50, 80, 120, 160, 200, 280, 360], "p^{fid.}_{T}(l) [GeV]"), ("nGenBJetCMSUnfold", "nGenBJetCMSUnfold", [0, 1, 2, 3], "N_{b-jet}"), ("nGenJetsCMSUnfold", "nGenJetsCMSUnfold", [0, 1, 2, 3, 4, 5, 6, 7], "N_{jet}"), ] #sel = "nGenLepCMS1-nGenJetCMS3p-nGenBTagCMS1p-nGenPhotonCMS1" #sel = "nGenLepCMS1" #sel = "nGenPhotonCMS1" #sel = "nGenLepCMS1-nGenPhotonCMS1" #sel = "nGenLepCMS1-nGenJetCMS4p-nGenPhotonCMS1" sel = "nGenLepCMS1-nGenJetCMS3p-nGenBTagCMS1p-nGenPhotonCMS1" #sel = "nGenLepCMS1-nGenJetCMS4p-nGenBTagCMS1p-nGenPhotonCMS1" gen_sel = cutInterpreter.cutString(sel) #+"&&overlapRemoval==1" gen_sel_nano = cutInterpreter.cutString(sel) #+"&&overlapRemoval==1" # TTG_P8, # TTG_SemiLep_P8, # TTG_Dilep_P8, # TTG_H7, # TTG_SemiLep_H7, # TTG_Dilep_H7, # TTG_Hpp, # TTG_SemiLep_Hpp, # TTG_Dilep_Hpp, gen_weight = "weight" #*(137.2)" #gen_weight = "weight*(35.92)" #gen_weight = "weight*(41.53)"
for i_year, year in enumerate(settings.years): setup = Setup(year=int(year), photonSelection=False, checkOnly=True, runOnLxPlus=False) setup = setup.sysClone( parameters=allRegions[settings.reco_selection]["parameters"]) # reco selection reco_selection = setup.selection("MC", channel="all", **setup.defaultParameters()) MET_filter_cut = "(year==%s&&" % year + getFilterCut( isData=False, year=int(year), skipBadChargedCandidate=True) + ")" reco_selection_str = MET_filter_cut + "&&triggered&&pTStitching==1&&overlapRemoval==1&&" + cutInterpreter.cutString( reco_selection['prefix']) # fiducial seletion fiducial_selection_str = cutInterpreter.cutString( settings.fiducial_selection ) + "&&pTStitching==1&&overlapRemoval==1" ttreeFormulas = { 'is_fiducial': fiducial_selection_str, 'is_reco': reco_selection_str, 'gen_weight': 'weight*(35.92*(year==2016)+41.53*(year==2017)+59.74*(year==2018))', 'reco_reweight': 'reweightHEM*reweightTrigger*reweightPU*reweightL1Prefire*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightBTag_SF',
"GenPhotonATLASUnfold0_pt/F", "GenPhotonCMSUnfold0_pt/F", "nGenLeptonATLASUnfold/I", "nGenPhotonATLASUnfold/I", "nGenBJetATLASUnfold/I", "nGenJetsATLASUnfold/I", "nGenLeptonCMSUnfold/I", "nGenPhotonCMSUnfold/I", "nGenBJetCMSUnfold/I", "nGenJetsCMSUnfold/I", ] weight_ = lambda event, sample: event.weight * norm Plot.setDefaults(stack=Stack([sample]), weight=staticmethod(weight_), selectionString=cutInterpreter.cutString( args.genSelection)) Plot2D.setDefaults(stack=Stack([sample]), weight=staticmethod(weight_), selectionString=cutInterpreter.cutString( args.genSelection)) unfold2D = Plot2D( name="unfoldingMatrix", texX="p^{gen}_{T}(#gamma) [GeV]", texY="p^{reco}_{T}(#gamma) [GeV]", attribute=( TreeVariable.fromString(args.genPtVariable + "/F"), lambda event, sample: event.PhotonGood0_pt if selection(event, sample) else 0., ), binning=args.genBinning + args.recoBinning,
skipBadChargedCandidate=True) #, skipVertexFilter=True ) if len(args.selection.split("-")) == 1 and args.selection in allRegions.keys(): print("Plotting region from SetupHelpers: %s" % args.selection) setup = Setup( year=args.year, photonSelection=False, checkOnly=False, runOnLxPlus=False) #photonselection always false for qcd estimate setup = setup.sysClone(parameters=allRegions[args.selection]["parameters"]) selection = setup.selection("MC", channel=args.mode, **setup.defaultParameters())["prefix"] selection = cutInterpreter.cutString(selection) if args.addCut: print cutInterpreter.cutString(args.addCut) selection += "&&" + cutInterpreter.cutString(args.addCut) print("Using selection string: %s" % args.selection) else: raise Exception("Region not implemented") selectionString = "&&".join([ filterCutMc, "pTStitching==1", "overlapRemoval==1", "triggered==1", selection ]) for s in mc: s.setSelectionString(selectionString) s.read_variables = read_variables
def calculation(arg): mode, cat, sel = arg TTG = TTG_16 # if mode == "2016": TTG = TTG_16 # elif mode == "2017": TTG = TTG_17 # elif mode == "2018": TTG = TTG_18 triggercut = False filtercut = False overlapcut = False selCut = sel if selCut == "entries": weight = "1" selCut = "all" # elif selCut == "genweight*lumi": # weight = "%f*weight"%lumi_scale # selCut = "all" # elif selCut == "genweight*lumi*SF": # weight = weightString # selCut = "all" else: weight = weightString if "triggerCut" in selCut: selCut = selCut.replace("triggerCut", "") triggercut = True if "METfilter" in selCut: selCut = selCut.replace("METfilter", "") filtercut = True if "overlapRemoval" in selCut: selCut = selCut.replace("overlapRemoval", "") overlapcut = True selCut = "-".join([cut for cut in selCut.split("-") if cut]) if not selCut: selCut = "all" #print("Calcuating yield for mode %s and category %s and selection %s"%(mode,cat,sel)) selCuts = [cutInterpreter.cutString("-".join([selCut, cat]))] filterCutMc = getFilterCut(int(mode), isData=False, skipBadChargedCandidate=True, skipVertexFilter=False) tr = TriggerSelector(int(mode)) triggerCutMc = tr.getSelection() # tr = TriggerSelector( int(mode), singleLepton=True ) # triggerCutMc = tr.getSelection( "MC" ) if triggercut: selCuts += [triggerCutMc] if filtercut: selCuts += [filterCutMc] if overlapcut: selCuts += ["overlapRemoval==1"] selCuts += ["pTStitching==1"] preSelectionSR = "&&".join(selCuts) # if not args.useCorrectedIsoVeto: preSelectionSR = preSelectionSR.replace("nLeptonVetoIsoCorr","nLeptonVeto") if mode != "2016" or (not "hoton" in selCut and cat != noCat_sel): yields[mode][cat][sel] = -1 return print TTG.getEventList(preSelectionSR).GetN() yields[mode][cat][sel] = TTG.getEventList( selectionString=preSelectionSR ).GetN( ) #int(round(TTG.getYieldFromDraw( selectionString=preSelectionSR, weightString=weight )['val']))
data_sample = Run2018 lumi_scale = data_sample.lumi * 0.001 filterCutData = getFilterCut(args.year, isData=True, skipBadChargedCandidate=True) setup = Setup( year=args.year, photonSelection=False, checkOnly=False, runOnLxPlus=False) #photonselection always false for qcd estimate setup = setup.sysClone(parameters=allRegions[args.selection]["parameters"]) selection = setup.selection("MC", channel="all", **setup.defaultParameters())["prefix"] selection = cutInterpreter.cutString(selection) selection += "&&triggered==1" if args.addCut: selection += "&&" + cutInterpreter.cutString(args.addCut) print("Using selection string: %s" % selection) dataB.setSelectionString([ filterCutData, "reweightHEM>0", cutInterpreter.cutString(args.mode), selection ]) dataB.setWeightString("weight") dataC.setSelectionString([ filterCutData, "reweightHEM>0", cutInterpreter.cutString(args.mode), selection ])
]) ttg182l = Sample.fromDirectory( "ttg2l_18", directory=[ os.path.join(data18_dir, name) for name in ["TTGLep_LO", "TTGLep_ptG100To200_LO", "TTGLep_ptG200_LO"] ]) sample = Sample.combine("ttg", [ ttg160l, ttg161l, ttg162l, ttg170l, ttg171l, ttg172l, ttg180l, ttg181l, ttg182l ]) #sample = Sample.combine( "ttg", [ttg160l, ttg161l, ttg162l] ) sel = "nGenPhotonCMS1" selection = cutInterpreter.cutString(sel) selection += "&&pTStitching==1&&overlapRemoval==1" weight = "weight*(35.92*(year==2016)+41.53*(year==2017)+59.74*(year==2018))" print selection print weight xfid = sample.getYieldFromDraw( selectionString=selection, weightString=weight)["val"] / (35.92 + 41.53 + 59.74) print xfid sel = "nPhoton1" selection += "&&" + cutInterpreter.cutString(sel) #weight += "*reweightPhotonElectronVetoSF*reweightPhotonSF" print selection
"photonCatMagic2": "misID", "photonCatMagic3": "fake", "photonCatMagic4": "PU", } filterCutMc = getFilterCut(args.year, isData=False, skipBadChargedCandidate=True, skipVertexFilter=False) if "invl" in args.selection.lower(): trigger = "triggeredInvIso==1" else: trigger = "triggered==1" if args.noOverlap: selection = "&&".join([ cutInterpreter.cutString(args.selection), filterCutMc, trigger, "pTStitching==1" ]) else: selection = "&&".join([ cutInterpreter.cutString(args.selection), filterCutMc, trigger, "pTStitching==1", "overlapRemoval==1" ]) print selection # Define a reader r = sample.treeReader( \ variables = [ TreeVariable.fromString("event/l"), TreeVariable.fromString('run/i'), TreeVariable.fromString('luminosityBlock/i') ], selectionString = selection, )
elif "5" in args.selection: misIDSF_val = misID5SF_val elif "2p" in args.selection: misIDSF_val = misID2pSF_val elif "3p" in args.selection: misIDSF_val = misID3pSF_val elif "4p" in args.selection: misIDSF_val = misID4pSF_val weightString = "%f*weight*reweightHEM*reweightTrigger*reweightL1Prefire*reweightPU*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightBTag_SF" % lumi_scale weightStringAR = "((%s)+(%s*%f*((nPhotonNoChgIsoNoSieie>0)*(PhotonNoChgIsoNoSieie0_photonCatMagic==2))))" % ( weightString, weightString, (misIDSF_val[args.year].val - 1)) mc_mu.setSelectionString( [filterCutMC, "reweightHEM>0", cutInterpreter.cutString("mu")]) mc_mu.setWeightString(weightStringAR) mc_e.setSelectionString( [filterCutMC, "reweightHEM>0", cutInterpreter.cutString("e")]) mc_e.setWeightString(weightStringAR) print "e", mc_e.selectionString print print "mu", mc_mu.selectionString setup = Setup( year=args.year, photonSelection=False, checkOnly=False, runOnLxPlus=False) #photonselection always false for qcd estimate setup = setup.sysClone(parameters=allRegions[args.selection]["parameters"])
mc.read_variables = read_variables_MC if len(args.selection.split("-")) == 1 and args.selection in allRegions.keys(): print("Plotting region from SetupHelpers: %s" % args.selection) setup = Setup( year=args.year, photonSelection=False, checkOnly=False, runOnLxPlus=False) #photonselection always false for qcd estimate setup = setup.sysClone(parameters=allRegions[args.selection]["parameters"]) selection = setup.selection("MC", channel=args.mode, **setup.defaultParameters())["prefix"] selection = cutInterpreter.cutString(selection) selection += "&&triggered==1" if args.addCut: print cutInterpreter.cutString(args.addCut) selection += "&&" + cutInterpreter.cutString(args.addCut) print("Using selection string: %s" % args.selection) lumiString = "(35.92*(year==2016)+41.53*(year==2017)+59.74*(year==2018))" ws = "(%s*weight*reweightHEM*reweightTrigger*reweightL1Prefire*reweightPU*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightBTag_SF)" % lumiString ws16 = "+(%s*(PhotonNoChgIsoNoSieie0_photonCatMagic==2)*(%f-1)*(year==2016))" % ( ws, misIDSF_val[2016].val) ws17 = "+(%s*(PhotonNoChgIsoNoSieie0_photonCatMagic==2)*(%f-1)*(year==2017))" % ( ws, misIDSF_val[2017].val) ws18 = "+(%s*(PhotonNoChgIsoNoSieie0_photonCatMagic==2)*(%f-1)*(year==2018))" % ( ws, misIDSF_val[2018].val) if not addSF:
sample.normalization = 1. sample.reduceFiles(factor=15) eventNorm = 1. / sample.normalization from TTGammaEFT.Analysis.regions import preFiringSumJetEta from TTGammaEFT.Analysis.regions import preFiringSumJetPtLog from TTGammaEFT.Analysis.Region import texString allRegions = [\ ( preFiringSumJetEta, -5, 5 ), ( preFiringSumJetPtLog, 30, 1000 ), ] allModes = ['all', 'mumu', 'mue', 'ee', 'SF'] selection = cutInterpreter.cutString(args.selection) addSelection = "Jet_pt<500&&Jet_pt>30&&Jet_cleanmask&&abs(Jet_eta)<3.5" metFilter = getFilterCut(2017, isData=True, skipBadChargedCandidate=True) ptMinVal = 30 ptMaxVal = 1000 etaMinVal = -5 etaMaxVal = 5 variableString = "Jet_pt:Jet_eta" ptBinning = [ val0 for region in preFiringSumJetPtLog for val0, val1 in region.vals.values() ] + [ptMaxVal] etaBinning = [ val0 for region in preFiringSumJetEta
res["cuts"].append(preselM3Window) return { "cut": "&&".join(res["cuts"]), "prefix": "-".join(res["prefixes"]) } if __name__ == "__main__": setup = Setup(year=2016, private=False) for name, dict in allRegions.items(): # if not "wjetsec3" in name.lower() and not "wjetsbarrel3" in name.lower(): continue if not "sr3p" in name.lower(): continue print print name # dict["parameters"]["invertLepIso"] = False print setup = setup.sysClone(parameters=dict["parameters"]) # setup = setup.sysClone({"selectionModifier":"eResUp"}) for channel in dict["channels"]: print print channel print res = setup.selection( "MC", channel=channel, **setup.defaultParameters(update=dict["parameters"])) print res["cut"] print cutInterpreter.cutString(res["prefix"]) # print res["prefix"] # print res["weightStr"]