# mc = [ TTZtoLLNuNu ] #elif args.background == "All": # mc = [ TTZtoLLNuNu , TTX, WZ, TTW, TZQ, rare ]#, nonprompt ] #else: # mc = [] mc = [] for sample in mc: sample.style = styles.fillStyle(sample.color) # reweighting if args.reweightPtGToSM: sel_string = "&&".join([ getFilterCut(isData=False), getLeptonSelection('all'), cutInterpreter.cutString(args.selection) ]) TTZ_ptG = TTZtoLLNuNu.get1DHistoFromDraw("gamma_pt[0]", [20, 0, 1000], selectionString=sel_string, weightString="weight") TTZ_ptG.Scale(1. / TTZ_ptG.Integral()) def get_reweight(var, histo): def reweight(event, sample): i_bin = histo.FindBin(getattr(event, var)) return histo.GetBinContent(i_bin) return reweight for signal in signals: logger.info("Computing PtG reweighting for signal %s", signal.name)
postfix = "ptZ0To100" s = Sample.fromDirectory( 'TTZ_LO', directory=[ '/afs/hephy.at/data/rschoefbeck02/cmgTuples/TopEFT_PP_v12/inclusive/TTZ_LO' ]) s_BSM = Sample.fromDirectory( 'ttZ0j_ll_DC1A_0p600000_DC1V_m0p240000_DC2A_m0p176700_DC2V_m0p176700', directory=[ '/afs/hephy.at/data/rschoefbeck02/cmgTuples/TopEFT_PP_v12/inclusive/ttZ0j_ll_DC1A_0p600000_DC1V_m0p240000_DC2A_m0p176700_DC2V_m0p176700' ]) h_TTZ_LO = s.get1DHistoFromDraw( 'genZ_cosThetaStar', [20, -1, 1], selectionString=cutInterpreter.cutString('njet3p-btag1p') + '&&genZ_pt<100&&nGenLep>=3', #selectionString = cutInterpreter.cutString('lepSelTTZ-njet3p-btag1p-onZ')+'&&genZ_pt>0', weightString='weight*100') h_BSM = s_BSM.get1DHistoFromDraw( 'genZ_cosThetaStar', [20, -1, 1], selectionString=cutInterpreter.cutString('njet3p-btag1p') + '&&genZ_pt<100&&nGenLep>=3', #selectionString = cutInterpreter.cutString('lepSelTTZ-njet3p-btag1p-onZ')+'&&genZ_pt>0', weightString='weight*100') h_BSM.Scale(h_TTZ_LO.Integral() / h_BSM.Integral()) ZPolarisationFit(
h.Scale( 1./s ) return templates if __name__ == "__main__": # Sample s = Sample.fromDirectory( 'TTZ_LO', directory = ['/afs/hephy.at/data/rschoefbeck02/cmgTuples/TopEFT_PP_v12/inclusive/TTZ_LO'] ) t_maker = PolarisationTemplateMaker(s, cacheDir = os.path.join( results_directory, 'PolarisationTemplateCache' ) ) # polarisation bin -> will be argument genZ_pt_bin = (0,25) genZ_eta_bin = (0, -1) # kinematic preselection cut_string = cutInterpreter.cutString('njet3p-btag1p') kin_cut_string = "genZ_pt>=%i" % genZ_pt_bin[0] if genZ_pt_bin[1]>0: kin_cut_string+="&&genZ_pt<%i"%( genZ_pt_bin[1] ) kin_cut_string+= "&&genZ_eta>=%i" % genZ_eta_bin[0] if genZ_eta_bin[1]>0: kin_cut_string+="&&genZ_eta<%i"%( genZ_eta_bin[1] ) selectionString = kin_cut_string+"&&"+cut_string templates = t_maker.cachedTemplate( selectionString, 'weight') templates['p'].legendText = '+' templates['m'].legendText = '-' templates['L'].legendText = 'L' templates['p'].style = styles.lineStyle( ROOT.kRed ) templates['m'].style = styles.lineStyle( ROOT.kGreen ) templates['L'].style = styles.lineStyle( ROOT.kBlue )
sample.read_variables = ['reweightBTagCSVv2_SF/F', 'reweightBTagDeepCSV_SF/F', 'reweightPU36fb/F', 'reweightLeptonSFSyst_tight_3l/F', 'reweightLeptonTrackingSF_tight_3l/F', 'reweightTrigger_tight_3l/F', "Z_pt/F"] sample.weight = lambda event, sample: event.reweightBTagDeepCSV_SF*event.reweightPU36fb*event.reweightLeptonSFSyst_tight_3l*event.reweightLeptonTrackingSF_tight_3l*event.reweightTrigger_tight_3l tr = triggerSelector(2016) sample.setSelectionString([getFilterCut(isData=False), getLeptonSelection(mode), tr.getSelection("MC")]) if not args.noData: stack = Stack(mc, data_sample) else: stack = Stack(mc) if args.small: for sample in stack.samples: sample.reduceFiles( to = 1 ) # Use some defaults Plot.setDefaults(stack = stack, weight = staticmethod(weight_), selectionString = cutInterpreter.cutString(args.selection), addOverFlowBin='upper') plots = [] plots.append(Plot( name = 'yield', texX = 'yield', texY = 'Number of Events', attribute = lambda event, sample: 0.5 + index, binning=[4, 0, 4], )) plots.append(Plot( name = 'flavor', texX = '', texY = 'Number of Events', attribute = lambda event, sample: 0.5 + event.flavor_bin, binning=[6, 0, 6], ))
h_target.Divide(h_source) template = h_target return template if __name__ == "__main__": # reweighting class cacheDir = os.path.join(results_directory, 'WZReweightingTemplate') if not os.path.isdir(cacheDir): os.makedirs(cacheDir) WZReweighting = WZReweighting(cacheDir=cacheDir) # reweighting selection selection = cutInterpreter.cutString('trilep-Zcand-onZ-lepSelTTZ-njet1p') # reweighting function f = WZReweighting.cachedReweightingFunc(selection, weight='weight') # plot the reweighting matrix if args.makePlots: histo = WZReweighting.cachedTemplate(selection, weight='weight', overwrite=False) histo.style = styles.lineStyle(ROOT.kOrange + 3, errors=True) histoPlot = Plot.fromHisto(target.name, texY="a.u.", texX="p_{T}(Z) (GeV)", histos=[[histo]])
texName="t#bar{t}Z", directory=directories['TTZ']) TTZToLLNuNu = Sample.fromDirectory(name="TTZToLLNuNu", treeName="Events", isData=False, color=color.TTZ, texName="t#bar{t}Z(ll, #nu#nu)", directory=directories['TTZToLLNuNu']) TTZToQQ = Sample.fromDirectory(name="TTZToQQ", treeName="Events", isData=False, color=color.WZ, texName="t#bar{t}Z(qq)", directory=directories['TTZToQQ']) dilepSelection = cutInterpreter.cutString( 'lepSelDilepSUSY-njet3p-btag1p-mt2ll100-met80') dilepSelection += '&&nlep==2&&nLeptons_tight_3l==2&&((nElectrons_tight_3l==1&&nMuons_tight_3l==1)||(nElectrons_tight_3l==2&&abs(Z_mass-91.2)>10)||(nMuons_tight_3l==2&&abs(Z_mass-91.2)>10))&&genZ_pt>=0' #dilepSelection = cutInterpreter.cutString('njet3p-btag1p') + '&&genZ_pt>=0' #dilepSelection += '&&(abs(genZ_daughter_flavor)==12||abs(genZ_daughter_flavor)==14||abs(genZ_daughter_flavor)==16)' invisibleSelection = dilepSelection + '&&(abs(genZ_daughter_flavor)==12||abs(genZ_daughter_flavor)==14||abs(genZ_daughter_flavor)==16)' leptonicSelection = dilepSelection + '&&(abs(genZ_daughter_flavor)==11||abs(genZ_daughter_flavor)==13)' tauSelection = dilepSelection + '&&(abs(genZ_daughter_flavor)==15)' hadronicSelection = dilepSelection + '&&(abs(genZ_daughter_flavor)<7)' selection = "&&".join([dilepSelection]) ## Sequence read_variables = [ "weight/F", "jet[pt/F,eta/F,phi/F,btagCSV/F,DFb/F,DFbb/F,id/I,btagDeepCSV/F]",
for dir in dirs[key] ] for key in dirs.keys() } DYJetsToLL_M50_LO_ext_comb = Sample.fromDirectory( name="DYJetsToLL_M50_LO_ext_comb", treeName="Events", isData=False, color=color.TTJets, texName="DY (LO)", directory=directories['DYJetsToLL_M50_LO_ext_comb']) ## selections and plotting selection = cutInterpreter.cutString( 'trilep-Zcand-lepSelTTZ-njet3p-btag1p-onZ') WZselection = cutInterpreter.cutString( 'trilep-Zcand-lepSelTTZ-njet2p-btag2p-onZ') DYselection = cutInterpreter.cutString('njet2p-btag2p-onZ') DYselection += "&&Sum$(lep_pt>40&&lep_tight>0)>0 && Sum$(lep_pt>20&&lep_tight>0)>1 " #&& Sum$(jet_pt>30&&abs(jet_eta)<2.4&&jet_id>0&&jet_btagDeepCSV>0.6324&&jet_hadronFlavour==5)>=2" trueVar = "jet_hadronFlavour" #trueVar = "jet_mcFlavour" #oneTrueB = "Sum$(jet_btagDeepCSV>0.6324&&abs(%s)==5)==1" #moreTrueB = "Sum$(jet_btagDeepCSV>0.6324&&abs(%s)==5)>1" oneTrueB = "Sum$(abs(%s)==5)==1" % trueVar moreTrueB = "Sum$(abs(%s)==5)>1" % trueVar trueCs = "Sum$(abs(%s)==4)>=1" % trueVar onlyTrueB = "(Sum$(jet_btagDeepCSV>0.6324)==Sum$(jet_btagDeepCSV>0.6324&&abs(%s)==5))" % trueVar
offZ2 = "&&abs(Z2_mass_4l-91.2)>20" if args.selection.count("offZ2") else "" offZ2 = "&&met_pt > 80" if args.selection.count("offZ2met") else offZ2 print offZ2 def getLeptonSelection( mode ): if mode=="mumumumu": return "nMuons_tight_4l==4&&nElectrons_tight_4l==0" + offZ2 elif mode=="mumumue": return "nMuons_tight_4l==3&&nElectrons_tight_4l==1" elif mode=="mumuee": return "nMuons_tight_4l==2&&nElectrons_tight_4l==2" + offZ2 elif mode=="mueee": return "nMuons_tight_4l==1&&nElectrons_tight_4l==3" elif mode=="eeee": return "nMuons_tight_4l==0&&nElectrons_tight_4l==4" + offZ2 elif mode=='all': return "nMuons_tight_4l+nElectrons_tight_4l==4" # reweighting if args.reweightPtZToSM: sel_string = "&&".join([getFilterCut(isData=False), getLeptonSelection('all'), cutInterpreter.cutString(args.selection)]) TTZ_ptZ = TTZtoLLNuNu.get1DHistoFromDraw("Z_pt", [20,0,1000], selectionString = sel_string, weightString="weight") TTZ_ptZ.Scale(1./TTZ_ptZ.Integral()) def get_reweight( var, histo ): def reweight(event, sample): i_bin = histo.FindBin(getattr( event, var ) ) return histo.GetBinContent(i_bin) return reweight for signal in signals: logger.info( "Computing PtZ reweighting for signal %s", signal.name ) signal_ptZ = signal.get1DHistoFromDraw("Z_pt", [20,0,1000], selectionString = sel_string, weightString="weight") signal_ptZ.Scale(1./signal_ptZ.Integral())
dl_mt2ll_Z_smear = mt2Calc.mt2ll() event.newMet_pt = newMet.Pt() event.newMet_phi = newMet.Phi() event.newMetSmeared_pt = newMetSmear.Pt() event.dl_mt2ll_Z = dl_mt2ll_Z event.dl_mt2ll_Z_smear = dl_mt2ll_Z_smear event.dPhi_nonZ_l1_newMet = deltaPhi(newMet.Phi(), l1.Phi()) event.dPhi_nonZ_l2_newMet = deltaPhi(newMet.Phi(), l2.Phi()) return 1#newMet.Pt(), newMetSmear.Pt(), dl_mt2ll_Z, dl_mt2ll_Z_smear, newMet.Phi() quadlepSelection = cutInterpreter.cutString('quadlep-lepSelQuad-njet1p-btag1p-onZ1-offZ2-min_mll12') quadlepSelection += "&&lep_pt[nonZ1_l1_index_4l]>40&&lep_pt[nonZ1_l2_index_4l]>20" #dilepSelection = cutInterpreter.cutString('dilepOS-njet3p-btag1p-offZ') # almost analysis like, no met cuts #dilepSelection += '&&(abs(lep_pdgId[0])!=(lep_pdgId[1]))' dilepSelection = cutInterpreter.cutString('lepSelDY-njet3p-btag1p') dilepSelection += '&&nlep==2&&(nElectrons_tight_4l==1&&nMuons_tight_4l==1)' dilepSelection += '&&(abs(genZ_daughter_flavor)==12||abs(genZ_daughter_flavor)==14||abs(genZ_daughter_flavor)==16)' #dilepSelection = 'nlep==2&&(abs(lep_pdgId[0])!=(lep_pdgId[1]))' sample = TTZToLLNuNu dilepS = copy.deepcopy(TTZToLLNuNu) quadlepS= copy.deepcopy(TTZToLLNuNu) dilep = copy.deepcopy(TTZToLLNuNu) quadlep = copy.deepcopy(TTZToLLNuNu)
#sample = TTLep_pow_17 if args.year == 2017 else TTLep_pow # Trigger from TopEFT.Tools.triggerSelector import triggerSelector tr = triggerSelector(args.year) # Event selection nonpromptSelection = "nLeptons_FO_3l>=3 && Sum$((lep_tight_3l*(lep_pt - lep_ptCorr) + lep_ptCorr)>40&&lep_FO_3l>0)>0&&Sum$((lep_tight_3l*(lep_pt - lep_ptCorr) + lep_ptCorr)>20&&lep_FO_3l>0)>1&&Sum$((lep_tight_3l*(lep_pt - lep_ptCorr) + lep_ptCorr)>10&&lep_FO_3l>0)>2 && !(nLeptons_tight_4l>=4) && nLeptons_tight_3l<3" promptSelection = "nLeptons_tight_3l==3 && nLeptons_FO_3l_genPrompt<=2 && Sum$(lep_pt>40&&lep_tight_3l>0)>0&&Sum$(lep_pt>20&&lep_tight_3l>0)>1&&Sum$(lep_pt>10&&lep_tight_3l>0)>2 && !(nLeptons_tight_4l>=4)" #np_sel_string = "&&".join([getFilterCut(isData=False, year=args.year), tr.getSelection("MC"), nonpromptSelection, cutInterpreter.cutString(args.selection), "abs(Z_mass - 91.2)<10"]) #sel_string = "&&".join([getFilterCut(isData=False, year=args.year), tr.getSelection("MC"), promptSelection, cutInterpreter.cutString(args.selection), "abs(Z_mass - 91.2)<10 && Z_fromTight>0"]) np_sel_string = "&&".join([ getFilterCut(isData=False, year=args.year), tr.getSelection("MC"), nonpromptSelection, cutInterpreter.cutString(args.selection) ]) sel_string = "&&".join([ getFilterCut(isData=False, year=args.year), tr.getSelection("MC"), promptSelection, cutInterpreter.cutString(args.selection) ]) # weights weight_central = "weight*reweightPU36fb*reweightBTagDeepCSV_SF*41.2" # preparation for looper loose_ID = "FO_3l" tight_ID = "tight_3l" nLeptons = 3
] sample.read_variables += ["nBTag_%s/I" % s for s in jet_systematics] sample.setSelectionString( [getFilterCut(isData=False), getLeptonSelection(mode)]) for s in signals: s.scale = lumi_scale #s.read_variables = ['reweightBTagCSVv2_SF/F','reweightPU36fb/F','nTrueInt/F'] #s.weight = lambda event, sample: event.reweightLeptonSF*event.reweightDilepTriggerBackup*event.reweightPU36fb s.setSelectionString( [getFilterCut(isData=False), getLeptonSelection(mode)]) # Use some defaults Plot.setDefaults(selectionString=cutInterpreter.cutString(args.selection)) stack_mc = Stack(mc) stack_signal = Stack(mc[1:] + [dipole1]) if args.signal == "ewkDM": stack_data = Stack(data_sample, signals) else: stack_data = Stack(data_sample) sys_stacks = { sys: copy.deepcopy(stack_mc) for sys in [None] + weight_systematics + jme_systematics } plots = [] nBtagBinning = [5, 1, 6] if args.selection.count('btagM') else [1, 0, 1] nbtags_data = Plot(
} sample = Sample.fromDirectory(name="WZTo3LNu_amcatnlo", treeName="Events", isData=False, color=color.TTJets, texName="WZ MG (NLO)", directory=directories['WZTo3LNu_amcatnlo']) sample2 = Sample.fromDirectory(name="WZTo3LNu_powheg", treeName="Events", isData=False, color=color.TTJets, texName="WZ powheg (NLO)", directory=directories['WZTo3LNu']) selection = cutInterpreter.cutString( 'trilep-Zcand-lepSelTTZ-njet3p-btag1p-onZ') WZselection = cutInterpreter.cutString( 'trilep-looseVeto-Zcand-lepSelTTZ-njet1p-btag0-onZ') WZselection = cutInterpreter.cutString('trilep-Zcand-onZ-lepSelTTZ-njet1p') selection = WZselection weight_central = "weight" weight_reweight = "weight * reweightTopPt" # binning binning = [25, 0, 500] binning = [10, 0, 500] binningCTS = [10, -1, 1] # LO
elif mode=='all': return "nGoodMuons+nGoodElectrons==2" ## backgrounds / mc #if args.background == "OnlyTTZ": # mc = [ TTZtoLLNuNu ] #elif args.background == "All": # mc = [ TTZtoLLNuNu , TTX, WZ, TTW, TZQ, rare ]#, nonprompt ] #else: # mc = [] mc = [] for sample in mc: sample.style = styles.fillStyle(sample.color) # reweighting if args.reweightPtGToSM: sel_string = "&&".join([getFilterCut(isData=False), getLeptonSelection('all'), cutInterpreter.cutString(args.selection)]) TTG_ptG = ttGamma0j_ll.get1DHistoFromDraw("photon_pt", [20,0,1000], selectionString = sel_string, weightString="weight") TTG_ptG.Scale(1./TTG_ptG.Integral()) def get_reweight( var, histo ): def reweight(event, sample): i_bin = histo.FindBin(getattr( event, var ) ) return histo.GetBinContent(i_bin) return reweight for signal in signals: logger.info( "Computing PtG reweighting for signal %s", signal.name ) signal_ptG = signal.get1DHistoFromDraw("photon_pt", [20,0,1000], selectionString = sel_string, weightString="weight") signal_ptG.Scale(1./signal_ptG.Integral())
# reco signals postProcessing_directory = "TopEFT_PP_v19/trilep/" data_directory = "/afs/hephy.at/data/rschoefbeck02/cmgTuples/" from TopEFT.samples.cmgTuples_ttZ0j_Summer16_mAODv2_postProcessed import * source_reco = ttZ0j_ll target_reco = ttZ0j_ll_DC1A_0p600000_DC1V_m0p240000_DC2A_0p176700_DC2V_0p176700 #target_reco = ttZ0j_ll_DC2A_0p200000_DC2V_0p200000 #target_reco = ttZ0j_ll_DC1A_0p500000_DC1V_0p500000 #target_reco = ttZ0j_ll_DC1A_1p000000 #target_reco = ttZ0j_ll_DC1A_0p600000_DC1V_m0p240000_DC2V_m0p250000 # cutInterpreter & selectionString from TopEFT.Tools.cutInterpreter import cutInterpreter reco_selection = cutInterpreter.cutString( 'lepSelTTZ-njet3p-btag1p-onZ') logger.debug("Setting selectionString %s for reco samples", reco_selection) source_reco.setSelectionString(reco_selection) target_reco.setSelectionString(reco_selection) # Histos pt_bins = [(0, 100), (100, 200), (200, -1), (-1, -1)] h_pt = { s: ROOT.TH1F('h_pt', 'h_pt', 10, 0, 500) for s in ['source', 'target', 'source_reweighted'] } h_pt['source'].legendText = "SM (reco)" h_pt['source'].style = styles.lineStyle(ROOT.kBlue, errors=True,
os.path.join(data_directory, postProcessing_directory, dir) for dir in dirs[key] ] for key in dirs.keys() } # Define samples TTZ = Sample.fromDirectory(name="TTZ", treeName="Events", isData=False, color=color.TTZ, texName="t#bar{t}Z,Z to inv.", directory=directories['TTZ']) quadlepSelection = cutInterpreter.cutString( 'quadlep-lepSelQuad-njet1p-btag1p-onZ1-offZ2-min_mll12' ) # offZ2 in cutinterpreter doesn't do anything quadlepSelection += "&&lep_pt[nonZ1_l1_index_4l]>40&&lep_pt[nonZ1_l2_index_4l]>20" dilepSelection = cutInterpreter.cutString('lepSelDY-njet3p-btag1p') dilepSelection += '&&nlep==2&&nLeptons_tight_4l==2&&(nElectrons_tight_4l==1&&nMuons_tight_4l==1)' #dilepSelection += '&&nlep==2&&nLeptons_tight_4l==2&&((nElectrons_tight_4l==1&&nMuons_tight_4l==1)||(nElectrons_tight_4l==2&&abs(Z1_mass_4l-91.2)>10)||(nMuons_tight_4l==2&&abs(Z1_mass_4l-91.2)>10))' dilepSelection += '&&genZ_pt>=0' #dilepSelection += '&&(abs(genZ_daughter_flavor)==12||abs(genZ_daughter_flavor)==14||abs(genZ_daughter_flavor)==16)' invisibleSelection = '(abs(genZ_daughter_flavor)==12||abs(genZ_daughter_flavor)==14||abs(genZ_daughter_flavor)==16)' leptonicSelection = '(abs(genZ_daughter_flavor)==11||abs(genZ_daughter_flavor)==13)' tauSelection = '(abs(genZ_daughter_flavor)==15)' hadronicSelection = '(abs(genZ_daughter_flavor)<7)'
h_err.GetBinLowEdge(i_binx), central * (1 - relerr), h_err.GetBinLowEdge(i_binx) + h_err.GetBinWidth(i_binx), central * (1 + relerr))) result.append(boxes) return result if __name__ == '__main__': from TopEFT.samples.cmgTuples_Summer16_mAODv2_postProcessed import * from TopEFT.samples.cmgTuples_signals_Summer16_mAODv2_postProcessed import * from TopEFT.Tools.cutInterpreter import cutInterpreter selection = "nGoodMuons+nGoodElectrons==3&&" + cutInterpreter.cutString( "lepSelTTZ-njet3p-btag1p-onZ") #weightString = "(1)" weightString = "weight*36.9" binning = [3, 0, 600] bsm = getTheorySysFromChain(ewkDM_ttZ_ll_DC2A_0p20_DC2V_0p20.chain, 'Z_pt', binning, selection, weightString) res = getTheorySysFromChain(TTZtoLLNuNu.chain, 'Z_pt', binning, selection, weightString) bsm['h_nominal'].Scale(res['h_nominal'].Integral() / bsm['h_nominal'].Integral()) #c1 = ROOT.TCanvas() #res['h_nominal'].Draw()