diff --git a/.gitignore b/.gitignore index a5e0c25b..901bd4c6 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,9 @@ docs/build/* callgrind.out.* *.orig *.rej +*.*~ +*.out +*_setup.py +*Tree.pyx +*Tree.cpp +#*.*# diff --git a/dependencies/FinalStateAnalysis b/dependencies/FinalStateAnalysis deleted file mode 160000 index 6334928c..00000000 --- a/dependencies/FinalStateAnalysis +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6334928c5c20c98f4f8cca36ed36628a5342ee3f diff --git a/dependencies/FinalStateAnalysis b/dependencies/FinalStateAnalysis new file mode 120000 index 00000000..344025ae --- /dev/null +++ b/dependencies/FinalStateAnalysis @@ -0,0 +1 @@ +../../FinalStateAnalysis \ No newline at end of file diff --git a/environment.sh b/environment.sh index 455d25ae..b88c1d5b 100644 --- a/environment.sh +++ b/environment.sh @@ -12,9 +12,11 @@ if [ -z $CONDOR_ID ]; then fi #Is the analysis blinded? -export blind='YES' +export blind='NO' #'YES' export TARGET_LUMI_8TeV=19.4 export TARGET_LUMI_7TeV=4.9 +export MEGAPATH='/scratch/mverzett/data':$hdfs + #check if dev area is up to date check_git_updates.sh diff --git a/lfvetau/BasePlotter.py b/lfvetau/BasePlotter.py new file mode 100644 index 00000000..11e6ad03 --- /dev/null +++ b/lfvetau/BasePlotter.py @@ -0,0 +1,1712 @@ +''' + +Base class which makes nice plots. + +Author: Evan K. Friis, UW + +''' + +import fnmatch +import re +import os +import math +import rootpy.plotting.views as views +from pdb import set_trace +from rootpy.plotting.hist import HistStack +import rootpy.plotting as plotting +from FinalStateAnalysis.MetaData.data_views import data_views +from FinalStateAnalysis.PlotTools.RebinView import RebinView +from FinalStateAnalysis.PlotTools.BlindView import BlindView, blind_in_range +from FinalStateAnalysis.Utilities.struct import struct +import FinalStateAnalysis.Utilities.prettyjson as prettyjson +from FinalStateAnalysis.MetaData.data_styles import data_styles +from FinalStateAnalysis.PlotTools.Plotter import Plotter +from FinalStateAnalysis.PlotTools.SubtractionView import SubtractionView, PositiveView +from FinalStateAnalysis.PlotTools.MedianView import MedianView +from FinalStateAnalysis.PlotTools.SystematicsView import SystematicsView +from FinalStateAnalysis.StatTools.quad import quad +import ROOT +import glob +from pdb import set_trace +from FinalStateAnalysis.PlotTools.THBin import zipBins + +def create_mapper(mapping): + def _f(path): + for key, out in mapping.iteritems(): + if key == path: + path = path.replace(key,out) + print 'path', path + return path + return _f + +def remove_name_entry(dictionary): + return dict( [ i for i in dictionary.iteritems() if i[0] != 'name'] ) + +def histo_diff_quad(mc_err, *systematics): + nbins = mc_err.GetNbinsX() + clone = mc_err.Clone() + sys_up = [i for i, _ in systematics] + sys_dw = [i for _, i in systematics] + #bin loop + for ibin in range(nbins+2): #from uflow to oflow + content = clone.GetBinContent(ibin) + error = clone.GetBinError(ibin) + + shifts_up = [abs(i.GetBinContent(ibin) - content) for i in sys_up] + shifts_dw = [abs(i.GetBinContent(ibin) - content) for i in sys_dw] + max_shift = [max(i, j) for i, j in zip(shifts_up, shifts_dw)] + #print shifts_up, shifts_dw, max_shift, error, content + + new_err = quad(error, *max_shift) + #print clone.GetTitle(), ibin, new_err, clone.GetBinContent(ibin) + clone.SetBinError(ibin, new_err) + + return clone + +def mean(histo): + '''compute histogram mean because root is not able to''' + nbins = histo.GetNbinsX() + wsum = sum( histo.GetBinCenter(i)*histo.GetBinContent(i) for i in xrange(1, nbins+1)) + entries = sum(histo.GetBinContent(i) for i in xrange(1, nbins+1)) + #print histo.GetTitle(), wsum, entries, nbins, histo.GetEntries(), histo.Integral() + return float(wsum)/entries + +def name_systematic(name): + '''makes functor that makes a name systematic (with postfix)''' + return lambda x: x+name + +def dir_systematic(name): + '''makes functor that makes a directory systematic''' + return lambda x: os.path.join(name,x) + +def parse_cgs_groups(file_path): + if not os.path.isfile(file_path): + raise NameError('%s is not a file!' % file_path) + + groups = {} + regex = re.compile('$ GROUP (?P\w+) (?P[a-zA-Z\_\, ]+)') + with open(file_path) as infile: + for line in infile: + match = regex.match(line) + if match: + groups[match.group('groupname')] = [ i.strip() for i in match.group('includes').split(',') ] + return groups + +def remove_empty_bins(histogram, weight, first = 0, last = 0): + ret = histogram.Clone() + last = last if last else ret.GetNbinsX() + 1 + for i in range(first, last+1): + if ret.GetBinContent(i) <= 0: + ret.SetBinContent(i, 0.9200*weight) #MEAN WEIGHT + ret.SetBinError(i, 1.8*weight) + return ret + +def change_histo_nbins(histogram, first = 0, last = 0): + nbins = int((last - first)/histogram.GetBinWidth(2)) + if nbins == histogram.GetNbinsX(): + return histogram + else: + name = "" + title ="" + name = histogram.GetName() + title = histogram.GetTitle() + newH = ROOT.TH1F(name, title, nbins , first, last) + for i in range (1, nbins+1): + newH.SetBinContent(i, histogram.GetBinContent(i)) + newH.SetBinError(i, histogram.GetBinError(i)) + return newH + +def find_fill_range(histo): + first, last = (0, 0) + for i in range(histo.GetNbinsX() + 1): + if histo.GetBinContent(i) > 0: + if first: + last = i + else: + first = i + return first, last + +class BasePlotter(Plotter): + def __init__ (self, blind_region=None, forceLumi=-1, use_embedded=False): + cwd = os.getcwd() + self.period = '8TeV' + self.sqrts = 8 + jobid = os.environ['jobid'] + self.use_embedded = use_embedded + print "\nPlotting e tau for %s\n" % jobid + + files = glob.glob('results/%s/LFVHETauAnalyzerMVA/*.root' % jobid) + #files = glob.glob('results/%s/ControlRegionZee_invertedAntiE/*.root' % jobid) + #files = glob.glob('results/%s/EmbeddedCheck/*.root' % jobid) + #files = glob.glob('results/%s/TTbarControlRegion/*.root' % jobid) + lumifiles = glob.glob('inputs/%s/*.lumicalc.sum' % jobid) + + #outputdir = 'plots/%s/ControlRegionZee_invertedAntiE' % jobid + outputdir = 'plots/%s/lfvet' % jobid + #outputdir = 'plots/%s/EmbeddedCheck' % jobid + #outputdir = 'plots/%s/TTBar' % jobid + if not os.path.exists(outputdir): + os.makedirs(outputdir) + + samples = [os.path.basename(i).split('.')[0] for i in files] + + self.blind_region=blind_region + blinder=None + if self.blind_region: + # Don't look at the SS all pass region + blinder = lambda x: BlindView(x, "os/.*ass*",blind_in_range(*self.blind_region)) + + super(BasePlotter, self).__init__(files, lumifiles, outputdir, blinder, forceLumi=forceLumi) + + self.mc_samples = [ + 'ggH[tWB][tWB]', + 'vbfH[tWB][tWB]', + #'GluGluToHToTauTau_M-125*', + #'VBF_HToTauTau_M-125*', + 'TTJets*', + 'T*_t*', + '[WZ][WZ]Jets', + #'Wplus*Jets_madgraph*', #superseded by fakes #add in case of optimization study + 'Z*jets_M50_skimmedLL', + #'WH*HToTauTau', + #'WH*HToWW', + ##'vbfHWW', + ##'ggHWW', + 'Z*jets_M50_skimmedTT' + + ] + + if use_embedded: + self.mc_samples.pop() + embedded_view, weight = self.make_embedded('os/gg/ept30/h_collmass_pfmet') + self.views['ZetauEmbedded'] = { + 'view' : embedded_view, + 'weight' : weight + } + self.views['fakes'] = {'view' : self.make_fakes('t')} # comment in case of optimization study + #self.views['efakes'] = {'view' : self.make_fakes('e')} + #self.views['etfakes'] = {'view' : self.make_fakes('et')} + + #names must match with what defined in self.mc_samples + self.datacard_names = { + ##'GluGluToHToTauTau_M-125*' : 'SMGG126' , + ##'VBF_HToTauTau_M-125*' : 'SMVBF126' , + 'ggH[tWB][tWB]' : 'SMGG126' , + 'vbfH[tWB][tWB]' : 'SMVBF126' , + 'TTJets*' : 'ttbar' , + 'T*_t*' : 'singlet' , + '[WZ][WZ]Jets' : 'diboson' , + 'Z*jets_M50_skimmedTT' : 'ztautau' , + 'ZetauEmbedded' : 'ztautau' , + 'Z*jets_M50_skimmedLL' : 'zjetsother', + #'missing1' : 'WWVBF126', + #'missing2' : 'WWGG126', + ##'vbfHWW' : 'WWVBF126', + ##'ggHWW' : 'WWGG126', + ##'vbfHBB' : 'BBVBF126', + ##'ggHBB' : 'BBGG126', + 'ggHiggsToETau' : 'LFVGG', + 'vbfHiggsToETau' : 'LFVVBF', +# 'Wplus*Jets_madgraph*' : 'wplusjets'#add in case of optimization study + 'fakes' : 'fakes', + #'WH*HToTauTau' : 'VHtautau' , #"VHtautau", + #'WH*HToWW' : 'VHWW' , #"VHWW", + ##'efakes' : 'efakes', + ##'etfakes' : 'etfakes' + } + + self.sample_groups = {#parse_cgs_groups('card_config/cgs.0.conf') + 'fullsimbkg' : ['SMGG126', 'SMVBF126','LFVGG', 'LFVVBF','ttbar', 'singlet', + 'diboson', 'zjetsother'],# 'WWVBF126', 'WWGG126','VHWW','VHtautau'], #wplusjets added in case of optimization study# 'wplusjets'], + 'simbkg' : ['SMGG126', 'SMVBF126', 'LFVGG', 'LFVVBF', 'ttbar', 'singlet', 'ztautau', + 'diboson', 'zjetsother'],#, 'WWVBF126', 'WWGG126','VHWW','VHtautau'],# 'wplusjets'], + 'realtau' : ['diboson', 'ttbar', 'singlet', 'ztautau', 'SMGG126', 'SMVBF126','LFVGG', 'LFVVBF'],#, 'VHtautau'], + 'Zee' : ['zjetsother'] + } + + self.systematics = { + 'PU_Uncertainty' : { + 'type' : 'yield', + '+' : dir_systematic('p1s'), + '-' : dir_systematic('m1s'), + 'apply_to' : ['fullsimbkg'], + }, + 'E_Trig' : { + 'type' : 'yield', + '+' : dir_systematic('trp1s'), + '-' : dir_systematic('trm1s'), + 'apply_to' : ['simbkg'], + }, + ##'E_ID' : { ## to comment in case of optimization study + ## 'type' : 'yield', + ## '+' : dir_systematic('eidp1s'), + ## '-' : dir_systematic('eidm1s'), + ## 'apply_to' : ['simbkg'], + ##}, + ##'E_Iso' : { ## to comment in case of optimization study + ## 'type' : 'yield', + ## '+' : dir_systematic('eisop1s'), + ## '-' : dir_systematic('eisom1s'), + ## 'apply_to' : ['simbkg'], + ##}, + 'JES' : { + 'type' : 'yield', + '+' : lambda x: os.path.join('jes_plus', x)+'_jes_plus' , + '-' : lambda x: os.path.join('jes_minus', x)+'_jes_minus' , + 'apply_to' : ['fullsimbkg'], + }, + 'TES' : { + 'type' : 'shape', + '+' : lambda x: os.path.join('tes_plus', x)+'_tes_plus' , + '-' : lambda x: os.path.join('tes_minus', x)+'_tes_minus' , + 'apply_to' : ['realtau'], + }, + ##'EES' : { + ## 'type' : 'shape', + ## '+' : lambda x: os.path.join('ees_plus', x) +'_ees_plus' , + ## '-' : lambda x: os.path.join('ees_minus', x)+'_ees_minus' , + ## 'apply_to' : ['simbkg'], + ##}, + 'UES' : { ## to comment in case of optimization study + 'type' : 'yield', + '+' : name_systematic('_ues_plus'), + '-' : name_systematic('_ues_minus'), + 'apply_to' : ['fullsimbkg'], + }, + 'shape_FAKES' : { ## to comment in case of optimization study + 'type' : 'shape', + '+' : dir_systematic('Up'), + '-' : dir_systematic('Down'), + 'apply_to' : ['fakes']#,'efakes','etfakes'], + }, + 'norm_etaufake' : { ## was shape etaufake + 'type' : 'yield', + '+' : dir_systematic('etaufakep1s'), + '-' : dir_systematic('etaufakem1s'), + 'apply_to' : ['Zee']#,'efakes','etfakes'], + }, + 'shape_ZeeMassShift' : { ## to comment in case of optimization study + 'type' : 'shape', + '+' : name_systematic('_Zee_p1s'), + '-' : name_systematic('_Zee_m1s'), + 'apply_to' : ['Zee']#,'efakes','etfakes'], + }, + ##'stat' : { + ## 'type' : 'stat', + ## '+' : lambda x: x, + ## '-' : lambda x: x, + ## 'apply_to' : ['fakes','simbkg'], + ## #'apply_to' : [ 'simbkg'],#['fakes','simbkg'], ## no fakes in case of optimization study + ##} + } + + + def make_fakes(self, obj='t'): + '''Sets up the fakes view''' + print 'making fakes for %s' %obj + data_view = self.get_view('data') + tfakes = views.SubdirectoryView(data_view, 'tLoose') + efakes = views.SubdirectoryView(data_view, 'eLoose') + etfakes= views.SubdirectoryView(data_view, 'etLoose') + central_fakes=SubtractionView(views.SumView(tfakes,efakes),etfakes, restrict_positive=True) + mc_views = self.mc_views() + if self.use_embedded: + mc_views.append(self.get_view('ZetauEmbedded')) + mc_sum = views.SumView(*mc_views) + mc_sum_t = views.SubdirectoryView(mc_sum, 'tLoose') + mc_sum_e = views.SubdirectoryView(mc_sum, 'eLoose') + mc_sum_et = views.SubdirectoryView(mc_sum, 'etLoose') + allmc=SubtractionView(views.SumView(mc_sum_t,mc_sum_e),mc_sum_et, restrict_positive=True) + + fakes_view = SubtractionView(central_fakes, allmc, restrict_positive=True) + #fakes_view =central_fakes + style = data_styles['Fakes*'] + return views.TitleView( + views.StyleView( + fakes_view, + **remove_name_entry(style) + ), + style['name'] + ) + #MedianView(highv=up_fakes, lowv=dw_fakes, centv=central_fakes, maxdiff=True), + + def make_embedded(self, normalization_path): + '''Configures the embedded view''' + embedded_view = self.get_view('ZetauEmbedded_Run2012*', 'unweighted_view') + zjets_view = self.get_view('Z*jets_M50_skimmedTT') + + embedded_histo = embedded_view.Get(normalization_path) + zjets_histo = zjets_view.Get(normalization_path) + + embed_int = embedded_histo.Integral() + zjets_int = zjets_histo.Integral() + + + scale_factor = zjets_int / embed_int + + scaled_view = views.TitleView( + views.StyleView(views.ScaleView(embedded_view, scale_factor), + **remove_name_entry(data_styles['ZetauEmbedded']) + ), + 'Z #rightarrow #tau#tau (embedded)' + ) + return scaled_view, scale_factor + + def simpleplot_mc(self, folder, signal, variable, rebin=1, xaxis='', + leftside=True, xrange=None, preprocess=None, sort=True,forceLumi=-1): + ''' Compare Monte Carlo signal to bkg ''' + #path = os.path.join(folder, variable) + #is_not_signal = lambda x: x is not signame + #set_trace() + + mc_stack_view = self.make_stack(rebin, preprocess, folder) + mc_stack=mc_stack_view.Get(variable) + + #self.canvas.SetLogy(False) + mc_stack.SetTitle('') + mc_stack.Draw() + + mc_stack.GetHistogram().GetXaxis().SetTitle(xaxis) + + if xrange: + mc_stack.GetXaxis().SetRangeUser(xrange[0], xrange[1]) + mc_stack.Draw() + + signalview=[] + mymax=0 + for sig in signal: + signal_view=self.get_view(sig) + if preprocess: + signal_view=preprocess(signal_view) + signal_view=self.get_wild_dir( + self.rebin_view(signal_view,rebin),folder) + signal=signal_view.Get(variable) + signalview.append(signal) + signal.Draw("SAME") + if signal.GetBinContent(signal.GetMaximumBin()) > mymax: + mymax = signal.GetBinContent(signal.GetMaximumBin()) + + self.keep.append(signal) + if mymax > mc_stack.GetMaximum(): + mc_stack.SetMaximum(mymax*1.2) + + self.keep.append(mc_stack) + + all_var=[] + all_var.extend([mc_stack]) + all_var.extend(signalview) + + self.add_legend(all_var, leftside, entries=len(mc_stack)+len(signalview)) + + def plot_data(self, folder, variable, rebin=1, xaxis='', + leftside=True, xrange=None, preprocess=None): + ''' Compare Monte Carlo signal to bkg ''' + #path = os.path.join(folder, variable) + #is_not_signal = lambda x: x is not signame + #set_trace() + data_view = self.get_view('data') + if preprocess: + data_view = preprocess( data_view ) + data_view = self.get_wild_dir( + self.rebin_view(data_view, rebin), + folder + ) + data = data_view.Get(variable) + data.Draw() + + + data.GetXaxis().SetTitle(xaxis) + + if xrange: + data.GetXaxis().SetRangeUser(xrange[0], xrange[1]) + data.Draw() + self.keep.append(data) + + + + def compare_data(self, folder, variable, folder2, rebin=1, xaxis='', + leftside=True, xrange=None, preprocess=None, show_ratio=False, ratio_range=0.2,rescale=1. ): + ''' Compare Monte Carlo signal to bkg ''' + #path = os.path.join(folder, variable) + #is_not_signal = lambda x: x is not signame + #set_trace() + data_view = self.get_view('data') + data_view2 = self.get_view('data') + if preprocess: + data_view = preprocess( data_view ) + data_view = self.get_wild_dir( + self.rebin_view(data_view, rebin), + folder + ) + data_view2 = self.get_wild_dir( + self.rebin_view(data_view2, rebin), + folder2 + ) + data = data_view.Get(variable) + data2 = data_view2.Get(variable) + + data.GetXaxis().SetTitle(xaxis) + #if (data.Integral()!=0 and data2.Integral()!=0) : + # data.Scale(1./data.Integral()) + # data2.Scale(1./data2.Integral()) + data.Draw() + data.GetYaxis().SetRangeUser(0, data.GetBinContent(data.GetMaximumBin())*1.2) + if xrange: + data.GetXaxis().SetRangeUser(xrange[0], xrange[1]) + data.Draw() + + data2.Draw("SAME") + data2.SetMarkerColor(2) + data2.Draw("SAME") + + + if show_ratio: + self.add_sn_ratio_plot(data2, data, xrange, ratio_range) + + self.keep.append(data) + self.keep.append(data2) + + + def add_ratio_bandplot(self, data_hist, mc_stack, err_hist, x_range=None, ratio_range=0.2): + #resize the canvas and the pad to fit the second pad + self.canvas.SetCanvasSize( self.canvas.GetWw(), int(self.canvas.GetWh()*1.3) ) + self.canvas.cd() + self.pad.SetPad(0, 0.33, 1., 1.) + self.pad.Draw() + self.canvas.cd() + #create lower pad + self.lower_pad = plotting.Pad('low', 'low', 0, 0., 1., 0.33) + self.lower_pad.Draw() + self.lower_pad.cd() + + + mc_hist = None + if isinstance(mc_stack, plotting.HistStack): + mc_hist = sum(mc_stack.GetHists()) + else: + mc_hist = mc_stack + data_clone = data_hist.Clone() + data_clone.Divide(mc_hist) + + band = err_hist.Clone() + + err = [] + ibin =1 + while ibin < band.GetXaxis().GetNbins()+1: + if mc_hist.GetBinContent(ibin) <> 0 : + err.append((ibin, band.GetBinError(ibin)/band.GetBinContent(ibin))) + ibin+=1 + + band.Divide(mc_hist.Clone()) + #print err + for ibin in err: + band.SetBinError(ibin[0], ibin[1]) + + if not x_range: + nbins = data_clone.GetNbinsX() + x_range = (data_clone.GetBinLowEdge(1), + data_clone.GetBinLowEdge(nbins)+data_clone.GetBinWidth(nbins)) + else: + data_clone.GetXaxis().SetRangeUser(*x_range) + + + ref_function = ROOT.TF1('f', "1.", *x_range) + ref_function.SetLineWidth(2) + ref_function.SetLineStyle(2) + + data_clone.Draw() + + if ratio_range: + data_clone.GetYaxis().SetRangeUser(1-ratio_range, 1+ratio_range) + ref_function.Draw('same') + band.SetMarkerStyle(0) + band.SetLineColor(1) + band.SetFillStyle(3001) + band.SetFillColor(1) + + band.Draw('samee2') + + self.keep.append(data_clone) + self.keep.append(band) + self.keep.append(ref_function) + self.pad.cd() + return data_clone + + + def add_ratio_diff(self, data_hist, mc_stack, err_hist, x_range=None, ratio_range=0.2): + #resize the canvas and the pad to fit the second pad + self.canvas.SetCanvasSize( self.canvas.GetWw(), int(self.canvas.GetWh()*1.3) ) + self.canvas.cd() + self.pad.SetPad(0, 0.33, 1., 1.) + self.pad.Draw() + self.canvas.cd() + #create lower pad + self.lower_pad = plotting.Pad('low', 'low', 0, 0., 1., 0.33) + self.lower_pad.Draw() + self.lower_pad.cd() + + + mc_hist = None + if isinstance(mc_stack, plotting.HistStack): + mc_hist = sum(mc_stack.GetHists()) + else: + mc_hist = mc_stack + data_clone = data_hist.Clone() + data_clone.Sumw2() + data_clone.Add(mc_hist, -1) + data_clone.Divide(mc_hist) + + band = err_hist.Clone() + + err = [] + ibin =1 + while ibin < band.GetXaxis().GetNbins()+1: + if mc_hist.GetBinContent(ibin) <> 0 : + err.append((ibin, band.GetBinError(ibin)/band.GetBinContent(ibin))) + + ibin+=1 + + band.Divide(mc_hist.Clone()) + #print err + for ibin in err: + band.SetBinError(ibin[0], ibin[1]) + + if self.blind_region: + for ibin in err: + if ibin >= band.FindBin(self.blind_region[0]) and ibin <= band.FindBin(self.blind_region[1]): + band.SetBinError(ibin, 10) + + if not x_range: + nbins = data_clone.GetNbinsX() + x_range = (data_clone.GetBinLowEdge(1), + data_clone.GetBinLowEdge(nbins)+data_clone.GetBinWidth(nbins)) + else: + data_clone.GetXaxis().SetRangeUser(*x_range) + + + ref_function = ROOT.TF1('f', "0.", *x_range) + ref_function.SetLineWidth(2) + ref_function.SetLineStyle(2) + + data_clone.Draw() + + if ratio_range: + data_clone.GetYaxis().SetRangeUser(-ratio_range, +ratio_range) + ref_function.Draw('same') + band.SetMarkerStyle(0) + band.SetLineColor(1) + band.SetFillStyle('x') + band.SetFillColor(1) + + band.Draw('samee2') + + self.keep.append(data_clone) + self.keep.append(band) + self.keep.append(ref_function) + self.pad.cd() + return data_clone + + def add_shape_systematics(self, histo, path, view, folder_systematics = [], name_systematics = []): + '''Adds shape systematics + add_shape_systematics(self, histo, path, view, folder_systematics = [], name_systematics = []) --> histo + histo is the central value + path is the path if the central value histo + view contains all the systematics. + folder_systematics is a list of tuples with the folders containing shifts (up, down) + name_systematics is a list of tuples containing the postfix to obtain the shifts (up, down) + ''' + + systematics = [] + for sys_up, sys_dw in folder_systematics: + h_up = view.Get(os.path.join(sys_up, path)) + h_dw = view.Get(os.path.join(sys_dw, path)) + systematics.append( + (h_up, h_dw) + ) + + #check if we have to apply met uncertainties + for sys_up, sys_dw in name_systematics: + h_up = view.Get(path + sys_up) + h_dw = view.Get(path + sys_dw) + systematics.append( + (h_up, h_dw) + ) + + + #ADD systematics + return histo_diff_quad(histo, *systematics) + + + + + def add_histo_error(self, histo, histoerr): + clone = histo.GetStack().Last().Clone('errhist') + for bin in range(1,clone.GetXaxis().GetNbins()): + error = histoerr.GetBinError(bin)*clone.GetBinContent(bin)/histoerr.GetBinContent(bin) if histoerr.GetBinContent(bin) <>0 else histoerr.GetBinError(bin) + clone.SetBinError(bin, error ) + + return clone + + + def plot_with_bkg_uncert (self, folder, variable, rebin=1, xaxis='', + leftside=True, xrange=None, preprocess=None, + show_ratio=False, ratio_range=0.2, sort=True, obj=['e1', 'e2'], plot_data=False): + + + #xsection uncertainties + #names must match with what defined in self.mc_samples + xsec_unc_mapper = { + 'TTJets*' : 0.026, + 'T*_t*' : 0.041, + '[WZ][WZ]Jets' : 0.056, #diboson + 'Wplus*Jets_madgraph*' : 0.035, #WJets + 'Z*jets_M50_skimmedLL' : 0.300, # theoretical 0.032 + 'Z*jets_M50_skimmedTT' : 0.032, + } + + + path = os.path.join(folder,variable) + + #make MC views with xsec error + mc_views_nosys = self.mc_views(rebin, preprocess) + mc_views = [] + for view, name in zip(mc_views_nosys, self.mc_samples): + new = SystematicsView( + view, + xsec_unc_mapper.get(name, 0.) #default to 0 + ) + mc_views.append(new) + + #make MC stack + mc_stack_view = views.StackView(*mc_views, sorted=sort) + mc_stack = mc_stack_view.Get( path ) + + #make histo clone for err computation + mc_sum_view = views.SumView(*mc_views) + mc_err = mc_sum_view.Get( path ) + + #Add MC-only systematics + folder_systematics = [ + ('p1s', 'm1s'), #PU correction + ] + + met_systematics = [ #it was without plus + ('_jes_plus', '_jes_minus'), + ('_mes_plus', '_mes_minus'), + #('_ees_plus', '_ees_minus'), + ('_tes_plus', '_tes_minus'), + ('_ues_plus', '_ues_minus'), + ] + + name_systematics = [] #which are not MET + #add MET sys if necessary + if 'collmass' in variable.lower() or \ + 'met' in variable.lower(): + if not variable.lower().startswith('type1'): + name_systematics.extend(met_systematics) # TO ADD WHEN RERUN WITH THE NEW BINNING + + #add them + + mc_err = self.add_shape_systematics( + mc_err, + path, + mc_sum_view, + folder_systematics, + name_systematics) + + #add jet category uncertainty + jetcat_unc_mapper = { + 0 : 0.017, + 1 : 0.035, + 2 : 0.05 + } + #find inn which jet category we are + regex = re.compile('\/\d\/') + found = regex.findall(path) + jet_unc = 0. + if found: + njet = int(found[0].strip('/')) + jet_unc = jetcat_unc_mapper.get(njet, 0. ) + mc_err = SystematicsView.add_error(mc_err, jet_unc) + + #check if we are using the embedded sample + if self.use_embedded: + embed_view = self.get_view('ZetauEmbedded') + if preprocess: + embed_view = preprocess(embed_view) + embed_view = RebinView( embed_view, rebin) + embed = embed_view.Get(path) + + #add xsec error + embed = SystematicsView.add_error( embed, xsec_unc_mapper['Z*jets_M50_skimmedTT']) + + #add them to backgrounds + mc_stack.Add(embed) + mc_err += embed + + mc_sum_view = views.SumView(mc_sum_view, embed_view) + + #Add MC and embed systematics + folder_systematics = [ + ('trp1s', 'trm1s'), #trig scale factor + ] + + #print folder_systematics + #Add as many eid sys as requested + for name in obj: + folder_systematics.extend([ + ('%sidp1s' % name, '%sidm1s' % name), #eID scale factor + ('%sisop1s' % name, '%sisom1s' % name), #e Iso scale factor + + ]) + + + mc_err = self.add_shape_systematics( + mc_err, + path, + mc_sum_view, + folder_systematics) + + #add lumi uncertainty + mc_err = SystematicsView.add_error( mc_err, 0.026 ) + + #get fakes + fakes_view = self.get_view('fakes') + if preprocess: + fakes_view = preprocess(fakes_view) + fakes_view = RebinView(fakes_view, rebin) + fakes = fakes_view.Get(path) + + fakes = self.add_shape_systematics( + fakes, + path, + fakes_view, + [('Up','Down')] + ) + fakes = SystematicsView.add_error(fakes, 0.30) + #add them to backgrounds + mc_stack.Add(fakes) + + mc_err.Sumw2() + mc_err.Add(fakes) + #set_trace() + + #####get efakes + ##efakes_view = self.get_view('efakes') + ##if preprocess: + ## efakes_view = preprocess(efakes_view) + ##efakes_view = RebinView(efakes_view, rebin) + ##efakes = efakes_view.Get(path) + ## + ##efakes = self.add_shape_systematics( + ## efakes, + ## path, + ## efakes_view, + ## [('Up','Down')] + ##) + #####add them to backgrounds + ##mc_stack.Add(efakes) + ## + ##mc_err.Sumw2() + ##mc_err.Add(efakes) + ###set_trace() + ##etfakes_view = self.get_view('etfakes') + ##if preprocess: + ## etfakes_view = preprocess(etfakes_view) + ##etfakes_view = RebinView(etfakes_view, rebin) + ##etfakes = etfakes_view.Get(path) + ## + ##etfakes = self.add_shape_systematics( + ## etfakes, + ## path, + ## etfakes_view, + ## [('Up','Down')] + ##) + #####add them to backgrounds + ##mc_stack.Add(etfakes) + ## + ##mc_err.Sumw2() + ##mc_err.Add(etfakes) + ###set_trace() + + #draw stack + mc_stack.Draw() + self.keep.append(mc_stack) + + #set cosmetics + self.canvas.SetLogy(True) + ##self.canvas.SetGridx(True) + ##self.canvas.SetGridy(True) + ##self.pad.SetGridx(True) + ##self.pad.SetGridy(True) + + mc_stack.GetHistogram().GetXaxis().SetTitle(xaxis) + if xrange: + mc_stack.GetXaxis().SetRangeUser(xrange[0], xrange[1]) + mc_stack.Draw() + + #set cosmetics + mc_err.SetMarkerStyle(0) + mc_err.SetLineColor(1) + mc_err.SetFillStyle('x') + mc_err.SetFillColor(1) + mc_err.Draw('pe2 same') + self.keep.append(mc_err) + + + + #Get signal + signals = [ + 'ggHiggsToETau', + 'vbfHiggsToETau', + ] + sig = [] + for name in signals: + sig_view = self.get_view(name) + if preprocess: + sig_view = preprocess(sig_view) + sig_view = RebinView(sig_view, rebin) + if not plot_data: + sig_view = views.ScaleView(sig_view, 100) + + histogram = sig_view.Get(path) + histogram.Draw('same') + self.keep.append(histogram) + sig.append(histogram) + for lfvh in sig: + if lfvh.GetMaximum() > mc_stack.GetMaximum(): + mc_stack.SetMaximum(1.2*lfvh.GetMaximum()) + + if plot_data==True: + # Draw data + data_view = self.get_view('data') + if preprocess: + data_view = preprocess( data_view ) + data_view = self.rebin_view(data_view, rebin) + data = data_view.Get(path) + + data.Draw('same') + print 'data', data.Integral() + self.keep.append(data) + + ## Make sure we can see everything + if data.GetMaximum() > mc_stack.GetMaximum(): + mc_stack.SetMaximum(1.2*data.GetMaximum()) + if lfvh.GetMaximum() > mc_stack.GetMaximum(): + mc_stack.SetMaximum(1.2*lfvh.GetMaximum()) + + if plot_data: + self.add_legend([data, mc_stack], leftside, entries=len(mc_stack.GetHists())+1) + #self.add_legend([data, sig[0], sig[1], mc_stack], leftside, entries=len(mc_stack.GetHists())+3) + else: + self.add_legend([sig[0], sig[1], mc_stack], leftside, entries=len(mc_stack.GetHists())+1) + if show_ratio and plot_data: + self.add_ratio_plot(data, mc_err, xrange, ratio_range, True) # add_ratio_diff(data, mc_stack, mc_err, xrange, ratio_range) + #self.add_ratio_plot(data, mc_stack, xrange, ratio_range, True) # add_ratio_diff(data, mc_stack, mc_err, xrange, ratio_range) + + +##----- + + def plot_without_uncert (self, folder, variable, rebin=1, xaxis='', + leftside=True, xrange=None, preprocess=None, + show_ratio=False, ratio_range=0.2, sort=True, obj=['e1', 'e2']): + + + + mc_stack_view = self.make_stack(rebin, preprocess, folder, sort) + + mc_stack = mc_stack_view.Get(variable) + mc_stack.Draw() + + self.canvas.SetLogy(True) + mc_stack.GetHistogram().GetXaxis().SetTitle(xaxis) + if xrange: + mc_stack.GetXaxis().SetRangeUser(xrange[0], xrange[1]) + mc_stack.Draw() + self.keep.append(mc_stack) + + + finalhisto= mc_stack.GetStack().Last().Clone() + finalhisto.Sumw2() + + histlist = mc_stack.GetHists(); + bkg_stack = mc_stack_view.Get(variable) + bkg_stack.GetStack().RemoveLast()## mettere il check se c'e` il fake altirmenti histo=mc_stack.GetStack().Last().Clone() + histo=bkg_stack.GetStack().Last().Clone() + histo.Sumw2() + + fake_p1s_histo=None + + if not folder.startswith('tLoose') and not folder.startswith('eLoose') and not folder.startswith('etLoose') : + isFakesIn= False + ##isEFakesIn=False + isETFakesIn=False + if 'Fakes' in self.mc_samples: + self.mc_samples.remove('Fakes') + if 'finalDYLL' in self.mc_samples: self.mc_samples.remove('finalDYLL') + isFakesIn=True + ##if 'eFakes' in self.mc_samples: + ## self.mc_samples.remove('eFakes') + ## if 'finalDYLL' in self.mc_samples: self.mc_samples.remove('finalDYLL') + ## isEFakesIn=True + ##if 'etFakes' in self.mc_samples: + ## self.mc_samples.remove('etFakes') + ## if 'finalDYLL' in self.mc_samples: self.mc_samples.remove('finalDYLL') + ## isETFakesIn=True + + + ibin =1 + + if isFakesIn: + self.mc_samples.append('Fakes') + # self.mc_samples.append('etFakes') + self.mc_samples.append('finalDYLL') + ## if isEFakesIn: + ## self.mc_samples.append('eFakes') + ## if not 'finalDYLL' in self.mc_samples: self.mc_samples.append('finalDYLL') + ## if isETFakesIn: + ## self.mc_samples.append('etFakes') + ## if not 'finalDYLL' in self.mc_samples: self.mc_samples.append('finalDYLL') + + + finalhisto.Draw('samee2') + finalhisto.SetMarkerStyle(0) + finalhisto.SetLineColor(1) + finalhisto.SetFillStyle(3001) + finalhisto.SetFillColor(1) + + + self.keep.append(finalhisto) + # Draw data + data_view = self.get_view('data') + if preprocess: + data_view = preprocess( data_view ) + data_view = self.get_wild_dir( + self.rebin_view(data_view, rebin), + folder + ) + data = data_view.Get(variable) + data.Draw('same') + print 'data', data.Integral() + self.keep.append(data) + ## Make sure we can see everything + if data.GetMaximum() > mc_stack.GetMaximum(): + mc_stack.SetMaximum(1.2*data.GetMaximum()) + + # # Add legend + self.add_legend([data, mc_stack], leftside, entries=len(mc_stack.GetHists())+1) + if show_ratio: + self.add_ratio_diff(data, mc_stack, finalhisto, xrange, ratio_range) + + def write_shapes(self, folder, variable, output_dir, br_strenght=1, + rebin=1, last = 400, preprocess=None): #, systematics): + '''Makes shapes for computing the limit and returns a list of systematic effects to be added to unc.vals/conf + make_shapes(folder, variable, output_dir, [rebin=1, preprocess=None) --> unc_conf_lines (list), unc_vals_lines (list) + ''' + output_dir.cd() + path = os.path.join(folder,variable) + + # Draw data + data_view = self.get_view('data') + if preprocess: + data_view = preprocess( data_view ) + data_view = self.rebin_view(data_view, rebin) + data = data_view.Get(path) + + data=change_histo_nbins(data, 0, last) + first_filled, last_filled = find_fill_range(data) + data.SetName('data_obs') + data.Write() + + #make MC views with xsec error + bkg_views = dict( + [(self.datacard_names[i], j) for i, j in zip(self.mc_samples, self.mc_views(rebin, preprocess))] + ) + bkg_weights = dict( + [(self.datacard_names[i], self.get_view(i, 'weight')) for i in self.mc_samples] + ) + #cache histograms, since getting them is time consuming + bkg_histos = {} + for name, view in bkg_views.iteritems(): + mc_histo = view.Get(path) + mc_histo = change_histo_nbins(mc_histo, 0, last) + first_filled_bkg, last_filled_bkg= find_fill_range(mc_histo) + print name, first_filled_bkg, last_filled_bkg, mc_histo.GetXaxis().GetNbins() + bkg_histos[name] = mc_histo.Clone() + mc_histo = remove_empty_bins( + mc_histo, bkg_weights[name], + first_filled_bkg, last_filled_bkg) + mc_histo.SetName(name) + mc_histo.Write() + + if self.use_embedded: + view = self.get_view('ZetauEmbedded') + weight = self.get_view('ZetauEmbedded', 'weight') + if preprocess: + view = preprocess(view) + view = self.rebin_view(view, rebin) + name = self.datacard_names['ZetauEmbedded'] + bkg_weights[name] = weight + bkg_views[name] = view + mc_histo = view.Get(path) + mc_histo = change_histo_nbins(mc_histo, 0, last) + first_filled_bkg, last_filled_bkg= find_fill_range(mc_histo) + bkg_histos[name] = mc_histo.Clone() + mc_histo = remove_empty_bins( + mc_histo, weight, + first_filled_bkg, last_filled_bkg) + mc_histo.SetName(name) + mc_histo.Write() + + fakes_view = self.get_view('fakes') + d_view = self.get_view('data') + weights_view = views.SumView( + views.SubdirectoryView(d_view, 'tLoose'), + views.SubdirectoryView(d_view, 'eLoose'), + views.SubdirectoryView(d_view, 'etLoose') + ) + if preprocess: + fakes_view = preprocess(fakes_view) + weights_view = preprocess(weights_view) + weights = weights_view.Get(os.path.join(folder,'weight')) + #print folder + fakes_view = self.rebin_view(fakes_view, rebin) + bkg_views['fakes'] = fakes_view + bkg_weights['fakes'] = mean(weights) + fake_shape = bkg_views['fakes'].Get(path) + fake_shape = change_histo_nbins(fake_shape, 0, last) + bkg_histos['fakes'] = fake_shape.Clone() + bkg_histos['fakes'] =change_histo_nbins(bkg_histos['fakes'] , 0, last) + first_filled_bkg, last_filled_bkg = find_fill_range(bkg_histos['fakes']) + fake_shape = remove_empty_bins( + fake_shape, bkg_weights['fakes'], + first_filled_bkg, last_filled_bkg) + fake_shape.SetName('fakes') + fake_shape.Write() + + #Get signal + signals = [ + 'ggHiggsToETau', + 'vbfHiggsToETau', + ] + for name in signals: + sig_view = self.get_view(name) + card_name = self.datacard_names[name] + if preprocess: + sig_view = preprocess(sig_view) + sig_view = views.ScaleView( + RebinView(sig_view, rebin), + br_strenght + ) + weights = self.get_view(name, 'weight') + bkg_views[card_name] = sig_view + bkg_weights[card_name] = weights + histogram = sig_view.Get(path) + histogram = change_histo_nbins(histogram, 0, last) + bkg_histos[card_name] = histogram.Clone() + first_filled_bkg, last_filled_bkg = find_fill_range(bkg_histos[card_name]) + histogram = remove_empty_bins( + histogram, bkg_weights[card_name], + first_filled_bkg, last_filled_bkg) + histogram.SetName(card_name) + histogram.Write() + + + unc_conf_lines = [] + unc_vals_lines = [] + category_name = output_dir.GetName() + for unc_name, info in self.systematics.iteritems(): + targets = [] + for target in info['apply_to']: + if target in self.sample_groups: + targets.extend(self.sample_groups[target]) + else: + targets.append(target) + + unc_conf = 'lnN' if info['type'] == 'yield' or info['type'] == 'stat' else 'shape' + #stat shapes are uncorrelated between samples + if info['type'] <> 'stat': + unc_conf_lines.append('%s %s' % (unc_name, unc_conf)) + shift = 0. + path_up = info['+'](path) + path_dw = info['-'](path) + for target in targets: + up = bkg_views[target].Get( + path_up + ) + down = bkg_views[target].Get( + path_dw + ) + if info['type'] == 'yield': + central = bkg_histos[target] + integral = central.Integral() + integral_up = up.Integral() + integral_down = down.Integral() + if integral == 0 and integral_up == 0 and integral_down ==0 : + shift=shift + else: + shift = max( + shift, + (integral_up - integral) / integral, + (integral - integral_down) / integral + ) + elif info['type'] == 'shape': + #remove empty bins also for shapes + #(but not in general to not spoil the stat uncertainties) + first_filled_bkg,last_filled_bkg= find_fill_range( bkg_histos[target]) + up = remove_empty_bins( + up, bkg_weights[target], + first_filled_bkg, last_filled_bkg) + first_filled_bkg, last_filled_bkg= find_fill_range( bkg_histos[target]) + down = remove_empty_bins( + down, bkg_weights[target], + first_filled_bkg, last_filled_bkg) + up.SetName('%s_%sUp' % (target, unc_name)) + down.SetName('%s_%sDown' % (target, unc_name)) + up.Write() + down.Write() + elif info['type'] == 'stat': + nbins = up.GetNbinsX() + up.Rebin(nbins) + yield_val = up.GetBinContent(1) + yield_err = up.GetBinError(1) + print target, yield_val, yield_err, + if yield_val==0: + unc_value = 0. + else: + unc_value = 1. + (yield_err / yield_val) + stat_unc_name = '%s_%s_%s' % (target, category_name, unc_name) + unc_conf_lines.append('%s %s' % (stat_unc_name, unc_conf)) + unc_vals_lines.append( + '%s %s %s %.2f' % (category_name, target, stat_unc_name, unc_value) + ) + else: + raise ValueError('systematic uncertainty type:"%s" not recognised!' % info['type']) + + if info['type'] <> 'stat': + shift += 1 + unc_vals_lines.append( + '%s %s %s %.2f' % (category_name, ','.join(targets), unc_name, shift) + ) + + return unc_conf_lines, unc_vals_lines + + ##----- + + def write_shapes_for_yields(self, folder, variable, output_dir, br_strenght=1, + rebin=1, preprocess=None): #, systematics): + '''Makes shapes for computing the limit and returns a list of systematic effects to be added to unc.vals/conf + make_shapes(folder, variable, output_dir, [rebin=1, preprocess=None) --> unc_conf_lines (list), unc_vals_lines (list) + ''' + output_dir.cd() + path = os.path.join(folder,variable) + + # Draw data + data_view = self.get_view('data') + if preprocess: + data_view = preprocess( data_view ) + data_view = self.rebin_view(data_view, rebin) + data = data_view.Get(path) + first_filled, last_filled = find_fill_range(data) + data.SetName('data_obs') + data.Write() + + #make MC views with xsec error + bkg_views = dict( + [(self.datacard_names[i], j) for i, j in zip(self.mc_samples, self.mc_views(rebin, preprocess))] + ) + bkg_weights = dict( + [(self.datacard_names[i], self.get_view(i, 'weight')) for i in self.mc_samples] + ) + #cache histograms, since getting them is time consuming + bkg_histos = {} + for name, view in bkg_views.iteritems(): + mc_histo = view.Get(path) + first_filled_bkg, last_filled_bkg= find_fill_range(mc_histo) + print name, first_filled_bkg, last_filled_bkg, mc_histo.GetXaxis().GetNbins() + bkg_histos[name] = mc_histo.Clone() + #mc_histo = remove_empty_bins( + # mc_histo, bkg_weights[name], + # first_filled_bkg, last_filled_bkg) + mc_histo.SetName(name) + mc_histo.Write() + + if self.use_embedded: + view = self.get_view('ZetauEmbedded') + weight = self.get_view('ZetauEmbedded', 'weight') + if preprocess: + view = preprocess(view) + view = self.rebin_view(view, rebin) + name = self.datacard_names['ZetauEmbedded'] + bkg_weights[name] = weight + bkg_views[name] = view + mc_histo = view.Get(path) + first_filled_bkg, last_filled_bkg= find_fill_range(mc_histo) + bkg_histos[name] = mc_histo.Clone() + #mc_histo = remove_empty_bins( + # mc_histo, weight, + # first_filled_bkg, last_filled_bkg) + mc_histo.SetName(name) + mc_histo.Write() + + fakes_view = self.get_view('fakes') + d_view = self.get_view('data') + weights_view = views.SumView( + views.SubdirectoryView(d_view, 'tLoose'), + views.SubdirectoryView(d_view, 'eLoose'), + views.SubdirectoryView(d_view, 'etLoose') + ) + if preprocess: + fakes_view = preprocess(fakes_view) + weights_view = preprocess(weights_view) + weights = weights_view.Get(os.path.join(folder,'weight')) + #print folder + fakes_view = self.rebin_view(fakes_view, rebin) + bkg_views['fakes'] = fakes_view + bkg_weights['fakes'] = mean(weights) + fake_shape = bkg_views['fakes'].Get(path) + bkg_histos['fakes'] = fake_shape.Clone() + first_filled_bkg, last_filled_bkg = find_fill_range(bkg_histos['fakes']) + #fake_shape = remove_empty_bins( + # fake_shape, bkg_weights['fakes'], + # first_filled_bkg, last_filled_bkg) + fake_shape.SetName('fakes') + fake_shape.Write() + + #Get signal + signals = [ + 'ggHiggsToETau', + 'vbfHiggsToETau', + ] + for name in signals: + sig_view = self.get_view(name) + card_name = self.datacard_names[name] + if preprocess: + sig_view = preprocess(sig_view) + sig_view = views.ScaleView( + RebinView(sig_view, rebin), + br_strenght + ) + weights = self.get_view(name, 'weight') + bkg_views[card_name] = sig_view + bkg_weights[card_name] = weights + histogram = sig_view.Get(path) + bkg_histos[card_name] = histogram.Clone() + first_filled_bkg, last_filled_bkg = find_fill_range(bkg_histos[card_name]) + #histogram = remove_empty_bins( + # histogram, bkg_weights[card_name], + # first_filled_bkg, last_filled_bkg) + histogram.SetName(card_name) + histogram.Write() + + + unc_conf_lines = [] + unc_vals_lines = [] + category_name = output_dir.GetName() + for unc_name, info in self.systematics.iteritems(): + targets = [] + for target in info['apply_to']: + if target in self.sample_groups: + targets.extend(self.sample_groups[target]) + else: + targets.append(target) + + unc_conf = 'lnN' if info['type'] == 'yield' or info['type'] == 'stat' else 'shape' + #stat shapes are uncorrelated between samples + if info['type'] <> 'stat': + unc_conf_lines.append('%s %s' % (unc_name, unc_conf)) + shift = 0. + path_up = info['+'](path) + path_dw = info['-'](path) + for target in targets: + up = bkg_views[target].Get( + path_up + ) + down = bkg_views[target].Get( + path_dw + ) + if info['type'] == 'yield': + central = bkg_histos[target] + integral = central.Integral() + integral_up = up.Integral() + integral_down = down.Integral() + if integral == 0 and integral_up == 0 and integral_down ==0 : + shift=shift + else: + shift = max( + shift, + (integral_up - integral) / integral, + (integral - integral_down) / integral + ) + elif info['type'] == 'shape': + #remove empty bins also for shapes + #(but not in general to not spoil the stat uncertainties) + first_filled_bkg,last_filled_bkg= find_fill_range( bkg_histos[target]) + ##up = remove_empty_bins( + ## up, bkg_weights[target], + ## first_filled_bkg, last_filled_bkg) + first_filled_bkg, last_filled_bkg= find_fill_range( bkg_histos[target]) + ##down = remove_empty_bins( + ## down, bkg_weights[target], + ## first_filled_bkg, last_filled_bkg) + up.SetName('%s_%sUp' % (target, unc_name)) + down.SetName('%s_%sDown' % (target, unc_name)) + up.Write() + down.Write() + elif info['type'] == 'stat': + nbins = up.GetNbinsX() + up.Rebin(nbins) + yield_val = up.GetBinContent(1) + yield_err = up.GetBinError(1) + print target, yield_val, yield_err, + if yield_val==0: + unc_value = 0. + else: + unc_value = 1. + (yield_err / yield_val) + stat_unc_name = '%s_%s_%s' % (target, category_name, unc_name) + unc_conf_lines.append('%s %s' % (stat_unc_name, unc_conf)) + unc_vals_lines.append( + '%s %s %s %.2f' % (category_name, target, stat_unc_name, unc_value) + ) + else: + raise ValueError('systematic uncertainty type:"%s" not recognised!' % info['type']) + + if info['type'] <> 'stat': + shift += 1 + unc_vals_lines.append( + '%s %s %s %.2f' % (category_name, ','.join(targets), unc_name, shift) + ) + + return unc_conf_lines, unc_vals_lines +## output_dir.cd() +## path = os.path.join(folder,variable) +## +## #make MC views with xsec error +## bkg_views = dict( +## [(self.datacard_names[i], j) for i, j in zip(self.mc_samples, self.mc_views(rebin, preprocess))] +## ) +## bkg_weights = dict( +## [(self.datacard_names[i], self.get_view(i, 'weight')) for i in self.mc_samples] +## ) +## #cache histograms, since getting them is time consuming +## bkg_histos = {} +## for name, view in bkg_views.iteritems(): +## mc_histo = view.Get(path) +## bkg_histos[name] = mc_histo.Clone() +## #mc_histo = remove_empty_bins( +## # mc_histo, bkg_weights[name]) +## mc_histo.SetName(name) +## mc_histo.Write() +## +## if self.use_embedded: +## view = self.get_view('ZetauEmbedded') +## weight = self.get_view('ZetauEmbedded', 'weight') +## if preprocess: +## view = preprocess(view) +## view = self.rebin_view(view, rebin) +## name = self.datacard_names['ZetauEmbedded'] +## bkg_weights[name] = weight +## bkg_views[name] = view +## mc_histo = view.Get(path) +## bkg_histos[name] = mc_histo.Clone() +## #mc_histo = remove_empty_bins( +## # mc_histo, weight) +## mc_histo.SetName(name) +## mc_histo.Write() +## +## fakes_view = self.get_view('fakes')#to comment for optimization study +## d_view = self.get_view('data') +## weights_view = views.SumView( +## views.SubdirectoryView(d_view, 'tLoose'), +## views.SubdirectoryView(d_view, 'eLoose'), +## views.SubdirectoryView(d_view, 'etLoose') +## ) +## if preprocess: +## fakes_view = preprocess(fakes_view) +## weights_view = preprocess(weights_view) +## weights = weights_view.Get(os.path.join(folder,'weight')) #to comment for optimization study +## fakes_view = self.rebin_view(fakes_view, rebin) +## bkg_views['fakes'] = fakes_view +## bkg_weights['fakes'] = mean(weights) +## fake_shape = bkg_views['fakes'].Get(path) +## bkg_histos['fakes'] = fake_shape.Clone() +## #fake_shape = remove_empty_bins( +## # fake_shape, bkg_weights['fakes']) +## fake_shape.SetName('fakes') +## fake_shape.Write() +## +## unc_conf_lines = [] +## unc_vals_lines = [] +## category_name = output_dir.GetName() +## for unc_name, info in self.systematics.iteritems(): +## print unc_name +## targets = [] +## for target in info['apply_to']: +## if target in self.sample_groups: +## targets.extend(self.sample_groups[target]) +## else: +## targets.append(target) +## +## unc_conf = 'lnN' if info['type'] == 'yield' or info['type'] == 'stat' else 'shape' +## #stat shapes are uncorrelated between samples +## if info['type'] <> 'stat': +## unc_conf_lines.append('%s %s' % (unc_name, unc_conf)) +## shift = 0. +## path_up = info['+'](path) +## path_dw = info['-'](path) +## for target in targets: +## up = bkg_views[target].Get( +## path_up +## ) +## down = bkg_views[target].Get( +## path_dw +## ) +## if info['type'] == 'yield': +## central = bkg_histos[target] +## integral = central.Integral() +## integral_up = up.Integral() +## integral_down = down.Integral() +## if integral == 0 and integral_up == 0 and integral_down ==0 : +## shift=shift +## else: +## shift = max( +## shift, +## (integral_up - integral) / integral, +## (integral - integral_down) / integral +## ) +## elif info['type'] == 'shape': +## #remove empty bins also for shapes +## #(but not in general to not spoil the stat uncertainties) +## #up = remove_empty_bins(up, bkg_weights[target]) +## #down = remove_empty_bins(down, bkg_weights[target]) +## up.SetName('%s_%sUp' % (target, unc_name)) +## down.SetName('%s_%sDown' % (target, unc_name)) +## up.Write() +## down.Write() +## elif info['type'] == 'stat': +## nbins = up.GetNbinsX() +## up.Rebin(nbins) +## yield_val = up.GetBinContent(1) +## yield_err = up.GetBinError(1) +## print target, yield_val, yield_err, +## if yield_val==0: +## unc_value = 0. +## else: +## unc_value = 1. + (yield_err / yield_val) +## stat_unc_name = '%s_%s_%s' % (target, category_name, unc_name) +## unc_conf_lines.append('%s %s' % (stat_unc_name, unc_conf)) +## unc_vals_lines.append( +## '%s %s %s %.2f' % (category_name, target, stat_unc_name, unc_value) +## ) +## else: +## raise ValueError('systematic uncertainty type:"%s" not recognised!' % info['type']) +## +## if info['type'] <> 'stat': +## shift += 1 +## unc_vals_lines.append( +## '%s %s %s %.2f' % (category_name, ','.join(targets), unc_name, shift) +## ) +## +## #Get signal +## signals = [ +## 'ggHiggsToETau', +## 'vbfHiggsToETau', +## ] +## for name in signals: +## sig_view = self.get_view(name) +## if preprocess: +## sig_view = preprocess(sig_view) +## sig_view = views.ScaleView( +## RebinView(sig_view, rebin), +## br_strenght +## ) +## +## histogram = sig_view.Get(path) +## histogram.SetName(self.datacard_names[name]) +## histogram.Write() +## +## # Draw data +## data_view = self.get_view('data') +## if preprocess: +## data_view = preprocess( data_view ) +## data_view = self.rebin_view(data_view, rebin) +## data = data_view.Get(path) +## data.SetName('data_obs') +## data.Write() +## +## return unc_conf_lines, unc_vals_lines + +##----- + + + + + + def write_shapes_for_optimization(self, folder, variable, output_dir, br_strenght=1, + rebin=1, preprocess=None): #, systematics): + '''Makes shapes for computing the limit and returns a list of systematic effects to be added to unc.vals/conf + make_shapes(folder, variable, output_dir, [rebin=1, preprocess=None) --> unc_conf_lines (list), unc_vals_lines (list) + ''' + output_dir.cd() + path = os.path.join(folder,variable) + + #make MC views with xsec error + bkg_views = dict( + [(self.datacard_names[i], j) for i, j in zip(self.mc_samples, self.mc_views(rebin, preprocess))] + ) + bkg_weights = dict( + [(self.datacard_names[i], self.get_view(i, 'weight')) for i in self.mc_samples] + ) + #cache histograms, since getting them is time consuming + bkg_histos = {} + for name, view in bkg_views.iteritems(): + mc_histo = view.Get(path) + bkg_histos[name] = mc_histo.Clone() + #mc_histo = remove_empty_bins( + # mc_histo, bkg_weights[name]) + mc_histo.SetName(name) + mc_histo.Write() + + if self.use_embedded: + view = self.get_view('ZetauEmbedded') + weight = self.get_view('ZetauEmbedded', 'weight') + if preprocess: + view = preprocess(view) + view = self.rebin_view(view, rebin) + name = self.datacard_names['ZetauEmbedded'] + bkg_weights[name] = weight + bkg_views[name] = view + mc_histo = view.Get(path) + bkg_histos[name] = mc_histo.Clone() + #mc_histo = remove_empty_bins( + # mc_histo, weight) + mc_histo.SetName(name) + mc_histo.Write() + + ## fakes_view = self.get_view('fakes')#to comment for optimization study + d_view = self.get_view('data') + weights_view = views.SumView( + views.SubdirectoryView(d_view, 'tLoose'), + views.SubdirectoryView(d_view, 'eLoose'), + views.SubdirectoryView(d_view, 'etLoose') + ) + if preprocess: + fakes_view = preprocess(fakes_view) + weights_view = preprocess(weights_view) +## weights = weights_view.Get(os.path.join(folder,'weight')) #to comment for optimization study +## fakes_view = self.rebin_view(fakes_view, rebin) +## bkg_views['fakes'] = fakes_view +## bkg_weights['fakes'] = mean(weights) +## fake_shape = bkg_views['fakes'].Get(path) +## bkg_histos['fakes'] = fake_shape.Clone() +## #fake_shape = remove_empty_bins( +## # fake_shape, bkg_weights['fakes']) +## fake_shape.SetName('fakes') +## fake_shape.Write() + + unc_conf_lines = [] + unc_vals_lines = [] + category_name = output_dir.GetName() + for unc_name, info in self.systematics.iteritems(): + targets = [] + for target in info['apply_to']: + if target in self.sample_groups: + targets.extend(self.sample_groups[target]) + else: + targets.append(target) + + unc_conf = 'lnN' if info['type'] == 'yield' or info['type'] == 'stat' else 'shape' + #stat shapes are uncorrelated between samples + if info['type'] <> 'stat': + unc_conf_lines.append('%s %s' % (unc_name, unc_conf)) + shift = 0. + path_up = info['+'](path) + path_dw = info['-'](path) + for target in targets: + up = bkg_views[target].Get( + path_up + ) + down = bkg_views[target].Get( + path_dw + ) + if info['type'] == 'yield': + central = bkg_histos[target] + integral = central.Integral() + integral_up = up.Integral() + integral_down = down.Integral() + if integral == 0 and integral_up == 0 and integral_down ==0 : + shift=shift + else: + shift = max( + shift, + (integral_up - integral) / integral, + (integral - integral_down) / integral + ) + elif info['type'] == 'shape': + #remove empty bins also for shapes + #(but not in general to not spoil the stat uncertainties) + #up = remove_empty_bins(up, bkg_weights[target]) + #down = remove_empty_bins(down, bkg_weights[target]) + up.SetName('%s_%sUp' % (target, unc_name)) + down.SetName('%s_%sDown' % (target, unc_name)) + up.Write() + down.Write() + elif info['type'] == 'stat': + nbins = up.GetNbinsX() + up.Rebin(nbins) + yield_val = up.GetBinContent(1) + yield_err = up.GetBinError(1) + #print target, yield_val, yield_err, + if yield_val==0: + unc_value = 0. + else: + unc_value = 1. + (yield_err / yield_val) + stat_unc_name = '%s_%s_%s' % (target, category_name, unc_name) + unc_conf_lines.append('%s %s' % (stat_unc_name, unc_conf)) + unc_vals_lines.append( + '%s %s %s %.2f' % (category_name, target, stat_unc_name, unc_value) + ) + else: + raise ValueError('systematic uncertainty type:"%s" not recognised!' % info['type']) + + if info['type'] <> 'stat': + shift += 1 + unc_vals_lines.append( + '%s %s %s %.2f' % (category_name, ','.join(targets), unc_name, shift) + ) + + #Get signal + signals = [ + 'ggHiggsToETau', + 'vbfHiggsToETau', + ] + for name in signals: + sig_view = self.get_view(name) + if preprocess: + sig_view = preprocess(sig_view) + sig_view = views.ScaleView( + RebinView(sig_view, rebin), + br_strenght + ) + + histogram = sig_view.Get(path) + histogram.SetName(self.datacard_names[name]) + histogram.Write() + + # Draw data + data_view = self.get_view('data') + if preprocess: + data_view = preprocess( data_view ) + data_view = self.rebin_view(data_view, rebin) + data = data_view.Get(path) + data.SetName('data_obs') + data.Write() + + return unc_conf_lines, unc_vals_lines + +##----- + + + + + diff --git a/lfvetau/CorrectFakeRateData.py b/lfvetau/CorrectFakeRateData.py new file mode 100644 index 00000000..0a4db484 --- /dev/null +++ b/lfvetau/CorrectFakeRateData.py @@ -0,0 +1,168 @@ +#! /bin/env python +''' + +Subtract expected WZ and ZZ contamination from FR numerator and denominators. + +Author: Evan K. Frii + +''' + +import logging +import sys +logging.basicConfig(stream=sys.stderr, level=logging.INFO) +from RecoLuminosity.LumiDB import argparse +import fnmatch +from FinalStateAnalysis.PlotTools.RebinView import RebinView +from FinalStateAnalysis.PlotTools.SubtractionView import SubtractionView +import glob +import os + +log = logging.getLogger("CorrectFakeRateData") +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--files', nargs='+') + parser.add_argument('--lumifiles', nargs='+') + parser.add_argument('--outputfile', required=True) + parser.add_argument('--denom', required=True, help='Path to denom') + parser.add_argument('--numerator', required=True, help='Path to numerator') + parser.add_argument('--rebin', type=str, default="1") + + args = parser.parse_args() + + from rootpy import io + import ROOT + import rootpy.plotting.views as views + import rootpy.plotting as plotting + from FinalStateAnalysis.MetaData.data_views import data_views + + files = [] + for pattern in args.files: + files.extend(glob.glob(pattern)) + + log.info("Loading data from %i files", len(files)) + + lumifiles = [] + for pattern in args.lumifiles: + lumifiles.extend(glob.glob(pattern)) + + the_views = data_views(files, lumifiles) + + outputdir = os.path.dirname(args.outputfile) + if outputdir and not os.path.exists(outputdir): + os.makedirs(outputdir) + + log.info("Rebinning with factor %s", args.rebin) + def rebin_view(x): + ''' Make a view which rebins histograms ''' + binning = None + binning = eval(args.rebin) + return RebinView(x, binning) + + def round_to_ints(x): + new = x.Clone() + new.Reset() + for bin in range(x.GetNbinsX()+1): + binsy = range(x.GetNbinsY()+1) if isinstance(x, ROOT.TH2) else [-1] + for biny in binsy: + nentries = ROOT.TMath.Nint(x.GetBinContent(bin, biny)) \ + if isinstance(x, ROOT.TH2) else \ + ROOT.TMath.Nint(x.GetBinContent(bin)) + centerx = x.GetXaxis().GetBinCenter(bin) + centery = x.GetYaxis().GetBinCenter(biny) \ + if isinstance(x, ROOT.TH2) else \ + 0. + for _ in range(nentries): + if isinstance(x, ROOT.TH2): + new.Fill(centerx, centery) + else: + new.Fill(centerx) + return new + + def int_view(x): + return views.FunctorView(x, round_to_ints) + + def get_view(sample_pattern): + for sample, sample_info in the_views.iteritems(): + if fnmatch.fnmatch(sample, sample_pattern): + return rebin_view(sample_info['view']) + raise KeyError("I can't find a view that matches %s, I have: %s" % ( + sample_pattern, " ".join(the_views.keys()))) + + #from pdb import set_trace; set_trace() + wz_view = get_view('WZ*') + zz_view = get_view('ZZ*') + data = rebin_view(the_views['data']['view']) + + corrected_view = int_view( + SubtractionView(data, wz_view, zz_view, restrict_positive=True)) + + log.debug('creating output file') + output = io.open(args.outputfile, 'RECREATE') + output.cd() + + log.debug('getting from corrected view') + corr_numerator = corrected_view.Get(args.numerator) + corr_denominator = corrected_view.Get(args.denom) + + log.info("Corrected: %0.2f/%0.2f = %0.1f%%", + corr_numerator.Integral(), + corr_denominator.Integral(), + 100*corr_numerator.Integral()/corr_denominator.Integral() + if corr_denominator.Integral() else 0 + ) + + uncorr_numerator = data.Get(args.numerator) + uncorr_denominator = data.Get(args.denom) + + wz_integral = wz_view.Get(args.numerator).Integral() + zz_integral = zz_view.Get(args.numerator).Integral() + + log.info("Numerator integrals data: %.2f WZ: %.2f, ZZ: %.2f. Corrected numerator: %.2f", + uncorr_numerator.Integral(), + wz_integral, + zz_integral, + corr_numerator.Integral() + ) + + log.info("Uncorrected: %0.2f/%0.2f = %0.1f%%", + uncorr_numerator.Integral(), + uncorr_denominator.Integral(), + 100*uncorr_numerator.Integral()/uncorr_denominator.Integral() + if uncorr_denominator.Integral() else 0 + ) + + + corr_numerator.SetName('numerator') + corr_denominator.SetName('denominator') + + uncorr_numerator.SetName('numerator_uncorr') + uncorr_denominator.SetName('denominator_uncorr') + + corr_numerator.Write() + corr_denominator.Write() + uncorr_numerator.Write() + uncorr_denominator.Write() + + #make the unbinned plots + args.rebin = '1' + wz_view = get_view('WZ*') + zz_view = get_view('ZZ*') + data = the_views['data']['view'] + + corrected_view = int_view( + SubtractionView(data, wz_view, zz_view, restrict_positive=True)) + + corr_numerator_unrebinned = corrected_view.Get(args.numerator) + corr_denominator_unrebinned = corrected_view.Get(args.denom) + uncorr_numerator_unrebinned = data.Get(args.numerator) + uncorr_denominator_unrebinned = data.Get(args.denom) + + corr_numerator_unrebinned.SetName('numerator_unrebinned') + corr_denominator_unrebinned.SetName('denominator_unrebinned') + uncorr_numerator_unrebinned.SetName('numerator_uncorr_unrebinned') + uncorr_denominator_unrebinned.SetName('denominator_uncorr_unrebinned') + + corr_numerator_unrebinned.Write() + corr_denominator_unrebinned.Write() + uncorr_numerator_unrebinned.Write() + uncorr_denominator_unrebinned.Write() diff --git a/lfvetau/EEAnalyzerMVA.py b/lfvetau/EEAnalyzerMVA.py new file mode 100644 index 00000000..10d38024 --- /dev/null +++ b/lfvetau/EEAnalyzerMVA.py @@ -0,0 +1,453 @@ +##Correction Factor still to add +from EETree import EETree +import os +import ROOT +import math +from pdb import set_trace +import glob +import array +import baseSelections as selections +import mcCorrections +import FinalStateAnalysis.PlotTools.pytree as pytree +from FinalStateAnalysis.PlotTools.decorators import memo_last +from FinalStateAnalysis.PlotTools.MegaBase import MegaBase +from math import sqrt, pi, sin, cos, acos, sinh + +def Z(row): + e1p=ROOT.TVector3(row.e1Pt*cos(row.e1Phi),row.e1Pt*sin(row.e1Phi),row.e1Pt*sinh(row.e1Eta)) + e2p=ROOT.TVector3(row.e2Pt*cos(row.e2Phi),row.e2Pt*sin(row.e2Phi),row.e2Pt*sinh(row.e2Eta)) + e1FourVector= ROOT.TLorentzVector(e1p, sqrt(e1p.Mag2()+row.e1Mass*row.e1Mass)) + e2FourVector= ROOT.TLorentzVector(e2p, sqrt(e2p.Mag2()+row.e2Mass*row.e2Mass)) + zFourVector = e1FourVector+e2FourVector + return zFourVector + +def zPhi(pt1, eta1, phi1, pt2, eta2, phi2): + px1 = pt1*cos(phi1) + py1 = pt1*sin(phi1) + pz1 = pt1*sinh(eta1) + px2 = pt2*cos(phi2) + py2 = pt2*sin(phi2) + pz2 = pt2*sinh(eta2) + + px = px1+px2 + py = py1+py2 + pt = sqrt(px*px+py*py) + phi = acos(px/pt) + return phi + +def deltaPhi(phi1, phi2): + PHI = abs(phi1-phi2) + if PHI<=pi: + return PHI + else: + return 2*pi-PHI +def deltaR(phi1, ph2, eta1, eta2): + deta = eta1 - eta2 + dphi = abs(phi1-phi2) + if (dphi>pi) : dphi = 2*pi-dphi + return sqrt(deta*deta + dphi*dphi); + +class EEAnalyzerMVA(MegaBase): + tree = 'ee/final/Ntuple' + def __init__(self, tree, outfile, **kwargs): + self.channel='EE' + super(EEAnalyzerMVA, self).__init__(tree, outfile, **kwargs) + self.tree = EETree(tree) + self.out=outfile + self.histograms = {} + self.pucorrector = mcCorrections.make_puCorrector('singlee') + self.pucorrectorUp = mcCorrections.make_puCorrectorUp('singlee') + self.pucorrectorDown = mcCorrections.make_puCorrectorDown('singlee') + + + def event_weight(self, row): + if row.run > 2: #FIXME! add tight ID correction + return [1.] + + etrig = 'e1' + if row.e2Pt > row.e1Pt : etrig = 'e2' + if bool(row.e1MatchesSingleE27WP80) and not bool(row.e2MatchesSingleE27WP80) : etrig = 'e1' + if not bool(row.e1MatchesSingleE27WP80) and bool(row.e2MatchesSingleE27WP80) : etrig = 'e2' + + allmcCorrections= mcCorrections.get_electronId_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronId_corrections13_MVA(row, 'e2') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e2') * mcCorrections.get_trigger_corrections_MVA(row, etrig) + + + trUp_mcCorrections = mcCorrections.get_electronId_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronId_corrections13_MVA(row, 'e2') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e2') * mcCorrections.get_trigger_corrections_p1s_MVA(row, etrig) + trDown_mcCorrections = mcCorrections.get_electronId_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronId_corrections13_MVA(row, 'e2') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e2') * mcCorrections.get_trigger_corrections_m1s_MVA(row, etrig) + + e1idUp_mcCorrections= mcCorrections.get_electronId_corrections13_p1s_MVA(row, 'e1') *\ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronId_corrections13_MVA(row, 'e2') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e2') * mcCorrections.get_trigger_corrections_MVA(row, etrig) + e1idDown_mcCorrections= mcCorrections.get_electronId_corrections13_m1s_MVA(row, 'e1') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronId_corrections13_MVA(row, 'e2') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e2') * mcCorrections.get_trigger_corrections_MVA(row, etrig) + e2idUp_mcCorrections= mcCorrections.get_electronId_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronId_corrections13_p1s_MVA(row, 'e2') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e2') * mcCorrections.get_trigger_corrections_MVA(row, etrig) + e2idDown_mcCorrections= mcCorrections.get_electronId_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronId_corrections13_m1s_MVA(row, 'e2') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e2') * mcCorrections.get_trigger_corrections_MVA(row, etrig) + + e1isoUp_mcCorrections= mcCorrections.get_electronId_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronIso_corrections13_p1s_MVA(row, 'e1') * \ + mcCorrections.get_electronId_corrections13_MVA(row, 'e2') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e2') * mcCorrections.get_trigger_corrections_MVA(row, etrig) + e1isoDown_mcCorrections= mcCorrections.get_electronId_corrections13_m1s_MVA(row, 'e1') * \ + mcCorrections.get_electronIso_corrections13_p1s_MVA(row, 'e1') * \ + mcCorrections.get_electronId_corrections13_MVA(row, 'e2') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e2') * mcCorrections.get_trigger_corrections_MVA(row, etrig) + e2isoUp_mcCorrections= mcCorrections.get_electronId_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronId_corrections13_MVA(row, 'e2') * \ + mcCorrections.get_electronIso_corrections13_p1s_MVA(row, 'e2') * mcCorrections.get_trigger_corrections_MVA(row, etrig) + e2isoDown_mcCorrections= mcCorrections.get_electronId_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronIso_corrections13_MVA(row, 'e1') * \ + mcCorrections.get_electronId_corrections13_MVA(row, 'e2') * \ + mcCorrections.get_electronIso_corrections13_m1s_MVA(row, 'e2') * mcCorrections.get_trigger_corrections_MVA(row, etrig) + + + #pucorrlist = self.pucorrector(row.nTruePU) + + weight = self.pucorrector(row.nTruePU) *\ + allmcCorrections + weight_up = self.pucorrectorUp(row.nTruePU) *\ + allmcCorrections + weight_down = self.pucorrectorDown(row.nTruePU) *\ + allmcCorrections + + weight_tr_up = self.pucorrector(row.nTruePU) *\ + trUp_mcCorrections + weight_tr_down = self.pucorrector(row.nTruePU) *\ + trDown_mcCorrections + + + weight_e1id_up = self.pucorrector(row.nTruePU) *\ + e1idUp_mcCorrections + weight_e2id_up = self.pucorrector(row.nTruePU) *\ + e2idUp_mcCorrections + weight_e1id_down = self.pucorrector(row.nTruePU) *\ + e1idDown_mcCorrections + weight_e2id_down = self.pucorrector(row.nTruePU) *\ + e2idDown_mcCorrections + weight_e1iso_up = self.pucorrector(row.nTruePU) *\ + e1isoUp_mcCorrections + weight_e2iso_up = self.pucorrector(row.nTruePU) *\ + e2isoUp_mcCorrections + weight_e1iso_down = self.pucorrector(row.nTruePU) *\ + e1isoDown_mcCorrections + weight_e2iso_down = self.pucorrector(row.nTruePU) *\ + e2isoDown_mcCorrections + + return [weight, weight_up, weight_down, weight_tr_up, weight_tr_down, weight_e1id_up, weight_e1id_down, weight_e1iso_up, weight_e1iso_down, weight_e2id_up, weight_e2id_down, weight_e2iso_up, weight_e2iso_down] + + + +## + def begin(self): + sign=['os', 'ss'] + jets = [0,1,2,3] + folder=[] + pudir = ['','p1s/', 'm1s/','trp1s/', 'trm1s/', 'e1idp1s/','e1idm1s/', 'e1isop1s/','e1isom1s/','e2idp1s/','e2idm1s/', 'e2isop1s/','e2isom1s/'] + + + for d in pudir : + for i in sign: + folder.append(d+i) + for j in jets: + folder.append(d+i+'/'+str(j)) + + #self.book('jobInfo', "jobInfo", "jobInfo", "inputfilename/C:events/l", type=pytree.PyTree) + + + + for f in folder: + #print f, f.startswith('os'), f.startswith('ss') + if f.startswith('os') or f.startswith('ss') : + self.book(f, "evtInfo", "evtInfo", "run/l:lumi/l:evt/l", type=pytree.PyTree) + + self.book(f,"e1Pt", "e1 p_{T}", 200, 0, 200) + self.book(f,"e1Phi", "e1 phi", 100, -3.2, 3.2) + self.book(f,"e1Eta", "e1 eta", 50, -2.5, 2.5) + self.book(f,"e2Pt", "e2 p_{T}", 200, 0, 200) + self.book(f,"e2Phi", "e2 phi", 100, -3.2, 3.2) + self.book(f,"e2Eta", "e2 eta", 50, -2.5, 2.5) + + self.book(f, "e1e2_DeltaPhi", "e1-e2 DeltaPhi" , 50, 0, 3.2) + self.book(f, "e1e2_DeltaR", "e1-e2 DeltaR" , 50, 0, 3.2) + + self.book(f, "e1e2Mass", "e1e2 Inv Mass", 320, 0, 320) + + self.book(f, "pfMetEt", "pfMetEt", 50, 0, 100) + self.book(f, "type1_pfMetEt", "type1_pfMetEt", 50, 0, 100) + self.book(f, "mvaMetEt", "mvaMetEt", 50, 0, 100) + self.book(f, "pfMetPhi", "pfMetPhi", 100, -3.2, 3.2) + self.book(f, "type1_pfMetPhi", "type1_pfMetPhi", 100, -3.2, 3.2) + self.book(f, "mvaMetPhi", "mvaMetPhi", 100, -3.2, 3.2) + + self.book(f, "type1_pfMetEt_par", "type1_pfMetEt_par", 100, -100, 100) + self.book(f, "type1_pfMetEt_perp", "type1_pfMetEt_perp", 50, 0, 100) + self.book(f, "pfMetEt_par", "pfMetEt_par", 100, -100, 100) + self.book(f, "pfMetEt_perp", "pfMetEt_perp", 50, 0, 100) + self.book(f, "mvaMetEt_par", "mvaMetEt_par", 100, -100, 100) + self.book(f, "mvaMetEt_perp", "mvaMetEt_perp", 50, 0, 100) + + + self.book(f, "e1PFMET_DeltaPhi_Ty1", "e1-type1PFMET DeltaPhi" , 50, 0, 3.2) + self.book(f, "e1PFMET_Mt_Ty1", "e1-type1PFMET M_{T}" , 200, 0, 200) + self.book(f, "e1PFMET_DeltaPhi", "e1-PFMET DeltaPhi" , 50, 0, 3.2) + self.book(f, "e1PFMET_Mt", "e1-PFMET M_{T}" , 200, 0, 200) + self.book(f, "e1MVAMET_DeltaPhi", "e1-MVAMET DeltaPhi" , 50, 0, 3.2) + self.book(f, "e1MVAMET_Mt", "e1-MVAMET M_{T}" , 200, 0, 200) + + self.book(f, "e2PFMET_DeltaPhi_Ty1", "e2-type1PFMET DeltaPhi" , 50, 0, 3.2) + self.book(f, "e2PFMET_Mt_Ty1", "e2-type1PFMET M_{T}" , 200, 0, 200) + self.book(f, "e2PFMET_DeltaPhi", "e2-PFMET DeltaPhi" , 50, 0, 3.2) + self.book(f, "e2PFMET_Mt", "e2-PFMET M_{T}" , 200, 0, 200) + self.book(f, "e2MVAMET_DeltaPhi", "e2-MVAMET DeltaPhi" , 50, 0, 3.2) + self.book(f, "e2MVAMET_Mt", "e2-MVAMET M_{T}" , 200, 0, 200) + + + self.book(f,"pfMetEt_jes","pfMetEt_jes", 50, 0, 100) + self.book(f,"pfMetPhi_jes","pfMetPhi_jes", 100, -3.2, 3.2) + self.book(f,"pfMetEt_mes","pfMetEt_mes",50, 0, 100 ) + self.book(f,"pfMetPhi_mes","pfMetPhi_mes", 100, -3.2, 3.2) + self.book(f,"pfMetEt_tes","pfMetEt_tes", 50, 0, 100) + self.book(f,"pfMetPhi_tes","pfMetPhi_tes", 100, -3.2, 3.2) + self.book(f,"pfMetEt_ees","pfMetEt_ees", 50, 0, 100) + self.book(f,"pfMetPhi_ees","pfMetPhi_ees", 100, -3.2, 3.2) + self.book(f,"pfMetEt_ues","pfMetEt_ues", 50, 0, 100) + self.book(f,"pfMetPhi_ues", "pfMetPhi_ues", 100, -3.2, 3.2) + + self.book(f,"pfMetEt_perp_jes","pfMetEt_perp_jes", 50, 0, 100) + self.book(f,"pfMetEt_perp_mes","pfMetEt_perp_mes", 50, 0, 100 ) + self.book(f,"pfMetEt_perp_tes","pfMetEt_perp_tes", 50, 0, 100) + self.book(f,"pfMetEt_perp_ees","pfMetEt_perp_ees", 50, 0, 100) + self.book(f,"pfMetEt_perp_ues","pfMetEt_perp_ues", 50, 0, 100) + + self.book(f,"pfMetEt_par_jes","pfMetEt_par_jes", 100, -100, 100) + self.book(f,"pfMetEt_par_mes","pfMetEt_par_mes",100, -100, 100 ) + self.book(f,"pfMetEt_par_tes","pfMetEt_par_tes", 100, -100, 100) + self.book(f,"pfMetEt_par_ees","pfMetEt_par_ees", 100, -100, 100) + self.book(f,"pfMetEt_par_ues","pfMetEt_par_ues", 100, -100, 100) + + self.book(f,"e2PFMET_Mt_jes","e2PFMET_Mt_jes", 200, 0, 200 ) + self.book(f,"e2PFMET_Mt_mes","e2PFMET_Mt_mes", 200, 0, 200 ) + self.book(f,"e2PFMET_Mt_ees","e2PFMET_Mt_ees", 200, 0, 200 ) + self.book(f,"e2PFMET_Mt_tes","e2PFMET_Mt_tes", 200, 0, 200 ) + self.book(f,"e2PFMET_Mt_ues","e2PFMET_Mt_ues", 200, 0, 200 ) + + + self.book(f,"e1PFMET_Mt_jes","e1PFMET_Mt_jes", 200, 0, 200 ) + self.book(f,"e1PFMET_Mt_mes","e1PFMET_Mt_mes", 200, 0, 200 ) + self.book(f,"e1PFMET_Mt_ees","e1PFMET_Mt_ees", 200, 0, 200 ) + self.book(f,"e1PFMET_Mt_tes","e1PFMET_Mt_tes", 200, 0, 200 ) + self.book(f,"e1PFMET_Mt_ues","e1PFMET_Mt_ues", 200, 0, 200 ) + + self.book(f,"e2PFMET_DeltaPhi_jes","e2PFMET_DeltaPhi_jes", 50, 0, 3.2 ) + self.book(f,"e2PFMET_DeltaPhi_mes","e2PFMET_DeltaPhi_mes", 50, 0, 3.2 ) + self.book(f,"e2PFMET_DeltaPhi_ees","e2PFMET_DeltaPhi_ees", 50, 0, 3.2 ) + self.book(f,"e2PFMET_DeltaPhi_tes","e2PFMET_DeltaPhi_tes", 50, 0, 3.2 ) + self.book(f,"e2PFMET_DeltaPhi_ues","e2PFMET_DeltaPhi_ues", 50, 0, 3.2 ) + + + + self.book(f,"e1PFMET_DeltaPhi_jes","e1PFMET_DeltaPhi_jes", 50, 0, 3.2 ) + self.book(f,"e1PFMET_DeltaPhi_mes","e1PFMET_DeltaPhi_mes", 50, 0, 3.2 ) + self.book(f,"e1PFMET_DeltaPhi_ees","e1PFMET_DeltaPhi_ees", 50, 0, 3.2 ) + self.book(f,"e1PFMET_DeltaPhi_tes","e1PFMET_DeltaPhi_tes", 50, 0, 3.2 ) + self.book(f,"e1PFMET_DeltaPhi_ues","e1PFMET_DeltaPhi_ues", 50, 0, 3.2 ) + + + self.book(f, "nPV", "N of vertices", 100, 0, 100) + self.book(f, "nPV_unweighted", "unweighted N of vertices", 100, 0, 100) + + def fill_histos(self, row, f='os', fakeRate = False): + + weight = self.event_weight(row) # pu central, +1 sigma, -1sigma + + histos = self.histograms + pudir =[''] + if row.run < 2: pudir.extend( ['p1s/', 'm1s/', 'trp1s/', 'trm1s/', 'e1idp1s/','e1idm1s/', 'e1isop1s/','e1isom1s/','e2idp1s/','e2idm1s/', 'e2isop1s/','e2isom1s/' ]) + + for n,d in enumerate(pudir) : + + folder = d+f + + histos[folder+'/e1Pt'].Fill(row.e1Pt, weight[n]) + histos[folder+'/e1Eta'].Fill(row.e1Eta, weight[n]) + histos[folder+'/e1Phi'].Fill(row.e1Phi, weight[n]) + + histos[folder+'/e2Pt'].Fill(row.e2Pt, weight[n]) + histos[folder+'/e2Eta'].Fill(row.e2Eta, weight[n]) + histos[folder+'/e2Phi'].Fill(row.e2Phi, weight[n]) + + histos[folder+'/e1e2_DeltaPhi'].Fill(deltaPhi(row.e1Phi, row.e2Phi), weight[n]) + histos[folder+'/e1e2_DeltaR'].Fill(row.e1_e2_DR, weight[n]) + + histos[folder+'/e1e2Mass'].Fill(row.e1_e2_Mass, weight[n]) + + histos[folder+'/e1PFMET_DeltaPhi'].Fill(deltaPhi(row.e1Phi, row.pfMetPhi), weight[n]) + histos[folder+'/e1MVAMET_DeltaPhi'].Fill(deltaPhi(row.e1Phi, row.mva_metPhi), weight[n]) + histos[folder+'/e1PFMET_DeltaPhi_Ty1'].Fill(deltaPhi(row.e1Phi, row.type1_pfMetPhi), weight[n]) + histos[folder+'/e1PFMET_Mt'].Fill(row.e1MtToPFMET, weight[n]) + histos[folder+'/e1MVAMET_Mt'].Fill(row.e1MtToMVAMET, weight[n]) + histos[folder+'/e1PFMET_Mt_Ty1'].Fill(row.e1MtToPfMet_Ty1, weight[n]) + + histos[folder+'/type1_pfMetEt'].Fill(row.type1_pfMetEt, weight[n]) + histos[folder+'/pfMetEt'].Fill(row.pfMetEt, weight[n]) + histos[folder+'/mvaMetEt'].Fill(row.mva_metEt, weight[n]) + + histos[folder+'/type1_pfMetPhi'].Fill(row.type1_pfMetPhi, weight[n]) + histos[folder+'/pfMetPhi'].Fill(row.pfMetPhi, weight[n]) + histos[folder+'/mvaMetPhi'].Fill(row.mva_metPhi, weight[n]) + + zphi = Z(row).Phi() + histos[folder+'/type1_pfMetEt_par'].Fill(row.pfMetEt*cos(deltaPhi(zphi, row.type1_pfMetPhi)), weight[n]) + histos[folder+'/type1_pfMetEt_perp'].Fill(row.pfMetEt*sin(deltaPhi(zphi, row.type1_pfMetPhi)), weight[n]) + histos[folder+'/pfMetEt_par'].Fill(row.pfMetEt*cos(deltaPhi(zphi, row.pfMetPhi)), weight[n]) + histos[folder+'/pfMetEt_perp'].Fill(row.pfMetEt*sin(deltaPhi(zphi, row.pfMetPhi)), weight[n]) + histos[folder+'/mvaMetEt_par'].Fill(row.mva_metEt*cos(deltaPhi(zphi, row.mva_metPhi)), weight[n]) + histos[folder+'/mvaMetEt_perp'].Fill(row.mva_metEt*sin(deltaPhi(zphi, row.mva_metPhi)), weight[n]) + + histos[folder+'/e2PFMET_DeltaPhi'].Fill(deltaPhi(row.e2Phi, row.pfMetPhi), weight[n]) + histos[folder+'/e2MVAMET_DeltaPhi'].Fill(deltaPhi(row.e2Phi, row.mva_metPhi), weight[n]) + histos[folder+'/e2PFMET_DeltaPhi_Ty1'].Fill(deltaPhi(row.e2Phi, row.type1_pfMetPhi), weight[n]) + histos[folder+'/e2PFMET_Mt'].Fill(row.e2MtToPFMET, weight[n]) + histos[folder+'/e2PFMET_Mt_Ty1'].Fill(row.e2MtToPfMet_Ty1, weight[n]) + histos[folder+'/e2MVAMET_Mt'].Fill(row.e2MtToMVAMET, weight[n]) + + histos[folder+'/pfMetEt_jes'].Fill(row.pfMet_jes_Et, weight[n]) + histos[folder+'/pfMetPhi_jes'].Fill(row.pfMet_jes_Phi, weight[n]) + histos[folder+'/pfMetEt_mes'].Fill(row.pfMet_mes_Et, weight[n]) + histos[folder+'/pfMetPhi_mes'].Fill(row.pfMet_mes_Phi, weight[n]) + histos[folder+'/pfMetEt_tes'].Fill(row.pfMet_tes_Et, weight[n]) + histos[folder+'/pfMetPhi_tes'].Fill(row.pfMet_tes_Phi, weight[n]) + histos[folder+'/pfMetEt_ees'].Fill(row.pfMet_ees_Et, weight[n]) + histos[folder+'/pfMetPhi_ees'].Fill(row.pfMet_ees_Phi, weight[n]) + histos[folder+'/pfMetEt_ues'].Fill(row.pfMet_ues_Et, weight[n]) + histos[folder+'/pfMetPhi_ues'].Fill(row.pfMet_ues_Phi, weight[n]) + + histos[folder+'/pfMetEt_par_jes'].Fill(row.pfMet_jes_Et*cos(deltaPhi(zphi, row.pfMet_jes_Phi)), weight[n]) + histos[folder+'/pfMetEt_par_mes'].Fill(row.pfMet_mes_Et*cos(deltaPhi(zphi, row.pfMet_mes_Phi)), weight[n]) + histos[folder+'/pfMetEt_par_tes'].Fill(row.pfMet_tes_Et*cos(deltaPhi(zphi, row.pfMet_tes_Phi)), weight[n]) + histos[folder+'/pfMetEt_par_ees'].Fill(row.pfMet_ees_Et*cos(deltaPhi(zphi, row.pfMet_ees_Phi)), weight[n]) + histos[folder+'/pfMetEt_par_ues'].Fill(row.pfMet_ues_Et*cos(deltaPhi(zphi, row.pfMet_ues_Phi)), weight[n]) + + histos[folder+'/pfMetEt_perp_jes'].Fill(row.pfMet_jes_Et*sin(deltaPhi(zphi, row.pfMet_jes_Phi)), weight[n]) + histos[folder+'/pfMetEt_perp_mes'].Fill(row.pfMet_mes_Et*sin(deltaPhi(zphi, row.pfMet_mes_Phi)), weight[n]) + histos[folder+'/pfMetEt_perp_tes'].Fill(row.pfMet_tes_Et*sin(deltaPhi(zphi, row.pfMet_tes_Phi)), weight[n]) + histos[folder+'/pfMetEt_perp_ees'].Fill(row.pfMet_ees_Et*sin(deltaPhi(zphi, row.pfMet_ees_Phi)), weight[n]) + histos[folder+'/pfMetEt_perp_ues'].Fill(row.pfMet_ues_Et*sin(deltaPhi(zphi, row.pfMet_ues_Phi)), weight[n]) + + + histos[folder+'/e2PFMET_Mt_jes'].Fill(row.e2MtToPfMet_jes, weight[n]) + histos[folder+'/e2PFMET_Mt_mes'].Fill(row.e2MtToPfMet_mes, weight[n]) + histos[folder+'/e2PFMET_Mt_ees'].Fill(row.e2MtToPfMet_ees, weight[n]) + histos[folder+'/e2PFMET_Mt_tes'].Fill(row.e2MtToPfMet_tes, weight[n]) + histos[folder+'/e2PFMET_Mt_ues'].Fill(row.e2MtToPfMet_ues, weight[n]) + + histos[folder+'/e1PFMET_Mt_jes'].Fill(row.e1MtToPfMet_jes, weight[n]) + histos[folder+'/e1PFMET_Mt_mes'].Fill(row.e1MtToPfMet_mes, weight[n]) + histos[folder+'/e1PFMET_Mt_ees'].Fill(row.e1MtToPfMet_ees, weight[n]) + histos[folder+'/e1PFMET_Mt_tes'].Fill(row.e1MtToPfMet_tes, weight[n]) + histos[folder+'/e1PFMET_Mt_ues'].Fill(row.e1MtToPfMet_ues, weight[n]) + + histos[folder+'/e2PFMET_DeltaPhi_jes'].Fill(deltaPhi(row.e2Phi, row.pfMet_jes_Phi), weight[n]) + histos[folder+'/e2PFMET_DeltaPhi_mes'].Fill(deltaPhi(row.e2Phi, row.pfMet_mes_Phi), weight[n]) + histos[folder+'/e2PFMET_DeltaPhi_ees'].Fill(deltaPhi(row.e2Phi, row.pfMet_ees_Phi), weight[n]) + histos[folder+'/e2PFMET_DeltaPhi_tes'].Fill(deltaPhi(row.e2Phi, row.pfMet_tes_Phi), weight[n]) + histos[folder+'/e2PFMET_DeltaPhi_ues'].Fill(deltaPhi(row.e2Phi, row.pfMet_ues_Phi), weight[n]) + + histos[folder+'/e1PFMET_DeltaPhi_jes'].Fill(deltaPhi(row.e1Phi, row.pfMet_jes_Phi), weight[n]) + histos[folder+'/e1PFMET_DeltaPhi_mes'].Fill(deltaPhi(row.e1Phi, row.pfMet_mes_Phi), weight[n]) + histos[folder+'/e1PFMET_DeltaPhi_ees'].Fill(deltaPhi(row.e1Phi, row.pfMet_ees_Phi), weight[n]) + histos[folder+'/e1PFMET_DeltaPhi_tes'].Fill(deltaPhi(row.e1Phi, row.pfMet_tes_Phi), weight[n]) + histos[folder+'/e1PFMET_DeltaPhi_ues'].Fill(deltaPhi(row.e1Phi, row.pfMet_ues_Phi), weight[n]) + +## still some type1 met histos missing + + histos[folder+'/nPV'].Fill(row.nvtx, weight[n]) + histos[folder+'/nPV_unweighted'].Fill(row.nvtx) + + + + + + + def process(self): + event = () + filename = 'unnamed' + evts_processed = 0 + #set_trace() + for row in self.tree: + #for i, row in enumerate(self.tree): + # if i >= 100: + # return + + #current_file = self.tree.inputfilename + #if filename != 'unnamed' and current_file <> filename: + # self.histograms['jobInfo/jobInfo'].Fill([[i for i in filename], evts_processed]) + #if not filename == 'unnamed' or current_file <> filename: + # filename = current_file + evts_processed += 1 + #self.histograms[folder+'/jobInfo'].Fill(row) + + + jn = row.jetVeto30 + if jn > 3 : jn = 3 + sign = 'ss' if row.e1_e2_SS else 'os' + #if row.run > 2 : #apply the trigger to data only (MC triggers enter in the scale factors) + if not bool(row.singleE27WP80Pass) : continue + if not bool(row.e1MatchesSingleE27WP80) and not bool(row.e1MatchesSingleE27WP80) : continue + + if jn != 0 and row.bjetCSVVeto30!=0 : continue + + if row.e1Pt < 30 : continue + if row.e2Pt < 30 : continue + if not abs(row.e1_e2_Mass-91.2) < 20: continue + + if not selections.eSelection(row, 'e1'): continue + if not selections.lepton_id_iso(row, 'e1', 'eid13Tight_etauiso01'): continue + if abs(row.e1Eta) > 1.4442 and abs(row.e1Eta) < 1.566 : continue + + if not selections.eSelection(row, 'e2'): continue + if not selections.lepton_id_iso(row, 'e2', 'eid13Tight_etauiso01'): continue + if abs(row.e2Eta) > 1.4442 and abs(row.e2Eta) < 1.566 : continue + + + #if not selections.vetos(row) : continue + if row.muVetoPt5IsoIdVtx : continue + if row.eVetoCicLooseIso : continue # change it with Loose + if row.tauVetoPt20EleTight3MuLoose : continue +# if row.tauHpsVetoPt20 : continue + + + folder = sign + self.histograms[folder+'/evtInfo'].Fill(row) + new_event = (row.run, row.lumi, row.evt) + if event != new_event : + event = new_event + self.fill_histos(row, folder) + folder = sign+'/'+str(int(jn)) + self.fill_histos(row, folder) + + if filename != 'unnamed': + self.histograms['jobInfo/jobInfo'].Fill([[i for i in filename], evts_processed]) + + def finish(self): + self.write_histos() + + diff --git a/lfvetau/EleFakeRateAnalyzerMVA.py b/lfvetau/EleFakeRateAnalyzerMVA.py new file mode 100644 index 00000000..461c2664 --- /dev/null +++ b/lfvetau/EleFakeRateAnalyzerMVA.py @@ -0,0 +1,584 @@ +##Correction Factor still to add +from EEETree import EEETree +import os +import ROOT +import math +import optimizer +import glob +import array +import mcCorrections +import baseSelections as selections +import FinalStateAnalysis.PlotTools.pytree as pytree +from FinalStateAnalysis.PlotTools.decorators import memo_last +from FinalStateAnalysis.PlotTools.MegaBase import MegaBase +from math import sqrt, pi, sin, cos, acos, sinh +from cutflowtracker import cut_flow_tracker +#Makes the cut flow histogram +cut_flow_step = ['allEvents', 'e1sel', 'e1IDiso', 'e2sel', 'e2IDiso', 'ZMass', 'tsel', 'tAntiMuon', 'tAntiEle', 'MtToMet', 'tRawIso10','tRawIso5', 'tLooseIso', 'tTightIso' +] + +from inspect import currentframe + +def get_linenumber(): + cf = currentframe() + return cf.f_back.f_lineno + +def deltaPhi(phi1, phi2): + PHI = abs(phi1-phi2) + if PHI<=pi: + return PHI + else: + return 2*pi-PHI +def deltaR(phi1, phi2, eta1, eta2): + deta = eta1 - eta2 + dphi = abs(phi1-phi2) + if (dphi>pi) : dphi = 2*pi-dphi + return sqrt(deta*deta + dphi*dphi); + + +class EleFakeRateAnalyzerMVA(MegaBase): + tree = 'eee/final/Ntuple' + def __init__(self, tree, outfile, **kwargs): + self.channel='EET' + super(EleFakeRateAnalyzerMVA, self).__init__(tree, outfile, **kwargs) + self.tree = EEETree(tree) + self.out=outfile + self.histograms = {} + self.pucorrector = mcCorrections.make_puCorrector('singlee') + self.mye1 = 'e1' + self.mye2 = 'e2' + self.mye3 = 'e3' + #optimizer_keys = [ i for i in optimizer.grid_search.keys() if i.startswith(self.channel) ] + self.grid_search = {} + #if len(optimizer_keys) > 1: + # for key in optimizer_keys: + # self.grid_search[key] = optimizer.grid_search[key] + #else: + # self.grid_search[''] = optimizer.grid_search[optimizer_keys[0]] + + + def event_weight(self, row): + if row.run > 2: #FIXME! add tight ID correction + return 1. + + + #if bool(row.e1MatchesEle27WP80) and not bool(row.e2MatchesEle27WP80) : etrig = 'e1' + #if not bool(row.e1MatchesEle27WP80) and bool(row.e2MatchesEle27WP80) : etrig = 'e2' + return self.pucorrector(row.nTruePU) * \ + mcCorrections.eid_correction( row, self.mye1, self.mye2, self.mye3) * \ + mcCorrections.eiso_correction(row, self.mye1, self.mye2, self.mye3) * \ + mcCorrections.trig_correction(row, self.mye3 ) + + def ee3DR(self, row): + mye1_mye3_dr = 100. + mye2_mye3_dr = 100. + try: + mye1_mye3_dr = getattr(row, self.mye1+'_'+self.mye3+'_DR') + except AttributeError: + mye1_mye3_dr =getattr(row, self.mye3+'_'+self.mye1+'_DR') + try : + mye2_mye3_dr = getattr(row, self.mye2+'_'+self.mye3+'_DR') + except AttributeError: + mye2_mye3_dr =getattr(row, self.mye3+'_'+self.mye2+'_DR') + + return mye1_mye3_dr if mye1_mye3_dr < mye2_mye3_dr else mye1_mye3_dr + + def ee3DPhi(self, row): + e1e3DPhi=deltaPhi(getattr(row, self.mye1+'Phi'), getattr(row, self.mye3+'Phi')) + e2e3DPhi=deltaPhi(getattr(row, self.mye2+'Phi'), getattr(row, self.mye3+'Phi')) + return e1e3DPhi if e1e3DPhi < e2e3DPhi else e2e3DPhi + + def Z(self, row): + e1p=ROOT.TVector3(getattr(row, self.mye1+'Pt')*cos(getattr(row, self.mye1+'Phi')),getattr(row, self.mye1+'Pt')*sin(getattr(row, self.mye1+'Phi')),getattr(row, self.mye1+'Pt')*sin(getattr(row, self.mye1+'Eta'))) + e2p=ROOT.TVector3(getattr(row, self.mye2+'Pt')*cos(getattr(row, self.mye2+'Phi')),getattr(row, self.mye2+'Pt')*sin(getattr(row, self.mye2+'Phi')),getattr(row, self.mye2+'Pt')*sin(getattr(row, self.mye2+'Eta'))) + e1FourVector= ROOT.TLorentzVector(e1p, sqrt(e1p.Mag2()+pow(getattr(row, self.mye1+'Mass'),2))) + e2FourVector= ROOT.TLorentzVector(e2p, sqrt(e2p.Mag2()+pow(getattr(row, self.mye2+'Mass'),2))) + zFourVector = e1FourVector+e2FourVector + return zFourVector + + + + +##add the trigger correction + + def begin(self): + + eiso = ['eLoose', 'eTigh'] + folder = [] + sign = ['ss','os'] + for iso in eiso: + for s in sign: + folder.append(s+'/'+iso) + j=0 + while j < 4 : + folder.append(s+'/'+iso+'/'+str(j)) + j+=1 + + for f in folder: + + ##self.book(f,"e1Pt", "e1 p_{T}", 200, 0, 200) + ##self.book(f,"e1Phi", "e1 phi", 100, -3.2, 3.2) + ##self.book(f,"e1Eta", "e1 eta", 46, -2.3, 2.3) + ## + ##self.book(f,"e2Pt", "e2 p_{T}", 200, 0, 200) + ##self.book(f,"e2Phi", "e2 phi", 100, -3.2, 3.2) + ##self.book(f,"e2Eta", "e2 eta", 46, -2.3, 2.3) + + self.book(f,"e3Pt", "e3 p_{T}", 200, 0, 200) + ##self.book(f,"e3Phi", "e3 phi", 100, -3.2, 3.2) + self.book(f,"e3Eta", "e3 eta", 46, -2.3, 2.3) + self.book(f,"e3AbsEta", "e3 abs eta", 23, 0, 2.3) + self.book(f,"e3Pt_vs_e3AbsEta", "e3 pt vs e3 abs eta", 23, 0, 2.3, 20, 0, 200., type=ROOT.TH2F) + + ##self.book(f, "e1e2Mass", "e1e2 Inv Mass", 32, 0, 320) + ## + ##self.book(f, "e3MtToPFMET", "e3 Met MT", 100, 0, 100) + ## + ## + ##self.book(f,"ee3DR", "e e3 DR", 50, 0, 10) + ##self.book(f,"ee3DPhi", "e e3 DPhi", 32, 0, 3.2) + ## + ##self.book(f,"ze3DR", "Z e3 DR", 50, 0, 10) + ##self.book(f,"ze3DPhi", "Z e3 DPhi", 32, 0, 3.2) + ##self.book(f,"Zpt", "Z p_{T}", 200, 0, 200) + ## + ## + ## + ##self.book(f, "type1_pfMetEt", "type1_pfMetEt",200, 0, 200) + ##self.book(f, "jetN_30", "Number of jets, p_{T}>30", 10, -0.5, 9.5) + ##self.book(f, "bjetCSVVeto30", "number of bjets", 10, -0.5, 9.5) + + for s in sign: + self.book(s+'/tNoCuts', "CUT_FLOW", "Cut Flow", len(cut_flow_step), 0, len(cut_flow_step)) + + xaxis = self.histograms[s+'/tNoCuts/CUT_FLOW'].GetXaxis() + self.cut_flow_histo = self.histograms[s+'/tNoCuts/CUT_FLOW'] + self.cut_flow_map = {} + for i, name in enumerate(cut_flow_step): + xaxis.SetBinLabel(i+1, name) + self.cut_flow_map[name] = i+0.5 + + def fill_histos(self, row, folder='os/tSuperLoose', fakeRate = False): + weight = self.event_weight(row) + histos = self.histograms + + + + + ##histos[folder+'/e1Pt'].Fill( getattr(row, self.mye1+'Pt'), weight) + ##histos[folder+'/e1Eta'].Fill(getattr(row, self.mye1+'Eta'), weight) + ##histos[folder+'/e1Phi'].Fill(getattr(row, self.mye1+'Phi'), weight) + ## + ##histos[folder+'/e2Pt'].Fill( getattr(row, self.mye2+'Pt' ), weight) + ##histos[folder+'/e2Eta'].Fill(getattr(row, self.mye2+'Eta'), weight) + ##histos[folder+'/e2Phi'].Fill(getattr(row, self.mye2+'Phi'), weight) + + histos[folder+'/e3Pt'].Fill( getattr(row, self.mye3+'Pt' ), weight) + histos[folder+'/e3Eta'].Fill(getattr(row, self.mye3+'Eta'), weight) + ##histos[folder+'/e3Phi'].Fill(getattr(row, self.mye3+'Phi'), weight) + histos[folder+'/e3AbsEta'].Fill(abs(getattr(row, self.mye3+'Eta')), weight) + histos[folder+'/e3Pt_vs_e3AbsEta'].Fill(abs(getattr(row, self.mye3+'Eta')), getattr(row, self.mye3+'Pt'), weight) + + ##histos[folder+'/e1e2Mass'].Fill(getattr(row, self.mye1+'_'+self.mye2+'_Mass'), weight) + ###histos[folder+'/tMtToPFMET'].Fill(row.tMtToPFMET,weight) + ## + ## + ##histos[folder+'/type1_pfMetEt'].Fill(row.type1_pfMet_Et) + ##histos[folder+'/ee3DR'].Fill(self.ee3DR(row)) + ##histos[folder+'/ee3DPhi'].Fill(self.ee3DPhi(row)) + ##histos[folder+'/jetN_30'].Fill(row.jetVeto30, weight) + ##histos[folder+'/bjetCSVVeto30'].Fill(row.bjetCSVVeto30, weight) + ## + ##histos[folder+'/ze3DR'].Fill(deltaR(self.Z(row).Phi(), getattr(row, self.mye3+'Phi'), self.Z(row).Eta(), getattr(row, self.mye3+'Eta'))) + ##histos[folder+'/ze3DPhi'].Fill(deltaPhi(self.Z(row).Phi(), getattr(row, self.mye3+'Phi'))) + ##histos[folder+'/Zpt'].Fill(self.Z(row).Pt()) + + + def process(self): + + cut_flow_histo = self.cut_flow_histo + cut_flow_trk = cut_flow_tracker(cut_flow_histo) + myevent =() + #print self.tree.inputfilename + for row in self.tree: + jn = row.jetVeto30 + if jn > 3 : jn = 3 + + #if row.run > 2: + if not bool(row.singleE27WP80Pass) : continue + # if hasattr(self.tree, 'row.e1MatchesEle27WP80') and hasattr(self.tree, 'row.e2MatchesEle27WP80') : + #if not bool(row.e1MatchesEle27WP80) and not bool(row.e2MatchesEle27WP80) : continue + + #else : + if not bool(row.e3MatchesSingleE27WP80) : continue + #if not bool(row.singleEPass) : continue + #if not bool(row.e1MatchesSingleE) and not bool(row.e2MatchesSingleE) : continue + + if row.bjetCSVVeto30!=0 : continue + if row.e1Pt < 30 : continue + if row.e2Pt < 30 : continue + if row.e3Pt < 30 : continue + +# for i, row in enumerate(self.tree): +# if i >= 100: +# return + # print bool(cut_flow_trk.disabled) + cut_flow_trk.new_row(row.run,row.lumi,row.evt) + #print row.run,row.lumi,row.evt + cut_flow_trk.Fill('allEvents') + if not selections.eSelection(row, 'e1'): continue + cut_flow_trk.Fill('e1sel') + if not selections.lepton_id_iso(row, 'e1', 'eid13Loose_idiso05'): continue + if abs(row.e1Eta) > 1.4442 and abs(row.e1Eta < 1.566) : continue + + + cut_flow_trk.Fill('e1IDiso') + if not selections.eSelection(row, 'e2'): continue + cut_flow_trk.Fill('e2sel') + if not selections.lepton_id_iso(row, 'e2', 'eid13Loose_idiso05'): continue + if abs(row.e2Eta) > 1.4442 and abs(row.e2Eta) < 1.566 : continue + +## cut_flow_trk.Fill('e2IDiso') +## if not abs(row.e1_e2_Mass-91.2) < 20: continue +## cut_flow_trk.Fill('ZMass') + if not selections.eSelection(row, 'e3'): continue + if not selections.lepton_id_iso(row, 'e3', 'eid13Loose_idiso05'): continue #very loose loose eid13Tight_mvaLoose + if abs(row.e3Eta) > 1.4442 and abs(row.e3Eta) < 1.566 : continue + + Zs= [(abs(row.e1_e2_Mass-91.2), ['e1', 'e2', 'e3']) , (abs(row.e2_e3_Mass-91.2), ['e2', 'e3', 'e1']), (abs(row.e1_e3_Mass-91.2), ['e1', 'e3', 'e2'])] + + for ele in range(0, 2) : + + if Zs[ele][0] == min(Zs[z][0] for z in range (0,2)) : + self.mye1 = Zs[ele][1][0] + self.mye2 = Zs[ele][1][1] + self.mye3 = Zs[ele][1][2] + + cut_flow_trk.Fill('tsel') + + + if row.tauVetoPt20EleTight3MuLoose : continue + if row.muVetoPt5IsoIdVtx : continue + if row.eVetoCicLooseIso : continue # change it with Loose + + #if not row.e3MtToMET < 50: continue + cut_flow_trk.Fill('MtToMet') + + + #if (row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt)==myevent: continue + #myevent=(row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt) + + eleiso = 'eLoose' + sign = 'ss' if row.e1_e2_SS else 'os' + folder = sign+'/'+eleiso + + self.fill_histos(row, folder) + folder=folder+'/'+str(int(jn)) + self.fill_histos(row, folder) + + if selections.lepton_id_iso(row, 'e3', 'eid13Loose_etauiso01'): + eleiso = 'eTigh' + folder = sign+'/'+eleiso + self.fill_histos(row, folder) + cut_flow_trk.Fill('tTightIso') + folder=folder+'/'+str(int(jn)) + self.fill_histos(row, folder) + + + + + cut_flow_trk.flush() + + + + def finish(self): + self.write_histos() + +####Correction Factor still to add +##from EEETree import EEETree +##import os +##import ROOT +##import math +##import optimizer +##import glob +##import array +##import mcCorrections +##import baseSelections as selections +##import FinalStateAnalysis.PlotTools.pytree as pytree +##from FinalStateAnalysis.PlotTools.decorators import memo_last +##from FinalStateAnalysis.PlotTools.MegaBase import MegaBase +##from math import sqrt, pi, sin, cos, acos, sinh +##from cutflowtracker import cut_flow_tracker +###Makes the cut flow histogram +##cut_flow_step = ['allEvents', 'e1sel', 'e1IDiso', 'e2sel', 'e2IDiso', 'ZMass', 'e3Pt','e3Eta','e3missinHits','e3conversion','e3ChargeId','e3Btag','e3DZ','tsel', 'MtToMet', 'tTightIso' +##] +## +##from inspect import currentframe +## +##def get_linenumber(): +## cf = currentframe() +## return cf.f_back.f_lineno +## +##def deltaPhi(phi1, phi2): +## PHI = abs(phi1-phi2) +## if PHI<=pi: +## return PHI +## else: +## return 2*pi-PHI +##def deltaR(phi1, phi2, eta1, eta2): +## deta = eta1 - eta2 +## dphi = abs(phi1-phi2) +## if (dphi>pi) : dphi = 2*pi-dphi +## return sqrt(deta*deta + dphi*dphi); +## +##def ee3DR(row): +## return row.e1_e3_DR if row.e1_e3_DR < row.e2_e3_DR else row.e2_e3_DR +## +##def ee3DPhi(row): +## e1e3DPhi=deltaPhi(row.e1Phi, row.e3Phi) +## e2e3DPhi=deltaPhi(row.e2Phi, row.e3Phi) +## return e1e3DPhi if e1e3DPhi < e2e3DPhi else e2e3DPhi +## +##def Z(row): +## e1p=ROOT.TVector3(row.e1Pt*cos(row.e1Phi),row.e1Pt*sin(row.e1Phi),row.e1Pt*sinh(row.e1Eta)) +## e2p=ROOT.TVector3(row.e2Pt*cos(row.e2Phi),row.e2Pt*sin(row.e2Phi),row.e2Pt*sinh(row.e2Eta)) +## e1FourVector= ROOT.TLorentzVector(e1p, sqrt(e1p.Mag2()+row.e1Mass*row.e1Mass)) +## e2FourVector= ROOT.TLorentzVector(e2p, sqrt(e2p.Mag2()+row.e2Mass*row.e2Mass)) +## zFourVector = e1FourVector+e2FourVector +## return zFourVector +## +## +##class EleFakeRateAnalyzerMVA(MegaBase): +## tree = 'eee/final/Ntuple' +## def __init__(self, tree, outfile, **kwargs): +## self.channel='EET' +## super(EleFakeRateAnalyzerMVA, self).__init__(tree, outfile, **kwargs) +## self.tree = EEETree(tree) +## self.out=outfile +## self.histograms = {} +## self.pucorrector = mcCorrections.make_puCorrector('singlee') +## +## #optimizer_keys = [ i for i in optimizer.grid_search.keys() if i.startswith(self.channel) ] +## ##self.grid_search = {} +## ##if len(optimizer_keys) > 1: +## ## for key in optimizer_keys: +## ## self.grid_search[key] = optimizer.grid_search[key] +## ##else: +## ## self.grid_search[''] = optimizer.grid_search[optimizer_keys[0]] +## +## +## def event_weight(self, row): +## if row.run > 2: #FIXME! add tight ID correction +## return 1. +## +## etrig = 'e1' +## if row.e2Pt > row.e1Pt : etrig = 'e2' +## if bool(row.e1MatchesSingleE27WP80) and not bool(row.e2MatchesSingleE27WP80) : etrig = 'e1' +## if not bool(row.e1MatchesSingleE27WP80) and bool(row.e2MatchesSingleE27WP80) : etrig = 'e2' +## +## if bool(row.e3MatchesSingleE27WP80) and not bool(row.e1MatchesSingleE27WP80) and not bool(row.e2MatchesSingleE27WP80) : etrig ='e3' +## +## return self.pucorrector(row.nTruePU) * \ +## mcCorrections.eid_correction( row, 'e1', 'e2', 'e3') * \ +## mcCorrections.eiso_correction(row, 'e1', 'e2', 'e3') * \ +## mcCorrections.trig_correction(row, etrig ) +## # mcCorrections.trig_correction(row, 'e3' ) +## +####add the trigger correction +## +## def begin(self): +## +## eiso = ['eLoose', 'eTigh'] +## folder = [] +## sign = ['ss','os'] +## for iso in eiso: +## for s in sign: +## folder.append(s+'/'+iso) +## j=0 +## while j < 4 : +## folder.append(s+'/'+iso+'/'+str(j)) +## j+=1 +## +## for f in folder: +## +## ##self.book(f,"e1Pt", "e1 p_{T}", 200, 0, 200) +## ##self.book(f,"e1Phi", "e1 phi", 100, -3.2, 3.2) +## ##self.book(f,"e1Eta", "e1 eta", 46, -2.3, 2.3) +## ## +## ##self.book(f,"e2Pt", "e2 p_{T}", 200, 0, 200) +## ##self.book(f,"e2Phi", "e2 phi", 100, -3.2, 3.2) +## ##self.book(f,"e2Eta", "e2 eta", 46, -2.3, 2.3) +## +## self.book(f,"e3Pt", "e3 p_{T}", 200, 0, 200) +## ##self.book(f,"e3Phi", "e3 phi", 100, -3.2, 3.2) +## self.book(f,"e3Eta", "e3 eta", 46, -2.3, 2.3) +## self.book(f,"e3AbsEta", "e3 abs eta", 23, 0, 2.3) +## +## ##self.book(f, "e1e2Mass", "e1e2 Inv Mass", 32, 0, 320) +## ## +## ##self.book(f, "e3MtToPFMET", "e3 Met MT", 100, 0, 100) +## ## +## ## +## ##self.book(f,"ee3DR", "e e3 DR", 50, 0, 10) +## ##self.book(f,"ee3DPhi", "e e3 DPhi", 32, 0, 3.2) +## ## +## ##self.book(f,"ze3DR", "Z e3 DR", 50, 0, 10) +## ##self.book(f,"ze3DPhi", "Z e3 DPhi", 32, 0, 3.2) +## ##self.book(f,"Zpt", "Z p_{T}", 200, 0, 200) +## ## +## ## +## ## +## ##self.book(f, "type1_pfMetEt", "type1_pfMetEt",200, 0, 200) +## ##self.book(f, "jetN_30", "Number of jets, p_{T}>30", 10, -0.5, 9.5) +## ##self.book(f, "bjetCSVVeto30", "number of bjets", 10, -0.5, 9.5) +## +## for s in sign: +## self.book(s+'/tNoCuts', "CUT_FLOW", "Cut Flow", len(cut_flow_step), 0, len(cut_flow_step)) +## +## xaxis = self.histograms[s+'/tNoCuts/CUT_FLOW'].GetXaxis() +## self.cut_flow_histo = self.histograms[s+'/tNoCuts/CUT_FLOW'] +## self.cut_flow_map = {} +## for i, name in enumerate(cut_flow_step): +## xaxis.SetBinLabel(i+1, name) +## self.cut_flow_map[name] = i+0.5 +## +## def fill_histos(self, row, folder='os/tSuperLoose', fakeRate = False): +## weight = self.event_weight(row) +## histos = self.histograms +## +## ##histos[folder+'/e1Pt'].Fill(row.e1Pt, weight) +## ##histos[folder+'/e1Eta'].Fill(row.e1Eta, weight) +## ##histos[folder+'/e1Phi'].Fill(row.e1Phi, weight) +## ## +## ##histos[folder+'/e2Pt'].Fill(row.e2Pt, weight) +## ##histos[folder+'/e2Eta'].Fill(row.e2Eta, weight) +## ##histos[folder+'/e2Phi'].Fill(row.e2Phi, weight) +## +## histos[folder+'/e3Pt'].Fill(row.e3Pt, weight) +## histos[folder+'/e3Eta'].Fill(row.e3Eta, weight) +## histos[folder+'/e3AbsEta'].Fill(abs(row.e3Eta), weight) +## ##histos[folder+'/e3Phi'].Fill(row.e3Phi, weight) +## +## ##histos[folder+'/e1e2Mass'].Fill(row.e1_e2_Mass, weight) +## ###histos[folder+'/tMtToPFMET'].Fill(row.tMtToPFMET,weight) +## ## +## ## +## ##histos[folder+'/type1_pfMetEt'].Fill(row.type1_pfMet_Et) +## ##histos[folder+'/ee3DR'].Fill(ee3DR(row)) +## ##histos[folder+'/ee3DPhi'].Fill(ee3DPhi(row)) +## ##histos[folder+'/jetN_30'].Fill(row.jetVeto30, weight) +## ##histos[folder+'/bjetCSVVeto30'].Fill(row.bjetCSVVeto30, weight) +## ## +## ##histos[folder+'/ze3DR'].Fill(deltaR(Z(row).Phi(), row.e3Phi, Z(row).Eta(), row.e3Eta)) +## ##histos[folder+'/ze3DPhi'].Fill(deltaPhi(Z(row).Phi(), row.e3Phi)) +## ##histos[folder+'/Zpt'].Fill(Z(row).Pt()) +## +## +## def process(self): +## +## cut_flow_histo = self.cut_flow_histo +## cut_flow_trk = cut_flow_tracker(cut_flow_histo) +## myevent =() +## #print self.tree.inputfilename +## for row in self.tree: +## jn = row.jetVeto30 +## if jn > 3 : jn = 3 +## +## #if row.run > 2: +## if not bool(row.singleE27WP80Pass) : continue +## # if hasattr(self.tree, 'row.e1MatchesEle27WP80') and hasattr(self.tree, 'row.e2MatchesEle27WP80') : +## #if not bool(row.e1MatchesEle27WP80) and not bool(row.e2MatchesEle27WP80) : continue +## +## #else : +## if not bool(row.e3MatchesSingleE27WP80) : continue +## #if not bool(row.singleEPass) : continue +## #if not bool(row.e1MatchesSingleE) and not bool(row.e2MatchesSingleE) : continue +## +## if row.bjetCSVVeto30!=0 : continue +## if row.e1Pt < 30 : continue +## if row.e2Pt < 30 : continue +## +### for i, row in enumerate(self.tree): +### if i >= 100: +### return +## # print bool(cut_flow_trk.disabled) +## cut_flow_trk.new_row(row.run,row.lumi,row.evt) +## #print row.run,row.lumi,row.evt +## cut_flow_trk.Fill('allEvents') +## if not selections.eSelection(row, 'e1'): continue +## cut_flow_trk.Fill('e1sel') +## if not selections.lepton_id_iso(row, 'e1', 'eid13Loose_idiso02'): continue +## if abs(row.e1Eta) > 1.4442 and abs(row.e1Eta < 1.566) : continue +## +## +## cut_flow_trk.Fill('e1IDiso') +## if not selections.eSelection(row, 'e2'): continue +## cut_flow_trk.Fill('e2sel') +## if not selections.lepton_id_iso(row, 'e2', 'eid13Loose_idiso02'): continue +## if abs(row.e2Eta) > 1.4442 and abs(row.e2Eta) < 1.566 : continue +## +## cut_flow_trk.Fill('e2IDiso') +## if not abs(row.e1_e2_Mass-91.2) < 20: continue +## cut_flow_trk.Fill('ZMass') +## #if not selections.eSelection(row, 'e3'): continue +## +## if row.e3Pt < 30: continue +## cut_flow_trk.Fill('e3Pt') +## if abs(row.e3Eta) >2.3: continue +## if abs(row.e3Eta) > 1.4442 and abs(row.e3Eta) < 1.566 : continue +#### cut_flow_trk.Fill('e3Eta') +#### if row.e3MissingHits: continue +#### cut_flow_trk.Fill('e3missinHits') +#### if row.e3HasConversion: continue +#### cut_flow_trk.Fill('e3conversion') +#### if not row.e3ChargeIdLoose: continue +#### cut_flow_trk.Fill('e3ChargeId') +#### if row.e3JetCSVBtag >0.8: continue +#### cut_flow_trk.Fill('e3Btag') +#### if row.e3DZ >0.2: continue +#### cut_flow_trk.Fill('e3DZ') +## +## ##if not selections.lepton_id_iso(row, 'e3', 'eid13Loose_idiso02'): continue #very loose loose eid13Tight_mvaLoose +## if not selections.lepton_id_iso(row, 'e3', 'eid13Tight_idiso02'): continue #very loose loose eid13Tight_mvaLoose +## +## cut_flow_trk.Fill('tsel') +## +## +## if row.tauVetoPt20EleTight3MuLoose : continue +## #if row.tauHpsVetoPt20 : continue +## if row.muVetoPt5IsoIdVtx : continue +## if row.eVetoCicLooseIso : continue # change it with Loose +## +## #if not row.e3MtToMET < 50: continue +## cut_flow_trk.Fill('MtToMet') +## +## # if etDR(row) < 1. : continue +## ##if (row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt)==myevent: continue +## ##myevent=(row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt) +## +## eleiso = 'eLoose' +## sign = 'ss' if row.e1_e2_SS else 'os' +## folder = sign+'/'+eleiso +## +## self.fill_histos(row, folder) +## folder=folder+'/'+str(int(jn)) +## self.fill_histos(row, folder) +## +## if selections.lepton_id_iso(row, 'e3', 'eid13Tight_etauiso01'): +## eleiso = 'eTigh' +## folder = sign+'/'+eleiso +## self.fill_histos(row, folder) +## cut_flow_trk.Fill('tTightIso') +## folder=folder+'/'+str(int(jn)) +## self.fill_histos(row, folder) +## +## +## +## +## cut_flow_trk.flush() +## +## +## +## def finish(self): +## self.write_histos() +## diff --git a/lfvetau/EmbeddedCheck.py b/lfvetau/EmbeddedCheck.py new file mode 100644 index 00000000..96bec627 --- /dev/null +++ b/lfvetau/EmbeddedCheck.py @@ -0,0 +1,623 @@ +from ETauTree import ETauTree +import sys +import logging +logging.basicConfig(stream=sys.stderr, level=logging.WARNING) +import os +from pdb import set_trace +import ROOT +import math +import glob +import array +import mcCorrections +import baseSelections as selections +import FinalStateAnalysis.PlotTools.pytree as pytree +from FinalStateAnalysis.PlotTools.decorators import memo_last +from FinalStateAnalysis.PlotTools.MegaBase import MegaBase +from math import sqrt, pi, cos +from fakerate_functions import tau_fake_rate, tau_fake_rate_up, tau_fake_rate_dw, e_fake_rate, e_fake_rate_up, e_fake_rate_dw +import itertools +import traceback +from FinalStateAnalysis.PlotTools.decorators import memo +import FinalStateAnalysis.PlotTools.pytree as pytree +from FinalStateAnalysis.Utilities.struct import struct + +@memo +def getVar(name, var): + return name+var + +met_et = 'pfMet_Et%s' +met_phi = 'pfMet_Phi%s' +t_pt = 'tPt%s' +etMass = 'e_t_Mass%s' +@memo +def met(shift=''): + return met_et % shift + +@memo +def metphi(shift=''): + return met_phi % shift +@memo +def tpt(shift=''): + return t_pt % shift + +@memo +def vismass(shift=''): + return etMass % shift + +@memo +def split(string, separator='#'): + return tuple(attr.split(separator)) + +def create_mapper(mapping): + def _f(path): + for key, out in mapping.iteritems(): + if key == path: + path = path.replace(key,out) + print 'path', path + return path + return _f + +def attr_getter(attribute): + '''return a function that gets an attribute''' + def f(row, weight): + return (getattr(row,attribute), weight) + return f + +def merge_functions(fcn_1, fcn_2): + '''merges two functions to become a TH2''' + def f(row, weight): + r1, w1 = fcn_1(row, weight) + r2, w2 = fcn_2(row, weight) + w = w1 if w1 and w2 else None + return ((r1, r2), w) + return f + +def collmass(row, met, metPhi): + ptnu =abs(met*cos(deltaPhi(metPhi, row.tPhi))) + visfrac = row.tPt/(row.tPt+ptnu) + #print met, cos(deltaPhi(metPhi, row.tPhi)), ptnu, visfrac + return (row.e_t_Mass / sqrt(visfrac)) + +def deltaPhi(phi1, phi2): + PHI = abs(phi1-phi2) + if PHI<=pi: + return PHI + else: + return 2*pi-PHI + +def deltaR(phi1, ph2, eta1, eta2): + deta = eta1 - eta2 + dphi = abs(phi1-phi2) + if (dphi>pi) : dphi = 2*pi-dphi + return sqrt(deta*deta + dphi*dphi); + +def make_collmass_systematics(shift): + if shift.startswith('tes'): + ptnu =abs(met*cos(deltaPhi(metPhi, row.tPhi))) + visfrac = tpt/(tpt+ptnu) + vis_mass = vismass(shift) + return (vis_mass / sqrt(visfrac)) + else: + met_name = met(shift) + phi_name = metphi(shift) + def collmass_shifted(row, weight): + met = getattr(row, met_name) + phi = getattr(row, phi_name) + return collmass(row, met, phi), weight + return collmass_shifted + +class EmbeddedCheck(MegaBase): + tree = 'et/final/Ntuple' + def __init__(self, tree, outfile, **kwargs): + logging.debug('EmbeddedCheck constructor') + self.channel='ET' + super(EmbeddedCheck, self).__init__(tree, outfile, **kwargs) + self.tree = ETauTree(tree) + self.out=outfile + self.histograms = {} + + #understand what we are running + target = os.path.basename(os.environ['megatarget']) + self.is_data = target.startswith('data_') + self.is_embedded = ('Embedded' in target) + self.is_mc = not (self.is_data or self.is_embedded) + self.efake = e_fake_rate(0.2) + self.efakeup = e_fake_rate_up(0.2) + self.efakedw = e_fake_rate_dw(0.2) + + #systematics used + self.systematics = { + 'trig' : (['', 'trp1s', 'trm1s'] if not self.is_data else []), + 'pu' : (['', 'p1s', 'm1s'] if self.is_mc else []), + 'eid' : (['', 'eidp1s','eidm1s'] if not self.is_data else []), + 'eiso' : (['', 'eisop1s','eisom1s'] if not self.is_data else []), + 'jes' : (['', '_jes_plus','_jes_minus'] if self.is_mc else ['']), + 'mvetos': (['', 'mVetoUp', 'mVetoDown'] if self.is_mc else ['']), + 'tvetos': (['', 'tVetoUp', 'tVetoDown'] if self.is_mc else ['']), + 'evetos': (['', 'eVetoUp', 'eVetoDown'] if self.is_mc else ['']), + 'met' : (["_mes_plus", "_ues_plus", "_mes_minus", "_ues_minus"] if self.is_mc else []), + 'tes' : (["", "_tes_plus", "_tes_minus"] if not self.is_data else ['']), + 'ees' : (["", "_ees_plus", '_ees_minus'] if not self.is_data else ['']) + } + + #self filling histograms + coll_mass = make_collmass_systematics('') #no sys shift + self.histo_locations = {} #just a mapping of the histograms we have to avoid changing self.histograms indexing an screw other files + self.hfunc = { #maps the name of non-trivial histograms to a function to get the proper value, the function MUST have two args (evt and weight). Used in fill_histos later + 'nTruePU' : lambda row, weight: (row.nTruePU,None), + 'weight' : lambda row, weight: (weight,None) if weight is not None else (1.,None), + 'Event_ID': lambda row, weight: (array.array("f", [row.run,row.lumi,int(row.evt)/10**5,int(row.evt)%10**5] ), None), + 'h_collmass_pfmet' : coll_mass, + 'h_collmass_vs_dPhi_pfmet' : merge_functions( + attr_getter('tToMETDPhi'), + coll_mass + ), + 'MetEt_vs_dPhi' : merge_functions( + lambda row, weight: (deltaPhi(row.tPhi, getattr(row, metphi())), weight), + attr_getter('type1_pfMet_Et') + ), + 'ePFMET_DeltaPhi' : lambda row, weight: (deltaPhi(row.ePhi, getattr(row, metphi())), weight), + 'tPFMET_DeltaPhi' : lambda row, weight: (deltaPhi(row.tPhi, getattr(row, metphi())), weight), + 'evtInfo' : lambda row, weight: (struct(run=row.run,lumi=row.lumi,evt=row.evt,weight=weight), None) + } + for shift in self.systematics['met']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + for shift in self.systematics['tes']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + for shift in self.systematics['jes']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + for shift in self.systematics['ees']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + + + #PU correctors + self.pucorrector = mcCorrections.make_shifted_weights( + mcCorrections.make_puCorrector('singlee'), + ['p1s', 'm1s'], + [mcCorrections.make_puCorrectorUp('singlee'), mcCorrections.make_puCorrectorDown('singlee')] + ) + self.trig_weight = mcCorrections.trig_efficiency if self.is_embedded else mcCorrections.trig_correction + + @staticmethod + def tau_veto(row): + if not row.tAntiMuonLoose2 or not row.tAntiElectronMVA5Tight or not row.tDecayFinding : + return False + + @staticmethod + def obj1_matches_gen(row): + return row.eGenPdgId == -1*row.eCharge*11 + + @staticmethod + def obj3_matches_gen(row): + return t.genDecayMode != -2 + + def event_weight(self, row, sys_shifts): + if self.is_data: + return {'' : 1.} + + weights = {} + for shift in sys_shifts: + embedded_weight = row.EmbPtWeight*mcCorrections.eEmb_correction( row, 'e', shift=shift) if self.is_embedded else 1. + + weights[shift] = embedded_weight *\ + mcCorrections.eid_correction( row, 'e', shift=shift) * \ + mcCorrections.eiso_correction(row, 'e', shift=shift) * \ + self.trig_weight(row, 'e', shift=shift) * \ + self.pucorrector(row.nTruePU, shift=shift) + + return weights +## + def begin(self): + logging.debug('Booking histograms directory tree') + sys_shifts = self.systematics['trig'] + \ + self.systematics['pu'] + \ + self.systematics['eid'] + \ + self.systematics['eiso'] + \ + self.systematics['mvetos'] + \ + self.systematics['tvetos'] + \ + self.systematics['evetos'] + \ + [i.strip('_') for i in self.systematics['jes']] + \ + [i.strip('_') for i in self.systematics['tes']] + \ + [i.strip('_') for i in self.systematics['ees']] + \ + ['tLoose', 'tLoose/Up', 'tLoose/Down', 'tLooseUnweight'] + \ + ['eLoose', 'eLoose/Up', 'eLoose/Down'] +\ + ['etLoose', 'etLoose/Up', 'etLoose/Down'] + sys_shifts = list( set( sys_shifts ) ) #remove double dirs + processtype=['gg'] + threshold=['ept30'] + signs =['os', 'ss'] + jetN = ['0', '1', '2', '3'] #[''.join(i) for i in itertools.product(['0', '1', '2', '3'], self.systematics['jes'])] + full_met_systematics = self.systematics['met']+self.systematics['jes']+ \ + self.systematics['tes']+self.systematics['ees'] + #remove empty string + full_met_systematics = [i for i in full_met_systematics if i] + folder=[] + + for tuple_path in itertools.product(sys_shifts, signs, processtype, threshold, jetN): + folder.append(os.path.join(*tuple_path)) + path = list(tuple_path) + path.append('selected') + folder.append(os.path.join(*path)) + + def book_with_sys(location, name, *args, **kwargs): + postfixes = kwargs['postfixes'] + del kwargs['postfixes'] + self.book(location, name, *args, **kwargs) + for postfix in postfixes: + #patch name to be removed + fix = postfix + self.book(location, name+fix, *args, **kwargs) + + self.book('os/gg/ept30/', "h_collmass_pfmet" , "h_collmass_pfmet", 32, 0, 320) + + for f in folder: + #self.book( + # f, + # 'evtInfo', 'evtInfo', + # 'run/l:lumi/l:evt/l:weight/D', + # type=pytree.PyTree + #) + self.book(f,"weight", "weight", 100, 0, 10) + self.book(f,"tPt", "tau p_{T}", 40, 0, 200) + self.book(f,"tPt_tes_plus", "tau p_{T} (tes+)", 40, 0, 200) + self.book(f,"tPt_tes_minus", "tau p_{T} (tes-)",40, 0, 200) + + self.book(f,"tPhi", "tau phi", 26, -3.25, 3.25) + self.book(f,"tEta", "tau eta", 10, -2.5, 2.5) + + self.book(f,"ePt", "e p_{T}", 40, 0, 200) + self.book(f,"ePt_ees_plus", "e p_{T} (ees+)", 40, 0, 200) + self.book(f,"ePt_ees_minus", "e p_{T} (ees-)",40, 0, 200) + + self.book(f,"ePhi", "e phi", 26, -3.2, 3.2) + self.book(f,"eEta", "e eta", 10, -2.5, 2.5) + + self.book(f, "e_t_DPhi", "e-tau DeltaPhi" , 20, 0, 3.2) + self.book(f, "e_t_DR", "e-tau DeltaR" , 20, 0, 3.2) + + #self.book(f, "h_collmass_pfmet", "h_collmass_pfmet", 32, 0, 320) + book_with_sys(f, "h_collmass_pfmet", "h_collmass_pfmet", 40, 0, 400, + postfixes=full_met_systematics) + + self.book(f, "h_collmass_vs_dPhi_pfmet", "h_collmass_vs_dPhi_pfmet", 20, 0, 3.2, 40, 0, 400, type=ROOT.TH2F) + + self.book(f, "e_t_Mass", "h_vismass", 40, 0, 400) + self.book(f, "e_t_Mass_tes_plus" , "h_vismass_tes_plus", 40 , 0, 400) + self.book(f, "e_t_Mass_tes_minus", "h_vismass_tes_minus",40 , 0, 400) + self.book(f, "e_t_Mass_ees_plus" , "h_vismass_ees_plus", 40 , 0, 400) + self.book(f, "e_t_Mass_ees_minus", "h_vismass_ees_minus",40 , 0, 400) + + self.book(f, "MetEt_vs_dPhi", "PFMet vs #Delta#phi(#tau,PFMet)", 20, 0, 3.2, 40, 0, 400, type=ROOT.TH2F) + + self.book(f, "tPFMET_DeltaPhi", "tau-type1PFMET DeltaPhi" , 20, 0, 3.2) + + self.book(f, "ePFMET_DeltaPhi", "e-PFMET DeltaPhi" , 20, 0, 3.2) + + #self.book(f,"tMtToPFMET", "tau-PFMET M_{T}" , 200, 0, 200) + book_with_sys(f, "tMtToPfMet", "tau-PFMET M_{T}" , 40, 0, 200, + postfixes=full_met_systematics) + #self.book(f,"eMtToPFMET", "e-PFMET M_{T}" , 200, 0, 200) + book_with_sys(f, "eMtToPfMet", "e-PFMET M_{T}" , 40, 0, 200, + postfixes=full_met_systematics) + + #self.book(f, "pfMetEt", "pfMetEt", 200, 0, 200) + book_with_sys(f, "pfMet_Et", "pfMet_Et", 40, 0, 200, postfixes=full_met_systematics) + + #self.book(f, "pfMetPhi", "pfMetPhi", 100, -3.2, 3.2) + book_with_sys(f, "pfMet_Phi", "pfMet_Phi", 26, -3.2, 3.2, postfixes=full_met_systematics) + + self.book(f, "jetVeto20", "Number of jets, p_{T}>20", 5, -0.5, 4.5) + self.book(f, "jetVeto30", "Number of jets, p_{T}>30", 5, -0.5, 4.5) + + #index dirs and histograms + for key, value in self.histograms.iteritems(): + location = os.path.dirname(key) + name = os.path.basename(key) + if location not in self.histo_locations: + self.histo_locations[location] = {name : value} + else: + self.histo_locations[location][name] = value + + def fakerate_weights(self, tEta): + tLoose = tau_fake_rate(tEta) + tLooseUp = tau_fake_rate_up(tEta) + tLooseDown= tau_fake_rate_dw(tEta) + + tLoose = tLoose / (1. - tLoose ) + tLooseUp = tLooseUp / (1. - tLooseUp ) + tLooseDown= tLooseDown / (1. - tLooseDown) + + + eLoose = self.efake + eLooseUp = self.efakeup + eLooseDown= self.efakedw + + eLoose = eLoose / (1. - eLoose ) + eLooseUp = eLooseUp / (1. - eLooseUp ) + eLooseDown= eLooseDown / (1. - eLooseDown) + + + etLoose = tLoose * eLoose + etLooseUp = tLooseUp * eLooseUp + etLooseDown= tLooseDown *eLooseDown + + frweight = { + 'tLoose' : tLoose , + 'tLoose/Up' : tLooseUp , + 'tLoose/Down' : tLooseDown, + 'eLoose' : eLoose , + 'eLoose/Up' : eLooseUp , + 'eLoose/Down' : eLooseDown, + 'etLoose' : etLoose , + 'etLoose/Up' : etLooseUp , + 'etLoose/Down' : etLooseDown, + 'tLooseUnweight' : 1., + } + + return frweight; + + def fill_histos(self, folder_str, row, weight, filter_label = ''): + '''fills histograms''' + #find all keys matching + for attr, value in self.histo_locations[folder_str].iteritems(): + name = attr + #if attr=='DEBUG': + # set_trace() + if filter_label: + if not attr.startswith(filter_label+'$'): + continue + attr = attr.replace(filter_label+'$', '') + if value.InheritsFrom('TH2'): + if attr in self.hfunc: + try: + result, out_weight = self.hfunc[attr](row, weight) + except Exception as e: + raise RuntimeError("Error running function %s. Error: \n\n %s" % (attr, str(e))) + r1, r2 = result + if out_weight is None: + value.Fill( r1, r2 ) #saves you when filling NTuples! + else: + value.Fill( r1, r2, out_weight ) + else: + attr1, attr2 = split(attr) + v1 = getattr(row,attr1) + v2 = getattr(row,attr2) + value.Fill( v1, v2, weight ) if weight is not None else value.Fill( v1, v2 ) + else: + if attr in self.hfunc: + try: + result, out_weight = self.hfunc[attr](row, weight) + except Exception as e: + raise RuntimeError("Error running function %s. Error: \n\n %s" % (attr, str(e))) + if out_weight is None: + value.Fill( result ) #saves you when filling NTuples! + else: + value.Fill( result, out_weight ) + else: + value.Fill( getattr(row,attr), weight ) if weight is not None else value.Fill( getattr(row,attr) ) + return None + + def process(self): + logging.debug('Starting processing') + systematics = self.systematics + + frw = [] + lock =() + ievt = 0 + logging.debug('Starting evt loop') + + #pre-compute static things + sys_shifts = systematics['trig'] + \ + systematics['pu'] + \ + systematics['eid'] + \ + systematics['eiso'] #+ \ + + jes_dirs = [i.strip('_') for i in systematics['jes']] + #anyway the first is always "" + tes_dirs = [i.strip('_') for i in systematics['tes']][1:] + ees_dirs = [i.strip('_') for i in systematics['ees']][1:] + + for row in self.tree: + if (ievt % 100) == 0: + logging.debug('New event') + ievt += 1 + #avoid double counting events! + evt_id = (row.run, row.lumi, row.evt) + if evt_id == lock: continue + if lock != () and evt_id == lock: + logging.info('Removing duplicate of event: %d %d %d' % evt_id) + + # + #preselection, common to everything and everyone + # + #trigger + if self.is_embedded : + if not bool(row.doubleMuPass) : continue + else: + if not bool(row.singleE27WP80Pass) : continue + if not bool(row.eMatchesSingleE27WP80): continue + + #objects + if not selections.eSelection(row, 'e'): continue + #if row.ePt < 30 : continue + if not selections.tauSelection(row, 't'): continue + if not row.tAntiElectronMVA5Tight : continue + if not row.tAntiMuon2Loose : continue + if not row.tLooseIso3Hits : continue + logging.debug('object selection passed') + #e ID/ISO + if not selections.lepton_id_iso(row, 'e', 'eid13Tight_idiso02'): continue + logging.debug('Passed preselection') + + + #TO ENHANCE Z->tautau + if row.e_t_DR > 2.0: continue + + # + # Compute event weight + # + #event weight + #sys_shifts is precomputed + + #set_trace() + weight_map = self.event_weight(row, sys_shifts) + + #Fill embedded sample normalization BEFORE the vetoes + if not row.e_t_SS: + self.fill_histos('os/gg/ept30', row, weight_map['']) + + # it is better vetoing on b-jets after the histo for the DY embedded + #bjet veto + if row.bjetCSVVeto30!=0 : continue + + #tau ID, id Tau is tight then go in full selection, otherwise use for fakes + isTauTight = bool(row.tTightIso3Hits) + isETight = bool(selections.lepton_id_iso(row, 'e', 'eid13Tight_etauiso01')) + etau_category = [''] + if (not isETight) and (not isTauTight): + etau_category = ['etLoose', 'etLoose/Up', 'etLoose/Down'] + elif (not isTauTight): + etau_category = ['tLoose', 'tLoose/Up', 'tLoose/Down', 'tLooseUnweight'] + elif (not isETight): + etau_category = ['eLoose', 'eLoose/Up', 'eLoose/Down'] + + #jet category + central = struct( + njets = min(row.jetVeto30, 3), + tPt = row.tPt, + ePt = row.ePt + ) + jets = [min(row.jetVeto30, 3), min(row.jetVeto30jes_plus, 3), min(row.jetVeto30jes_minus, 3)] + tpts = [row.tPt_tes_plus, row.tPt_tes_minus] + epts = [row.ePt_ees_plus, row.ePt_ees_minus] + sys_effects = [(name, central.clone(njets = jnum)) for name, jnum in zip(jes_dirs, jets)] + sys_effects.extend( + [(name, central.clone(tPt = pt)) for name, pt in zip(tes_dirs, tpts)] + ) + sys_effects.extend( + [(name, central.clone(ePt = pt)) for name, pt in zip(ees_dirs, epts)] + ) + + # + # Full tight selection + # + passes_full_selection = False + selection_categories = [] + for name, shifted in sys_effects: + #preselection. flat pt values + if shifted.ePt > 30 and shifted.tPt > 30: + selection_categories.append((name, '%i' % shifted.njets, '')) + else: + continue + + if shifted.njets == 0 : + if shifted.tPt < 35: continue + if shifted.ePt < 40: continue + if deltaPhi(row.ePhi, row.tPhi) < 2.7 : continue + if row.tMtToPfMet > 50 : continue + selection_categories.append((name, '0', 'selected')) + passes_full_selection = True + elif shifted.njets == 1 : + if shifted.tPt < 40: continue + if shifted.ePt < 35: continue + if row.tMtToPfMet > 35 : continue + selection_categories.append((name, '1', 'selected')) + passes_full_selection = True + elif shifted.njets == 2 : + if shifted.tPt < 40: continue + if shifted.ePt < 30: continue + if row.tMtToPfMet > 35 : continue + if row.vbfMass < 550 : continue + if row.vbfDeta < 3.5 : continue + selection_categories.append((name, '2', 'selected')) + passes_full_selection = True + + if passes_full_selection: + logging.debug('Passed full selection') + + # + #different selections + # + sign = 'ss' if row.e_t_SS else 'os' + processtype ='gg' + ptthreshold = ['ept30'] + + # + # Lepton vetoes + # + tvetoes = [row.tauVetoPt20EleTight3MuLoose, row.tauVetoPt20EleTight3MuLoose_tes_plus, row.tauVetoPt20EleTight3MuLoose_tes_minus] + mvetoes = [row.muVetoPt5IsoIdVtx , row.muVetoPt5IsoIdVtx_mes_plus , row.muVetoPt5IsoIdVtx_mes_minus ] + evetoes = [row.eVetoCicLooseIso , row.eVetoCicLooseIso_ees_plus , row.eVetoCicLooseIso_ees_minus ] + + tdirs = [ i for i, j in zip( systematics['tvetos'], tvetoes) if not j] + mdirs = [ i for i, j in zip( systematics['mvetos'], mvetoes) if not j] + edirs = [ i for i, j in zip( systematics['evetos'], evetoes) if not j] + + #if any of the lists is empty + #set_trace() + if not tdirs or not mdirs or not edirs: + continue + logging.debug('Passed Vetoes') + + #make all possible veto combos... + all_dirs = [''.join(i) for i in itertools.product(tdirs, mdirs, edirs)] + #...and choose only the meaningful ones + veto_sys = set(systematics['tvetos']+systematics['mvetos']+systematics['evetos']) + all_dirs = [i for i in all_dirs if i in veto_sys] + + sys_directories = all_dirs + sys_shifts + #remove duplicates + sys_directories = list(set(sys_directories)) + + #at least one loose object + if (not isETight) or (not isTauTight): + #if is a loose tau just compute the fakes! + sys_directories = etau_category + + #gather the one and only weight we do care about + mc_weight = weight_map[''] + + #weights are the fr ones... + weight_map = self.fakerate_weights(row.tEta) + for i in weight_map: + #...times the mc weight (if any) + weight_map[i] *= mc_weight + + + #Fill histograms in appropriate direcotries + #if passes_full_selection: + #dirs = [os.path.join(sys, sign, processtype, e_thr, jet_dir) for sys, e_thr, jet_dir in itertools.product(sys_directories, ptthreshold, jet_directories)] + #if len(dirs) <> len(set(dirs)): + # set_trace() + for sys, e_thr, selection in itertools.product(sys_directories, ptthreshold, selection_categories): + selection_sys, jet_dir, selection_step = selection + #if we have multiple systematic shifts applied + #reject the combination + if selection_sys and sys: + continue + + #if we fill a histogram, lock the event + lock = evt_id + dir_name = os.path.join(sys, selection_sys, sign, processtype, + e_thr, jet_dir, selection_step) + if dir_name[-1] == '/': + dir_name = dir_name[:-1] + if passes_full_selection: + logging.debug('Filling %s' % dir_name) + #fill them! + weight_to_use = weight_map[sys] if sys in weight_map else weight_map[''] + self.fill_histos(dir_name, row, weight_to_use) + + + def finish(self): + self.write_histos() + + diff --git a/lfvetau/FakeTauControlRegion.py b/lfvetau/FakeTauControlRegion.py new file mode 100644 index 00000000..de3fcc96 --- /dev/null +++ b/lfvetau/FakeTauControlRegion.py @@ -0,0 +1,172 @@ +##Correction Factor still to add +from ETauTree import ETauTree +import os +import ROOT +import math +import optimizer +import glob +import array +import mcCorrections +import baseSelections as selections +import FinalStateAnalysis.PlotTools.pytree as pytree +from FinalStateAnalysis.PlotTools.decorators import memo_last +from FinalStateAnalysis.PlotTools.MegaBase import MegaBase +from math import sqrt, pi, sin, cos, acos, sinh +from cutflowtracker import cut_flow_tracker +#Makes the cut flow histogram +cut_flow_step = ['allEvents', 'e1sel', 'e1IDiso', 'e2sel', 'e2IDiso', 'ZMass', 'tsel', 'tAntiMuon', 'tAntiEle', 'MtToMet', 'tRawIso10','tRawIso5', 'tLooseIso', 'tTightIso' +] + +from inspect import currentframe + +def get_linenumber(): + cf = currentframe() + return cf.f_back.f_lineno + +def deltaPhi(phi1, phi2): + PHI = abs(phi1-phi2) + if PHI<=pi: + return PHI + else: + return 2*pi-PHI +def deltaR(phi1, phi2, eta1, eta2): + deta = eta1 - eta2 + dphi = abs(phi1-phi2) + if (dphi>pi) : dphi = 2*pi-dphi + return sqrt(deta*deta + dphi*dphi); + +def etDR(row): + return row.e1_t_DR if row.e1_t_DR < row.e2_t_DR else row.e2_t_DR + +def etDPhi(row): + e1tDPhi=deltaPhi(row.e1Phi, row.tPhi) + e2tDPhi=deltaPhi(row.e2Phi, row.tPhi) + return e1tDPhi if e1tDPhi < e2tDPhi else e2tDPhi + +def Z(row): + e1p=ROOT.TVector3(row.e1Pt*cos(row.e1Phi),row.e1Pt*sin(row.e1Phi),row.e1Pt*sinh(row.e1Eta)) + e2p=ROOT.TVector3(row.e2Pt*cos(row.e2Phi),row.e2Pt*sin(row.e2Phi),row.e2Pt*sinh(row.e2Eta)) + e1FourVector= ROOT.TLorentzVector(e1p, sqrt(e1p.Mag2()+row.e1Mass*row.e1Mass)) + e2FourVector= ROOT.TLorentzVector(e2p, sqrt(e2p.Mag2()+row.e2Mass*row.e2Mass)) + zFourVector = e1FourVector+e2FourVector + return zFourVector + + +class FakeTauControlRegion(MegaBase): + tree = 'et/final/Ntuple' + def __init__(self, tree, outfile, **kwargs): + self.channel='ET' + super(FakeTauControlRegion, self).__init__(tree, outfile, **kwargs) + self.tree = ETauTree(tree) + self.out=outfile + self.histograms = {} + self.pucorrector = mcCorrections.make_puCorrector('singlee') + + #optimizer_keys = [ i for i in optimizer.grid_search.keys() if i.startswith(self.channel) ] + #self.grid_search = {} + #if len(optimizer_keys) > 1: + # for key in optimizer_keys: + # self.grid_search[key] = optimizer.grid_search[key] + #else: + # self.grid_search[''] = optimizer.grid_search[optimizer_keys[0]] + + + def event_weight(self, row): + if row.run > 2: #FIXME! add tight ID correction + return 1. + + + + + return self.pucorrector(row.nTruePU) * \ + mcCorrections.eid_correction( row, 'e') * \ + mcCorrections.eiso_correction(row, 'e') * \ + mcCorrections.trig_correction(row, 'e') + + def begin(self): + + tauiso = [ 'tLoose', 'tTigh'] + folder = [] + sign = ['ss','os'] + for iso in tauiso: + for s in sign: + folder.append(s+'/'+iso) + folder.append(s+'/'+iso+'/tptregion') + j=0 + while j < 4 : + folder.append(s+'/'+iso+'/'+str(j)) + folder.append(s+'/'+iso+'/'+str(j)+'/tptregion') + j+=1 + + for f in folder: + + self.book(f,"ePt", "e p_{T}", 200, 0, 200) + self.book(f,"ePhi", "e phi", 100, -3.2, 3.2) + self.book(f,"eEta", "e eta", 50, -2.5, 2.5) + self.book(f,"tPt", "t p_{T}", 200, 0, 200) + self.book(f,"tPhi", "t phi", 100, -3.2, 3.2) + self.book(f,"tEta", "t eta", 50, -2.5, 2.5) + self.book(f,"tAbsEta", "t abs eta", 50, -2.5, 2.5) + + + def fill_histos(self, row, folder='os/Loose', fakeRate = False): + weight = self.event_weight(row) + histos = self.histograms + + histos[folder+'/ePt'].Fill(row.ePt, weight) + histos[folder+'/eEta'].Fill(row.eEta, weight) + histos[folder+'/ePhi'].Fill(row.ePhi, weight) + + histos[folder+'/tPt'].Fill(row.tPt, weight) + histos[folder+'/tEta'].Fill(row.tEta, weight) + histos[folder+'/tAbsEta'].Fill(abs(row.tEta), weight) + histos[folder+'/tPhi'].Fill(row.tPhi, weight) + + def process(self): + + #print self.tree.inputfilename + for row in self.tree: + jn = row.jetVeto30 + if jn > 3 : jn = 3 + + if not bool(row.singleE27WP80Pass) : continue + + if not bool(row.eMatchesSingleE27WP80) : continue + + if row.bjetCSVVeto30!=0 : continue + if not selections.eLowPtSelection(row, 'e'): continue + if not selections.lepton_id_iso(row, 'e', 'eid13Loose_idantiso'): continue + if abs(row.eEta) > 1.4442 and abs(row.eEta < 1.566) : continue + + + if not selections.tauSelection(row, 't'): continue + if row.tPt < 30 : continue + + if not row.tAntiMuon2Loose: continue + if not row.tAntiElectronMVA5Tight: continue #was 3 + if row.tauVetoPt20EleTight3MuLoose : continue + #if row.tauHpsVetoPt20 : continue + if row.muVetoPt5IsoIdVtx : continue + if row.eVetoCicLooseIso : continue # change it with Loose + + sign = 'ss' if row.e_t_SS else 'os' + if row.tLooseIso3Hits : + tauiso = 'tLoose' + folder = sign+'/'+tauiso + self.fill_histos(row, folder) + folder=folder+'/'+str(int(jn)) + self.fill_histos(row, folder) + + if row.tTightIso3Hits : + tauiso = 'tTigh' + folder = sign+'/'+tauiso + self.fill_histos(row, folder) + folder=folder+'/'+str(int(jn)) + self.fill_histos(row, folder) + + + + def finish(self): + self.write_histos() + + diff --git a/lfvetau/LFVHETauAnalyzerMVA.py b/lfvetau/LFVHETauAnalyzerMVA.py new file mode 100644 index 00000000..5278c94c --- /dev/null +++ b/lfvetau/LFVHETauAnalyzerMVA.py @@ -0,0 +1,696 @@ +from ETauTree import ETauTree +import sys +import logging +logging.basicConfig(stream=sys.stderr, level=logging.WARNING) +import os +from pdb import set_trace +import ROOT +import math +import glob +import array +import mcCorrections +import baseSelections as selections +import FinalStateAnalysis.PlotTools.pytree as pytree +from FinalStateAnalysis.PlotTools.decorators import memo_last +from FinalStateAnalysis.PlotTools.MegaBase import MegaBase +from math import sqrt, pi, cos +from fakerate_functions import tau_fake_rate, tau_fake_rate_up, tau_fake_rate_dw, e_fake_rate, e_fake_rate_up, e_fake_rate_dw +import itertools +import traceback +from FinalStateAnalysis.PlotTools.decorators import memo +import FinalStateAnalysis.PlotTools.pytree as pytree +from FinalStateAnalysis.Utilities.struct import struct +import optimizer + +@memo +def getVar(name, var): + return name+var + +met_et = 'pfMet_Et%s' +met_phi = 'pfMet_Phi%s' +ty1met_et = 'type1_pfMet_Et%s' +ty1met_phi = 'type1_pfMet_Phi%s' +t_pt = 'tPt%s' +etMass = 'e_t_Mass%s' +@memo +def met(shift=''): + if not 'es' in shift : + return ty1met_et %shift + return met_et % shift + +@memo +def metphi(shift=''): + if not 'es' in shift : + return ty1met_phi %shift + return met_phi % shift +@memo +def tpt(shift=''): + return t_pt % shift + +@memo +def vismass(shift=''): + return etMass % shift + +@memo +def split(string, separator='#'): + return tuple(attr.split(separator)) + +def create_mapper(mapping): + def _f(path): + for key, out in mapping.iteritems(): + if key == path: + path = path.replace(key,out) + print 'path', path + return path + return _f + +def attr_getter(attribute): + '''return a function that gets an attribute''' + def f(row, weight): + return (getattr(row,attribute), weight) + return f + +def merge_functions(fcn_1, fcn_2): + '''merges two functions to become a TH2''' + def f(row, weight): + r1, w1 = fcn_1(row, weight) + r2, w2 = fcn_2(row, weight) + w = w1 if w1 and w2 else None + return ((r1, r2), w) + return f + +def collmass(row, met, metPhi): + ptnu =abs(met*cos(deltaPhi(metPhi, row.tPhi))) + visfrac = row.tPt/(row.tPt+ptnu) + #print met, cos(deltaPhi(metPhi, row.tPhi)), ptnu, visfrac + return (row.e_t_Mass / sqrt(visfrac)) + +def deltaPhi(phi1, phi2): + PHI = abs(phi1-phi2) + if PHI<=pi: + return PHI + else: + return 2*pi-PHI + +def deltaR(phi1, ph2, eta1, eta2): + deta = eta1 - eta2 + dphi = abs(phi1-phi2) + if (dphi>pi) : dphi = 2*pi-dphi + return sqrt(deta*deta + dphi*dphi); + + + +def make_collmass_systematics(shift): + + if shift.startswith('tes'): + ptnu =abs(met*cos(deltaPhi(metPhi, row.tPhi))) + visfrac = tpt/(tpt+ptnu) + vis_mass = vismass(shift) + return (vis_mass / sqrt(visfrac)) + elif shift.startswith('ees'): + ptnu =abs(met*cos(deltaPhi(metPhi, row.tPhi))) + visfrac = tpt/(tpt+ptnu) + vis_mass = vismass(shift) + return (vis_mass / sqrt(visfrac)) + + elif shift.startswith('_Zee'): + ptnu =abs(row.type1_pfMet_Et*cos(deltaPhi(row.type1_pfMet_Phi, row.tPhi))) + visfrac = row.tPt/(row.tPt+ptnu) + if shift== '_Zee_p1s': + return 1.025*(row.e_t_Mass / sqrt(visfrac)) + else: + return 0.975*(row.e_t_Mass / sqrt(visfrac)) + + else: + met_name = met(shift) + phi_name = metphi(shift) + def collmass_shifted(row, weight): + met = getattr(row, met_name) + phi = getattr(row, phi_name) + return collmass(row, met, phi), weight + return collmass_shifted + +class LFVHETauAnalyzerMVA(MegaBase): + tree = 'et/final/Ntuple' + def __init__(self, tree, outfile, **kwargs): + logging.debug('LFVHETauAnalyzerMVA constructor') + self.channel='ET' + super(LFVHETauAnalyzerMVA, self).__init__(tree, outfile, **kwargs) + self.tree = ETauTree(tree) + self.out=outfile + self.histograms = {} + + #understand what we are running + target = os.path.basename(os.environ['megatarget']) + self.is_data = target.startswith('data_') + self.is_embedded = ('Embedded' in target) + self.is_mc = not (self.is_data or self.is_embedded) + self.is_Zee = (target.endswith('skimmedLL') or target.endswith('skimmedLL.root') ) + #self.efake = e_fake_rate(0.2) # new fakerate shows a pt dependence + #self.efakeup = e_fake_rate_up(0.2) + #self.efakedw = e_fake_rate_dw(0.2) + + #systematics used + self.systematics = { + + 'trig' : (['', 'trp1s', 'trm1s'] if not self.is_data else []), + 'pu' : (['', 'p1s', 'm1s'] if self.is_mc else []), + 'eid' : (['', 'eidp1s','eidm1s'] if not self.is_data else []), + 'etaufake' : (['', 'etaufakep1s','etaufakem1s'] if self.is_Zee else []), + 'eiso' : (['', 'eisop1s','eisom1s'] if not self.is_data else []), + 'jes' : (['', '_jes_plus','_jes_minus'] if self.is_mc else ['']), + 'mvetos': (['', 'mVetoUp', 'mVetoDown'] if self.is_mc else ['']), + 'tvetos': (['', 'tVetoUp', 'tVetoDown'] if self.is_mc else ['']), + 'evetos': (['', 'eVetoUp', 'eVetoDown'] if self.is_mc else ['']), + 'met' : (["_mes_plus", "_ues_plus", "_mes_minus", "_ues_minus"] if self.is_mc else []), + 'tes' : (["", "_tes_plus", "_tes_minus"] if not self.is_data else ['']), + 'ees' : (["", "_ees_plus", '_ees_minus'] if not self.is_data else ['']), + 'Zee' : (['', '_Zee_p1s','Zee_m1s'] if self.is_Zee else ['']), + + #### 'trig' : (['', 'trp1s', 'trm1s'] if not self.is_data else []), + #### 'pu' : (['', 'p1s', 'm1s'] if self.is_mc else []), + #### 'eid' : (['', 'eidp1s','eidm1s'] if False else []), + #### 'eiso' : (['', 'eisop1s','eisom1s'] if False else []), + #### 'jes' : (['', '_jes_plus','_jes_minus'] if self.is_mc else ['']), + #### 'mvetos': (['', 'mVetoUp', 'mVetoDown'] if False else ['']), + #### 'tvetos': (['', 'tVetoUp', 'tVetoDown'] if False else ['']), + #### 'evetos': (['', 'eVetoUp', 'eVetoDown'] if False else ['']), + #### 'met' : (["_mes_plus", "_ues_plus", "_mes_minus", "_ues_minus"] if self.is_mc else []), + #### 'tes' : (["", "_tes_plus", "_tes_minus"] if not self.is_data else ['']), + #### 'ees' : (["", "_ees_plus", '_ees_minus'] if not self.is_data else ['']) + } + + #self filling histograms + coll_mass = make_collmass_systematics('') #no sys shift + self.histo_locations = {} #just a mapping of the histograms we have to avoid changing self.histograms indexing an screw other files + self.hfunc = { #maps the name of non-trivial histograms to a function to get the proper value, the function MUST have two args (evt and weight). Used in fill_histos later + 'nTruePU' : lambda row, weight: (row.nTruePU,None), + 'weight' : lambda row, weight: (weight,None) if weight is not None else (1.,None), + 'Event_ID': lambda row, weight: (array.array("f", [row.run,row.lumi,int(row.evt)/10**5,int(row.evt)%10**5] ), None), + 'h_collmass_pfmet' : coll_mass, + 'h_collmass_vs_dPhi_pfmet' : merge_functions( + attr_getter('tToMETDPhi'), + coll_mass + ), + 'MetEt_vs_dPhi' : merge_functions( + lambda row, weight: (deltaPhi(row.tPhi, getattr(row, metphi())), weight), + attr_getter('type1_pfMet_Et') + ), + 'ePFMET_DeltaPhi' : lambda row, weight: (deltaPhi(row.ePhi, getattr(row, metphi())), weight), + 'tPFMET_DeltaPhi' : lambda row, weight: (deltaPhi(row.tPhi, getattr(row, metphi())), weight), + 'evtInfo' : lambda row, weight: (struct(run=row.run,lumi=row.lumi,evt=row.evt,weight=weight), None) + } + for shift in self.systematics['met']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + for shift in self.systematics['tes']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + for shift in self.systematics['jes']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + for shift in self.systematics['ees']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + for shift in self.systematics['Zee']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + + + #PU correctors + self.pucorrector = mcCorrections.make_shifted_weights( + mcCorrections.make_puCorrector('singlee'), + ['p1s', 'm1s'], + [mcCorrections.make_puCorrectorUp('singlee'), mcCorrections.make_puCorrectorDown('singlee')] + ) + self.trig_weight = mcCorrections.trig_efficiency if self.is_embedded else mcCorrections.trig_correction + + @staticmethod + def tau_veto(row): + if not row.tAntiMuonLoose2 or not row.tAntiElectronMVA5Tight or not row.tDecayFinding : + return False + + @staticmethod + def obj1_matches_gen(row): + return row.eGenPdgId == -1*row.eCharge*11 + + @staticmethod + def obj3_matches_gen(row): + return t.genDecayMode != -2 + + def event_weight(self, row, sys_shifts): + if self.is_data: + return {'' : 1.} + + weights = {} + for shift in sys_shifts: + embedded_weight = row.EmbPtWeight*mcCorrections.eEmb_correction( row, 'e', shift=shift) if self.is_embedded else 1. + + weights[shift] = embedded_weight *\ + mcCorrections.eid_correction( row, 'e', shift=shift) * \ + mcCorrections.eiso_correction(row, 'e', shift=shift) * \ + self.trig_weight(row, 'e', shift=shift) * \ + self.pucorrector(row.nTruePU, shift=shift) + if self.is_Zee: weights[shift] =weights[shift]*mcCorrections.etaufake_correction( row, 't', shift=shift) + + return weights +## + def begin(self): + logging.debug('Booking histograms directory tree') + sys_shifts = self.systematics['trig'] + \ + self.systematics['pu'] + \ + self.systematics['eid'] + \ + self.systematics['etaufake'] + \ + self.systematics['eiso'] + \ + self.systematics['mvetos'] + \ + self.systematics['tvetos'] + \ + self.systematics['evetos'] + \ + [i.strip('_') for i in self.systematics['jes']] + \ + [i.strip('_') for i in self.systematics['tes']] + \ + [i.strip('_') for i in self.systematics['ees']] + \ + ['tLoose', 'tLoose/Up', 'tLoose/Down', 'tLooseUnweight'] + \ + ['eLoose', 'eLoose/Up', 'eLoose/Down'] +\ + ['etLoose', 'etLoose/Up', 'etLoose/Down'] + sys_shifts = list( set( sys_shifts ) ) #remove double dirs + processtype=['gg'] + threshold=['ept30'] + signs =['os', 'ss'] + jetN = ['0', '1', '2', '3'] #[''.join(i) for i in itertools.product(['0', '1', '2', '3'], self.systematics['jes'])] + full_met_systematics = self.systematics['met']+self.systematics['jes']+ \ + self.systematics['tes']+self.systematics['ees'] + #remove empty string + full_met_systematics = [i for i in full_met_systematics if i] + folder=[] + + for tuple_path in itertools.product(sys_shifts, signs, processtype, threshold, jetN): + folder.append(os.path.join(*tuple_path)) + path = list(tuple_path) + path.append('selected') + folder.append(os.path.join(*path)) + prefix_path = os.path.join(*tuple_path) + for region in optimizer.regions[tuple_path[-1]]: + folder.append( + os.path.join(prefix_path, region) + ) + + + def book_with_sys(location, name, *args, **kwargs): + postfixes = kwargs['postfixes'] + del kwargs['postfixes'] + self.book(location, name, *args, **kwargs) + for postfix in postfixes: + #patch name to be removed + fix = postfix + if 'type1_' in name : name=name[6:] ## remove this line when the ntuples have the correct systematics + #print name + self.book(location, name+fix, *args, **kwargs) + + self.book('os/gg/ept30/', "h_collmass_pfmet" , "h_collmass_pfmet", 32, 0, 320) + self.book('os/gg/ept30/', "e_t_Mass", "h_vismass", 32, 0, 320) + + for f in folder: + #print f + #self.book( + # f, + # 'evtInfo', 'evtInfo', + # 'run/l:lumi/l:evt/l:weight/D', + # type=pytree.PyTree + #) + self.book(f,"weight", "weight", 100, 0, 10) + self.book(f,"tPt", "tau p_{T}", 40, 0, 200) + self.book(f,"tPt_tes_plus", "tau p_{T} (tes+)", 40, 0, 200) + self.book(f,"tPt_tes_minus", "tau p_{T} (tes-)",40, 0, 200) + + self.book(f,"tPhi", "tau phi", 26, -3.25, 3.25) + self.book(f,"tEta", "tau eta", 10, -2.5, 2.5) + + self.book(f,"ePt", "e p_{T}", 40, 0, 200) + self.book(f,"ePt_ees_plus", "e p_{T} (ees+)", 40, 0, 200) + self.book(f,"ePt_ees_minus", "e p_{T} (ees-)",40, 0, 200) + + self.book(f,"ePhi", "e phi", 26, -3.2, 3.2) + self.book(f,"eEta", "e eta", 10, -2.5, 2.5) + + self.book(f, "e_t_DPhi", "e-tau DeltaPhi" , 20, 0, 3.2) + self.book(f, "e_t_DR", "e-tau DeltaR" , 20, 0, 3.2) + + #self.book(f, "h_collmass_pfmet", "h_collmass_pfmet", 32, 0, 320) + book_with_sys(f, "h_collmass_pfmet", "h_collmass_pfmet", 40, 0, 400, + postfixes=full_met_systematics) + + self.book(f, "h_collmass_vs_dPhi_pfmet", "h_collmass_vs_dPhi_pfmet", 20, 0, 3.2, 40, 0, 400, type=ROOT.TH2F) + + self.book(f, "e_t_Mass", "h_vismass", 40, 0, 400) + self.book(f, "e_t_Mass_tes_plus" , "h_vismass_tes_plus", 40 , 0, 400) + self.book(f, "e_t_Mass_tes_minus", "h_vismass_tes_minus",40 , 0, 400) + self.book(f, "e_t_Mass_ees_plus" , "h_vismass_ees_plus", 40 , 0, 400) + self.book(f, "e_t_Mass_ees_minus", "h_vismass_ees_minus",40 , 0, 400) + + self.book(f, "MetEt_vs_dPhi", "PFMet vs #Delta#phi(#tau,PFMet)", 20, 0, 3.2, 40, 0, 400, type=ROOT.TH2F) + + self.book(f, "tPFMET_DeltaPhi", "tau-type1PFMET DeltaPhi" , 20, 0, 3.2) + + self.book(f, "ePFMET_DeltaPhi", "e-PFMET DeltaPhi" , 20, 0, 3.2) + + #self.book(f,"tMtToPFMET", "tau-PFMET M_{T}" , 200, 0, 200) + book_with_sys(f, "tMtToPfMet", "tau-PFMET M_{T}" , 40, 0, 200, + postfixes=full_met_systematics) + #self.book(f,"eMtToPFMET", "e-PFMET M_{T}" , 200, 0, 200) + book_with_sys(f, "eMtToPfMet", "e-PFMET M_{T}" , 40, 0, 200, + postfixes=full_met_systematics) + + #self.book(f, "pfMetEt", "pfMetEt", 200, 0, 200) + book_with_sys(f, "type1_pfMet_Et", "type1_pfMet_Et", 40, 0, 200, postfixes=full_met_systematics) + + #self.book(f, "pfMetPhi", "pfMetPhi", 100, -3.2, 3.2) + book_with_sys(f, "type1_pfMet_Phi", "type1_pfMet_Phi", 26, -3.2, 3.2, postfixes=full_met_systematics) + + self.book(f, "jetVeto20", "Number of jets, p_{T}>20", 5, -0.5, 4.5) + self.book(f, "jetVeto30", "Number of jets, p_{T}>30", 5, -0.5, 4.5) + + #index dirs and histograms + for key, value in self.histograms.iteritems(): + location = os.path.dirname(key) + name = os.path.basename(key) + if location not in self.histo_locations: + self.histo_locations[location] = {name : value} + else: + self.histo_locations[location][name] = value + + def fakerate_weights(self, tEta, ePt): + tLoose = tau_fake_rate(tEta) + tLooseUp = tau_fake_rate_up(tEta) + tLooseDown= tau_fake_rate_dw(tEta) + + tLoose = tLoose / (1. - tLoose ) + tLooseUp = tLooseUp / (1. - tLooseUp ) + tLooseDown= tLooseDown / (1. - tLooseDown) + + + eLoose = e_fake_rate(ePt)#self.efake + eLooseUp = e_fake_rate_up(ePt)#self.efakeup + eLooseDown= e_fake_rate_dw(ePt)#self.efakedw + + eLoose = eLoose / (1. - eLoose ) + eLooseUp = eLooseUp / (1. - eLooseUp ) + eLooseDown= eLooseDown / (1. - eLooseDown) + + + etLoose = tLoose * eLoose + etLooseUp = tLooseUp * eLooseUp + etLooseDown= tLooseDown *eLooseDown + + frweight = { + 'tLoose' : tLoose , + 'tLoose/Up' : tLooseUp , + 'tLoose/Down' : tLooseDown, + 'eLoose' : eLoose , + 'eLoose/Up' : eLooseUp , + 'eLoose/Down' : eLooseDown, + 'etLoose' : etLoose , + 'etLoose/Up' : etLooseUp , + 'etLoose/Down' : etLooseDown, + 'tLooseUnweight' : 1., + } + + return frweight; + + def fill_histos(self, folder_str, row, weight, filter_label = ''): + '''fills histograms''' + #find all keys matching + for attr, value in self.histo_locations[folder_str].iteritems(): + name = attr + #if attr=='DEBUG': + # set_trace() + if filter_label: + if not attr.startswith(filter_label+'$'): + continue + attr = attr.replace(filter_label+'$', '') + if value.InheritsFrom('TH2'): + if attr in self.hfunc: + try: + result, out_weight = self.hfunc[attr](row, weight) + except Exception as e: + raise RuntimeError("Error running function %s. Error: \n\n %s" % (attr, str(e))) + r1, r2 = result + if out_weight is None: + value.Fill( r1, r2 ) #saves you when filling NTuples! + else: + value.Fill( r1, r2, out_weight ) + else: + attr1, attr2 = split(attr) + v1 = getattr(row,attr1) + v2 = getattr(row,attr2) + value.Fill( v1, v2, weight ) if weight is not None else value.Fill( v1, v2 ) + else: + if attr in self.hfunc: + try: + result, out_weight = self.hfunc[attr](row, weight) + except Exception as e: + raise RuntimeError("Error running function %s. Error: \n\n %s" % (attr, str(e))) + if out_weight is None: + value.Fill( result ) #saves you when filling NTuples! + else: + value.Fill( result, out_weight ) + else: + value.Fill( getattr(row,attr), weight ) if weight is not None else value.Fill( getattr(row,attr) ) + return None + + def process(self): + logging.debug('Starting processing') + systematics = self.systematics + + frw = [] + lock =() + ievt = 0 + logging.debug('Starting evt loop') + + #pre-compute static things + sys_shifts = systematics['trig'] + \ + systematics['pu'] + \ + systematics['eid'] + \ + systematics['etaufake'] + \ + systematics['eiso'] #+ \ + + jes_dirs = [i.strip('_') for i in systematics['jes']] + #anyway the first is always "" + tes_dirs = [i.strip('_') for i in systematics['tes']][1:] + ees_dirs = [i.strip('_') for i in systematics['ees']][1:] + + for row in self.tree: + if (ievt % 100) == 0: + logging.debug('New event') + ievt += 1 + #avoid double counting events! + evt_id = (row.run, row.lumi, row.evt) + if evt_id == lock: continue + if lock != () and evt_id == lock: + logging.info('Removing duplicate of event: %d %d %d' % evt_id) + + # + #preselection, common to everything and everyone + # + #trigger + if self.is_embedded : + if not bool(row.doubleMuPass) : continue + else: + if not bool(row.singleE27WP80Pass) : continue + if not bool(row.eMatchesSingleE27WP80): continue + + #objects + if not selections.eSelection(row, 'e'): continue + #if row.ePt < 30 : continue + if not selections.tauSelection(row, 't'): continue + if not row.tAntiElectronMVA5Tight : continue + if not row.tAntiMuon2Loose : continue + if not row.tLooseIso3Hits : continue + logging.debug('object selection passed') + #e ID/ISO + if not selections.lepton_id_iso(row, 'e', 'eid13Loose_idiso05'): continue + logging.debug('Passed preselection') + + # + # Compute event weight + # + #event weight + #sys_shifts is precomputed + + #set_trace() + weight_map = self.event_weight(row, sys_shifts) + + #Fill embedded sample normalization BEFORE the vetoes + if not row.e_t_SS: + self.fill_histos('os/gg/ept30', row, weight_map['']) + + # it is better vetoing on b-jets after the histo for the DY embedded + #bjet veto + if row.bjetCSVVeto30!=0 : continue + + #tau ID, id Tau is tight then go in full selection, otherwise use for fakes + isTauTight = bool(row.tTightIso3Hits) + isETight = bool(selections.lepton_id_iso(row, 'e', 'eid13Tight_etauiso01')) + etau_category = [''] + if (not isETight) and (not isTauTight): + etau_category = ['etLoose', 'etLoose/Up', 'etLoose/Down'] + elif (not isTauTight): + etau_category = ['tLoose', 'tLoose/Up', 'tLoose/Down', 'tLooseUnweight'] + elif (not isETight): + etau_category = ['eLoose', 'eLoose/Up', 'eLoose/Down'] + + #jet category + central = struct( + njets = min(row.jetVeto30, 3), + tPt = row.tPt, + ePt = row.ePt + ) + jets = [min(row.jetVeto30, 3), min(row.jetVeto30jes_plus, 3), min(row.jetVeto30jes_minus, 3)] + tpts = [row.tPt_tes_plus, row.tPt_tes_minus] + epts = [row.ePt_ees_plus, row.ePt_ees_minus] + #print 'electron pt' , row.ePt, row.ePt_ees_plus, row.ePt_ees_minus + sys_effects = [(name, central.clone(njets = jnum)) for name, jnum in zip(jes_dirs, jets)] + sys_effects.extend( + [(name, central.clone(tPt = pt)) for name, pt in zip(tes_dirs, tpts)] + ) + sys_effects.extend( + [(name, central.clone(ePt = pt)) for name, pt in zip(ees_dirs, epts)] + ) + + # + # Full tight selection + # + passes_full_selection = False + selection_categories = [] + for name, shifted in sys_effects: + #preselection. flat pt values + if shifted.ePt > 30 and shifted.tPt > 30: + selection_categories.append((name, '%i' % shifted.njets, '')) + else: + continue + + if shifted.njets == 0 : + + selection_categories.extend([ + (name, '0', i) for i in optimizer.compute_regions_0jet( + shifted.tPt, shifted.ePt, deltaPhi(row.ePhi, row.tPhi), + row.tMtToPfMet) + ]) + + if shifted.tPt < 30: continue #was 35 + if shifted.ePt < 45: continue #was 40 + if deltaPhi(row.ePhi, row.tPhi) < 2.3 : continue #was 2.7 + if row.tMtToPfMet > 70 : continue #was 50 + selection_categories.append((name, '0', 'selected')) + passes_full_selection = True + elif shifted.njets == 1 : + selection_categories.extend([ + (name, '1', i) for i in optimizer.compute_regions_1jet( + shifted.tPt, shifted.ePt, row.tMtToPfMet) + ]) + + if shifted.tPt < 40: continue #was 40 + if shifted.ePt < 35: continue #was 35 + if row.tMtToPfMet > 40 : continue #was 35 + selection_categories.append((name, '1', 'selected')) + passes_full_selection = True + elif shifted.njets == 2 : + selection_categories.extend([ + (name, '2', i) for i in optimizer.compute_regions_2jet( + shifted.tPt, shifted.ePt, row.tMtToPfMet, row.vbfMass, + row.vbfDeta) + ]) + + if shifted.tPt < 30: continue #was 40 + if shifted.ePt < 35: continue #was 30 + if row.tMtToPfMet > 50 : continue #was 35 + if row.vbfMass < 400 : continue #was 550 + if row.vbfDeta < 2.3 : continue #was 3.5 + selection_categories.append((name, '2', 'selected')) + passes_full_selection = True + + if passes_full_selection: + logging.debug('Passed full selection') + + # + #different selections + # + sign = 'ss' if row.e_t_SS else 'os' + processtype ='gg' + ptthreshold = ['ept30'] + + # + # Lepton vetoes + # + tvetoes = [row.tauVetoPt20EleTight3MuLoose, row.tauVetoPt20EleTight3MuLoose_tes_plus, row.tauVetoPt20EleTight3MuLoose_tes_minus] + mvetoes = [row.muVetoPt5IsoIdVtx , row.muVetoPt5IsoIdVtx_mes_plus , row.muVetoPt5IsoIdVtx_mes_minus ] + evetoes = [row.eVetoCicLooseIso , row.eVetoCicLooseIso_ees_plus , row.eVetoCicLooseIso_ees_minus ] + + tdirs = [ i for i, j in zip( systematics['tvetos'], tvetoes) if not j] + mdirs = [ i for i, j in zip( systematics['mvetos'], mvetoes) if not j] + edirs = [ i for i, j in zip( systematics['evetos'], evetoes) if not j] + + #if any of the lists is empty + #set_trace() + if not tdirs or not mdirs or not edirs: + continue + logging.debug('Passed Vetoes') + + #make all possible veto combos... + all_dirs = [''.join(i) for i in itertools.product(tdirs, mdirs, edirs)] + #...and choose only the meaningful ones + veto_sys = set(systematics['tvetos']+systematics['mvetos']+systematics['evetos']) + all_dirs = [i for i in all_dirs if i in veto_sys] + + sys_directories = all_dirs + sys_shifts + #remove duplicates + sys_directories = list(set(sys_directories)) + + #at least one loose object + if (not isETight) or (not isTauTight): + #if is a loose tau just compute the fakes! + sys_directories = etau_category + + #gather the one and only weight we do care about + mc_weight = weight_map[''] + + #weights are the fr ones... + weight_map = self.fakerate_weights(row.tEta, row.ePt) + for i in weight_map: + #...times the mc weight (if any) + weight_map[i] *= mc_weight + + + #Fill histograms in appropriate direcotries + #if passes_full_selection: + #dirs = [os.path.join(sys, sign, processtype, e_thr, jet_dir) for sys, e_thr, jet_dir in itertools.product(sys_directories, ptthreshold, jet_directories)] + #if len(dirs) <> len(set(dirs)): + # set_trace() + for sys, e_thr, selection in itertools.product(sys_directories, ptthreshold, selection_categories): + selection_sys, jet_dir, selection_step = selection + #if we have multiple systematic shifts applied + #reject the combination + if selection_sys and sys: + continue + + #if we fill a histogram, lock the event + lock = evt_id + dir_name = os.path.join(sys, selection_sys, sign, processtype, + e_thr, jet_dir, selection_step) + + if dir_name[-1] == '/': + dir_name = dir_name[:-1] + if passes_full_selection: + logging.debug('Filling %s' % dir_name) + #fill them! + weight_to_use = weight_map[sys] if sys in weight_map else weight_map[''] + self.fill_histos(dir_name, row, weight_to_use) + + + def finish(self): + self.write_histos() + + diff --git a/lfvetau/NewEleFakeRateFromW.py b/lfvetau/NewEleFakeRateFromW.py new file mode 100644 index 00000000..750d5609 --- /dev/null +++ b/lfvetau/NewEleFakeRateFromW.py @@ -0,0 +1,244 @@ +##Correction Factor still to add +from EETree import EETree +import os +import ROOT +import math +import optimizer +import glob +import array +import mcCorrections +import baseSelections as selections +import FinalStateAnalysis.PlotTools.pytree as pytree +from FinalStateAnalysis.PlotTools.decorators import memo_last +from FinalStateAnalysis.PlotTools.MegaBase import MegaBase +from math import sqrt, pi, sin, cos, acos, sinh +from cutflowtracker import cut_flow_tracker +#Makes the cut flow histogram +cut_flow_step = ['allEvents', 'e1sel', 'e1IDiso', 'e2sel', 'e2IDiso', 'ZMass', 'tsel', 'tAntiMuon', 'tAntiEle', 'MtToMet', 'tRawIso10','tRawIso5', 'tLooseIso', 'tTightIso' +] + +from inspect import currentframe + +def get_linenumber(): + cf = currentframe() + return cf.f_back.f_lineno + +def deltaPhi(phi1, phi2): + PHI = abs(phi1-phi2) + if PHI<=pi: + return PHI + else: + return 2*pi-PHI +def deltaR(phi1, phi2, eta1, eta2): + deta = eta1 - eta2 + dphi = abs(phi1-phi2) + if (dphi>pi) : dphi = 2*pi-dphi + return sqrt(deta*deta + dphi*dphi); + + +class NewEleFakeRateFromW(MegaBase): + tree = 'ee/final/Ntuple' + def __init__(self, tree, outfile, **kwargs): + self.channel='EE' + super(NewEleFakeRateFromW, self).__init__(tree, outfile, **kwargs) + self.tree = EETree(tree) + self.out=outfile + self.histograms = {} + self.pucorrector = mcCorrections.make_puCorrector('singlee') + self.mye1 = 'e1' + self.mye2 = 'e2' + #optimizer_keys = [ i for i in optimizer.grid_search.keys() if i.startswith(self.channel) ] + self.grid_search = {} + #if len(optimizer_keys) > 1: + # for key in optimizer_keys: + # self.grid_search[key] = optimizer.grid_search[key] + #else: + # self.grid_search[''] = optimizer.grid_search[optimizer_keys[0]] + + + def event_weight(self, row): + if row.run > 2: #FIXME! add tight ID correction + return 1. + + + return self.pucorrector(row.nTruePU) * \ + mcCorrections.eid_correction( row, 'e1', 'e2') * \ + mcCorrections.eiso_correction(row, 'e1', 'e2') * \ + mcCorrections.trig_correction(row, 'e2' ) + + + +##add the trigger correction + + def begin(self): + + eiso = ['eLoose', 'eTigh'] + folder = [] + sign = ['ss','os'] + for iso in eiso: + for s in sign: + folder.append(s+'/'+iso) + j=0 + while j < 4 : + folder.append(s+'/'+iso+'/'+str(j)) + j+=1 + + for f in folder: + + self.book(f,"e1Pt", "e1 p_{T}", 200, 0, 200) + ##self.book(f,"e1Phi", "e1 phi", 100, -3.2, 3.2) + ##self.book(f,"e1Eta", "e1 eta", 46, -2.3, 2.3) + ## + self.book(f,"e2Pt", "e2 p_{T}", 200, 0, 200) + self.book(f,"e2Phi", "e2 phi", 100, -3.2, 3.2) + self.book(f,"e2Eta", "e2 eta", 46, -2.3, 2.3) + self.book(f,"e2AbsEta", "e2 abs eta", 23, 0, 2.3) + self.book(f,"e2Pt_vs_e2AbsEta", "e2 pt vs e2 abs eta", 23, 0, 2.3, 20, 0, 200., type=ROOT.TH2F) + + + self.book(f, "e1e2Mass", "e1e2 Inv Mass", 32, 0, 320) + self.book(f, "e1MtToPFMET", "e1MtToPFMET", 20, 0, 200 ) + self.book(f, "e2MtToPFMET", "e2MtToPFMET", 20, 0, 200 ) + self.book(f, "e1e2_DeltaPhi", "e1-e2 DeltaPhi" , 32, 0, 3.2) + self.book(f, "e1e2_DeltaR", "e1-e2 DeltaR" , 32, 0, 3.2) + + + for s in sign: + self.book(s+'/tNoCuts', "CUT_FLOW", "Cut Flow", len(cut_flow_step), 0, len(cut_flow_step)) + + xaxis = self.histograms[s+'/tNoCuts/CUT_FLOW'].GetXaxis() + self.cut_flow_histo = self.histograms[s+'/tNoCuts/CUT_FLOW'] + self.cut_flow_map = {} + for i, name in enumerate(cut_flow_step): + xaxis.SetBinLabel(i+1, name) + self.cut_flow_map[name] = i+0.5 + + def fill_histos(self, row, folder='os/tSuperLoose', fakeRate = False): + weight = self.event_weight(row) + histos = self.histograms + + + + + ##histos[folder+'/e1Pt'].Fill( getattr(row, self.mye1+'Pt'), weight) + ##histos[folder+'/e1Eta'].Fill(getattr(row, self.mye1+'Eta'), weight) + ##histos[folder+'/e1Phi'].Fill(getattr(row, self.mye1+'Phi'), weight) + ## + histos[folder+'/e2Pt'].Fill( row.e2Pt , weight) + histos[folder+'/e2Eta'].Fill(row.e2Eta, weight) + histos[folder+'/e2Phi'].Fill(row.e2Phi, weight) + histos[folder+'/e2AbsEta'].Fill(abs(row.e2Eta), weight) + histos[folder+'/e2Pt_vs_e2AbsEta'].Fill(abs(row.e2Eta), row.e2Pt, weight) + + histos[folder+'/e1e2Mass'].Fill(getattr(row, 'e1_e2_Mass'), weight) + histos[folder+'/e1MtToPFMET'].Fill(row.e1MtToPfMet,weight) + histos[folder+'/e2MtToPFMET'].Fill(row.e2MtToPfMet,weight) + + histos[folder+'/e1e2_DeltaPhi'].Fill(deltaPhi(row.e1Phi, row.e2Phi), weight) + histos[folder+'/e1e2_DeltaR'].Fill(row.e1_e2_DR, weight) + + + ###histos[folder+'/type1_pfMetEt'].Fill(row.type1_pfMet_Et) + ##histos[folder+'/ee3DR'].Fill(self.ee3DR(row)) + ##histos[folder+'/ee3DPhi'].Fill(self.ee3DPhi(row)) + ##histos[folder+'/jetN_30'].Fill(row.jetVeto30, weight) + ##histos[folder+'/bjetCSVVeto30'].Fill(row.bjetCSVVeto30, weight) + ## + ##histos[folder+'/ze3DR'].Fill(deltaR(self.Z(row).Phi(), getattr(row, self.mye3+'Phi'), self.Z(row).Eta(), getattr(row, self.mye3+'Eta'))) + ##histos[folder+'/ze3DPhi'].Fill(deltaPhi(self.Z(row).Phi(), getattr(row, self.mye3+'Phi'))) + ##histos[folder+'/Zpt'].Fill(self.Z(row).Pt()) + + + def process(self): + + cut_flow_histo = self.cut_flow_histo + cut_flow_trk = cut_flow_tracker(cut_flow_histo) + myevent =() + #print self.tree.inputfilename + for row in self.tree: + jn = row.jetVeto30 + if jn > 3 : jn = 3 + + #if row.run > 2: + if not bool(row.singleE27WP80Pass) : continue + # if hasattr(self.tree, 'row.e1MatchesEle27WP80') and hasattr(self.tree, 'row.e2MatchesEle27WP80') : + #if not bool(row.e1MatchesEle27WP80) and not bool(row.e2MatchesEle27WP80) : continue + + #else : + if not bool(row.e2MatchesSingleE27WP80) : continue + if not bool(row.e1MatchesSingleE27WP80) : continue + #if not bool(row.singleEPass) : continue + #if not bool(row.e1MatchesSingleE) and not bool(row.e2MatchesSingleE) : continue + + if row.bjetCSVVeto30!=0 : continue + #if jn !=0: continue + if row.e1Pt < 30 : continue + if row.e2Pt < 30 : continue + +# for i, row in enumerate(self.tree): +# if i >= 100: +# return + # print bool(cut_flow_trk.disabled) + cut_flow_trk.new_row(row.run,row.lumi,row.evt) + #print row.run,row.lumi,row.evt + cut_flow_trk.Fill('allEvents') + if not selections.eSelection(row, 'e1'): continue + cut_flow_trk.Fill('e1sel') + if not selections.lepton_id_iso(row, 'e1', 'eid13Tight_etauiso01'): continue ##good iso point 0.5 + if abs(row.e1Eta) > 1.4442 and abs(row.e1Eta < 1.566) : continue + + + cut_flow_trk.Fill('e1IDiso') + if not selections.eSelection(row, 'e2'): continue + cut_flow_trk.Fill('e2sel') + if not selections.lepton_id_iso(row, 'e2', 'eid13Loose_idiso05'): continue # goodisopoint 0.5 + if abs(row.e2Eta) > 1.4442 and abs(row.e2Eta) < 1.566 : continue + + + ##if not row.e1_e2_SS: continue + ## cut_flow_trk.Fill('e2IDiso') + if abs(row.e1_e2_Mass-91.2) < 20: continue + ## cut_flow_trk.Fill('ZMass')' + + if row.e1MtToPfMet < 20 : continue + + + cut_flow_trk.Fill('tsel') + + + if row.tauVetoPt20EleTight3MuLoose : continue + if row.muVetoPt5IsoIdVtx : continue + if row.eVetoCicLooseIso : continue # change it with Loose + + #if not row.e3MtToMET < 50: continue + cut_flow_trk.Fill('MtToMet') + + + #if (row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt)==myevent: continue + #myevent=(row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt) + + eleiso = 'eLoose' + sign = 'ss' if row.e1_e2_SS else 'os' + folder = sign+'/'+eleiso + + self.fill_histos(row, folder) + folder=folder+'/'+str(int(jn)) + self.fill_histos(row, folder) + + if selections.lepton_id_iso(row, 'e2', 'eid13Tight_etauiso01'): + eleiso = 'eTigh' + folder = sign+'/'+eleiso + self.fill_histos(row, folder) + cut_flow_trk.Fill('tTightIso') + folder=folder+'/'+str(int(jn)) + self.fill_histos(row, folder) + + + + + cut_flow_trk.flush() + + + + def finish(self): + self.write_histos() diff --git a/lfvetau/Rakefile b/lfvetau/Rakefile new file mode 100644 index 00000000..b209b765 --- /dev/null +++ b/lfvetau/Rakefile @@ -0,0 +1,508 @@ +# Get common recipes +recipes = ENV['fsa'] + '/PlotTools/rake/recipes.rake' +import recipes + +require ENV['CMSSW_BASE'] + '/src/FinalStateAnalysis/PlotTools/rake/tools.rb' +require 'pathname' + +$jobid = ENV['jobid'] +$blind = ENV['blind'] + +# Figure out what run period we are in +$period = '8TeV' +PU = ENV['PU'] +#if $jobid.include? '8TeV' +# $period = '8TeV' +#end + + +################################################################################ +## Sample names ################################################################ +################################################################################ +# +# Get sample names containing a substring +def get_sample_names(substring) + inputs = Dir.glob("inputs/#{$jobid}/*.txt").select {|x| x.include? substring} + inputs = inputs.map{|x| File.basename(x).sub(".txt", "")} + return inputs +end +# +samples = Hash[ + "ttbar" => get_sample_names('TTJets'), + "singlet" => get_sample_names('T_t'), + "singletbar" => get_sample_names('Tbar_t'), + "wjets" => get_sample_names('Wplus'), + "wwjets" => get_sample_names('WWJets'), + "wzjets" => get_sample_names('WZJets'), + "zzjets" => get_sample_names('ZZJets'), + "zjets" => get_sample_names('jets_M50'), + "zjets_skimmedLL" => get_sample_names('jets_M50_skimmedLL'), + "zjets_skimmedTT" => get_sample_names('jets_M50_skimmedTT'), + "diboson" =>get_sample_names('WWJets')+get_sample_names('WZJets')+get_sample_names('ZZJets'), + "extra" => Array['T_tW-channel-DR_TuneZ2star_8TeV-powheg-tauola','Tbar_tW-channel-DR_TuneZ2star_8TeV-powheg-tauola','Tbar_t-channel','T_t-channel'], + "ewk" => Array['Zjets_M50', + 'WWJetsTo2L2Nu_TuneZ2_8TeV', + 'TTJets_FullLeptMGDecays_8TeV-madgraph-tauola','TTJets_SemiLeptMGDecays_8TeV-madgraph-tauola'], + # Maria's version + + + "signalMCgg" => get_sample_names('ggHiggsToETau'), + "signalMCvbf" => get_sample_names('vbfHiggsToETau'), + "signalMCMuTaugg" => get_sample_names('ggHiggsToMuTau'), + "signalMCMuTauvbf" => get_sample_names('vbfHiggsToMuTau'), + "ggHiggsTo2Taus" => get_sample_names('GluGluToHToTauTau_M-125_8TeV-powheg-pythia6'), + "vbfHiggsTo2Taus" => get_sample_names('VBF_HToTauTau_M-125_8TeV-powheg-pythia6'), + "Zembedded" => get_sample_names('ZetauEmbedded'), + "dataSingleE" => get_sample_names('data_SingleElectron_Run2012') + +] + + + +# Function to get the .root files for an analyzer and samples +def get_analyzer_results(analyzer, the_samples) + output = Array.new + analyzer_base = analyzer.sub('.py', '') + the_samples.each do |sample| + output << "results/#{$jobid}/#{analyzer_base}/#{sample}.root" + end + return output +end +def get_plotter_results(analyzer) + output = Array.new + analyzer_base = analyzer.sub('.py', '') +end + + +$frfit_dir = "results/#{$jobid}/fakerate_fits" +directory $frfit_dir +$frfit_dirMC = "results/#{$jobid}/fakerate_fits_MC" +directory $frfit_dirMC +$efrfit_dir = "results/#{$jobid}/efakerate_fits" +directory $efrfit_dir +$efrfit_dirMC = "results/#{$jobid}/efakerate_fits_MC" +directory $efrfit_dirMC +exponential = "scale*TMath::Exp(x*decay)+offset" +exponential_vars = "scale[4., 0, 10],decay[-1e-2, -1, -1e-4],offset[1e-2, 0, 0.5]" +exponentialpol1 = "scale*TMath::Exp(x*decay)+offset+slope*x" +exponentialpol1_vars = "scale[4., 0, 10],decay[-1e-2, -1, -1e-4],offset[1e-2, 0, 0.5],slope[1e-1, 1, 1]" +landau = "scale*TMath::Landau(x,mu,sigma,0)+offset" +landau_vars = "scale[0.5, 0, 15],mu[5, 0, 30],sigma[1.9, 0.1, 20],offset[1e-2, 0, 0.5]" + +pol0 = "const" +pol0_vars = "const[ 0.5, 0, 1]" +pol1 = "p0+p1*x" +pol1_vars = "p0[ 0.5, 0, 1], p1[ -0.1, -1, 1]" +pol2 = "p0+p1*x+p2*x*x" +pol2_vars = "p0[ 0.05, 0, 1], p1[ -0.005, -1, 1], p2[ 0.005, 0, 1]" + +erf = "scale*TMath::Erf((x-shift)*steep)+offset" +erf_vars = "scale[0.1, 0, 10],shift[1., 0.8, 1.2],steep[100.], offset[1e-2, 0, 0.5]" + + +#$fr_sample = "\"wzjets\", \"wwjets\", \"zzjets\", \"datasingleE\"" +#$fr_sample = "dataSingleE" + +#$fr_binning = "30,35,40,45,50,55,60,70,80,90,100,120,140,160,180,200" +#$fr_binning = "30,40,50,60,70,80,100,120,140,160,200" +$fr_binning = "0,30,40,50,60,70,80,100,120,150,200" +#$fr_binning = "30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200" +$fr_analyzer = "TauFakeRateAnalyzerMVA" #was TauFakeRateAnalyzer +$efr_analyzer = "EleFakeRateAnalyzerMVA" +fr_fits = Hash.new +efr_fits = Hash.new +# Fake rate fit configurations + +#mysamples = [ "zjets_skimmedLL", "zjets_skimmedTT"] +#for s in mysamples + +fr_fits["t_os_tLoose_tTigh_tPt"] = Hash[ + "samples" => Array["diboson", "dataSingleE"],#, "zjets_skimmedLL"], + "analyzer" => $fr_analyzer, + #"function" => exponential, + #"variables" => exponential_vars, + "function" => pol0, + "variables"=>pol0_vars, + #"rebin" => 1, + "rebin" => $fr_binning, + "range" => "30 200", + "title" => "#tau p_{T} (GeV)", + "min"=>"0", + "max"=>"1" + ] +efr_fits["e_os_eLoose_eTigh_e3Pt"] = Hash[ + "samples" => Array["diboson", "dataSingleE"], + "analyzer" => $efr_analyzer, + #"function" => exponential, + #"variables" => exponential_vars, + "function" => pol0, + "variables"=>pol0_vars, + #"rebin" => 1, + "rebin" => "30,50,70,100,150,200",#$fr_binning, + "range" => "30 200", + "title" => "e p_{T} (GeV)", + "min"=>"0", + "max"=>"1.2" + ] + +fr_fits["t_os_tLoose_tTigh_tAbsEta"] = Hash[ + "samples" => Array["diboson", "dataSingleE", "zjets_skimmedLL"], + "analyzer" => $fr_analyzer, + "function" => pol2, + "variables" => pol2_vars, + "rebin" => 1, + "range" => "0 2.3", + #"range" => "-2.3 2.3", + "title" => "#tau #eta", + "min"=>"0", + "max"=>"1" + ] + + +efr_fits["e_os_eLoose_eTigh_e3AbsEta"] = Hash[ + "samples" => Array["diboson", "dataSingleE"],#, "zjets_skimmedLL"], + "analyzer" => $efr_analyzer, + "function" => pol0, + "variables" => pol0_vars, + "rebin" => "0.,0.4,0.8,1.2,1.6,2.0,2.3",#4, + "range" => "0 2.3", + #"range" => "-2.3 2.3", + "title" => "e #eta", + "min"=>"0", + "max"=>"1.2" + ] + + + + +task :fits => [] +task :fitsMC => [] + +task :efitsMC => [] + +task :efits => [] + +##fr_fits.each do |fit, fit_info| +## fit_configuration = fit.split("_") +## sign = fit_configuration[1] +## denom = fit_configuration[2] +## num = fit_configuration[3] +## var = fit_configuration[4] +## +## subsample_inputs = samples['zjets_skimmedLL'] +## fit_output = $frfit_dirMC + "/#{fit}.root" +## subsamples_inputs_result_list = subsample_inputs.map{|x| "results/#{$jobid}/#{fit_info['analyzer']}/#{x}.root"} +## subsample_input_list = subsamples_inputs_result_list.join(" ") +## denom_path = Array[sign, denom,var].join("/") +## num_path = Array[sign, num, var].join("/") +## +## file fit_output => subsamples_inputs_result_list + [fit_info['analyzer'] + '.py'] do |t| +## sh "mkdir -p #{$frfit_dirMC}" +## sh "fit_efficiency_chi2.py #{fit_output} '#{num_path}' '#{denom_path}' \'#{pol0}\' \'#{pol0_vars}\' #{subsample_input_list} --plot --xrange #{fit_info['range']} --xtitle \'#{fit_info['title']}\' --min \'#{fit_info['min']}\' --max \'#{fit_info['max']}\' --show-error --rebin #{fit_info['rebin']} " +## puts "" +## end +## +## task :fits_MC => fit_output +## +##end +## +efr_fits.each do |fit, fit_info| + fit_configuration = fit.split("_") + sign = fit_configuration[1] + denom = fit_configuration[2] + num = fit_configuration[3] + var = fit_configuration[4] + + subsample_inputs = samples['zjets_skimmedLL'] + fit_output = $efrfit_dirMC + "/#{fit}.root" + subsamples_inputs_result_list = subsample_inputs.map{|x| "results/#{$jobid}/#{fit_info['analyzer']}/#{x}.root"} + subsample_input_list = subsamples_inputs_result_list.join(" ") + denom_path = Array[sign, denom,var].join("/") + num_path = Array[sign, num, var].join("/") + + file fit_output => subsamples_inputs_result_list + [fit_info['analyzer'] + '.py'] do |t| + sh "mkdir -p #{$efrfit_dirMC}" + sh "fit_efficiency_chi2.py #{fit_output} '#{num_path}' '#{denom_path}' \'#{pol0}\' \'#{pol0_vars}\' #{subsample_input_list} --plot --xrange #{fit_info['range']} --xtitle \'#{fit_info['title']}\' --min \'#{fit_info['min']}\' --max \'#{fit_info['max']}\' --show-error --rebin #{fit_info['rebin']} " + puts "" + end + + task :efits_MC => fit_output +end + +fr_fits.each do |fit, fit_info| + fit_configuration = fit.split("_") + sign = fit_configuration[1] + denom = fit_configuration[2] + num = fit_configuration[3] + var = fit_configuration[4] + + subsample_inputs = [] + fit_info['samples'].each do |sample| + subsample_inputs += samples[sample] + end + + fit_output = $frfit_dir + "/#{fit}.root" + subsamples_inputs_result_list = subsample_inputs.map{|x| "results/#{$jobid}/#{fit_info['analyzer']}/#{x}.root"} + # subsamples_inputs_result_list = subsample_inputs.map{|x| "results/#{$jobid}/TauFakeRateAnalyzerMVA_eTight/#{x}.root"} + subsample_input_list = subsamples_inputs_result_list.join(" ") + + # Path to histograms in root files + denom_path = Array[sign, denom,var].join("/") + num_path = Array[sign, num, var].join("/") + + corrected_file = fit_output.sub('.root', '.corrected_inputs.root') + file corrected_file => subsamples_inputs_result_list + [fit_info['analyzer'] + '.py', "CorrectFakeRateData.py"] do |t| + sh "mkdir -p #{$frfit_dir}" + sh "python CorrectFakeRateData.py --files #{subsample_input_list} --lumifiles inputs/#{$jobid}/*sum --outputfile #{t.name} --numerator '#{num_path}' --denom '#{denom_path}' --rebin #{fit_info['rebin']}" + puts "" + end + + file fit_output => corrected_file do |t| + sh "fit_efficiency_chi2.py #{fit_output} numerator denominator \'#{fit_info['function']}\' \'#{fit_info['variables']}\' #{corrected_file} --plot --xrange #{fit_info['range']} --xtitle \'#{fit_info['title']}\' --min \'#{fit_info['min']}\' --max \'#{fit_info['max']}\' --show-error " + puts "" + end + + # file fit_output do |t| + # sh "mkdir -p #{$frfit_dir}" + # sh "fit_efficiency_chi2.py --rebin #{fit_info['rebin']} #{fit_output} #{num_path} #{denom_path} \'#{fit_info['function']}\' \'#{fit_info['variables']}\' #{subsample_input_list} --plot --xrange #{fit_info['range']} --xtitle \'#{fit_info['title']}\'" + # puts "" + # end + + task :fits => fit_output + +end +#end + +efr_fits.each do |fit, fit_info| + fit_configuration = fit.split("_") + sign = fit_configuration[1] + denom = fit_configuration[2] + num = fit_configuration[3] + var = fit_configuration[4] + + subsample_inputs = [] + fit_info['samples'].each do |sample| + subsample_inputs += samples[sample] + end + fit_output = $efrfit_dir + "/#{fit}.root" + subsamples_inputs_result_list = subsample_inputs.map{|x| "results/#{$jobid}/#{fit_info['analyzer']}/#{x}.root"} + subsample_input_list = subsamples_inputs_result_list.join(" ") + + # Path to histograms in root files + denom_path = Array[sign, denom,var].join("/") + num_path = Array[sign, num, var].join("/") + + corrected_file = fit_output.sub('.root', '.corrected_inputs.root') + #file corrected_file => subsamples_inputs_result_list + [fit_info['analyzer'] + '.py', "CorrectFakeRateData.py"] do |t| + file corrected_file do |t| + sh "mkdir -p #{$efrfit_dir}" + sh "python CorrectFakeRateData.py --files #{subsample_input_list} --lumifiles inputs/#{$jobid}/*sum --outputfile #{t.name} --numerator '#{num_path}' --denom '#{denom_path}' --rebin #{fit_info['rebin']}" + puts "" + end + + file fit_output => corrected_file do |t| + sh "fit_efficiency_chi2.py #{fit_output} numerator denominator \'#{fit_info['function']}\' \'#{fit_info['variables']}\' #{corrected_file} --plot --xrange #{fit_info['range']} --xtitle \'#{fit_info['title']}\' --min \'#{fit_info['min']}\' --max \'#{fit_info['max']}\' --show-error " + puts "" + end + + + task :efits => fit_output + +end + + + +################################################################################ +## Recipes to analyze the GG channel of the LFV HToMuTau analysis +## targets: +## mt +################################################################################ + +task :genkin => get_analyzer_results("LFVHAnalyzeGEN.py", samples['signalMCgg'] + samples['signalMCvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus'] ) + + +task :genkinMuTau => get_analyzer_results("LFVHAnalyzeGENMuTau.py", samples['signalMCMuTaugg'] + samples['signalMCMuTauvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus']) + +task :genkinMuTauSIGNAL => get_analyzer_results("LFVHAnalyzeGENMuTau.py", samples['signalMCMuTaugg']) + +task :genkinEMu => get_analyzer_results("LFVHAnalyzeGENEMu.py", samples['signalMCMuTaugg'] + samples['signalMCMuTauvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus']) + + +task :recoplots => get_analyzer_results("LFVHETauAnalyzer.py", samples['signalMCgg'] + samples['signalMCvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus'] + samples['ttbar'] + samples['singlet'] + samples['singletbar'] + samples['wjets'] + samples['wwjets'] + samples['wzjets'] + samples['zzjets'] + samples['zjets'])# + samples['dataSingleE'] ) + +task :recoplotsMVA => get_analyzer_results("LFVHETauAnalyzerMVA.py", samples['signalMCgg'] + samples['signalMCvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus'] + samples['ttbar'] + samples['singlet'] + samples['singletbar'] + samples['wjets'] + samples['wwjets'] + samples['wzjets'] + samples['zzjets'] + samples['zjets'] + + samples['Zembedded']+ + samples['dataSingleE'] + ) +task :testMVA => get_analyzer_results("LFVHETauAnalyzerMVA.py", samples['signalMCgg'] + samples['signalMCvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus'] + samples['ttbar'] + samples['singlet'] + samples['singletbar'] + samples['wjets'] + + samples['wwjets'] + samples['wzjets'] + samples['zzjets'] + samples['zjets'] + + samples['Zembedded']+ + samples['dataSingleE'] + ) + + +task :recoplotsMVAeMtcut => get_analyzer_results("LFVHETauAnalyzerMVA_eMTCut.py", samples['signalMCgg'] + samples['signalMCvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus'] + samples['ttbar'] + samples['singlet'] + samples['singletbar'] + samples['wjets'] + samples['wwjets'] + samples['wzjets'] + samples['zzjets'] + samples['zjets'] + + samples['dataSingleE'] ) + + +task :recoplotsMuTau => get_analyzer_results("LFVHMuTauAnalyzer.py", samples['signalMCMuTaugg'] + samples['signalMCMuTauvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus'] + samples['ttbar'] + samples['singlet'] + samples['singletbar'] + samples['wjets'] + samples['wwjets'] + samples['wzjets'] + samples['zzjets'] + samples['zjets'] ) + +task :controlplots => get_analyzer_results("EEAnalyzer.py",samples['signalMCgg'] + samples['signalMCvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus'] + samples['ttbar'] + samples['singlet'] + samples['singletbar'] + samples['wjets'] + samples['wwjets'] + samples['wzjets'] + samples['zzjets'] + samples['zjets'] + samples['dataSingleE']) + +task :controlplotsMVA => get_analyzer_results("EEAnalyzerMVA.py", #samples['signalMCvbf'] )#samples['zjets']) + samples['signalMCgg'] + samples['signalMCvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus'] + samples['ttbar'] + samples['singlet'] + samples['singletbar'] + samples['wjets'] + samples['wwjets'] + samples['wzjets'] + samples['zzjets'] + samples['zjets'] + samples['Zembedded']+ samples['dataSingleE']) + + +task :fakeeet => get_analyzer_results("TauFakeRateAnalyzer.py", samples['signalMCgg'] + samples['signalMCvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus'] + samples['ttbar'] + samples['singlet'] + samples['singletbar'] + samples['wjets'] + + samples['wwjets'] + samples['zzjets'] + + samples['wzjets'] + + samples['zjets'] + samples['Zembedded']+samples['dataSingleE']) + +task :fakeeetMVA => get_analyzer_results("TauFakeRateAnalyzerMVA.py",# samples['signalMCgg'] + samples['signalMCvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus'] + samples['ttbar'] + samples['singlet'] + samples['singletbar'] + samples['wjets'] + samples['Zembedded'] + + samples['zjets'] + + samples['wwjets'] + samples['zzjets'] + + samples['wzjets'] + + samples['dataSingleE']) + +task :fakeeeeMVA => get_analyzer_results("EleFakeRateAnalyzerMVA.py", #samples['signalMCgg'] + samples['signalMCvbf'] + samples['ggHiggsTo2Taus'] + samples['vbfHiggsTo2Taus'] + samples['ttbar'] + samples['singlet'] + samples['singletbar'] + samples['wjets'] + samples['Zembedded']+ + samples['zjets'] + + samples['wwjets'] + samples['zzjets'] + + samples['wzjets'] + + samples['dataSingleE'] + ) + + +$etdir = "plots/#{$jobid}/LFVHETauAnalyzer/et/" +directory $etdir +file "#{$emtdir}/plot#{$period}.root" do |t| + sh "echo $jobid" + sh "python myNewPlotterReco.py" + +end + + +task :drawTauFakeRate => get_plotter_results("plotTauFakeRate.py") + +## +## Make shapes +## +$shapedir = "plots/#{$jobid}/lfvet/" + +#All shape files are made at the same time +## file "#{$shapedir}/.shapes_timestamp" => get_analyzer_results("LFVHETauAnalyzer.py", +## samples['signalMCgg'] + \ +## samples['signalMCvbf'] + \ +## samples['ggHiggsTo2Taus'] + \ +## samples['vbfHiggsTo2Taus'] + \ +## samples['ttbar'] + \ +## samples['singlet'] + \ +## samples['singletbar'] + \ +## samples['wjets'] + \ +## samples['wwjets'] + \ +## samples['wzjets'] + \ +## samples['zzjets'] + \ +## samples['zjets']) do |t| +## sh "python plotRecoQuantitiesMVA.py" +## sh "touch #{t.name}" +## end +file "#{$shapedir}/.shapes_timestamp" => [] do |t| + sh "python plotRecoQuantitiesMVA.py" +end + + +## +## Prepare datacards +## +$limitdir = "limits/#{$jobid}/" +def prepare_limit(category) + #input file does not matter since shape files are made all at once + datacard = "#{$limitdir}/#{category}/126/datacard_et_#{category}.txt" + file datacard => "#{$shapedir}/.shapes_timestamp" do |t| + sh "./prepare_limit.sh #{category}" + end + return datacard +end + +card_0_jet = prepare_limit('0') +card_1_jet = prepare_limit('1') +card_2_jet = prepare_limit('2') + +#combine cards +cmb_card = "#{$limitdir}/cmb/126/datacard_et_cmb.txt" +file cmb_card => [card_0_jet, + card_1_jet, + card_2_jet] do |t| + sh "mkdir -p #{$limitdir}/cmb/126" + sh "hadd -f #{$limitdir}/cmb/shapesETauHad.root #{$limitdir}/[0-9]/shapesETau$category?Jet.root" + chdir("#{$limitdir}/cmb/126") do + sh "rm -f shapesETauHad.root" + sh "ln -s ../shapesETauHad.root" + local_paths = t.prerequisites.map{|x| x.sub($limitdir,'../../')} + local_name = File.basename(t.name) + sh "combine_cards_with_names.sh #{ local_paths.join(' ')} > #{local_name}" + end +end + +#TODO combination! +task :shapes => [ + card_0_jet, + card_1_jet, + card_2_jet, + cmb_card +] + +## +## Compute Limit +## +def compute_limit(category) + #input file does not matter since shape files are made all at once + datacard = "#{$limitdir}/#{category}/126/datacard_et_#{category}.txt" + timestamp = "#{$limitdir}/#{category}/.limit_timestamp" + file timestamp => datacard do |t| + sh "compute_limits.sh #{$limitdir}/#{category} 1 #{$blind}" + sh "touch #{t.name}" + end + return timestamp +end + +limit_0_jet = compute_limit('0') +limit_1_jet = compute_limit('1') +limit_2_jet = compute_limit('2') +limit_c_jet = compute_limit('cmb') +#TODO combination! +task :limits => [ + limit_0_jet, + limit_1_jet, + limit_2_jet, + limit_c_jet, +] + +## +## Compute Significance +## +def compute_significance(category) + #input file does not matter since shape files are made all at once + datacard = "#{$limitdir}/#{category}/126/datacard_et_#{category}.txt" + timestamp = "#{$limitdir}/#{category}/.significance_timestamp" + file timestamp => datacard do |t| + chdir("#{$limitdir}/#{category}") do + dcard_name = File.basename(datacard) + sh "compute_significance.sh 126 #{$blind} #{dcard_name}" + end + sh "touch #{t.name}" + end + return timestamp +end + +significance_0_jet = compute_significance('0') +significance_1_jet = compute_significance('1') +significance_2_jet = compute_significance('2') +significance_c_jet = compute_significance('cmb') +#TODO combination! +task :significance => [ + significance_0_jet, + significance_1_jet, + significance_2_jet, +# significance_c_jet +] + diff --git a/lfvetau/TTbarControlRegion.py b/lfvetau/TTbarControlRegion.py new file mode 100644 index 00000000..f6f7cfb2 --- /dev/null +++ b/lfvetau/TTbarControlRegion.py @@ -0,0 +1,623 @@ +from ETauTree import ETauTree +import sys +import logging +logging.basicConfig(stream=sys.stderr, level=logging.WARNING) +import os +from pdb import set_trace +import ROOT +import math +import glob +import array +import mcCorrections +import baseSelections as selections +import FinalStateAnalysis.PlotTools.pytree as pytree +from FinalStateAnalysis.PlotTools.decorators import memo_last +from FinalStateAnalysis.PlotTools.MegaBase import MegaBase +from math import sqrt, pi, cos +from fakerate_functions import tau_fake_rate, tau_fake_rate_up, tau_fake_rate_dw, e_fake_rate, e_fake_rate_up, e_fake_rate_dw +import itertools +import traceback +from FinalStateAnalysis.PlotTools.decorators import memo +import FinalStateAnalysis.PlotTools.pytree as pytree +from FinalStateAnalysis.Utilities.struct import struct + +@memo +def getVar(name, var): + return name+var + +met_et = 'pfMet_Et%s' +met_phi = 'pfMet_Phi%s' +t_pt = 'tPt%s' +etMass = 'e_t_Mass%s' +@memo +def met(shift=''): + return met_et % shift + +@memo +def metphi(shift=''): + return met_phi % shift +@memo +def tpt(shift=''): + return t_pt % shift + +@memo +def vismass(shift=''): + return etMass % shift + +@memo +def split(string, separator='#'): + return tuple(attr.split(separator)) + +def create_mapper(mapping): + def _f(path): + for key, out in mapping.iteritems(): + if key == path: + path = path.replace(key,out) + print 'path', path + return path + return _f + +def attr_getter(attribute): + '''return a function that gets an attribute''' + def f(row, weight): + return (getattr(row,attribute), weight) + return f + +def merge_functions(fcn_1, fcn_2): + '''merges two functions to become a TH2''' + def f(row, weight): + r1, w1 = fcn_1(row, weight) + r2, w2 = fcn_2(row, weight) + w = w1 if w1 and w2 else None + return ((r1, r2), w) + return f + +def collmass(row, met, metPhi): + ptnu =abs(met*cos(deltaPhi(metPhi, row.tPhi))) + visfrac = row.tPt/(row.tPt+ptnu) + #print met, cos(deltaPhi(metPhi, row.tPhi)), ptnu, visfrac + return (row.e_t_Mass / sqrt(visfrac)) + +def deltaPhi(phi1, phi2): + PHI = abs(phi1-phi2) + if PHI<=pi: + return PHI + else: + return 2*pi-PHI + +def deltaR(phi1, ph2, eta1, eta2): + deta = eta1 - eta2 + dphi = abs(phi1-phi2) + if (dphi>pi) : dphi = 2*pi-dphi + return sqrt(deta*deta + dphi*dphi); + +def make_collmass_systematics(shift): + if shift.startswith('tes'): + ptnu =abs(met*cos(deltaPhi(metPhi, row.tPhi))) + visfrac = tpt/(tpt+ptnu) + vis_mass = vismass(shift) + return (vis_mass / sqrt(visfrac)) + else: + met_name = met(shift) + phi_name = metphi(shift) + def collmass_shifted(row, weight): + met = getattr(row, met_name) + phi = getattr(row, phi_name) + return collmass(row, met, phi), weight + return collmass_shifted + +class TTbarControlRegion(MegaBase): + tree = 'et/final/Ntuple' + def __init__(self, tree, outfile, **kwargs): + logging.debug('TTbar constructor') + self.channel='ET' + super(TTbarControlRegion, self).__init__(tree, outfile, **kwargs) + self.tree = ETauTree(tree) + self.out=outfile + self.histograms = {} + + #understand what we are running + target = os.path.basename(os.environ['megatarget']) + self.is_data = target.startswith('data_') + self.is_embedded = ('Embedded' in target) + self.is_mc = not (self.is_data or self.is_embedded) + self.efake = e_fake_rate(0.2) + self.efakeup = e_fake_rate_up(0.2) + self.efakedw = e_fake_rate_dw(0.2) + + #systematics used + self.systematics = { + 'trig' : (['', 'trp1s', 'trm1s'] if not self.is_data else []), + 'pu' : (['', 'p1s', 'm1s'] if self.is_mc else []), + 'eid' : (['', 'eidp1s','eidm1s'] if not self.is_data else []), + 'eiso' : (['', 'eisop1s','eisom1s'] if not self.is_data else []), + 'jes' : (['', '_jes_plus','_jes_minus'] if self.is_mc else ['']), + 'mvetos': (['', 'mVetoUp', 'mVetoDown'] if self.is_mc else ['']), + 'tvetos': (['', 'tVetoUp', 'tVetoDown'] if self.is_mc else ['']), + 'evetos': (['', 'eVetoUp', 'eVetoDown'] if self.is_mc else ['']), + 'met' : (["_mes_plus", "_ues_plus", "_mes_minus", "_ues_minus"] if self.is_mc else []), + 'tes' : (["", "_tes_plus", "_tes_minus"] if not self.is_data else ['']), + 'ees' : (["", "_ees_plus", '_ees_minus'] if not self.is_data else ['']) + } + + #self filling histograms + coll_mass = make_collmass_systematics('') #no sys shift + self.histo_locations = {} #just a mapping of the histograms we have to avoid changing self.histograms indexing an screw other files + self.hfunc = { #maps the name of non-trivial histograms to a function to get the proper value, the function MUST have two args (evt and weight). Used in fill_histos later + 'nTruePU' : lambda row, weight: (row.nTruePU,None), + 'weight' : lambda row, weight: (weight,None) if weight is not None else (1.,None), + 'Event_ID': lambda row, weight: (array.array("f", [row.run,row.lumi,int(row.evt)/10**5,int(row.evt)%10**5] ), None), + 'h_collmass_pfmet' : coll_mass, + 'h_collmass_vs_dPhi_pfmet' : merge_functions( + attr_getter('tToMETDPhi'), + coll_mass + ), + 'MetEt_vs_dPhi' : merge_functions( + lambda row, weight: (deltaPhi(row.tPhi, getattr(row, metphi())), weight), + attr_getter('type1_pfMet_Et') + ), + 'ePFMET_DeltaPhi' : lambda row, weight: (deltaPhi(row.ePhi, getattr(row, metphi())), weight), + 'tPFMET_DeltaPhi' : lambda row, weight: (deltaPhi(row.tPhi, getattr(row, metphi())), weight), + 'evtInfo' : lambda row, weight: (struct(run=row.run,lumi=row.lumi,evt=row.evt,weight=weight), None) + } + for shift in self.systematics['met']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + for shift in self.systematics['tes']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + for shift in self.systematics['jes']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + for shift in self.systematics['ees']: + #patch name + postfix = shift + self.hfunc['h_collmass_pfmet%s' % postfix] = make_collmass_systematics(shift) + + + #PU correctors + self.pucorrector = mcCorrections.make_shifted_weights( + mcCorrections.make_puCorrector('singlee'), + ['p1s', 'm1s'], + [mcCorrections.make_puCorrectorUp('singlee'), mcCorrections.make_puCorrectorDown('singlee')] + ) + self.trig_weight = mcCorrections.trig_efficiency if self.is_embedded else mcCorrections.trig_correction + + @staticmethod + def tau_veto(row): + if not row.tAntiMuonLoose2 or not row.tAntiElectronMVA5Tight or not row.tDecayFinding : + return False + + @staticmethod + def obj1_matches_gen(row): + return row.eGenPdgId == -1*row.eCharge*11 + + @staticmethod + def obj3_matches_gen(row): + return t.genDecayMode != -2 + + def event_weight(self, row, sys_shifts): + if self.is_data: + return {'' : 1.} + + weights = {} + for shift in sys_shifts: + embedded_weight = row.EmbPtWeight*mcCorrections.eEmb_correction( row, 'e', shift=shift) if self.is_embedded else 1. + + weights[shift] = embedded_weight *\ + mcCorrections.eid_correction( row, 'e', shift=shift) * \ + mcCorrections.eiso_correction(row, 'e', shift=shift) * \ + self.trig_weight(row, 'e', shift=shift) * \ + self.pucorrector(row.nTruePU, shift=shift) + + return weights +## + def begin(self): + logging.debug('Booking histograms directory tree') + sys_shifts = self.systematics['trig'] + \ + self.systematics['pu'] + \ + self.systematics['eid'] + \ + self.systematics['eiso'] + \ + self.systematics['mvetos'] + \ + self.systematics['tvetos'] + \ + self.systematics['evetos'] + \ + [i.strip('_') for i in self.systematics['jes']] + \ + [i.strip('_') for i in self.systematics['tes']] + \ + [i.strip('_') for i in self.systematics['ees']] + \ + ['tLoose', 'tLoose/Up', 'tLoose/Down', 'tLooseUnweight'] + \ + ['eLoose', 'eLoose/Up', 'eLoose/Down'] +\ + ['etLoose', 'etLoose/Up', 'etLoose/Down'] + sys_shifts = list( set( sys_shifts ) ) #remove double dirs + processtype=['gg'] + threshold=['ept30'] + signs =['os', 'ss'] + jetN = ['0', '1', '2', '3'] #[''.join(i) for i in itertools.product(['0', '1', '2', '3'], self.systematics['jes'])] + full_met_systematics = self.systematics['met']+self.systematics['jes']+ \ + self.systematics['tes']+self.systematics['ees'] + #remove empty string + full_met_systematics = [i for i in full_met_systematics if i] + folder=[] + + for tuple_path in itertools.product(sys_shifts, signs, processtype, threshold, jetN): + folder.append(os.path.join(*tuple_path)) + path = list(tuple_path) + path.append('selected') + folder.append(os.path.join(*path)) + + def book_with_sys(location, name, *args, **kwargs): + postfixes = kwargs['postfixes'] + del kwargs['postfixes'] + self.book(location, name, *args, **kwargs) + for postfix in postfixes: + #patch name to be removed + fix = postfix + self.book(location, name+fix, *args, **kwargs) + + self.book('os/gg/ept30/', "h_collmass_pfmet" , "h_collmass_pfmet", 32, 0, 320) + self.book('os/gg/ept30/', "e_t_Mass", "h_vismass", 32, 0, 320) + + for f in folder: + #self.book( + # f, + # 'evtInfo', 'evtInfo', + # 'run/l:lumi/l:evt/l:weight/D', + # type=pytree.PyTree + #) + self.book(f,"weight", "weight", 100, 0, 10) + self.book(f,"tPt", "tau p_{T}", 40, 0, 200) + self.book(f,"tPt_tes_plus", "tau p_{T} (tes+)", 40, 0, 200) + self.book(f,"tPt_tes_minus", "tau p_{T} (tes-)",40, 0, 200) + + self.book(f,"tPhi", "tau phi", 26, -3.25, 3.25) + self.book(f,"tEta", "tau eta", 10, -2.5, 2.5) + + self.book(f,"ePt", "e p_{T}", 40, 0, 200) + self.book(f,"ePt_ees_plus", "e p_{T} (ees+)", 40, 0, 200) + self.book(f,"ePt_ees_minus", "e p_{T} (ees-)",40, 0, 200) + + self.book(f,"ePhi", "e phi", 26, -3.2, 3.2) + self.book(f,"eEta", "e eta", 10, -2.5, 2.5) + + self.book(f, "e_t_DPhi", "e-tau DeltaPhi" , 20, 0, 3.2) + self.book(f, "e_t_DR", "e-tau DeltaR" , 20, 0, 3.2) + + #self.book(f, "h_collmass_pfmet", "h_collmass_pfmet", 32, 0, 320) + book_with_sys(f, "h_collmass_pfmet", "h_collmass_pfmet", 40, 0, 400, + postfixes=full_met_systematics) + + self.book(f, "h_collmass_vs_dPhi_pfmet", "h_collmass_vs_dPhi_pfmet", 20, 0, 3.2, 40, 0, 400, type=ROOT.TH2F) + + self.book(f, "e_t_Mass", "h_vismass", 40, 0, 400) + self.book(f, "e_t_Mass_tes_plus" , "h_vismass_tes_plus", 40 , 0, 400) + self.book(f, "e_t_Mass_tes_minus", "h_vismass_tes_minus",40 , 0, 400) + self.book(f, "e_t_Mass_ees_plus" , "h_vismass_ees_plus", 40 , 0, 400) + self.book(f, "e_t_Mass_ees_minus", "h_vismass_ees_minus",40 , 0, 400) + + self.book(f, "MetEt_vs_dPhi", "PFMet vs #Delta#phi(#tau,PFMet)", 20, 0, 3.2, 40, 0, 400, type=ROOT.TH2F) + + self.book(f, "tPFMET_DeltaPhi", "tau-type1PFMET DeltaPhi" , 20, 0, 3.2) + + self.book(f, "ePFMET_DeltaPhi", "e-PFMET DeltaPhi" , 20, 0, 3.2) + + #self.book(f,"tMtToPFMET", "tau-PFMET M_{T}" , 200, 0, 200) + book_with_sys(f, "tMtToPfMet", "tau-PFMET M_{T}" , 40, 0, 200, + postfixes=full_met_systematics) + #self.book(f,"eMtToPFMET", "e-PFMET M_{T}" , 200, 0, 200) + book_with_sys(f, "eMtToPfMet", "e-PFMET M_{T}" , 40, 0, 200, + postfixes=full_met_systematics) + + #self.book(f, "pfMetEt", "pfMetEt", 200, 0, 200) + book_with_sys(f, "pfMet_Et", "pfMet_Et", 40, 0, 200, postfixes=full_met_systematics) + + #self.book(f, "pfMetPhi", "pfMetPhi", 100, -3.2, 3.2) + book_with_sys(f, "pfMet_Phi", "pfMet_Phi", 26, -3.2, 3.2, postfixes=full_met_systematics) + + self.book(f, "jetVeto20", "Number of jets, p_{T}>20", 5, -0.5, 4.5) + self.book(f, "jetVeto30", "Number of jets, p_{T}>30", 5, -0.5, 4.5) + + #index dirs and histograms + for key, value in self.histograms.iteritems(): + location = os.path.dirname(key) + name = os.path.basename(key) + if location not in self.histo_locations: + self.histo_locations[location] = {name : value} + else: + self.histo_locations[location][name] = value + + def fakerate_weights(self, tEta): + tLoose = tau_fake_rate(tEta) + tLooseUp = tau_fake_rate_up(tEta) + tLooseDown= tau_fake_rate_dw(tEta) + + tLoose = tLoose / (1. - tLoose ) + tLooseUp = tLooseUp / (1. - tLooseUp ) + tLooseDown= tLooseDown / (1. - tLooseDown) + + + eLoose = self.efake + eLooseUp = self.efakeup + eLooseDown= self.efakedw + + eLoose = eLoose / (1. - eLoose ) + eLooseUp = eLooseUp / (1. - eLooseUp ) + eLooseDown= eLooseDown / (1. - eLooseDown) + + + etLoose = tLoose * eLoose + etLooseUp = tLooseUp * eLooseUp + etLooseDown= tLooseDown *eLooseDown + + frweight = { + 'tLoose' : tLoose , + 'tLoose/Up' : tLooseUp , + 'tLoose/Down' : tLooseDown, + 'eLoose' : eLoose , + 'eLoose/Up' : eLooseUp , + 'eLoose/Down' : eLooseDown, + 'etLoose' : etLoose , + 'etLoose/Up' : etLooseUp , + 'etLoose/Down' : etLooseDown, + 'tLooseUnweight' : 1., + } + + return frweight; + + def fill_histos(self, folder_str, row, weight, filter_label = ''): + '''fills histograms''' + #find all keys matching + for attr, value in self.histo_locations[folder_str].iteritems(): + name = attr + #if attr=='DEBUG': + # set_trace() + if filter_label: + if not attr.startswith(filter_label+'$'): + continue + attr = attr.replace(filter_label+'$', '') + if value.InheritsFrom('TH2'): + if attr in self.hfunc: + try: + result, out_weight = self.hfunc[attr](row, weight) + except Exception as e: + raise RuntimeError("Error running function %s. Error: \n\n %s" % (attr, str(e))) + r1, r2 = result + if out_weight is None: + value.Fill( r1, r2 ) #saves you when filling NTuples! + else: + value.Fill( r1, r2, out_weight ) + else: + attr1, attr2 = split(attr) + v1 = getattr(row,attr1) + v2 = getattr(row,attr2) + value.Fill( v1, v2, weight ) if weight is not None else value.Fill( v1, v2 ) + else: + if attr in self.hfunc: + try: + result, out_weight = self.hfunc[attr](row, weight) + except Exception as e: + raise RuntimeError("Error running function %s. Error: \n\n %s" % (attr, str(e))) + if out_weight is None: + value.Fill( result ) #saves you when filling NTuples! + else: + value.Fill( result, out_weight ) + else: + value.Fill( getattr(row,attr), weight ) if weight is not None else value.Fill( getattr(row,attr) ) + return None + + def process(self): + logging.debug('Starting processing') + systematics = self.systematics + + frw = [] + lock =() + ievt = 0 + logging.debug('Starting evt loop') + + #pre-compute static things + sys_shifts = systematics['trig'] + \ + systematics['pu'] + \ + systematics['eid'] + \ + systematics['eiso'] #+ \ + + jes_dirs = [i.strip('_') for i in systematics['jes']] + #anyway the first is always "" + tes_dirs = [i.strip('_') for i in systematics['tes']][1:] + ees_dirs = [i.strip('_') for i in systematics['ees']][1:] + + for row in self.tree: + if (ievt % 100) == 0: + logging.debug('New event') + ievt += 1 + #avoid double counting events! + evt_id = (row.run, row.lumi, row.evt) + if evt_id == lock: continue + if lock != () and evt_id == lock: + logging.info('Removing duplicate of event: %d %d %d' % evt_id) + + # + #preselection, common to everything and everyone + # + #trigger + if self.is_embedded : + if not bool(row.doubleMuPass) : continue + else: + if not bool(row.singleE27WP80Pass) : continue + if not bool(row.eMatchesSingleE27WP80): continue + + #objects + if not selections.eSelection(row, 'e'): continue + #if row.ePt < 30 : continue + if not selections.tauSelection(row, 't'): continue + if not row.tAntiElectronMVA5Tight : continue + if not row.tAntiMuon2Loose : continue + if not row.tLooseIso3Hits : continue + logging.debug('object selection passed') + #e ID/ISO + if not selections.lepton_id_iso(row, 'e', 'eid13Tight_idiso02'): continue + logging.debug('Passed preselection') + + # + # Compute event weight + # + #event weight + #sys_shifts is precomputed + + #set_trace() + weight_map = self.event_weight(row, sys_shifts) + + #Fill embedded sample normalization BEFORE the vetoes + if not row.e_t_SS: + self.fill_histos('os/gg/ept30', row, weight_map['']) + + # it is better vetoing on b-jets after the histo for the DY embedded + #bjet veto + if row.bjetCSVVeto30==0 : continue + + #tau ID, id Tau is tight then go in full selection, otherwise use for fakes + isTauTight = bool(row.tTightIso3Hits) + isETight = bool(selections.lepton_id_iso(row, 'e', 'eid13Tight_etauiso01')) + etau_category = [''] + if (not isETight) and (not isTauTight): + etau_category = ['etLoose', 'etLoose/Up', 'etLoose/Down'] + elif (not isTauTight): + etau_category = ['tLoose', 'tLoose/Up', 'tLoose/Down', 'tLooseUnweight'] + elif (not isETight): + etau_category = ['eLoose', 'eLoose/Up', 'eLoose/Down'] + + #jet category + central = struct( + njets = 2, + #njets = min(row.jetVeto30, 3), + tPt = row.tPt, + ePt = row.ePt + ) + #jets = [min(row.jetVeto30, 3), min(row.jetVeto30jes_plus, 3), min(row.jetVeto30jes_minus, 3)] + jets = [2,2,2] + tpts = [row.tPt_tes_plus, row.tPt_tes_minus] + epts = [row.ePt_ees_plus, row.ePt_ees_minus] + sys_effects = [(name, central.clone(njets = jnum)) for name, jnum in zip(jes_dirs, jets)] + sys_effects.extend( + [(name, central.clone(tPt = pt)) for name, pt in zip(tes_dirs, tpts)] + ) + sys_effects.extend( + [(name, central.clone(ePt = pt)) for name, pt in zip(ees_dirs, epts)] + ) + + # + # Full tight selection + # + passes_full_selection = False + selection_categories = [] + for name, shifted in sys_effects: + #preselection. flat pt values + if shifted.ePt > 30 and shifted.tPt > 30: + selection_categories.append((name, '%i' % shifted.njets, '')) + else: + continue + + ##if shifted.njets == 0 : + ## if shifted.tPt < 35: continue + ## if shifted.ePt < 40: continue + ## if deltaPhi(row.ePhi, row.tPhi) < 2.7 : continue + ## if row.tMtToPfMet > 50 : continue + ## selection_categories.append((name, '0', 'selected')) + ## passes_full_selection = True + ##elif shifted.njets == 1 : + ## if shifted.tPt < 40: continue + ## if shifted.ePt < 35: continue + ## if row.tMtToPfMet > 35 : continue + ## selection_categories.append((name, '1', 'selected')) + ## passes_full_selection = True + ##el + if shifted.njets == 2 : + if shifted.tPt < 40: continue + if shifted.ePt < 30: continue + if row.tMtToPfMet > 35 : continue + if row.vbfMass < 550 : continue + if row.vbfDeta < 3.5 : continue + selection_categories.append((name, '2', 'selected')) + passes_full_selection = True + + if passes_full_selection: + logging.debug('Passed full selection') + + # + #different selections + # + sign = 'ss' if row.e_t_SS else 'os' + processtype ='gg' + ptthreshold = ['ept30'] + + # + # Lepton vetoes + # + tvetoes = [row.tauVetoPt20EleTight3MuLoose, row.tauVetoPt20EleTight3MuLoose_tes_plus, row.tauVetoPt20EleTight3MuLoose_tes_minus] + mvetoes = [row.muVetoPt5IsoIdVtx , row.muVetoPt5IsoIdVtx_mes_plus , row.muVetoPt5IsoIdVtx_mes_minus ] + evetoes = [row.eVetoCicLooseIso , row.eVetoCicLooseIso_ees_plus , row.eVetoCicLooseIso_ees_minus ] + + tdirs = [ i for i, j in zip( systematics['tvetos'], tvetoes) if not j] + mdirs = [ i for i, j in zip( systematics['mvetos'], mvetoes) if not j] + edirs = [ i for i, j in zip( systematics['evetos'], evetoes) if not j] + + #if any of the lists is empty + #set_trace() + if not tdirs or not mdirs or not edirs: + continue + logging.debug('Passed Vetoes') + + #make all possible veto combos... + all_dirs = [''.join(i) for i in itertools.product(tdirs, mdirs, edirs)] + #...and choose only the meaningful ones + veto_sys = set(systematics['tvetos']+systematics['mvetos']+systematics['evetos']) + all_dirs = [i for i in all_dirs if i in veto_sys] + + sys_directories = all_dirs + sys_shifts + #remove duplicates + sys_directories = list(set(sys_directories)) + + #at least one loose object + if (not isETight) or (not isTauTight): + #if is a loose tau just compute the fakes! + sys_directories = etau_category + + #gather the one and only weight we do care about + mc_weight = weight_map[''] + + #weights are the fr ones... + weight_map = self.fakerate_weights(row.tEta) + for i in weight_map: + #...times the mc weight (if any) + weight_map[i] *= mc_weight + + + #Fill histograms in appropriate direcotries + #if passes_full_selection: + #dirs = [os.path.join(sys, sign, processtype, e_thr, jet_dir) for sys, e_thr, jet_dir in itertools.product(sys_directories, ptthreshold, jet_directories)] + #if len(dirs) <> len(set(dirs)): + # set_trace() + for sys, e_thr, selection in itertools.product(sys_directories, ptthreshold, selection_categories): + selection_sys, jet_dir, selection_step = selection + #if we have multiple systematic shifts applied + #reject the combination + if selection_sys and sys: + continue + + #if we fill a histogram, lock the event + lock = evt_id + dir_name = os.path.join(sys, selection_sys, sign, processtype, + e_thr, jet_dir, selection_step) + if dir_name[-1] == '/': + dir_name = dir_name[:-1] + if passes_full_selection: + logging.debug('Filling %s' % dir_name) + #fill them! + weight_to_use = weight_map[sys] if sys in weight_map else weight_map[''] + self.fill_histos(dir_name, row, weight_to_use) + + + def finish(self): + self.write_histos() + + diff --git a/lfvetau/TauFakeRateAnalyzerMVA.py b/lfvetau/TauFakeRateAnalyzerMVA.py new file mode 100644 index 00000000..ac7cc75b --- /dev/null +++ b/lfvetau/TauFakeRateAnalyzerMVA.py @@ -0,0 +1,332 @@ +##Correction Factor still to add +from EETauTree import EETauTree +import os +import ROOT +import math +import optimizer +import glob +import array +import mcCorrections +import baseSelections as selections +import FinalStateAnalysis.PlotTools.pytree as pytree +from FinalStateAnalysis.PlotTools.decorators import memo_last +from FinalStateAnalysis.PlotTools.MegaBase import MegaBase +from math import sqrt, pi, sin, cos, acos, sinh +from cutflowtracker import cut_flow_tracker +#Makes the cut flow histogram +cut_flow_step = ['allEvents', 'e1sel', 'e1IDiso', 'e2sel', 'e2IDiso', 'ZMass', 'tsel', 'tAntiMuon', 'tAntiEle', 'MtToMet', 'tRawIso10','tRawIso5', 'tLooseIso', 'tTightIso' +] + +from inspect import currentframe + +def get_linenumber(): + cf = currentframe() + return cf.f_back.f_lineno + +def deltaPhi(phi1, phi2): + PHI = abs(phi1-phi2) + if PHI<=pi: + return PHI + else: + return 2*pi-PHI +def deltaR(phi1, phi2, eta1, eta2): + deta = eta1 - eta2 + dphi = abs(phi1-phi2) + if (dphi>pi) : dphi = 2*pi-dphi + return sqrt(deta*deta + dphi*dphi); + +def etDR(row): + return row.e1_t_DR if row.e1_t_DR < row.e2_t_DR else row.e2_t_DR + +def etDPhi(row): + e1tDPhi=deltaPhi(row.e1Phi, row.tPhi) + e2tDPhi=deltaPhi(row.e2Phi, row.tPhi) + return e1tDPhi if e1tDPhi < e2tDPhi else e2tDPhi + +def Z(row): + e1p=ROOT.TVector3(row.e1Pt*cos(row.e1Phi),row.e1Pt*sin(row.e1Phi),row.e1Pt*sinh(row.e1Eta)) + e2p=ROOT.TVector3(row.e2Pt*cos(row.e2Phi),row.e2Pt*sin(row.e2Phi),row.e2Pt*sinh(row.e2Eta)) + e1FourVector= ROOT.TLorentzVector(e1p, sqrt(e1p.Mag2()+row.e1Mass*row.e1Mass)) + e2FourVector= ROOT.TLorentzVector(e2p, sqrt(e2p.Mag2()+row.e2Mass*row.e2Mass)) + zFourVector = e1FourVector+e2FourVector + return zFourVector + + +class TauFakeRateAnalyzerMVA(MegaBase): + tree = 'eet/final/Ntuple' + def __init__(self, tree, outfile, **kwargs): + self.channel='EET' + super(TauFakeRateAnalyzerMVA, self).__init__(tree, outfile, **kwargs) + self.tree = EETauTree(tree) + self.out=outfile + self.histograms = {} + self.pucorrector = mcCorrections.make_puCorrector('singlee') + + optimizer_keys = [ i for i in optimizer.grid_search.keys() if i.startswith(self.channel) ] + self.grid_search = {} + if len(optimizer_keys) > 1: + for key in optimizer_keys: + self.grid_search[key] = optimizer.grid_search[key] + else: + self.grid_search[''] = optimizer.grid_search[optimizer_keys[0]] + + + def event_weight(self, row): + if row.run > 2: #FIXME! add tight ID correction + return 1. + + etrig = 'e1' + if row.e2Pt > row.e1Pt : etrig = 'e2' + if bool(row.e1MatchesSingleE27WP80) and not bool(row.e2MatchesSingleE27WP80) : etrig = 'e1' + if not bool(row.e1MatchesSingleE27WP80) and bool(row.e2MatchesSingleE27WP80) : etrig = 'e2' + + #if bool(row.e1MatchesEle27WP80) and not bool(row.e2MatchesEle27WP80) : etrig = 'e1' + #if not bool(row.e1MatchesEle27WP80) and bool(row.e2MatchesEle27WP80) : etrig = 'e2' + + + + return self.pucorrector(row.nTruePU) * \ + mcCorrections.eid_correction( row, 'e1', 'e2') * \ + mcCorrections.eiso_correction(row, 'e1', 'e2') * \ + mcCorrections.trig_correction(row, etrig ) + + def begin(self): + + tauiso = ['tNoCuts', 'tSuperSuperLoose', 'tSuperLoose', 'tLoose', 'tTigh'] + folder = [] + sign = ['ss','os'] + for iso in tauiso: + for s in sign: + folder.append(s+'/'+iso) + folder.append(s+'/'+iso+'/tptregion') + j=0 + while j < 4 : + folder.append(s+'/'+iso+'/'+str(j)) + folder.append(s+'/'+iso+'/'+str(j)+'/tptregion') + j+=1 + + for f in folder: + + self.book(f,"e1Pt", "e1 p_{T}", 200, 0, 200) + self.book(f,"e1Phi", "e1 phi", 100, -3.2, 3.2) + self.book(f,"e1Eta", "e1 eta", 50, -2.5, 2.5) + + self.book(f,"e2Pt", "e2 p_{T}", 200, 0, 200) + self.book(f,"e2Phi", "e2 phi", 100, -3.2, 3.2) + self.book(f,"e2Eta", "e2 eta", 50, -2.5, 2.5) + + self.book(f, "e1e2Mass", "e1e2 Inv Mass", 32, 0, 320) + + self.book(f, "tMtToPfMet", "#tau Met MT", 100, 0, 100) + + self.book(f,"tRawIso3Hits", "tRawIso3Hits", 500, 0, 100) + self.book(f,"tPt", "t p_{T}", 200, 0, 200) + self.book(f,"tPtbarrel", "t p_{T} barrel", 200, 0, 200) + self.book(f,"tPtendcap", "t p_{T} endcap", 200, 0, 200) + self.book(f,"tPhi", "t phi", 100, -3.2, 3.2) + self.book(f,"tEta", "t eta", 50, -2.5, 2.5) + self.book(f,"tAbsEta", "t abs eta", 50, -2.5, 2.5) + + self.book(f,"etDR", "e t DR", 50, 0, 10) + self.book(f,"etDPhi", "e t DPhi", 32, 0, 3.2) + + self.book(f,"ztDR", "Z #tau DR", 50, 0, 10) + self.book(f,"ztDPhi", "Z #tau DPhi", 32, 0, 3.2) + self.book(f,"Zpt", "Z p_{T}", 200, 0, 200) + + + + self.book(f, "type1_pfMetEt", "type1_pfMetEt",200, 0, 200) + self.book(f, "jetN_30", "Number of jets, p_{T}>30", 10, -0.5, 9.5) + self.book(f, "bjetCSVVeto30", "number of bjets", 10, -0.5, 9.5) + + for s in sign: + self.book(s+'/tNoCuts', "CUT_FLOW", "Cut Flow", len(cut_flow_step), 0, len(cut_flow_step)) + + xaxis = self.histograms[s+'/tNoCuts/CUT_FLOW'].GetXaxis() + self.cut_flow_histo = self.histograms[s+'/tNoCuts/CUT_FLOW'] + self.cut_flow_map = {} + for i, name in enumerate(cut_flow_step): + xaxis.SetBinLabel(i+1, name) + self.cut_flow_map[name] = i+0.5 + + def fill_histos(self, row, folder='os/tSuperLoose', fakeRate = False): + weight = self.event_weight(row) + histos = self.histograms + + histos[folder+'/e1Pt'].Fill(row.e1Pt, weight) + histos[folder+'/e1Eta'].Fill(row.e1Eta, weight) + histos[folder+'/e1Phi'].Fill(row.e1Phi, weight) + + histos[folder+'/e2Pt'].Fill(row.e2Pt, weight) + histos[folder+'/e2Eta'].Fill(row.e2Eta, weight) + histos[folder+'/e2Phi'].Fill(row.e2Phi, weight) + + histos[folder+'/e1e2Mass'].Fill(row.e1_e2_Mass, weight) + histos[folder+'/tMtToPfMet'].Fill(row.tMtToPfMet,weight) + + #histos[folder+'/tRawIso3Hits'].Fill(row.tRawIso3Hits, weight) + histos[folder+'/tPt'].Fill(row.tPt, weight) + if abs(row.tEta) < 1.5 : histos[folder+'/tPtbarrel'].Fill(row.tPt, weight) + if abs(row.tEta) > 1.5 : histos[folder+'/tPtendcap'].Fill(row.tPt, weight) + histos[folder+'/tEta'].Fill(row.tEta, weight) + histos[folder+'/tAbsEta'].Fill(abs(row.tEta), weight) + histos[folder+'/tPhi'].Fill(row.tPhi, weight) + + histos[folder+'/type1_pfMetEt'].Fill(row.type1_pfMet_Et) + histos[folder+'/etDR'].Fill(etDR(row)) + histos[folder+'/etDPhi'].Fill(etDPhi(row)) + histos[folder+'/jetN_30'].Fill(row.jetVeto30, weight) + histos[folder+'/bjetCSVVeto30'].Fill(row.bjetCSVVeto30, weight) + + histos[folder+'/ztDR'].Fill(deltaR(Z(row).Phi(), row.tPhi, Z(row).Eta(), row.tEta)) + histos[folder+'/ztDPhi'].Fill(deltaPhi(Z(row).Phi(), row.tPhi)) + histos[folder+'/Zpt'].Fill(Z(row).Pt()) + + + def process(self): + + cut_flow_histo = self.cut_flow_histo + cut_flow_trk = cut_flow_tracker(cut_flow_histo) + myevent =() + #print self.tree.inputfilename + for row in self.tree: + jn = row.jetVeto30 + if jn > 3 : jn = 3 + + #if row.run > 2: + if not bool(row.singleE27WP80Pass) : continue + # if hasattr(self.tree, 'row.e1MatchesEle27WP80') and hasattr(self.tree, 'row.e2MatchesEle27WP80') : + #if not bool(row.e1MatchesEle27WP80) and not bool(row.e2MatchesEle27WP80) : continue + + #else : + if not bool(row.e1MatchesSingleE27WP80) and not bool(row.e2MatchesSingleE27WP80) : continue + #if not bool(row.singleEPass) : continue + #if not bool(row.e1MatchesSingleE) and not bool(row.e2MatchesSingleE) : continue + + if row.bjetCSVVeto30!=0 : continue + if row.e1Pt < 30 : continue + if row.e2Pt < 30 : continue + +# for i, row in enumerate(self.tree): +# if i >= 100: +# return + # print bool(cut_flow_trk.disabled) + cut_flow_trk.new_row(row.run,row.lumi,row.evt) + #print row.run,row.lumi,row.evt + cut_flow_trk.Fill('allEvents') + if not selections.eSelection(row, 'e1'): continue + cut_flow_trk.Fill('e1sel') + if not selections.lepton_id_iso(row, 'e1', 'eid13Tight_etauiso01'): continue + if abs(row.e1Eta) > 1.4442 and abs(row.e1Eta < 1.566) : continue + + + cut_flow_trk.Fill('e1IDiso') + if not selections.eSelection(row, 'e2'): continue + cut_flow_trk.Fill('e2sel') + if not selections.lepton_id_iso(row, 'e2', 'eid13Tight_etauiso01'): continue + if abs(row.e2Eta) > 1.4442 and abs(row.e2Eta) < 1.566 : continue + + cut_flow_trk.Fill('e2IDiso') + if not abs(row.e1_e2_Mass-91.2) < 20: continue + cut_flow_trk.Fill('ZMass') + if not selections.tauSelection(row, 't'): continue + if row.tPt < 30 : continue + cut_flow_trk.Fill('tsel') + + if not row.tAntiMuon2Loose: continue + cut_flow_trk.Fill('tAntiMuon') + if not row.tAntiElectronMVA5Tight: continue #was 3 + cut_flow_trk.Fill('tAntiEle') + + if row.tauVetoPt20EleTight3MuLoose : continue + #if row.tauHpsVetoPt20 : continue + if row.muVetoPt5IsoIdVtx : continue + if row.eVetoCicLooseIso : continue # change it with Loose + + + # if not row.tMtToMET < 50: continue + cut_flow_trk.Fill('MtToMet') + + # if etDR(row) < 1. : continue + if (row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt)==myevent: continue + myevent=(row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt) + + tauiso = 'tNoCuts' + sign = 'ss' if row.e1_e2_SS else 'os' + folder = sign+'/'+tauiso + + self.fill_histos(row, folder) + folder=folder+'/'+str(int(jn)) + self.fill_histos(row, folder) + + + if row.tPt < 90 and row.tPt>65 : + folder = folder+'/tptregion' + self.fill_histos(row, folder) + folder = sign+'/'+tauiso+'/tptregion' + self.fill_histos(row, folder) + + if not row.tRawIso3Hits < 10 : continue + cut_flow_trk.Fill('tRawIso10') + tauiso = 'tSuperSuperLoose' + folder = sign+'/'+tauiso + self.fill_histos(row, folder) + folder=folder+'/'+str(int(jn)) + self.fill_histos(row, folder) + if row.tPt < 90 and row.tPt>65 : + folder = folder+'/tptregion' + self.fill_histos(row, folder) + folder = sign+'/'+tauiso+'/tptregion' + self.fill_histos(row, folder) + + if not row.tRawIso3Hits < 5 : continue + cut_flow_trk.Fill('tRawIso5') + tauiso = 'tSuperLoose' + folder = sign+'/'+tauiso + self.fill_histos(row, folder) + folder=folder+'/'+str(int(jn)) + self.fill_histos(row, folder) + if row.tPt < 90 and row.tPt>65 : + folder = folder+'/tptregion' + self.fill_histos(row, folder) + folder = sign+'/'+tauiso+'/tptregion' + self.fill_histos(row, folder) + + + if row.tLooseIso3Hits : + tauiso = 'tLoose' + folder = sign+'/'+tauiso + self.fill_histos(row, folder) + cut_flow_trk.Fill('tLooseIso') + folder=folder+'/'+str(int(jn)) + self.fill_histos(row, folder) + if row.tPt < 90 and row.tPt>65 : + folder = folder+'/tptregion' + self.fill_histos(row, folder) + folder = sign+'/'+tauiso+'/tptregion' + self.fill_histos(row, folder) + + if row.tTightIso3Hits : + tauiso = 'tTigh' + folder = sign+'/'+tauiso + self.fill_histos(row, folder) + cut_flow_trk.Fill('tTightIso') + folder=folder+'/'+str(int(jn)) + self.fill_histos(row, folder) + if row.tPt < 90 and row.tPt>65 : + folder = folder+'/tptregion' + self.fill_histos(row, folder) + folder = sign+'/'+tauiso+'/tptregion' + self.fill_histos(row, folder) + + + + cut_flow_trk.flush() + + + + def finish(self): + self.write_histos() + + diff --git a/lfvetau/baseSelections.py b/lfvetau/baseSelections.py new file mode 100755 index 00000000..dc77d8e4 --- /dev/null +++ b/lfvetau/baseSelections.py @@ -0,0 +1,146 @@ +from FinalStateAnalysis.PlotTools.decorators import memo +from FinalStateAnalysis.Utilities.struct import struct +from electronids import electronIds + +@memo +def getVar(name, var): + return name+var + +@memo +def splitEid(label): + return label.split('_')[-1], label.split('_')[0] + +#OBJECT SELECTION +def muSelection(row, name): + if getattr( row, getVar(name,'Pt')) < 30: return False + if getattr( row, getVar(name,'AbsEta')) > 2.1: return False + if not getattr( row, getVar(name,'PixHits')): return False + if getattr( row, getVar(name,'JetCSVBtag')) > 0.8: return False + #if getattr( row, getVar(name,'JetBtag')) > 3.3: return False #was 3.3 + if abs(getattr( row, getVar(name,'DZ'))) > 0.2: return False + return True + +def eSelection(row, name): + eAbsEta = getattr( row, getVar(name,'AbsEta')) + ept = getattr( row, getVar(name,'Pt_ees_minus')) + if ept: + if ept < 30: return False + else: + if getattr( row, getVar(name,'Pt')) < 30: return False #was 20 + + if eAbsEta > 2.3: return False + if getattr( row, getVar(name,'MissingHits')): return False + if getattr( row, getVar(name,'HasConversion')): return False + if eAbsEta > 1.4442 and eAbsEta < 1.566: return False +# if not getattr( row, getVar(name,'ChargeIdTight')): return False + if not getattr( row, getVar(name,'ChargeIdLoose')): return False + if getattr( row, getVar(name,'JetCSVBtag')) > 0.8: return False + ###if getattr( row, getVar(name,'JetBtag')) > 3.3: return False + if abs(getattr( row, getVar(name,'DZ'))) > 0.2: return False + #if getattr(row, getVar(name, 'MuonIdIsoVtxOverlap')): return False + return True +def eLowPtSelection(row, name): + eAbsEta = getattr( row, getVar(name,'AbsEta')) + ept = getattr( row, getVar(name,'Pt_ees_minus')) + if ept: + if ept < 15: return False + else: + if getattr( row, getVar(name,'Pt')) < 15: return False #was 20 + + if eAbsEta > 2.3: return False + if getattr( row, getVar(name,'MissingHits')): return False + if getattr( row, getVar(name,'HasConversion')): return False + if eAbsEta > 1.4442 and eAbsEta < 1.566: return False +# if not getattr( row, getVar(name,'ChargeIdTight')): return False + if not getattr( row, getVar(name,'ChargeIdLoose')): return False + if getattr( row, getVar(name,'JetCSVBtag')) > 0.8: return False + ###if getattr( row, getVar(name,'JetBtag')) > 3.3: return False + if abs(getattr( row, getVar(name,'DZ'))) > 0.2: return False + #if getattr(row, getVar(name, 'MuonIdIsoVtxOverlap')): return False + return True + +def tauSelection(row, name): + tpt = getattr( row, getVar(name,'Pt_tes_minus')) + if tpt: + if tpt < 30: return False + else: + if getattr( row, getVar(name,'Pt')) < 30: return False + if getattr( row, getVar(name,'AbsEta')) > 2.3: return False + if abs(getattr( row, getVar(name,'DZ'))) > 0.2: return False + if getattr( row, getVar(name, 'MuonIdIsoVtxOverlap')): return False + if getattr( row, getVar(name, 'ElectronPt10IdIsoVtxOverlap')): return False # change to tCiCLooseElecOverlap + + return True + + +#VETOS +def vetos(row): + if row.muVetoPt5IsoIdVtx: return False + if row.eVetoMVAIsoVtx: return False + if row.eVetoCicTightIso: return False # change it to loose + if row.tauVetoPt20: return False + + return True + +def lepton_id_iso(row, name, label): #label in the format eidtype_isotype + 'One function to rule them all' + LEPTON_ID = False + isolabel, eidlabel = splitEid(label) #memoizes to be faster! + if name[0] == 'e': + LEPTON_ID = electronIds[eidlabel](row, name) + else: + LEPTON_ID = getattr(row, getVar(name, 'PFIDTight')) + if not LEPTON_ID: + return False + RelPFIsoDB = getattr(row, getVar(name, 'RelPFIsoDB')) + AbsEta = getattr(row, getVar(name, 'AbsEta')) + if isolabel == 'h2taucuts': + return bool( RelPFIsoDB < 0.1 or (RelPFIsoDB < 0.15 and AbsEta < 1.479)) + if isolabel == 'h2taucuts020': + return bool( RelPFIsoDB < 0.15 or (RelPFIsoDB < 0.20 and AbsEta < 1.479)) + if isolabel == 'idiso02': + return bool( RelPFIsoDB < 0.20 ) + if isolabel == 'idiso05': + return bool( RelPFIsoDB < 0.5 ) + if isolabel == 'idantiso': + return bool( RelPFIsoDB > 0.20 ) + if isolabel == 'etauiso012' or isolabel == 'mutauiso012': + return bool( RelPFIsoDB < 0.12 ) + if isolabel == 'etauiso01' or isolabel == 'mutauiso01': + return bool( RelPFIsoDB < 0.1 ) + ##put the new iso + if isolabel == 'mvaLoose' : + if AbsEta < 0.8: + return bool( RelPFIsoDB < 0.426 ) + if AbsEta > 0.8 and AbsEta < 1.479 : + return bool(RelPFIsoDB < 0.481) + if AbsEta > 1.479 and AbsEta < 2.5: + return bool(RelPFIsoDB < 0.390) + return False + if isolabel == 'mvaTight': + if AbsEta < 0.8: + return bool( RelPFIsoDB < 0.105 ) + if AbsEta > 0.8 and AbsEta < 1.479 : + return bool(RelPFIsoDB < 0.178) + if AbsEta > 1.479 and AbsEta < 2.5: + return bool(RelPFIsoDB < 0.150) + return False + + + +def control_region_ee(row): + '''Figure out what control region we are in. Shared among two codes, to avoid mismatching copied here''' + if row.e1_e2_SS and lepton_id_iso(row, 'e1', 'eid12Medium_h2taucuts') and row.e1MtToMET > 30: + return 'wjets' + elif row.e1_e2_SS and row.e1RelPFIsoDB > 0.3 and row.type1_pfMetEt < 25: #and row.metSignificance < 3: # + return 'qcd' + elif lepton_id_iso(row,'e1', 'eid12Medium_h2taucuts') and lepton_id_iso(row,'e2', 'eid12Medium_h2taucuts') \ + and not any([ row.muVetoPt5IsoIdVtx, + row.tauVetoPt20Loose3HitsVtx, + row.eVetoMVAIsoVtx, + ]): + return 'zee' + else: + return None + + diff --git a/lfvetau/card_config/cgs.0.conf b/lfvetau/card_config/cgs.0.conf new file mode 100644 index 00000000..299f6d87 --- /dev/null +++ b/lfvetau/card_config/cgs.0.conf @@ -0,0 +1,20 @@ +# cgs.config: Specification of groups, categories, and samples + +#$ GROUP signal Higgs_vtth_sm_120,Higgs_gf_sm_120,Higgs_vbf_sm_120 +#$ GROUP background Ztt,ttbar,EWK,Fakes +#categories: emu_vbf +#signals: signal +#backgrounds: background +#data: data_obs + +$ GROUP signal LFVGG,LFVVBF +$ GROUP gghiggs LFVGG,SMGG126 +$ GROUP vbfhiggs LFVVBF,SMVBF126 +$ GROUP zjets ztautau,zjetsother +$ GROUP background diboson,ttbar,singlet,ztautau,zjetsother,fakes,SMVBF126,SMGG126 +$ GROUP simulated diboson,ttbar,singlet,ztautau,zjetsother,LFVVBF,SMVBF126,WWVBF126,LFVGG,SMGG126,WWGG126 + +categories: gg0etau +signals: signal +backgrounds: background +data: data_obs diff --git a/lfvetau/card_config/cgs.1.conf b/lfvetau/card_config/cgs.1.conf new file mode 100644 index 00000000..e9a51211 --- /dev/null +++ b/lfvetau/card_config/cgs.1.conf @@ -0,0 +1,20 @@ +# cgs.config: Specification of groups, categories, and samples + +#$ GROUP signal Higgs_vtth_sm_120,Higgs_gf_sm_120,Higgs_vbf_sm_120 +#$ GROUP background Ztt,ttbar,EWK,Fakes +#categories: emu_vbf +#signals: signal +#backgrounds: background +#data: data_obs + +$ GROUP signal LFVGG,LFVVBF +$ GROUP gghiggs LFVGG,SMGG126 +$ GROUP vbfhiggs LFVVBF,SMVBF126 +$ GROUP zjets ztautau,zjetsother +$ GROUP background diboson,ttbar,singlet,ztautau,zjetsother,fakes,SMVBF126,SMGG126 +$ GROUP simulated diboson,ttbar,singlet,ztautau,zjetsother,LFVVBF,SMVBF126,LFVGG,SMGG126 + +categories: boostetau +signals: signal +backgrounds: background +data: data_obs diff --git a/lfvetau/card_config/cgs.2.conf b/lfvetau/card_config/cgs.2.conf new file mode 100644 index 00000000..2bd819a3 --- /dev/null +++ b/lfvetau/card_config/cgs.2.conf @@ -0,0 +1,20 @@ +# cgs.config: Specification of groups, categories, and samples + +#$ GROUP signal Higgs_vtth_sm_120,Higgs_gf_sm_120,Higgs_vbf_sm_120 +#$ GROUP background Ztt,ttbar,EWK,Fakes +#categories: emu_vbf +#signals: signal +#backgrounds: background +#data: data_obs + +$ GROUP signal LFVGG,LFVVBF +$ GROUP gghiggs LFVGG,SMGG126 +$ GROUP vbfhiggs LFVVBF,SMVBF126 +$ GROUP zjets ztautau,zjetsother +$ GROUP background diboson,ttbar,singlet,ztautau,zjetsother,fakes,SMVBF126,SMGG126 +$ GROUP simulated diboson,ttbar,singlet,ztautau,zjetsother,LFVVBF,SMVBF126,LFVGG,SMGG126 + +categories: vbfetau +signals: signal +backgrounds: background +data: data_obs diff --git a/lfvetau/card_config/cgs.conf b/lfvetau/card_config/cgs.conf new file mode 100644 index 00000000..a3bba4a2 --- /dev/null +++ b/lfvetau/card_config/cgs.conf @@ -0,0 +1,20 @@ +# cgs.config: Specification of groups, categories, and samples + +#$ GROUP signal Higgs_vtth_sm_120,Higgs_gf_sm_120,Higgs_vbf_sm_120 +#$ GROUP background Ztt,ttbar,EWK,Fakes +#categories: emu_vbf +#signals: signal +#backgrounds: background +#data: data_obs + +$ GROUP signal LFVGG,LFVVBF +$ GROUP gghiggs LFVGG,SMGG126,WWGG126 +$ GROUP vbfhiggs LFVVBF,SMVBF126,WWVBF126 +$ GROUP zjets ztautau,zjetsother +$ GROUP background ww,ttbar,singlet,ztautau,zjetsother,fakes +$ GROUP simulated ww,ttbar,singlet,ztautau,zjetsother,LFVVBF,SMVBF126,WWVBF126,LFVGG,SMGG126,WWGG126 + +categories: gg0etau,boostetau,vbfetau +signals: signal +backgrounds: background +data: data_obs diff --git a/lfvetau/card_config/unc.0.conf b/lfvetau/card_config/unc.0.conf new file mode 100755 index 00000000..690f525e --- /dev/null +++ b/lfvetau/card_config/unc.0.conf @@ -0,0 +1,26 @@ +lumi lnN +#Effi_E lnN +Effi_et_Tau lnN +Effi_et_Tau_highPt lnN + +Theo_Scale_gg lnN +Theo_PDF_gg lnN + +Theo_UE lnN +Theo_Scale_vbf lnN +Theo_PDF_vbf lnN + +Norm_ZTauTau lnN +Norm_ZTauTau_et_0Jet_extra lnN +Norm_ZJetsOther lnN + +Norm_DIBOSON lnN +Norm_TT lnN +Norm_FAKES lnN +Norm_FAKES_mt_0Jet lnN +Norm_SMGG lnN +Norm_SMVBF lnN +Norm_TOP lnN + +#shape_MuTau_TES shape + diff --git a/lfvetau/card_config/unc.0.vals b/lfvetau/card_config/unc.0.vals new file mode 100755 index 00000000..a27143d8 --- /dev/null +++ b/lfvetau/card_config/unc.0.vals @@ -0,0 +1,24 @@ +gg0etau gghiggs Theo_PDF_gg 1.097 +gg0etau vbfhiggs Theo_PDF_vbf 1.036 + +gg0etau gghiggs Theo_UE 1.04 +gg0etau vbfhiggs Theo_UE 1.1 + +gg0etau gghiggs Theo_Scale_gg 1.08 +gg0etau vbfhiggs Theo_Scale_vbf 1.04 + +gg0etau ztautau Norm_ZTauTau 1.03 +gg0etau ztautau Norm_ZTauTau_et_0Jet_extra 1.05 +gg0etau zjetsother Norm_ZJetsOther 1.3 + +#TODO: check eff electron +gg0etau simulated lumi 1.026 +gg0etau simulated Effi_et_Tau 1.06 +gg0etau simulated Effi_et_Tau_highPt 1.03 + +gg0etau diboson Norm_DIBOSON 1.15 +gg0etau singlet Norm_TOP 1.10 +gg0etau ttbar Norm_TT 1.10 +gg0etau fakes Norm_FAKES 1.30 + + diff --git a/lfvetau/card_config/unc.1.conf b/lfvetau/card_config/unc.1.conf new file mode 100755 index 00000000..e647f7b0 --- /dev/null +++ b/lfvetau/card_config/unc.1.conf @@ -0,0 +1,24 @@ +lumi lnN +#Effi_E lnN +Effi_et_Tau lnN +Effi_et_Tau_highPt lnN + +Theo_UE lnN + +Theo_Scale_gg lnN +Theo_PDF_gg lnN + +Theo_Scale_vbf lnN +Theo_PDF_vbf lnN + +Norm_ZTauTau lnN +Norm_ZTauTau_et_1Jet_extra lnN +Norm_ZJetsOther lnN +Norm_DIBOSON lnN +Norm_TT lnN +Norm_FAKES lnN +Norm_SMGG lnN +Norm_SMVBF lnN +Norm_TOP lnN + +shape_MuTau_TES shape diff --git a/lfvetau/card_config/unc.1.vals b/lfvetau/card_config/unc.1.vals new file mode 100755 index 00000000..919bef15 --- /dev/null +++ b/lfvetau/card_config/unc.1.vals @@ -0,0 +1,24 @@ +boostetau gghiggs Theo_PDF_gg 1.097 +boostetau vbfhiggs Theo_PDF_vbf 1.036 + +boostetau gghiggs Theo_UE 0.95 +boostetau vbfhiggs Theo_UE 1.0 + +boostetau gghiggs Theo_Scale_gg 1.1 +boostetau vbfhiggs Theo_Scale_vbf 1.015 + +boostetau ztautau Norm_ZTauTau 1.03 +boostetau ztautau Norm_ZTauTau_et_1Jet_extra 1.05 +boostetau zjetsother Norm_ZJetsOther 1.3 + +boostetau simulated lumi 1.026 +#boostetau simulated Effi_E 1.02 +boostetau simulated Effi_et_Tau 1.06 +boostetau simulated Effi_et_Tau_highPt 1.03 + +boostetau diboson Norm_DIBOSON 1.15 +boostetau singlet Norm_TOP 1.10 +boostetau ttbar Norm_TT 1.10 +boostetau fakes Norm_FAKES 1.30 + +#boostetau signal,ztautau,SMVBF126,SMGG126 shape_ETau_TES 1 diff --git a/lfvetau/card_config/unc.2.conf b/lfvetau/card_config/unc.2.conf new file mode 100755 index 00000000..de8fc7a3 --- /dev/null +++ b/lfvetau/card_config/unc.2.conf @@ -0,0 +1,26 @@ +lumi lnN +#Effi_E lnN +Effi_et_Tau lnN +Effi_et_Tau_highPt lnN + +Theo_UE lnN + +Theo_Scale_gg lnN +Theo_PDF_gg lnN + +Theo_Scale_vbf lnN +Theo_PDF_vbf lnN + +Norm_ZTauTau lnN +Norm_ZTauTau_et_vbf_extra lnN +Norm_WW_et_vbf_extra lnN +Norm_TT_et_vbf_extra lnN +Norm_ZJetsOther lnN +Norm_DIBOSON lnN +Norm_TT lnN +Norm_FAKES lnN +Norm_SMGG lnN +Norm_SMVBF lnN +Norm_WWGG lnN +Norm_WWVBF lnN +Norm_TOP lnN diff --git a/lfvetau/card_config/unc.2.vals b/lfvetau/card_config/unc.2.vals new file mode 100755 index 00000000..f6d5102a --- /dev/null +++ b/lfvetau/card_config/unc.2.vals @@ -0,0 +1,26 @@ +vbfetau gghiggs Theo_PDF_gg 1.097 +vbfetau vbfhiggs Theo_PDF_vbf 1.036 + +vbfetau gghiggs Theo_UE 0.90 +vbfetau vbfhiggs Theo_UE 0.99 + +vbfetau gghiggs Theo_Scale_gg 0.7 +vbfetau vbfhiggs Theo_Scale_vbf 1.02 + +vbfetau ztautau Norm_ZTauTau 1.03 +vbfetau ztautau Norm_ZTauTau_et_vbf_extra 1.10 +vbfetau zjetsother Norm_ZJetsOther 1.3 + +vbfetau ww Norm_WW_et_vbf_extra 1.5 +vbfetau ttbar Norm_TT_et_vbf_extra 1.33 + +vbfetau simulated lumi 1.026 +#vbfetau simulated Effi_E 1.02 +vbfetau simulated Effi_et_Tau 1.06 +vbfetau simulated Effi_et_Tau_highPt 1.03 + +vbfetau diboson Norm_DIBOSON 1.15 +vbfetau singlet Norm_TOP 1.10 +vbfetau ttbar Norm_TT 1.10 +vbfetau fakes Norm_FAKES 1.30 + diff --git a/lfvetau/computeEmbeddedPeakScaleFactor.py b/lfvetau/computeEmbeddedPeakScaleFactor.py new file mode 100644 index 00000000..a5df2ef7 --- /dev/null +++ b/lfvetau/computeEmbeddedPeakScaleFactor.py @@ -0,0 +1,153 @@ +from FinalStateAnalysis.Utilities.rootbindings import ROOT +import math +import logging +import sys + +ROOT.gStyle.SetOptStat(0) +ROOT.gStyle.SetOptTitle(0) +ROOT.gROOT.SetBatch() + +file_dataA = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/ZetauEmbedded_Run2012A.root') +file_dataB = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/ZetauEmbedded_Run2012B.root') +file_dataC = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/ZetauEmbedded_Run2012C.root') +file_dataD = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/ZetauEmbedded_Run2012D.root') + +dataA=file_dataA.Get('os/gg/ept30/h_collmass_pfmet') +dataB=file_dataB.Get('os/gg/ept30/h_collmass_pfmet') +dataC=file_dataC.Get('os/gg/ept30/h_collmass_pfmet') +dataD=file_dataD.Get('os/gg/ept30/h_collmass_pfmet') + +data=dataC.Clone() +data.Add(dataB) +data.Add(dataA) +data.Add(dataD) + +c= ROOT.TCanvas("c","c", 800, 1000) +c.Draw() +c.SetGridx(1) +c.SetGridy(1) + + +njets=[0,1,2,3,4] + +file_MC0 = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/Z0jets_M50_skimmedTT.root') +file_MC1 = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/Z1jets_M50_skimmedTT.root') +file_MC2 = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/Z2jets_M50_skimmedTT.root') +file_MC3 = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/Z3jets_M50_skimmedTT.root') +file_MC4 = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/Z4jets_M50_skimmedTT.root') + +mc0 = file_MC0.Get('os/gg/ept30/h_collmass_pfmet') +mc1 = file_MC1.Get('os/gg/ept30/h_collmass_pfmet') +mc2 = file_MC2.Get('os/gg/ept30/h_collmass_pfmet') +mc3 = file_MC3.Get('os/gg/ept30/h_collmass_pfmet') +mc4 = file_MC4.Get('os/gg/ept30/h_collmass_pfmet') + +datalumi = 0 +#for i in ['A', 'B', 'C', 'D'] : +for i in ['C', 'D'] : + lumifile = 'inputs/newNtuple_5Nov/data_SingleElectron_Run2012%s_22Jan2013_v1.lumicalc.sum' %(i) + f = open(lumifile, 'r') + lumistring = f.readline() + datalumi += float(lumistring) + + +mc_lumi=[] + +for i in range(0,5): + + lumifile = 'inputs/newNtuple_5Nov/Z%sjets_M50_skimmedTT.lumicalc.sum' %(str(int(i))) + f = open(lumifile, 'r') + lumistring = f.readline() + mc_lumi.append(float(lumistring)) + +mc=mc0.Clone() +mc.Scale(datalumi/mc_lumi[0]) +mc.Add(mc1, datalumi/mc_lumi[1]) +mc.Add(mc2, datalumi/mc_lumi[2]) +mc.Add(mc3, datalumi/mc_lumi[3]) +mc.Add(mc4, datalumi/mc_lumi[4]) +mc.SetMarkerStyle(20) +mc.SetMarkerColor(4) +mc.SetLineColor(4) + +data.Scale(mc.Integral()/data.Integral() ) + +max_data = data.GetBinContent(data.GetMaximumBin()) +max_mc = mc.GetBinContent(mc.GetMaximumBin()) +mc.Draw() +data.SetMarkerStyle(20) +data.SetMarkerColor(2) +data.SetLineColor(2) +data.Draw() + +m = ROOT.RooRealVar('m', 'Collinear Mass', 90,40,250) +os_data = ROOT.RooDataHist('z_data','z_data',ROOT.RooArgList(m),data) +os_mc = ROOT.RooDataHist('z_mc','z_mc',ROOT.RooArgList(m),mc) +mean = ROOT.RooRealVar('mean', 'mean', 90,70,110) +sigmaL = ROOT.RooRealVar('sigmaL' ,'sigmaL' ,10 ,0 ,100) +sigmaR = ROOT.RooRealVar('sigmaR' ,'sigmaR' ,10 ,0 ,100) +alphaL = ROOT.RooRealVar('alphaL' ,'alphaL' ,1 ,0 ,30 ) +alphaR = ROOT.RooRealVar('alphaR' ,'alphaR' ,1 ,0 ,30 ) +command = ROOT.RooCruijff("os_func", "os_func",m, mean, sigmaL, sigmaR, alphaL, alphaR) + + +frame = m.frame(ROOT.RooFit.Title("Z mass peak")) + +fit_result_mc = command.fitTo( + os_mc, + ROOT.RooFit.Save(True)) + +os_mc.plotOn( + frame,ROOT.RooFit.LineColor(ROOT.EColor.kBlue),ROOT.RooFit.MarkerColor(ROOT.EColor.kBlue) +) +mcpeak = mean.getVal() +mcpeakerr = mean.getError() + +command.plotOn(frame,#ROOT.RooFit.VisualizeError(fit_result_mc,1), + ROOT.RooFit.LineColor(ROOT.EColor.kBlue)) + +leg = ROOT.TLegend(0.5,0.8,0.85,0.7) + + +print '____________________' + +fit_result = command.fitTo( + os_data, + ROOT.RooFit.Save(True)) +os_data.plotOn(frame,ROOT.RooFit.LineColor(ROOT.EColor.kRed),ROOT.RooFit.MarkerColor(ROOT.EColor.kRed) ) +command.plotOn(frame,ROOT.RooFit.LineColor(ROOT.EColor.kRed)) +datapeak = mean.getVal() +datapeakerr= mean.getError() +print 'datapeak : %f +/- %f, mcpeak: %f +/- %f' %(datapeak,datapeakerr, mcpeak, mcpeakerr ) + +leg.AddEntry(mc, "DY->#tau#tau (MC)", "lp") +leg.AddEntry(data, "DY->#tau#tau (embedded)", "lp") + +pt = ROOT.TPaveText(0.5, 0.5, 0.85, 0.65) +pt.AddText("MC peak: %.2f #pm %.2f GeV" %(mcpeak, mcpeakerr)) +pt.AddText("Emb peak: %.2f #pm %.2f GeV" %(datapeak, datapeakerr)) + +frame.addObject(pt) + +#text = ROOT.TText(124.5,1400, 'MC peak: %.2f #pm %.2f GeV' %(mcpeak, mcpeakerr)) +#text2 = ROOT.TText(120, 1300, 'Emb peak: %.2f #pm %.2f GeV'%(datapeak, datapeakerr)) +#text.SetTextSize(0.03) +#text.SetTextColor(4) +#text2.SetTextSize(0.03) +#frame.addObject(text) +#text2.SetTextColor(2) +#frame.addObject(text2) +leg.SetFillColor(0) +frame.addObject(leg) +frame.Draw() +#pt.AddText(text) +#pt.AddText(text2) +#Leg.Draw() +#leg.SetFillColor(0) +pt.SetFillColor(0) +pt.SetShadowColor(0) +pt.Paint() + +c.Update() +c.SaveAs('Zfit.pdf') +c.SaveAs('Zfit.png') diff --git a/lfvetau/computeyields.py b/lfvetau/computeyields.py new file mode 100644 index 00000000..e8c4c1d9 --- /dev/null +++ b/lfvetau/computeyields.py @@ -0,0 +1,150 @@ +import ROOT +import sys +import os +import math + +try: + massrange = sys.argv[1].split(',') + print massrange +except: + + print 'please give me the mass range in the format 50,300 ' + +ROOT.gROOT.SetBatch() # don't pop up canvases + +ROOT.gROOT.SetStyle('Plain') # white background +ROOT.gStyle.SetOptStat(0) + +samples = [ + "zjetsother", + "diboson", + "SMVBF126", + "singlet", + "SMGG126", + "ttbar", + "ztautau", + "fakes", + "LFVGG", + "LFVVBF" +] + +names = { + "zjetsother" : "zjetsother", + "diboson" : "diboson" , + "SMVBF126" : "SMH" , + "SMGG126" : "SMH" , + "singlet" : "singlet" , + "ttbar" : "ttbar" , + "ztautau" : "ztautau" , + "fakes" : "fakes" , + "LFVGG" : "LFVH" , + "LFVVBF" : "LFVH" +} + + +mymapper ={ + "fakes" : ["Fakes "], + "ztautau" : ["$ Z \\rightarrow \\tau\\tau$ "], + "diboson" : ["EWK Diboson "], + "zjetsother" : ["$Z \\rightarrow ee, \\mu\\mu$"], + "ttbar" : ["$t\\bar{t}$ "], + "singlet" : ["$t$, $\\bar{t}$ "], + "SMH" : ["SM Higgs Background "], + "tot" : ["Sum of Backgrounds "], + "LFVH" : ["LFV Higgs Signal "], +} + + + + +for jet in range (0, 3) : + + #norm_file = 'plots/newNtuple_5Nov/lfvet/unc.%s.vals' %(str(jet)) + #f = open(norm_file, 'r') + #lines= f.readlines() + #for line in lines : + # norm= line.split() + # print jet, norm, norm[1].split(',') + + + #print jet + file1 = ROOT.TFile.Open('plots/newNtuple_5Nov/lfvet/shapes.%s.root' %(str(jet))) + #print 'plots/newNtuple_5Nov/lfvet/shapes.%s.root' %(str(jet)) + #print file1.GetListOfKeys()[0].GetName() + mydir = file1.Get(file1.GetListOfKeys()[0].GetName()) + mylist=mydir.GetListOfKeys() + totbackground =0. + totbackgrounderr2=0. + for sample in samples: + #print sample + histo = mydir.Get(sample) + sublist = filter(lambda x : sample in x.GetName() and sample != x.GetName(), mylist) + + integral = 0 + err2=0 + + for i in range(histo.GetXaxis().FindBin(float(massrange[0])), histo.GetXaxis().FindBin(float(massrange[1]))+1): + integral += histo.GetBinContent(i) + err2 += histo.GetBinError(i)*histo.GetBinError(i) + if integral != 0: + print sample, integral, math.sqrt(err2), math.sqrt(err2)/integral + else: + print sample, integral, math.sqrt(err2) + + for histname in sublist: + #print 'histo name', histname.GetName() + systHistInt=0. + h = mydir.Get(histname.GetName()).Clone() + #print histo.Integral(), h.Integral() + for i in range(histo.GetXaxis().FindBin(float(massrange[0])), histo.GetXaxis().FindBin(float(massrange[1]))+1): + systHistInt+=pow(h.GetBinContent(i) - histo.GetBinContent(i), 2) + #print sample, h.GetBinContent(i) , histo.GetBinContent(i), h.GetBinContent(i) - histo.GetBinContent(i) + #print histname.GetName(), systHistInt + #print systHistInt, integral + err2+= systHistInt + + + + #print histo.FindBin(float(massrange[0])), histo.FindBin(float(massrange[1])), histo.GetXaxis().GetNbins() + + if integral != 0: + print sample, integral, math.sqrt(err2), math.sqrt(err2)/integral + else: + print sample, integral, math.sqrt(err2), + + if not 'LFV' in sample: + totbackground += integral + totbackgrounderr2 += err2 + #print totbackground, totbackgrounderr2 + + try: + print mymapper[names[sample]][1+3*jet],mymapper[names[sample]][3+3*jet] + mymapper[names[sample]][1+3*jet]=float(mymapper[names[sample]][1+3*jet]) + integral + mymapper[names[sample]][3+3*jet]=math.sqrt(pow(float(mymapper[names[sample]][3+3*jet]),2) + err2) + + except: + mymapper[names[sample]].extend([ integral, "$\pm$" , math.sqrt(err2)]) + + print 'tot', totbackground, math.sqrt(totbackgrounderr2) + mylist = ["%.2f" % totbackground, "$\pm$" , "%.2f" % math.sqrt(totbackgrounderr2)] + mymapper["tot"].extend(mylist) + + #error=ROOT.Double() + #print histo.IntegralAndError(histo.FindBin(float(massrange[0])), histo.FindBin(float(massrange[1])), error), error + +for k,v in mymapper.iteritems(): + for n,obj in enumerate(v) : + if isinstance(obj, float): mymapper[k][n]=str('%.2f' %obj) + +outfile = open('yields_MassRange_%s_%s.tex' %(str(massrange[0]),str(massrange[1])) ,'w') +outfile.write('\\begin{table}[h] \n') +outfile.write('\\begin{tabular}{|c|rcl|rcl|rcl|}\n') + +for k,v in mymapper.iteritems(): + print ' & '.join(v)+'\\\\\\hline\n' + outfile.write(' & '.join(v)+'\\\\\n') + +outfile.write('\\end{tabular}\n') +outfile.write('\\end{table}\n') + +outfile.close() diff --git a/lfvetau/cutflowtracker.py b/lfvetau/cutflowtracker.py new file mode 100644 index 00000000..18795e71 --- /dev/null +++ b/lfvetau/cutflowtracker.py @@ -0,0 +1,46 @@ +##author Mauro Verzetti + +import os + +class cut_flow_tracker(object): + def __init__(self, hist): + self.labels = [hist.GetXaxis().GetBinLabel(i+1) for i in range(hist.GetNbinsX())] + self.cut_flow = dict([ (i, False) for i in self.labels]) + self.hist = hist + self.evt_info = [-1, -1, -1] + self.disabled = 'CutFlow' not in os.environ + self.sync_mode = 'SYNC' in os.environ + + def fill(self, label): + self.cut_flow[label] = True + + def Fill(self, *labels): + if self.disabled: + return + for label in labels: + self.fill(label) + + def flush(self): + if self.disabled: + return + final_i = -1 + for i, label in enumerate(self.labels): + val = self.cut_flow[label] + if val: + self.hist.Fill(i+0.5) + final_i = i + if self.sync_mode: + fails = '' + try: + fails = 'fails %s' % (self.labels[final_i+1]) + except IndexError: + fails = 'passes the selection' #if len(self.labels) == final_i else 'passes the selection' + print 'Event %i:%i:%i ' % tuple(self.evt_info) + fails + + def new_row(self, *args): + if self.disabled: + return + if self.evt_info != list(args): + self.flush() + self.evt_info = list(args) + self.cut_flow = dict([ (i, False) for i in self.labels]) diff --git a/lfvetau/dump_xsec.py b/lfvetau/dump_xsec.py new file mode 100644 index 00000000..984e81aa --- /dev/null +++ b/lfvetau/dump_xsec.py @@ -0,0 +1,29 @@ +import FinalStateAnalysis.Utilities.prettyjson as prettyjson +import os +import glob +from pdb import set_trace + +def dump_xsec(jobid): + json_files = [i for i in glob.glob('inputs/%s/*.json' % jobid) if 'data_' not in i] + lumi_files = [i for i in glob.glob('inputs/%s/*.sum' % jobid) if 'data_' not in i] + + datasets = {} + for json_file in json_files: + dname = json_file.split('/')[-1].split('.')[0] + datasets[dname] = {} + datasets[dname]['numevts'] = prettyjson.loads(open(json_file).read())['n_evts'] + + for lumi_file in lumi_files: + dname = lumi_file.split('/')[-1].split('.')[0] + datasets[dname]['lumi'] = float( open(lumi_file).read().strip() ) + + out_format = '%60s'+'%15s'*3 + print out_format % ('dataset', '# evts', 'lumi [/pb]', 'xsec [pb]') + for dataset, info in datasets.iteritems(): + print out_format % (dataset, '%.3f' % info['numevts'], '%.3f' % info['lumi'], '%.5f' % (info['numevts']/info['lumi']) ) + +#print '\n\n%s\n' % os.environ['jobid7'] +#dump_xsec(os.environ['jobid7']) + +print '\n\n%s\n' % os.environ['jobid'] +dump_xsec(os.environ['jobid8']) diff --git a/lfvetau/electronids.py b/lfvetau/electronids.py new file mode 100644 index 00000000..279e39c8 --- /dev/null +++ b/lfvetau/electronids.py @@ -0,0 +1,118 @@ +#This module provides additional electron ID's starting from MVA's raw values +from FinalStateAnalysis.PlotTools.decorators import memo +@memo +def getVar(name, var): + return name+var + +def h2etau_looseId(row, name): + return bool(getattr(row, getVar(name, 'CBID_LOOSE'))) +def h2etau_tightId(row, name): + return bool(getattr(row, getVar(name, 'CBID_TIGHT'))) + +def zh_loose_2012eid(row, name): + value = getattr(row, getVar(name, 'MVANonTrig')) + pt = getattr(row, getVar(name, 'Pt')) + fabseta = getattr(row, getVar(name, 'AbsEta')) + if pt > 10. and fabseta < 0.8: + return (value > 0.5) + elif pt > 10. and fabseta >=0.8 and fabseta < 1.479: + return (value > 0.12) + elif pt > 10. and fabseta >= 1.479: + return (value > 0.6) + return False + +def h2tau_2012_LooseId(row, name): + return bool( getattr(row, getVar(name, 'MVAIDH2TauWP'))) + + +def h2tau_2012_tightId(row, name): + mva_output = getattr(row, getVar(name, 'MVANonTrig')) + pT = getattr(row, getVar(name, 'Pt')) + abseta = getattr(row, getVar(name, 'AbsEta')) + if pT > 20 and abseta < 0.8: + return ( mva_output > 0.925 ) + elif pT > 20 and 0.8 < abseta < 1.479: + return ( mva_output > 0.975 ) + elif pT > 20 and abseta > 1.479: + return ( mva_output > 0.985 ) + return False + + +#LEPTON ID-ISO +def summer_2013_eid(row, name): + mva_output = getattr(row, getVar(name, 'MVANonTrig')) #was eMVATrigNoIP + pT = getattr(row, getVar(name, 'Pt')) + abseta= getattr(row, getVar(name, 'AbsEta')) + if pT < 20 and abseta < 0.8: + return ( mva_output > 0.925 ) + elif pT < 20 and 0.8 < abseta < 1.479: + return ( mva_output > 0.915 ) + elif pT < 20 and abseta > 1.479: + return ( mva_output > 0.965 ) + elif pT > 20 and abseta < 0.8: + return ( mva_output > 0.905 ) + elif pT > 20 and 0.8 < abseta < 1.479: + return ( mva_output > 0.955 ) + elif pT > 20 and abseta > 1.479: + return ( mva_output > 0.975 ) + return False + +def summer_2013_eid_tight(row, name): + mva_output = getattr(row, getVar(name, 'MVANonTrig')) + pT = getattr(row, getVar(name, 'Pt')) + abseta= getattr(row, getVar(name, 'AbsEta')) + if pT > 20 and abseta < 0.8: + return ( mva_output > 0.925) + elif pT > 20 and 0.8 < abseta < 1.479: + return ( mva_output > 0.975 ) + elif pT > 20 and abseta > 1.479: + return ( mva_output > 0.985 ) + return False + +#add electron id from AN 2012_463 +def hWW_eid_tight(row, name): + mva_output = getattr(row, getVar(name,'MVATrig')) #check if it is correct + pT = getattr(row, getVar(name, 'Pt')) + abseta= getattr(row, getVar(name, 'AbsEta')) + if pT>20: + if abseta <0.8: + if mva_output >= 0.914 : return True + if abseta <1.479 and abseta > 0.8 : + if mva_output >=0.964 : return True + if abseta <2.5 and abseta > 1.479: + if mva_output >=0.899 : return True + + return False + +def hWW_eid_loose(row, name): + mva_output = getattr(row, getVar(name,'MVATrig')) #check if correct + pT = getattr(row, getVar(name, 'Pt')) + abseta= getattr(row, getVar(name, 'AbsEta')) + if pT>20: + if abseta <0.8: + if mva_output >= 0.877 : return True + if abseta <1.479 and abseta > 0.8 : + if mva_output >=0.811 : return True + if abseta <2.5 and abseta > 1.479: + if mva_output >=0.707 : return True + + return False + + + +#ID MVA cut value (tight lepton) 0.913 0.964 0.899 +#Isolation cut value (tight lepton) 0.105 0.178 0.150 +#ID MVA cut value (loose lepton) 0.877 0.811 0.707 +#Isolation cut value (loose lepton) 0.426 0.481 0.390 + +electronIds = { + 'eidCBLoose' : h2etau_looseId, + 'eidCBTight' : h2etau_tightId, + 'eid12Loose' : zh_loose_2012eid, + 'eid12Medium': h2tau_2012_LooseId, + 'eid12Tight' : h2tau_2012_tightId, + 'eid14Loose' : hWW_eid_loose, + 'eid14Tight' : hWW_eid_tight, + 'eid13Loose' : summer_2013_eid, + 'eid13Tight' : summer_2013_eid_tight, +} diff --git a/lfvetau/environment.sh b/lfvetau/environment.sh new file mode 100644 index 00000000..dfb159e1 --- /dev/null +++ b/lfvetau/environment.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +source ../environment.sh +source jobid.sh +export jobid=$jobid8 +export MEGAPATH=/hdfs/store/user/taroni/ +export blind='YES' diff --git a/lfvetau/fakerate_functions.py b/lfvetau/fakerate_functions.py new file mode 100644 index 00000000..afe34a9b --- /dev/null +++ b/lfvetau/fakerate_functions.py @@ -0,0 +1,56 @@ +import os +import re +import glob +import ROOT +from math import sqrt +from FinalStateAnalysis.StatTools.RooFunctorFromWS import build_roofunctor, make_corrector_from_histo + +frfit_dir = os.path.join('results', os.environ['jobid'], 'fakerate_fits')+'/' + +tau_fake_rate = build_roofunctor( + frfit_dir+'t_os_tLoose_tTigh_tAbsEta.root', + 'fit_efficiency', + 'efficiency' +) + +def inflate_systematics(functor, inflation): + '''Christian recipe: INFLATE ALL THE SYSTEMATICS!''' + def fcn(*args): + return functor(*args) * inflation + return fcn + + #tau_fake_rate_up = inflate_systematics(tau_fake_rate, 1.3) + #tau_fake_rate_dw = inflate_systematics(tau_fake_rate, 0.7) + +###Tau fakes as it should be +tau_fake_rate_up = make_corrector_from_histo( + frfit_dir+'t_os_tLoose_tTigh_tAbsEta_2.root', + 'efficiency_up', + '1D' +) + +tau_fake_rate_dw = make_corrector_from_histo( + frfit_dir+'t_os_tLoose_tTigh_tAbsEta_2.root', + 'efficiency_dw', + '1D' +) + +efrfit_dir = os.path.join('results', os.environ['jobid'], 'efakerate_fits')+'/' + +e_fake_rate = build_roofunctor( + efrfit_dir+'e_os_eLoose_eTigh_e3AbsEta.root', + 'fit_efficiency', + 'efficiency' +) +e_fake_rate_up = make_corrector_from_histo( + efrfit_dir+'e_os_eLoose_eTigh_e3AbsEta_2.root', + 'efficiency_up', + '1D' +) + +e_fake_rate_dw = make_corrector_from_histo( + efrfit_dir+'e_os_eLoose_eTigh_e3AbsEta_2.root', + 'efficiency_dw', + '1D' +) + diff --git a/lfvetau/jobid.sh b/lfvetau/jobid.sh new file mode 100644 index 00000000..f71c987c --- /dev/null +++ b/lfvetau/jobid.sh @@ -0,0 +1,10 @@ +#export jobid8='MCntuples_25March' +#export jobid8='MCntuples_otherCh2' +#export jobid8='MCntuples_14April' +#export jobid8='MCntuples_13May' +#export jobid8='newNtuple_3JuneLfvHnoteXsec' +#export jobid8='newNtuple_2Sept' +#export jobid8='newNtuple_1Oct' +export jobidmt='MCntuples_8April' +export jobid8='newNtuple_5Nov' +export jobidSync='newNtuple_Sync' diff --git a/lfvetau/make_proxies.sh b/lfvetau/make_proxies.sh new file mode 100755 index 00000000..289e1493 --- /dev/null +++ b/lfvetau/make_proxies.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# Generate the cython proxies used in the analyses + +source jobid.sh + +export jobid=$jobid8 +#export jobid="newNtuple_9Oct" + +export datasrc=/hdfs/store/user/$USER/$jobid +#export datasrc=`ls -d /nfs_scratch/taroni/$jobid | head -n 1` + +if [ -z $1 ]; then + export afile=`find $datasrc/ | grep root | head -n 1` +else + export afile=$1 +fi + +echo "Building cython wrappers from file: $afile" + +rake "make_wrapper[$afile, eee/final/Ntuple, EEETree]" +rake "make_wrapper[$afile, ee/final/Ntuple, EETree]" +rake "make_wrapper[$afile, eet/final/Ntuple, EETauTree]" +rake "make_wrapper[$afile, et/final/Ntuple, ETauTree]" +ls *pyx | sed "s|pyx|so|" | xargs -n 1 -P 10 rake + +export jobid=$jobidSync +export datasrc=/hdfs/store/user/$USER/$jobid +if [ -z $1 ]; then + export afile=`find $datasrc/ | grep root | head -n 1` +else + export afile=$1 +fi + +rake "make_wrapper[$afile, eg/final/Ntuple, EGTree]" +ls *pyx | sed "s|pyx|so|" | xargs -n 1 -P 10 rake + +#export jobid=$jobidmt +#export datasrc=/hdfs/store/user/$USER/$jobid +# +#if [ -z $1 ]; then +# export afile=`find $datasrc/ | grep root | head -n 1` +#else +# export afile=$1 +#fi +# +#rake "make_wrapper[$afile, mt/final/Ntuple, MuTauTree]" +#ls *pyx | sed "s|pyx|so|" | xargs -n 1 -P 10 rake diff --git a/lfvetau/mcCorrections.py b/lfvetau/mcCorrections.py new file mode 100755 index 00000000..dddc6b81 --- /dev/null +++ b/lfvetau/mcCorrections.py @@ -0,0 +1,166 @@ +import os +import glob +import FinalStateAnalysis.TagAndProbe.HetauCorrection as HetauCorrection +import FinalStateAnalysis.TagAndProbe.PileupWeight as PileupWeight +from FinalStateAnalysis.PlotTools.decorators import memo, memo_last + +@memo +def getVar(name, var): + return name+var + +is7TeV = bool('7TeV' in os.environ['jobid']) +pu_distributions = { + 'singlee' : glob.glob(os.path.join( 'inputs', os.environ['jobid'], 'data_SingleElectron*pu.root'))} +pu_distributionsUp = { + 'singlee' : glob.glob(os.path.join( 'inputs', os.environ['jobid'], 'data_SingleElectron*pu_up.root'))} +pu_distributionsDown = { + 'singlee' : glob.glob(os.path.join( 'inputs', os.environ['jobid'], 'data_SingleElectron*pu_down.root'))} +mc_pu_tag = 'S6' if is7TeV else 'S10' + + +def make_puCorrector(dataset, kind=None): + 'makes PU reweighting according to the pu distribution of the reference data and the MC, MC distribution can be forced' + if not kind: + kind = mc_pu_tag + weights = [] + if dataset in pu_distributions:# and dataset in pu_distributionsUp and dataset in pu_distributionsDown: + return PileupWeight.PileupWeight( 'S6' if is7TeV else 'S10', *(pu_distributions[dataset])) +# weights = (PileupWeight.PileupWeight( 'S6' if is7TeV else 'S10', *(pu_distributions[dataset])), PileupWeight.PileupWeight( 'S6' if is7TeV else 'S10', *(pu_distributionsUp[dataset])), PileupWeight.PileupWeight( 'S6' if is7TeV else 'S10', *(pu_distributionsDown[dataset]))) +# return weights + else: + raise KeyError('dataset not present. Please check the spelling or add it to mcCorrectors.py') + +def make_puCorrectorUp(dataset, kind=None): + 'makes PU reweighting according to the pu distribution of the reference data and the MC, MC distribution can be forced' + if not kind: + kind = mc_pu_tag + if dataset in pu_distributions: + return PileupWeight.PileupWeight( 'S6' if is7TeV else 'S10', *(pu_distributionsUp[dataset])) + else: + raise KeyError('dataset not present. Please check the spelling or add it to mcCorrectors.py') + +def make_puCorrectorDown(dataset, kind=None): + 'makes PU reweighting according to the pu distribution of the reference data and the MC, MC distribution can be forced' + if not kind: + kind = mc_pu_tag + if dataset in pu_distributions: + return PileupWeight.PileupWeight( 'S6' if is7TeV else 'S10', *(pu_distributionsDown[dataset])) + else: + raise KeyError('dataset not present. Please check the spelling or add it to mcCorrectors.py') + +def make_shifted_weights(default, shifts, functors): + '''make_shifted_weights(default, shifts, functors) --> functor + takes as imput the central value functor and two lists + the name of the shifts and the shifted functors + the returned functor takes one additional string to + select the shifted functor. If the shift kwarg is missing + or does not match any shift tag the central (default) + fuctor output is returned''' + #make default fetching faster + default = default + def functor(*args, **kwargs): + shift = '' + if 'shift' in kwargs: + shift = kwargs['shift'] + del kwargs['shift'] + + #check if to apply shifts + for tag, fcn in zip(shifts, functors): + if tag == shift: + return fcn(*args, **kwargs) + + return default(*args, **kwargs) + return functor + +def make_multiple(fcn, indexed=False, shift=0): + '''make_multiple(fcn, indexed=True, shift=0) --> functor + takes as imput a weight correction function of pt and eta + and returns a functor multiple(row,*args) --> weight + where *args are the base name of the objects upon which + compute the correction. + + If indexed is true means that fcn returns a tuple + (weight, error) shift +/-1 makes the functor return the + weight+/-error in this case''' + def multiple(row,*args): + ret = 1. + for arg in args: + abseta = getattr( + row, + getVar(arg,'Eta') + ) + pt = getattr(row, getVar(arg,'Pt')) + if pt<30: pt =30 #only fakerate checks allow pt < 30. This is an approximation to not re-run the Tag and Probe + fcn_ret = fcn(pt,abseta) + if indexed: + value, err = fcn_ret + if shift == 1: + ret *= (value + err) + elif shift == -1: + ret *= (value - err) + else: + ret *= value + else: + ret *= fcn_ret + return ret + return multiple + + +##put here the trigger correction as in https://github.com/mverzett/UWHiggs/blob/WH_At_Paper/wh/mcCorrectors.py + +correct_e = make_multiple(HetauCorrection.correct_hamburg_e ) +correct_eid13_mva = make_multiple(HetauCorrection.correct_eid13_mva ) +correct_eid13_p1s_mva = make_multiple(HetauCorrection.correct_eid13_p1s_mva) +correct_eid13_m1s_mva = make_multiple(HetauCorrection.correct_eid13_m1s_mva) + +correct_eiso13_mva = make_multiple(HetauCorrection.correct_eiso13_mva ) +correct_eiso13_p1s_mva = make_multiple(HetauCorrection.correct_eiso13_p1s_mva) +correct_eiso13_m1s_mva = make_multiple(HetauCorrection.correct_eiso13_m1s_mva) + +#correct_eid_mva = make_multiple(HetauCorrection.scale_eleId_hww) +#correct_eReco_mva = make_multiple(HetauCorrection.scale_elereco_hww) +#correct_eIso_mva = make_multiple(HetauCorrection.scale_eleIso_hww) +correct_trigger_mva = make_multiple(HetauCorrection.single_ele_mva, indexed=True) +correct_trigger_mva_up = make_multiple(HetauCorrection.single_ele_mva, indexed=True, shift=1) +correct_trigger_mva_dw = make_multiple(HetauCorrection.single_ele_mva, indexed=True, shift=-1) + +efficiency_trigger_mva = make_multiple(HetauCorrection.single_ele_eff_mva, indexed=True) +efficiency_trigger_mva_up = make_multiple(HetauCorrection.single_ele_eff_mva, indexed=True, shift=1) +efficiency_trigger_mva_dw = make_multiple(HetauCorrection.single_ele_eff_mva, indexed=True, shift=-1) + +correct_eEmb = make_multiple(HetauCorrection.correct_eEmb,indexed=True) +correct_eEmb_p1s = make_multiple(HetauCorrection.correct_eEmb,indexed=True, shift=1) +correct_eEmb_m1s = make_multiple(HetauCorrection.correct_eEmb,indexed=True, shift=-1) + + +eiso_correction = make_shifted_weights( + correct_eiso13_mva, + ['eisop1s','eisom1s'], + [correct_eiso13_p1s_mva, correct_eiso13_m1s_mva], +) + +eid_correction = make_shifted_weights( + correct_eid13_mva, + ['eidp1s','eidm1s'], + [correct_eid13_p1s_mva, correct_eid13_m1s_mva] +) + +eEmb_correction = make_shifted_weights( + correct_eEmb, + ['eEmbp1s','eEmbm1s'], + [correct_eEmb_p1s, correct_eEmb_m1s] +) + + +trig_correction = make_shifted_weights( + correct_trigger_mva, + ['trp1s', 'trm1s'], + [correct_trigger_mva_up, correct_trigger_mva_dw] +) + +trig_efficiency = make_shifted_weights( + efficiency_trigger_mva, + ['trp1s', 'trm1s'], + [efficiency_trigger_mva_up, efficiency_trigger_mva_dw] +) + diff --git a/lfvetau/myNewPlotterReco.py b/lfvetau/myNewPlotterReco.py new file mode 100644 index 00000000..cc1b4461 --- /dev/null +++ b/lfvetau/myNewPlotterReco.py @@ -0,0 +1,158 @@ +import rootpy.plotting.views as views +from FinalStateAnalysis.PlotTools.SimplePlotter import SimplePlotter +from FinalStateAnalysis.PlotTools.Plotter import Plotter +from FinalStateAnalysis.PlotTools.BlindView import BlindView +from FinalStateAnalysis.PlotTools.PoissonView import PoissonView +from FinalStateAnalysis.PlotTools.MedianView import MedianView +from FinalStateAnalysis.PlotTools.ProjectionView import ProjectionView +#from FinalStateAnalysis.PlotTools.FixedIntegralView import FixedIntegralView +from FinalStateAnalysis.PlotTools.RebinView import RebinView +from FinalStateAnalysis.MetaData.data_styles import data_styles, colors +from FinalStateAnalysis.PlotTools.decorators import memo +from FinalStateAnalysis.MetaData.datacommon import br_w_leptons, br_z_leptons +from optparse import OptionParser +import os +import ROOT +import glob +import math +import logging +from fnmatch import fnmatch +from yellowhiggs import xs, br, xsbr + +ROOT.gROOT.SetBatch() +ROOT.gStyle.SetOptStat(0) +jobid = os.environ['jobid'] + +print jobid +mc_samples = [ + 'ggHiggsToETau', + 'vbfHiggsToETau', + 'GluGluToHToTauTau_M-125_8TeV-powheg-pythia6', + 'VBF_HToTauTau_M-125_8TeV-powheg-pythia6', + 'Zjets_M50_skimmedLL', + 'Z1jets_M50_skimmedLL', + 'Z2jets_M50_skimmedLL', + 'Z3jets_M50_skimmedLL', + 'Z4jets_M50_skimmedLL', + 'Zjets_M50_skimmedTT', + 'Z1jets_M50_skimmedTT', + 'Z2jets_M50_skimmedTT', + 'Z3jets_M50_skimmedTT', + 'Z4jets_M50_skimmedTT', + 'TTJets*', + 'T_t*', + 'Tbar_t*', + 'WplusJets_madgraph_skimmed', + 'Wplus1Jets_madgraph', + 'Wplus1Jets_madgraph_tapas', + 'Wplus2Jets_madgraph', + 'Wplus2Jets_madgraph_tapas', + 'Wplus3Jets_madgraph', + 'Wplus4Jets_madgraph', + 'WWJets*', + 'WZJets*', + 'ZZJets*' +] + +files = [] +lumifiles = [] +channel = 'et' +for x in mc_samples: + files.extend(glob.glob('results/%s/LFVHETauAnalyzer/%s.root' % (jobid, x))) + lumifiles.extend(glob.glob('inputs/%s/%s.lumicalc.sum' % (jobid, x))) + + + +period = '8TeV' +sqrts = 7 if '7TeV' in jobid else 8 + +def remove_name_entry(dictionary): + return dict( [ i for i in dictionary.iteritems() if i[0] != 'name'] ) + + +#sign = ['os', 'ss'] +process = ['gg'] +#ptcut = [0, 40] +njets= [0,1,2,3] +sign = ['os'] + +ptcut = [0] #was 0 + + + +outputdir = 'plots/%s/LFVHETauAnalyzer/%s/' % (jobid, channel) +if not os.path.exists(outputdir): + os.makedirs(outputdir) + +plotter = Plotter(files, lumifiles, outputdir, None, forceLumi=19800) +#plotter = SimplePlotter(files, lumifiles, outputdir) + + + + +EWKDiboson = views.StyleView( + views.SumView( + *[ plotter.get_view(regex) for regex in \ + filter(lambda x : x.startswith('WW') or x.startswith('WZ') or x.startswith('ZZ') or x.startswith('WG'), mc_samples )] + ), **remove_name_entry(data_styles['WW*'#,'WZ*', 'WG*', 'ZZ*' +]) +) +Wplus = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('Wplus'), mc_samples )]), **remove_name_entry(data_styles['Wplus*Jets*'])) +DYLL = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('skimmedLL'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*LL'])) +DYTT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('jets_M50_skimmedTT'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*TT'])) +singleT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('T_') or x.startswith('Tbar_'), mc_samples)]), **remove_name_entry(data_styles['T*_t*'])) + +SMH = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : 'HToTauTau' in x , mc_samples)]), **remove_name_entry(data_styles['GluGluToHToTauTau*'])) + + +plotter.views['EWKDiboson']={'view' : EWKDiboson } +plotter.views['Wplus']={'view' : Wplus } +plotter.views['DYLL']={'view' : DYLL } +plotter.views['DYTT']={'view' : DYTT } +plotter.views['singleT']={'view' : singleT } +plotter.views['SMH']={'view' : SMH } + + +new_mc_samples = filter( lambda x : not x.startswith('T_') and not x.startswith('Tbar_') and not x.endswith('jets_M50_skimmedLL') and not x.endswith('jets_M50_skimmedTT') and not x.startswith('Wplus') and not x.startswith('WW') and not x.startswith('WZ') and not x.startswith('ZZ') and not x.startswith('WG') and not x.endswith('HiggsToETau') and not 'HToTauTau' in x, mc_samples) + +new_sigsamples= filter(lambda x: x.endswith('HiggsToETau'), mc_samples) + +print new_sigsamples +new_mc_samples.extend(['EWKDiboson', 'SMH', 'singleT','Wplus', 'DYLL', 'DYTT' +]) +print new_mc_samples + +histoname = [('tPt','#tau p_{T} (GeV)',5), ('tPhi','#tau #phi',5), ('tEta','#tau #eta',2), + ('ePt', 'e p_{T} (GeV)', 5), ('ePhi','e #phi', 5), ('eEta','e #eta',2), + ('et_DeltaPhi','e-#tau #Delta#phi',1), ('et_DeltaR','e-#tau #DeltaR',1), ('tPFMET_DeltaPhi','#tau-PFMET #Delta#phi',2) , + ('tPFMET_Mt','#tau-PFMET M_{T} (GeV)',5), ('tMVAMET_DeltaPhi','#tau-MVAMET #Delta#phi',2), ('tMVAMET_Mt','#tau-MVAMET M_{T} (GeV)',5), + ('ePFMET_DeltaPhi','e-PFMET #Delta#phi',2), ('ePFMET_Mt','e-PFMET M_{T} (GeV)',5), ('eMVAMET_DeltaPhi','e-MVAMET #Delta#phi',2), + ('eMVAMET_Mt','e-MVAMET #M_{T} (GeV)',5), ('jetN_20','Number of jets',1), ('jetN_30','Number of jets',1), + ('h_collmass_pfmet','M_{e#tau}coll (GeV)',1), ('h_collmass_mvamet','M_{e#tau}coll (GeV)',1), ('h_vismass','M_{e#tau} vis (GeV)',1) ] + +plotter.mc_samples = new_mc_samples +#plotter.mc_samples = mc_samples +for i in sign : + for j in process: + for k in ptcut : + for nj in njets: + + + for n,h in enumerate(histoname) : + foldername = i+'/'+j+'/ept'+str(int(k))+'/'+str(int(nj)) + + #plotter.canvas.SetLogy(True) + plotter.plot_mc(foldername, ['ggHiggsToETau','vbfHiggToETau'],h[0], rebin=h[2], xaxis=h[1], leftside=False, show_ratio=False, ratio_range=1.5, rescale=10) + #plotter.simpleplot_mc(foldername,h[0], rebin=h[2], xaxis= h[1], leftside=False) + if not os.path.exists(outputdir+foldername): + os.makedirs(outputdir+foldername) + + plotter.save(foldername+'/mc_'+h) + + foldername = i+'/'+j+'/ept'+str(k)+'/'+str(nj)+'/selected' + #plotter.canvas.SetLogy(True) + plotter.plot_mc(foldername, ['ggHiggsToETau','vbfHiggsToETau'],h[0], rebin=h[2], xaxis= h[1], leftside=False, show_ratio=False,ratio_range=3, rescale=10) + if not os.path.exists(outputdir+foldername): + os.makedirs(outputdir+foldername) + + plotter.save(foldername+'/mc_'+h[0]) diff --git a/lfvetau/optimizer.py b/lfvetau/optimizer.py new file mode 100644 index 00000000..489a805c --- /dev/null +++ b/lfvetau/optimizer.py @@ -0,0 +1,150 @@ + #author Mauro Verzetti +'small interface module to deal with optimizization' + +import os +import itertools + +#RUN_OPTIMIZATION = ('RUN_OPTIMIZATION' in os.environ) and eval(os.environ['RUN_OPTIMIZATION']) +RUN_OPTIMIZATION = True + +_0jets = { + 'tPt' : range(30,50,10)+[35,45], + 'ePt' : range(30,50,10)+[35,45], + 'dphi' : [3.00, 2.7, 2.4, 2.2], + 'tMtToPfMet' : range(20,50,10)+[35], +} +_0jets_default = { + 'tPt' : 30, + 'ePt' : 50, + 'dphi': 2.5, + 'tMtToPfMet' :50, +} +_0jet_region_templates = ['tPt%i', 'ePt%i', 'dphi%.2f', 'tMtToPfMet%i'] #'tPt%i_ePt%i_dphi%.2f_tMtToPfMet%i' +def _get_0jet_regions(tPt, ePt, dphi, tMtToPfMet): + pass_tPt = [i for i in _0jets['tPt' ] if tPt > i] + pass_ePt = [i for i in _0jets['ePt' ] if ePt > i] + pass_dphi = [i for i in _0jets['dphi' ] if dphi > i] + pass_tMtToPfMet = [i for i in _0jets['tMtToPfMet'] if tMtToPfMet < i] + + cuts = [pass_tPt, pass_ePt, pass_dphi, pass_tMtToPfMet] + pass_default_tPt = tPt > _0jets_default['tPt' ] + pass_default_ePt = ePt > _0jets_default['ePt' ] + pass_default_dphi = dphi > _0jets_default['dphi' ] + pass_default_tMtToPfMet = tMtToPfMet < _0jets_default['tMtToPfMet'] + + defaults = [pass_default_tPt, pass_default_ePt, pass_default_dphi, pass_default_tMtToPfMet] + ret = [] + for cut_idx, opts in enumerate(cuts): + if all(j for i,j in enumerate(defaults) if i != cut_idx): + ret.extend([_0jet_region_templates[cut_idx] % i for i in opts]) + + return ret +_1jets = { + 'tPt' : range(30,50,10)+[35,45], + 'ePt' : range(30,50,10)+[35,45], + 'tMtToPfMet' : range(20,50,10)+[35], +} +_1jets_default = { + 'tPt' : 30, + 'ePt' : 40, + 'tMtToPfMet' :35, +} + +_1jet_region_templates = ['tPt%i', 'ePt%i', 'tMtToPfMet%i']#'tPt%i_ePt%i_tMtToPfMet%i' +def _get_1jet_regions(tPt, ePt, tMtToPfMet): + pass_tPt = [i for i in _1jets['tPt' ] if tPt > i] + pass_ePt = [i for i in _1jets['ePt' ] if ePt > i] + pass_tMtToPfMet = [i for i in _1jets['tMtToPfMet'] if tMtToPfMet < i] + + cuts = [pass_tPt, pass_ePt, pass_tMtToPfMet] + pass_default_tPt = tPt > _1jets_default['tPt' ] + pass_default_ePt = ePt > _1jets_default['ePt' ] + pass_default_tMtToPfMet = tMtToPfMet < _1jets_default['tMtToPfMet'] + + defaults = [pass_default_tPt, pass_default_ePt, pass_default_tMtToPfMet] + ret = [] + for cut_idx, opts in enumerate(cuts): + if all(j for i,j in enumerate(defaults) if i != cut_idx): + ret.extend([_1jet_region_templates[cut_idx] % i for i in opts]) + + return ret + + + +_2jets = { + 'tPt' : range(30,50,10)+[35,45], + 'ePt' : range(30,50,10)+[35,45], + 'tMtToPfMet' : range(20,50,10)+[35], + 'vbf_mass' : range(400, 600, 100) + [550], + 'vbf_deta' : [2.5, 3.0, 3.5, 4.0], +} +_2jets_default = { + 'tPt' : 30, + 'ePt' : 40, + 'tMtToPfMet' : 35, + 'vbf_mass' : 400, + 'vbf_deta' : 2.5, +} + +_2jet_region_templates = ['tPt%i', 'ePt%i', 'tMtToPfMet%i', 'vbf_mass%i', 'vbf_deta%.1f' ]#'tPt%i_ePt%i_tMtToPfMet%i_vbf_mass%i_vbf_deta%.1f' +def _get_2jet_regions(tPt, ePt, tMtToPfMet, vbf_mass, vbf_deta): + pass_tPt = [i for i in _2jets['tPt' ] if tPt > i] + pass_ePt = [i for i in _2jets['ePt' ] if ePt > i] + pass_tMtToPfMet = [i for i in _2jets['tMtToPfMet'] if tMtToPfMet < i] + pass_vbf_mass = [i for i in _2jets['vbf_mass'] if vbf_mass > i] + pass_vbf_deta = [i for i in _2jets['vbf_deta'] if vbf_deta > i] + + cuts = [pass_tPt, pass_ePt, pass_tMtToPfMet, pass_vbf_mass, pass_vbf_deta] + pass_default_tPt = tPt > _2jets_default['tPt' ] + pass_default_ePt = ePt > _2jets_default['ePt' ] + pass_default_tMtToPfMet = tMtToPfMet < _2jets_default['tMtToPfMet'] + pass_default_vbf_mass = vbf_mass > _2jets_default['vbf_mass'] + pass_default_vbf_deta = vbf_deta > _2jets_default['vbf_deta'] + + defaults = [pass_default_tPt, pass_default_ePt, pass_default_tMtToPfMet, pass_default_vbf_mass, pass_default_vbf_deta] + ret = [] + for cut_idx, opts in enumerate(cuts): + if all(j for i,j in enumerate(defaults) if i != cut_idx): + ret.extend([_2jet_region_templates[cut_idx] % i for i in opts]) + + return ret + + + +def empty(*args): + return [] +## +compute_regions_0jet = _get_0jet_regions if RUN_OPTIMIZATION else empty +compute_regions_1jet = _get_1jet_regions if RUN_OPTIMIZATION else empty +compute_regions_2jet = _get_2jet_regions if RUN_OPTIMIZATION else empty +## +ret0 = [] +defaults = [_0jets_default['tPt'], _0jets_default['ePt'], _0jets_default['dphi'], _0jets_default['tMtToPfMet']] +cuts0 = [_0jets['tPt'], _0jets['ePt'], _0jets['dphi'], _0jets['tMtToPfMet']] +cuts1 = [_1jets['tPt'], _1jets['ePt'], _1jets['tMtToPfMet']] +cuts2 = [_2jets['tPt'], _2jets['ePt'], _2jets['tMtToPfMet'], _2jets['vbf_mass'], _2jets['vbf_deta']] +for cut_idx, opts in enumerate(cuts0): + if all(j for i,j in enumerate(defaults) if i != cut_idx): + ret0.extend([_0jet_region_templates[cut_idx] % i for i in opts]) + +regions = {'0' : [], '1' : [], '2' : [], '3' : []} + +if RUN_OPTIMIZATION: + + regions = { + '0' : [_0jet_region_templates[cut_idx] % i for cut_idx, opts in enumerate(cuts0) for i in opts],#itertools.product(_0jets['tPt'], _0jets['ePt'], _0jets['dphi'], _0jets['tMtToPfMet'])], + '1' : [_1jet_region_templates[cut_idx] % i for cut_idx, opts in enumerate(cuts1) for i in opts],#[_1jet_region_template % i for i in itertools.product(_1jets['tPt'], _1jets['ePt'], _1jets['tMtToPfMet'])], + '2' : [_2jet_region_templates[cut_idx] % i for cut_idx, opts in enumerate(cuts2) for i in opts],#[_2jet_region_template % i for i in itertools.product(_2jets['tPt'], _2jets['ePt'], _2jets['tMtToPfMet'], _2jets['vbf_mass'], _2jets['vbf_deta'])], + #'0':[_0jet_region_template % i for i in itertools.product(_0jets['tPt'], [_0jets_default['ePt']], [_0jets_default['dphi']], [_0jets_default['tMtToPfMet']])] + [_0jet_region_template % i for i in itertools.product([_0jets_default['tPt']] , _0jets['ePt'], [_0jets_default['dphi']], [_0jets_default['tMtToPfMet']])], + #'1':[_1jet_region_template % i for i in itertools.product(_1jets['tPt'], [_1jets_default['ePt']],[_1jets_default['tMtToPfMet']])], + #'2':[_2jet_region_template % i for i in itertools.product(_2jets['tPt'], [_2jets_default['ePt']],[_2jets_default['tMtToPfMet']], [_2jets_default['vbf_mass']], [_2jets_default['vbf_deta']])], + '3' : []} + + print regions['0'] + +if __name__ == "__main__": + from pdb import set_trace + set_trace() + #print '\n'.join(grid_search.keys()) +else: + print "Running optimization: %s" % RUN_OPTIMIZATION diff --git a/lfvetau/plotControlEE.py b/lfvetau/plotControlEE.py new file mode 100644 index 00000000..45a75c23 --- /dev/null +++ b/lfvetau/plotControlEE.py @@ -0,0 +1,146 @@ +import rootpy.plotting.views as views +from FinalStateAnalysis.PlotTools.Plotter import Plotter +from FinalStateAnalysis.PlotTools.BlindView import BlindView +from FinalStateAnalysis.PlotTools.PoissonView import PoissonView +from FinalStateAnalysis.PlotTools.MedianView import MedianView +from FinalStateAnalysis.PlotTools.ProjectionView import ProjectionView +#from FinalStateAnalysis.PlotTools.FixedIntegralView import FixedIntegralView +from FinalStateAnalysis.PlotTools.RebinView import RebinView +from FinalStateAnalysis.MetaData.data_styles import data_styles, colors +from FinalStateAnalysis.PlotTools.decorators import memo +from FinalStateAnalysis.MetaData.datacommon import br_w_leptons, br_z_leptons +from optparse import OptionParser +import os +import ROOT +import glob +import math +import logging +from fnmatch import fnmatch +from yellowhiggs import xs, br, xsbr + +ROOT.gROOT.SetBatch() +ROOT.gStyle.SetOptStat(0) +jobid = os.environ['jobid'] + +print jobid +mc_samples = [ + 'ggHiggsToETau', + 'vbfHiggsToETau', + 'GluGluToHToTauTau_M-125_8TeV-powheg-pythia6', + 'VBF_HToTauTau_M-125_8TeV-powheg-pythia6', + 'Zjets_M50_skimmedLL', + 'Z1jets_M50_skimmedLL', + 'Z2jets_M50_skimmedLL', + 'Z3jets_M50_skimmedLL', + 'Z4jets_M50_skimmedLL', + 'Zjets_M50_skimmedTT', + 'Z1jets_M50_skimmedTT', + 'Z2jets_M50_skimmedTT', + 'Z3jets_M50_skimmedTT', + 'Z4jets_M50_skimmedTT', + 'TTJets*', + 'T_t*', + 'Tbar_t*', + 'WplusJets_madgraph_skimmed', + 'Wplus1Jets_madgraph', + # 'Wplus1Jets_madgraph_tapas', + 'Wplus2Jets_madgraph', + # 'Wplus2Jets_madgraph_tapas', + 'Wplus3Jets_madgraph', + 'Wplus4Jets_madgraph', + 'WWJets*', + 'WZJets*', + 'ZZJets*', + 'data*' +] + +files = [] +lumifiles = [] +channel = 'ee' +for x in mc_samples: + files.extend(glob.glob('results/%s/EEAnalyzer/%s.root' % (jobid, x))) + lumifiles.extend(glob.glob('inputs/%s/%s.lumicalc.sum' % (jobid, x))) + + + +period = '8TeV' +sqrts = 7 if '7TeV' in jobid else 8 + +def remove_name_entry(dictionary): + return dict( [ i for i in dictionary.iteritems() if i[0] != 'name'] ) + + +sign = ['os', 'ss'] + +outputdir = 'plots/%s/EEAnalyzer/%s/' % (jobid, channel) +if not os.path.exists(outputdir): + os.makedirs(outputdir) + +plotter = Plotter(files, lumifiles, outputdir) + + + + +EWKDiboson = views.StyleView( + views.SumView( + *[ plotter.get_view(regex) for regex in \ + filter(lambda x : x.startswith('WW') or x.startswith('WZ') or x.startswith('ZZ') or x.startswith('WG'), mc_samples )] + ), **remove_name_entry(data_styles['WW*'#,'WZ*', 'WG*', 'ZZ*' +]) +) +Wplus = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('Wplus'), mc_samples )]), **remove_name_entry(data_styles['Wplus*Jets*'])) +DYLL = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('skimmedLL'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*LL'])) +DYTT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('jets_M50_skimmedTT'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*TT'])) +TT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('TT') , mc_samples)]), **remove_name_entry(data_styles['TTJets*'])) +singleT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('T_') or x.startswith('Tbar_'), mc_samples)]), **remove_name_entry(data_styles['T*_t*'])) +SMH = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : 'HToTauTau' in x , mc_samples)]), **remove_name_entry(data_styles['GluGluToHToTauTau*'])) + + +plotter.views['EWKDiboson']={'view' : EWKDiboson } +plotter.views['Wplus']={'view' : Wplus } +plotter.views['DYLL']={'view' : DYLL } +plotter.views['DYTT']={'view' : DYTT } +plotter.views['singleT']={'view' : singleT } +plotter.views['SMH']={'view' : SMH } +plotter.views['TT']={'view' : TT } + + +new_mc_samples = filter( lambda x : not x.startswith('T_') and not x.startswith('Tbar_') and not x.endswith('jets_M50_skimmedLL') and not x.endswith('jets_M50_skimmedTT') and not x.startswith('Wplus') and not x.startswith('WW') and not x.startswith('WZ') and not x.startswith('ZZ') and not x.startswith('WG') and not x.endswith('HiggsToETau') and not 'HToTauTau' and not x.startswith('data') in x, mc_samples) +#new_sigsamples= filter(lambda x: x.endswith('HiggsToETau'), mc_samples) + +#print new_sigsamples +new_mc_samples.extend(['EWKDiboson', 'SMH', 'singleT','Wplus', 'DYLL', 'DYTT' , 'TT' +]) +print new_mc_samples + +histoname = ['e1Pt','e1Phi','e1Eta','e2Pt','e2Phi','e2Eta', + 'e1e2_DeltaPhi','e1e2_DeltaR', 'e1e2Mass', + 'type1_pfMetEt', 'pfMetEt', 'type1_pfMetPhi','pfMetPhi', 'mvaMetEt', 'mvaMetPhi', + 'pfMetEt_par','pfMetEt_perp', 'type1_pfMetEt_par','type1_pfMetEt_perp', 'mvaMetEt_par','mvaMetEt_perp', + 'e1PFMET_DeltaPhi','e1PFMET_Mt','e1MVAMET_DeltaPhi','e1MVAMET_Mt','e2PFMET_DeltaPhi','e2PFMET_Mt','e2MVAMET_DeltaPhi','e2MVAMET_Mt'] +axistitle = ['e1 p_{T} (GeV)','e1 #phi','e1 #eta', 'e2 p_{T} (GeV)','e2 #phi','e2 #eta','e1-e2 #Delta#phi','e1-e2 #DeltaR', 'e1-e2 Inv Mass (GeV)', 'type1PF MET (GeV)', 'PF MET (GeV)', 'type1 PF MET #phi', 'PF MET #phi', 'MVA MET (GeV)', 'MVA MET #phi', 'PF MET parallel (GeV)', 'PF MET perpendicular (GeV)', 'type1PF MET parallel (GeV)', 'type1PF MET perpendicular (GeV)', 'MVA MET parallel (GeV)', 'MVA MET perpendicular (GeV)', +'e1-type1PFMET #Delta#phi','e1-type1PFMET M_{T} (GeV) ','e1-MVAMET #Delta#phi','e1-MVAMET M_{T} (GeV)','e2-type1PFMET #Delta#phi','e2-type1PFMET M_{T} (GeV)','e2-MVAMET #Delta#phi','e2-MVAMET #M_{T} (GeV)'] + +#rebins = [5, 5, 2, 5, 5, 2, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 5, 1, 5, 1, 5, 1, 5] +rebins = [] +for n in histoname : + rebins.append(1) + + +plotter.mc_samples = new_mc_samples +#plotter.mc_samples = mc_samples +for i in sign : + for n,h in enumerate(histoname) : + foldername = i + #plotter.pad.SetLogy(True) + +# plotter.plot_mc(foldername, ['ggHiggsToETau','vbfHiggsToETau'],h, rebin=rebins[n], xaxis= axistitle[n], leftside=False, show_ratio=False, ratio_range=1.5, rescale=10) + plotter.plot_mc_vs_data(foldername,h, rebin=rebins[n], xaxis= axistitle[n], leftside=False, show_ratio=True, ratio_range=1.5, sort=True) + + + if not os.path.exists(outputdir+foldername): + os.makedirs(outputdir+foldername) + + plotter.save(foldername+'/'+h) + + diff --git a/lfvetau/plotControlEE_MVA.py b/lfvetau/plotControlEE_MVA.py new file mode 100644 index 00000000..d023c28d --- /dev/null +++ b/lfvetau/plotControlEE_MVA.py @@ -0,0 +1,204 @@ +import rootpy.plotting.views as views +from FinalStateAnalysis.PlotTools.Plotter import Plotter +from FinalStateAnalysis.PlotTools.BlindView import BlindView +from FinalStateAnalysis.PlotTools.PoissonView import PoissonView +from FinalStateAnalysis.PlotTools.MedianView import MedianView +from FinalStateAnalysis.PlotTools.ProjectionView import ProjectionView +#from FinalStateAnalysis.PlotTools.FixedIntegralView import FixedIntegralView +from FinalStateAnalysis.PlotTools.RebinView import RebinView +from FinalStateAnalysis.MetaData.data_styles import data_styles, colors +from FinalStateAnalysis.PlotTools.decorators import memo +from FinalStateAnalysis.MetaData.datacommon import br_w_leptons, br_z_leptons +from FinalStateAnalysis.PlotTools.SubtractionView import SubtractionView, PositiveView +from optparse import OptionParser +import os +import ROOT +import glob +import math +import logging +import pdb +import array +from fnmatch import fnmatch +from yellowhiggs import xs, br, xsbr + +from BasePlotter import BasePlotter + + +ROOT.gROOT.SetBatch() +ROOT.gStyle.SetOptStat(0) +jobid = os.environ['jobid'] + +print jobid +mc_samples = [ + 'ggHiggsToETau', + 'vbfHiggsToETau', + 'GluGluToHToTauTau_M-125_8TeV-powheg-pythia6', + 'VBF_HToTauTau_M-125_8TeV-powheg-pythia6', + #'Zjets_M50', + 'Zjets_M50_skimmedLL', + 'Z1jets_M50_skimmedLL', + 'Z2jets_M50_skimmedLL', + 'Z3jets_M50_skimmedLL', + 'Z4jets_M50_skimmedLL', + 'Zjets_M50_skimmedTT', + 'Z1jets_M50_skimmedTT', + 'Z2jets_M50_skimmedTT', + 'Z3jets_M50_skimmedTT', + 'Z4jets_M50_skimmedTT', + 'TTJets*', + 'T_t*', + 'Tbar_t*', + 'WplusJets_madgraph_skimmed', + 'Wplus1Jets_madgraph', + # 'Wplus1Jets_madgraph_tapas', + 'Wplus2Jets_madgraph', + # 'Wplus2Jets_madgraph_tapas', + 'Wplus3Jets_madgraph', + 'Wplus4Jets_madgraph', + 'WWJets*', + 'WZJets*', + 'ZZJets*', + 'data*' +] + +files = [] +lumifiles = [] +channel = 'ee' +for x in mc_samples: +# files.extend(glob.glob('results/%s/EEAnalyzerMVA/%s.root' % (jobid, x))) + files.extend(glob.glob('results/%s/EEAnalyzerMVA_noTrCorr/%s.root' % (jobid, x))) + lumifiles.extend(glob.glob('inputs/%s/%s.lumicalc.sum' % (jobid, x))) + + + +period = '8TeV' +sqrts = 7 if '7TeV' in jobid else 8 + +def remove_name_entry(dictionary): + return dict( [ i for i in dictionary.iteritems() if i[0] != 'name'] ) + + +sign = ['os'] +jets = [0, 1, 2, 3] +#outputdir = 'plots/%s/EEAnalyzerMVA/%s/' % (jobid, channel) +outputdir = 'plots/%s/EEAnalyzerMVA_noTrCorr/%s/' % (jobid, channel) +if not os.path.exists(outputdir): + os.makedirs(outputdir) + +plotter = BasePlotter(files, lumifiles, outputdir) + + + + +EWKDiboson = views.StyleView( + views.SumView( + *[ plotter.get_view(regex) for regex in \ + filter(lambda x : x.startswith('WW') or x.startswith('WZ') or x.startswith('ZZ') or x.startswith('WG'), mc_samples )] + ), **remove_name_entry(data_styles['WW*'#,'WZ*', 'WG*', 'ZZ*' +]) +) +Wplus = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('Wplus'), mc_samples )]), **remove_name_entry(data_styles['Wplus*Jets*'])) +DYLL = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('skimmedLL'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*LL'])) +DYTT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('jets_M50_skimmedTT'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*TT'])) +TT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('TT') , mc_samples)]), **remove_name_entry(data_styles['TTJets*'])) +singleT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('T_') or x.startswith('Tbar_'), mc_samples)]), **remove_name_entry(data_styles['T*_t*'])) +SMH = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : 'HToTauTau' in x , mc_samples)]), **remove_name_entry(data_styles['GluGluToHToTauTau*'])) + +plotter.views['EWKDiboson']={'view' : EWKDiboson } +plotter.views['Wplus']={'view' : Wplus } +plotter.views['DYLL']={'view' : DYLL } +plotter.views['DYTT']={'view' : DYTT } +plotter.views['singleT']={'view' : singleT } +plotter.views['SMH']={'view' : SMH } +plotter.views['TT']={'view' : TT } +#plotter.views['DY']={'view' : DY } +new_mc_samples =[] + +new_mc_samples.extend(['EWKDiboson', 'SMH', 'singleT','Wplus', 'TT', + 'DYLL', 'DYTT' +]) +print new_mc_samples + +binx = array.array('l', [5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,100, 120, 140, 160,200 ]) + +histoname = [('e1Pt', 'e1 p_{T} (GeV)', 5), ('e1Phi','e1 #phi',5) ,('e1Eta','e1 #eta',2), + ('e2Pt','e2 p_{T} (GeV)',5) ,('e2Phi','e2 #phi',5), ('e2Eta','e2 #eta',2), + ('e1e2_DeltaPhi','e1-e2 #Delta#phi',2), ('e1e2_DeltaR','e1-e2 #DeltaR',2), ('e1e2Mass', 'e1-e2 Inv Mass (GeV)',5), + ('type1_pfMetEt', 'type1PF MET (GeV)', 2) , ('pfMetEt', 'PF MET (GeV)', 2), + ('type1_pfMetPhi','type1 PF MET #phi', 5), ('pfMetPhi','PF MET #phi',5), + ('pfMetEt_par','PF MET parallel (GeV)',2), ('pfMetEt_perp','PF MET perpendicular (GeV)',2), + ('type1_pfMetEt_par', 'type1PF MET parallel (GeV)', 2),('type1_pfMetEt_perp', 'type1PF MET perpendicular (GeV)',2), + ('e1PFMET_DeltaPhi','e1-type1PFMET #Delta#phi',2), ('e1PFMET_Mt','e1-type1PFMET M_{T} (GeV) ',5), + ('e2PFMET_DeltaPhi','e2-type1PFMET #Delta#phi',2),('e2PFMET_Mt','e2-type1PFMET M_{T} (GeV)',5), + ('nPV_unweighted', 'unweighted N of vertices', 1), ('nPV', 'number of vertices', 1) +] + + +plotter.mc_samples = new_mc_samples +foldernames = [] +for i in sign : + foldernames.append(i) + for j in jets : + foldernames.append(i+'/'+str(int(j))) + +def create_mapper(mapping): + def _f(path): + for key, out in mapping.iteritems(): + if key == path: + path = path.replace(key,out) + print 'path', path + return path + return _f + +def get_ss(x): + return x.replace('os/', 'ss/') + +print foldernames +mymapper = {"os/": "ss/", + "os/0/": "ss/0/", + "os/1": "ss/1", + "os/2": "ss/2", + "os/3": "ss/3" +} +QCD = views.TitleView(views.StyleView(SubtractionView( + views.PathModifierView( plotter.data, get_ss), + views.PathModifierView( TT, get_ss), + views.PathModifierView( singleT, get_ss), + views.PathModifierView( SMH, get_ss ), + views.PathModifierView( DYTT, get_ss ), + views.PathModifierView( DYLL, get_ss ), + views.PathModifierView( Wplus, get_ss ), + views.PathModifierView( EWKDiboson, get_ss )) + ,**data_styles['QCD*']), 'QCD') + + +plotter.views['QCD']= {'view': QCD} +plotter.mc_samples.extend(['QCD']) + + +for foldername in foldernames: + if foldername.startswith("ss") and bool('QCD' in plotter.mc_samples)==True: + plotter.mc_samples.remove('QCD') + if foldername.startswith("os") and bool('QCD' in plotter.mc_samples)==False: + plotter.mc_samples.extend(['QCD']) + + for n,h in enumerate(histoname) : + + + plotter.pad.SetLogy(True) + #plotter.plot('QCD', foldername+'/'+h[0], 'hist') + print foldername + #plotter.plot_mc_vs_data(foldername, h[0], rebin=h[2], xaxis=h[1], leftside=False, show_ratio=True, ratio_range=0.2, sorted=True) + + plotter.plot_with_bkg_uncert(foldername,h[0], rebin=h[2], xaxis=h[1], leftside=False, show_ratio=True, ratio_range=0.5, sort=True, obj1='e1', obj2='e2') + + + + + if not os.path.exists(outputdir+foldername): + os.makedirs(outputdir+foldername) + + plotter.save(foldername+'/'+h[0]) + + + diff --git a/lfvetau/plotControlEE_MVAcorrected.py b/lfvetau/plotControlEE_MVAcorrected.py new file mode 100644 index 00000000..3ee27075 --- /dev/null +++ b/lfvetau/plotControlEE_MVAcorrected.py @@ -0,0 +1,203 @@ +import rootpy.plotting.views as views +from FinalStateAnalysis.PlotTools.Plotter import Plotter +from FinalStateAnalysis.PlotTools.BlindView import BlindView +from FinalStateAnalysis.PlotTools.PoissonView import PoissonView +from FinalStateAnalysis.PlotTools.MedianView import MedianView +from FinalStateAnalysis.PlotTools.ProjectionView import ProjectionView +#from FinalStateAnalysis.PlotTools.FixedIntegralView import FixedIntegralView +from FinalStateAnalysis.PlotTools.RebinView import RebinView +from FinalStateAnalysis.MetaData.data_styles import data_styles, colors +from FinalStateAnalysis.PlotTools.decorators import memo +from FinalStateAnalysis.MetaData.datacommon import br_w_leptons, br_z_leptons +from FinalStateAnalysis.PlotTools.SubtractionView import SubtractionView, PositiveView +from optparse import OptionParser +import os +import ROOT +import glob +import math +import logging +import pdb +import array +from fnmatch import fnmatch +from yellowhiggs import xs, br, xsbr + +from BasePlotter import BasePlotter + + +ROOT.gROOT.SetBatch() +ROOT.gStyle.SetOptStat(0) +jobid = os.environ['jobid'] + +print jobid +mc_samples = [ + 'ggHiggsToETau', + 'vbfHiggsToETau', + 'GluGluToHToTauTau_M-125_8TeV-powheg-pythia6', + 'VBF_HToTauTau_M-125_8TeV-powheg-pythia6', + #'Zjets_M50', + 'Zjets_M50_skimmedLL', + 'Z1jets_M50_skimmedLL', + 'Z2jets_M50_skimmedLL', + 'Z3jets_M50_skimmedLL', + 'Z4jets_M50_skimmedLL', + 'Zjets_M50_skimmedTT', + 'Z1jets_M50_skimmedTT', + 'Z2jets_M50_skimmedTT', + 'Z3jets_M50_skimmedTT', + 'Z4jets_M50_skimmedTT', + 'TTJets*', + 'T_t*', + 'Tbar_t*', + 'WplusJets_madgraph_skimmed', + 'Wplus1Jets_madgraph', + # 'Wplus1Jets_madgraph_tapas', + 'Wplus2Jets_madgraph', + # 'Wplus2Jets_madgraph_tapas', + 'Wplus3Jets_madgraph', + 'Wplus4Jets_madgraph', + 'WWJets*', + 'WZJets*', + 'ZZJets*', + 'data*' +] + +files = [] +lumifiles = [] +channel = 'ee' +for x in mc_samples: + files.extend(glob.glob('results/%s/EEAnalyzerMVA/%s.root' % (jobid, x))) + lumifiles.extend(glob.glob('inputs/%s/%s.lumicalc.sum' % (jobid, x))) + + + +period = '8TeV' +sqrts = 7 if '7TeV' in jobid else 8 + +def remove_name_entry(dictionary): + return dict( [ i for i in dictionary.iteritems() if i[0] != 'name'] ) + + +sign = ['os'] +jets = [0, 1, 2, 3] +outputdir = 'plots/%s/EEAnalyzerMVA/%s/' % (jobid, channel) +if not os.path.exists(outputdir): + os.makedirs(outputdir) + +plotter = BasePlotter(files, lumifiles, outputdir) + + + + +EWKDiboson = views.StyleView( + views.SumView( + *[ plotter.get_view(regex) for regex in \ + filter(lambda x : x.startswith('WW') or x.startswith('WZ') or x.startswith('ZZ') or x.startswith('WG'), mc_samples )] + ), **remove_name_entry(data_styles['WW*'#,'WZ*', 'WG*', 'ZZ*' +]) +) +Wplus = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('Wplus'), mc_samples )]), **remove_name_entry(data_styles['Wplus*Jets*'])) +DYLL = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('skimmedLL'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*LL'])) +DYTT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('jets_M50_skimmedTT'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*TT'])) +TT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('TT') , mc_samples)]), **remove_name_entry(data_styles['TTJets*'])) +singleT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('T_') or x.startswith('Tbar_'), mc_samples)]), **remove_name_entry(data_styles['T*_t*'])) +SMH = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : 'HToTauTau' in x , mc_samples)]), **remove_name_entry(data_styles['GluGluToHToTauTau*'])) + +plotter.views['EWKDiboson']={'view' : EWKDiboson } +plotter.views['Wplus']={'view' : Wplus } +plotter.views['DYLL']={'view' : DYLL } +plotter.views['DYTT']={'view' : DYTT } +plotter.views['singleT']={'view' : singleT } +plotter.views['SMH']={'view' : SMH } +plotter.views['TT']={'view' : TT } +#plotter.views['DY']={'view' : DY } +new_mc_samples =[] + +new_mc_samples.extend(['EWKDiboson', 'SMH', 'singleT','Wplus', 'TT', + 'DYLL', 'DYTT' +]) +print new_mc_samples + +binx = array.array('l', [5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,100, 120, 140, 160,200 ]) + +histoname = [('e1Pt', 'e1 p_{T} (GeV)', 5), ('e1Phi','e1 #phi',5) ,('e1Eta','e1 #eta',2), + ('e2Pt','e2 p_{T} (GeV)',5) ,('e2Phi','e2 #phi',5), ('e2Eta','e2 #eta',2), + ('e1e2_DeltaPhi','e1-e2 #Delta#phi',2), ('e1e2_DeltaR','e1-e2 #DeltaR',2), ('e1e2Mass', 'e1-e2 Inv Mass (GeV)',5), + ('type1_pfMetEt', 'type1PF MET (GeV)', 2) , ('pfMetEt', 'PF MET (GeV)', 2), + ('type1_pfMetPhi','type1 PF MET #phi', 5), ('pfMetPhi','PF MET #phi',5), + ('pfMetEt_par','PF MET parallel (GeV)',2), ('pfMetEt_perp','PF MET perpendicular (GeV)',2), + ('type1_pfMetEt_par', 'type1PF MET parallel (GeV)', 2),('type1_pfMetEt_perp', 'type1PF MET perpendicular (GeV)',2), + ('e1PFMET_DeltaPhi','e1-type1PFMET #Delta#phi',2), ('e1PFMET_Mt','e1-type1PFMET M_{T} (GeV) ',5), + ('e2PFMET_DeltaPhi','e2-type1PFMET #Delta#phi',2),('e2PFMET_Mt','e2-type1PFMET M_{T} (GeV)',5), + #('nPV_unweighted', 'unweighted N of vertices', 1), + ('nPV', 'number of vertices', 1) +] + + +plotter.mc_samples = new_mc_samples +foldernames = [] +for i in sign : + foldernames.append(i) + for j in jets : + foldernames.append(i+'/'+str(int(j))) + +def create_mapper(mapping): + def _f(path): + for key, out in mapping.iteritems(): + if key == path: + path = path.replace(key,out) + print 'path', path + return path + return _f + +def get_ss(x): + return x.replace('os/', 'ss/') + +print foldernames +mymapper = {"os/": "ss/", + "os/0/": "ss/0/", + "os/1": "ss/1", + "os/2": "ss/2", + "os/3": "ss/3" +} +QCD = views.TitleView(views.StyleView(SubtractionView( + views.PathModifierView( plotter.data, get_ss), + views.PathModifierView( TT, get_ss), + views.PathModifierView( singleT, get_ss), + views.PathModifierView( SMH, get_ss ), + views.PathModifierView( DYTT, get_ss ), + views.PathModifierView( DYLL, get_ss ), + views.PathModifierView( Wplus, get_ss ), + views.PathModifierView( EWKDiboson, get_ss )) + ,**data_styles['QCD*']), 'QCD') + + +plotter.views['QCD']= {'view': QCD} +plotter.mc_samples.extend(['QCD']) + + +for foldername in foldernames: + if foldername.startswith("ss") and bool('QCD' in plotter.mc_samples)==True: + plotter.mc_samples.remove('QCD') + if foldername.startswith("os") and bool('QCD' in plotter.mc_samples)==False: + plotter.mc_samples.extend(['QCD']) + + for n,h in enumerate(histoname) : + + + plotter.pad.SetLogy(True) + #plotter.plot('QCD', foldername+'/'+h[0], 'hist') + print foldername + #plotter.plot_mc_vs_data(foldername, h[0], rebin=h[2], xaxis=h[1], leftside=False, show_ratio=True, ratio_range=0.2, sorted=True) + + plotter.plot_with_bkg_uncert(foldername,h[0], rebin=h[2], xaxis=h[1], leftside=False, show_ratio=True, ratio_range=0.5, sort=True, obj1='e1', obj2='e2') + + + + + if not os.path.exists(outputdir+foldername): + os.makedirs(outputdir+foldername) + + plotter.save(foldername+'/'+h[0]) + + + diff --git a/lfvetau/plotControlFakeMVA.py b/lfvetau/plotControlFakeMVA.py new file mode 100644 index 00000000..ea69bc41 --- /dev/null +++ b/lfvetau/plotControlFakeMVA.py @@ -0,0 +1,171 @@ +#from mauro plotters +import os +from sys import argv, stdout, stderr +import ROOT +import sys +from FinalStateAnalysis.PlotTools.MegaBase import make_dirs +from FinalStateAnalysis.MetaData.data_styles import data_styles +from FinalStateAnalysis.PlotTools.BlindView import BlindView, blind_in_range +import glob +import logging +import sys +from BasePlotter import BasePlotter + +jobid = os.environ['jobid'] +#jobid = 'MCntuples_3March' +channel = 'et' +import rootpy.plotting.views as views + +ROOT.gROOT.SetStyle("Plain") +ROOT.gROOT.SetBatch(True) +ROOT.gStyle.SetOptStat(0) + +print "\nPlotting %s for %s\n" % (channel, jobid) + + + +mc_samples = [ + 'ggHiggsToETau', + 'vbfHiggsToETau', + 'GluGluToHToTauTau_M-125_8TeV-powheg-pythia6', + 'VBF_HToTauTau_M-125_8TeV-powheg-pythia6', + #'Zjets_M50', + 'Zjets_M50_skimmedLL', + 'Z1jets_M50_skimmedLL', + 'Z2jets_M50_skimmedLL', + 'Z3jets_M50_skimmedLL', + 'Z4jets_M50_skimmedLL', + 'Zjets_M50_skimmedTT', + 'Z1jets_M50_skimmedTT', + 'Z2jets_M50_skimmedTT', + 'Z3jets_M50_skimmedTT', + 'Z4jets_M50_skimmedTT', + 'TTJets*', + 'T_t*', + 'Tbar_t*', + #'WplusJets_madgraph_skimmed', + 'Wplus1Jets_madgraph*', + #'Wplus1Jets_madgraph_tapas', + 'Wplus2Jets_madgraph*', + #'Wplus2Jets_madgraph_tapas', + 'Wplus3Jets_madgraph', + 'Wplus4Jets_madgraph', + 'WWJets*', + 'WZJets*', + 'ZZJets*', + 'Fake*', + 'data*' +] + + +files = [] +lumifiles = [] + +for x in mc_samples: + files.extend(glob.glob('results/%s/LFVHETauAnalyzerMVA/%s.root' % (jobid, x))) + lumifiles.extend(glob.glob('inputs/%s/%s.lumicalc.sum' % (jobid, x))) + +sign = ['ss'] +jets = [0, 1, 2, 3] +processtype=['gg'] +threshold=['ept30'] + +outputdir = 'plots/%s/ControlFakeTau/%s/' % (jobid, channel) +if not os.path.exists(outputdir): + os.makedirs(outputdir) + + + +def remove_name_entry(dictionary): + return dict( [ i for i in dictionary.iteritems() if i[0] != 'name'] ) + + +plotter = BasePlotter(channel,files, lumifiles, outputdir) + +EWKDiboson = views.StyleView( + views.SumView( + *[ plotter.get_view(regex) for regex in \ + filter(lambda x : x.startswith('WW') or x.startswith('WZ') or x.startswith('ZZ') or x.startswith('WG'), mc_samples )] + ), **remove_name_entry(data_styles['WW*'#,'WZ*', 'WG*', 'ZZ*' +]) +) +Wplus = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('Wplus'), mc_samples )]), **remove_name_entry(data_styles['Wplus*Jets*'])) +DYLL = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('skimmedLL'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*LL'])) +DYTT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('jets_M50_skimmedTT'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*TT'])) +TT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('TT') , mc_samples)]), **remove_name_entry(data_styles['TTJets*'])) +singleT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('T_') or x.startswith('Tbar_'), mc_samples)]), **remove_name_entry(data_styles['T*_t*'])) +SMH = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : 'HToTauTau' in x , mc_samples)]), **remove_name_entry(data_styles['GluGluToHToTauTau*'])) + + +plotter.views['EWKDiboson']={'view' : EWKDiboson } +plotter.views['Wplus']={'view' : Wplus } +plotter.views['DYLL']={'view' : DYLL } +plotter.views['DYTT']={'view' : DYTT } +plotter.views['singleT']={'view' : singleT } +plotter.views['SMH']={'view' : SMH } +plotter.views['TT']={'view' : TT } +#plotter.views['DY']={'view' : DY } +new_mc_samples =[] + +new_mc_samples.extend(['EWKDiboson', + 'SMH', 'singleT',#'Wplus', + 'TT'#, + #'DYLL', #'DYTT' +]) +def get_fakeTaus(x): + y=x + if x.startswith('os') or x.startswith('ss'): + y = x.replace('.*s/', 'tLoose/*s/') + + return y + +print mc_samples + +#myFake = views.TitleView(views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('data'), mc_samples)]),**remove_name_entry(data_styles['Fakes*'])), 'Fakes') + +Fakes = views.SubdirectoryView(views.TitleView(views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : 'Fake*' in x, mc_samples)]), **remove_name_entry(data_styles['Fakes*'])),'Fakes'), 'tLoose/') + + +plotter.views['Fakes']= {'view': Fakes} +new_mc_samples.extend(['Fakes']) + +print new_mc_samples + +histoname = [('tPt', 'p_T(#tau) (GeV)', 5), ('tEta', '#eta(#tau)', 2), ('tPhi', '#phi(#tau)', 5), + ('ePt', 'p_T(e) (GeV)', 5), ('eEta', '#eta(e)', 2), ('ePhi', '#phi(e)', 5), + ('et_DeltaPhi', 'e#tau #Delta#phi', 1.), ('et_DeltaR', 'e#tau #Delta{R}', 1.), + ('h_collmass_pfmet', 'M_{coll}(e#tau) (GeV)', 1.), ('h_vismass', 'M_{vis} (GeV)', 1.), + ('jetN_30', 'number of jets (p_T > 30 GeV)', 1.) ] + + +plotter.mc_samples = new_mc_samples +foldernames = [] +for i in sign: + for j in processtype: + for k in threshold: + #foldernames.append(i+'/'+j+'/'+k) + for jn in jets: + + foldernames.append(i+'/'+j+'/'+k +'/'+str(jn)) + #foldernames.append(i+'/'+j+'/'+k +'/'+str(jn)+'/selected') + + + +for foldername in foldernames: + for n,h in enumerate(histoname) : + + + plotter.pad.SetLogy(False) + #print foldername + + plotter.plot_with_bkg_uncert(foldername,h[0], rebin=int(h[2]), xaxis=h[1], leftside=False, show_ratio=True, ratio_range=1., sort=True, obj=['e']) + #plotter.plot_mc_vs_data(foldername, h[0], rebin=int(h[2]), xaxis=h[1], leftside=False, show_ratio=False, ratio_range=0.2) + + + + if not os.path.exists(outputdir+foldername): + os.makedirs(outputdir+foldername) + + plotter.save(foldername+'/'+h[0]) + + diff --git a/lfvetau/plotEleFakeRate.py b/lfvetau/plotEleFakeRate.py new file mode 100644 index 00000000..8f8b1b1c --- /dev/null +++ b/lfvetau/plotEleFakeRate.py @@ -0,0 +1,214 @@ +import ROOT +import math + +ROOT.gStyle.SetOptStat(0) +ROOT.gStyle.SetOptTitle(0) +ROOT.gROOT.SetBatch() + +file_data = ROOT.TFile('results/newNtuple_3Dec/efakerate_fits/e_os_eLoose_eTigh_e3AbsEta.corrected_inputs.root') + +data_uncorrected = file_data.Get('numerator_uncorr') +den_data_uncorrected = file_data.Get('denominator_uncorr') +data_corrected = file_data.Get('numerator') +den_data_corrected = file_data.Get('denominator') + +data_uncorrected.Sumw2() +data_corrected.Sumw2() + +data_uncorrected.Divide(den_data_uncorrected) +data_corrected.Divide(den_data_corrected) + +c= ROOT.TCanvas("c","c", 800, 1000) +c.Draw() +c.SetGridx(1) +c.SetGridy(1) + +pad1 = ROOT.TPad("pad1", "pad1", 0, 0.2, 1, 1) +pad1.Draw() +pad1.cd() +pad1.SetGridx(1) +pad1.SetGridy(1) +data_uncorrected.Draw('PE') +data_uncorrected.SetMarkerStyle(23) +data_uncorrected.GetXaxis().SetRangeUser(0, 2.2) +data_uncorrected.GetYaxis().SetRangeUser(0, 1.2) +data_uncorrected.SetMarkerColor(4) +data_uncorrected.SetLineColor(4) +data_uncorrected.GetYaxis().SetTitle("fakerate") +data_uncorrected.GetYaxis().SetTitleOffset(1.3) +data_uncorrected.GetXaxis().SetTitle("e |#eta|") +data_corrected.Draw('SAMEPE') +data_corrected.SetMarkerStyle(22) + +file_MC = ROOT.TFile('results/newNtuple_3Dec/efakerate_fits_MC/e_os_eLoose_eTigh_e3AbsEta_plot.root') + +cMC = file_MC.Get('asdf') +dataMC = cMC.GetPrimitive('hxy_xy_data') + +pad1.cd() +dataMC.Draw('SAMEP') +#dataMC.GetYaxis().SetRangeUser(0,1) + + +dataMC.SetMarkerStyle(20) +dataMC.SetMarkerColor(2) +dataMC.SetLineColor(2) + +leg=ROOT.TLegend(0.3,0.85,0.7,0.7) +leg.SetFillColor(0) +leg.AddEntry(data_corrected, 'data, bkg corrected', 'lp') +leg.AddEntry(data_uncorrected, 'data', 'lp') +leg.AddEntry(dataMC, 'Z+jets MC', 'lp') +leg.Draw() + +c.cd() + +pad2 = ROOT.TPad("pad2", "pad2", 0, 0.05, 1,0.2) +pad2.Draw() +pad2.cd() +pad2.SetGridx(1) +pad2.SetGridy(1) + +denom=ROOT.TH1F("denom", "denom", data_corrected.GetXaxis().GetNbins(), data_corrected.GetXaxis().GetXmin(), data_corrected.GetXaxis().GetXmax()) +for bin in range(1, dataMC.GetN()+1): + denom.Fill( dataMC.GetX()[bin-1],dataMC.GetY()[bin-1]) + x = dataMC.GetX()[bin-1] + err = math.sqrt(math.pow(data_corrected.GetBinError(bin)/dataMC.GetY()[bin-1],2)+math.pow(dataMC.GetErrorY(bin-1)/dataMC.GetY()[bin-1],2)*math.pow(data_corrected.GetBinContent(bin)/dataMC.GetY()[bin-1],2)) + + print x, data_corrected.GetBinContent(bin)/dataMC.GetY()[bin-1] , err, data_corrected.GetBinError(bin), dataMC.GetErrorY(bin-1) + denom.SetBinError(denom.FindBin(x), err) + +ratio= data_corrected.Clone() +ratio.Divide(denom) +ratio.SetMarkerStyle(22) +ratio.Draw('PE1') +ratio.GetXaxis().SetRangeUser(0,2.2) +ratio.GetYaxis().SetTitle('data/mc') +ratio.GetYaxis().SetRangeUser(0,5) +ratio.GetYaxis().SetTitleSize(0.1) +ratio.GetYaxis().SetLabelSize(.1) +ratio.GetXaxis().SetTitleSize(.1) +ratio.GetXaxis().SetLabelSize(.1) +ratio.GetYaxis().SetNdivisions(504, False) +ratio.GetYaxis().SetTitleOffset(0.3) + +myline = ROOT.TLine(0.05, 1, 2.29, 1) +myline.SetLineColor(2) +myline.SetLineWidth(2) +myline.SetLineStyle(2) +myline.Draw() + + +c.Update() +c.SaveAs('eFakerateComparison_Eta.pdf') + + +file_data.Close() +file_MC.Close() + +file_data = ROOT.TFile('results/newNtuple_3Dec/efakerate_fits/e_os_eLoose_eTigh_e3Pt.corrected_inputs.root') + +data_uncorrected = file_data.Get('numerator_uncorr') +den_data_uncorrected = file_data.Get('denominator_uncorr') +data_corrected = file_data.Get('numerator') +den_data_corrected = file_data.Get('denominator') + +data_uncorrected.Sumw2() +data_corrected.Sumw2() + +data_uncorrected.Divide(den_data_uncorrected) +data_corrected.Divide(den_data_corrected) + +c= ROOT.TCanvas("c","c", 800, 1000) +c.Draw() +c.SetGridx(1) +c.SetGridy(1) +pad1 = ROOT.TPad("pad1", "pad1", 0, 0.2, 1, 1) +pad1.Draw() +pad1.cd() +pad1.SetGridx(1) +pad1.SetGridy(1) +pad1.cd() +data_uncorrected.Draw('SAMEPE') +data_uncorrected.GetXaxis().SetRangeUser(0, 2.3) +data_uncorrected.GetYaxis().SetRangeUser(0, 1.2) +data_uncorrected.GetYaxis().SetTitle("fakerate") +data_uncorrected.GetYaxis().SetTitleOffset(1.3) +data_uncorrected.GetXaxis().SetTitle("e p_{T} (GeV)") +data_uncorrected.SetMarkerStyle(23) +data_uncorrected.SetMarkerColor(4) +data_uncorrected.SetLineColor(4) +data_corrected.Draw('PE') +data_corrected.SetMarkerStyle(22) + +file_MC = ROOT.TFile('results/newNtuple_3Dec/efakerate_fits_MC/e_os_eLoose_eTigh_e3Pt_plot.root') + +cMC = file_MC.Get('asdf') +dataMC = cMC.GetPrimitive('hxy_xy_data') + +pad1.cd() +dataMC.Draw('SAMEP') +#dataMC.GetYaxis().SetRangeUser(0,1) + + +dataMC.SetMarkerStyle(20) +dataMC.SetMarkerColor(2) +dataMC.SetLineColor(2) + +leg=ROOT.TLegend(0.3,0.85,0.7,0.7) +leg.SetFillColor(0) +leg.AddEntry(data_corrected, 'data, bkg corrected', 'lp') +leg.AddEntry(data_uncorrected, 'data', 'lp') +leg.AddEntry(dataMC, 'Z+jets MC', 'lp') +leg.Draw() +c.cd() + +pad2 = ROOT.TPad("pad2", "pad2", 0, 0.05, 1,0.2) +pad2.Draw() +pad2.cd() +pad2.SetGridx(1) +pad2.SetGridy(1) + +halfbinwidths=[5,5,5,5,5,10,10,15,24.7] + +denom=ROOT.TGraphErrors(dataMC.GetN()) + +for ibin in range(1, dataMC.GetN()+1): + + denom.SetPoint(ibin-1, dataMC.GetX()[ibin-1],data_corrected.GetBinContent(ibin)/dataMC.GetY()[ibin-1]) + x = dataMC.GetX()[ibin-1] + err = math.sqrt(math.pow(data_corrected.GetBinError(ibin)/dataMC.GetY()[ibin-1],2)+math.pow(dataMC.GetErrorY(ibin-1)/dataMC.GetY()[ibin-1],2)*math.pow(data_corrected.GetBinContent(ibin)/dataMC.GetY()[ibin-1],2)) + print x, data_corrected.GetBinContent(ibin)/dataMC.GetY()[ibin-1] , err, data_corrected.GetBinError(ibin), dataMC.GetErrorY(ibin-1) + denom.SetPointError(ibin-1, halfbinwidths[ibin-1], err) + + + +ratio= denom +#ratio.Divide(denom) +ratio.SetMarkerStyle(22) +ratio.Draw('APE') +ratio.GetXaxis().SetRangeUser(30,200) +ratio.GetYaxis().SetTitle('data/mc') +ratio.GetYaxis().SetRangeUser(0,4) +ratio.GetYaxis().SetTitleSize(0.15) +ratio.GetYaxis().SetLabelSize(.15) +ratio.GetXaxis().SetTitleSize(.1) +ratio.GetXaxis().SetLabelSize(.1) +ratio.GetYaxis().SetNdivisions(504, False) + +line = ROOT.TLine(35, 1, 190, 1) +line.SetLineColor(2) +line.SetLineWidth(2) +line.SetLineStyle(2) +line.Draw() + +ratio.GetYaxis().SetTitleOffset(0.3) +pad2.Update() + +c.Update() +c.SaveAs('eFakerateComparison_Pt.pdf') + + +file_data.Close() +file_MC.Close() + diff --git a/lfvetau/plotGenQuantities.py b/lfvetau/plotGenQuantities.py new file mode 100644 index 00000000..cf9de9f9 --- /dev/null +++ b/lfvetau/plotGenQuantities.py @@ -0,0 +1,130 @@ +#from lfvmutau plotter +import os +from sys import argv, stdout, stderr +import ROOT +import sys +from FinalStateAnalysis.PlotTools.MegaBase import make_dirs + +ROOT.gROOT.SetStyle("Plain") +ROOT.gROOT.SetBatch(True) +ROOT.gStyle.SetOptStat(0) + +shape_norm = True +if shape_norm == False: + ynormlabel = "Normalized to Data " +else: + ynormlabel = "Normalized to 1 " + +canvas = ROOT.TCanvas("canvas","canvas",800,800) +LFVStack = ROOT.THStack("stack","") + +#lfvfilelist = ['results/MCntuples25Feb/LFVHAnalyzeGEN/ggHiggsToETau.root', 'results/MCntuples25Feb/LFVHAnalyzeGEN/vbfHiggsToETau.root'] +#smfilelist = ['results/MCntuples25Feb/LFVHAnalyzeGEN/GluGluToHToTauTau_M-125_8TeV-powheg-pythia6.root', 'results/MCntuples25Feb/LFVHAnalyzeGEN/VBF_HToTauTau_M-125_8TeV-powheg-pythia6.root'] + +lfvfilelist = ['results/MCntuples_otherCh2/LFVHAnalyzeGENEMu/ggHiggsToMuTau.root', 'results/MCntuples_otherCh2/LFVHAnalyzeGENEMu/vbfHiggsToMuTau.root','results/MCntuples_otherCh2/LFVHAnalyzeGENMuTau/ggHiggsToMuTau.root', 'results/MCntuples_otherCh2/LFVHAnalyzeGENMuTau/vbfHiggsToMuTau.root'] +smfilelist = ['results/MCntuples_otherCh2/LFVHAnalyzeGENEMu/GluGluToHToTauTau_M-125_8TeV-powheg-pythia6.root', 'results/MCntuples_otherCh2/LFVHAnalyzeGENEMu/VBF_HToTauTau_M-125_8TeV-powheg-pythia6.root','results/MCntuples_otherCh2/LFVHAnalyzeGENMuTau/GluGluToHToTauTau_M-125_8TeV-powheg-pythia6.root', 'results/MCntuples_otherCh2/LFVHAnalyzeGENMuTau/VBF_HToTauTau_M-125_8TeV-powheg-pythia6.root'] + +for n, file in enumerate(lfvfilelist): + + ETaufile = ROOT.TFile(file) + SMHTauTaufile = ROOT.TFile(smfilelist[n]) + + gendir = ETaufile.Get('gen') + hlist = gendir.GetListOfKeys() + + iter = ROOT.TIter(hlist) + + filepath = 'plots/'+ETaufile.GetName()[8 : len(ETaufile.GetName()) -5] + print filepath + startingdir = os.getcwd() + print startingdir + dirs = filepath.split('/') + print dirs + thechannel = 'e #tau_{h}' + for d in dirs: + currentdir = os.getcwd() + dirlist = os.listdir(currentdir) + + if d in dirlist: + os.chdir(d) + else: + os.makedirs(d) + os.chdir(d) + if d == "LFVHAnalyzeGENEMu" : + thechannel='#mu #tau_{e}' + thesmchannel = "#tau_{#mu}#tau_{e}" + if d == "LFVHAnalyzeGENMuTau" : + thechannel='#mu #tau_{h}' + thesmchannel = "#tau_{#mu}#tau_{h}" + + os.chdir(startingdir) + + for i in iter: + lfv_histo = ETaufile.Get('gen/'+i.GetName()) + sm_histo = SMHTauTaufile.Get('gen/'+i.GetName()) + + if lfv_histo.Integral() != 0 and sm_histo.Integral() != 0 : + lfv_histo.Scale(1./lfv_histo.Integral()) + sm_histo.Scale(1./sm_histo.Integral()) + + p = sm_histo.GetName()[0:1] + if p == 't' : p = '#tau' + if p == 'h' : + lfv_histo.Draw("E") + else: + + sm_histo.Draw("E") + lfv_histo.Draw("ESAME") + + lfv_histo.SetLineWidth(2) + sm_histo.SetLineColor(2) + sm_histo.SetLineWidth(2) + + + canvas.SetLogy(0) + variable='' + if sm_histo.GetName()[4:7] == "Phi" : variable = "#phi" + if sm_histo.GetName()[4:7] == "Eta" : variable = "#eta" + if sm_histo.GetName()[4:10] == "Energy" : + variable = "energy (GeV)" + canvas.SetLogy(1) + if sm_histo.GetName()[4:6] == "Pt" : + variable = "p_{T} (GeV)" + canvas.SetLogy(1) + if sm_histo.GetName()[9:17] == "DeltaPhi" : + variable = "#Delta#phi" + p = 'e#tau' + canvas.SetLogy(1) + if sm_histo.GetName() == "higgsPt" : + variable = "p_{T} (GeV)" + canvas.SetLogy(1) + axislabel = p+" "+variable + if variable != '' : lfv_histo.GetXaxis().SetTitle(axislabel) + + maxlfv = lfv_histo.GetBinContent(lfv_histo.GetMaximumBin()) + maxsm = sm_histo.GetBinContent(sm_histo.GetMaximumBin()) + if canvas.GetLogy()==0 : + if maxlfv > maxsm : + lfv_histo.GetYaxis().SetRangeUser(0, maxlfv*1.3) + else : + lfv_histo.GetYaxis().SetRangeUser(0, maxsm*1.3) + + if sm_histo.GetName()[4:13] == 'DecayMode': + legend = ROOT.TLegend(0.6,0.75,0.8,0.85) + else : + legend = ROOT.TLegend(0.38,0.15,0.62,0.25) + + legend.SetFillColor(0) + if thechannel == 'e #tau_{h}' : + legend.AddEntry(sm_histo, "H #rightarrow #tau_{e}#tau_{h} ") + legend.AddEntry(lfv_histo, "H #rightarrow e#tau_{h} ") + else: + legend.AddEntry(sm_histo, "H #rightarrow " +thesmchannel) + legend.AddEntry(lfv_histo, "H #rightarrow "+ thechannel) + + if p != 'h' : + legend.Draw() + + canvas.Update() + canvas.SaveAs(filepath+'/gen_'+i.GetName()+'.png') + diff --git a/lfvetau/plotMyReco.py b/lfvetau/plotMyReco.py new file mode 100644 index 00000000..644c2a26 --- /dev/null +++ b/lfvetau/plotMyReco.py @@ -0,0 +1,286 @@ +#from lfvmutau plotter +import os +from sys import argv, stdout, stderr +import ROOT +import sys +from FinalStateAnalysis.PlotTools.MegaBase import make_dirs +from math import sqrt + +ROOT.gROOT.SetStyle("Plain") +ROOT.gROOT.SetBatch(True) +ROOT.gStyle.SetOptStat(0) + +shape_norm = True +if shape_norm == False: + ynormlabel = "Normalized to Data " +else: + ynormlabel = "Normalized to 1 " + +#jobid = os.environ['jobid'] +jobid = 'MCntuples_14April' +import inspect + +def lineno(): + """Returns the current line number in our program.""" + return inspect.currentframe().f_back.f_lineno + +lfvfilelist = [] +legendlist = [] + +if jobid!='MCntuples_14April': + lfvfilelist = ['ggHiggsToETau','vbfHiggsToETau', + ['Zjets_M50'], + ['WplusJets_madgraph'], + ['WZJetsTo3LNu_TuneZ2_8TeV-madgraph-tauola', 'WZJetsTo2L2Q_TuneZ2star_8TeV-madgraph-tauola', 'WWJetsTo2L2Nu_TuneZ2star_8TeV-madgraph-tauola', 'ZZJetsTo4L_TuneZ2star_8TeV-madgraph-tauola','ZZJetsTo2L2Q_TuneZ2star_8TeV-madgraph-tauola'], + ['TTJetsFullLepMGDecays', 'TTJetsSemiLepMGDecays'], + ['T_t-channel_TuneZ2star_8TeV-powheg-tauola','T_tW-channel-DR_TuneZ2star_8TeV-powheg-tauola','Tbar_t-channel_TuneZ2star_8TeV-powheg-tauola','Tbar_tW-channel-DR_TuneZ2star_8TeV-powheg-tauola'], ['GluGluToHToTauTau_M-125_8TeV-powheg-pythia6', 'VBF_HToTauTau_M-125_8TeV-powheg-pythia6'] + ] + + legendlist = ['LFV GG Higgs', 'LFV VBF Higgs', 'DY + jets', 'W + jets', 'EWK Dibosons', 'ttbar', 'single Top', 'SM Higgs'] + +else: + lfvfilelist = ['ggHiggsToETau','vbfHiggsToETau', + ['Zjets_M50'], + #['Zjets_M50_skimmedLL', 'Z1jets_M50_skimmedLL', 'Z2jets_M50_skimmedLL', 'Z3jets_M50_skimmedLL'], + #['Zjets_M50_skimmedTT', 'Z1jets_M50_skimmedTT', 'Z2jets_M50_skimmedTT', 'Z3jets_M50_skimmedTT','Z4jets_M50_skimmedTT'], + ['WplusJets_madgraph_skimmed','Wplus4Jets_madgraph','Wplus2Jets_madgraph_tapas', 'Wplus2Jets_madgraph', 'Wplus1Jets_madgraph_tapas', 'Wplus3Jets_madgraph', 'Wplus1Jets_madgraph'], + ['WZJetsTo3LNu_TuneZ2_8TeV-madgraph-tauola', 'WZJetsTo2L2Q_TuneZ2star_8TeV-madgraph-tauola', 'WWJetsTo2L2Nu_TuneZ2star_8TeV-madgraph-tauola', 'ZZJetsTo4L_TuneZ2star_8TeV-madgraph-tauola','ZZJetsTo2L2Q_TuneZ2star_8TeV-madgraph-tauola'], + #['TTJetsFullLepMGDecays', 'TTJetsSemiLepMGDecays'], + ['TTJetsSemiLepMGDecays'], + ['T_t-channel_TuneZ2star_8TeV-powheg-tauola','T_tW-channel-DR_TuneZ2star_8TeV-powheg-tauola','Tbar_t-channel_TuneZ2star_8TeV-powheg-tauola','Tbar_tW-channel-DR_TuneZ2star_8TeV-powheg-tauola'], ['GluGluToHToTauTau_M-125_8TeV-powheg-pythia6', 'VBF_HToTauTau_M-125_8TeV-powheg-pythia6'] + ] + + legendlist = ['LFV GG Higgs', 'LFV VBF Higgs', 'Z/#gamma* #rightarrow ll + jets', #'Z/#gamma* #rightarrow #tau#tau + jets', + 'W + jets', 'EWK Dibosons', 'ttbar', 'single Top', 'SM Higgs'] + + + +sourcepath = 'results/'+jobid+'/LFVHETauAnalyzer/' +sign = ['os', 'ss'] +process = ['gg']#['gg','vbf'] +ptcut = [0]#[0, 40] +jetN = [0]#[0,1,2,3] +legend = ROOT.TLegend(0.58,0.15,0.82,0.35) +legend.SetFillColor(0) + + +histolist = ['tPt', 'tPhi', 'tEta', +'ePt', 'ePhi', 'eEta', +'et_DeltaPhi', 'et_DeltaR', +'tPFMET_DeltaPhi', 'tPFMET_Mt', 'tMVAMET_DeltaPhi', 'tMVAMET_Mt', +'ePFMET_DeltaPhi','ePFMET_Mt', 'eMVAMET_DeltaPhi', 'eMVAMET_Mt', +'jetN_20', 'jetN_30', +'h_collmass_pfmet', 'h_collmass_mvamet', 'h_vismass' +] +rebins = [5,5,2,5,5,2,1, 1, 2, 5, 2, 5, 2, 5, 2, 5,1,1,1,1,1] +axistitle = ['#tau p_{T} (GeV)','#tau #phi','#tau #eta', +'e p_{T} (GeV)','e #phi','e #eta', +'e-#tau #Delta#phi','e-#tau #DeltaR', +'#tau-PFMET #Delta#phi','#tau-PFMET M_{T} (GeV) ','#tau-MVAMET #Delta#phi','#tau-MVAMET M_{T} (GeV)', +'e-PFMET #Delta#phi','e-PFMET M_{T} (GeV)','e-MVAMET #Delta#phi','e-MVAMET #M_{T} (GeV)', +'Number of jets (p_{T}>20)','Number of jets (p_{T}>30)', +'M_{e#tau}coll (GeV)','M_{e#tau}coll (GeV)','M_{e#tau} vis (GeV)' +] + +scalefactors=[] +for myfile in lfvfilelist: + if type(myfile) is list: + for subfile in myfile : + lumifile = 'inputs/'+jobid+'/'+subfile[:(len(subfile))]+'.lumicalc.sum' + f = open(lumifile, 'r') + lumistring = f.readline() + #print lumistring + intlumi = float(lumistring) + f.close() + scalefactor = 19800./intlumi + scalefactors.append(scalefactor) + else : + lumifile = 'inputs/'+jobid+'/'+myfile[:(len(myfile))]+'.lumicalc.sum' + f = open(lumifile, 'r') + lumistring = f.readline() + #print lumistring + intlumi = float(lumistring) + f.close() + scalefactor = 19800./intlumi + scalefactors.append(scalefactor) +canvas = ROOT.TCanvas("canvas","canvas",800,800) +canvas.Draw() + +for s in sign : + outfile = ROOT.TFile("outfile"+s+".root", "RECREATE") + #print lineno() + for pr in process: + #print lineno() + for c in ptcut: + for jn in jetN: + #print lineno() + filepath = 'plots/'+jobid+'/LFVHETauAnalyzer/et/'+s+'/'+pr+'/ept'+str(c)+'/'+str(jn) + + startingdir = os.getcwd() + dirs = filepath.split('/') + thechannel = 'e #tau_{h}' + for d in dirs: + currentdir = os.getcwd() + dirlist = os.listdir(currentdir) + + if d in dirlist: + os.chdir(d) + else: + os.makedirs(d) + os.chdir(d) + if d == "LFVHAnalyzeGENEMu" : + thechannel='#mu #tau_{e}' + thesmchannel = "#tau_{#mu}#tau_{e}" + if d == "LFVHAnalyzeGENMuTau" : + thechannel='#mu #tau_{h}' + thesmchannel = "#tau_{#mu}#tau_{h}" + + os.chdir(startingdir) + + + for nhisto, i in enumerate(histolist): + #outfile = ROOT.TFile(filepath+'/'+i+".root","RECREATE") + outfile.cd() + legend.Clear() + LFVStack = ROOT.THStack("stack"+i,"") + histos = [] #fare vector di histo e clonare quello che leggo + ETaufiles = [] + nfile=-1 + newhistos =[] + for n, file in enumerate(lfvfilelist): + nfile+=1 + if type(file) is not list and file.endswith('HiggsToETau') : continue + #print n, " " , file + nn = n-2 + if type(file) is list : + + file0=ROOT.TFile(sourcepath+file[0]+'.root') + h0 = file0.Get(s+'/'+pr+'/ept'+str(c)+'/'+str(jn)+'/'+i).Clone() + h0.Scale(scalefactors[nfile]) + newhisto=h0.Clone() + outfile.cd() + newhistos.append(file0.Get(s+'/'+pr+'/ept'+str(c)+'/'+str(jn)+'/'+i).Clone()) + newhistos[nn].Scale(scalefactors[nfile]) + newhistos[nn].SetName('h'+str(n)) + for isub, subfile in enumerate(file) : + #print sourcepath+subfile+'.root' + if isub ==0 : continue + nfile +=1 + newFile = ROOT.TFile(sourcepath+subfile+'.root') + outfile.cd() + newhistos[nn].Add(newFile.Get(s+'/'+pr+'/ept'+str(c)+'/'+str(jn)+'/'+i).Clone(),scalefactors[nfile]) + + #print lineno() , nfile + newFile.Close() + + histos.append(newhistos[nn]) + histos[nn].Rebin(rebins[nhisto]) + histos[nn].SetLineColor(n+1) + histos[nn].SetFillColor(n+1) + histos[nn].SetMarkerColor(n+1) + # LFVStack.Add(histos[nn]) + #legend.AddEntry(histos[nn], legendlist[n]) + file0.Close() + + #else: + + #ETaufiles.append(ROOT.TFile(sourcepath+file+'.root')) + #histos.append(ETaufiles[nn].Get(s+'/'+pr+'/ept'+str(c)+'/'+str(jn)+'/'+i).Clone()) + #histos[nn].SetName('h'+str(n)) + #histos[nn].Scale(scalefactors[nfile]) + #histos[nn].Rebin(rebins[nhisto]) + #histos[nn].SetLineWidth(1) + #histos[nn].SetLineColor(n+1) + #histos[nn].SetFillColor(n+1) + #histos[nn].SetMarkerColor(n+1) + + #LFVStack.Add(histos[nn]) + #legend.AddEntry(histos[nn], legendlist[n]) + + legend.Clear() + LFVStack.Add(histos[4]) + LFVStack.Add(histos[5]) + LFVStack.Add(histos[2]) + LFVStack.Add(histos[3]) + LFVStack.Add(histos[1]) + LFVStack.Add(histos[0]) + legend.AddEntry(histos[4],legendlist[6]) + legend.AddEntry(histos[5],legendlist[7]) + legend.AddEntry(histos[2],legendlist[4]) + legend.AddEntry(histos[3],legendlist[5]) + legend.AddEntry(histos[0],legendlist[2]) + legend.AddEntry(histos[1],legendlist[3]) + # print histos + canvas.cd() + canvas.Clear() + #LFVStack.Print() + #newStack=LFVStack.Clone() + #newStack.RecursiveRemove() + #newStacl.Add(LFVStack.GetHists() ) + + LFVStack.Draw('hist') + + LFVStack.GetXaxis().SetTitle(axistitle[nhisto]) + + ggLFVHfile = ROOT.TFile(sourcepath+lfvfilelist[0]+'.root') + vbfLFVHfile = ROOT.TFile(sourcepath+lfvfilelist[1]+'.root') + ggLFVHhisto= ggLFVHfile.Get(s+'/'+pr+'/ept'+str(c)+'/'+str(jn)+'/'+i).Clone() + vbfLFVHhisto= vbfLFVHfile.Get(s+'/'+pr+'/ept'+str(c)+'/'+str(jn)+'/'+i).Clone() + ggLFVHhisto.Rebin(rebins[nhisto]) + vbfLFVHhisto.Rebin(rebins[nhisto]) + + ggLFVHhisto.SetLineColor(1) + ggLFVHhisto.SetLineStyle(1) + ggLFVHhisto.SetLineWidth(2) + vbfLFVHhisto.SetLineColor(1) + vbfLFVHhisto.SetLineStyle(2) + vbfLFVHhisto.SetLineWidth(2) + outfile.cd() + ibin=0 + snhisto = ggLFVHhisto.Clone() + + snhisto.SetName("significance_vs_"+i) + snhisto.SetTitle("significance_vs_"+i) + + while ibin<=LFVStack.GetXaxis().GetNbins(): + ibin+=1 + bkg = 0 + for h in histos: + bkg+=h.GetBinContent(ibin) + sig = ggLFVHhisto.GetBinContent(ibin) + vbfLFVHhisto.GetBinContent(ibin) + + if sig+bkg> 0: + sn = sig/sqrt(sig+bkg) + snhisto.SetBinContent(ibin, sn) + err = sqrt(sig/(2*sqrt(sig+bkg))) + snhisto.SetBinError(ibin, err) + #print bkg, sig, sn, err + snhisto.GetXaxis().SetTitle(axistitle[nhisto]) + snhisto.GetYaxis().SetTitle("S/#sqrt{S+B}") + + ggLFVHhisto.Scale(10*scalefactors[0]) + vbfLFVHhisto.Scale(10*scalefactors[1]) + mymax = max (ggLFVHhisto.GetBinContent(ggLFVHhisto.GetMaximumBin()), vbfLFVHhisto.GetBinContent(vbfLFVHhisto.GetMaximumBin())) + if mymax > LFVStack.GetMaximum() : LFVStack.SetMaximum( 1.2*mymax ) + + ggLFVHhisto.Draw('SAMEHIST') + vbfLFVHhisto.Draw('SAMEHIST') + legend.AddEntry(ggLFVHhisto, 'LFV GG Higgs x 10') + legend.AddEntry(vbfLFVHhisto, 'LFV VBF Higgs x 10') + legend.Draw() + canvas.SetLogy(0) + if i.endswith('Pt') : canvas.SetLogy(1) + canvas.Update() + canvas.SaveAs(filepath+'/mc_'+i+'.png') + #outfile.cd() + + #canvas.Clear() + snhisto.Draw() + canvas.SaveAs(filepath+'/mc_significance_vs_'+i+'.png') + #canvas.Write() + + + + outfile.Close() + os.system("rm outfile"+s+".root") + diff --git a/lfvetau/plotMyRecoMuTau.py b/lfvetau/plotMyRecoMuTau.py new file mode 100644 index 00000000..372716c6 --- /dev/null +++ b/lfvetau/plotMyRecoMuTau.py @@ -0,0 +1,245 @@ +#from lfvmutau plotter +import os +from sys import argv, stdout, stderr +import ROOT +import sys +from FinalStateAnalysis.PlotTools.MegaBase import make_dirs + +ROOT.gROOT.SetStyle("Plain") +ROOT.gROOT.SetBatch(True) +ROOT.gStyle.SetOptStat(0) + +shape_norm = True +if shape_norm == False: + ynormlabel = "Normalized to Data " +else: + ynormlabel = "Normalized to 1 " + + +import inspect + +def lineno(): + """Returns the current line number in our program.""" + return inspect.currentframe().f_back.f_lineno + + +lfvfilelist = ['ggHiggsToMuTau', 'vbfHiggsToMuTau', +'Zjets_M50', +'WplusJets_madgraph', +#'Wplus4Jets_madgraph.root', +#'Wplus2Jets_madgraph_tapas.root', +#'Wplus2Jets_madgraph.root', +#'Wplus1Jets_madgraph_tapas.root', +#'Wplus3Jets_madgraph.root', +#'Wplus1Jets_madgraph.root', +['WZJetsTo3LNu_TuneZ2_8TeV-madgraph-tauola', 'WZJetsTo2L2Q_TuneZ2star_8TeV-madgraph-tauola', 'WWJetsTo2L2Nu_TuneZ2star_8TeV-madgraph-tauola', 'ZZJetsTo4L_TuneZ2star_8TeV-madgraph-tauola','ZZJetsTo2L2Q_TuneZ2star_8TeV-madgraph-tauola'], +['TTJetsFullLepMGDecays', 'TTJetsSemiLepMGDecays'], +['T_t-channel_TuneZ2star_8TeV-powheg-tauola','T_tW-channel-DR_TuneZ2star_8TeV-powheg-tauola','Tbar_t-channel_TuneZ2star_8TeV-powheg-tauola','Tbar_tW-channel-DR_TuneZ2star_8TeV-powheg-tauola'], ['GluGluToHToTauTau_M-125_8TeV-powheg-pythia6', 'VBF_HToTauTau_M-125_8TeV-powheg-pythia6'] +] + + + +legendlist = ['LFV GG Higgs', 'LFV VBF Higgs', 'DY + jets', 'W + jets', 'EWK Dibosons', 'ttbar', 'single Top', 'SM Higgs' +] +#print 'lfvfilelist ', len(lfvfilelist), ' legendlist ' , len(legendlist) +sourcepath = 'results/MCntuples_8April/LFVHMuTauAnalyzer/' +sign = ['os', 'ss'] +process = ['gg','vbf'] +ptcut = [0, 40] +jetN = [0,1,2,3] +legend = ROOT.TLegend(0.58,0.15,0.82,0.35) +legend.SetFillColor(0) + + +histolist = ['tPt', 'tPhi', 'tEta', +'mPt', 'mPhi', 'mEta', +'mt_DeltaPhi', 'mt_DeltaR', +'tPFMET_DeltaPhi', 'tPFMET_Mt', 'tMVAMET_DeltaPhi', 'tMVAMET_Mt', +'mPFMET_DeltaPhi','mPFMET_Mt', 'mMVAMET_DeltaPhi', 'mMVAMET_Mt', +'jetN_20', 'jetN_30', +'h_collmass_pfmet', 'h_collmass_mvamet', 'h_vismass' +] +rebins = [5,5,2,5,5,2,1, 1, 2, 5, 2, 5, 2, 5, 2, 5,1,1,1,1,1] +axistitle = ['#tau p_{T} (GeV)','#tau #phi','#tau #eta', +'#mu p_{T} (GeV)','#mu #phi','#mu #eta', +'#mu-#tau #Delta#phi','#mu-#tau #DeltaR', +'#tau-PFMET #Delta#phi','#tau-PFMET M_{T} (GeV) ','#tau-MVAMET #Delta#phi','#tau-MVAMET M_{T} (GeV)', +'#mu-PFMET #Delta#phi','#mu-PFMET M_{T} (GeV)','#mu-MVAMET #Delta#phi','#mu-MVAMET #M_{T} (GeV)', +'Number of jets (p_{T}>20)','Number of jets (p_{T}>30)', +'M_{#mu#tau}coll (GeV)','M_{#mu#tau}coll (GeV)','M_{#mu#tau} vis (GeV)' +] + +scalefactors=[] +for myfile in lfvfilelist: + if type(myfile) is list: + for subfile in myfile : + lumifile = 'inputs/MCntuples_8April/'+subfile[:(len(subfile))]+'.lumicalc.sum' + f = open(lumifile, 'r') + lumistring = f.readline() + #print lumistring + intlumi = float(lumistring) + f.close() + scalefactor = 19800./intlumi + scalefactors.append(scalefactor) + else : + lumifile = 'inputs/MCntuples_8April/'+myfile[:(len(myfile))]+'.lumicalc.sum' + f = open(lumifile, 'r') + lumistring = f.readline() + #print lumistring + intlumi = float(lumistring) + f.close() + scalefactor = 19800./intlumi + scalefactors.append(scalefactor) +canvas = ROOT.TCanvas("canvas","canvas",800,800) +canvas.Draw() + +for s in sign : + #print lineno() + for pr in process: + #print lineno() + for c in ptcut: + for jn in jetN: + #print lineno() + filepath = 'plots/MCntuples_8April/LFVHETauAnalyzer/mt/'+s+'/'+pr+'/mpt'+str(c)+'/'+str(jn) + + startingdir = os.getcwd() + dirs = filepath.split('/') + thechannel = '#mu #tau_{h}' + for d in dirs: + currentdir = os.getcwd() + dirlist = os.listdir(currentdir) + + if d in dirlist: + os.chdir(d) + else: + os.makedirs(d) + os.chdir(d) + if d == "LFVHAnalyzeGENEMu" : + thechannel='#mu #tau_{e}' + thesmchannel = "#tau_{#mu}#tau_{e}" + if d == "LFVHAnalyzeGENMuTau" : + thechannel='#mu #tau_{h}' + thesmchannel = "#tau_{#mu}#tau_{h}" + + os.chdir(startingdir) + + + for nhisto, i in enumerate(histolist): + outfile = ROOT.TFile(filepath+'/'+i+".root","RECREATE") + legend.Clear() + LFVStack = ROOT.THStack("stack"+i,"") + histos = [] #fare vector di histo e clonare quello che leggo + ETaufiles = [] + nfile=-1 + newhistos =[] + for n, file in enumerate(lfvfilelist): + nfile+=1 + if type(file) is not list and file.endswith('HiggsToMuTau') : continue + #print n, " " , file + nn = n-2 + if type(file) is list : + + file0=ROOT.TFile(sourcepath+file[0]+'.root') + h0 = file0.Get(s+'/'+pr+'/mpt'+str(c)+'/'+str(jn)+'/'+i).Clone() + h0.Scale(scalefactors[nfile]) + newhisto=h0.Clone() + outfile.cd() + newhistos.append(file0.Get(s+'/'+pr+'/mpt'+str(c)+'/'+str(jn)+'/'+i).Clone()) + newhistos[n-4].Scale(scalefactors[nfile]) + + newhistos[n-4].SetName('h'+str(n)) + for isub, subfile in enumerate(file) : + #print sourcepath+subfile+'.root' + if isub ==0 : continue + nfile +=1 + newFile = ROOT.TFile(sourcepath+subfile+'.root') + outfile.cd() + newhistos[n-4].Add(newFile.Get(s+'/'+pr+'/mpt'+str(c)+'/'+str(jn)+'/'+i).Clone(),scalefactors[nfile]) + #newhistos[n-4].Scale() + #print lineno() , nfile + newFile.Close() + + histos.append(newhistos[n-4]) + histos[nn].Rebin(rebins[nhisto]) + histos[nn].SetLineColor(n+1) + histos[nn].SetFillColor(n+1) + histos[nn].SetMarkerColor(n+1) + # LFVStack.Add(histos[nn]) + #legend.AddEntry(histos[nn], legendlist[n]) + file0.Close() + + else: + + ETaufiles.append(ROOT.TFile(sourcepath+file+'.root')) + + histos.append(ETaufiles[nn].Get(s+'/'+pr+'/mpt'+str(c)+'/'+str(jn)+'/'+i).Clone()) + histos[nn].Scale(scalefactors[nfile]) + histos[nn].SetName('h'+str(n)) + + histos[nn].Rebin(rebins[nhisto]) + histos[nn].SetLineWidth(1) + histos[nn].SetLineColor(n+1) + histos[nn].SetFillColor(n+1) + histos[nn].SetMarkerColor(n+1) + + #LFVStack.Add(histos[nn]) + #legend.AddEntry(histos[nn], legendlist[n]) + + legend.Clear() + LFVStack.Add(histos[4]) + LFVStack.Add(histos[5]) + LFVStack.Add(histos[2]) + LFVStack.Add(histos[3]) + LFVStack.Add(histos[1]) + LFVStack.Add(histos[0]) + legend.AddEntry(histos[4],legendlist[6]) + legend.AddEntry(histos[5],legendlist[7]) + legend.AddEntry(histos[2],legendlist[4]) + legend.AddEntry(histos[3],legendlist[5]) + legend.AddEntry(histos[0],legendlist[2]) + legend.AddEntry(histos[1],legendlist[3]) + # print histos + canvas.cd() + canvas.Clear() + #LFVStack.Print() + #newStack=LFVStack.Clone() + #newStack.RecursiveRemove() + #newStacl.Add(LFVStack.GetHists() ) + + LFVStack.Draw('hist') + + LFVStack.GetXaxis().SetTitle(axistitle[nhisto]) + ggLFVHfile = ROOT.TFile(sourcepath+lfvfilelist[0]+'.root') + vbfLFVHfile = ROOT.TFile(sourcepath+lfvfilelist[1]+'.root') + ggLFVHhisto= ggLFVHfile.Get(s+'/'+pr+'/mpt'+str(c)+'/'+str(jn)+'/'+i).Clone() + vbfLFVHhisto= vbfLFVHfile.Get(s+'/'+pr+'/mpt'+str(c)+'/'+str(jn)+'/'+i).Clone() + ggLFVHhisto.Rebin(rebins[nhisto]) + vbfLFVHhisto.Rebin(rebins[nhisto]) + + ggLFVHhisto.SetLineColor(1) + ggLFVHhisto.SetLineStyle(1) + ggLFVHhisto.SetLineWidth(2) + vbfLFVHhisto.SetLineColor(1) + vbfLFVHhisto.SetLineStyle(2) + vbfLFVHhisto.SetLineWidth(2) + ggLFVHhisto.Scale(10*scalefactors[0]) + vbfLFVHhisto.Scale(10*scalefactors[1]) + mymax = max(ggLFVHhisto.GetBinContent(ggLFVHhisto.GetMaximumBin()), vbfLFVHhisto.GetBinContent(vbfLFVHhisto.GetMaximumBin())) + if mymax > LFVStack.GetMaximum() : + LFVStack.SetMaximum(1.2*mymax ) + print mymax, LFVStack.GetMaximum() + #LFVStack.Draw('hist') + ggLFVHhisto.Draw('SAMEHIST') + vbfLFVHhisto.Draw('SAMEHIST') + legend.AddEntry(ggLFVHhisto, 'LFV GG Higgs x 10') + legend.AddEntry(vbfLFVHhisto, 'LFV VBF Higgs x 10') + legend.Draw() + canvas.SetLogy(0) + if i.endswith('Pt') : canvas.SetLogy(1) + canvas.Update() + canvas.SaveAs(filepath+'/mc_'+i+'.png') + outfile.cd() + canvas.Write() + outfile.Close() + + os.system("rm "+filepath+'/'+i+".root") diff --git a/lfvetau/plotRecoQuantities.py b/lfvetau/plotRecoQuantities.py new file mode 100644 index 00000000..23b3f896 --- /dev/null +++ b/lfvetau/plotRecoQuantities.py @@ -0,0 +1,123 @@ +#from mauro plotters +import os +from sys import argv, stdout, stderr +import ROOT +import sys +from FinalStateAnalysis.PlotTools.MegaBase import make_dirs +import glob +import logging +import sys + +#jobid = os.environ['jobid'] +jobid = 'MCntuples_3March' +channel = 'et' + +print "\nPlotting %s for %s\n" % (channel, jobid) + +mcsamples = [ + 'ggHiggsToETau', + 'vbfHiggsToETau', + 'GluGluToHToTauTau_M-125_8TeV-powheg-pythia6', + 'VBF_HToTauTau_M-125_8TeV-powheg-pythia6', + # 'TTJets*', + # 'T_t*', + # 'Tbar_t', + # 'Wplus*', + # 'WWJets*', + # 'WZJets*', + # 'ZZJets*', + # 'Z*jets_M50' +] + +files = [] +lumifiles = [] + +for x in mcsamples: + files.extend(glob.glob('results/%s/LFVHETauAnalyzer/%s.root' % (jobid, x))) + lumifiles.extend(glob.glob('inputs/%s/%s.lumicalc.sum' % (jobid, x))) + outputdir = 'plots/%s/LFVHETauAnalyzer/%s' % (jobid, channel) + base_out_dir = outputdir + if not os.path.exists(outputdir): + os.makedirs(outputdir) + +# blinder = None +# blind = 'blind' not in os.environ or os.environ['blind'] == 'YES' +# print '\n\nRunning Blind: %s\n\n' % blind + + +#if blind: + # Don't look at the SS all pass region +# blinder = lambda x: BlindView(x, "ss/tau_os/p1p2p3/.*") + + + +import rootpy.plotting.views as views + +ROOT.gROOT.SetStyle("Plain") +ROOT.gROOT.SetBatch(True) +ROOT.gStyle.SetOptStat(0) + +canvas = ROOT.TCanvas("canvas","canvas",800,800) +LFVStack = ROOT.THStack("stack","") + +file0 = ROOT.TFile('results/%s/LFVHETauAnalyzer/ggHiggsToETau.root' % (jobid) ) + + +dir = file0.Get('os/gg/ept0') +hlist = dir.GetListOfKeys() +iter = ROOT.TIter(hlist) +for i in iter: + print i.GetName() + histo0 = file0.Get('%s/%s' % ('os/gg/ept0', i.GetName())) + if histo0.Integral() != 0 : + histo0.Scale(1./histo0.Integral()) + histo0.Draw("E") + histo0.SetLineWidth(2) + histo0.SetLineColor(1) + variable='' + if histo0.GetName()[4:7] == "Phi" : variable = "#phi" + if histo0.GetName()[4:7] == "Eta" : variable = "#eta" + if histo0.GetName()[4:10] == "Energy" : + variable = "energy (GeV)" + canvas.SetLogy(1) + if histo0.GetName()[4:6] == "Pt" : + variable = "p_{T} (GeV)" + canvas.SetLogy(1) + if histo0.GetName()[9:17] == "DeltaPhi" : + variable = "#Delta#phi" + p = 'e#tau' + canvas.SetLogy(1) +# axislabel = p+" "+variable + axislabel = variable + if variable != '' : histo0.GetXaxis().SetTitle(axislabel) + maxy = histo0.GetBinContent(histo0.GetMaximumBin()) + legend = ROOT.TLegend(0.7,0.85,0.88,0.75) + legend.SetFillColor(0) + legend.AddEntry(histo0, "ggHigsToETau") + + for n, sample in enumerate(mcsamples) : + if n == 0 : continue + file = ROOT.TFile("results/%s/LFVHETauAnalyzer/%s.root" %(jobid, sample)) + histo=file.Get('%s/%s' % ('os/gg/ept0', i.GetName())) + + + if histo.Integral() != 0 : + histo.Scale(1./histo.Integral()) + + p = histo.GetName()[0:1] + if p == 't' : p = '#tau' + histo.Draw("ESAME") + + histo.SetLineColor(n) + histo.SetLineWidth(2) + + canvas.SetLogy(0) + if maxy < histo.GetBinContent(histo.GetMaximumBin()) : maxy = histo.GetBinContent(histo.GetMaximumBin()) + legend.AddEntry(histo, "mcsamples") + + if canvas.GetLogy()==0 : + histo0.GetYaxis().SetRangeUser(0, maxy*1.3) + + canvas.Update() + canvas.SaveAs(outputdir+'/os_ept0_'+i.GetName()+'.png') + diff --git a/lfvetau/plotRecoQuantitiesMVA.py b/lfvetau/plotRecoQuantitiesMVA.py new file mode 100644 index 00000000..3b649c53 --- /dev/null +++ b/lfvetau/plotRecoQuantitiesMVA.py @@ -0,0 +1,145 @@ + +#from mauro plotters + +#Set logging before anything to override rootpy very verbose defaults +import sys +import logging +logging.basicConfig(stream=sys.stderr, level=logging.INFO) + +import os +import ROOT +from pdb import set_trace +from FinalStateAnalysis.PlotTools.MegaBase import make_dirs +from FinalStateAnalysis.MetaData.data_styles import data_styles +from FinalStateAnalysis.PlotTools.BlindView import BlindView, blind_in_range +from FinalStateAnalysis.PlotTools.SubtractionView import SubtractionView, PositiveView +import itertools +import glob +import sys +from BasePlotter import BasePlotter +from argparse import ArgumentParser + +parser = ArgumentParser(description=__doc__) +parser.add_argument('--no-plots', dest='no_plots', action='store_true', + default=False, help='Does not print plots') +parser.add_argument('--no-shapes', dest='no_shapes', action='store_true', + default=False, help='Does not create shapes for limit computation') +args = parser.parse_args() + +jobid = os.environ['jobid'] +#jobid = 'MCntuples_3March' +channel = 'et' +import rootpy.plotting.views as views + +ROOT.gROOT.SetStyle("Plain") +ROOT.gROOT.SetBatch(True) +ROOT.gStyle.SetOptStat(0) + +print "\nPlotting %s for %s\n" % (channel, jobid) + +#check if blind +blind = 'blind' not in os.environ or os.environ['blind'] == 'YES' +print 'blind?', blind +blind_region=[100, 150] if blind else None +#blind_region=[100, 200] if blind else None + +embedded = True + +plotter = BasePlotter(blind_region,use_embedded=embedded) +if not args.no_plots: + signs = ['os'] + jets = ['0', + '1', + '2' + ] + processtype = ['gg'] + threshold = ['ept30'] + + histo_info = [ + ('h_collmass_pfmet', 'M_{coll}(e#tau) (GeV)', 1)##, + ##('tPt', 'p_{T}(#tau) (GeV)', 1), + ##('tEta', '#eta(#tau)', 1), + ##('tPhi', '#phi(#tau)', 1), + ##('ePt', 'p_{T}(e) (GeV)', 1), + ##('eEta', '#eta(e)', 1), + ##('ePhi', '#phi(e)', 1), + ##('e_t_DPhi', 'e#tau #Delta#phi', 1), + ##('e_t_DR', 'e#tau #Delta R', 1), + ##('e_t_Mass', 'M_{vis} (GeV)', 1), + ##('jetVeto30', 'number of jets (p_{T} > 30 GeV)', 1) , + ##('eMtToPfMet', 'M_{T} e-PFMET', 1), + ##('tMtToPfMet', 'M_{T} #tau-PFMET', 1) , + ##('type1_pfMet_Et', 'pfMet', 1), + ##('vbfMass', 'M(j_{1},j_{2}) (GeV)', 1), + ##('vbfDeta', '#Delta#eta (j_{1}, j_{2})', 1) + + ] + + logging.debug("Starting plotting") + for sign, proc, thr, njet in itertools.product(signs, processtype, threshold, jets): + path = os.path.join(sign, proc, thr, njet) + + plotter.set_subdir(os.path.join('embedded',path)) if embedded else plotter.set_subdir(path) + + for var, xlabel, rebin in histo_info: + logging.debug("Plotting %s/%s" % (path, var) ) + plotter.pad.SetLogy(False) + ## if int(njet)==2: + ## if 'collmass' in var or 'Mass' in var: + ## rebin=rebin + ## elif not 'Eta' in var and not 'jet' in var: + ## rebin = rebin*2 + plotter.plot_with_bkg_uncert(path, var, rebin, xlabel, + leftside=False, show_ratio=True, ratio_range=1., + sort=True, obj=['e'], plot_data=True) + + + plotter.save(var,dotroot=True) + + plotter.set_subdir(os.path.join('embedded', path+'/selected'))if embedded else plotter.set_subdir(path+'/selected') + + for var, xlabel, rebin in histo_info: + ##if int(njet)==1: + ## if not 'Eta' in var and not 'jet' in var: rebin = rebin + ##if int(njet) ==2: + ## if 'collmass' in var or 'Mass' in var: rebin=rebin + ## if 'Pt' in var or 'Mt' in var or 'pfMet' in var : rebin=rebin*4 + + logging.debug("Plotting %s/%s" % (path, var) ) + plotter.pad.SetLogy(False) + plotter.plot_with_bkg_uncert(path+'/selected', var, rebin, xlabel, + leftside=False, show_ratio=True, ratio_range=1., + sort=True, obj=['e'], plot_data=True) + + + plotter.save(var,dotroot=True) + +#make shapes for limit setting +if not args.no_shapes: + signal_region = 'os/gg/ept30/%s/selected' + ##signal_region = 'os/gg/ept30/%s' + jets_names = [ + ('0', 'gg0etau' , 1), + ('1', 'boostetau', 1),#was 2 + ('2', 'vbfetau' , 1),#was 5 + ] + pjoin = os.path.join + for njets, cat_name, rebin in jets_names: + output_path = plotter.base_out_dir + tfile = ROOT.TFile(pjoin(output_path, 'shapes.%s.root' % njets), 'recreate') + output_dir = tfile.mkdir(cat_name) + unc_conf_lines, unc_vals_lines = plotter.write_shapes( + signal_region % njets, 'h_collmass_pfmet', output_dir, rebin=rebin, + br_strenght=1, last=300) + logging.warning('shape file %s created' % tfile.GetName()) + tfile.Close() + with open(pjoin(output_path, 'unc.%s.conf' % njets), 'w') as conf: + conf.write('\n'.join(unc_conf_lines)) + with open(pjoin(output_path, 'unc.%s.vals' % njets), 'w') as vals: + vals.write('\n'.join(unc_vals_lines)) + + with open(pjoin(output_path,'.shapes_timestamp'),'w') as stamp: + stamp.write('no use') + + + diff --git a/lfvetau/plotSoverSN.py b/lfvetau/plotSoverSN.py new file mode 100644 index 00000000..eb6da1e4 --- /dev/null +++ b/lfvetau/plotSoverSN.py @@ -0,0 +1,398 @@ +import ROOT +import sys +import os +import math + +try: + massrange = sys.argv[1].split(',') + print massrange +except: + + print 'please give me the mass range in the format 50,300 ' + +ROOT.gROOT.SetBatch() # don't pop up canvases + +ROOT.gROOT.SetStyle('Plain') # white background +ROOT.gStyle.SetOptStat(0) +ROOT.gStyle.SetOptTitle(0) +ROOT.gStyle.SetPadGridX(1) +ROOT.gStyle.SetPadGridY(1) + + + +samples = [ + "zjetsother", + "diboson", + "SMVBF126", + "singlet", + "SMGG126", + "ttbar", + "ztautau", + "wplusjets", + "LFVGG", + "LFVVBF" +] + +names = { + "zjetsother" : "zjetsother", + "diboson" : "diboson" , + "SMVBF126" : "SMH" , + "SMGG126" : "SMH" , + "singlet" : "singlet" , + "ttbar" : "ttbar" , + "ztautau" : "ztautau" , + "wplusjets" : "wplusjets" , + "LFVGG" : "LFVH" , + "LFVVBF" : "LFVH" +} + + +mymapper ={ + "wplusjets" : ["W $+$ jets "], + "ztautau" : ["$ Z \\rightarrow \\tau\\tau$ "], + "diboson" : ["EWK Diboson "], + "zjetsother" : ["$Z \\rightarrow ee, \\mu\\mu$"], + "ttbar" : ["$t\\bar{t}$ "], + "singlet" : ["$t$, $\\bar{t}$ "], + "SMH" : ["SM Higgs Background "], + "tot" : ["Sum of Backgrounds "], + "LFVH" : ["LFV Higgs Signal "], +} + +dirname = ['gg0etau', 'boostetau', 'vbfetau'] + +tPt = [30, 35, 40, 45, 50, 60, 70, 80, 90] +ePt = [30, 35, 40, 45, 50, 60, 70, 80, 90] +tMtToPfMet = [5, 10, 20, 30, 35, 40, 50, 60, 70, 80, 90] + +dphi = [0.50, 1.0, 1.50, 2.20, 2.40, 2.70, 3.00] + +vbf_mass = [200, 300, 400, 450, 500, 550, 600, 700, 800, 900] +vbf_deta = [2.0, 2.5, 3.0, 3.5, 4.0] + + + + +for jet in range (0, 3) : + file1 = ROOT.TFile.Open('plots/newNtuple_5Nov/lfvet/shapes.%s.root' %(str(jet))) + cuts= [ + ['selected', 'tPt30', 'tPt35', 'tPt40', 'tPt45','tPt50','tPt60','tPt70','tPt80','tPt90', + 'ePt30', 'ePt35', 'ePt40', 'ePt45','ePt50','ePt60','ePt70','ePt80','ePt90', + 'dphi0.50','dphi1.00','dphi1.50','dphi2.20','dphi2.40','dphi2.70', 'dphi3.00', + 'tMtToPfMet5','tMtToPfMet10','tMtToPfMet20','tMtToPfMet30','tMtToPfMet35','tMtToPfMet40', 'tMtToPfMet50', 'tMtToPfMet60', 'tMtToPfMet70' , 'tMtToPfMet80', 'tMtToPfMet90' ], + ['selected', 'tPt30', 'tPt35', 'tPt40', 'tPt45','tPt50','tPt60','tPt70','tPt80','tPt90', + 'ePt30', 'ePt35', 'ePt40', 'ePt45', 'ePt50','ePt60','ePt70','ePt80','ePt90', + 'tMtToPfMet5','tMtToPfMet10','tMtToPfMet20','tMtToPfMet30','tMtToPfMet35','tMtToPfMet40' , 'tMtToPfMet50', 'tMtToPfMet60', 'tMtToPfMet70' , 'tMtToPfMet80', 'tMtToPfMet90'], + ['selected', 'tPt30', 'tPt35', 'tPt40', 'tPt45','tPt50','tPt60','tPt70','tPt80','tPt90', + 'ePt30', 'ePt35', 'ePt40', 'ePt45', 'ePt50','ePt60','ePt70','ePt80','ePt90', + 'tMtToPfMet5','tMtToPfMet10','tMtToPfMet20','tMtToPfMet30','tMtToPfMet35','tMtToPfMet40', 'tMtToPfMet50', 'tMtToPfMet60', 'tMtToPfMet70', 'tMtToPfMet80', 'tMtToPfMet90', + 'vbf_mass200', 'vbf_mass300', 'vbf_mass400', 'vbf_mass450', 'vbf_mass500', 'vbf_mass550', 'vbf_mass600', 'vbf_mass700', 'vbf_mass800', 'vbf_mass900', + 'vbf_deta2.0','vbf_deta2.5','vbf_deta3.0','vbf_deta3.5', 'vbf_deta4.0' ] + ] + + + tPtcount =0 + ePtcount =0 + dphicount=0 + tMtcount =0 + vbfmasscount =0 + vbfdetacount =0 + + tPt_g = ROOT.TGraphErrors(len(tPt)) + ePt_g = ROOT.TGraphErrors(len(tPt)) + dphi_g = ROOT.TGraphErrors(len(dphi)) + tMt_g = ROOT.TGraphErrors(len(tMtToPfMet)) + vbfMass_g = ROOT.TGraphErrors(len(vbf_mass)) + vbfdeta_g = ROOT.TGraphErrors(len(vbf_deta)) + + tPt_ratio = ROOT.TGraphErrors(len(tPt)) + ePt_ratio = ROOT.TGraphErrors(len(tPt)) + dphi_ratio = ROOT.TGraphErrors(len(dphi)) + tMt_ratio = ROOT.TGraphErrors(len(tMtToPfMet)) + vbfMass_ratio = ROOT.TGraphErrors(len(vbf_mass)) + vbfdeta_ratio = ROOT.TGraphErrors(len(vbf_deta)) + + + for cut in cuts[jet]: + mydir = file1.Get("%s_%s" %(dirname[jet], cut)) + #mylist=mydir.GetListOfKeys() + totbackground =0. + totbackgrounderr2=0. + sig_int =0. + sig_err2 =0. + + + + + for sample in samples: + print dirname[jet], cut, mydir.GetName(), sample + histo = mydir.Get(sample) + integral = 0 + err2=0 + + for i in range(histo.GetXaxis().FindBin(float(massrange[0])), histo.GetXaxis().FindBin(float(massrange[1]))+1): + integral += histo.GetBinContent(i) + err2 += histo.GetBinError(i)*histo.GetBinError(i) + + if not 'LFV' in sample: + totbackground += integral + totbackgrounderr2 += err2 + + else: + sig_int =integral + sig_err2 =err2 + + + den = (sig_int+totbackground) + #print "_".join([dirname[jet], cut]), sig_int/(sig_int+totbackground) , math.sqrt((sig_err2+totbackgrounderr2)* pow(sig_int,2) / pow(den,4)) + if den==0 : + if 'tPt' in cut : tPtcount+=1 + if 'ePt' in cut : ePtcount+=1 + if 'dphi' in cut : dphicount+=1 + if 'tMtToPfMet' in cut : tMtcount+=1 + if 'vbf_mass' in cut :vbfmasscount+=1 + if 'vbf_deta' in cut : vbfdetacount+=1 + continue + + error = math.sqrt((totbackgrounderr2 * pow(sig_int,2) +sig_err2*(4*pow(den,2) - pow(sig_int,2) ))/4*pow(den,3)) + if 'tPt' in cut : + tPt_g.SetPoint(tPtcount, tPt[tPtcount], sig_int/math.sqrt(sig_int+totbackground)) + tPt_g.SetPointError(tPtcount, 0, math.sqrt((sig_err2+totbackgrounderr2)* pow(sig_int,2) / pow(den,4))) + tPt_ratio.SetPoint(tPtcount, tPt[tPtcount], sig_int/error) + tPt_ratio.SetPointError(tPtcount, 0, math.sqrt(sig_err2)/error) + tPtcount+=1 + if 'ePt' in cut : + print ePtcount, len(ePt), ePt[ePtcount] + ePt_g.SetPoint(ePtcount, ePt[ePtcount], sig_int/math.sqrt(sig_int+totbackground)) + ePt_g.SetPointError(ePtcount, 0, math.sqrt((sig_err2+totbackgrounderr2)* pow(sig_int,2) / pow(den,4))) + ePt_ratio.SetPoint(ePtcount, ePt[ePtcount], sig_int/error) + ePt_ratio.SetPointError(ePtcount, 0, math.sqrt(sig_err2)/error) + ePtcount+=1 + if 'dphi' in cut : + dphi_g.SetPoint(dphicount, dphi[dphicount], sig_int/math.sqrt(sig_int+totbackground)) + dphi_g.SetPointError(dphicount, 0, math.sqrt((sig_err2+totbackgrounderr2)* pow(sig_int,2) / pow(den,4))) + dphi_ratio.SetPoint(dphicount, dphi[dphicount], sig_int/error) + dphi_ratio.SetPointError(dphicount, 0, math.sqrt(sig_err2)/error) + dphicount+=1 + if 'tMtToPfMet' in cut : + tMt_g.SetPoint(tMtcount, tMtToPfMet[tMtcount], sig_int/math.sqrt(sig_int+totbackground)) + tMt_g.SetPointError(tMtcount, 0, math.sqrt((sig_err2+totbackgrounderr2)* pow(sig_int,2) / pow(den,4))) + tMt_ratio.SetPoint(tMtcount, tMtToPfMet[tMtcount], sig_int/error) + tMt_ratio.SetPointError(tMtcount, 0, math.sqrt(sig_err2)/error) + tMtcount+=1 + if 'vbf_mass' in cut : + vbfMass_g.SetPoint(vbfmasscount, vbf_mass[vbfmasscount], sig_int/math.sqrt(sig_int+totbackground)) + vbfMass_g.SetPointError(vbfmasscount, 0, math.sqrt((sig_err2+totbackgrounderr2)* pow(sig_int,2) / pow(den,4))) + vbfMass_ratio.SetPoint(vbfmasscount, vbf_mass[vbfmasscount], sig_int/error) + vbfMass_ratio.SetPointError(vbfmasscount, 0, math.sqrt(sig_err2)/error) + vbfmasscount+=1 + if 'vbf_deta' in cut : + vbfdeta_g.SetPoint(vbfdetacount, vbf_deta[vbfdetacount], sig_int/math.sqrt(sig_int+totbackground)) + vbfdeta_g.SetPointError(vbfdetacount, 0, math.sqrt((sig_err2+totbackgrounderr2)* pow(sig_int,2) / pow(den,4))) + vbfdeta_ratio.SetPoint(vbfdetacount, vbf_deta[vbfdetacount], sig_int/error) + vbfdeta_ratio.SetPointError(vbfdetacount, 0, math.sqrt(sig_err2)/error) + vbfdetacount+=1 + + c=ROOT.TCanvas("c", "c", 800, 800) + massInterval = massrange[0]+'_'+massrange[1] + c.Draw() + c.Divide(1,2) + + c.SetGridx(1) + c.SetGridy(1) + if jet==0: + c.cd(1) + tPt_g.SetTitle("0 jet, #tau p_{T} threshold") + tPt_g.Draw("AP") + tPt_g.GetXaxis().SetTitle("#tau p_{T} (GeV)") + tPt_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + tPt_g.SetMarkerStyle(20) + ##tPt_g.SetMarkerSize(0.3) + c.cd(2) + tPt_ratio.Draw("AP") + tPt_ratio.GetXaxis().SetTitle("#tau p_{T} (GeV)") + tPt_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + tPt_ratio.SetMarkerStyle(20) + + c.Update() + c.SaveAs("0jet_tPt_%s.pdf" %massInterval) + c.Clear() + c.Divide(1,2) + c.cd(1) + ePt_g.SetTitle("0 jet, e p_{T} threshold") + ePt_g.Draw("AP") + ePt_g.GetXaxis().SetTitle("e p_{T} (GeV)") + ePt_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + ePt_g.SetMarkerStyle(20) + c.cd(2) + ePt_ratio.Draw("AP") + ePt_ratio.GetXaxis().SetTitle("e p_{T} (GeV)") + ePt_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + ePt_ratio.SetMarkerStyle(20) + c.Update() + c.SaveAs("0jet_ePt_%s.pdf" %massInterval) + c.Clear() + c.Divide(1,2) + c.cd(1) + dphi_g.SetTitle("0 jet, #Delta#Phi") + dphi_g.Draw("AP") + dphi_g.GetXaxis().SetTitle("e - #tau #Delta#Phi") + dphi_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + dphi_g.SetMarkerStyle(20) + c.cd(2) + dphi_ratio.Draw("AP") + dphi_ratio.GetXaxis().SetTitle("e - #tau #Delta#Phi") + dphi_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + dphi_ratio.SetMarkerStyle(20) + c.Update() + c.SaveAs("0jet_dphi_%s.pdf" %massInterval) + c.Clear() + c.Divide(1,2) + c.cd(1) + tMt_g.SetTitle("0 jet, #tau Met M_{T}") + tMt_g.Draw("AP") + tMt_g.GetXaxis().SetTitle("#tau Met M_{T}") + tMt_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + tMt_g.SetMarkerStyle(20) + c.cd(2) + tMt_ratio.Draw("AP") + tMt_ratio.GetXaxis().SetTitle("#tau Met M_{T}") + tMt_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + tMt_ratio.SetMarkerStyle(20) + c.Update() + c.SaveAs("0jet_tMt_%s.pdf" %massInterval) + c.Clear() + c.Divide(1,2) + c.cd(1) + if jet==1: + c.Clear() + c.Divide(1,2) + c.cd(1) + tPt_g.SetTitle("1 jet, #tau p_{T} threshold") + tPt_g.Draw("AP") + tPt_g.GetXaxis().SetTitle("#tau p_{T} (GeV)") + tPt_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + tPt_g.SetMarkerStyle(20) + c.cd(2) + tPt_ratio.Draw("AP") + tPt_ratio.GetXaxis().SetTitle("#tau p_{T} (GeV)") + tPt_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + tPt_ratio.SetMarkerStyle(20) + c.Update() + c.SaveAs("1jet_tPt_%s.pdf" %massInterval) + c.Clear() + c.Divide(1,2) + c.cd(1) + ePt_g.SetTitle("1 jet, e p_{T} threshold") + ePt_g.Draw("AP") + ePt_g.GetXaxis().SetTitle("e p_{T} (GeV)") + ePt_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + ePt_g.SetMarkerStyle(20) + c.cd(2) + ePt_ratio.Draw("AP") + ePt_ratio.GetXaxis().SetTitle("e p_{T} (GeV)") + ePt_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + ePt_ratio.SetMarkerStyle(20) + c.Update() + c.SaveAs("1jet_ePt_%s.pdf" %massInterval) + c.Clear() + c.Divide(1,2) + c.cd(1) + tMt_g.SetTitle("1 jet, #tau Met M_{T}") + tMt_g.Draw("AP") + tMt_g.GetXaxis().SetTitle("#tau Met M_{T}") + tMt_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + tMt_g.SetMarkerStyle(20) + c.cd(2) + tMt_ratio.Draw("AP") + tMt_ratio.GetXaxis().SetTitle("#tau Met M_{T}") + tMt_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + tMt_ratio.SetMarkerStyle(20) + c.Update() + c.SaveAs("1jet_tMt_%s.pdf" %massInterval) + c.Clear() + c.Divide(1,2) + c.cd(1) + if jet==2: + c.Clear() + c.Divide(1,2) + c.cd(1) + tPt_g.SetTitle("2 jet, #tau p_{T} threshold") + tPt_g.Draw("AP") + tPt_g.GetXaxis().SetTitle("#tau p_{T} (GeV)") + tPt_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + tPt_g.SetMarkerStyle(20) + c.cd(2) + tPt_ratio.Draw("AP") + tPt_ratio.GetXaxis().SetTitle("#tau p_{T} (GeV)") + tPt_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + tPt_ratio.SetMarkerStyle(20) + c.Update() + c.SaveAs("2jet_tPt_%s.pdf" %massInterval) + c.Clear() + c.Divide(1,2) + c.cd(1) + ePt_g.SetTitle("2 jet, e p_{T} threshold") + ePt_g.Draw("AP") + ePt_g.GetXaxis().SetTitle("e p_{T} (GeV)") + ePt_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + ePt_g.SetMarkerStyle(20) + c.cd(2) + ePt_ratio.Draw("AP") + ePt_ratio.GetXaxis().SetTitle("e p_{T} (GeV)") + ePt_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + ePt_ratio.SetMarkerStyle(20) + c.Update() + c.SaveAs("2jet_ePt_%s.pdf" %massInterval) + c.Clear() + c.Divide(1,2) + c.cd(1) + tMt_g.SetTitle("2 jet, #tau Met M_{T}") + tMt_g.Draw("AP") + tMt_g.GetXaxis().SetTitle("#tau Met M_{T}") + tMt_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + tMt_g.SetMarkerStyle(20) + c.cd(2) + tMt_ratio.Draw("AP") + tMt_ratio.GetXaxis().SetTitle("#tau Met M_{T}") + tMt_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + tMt_ratio.SetMarkerStyle(20) + c.Update() + c.SaveAs("2jet_tMt_%s.pdf" %massInterval) + c.Clear() + c.Divide(1,2) + c.cd(1) + vbfMass_g.SetTitle("2 jet, vbf Mass") + vbfMass_g.Draw("AP") + vbfMass_g.GetXaxis().SetTitle("M_{jj}") + vbfMass_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + vbfMass_g.SetMarkerStyle(20) + c.cd(2) + vbfMass_ratio.Draw("AP") + vbfMass_ratio.GetXaxis().SetTitle("M_{jj}") + vbfMass_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + vbfMass_ratio.SetMarkerStyle(20) + + c.Update() + c.SaveAs("2jet_vbfMass_%s.pdf" %massInterval) + c.Clear() + c.Divide(1,2) + c.cd(1) + vbfdeta_g.SetTitle("2 jet, #Delta#eta_{jj}") + vbfdeta_g.Draw("AP") + vbfdeta_g.GetXaxis().SetTitle("#Delta#eta_{jj}") + vbfdeta_g.GetYaxis().SetTitle("S/#sqrt(S+B)") + vbfdeta_g.SetMarkerStyle(20) + c.cd(2) + vbfdeta_ratio.Draw("AP") + vbfdeta_ratio.GetXaxis().SetTitle("2 jet, #Delta#eta_{jj}") + vbfdeta_ratio.GetYaxis().SetTitle("S/#sigma(S/#sqrt(S+B))") + vbfdeta_ratio.SetMarkerStyle(20) + + c.Update() + c.SaveAs("2jet_vbfDeta_%s.pdf" %massInterval) + c.Clear() + + + file1.Close() + diff --git a/lfvetau/plotTauFakeRateMVA.py b/lfvetau/plotTauFakeRateMVA.py new file mode 100644 index 00000000..d59f2a69 --- /dev/null +++ b/lfvetau/plotTauFakeRateMVA.py @@ -0,0 +1,190 @@ +import rootpy.plotting.views as views +#from FinalStateAnalysis.PlotTools.SimplePlotter import SimplePlotter +from FinalStateAnalysis.PlotTools.Plotter import Plotter +from FinalStateAnalysis.PlotTools.BlindView import BlindView +from FinalStateAnalysis.PlotTools.PoissonView import PoissonView +from FinalStateAnalysis.PlotTools.MedianView import MedianView +from FinalStateAnalysis.PlotTools.ProjectionView import ProjectionView +#from FinalStateAnalysis.PlotTools.FixedIntegralView import FixedIntegralView +from FinalStateAnalysis.PlotTools.RebinView import RebinView +from FinalStateAnalysis.MetaData.data_styles import data_styles, colors +from FinalStateAnalysis.PlotTools.decorators import memo +from FinalStateAnalysis.MetaData.datacommon import br_w_leptons, br_z_leptons +from optparse import OptionParser +import os +import ROOT +import glob +import math +import logging +from fnmatch import fnmatch +from yellowhiggs import xs, br, xsbr + +ROOT.gROOT.SetBatch() +ROOT.gStyle.SetOptStat(0) + +jobid = os.environ['jobid'] + +print jobid +mc_samples = [ + 'ggHiggsToETau', + 'vbfHiggsToETau', + 'GluGluToHToTauTau_M-125_8TeV-powheg-pythia6', + 'VBF_HToTauTau_M-125_8TeV-powheg-pythia6', + 'Zjets_M50_skimmedLL', + 'Z1jets_M50_skimmedLL', + 'Z2jets_M50_skimmedLL', + 'Z3jets_M50_skimmedLL', + 'Z4jets_M50_skimmedLL', + 'Zjets_M50_skimmedTT', + 'Z1jets_M50_skimmedTT', + 'Z2jets_M50_skimmedTT', + 'Z3jets_M50_skimmedTT', + 'Z4jets_M50_skimmedTT', + 'TTJets*', + 'T_t*', + 'Tbar_t*', + 'WplusJets_madgraph_skimmed', + 'Wplus1Jets_madgraph', + 'Wplus1Jets_madgraph_tapas', + 'Wplus2Jets_madgraph', + 'Wplus2Jets_madgraph_tapas', + 'Wplus3Jets_madgraph', + 'Wplus4Jets_madgraph', + 'WWJets*', + 'WZJets*', + 'ZZJets*', + 'data*' +] + +files = [] +lumifiles = [] +channel = 'eet' +for x in mc_samples: + #print x + files.extend(glob.glob('results/%s/TauFakeRateAnalyzerMVA/%s.root' % (jobid, x))) + lumifiles.extend(glob.glob('inputs/%s/%s.lumicalc.sum' % (jobid, x))) + + + +period = '8TeV' +sqrts = 7 if '7TeV' in jobid else 8 + +def remove_name_entry(dictionary): + return dict( [ i for i in dictionary.iteritems() if i[0] != 'name'] ) + + +#sign = ['ss','os'] +sign = ['os'] +tauiso = ['tNoCuts', 'tSuperSuperLoose', 'tSuperLoose', 'tLoose', 'tTigh'] +#sign = ['os'] +#tauiso=['tTigh'] +outputdir = 'plots/%s/TauFakeRateAnalyzerMVA/%s/' % (jobid, channel) +if not os.path.exists(outputdir): + os.makedirs(outputdir) + +plotter = Plotter(files, lumifiles, outputdir) + + + + +EWKDiboson = views.StyleView( + views.SumView( + *[ plotter.get_view(regex) for regex in \ + filter(lambda x : x.startswith('WW') or x.startswith('WZ') or x.startswith('ZZ') or x.startswith('WG'), mc_samples )] +# filter(lambda x : x.startswith('WZ') , mc_samples )] + ), **remove_name_entry(data_styles['WW*'#,'WZ*', 'WG*', 'ZZ*' +]) +) +Wplus = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('Wplus'), mc_samples )]), **remove_name_entry(data_styles['Wplus*Jets*'])) +DYLL = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('skimmedLL'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*LL'])) +DYTT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.endswith('jets_M50_skimmedTT'), mc_samples )]), **remove_name_entry(data_styles['Z*jets*TT'])) +singleT = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : x.startswith('T_') or x.startswith('Tbar_'), mc_samples)]), **remove_name_entry(data_styles['T*_t*'])) + +SMH = views.StyleView(views.SumView( *[ plotter.get_view(regex) for regex in filter(lambda x : 'HToTauTau' in x , mc_samples)]), **remove_name_entry(data_styles['GluGluToHToTauTau*'])) + + +plotter.views['EWKDiboson']={'view' : EWKDiboson } +plotter.views['Wplus']={'view' : Wplus } +plotter.views['DYLL']={'view' : DYLL } +plotter.views['DYTT']={'view' : DYTT } +plotter.views['singleT']={'view' : singleT } +plotter.views['SMH']={'view' : SMH } + + +new_mc_samples = filter( lambda x : x.startswith('TTJets') , mc_samples) +#new_mc_samples = [] +#new_sigsamples= filter(lambda x: x.endswith('HiggsToETau'), mc_samples) + +#print new_sigsamples +new_mc_samples.extend(['EWKDiboson', 'SMH', 'singleT','Wplus', 'DYLL', 'DYTT' , +]) +#new_mc_samples.extend(['EWKDiboson','DYLL', 'DYTT']) +print new_mc_samples + + +#histoname = ['e1Pt','e1Phi','e1Eta','e2Pt','e2Phi','e2Eta', +# 'e1e2Mass', 'tPt','tPhi','tEta'] +#axistitle = ['e1 p_{T} (GeV)','e1 #phi','e1 #eta', 'e2 p_{T} (GeV)','e2 #phi','e2 #eta', 'e1-e2 Inv Mass (GeV)','#tau p_{T} (GeV)','#tau #phi','#tau #eta'] + +histoname = [('e1Pt','e1 p_{T} (GeV)', 2),('e1Phi','e1 #phi',4),('e1Eta','e1 #eta',2),('e2Pt','e2 p_{T} (GeV)',2),('e2Phi','e2 #phi',4),('e2Eta','e2 #eta',2), + ('e1e2Mass', 'e1-e2 Inv Mass (GeV)',1), ('tPt','#tau p_{T} (GeV)',2), ('tPhi','#tau #phi',4),('tEta','#tau #eta',2),('tAbsEta','#tau |#eta|',2), + ('etDR', 'e #tau dR', 2), ('etDPhi', 'e #tau #Delta#phi', 2), ('ztDR', 'Z #tau dR', 2), ('ztDPhi', 'Z #tau #Delta#phi', 2), ('type1_pfMetEt', 'type1_pfMet', 2), ( 'jetN_30', 'Number of jets, p_{T}>30', 1), ('bjetCSVVeto30', 'Number of b-jets',1) , ('tRawIso3Hits', 'tRawIso3Hits', 1) +] + +#rebins = [5, 5, 2, 5, 5, 2, 1, 5, 5, 2, 1] +#rebins = [] +#for n in histoname : +# rebins.append(1) + + +plotter.mc_samples = new_mc_samples + +print plotter.mc_samples + +for i in tauiso : + for s in sign : + #for histo, axis in zip(histoname, axistitle): +# for n,h in enumerate(histoname) : + foldername = s+'/'+i + if not os.path.exists(outputdir+foldername): + os.makedirs(outputdir+foldername) + + if i == 'tNoCuts': + plotter.plot_mc_vs_data(foldername,'CUT_FLOW', rebin=1, xaxis='CUT_FLOW', leftside=False, show_ratio=False, ratio_range=1.5, sorted=True) + plotter.save(foldername+'/CUT_FLOW') + + foldername = s+'/'+i + for h in histoname: + #plotter.canvas.SetLogy(True) + #plotter.plot_data(foldername, h, rebin=rebins[n], xaxis= axistitle[n], leftside=False) + #plotter.plot_mc_vs_data(foldername,h, rebin=rebins[n], xaxis= axistitle[n], leftside=False, show_ratio=True, ratio_range=1.5, sort=True) + + plotter.plot_mc_vs_data(foldername,h[0], rebin=h[2], xaxis=h[1], leftside=False, show_ratio=True, ratio_range=1.5, sorted=True) + plotter.save(foldername+'/'+h[0]) + + +# foldername = s+'/'+i+'/tptregion' +# if not os.path.exists(outputdir+foldername): +# os.makedirs(outputdir+foldername) + +# for h in histoname: +# plotter.plot_mc_vs_data(foldername,h[0], rebin=h[2], xaxis=h[1], leftside=False, show_ratio=True, ratio_range=1.5, sorted=True) +# plotter.save(foldername+'/'+h[0]) + + + jets=0 + while jets < 4 : + foldername = s+'/'+i+'/'+str(int(jets)) + if not os.path.exists(outputdir+foldername): + os.makedirs(outputdir+foldername) + for h in histoname: + plotter.plot_mc_vs_data(foldername,h[0], rebin=h[2], xaxis=h[1], leftside=False, show_ratio=True, ratio_range=1.5, sorted=True) + plotter.save(foldername+'/'+h[0]) + # foldername = s+'/'+i+'/'+str(int(jets))+'/tptregion' + # if not os.path.exists(outputdir+foldername): + # os.makedirs(outputdir+foldername) + # for h in histoname: + # plotter.plot_mc_vs_data(foldername,h[0], rebin=h[2], xaxis=h[1], leftside=False, show_ratio=True, ratio_range=1.5, sorted=True) + # plotter.save(foldername+'/'+h[0]) + jets+=1 + diff --git a/lfvetau/prepare_limit.sh b/lfvetau/prepare_limit.sh new file mode 100755 index 00000000..a7980142 --- /dev/null +++ b/lfvetau/prepare_limit.sh @@ -0,0 +1,76 @@ +#! /bin/env bash + +#set -o nounset +set -o errexit + +#$1 --> category (0, 1, 2) --> 0 jets, 1 jet, vbf +#$2 --> files extensions produced by plotting (used inoptimization) +category=$1 +file_extension=$2 +if [ -z "$file_extension" ]; +then + file_extension=$category +fi +input_dir=plots/$jobid/lfvet/ +output_dir=limits/$jobid/$category/ +mkdir -p $output_dir + +#copy basic config +cp card_config/cgs.$category.conf $output_dir/cgs.conf +cp card_config/unc.$category.conf $output_dir/unc.conf +cp card_config/unc.$category.vals $output_dir/unc.vals + +#copy shape file +shapes=shapesETau$category'Jet.root' +cp $input_dir/shapes.$file_extension.root $output_dir/$shapes + +#add dynamically assigned variables +cat $input_dir/unc.$file_extension.conf >> $output_dir/unc.conf +cat $input_dir/unc.$file_extension.vals >> $output_dir/unc.vals + +#add bbb errors +if [ $category == '0' ]; then + command='gg0etau:fakes' + category_name='gg0etau' + samples='fakes' +elif [ $category == '1' ]; then + command='boostetau:fakes' + category_name='boostetau' + samples='fakes' +else + command='vbfetau:fakes,ttbar,singlet,ztautau,SMVBF126,WWVBF126' + category_name='vbfetau' + samples='fakes ttbar singlet ztautau SMVBF126' +fi + +#TODO: normalize or not? +#will it work with input and output in the same place? +echo >> $output_dir/unc.conf +echo >> $output_dir/unc.vals +echo >> $output_dir/unc.conf +echo >> $output_dir/unc.vals +echo "#bbb uncertainties" >> $output_dir/unc.conf +echo "#bbb uncertainties" >> $output_dir/unc.vals +for sample in $samples; do + echo "adding bbb errors for $sample" + echo add_stat_shapes.py $output_dir/$shapes $output_dir/$shapes --normalize --filter $category_name/$sample --prefix $category_name + bbb_added=$(add_stat_shapes.py $output_dir/$shapes $output_dir/$shapes --normalize --filter $category_name/$sample --prefix $category_name | grep _bin_) + echo >> $output_dir/unc.conf + echo >> $output_dir/unc.vals + for unc in $bbb_added; do + echo $unc shape >> $output_dir/unc.conf + echo $category_name $sample $unc 1.0 >> $output_dir/unc.vals + done +done + +#build datacard +pushd $output_dir +mkdir -p 126 +pushd 126 +rm -rf $shapes +ln -s ../$shapes +popd +create-datacard.py -i $shapes -o 126/datacard_et_$category.txt +popd + +exit 0 diff --git a/lfvetau/produceShapeForOptimization.py b/lfvetau/produceShapeForOptimization.py new file mode 100644 index 00000000..3a43e2c8 --- /dev/null +++ b/lfvetau/produceShapeForOptimization.py @@ -0,0 +1,88 @@ +#from mauro plotters + +#Set logging before anything to override rootpy very verbose defaults +import sys +import logging +logging.basicConfig(stream=sys.stderr, level=logging.INFO) + +import os +import ROOT +from pdb import set_trace +from FinalStateAnalysis.PlotTools.MegaBase import make_dirs +from FinalStateAnalysis.MetaData.data_styles import data_styles +from FinalStateAnalysis.PlotTools.BlindView import BlindView, blind_in_range +from FinalStateAnalysis.PlotTools.SubtractionView import SubtractionView, PositiveView +import itertools +import glob +import sys +from BasePlotter import BasePlotter +from argparse import ArgumentParser + +parser = ArgumentParser(description=__doc__) +parser.add_argument('--no-plots', dest='no_plots', action='store_true', + default=False, help='Does not print plots') +parser.add_argument('--no-shapes', dest='no_shapes', action='store_true', + default=False, help='Does not create shapes for limit computation') +args = parser.parse_args() + +jobid = os.environ['jobid'] +#jobid = 'MCntuples_3March' +channel = 'et' +import rootpy.plotting.views as views + +ROOT.gROOT.SetStyle("Plain") +ROOT.gROOT.SetBatch(True) +ROOT.gStyle.SetOptStat(0) + +print "\nPlotting %s for %s\n" % (channel, jobid) + +#check if blind +blind = 'blind' not in os.environ or os.environ['blind'] == 'YES' +blind_region=[100, 150] if blind else None + +embedded = False + +plotter = BasePlotter(blind_region,use_embedded=embedded) + + +cuts= [ + ['selected', 'tPt30', 'tPt35', 'tPt40', 'tPt45','tPt50','tPt60','tPt70','tPt80','tPt90', + 'ePt30', 'ePt35', 'ePt40', 'ePt45','ePt50','ePt60','ePt70','ePt80','ePt90', + 'dphi0.50','dphi1.00','dphi1.50','dphi2.20','dphi2.40','dphi2.70', 'dphi3.00', + 'tMtToPfMet5','tMtToPfMet10','tMtToPfMet20','tMtToPfMet30','tMtToPfMet35','tMtToPfMet40', 'tMtToPfMet50', 'tMtToPfMet60', 'tMtToPfMet70' , 'tMtToPfMet80', 'tMtToPfMet90' ], + ['selected', 'tPt30', 'tPt35', 'tPt40', 'tPt45','tPt50','tPt60','tPt70','tPt80','tPt90', + 'ePt30', 'ePt35', 'ePt40', 'ePt45', 'ePt50','ePt60','ePt70','ePt80','ePt90', + 'tMtToPfMet5','tMtToPfMet10','tMtToPfMet20','tMtToPfMet30','tMtToPfMet35','tMtToPfMet40' , 'tMtToPfMet50', 'tMtToPfMet60', 'tMtToPfMet70' , 'tMtToPfMet80', 'tMtToPfMet90'], + ['selected', 'tPt30', 'tPt35', 'tPt40', 'tPt45','tPt50','tPt60','tPt70','tPt80','tPt90', + 'ePt30', 'ePt35', 'ePt40', 'ePt45', 'ePt50','ePt60','ePt70','ePt80','ePt90', + 'tMtToPfMet5','tMtToPfMet10','tMtToPfMet20','tMtToPfMet30','tMtToPfMet35','tMtToPfMet40', 'tMtToPfMet50', 'tMtToPfMet60', 'tMtToPfMet70', 'tMtToPfMet80', 'tMtToPfMet90', + 'vbf_mass200', 'vbf_mass300', 'vbf_mass400', 'vbf_mass450', 'vbf_mass500', 'vbf_mass550', 'vbf_mass600', 'vbf_mass700', 'vbf_mass800', 'vbf_mass900', + 'vbf_deta2.0','vbf_deta2.5','vbf_deta3.0','vbf_deta3.5', 'vbf_deta4.0' ] +] + +jets_names = [ + ('0', 'gg0etau_%s' , 1), + ('1', 'boostetau_%s', 1), + ('2', 'vbfetau_%s' , 5), +] +pjoin = os.path.join +for njets, cat_name, rebin in jets_names: + output_path = plotter.base_out_dir + tfile = ROOT.TFile(pjoin(output_path, 'shapes.%s.root' % njets), 'recreate') + for cut in cuts[int(njets)]: + signal_region = 'os/gg/ept30/%s/%s' %(njets, cut) + output_dir = tfile.mkdir(cat_name %cut) + unc_conf_lines, unc_vals_lines = plotter.write_shapes_for_optimization( + signal_region, 'h_collmass_pfmet', output_dir, rebin=rebin) + logging.warning('shape file %s created' % tfile.GetName()) + tfile.Close() + with open(pjoin(output_path, 'unc.%s.conf' % njets), 'w') as conf: + conf.write('\n'.join(unc_conf_lines)) + with open(pjoin(output_path, 'unc.%s.vals' % njets), 'w') as vals: + vals.write('\n'.join(unc_vals_lines)) + +with open(pjoin(output_path,'.shapes_timestamp'),'w') as stamp: + stamp.write('no use') + + + diff --git a/lfvetau/produceShapeForYields.py b/lfvetau/produceShapeForYields.py new file mode 100644 index 00000000..4127d8d4 --- /dev/null +++ b/lfvetau/produceShapeForYields.py @@ -0,0 +1,71 @@ +#from mauro plotters + +#Set logging before anything to override rootpy very verbose defaults +import sys +import logging +logging.basicConfig(stream=sys.stderr, level=logging.INFO) + +import os +import ROOT +from pdb import set_trace +from FinalStateAnalysis.PlotTools.MegaBase import make_dirs +from FinalStateAnalysis.MetaData.data_styles import data_styles +from FinalStateAnalysis.PlotTools.BlindView import BlindView, blind_in_range +from FinalStateAnalysis.PlotTools.SubtractionView import SubtractionView, PositiveView +import itertools +import glob +import sys +from BasePlotter import BasePlotter +from argparse import ArgumentParser + +parser = ArgumentParser(description=__doc__) +parser.add_argument('--no-plots', dest='no_plots', action='store_true', + default=False, help='Does not print plots') +parser.add_argument('--no-shapes', dest='no_shapes', action='store_true', + default=False, help='Does not create shapes for limit computation') +args = parser.parse_args() + +jobid = os.environ['jobid'] +#jobid = 'MCntuples_3March' +channel = 'et' +import rootpy.plotting.views as views + +ROOT.gROOT.SetStyle("Plain") +ROOT.gROOT.SetBatch(True) +ROOT.gStyle.SetOptStat(0) + +print "\nPlotting %s for %s\n" % (channel, jobid) + +#check if blind +blind = 'blind' not in os.environ or os.environ['blind'] == 'YES' +blind_region=[100, 150] if blind else None + +embedded = True + +plotter = BasePlotter(blind_region,use_embedded=embedded) + +signal_region = 'os/gg/ept30/%s/selected' +jets_names = [ + ('0', 'gg0etau' , 1), + ('1', 'boostetau', 1), + ('2', 'vbfetau' , 5), +] +pjoin = os.path.join +for njets, cat_name, rebin in jets_names: + output_path = plotter.base_out_dir + tfile = ROOT.TFile(pjoin(output_path, 'shapes.%s.root' % njets), 'recreate') + output_dir = tfile.mkdir(cat_name) + unc_conf_lines, unc_vals_lines = plotter.write_shapes_for_yields( + signal_region % njets, 'h_collmass_pfmet', output_dir, rebin=rebin) + logging.warning('shape file %s created' % tfile.GetName()) + tfile.Close() + with open(pjoin(output_path, 'unc.%s.conf' % njets), 'w') as conf: + conf.write('\n'.join(unc_conf_lines)) + with open(pjoin(output_path, 'unc.%s.vals' % njets), 'w') as vals: + vals.write('\n'.join(unc_vals_lines)) + +with open(pjoin(output_path,'.shapes_timestamp'),'w') as stamp: + stamp.write('no use') + + + diff --git a/lfvetau/readNtuple.py b/lfvetau/readNtuple.py new file mode 100644 index 00000000..43777afb --- /dev/null +++ b/lfvetau/readNtuple.py @@ -0,0 +1,37 @@ +import ROOT +import os +from sys import argv, stdout, stderr +import sys +import glob +import logging + +#jobid = os.environ['jobid'] +jobid = 'newNtuple_2Sept_old' +from os import listdir +from os.path import isfile, join + +ROOT.gROOT.SetBatch(True) +ROOT.gStyle.SetPadGridY(True) +ROOT.gStyle.SetPadGridX(True) + +from ETauTree import ETauTree +import math + +##oldntuple +#f=ROOT.TFile("/hdfs/store/user/taroni/newNtuple_3June/data_SingleElectron_Run2012B_22Jan2013_v1/make_ntuples_cfg-patTuple_cfg-00FB7F10-C77D-E211-B334-003048CF9B4E.root") +##newNtuple +#f = ROOT.TFile("/hdfs/store/user/taroni/newNtuple_2Sept/data_SingleElectron_Run2012B_22Jan2013_v1/make_ntuples_cfg-patTuple_cfg-00FB7F10-C77D-E211-B334-003048CF9B4E.root") +##mc +#f= ROOT.TFile("/hdfs/store/user/taroni/newNtuple_2Sept/Zjets_M50_skimmedLL/make_ntuples_cfg-patTuple_cfg-00037C53-AAD1-E111-B1BE-003048D45F38.root") +#more trees in ntuple +#f= ROOT.TFile("/hdfs/store/user/taroni/testneNtupleMoreTrees/Zjets_M50/make_ntuples_cfg-patTuple_cfg-00037C53-AAD1-E111-B1BE-003048D45F38.root") +#one tree only +#f= ROOT.TFile("/hdfs/store/user/taroni/testneNtuple/Zjets_M50/make_ntuples_cfg-patTuple_cfg-00037C53-AAD1-E111-B1BE-003048D45F38.root") +f= ROOT.TFile("/hdfs/store/user/taroni/newNtuple_3Dec/data_SingleElectron_Run2012A_22Jan2013_v1/make_ntuples_cfg-patTuple_cfg-08172766-C973-E211-B14C-0030487D8541.root") + +f.ls() +mytree = f.Get("et/final/Ntuple") + +for n, event in enumerate(mytree) : + if n > 100 : break + print event.run, event.lumi, event.evt, event.tPhi, event.ePhi, event.tEta, event.eEta, event.tPt, event.ePt diff --git a/lfvetau/run.sh b/lfvetau/run.sh new file mode 100755 index 00000000..e1caa128 --- /dev/null +++ b/lfvetau/run.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# Run all of the analysis + +set -o nounset +set -o errexit + +export MEGAPATH=/hdfs/store/user/taroni/ +source jobid.sh +export jobid=$jobid8 +#export jobid='newNtuple_9Oct' +#export jobid='newNtuple_11June' +#export jobid='newNtuple_3JuneOldWXJetsXsec' +#rake genkin +#rake recoplots +#rake recoplotsMVA +#rake recoplotsMVAeMtcut +#rake controlplots +#rake controlplotsMVA +#rake fakeeet +rake fakeeetMVA +#rake fakeeeeMVA +#rake fits +#rake drawTauFakeRate +#export jobid=$jobidmt +#rake recoplotsMuTau +#rake drawplots +#rake genkinEMu +#rake genkinMuTau diff --git a/lfvetau/setup.sh b/lfvetau/setup.sh new file mode 100755 index 00000000..419b693c --- /dev/null +++ b/lfvetau/setup.sh @@ -0,0 +1,37 @@ +#!/bin/bash +export OVERRIDE_META_TREE_data_ET='et/metaInfo' + +export IGNORE_LUMI_ERRORS=1 + +source jobid.sh + +./make_proxies.sh +export datasrc=/hdfs/store/user/$USER/ #$(ls -d /scratch/*/data/$jobid | awk -F +export MEGAPATH=/hdfs/store/user/$USER + +export jobid=$jobidSync +rake "meta:getinputs[$jobid, $datasrc,eg/metaInfo]" +rake "meta:getmeta[inputs/$jobid, eg/metaInfo, 8]" + + +#export jobid='newNtuple_10Oct' +echo $jobid +$jobid '{print $1}' +#export datasrc=/nfs_scratch/taroni/data +#export MEGAPATH=/nfs_scratch/taroni/data + +export jobid=$jobid8 +rake "meta:getinputs[$jobid, $datasrc,et/metaInfo]" +rake "meta:getmeta[inputs/$jobid, et/metaInfo, 8]" + + +#export jobid=$jobidmt +#./make_proxies.sh +#rake "meta:getinputs[$jobid, $datasrc,em/metaInfo]" +#rake "meta:getmeta[inputs/$jobid, em/metaInfo, 8]" +#rake "meta:getinputs[$jobid, $datasrc,mt/metaInfo]" +#rake "meta:getmeta[inputs/$jobid, mt/metaInfo, 8]" + + +unset OVERRIDE_META_TREE_data_ET +unset IGNORE_LUMI_ERRORS diff --git a/lfvetau/setupBatch.sh b/lfvetau/setupBatch.sh new file mode 100644 index 00000000..73e99b35 --- /dev/null +++ b/lfvetau/setupBatch.sh @@ -0,0 +1,6 @@ +export MEGAPATH=/hdfs/store/user/taroni +export farmout=1 +export dryrun=1 +export CutFlow=1 +source jobid.sh +export jobid=$jobid8 diff --git a/wh/ChargeFlipProbabilityEE.py b/wh/ChargeFlipProbabilityEE.py index 2eb0d82e..8ad26532 100755 --- a/wh/ChargeFlipProbabilityEE.py +++ b/wh/ChargeFlipProbabilityEE.py @@ -100,8 +100,8 @@ def preselection(row): continue evt_weight = pucorrector(row.nTruePU) * \ - mcCorrectors.get_electron_corrections(row,'e1','e2') - + mcCorrectors.get_electron_corrections(row,'e1','e2') *\ + mcCorrectors.double_electron_trigger(row) for iso_label in lep_id: if not selections.lepton_id_iso(row, 'e1', iso_label): continue diff --git a/wh/ControlEM.py b/wh/ControlEM.py index 65c4a8de..48d734c6 100644 --- a/wh/ControlEM.py +++ b/wh/ControlEM.py @@ -74,6 +74,11 @@ def begin(self): 5, -0.5, 4.5) self.book('em' + folder, 'eVetoCicTightIso', 'Number of extra CiC tight electrons', 5, -0.5, 4.5) + self.book('em' + folder, "trig_weight" , "mva_metEt", 200, 0, 2) + self.book('em' + folder, "PU_weight" , "mva_metEt", 200, 0, 2) + self.book('em' + folder, "idIso_weight", "mva_metEt", 200, 0, 2) + self.book('em' + folder, "emMass_noweight", "m_{e#mu} (GeV)", 240, 0, 240) + def correction(self, row): return self.pucorrector(row.nTruePU) * \ @@ -100,12 +105,20 @@ def fill_folder(x, w): histos[x + '/eJetBtag'].Fill(row.eJetBtag, w) histos[x + '/mJetBtag'].Fill(row.mJetBtag, w) histos[x + '/emMass'].Fill(row.e_m_Mass, w) + histos[x + '/emMass_noweight'].Fill(row.e_m_Mass) histos[x + '/bjetVeto'].Fill(row.bjetVeto, w) histos[x + '/bjetCSVVeto'].Fill(row.bjetCSVVeto, w) histos[x + '/muVetoPt5'].Fill(row.muVetoPt5, w) histos[x + '/tauVetoPt20'].Fill(row.tauVetoPt20, w) histos[x + '/eVetoCicTightIso'].Fill(row.eVetoCicTightIso, w) + if row.run < 2: + histos[x + "/trig_weight" ].Fill(mcCorrectors.correct_mueg_mu(row.mPt, row.mAbsEta)* + mcCorrectors.correct_mueg_e(row.ePt, row.eAbsEta) ) + histos[x + "/PU_weight" ].Fill(self.pucorrector(row.nTruePU)) + histos[x + "/idIso_weight"].Fill(mcCorrectors.get_muon_corrections(row,'m') * + mcCorrectors.get_electron_corrections(row,'e') ) + passes_e_id_iso = self.obj2_id(row) if row.e_m_SS and passes_e_id_iso: @@ -122,11 +135,15 @@ def preselection(self, row): Excludes FR object IDs and sign cut. ''' mu17e8 = (row.mu17ele8isoPass and row.mPt >= 20) if use_iso_trigger else (row.mu17ele8Pass and row.mPt >= 20) - mu8e17 = (row.mu8ele17isoPass and row.ePt >= 20) #if use_iso_trigger else (row.mu17ele8Pass and row.mPt < 20) + mu8e17 = (row.mu8ele17isoPass and row.ePt >= 20) if use_iso_trigger else (row.mu8ele17Pass and row.mPt < 20) if not (mu17e8 or mu8e17): return False if not selections.muSelection(row, 'm'): return False #applies basic selection (eta, pt > 10, DZ, pixHits, jetBTag) if not selections.eSelection(row, 'e'): return False #applies basic selection (eta, pt > 10, DZ, missingHits, jetBTag, HasConversion and chargedIdTight) - if not selections.vetos(row): return False #applies mu bjet e additional tau vetoes + #if not selections.vetos(row): return False #applies mu bjet e additional tau vetoes + if row.muVetoPt5IsoIdVtx: return False + if row.eVetoMVAIsoVtx: return False + if row.tauVetoPt20Loose3HitsVtx: return False + return True def obj1_id(self, row): @@ -145,9 +162,9 @@ def obj2_weight(self, row): mu17e8 = (row.mu17ele8isoPass and row.mPt >= 20) if use_iso_trigger else (row.mu17ele8Pass and row.mPt >= 20) fr = 0. if mu17e8: - fr = frfits.lowpt_e_fr[subleptonId](electronJetPt=max(row.eJetPt, row.ePt), electronPt=row.ePt) + fr = frfits.lowpt_e_fr[subleptonId](electronJetPt=max(row.eJetPt, row.ePt), electronPt=row.ePt, numJets20=row.jetVeto20) else: - fr = frfits.highpt_e_fr[subleptonId](electronJetPt=max(row.eJetPt, row.ePt),electronPt=row.ePt) + fr = frfits.highpt_e_fr[subleptonId](electronJetPt=max(row.eJetPt, row.ePt), electronPt=row.ePt, numJets20=row.jetVeto20) return fr / (1. - fr) def process(self): diff --git a/wh/ControlZEE.py b/wh/ControlZEE.py index 9aca6433..a5a83d1f 100644 --- a/wh/ControlZEE.py +++ b/wh/ControlZEE.py @@ -47,9 +47,9 @@ def f_(*args,**kargs): leading_e_fr_wjets = make_weight(frfits.highpt_ee_fr[leading_iso] ) -leading_e_fr_qcd = make_weight(frfits.highpt_ee_qcd_fr[leading_iso] ) +leading_e_fr_qcd = None #make_weight(frfits.highpt_ee_qcd_fr[leading_iso] ) subleading_e_fr_wjets = make_weight(frfits.lowpt_ee_fr[subleading_iso] ) -subleading_e_fr_qcd = make_weight(frfits.lowpt_ee_qcd_fr[subleading_iso] ) +subleading_e_fr_qcd = None #make_weight(frfits.lowpt_ee_qcd_fr[subleading_iso] ) def assign_charge_weight_one_maps(dir_, row): if "charge_weightSysUp" in dir_: return charge_flip_up_lead(row.e1AbsEta,row.e1Pt) + charge_flip_up_sub(row.e2AbsEta,row.e2Pt) @@ -65,9 +65,10 @@ def assign_id_weight(dir_, row): return 1 leading_fr = leading_e_fr_wjets if 'wjet_w' in dir_ else leading_e_fr_qcd sublead_fr = subleading_e_fr_wjets if 'wjet_w' in dir_ else subleading_e_fr_qcd - if "f1f2" in dir_: return leading_fr( electronJetPt=max(row.e1JetPt, row.e1Pt), electronPt=row.e1Pt) * sublead_fr( electronJetPt=max(row.e2JetPt, row.e2Pt), electronPt=row.e2Pt ) - if "f2" in dir_: return sublead_fr( electronJetPt=max(row.e2JetPt, row.e2Pt), electronPt=row.e2Pt) - if "f1" in dir_: return leading_fr( electronJetPt=max(row.e1JetPt, row.e1Pt), electronPt=row.e1Pt) + if "f1f2" in dir_: return leading_fr( electronJetPt=max(row.e1JetPt, row.e1Pt), electronPt=row.e1Pt, numJets20=row.jetVeto20) *\ + sublead_fr( electronJetPt=max(row.e2JetPt, row.e2Pt), electronPt=row.e2Pt, numJets20=row.jetVeto20) + if "f2" in dir_: return sublead_fr( electronJetPt=max(row.e2JetPt, row.e2Pt), electronPt=row.e2Pt, numJets20=row.jetVeto20) + if "f1" in dir_: return leading_fr( electronJetPt=max(row.e1JetPt, row.e1Pt), electronPt=row.e1Pt, numJets20=row.jetVeto20) class ControlZEE(MegaBase): tree = 'ee/final/Ntuple' @@ -88,6 +89,9 @@ def book_(dirname): self.book(dirname, 'SCEnergy', 'electron Super Cluster energy', 500, 0, 1000) self.book(dirname, 'SCDPhi' , 'electron Super Cluster DeltaPhi', 180, 0, math.Pi()) self.book(dirname, 'TrkMass' , 'Dielectrons invariant mass; M_{ee} [GeV];counts', 110, 40, 150) + self.book(dirname, 'TrkMass_low' , 'Dielectrons invariant mass; M_{ee} [GeV];counts', 110, 40, 150) + self.book(dirname, 'TrkMass_high', 'Dielectrons invariant mass; M_{ee} [GeV];counts', 110, 40, 150) + self.book(dirname, 'TrkMass_NoWeight', 'Dielectrons invariant mass; M_{ee} [GeV];counts', 110, 40, 150) self.book(dirname, 'TrkMass_NOSCALE' , 'Dielectrons invariant mass; M_{ee} [GeV];counts', 110, 40, 150) self.book(dirname, 'SCMass' , 'Dielectrons Super Cluster invariant mass; M_{ee} [GeV];counts', 110, 40, 150) self.book(dirname, "e1Pt" , "electron 1 Pt", 400, 0, 800) @@ -96,10 +100,14 @@ def book_(dirname): self.book(dirname, "e2AbsEta", "electron 2 abseta", 100, 0., 2.5) self.book(dirname, "type1_pfMetEt", "metEt" , 300, 0, 300) self.book(dirname, "mva_metEt", "mva_metEt", 300, 0, 300) + self.book(dirname, "trig_weight" , "mva_metEt", 200, 0, 2) + self.book(dirname, "PU_weight" , "mva_metEt", 200, 0, 2) + self.book(dirname, "idIso_weight", "mva_metEt", 200, 0, 2) + self.dirs = ['/'.join([sign,id_,weight,ch_weight]) for sign in ['os','ss'] for id_ in [h+k for h in ['p1','f1'] for k in ['p2','f2'] ] - for weight in ['','wjet_w','qcd_w'] + for weight in ['','wjet_w'] #,'qcd_w'] for ch_weight in ["","charge_weight","charge_weightSysUp","charge_weightSysDwn"] if 'f' in id_ or weight == '' if ch_weight == '' or sign == 'os' @@ -118,14 +126,6 @@ def book_(dirname): self.dir_based_histograms[location] = {} self.dir_based_histograms[location][name] = hist - def evt_weight(self, row): - if row.run > 2: - return 1. - else: - return self.pucorrector(row.nTruePU) * \ - mcCorrectors.get_electron_corrections(row,'e1','e2') - - def preselection(self, row): ''' Preselection applied to events. @@ -137,13 +137,16 @@ def preselection(self, row): if row.e1Pt < 20: return False if not selections.eSelection(row, 'e1'): return False if not selections.eSelection(row, 'e2'): return False - if not selections.vetos(row): return False + if row.muVetoPt5IsoIdVtx: return False + if row.eVetoMVAIsoVtx: return False + if row.tauVetoPt20Loose3HitsVtx: return False + #if not selections.vetos(row): return False if row.e1_e2_Mass < 40: return False if not (row.jetVeto40 >= 1): return False return True def obj1_id(self, row): - return selections.lepton_id_iso(row, 'e1', subleading_iso) + return selections.lepton_id_iso(row, 'e1', leading_iso) def obj2_id(self, row): return selections.lepton_id_iso(row, 'e2', subleading_iso) @@ -151,15 +154,21 @@ def obj2_id(self, row): def mc_weight(self, row): if row.run > 2: return 1. - return self.pucorrector(row.nTruePU) * \ - mcCorrectors.get_electron_corrections(row,'e1','e2') - + else: + return self.pucorrector(row.nTruePU) * \ + mcCorrectors.get_electron_corrections(row,'e2') *\ + mcCorrectors.electron_tight_corrections(row.e1Pt, row.e1AbsEta) *\ + mcCorrectors.double_electron_trigger(row) + #'e1', def process(self): histos = self.dir_based_histograms mc_weight = self.mc_weight def fill_histos(dirname, row, weight): - mass = frfits.mass_scaler[leading_iso](row.e1_e2_Mass) if "charge_weight" in dirname else row.e1_e2_Mass + mass = frfits.default_scaler(row.e1_e2_Mass) if "charge_weight" in dirname else row.e1_e2_Mass + mass_up = frfits.default_scaler_up(row.e1_e2_Mass) if "charge_weight" in dirname else row.e1_e2_Mass + mass_dw = frfits.default_scaler_down(row.e1_e2_Mass) if "charge_weight" in dirname else row.e1_e2_Mass + histos[dirname]['ePt' ].Fill(row.e1Pt,weight) histos[dirname]['eAbsEta' ].Fill(row.e1AbsEta,weight) histos[dirname]['SCEnergy'].Fill(row.e1SCEnergy,weight) @@ -167,14 +176,21 @@ def fill_histos(dirname, row, weight): histos[dirname]['eAbsEta' ].Fill(row.e2AbsEta,weight) histos[dirname]['SCEnergy'].Fill(row.e2SCEnergy,weight) histos[dirname]['TrkMass' ].Fill(mass, weight) + histos[dirname]['TrkMass_low' ].Fill(mass_dw, weight) + histos[dirname]['TrkMass_high' ].Fill(mass_up, weight) #histos[dirname]['SCMass' ].Fill(sc_inv_mass(row),weight) histos[dirname]["e1Pt" ].Fill(row.e1Pt,weight) histos[dirname]["e2Pt" ].Fill(row.e2Pt,weight) histos[dirname]["e1AbsEta"].Fill(row.e1AbsEta,weight) histos[dirname]["e2AbsEta"].Fill(row.e2AbsEta,weight) histos[dirname]['TrkMass_NOSCALE'].Fill(row.e1_e2_Mass, weight) + histos[dirname]['TrkMass_NoWeight'].Fill(row.e1_e2_Mass) histos[dirname]['type1_pfMetEt'].Fill(row.type1_pfMetEt, weight) histos[dirname]['mva_metEt'].Fill(row.mva_metEt, weight) + if row.run < 2: + histos[dirname]["trig_weight" ].Fill( self.pucorrector(row.nTruePU) ) + histos[dirname]["PU_weight" ].Fill( mcCorrectors.get_electron_corrections(row,'e1','e2') ) + histos[dirname]["idIso_weight"].Fill( mcCorrectors.double_electron_trigger(row) ) for row in self.tree: if not self.preselection(row): diff --git a/wh/ControlZMM.py b/wh/ControlZMM.py index b7afea45..d2d9b706 100644 --- a/wh/ControlZMM.py +++ b/wh/ControlZMM.py @@ -112,6 +112,9 @@ def preselection(self, row): if row.m1_m2_Mass > 120: return False if not selections.muSelection(row, 'm1'): return False #applies basic selection (eta, pt > 10, DZ, pixHits, jetBTag) if not selections.muSelection(row, 'm2'): return False #applies basic selection (eta, pt > 10, DZ, pixHits, jetBTag) + if row.muVetoPt5IsoIdVtx: return False + if row.eVetoMVAIsoVtx: return False + if row.tauVetoPt20Loose3HitsVtx: return False return True def obj1_id(self, row): diff --git a/wh/FakeRatesEE.py b/wh/FakeRatesEE.py index abfc4f04..05199c66 100755 --- a/wh/FakeRatesEE.py +++ b/wh/FakeRatesEE.py @@ -28,9 +28,32 @@ from array import array from pprint import pprint import ROOT - +import FinalStateAnalysis.PlotTools.pytree as pytree +from cutflowtracker import cut_flow_tracker +import math + +def inv_mass(pt1,eta1,phi1,pt2,eta2,phi2): + return math.sqrt( + 2*pt1*pt2*(math.cosh(eta1 - eta2) - math.cos(phi1 - phi2)) + ) + +cut_flow_step = ['bare', 'trigger', 'e1_e2_DR', + 'e1 presel', 'e2 presel', 'jet requirement', + 'muon veto', 'bjet veto', 'electron veto', 'tau veto', + 'region assignment', +] + + +region_for_event_list = os.environ.get('EVTLIST_REGION','') +zMassCut = 'NoZmass' in region_for_event_list +if zMassCut: + cut_flow_step.append('zMassCut') +region_for_event_list = region_for_event_list.replace('NoZmass','') +SYNC = ('SYNC' in os.environ) and eval(os.environ['SYNC']) +print region_for_event_list + class FakeRatesEE(MegaBase): - tree = 'ee/final/Ntuple' + tree = 'ee/final/Ntuple' if not SYNC else 'Ntuple' def __init__(self, tree, outfile, **kwargs): super(FakeRatesEE, self).__init__(tree, outfile, **kwargs) # Use the cython wrapper @@ -47,7 +70,13 @@ def __init__(self, tree, outfile, **kwargs): for j in self.iso_points] def begin(self): - for region in ['wjets', 'qcd', 'wjetsNoZmass', 'qcdNoZmass']: + self.book('', "CUT_FLOW", "Cut Flow", len(cut_flow_step), 0, len(cut_flow_step)) + xaxis = self.histograms['CUT_FLOW'].GetXaxis() + self.cut_flow_histo = self.histograms['CUT_FLOW'] + for i, name in enumerate(cut_flow_step): + xaxis.SetBinLabel(i+1, name) + + for region in ['wjetsLtLow', 'qcd', 'wjetsNoZmass', 'wjetsLtLowNoZmass', 'qcdNoZmass']: for denom in ['pt10', 'pt20']: denom_key = (region, denom) denom_histos = {} @@ -57,9 +86,17 @@ def begin(self): denom_histos['electronInfo'] = self.book( os.path.join(region, denom), 'electronInfo', "electronInfo", - 'electronPt:electronJetPt:weight:'+':'.join(self.lepIds), + 'electronPt:electronJetPt:electronJetCSVBtag:electronJetMass:numJets20:numJets40:weight:'+':'.join(self.lepIds), type=ROOT.TNtuple) + denom_histos['evtInfo'] = self.book( + os.path.join(region, denom), + 'evtInfo', 'evtInfo', + 'run/l:lumi/l:evt/l:e1Pt/D:e1Eta/D:e1Phi/D:e2Pt/D:e2Eta/D:e2Phi/D:e1ChargeIdTight/D' +\ + ':e2ChargeIdTight/D:e2RelPFIsoDB/D:e2MtToMET/D:e1MtToMET/D:bjetCSVVetoZHLike/D'+\ + ':e1RelPFIsoDB/D:e2MVAIDH2TauWP/D:e1MVAIDH2TauWP/D:e1_e2_SS/D:e1MVANonTrig/D:e2MVANonTrig/D', + type=pytree.PyTree) + for numerator in self.lepIds: num_key = (region, denom, numerator) num_histos = {} @@ -94,17 +131,32 @@ def book_histo(name, *args, **kwargs): def process(self): + cut_flow_histo = self.cut_flow_histo + cut_flow_trk = cut_flow_tracker(cut_flow_histo) - def preselection(row): + def preselection(row, cut_flow_trk): if not row.doubleEPass: return False if not (row.e1MatchesDoubleEPath > 0 and \ row.e2MatchesDoubleEPath > 0): return False + cut_flow_trk.Fill('trigger') + + if row.e1_e2_DR < 0.5: return False + cut_flow_trk.Fill('e1_e2_DR') + if not row.e1Pt > 20: return False if not selections.eSelection(row, 'e1'): return False if not row.e1MVAIDH2TauWP: return False + cut_flow_trk.Fill('e1 presel') + if not selections.eSelection(row, 'e2'): return False - if not (row.jetVeto40_DR05 >= 1): return False - if not selections.vetos(row): return False + cut_flow_trk.Fill('e2 presel') + + #if not (row.jetVeto40_DR05 >= 1): return False + if row.jetVeto20 == 0: return False + cut_flow_trk.Fill('jet requirement') + + if not selections.vetos(row, cut_flow_trk): return False + return True def fill(the_histos, row, fillNtuple=False): @@ -135,13 +187,20 @@ def fill(the_histos, row, fillNtuple=False): #print id_iso_vals #print self.lepIds id_iso_vals = [float( i ) for i in id_iso_vals ] - the_histos['electronInfo'].Fill( array("f", [row.e2Pt, row.e2JetPt, weight]+id_iso_vals) ) + electron_jet_mass = -1 #inv_mass(row.e2Pt, row.e2Eta, row.e2Phi, row.leadingJetPt, row.leadingJetEta, row.leadingJetPhi) + + the_histos['electronInfo'].Fill( array("f", [row.e2Pt, max(row.e2Pt, row.e2JetPt), max(0, row.e2JetCSVBtag), + electron_jet_mass, row.jetVeto20, row.jetVeto40_DR05, weight]+id_iso_vals) ) + the_histos['evtInfo'].Fill( row ) histos = self.histograms #pprint( histos) for row in self.tree: - if not preselection(row): + cut_flow_trk.new_row(row.run,row.lumi,row.evt) + cut_flow_trk.Fill('bare') + + if not preselection(row, cut_flow_trk): continue region = selections.control_region_ee(row) @@ -150,7 +209,18 @@ def fill(the_histos, row, fillNtuple=False): if region == 'zee': continue + + if region_for_event_list and region == region_for_event_list: + cut_flow_trk.Fill('region assignment') + if zMassCut and not (60 < row.e1_e2_Mass < 120): + cut_flow_trk.Fill('zMassCut') + # This is a QCD or Wjets + if region_for_event_list and region == region_for_event_list\ + and (not zMassCut or not (60 < row.e1_e2_Mass < 120)) \ + and not SYNC: + print '%i:%i:%i' % (row.run, row.lumi, row.evt) + continue def make_region_plots(full_region): fill(histos[full_region], row, True) @@ -169,11 +239,15 @@ def make_region_plots(full_region): make_region_plots((region, 'pt10')) if not (row.e1_e2_Mass > 60 and row.e1_e2_Mass < 120): make_region_plots((region+'NoZmass', 'pt10')) - + if region == 'wjetsLtLow' and row.e1MtToMET > 55: + make_region_plots(('wjetsNoZmass', 'pt10')) + if row.e2Pt > 20: make_region_plots((region, 'pt20')) if not (row.e1_e2_Mass > 60 and row.e1_e2_Mass < 120): make_region_plots((region+'NoZmass', 'pt20')) + if region == 'wjetsLtLow' and row.e1MtToMET > 55: + make_region_plots(('wjetsNoZmass', 'pt20')) def finish(self): diff --git a/wh/FakeRatesEM.py b/wh/FakeRatesEM.py index 3597fa16..90038564 100644 --- a/wh/FakeRatesEM.py +++ b/wh/FakeRatesEM.py @@ -21,11 +21,32 @@ import mcCorrectors import os from array import array +import FinalStateAnalysis.PlotTools.pytree as pytree +from cutflowtracker import cut_flow_tracker +import math + +def inv_mass(pt1,eta1,phi1,pt2,eta2,phi2): + return math.sqrt( + 2*pt1*pt2*(math.cosh(eta1 - eta2) - math.cos(phi1 - phi2)) + ) + +cut_flow_step = ['bare', #'trigger', + 'e_m_SS', 'e_m_DR', #'e_m_Mass', + 'mu presel', 'e presel', 'jet presence', + 'muon veto', 'bjet veto', 'electron veto', 'tau veto', + 'trigger', 'tag ID ISO', 'tag MT', 'probe MT' +] + +REGION = os.environ['REGION'] if ('REGION' in os.environ) else '' +env_lepton = REGION[0] if REGION else '' +env_region = REGION[1:] if REGION else '' def control_region(row): # Figure out what control region we are in. - if row.mPFIDTight and row.mRelPFIsoDB < 0.15 and row.mMtToMET > 35 and row.eMtToMET < 35: - return 'wjets' + if row.mPFIDTight and row.mRelPFIsoDB < 0.15 and row.mMtToMET > 35 and row.eMtToMET < 35 and row.bjetCSVVetoZHLike == 0: + return 'wjetsLtLow' + elif row.mPFIDTight and row.mRelPFIsoDB < 0.15 and row.mMtToMET > 35 and row.eMtToMET < 35 and row.bjetCSVVetoZHLike > 0: + return 'ttbar' elif row.mPFIDTight and row.mRelPFIsoDB > 0.3 and row.type1_pfMetEt < 25: return 'qcd' else: @@ -33,8 +54,10 @@ def control_region(row): def control_region_muon(row): # Figure out what control region we are in. - if selections.lepton_id_iso(row, 'e', 'eid12Medium_h2taucuts') and row.eMtToMET > 35 and row.mMtToMET < 35: - return 'Mwjets' + if selections.lepton_id_iso(row, 'e', 'eid12Medium_h2taucuts') and row.eMtToMET > 35 and row.mMtToMET < 35 and row.bjetCSVVetoZHLike == 0: + return 'MwjetsLtLow' + elif selections.lepton_id_iso(row, 'e', 'eid12Medium_h2taucuts') and row.eMtToMET > 35 and row.mMtToMET < 35 and row.bjetCSVVetoZHLike > 0: + return 'Mttbar' elif row.eRelPFIsoDB > 0.3 and row.type1_pfMetEt < 25: # selections.electronIds['eid12Medium'](row, 'e') and return 'Mqcd' else: @@ -42,11 +65,11 @@ def control_region_muon(row): class FakeRatesEM(MegaBase): - tree = 'em/final/Ntuple' + tree = 'em/final/Ntuple' if 'SYNC' not in os.environ else 'Ntuple' def __init__(self, tree, outfile, **kwargs): super(FakeRatesEM, self).__init__(tree, outfile, **kwargs) # Use the cython wrapper - self.tree = EMuTree.EMuTree(tree) + self.tree = tree #EMuTree.EMuTree(tree) self.out = outfile # Histograms for each category self.histograms = {} @@ -61,7 +84,13 @@ def __init__(self, tree, outfile, **kwargs): def begin(self): #Electron Fake Rates - for region in ['wjets', 'qcd']: + self.book('', "CUT_FLOW", "Cut Flow", len(cut_flow_step), 0, len(cut_flow_step)) + xaxis = self.histograms['CUT_FLOW'].GetXaxis() + self.cut_flow_histo = self.histograms['CUT_FLOW'] + for i, name in enumerate(cut_flow_step): + xaxis.SetBinLabel(i+1, name) + + for region in ['wjets', 'wjetsLtLow', 'qcd', 'ttbar']: for denom in ['pt10','pt20']: denom_key = (region, denom) denom_histos = {} @@ -71,9 +100,20 @@ def begin(self): denom_histos['electronInfo'] = self.book( os.path.join(region, denom), 'electronInfo', "electronInfo", - 'electronPt:electronJetPt:weight:'+':'.join(self.lepIds), + 'electronPt:electronJetPt:electronJetCSVBtag:electronJetMass:numJets20:numJets40:weight:'+':'.join(self.lepIds), type=ROOT.TNtuple) + denom_histos['evtInfo'] = self.book( + os.path.join(region, denom), + 'evtInfo', 'evtInfo', + 'run/l:lumi/l:evt/l' +\ + ':ePt/D:eEta/D:ePhi/D:mPt/D:mEta/D:mPhi/D' +\ + ':eChargeIdTight/D:mPFIDTight/D:mRelPFIsoDB/D' +\ + ':mMtToMET/D:eMtToMET/D:bjetCSVVetoZHLike/D' +\ + ':eRelPFIsoDB/D:eMVAIDH2TauWP/D:e_m_SS/D', + type=pytree.PyTree) + + for numerator in self.lepIds: num_key = (region, denom, numerator) num_histos = {} @@ -99,8 +139,8 @@ def book_histo(name, *args, **kwargs): book_histo('eJetptDvseJetPt', '' , 200, 0, 200, 200, 0, 1,type=ROOT.TH2F) #Muon Fake Rates - for region in ['Mwjets', 'Mqcd']: - for denom in ['pt10', 'pt20']: #, 'pt10b', 'pt10t', 'pt10f']: + for region in ['Mwjets', 'MwjetsLtLow', 'Mqcd', 'Mttbar']: + for denom in ['pt10', 'pt20']: denom_key = (region, denom) denom_histos = {} self.histograms[denom_key] = denom_histos @@ -109,9 +149,20 @@ def book_histo(name, *args, **kwargs): denom_histos['muonInfo'] = self.book( os.path.join(region, denom), 'muonInfo', "muonInfo", - 'muonPt:muonJetPt:muonJetBtag:weight:'+':'.join(self.muon_lepIds), + 'muonPt:muonJetPt:muonJetCSVBtag:muonJetMass:numJets20:numJets40:weight:'+':'.join(self.muon_lepIds), type=ROOT.TNtuple) + denom_histos['evtInfo'] = self.book( + os.path.join(region, denom), + 'evtInfo', 'evtInfo', + 'run/l:lumi/l:evt/l' +\ + ':ePt/D:eEta/D:ePhi/D' +\ + ':mPt/D:mEta/D:mPhi/D' +\ + ':eChargeIdTight/D:mPFIDTight/D:mRelPFIsoDB/D' +\ + ':mMtToMET/D:eMtToMET/D:bjetCSVVetoZHLike/D' +\ + ':eRelPFIsoDB/D:eMVAIDH2TauWP/D:e_m_SS/D', + type=pytree.PyTree) + for numerator in self.muon_lepIds: num_key = (region, denom, numerator) num_histos = {} @@ -138,17 +189,34 @@ def book_histo(name, *args, **kwargs): book_histo('muonAbsEta', 'Muon Abs Eta', 100, -2.5, 2.5) book_histo('muonPtRatio', 'Muon Pt', 100, 0., 1.) book_histo('muonPtDiff', 'Muon Pt', 200, 0., 200.) + book_histo('eMtToMET', 'e MT', 100, 0, 200) + def process(self): + cut_flow_histo = self.cut_flow_histo + cut_flow_trk = cut_flow_tracker(cut_flow_histo) def preselection(row): if not row.e_m_SS: return False + cut_flow_trk.Fill('e_m_SS') + + if row.e_m_DR < 0.5: return False + cut_flow_trk.Fill('e_m_DR') + if not selections.muSelection(row, 'm'): return False #applies basic selection (eta, pt > 10, DZ, pixHits, jetBTag) + cut_flow_trk.Fill('mu presel') + if not selections.eSelection(row, 'e'): return False #applies basic selection (eta, pt > 10, DZ, missingHits, jetBTag, HasConversion and chargedIdTight) if not row.eChargeIdTight: return False - if not (row.jetVeto40_DR05 >= 1): return False - if not selections.vetos(row): return False #applies mu bjet e additional tau vetoes + cut_flow_trk.Fill('e presel') + + #if not (row.jetVeto40_DR05 >= 1): return False + if row.jetVeto20 == 0: return False + cut_flow_trk.Fill('jet presence') + + if not selections.vetos(row, cut_flow_trk, False): return False #applies mu bjet e additional tau vetoes + #DO NOT APPLY BJet veto. It is used for ttbar CR return True #if self.is7TeV: #base_selection = 'mu17ele8Pass && ' + base_selection @@ -171,17 +239,22 @@ def fill(the_histos, row, fillNtuple=False): the_histos['eJetptDvseJetPt'].Fill(max(row.eJetPt, row.ePt),row.eJetptD) if fillNtuple: id_iso_vals = [ float( selections.lepton_id_iso(row, 'e', label) ) for label in self.lepIds] - the_histos['electronInfo'].Fill( array("f", [row.ePt, row.eJetPt, weight]+id_iso_vals) ) + electron_jet_mass = -1. #inv_mass(row.ePt, row.eEta, row.ePhi, row.leadingJetPt, row.leadingJetEta, row.leadingJetPhi) + + the_histos['electronInfo'].Fill( array("f", [row.ePt, max(row.eJetPt, row.ePt), max(0, row.eJetCSVBtag), + electron_jet_mass, row.jetVeto20, row.jetVeto40_DR05, weight]+id_iso_vals) ) + the_histos['evtInfo'].Fill(row) def fill_muon(the_histos, row, fillNtuple=False): - weight = 1 + weight = 1. if row.run == 1: weight = self.pucorrector(row.nTruePU) *\ mcCorrectors.correct_mueg_mu(row.mPt, row.mAbsEta) * \ mcCorrectors.correct_mueg_e(row.ePt, row.eAbsEta) the_histos['muonPt'].Fill(row.mPt, weight) + the_histos['eMtToMET'].Fill(row.eMtToMET) the_histos['muonJetPt'].Fill(max(row.mJetPt, row.mPt), weight) the_histos['muonAbsEta'].Fill(row.mAbsEta, weight) the_histos['muonPtRatio'].Fill(row.mPt/max(row.mJetPt, row.mPt), weight) @@ -190,11 +263,17 @@ def fill_muon(the_histos, row, fillNtuple=False): pfidiso02 = float( row.mPFIDTight and row.mRelPFIsoDB < 0.2) h2taucuts = float( row.mPFIDTight and ((row.mRelPFIsoDB < 0.15 and row.mAbsEta < 1.479) or row.mRelPFIsoDB < 0.1 )) h2taucuts020 = float( row.mPFIDTight and ((row.mRelPFIsoDB < 0.20 and row.mAbsEta < 1.479) or row.mRelPFIsoDB < 0.15)) - the_histos['muonInfo'].Fill( array("f", [row.mPt, row.mJetPt, row.mJetBtag, weight, pfidiso02, h2taucuts, h2taucuts020] ) ) - + muon_jet_mass = -1. #inv_mass(row.mPt, row.mEta, row.mPhi, row.leadingJetPt, row.leadingJetEta, row.leadingJetPhi) + + the_histos['muonInfo'].Fill( array("f", [row.mPt, max(row.mJetPt, row.mPt), max(0, row.mJetCSVBtag), + muon_jet_mass, row.jetVeto20, row.jetVeto40_DR05, weight, + pfidiso02, h2taucuts, h2taucuts020] ) ) + the_histos['evtInfo'].Fill(row) def fill_region(region,pt_cut): + if region is None: + return None fill(histos[(region, pt_cut)], row, True) for idlabel, idfcn in selections.electronIds.iteritems(): @@ -211,6 +290,8 @@ def fill_region(region,pt_cut): fill(histos[(region, pt_cut, idlabel+'_h2taucuts020')], row) def fill_muon_region(region, tag): + if region is None: + return None # This is a QCD or Wjets fill_muon(histos[(region, tag)], row, True) @@ -227,25 +308,56 @@ def fill_muon_region(region, tag): histos = self.histograms for row in self.tree: + cut_flow_trk.new_row(row.run,row.lumi,row.evt) + cut_flow_trk.Fill('bare') if not preselection(row): continue region = control_region(row) - if region is None: - continue - # This is a QCD or Wjets + muon_region = control_region_muon(row) + is7TeV = bool('7TeV' in os.environ['jobid']) use_iso_trigger = not is7TeV mu17e8 = (row.mu17ele8isoPass and row.mPt >= 20) if use_iso_trigger else (row.mu17ele8Pass and row.mPt >= 20) - mu8e17 = (row.mu8ele17isoPass and row.ePt >= 20) #if use_iso_trigger else (row.mu17ele8Pass and row.mPt < 20) + mu8e17 = (row.mu8ele17isoPass and row.ePt >= 20) if use_iso_trigger else (row.mu8ele17Pass and row.ePt >= 20) + + if env_lepton and env_lepton == 'e': + if mu17e8: + cut_flow_trk.Fill('trigger') + if row.mPFIDTight and row.mRelPFIsoDB < 0.15: + cut_flow_trk.Fill('tag ID ISO') + if row.mMtToMET > 35: + cut_flow_trk.Fill('tag MT') + if row.eMtToMET < 35: + cut_flow_trk.Fill('probe MT') + + elif env_lepton and env_lepton == 'm': + if mu8e17: + cut_flow_trk.Fill('trigger') + if selections.lepton_id_iso(row, 'e', 'eid12Medium_h2taucuts'): + cut_flow_trk.Fill('tag ID ISO') + if row.eMtToMET > 35: + cut_flow_trk.Fill('tag MT') + if row.mMtToMET < 35: + cut_flow_trk.Fill('probe MT') + + cut_flow_trk.flush() + if mu17e8: fill_region(region,'pt10') + if region == 'wjetsLtLow' and row.mMtToMET > 55: + fill_region('wjets', 'pt10') if mu8e17: fill_region(region,'pt20') - muon_region = control_region_muon(row) + if region == 'wjetsLtLow' and row.mMtToMET > 55: + fill_region('wjets', 'pt20') if muon_region: fill_muon_region(muon_region, 'pt10') + if muon_region == 'MwjetsLtLow' and row.eMtToMET > 55: + fill_muon_region('Mwjets', 'pt10') if row.mPt > 20: fill_muon_region(muon_region, 'pt20') + if muon_region == 'MwjetsLtLow' and row.eMtToMET > 55: + fill_muon_region('Mwjets', 'pt20') def finish(self): self.write_histos() diff --git a/wh/FakeRatesMM.py b/wh/FakeRatesMM.py index 86808db5..3846c97c 100755 --- a/wh/FakeRatesMM.py +++ b/wh/FakeRatesMM.py @@ -22,19 +22,39 @@ import baseSelections as selections import os import ROOT +from cutflowtracker import cut_flow_tracker +import math + +def inv_mass(pt1,eta1,phi1,pt2,eta2,phi2): + return math.sqrt( + 2*pt1*pt2*(math.cosh(eta1 - eta2) - math.cos(phi1 - phi2)) + ) + +#Makes the cut flow histogram +cut_flow_step = ['bare', 'trigger', + 'm1_m2_SS', 'm1_m2_DR', 'm1_m2_Mass', + 'muon Pt', 'muon AbsEta', 'muon PixHits', + 'muon JetCSVBtag', 'muon DZ', #'lead mu preselection', + 'lead mu pt', 'm1PFIDTight', 'sublead mu preselection', + 'muon veto', 'bjet veto', 'electron veto', + 'Jet presence', 'muon isolation', 'MET', 'region assignment' +] + def control_region(row): # Figure out what control region we are in. if row.m1RelPFIsoDB < 0.15 and row.m1MtToMET > 35 and row.m2MtToMET < 35: - return 'wjets' + return 'wjetsLtLow' elif row.m1RelPFIsoDB > 0.3 and row.type1_pfMetEt < 25: return 'qcd' else: return None +region_for_event_list = os.environ.get('EVTLIST_REGION','') +SYNC = ('SYNC' in os.environ) and eval(os.environ['SYNC']) class FakeRatesMM(MegaBase): - tree = 'mm/final/Ntuple' + tree = 'mm/final/Ntuple' if not SYNC else 'Ntuple' def __init__(self, tree, outfile, **kwargs): super(FakeRatesMM, self).__init__(tree, outfile, **kwargs) # Use the cython wrapper @@ -47,8 +67,14 @@ def __init__(self, tree, outfile, **kwargs): self.pucorrector = mcCorrectors.make_puCorrector('doublemu') def begin(self): - for region in ['wjets', 'qcd']:#, 'all']: - for denom in ['pt10', 'pt20']: #, 'pt10b', 'pt10t', 'pt10f']: + self.book('', "CUT_FLOW", "Cut Flow", len(cut_flow_step), 0, len(cut_flow_step)) + xaxis = self.histograms['CUT_FLOW'].GetXaxis() + self.cut_flow_histo = self.histograms['CUT_FLOW'] + for i, name in enumerate(cut_flow_step): + xaxis.SetBinLabel(i+1, name) + + for region in ['wjets', 'wjetsLtLow', 'qcd']:#, 'all']: + for denom in ['pt10', 'pt20']: denom_key = (region, denom) denom_histos = {} self.histograms[denom_key] = denom_histos @@ -57,7 +83,7 @@ def begin(self): denom_histos['muonInfo'] = self.book( os.path.join(region, denom), 'muonInfo', "muonInfo", - 'muonPt:muonJetPt:muonJetCSVBtag:muonPVDXY:weight:'+':'.join(self.lepIds), + 'muonPt:muonJetPt:muonAbsEta:muonJetCSVBtag:muonPVDXY:muonJetMass:numJets20:numJets40:weight:'+':'.join(self.lepIds), type=ROOT.TNtuple) for numerator in self.lepIds: @@ -99,24 +125,45 @@ def book_histo(name, *args, **kwargs): def process(self): - def preselection(row): + cut_flow_histo = self.cut_flow_histo + cut_flow_trk = cut_flow_tracker(cut_flow_histo) + cut_flow_trk.Fill('bare') + + def preselection(row, cut_flow_trk): double_mu_pass = row.doubleMuPass and \ row.m1MatchesDoubleMuPaths > 0 and \ row.m2MatchesDoubleMuPaths > 0 double_muTrk_pass = row.doubleMuTrkPass and \ - row.m1MatchesDoubleMuTrkPaths > 0 and \ - row.m2MatchesDoubleMuTrkPaths > 0 + row.m1MatchesMu17TrkMu8Path > 0 and \ + row.m2MatchesMu17TrkMu8Path > 0 if not ( double_mu_pass or double_muTrk_pass ): return False + cut_flow_trk.Fill('trigger') if not row.m1_m2_SS: return False + cut_flow_trk.Fill('m1_m2_SS') + + if row.m1_m2_DR < 0.5: return False + cut_flow_trk.Fill('m1_m2_DR') + if row.m2Pt > row.m1Pt: return False if row.m1_m2_Mass < 20: return False + cut_flow_trk.Fill('m1_m2_Mass') + + if not selections.muSelection(row, 'm1', cut_flow_trk): return False if not row.m1Pt > 20: return False + cut_flow_trk.Fill('lead mu pt') if not row.m1PFIDTight: return False - if not selections.muSelection(row, 'm1'): return False + cut_flow_trk.Fill('m1PFIDTight') + #cut_flow_trk.Fill('lead mu preselection') + if not selections.muSelection(row, 'm2'): return False - if not selections.vetos(row): return False #applies mu bjet e additional tau vetoes - if not (row.jetVeto40_DR05 >= 1): return False + cut_flow_trk.Fill('sublead mu preselection') + + if not selections.vetos(row, cut_flow_trk): return False #applies mu bjet e additional tau vetoes + #if not (row.jetVeto40_DR05 >= 1): return False + if row.jetVeto20 == 0: return False + cut_flow_trk.Fill('Jet presence') + return True def fill(the_histos, row, fillNtuple=False): @@ -145,39 +192,55 @@ def fill(the_histos, row, fillNtuple=False): pfidiso02 = float( row.m2PFIDTight and row.m2RelPFIsoDB < 0.2) h2taucuts = float( row.m2PFIDTight and ((row.m2RelPFIsoDB < 0.15 and row.m2AbsEta < 1.479) or row.m2RelPFIsoDB < 0.1 )) h2taucuts020 = float( row.m2PFIDTight and ((row.m2RelPFIsoDB < 0.20 and row.m2AbsEta < 1.479) or row.m2RelPFIsoDB < 0.15)) - the_histos['muonInfo'].Fill( array("f", [row.m2Pt, row.m2JetPt, max(0, row.m2JetCSVBtag), abs(row.m2PVDXY), weight, pfidiso02, h2taucuts, h2taucuts020] ) ) - + muon_jet_mass = -1. #inv_mass(row.m1Pt, row.m1Eta, row.m1Phi, row.leadingJetPt, row.leadingJetEta, row.leadingJetPhi) + + the_histos['muonInfo'].Fill( array("f", [row.m2Pt, max(row.m2JetPt, row.m2Pt), row.m2AbsEta, max(0, row.m2JetCSVBtag), + abs(row.m2PVDXY), muon_jet_mass, row.jetVeto20, row.jetVeto40_DR05, weight, + pfidiso02, h2taucuts, h2taucuts020] ) ) + + def fill_region(region, tag): + # This is a QCD or Wjets + fill(histos[(region, tag)], row, True) + + if row.m2PFIDTight: + if row.m2RelPFIsoDB < 0.2: + fill(histos[(region, tag, 'pfidiso02')], row) + + if (row.m2RelPFIsoDB < 0.15 and row.m2AbsEta < 1.479) or row.m2RelPFIsoDB < 0.1: + fill(histos[(region, tag, 'h2taucuts')], row) + + if (row.m2RelPFIsoDB < 0.20 and row.m2AbsEta < 1.479) or row.m2RelPFIsoDB < 0.15: + fill(histos[(region, tag, 'h2taucuts020')], row) + histos = self.histograms for row in self.tree: - if not preselection(row): + cut_flow_trk.new_row(row.run,row.lumi,row.evt) + cut_flow_trk.Fill('bare') + if not preselection(row, cut_flow_trk): continue + region = control_region(row) + if row.m1RelPFIsoDB > 0.3: + cut_flow_trk.Fill('muon isolation') + if row.type1_pfMetEt < 25: + cut_flow_trk.Fill('MET') + if region is None: continue + cut_flow_trk.Fill('region assignment') - def fill_region(region, tag): - # This is a QCD or Wjets - fill(histos[(region, tag)], row, True) - - if row.m2PFIDTight: - if row.m2RelPFIsoDB < 0.2: - fill(histos[(region, tag, 'pfidiso02')], row) - - if (row.m2RelPFIsoDB < 0.15 and row.m2AbsEta < 1.479) or row.m2RelPFIsoDB < 0.1: - fill(histos[(region, tag, 'h2taucuts')], row) + if region_for_event_list and region == region_for_event_list: + print '%i:%i:%i' % (row.run, row.lumi, row.evt) + continue - if (row.m2RelPFIsoDB < 0.20 and row.m2AbsEta < 1.479) or row.m2RelPFIsoDB < 0.15: - fill(histos[(region, tag, 'h2taucuts020')], row) - - #if (row.m2RelPFIsoDB < 0.25 and row.m2AbsEta < 1.479) or row.m2RelPFIsoDB < 0.20: - # fill(histos[(region, tag, 'h2taucuts025')], row) - fill_region(region, 'pt10') - #fill_region('all', 'pt10') + if region == 'wjetsLtLow' and row.m1MtToMET > 55: + fill_region('wjets', 'pt10') if row.m2Pt > 20: fill_region(region, 'pt20') - #fill_region('all', 'pt20') + if region == 'wjetsLtLow' and row.m1MtToMET > 55: + fill_region('wjets', 'pt20') def finish(self): self.write_histos() diff --git a/wh/PlotControlEM.py b/wh/PlotControlEM.py index c4ef81c1..78901e67 100644 --- a/wh/PlotControlEM.py +++ b/wh/PlotControlEM.py @@ -8,6 +8,9 @@ import glob from FinalStateAnalysis.PlotTools.Plotter import Plotter from FinalStateAnalysis.MetaData.data_styles import data_styles +import logging +import sys +logging.basicConfig(stream=sys.stderr, level=logging.INFO) jobid = os.environ['jobid'] @@ -92,7 +95,6 @@ def correct_for_contrib_in_fakes(x, fudge_factor=1.0): 0.95 if sqrts == 7 else 1.0) } -print plotter.views.keys() ww_name = 'WWJetsTo2L2Nu' if sqrts == 7 else 'WWJetsTo2L2Nu_TuneZ2_8TeV' plotter.views['WWJetsTo2L2Nu-no-fakes'] = { 'view': correct_for_contrib_in_fakes(plotter.views[ww_name]['view']) @@ -129,31 +131,32 @@ def correct_for_contrib_in_fakes(x, fudge_factor=1.0): for suffix, samples in [('', os_ss_samples), ('-fakes', fakes_samples)]: plotter.mc_samples = samples + #from pdb import set_trace; set_trace() plotter.plot_mc_vs_data('em', 'emMass', rebin=5, leftside=False, - xaxis='m_{e#mu} (GeV)') + xaxis='m_{e#mu} (GeV)')#, show_ratio=True) plotter.add_cms_blurb(sqrts) plotter.save('mass' + suffix) plotter.plot_mc_vs_data('em', 'emMass', rebin=10, leftside=False, - xaxis='m_{e#mu} (GeV)') + xaxis='m_{e#mu} (GeV)')#, show_ratio=True) plotter.add_cms_blurb(sqrts) plotter.save('mass_rebin' + suffix) - plotter.plot_mc_vs_data('em', 'mPt', rebin=10) + plotter.plot_mc_vs_data('em', 'mPt', rebin=10, show_ratio=True) plotter.save('mPt' + suffix) - plotter.plot_mc_vs_data('em', 'ePt', rebin=10) + plotter.plot_mc_vs_data('em', 'ePt', rebin=10, show_ratio=True) plotter.save('ePt' + suffix) - plotter.plot_mc_vs_data('em', 'mAbsEta', rebin=5) + plotter.plot_mc_vs_data('em', 'mAbsEta', rebin=5, show_ratio=True) plotter.save('mAbsEta' + suffix) - plotter.plot_mc_vs_data('em', 'eAbsEta', rebin=5) + plotter.plot_mc_vs_data('em', 'eAbsEta', rebin=5, show_ratio=True) plotter.save('eAbsEta' + suffix) - plotter.plot_mc_vs_data('em', 'nvtx') + plotter.plot_mc_vs_data('em', 'nvtx', show_ratio=True) plotter.save('nvtx' + suffix) - plotter.plot_mc_vs_data('em', 'bjetCSVVeto') + plotter.plot_mc_vs_data('em', 'bjetCSVVeto', show_ratio=True) plotter.save('bjetCSVVeto' + suffix) - plotter.plot_mc_vs_data('em', 'bjetVeto') + plotter.plot_mc_vs_data('em', 'bjetVeto', show_ratio=True) plotter.save('bjetVeto' + suffix) diff --git a/wh/PlotControlZEE.py b/wh/PlotControlZEE.py index b758adae..54c1feeb 100644 --- a/wh/PlotControlZEE.py +++ b/wh/PlotControlZEE.py @@ -8,13 +8,19 @@ import glob from FinalStateAnalysis.PlotTools.Plotter import Plotter from FinalStateAnalysis.PlotTools.MedianView import MedianView +from FinalStateAnalysis.PlotTools.DifferentialView import DifferentialView import rootpy.plotting.views as views +import rootpy.plotting as plotting from FinalStateAnalysis.PlotTools.HistToTGRaphErrors import HistStackToTGRaphErrors from FinalStateAnalysis.MetaData.data_styles import data_styles, colors import ROOT +import logging +import sys +logging.basicConfig(stream=sys.stderr, level=logging.INFO) ROOT.gROOT.SetBatch(True) - +ROOT.gStyle.SetOptStat(0) +ROOT.gStyle.SetOptTitle(0) class ControlZEEPlotter(Plotter): def __init__(self): @@ -22,11 +28,11 @@ def __init__(self): self.output_dir = os.path.join('results', self.jobid, 'plots', 'zee') samples = [ 'Zjets_M50', - #'WZ*', - #'ZZ*', - #'WW*', + 'WZ*', + 'ZZ*', + 'WW*', 'TT*', - #'WplusJets*', + 'WplusJets*', "data_DoubleE*", ] files = [] @@ -36,7 +42,19 @@ def __init__(self): files.extend(glob.glob('results/%s/ControlZEE/%s.root' % (self.jobid, x))) lumifiles.extend(glob.glob('inputs/%s/%s.lumicalc.sum' % (self.jobid, x))) super(ControlZEEPlotter, self).__init__(files, lumifiles, self.output_dir) - self.mc_samples = ['Zjets_M50'] + self.mc_samples = filter(lambda x: 'data' not in x, samples) + + def make_fakes_view(self, data_view, sign, weight_type): + # View of weighted obj1-fails data + obj1_view = views.SubdirectoryView(data_view, '%s/f1p2/%s' % (sign, weight_type)) + # View of weighted obj2-fails data + obj2_view = views.SubdirectoryView(data_view, '%s/p1f2/%s' % (sign, weight_type)) + # View of weighted obj1&2-fails data + obj12_view = views.SubdirectoryView(data_view, '%s/f1f2/%s' % (sign, weight_type)) + # Give the individual object views nice colors + subtract_obj12_view = views.ScaleView(obj12_view, -1) + return obj1_view, obj2_view, subtract_obj12_view + def get_flip_data(self, rebin=1, xaxis='', data_type='data'): data_view = self.get_view(data_type) @@ -45,57 +63,54 @@ def get_flip_data(self, rebin=1, xaxis='', data_type='data'): ss_p1p2_view = views.SubdirectoryView( data_view, 'ss/p1p2') ss_p1p2_view = views.TitleView( views.StyleView(ss_p1p2_view, **data_styles['data*']), 'observed;%s' % xaxis) - def make_fakes_view(sign, weight_type): - # View of weighted obj1-fails data - obj1_view = views.SubdirectoryView(data_view, '%s/f1p2/%s' % (sign, weight_type)) - # View of weighted obj2-fails data - obj2_view = views.SubdirectoryView(data_view, '%s/p1f2/%s' % (sign, weight_type)) - # View of weighted obj1&2-fails data - obj12_view = views.SubdirectoryView(data_view, '%s/f1f2/%s' % (sign, weight_type)) - # Give the individual object views nice colors - subtract_obj12_view = views.ScaleView(obj12_view, -1) - return obj1_view, obj2_view, subtract_obj12_view + #def make_fakes_view(sign, weight_type): + # # View of weighted obj1-fails data + # obj1_view = views.SubdirectoryView(data_view, '%s/f1p2/%s' % (sign, weight_type)) + # # View of weighted obj2-fails data + # obj2_view = views.SubdirectoryView(data_view, '%s/p1f2/%s' % (sign, weight_type)) + # # View of weighted obj1&2-fails data + # obj12_view = views.SubdirectoryView(data_view, '%s/f1f2/%s' % (sign, weight_type)) + # # Give the individual object views nice colors + # subtract_obj12_view = views.ScaleView(obj12_view, -1) + # return obj1_view, obj2_view, subtract_obj12_view #Get fakes according to WJets or QCD - ss_f1p2_qcd_view, ss_p1f2_qcd_view, ss_f1f2_qcd_view = make_fakes_view('ss','qcd_w') - ss_f1p2_wje_view, ss_p1f2_wje_view, ss_f1f2_wje_view = make_fakes_view('ss','wjet_w') + #ss_f1p2_qcd_view, ss_p1f2_qcd_view, ss_f1f2_qcd_view = self.make_fakes_view(data_view, 'ss','qcd_w') + ss_f1p2_wje_view, ss_p1f2_wje_view, ss_f1f2_wje_view = self.make_fakes_view(data_view, 'ss','wjet_w') - ss_fakes_1 = MedianView(ss_f1p2_qcd_view, ss_f1p2_wje_view) - ss_fakes_2 = MedianView(ss_p1f2_qcd_view, ss_p1f2_wje_view) - ss_fakes_12 = MedianView(ss_f1f2_qcd_view, ss_f1f2_wje_view) + ss_fakes_1 = ss_f1p2_wje_view #MedianView(lowv=ss_f1p2_qcd_view, highv=ss_f1p2_wje_view) + ss_fakes_2 = ss_p1f2_wje_view #MedianView(lowv=ss_p1f2_qcd_view, highv=ss_p1f2_wje_view) + ss_fakes_12 = ss_f1f2_wje_view #MedianView(lowv=ss_f1f2_qcd_view, highv=ss_f1f2_wje_view) ss_fakes_est = views.SumView(ss_fakes_1, ss_fakes_2, ss_fakes_12) ss_fakes_est = views.TitleView( views.StyleView(ss_fakes_est, **data_styles['Zjets*']), 'Fakes;%s' % xaxis) - ## os_f1p2_qcd_views, os_p1f2_qcd_views, os_f1f2_qcd_views = zip(make_fakes_view('os', 'qcd_w/charge_weightSysUp'), make_fakes_view('os', 'qcd_w/charge_weightSysDwn')) - ## os_f1p2_wje_views, os_p1f2_wje_views, os_f1f2_wje_views = zip(make_fakes_view('os', 'wjet_w/charge_weightSysUp'),make_fakes_view('os', 'wjet_w/charge_weightSysDwn')) - - ## os_f1p2_qcd_view = MedianView( *os_f1p2_qcd_views ) - ## os_p1f2_qcd_view = MedianView( *os_p1f2_qcd_views ) - ## os_f1f2_qcd_view = MedianView( *os_f1f2_qcd_views ) - - ## os_f1p2_wje_view = MedianView( *os_f1p2_wje_views ) - ## os_p1f2_wje_view = MedianView( *os_p1f2_wje_views ) - ## os_f1f2_wje_view = MedianView( *os_f1f2_wje_views ) - - ## os_fakes_1 = MedianView(os_f1p2_qcd_view, os_f1p2_wje_view) - ## os_fakes_2 = MedianView(os_p1f2_qcd_view, os_p1f2_wje_view) - ## os_fakes_12 = MedianView(os_f1f2_qcd_view, os_f1f2_wje_view) - ## os_fakes_est = views.SumView(os_fakes_1, os_fakes_2, os_fakes_12) - ## neg_os_fakes = views.ScaleView(os_fakes_est, -1) - os_flip_est_up = views.SubdirectoryView( data_view, 'os/p1p2/charge_weightSysUp') os_flip_est = views.SubdirectoryView( data_view, 'os/p1p2/charge_weight') - os_flip_est = MedianView(highv=os_flip_est_up, centv=os_flip_est) + #os_flip_est = MedianView(highv=os_flip_est_up, centv=os_flip_est) os_flip_est_nofake = os_flip_est#views.SumView(os_flip_est, neg_os_fakes) os_flip_est_nofake = views.TitleView( views.StyleView(os_flip_est_nofake, **data_styles['WZ*']), 'charge-fakes;%s' % xaxis) return ss_p1p2_view, ss_fakes_est, os_flip_est_nofake - def make_charge_flip_control_plot(self, variable, xaxis='', rebin=1, legend_on_the_left=False, data_type='data', x_range=None): - ss_p1p2_view, ss_fakes_est, os_flip_est_nofake = self.get_flip_data(rebin,xaxis,data_type) - events_estimate = views.StackView( ss_fakes_est,os_flip_est_nofake) + def make_charge_flip_control_plot(self, variable, xaxis='', rebin=1, legend_on_the_left=False, + data_type='data', x_range=None, apply_scale='', show_ratio=False, + differential=False): + ss_p1p2_view, ss_fakes_est, os_flip_est_nofake = self.get_flip_data(rebin, xaxis, data_type) + + if differential: + ss_p1p2_view = DifferentialView(ss_p1p2_view ) + ss_fakes_est = DifferentialView(ss_fakes_est ) + os_flip_est_nofake = DifferentialView(os_flip_est_nofake) + + fakes_hist = ss_fakes_est.Get(variable) + flip_hist = os_flip_est_nofake.Get(variable) + if apply_scale: + flip_hist = MedianView.apply_view(flip_hist, os_flip_est_nofake.Get(variable+apply_scale)) obs_hist = ss_p1p2_view.Get(variable) - estimate_hist = events_estimate.Get(variable) + estimate_hist = plotting.HistStack() + estimate_hist.Add(fakes_hist) + estimate_hist.Add(flip_hist) + estimate_error = HistStackToTGRaphErrors( estimate_hist ) estimate_error.SetFillStyle(3013) estimate_error.SetFillColor(ROOT.EColor.kBlack) @@ -127,17 +142,80 @@ def make_charge_flip_control_plot(self, variable, xaxis='', rebin=1, legend_on_t #legend.AddEntry(estimate_error,'f') legend.Draw() self.add_cms_blurb(self.sqrts) + if show_ratio: + self.add_ratio_plot(obs_hist, estimate_hist, x_range, ratio_range=0.2) + + + def plot_zee_control(self, variable, xaxis='', rebin=1, legend_on_the_left=False, + x_range=None, show_ratio=False, logscale=False): + data_view = self.get_view('data') + data_view = self.rebin_view(data_view, rebin) if rebin != 1 else data_view + mc_views = [self.get_view(i) for i in [ 'ZZ*', 'WZ*', 'WW*', 'TT*', 'Zjets_M50' ] ] + if rebin != 1: + mc_views = [self.rebin_view(i, rebin) for i in mc_views] + + zee_data = views.SubdirectoryView( data_view, 'os/p1p2/') + zee_mcs = [ views.SubdirectoryView( i, 'os/p1p2/') for i in mc_views] + + #os_f1p2_qcd_view, os_p1f2_qcd_view, os_f1f2_qcd_view = self.make_fakes_view(data_view, 'os','qcd_w') + os_f1p2_wje_view, os_p1f2_wje_view, os_f1f2_wje_view = self.make_fakes_view(data_view, 'os','wjet_w') + os_fakes_1 = os_f1p2_wje_view #MedianView(lowv=os_f1p2_qcd_view, highv=os_f1p2_wje_view) + os_fakes_2 = os_p1f2_wje_view #MedianView(lowv=os_p1f2_qcd_view, highv=os_p1f2_wje_view) + os_fakes_12 = os_f1f2_wje_view #MedianView(lowv=os_f1f2_qcd_view, highv=os_f1f2_wje_view) + os_fakes_est = views.SumView(os_fakes_1, os_fakes_2, os_fakes_12) + os_fakes_est = views.TitleView( views.StyleView(os_fakes_est, **data_styles['WplusJets*']), 'Fakes;%s' % xaxis) + + zee_mcs = zee_mcs[:-1]+[os_fakes_est]+zee_mcs[-1:] + events_estimate = views.StackView( *zee_mcs) + estimate_hist = events_estimate.Get(variable) + obs_hist = zee_data.Get(variable) + hmax = max( [ estimate_hist.GetMaximum(), max(list(obs_hist)) ] ) + if logscale: + obs_hist.GetYaxis().SetRangeUser(10**-2,hmax*10**4) + self.pad.SetLogy(True) + else: + obs_hist.GetYaxis().SetRangeUser(0.,hmax*1.3) + + if x_range: + obs_hist.GetXaxis().SetRangeUser(x_range[0],x_range[1]) + + obs_hist.Draw() + estimate_hist.Draw('same') + obs_hist.Draw('same') + self.canvas.Update() + self.keep.extend([ + estimate_hist, + obs_hist + ]) + + legend = self.add_legend([obs_hist], leftside=legend_on_the_left, entries=len(zee_mcs)+1) + legend.AddEntry(estimate_hist,'f') + #legend.AddEntry(estimate_error,'f') + legend.Draw() + if show_ratio: + self.add_ratio_plot(obs_hist, estimate_hist, x_range, ratio_range=0.2) + self.add_cms_blurb(self.sqrts) plotter = ControlZEEPlotter() +########################################################################### +## CHARGE FLIP CONTROL PLOTS ######################################### +########################################################################### + #Charge flip control plots -plotter.make_charge_flip_control_plot('TrkMass','Tracker Inv Mass (GeV)',2) +plotter.make_charge_flip_control_plot('TrkMass','Tracker Inv Mass (GeV)', + [40,50,60,70,75,80]+[80+i for i in range(1,21,2)]+[105,110,120,130,140], + apply_scale='_high', show_ratio=True, differential=True) plotter.save('EE_Charge_Flip_xcheck_trk_invMass') plotter.canvas.SaveAs('test.root') #plotter.save('EE_Charge_Flip_xcheck_trk_invMass_log') +plotter.make_charge_flip_control_plot('TrkMass','Tracker Inv Mass (GeV)',[40, 150],apply_scale='_high', + show_ratio=True) +plotter.save('EE_Charge_Flip_xcheck_trk_invMass_counting') + plotter.make_charge_flip_control_plot('TrkMass_NOSCALE','Tracker Inv Mass (GeV)',2) plotter.save('EE_Charge_Flip_xcheck_trk_invMass_NoScale') @@ -176,3 +254,93 @@ def make_charge_flip_control_plot(self, variable, xaxis='', rebin=1, legend_on_t plotter.make_charge_flip_control_plot('ePt','electron p_{T}',2,x_range=[0,200], data_type='Zjets_M50') plotter.save('EE_Charge_Flip_closure_ePt') + +########################################################################### +## DATA/MC PLOTS ######################################### +########################################################################### + +#plotter.plot_mc_vs_data('os/p1p2/', 'TrkMass', rebin=2, xaxis='m_{#mu#mu} (GeV)', +# leftside=False, show_ratio=True) +#plotter.add_cms_blurb(plotter.sqrts) +#for obj in plotter.keep: +# if obj.ClassName().startswith('TH1F'): +# print obj.GetTitle(), obj.Integral() +# if obj.ClassName().startswith('THStack'): +# for histo in obj.GetHists(): +# print histo.GetTitle(), histo.Integral() +# +#plotter.save('mass') +# +# +#plotter.plot_mc_vs_data('os/p1p2/', 'TrkMass', rebin=2, xaxis='m_{#mu#mu} (GeV)', +# leftside=False, show_ratio=True) +#plotter.add_cms_blurb(plotter.sqrts) +#plotter.pad.SetLogy(True) +#plotter.save('mass_log') +# +#plotter.plot_mc_vs_data('os/p1p2/', 'TrkMass', rebin=10, xaxis='m_{#mu#mu} (GeV)', +# leftside=False, show_ratio=True) +#plotter.add_cms_blurb(plotter.sqrts) +#plotter.save('mass_rebin') +# +#plotter.plot_mc_vs_data('os/p1p2/', 'e1Pt', leftside=False, show_ratio=True) +#plotter.save('e1Pt') +#plotter.plot_mc_vs_data('os/p1p2/', 'e1Pt', 5, leftside=False, show_ratio=True) +#plotter.save('e1Pt_rebin') +#plotter.plot_mc_vs_data('os/p1p2/', 'e2Pt', leftside=False, show_ratio=True) +#plotter.save('e2Pt') +#plotter.plot_mc_vs_data('os/p1p2/', 'e2Pt', 5, leftside=False, show_ratio=True) +#plotter.save('e2Pt_rebin') +# +#plotter.plot_mc_vs_data('os/p1p2/', 'e1AbsEta', leftside=False, show_ratio=True) +#plotter.save('e1AbsEta') +#plotter.plot_mc_vs_data('os/p1p2/', 'e2AbsEta', leftside=False, show_ratio=True) +#plotter.save('e2AbsEta') +# +#plotter.plot_mc_vs_data('os/p1p2/', 'e1AbsEta', 5, leftside=False, show_ratio=True) +#plotter.save('e1AbsEta_rebin') +#plotter.plot_mc_vs_data('os/p1p2/', 'e2AbsEta', 5, leftside=False, show_ratio=True) +#plotter.save('e2AbsEta_rebin') +# +# +#plotter.plot_mc_vs_data('os/p1p2/', "trig_weight" , 5, leftside=False, show_ratio=True) +#plotter.save('trig_weight') +# +#plotter.plot_mc_vs_data('os/p1p2/', "PU_weight" , 5, leftside=False, show_ratio=True) +#plotter.save('PU_weight') +# +#plotter.plot_mc_vs_data('os/p1p2/', "idIso_weight", 5, leftside=False, show_ratio=True) +#plotter.save('idIso_weight') + + +#plotter.plot_mc_vs_data('os/p1p2/', 'nvtx', show_ratio=True) +#plotter.save('nvtx') + +########################################################################### +## DATA/MC+FAKES PLOTS ######################################### +########################################################################### + +plotter.plot_zee_control('TrkMass', rebin=2, xaxis='m_{#mu#mu} (GeV)', + legend_on_the_left=False, show_ratio=True, logscale=True) +plotter.save('mass_wfakes_log') + +#print data_styles.keys() +plotter.plot_zee_control('TrkMass', rebin=2, xaxis='m_{#mu#mu} (GeV)', + legend_on_the_left=False, show_ratio=True) +plotter.save('mass_wfakes') + +plotter.plot_zee_control('e1Pt', rebin=5, xaxis='p_{T}^{#mu1} (GeV)', + legend_on_the_left=False, show_ratio=True, x_range=[0,200]) +plotter.save('e1Pt_wfakes') + +plotter.plot_zee_control('e2Pt', rebin=5, xaxis='p_{T}^{#mu2} (GeV)', + legend_on_the_left=False, show_ratio=True, x_range=[0,200]) +plotter.save('e2Pt_wfakes') + +plotter.plot_zee_control('e1AbsEta', rebin=5, xaxis='|#eta|^{#mu1}', + legend_on_the_left=False, show_ratio=True) +plotter.save('e1AbsEta_wfakes') + +plotter.plot_zee_control('e2AbsEta', rebin=5, xaxis='|#eta|^{#mu2}', + legend_on_the_left=False, show_ratio=True) +plotter.save('e2AbsEta_wfakes') diff --git a/wh/PlotControlZMM.py b/wh/PlotControlZMM.py index 237004e3..a9affcf7 100644 --- a/wh/PlotControlZMM.py +++ b/wh/PlotControlZMM.py @@ -7,6 +7,9 @@ import os import glob from FinalStateAnalysis.PlotTools.Plotter import Plotter +import logging +import sys +logging.basicConfig(stream=sys.stderr, level=logging.INFO) jobid = os.environ['jobid'] @@ -14,11 +17,11 @@ samples = [ 'Zjets_M50', - #'WZ*', - #'ZZ*', - #'WW*', + 'WZ*', + 'ZZ*', + 'WW*', 'TT*', - #'WplusJets*', + 'WplusJets*', "data_DoubleMu*", ] @@ -30,36 +33,36 @@ lumifiles.extend(glob.glob('inputs/%s/%s.lumicalc.sum' % (jobid, x))) plotter = Plotter(files, lumifiles, output_dir) -plotter.mc_samples = ['Zjets_M50'] +plotter.mc_samples = filter(lambda x: 'data' not in x.lower(), samples) #['Zjets_M50'] sqrts = 7 if '7TeV' in jobid else 8 -plotter.plot_mc_vs_data('zmm', 'm1m2Mass', rebin=2, xaxis='m_{#mu#mu} (GeV)') +plotter.plot_mc_vs_data('zmm', 'm1m2Mass', rebin=2, xaxis='m_{#mu#mu} (GeV)')#, show_ratio=True) plotter.add_cms_blurb(sqrts) plotter.save('mass') -plotter.plot_mc_vs_data('zmm', 'm1m2Mass', rebin=6, xaxis='m_{#mu#mu} (GeV)') +plotter.plot_mc_vs_data('zmm', 'm1m2Mass', rebin=6, xaxis='m_{#mu#mu} (GeV)')#, show_ratio=True) plotter.add_cms_blurb(sqrts) plotter.save('mass_rebin') -plotter.plot_mc_vs_data('zmm', 'm1Pt') +plotter.plot_mc_vs_data('zmm', 'm1Pt', show_ratio=True) plotter.save('m1Pt') -plotter.plot_mc_vs_data('zmm', 'm1Pt', 5) +plotter.plot_mc_vs_data('zmm', 'm1Pt', 5, show_ratio=True) plotter.save('m1Pt_rebin') -plotter.plot_mc_vs_data('zmm', 'm2Pt') +plotter.plot_mc_vs_data('zmm', 'm2Pt', show_ratio=True) plotter.save('m2Pt') -plotter.plot_mc_vs_data('zmm', 'm2Pt', 5) +plotter.plot_mc_vs_data('zmm', 'm2Pt', 5, show_ratio=True) plotter.save('m2Pt_rebin') -plotter.plot_mc_vs_data('zmm', 'm1AbsEta') +plotter.plot_mc_vs_data('zmm', 'm1AbsEta', show_ratio=True) plotter.save('m1AbsEta') -plotter.plot_mc_vs_data('zmm', 'm2AbsEta') +plotter.plot_mc_vs_data('zmm', 'm2AbsEta', show_ratio=True) plotter.save('m2AbsEta') -plotter.plot_mc_vs_data('zmm', 'm1AbsEta', 5) +plotter.plot_mc_vs_data('zmm', 'm1AbsEta', 5, show_ratio=True) plotter.save('m1AbsEta_rebin') -plotter.plot_mc_vs_data('zmm', 'm2AbsEta', 5) +plotter.plot_mc_vs_data('zmm', 'm2AbsEta', 5, show_ratio=True) plotter.save('m2AbsEta_rebin') -plotter.plot_mc_vs_data('zmm', 'nvtx') +plotter.plot_mc_vs_data('zmm', 'nvtx', show_ratio=True) plotter.save('nvtx') diff --git a/wh/Rakefile b/wh/Rakefile index 7b4e87cd..190bcaff 100755 --- a/wh/Rakefile +++ b/wh/Rakefile @@ -2,6 +2,7 @@ recipes = ENV['CMSSW_BASE'] + '/src/FinalStateAnalysis/PlotTools/rake/recipes.rake' import recipes require ENV['CMSSW_BASE'] + '/src/FinalStateAnalysis/PlotTools/rake/tools.rb' +require 'pathname' $jobid = ENV['jobid'] $blind = ENV['blind'] @@ -12,7 +13,8 @@ if $jobid.include? '7TeV' $period = '7TeV' end -check_luminosity(['data_DoubleElectron', 'data_DoubleMu', 'data_MuEG'], $period, $jobid) +#check_luminosity(['data_DoubleElectron', 'data_DoubleMu', 'data_MuEG'], $period, $jobid) +check_luminosity(['data_DoubleMu'], $period, $jobid) task :getlumi, [:sample] do |t, args| puts get_lumi(args.sample, $jobid) @@ -33,10 +35,13 @@ end #puts get_sample_names('data_DoubleMu') samples = Hash[ - "ewk" => Array['Zjets_M50', 'WplusJets_madgraph', 'TTplusJets_madgraph'] + get_sample_names('WZ') + get_sample_names('WWJet')+ get_sample_names('ZZ'), #'ZZJetsTo4L_pythia', + "ewk" => Array['Zjets_M50', 'WplusJets_madgraph'] + get_sample_names('TTplusJets') + get_sample_names('WZ') + get_sample_names('WWJet')+ get_sample_names('ZZ'), #'ZZJetsTo4L_pythia', "wjets" => Array['WplusJets_madgraph'], + "ttbar" => get_sample_names('TTplusJets'), "zjets" => Array['Zjets_M50'], "diboson" => get_sample_names('ZZ')+get_sample_names('WZ') + get_sample_names('WWJet'), + "diboson4fakes" => get_sample_names('ZZ')+get_sample_names('WZJetsTo3LNu_ZToTauTau'), + "WZ" => get_sample_names('WZJetsTo3LNu_ZToTauTau'), "signal" => get_sample_names('VH') + get_sample_names('WH'), # Automagically figure out what data samples we have "data_m" => get_sample_names("data_SingleMu"), @@ -182,71 +187,95 @@ end def make_kNN(chan, datas) return Hash[ - "samples" => Array["diboson", "data_#{datas.downcase}"], + "samples" => Array["diboson4fakes", "data_#{datas.downcase}"], "analyzer" => "FakeRates#{chan.upcase}", 'fitter' => 'train_kNN.py', + #'neighbors' => neighbors, ] end -iso_points = ['idiso02', 'h2taucuts', 'h2taucuts020'] -electronIds = ['eid12Loose', 'eid12Medium', 'eid12Tight'] #, 'eid13Loose', 'eid13Tight'] +iso_points = ['h2taucuts', 'h2taucuts020'] #'idiso02', +electronIds = ['eid12Medium', 'eid12Tight'] #, 'eid13Loose', 'eid12Loose', 'eid13Tight'] elewps = electronIds.product(iso_points) elewps = elewps.map{|x,y| "#{x}_#{y}"} -pt_regions = ['pt10', 'pt20'] -muonwps = ['h2taucuts', 'h2taucuts020', 'pfidiso02'] +ee_wps = ['eid12Medium_h2taucuts020', 'eid12Tight_h2taucuts'] +em_wps = ['eid12Medium_h2taucuts'] + +pt_regions = ['pt10']#, 'pt20'] +muonwps = ['h2taucuts', 'h2taucuts020'] #, 'pfidiso02'] fr_fits = Hash.new kNN_trains = Hash.new -muon_kNN_vars= ['muonJetPt', 'muonPt', 'muonJetCSVBtag'] #'muonPVDXY'] -electron_kNN_vars= ['electronJetPt', 'electronPt'] - +muon_kNN_vars= ['muonJetPt', 'muonPt', 'numJets20'] +electron_kNN_vars= ['electronJetPt', 'electronPt', 'numJets20'] +neighbors = ['50'] # '100'] #, ] ############# # EM datas ############# -emcrs = ['qcd', 'wjets'] -elewps.each do |ele_wp| +emcrs = [ 'wjets', 'ttbar']#'qcd', +#elewps.select{|x| x.start_with?('eid12Medium')}.each do |ele_wp| +em_wps.each do |ele_wp| emcrs.each do |cr| - pt_regions.each do |ptr| - key = "e_#{cr}_#{ptr}_#{ele_wp}_eJetPt" - #fr_fits[key] = make_exponential('em', 'em') #fit EVERYTHING with exponential, in case change afterwards - - key = "e_#{cr}_#{ptr}_#{ele_wp}_electronInfo" - kNN_trains[key] = make_kNN('em', 'em') - kNN_trains[key]['vars'] = electron_kNN_vars + neighbors.each do |k| + pt_regions.each do |ptr| + key = "em_#{cr}_#{ptr}_#{ele_wp}_eJetPt" + #fr_fits[key] = make_exponential('em', 'em') #fit EVERYTHING with exponential, in case change afterwards + + key = "em_#{cr}_#{ptr}_#{ele_wp}_electronInfo_k#{k}" + kNN_trains[key] = make_kNN('em', 'em') + kNN_trains[key]['vars'] = electron_kNN_vars + + if cr == 'ttbar' + kNN_trains[key]['samples'] = Array['data_em'] + end + end end end end #muon fakes from em -#mmcrs = ['qcd', 'wjets'] +mmcrs = [ 'wjets', 'ttbar']#'qcd', #muonwps.each do |mu_wp| -# mmcrs.each do |cr| -# pt_regions.each do |ptr| -# key = "m_M#{cr}_#{ptr}_#{mu_wp}_muonInfo" -# kNN_trains[key] = make_kNN('em', 'em') -# kNN_trains[key]['vars'] = muon_kNN_vars -# end -# end -#end +['h2taucuts'].each do |mu_wp| + mmcrs.each do |cr| + neighbors.each do |k| + pt_regions.each do |ptr| + key = "em_M#{cr}_#{ptr}_#{mu_wp}_muonInfo_k#{k}" + kNN_trains[key] = make_kNN('em', 'em') + kNN_trains[key]['vars'] = muon_kNN_vars + + if cr == 'ttbar' + kNN_trains[key]['samples'] = Array['data_em'] + end + end + end + end +end ############# # EE datas ############# -eecrs = ['qcd', 'wjetsNoZmass'] -elewps.each do |ele_wp| - eecrs.each do |cr| - key = "ee_#{cr}_pt10_#{ele_wp}_electronJetPt" - #fr_fits[key] = make_landau('ee', 'ee') #fit pt10 with landau - key = "ee_#{cr}_pt20_#{ele_wp}_electronJetPt" - #fr_fits[key] = make_exponential('ee', 'ee') #fit pt20 with expo - - key = "ee_#{cr}_pt10_#{ele_wp}_electronInfo" - kNN_trains[key] = make_kNN('ee', 'ee') - kNN_trains[key]['vars'] = electron_kNN_vars - key = "ee_#{cr}_pt20_#{ele_wp}_electronInfo" - kNN_trains[key] = make_kNN('ee', 'ee') - kNN_trains[key]['vars'] = electron_kNN_vars +eecrs = [ 'wjetsNoZmass'] #['qcd', +#elewps.each do |ele_wp| +ee_wps.each do |ele_wp| + neighbors.each do |k| + eecrs.each do |cr| + pt_regions.each do |ptr| + key = "ee_#{cr}_#{ptr}_#{ele_wp}_electronJetPt" + #fr_fits[key] = make_landau('ee', 'ee') #fit pt10 with landau + + key = "ee_#{cr}_#{ptr}_#{ele_wp}_electronInfo_k#{k}" + kNN_trains[key] = make_kNN('ee', 'ee') + kNN_trains[key]['vars'] = electron_kNN_vars + #if ele_wp == 'eid12Tight_h2taucuts': + # kNN_trains[key]['samples'] = Array["WZ", 'data_ee'] + #end + if cr == 'qcd' + kNN_trains[key]['samples'] = Array['data_ee'] + end + end + end end end #fr_fits["ee_wjetsNoZmass_pt10_eid12Medium_h2taucuts020_electronJetPt"] = make_constant('ee', 'ee') @@ -254,21 +283,26 @@ end ############# # MM datas ############# -mmcrs = ['qcd', 'wjets'] +muon_kNN_vars= ['muonJetPt', 'muonPt', 'numJets20'] +mmcrs = [ 'wjets']#'qcd', muonwps.each do |mu_wp| mmcrs.each do |cr| - pt_regions.each do |ptr| - key = "m_#{cr}_#{ptr}_#{mu_wp}_muonJetPt" - #fr_fits[key] = make_landau('mm', 'mm') - - key = "m_#{cr}_#{ptr}_#{mu_wp}_muonInfo" - kNN_trains[key] = make_kNN('mm', 'mm') - kNN_trains[key]['vars'] = muon_kNN_vars + neighbors.each do |k| + pt_regions.each do |ptr| + key = "mm_#{cr}_#{ptr}_#{mu_wp}_muonJetPt" + #fr_fits[key] = make_landau('mm', 'mm') + + key = "mm_#{cr}_#{ptr}_#{mu_wp}_muonInfo_k#{k}" + kNN_trains[key] = make_kNN('mm', 'mm') + kNN_trains[key]['vars'] = muon_kNN_vars + + if cr == 'qcd' + kNN_trains[key]['samples'] = Array['data_mm'] + end + end end end end -#fr_fits["m_qcd_pt20_h2taucuts_muonJetPt"] = make_cruijff('mm', 'mm') -#fr_fits["m_qcd_pt20_h2taucuts020_muonJetPt"] = make_cruijff('mm', 'mm') #Tau stuff (by hand, they are just 2) @@ -359,19 +393,20 @@ kNN_trains.each do |kNN, info| cut = '' var = '' - if kNN_configuration.size() == 6 #check if we have an idIso with _ separation + if kNN_configuration.size() == 7 #check if we have an idIso with _ separation region = kNN_configuration[1] denom = kNN_configuration[2] cut = "#{kNN_configuration[3]}_#{kNN_configuration[4]}" var = kNN_configuration[5] + neigh = kNN_configuration[6].sub('k','') else #otherwise as usual region = kNN_configuration[1] denom = kNN_configuration[2] cut = kNN_configuration[3] var = kNN_configuration[4] + neigh = kNN_configuration[5].sub('k','') end - knn_output = $frfit_dir + "/#{kNN}.kNN.root" # Expand sample list subsample_inputs = [] @@ -379,18 +414,42 @@ kNN_trains.each do |kNN, info| subsample_inputs += samples[sample] end subsamples_inputs_result_list = subsample_inputs.map{|x| "results/#{$jobid}/#{info['analyzer']}/#{x}.root"} - subsample_input_list = subsamples_inputs_result_list.join(" ") + + data_list = subsamples_inputs_result_list.select{|x| x.include? 'data_'} + mc_list = subsamples_inputs_result_list.select{|x| not x.include? 'data_'} + #subsample_input_list = subsamples_inputs_result_list.join(" ") # Path to tree in root files tree_path = Array[region, denom, var].join("/") vars_str = info['vars'].join(" ") - file knn_output => subsamples_inputs_result_list + [info['analyzer'] + '.py',] do |t| + + knn_all_output = $frfit_dir + "/#{kNN}.ALL.kNN.root" + file knn_all_output => data_list + [info['analyzer'] + '.py',] do |t| + sh "mkdir -p #{$frfit_dir}" + sh "train_kNN.py --files #{data_list.join(' ')} #{mc_list.join(' ')} --outputfile #{t.name} --tree #{tree_path} --cut #{cut} --variables #{vars_str} --makePlots 1 --neighbors 100" #{neigh}" + end + task :kNN => knn_all_output + kNN_files << knn_all_output + + knn_data_output = $frfit_dir + "/#{kNN}.data.kNN.root" + file $frfit_dir + "/#{kNN}.data.kNN.root" => data_list + [info['analyzer'] + '.py',] do |t| sh "mkdir -p #{$frfit_dir}" - sh "train_kNN.py --files #{subsample_input_list} --outputfile #{knn_output} --tree #{tree_path} --cut #{cut} --variables #{vars_str} --makePlots 0" + sh "train_kNN.py --files #{data_list.join(' ')} --outputfile #{t.name} --tree #{tree_path} --cut #{cut} --variables #{vars_str} --makePlots 1 --neighbors #{neigh}" + end + task :kNN => knn_data_output + kNN_files << knn_data_output + + mc_list.each do |mc_input| + base = Pathname.new(mc_input).basename.sub('.root','') + kNN_mc_output = $frfit_dir + "/#{kNN}.#{base}.kNN.root" + file kNN_mc_output => [mc_input] + [info['analyzer'] + '.py',] do |t| + sh "mkdir -p #{$frfit_dir}" + sh "train_kNN.py --files #{mc_input} --outputfile #{t.name} --tree #{tree_path} --cut #{cut} --variables #{vars_str} --makePlots 0 --neighbors #{neigh} --forceLumi -1" + end + task :kNN => kNN_mc_output + kNN_files << kNN_mc_output end - task :kNN => knn_output - kNN_files << knn_output end #Rake.application.tasks.each do |t| @@ -423,17 +482,18 @@ task :fits => :charge_fakes fake_rate_files = fr_fits.keys.map{|x| "#{$frfit_dir}/#{x}.root"} # IF the FR fits change, make sure we re-run the analyses -file "WHAnalyzeEMT.py" => kNN_files.select{|x| x.start_with?("#{$frfit_dir}/e_")} + \ - kNN_files.select{|x| x.start_with?("#{$frfit_dir}/m_")} + \ +file "WHAnalyzeEMT.py" => kNN_files.select{|x| x.start_with?("#{$frfit_dir}/em_")} + \ + #kNN_files.select{|x| x.start_with?("#{$frfit_dir}/m_")} + \ fake_rate_files.select{|x| x.start_with?("#{$frfit_dir}/t_")} + \ Array["optimizer.py", 'WHAnalyzerBase.py', "fakerate_functions.py", "mcCorrectors.py"] do |t| + puts t.investigation sh "touch #{t.name}" end -file "WHAnalyzeMMT.py" => kNN_files.select{|x| x.start_with?("#{$frfit_dir}/m_")} + \ +file "WHAnalyzeMMT.py" => kNN_files.select{|x| x.start_with?("#{$frfit_dir}/mm_")} + \ fake_rate_files.select{|x| x.start_with?("#{$frfit_dir}/t_")} + \ ["optimizer.py", 'WHAnalyzerBase.py', @@ -452,9 +512,9 @@ file "WHAnalyzeEET.py" => kNN_files.select{|x| x.start_with?( "#{$frfit_dir}/ee_ sh "touch #{t.name}" end -mmt_results = get_analyzer_results("WHAnalyzeMMT.py", samples['ewk'] + samples['data_mm'] + samples['signal']) -emt_results = get_analyzer_results("WHAnalyzeEMT.py", samples['ewk'] + samples['data_em'] + samples['signal']) -eet_results = get_analyzer_results("WHAnalyzeEET.py", samples['ewk'] + samples['data_ee'] + samples['signal']) +mmt_results = get_analyzer_results("WHAnalyzeMMT.py", samples['diboson'] + samples['data_mm'] + samples['signal'] + samples["ttbar"] + samples['zjets']) +emt_results = get_analyzer_results("WHAnalyzeEMT.py", samples['diboson'] + samples['data_em'] + samples['signal'] + samples["ttbar"] + samples['zjets']) +eet_results = get_analyzer_results("WHAnalyzeEET.py", samples['diboson'] + samples['data_ee'] + samples['signal'] + samples["ttbar"] + samples['zjets']) mmt_debug = get_analyzer_results("WHAnalyzeMMT.py", Array['Zjets_M50'] ) task :mmt_dbg => mmt_debug @@ -467,7 +527,7 @@ task :eet => eet_results task :mmcontrol => get_analyzer_results("ControlZMM.py", samples['ewk'] + samples['data_mm']) task :emcontrol => get_analyzer_results("ControlEM.py", samples['ewk'] + samples['data_em']) -task :eecontrol => get_analyzer_results("ControlZEE.py", samples['data_ee'] + samples["zjets"]) + Dir.glob("#{$frfit_dir}/ee*h2taucuts_electronJetPt.root") #samples['ewk'] + +task :eecontrol => get_analyzer_results("ControlZEE.py", samples['data_ee'] + samples["ewk"]) + Dir.glob("#{$frfit_dir}/ee*h2taucuts_electronJetPt.root") #samples['ewk'] + task :mmt_signal => get_analyzer_results("WHAnalyzeMMT.py", samples['signal']) task :emt_signal => get_analyzer_results("WHAnalyzeEMT.py", samples['signal']) @@ -523,6 +583,7 @@ file "#{$zeedir}/.plot_timestamp" => Array["PlotControlZEE.py"]+get_analyzer_res end task :plot_zee => "#{$zeedir}/.plot_timestamp" + ################################################################################ # Recipes to make signal plots # targets: @@ -555,15 +616,14 @@ file "#{$eetdir}/eet_shapes_#{$period}.root" => ['WHPlotterEET.py', 'WHPlotterBa end task :plot_eet => "#{$eetdir}/eet_shapes_#{$period}.root" -################################################################################ -# Recipes to make data cards (plots come for free) -# targets: -# mmt_shapes -# emt_shapes -# eet_shapes -# cards -# copycards -> move cards to official HTT CVS area -################################################################################ + +################################################################################# +#### +#### HELPER FUNCTIONS TO COMPUTE LIMITS +#### +################################################################################# +$carddir = "FIXME" #TO BE CHANGED BY THE DIFFERENT PARTS + #categories $categories_map = Hash[ @@ -572,6 +632,7 @@ $categories_map = Hash[ 'mmt' => File.open("card_config/cgs.conf.mmt.#{$period}").select {|x| x.start_with? 'categories'}[0].split(':')[1].split(',').map{|x| x.strip}, ] + # Recipes for adding stat. error shapes. Makes a new file task: # input_file_stat_errors.root => input_file.root def add_fake_errors(input_file, channel, prefix) @@ -583,55 +644,6 @@ def add_fake_errors(input_file, channel, prefix) return output_file end -mmt_shape_file = "#{$mmtdir}/mmt_shapes_#{$period}.root" -$categories_map['mmt'].each do |category| - mmt_shape_file = add_fake_errors(mmt_shape_file, "mmt", category) -end -task :mmt_shapes => mmt_shape_file - -emt_shape_file = "#{$emtdir}/emt_shapes_#{$period}.root" -$categories_map['emt'].each do |category| - emt_shape_file = add_fake_errors(emt_shape_file, "emt", category) -end -task :emt_shapes => emt_shape_file - -eet_shape_file = "#{$eetdir}/eet_shapes_#{$period}.root" -$categories_map['eet'].each do |category| - eet_shape_file = add_fake_errors(eet_shape_file, "eet", category) -end -task :eet_shapes => eet_shape_file - -$carddir = "results/#{$jobid}/cards" - -cardmasses = get_sample_names('VH') #get all the VH samples available -cardmasses = cardmasses.select{|x| x.include? "H2Tau"} #get only the H2Tau, to avid repetition -cardmasses = cardmasses.map{|x| x.sub("VH_H2Tau_M-","")} #remove trailing name, the leftover is the mass (as a string) -cardmasses = cardmasses.map{|x| Integer(x)} #maps to integers (don't know if it's really needed but can't harm) -cardmasses = cardmasses.select{|x| x <= 140} #FIXME! to be removed! -#puts cardmasses - -$to_extrapolate = cardmasses.select{|x| x > 140} - -def make_morphing_task(channel, shapefile) - ret = "#{$carddir}/#{channel}/shapes.root" - file ret => [shapefile] do |t| - sh "mkdir -p `dirname #{t.name}`" - sh "cp #{t.prerequisites[0]} #{t.name}" - sh "horizontal-morphing.py --categories='#{$categories_map[channel].join(',')}' --samples='WH_hww{MASS}' --uncerts='' --masses='110,120,130,140' --step-size=5 -i #{t.name}"# --extrapolate=#{$to_extrapolate.join(",")} #{t.name}" #FIXME! to be removed! - end - return ret -end - -mmt_morphed_shape_file = make_morphing_task('mmt', mmt_shape_file) -task :mmt_morphed_shapes => mmt_morphed_shape_file - -emt_morphed_shape_file = make_morphing_task('emt', emt_shape_file) -task :emt_morphed_shapes => emt_morphed_shape_file - -eet_morphed_shape_file = make_morphing_task('eet', eet_shape_file) -task :eet_morphed_shapes => eet_morphed_shape_file - - def make_cgs_conf_task(channel) ret = "#{$carddir}/#{channel}/cgs.conf" file ret => ["card_config/cgs.conf.#{channel}.#{$period}"] do |t| @@ -659,10 +671,11 @@ def make_unc_conf_task(channel, opttag='') return ret end -def make_unc_vals_task(channel, opttag='') +def make_unc_vals_task(channel, opttag='', use_postfit=false) ret = "#{$carddir}/#{channel}/unc.vals" + additional = Array[] file ret => ["card_config/unc.vals.#{channel}.#{$period}", - "#{$carddir}/#{channel}/shapes.root"] do |t| #+ Dir.glob("results/#{$jobid}/plots/#{channel}/*_statshapes.root") shapes.root depends on statshapes, so are redundant + "#{$carddir}/#{channel}/shapes.root"] do |t| #+ Dir.glob("results/#{$jobid}/plots/#{channel}/*_statshapes.root") shapes.root depends on statshapes, so are redundant # Copy the basic template carddir_channel = File.dirname(t.name) sh "mkdir -p #{carddir_channel}" @@ -673,8 +686,13 @@ def make_unc_vals_task(channel, opttag='') Dir.glob("results/#{$jobid}/plots/#{channel}/#{channel}#{opttag}_shapes_*_statshapes.txt").each do |list| category = list.split('/')[-1].split('_')[-2] sh "cat #{list} | xargs -n 1 -I {} echo '#{category} fakes {} 1.0' >> #{t.name}" + #sh "python get_fake_systematic.py #{carddir_channel}/shapes.root #{$categories_map[channel].join(',')} CMS_vhtt_#{category}_fakes_#{$period} >> #{t.name}" + end + if !use_postfit + sh "echo #{$categories_map[channel].join(',')} fakes CMS_vhtt_#{channel}_fakes_#{$period} 1.50 >> #{t.name}" + else + sh "./get_fake_systematic.py results/#{$jobid}/plots/#{channel}/f3/postfit/#{channel}.pulls.all.raw_txt CMS_vhtt_#{channel}_fakes_#{$period} 1.50 '#{$categories_map[channel].join(',')} fakes' >> #{t.name}" end - sh "python get_fake_systematic.py #{carddir_channel}/shapes.root #{$categories_map[channel].join(',')} CMS_vhtt_#{channel}_fakes_#{$period} >> #{t.name}" end end @@ -721,21 +739,264 @@ def combine_channels(mass) return card end +def copy_file(source, target) + file target => [source] do |t| + sh "mkdir -p `dirname #{t.name}`" + sh "cp -v #{t.prerequisites[0]} #{t.name}" + end + return target +end + + + +################################################################################# +#### Background-only f3 pulls, to convince yourself and the others ############# +#### (but mostly the others) that the background estimation is fine ############# +################################################################################# + +$carddir = "results/#{$jobid}/bkg-only-pulls" #change global var so we can reuse the functions + +mmt_f3_shape_file = "#{$mmtdir}/mmt_f3_shapes_#{$period}.root" +$categories_map['mmt'].each do |category| + mmt_f3_shape_file = add_fake_errors(mmt_f3_shape_file, "mmt_f3", category) +end +task :mmt_f3_shapes => mmt_f3_shape_file + +emt_f3_shape_file = "#{$emtdir}/emt_f3_shapes_#{$period}.root" +$categories_map['emt'].each do |category| + emt_f3_shape_file = add_fake_errors(emt_f3_shape_file, "emt_f3", category) +end +task :emt_f3_shapes => emt_f3_shape_file + +eet_f3_shape_file = "#{$eetdir}/eet_f3_shapes_#{$period}.root" +$categories_map['eet'].each do |category| + eet_f3_shape_file = add_fake_errors(eet_f3_shape_file, "eet_f3", category) +end +task :eet_f3_shapes => eet_f3_shape_file + +def make_copy_task(channel, shapefile) + ret = "#{$carddir}/#{channel}/shapes.root" + file ret => [shapefile] do |t| + sh "mkdir -p `dirname #{t.name}`" + sh "cp #{t.prerequisites[0]} #{t.name}" + end + return ret +end + +task :mmt_copied_f3_shapes => copy_file(mmt_f3_shape_file, "#{$carddir}/mmt/shapes.root") +task :emt_copied_f3_shapes => copy_file(emt_f3_shape_file, "#{$carddir}/emt/shapes.root") +task :eet_copied_f3_shapes => copy_file(eet_f3_shape_file, "#{$carddir}/eet/shapes.root") + +def make_f3_timestamp_task(channel) + timestamp = "#{$carddir}/#{channel}/.creation_timestamp" + #creates the tasks + make_unc_conf_task(channel, '_f3') + make_unc_vals_task(channel, '_f3') + make_cgs_conf_task(channel) + file timestamp => make_datacard_task(120, channel) do |t| + sh "touch #{t.name}" + end +end + +file "#{$carddir}/llt/.creation_timestamp" => combine_channels(120) do |t| + sh "touch #{t.name}" +end + + +task :f3_cards_mmt => make_f3_timestamp_task('mmt') +task :f3_cards_emt => make_f3_timestamp_task('emt') +task :f3_cards_eet => make_f3_timestamp_task('eet') +task :f3_cards_llt => "#{$carddir}/llt/.creation_timestamp" + +task :f3_cards => [:f3_cards_mmt, + :f3_cards_emt, + :f3_cards_eet, + :f3_cards_llt + ] + + +def make_pulls_task(channel) + pulls_timestamp = "#{$carddir}/#{channel}/.pulls_computed" + carddir = $carddir #makes a copy so that if $cardir changes this does not + file pulls_timestamp => "#{$carddir}/#{channel}/.creation_timestamp" do |t| + chdir("#{carddir}/#{channel}") do + sh "limit.py --max-likelihood --stable --rMin 0 --rMax 0 120" + sh "python $CMSSW_BASE/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py 120/out/mlfit.root -f html > #{channel}.pulls.html" + sh "python $CMSSW_BASE/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py 120/out/mlfit.root -a -f html > #{channel}.pulls.all.html" + sh "python $CMSSW_BASE/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py 120/out/mlfit.root -a -f txt > #{channel}.pulls.all.raw_txt" + end + sh "touch #{t.name}" + end + return pulls_timestamp +end + +task :f3_pulls_mmt => make_pulls_task('mmt') +task :f3_pulls_emt => make_pulls_task('emt') +task :f3_pulls_eet => make_pulls_task('eet') +task :f3_pulls_llt => make_pulls_task('llt') +task :f3_pulls => Array[:f3_pulls_mmt, + :f3_pulls_emt, + :f3_pulls_eet, + :f3_pulls_llt] + + + + + +################################################################################# +#### Background-only f3 post-fit plots, to convince yourself and the ###### +#### others (but mostly the others) that the background estimation is fine ###### +################################################################################# + +def make_f3_postfit_shapes_task(channel) + shape_file = "results/#{$jobid}/plots/#{channel}/f3/postfit/#{channel}_f3_postfit_shapes.root" + carddir = $carddir #makes a copy so that if $cardir changes this does not + file shape_file => "#{carddir}/#{channel}/.pulls_computed" do |t| + sh "mkdir -p `dirname #{t.name}`" + sh "cp #{carddir}/#{channel}/shapes.root #{t.name}" #FIXME this may create to rake some problems if next command fails! + sh "#{ENV['CMSSW_BASE']}/src/HiggsAnalysis/HiggsToTauTau/test/postfit.py #{t.name} #{carddir}/#{channel}/120/vhtt_#{channel}.txt --verbose --bins #{$categories_map[channel].join(' ')} --fitresults #{carddir}/#{channel}/120/out/mlfit.txt" + end + return shape_file +end + +task :f3_postfit_shapes_mmt => make_f3_postfit_shapes_task('mmt') +task :f3_postfit_shapes_emt => make_f3_postfit_shapes_task('emt') +task :f3_postfit_shapes_eet => make_f3_postfit_shapes_task('eet') +#task :f3_postfit_shapes_llt => make_f3_postfit_shapes_task('llt') +task :f3_postfit_shapes => Array[:f3_postfit_shapes_mmt, + :f3_postfit_shapes_emt, + :f3_postfit_shapes_eet] + + +#categories +$xlabels_map = Hash[ + 'eet' => '"M_{e_{2}#tau} (GeV)"', + 'emt' => '"M_{l_{2}#tau} (GeV)"', + 'mmt' => '"M_{#mu_{2}#tau} (GeV)"', +] + + +def make_f3_postfit_plots_task(channel) + resultdir = "results/#{$jobid}/plots/#{channel}/f3/postfit" + shape_file = "#{resultdir}/#{channel}_f3_postfit_shapes.root" + plot_timestamp = "#{resultdir}/.postfit_plots_timestamp" + carddir = $carddir #makes a copy so that if $cardir changes this does not + file plot_timestamp => shape_file do |t| + sh "mkdir -p `dirname #{t.name}`" + $categories_map[channel].each do |category| + sh "shapes2hist.py #{resultdir}/#{channel}_f3_postfit_shapes.root #{category} -e 'WH*' -o #{resultdir}/#{category}.png -x #{$xlabels_map[channel]} -d 1 -y 'Events / GeV' --show-errors" + sh "shapes2hist.py #{resultdir}/#{channel}_f3_postfit_shapes.root #{category} -e 'WH*' -o #{resultdir}/#{category}.pdf -x #{$xlabels_map[channel]} -d 1 -y 'Events / GeV' --show-errors" + end + sh "cp -v #{carddir}/#{channel}/#{channel}.pulls* #{resultdir}/." + sh "shapes2hist.py #{resultdir}/#{channel}_f3_postfit_shapes.root #{$categories_map[channel].join(' ')} -e 'WH*' -o #{resultdir}/#{channel}_categories_summed.png -x #{$xlabels_map[channel]} -d 1 -y 'Events / GeV' --show-errors" + sh "shapes2hist.py #{resultdir}/#{channel}_f3_postfit_shapes.root #{$categories_map[channel].join(' ')} -e 'WH*' -o #{resultdir}/#{channel}_categories_summed.pdf -x #{$xlabels_map[channel]} -d 1 -y 'Events / GeV' --show-errors" + sh "touch #{t.name}" + end + return plot_timestamp +end + +task :f3_postfit_plots_mmt => make_f3_postfit_plots_task('mmt') +task :f3_postfit_plots_emt => make_f3_postfit_plots_task('emt') +task :f3_postfit_plots_eet => make_f3_postfit_plots_task('eet') +#task :f3_postfit_plots_llt => make_f3_postfit_plots_task('llt') +task :f3_postfit_plots => Array[:f3_postfit_plots_mmt, + :f3_postfit_plots_emt, + :f3_postfit_plots_eet] + + + + + + + + +################################################################################ +# Recipes to make data cards (plots come for free) +# targets: +# mmt_shapes +# emt_shapes +# eet_shapes +# cards +# copycards -> move cards to official HTT CVS area +################################################################################ + +$carddir = "results/#{$jobid}/cards" + +mmt_shape_file = "#{$mmtdir}/mmt_shapes_#{$period}.root" +$categories_map['mmt'].each do |category| + mmt_shape_file = add_fake_errors(mmt_shape_file, "mmt", category) +end +task :mmt_shapes => mmt_shape_file + +emt_shape_file = "#{$emtdir}/emt_shapes_#{$period}.root" +$categories_map['emt'].each do |category| + emt_shape_file = add_fake_errors(emt_shape_file, "emt", category) +end +task :emt_shapes => emt_shape_file + +eet_shape_file = "#{$eetdir}/eet_shapes_#{$period}.root" +$categories_map['eet'].each do |category| + eet_shape_file = add_fake_errors(eet_shape_file, "eet", category) +end +task :eet_shapes => eet_shape_file + +$carddir = "results/#{$jobid}/cards" + +cardmasses = get_sample_names('VH') #get all the VH samples available +cardmasses = cardmasses.select{|x| (not x.include? "HWW") and (not x.include? "lepdecay")} #get only the H2Tau, to avid repetition +if $period == '8TeV' + cardmasses = cardmasses.map{|x| x.sub("VH_H2Tau_M-","")} #remove trailing name, the leftover is the mass (as a string) +else + cardmasses = cardmasses.map{|x| x.sub("VH_","")} #remove trailing name, the leftover is the mass (as a string) +end +cardmasses = cardmasses.map{|x| Integer(x)} #maps to integers (don't know if it's really needed but can't harm) +cardmasses = cardmasses.select{|x| x <= 140} #FIXME! to be removed! +#puts cardmasses + +$to_extrapolate = cardmasses.select{|x| x > 140} + +def make_morphing_task(channel, shapefile) + ret = "#{$carddir}/#{channel}/shapes.root" + file ret => [shapefile] do |t| + sh "mkdir -p `dirname #{t.name}`" + sh "cp #{t.prerequisites[0]} #{t.name}" + sh "horizontal-morphing.py --categories='#{$categories_map[channel].join(',')}' --samples='WH_hww{MASS}' --uncerts='' --masses='110,120,130,140' --step-size=5 #{t.name}"# --extrapolate=#{$to_extrapolate.join(",")} #{t.name}" #FIXME! to be removed! + end + return ret +end + +mmt_morphed_shape_file = make_morphing_task('mmt', mmt_shape_file) +task :mmt_morphed_shapes => mmt_morphed_shape_file + +emt_morphed_shape_file = make_morphing_task('emt', emt_shape_file) +task :emt_morphed_shapes => emt_morphed_shape_file + +eet_morphed_shape_file = make_morphing_task('eet', eet_shape_file) +task :eet_morphed_shapes => eet_morphed_shape_file + + + def make_timestamp_task(cardmasses, channel) timestamp = "#{$carddir}/#{channel}/.creation_timestamp" #creates the tasks make_unc_conf_task(channel) - make_unc_vals_task(channel) + make_unc_vals_task(channel, '', true) make_cgs_conf_task(channel) file timestamp => cardmasses.map{|x| make_datacard_task(x, channel)} do |t| + sh "mkdir -p `dirname #{t.name}`" sh "touch #{t.name}" end end file "#{$carddir}/llt/.creation_timestamp" => cardmasses.map{|x| combine_channels(x)} do |t| + sh "mkdir -p `dirname #{t.name}`" sh "touch #{t.name}" end +task :cards_mmt => make_timestamp_task(cardmasses, 'mmt') +task :cards_emt => make_timestamp_task(cardmasses, 'emt') +task :cards_eet => make_timestamp_task(cardmasses, 'eet') + task :cards => [make_timestamp_task(cardmasses, 'mmt'), make_timestamp_task(cardmasses, 'emt'), make_timestamp_task(cardmasses, 'eet'), @@ -860,16 +1121,17 @@ end #### Copying card configuration to official place ############################### ################################################################################# $httcombodir = "#{ENV['CMSSW_BASE']}/src/HiggsAnalysis/HiggsToTauTau/setup/vhtt/" -$auxdir = "#{ENV['CMSSW_BASE']}/src/auxiliaries/datacards/collected/vhtt/" +$auxdir = "#{ENV['CMSSW_BASE']}/src/auxiliaries/shapes/VHTT/" -def copy_file(source, target) +def copy_and_trim_file(source, target) file target => [source] do |t| sh "mkdir -p `dirname #{t.name}`" - sh "cp -v #{t.prerequisites[0]} #{t.name}" + sh "grep -v -E 'CMS_vhtt_.*_bin_[0-9]+' #{source} > #{t.name}" end return target end + def copy_configs(channel, vhtt_number) dir = "#{$carddir}/#{channel}" htt_id = "sm-#{$period}-#{vhtt_number}" @@ -878,10 +1140,10 @@ def copy_configs(channel, vhtt_number) copy_file("#{dir}/cgs.conf", "#{$httcombodir}/cgs-#{htt_id}.conf") #copies unc.conf - copy_file("#{dir}/unc.conf", "#{$httcombodir}/unc-#{htt_id}.conf") + copy_and_trim_file("#{dir}/unc.conf", "#{$httcombodir}/unc-#{htt_id}.conf") #copies unc.vals - copy_file("#{dir}/unc.vals", "#{$httcombodir}/unc-#{htt_id}.vals") + copy_and_trim_file("#{dir}/unc.vals", "#{$httcombodir}/unc-#{htt_id}.vals") return ["#{$httcombodir}/cgs-#{htt_id}.conf", "#{$httcombodir}/unc-#{htt_id}.conf", "#{$httcombodir}/unc-#{htt_id}.vals"] end @@ -903,149 +1165,3 @@ task :copycards => ["#{$auxdir}/vhtt_llt.inputs-sm-#{$period}.root"] + end -################################################################################# -#### Background-only f3 pulls, to convince yourself and the others ############# -#### (but mostly the others) that the background estimation is fine ############# -################################################################################# - -$carddir = "results/#{$jobid}/bkg-only-pulls" #change global var so we can reuse the functions - -mmt_f3_shape_file = "#{$mmtdir}/mmt_f3_shapes_#{$period}.root" -$categories_map['mmt'].each do |category| - mmt_f3_shape_file = add_fake_errors(mmt_f3_shape_file, "mmt_f3", category) -end -task :mmt_f3_shapes => mmt_f3_shape_file - -emt_f3_shape_file = "#{$emtdir}/emt_f3_shapes_#{$period}.root" -$categories_map['emt'].each do |category| - emt_f3_shape_file = add_fake_errors(emt_f3_shape_file, "emt_f3", category) -end -task :emt_f3_shapes => emt_f3_shape_file - -eet_f3_shape_file = "#{$eetdir}/eet_f3_shapes_#{$period}.root" -$categories_map['eet'].each do |category| - eet_f3_shape_file = add_fake_errors(eet_f3_shape_file, "eet_f3", category) -end -task :eet_f3_shapes => eet_f3_shape_file - -def make_copy_task(channel, shapefile) - ret = "#{$carddir}/#{channel}/shapes.root" - file ret => [shapefile] do |t| - sh "mkdir -p `dirname #{t.name}`" - sh "cp #{t.prerequisites[0]} #{t.name}" - end - return ret -end - -task :mmt_copied_f3_shapes => copy_file(mmt_f3_shape_file, "#{$carddir}/mmt/shapes.root") -task :emt_copied_f3_shapes => copy_file(emt_f3_shape_file, "#{$carddir}/emt/shapes.root") -task :eet_copied_f3_shapes => copy_file(eet_f3_shape_file, "#{$carddir}/eet/shapes.root") - -def make_f3_timestamp_task(cardmasses, channel) - timestamp = "#{$carddir}/#{channel}/.creation_timestamp" - #creates the tasks - make_unc_conf_task(channel, '_f3') - make_unc_vals_task(channel, '_f3') - make_cgs_conf_task(channel) - file timestamp => cardmasses.map{|x| make_datacard_task(x, channel)} do |t| - sh "touch #{t.name}" - end -end - -file "#{$carddir}/llt/.creation_timestamp" => combine_channels(120) do |t| - sh "touch #{t.name}" -end - - -task :f3_cards_mmt => make_f3_timestamp_task([120], 'mmt') -task :f3_cards_emt => make_f3_timestamp_task([120], 'emt') -task :f3_cards_eet => make_f3_timestamp_task([120], 'eet') -task :f3_cards_llt => "#{$carddir}/llt/.creation_timestamp" - -task :f3_cards => [:f3_cards_mmt, - :f3_cards_emt, - :f3_cards_eet, - :f3_cards_llt - ] - - -def make_pulls_task(channel) - pulls_timestamp = "#{$carddir}/#{channel}/.pulls_computed" - carddir = $carddir #makes a copy so that if $cardir changes this does not - file pulls_timestamp => "#{$carddir}/#{channel}/.creation_timestamp" do |t| - chdir("#{carddir}/#{channel}") do - sh "limit.py --max-likelihood --stable --rMin 0 --rMax 0 120" - sh "python $CMSSW_BASE/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py 120/out/mlfit.root -f html > #{channel}.pulls.html" - sh "python $CMSSW_BASE/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py 120/out/mlfit.root -a -f html > #{channel}.pulls.all.html" - sh "python $CMSSW_BASE/src/HiggsAnalysis/CombinedLimit/test/diffNuisances.py 120/out/mlfit.root -a -f txt > #{channel}.pulls.all.raw_txt" - end - sh "touch #{t.name}" - end - return pulls_timestamp -end - -task :f3_pulls_mmt => make_pulls_task('mmt') -task :f3_pulls_emt => make_pulls_task('emt') -task :f3_pulls_eet => make_pulls_task('eet') -task :f3_pulls_llt => make_pulls_task('llt') -task :f3_pulls => Array[:f3_pulls_mmt, - :f3_pulls_emt, - :f3_pulls_eet, - :f3_pulls_llt] - -################################################################################# -#### Background-only f3 post-fit plots, to convince yourself and the ###### -#### others (but mostly the others) that the background estimation is fine ###### -################################################################################# - -def make_f3_postfit_shapes_task(channel) - shape_file = "results/#{$jobid}/plots/#{channel}/f3/postfit/#{channel}_f3_postfit_shapes.root" - carddir = $carddir #makes a copy so that if $cardir changes this does not - file shape_file => "#{$carddir}/#{channel}/.pulls_computed" do |t| - sh "mkdir -p `dirname #{t.name}`" - sh "cp #{carddir}/#{channel}/shapes.root #{t.name}" #FIXME this may create to rake some problems if next command fails! - sh "#{ENV['CMSSW_BASE']}/src/HiggsAnalysis/HiggsToTauTau/test/postfit.py #{t.name} #{$carddir}/#{channel}/120/vhtt_#{channel}.txt --verbose --bins #{$categories_map[channel].join(' ')} --fitresults #{$carddir}/#{channel}/120/out/mlfit.txt" - end - return shape_file -end - -task :f3_postfit_shapes_mmt => make_f3_postfit_shapes_task('mmt') -task :f3_postfit_shapes_emt => make_f3_postfit_shapes_task('emt') -task :f3_postfit_shapes_eet => make_f3_postfit_shapes_task('eet') -#task :f3_postfit_shapes_llt => make_f3_postfit_shapes_task('llt') -task :f3_postfit_shapes => Array[:f3_postfit_shapes_mmt, - :f3_postfit_shapes_emt, - :f3_postfit_shapes_eet] - - -#categories -$xlabels_map = Hash[ - 'eet' => '"M_{e_{2}#tau} (GeV)"', - 'emt' => '"M_{l_{2}#tau} (GeV)"', - 'mmt' => '"M_{#mu_{2}#tau} (GeV)"', -] - - -def make_f3_postfit_plots_task(channel) - resultdir = "results/#{$jobid}/plots/#{channel}/f3/postfit" - shape_file = "#{resultdir}/#{channel}_f3_postfit_shapes.root" - plot_timestamp = "#{resultdir}/.postfit_plots_timestamp" - file plot_timestamp => shape_file do |t| - sh "mkdir -p `dirname #{t.name}`" - $categories_map[channel].each do |category| - sh "shapes2hist.py #{resultdir}/#{channel}_f3_postfit_shapes.root #{category} -e 'WH*' -o #{resultdir}/#{category}.png -x #{$xlabels_map[channel]}" - sh "shapes2hist.py #{resultdir}/#{channel}_f3_postfit_shapes.root #{category} -e 'WH*' -o #{resultdir}/#{category}.pdf -x #{$xlabels_map[channel]}" - end - sh "shapes2hist.py #{resultdir}/#{channel}_f3_postfit_shapes.root #{$categories_map[channel].join(' ')} -e 'WH*' -o #{resultdir}/#{channel}_categories_summed.png -x #{$xlabels_map[channel]}" - sh "touch #{t.name}" - end - return plot_timestamp -end - -task :f3_postfit_plots_mmt => make_f3_postfit_plots_task('mmt') -task :f3_postfit_plots_emt => make_f3_postfit_plots_task('emt') -task :f3_postfit_plots_eet => make_f3_postfit_plots_task('eet') -#task :f3_postfit_plots_llt => make_f3_postfit_plots_task('llt') -task :f3_postfit_plots => Array[:f3_postfit_plots_mmt, - :f3_postfit_plots_emt, - :f3_postfit_plots_eet] diff --git a/wh/WHAnalyzeEET.py b/wh/WHAnalyzeEET.py index 7dc5a081..dd61b453 100755 --- a/wh/WHAnalyzeEET.py +++ b/wh/WHAnalyzeEET.py @@ -13,9 +13,27 @@ import fakerate_functions as frfits import optimizer import math -import array +from array import array from chargeflipcuts import charge_flip_funcs from FinalStateAnalysis.PlotTools.decorators import memo_last +import FinalStateAnalysis.PlotTools.pytree as pytree + +#initialize FRFits +optimizer_keys = [ i for i in optimizer.grid_search.keys() if i.startswith('EET') ] +grid_search = {} +if len(optimizer_keys) > 1: + grid_search[key] = optimizer.grid_search[key] + +NO_HISTO_FILL = ('NO_HISTO_FILL' in os.environ) and eval(os.environ['NO_HISTO_FILL']) + +if not NO_HISTO_FILL: + for key in optimizer_keys: + frfits.highpt_ee_fr.__activate__(optimizer.grid_search[key]['leading_iso']) + frfits.highpt_ee_qcd_fr.__activate__(optimizer.grid_search[key]['leading_iso']) + frfits.lowpt_ee_fr.__activate__(optimizer.grid_search[key]['subleading_iso']) + frfits.lowpt_ee_qcd_fr.__activate__(optimizer.grid_search[key]['subleading_iso']) + +SYNC = ('SYNC' in os.environ) and eval(os.environ['SYNC']) #mtr = frfits.mt_likelihood_ratio ################################################################################ @@ -23,7 +41,7 @@ ################################################################################ class WHAnalyzeEET(WHAnalyzerBase): - tree = 'eet/final/Ntuple' + tree = 'eet/final/Ntuple' if not SYNC else 'Ntuple' def __init__(self, tree, outfile, **kwargs): self.channel = 'EET' super(WHAnalyzeEET, self).__init__(tree, outfile, EETauTree, **kwargs) @@ -116,6 +134,7 @@ def log_prob(row, weight): self.hfunc["e*1_t_Mass" ] = mass_scaler( attr_getter('e1_t_Mass')) self.hfunc["e1_e*2_Mass"] = mass_scaler( attr_getter('e1_e2_Mass')) self.hfunc["e*2_t_Mass" ] = mass_scaler( attr_getter('e2_t_Mass')) + self.hfunc['SYNC'] = lambda row, weight: (row, None) #((row.run, row.lumi, row.evt, row.e1Pt, row.e1Eta, row.e1Phi, row.e2Pt, row.e2Eta, row.e2Phi, row.tPt, row.tEta, row.tPhi, weight), None) #self.hfunc['evt_info'] = lambda row, weight: (array.array("f", [row.e1Pt, row.e2Pt, row.tPt, row.LT, weight] ), None) self.pucorrector = mcCorrectors.make_puCorrector('doublee') @@ -127,14 +146,16 @@ def anti_charge_flip(row, rejection_power=80): def book_histos(self, folder): #PLOTS TO FILL IN ANY CASE + LTBinning = array('d',[0, 80, 100, 600]) + nLTBins = len(LTBinning) -1 for key in self.grid_search: prefix = key+'$' if key else '' self.book(folder, prefix+"LT", "L_T", 100, 0, 300) - self.book(folder, prefix+"e2_t_Mass", "subleadingMass", 200, 0, 200) + self.book(folder, prefix+"e2_t_Mass", "subleadingMass", 300, 0, 300) self.book(folder, prefix+"e2_t_Pt", "subleadingPt", 400, 0, 400) #Charge mis-id special histograms if 'c2' in folder: - self.book(folder, prefix+"e*2_t_Mass", "subleadingMass with misid sclaing correction", 200, 0, 200) + self.book(folder, prefix+"e*2_t_Mass", "subleadingMass with misid sclaing correction", 300, 0, 300) if len(self.grid_search.keys()) == 1: if 'c1' in folder: @@ -144,38 +165,43 @@ def book_histos(self, folder): self.book(folder, "e1_e*2_Mass", "E 1-2 Mass with misid sclaing correction", 120, 0, 120) #self.book(folder, "e*2_t_Mass#faking_prob", '', 200, 0, 200, 1100, 0., 1.1, type=ROOT.TH2F) #self.book(folder, "e*2_t_Mass#log_prob" , '', 200, 0, 200, 1000, -10, 1, type=ROOT.TH2F) - self.book(folder, "e*2_t_Mass#LT" , '', 200, 0, 200, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, "e*2_t_Mass#tPt" , '', 200, 0, 200, 200, 0, 200, type=ROOT.TH2F) + self.book(folder, "e*2_t_Mass#LT" , '', 300, 0, 300, nLTBins, LTBinning, type=ROOT.TH2F) + + self.book(folder, prefix+"e2_t_Mass#LT" , "subleadingMass", 300, 0, 300, nLTBins, LTBinning, type=ROOT.TH2F) - self.book(folder, prefix+"e2_t_Mass#LT" , "subleadingMass", 200, 0, 200, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"e2_t_Mass#tPt", "subleadingMass", 200, 0, 200, 200, 0, 200, type=ROOT.TH2F) + self.book(folder, 'SYNC', 'SYNC', + 'run/l:lumi/l:evt/l' +\ + ':e1Pt/D:e1Eta/D:e1Phi/D:e1RelPFIsoDB/D:e1MVANonTrig/D' +\ + ':e2Pt/D:e2Eta/D:e2Phi/D:e2RelPFIsoDB/D:e2MVANonTrig/D' +\ + ':tPt/D:tEta/D:tPhi/D:tAntiMuonTight/D:tDecayMode/D:tLooseIso3Hits/D:tAntiElectronMVA3Loose/D' +\ + ':tAntiMuonLoose/D:tAntiElectronMVA3Medium/D:tAntiElectronMVA3Tight/D' +\ + ':e1_e2_Mass/D:e2_t_Mass/D:LT/D:e1_e2_SS/D:e1_t_SS/D', + type=pytree.PyTree) #Pt - self.book(folder, prefix+"e1Pt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"e2Pt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"tPt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"e1JetPt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"e2JetPt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) + self.book(folder, prefix+"e1Pt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"e2Pt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"tPt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"e1JetPt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"e2JetPt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) #eta - self.book(folder, prefix+"e1AbsEta#LT" , "subleadingMass", 100, 0, 2.5, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"e2AbsEta#LT" , "subleadingMass", 100, 0, 2.5, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"tAbsEta#LT" , "subleadingMass", 100, 0, 2.5, 120, 0, 600, type=ROOT.TH2F) + self.book(folder, prefix+"e1AbsEta#LT" , "subleadingMass", 100, 0, 2.5, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"e2AbsEta#LT" , "subleadingMass", 100, 0, 2.5, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"tAbsEta#LT" , "subleadingMass", 100, 0, 2.5, nLTBins, LTBinning, type=ROOT.TH2F) #DR - self.book(folder, prefix+"e1_t_DR#LT" , "subleadingMass", 100, 0, 10, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"e2_t_DR#LT" , "subleadingMass", 100, 0, 10, 120, 0, 600, type=ROOT.TH2F) + self.book(folder, prefix+"e1_t_DR#LT" , "subleadingMass", 100, 0, 10, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"e2_t_DR#LT" , "subleadingMass", 100, 0, 10, nLTBins, LTBinning, type=ROOT.TH2F) #Jet BTag self.book(folder, "e1JetBtag#LT", "Muon 2 Pt", 30, -10, 5, 60, 0, 600, type=ROOT.TH2F) self.book(folder, "e2JetBtag#LT", "Muon 2 Pt", 30, -10, 5, 60, 0, 600, type=ROOT.TH2F) - #self.book(folder, prefix+"e2_t_Mass#faking_prob" , "subleadingMass", 200, 0, 200, 1100, 0., 1.1, type=ROOT.TH2F) - #self.book(folder, prefix+"e2_t_Mass#log_prob" , "subleadingMass", 200, 0, 200, 1000, -10, 1, type=ROOT.TH2F) - # - #self.book(folder, prefix+'faking_prob' , "", 1100, 0., 1.1) - #self.book(folder, prefix+'log_prob' , "", 1000, -10, 1) + #tau ISO + self.book(folder, "tRawIso3Hits#LT", "Muon 2 Pt", 200, 0, 200, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "tRawIsoMVA2#LT", "Muon 2 Pt" , 200, -1, -1, nLTBins, LTBinning, type=ROOT.TH2F) self.book(folder, "e2RelPFIsoDB", "e2RelPFIsoDB", 30, 0, 0.3) self.book(folder, "e1RelPFIsoDB", "e1RelPFIsoDB", 30, 0, 0.3) @@ -216,6 +242,12 @@ def preselection(self, row, cut_flow_trk = None, LT_threshold = 80., taupt_thr = Excludes FR object IDs and sign cut. ''' + if row.e1_t_DR < 0.5 or row.e2_t_DR < 0.5 or row.e1_e2_DR < 0.5: return False + cut_flow_trk.Fill('DR separation') + + if row.e2_t_Mass < 20: return False + cut_flow_trk.Fill('sub mass cut') + if not row.doubleEPass: return False if not (row.e1MatchesDoubleEPath > 0 and \ row.e2MatchesDoubleEPath > 0): return False @@ -224,35 +256,9 @@ def preselection(self, row, cut_flow_trk = None, LT_threshold = 80., taupt_thr = if row.e1Pt < 20: return False if not selections.eSelection(row, 'e1'): return False cut_flow_trk.Fill('obj1 Presel') - #FIXME - #if row.e1Pt < 20: return False - #cut_flow_trk.Fill('pt requirements 1') - #if row.e1AbsEta > 2.5: return False - #cut_flow_trk.Fill('eta requirements 1') - #if row.e1MissingHits: return False - #cut_flow_trk.Fill('MissingHits 1') - #if row.e1HasConversion: return False - #cut_flow_trk.Fill('HasConversion 1') - #if row.e1JetBtag > 3.3: return False - #cut_flow_trk.Fill('JetBtag 1') - #if abs(row.e1DZ) > 0.2: return False - #cut_flow_trk.Fill('DZ 1') if not selections.eSelection(row, 'e2'): return False cut_flow_trk.Fill('obj2 Presel') - #FIXME - #if row.e2Pt < 10: return False - #cut_flow_trk.Fill('pt requirements 2') - #if row.e2AbsEta > 2.5: return False - #cut_flow_trk.Fill('eta requirements 2') - #if row.e2MissingHits: return False - #cut_flow_trk.Fill('MissingHits 2') - #if row.e2HasConversion: return False - #cut_flow_trk.Fill('HasConversion 2') - #if row.e2JetBtag > 3.3: return False - #cut_flow_trk.Fill('JetBtag 2') - #if abs(row.e2DZ) > 0.2: return False - #cut_flow_trk.Fill('DZ 2') if not selections.tauSelection(row, 't'): return False if row.tPt < taupt_thr: return False @@ -262,29 +268,11 @@ def preselection(self, row, cut_flow_trk = None, LT_threshold = 80., taupt_thr = if row.LT < LT_threshold: return False cut_flow_trk.Fill('LT') - if row.e1_e2_SS and row.e1_t_SS: return False #remove three SS leptons + #if row.e1_e2_SS and row.e1_t_SS: return False #remove three SS leptons if row.e1_e2_Mass < 20: return False - if not selections.vetos(row): return False #applies mu bjet e additional tau vetoes - cut_flow_trk.Fill('vetos') - - #REMOVE CHARGE FAKES! - #FIXME - #if not row.e1ChargeIdLoose: return False - #if not row.e2ChargeIdLoose: return False - #cut_flow_trk.Fill('ChargeIdLoose') - #cut_flow_trk.Fill('charge_fakes') - - #FIXME: ONLY FOR CUT-FLOW PRODUCTION - #if not selections.summer_2013_eid(row, 'e1'): return False - #cut_flow_trk.Fill('obj1 ID') - #if not selections.lepton_id_iso(row, 'e1', 'eid13Looseh2taucuts'): return False - #cut_flow_trk.Fill('obj1 Iso') - #if not selections.summer_2013_eid(row, 'e2'): return False - #cut_flow_trk.Fill('obj2 ID') - #if not selections.lepton_id_iso(row, 'e2', 'eid13Looseh2taucuts'): return False - #cut_flow_trk.Fill('obj2 Iso') - #if not row.tLooseIso3Hits: return False - #cut_flow_trk.Fill('obj3 IDIso') + cut_flow_trk.Fill('dilepton mass cut') + + if not selections.vetos(row, cut_flow_trk): return False #applies mu bjet e additional tau vetoes return True @@ -294,6 +282,11 @@ def sign_cut(row): ''' Returns true if muons are SS ''' return bool(row.e1_e2_SS) + @staticmethod + def tau_sign_cut(row): + ''' Returns true if muons are SS ''' + return not bool(row.e1_t_SS) + #There is no call to self, so just promote it to statucmethod, to allow usage by other dedicated analyzers @staticmethod def obj1_id(row, leadleptonId='eid13Looseh2taucuts', subleadleptonId=None): @@ -312,6 +305,18 @@ def obj3_id(row, tauId=None, LT_threshold = 80., taupt_thr = 0.): else: return bool( getattr(row, tauId) ) + @staticmethod + def obj1_matches_gen(row): + return row.e1GenPdgId == -1*row.e1Charge*11 + + @staticmethod + def obj2_matches_gen(row): + return row.e2GenPdgId == -1*row.e2Charge*11 + + @staticmethod + def obj3_matches_gen(row): + return t.genDecayMode != -2 + #There is no call to self, so just promote it to statucmethod, to allow usage by other dedicated analyzers @staticmethod def anti_wz(row): @@ -326,36 +331,33 @@ def anti_wz(row): def enhance_wz(self, row): # Require the "tau" to be a electron, and require the third electron # to have M_Z +- 20 - if self.anti_wz(row): - return False - # Make sure any Z is from e1 - e2_good_Z = bool(71 < row.e2_t_Mass < 111) - return not e2_good_Z + return row.tCiCTightElecOverlap def event_weight(self, row): - if row.run > 2: + if row.run > 2: #FIXME! add tight ID correction return 1. return self.pucorrector(row.nTruePU) * \ - mcCorrectors.get_electron_corrections(row,'e1','e2') - + mcCorrectors.get_electron_corrections(row,'e2') *\ + mcCorrectors.electron_tight_corrections(row.e1Pt, row.e1AbsEta) *\ + mcCorrectors.double_electron_trigger(row) def obj1_weight(self, row, leadleptonId='eid13Looseh2taucuts', subleadleptonId=None): - return frfits.highpt_ee_fr[leadleptonId](electronJetPt=max(row.e1JetPt, row.e1Pt), electronPt=row.e1Pt) + return frfits.highpt_ee_fr[leadleptonId](electronJetPt=max(row.e1JetPt, row.e1Pt), electronPt=row.e1Pt, numJets20=row.jetVeto20+1) def obj2_weight(self, row, leadleptonId=None, subleadleptonId='eid13Looseh2taucuts'): - return frfits.lowpt_ee_fr[subleadleptonId](electronJetPt=max(row.e2JetPt, row.e2Pt), electronPt=row.e2Pt) + return frfits.lowpt_ee_fr[subleadleptonId](electronJetPt=max(row.e2JetPt, row.e2Pt), electronPt=row.e2Pt, numJets20=row.jetVeto20+1) def obj3_weight(self, row, notUsed1=None, notUsed2=None): - return frfits.tau_fr(row.tPt) + return frfits.tau_fr(row.tPt, row.tAbsEta) def obj1_qcd_weight(self, row, leadleptonId='eid13Looseh2taucuts', subleadleptonId=None): - return frfits.highpt_ee_qcd_fr[leadleptonId](electronJetPt=max(row.e1JetPt, row.e1Pt), electronPt=row.e1Pt) + return frfits.highpt_ee_qcd_fr[leadleptonId](electronJetPt=max(row.e1JetPt, row.e1Pt), electronPt=row.e1Pt, numJets20=row.jetVeto20+1) def obj2_qcd_weight(self, row, leadleptonId=None, subleadleptonId='eid13Looseh2taucuts'): - return frfits.lowpt_ee_qcd_fr[subleadleptonId](electronJetPt=max(row.e2JetPt, row.e2Pt), electronPt=row.e2Pt) + return frfits.lowpt_ee_qcd_fr[subleadleptonId](electronJetPt=max(row.e2JetPt, row.e2Pt), electronPt=row.e2Pt, numJets20=row.jetVeto20+1) def obj3_qcd_weight(self, row, notUsed1=None, notUsed2=None): - return frfits.tau_qcd_fr(row.tPt) + return frfits.tau_qcd_fr(row.tPt, row.tAbsEta) # For measuring charge flip probability # Not really used in this channel diff --git a/wh/WHAnalyzeEMT.py b/wh/WHAnalyzeEMT.py index 9705b06d..fe0f9032 100644 --- a/wh/WHAnalyzeEMT.py +++ b/wh/WHAnalyzeEMT.py @@ -12,19 +12,52 @@ import mcCorrectors import baseSelections as selections import fakerate_functions as frfits +from array import array import ROOT import math import optimizer +import FinalStateAnalysis.PlotTools.pytree as pytree from FinalStateAnalysis.PlotTools.decorators import memo_last +#def make_bins(n, xlow, xhigh): +# step = float(xhigh - xlow) / float(n) +# ret = [] +# for i in xrange(n): +# ret.append(xlow + step*i) +# ret.append(xhigh) +# return array('d', ret) + +#initialize FRFits +optimizer_keys = [ i for i in optimizer.grid_search.keys() if i.startswith('EMT') ] +grid_search = {} +if len(optimizer_keys) > 1: + grid_search[key] = optimizer.grid_search[key] + +NO_HISTO_FILL = ('NO_HISTO_FILL' in os.environ) and eval(os.environ['NO_HISTO_FILL']) + +if not NO_HISTO_FILL: + for key in optimizer_keys: + frfits.highpt_mue_fr[optimizer.grid_search[key]['leading_iso']] + frfits.highpt_mue_qcd_fr[optimizer.grid_search[key]['leading_iso']] + frfits.lowpt_mue_fr[optimizer.grid_search[key]['leading_iso']] + frfits.lowpt_mue_qcd_fr[optimizer.grid_search[key]['leading_iso']] + frfits.highpt_e_fr[optimizer.grid_search[key]['subleading_iso']] + frfits.highpt_e_qcd_fr[optimizer.grid_search[key]['subleading_iso']] + frfits.lowpt_e_fr[optimizer.grid_search[key]['subleading_iso']] + frfits.lowpt_e_qcd_fr[optimizer.grid_search[key]['subleading_iso']] + +SYNC = ('SYNC' in os.environ) and eval(os.environ['SYNC']) + + ################################################################################ #### Analysis logic ############################################################ ################################################################################ is7TeV = bool('7TeV' in os.environ['jobid']) use_iso_trigger = not is7TeV +region_for_event_list = os.environ.get('EVTLIST_REGION','') class WHAnalyzeEMT(WHAnalyzerBase): - tree = 'emt/final/Ntuple' + tree = 'emt/final/Ntuple' if not SYNC else 'Ntuple' def __init__(self, tree, outfile, **kwargs): self.channel = 'EMT' @@ -58,27 +91,27 @@ def sub_mass(row, weight): lead_iso = self.grid_search['']['leading_iso'] sublead_iso = self.grid_search['']['subleading_iso'] - def f_prob(row, weight): - p_m = ( self.obj1_weight(row, lead_iso, sublead_iso) + \ - self.obj1_qcd_weight(row, lead_iso, sublead_iso))/2 - p_e = ( self.obj2_weight(row, lead_iso, sublead_iso) + \ - self.obj2_qcd_weight(row, lead_iso, sublead_iso))/2 - p_t = frfits.tau_fr(row.tPt) - return ((p_m + p_e*(1 - p_m) + p_t*(1 - p_m)*(1 - p_e)), weight) - - def log_prob(row, weight): - prob, weight = f_prob(row, weight) - return math.log(prob), weight + #def f_prob(row, weight): + # p_m = ( self.obj1_weight(row, lead_iso, sublead_iso) + \ + # self.obj1_qcd_weight(row, lead_iso, sublead_iso))/2 + # p_e = ( self.obj2_weight(row, lead_iso, sublead_iso) + \ + # self.obj2_qcd_weight(row, lead_iso, sublead_iso))/2 + # p_t = frfits.tau_fr(row.tPt) + # return ((p_m + p_e*(1 - p_m) + p_t*(1 - p_m)*(1 - p_e)), weight) + # + #def log_prob(row, weight): + # prob, weight = f_prob(row, weight) + # return math.log(prob), weight - self.hfunc['faking_prob'] = f_prob - self.hfunc['log_prob'] = log_prob - self.hfunc["subMass#faking_prob"] = merge_functions( sub_mass, f_prob ) - self.hfunc["subMass#log_prob" ] = merge_functions( sub_mass, log_prob) + #self.hfunc['faking_prob'] = f_prob + #self.hfunc['log_prob'] = log_prob + #self.hfunc["subMass#faking_prob"] = merge_functions( sub_mass, f_prob ) + #self.hfunc["subMass#log_prob" ] = merge_functions( sub_mass, log_prob) self.hfunc["subMass#LT" ] = merge_functions( sub_mass, attr_getter('LT')) self.hfunc["subMass#tPt"] = merge_functions( sub_mass, attr_getter('tPt')) - self.hfunc["subMass*#faking_prob"] = merge_functions( mass_scaler( sub_mass ), f_prob ) - self.hfunc["subMass*#log_prob" ] = merge_functions( mass_scaler( sub_mass ), log_prob) + #self.hfunc["subMass*#faking_prob"] = merge_functions( mass_scaler( sub_mass ), f_prob ) + #self.hfunc["subMass*#log_prob" ] = merge_functions( mass_scaler( sub_mass ), log_prob) self.hfunc["subMass*#LT" ] = merge_functions( mass_scaler( sub_mass ), attr_getter('LT')) self.hfunc["subMass*#tPt"] = merge_functions( mass_scaler( sub_mass ), attr_getter('tPt')) @@ -96,80 +129,78 @@ def log_prob(row, weight): self.hfunc["subMass*" ] = mass_scaler( sub_mass ) self.hfunc["_recoilDaught" ] = lambda row, weight: (math.sqrt(row.recoilDaught) , weight) self.hfunc["_recoilWithMet"] = lambda row, weight: (math.sqrt(row.recoilWithMet), weight) + self.hfunc['SYNC'] = lambda row, weight: (row, None) #((row.run, row.lumi, row.evt, row.mPt, row.mEta, row.mPhi, row.ePt, row.eEta, row.ePhi, row.tPt, row.tEta, row.tPhi, weight), None ) self.pucorrector = mcCorrectors.make_puCorrector('mueg') def book_histos(self, folder): + if region_for_event_list and region_for_event_list != folder: + return for key in self.grid_search: prefix = key+'$' if key else '' - self.book(folder, prefix+"subMass", "Subleading Mass", 200, 0, 200) + #self.book(folder, prefix+"subMass", "Subleading Mass", 200, 0, 200) self.book(folder, prefix+"LT", "L_T", 100, 0, 300) #Charge mis-id special histograms - if 'c2' in folder: - self.book(folder, prefix+"subMass*", "Subleading Mass", 200, 0, 200) + #if 'c2' in folder: + # self.book(folder, prefix+"subMass*", "Subleading Mass", 200, 0, 200) if len(self.grid_search.keys()) == 1: + LTBinning = array('d',[0, 80, 130, 600]) + nLTBins = len(LTBinning) -1 if 'c2' in folder: self.book(folder, "e*_t_Mass", "Electron-Tau Mass", 200, 0, 200) self.book(folder, "e*_m_Mass", "Electron-Muon Mass", 200, 0, 200) #self.book(folder, "subMass*#faking_prob", '', 200, 0, 200, 220, 0., 1.1, type=ROOT.TH2F) #self.book(folder, "subMass*#log_prob" , '', 200, 0, 200, 200, -2, 1, type=ROOT.TH2F) - self.book(folder, "subMass*#LT" , '', 200, 0, 200, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, "subMass*#tPt" , '', 200, 0, 200, 200, 0, 200, type=ROOT.TH2F) - - - self.book(folder, prefix+"subMass#LT" , "subleadingMass", 200, 0, 200, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"subMass#tPt", "subleadingMass", 200, 0, 200, 200, 0, 200, type=ROOT.TH2F) + self.book(folder, "subMass*#LT" , '', 300, 0, 300, nLTBins, LTBinning, type=ROOT.TH2F) + + self.book(folder, 'SYNC', 'SYNC', + 'run/l:lumi/l:evt/l' +\ + ':mPt/D:mEta/D:mPhi/D:mPFIDTight/D:mRelPFIsoDB/D' +\ + ':ePt/D:eEta/D:ePhi/D:eRelPFIsoDB/D:eMVANonTrig/D' +\ + ':tPt/D:tEta/D:tPhi/D:tAntiMuonTight/D:tDecayMode/D:tLooseIso3Hits/D:tAntiElectronMVA3Loose/D' +\ + ':tAntiMuonLoose/D:tAntiElectronMVA3Medium/D' +\ + ':e_m_Mass/D:m_t_Mass/D:e_t_Mass/D:LT/D:e_m_SS/D:m_t_SS/D', + type=pytree.PyTree) + + self.book(folder, prefix+"subMass#LT" , "subleadingMass", 300, 0, 300, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "e_m_Mass#LT", "Electron-Muon Mass", 200, 0, 200, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "m_t_Mass#LT", "Electron-Muon Mass", 200, 0, 200, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "e_t_Mass#LT", "Electron-Tau Mass" , 200, 0, 200, nLTBins, LTBinning, type=ROOT.TH2F) #Pt - self.book(folder, prefix+"mPt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"ePt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"tPt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"mJetPt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"eJetPt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) + self.book(folder, prefix+"mPt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"ePt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"tPt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"mJetPt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"eJetPt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) #eta - self.book(folder, prefix+"mAbsEta#LT" , "subleadingMass", 100, 0, 2.5, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"eAbsEta#LT" , "subleadingMass", 100, 0, 2.5, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"tAbsEta#LT" , "subleadingMass", 100, 0, 2.5, 120, 0, 600, type=ROOT.TH2F) + self.book(folder, prefix+"mAbsEta#LT" , "subleadingMass", 100, 0, 2.5, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"eAbsEta#LT" , "subleadingMass", 100, 0, 2.5, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"tAbsEta#LT" , "subleadingMass", 100, 0, 2.5, nLTBins, LTBinning, type=ROOT.TH2F) #DR - self.book(folder, prefix+"m_t_DR#LT" , "subleadingMass", 100, 0, 10, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, prefix+"e_t_DR#LT" , "subleadingMass", 100, 0, 10, 120, 0, 600, type=ROOT.TH2F) + self.book(folder, prefix+"m_t_DR#LT" , "subleadingMass", 100, 0, 10, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, prefix+"e_t_DR#LT" , "subleadingMass", 100, 0, 10, nLTBins, LTBinning, type=ROOT.TH2F) #Jet BTag - self.book(folder, "mJetBtag#LT", "Muon 2 Pt", 30, -10, 5, 60, 0, 600, type=ROOT.TH2F) - self.book(folder, "eJetBtag#LT", "Muon 2 Pt", 30, -10, 5, 60, 0, 600, type=ROOT.TH2F) + self.book(folder, "mJetBtag#LT", "Muon 2 Pt", 30, -10, 5, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "eJetBtag#LT", "Muon 2 Pt", 30, -10, 5, nLTBins, LTBinning, type=ROOT.TH2F) + + #tau ISO + self.book(folder, "tRawIso3Hits#LT", "Muon 2 Pt", 200, 0, 200, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "tRawIsoMVA2#LT", "Muon 2 Pt" , 200, -1, -1, nLTBins, LTBinning, type=ROOT.TH2F) self.book(folder, "subPt" , "SubLeading Pt", 100, 0, 100) self.book(folder, "leadPt" , "Leading Pt" , 100, 0, 100) self.book(folder, "subJetPt" , "SubLeading Pt", 100, 0, 100) self.book(folder, "leadJetPt" , "Leading Pt" , 100, 0, 100) - self.book(folder, "e_m_Mass", "Electron-Muon Mass", 200, 0, 200) - self.book(folder, "m_t_Mass", "Electron-Muon Mass", 200, 0, 200) - self.book(folder, "e_t_Mass", "Electron-Tau Mass", 200, 0, 200) self.book(folder, "nTruePU", "NPU", 62, -1.5, 60.5) - self.book(folder, "mPt", "Muon Pt", 100, 0, 100) - self.book(folder, "ePt", "Electron Pt", 100, 0, 100) - self.book(folder, "mJetPt", "Muon Pt", 100, 0, 100) - self.book(folder, "eJetPt", "Electron Pt", 100, 0, 100) - self.book(folder, "tPt", "Tau Pt", 100, 0, 100) - self.book(folder, "mAbsEta", "Muon AbsEta", 100, 0, 2.4) - self.book(folder, "eAbsEta", "Electron AbsEta", 100, 0, 2.5) - self.book(folder, "tAbsEta", "Tau AbsEta", 100, 0, 2.3) self.book(folder, "eChargeIdTight", "Elec charge ID tight", 2, -0.5, 1.5) self.book(folder, "tLeadDR", "DR between leading lepton and tau", 100, 0, 5) self.book(folder, "tSubDR", "DR between subleading lepton and tau", 100, 0, 5) self.book(folder, "e_m_DR", "", 200, 0, 5) - self.book(folder, "m_t_DR", "", 200, 0, 5) - self.book(folder, "e_t_DR", "", 200, 0, 5) - - - #let's look for osme other possible selections - self.book(folder, "Mass" , "mass" , 800, 0, 800 ) - self.book(folder, "pt_ratio" , "pt_ratio" , 100, 0, 1) - self.book(folder, "tToMETDPhi" , "tToMETDPhi" , 100, 0, 4) - self.book(folder, "type1_pfMetEt" , "metEt" , 300, 0, 2000) #There is no call to self, so just promote it to statucmethod, to allow usage by other dedicated analyzers @@ -178,6 +209,12 @@ def preselection( self, row, cut_flow_trk = None, LT_threshold = 80., taupt_thr Excludes FR object IDs and sign cut. ''' + if row.e_t_DR < 0.5 or row.m_t_DR < 0.5 or row.e_m_DR < 0.5: return False + cut_flow_trk.Fill('DR separation') + + if self.hfunc['subMass'](row, 1)[0] < 20: return False + cut_flow_trk.Fill('sub mass cut') + mu17e8, mu8e17 = False, False if use_iso_trigger: mu17e8 = row.mu17ele8isoPass and \ @@ -202,56 +239,24 @@ def preselection( self, row, cut_flow_trk = None, LT_threshold = 80., taupt_thr cut_flow_trk.Fill('trigger') if not selections.muSelection(row, 'm'): return False #applies basic selection (eta, pt > 10, DZ, pixHits, jetBTag) - #cut_flow_trk.Fill('pt requirements 1', 'eta requirements 1', 'MissingHits 1', 'HasConversion 1', 'JetBtag 1', 'DZ 1',) cut_flow_trk.Fill('obj1 Presel') if not selections.eSelection(row, 'e'): return False #applies basic selection (eta, pt > 10, DZ, missingHits, jetBTag, HasConversion and chargedIdTight) cut_flow_trk.Fill('obj2 Presel') - #FIXME - #if row.ePt < 10: return False - #cut_flow_trk.Fill('pt requirements 2') - #if row.eAbsEta > 2.5: return False - #cut_flow_trk.Fill('eta requirements 2') - #if row.eMissingHits: return False - #cut_flow_trk.Fill('MissingHits 2') - #if row.eHasConversion: return False - #cut_flow_trk.Fill('HasConversion 2') - #if row.eJetBtag > 3.3: return False - #cut_flow_trk.Fill('JetBtag 2') - #if abs(row.eDZ) > 0.2: return False - #cut_flow_trk.Fill('DZ 2') - if not selections.tauSelection(row, 't'): return False #applies basic selection (eta, pt > 20, DZ) if row.tPt < taupt_thr: return False - if row.tMuOverlap: return False - if not row.tAntiMuonTight: return False cut_flow_trk.Fill('obj3 Presel') if row.LT < LT_threshold: return False cut_flow_trk.Fill('LT') - if row.e_m_SS and row.e_t_SS: return False #remove three SS leptons - if not selections.vetos(row): return False #applies mu bjet e additional tau vetoes - cut_flow_trk.Fill('vetos') - #FIXME - if not row.eChargeIdTight: return False - #cut_flow_trk.Fill('ChargeIdTight') - - if e_m_Mass < 20: return False - cut_flow_trk.Fill('charge_fakes') #no charge fakes here + if row.e_m_Mass < 20: return False + cut_flow_trk.Fill('dilepton mass cut') - #FIXME: ONLY FOR CUT-FLOW PRODUCTION - #if not row.mPFIDTight: return False - #cut_flow_trk.Fill('obj1 ID') - #if not selections.lepton_id_iso(row, 'm', 'h2taucuts'): return False - #cut_flow_trk.Fill('obj1 Iso') - #if not selections.summer_2013_eid(row, 'e'): return False - #cut_flow_trk.Fill('obj2 ID') - #if not selections.lepton_id_iso(row, 'e', 'h2taucuts'): return False - #cut_flow_trk.Fill('obj2 Iso') - #if not row.tLooseIso3Hits: return False - #cut_flow_trk.Fill('obj3 IDIso') + if not selections.vetos(row, cut_flow_trk): return False #applies mu bjet e additional tau vetoes + + cut_flow_trk.Fill('charge_fakes') #no charge fakes here return True @@ -261,6 +266,11 @@ def sign_cut( row): ''' Returns true if muons are SS ''' return bool(row.e_m_SS) + @staticmethod + def tau_sign_cut(row): + ''' Returns true if muons are SS ''' + return not bool(row.m_t_SS) + #There is no call to self, so just promote it to statucmethod, to allow usage by other dedicated analyzers @staticmethod def obj1_id( row, ledleptonId='h2taucuts', subledleptonId='h2taucuts'): @@ -279,18 +289,33 @@ def obj3_id(row, tauId=None, LT_threshold = 80., taupt_thr = 0.): else: return bool( getattr(row, tauId) ) + @staticmethod + def obj1_matches_gen(row): + return row.mGenPdgId == -1*row.mCharge*13 + + @staticmethod + def obj2_matches_gen(row): + return row.eGenPdgId == -1*row.eCharge*11 + + @staticmethod + def obj3_matches_gen(row): + return t.genDecayMode != -2 + #There is no call to self, so just promote it to statucmethod, to allow usage by other dedicated analyzers @staticmethod def anti_wz( row): - if row.e_t_Zcompat < 20: - if not row.tAntiElectronMVA3Medium: - return False - elif not row.tAntiElectronMVA3Loose: + if not row.tAntiMuonLoose or not row.tAntiElectronMVA3Loose: + return False + if row.e_t_Zcompat < 20 and (not row.tAntiElectronMVA3Medium): return False + if row.m_t_Zcompat < 20 and not (row.tAntiMuonTight and \ + (( row.tDecayMode == 0 and row.tLeadChargeCandEMFraction > 0.2 ) \ + or row.tDecayMode <> 0)): + return False return True def enhance_wz(self, row): - if row.e_t_Zcompat < 15 and not row.tAntiElectronMVA: + if row.e_t_Zcompat < 15 and row.tCiCTightElecOverlap: return True return False @@ -306,36 +331,36 @@ def event_weight(self, row): def obj1_weight(self, row, ledleptonId='h2taucuts', subledleptonId='h2taucuts'): mu17e8 = (row.mu17ele8isoPass and row.mPt >= 20) if use_iso_trigger else (row.mu17ele8Pass and row.mPt >= 20) if mu17e8: - return frfits.highpt_mu_fr[ledleptonId](muonJetPt=max(row.mJetPt, row.mPt), muonPt=row.mPt, muonJetCSVBtag=max(0, row.mJetCSVBtag)) + return frfits.highpt_mue_fr[ledleptonId](muonJetPt=max(row.mJetPt, row.mPt), muonPt=row.mPt, numJets20=row.jetVeto20+1) # else: - return frfits.lowpt_mu_fr[ledleptonId](muonJetPt=max(row.mJetPt, row.mPt), muonPt=row.mPt, muonJetCSVBtag=max(0, row.mJetCSVBtag)) + return frfits.lowpt_mue_fr[ledleptonId](muonJetPt=max(row.mJetPt, row.mPt), muonPt=row.mPt, numJets20=row.jetVeto20+1) # def obj2_weight(self, row, ledleptonId='h2taucuts', subledleptonId='h2taucuts'): mu17e8 = (row.mu17ele8isoPass and row.mPt >= 20) if use_iso_trigger else (row.mu17ele8Pass and row.mPt >= 20) if mu17e8: - return frfits.lowpt_e_fr[subledleptonId](electronJetPt=max(row.eJetPt, row.ePt), electronPt=row.ePt) + return frfits.lowpt_e_fr[subledleptonId](electronJetPt=max(row.eJetPt, row.ePt), electronPt=row.ePt, numJets20=row.jetVeto20+1) else: - return frfits.highpt_e_fr[subledleptonId](electronJetPt=max(row.eJetPt, row.ePt), electronPt=row.ePt) + return frfits.highpt_e_fr[subledleptonId](electronJetPt=max(row.eJetPt, row.ePt), electronPt=row.ePt, numJets20=row.jetVeto20+1) def obj3_weight(self, row, notUsed1=None, notUsed2=None): - return frfits.tau_fr(row.tPt) + return frfits.tau_fr(row.tPt, row.tAbsEta) def obj1_qcd_weight(self, row, ledleptonId='h2taucuts', subledleptonId='h2taucuts'): mu17e8 = (row.mu17ele8isoPass and row.mPt >= 20) if use_iso_trigger else (row.mu17ele8Pass and row.mPt >= 20) if mu17e8: - return frfits.highpt_mu_qcd_fr[ledleptonId](muonJetPt=max(row.mJetPt, row.mPt), muonPt=row.mPt, muonJetCSVBtag=max(0, row.mJetCSVBtag)) + return frfits.highpt_mue_qcd_fr[ledleptonId](muonJetPt=max(row.mJetPt, row.mPt), muonPt=row.mPt, numJets20=row.jetVeto20+1) # else: - return frfits.lowpt_mu_qcd_fr[ledleptonId](muonJetPt=max(row.mJetPt, row.mPt), muonPt=row.mPt, muonJetCSVBtag=max(0, row.mJetCSVBtag)) + return frfits.lowpt_mue_qcd_fr[ledleptonId](muonJetPt=max(row.mJetPt, row.mPt), muonPt=row.mPt, numJets20=row.jetVeto20+1) # def obj2_qcd_weight(self, row, ledleptonId='h2taucuts', subledleptonId='h2taucuts'): mu17e8 = (row.mu17ele8isoPass and row.mPt >= 20) if use_iso_trigger else (row.mu17ele8Pass and row.mPt >= 20) if mu17e8: - return frfits.lowpt_e_qcd_fr[subledleptonId](electronJetPt=max(row.eJetPt, row.ePt), electronPt=row.ePt) + return frfits.lowpt_e_qcd_fr[subledleptonId](electronJetPt=max(row.eJetPt, row.ePt), electronPt=row.ePt, numJets20=row.jetVeto20+1) else: - return frfits.highpt_e_qcd_fr[subledleptonId](electronJetPt=max(row.eJetPt, row.ePt), electronPt=row.ePt) + return frfits.highpt_e_qcd_fr[subledleptonId](electronJetPt=max(row.eJetPt, row.ePt), electronPt=row.ePt, numJets20=row.jetVeto20+1) def obj3_qcd_weight(self, row, notUsed1=None, notUsed2=None): - return frfits.tau_qcd_fr(row.tPt) + return frfits.tau_qcd_fr(row.tPt, row.tAbsEta) # For measuring charge flip probability # Not really used in this channel @@ -346,17 +371,11 @@ def obj1_obj3_SS(self, row): ## return 0 def obj2_charge_flip(self, row, ledleptonId='h2taucuts', subledleptonId='h2taucuts'): - if ledleptonId.startswith('lead4muon_'): - ledleptonId = subledleptonId - subledleptonId = subledleptonId return frfits.e_charge_flip[ledleptonId](row.eAbsEta,row.ePt) \ if row.ePt > row.mPt else \ frfits.e_charge_flip[subledleptonId](row.eAbsEta,row.ePt) def obj2_charge_flip_sysup(self, row, ledleptonId='h2taucuts', subledleptonId='h2taucuts'): - if ledleptonId.startswith('lead4muon_'): - ledleptonId = subledleptonId - subledleptonId = subledleptonId return frfits.e_charge_flip_up[ledleptonId](row.eAbsEta,row.ePt) \ if row.ePt > row.mPt else \ frfits.e_charge_flip_up[subledleptonId](row.eAbsEta,row.ePt) \ diff --git a/wh/WHAnalyzeMMT.py b/wh/WHAnalyzeMMT.py index 64272203..613b7825 100755 --- a/wh/WHAnalyzeMMT.py +++ b/wh/WHAnalyzeMMT.py @@ -12,14 +12,35 @@ import baseSelections as selections import fakerate_functions as frfits import math +from array import array from FinalStateAnalysis.PlotTools.decorators import memo_last import optimizer +import FinalStateAnalysis.PlotTools.pytree as pytree + +#initialize FRFits +optimizer_keys = [ i for i in optimizer.grid_search.keys() if i.startswith('MMT') ] +print optimizer_keys +grid_search = {} +if len(optimizer_keys) > 1: + grid_search[key] = optimizer.grid_search[key] + +NO_HISTO_FILL = ('NO_HISTO_FILL' in os.environ) and eval(os.environ['NO_HISTO_FILL']) + +if not NO_HISTO_FILL: + for key in optimizer_keys: + frfits.highpt_mu_fr.__activate__(optimizer.grid_search[key]['leading_iso']) + frfits.highpt_mu_qcd_fr.__activate__(optimizer.grid_search[key]['leading_iso']) + frfits.lowpt_mu_fr.__activate__(optimizer.grid_search[key]['subleading_iso']) + frfits.lowpt_mu_qcd_fr.__activate__(optimizer.grid_search[key]['subleading_iso']) + +SYNC = ('SYNC' in os.environ) and eval(os.environ['SYNC']) + ################################################################################ #### Analysis logic ############################################################ ################################################################################ class WHAnalyzeMMT(WHAnalyzerBase): - tree = 'mmt/final/Ntuple' + tree = 'mmt/final/Ntuple' if not SYNC else 'Ntuple' def __init__(self, tree, outfile, **kwargs): self.channel = 'MMT' super(WHAnalyzeMMT, self).__init__(tree, outfile, MuMuTauTree, **kwargs) @@ -40,123 +61,127 @@ def f(row, weight): lead_iso = self.grid_search['']['leading_iso'] sublead_iso = self.grid_search['']['subleading_iso'] - @memo_last - def f_par_prob(m1Pt, m1JetPt, - m2Pt, m2JetPt, - tPt): - p_m1 = (( frfits.highpt_mu_fr[lead_iso](muonJetPt=max(m1JetPt, m1Pt), muonPt=m1Pt) +\ - frfits.highpt_mu_qcd_fr[lead_iso](muonJetPt=max(m1JetPt, m1Pt), muonPt=m1Pt) )/2) - p_m2 = (( frfits.lowpt_mu_fr[sublead_iso](muonJetPt=max(m2JetPt, m2Pt), muonPt=m2Pt) + \ - frfits.lowpt_mu_qcd_fr[sublead_iso](muonJetPt=max(m2JetPt, m2Pt), muonPt=m2Pt))/2) - p_t = frfits.tau_fr(tPt) - return (p_m1 + p_m2*(1 - p_m1) + p_t*(1 - p_m1)*(1 - p_m2)) - - def f_prob(row, weight): - val = f_par_prob(row.m1Pt, row.m1JetPt, - row.m2Pt, row.m2JetPt, - row.tPt) - return val, weight - - def log_prob(row, weight): - prob, weight = f_prob(row, weight) - return ROOT.TMath.Log10(prob), weight + #@memo_last + #def f_par_prob(m1Pt, m1JetPt, + # m2Pt, m2JetPt, + # tPt): + # p_m1 = (( frfits.highpt_mu_fr[lead_iso](muonJetPt=max(m1JetPt, m1Pt), muonPt=m1Pt) +\ + # frfits.highpt_mu_qcd_fr[lead_iso](muonJetPt=max(m1JetPt, m1Pt), muonPt=m1Pt) )/2) + # p_m2 = (( frfits.lowpt_mu_fr[sublead_iso](muonJetPt=max(m2JetPt, m2Pt), muonPt=m2Pt) + \ + # frfits.lowpt_mu_qcd_fr[sublead_iso](muonJetPt=max(m2JetPt, m2Pt), muonPt=m2Pt))/2) + # p_t = frfits.tau_fr(tPt) + # return (p_m1 + p_m2*(1 - p_m1) + p_t*(1 - p_m1)*(1 - p_m2)) + # + #def f_prob(row, weight): + # val = f_par_prob(row.m1Pt, row.m1JetPt, + # row.m2Pt, row.m2JetPt, + # row.tPt) + # return val, weight + # + #def log_prob(row, weight): + # prob, weight = f_prob(row, weight) + # return ROOT.TMath.Log10(prob), weight - self.hfunc['faking_prob'] = f_prob - self.hfunc['log_prob'] = log_prob - self.hfunc["m2_t_Mass#faking_prob"] = merge_functions( attr_getter('m2_t_Mass'), f_prob ) - self.hfunc["m2_t_Mass#log_prob" ] = merge_functions( attr_getter('m2_t_Mass'), log_prob) + #self.hfunc['faking_prob'] = f_prob + #self.hfunc['log_prob'] = log_prob + #self.hfunc["m2_t_Mass#faking_prob"] = merge_functions( attr_getter('m2_t_Mass'), f_prob ) + #self.hfunc["m2_t_Mass#log_prob" ] = merge_functions( attr_getter('m2_t_Mass'), log_prob) self.hfunc['subMTMass'] = lambda row, weight: (row.m2_t_Mass, weight) if row.m1MtToMET > row.m2MtToMET else (row.m1_t_Mass, weight) #maps the name of non-trivial histograms to a function to get the proper value, the function MUST have two args (evt and weight). Used in WHAnalyzerBase.fill_histos later self.hfunc['pt_ratio' ] = lambda row, weight: (row.m2Pt/row.m1Pt, weight) + self.hfunc['SYNC'] = lambda row, weight: (row, None)#((row.run, row.lumi, row.evt, row.m1Pt, row.m1Eta, row.m1Phi, row.m2Pt, row.m2Eta, row.m2Phi, row.tPt, row.tEta, row.tPhi, weight), None) self.pucorrector = mcCorrectors.make_puCorrector('doublemu') def book_histos(self, folder): + LTBinning = array('d',[0, 80, 130, 600]) + nLTBins = len(LTBinning) -1 for key in self.grid_search: prefix = key+'$' if key else '' - self.book(folder, prefix+"m2_t_Mass", "subleadingMass", 200, 0, 200) + #self.book(folder, prefix+"m2_t_Mass", "subleadingMass", 200, 0, 200) self.book(folder, prefix+"LT" , "LT" , 100, 0., 500) self.book(folder, prefix+"m2_t_Pt", "subleadingPt", 400, 0, 400) if len(self.grid_search.keys()) == 1: - self.book(folder, "m2_t_Mass#LT" , "subleadingMass", 200, 0, 200, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, "m2_t_Mass#tPt", "subleadingMass", 200, 0, 200, 200, 0, 200, type=ROOT.TH2F) - #self.book(folder, "m2_t_Mass#faking_prob" , "subleadingMass", 200, 0, 200, 1100, 0., 1.1, type=ROOT.TH2F) - #self.book(folder, "m2_t_Mass#log_prob" , "subleadingMass", 200, 0, 200, 1000, -10, 1, type=ROOT.TH2F) - #self.book(folder, 'faking_prob' , "", 1100, 0., 1.1) - #self.book(folder, 'log_prob' , "", 1000, -10, 1) - + self.book(folder, 'SYNC', 'SYNC', + 'run/l:lumi/l:evt/l' +\ + ':m1Pt/D:m1Eta/D:m1Phi/D:m1PFIDTight/D:m1RelPFIsoDB/D' +\ + ':m2Pt/D:m2Eta/D:m2Phi/D:m2PFIDTight/D:m2RelPFIsoDB/D' +\ + ':tPt/D:tEta/D:tPhi/D:tAntiMuonTight/D:tDecayMode/D:tLooseIso3Hits/D:tAntiElectronMVA3Loose/D' +\ + ':m1_m2_Mass/D:m2_t_Mass/D:LT/D:m1_m2_SS/D:m1_t_SS/D', + type=pytree.PyTree) + + self.book(folder, "m2_t_Mass#LT" , "subleadingMass", 300, 0, 300, nLTBins, LTBinning, type=ROOT.TH2F) + #self.book(folder, "Event_ID", "Event ID", 'run:lumi:evt1:evt2', type=ROOT.TNtuple) + #self.book(folder, "DEBUG", "DEBUG", 'run:lumi:m2_t_Mass:LT:m1Pt:m2Pt:tPt', type=ROOT.TNtuple) #Pt - self.book(folder, "m1Pt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, "tPt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, "m2Pt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, "m1JetPt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, "m2JetPt#LT" , "subleadingMass", 150, 0, 150, 120, 0, 600, type=ROOT.TH2F) + self.book(folder, "m1Pt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "tPt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "m2Pt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "m1JetPt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "m2JetPt#LT" , "subleadingMass", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) #eta - self.book(folder, "m1AbsEta#LT" , "subleadingMass", 100, 0, 2.5, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, "m2AbsEta#LT" , "subleadingMass", 100, 0, 2.5, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, "tAbsEta#LT" , "subleadingMass", 100, 0, 2.5, 120, 0, 600, type=ROOT.TH2F) + self.book(folder, "m1AbsEta#LT" , "subleadingMass", 100, 0, 2.5, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "m2AbsEta#LT" , "subleadingMass", 100, 0, 2.5, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "tAbsEta#LT" , "subleadingMass", 100, 0, 2.5, nLTBins, LTBinning, type=ROOT.TH2F) #DR - self.book(folder, "m1_t_DR#LT" , "subleadingMass", 100, 0, 10, 120, 0, 600, type=ROOT.TH2F) - self.book(folder, "m2_t_DR#LT" , "subleadingMass", 100, 0, 10, 120, 0, 600, type=ROOT.TH2F) - + self.book(folder, "m1_t_DR#LT" , "subleadingMass", 100, 0, 10, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "m2_t_DR#LT" , "subleadingMass", 100, 0, 10, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "m1_m2_DR#LT", "subleadingMass", 100, 0, 10, nLTBins, LTBinning, type=ROOT.TH2F) + #Jet BTag - self.book(folder, "m2JetBtag#LT", "Muon 2 Pt", 100, -100, 100, 60, 0, 600, type=ROOT.TH2F) - self.book(folder, "m1JetBtag#LT", "Muon 2 Pt", 100, -100, 100, 60, 0, 600, type=ROOT.TH2F) - self.book(folder, "m1JetCSVBtag#LT", "Muon 2 Pt", 120, -5, 1, 60, 0, 600, type=ROOT.TH2F) - self.book(folder, "m2JetCSVBtag#LT", "Muon 2 Pt", 120, -5, 1, 60, 0, 600, type=ROOT.TH2F) - + self.book(folder, "m2JetBtag#LT", "Muon 2 Pt", 100, -100, 100, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "m1JetBtag#LT", "Muon 2 Pt", 100, -100, 100, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "m1JetCSVBtag#LT", "Muon 2 Pt", 120, -5, 1 , nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "m2JetCSVBtag#LT", "Muon 2 Pt", 120, -5, 1 , nLTBins, LTBinning, type=ROOT.TH2F) #Mt To MET - self.book(folder, "m1MtToMET#LT", "Muon 2 Pt", 150, 0, 150, 60, 0, 600, type=ROOT.TH2F) - self.book(folder, "m2MtToMET#LT", "Muon 2 Pt", 150, 0, 150, 60, 0, 600, type=ROOT.TH2F) + self.book(folder, "m1MtToMET#LT", "Muon 2 Pt", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "m2MtToMET#LT", "Muon 2 Pt", 150, 0, 150, nLTBins, LTBinning, type=ROOT.TH2F) + + #tau ISO + self.book(folder, "tRawIso3Hits#LT", "Muon 2 Pt", 200, 0, 200, nLTBins, LTBinning, type=ROOT.TH2F) + self.book(folder, "tRawIsoMVA2#LT", "Muon 2 Pt" , 200, -1, -1, nLTBins, LTBinning, type=ROOT.TH2F) self.book(folder, "m2RelPFIsoDB", "m2Iso", 100, 0, 0.3) self.book(folder, "m1_t_Mass", "leadingMass", 200, 0, 200) self.book(folder, "m1_m2_Mass", "Muon 1-2 Mass", 120, 0, 120) - self.book(folder, "m2_t_DR", "m2_t_DR", 100, 0, 5) - self.book(folder, "m1_t_DR", "m1_t_DR", 100, 0, 5) - self.book(folder, "m1JetPt", "Muon 1 Jet Pt", 100, 0, 200) - self.book(folder, "m2JetPt", "Muon 2 Jet Pt", 100, 0, 200) # Rank muons by less MT to MET, for WZ control region self.book(folder, "weight", "Event weight", 100, 0, 5) self.book(folder, "rho", "Fastjet #rho", 100, 0, 25) self.book(folder, "nvtx", "Number of vertices", 31, -0.5, 30.5) - self.book(folder, "m1Pt", "Muon 1 Pt", 100, 0, 100) - self.book(folder, "m2Pt", "Muon 2 Pt", 100, 0, 100) - self.book(folder, "m1AbsEta", "Muon 1 AbsEta", 100, 0, 2.4) - self.book(folder, "m2AbsEta", "Muon 2 AbsEta", 100, 0, 2.4) - self.book(folder, "tPt", "Tau Pt", 100, 0, 100) - self.book(folder, "tAbsEta", "Tau AbsEta", 100, 0, 2.3) self.book(folder, "subMTMass", "subMTMass", 200, 0, 200) - #self.book(folder, "tDecayMode", "Tau AbsEta", 15, -0.5, 14.5) self.book(folder, "nTruePU", "NPU", 62, -1.5, 60.5) self.book(folder, "m1DZ", "m1DZ", 100, 0., 1) self.book(folder, "m2DZ", "m2DZ", 100, 0., 1) self.book(folder, "tDZ" , "tDZ" , 100, 0., 1) self.book(folder, "doubleMuPrescale", "HLT prescale", 26, -5.5, 20.5) - #let's look for osme other possible selections - self.book(folder, "pt_ratio" , "pt_ratio" , 100, 0, 1) - self.book(folder, "tToMETDPhi" , "tToMETDPhi" , 100, 0, 4) - self.book(folder, "Mass" , "mass" , 800, 0, 800 ) - self.book(folder, "type1_pfMetEt" , "metEt" , 300, 0, 2000) - def preselection(self, row, cut_flow_trk = None, LT_threshold = 80., taupt_thr = 0.): ''' Preselection applied to events. Excludes FR object IDs and sign cut. ''' + + # + # NEW EXPLICIT CUT, BEFORE IT WAS IMPLICIT AND MADE WHILE PLOTTING! + # + if row.m1_t_DR < 0.5 or row.m2_t_DR < 0.5 or row.m1_m2_DR < 0.5: return False + cut_flow_trk.Fill('DR separation') + + if row.m2_t_Mass < 20: return False + cut_flow_trk.Fill('sub mass cut') + double_mu_pass = row.doubleMuPass and \ row.m1MatchesDoubleMuPaths > 0 and \ row.m2MatchesDoubleMuPaths > 0 double_muTrk_pass = row.doubleMuTrkPass and \ - row.m1MatchesDoubleMuTrkPaths > 0 and \ - row.m2MatchesDoubleMuTrkPaths > 0 + row.m1MatchesMu17TrkMu8Path > 0 and \ + row.m2MatchesMu17TrkMu8Path > 0 if not ( double_mu_pass or double_muTrk_pass ): return False cut_flow_trk.Fill('trigger') @@ -164,12 +189,9 @@ def preselection(self, row, cut_flow_trk = None, LT_threshold = 80., taupt_thr = if row.m1Pt < 20: return False if not selections.muSelection(row, 'm1'): return False #applies basic selection (eta, pt > 10, DZ, pixHits, jetBTag) cut_flow_trk.Fill('obj1 Presel') - #cut_flow_trk.Fill('pt requirements 1', 'eta requirements 1', 'MissingHits 1', 'HasConversion 1', 'JetBtag 1', 'DZ 1',) - if not selections.muSelection(row, 'm2'): return False #applies basic selection (eta, pt > 10, DZ, pixHits, jetBTag) cut_flow_trk.Fill('obj2 Presel') - #cut_flow_trk.Fill('pt requirements 2', 'eta requirements 2', 'MissingHits 2', 'HasConversion 2', 'JetBtag 2', 'DZ 2',) if not selections.tauSelection(row, 't'): return False #applies basic selection (eta, pt > 20, DZ) if not row.tAntiElectronMVA3Loose: return False @@ -179,27 +201,18 @@ def preselection(self, row, cut_flow_trk = None, LT_threshold = 80., taupt_thr = if row.LT < LT_threshold: return False cut_flow_trk.Fill('LT') - if row.m1_m2_SS and row.m1_t_SS: return False #remove three SS leptons if row.m1_m2_Mass < 20: return False - if not selections.vetos(row): return False #applies mu bjet e additional tau vetoes - cut_flow_trk.Fill('vetos') - #cut_flow_trk.Fill('ChargeIdTight') - cut_flow_trk.Fill('charge_fakes') #no charge fakes here - - #FIXME: ONLY FOR CUT-FLOW PRODUCTION - #if not row.m1PFIDTight: return False - #cut_flow_trk.Fill('obj1 ID') - #if not selections.lepton_id_iso(row, 'm1', 'h2taucuts'): return False - #cut_flow_trk.Fill('obj1 Iso') - #if not row.m2PFIDTight: return False - #cut_flow_trk.Fill('obj2 ID') - #if not selections.lepton_id_iso(row, 'm2', 'h2taucuts'): return False - #cut_flow_trk.Fill('obj2 Iso') - #if not row.tLooseIso3Hits: return False - #cut_flow_trk.Fill('obj3 IDIso') + cut_flow_trk.Fill('dilepton mass cut') + + if not selections.vetos(row, cut_flow_trk): return False #applies mu bjet e additional tau vetoes return True + @staticmethod + def tau_sign_cut(row): + ''' Returns true if muons are SS ''' + return not bool(row.m1_t_SS) + @staticmethod def sign_cut(row): ''' Returns true if muons are SS ''' @@ -215,26 +228,37 @@ def obj2_id(row, leadleptonId=None, subleadleptonId='h2taucuts'): @staticmethod def obj3_id(row, tauId=None, LT_threshold = 80., taupt_thr = 0.): + retval = False if row.LT >= LT_threshold and row.tPt >= taupt_thr: - return bool(row.tLooseIso3Hits) + retval = bool(row.tLooseIso3Hits) else: - return bool( getattr(row, tauId) ) + retval = bool( getattr(row, tauId) ) + return retval + + @staticmethod + def obj1_matches_gen(row): + return row.m1GenPdgId == -1*row.m1Charge*13 + + @staticmethod + def obj2_matches_gen(row): + return row.m2GenPdgId == -1*row.m2Charge*13 + + @staticmethod + def obj3_matches_gen(row): + return t.genDecayMode != -2 @staticmethod def anti_wz(row): - return row.tAntiMuonTight # and not row.tMuOverlap + return bool(row.tAntiMuonTight and (( row.tDecayMode == 0 and row.tLeadChargeCandEMFraction > 0.2 ) or row.tDecayMode <> 0)) # and not row.tMuOverlap def enhance_wz(self, row): # Require the "tau" to be a muon, and require the third muon # to have M_Z +- 20 - if self.anti_wz(row): - return False # Cut on m2 PT > 20 #if row.m2Pt < 20: #return False # Make sure any Z is from m1 - m2_good_Z = bool(71 < row.m2_t_Mass < 111) - return not m2_good_Z + return row.tMuOverlap def event_weight(self, row): if row.run > 2: @@ -244,22 +268,22 @@ def event_weight(self, row): mcCorrectors.double_muon_trigger(row,'m1','m2') def obj1_weight(self, row, ledleptonId='h2taucuts', subledleptonId=None): - return frfits.highpt_mu_fr[ledleptonId](muonJetPt=max(row.m1JetPt, row.m1Pt), muonPt=row.m1Pt, muonJetCSVBtag=max(0, row.m1JetCSVBtag)) #muonPVDXY=row.m1PVDXY) #, muonJetBtag=row.m1JetBtag) + return frfits.highpt_mu_fr[ledleptonId](muonJetPt=max(row.m1JetPt, row.m1Pt), muonPt=row.m1Pt, numJets20=row.jetVeto20+1) # def obj2_weight(self, row, ledleptonId=None, subledleptonId='h2taucuts'): - return frfits.lowpt_mu_fr[subledleptonId](muonJetPt=max(row.m2JetPt, row.m2Pt), muonPt=row.m2Pt, muonJetCSVBtag=max(0, row.m2JetCSVBtag)) #, muonPVDXY=row.m2PVDXY) #, muonJetBtag=row.m2JetBtag) + return frfits.lowpt_mu_fr[subledleptonId](muonJetPt=max(row.m2JetPt, row.m2Pt), muonPt=row.m2Pt, numJets20=row.jetVeto20+1) # def obj3_weight(self, row, notUsed1=None, notUsed2=None): - return frfits.tau_fr(row.tPt) + return frfits.tau_fr(row.tPt, row.tAbsEta) def obj1_qcd_weight(self, row, ledleptonId='h2taucuts', subledleptonId=None): - return frfits.highpt_mu_qcd_fr[ledleptonId](muonJetPt=max(row.m1JetPt, row.m1Pt), muonPt=row.m1Pt, muonJetCSVBtag=max(0, row.m1JetCSVBtag)) #, muonPVDXY=row.m1PVDXY) #, muonJetBtag=row.m1JetBtag) + return frfits.highpt_mu_qcd_fr[ledleptonId](muonJetPt=max(row.m1JetPt, row.m1Pt), muonPt=row.m1Pt, numJets20=row.jetVeto20+1) # def obj2_qcd_weight(self, row, ledleptonId=None, subledleptonId='h2taucuts'): - return frfits.lowpt_mu_qcd_fr[subledleptonId](muonJetPt=max(row.m2JetPt, row.m2Pt), muonPt=row.m2Pt, muonJetCSVBtag=max(0, row.m2JetCSVBtag)) #, muonPVDXY=row.m2PVDXY) #, muonJetBtag=row.m2JetBtag) + return frfits.lowpt_mu_qcd_fr[subledleptonId](muonJetPt=max(row.m2JetPt, row.m2Pt), muonPt=row.m2Pt, numJets20=row.jetVeto20+1) # def obj3_qcd_weight(self, row, notUsed1=None, notUsed2=None): - return frfits.tau_qcd_fr(row.tPt) + return frfits.tau_qcd_fr(row.tPt, row.tAbsEta) # For measuring charge flip probability # Not really used in this channel diff --git a/wh/WHAnalyzerBase.py b/wh/WHAnalyzerBase.py index d176cb88..f3d10b4a 100644 --- a/wh/WHAnalyzerBase.py +++ b/wh/WHAnalyzerBase.py @@ -57,9 +57,23 @@ import os import ROOT import math +from pdb import set_trace import itertools +import array from FinalStateAnalysis.PlotTools.decorators import memo from FinalStateAnalysis.Utilities.struct import struct +from cutflowtracker import cut_flow_tracker +#Makes the cut flow histogram +cut_flow_step = ['bare', 'WH Event', + #'obj1 GenMatching', 'obj2 GenMatching', 'obj3 GenMatching', + 'DR separation', 'sub mass cut', 'trigger', + 'obj1 Presel', 'obj2 Presel', 'obj3 Presel', + 'LT', 'dilepton mass cut', 'muon veto', 'bjet veto', 'electron veto', + 'tau veto', 'sign cut', 'tau sign', 'anti WZ', + 'obj1 IDIso', 'obj2 IDIso', 'obj3 IDIso', + 'anti charge flip', +] + @memo def joinDirs(*args): @@ -75,40 +89,6 @@ def inv_mass(*args): v.SetPtEtaPhiM(*i) return sum(lorentz_vecs,ROOT.TLorentzVector()).M() #We need to give the staring point otherwise it starts from an int and it does not work -class cut_flow_tracker(object): - def __init__(self, hist): - self.labels = [hist.GetXaxis().GetBinLabel(i+1) for i in range(hist.GetNbinsX())] - self.cut_flow = dict([ (i, False) for i in self.labels]) - self.hist = hist - self.evt_info = [-1, -1, -1] - self.disabled = True #'CutFlow' not in os.environ - if not self.disabled: - print "running cut flow" - - def fill(self, label): - self.cut_flow[label] = True - - def Fill(self, *labels): - if self.disabled: - return - for label in labels: - self.fill(label) - - def flush(self): - if self.disabled: - return - for i, label in enumerate(self.labels): - val = self.cut_flow[label] - if val: - self.hist.Fill(i+0.5) - - def new_row(self, *args): - if self.disabled: - return - if self.evt_info != list(args): - self.flush() - self.evt_info = list(args) - self.cut_flow = dict([ (i, False) for i in self.labels]) class WHAnalyzerBase(MegaBase): def __init__(self, tree, outfile, wrapper, **kwargs): @@ -140,65 +120,68 @@ def build_wh_folder_structure(): #folders = [] flag_map = {} for sign in ['ss', 'os']: - for failing_objs in [(), (1,), (2,), (3,), (1,3), (2, 3), (1,2), (1,2,3)]: - cut_key = [sign == 'ss'] - region_label = '' - for i in range(1,4): - if i in failing_objs: - region_label += 'f' + str(i) - cut_key.append(False) - else: - region_label += 'p' + str(i) - cut_key.append(True) - # Figure out which objects to weight for FR - weights_to_apply = [] - # Single fake - if len(failing_objs) == 1: - weights_to_apply.append( - (failing_objs, "w%i" % failing_objs)) - # A version using the QCD fake rate - weights_to_apply.append( - (failing_objs, "q%i" % failing_objs)) - if len(failing_objs) == 2: - # in the 1-2 case, apply both. Otherwise, just apply the - # first (a light lepton) - if 3 not in failing_objs: - weights_to_apply.append( - (failing_objs, "w%i%i" % failing_objs)) - # Using QCD rate - weights_to_apply.append( - (failing_objs, "q%i%i" % failing_objs)) - else: + for tau_sign in ['tau_ss', 'tau_os']: + for failing_objs in [(), (1,), (2,), (3,), (1,3), (2, 3), (1,2), (1,2,3)]: + cut_key = [sign == 'ss'] + tau_sign_key = (tau_sign == 'tau_os') + cut_key.append( tau_sign_key ) + region_label = '' + for i in range(1,4): + if i in failing_objs: + region_label += 'f' + str(i) + cut_key.append(False) + else: + region_label += 'p' + str(i) + cut_key.append(True) + # Figure out which objects to weight for FR + weights_to_apply = [] + # Single fake + if len(failing_objs) == 1: weights_to_apply.append( - (failing_objs, "w%i" % failing_objs[0])) - # Using QCD rate + (failing_objs, "w%i" % failing_objs)) + # A version using the QCD fake rate weights_to_apply.append( - (failing_objs, "q%i" % failing_objs[0])) - - if len(failing_objs) == 3: - weights_to_apply.append( ((3,), "w3") ) - weights_to_apply.append( ((1,3,), "w13") ) - weights_to_apply.append( ((2,3,), "w23") ) - # QCD weight versions - weights_to_apply.append( ((1,3,), "q13") ) - weights_to_apply.append( ((2,3,), "q23") ) - # Needed for f3 CR - weights_to_apply.append( ((1,2), "w12")) - weights_to_apply.append( ((1,), "w1")) - weights_to_apply.append( ((2,), "w2")) - weights_to_apply.append( ((1,2), "q12")) - weights_to_apply.append( ((1,), "q1")) - weights_to_apply.append( ((2,), "q2")) - - #folders_to_add = [ (sign, region_label) ] - # Which objects to weight for each region - weights_to_add = [] - for failing_objs, weight_to_apply in weights_to_apply: - #folders_to_add.append( (sign, region_label, weight_to_apply) ) - weights_to_add.append(weight_to_apply) - - flag_map[tuple(cut_key)] = ((sign, region_label), tuple(weights_to_add)) - #folders.extend(folders_to_add) + (failing_objs, "q%i" % failing_objs)) + if len(failing_objs) == 2: + # in the 1-2 case, apply both. Otherwise, just apply the + # first (a light lepton) + if 3 not in failing_objs: + weights_to_apply.append( + (failing_objs, "w%i%i" % failing_objs)) + # Using QCD rate + weights_to_apply.append( + (failing_objs, "q%i%i" % failing_objs)) + else: + weights_to_apply.append( + (failing_objs, "w%i" % failing_objs[0])) + # Using QCD rate + weights_to_apply.append( + (failing_objs, "q%i" % failing_objs[0])) + + if len(failing_objs) == 3: + weights_to_apply.append( ((3,), "w3") ) + weights_to_apply.append( ((1,3,), "w13") ) + weights_to_apply.append( ((2,3,), "w23") ) + # QCD weight versions + weights_to_apply.append( ((1,3,), "q13") ) + weights_to_apply.append( ((2,3,), "q23") ) + # Needed for f3 CR + weights_to_apply.append( ((1,2), "w12")) + weights_to_apply.append( ((1,), "w1")) + weights_to_apply.append( ((2,), "w2")) + weights_to_apply.append( ((1,2), "q12")) + weights_to_apply.append( ((1,), "q1")) + weights_to_apply.append( ((2,), "q2")) + + #folders_to_add = [ (sign, region_label) ] + # Which objects to weight for each region + weights_to_add = [] + for failing_objs, weight_to_apply in weights_to_apply: + #folders_to_add.append( (sign, region_label, weight_to_apply) ) + weights_to_add.append(weight_to_apply) + + flag_map[tuple(cut_key)] = ((sign, tau_sign, region_label), tuple(weights_to_add)) + #folders.extend(folders_to_add) return flag_map @@ -207,6 +190,8 @@ def fill_histos(self, histos, folder_str, row, weight, filter_label = ''): #find all keys matching for attr in self.histo_locations[folder_str]: name = attr + #if attr=='DEBUG': + # set_trace() if filter_label: if not attr.startswith(filter_label+'$'): continue @@ -239,57 +224,43 @@ def fill_histos(self, histos, folder_str, row, weight, filter_label = ''): def begin(self): # Loop over regions, book histograms, book the minimal amount book_charge_flip_cr = hasattr(self, 'anti_charge_flip') - for _, folders in self.build_wh_folder_structure().iteritems(): - base_folder, weight_folders = folders - folder = "/".join(base_folder) - self.book_histos(folder) # in subclass - if book_charge_flip_cr: - self.book_histos(folder+'/charge_flip_CR') - # Each of the weight subfolders - for weight_folder in weight_folders: - hfolder = "/".join(base_folder + (weight_folder,)) - self.book_histos(hfolder) + no_histo_fill = ('NO_HISTO_FILL' in os.environ) and eval(os.environ['NO_HISTO_FILL']) + if not no_histo_fill: + for _, folders in self.build_wh_folder_structure().iteritems(): + base_folder, weight_folders = folders + folder = "/".join(base_folder) + self.book_histos(folder) # in subclass if book_charge_flip_cr: - self.book_histos(hfolder+'/charge_flip_CR') - - # Add WZ control region - self.book_histos('ss/p1p2p3_enhance_wz') - # Where second light lepton fails - self.book_histos('ss/p1f2p3_enhance_wz') - self.book_histos('ss/p1f2p3_enhance_wz/w2') - - # Add charge-fake control region - probability that obj1 will flip into - # ss/p1p2p3 - for i in itertools.product(['p3','f3'],['c1','c2','c1_sysup','c2_sysup']): - self.book_histos('os/p1p2%s/%s' % i) - self.book_histos('os/p1p2%s/%s/charge_flip_CR' % i) - - for key in self.histograms: - charpos = key.rfind('/') - location = key[ : charpos]+'/' - name = key[ charpos + 1 :] - if location in self.histo_locations: - self.histo_locations[location].append(name) - else: - self.histo_locations[location] = [name] - - #Makes the cut flow histogram - cut_flow_step = ['bare', 'WH Event', - #'obj1 GenMatching', 'obj2 GenMatching', 'obj3 GenMatching', - 'trigger', - 'obj1 Presel', 'obj2 Presel', -## 'pt requirements 1', 'eta requirements 1', -## 'MissingHits 1', 'HasConversion 1', 'JetBtag 1', 'DZ 1', -## 'pt requirements 2', 'eta requirements 2', -## 'MissingHits 2', 'HasConversion 2', 'JetBtag 2', 'DZ 2', - 'obj3 Presel', - 'LT', 'vetos', -## 'ChargeIdLoose', -## 'charge_fakes', - #'obj1 ID', 'obj1 Iso', 'obj2 ID', 'obj2 Iso', - 'obj1 IDIso', 'obj2 IDIso', 'obj3 IDIso', - 'sign cut', 'anti WZ', - ] + self.book_histos(folder+'/charge_flip_CR') + # Each of the weight subfolders + for weight_folder in weight_folders: + hfolder = "/".join(base_folder + (weight_folder,)) + self.book_histos(hfolder) + if book_charge_flip_cr: + self.book_histos(hfolder+'/charge_flip_CR') + + # Add WZ control region + self.book_histos('ss/tau_os/p1p2p3_enhance_wz') + # Where second light lepton fails + self.book_histos('ss/tau_os/p1f2p3_enhance_wz') + self.book_histos('ss/tau_os/p1f2p3_enhance_wz/w2') + + # Add charge-fake control region - probability that obj1 will flip into + # ss/p1p2p3 + for i in itertools.product(['tau_os','tau_ss'],['p3','f3'],['c1','c2','c1_sysup','c2_sysup']): + self.book_histos('os/%s/p1p2%s/%s' % i) + self.book_histos('os/%s/p1p2%s/%s/charge_flip_CR' % i) + + for key in self.histograms: + charpos = key.rfind('/') + location = key[ : charpos]+'/' + name = key[ charpos + 1 :] + if location in self.histo_locations: + self.histo_locations[location].append(name) + else: + self.histo_locations[location] = [name] + #END if not no_histo_fill: + self.book('ss', "CUT_FLOW", "Cut Flow", len(cut_flow_step), 0, len(cut_flow_step)) xaxis = self.histograms['ss/CUT_FLOW'].GetXaxis() self.cut_flow_histo = self.histograms['ss/CUT_FLOW'] @@ -303,14 +274,22 @@ def process(self): # string using a dictionary # key = (sign, obj1, obj2, obj3) cut_region_map = self.build_wh_folder_structure() + region_for_event_list = os.environ.get('EVTLIST_REGION','') + no_histo_fill = ('NO_HISTO_FILL' in os.environ) and eval(os.environ['NO_HISTO_FILL']) + print_region = ('PRINT_REGION' in os.environ) and eval(os.environ['PRINT_REGION']) + debug_mode = ('DEBUG_MODE' in os.environ) and eval(os.environ['DEBUG_MODE']) # Reduce number of self lookups and get the derived functions here histos = self.histograms preselection = self.preselection sign_cut = self.sign_cut + tau_sign_cut = self.tau_sign_cut obj1_id = self.obj1_id obj2_id = self.obj2_id obj3_id = self.obj3_id + obj1_matches_gen = self.obj1_matches_gen + obj2_matches_gen = self.obj2_matches_gen + obj3_matches_gen = self.obj3_matches_gen fill_histos = self.fill_histos anti_wz_cut = self.anti_wz anti_charge_flip_cut = self.anti_charge_flip if hasattr(self,'anti_charge_flip') else None @@ -361,19 +340,27 @@ def process(self): cut_flow_trk.Fill('bare') # Apply basic preselection # - if not cut_flow_trk.disabled: - if row.processID != 26: - continue + #if not cut_flow_trk.disabled: + # if row.processID != 26: + # continue + + #if row.lumi == 2348 and row.evt == 704038: + # print 'preselection', preselection(row, cut_flow_trk, cut_settings['LT'] if lt_tpt_in_presel else 0., cut_settings['tauPT'] if lt_tpt_in_presel else 0.) cut_flow_trk.Fill('WH Event') lt_tpt_in_presel = not bool(cut_settings['tauID']) if not preselection(row, cut_flow_trk, cut_settings['LT'] if lt_tpt_in_presel else 0., cut_settings['tauPT'] if lt_tpt_in_presel else 0.): continue + + if os.environ['megatarget'].startswith('Zjets') and not obj3_matches_gen(row): + continue + # Get the generic event weight event_weight = weight_func(row) # Get the cuts that define the region sign_result = sign_cut(row) + tau_sign_result = tau_sign_cut(row) obj1_id_result = obj1_id(row, cut_settings['leading_iso'], cut_settings['subleading_iso']) obj2_id_result = obj2_id(row, cut_settings['leading_iso'], cut_settings['subleading_iso']) obj3_id_result = obj3_id(row, cut_settings['tauID'], cut_settings['LT'], cut_settings['tauPT']) @@ -384,22 +371,36 @@ def process(self): to_fill = ('',) \ if not anti_charge_flip_cut else \ ('','charge_flip_CR/') if anti_charge_flip else ('charge_flip_CR/',) - - #if not cut_flow_trk.disabled: - if obj1_id_result: - cut_flow_trk.Fill('obj1 IDIso') - if obj2_id_result: - cut_flow_trk.Fill('obj2 IDIso') - if obj3_id_result: - cut_flow_trk.Fill('obj3 IDIso') - if sign_result: - cut_flow_trk.Fill('sign cut') - if anti_wz : - cut_flow_trk.Fill('anti WZ') + + if sign_result: + cut_flow_trk.Fill('sign cut') + if tau_sign_result: + cut_flow_trk.Fill('tau sign') + if anti_wz : + cut_flow_trk.Fill('anti WZ') + if obj1_id_result: + cut_flow_trk.Fill('obj1 IDIso') + if obj2_id_result: + cut_flow_trk.Fill('obj2 IDIso') + if obj3_id_result: + cut_flow_trk.Fill('obj3 IDIso') + if anti_charge_flip: + cut_flow_trk.Fill('anti charge flip') + + + if os.environ['megatarget'].startswith('Zjets'): + #remove matched but not id/Iso leptons + if obj1_matches_gen(row) and not obj1_id_result: + continue + if obj2_matches_gen(row) and not obj2_id_result: + continue # Figure out which folder/region we are in region_result = cut_region_map.get( - (sign_result, obj1_id_result, obj2_id_result, obj3_id_result)) + (sign_result, tau_sign_result, obj1_id_result, obj2_id_result, obj3_id_result)) + + if debug_mode: + set_trace() # Ignore stupid regions we don't care about if region_result is None: @@ -407,7 +408,22 @@ def process(self): if anti_wz: base_folder, weights = region_result + base_folder = joinDirs(*base_folder) + if print_region: + print 'event %i:%i:%i assigned to region(s) %s' % \ + (row.run, row.lumi, row.evt, \ + ', '.join( joinDirs(base_folder,i) for i in to_fill) ) + if region_for_event_list: + if base_folder == region_for_event_list and \ + '' in to_fill: + print '%i:%i:%i' % (row.run, row.lumi, row.evt) + else: + continue + if no_histo_fill: # and region_for_event_list and \ + #base_folder == region_for_event_list: + continue + # Fill the un-fr-weighted histograms for i in to_fill: folder = joinDirs(base_folder,i) @@ -430,32 +446,31 @@ def process(self): if not sign_result and obj1_id_result and obj2_id_result: # Object 1 can only flip if it is OS with the tau obj1_obj3_SS = self.obj1_obj3_SS(row) - if (obj1_obj3_SS and obj1_charge_flip) \ - or ( not obj1_obj3_SS and obj2_charge_flip): #there is the function --> we have to compute it, otherwise skip and save some precious filling time! - charge_flip_prob = obj1_charge_flip(row, cut_settings['leading_iso'], cut_settings['subleading_iso']) \ - if obj1_obj3_SS else \ - obj2_charge_flip(row, cut_settings['leading_iso'], cut_settings['subleading_iso']) - charge_flip_sysu = obj1_charge_flip_sysup(row, cut_settings['leading_iso'], cut_settings['subleading_iso']) \ - if obj1_obj3_SS else \ - obj2_charge_flip_sysup(row, cut_settings['leading_iso'], cut_settings['subleading_iso']) - directory = joinDirs(base_folder,'c1' if obj1_obj3_SS else 'c2') - directory_up = joinDirs(base_folder,'c1_sysup' if obj1_obj3_SS else 'c2_sysup') - charge_flip_prob = charge_flip_prob/(1. - charge_flip_prob) - for i in to_fill: - fill_histos(histos, joinDirs(directory,i), row, event_weight*charge_flip_prob, cut_label) - fill_histos(histos, joinDirs(directory_up,i), row, event_weight*charge_flip_sysu, cut_label) - - elif sign_result and obj1_id_result and obj3_id_result: + for flipfunc, flipfunc_sysup, dirname in [(obj1_charge_flip, obj1_charge_flip_sysup, 'c1'), (obj2_charge_flip, obj2_charge_flip_sysup, 'c2')]: + if flipfunc: + charge_flip_prob = flipfunc(row, cut_settings['leading_iso'], cut_settings['subleading_iso']) + charge_flip_sysu = flipfunc_sysup(row, cut_settings['leading_iso'], cut_settings['subleading_iso']) + directory = joinDirs(base_folder, dirname) + directory_up = joinDirs(base_folder, '%s_sysup' % dirname) + charge_flip_prob = charge_flip_prob/(1. - charge_flip_prob) + for i in to_fill: + fill_histos(histos, joinDirs(directory,i), row, event_weight*charge_flip_prob, cut_label) + fill_histos(histos, joinDirs(directory_up,i), row, event_weight*charge_flip_sysu, cut_label) + + elif sign_result and obj1_id_result and obj3_id_result and tau_sign_result: + if no_histo_fill or region_for_event_list: + continue + # WZ object topology fails. Check if we are in signal region. if self.enhance_wz(row): # Signal region if obj2_id_result: - fill_histos(histos, 'ss/p1p2p3_enhance_wz/', row, event_weight, cut_label) + fill_histos(histos, 'ss/tau_os/p1p2p3_enhance_wz/', row, event_weight, cut_label) else: - fill_histos(histos, 'ss/p1f2p3_enhance_wz/', row, event_weight, cut_label) + fill_histos(histos, 'ss/tau_os/p1f2p3_enhance_wz/', row, event_weight, cut_label) fake_rate_obj2 = self.obj2_weight(row, cut_settings['leading_iso'], cut_settings['subleading_iso']) fake_weight = fake_rate_obj2/(1.-fake_rate_obj2) - fill_histos(histos, 'ss/p1f2p3_enhance_wz/w2/', row, event_weight*fake_weight, cut_label) + fill_histos(histos, 'ss/tau_os/p1f2p3_enhance_wz/w2/', row, event_weight*fake_weight, cut_label) cut_flow_trk.flush() def finish(self): diff --git a/wh/WHPlotterBase.py b/wh/WHPlotterBase.py index 4e29d70b..e591b3d3 100644 --- a/wh/WHPlotterBase.py +++ b/wh/WHPlotterBase.py @@ -19,10 +19,14 @@ from FinalStateAnalysis.PlotTools.MedianView import MedianView from FinalStateAnalysis.PlotTools.ProjectionView import ProjectionView from FinalStateAnalysis.PlotTools.FixedIntegralView import FixedIntegralView +from FinalStateAnalysis.PlotTools.DifferentialView import DifferentialView +from FinalStateAnalysis.PlotTools.SubtractionView import SubtractionView, PositiveView from FinalStateAnalysis.PlotTools.RebinView import RebinView from FinalStateAnalysis.MetaData.data_styles import data_styles, colors from FinalStateAnalysis.PlotTools.decorators import memo from FinalStateAnalysis.MetaData.datacommon import br_w_leptons, br_z_leptons +from FinalStateAnalysis.Utilities.floatformatting import smart_float_format +from pdb import set_trace from optparse import OptionParser import os import ROOT @@ -30,16 +34,27 @@ import math import logging from fnmatch import fnmatch -from yellowhiggs import xs, br, xsbr + +ROOT.gStyle.SetOptTitle(0) +#ROOT.gROOT.SetStyle('Plain') parser = OptionParser(description=__doc__) parser.add_option('--dry-run', action='store_true', dest='dry_run', default = False, help='produces only shape file and minimal histograms') + +parser.add_option('--no-mc-data', action='store_true', dest='no_mc_data', default = False) +parser.add_option('--no-wz', action='store_true', dest='no_wz', default = False) +parser.add_option('--no-signal', action='store_true', dest='no_signal', default = False) +parser.add_option('--no-f3', action='store_true', dest='no_f3', default = False) +parser.add_option('--no-shapes', action='store_true', dest='no_shapes', default = False) + parser.add_option('--prefix', metavar='label', type=str, dest='prefix', default = '', help='prefix to eppend before histogram name o be used to make the shapes' ) parser.add_option('--prefixes', metavar='label', type=str, dest='prefixes', default = '', help='prefix to eppend before histogram name o be used to make the shapes' ) +project_x = lambda x: ProjectionView(x, 'X', [0, 650]) + def get_chi_square(hdata, hexp): chi2 = 0. nbins = 0. @@ -68,6 +83,81 @@ def _f(path): def remove_name_entry(dictionary): return dict( [ i for i in dictionary.iteritems() if i[0] != 'name'] ) +def fake_rate_estimate(histograms): #always, ALWAYS give as 1,2,0 + cat1 = histograms.next() + cat2 = histograms.next() + cat0 = histograms.next() + ret = cat0.Clone() + ret.Reset() + integral = sum( i.Integral() for i in [cat0, cat1, cat2]) + entries = sum( i.GetEntries() for i in [cat0, cat1, cat2]) + weight = integral / float(entries) + + for i in range(ret.GetNbinsX() + 2): + c1 = cat1.GetBinContent(i) + c2 = cat2.GetBinContent(i) + c0 = cat0.GetBinContent(i) + e1 = cat1.GetBinError(i) + e2 = cat2.GetBinError(i) + e0 = cat0.GetBinError(i) + content = c1+c2-c0 + #if content <= 0: + # print "bin [%.1f, %.1f]" % (cat0.GetBinLowEdge(i), cat0.GetBinLowEdge(i)+cat0.GetBinWidth(i)) + # print "content %s c0: %s c1: %s c2: %s" % (content, c0, c1, c2) + if content <= 0 and c0 > 0: + #print "setting content to %s +/- %s" % (c0, e0) + ret.SetBinContent(i,c0) + ret.SetBinError(i,e0) + elif content <= 0: + #print "setting content to 10**-5 +/- %s" % (1.8*weight) + ret.SetBinContent(i,10**-5) + ret.SetBinError(i,1.8*weight) + else: + ret.SetBinContent(i,c1+c2-c0) + ret.SetBinError(i,quad(e1,e2,e0)) + return ret + +def make_empty_bin_remover(weight): + def mc_empty_bin_remover(histogram): + ret = histogram.Clone() + for i in range(ret.GetNbinsX() + 2): + if ret.GetBinContent(i) <= 0: + ret.SetBinContent(i, 0.9200*weight) #MEAN WEIGHT + ret.SetBinError(i, 1.8*weight) + return ret + return mc_empty_bin_remover + +class MultyErrorView(object): #FIXME + '''takes a StackView and adds systematics''' + def __init__(self, stackview, systematics_map): + self.stack_view = stackview + self.systematics_map = systematics_map + + def Get(self, path): + stack_components = self.stack_view.Get(path).GetHists() + stack_sum = sum(stack_components) + + for bin in range(1, stack_sum.GetNbinsX() + 1): + #sys_errors = [ sum(hist.GetBinContent(bin)*value for hist in stack_components if fnmatch(hist.GetTitle(), key)) for key, value in self.systematics_map ] + sys_errors = [] + for key, value in self.systematics_map.iteritems(): + it = 0 + for hist in stack_components: + if fnmatch(hist.GetTitle(), key): + print '%s matches %s' % (hist.GetTitle(), key) + it += hist.GetBinContent(bin)*value + sys_errors.append(it) + + error = quad( stack_sum.GetBinError(bin), *sys_errors ) + stack_sum.SetBinError(bin, error) + + stack_sum.SetMarkerSize(0) + stack_sum.SetFillColor(1) + stack_sum.SetFillStyle(3013) + stack_sum.legendstyle = 'f' + return stack_sum + + class BackgroundErrorView(object): ''' Compute the total background error in each bin. ''' def __init__(self, fakes, wz, zz, charge_fake, wz_error=0.1, zz_error=0.04, @@ -183,7 +273,7 @@ def unsuck(x): class WHPlotterBase(Plotter): def __init__(self, channel, obj1_charge_mapper={}, obj2_charge_mapper={}): cwd = os.getcwd() - os.chdir( os.path.dirname(__file__) ) + #os.chdir( os.path.dirname(__file__) ) self.channel = channel jobid = os.environ['jobid'] print "\nPlotting %s for %s\n" % (channel, jobid) @@ -199,7 +289,7 @@ def __init__(self, channel, obj1_charge_mapper={}, obj2_charge_mapper={}): 'ZZ*', 'VH*', 'WW*', - 'TTplusJets_madgraph', + #'TTplusJets_madgraph', "data_*", ] @@ -223,17 +313,18 @@ def __init__(self, channel, obj1_charge_mapper={}, obj2_charge_mapper={}): self.obj2_charge_mapper=obj2_charge_mapper #maps scaled charge flip histograms to the ones of the normal categories if blind: # Don't look at the SS all pass region - blinder = lambda x: BlindView(x, "ss/p1p2p3/.*") + blinder = lambda x: BlindView(x, "ss/tau_os/p1p2p3/.*") super(WHPlotterBase, self).__init__(files, lumifiles, self.outputdir, blinder) self.defaults = {} #allows to set some options and avoid repeating them each function call - os.chdir( cwd ) + self.mc_samples = filter(lambda x: not x.startswith('data'), samples) + #os.chdir( cwd ) #create a fake wiew summing up all HWW - self.views['VH_hww_sum'] = { - 'unweighted_view' : views.SumView( - *[item['unweighted_view'] for name, item in self.views.iteritems() if fnmatch(name, 'VH_*_HWW*')] - ) - } + #self.views['VH_hww_sum'] = { + # 'unweighted_view' : views.SumView( + # *[item['unweighted_view'] for name, item in self.views.iteritems() if fnmatch(name, 'VH_*_HWW*')] + # ) + #} def set_subdir(self, folder): self.outputdir = '/'.join([self.base_out_dir, folder]) @@ -243,44 +334,91 @@ def apply_to_dict(self, dictionary, viewtype, *args, **kwargs): #project, projec for key, val in dictionary.iteritems(): if isinstance(val, dict): ret[key] = self.apply_to_dict(val, viewtype, *args, **kwargs) #project, project_axis, rebin) + elif val is None: + ret[key] = None else: ret[key] = viewtype(val, *args, **kwargs) # self.rebin_view( ProjectionView(val, project_axis, project), rebin ) return ret - @memo - def make_signal_views(self, unblinded=False, qcd_weight_fraction=0): + def make_additional_fakes_view(self, unblinded=False, rebin=1, + project=None, project_axis=None, tau_charge='tau_os' ): + other_tau_sign = 'tau_os' if tau_charge == 'tau_ss' else 'tau_ss' + def preprocess(view): + ret = view + if project and project_axis: + ret = ProjectionView(ret, project_axis, project) + return RebinView( ret, rebin ) + + zjets_view = preprocess( self.get_view('Zjets*') ) + all_data_view = preprocess( self.get_view('data') ) + zjets_fakes = views.SumView( + views.SubdirectoryView( zjets_view, 'ss/%s/f1p2p3/w1/' % tau_charge ), + views.SubdirectoryView( zjets_view, 'ss/%s/p1f2p3/w2/' % tau_charge ) + #,views.SubdirectoryView( zjets_view, 'ss/%s/f1f2p3/w12/' % tau_charge ) #FIXME: check that effect is small + ) + tau_fakes = views.SubdirectoryView(all_data_view, 'ss/%s/p1p2f3/w3/' % tau_charge) + full_fakes = views.SumView(tau_fakes, zjets_fakes) + + return { + 'fakes' : full_fakes, + 'zjet_fakes' : zjets_fakes, + 'tau_fakes' : tau_fakes, + } + + def make_signal_views(self, unblinded=False, qcd_weight_fraction=0, #MARK + rebin=1, project=None, project_axis=None, tau_charge='tau_os' ): ''' Make signal views with FR background estimation ''' + other_tau_sign = 'tau_os' if tau_charge == 'tau_ss' else 'tau_ss' + def preprocess(view): + ret = view + if project and project_axis: + ret = ProjectionView(ret, project_axis, project) + return RebinView( ret, rebin ) + + all_wz_view_tautau = preprocess( self.get_view('WZJetsTo3LNu*ZToTauTau*') ) wz_view_tautau = views.SubdirectoryView( - self.get_view('WZJetsTo3LNu*ZToTauTau*'), - 'ss/p1p2p3/' + all_wz_view_tautau, + 'ss/%s/p1p2p3/' % tau_charge ) + + tomatch = 'WZJetsTo3LNu' if self.sqrts == 7 else 'WZJetsTo3LNu_pythia' + all_wz_view_3l = preprocess( self.get_view(tomatch) ) wz_view_3l = views.SubdirectoryView( - self.get_view('WZJetsTo3LNu_pythia'), - 'ss/p1p2p3/' + all_wz_view_3l, + 'ss/%s/p1p2p3/' % tau_charge ) - zz_view = views.SubdirectoryView( - self.get_view('ZZJetsTo4L*'), - 'ss/p1p2p3/' + all_wz_view = views.SumView(all_wz_view_tautau, all_wz_view_3l) + + zz_view = preprocess( + views.SubdirectoryView( + self.get_view('ZZJetsTo4L*'), + 'ss/%s/p1p2p3/' % tau_charge + ) ) - all_data_view = self.get_view('data') - if unblinded: - all_data_view = self.get_view('data', 'unblinded_view') - data_view = views.SubdirectoryView(all_data_view, 'ss/p1p2p3/') + all_data_view = self.get_view('data') + #if unblinded: + # all_data_view = self.get_view('data', 'unblinded_view') + all_data_view = preprocess(all_data_view) + + data_view = views.SubdirectoryView(all_data_view, 'ss/%s/p1p2p3/' % tau_charge) def make_fakes(qcd_fraction): def make_fakes_view(weight_type, scale): - scaled_data = views.ScaleView(all_data_view, scale) + scaled_bare_data = views.ScaleView(all_data_view, scale) + scaled_wz_data = views.ScaleView(all_wz_view, scale) + scaled_data = SubtractionView(scaled_bare_data, scaled_wz_data, restrict_positive=True) + # View of weighted obj1-fails data obj1_view = views.SubdirectoryView( - scaled_data, 'ss/f1p2p3/%s1' % weight_type) + scaled_data, 'ss/%s/f1p2p3/%s1' % (tau_charge, weight_type)) # View of weighted obj2-fails data obj2_view = views.SubdirectoryView( - scaled_data, 'ss/p1f2p3/%s2' % weight_type) + scaled_data, 'ss/%s/p1f2p3/%s2' % (tau_charge, weight_type)) # View of weighted obj1&2-fails data obj12_view = views.SubdirectoryView( - scaled_data, 'ss/f1f2p3/%s12' % weight_type) + scaled_data, 'ss/%s/f1f2p3/%s12' % (tau_charge, weight_type)) # Give the individual object views nice colors obj1_view = views.TitleView( @@ -293,20 +431,18 @@ def make_fakes_view(weight_type, scale): views.StyleView(obj12_view, **remove_name_entry(data_styles['WW*'])), 'Reducible bkg. 12') - subtract_obj12_view = views.ScaleView(obj12_view, -1) - return obj1_view, obj2_view, obj12_view, subtract_obj12_view + return obj1_view, obj2_view, obj12_view - qcd1, qcd2, qcd12, negqcd12 = make_fakes_view('q', qcd_fraction) - wjet1, wjet2, wjet12, negwjet12 = make_fakes_view( + qcd1, qcd2, qcd12 = make_fakes_view('q', qcd_fraction) + wjet1, wjet2, wjet12 = make_fakes_view( 'w', 1 - qcd_fraction) obj1_view = views.SumView(qcd1, wjet1) obj2_view = views.SumView(qcd2, wjet2) obj12_view = views.SumView(qcd12, wjet12) - subtract_obj12_view = views.SumView(negqcd12, negwjet12) # Corrected fake view - fakes_view = views.SumView(obj1_view, obj2_view, subtract_obj12_view) + fakes_view = views.MultiFunctorView(fake_rate_estimate, obj1_view, obj2_view, obj12_view) fakes_view = views.TitleView( views.StyleView(fakes_view, **remove_name_entry(data_styles['Zjets*'])), 'Reducible bkg.') return obj1_view, obj2_view, obj12_view, fakes_view @@ -320,11 +456,11 @@ def make_fakes_view(weight_type, scale): views.StyleView( views.SumView( views.PathModifierView( - views.SubdirectoryView(all_data_view, 'os/p1p2p3/c1'), + views.SubdirectoryView(all_data_view, 'os/%s/p1p2p3/c1' % other_tau_sign), #FIXME: needs to be fixed for charge 3 region create_mapper(self.obj1_charge_mapper) ), views.PathModifierView( - views.SubdirectoryView(all_data_view, 'os/p1p2p3/c2'), + views.SubdirectoryView(all_data_view, 'os/%s/p1p2p3/c2' % tau_charge), create_mapper(self.obj2_charge_mapper) ), ), @@ -335,11 +471,11 @@ def make_fakes_view(weight_type, scale): views.StyleView( views.SumView( views.PathModifierView( - views.SubdirectoryView(all_data_view, 'os/p1p2p3/c1_sysup'), + views.SubdirectoryView(all_data_view, 'os/%s/p1p2p3/c1_sysup' % other_tau_sign), create_mapper(self.obj1_charge_mapper) ), views.PathModifierView( - views.SubdirectoryView(all_data_view, 'os/p1p2p3/c2_sysup'), + views.SubdirectoryView(all_data_view, 'os/%s/p1p2p3/c2_sysup' % tau_charge), create_mapper(self.obj2_charge_mapper) ), ), @@ -370,20 +506,36 @@ def make_fakes_view(weight_type, scale): # Add signal data_total_lumi = self.views['data']['intlumi'] - for mass in range(110, 165, 5): - vh_view = views.SubdirectoryView( - self.get_view('VH_*%i' % mass), - 'ss/p1p2p3/' - ) - output['vh%i' % mass] = vh_view + for mass in range(90, 165, 5): + try: + vh_base_name = 'VH_%s' % mass if self.sqrts == 7 else 'VH_H2Tau_M-%s' % mass + vh_base_name = 'VHtautau_lepdecay_%s' % mass \ + if 'VHtautau_lepdecay_%s' % mass in self.views else \ + vh_base_name + #print 'using %s' % vh_base_name + vh_base_view = self.views[vh_base_name]['view'] + + vh_view = views.SubdirectoryView( + vh_base_view, #self.get_view('VH_*%i' % mass), + 'ss/%s/p1p2p3/' % tau_charge + ) + output['vh%i' % mass] = preprocess(vh_view) + except KeyError: + #logging.warning('No sample found matching VH_*%i' % mass) + continue + if mass % 10 == 0 and mass < 150: # Only have 10 GeV steps for WW - ww_view = views.SubdirectoryView( - self.get_view('VH_%i_HWW*' % mass), - 'ss/p1p2p3/' - ) - output['vh%i_hww' % mass] = ww_view - output['signal%i' % mass] = views.SumView(ww_view, vh_view) + try: + ww_view = views.SubdirectoryView( + self.get_view('VH_%i_HWW*' % mass), + 'ss/%s/p1p2p3/' % tau_charge + ) + output['vh%i_hww' % mass] = preprocess(ww_view) + except KeyError: + #logging.warning('No sample found matching VH_%i_HWW*' % mass) + continue + #output['signal%i' % mass] = views.SumView(ww_view, vh_view) if ww_view else vh_view return output @@ -396,12 +548,12 @@ def make_qcd_proj_views(self, control_region, rebin): mapping = { 1: { - 'obs': 'ss/f1p2p3', - 'qcd': 'ss/f1f2f3/w23', + 'obs': 'ss/tau_os/f1p2p3', + 'qcd': 'ss/tau_os/f1f2f3/w23', }, 2: { - 'obs': 'ss/p1f2p3', - 'qcd': 'ss/f1f2f3/w13', + 'obs': 'ss/tau_os/p1f2p3', + 'qcd': 'ss/tau_os/f1f2f3/w13', }, } @@ -420,36 +572,47 @@ def make_qcd_proj_views(self, control_region, rebin): @memo def make_obj3_fail_cr_views(self, qcd_correction=False, - qcd_weight_fraction=0): + qcd_weight_fraction=0, tau_charge='tau_os'): ''' Make views when obj3 fails, estimating the bkg in obj1 pass using f1p2f3 ''' + other_tau_sign = 'tau_os' if tau_charge == 'tau_ss' else 'tau_ss' + + all_wz_ztt_view = self.get_view('WZJetsTo3LNu*ZToTauTau*') wz_view = views.SubdirectoryView( - self.get_view('WZJetsTo3LNu*ZToTauTau*'), - 'ss/p1p2f3/' + all_wz_ztt_view, + 'ss/%s/p1p2f3/' % tau_charge ) + + tomatch = 'WZJetsTo3LNu' if self.sqrts == 7 else 'WZJetsTo3LNu_pythia' + all_wz_3l_view = self.get_view(tomatch) wz_view_3l = views.SubdirectoryView( - self.get_view('WZJetsTo3LNu_pythia'), - 'ss/p1p2f3/' + all_wz_3l_view, + 'ss/%s/p1p2f3/' % tau_charge ) + + all_wz_view = views.SumView(all_wz_ztt_view, all_wz_3l_view) + zz_view = views.SubdirectoryView( self.get_view('ZZJetsTo4L*'), - 'ss/p1p2f3/' + 'ss/%s/p1p2f3/' % tau_charge ) all_data_view = self.get_view('data') - data_view = views.SubdirectoryView(all_data_view, 'ss/p1p2f3/') + data_view = views.SubdirectoryView(all_data_view, 'ss/%s/p1p2f3/' % tau_charge) def make_fakes(qcd_fraction): def make_fakes_view(weight_type, scale): - scaled_data = views.ScaleView(all_data_view, scale) + scaled_bare_data = views.ScaleView(all_data_view, scale) + scaled_wz_data = views.ScaleView(all_wz_view, scale) + scaled_data = SubtractionView(scaled_bare_data, scaled_wz_data, restrict_positive=True) # View of weighted obj1-fails data obj1_view = views.SubdirectoryView( - scaled_data, 'ss/f1p2f3/%s1' % weight_type) + scaled_data, 'ss/%s/f1p2f3/%s1' % (tau_charge, weight_type)) # View of weighted obj2-fails data obj2_view = views.SubdirectoryView( - scaled_data, 'ss/p1f2f3/%s2' % weight_type) + scaled_data, 'ss/%s/p1f2f3/%s2' % (tau_charge, weight_type)) # View of weighted obj1&2-fails data obj12_view = views.SubdirectoryView( - scaled_data, 'ss/f1f2f3/%s12' % weight_type) + scaled_data, 'ss/%s/f1f2f3/%s12' % (tau_charge, weight_type)) # Give the individual object views nice colors obj1_view = views.TitleView( @@ -486,15 +649,15 @@ def make_fakes_view(weight_type, scale): fakes_view_1 = make_fakes(1)[-1] style_dict_no_name = remove_name_entry(data_styles['TT*']) - charge_fakes = views.TitleView( + charge_fakes = views.TitleView( #FIXME views.StyleView( views.SumView( views.PathModifierView( - views.SubdirectoryView(all_data_view, 'os/p1p2f3/c1'), + views.SubdirectoryView(all_data_view, 'os/%s/p1p2f3/c1' % other_tau_sign), create_mapper(self.obj1_charge_mapper) ), views.PathModifierView( - views.SubdirectoryView(all_data_view, 'os/p1p2f3/c2'), + views.SubdirectoryView(all_data_view, 'os/%s/p1p2f3/c2' % tau_charge), create_mapper(self.obj2_charge_mapper) ), ), @@ -505,11 +668,11 @@ def make_fakes_view(weight_type, scale): views.StyleView( views.SumView( views.PathModifierView( - views.SubdirectoryView(all_data_view, 'os/p1p2f3/c1_sysup'), + views.SubdirectoryView(all_data_view, 'os/%s/p1p2f3/c1_sysup' % other_tau_sign), create_mapper(self.obj1_charge_mapper) ), views.PathModifierView( - views.SubdirectoryView(all_data_view, 'os/p1p2f3/c2_sysup'), + views.SubdirectoryView(all_data_view, 'os/%s/p1p2f3/c2_sysup' % tau_charge), create_mapper(self.obj2_charge_mapper) ), ), @@ -542,110 +705,148 @@ def make_fakes_view(weight_type, scale): mass = 120 vh_view = views.SubdirectoryView( self.get_view('VH_*%i' % mass), - 'ss/p1p2f3/' + 'ss/tau_os/p1p2f3/' ) output['vh%i' % mass] = vh_view - ww_view = views.SubdirectoryView( - self.get_view('VH_%i_HWW*' % mass), - 'ss/p1p2f3/' - ) + try: + ww_view = views.SubdirectoryView( + self.get_view('VH_%i_HWW*' % mass), + 'ss/tau_os/p1p2f3/' + ) + except KeyError: + #logging.warning('No sample found matching VH_%i_HWW*' % mass) + ww_view = None output['vh%i_hww' % mass] = ww_view - output['signal%i' % mass] = views.SumView(ww_view, vh_view) + output['signal%i' % mass] = views.SumView(ww_view, vh_view) if ww_view else vh_view return output - def make_wz_cr_views(self, rebin): + def make_wz_cr_views(self, rebin=1, project=None, project_axis=None): ''' Make WZ control region views with FR background estimation ''' - wz_view = views.SubdirectoryView( - self.rebin_view(self.get_view('WZJetsTo3LNu*'), rebin), - 'ss/p1p2p3_enhance_wz/' - ) - zz_view = views.SubdirectoryView( - self.rebin_view(self.get_view('ZZJetsTo4L*'), rebin), - 'ss/p1p2p3_enhance_wz/' - ) - all_data_view = self.rebin_view(self.get_view('data'), rebin) + def preprocess(view): + ret = view + if project and project_axis: + ret = ProjectionView(ret, project_axis, project) + return RebinView( ret, rebin ) + + wz_view_tautau_all = preprocess( self.get_view('WZJetsTo3LNu*ZToTauTau*') ) + wz_view_tautau = views.SubdirectoryView(wz_view_tautau_all, 'ss/tau_os/p1p2p3_enhance_wz/') + + tomatch = 'WZJetsTo3LNu' if self.sqrts == 7 else 'WZJetsTo3LNu_pythia' + wz_view_3l_all = preprocess( self.get_view(tomatch) ) + wz_view_3l = views.SubdirectoryView(wz_view_3l_all, 'ss/tau_os/p1p2p3_enhance_wz/') + wz_view_all = views.SumView(wz_view_tautau_all, wz_view_3l_all) + + zz_view_all = preprocess( self.get_view('ZZJetsTo4L*') ) + zz_view = views.SubdirectoryView(zz_view_all, 'ss/tau_os/p1p2p3_enhance_wz/') + + all_data_view = preprocess( self.get_view('data') ) data_view = views.SubdirectoryView( - all_data_view, 'ss/p1p2p3_enhance_wz/') + all_data_view, 'ss/tau_os/p1p2p3_enhance_wz/') # View of weighted obj2-fails data fakes_view = views.SubdirectoryView( - all_data_view, 'ss/p1f2p3_enhance_wz/w2') + all_data_view, 'ss/tau_os/p1f2p3_enhance_wz/w2') fakes_view = views.StyleView(fakes_view, **remove_name_entry(data_styles['Zjets*'])) # Correct - wz_in_fakes_view = views.SubdirectoryView( - self.rebin_view(self.get_view('WZJetsTo3LNu*'), rebin), - 'ss/p1f2p3_enhance_wz/w2' - ) - zz_in_fakes_view = views.SubdirectoryView( - self.rebin_view(self.get_view('ZZJetsTo4L*'), rebin), - 'ss/p1f2p3_enhance_wz/w2' - ) + wz_in_fakes_view = views.SubdirectoryView(wz_view_all, 'ss/tau_os/p1f2p3_enhance_wz/w2') + zz_in_fakes_view = views.SubdirectoryView(zz_view_all, 'ss/tau_os/p1f2p3_enhance_wz/w2') diboson_view = views.SumView(wz_in_fakes_view, zz_in_fakes_view) inverted_diboson_view = views.ScaleView(diboson_view, -1) fakes_view = views.SumView(fakes_view, inverted_diboson_view) - fakes_view = views.TitleView(fakes_view, 'Reducible bkg.') output = { - 'wz': wz_view, - 'zz': zz_view, - 'data': data_view, - 'fakes': fakes_view + 'wz_ztt': wz_view_tautau, + 'wz_3l' : wz_view_3l, + 'zz' : zz_view, + 'data' : data_view, + 'fakes' : fakes_view } - - # Add signal - for mass in [110, 120, 130, 140]: - vh_view = views.SubdirectoryView( - self.rebin_view(self.get_view('VH_*%i' % mass, 'unweighted_view'), rebin), - 'ss/p1p2p3/' - ) - output['vh%i' % mass] = vh_view - return output + # Add signal + #for mass in [110, 120, 130, 140]: + # vh_view = views.SubdirectoryView( + # self.rebin_view(self.get_view('VH_*%i' % mass, 'unweighted_view'), rebin), + # 'ss/tau_os/p1p2p3/' + # ) + # output['vh%i' % mass] = vh_view + # + #return output def write_shapes(self, variable, rebin, outdir, - qcd_fraction=0, show_charge_fakes=False, - project=None, project_axis=None): + qcd_fraction=0., #[1., 0., -1.], + show_charge_fakes=False, + project=None, project_axis=None, different_fakes=False): ''' Write final shapes for [variable] into a TDirectory [outputdir] ''' show_charge_fakes = show_charge_fakes if 'show_charge_fakes' not in self.defaults else self.defaults['show_charge_fakes'] sig_view = self.make_signal_views(unblinded=(not self.blind), - qcd_weight_fraction=qcd_fraction) - - if project and project_axis: - sig_view = self.apply_to_dict( sig_view, ProjectionView, project_axis, project ) - sig_view = self.apply_to_dict( sig_view, RebinView, rebin ) + qcd_weight_fraction=qcd_fraction, + rebin=rebin, project=project, project_axis=project_axis) + different_fakes_views = self.make_additional_fakes_view( unblinded=(not self.blind), rebin=rebin, + project=project, project_axis=project_axis) + + outdir.cd() - wz = views.SumView(sig_view['wz'], sig_view['wz_3l']).Get(variable) - zz = sig_view['zz'].Get(variable) + wz_weight = self.get_view('WZJetsTo3LNu*ZToTauTau*', 'weight') + zz_weight = self.get_view('ZZJetsTo4L*', 'weight') + print "wz_weight: %s" % wz_weight + print "zz_weight: %s" % zz_weight + + wz = views.FunctorView( views.SumView(sig_view['wz'], sig_view['wz_3l']), make_empty_bin_remover(wz_weight)).Get(variable) + zz = views.FunctorView( sig_view['zz'], make_empty_bin_remover(zz_weight)).Get(variable) obs = sig_view['data'].Get(variable) - fakes = sig_view['fakes'].Get(variable) + fakes = sig_view['fakes'].Get(variable) if not different_fakes else different_fakes_views['fakes'].Get(variable) + + fakes_down = different_fakes_views['fakes'].Get(variable) + fakes_up = PositiveView( + views.SumView( views.ScaleView(sig_view['fakes'], 2.), + views.ScaleView(different_fakes_views['fakes'], -1.) + ) + ).Get(variable) wz.SetName('wz') zz.SetName('zz') obs.SetName('data_obs') fakes.SetName('fakes') - + fakes_down.SetName('fakes_CMS_vhtt_%s_fakeshape_%sTeVDown' % (outdir.GetName(), self.sqrts)) + fakes_up.SetName('fakes_CMS_vhtt_%s_fakeshape_%sTeVUp' % (outdir.GetName(), self.sqrts)) #for mass in [110, 115, 120, 125, 130, 135, 140]: - for mass in range(110, 165, 5): - vh = sig_view['vh%i' % mass].Get(variable) - vh.SetName('WH%i' % mass) - vh.Write() + #set_trace() + for mass in range(90, 165, 5): + try: + vh = None + if mass == 90 and self.sqrts == 8: + vh = views.ScaleView(sig_view['vh100'], 1.3719).Get(variable) + elif mass == 95 and self.sqrts == 8: + vh = views.ScaleView(sig_view['vh100'], 1.1717).Get(variable) + else: + vh = sig_view['vh%i' % mass].Get(variable) + vh.SetName('WH%i' % mass) + vh.SetLineColor(0) + vh.Write() + except KeyError: + #logging.warning('No sample found matching VH_*%i' % mass) + continue + if mass % 10 == 0 and mass < 150: # Only have 10 GeV steps for WW - ww = sig_view['vh%i_hww' % mass].Get(variable) - ww.SetName('WH_hww%i' % mass) - ww.Write() + if 'vh%i_hww' % mass in sig_view: + ww = sig_view['vh%i_hww' % mass].Get(variable) + ww.SetName('WH_hww%i' % mass) + ww.Write() wz.Write() zz.Write() obs.Write() fakes.Write() + fakes_down.Write() + fakes_up.Write() #charge_fakes_CMS_vhtt_emt_chargeFlip_8TeVUpx if show_charge_fakes: logging.info('adding charge fakes shape errors') @@ -700,9 +901,10 @@ def write_f3_shapes(self, variable, rebin, outdir, vh.SetName('WH%i' % mass) vh.Write() # Only have 10 GeV steps for WW - ww = sig_view['vh%i_hww' % mass].Get(variable) - ww.SetName('WH_hww%i' % mass) - ww.Write() + if sig_view['vh%i_hww' % mass]: + ww = sig_view['vh%i_hww' % mass].Get(variable) + ww.SetName('WH_hww125') + ww.Write() #charge_fakes_CMS_vhtt_emt_chargeFlip_8TeVUpx if show_charge_fakes: @@ -719,20 +921,22 @@ def write_f3_shapes(self, variable, rebin, outdir, def plot_final(self, variable, rebin=1, xaxis='', maxy=24, show_error=False, qcd_correction=False, stack_higgs=True, - qcd_weight_fraction=0.5, x_range=None, show_charge_fakes=False, - leftside_legend=False, higgs_xsec_multiplier=5, project=None, - project_axis=None, **kwargs): + qcd_weight_fraction=0., x_range=None, show_charge_fakes=False, + leftside_legend=False, higgs_xsec_multiplier=1, project=None, + project_axis=None, differential=False, yaxis='Events', tau_charge='tau_os', **kwargs): ''' Plot the final output - with bkg. estimation ''' show_charge_fakes = show_charge_fakes if 'show_charge_fakes' not in self.defaults else self.defaults['show_charge_fakes'] sig_view = self.make_signal_views(unblinded=(not self.blind), - qcd_weight_fraction=qcd_weight_fraction) - if project and project_axis: - sig_view = self.apply_to_dict( sig_view, ProjectionView, project_axis, project ) - sig_view = self.apply_to_dict( sig_view, RebinView, rebin ) #Rebin + qcd_weight_fraction=qcd_weight_fraction, + rebin=rebin, project=project, + project_axis=project_axis, tau_charge=tau_charge) + + if differential: + sig_view = self.apply_to_dict(sig_view, DifferentialView) vh_10x = views.TitleView( views.StyleView( - views.ScaleView(sig_view['signal120'], higgs_xsec_multiplier), + views.ScaleView(sig_view['vh125'], higgs_xsec_multiplier), **remove_name_entry(data_styles['VH*']) ), "(%i#times) m_{H} = 125" % higgs_xsec_multiplier @@ -740,48 +944,53 @@ def plot_final(self, variable, rebin=1, xaxis='', maxy=24, charge_fakes_view = MedianView(highv=sig_view['charge_fakes']['sys_up'], centv=sig_view['charge_fakes']['central']) # Fudge factor to go from 120->125 - change in xsec*BR - vh_10x = views.ScaleView(vh_10x, .783) + #vh_10x = views.ScaleView(vh_10x), .783) tostack = [sig_view['wz_3l'], sig_view['zz'], sig_view['wz'], sig_view['fakes'], vh_10x] if stack_higgs else \ [sig_view['wz_3l'], sig_view['zz'], sig_view['wz'], sig_view['fakes']] if show_charge_fakes: tostack = tostack[:2]+[charge_fakes_view]+tostack[2:] + + vh_hww = views.ScaleView(sig_view['vh120_hww'], .783) if 'vh120_hww' in sig_view else None + if vh_hww: + tostack = tostack[:-1] + [vh_hww] + tostack[-1:] + stack = views.StackView( *tostack ) histo = stack.Get(variable) histo.Draw() histo.GetHistogram().GetXaxis().SetTitle(xaxis) + histo.GetHistogram().GetYaxis().SetTitle(yaxis) + if x_range: histo.GetHistogram().GetXaxis().SetRangeUser(x_range[0], x_range[1]) - if isinstance(maxy, (int, long, float)): - #print "setting maxy to %s" % maxy - histo.SetMaximum(maxy) - self.canvas.Update() - else: - histo.SetMaximum(sum(histo.GetHists()).GetMaximum()*1.2) self.keep.append(histo) # Add legend - legend = self.add_legend(histo, leftside=leftside_legend, entries=4) - + entries = len(tostack)+1 if show_error: - correct_qcd_view = None - if qcd_weight_fraction == 0: - fakes05 = sig_view['weighted_fakes'][0.5] - correct_qcd_view = MedianView(highv=fakes05, centv=sig_view['fakes']) - - elif qcd_weight_fraction == 0.5: - fakes1 = sig_view['weighted_fakes'][1.] - correct_qcd_view = MedianView(highv=fakes1, centv=sig_view['fakes']) + entries += 1 + legend = self.add_legend(histo, leftside=leftside_legend, entries=entries) - elif qcd_weight_fraction == 1: - fakes05 = sig_view['weighted_fakes'][0.5] - correct_qcd_view = MedianView(lowv=fakes05, centv=sig_view['fakes']) + if show_error: + #correct_qcd_view = None + #if qcd_weight_fraction == 0: + # fakes05 = sig_view['weighted_fakes'][1.] + # correct_qcd_view = MedianView(lowv=fakes05, centv=sig_view['fakes']) + # + #elif qcd_weight_fraction == 0.5: + # fakes1 = sig_view['weighted_fakes'][1.] + # correct_qcd_view = MedianView(highv=fakes1, centv=sig_view['fakes']) + # + #elif qcd_weight_fraction == 1: + # fakes05 = sig_view['weighted_fakes'][0.5] + # correct_qcd_view = MedianView(lowv=fakes05, centv=sig_view['fakes']) bkg_error_view = BackgroundErrorView( - correct_qcd_view, #sig_view['fakes'], + sig_view['fakes'], #correct_qcd_view, #sig_view['fakes'], views.SumView( sig_view['wz'], sig_view['wz_3l']), sig_view['zz'], charge_fakes_view, + fake_error=0.3, **kwargs ) bkg_error = bkg_error_view.Get(variable) @@ -790,29 +999,40 @@ def plot_final(self, variable, rebin=1, xaxis='', maxy=24, legend.AddEntry(bkg_error) # Use poisson error bars on the data - sig_view['data'] = PoissonView(sig_view['data'], x_err=False) + sig_view['data'] = PoissonView(sig_view['data'], x_err=False, is_scaled=differential) #PoissonView(, x_err=False) data = sig_view['data'].Get(variable) - if not self.blind: + ymax = histo.GetMaximum() + if not self.blind or tau_charge != 'tau_os': + #print "drawing", data.Integral() data.Draw('pe,same') + legend.AddEntry(data) + ymax = max(ymax, data.GetMaximum()) self.keep.append(data) + if isinstance(maxy, (int, long, float)): + #print "setting maxy to %s" % maxy + histo.SetMaximum(maxy) + self.canvas.Update() + else: + histo.SetMaximum(ymax*1.2) + if not stack_higgs: higgs_plot = vh_10x.Get(variable) higgs_plot.Draw('same') self.keep.append(higgs_plot) - #legend.AddEntry(data) legend.Draw() - def plot_final_wz(self, variable, rebin=1, xaxis='', maxy=None): + def plot_final_wz(self, variable, rebin=1, xaxis='', maxy=None, project=None, project_axis=None): ''' Plot the final WZ control region - with bkg. estimation ''' - sig_view = self.make_wz_cr_views(rebin) - + sig_view = self.make_wz_cr_views(rebin, project, project_axis) + stack = views.StackView( - sig_view['fakes'], - sig_view['wz'], + sig_view['wz_ztt'], sig_view['zz'], + sig_view['fakes'], + sig_view['wz_3l'] ) histo = stack.Get(variable) histo.Draw() @@ -827,20 +1047,24 @@ def plot_final_wz(self, variable, rebin=1, xaxis='', maxy=None): self.keep.append(histo) # Add legend - self.add_legend(histo, leftside=False, entries=4) + self.add_legend(histo, leftside=False, entries=len(histo)) def plot_final_f3(self, variable, rebin=1, xaxis='', maxy=None, show_error=True, qcd_correction=False, - qcd_weight_fraction=0.5, x_range=None, #): + qcd_weight_fraction=0., x_range=None, #): show_chi2=False,project=None, - project_axis=None, **kwargs): + project_axis=None, differential=False, + yaxis='Events', tau_charge='tau_os', show_ratio=False, + ratio_range=None, fit=None, **kwargs): ''' Plot the final F3 control region - with bkg. estimation ''' - + show_chi2 = False #broken sig_view = self.make_obj3_fail_cr_views( - qcd_correction, qcd_weight_fraction) + qcd_correction, qcd_weight_fraction, tau_charge) if project and project_axis: sig_view = self.apply_to_dict( sig_view, ProjectionView, project_axis, project ) sig_view = self.apply_to_dict( sig_view, RebinView, rebin ) #Rebin + if differential: + sig_view = self.apply_to_dict(sig_view, DifferentialView) charge_fakes_view = MedianView(highv=sig_view['charge_fakes']['sys_up'], centv=sig_view['charge_fakes']['central']) @@ -854,6 +1078,8 @@ def plot_final_f3(self, variable, rebin=1, xaxis='', maxy=None, histo = stack.Get(variable) histo.Draw() histo.GetHistogram().GetXaxis().SetTitle(xaxis) + histo.GetHistogram().GetYaxis().SetTitle(yaxis) + if x_range: histo.GetHistogram().GetXaxis().SetRangeUser(x_range[0], x_range[1]) @@ -861,38 +1087,40 @@ def plot_final_f3(self, variable, rebin=1, xaxis='', maxy=None, # Add legend legend = self.add_legend(histo, leftside=False, entries=4) - latex = ROOT.TLatex(0.01, 0.9, "") - pad = ROOT.TPad('da','fuq',0.1,0.8,0.5,0.9) - self.canvas.cd() - latexit = '' + #latex = ROOT.TLatex(0.01, 0.9, "") + #pad = ROOT.TPad('da','fuq',0.1,0.8,0.5,0.9) + #self.canvas.cd() + #latexit = '' + bkg_error = None if show_error: - correct_qcd_view = None - if qcd_weight_fraction == 0: - fakes05 = sig_view['weighted_fakes'][0.5] - correct_qcd_view = MedianView(highv=fakes05, centv=sig_view['fakes']) - - elif qcd_weight_fraction == 0.5: - fakes1 = sig_view['weighted_fakes'][1.] - correct_qcd_view = MedianView(highv=fakes1, centv=sig_view['fakes']) - - elif qcd_weight_fraction == 1: - fakes05 = sig_view['weighted_fakes'][0.5] - correct_qcd_view = MedianView(lowv=fakes05, centv=sig_view['fakes']) + #correct_qcd_view = None + #if qcd_weight_fraction == 0: + # fakes05 = sig_view['weighted_fakes'][1.] + # correct_qcd_view = MedianView(lowv=fakes05, centv=sig_view['fakes']) + # + #elif qcd_weight_fraction == 0.5: + # fakes1 = sig_view['weighted_fakes'][1.] + # correct_qcd_view = MedianView(highv=fakes1, centv=sig_view['fakes']) + # + #elif qcd_weight_fraction == 1: + # fakes05 = sig_view['weighted_fakes'][0.5] + # correct_qcd_view = MedianView(lowv=fakes05, centv=sig_view['fakes']) bkg_error_view = BackgroundErrorView( - correct_qcd_view, #sig_view['fakes'], + sig_view['fakes'], #correct_qcd_view, views.SumView(sig_view['wz'], sig_view['wz_3l']), sig_view['zz'], charge_fakes_view, + fake_error=0.3, **kwargs ) bkg_error = bkg_error_view.Get(variable) self.keep.append(bkg_error) bkg_error.Draw('pe2,same') legend.AddEntry(bkg_error) - if show_chi2: - chival = get_chi_square(data, bkg_error) - latexit = '#chi^{2}/#bins = %.2f / %i' % chival + #if show_chi2: + # chival = get_chi_square(data, bkg_error) + # latexit = '#chi^{2}/#bins = %.2f / %i' % chival data.Draw('same') if isinstance(maxy, (int, long, float)): @@ -903,15 +1131,30 @@ def plot_final_f3(self, variable, rebin=1, xaxis='', maxy=None, histo.SetMaximum(2 * max(data.GetMaximum(), histo.GetMaximum())) self.keep.append(data) self.keep.append(histo) - if latexit: - pad.cd() - latex.DrawLatex(0.01, 0.01, latexit) - self.canvas.cd() - pad.Draw() - self.keep.append(latex) - self.keep.append(pad) - #legend.AddEntry(data) legend.Draw() + if show_ratio: + ratio_plot = self.add_ratio_plot(data, bkg_error, x_range, ratio_range=ratio_range) + if fit: + #set_trace() + fitrange = fit.get('range', False) + if not fitrange: + nbins = ratio_plot.GetNbinsX() + fitrange = x_range if x_range else [ ratio_plot.GetBinLowEdge(1), + ratio_plot.GetBinLowEdge(nbins)+ratio_plot.GetBinWidth(nbins)] + self.lower_pad.cd() + function = self.fit_shape(ratio_plot, fit['model'], fitrange, fit.get('options','IRMENS')) + toprint = '#chi^{2} / DoF = %.2f / %i\n' % (function.GetChisquare() , function.GetNDF()) + for i in range(function.GetNpar()): + name = function.GetParName(i) + value = function.GetParameter(i) + error = function.GetParError(i) + toprint += '%s = %s\n' % (name, smart_float_format((value, error))) #%f #pm %f + + stat_box = self.make_text_box(toprint[:-1],fit.get('stat position','bottom-left')) + stat_box.Draw() + self.keep.append(stat_box) + #print toprint + self.pad.cd() def plot_final_f3_split(self, variable, rebin=1, xaxis='', maxy=None): ''' Plot the final F3 control region - with bkg. estimation ''' diff --git a/wh/WHPlotterEET.py b/wh/WHPlotterEET.py index 87a25111..6a37348d 100755 --- a/wh/WHPlotterEET.py +++ b/wh/WHPlotterEET.py @@ -12,7 +12,7 @@ import ROOT import sys import WHPlotterBase -from WHPlotterBase import make_styler, remove_name_entry, parser +from WHPlotterBase import make_styler, remove_name_entry, parser, project_x import rootpy.plotting.views as views from FinalStateAnalysis.MetaData.data_styles import data_styles, colors #from pudb import set_trace; set_trace() @@ -33,6 +33,14 @@ def __init__(self): super(WHPlotterEET, self).__init__('EET', obj1_charge_mapper, obj2_charge_mapper) +rebin_slim = range(20, 81, 10)+[100, 130, 300] +categories = { + 'LTCut' : [80, 650], + 'LTLow' : [0, 100], + 'LTHigh': [100, 650], + 'Full' : [0, 650], +} + if __name__ == "__main__": plotter = WHPlotterEET() sqrts = plotter.sqrts @@ -40,406 +48,420 @@ def __init__(self): options,NOTUSED = parser.parse_args() if not options.dry_run: - ########################################################################### - ## Zmm control plots ##################################################### - ########################################################################### - plotter.set_subdir('mc_data') - ## # Control Z->mumu + jet region - ## plotter.plot_mc_vs_data('os/p1p2f3', 'e1_e2_Mass', xaxis='m_{ee} (GeV)', xrange=(60, 120)) - ## plotter.add_cms_blurb(sqrts) - ## plotter.save('mcdata-os-p1p2f3-e1e2Mass') - - ## plotter.plot_mc_vs_data('os/p1p2f3/w3', 'e1_e2_Mass') - ## plotter.save('mcdata-os-p1p2f3-w3-e1e2Mass') - - ## plotter.plot_mc_vs_data('os/p1f2p3', 'e1_e2_Mass', xaxis='m_{ee} (GeV)', xrange=(60, 120)) - ## plotter.add_cms_blurb(sqrts) - ## plotter.save('mcdata-os-p1f2p3-e1e2Mass') - - ## # Check PU variables - ## plotter.plot_mc_vs_data('os/p1p2f3', 'rho') - ## plotter.save('mcdata-os-p1p2f3-rho') - - ## plotter.plot_mc_vs_data('os/p1p2f3', 'nvtx') - ## plotter.save('mcdata-os-p1p2f3-nvtx') - - ## # Lower stat but closer to signal region - ## plotter.plot_mc_vs_data('os/p1p2p3', 'rho') - ## plotter.save('mcdata-os-p1p2p3-rho') - - ## plotter.plot_mc_vs_data('os/p1p2p3', 'nvtx') - ## plotter.save('mcdata-os-p1p2p3-nvtx') - - ## # Make Z->mumu + tau jet control - - ## # - ## # Makes Tau fr control plot - ## # - ## zmm_weighted = plotter.plot('data', 'os/p1p2f3/w3/e1_e2_Mass', 'hist', styler=make_styler(2, 'hist'), xrange=(60, 120)) - ## zmm_weighted.SetTitle("Zee + fake #tau_{h} est.") - ## zmm_weighted.legendstyle='l' - ## zmm_weighted.GetXaxis().SetTitle("m_{ee} (GeV)") - - ## zmm_unweighted = plotter.plot('data', 'os/p1p2p3/e1_e2_Mass', 'same', styler=make_styler(1), xrange=(60, 120)) - ## zmm_unweighted.SetTitle("Zee observed") - ## zmm_unweighted.SetTitle("Zee + fake #tau_{h} obs.") - ## zmm_unweighted.legendstyle='pe' - - ## plotter.add_legend([zmm_weighted, zmm_unweighted]) - ## plotter.add_cms_blurb(sqrts) - ## plotter.save('zmm-os-fr-control') - - ## # - ## # Makes charge fr control plot - ## # - - ## zeet_os_weighted = plotter.plot('data', 'os/p1p2f3/c1/e1_e2_Mass', 'hist', styler=make_styler(2, 'hist'), xrange=(60, 120)) - ## zeet_os_weighted.SetTitle("Ze^{#pm}e^{#mp} + fake #tau_{h} charge flip est.") - ## zeet_os_weighted.legendstyle='l' - ## zeet_os_weighted.GetXaxis().SetTitle("M_{ee} (GeV)") - - ## zee_ss_unweighted = plotter.plot('data', 'ss/p1p2f3/e1_e2_Mass', 'same', styler=make_styler(1), xrange=(60, 120)) - ## zee_ss_unweighted.SetTitle("Ze^{#pm}e^{#pm} + fake #tau_{h} obs.") - ## zee_ss_unweighted.legendstyle='pe' - - ## plotter.add_legend([zeet_os_weighted, zee_ss_unweighted]) - ## plotter.add_cms_blurb(sqrts) - ## plotter.save('zee-os-ss-charge-flip-control') - - ## plotter.plot('Zjets_M50', 'os/p1p2f3/weight') - ## plotter.save('zee-mc-event-weights') - ## # Check MC weights - ## ## plotter.plot('Zjets_M50', 'os/p1p2f3/weight_nopu') - ## ## plotter.save('zee-mc-event-weight_nopu') - - - ## ########################################################################### - ## ## FR sideband MC-vs-Data ################################################ - ## ########################################################################### - - plotter.plot_mc_vs_data('ss/p1f2p3', 'e2_t_Mass', rebin=10, xaxis='m_{e2#tau} (GeV)', leftside=False) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1f2p3-subMass') - - plotter.plot_mc_vs_data('ss/p1f2p3', 'LT', rebin=10, xaxis='LT (GeV)', leftside=False) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1f2p3-LT') - - ########################################################################### - ## Signal region plots ################################################ - ########################################################################### - plotter.set_subdir('') - rebin_slim = [0,20]+range(30, 81, 10)+[100, 200] - - plotter.plot_final('e2_t_Mass#LT', rebin_slim, xaxis='m_{e_{2}#tau} (GeV)', maxy=None, project=[0, 130], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTLow') - - plotter.plot_final('e2_t_Mass#LT', rebin_slim, xaxis='m_{e_{2}#tau} (GeV)', maxy=None, project=[130, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTHigh') - - plotter.plot_final('e2_t_Mass#LT', rebin_slim, xaxis='m_{e_{2}#tau} (GeV)', maxy=None, project=[80, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTCut') - - plotter.plot_final('e2_t_Mass#LT', 20, xaxis='m_{e_{2}#tau} (GeV)', maxy=None, project=[80, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTCut-flatbin') - - plotter.plot_final('e1Pt', 10, maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.save('final-e1Pt') - - plotter.plot_final('e2Pt', 10, maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.save('final-e2Pt') - - plotter.plot_final('tPt', 10, maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.save('final-tPt') - - plotter.plot_final('e1AbsEta', 10, maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.save('final-e1AbsEta') - - plotter.plot_final('e2AbsEta', 10, maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.save('final-e2AbsEta') - - plotter.plot_final('tAbsEta', 10, maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.save('final-tAbsEta') - - plotter.plot_final('e2_t_Mass', 20, xaxis='m_{e_{2}#tau} (GeV)', maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass') - - plotter.plot_final('e1_t_Mass', 20, xaxis='m_{e_{1}#tau} (GeV)', maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.save('final-e1tMass') - - plotter.plot_final('e1_e2_Mass', 20, xaxis='m_{ee} (GeV)', maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.save('final-e1e2Mass') - - plotter.plot_final('tAbsEta', 5, xaxis='|#eta_#tau|', maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.save('final-tAbsEta') - - plotter.plot_final('e2RelPFIsoDB', 10, maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.save('final-e2Iso') - - ## plotter.plot_final('metSignificance', 5) - ## plotter.add_cms_blurb(sqrts) - ## plotter.save('final-metSig') - - plotter.plot_final('LT', 10, maxy='auto', xaxis='m_{e_{1}#tau} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-LT') - - plotter.plot_final('e2_t_Mass#LT', 20, xaxis='subleading mass from projection', maxy=None, project=[0, 600], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMassProj') - - plotter.plot_final('e2_t_Mass#LT', 20, xaxis='M_{e_{2}#tau} (GeV)', maxy=None, project=[0, 70], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTLo') - - plotter.plot_final('e2_t_Mass#LT', 20, xaxis='M_{e_{2}#tau} (GeV)', maxy=None, project=[70, 600], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTHi') - - ########################################################################### - ## f3 region plots ################################################ - ########################################################################### - categories = { - 'LTCut' : [80, 650], - 'LTLow' : [0, 130], - 'LTHigh': [130, 650], - } - - for label, proj_range in categories.iteritems(): - factor = 1.5 if label == 'LTHigh' else 1 - plotter.set_subdir('f3/%s' % label) - plotter.plot_final_f3('e2_t_Mass#LT', rebin_slim, xaxis='m_{e_{2}#tau} (GeV)', maxy=None, project=proj_range, project_axis='X') + if not options.no_mc_data: + ########################################################################### + ## Zee control plots ##################################################### + ########################################################################### + ## # Control Z->ee + jet region + plotter.set_subdir('mc_data/zee') + + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'e1_e2_Mass', rebin=10, + xaxis='m_{#mu#mu} (GeV)', leftside=False) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-%s' % label) + plotter.save('mcdata-Zee-e1e2Mass') - plotter.plot_final_f3('e2_t_Mass#LT', 200, xaxis='m_{e_{2}#tau} (GeV)', maxy=None, project=proj_range, project_axis='X') + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'e1Pt#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-%s-counting' % label, dotc=True, dotroot=True) + plotter.save('mcdata-Zee-e1Pt') - #pt - plotter.plot_final_f3("e1Pt#LT" , int(factor*10), xaxis='p_{Te_{1}} (GeV)', maxy=None, project=proj_range, project_axis='X') + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'e2Pt#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e1Pt-%s' % label) + plotter.save('mcdata-Zee-e2Pt') - plotter.plot_final_f3("e2Pt#LT" , int(factor*10), xaxis='p_{Te_{2}} (GeV)', maxy=None, project=proj_range, project_axis='X') + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'e1AbsEta#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e2Pt-%s' % label) + plotter.save('mcdata-Zee-e1AbsEta') - plotter.plot_final_f3("e1JetPt#LT" , int(factor*10), xaxis='p_{T Jet e_{1}} (GeV)', maxy=None, project=proj_range, project_axis='X') + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'e2AbsEta#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e1JetPt-%s' % label) + plotter.save('mcdata-Zee-e2AbsEta') - plotter.plot_final_f3("e2JetPt#LT" , int(factor*10), xaxis='p_{T Jet e_{2}} (GeV)', maxy=None, project=proj_range, project_axis='X') + # Check PU variables + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'rho') + plotter.save('mcdata-Zee-rho') + + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'nvtx') + plotter.save('mcdata-Zee-nvtx') + + if not options.no_wz: + ########################################################################### + ## WZ control plots ##################################################### + ########################################################################### + plotter.set_subdir('mc_data/wz_enhanced') + + plotter.plot_mc_vs_data('ss/tau_os/p1p2p3_enhance_wz', 'e2_t_Mass#LT', xaxis='m_{#mu#mu} (GeV)', + xrange=(0, 120), rebin=10, leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e2JetPt-%s' % label) - - #eta - plotter.plot_final_f3("e1AbsEta#LT", 10, xaxis='|#eta_{e_{1}}|', maxy=None, project=proj_range, project_axis='X') + plotter.save('mcdata-ss-p1p2p3-enhance_wz-subMass') + + plotter.plot_mc_vs_data('ss/tau_os/p1p2p3_enhance_wz', 'e1_t_Mass', xaxis='m_{#mu#mu} (GeV)', + xrange=(0, 120), rebin=10, leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e1AbsEta-%s' % label) + plotter.save('mcdata-ss-p1p2p3-enhance_wz-leadMass') + + plotter.set_subdir('WZ_enhanced') - plotter.plot_final_f3("e2AbsEta#LT", 10, xaxis='|#eta_{e_{2}}|' , maxy=None, project=proj_range, project_axis='X') + plotter.plot_final_wz('e1_t_Mass', 10, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)') plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e2AbsEta-%s' % label) + plotter.save('final-wz-leadMass') - #DR - plotter.plot_final_f3("e1_t_DR#LT", 10, xaxis='#DeltaR_{e_{1}#tau}', maxy=None, project=proj_range, project_axis='X') + plotter.plot_final_wz('e2_t_Mass#LT', 10, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', + project=[0,650], project_axis='X') plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e1_t_DR-%s' % label) + plotter.save('final-wz-subMass') - plotter.plot_final_f3("e2_t_DR#LT", 10, xaxis='#DeltaR_{e_{2}#tau}' , maxy=None, project=proj_range, project_axis='X') + plotter.plot_final_wz('e2Pt#LT', 5, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', + project=[0,650], project_axis='X') plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e2_t_DR-%s' % label) - - - - plotter.set_subdir('f3') - plotter.plot_final_f3('e2_t_Mass', 20, xaxis='m_{e_{2}#tau} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass') - - plotter.plot_final_f3('e2_t_Mass', 200, xaxis='m_{e_{2}#tau} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-counting-like', dotc=True, dotroot=True) - - plotter.plot_final_f3('e2_t_Mass', 20, xaxis='m_{e_{2}#tau} (GeV)', qcd_weight_fraction=0, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-wjetfake-subMass') - - plotter.plot_final_f3('e2_t_Mass', 20, xaxis='m_{e_{2}#tau} (GeV)', qcd_weight_fraction=1, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-qcdfake-subMass') - - plotter.plot_final_f3('e1_t_Mass', 20, xaxis='m_{e_{1}#tau} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e1tMass') - - plotter.plot_final_f3('e1_e2_Mass', 20, xaxis='m_{ee} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e1e2Mass') - - plotter.plot_final_f3('e1Pt', 10, qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e1Pt') - - plotter.plot_final_f3('e2Pt', 10, qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e2Pt') - - plotter.plot_final_f3('e2JetPt', 10, qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e2JetPt') - - plotter.plot_final_f3('tPt', 10, qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-tPt') - - plotter.plot_final_f3('e1AbsEta', 10, qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e1AbsEta') - - plotter.plot_final_f3('e2AbsEta', 10, qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e2AbsEta') - - plotter.plot_final_f3('tAbsEta', 10, qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-tAbsEta') - - plotter.plot_final_f3('LT', 5, qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-LT') - - plotter.plot_final_f3('e1_t_DR', 20, xaxis='#DeltaR_{e_{1}#tau}', x_range=[0,5]) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e1_t_DR') - - plotter.plot_final_f3('e2_t_DR', 20, xaxis='#DeltaR_{e_{2}#tau}', x_range=[0,5]) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e2_t_DR') - - plotter.plot_final_f3('e2_t_Mass#LT', 20, xaxis='subleading mass from projection', maxy=None, project=[0, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMassProj') - - plotter.plot_final_f3('e2_t_Mass#LT', 20, xaxis='M_{e_{2}#tau} (GeV)', maxy=None, project=[0, 70], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-LTLo') - - plotter.plot_final_f3('e2_t_Mass#LT', 20, xaxis='M_{e_{2}#tau} (GeV)', maxy=None, project=[70, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-LTHi') - - ########################################################################### - ## charge flip region plots ####################################### - ########################################################################### - plotter.set_subdir('charge_flip_CR_f3') - - plotter.plot_final_f3('charge_flip_CR/e2_t_Mass', 20, xaxis='m_{e_{2}#tau} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-chargeFlip-subMass') - - plotter.plot_final_f3('charge_flip_CR/e1_t_Mass', 20, xaxis='m_{e_{1}#tau} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-chargeFlip-e1tMass') - - plotter.plot_final_f3('charge_flip_CR/e1_e2_Mass', 5, xaxis='m_{ee} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-chargeFlip-e1e2Mass') - - #END OF if not options.dry_run: - ########################################################################### - ## Making shape file ################################################# - ########################################################################### - plotter.set_subdir('') - prefixes = [options.prefix+'$'] if options.prefix else [''] - prefixes = [i+'$' for i in options.prefixes.split(',') if i] if options.prefixes else prefixes - for prefix in prefixes: - shape_prefix = prefix if len(prefixes) > 1 else '' - shape_prefix = shape_prefix.replace(':','_').replace('$','_') - - plotter.plot_final_f3(prefix+'e2_t_Mass', 20, qcd_weight_fraction=0.5, xaxis='m_{e_{2}#tau} (GeV)', show_error=True, maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.canvas.SetGridx() - plotter.canvas.SetGridy() - plotter.save('final-%s-f3-subMass' % shape_prefix) - - plotter.plot_final(prefix+'e2_t_Mass', 20, qcd_weight_fraction=0.5, xaxis='m_{e_{2}#tau} (GeV)', maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.canvas.SetGridx() - plotter.canvas.SetGridy() - plotter.save('final-%s-subMass' % shape_prefix) - - plotter.plot_final(prefix+'LT', 10, qcd_weight_fraction=0.5, xaxis='m_{e_{2}#tau} (GeV)', maxy='auto') - plotter.add_cms_blurb(sqrts) - plotter.canvas.SetGridx() - plotter.canvas.SetGridy() - plotter.save('final-%s-LT' % shape_prefix) - - shape_file = ROOT.TFile( - os.path.join(plotter.outputdir, 'LTCut_eet_shapes_%s.root' % ( plotter.period) ), 'RECREATE') - shape_dir = shape_file.mkdir('eetCatHigh') - plotter.write_shapes(prefix+'e2_t_Mass#LT', 20, shape_dir, qcd_fraction=0.5, project=[80, 650], project_axis='X') - shape_dir = shape_file.mkdir('eetCatHigh_w') - plotter.write_shapes(prefix+'e2_t_Mass#LT', 20, shape_dir, qcd_fraction=0.0, project=[80, 650], project_axis='X') - shape_dir = shape_file.mkdir('eetCatHigh_q') - plotter.write_shapes(prefix+'e2_t_Mass#LT', 20, shape_dir, qcd_fraction=1.0, project=[80, 650], project_axis='X') - logging.warning('shape file %s created' % shape_file.GetName()) - shape_file.Close() - - - shape_file = ROOT.TFile( - os.path.join(plotter.outputdir, '%seet_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') - - shape_dir = shape_file.mkdir('eetCatLow') - plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0, 100], project_axis='X') - shape_dir = shape_file.mkdir('eetCatLow_w') - plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0, 100], project_axis='X') - shape_dir = shape_file.mkdir('eetCatLow_q') - plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0, 100], project_axis='X') + plotter.save('final-wz-e2Pt') - shape_dir = shape_file.mkdir('eetCatHigh') - plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[100, 650], project_axis='X') - shape_dir = shape_file.mkdir('eetCatHigh_w') - plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[100, 650], project_axis='X') - shape_dir = shape_file.mkdir('eetCatHigh_q') - plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[100, 650], project_axis='X') - - logging.warning('shape file %s created' % shape_file.GetName()) - shape_file.Close() - - - shape_file = ROOT.TFile( - os.path.join(plotter.outputdir, '%seet_f3_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') - - shape_dir = shape_file.mkdir('eetCatLow') - plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0., 130], project_axis='X') - shape_dir = shape_file.mkdir('eetCatLow_w') - plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') - shape_dir = shape_file.mkdir('eetCatLow_q') - plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') - - shape_dir = shape_file.mkdir('eetCatHigh') - plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[130, 650], project_axis='X') - shape_dir = shape_file.mkdir('eetCatHigh_w') - plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') - shape_dir = shape_file.mkdir('eetCatHigh_q') - plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + plotter.plot_final_wz('e2JetPt#LT', 5, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', + project=[0,650], project_axis='X') + plotter.add_cms_blurb(sqrts) + plotter.save('final-wz-e2JetPt') + + if not options.no_signal: + ########################################################################### + ## Signal region plots ################################################ + ########################################################################### + plotter.set_subdir('') + + for label, proj_range in categories.iteritems(): + factor = 1.5 if label == 'LTHigh' else 1 + for tau_charge in ['tau_os', 'tau_ss']: + if tau_charge == 'tau_os': + plotter.set_subdir('%s' % label) + else: + plotter.set_subdir('%s_charge3' % label) + + plotter.plot_final('e2_t_Mass#LT', rebin_slim, xaxis='m_{e_{2}#tau} (GeV)', + maxy=None, project=proj_range, project_axis='X', + differential=True, yaxis='Events / GeV', show_error=True, + tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-subMass-%s' % label) + + plotter.plot_final('e2_t_Mass#LT', rebin_slim, xaxis='m_{e_{2}#tau} (GeV)', + maxy=None, project=proj_range, project_axis='X', + differential=True, yaxis='Events / GeV', show_error=True, + tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.canvas.SetLogy(True) + plotter.save('final-subMass-logscale-%s' % label) + + plotter.plot_final('e2_t_Mass#LT', 300, xaxis='m_{e_{2}#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge, + show_error=True) + plotter.add_cms_blurb(sqrts) + plotter.save('final-subMass-%s-counting' % label, dotc=True, dotroot=True) + + #pt + plotter.plot_final("e1Pt#LT" , int(factor*10), xaxis='p_{Te_{1}} (GeV)', + maxy=None, project=proj_range, project_axis='X', + tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-e1Pt-%s' % label) + + plotter.plot_final("e2Pt#LT", int(factor*10), xaxis='p_{Te_{2}} (GeV)', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-e2Pt-%s' % label) + + plotter.plot_final("tPt#LT", int(factor*10), xaxis='p_{T#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-tPt-%s' % label) + + #plotter.plot_final("e1JetPt#LT", int(factor*10), xaxis='p_{T Jet e_{1}} (GeV)', + # maxy=None, project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-e1JetPt-%s' % label) + # + #plotter.plot_final("e2JetPt#LT", int(factor*10), xaxis='p_{T Jet e_{2}} (GeV)', + # maxy=None, project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-e2JetPt-%s' % label) + + #eta + plotter.plot_final("e1AbsEta#LT", 10, xaxis='|#eta_{e_{1}}|', maxy=None, project=proj_range, + project_axis='X', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-e1AbsEta-%s' % label) + + plotter.plot_final("e2AbsEta#LT", 10, xaxis='|#eta_{e_{2}}|', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-e2AbsEta-%s' % label) + + #DR + #plotter.plot_final("e1_t_DR#LT", 10, xaxis='#DeltaR_{e_{1}#tau}', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-e1_t_DR-%s' % label) + # + #plotter.plot_final("e2_t_DR#LT", 10, xaxis='#DeltaR_{e_{2}#tau}', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-e2_t_DR-%s' % label) + + #plotter.plot_final('e2RelPFIsoDB', 10, maxy='auto') + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-e2Iso') + + ## plotter.plot_final('metSignificance', 5) + ## plotter.add_cms_blurb(sqrts) + ## plotter.save('final-metSig') + + plotter.plot_final('LT', 10, maxy='auto', xaxis='m_{e_{1}#tau} (GeV)') + plotter.add_cms_blurb(sqrts) + plotter.save('final-LT') + + if not options.no_f3: + ########################################################################### + ## f3 region plots ################################################ + ########################################################################### + + for label, proj_range in categories.iteritems(): + factor = 1.5 if label == 'LTHigh' else 1 + for tau_charge in ['tau_os', 'tau_ss']: + if tau_charge == 'tau_os': + plotter.set_subdir('f3/%s' % label) + else: + plotter.set_subdir('f3/%s_charge3' % label) + + plotter.plot_final_f3('e2_t_Mass#LT', rebin_slim, xaxis='m_{e_{2}#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', differential=True, + yaxis='Events / GeV', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-subMass-%s' % label) + + plotter.plot_final_f3('e2_t_Mass#LT', 300, xaxis='m_{e_{2}#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-subMass-%s-counting' % label, dotc=True, dotroot=True) + + #pt + plotter.plot_final_f3("e1Pt#LT", int(factor*10), xaxis='p_{Te_{1}} (GeV)', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-e1Pt-%s' % label) + + plotter.plot_final_f3("e2Pt#LT", int(factor*10), xaxis='p_{Te_{2}} (GeV)', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-e2Pt-%s' % label) + + plotter.plot_final_f3("tPt#LT", int(factor*10), xaxis='p_{T#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-tPt-%s' % label) + + #plotter.plot_final_f3("e1JetPt#LT", int(factor*10), xaxis='p_{T Jet e_{1}} (GeV)', + # maxy=None, project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-e1JetPt-%s' % label) + # + #plotter.plot_final_f3("e2JetPt#LT" , int(factor*10), xaxis='p_{T Jet e_{2}} (GeV)', + # maxy=None, project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-e2JetPt-%s' % label) + + #eta + plotter.plot_final_f3("e1AbsEta#LT", 10, xaxis='|#eta_{e_{1}}|', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-e1AbsEta-%s' % label) + + plotter.plot_final_f3("e2AbsEta#LT", 10, xaxis='|#eta_{e_{2}}|', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-e2AbsEta-%s' % label) + + #DR + #plotter.plot_final_f3("e1_t_DR#LT", 10, xaxis='#DeltaR_{e_{1}#tau}', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-e1_t_DR-%s' % label) + # + #plotter.plot_final_f3("e2_t_DR#LT", 10, xaxis='#DeltaR_{e_{2}#tau}', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-e2_t_DR-%s' % label) + + + + plotter.set_subdir('f3') + + plotter.plot_final_f3('LT', 5, show_error=True) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-LT') + + ########################################################################### + ## charge flip region plots ####################################### + ########################################################################### + #plotter.set_subdir('charge_flip_CR_f3') + # + #plotter.plot_final_f3('charge_flip_CR/e2_t_Mass', 20, xaxis='m_{e_{2}#tau} (GeV)', show_error=True) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-chargeFlip-subMass') + # + #plotter.plot_final_f3('charge_flip_CR/e1_t_Mass', 20, xaxis='m_{e_{1}#tau} (GeV)', show_error=True) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-chargeFlip-e1tMass') + # + #plotter.plot_final_f3('charge_flip_CR/e1_e2_Mass', 5, xaxis='m_{ee} (GeV)', show_error=True) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-chargeFlip-e1e2Mass') - logging.warning('shape file %s created' % shape_file.GetName()) - shape_file.Close() + #END OF if not options.dry_run: + if not options.no_shapes: + ########################################################################### + ## Making shape file ################################################# + ########################################################################### + plotter.set_subdir('') + prefixes = [options.prefix+'$'] if options.prefix else [''] + prefixes = [i+'$' for i in options.prefixes.split(',') if i] if options.prefixes else prefixes + for prefix in prefixes: + shape_prefix = prefix if len(prefixes) > 1 else '' + shape_prefix = shape_prefix.replace(':','_').replace('$','_') + binning_7TeV = [0,40,80,120,200] + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, 'LTCut_eet_shapes_%s.root' % ( plotter.period) ), 'RECREATE') + shape_dir = shape_file.mkdir('eetCatHigh') + plotter.write_shapes(prefix+'e2_t_Mass#LT', binning_7TeV, shape_dir, qcd_fraction=0., project=[80, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatHigh_w') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', 20, shape_dir, qcd_fraction=0.0, project=[80, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatHigh_q') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', 20, shape_dir, qcd_fraction=1.0, project=[80, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, 'LTCut_eet_shapes_%s_different_fakes.root' % ( plotter.period) ), 'RECREATE') + shape_dir = shape_file.mkdir('eetCatHigh') + plotter.write_shapes(prefix+'e2_t_Mass#LT', binning_7TeV, shape_dir, qcd_fraction=0., project=[80, 650], project_axis='X', different_fakes=True) + #shape_dir = shape_file.mkdir('eetCatHigh_w') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', 20, shape_dir, qcd_fraction=0.0, project=[80, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatHigh_q') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', 20, shape_dir, qcd_fraction=1.0, project=[80, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, 'LTAll_eet_shapes_%s.root' % ( plotter.period) ), 'RECREATE') + shape_dir = shape_file.mkdir('eet') + plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[0, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eet_w') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eet_q') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, '%seet_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') + + shape_dir = shape_file.mkdir('eetCatLow') + plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[0, 100], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatLow_w') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0, 100], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatLow_q') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0, 100], project_axis='X') + + shape_dir = shape_file.mkdir('eetCatHigh') + plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[100, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatHigh_w') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[100, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatHigh_q') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[100, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, '%seet_shapes_%s_different_fakes.root' % (shape_prefix, plotter.period) ), 'RECREATE') + + shape_dir = shape_file.mkdir('eetCatLow') + plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[0, 100], project_axis='X', different_fakes=True) + #shape_dir = shape_file.mkdir('eetCatLow_w') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0, 100], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatLow_q') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0, 100], project_axis='X') + + shape_dir = shape_file.mkdir('eetCatHigh') + plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[100, 650], project_axis='X', different_fakes=True) + #shape_dir = shape_file.mkdir('eetCatHigh_w') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[100, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatHigh_q') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[100, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + + #shape_file = ROOT.TFile( + # os.path.join(plotter.outputdir, '%seet_f3_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') + # + #shape_dir = shape_file.mkdir('eetCatLow') + #plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0., 130], project_axis='X') + ##shape_dir = shape_file.mkdir('eetCatLow_w') + ##plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') + ##shape_dir = shape_file.mkdir('eetCatLow_q') + ##plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') + # + #shape_dir = shape_file.mkdir('eetCatHigh') + #plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[130, 650], project_axis='X') + ##shape_dir = shape_file.mkdir('eetCatHigh_w') + ##plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') + ##shape_dir = shape_file.mkdir('eetCatHigh_q') + ##plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + # + #logging.warning('shape file %s created' % shape_file.GetName()) + #shape_file.Close() + # + #shape_file = ROOT.TFile( + # os.path.join(plotter.outputdir, '%seet_f3_all_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') + # + #shape_dir = shape_file.mkdir('eetCatLow') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatLow_w') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatLow_q') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') + # + #shape_dir = shape_file.mkdir('eetCatHigh') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatHigh_w') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatHigh_q') + #plotter.write_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + # + #shape_dir = shape_file.mkdir('eetCatLowf3') + #plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatLowf3_w') + #plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatLowf3_q') + #plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') + # + #shape_dir = shape_file.mkdir('eetCatHighf3') + #plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatHighf3_w') + #plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('eetCatHighf3_q') + #plotter.write_f3_shapes(prefix+'e2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + # + #logging.warning('shape file %s created' % shape_file.GetName()) + #shape_file.Close() diff --git a/wh/WHPlotterEMT.py b/wh/WHPlotterEMT.py index 1d6787d3..f1e0b7c3 100644 --- a/wh/WHPlotterEMT.py +++ b/wh/WHPlotterEMT.py @@ -12,7 +12,7 @@ import ROOT import sys import WHPlotterBase -from WHPlotterBase import make_styler, parser +from WHPlotterBase import make_styler, parser, project_x import rootpy.plotting.views as views from FinalStateAnalysis.MetaData.data_styles import data_styles, colors @@ -28,455 +28,478 @@ def __init__(self): "subMass" : "subMass*" ,} super(WHPlotterEMT, self).__init__('EMT', {}, obj2_charge_mapper) +categories = { + 'LTCut' : [80, 650], + 'LTLow' : [0, 130], + 'LTHigh': [130, 650], + 'Full' : [0, 650], +} +rebin_slim = [20]+range(30, 81, 10)+[100,130,200] +binning_7TeV = [0,40,80,120,200] + if __name__ == "__main__": plotter = WHPlotterEMT() sqrts = plotter.sqrts plotter.defaults['show_charge_fakes'] = True options,NOTUSED = parser.parse_args() if not options.dry_run: - ########################################################################### - ## Zmm control plots ##################################################### - ########################################################################### - plotter.set_subdir('mc_data') - - # Control Z->tautau + jet region - plotter.plot_mc_vs_data('os/p1p2f3', 'e_m_Mass', rebin=10, xaxis='m_{e#mu} (GeV)', leftside=False) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-os-p1p2f3-emMass') - - plotter.plot_mc_vs_data('os/p1p2f3', 'nTruePU', rebin=1, xaxis='True PU') - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-os-p1p2f3-nTruePU') - - plotter.plot('Zjets_M50', 'os/p1p2f3/nTruePU', 'nTruePU', rebin=1, xaxis='True PU') - plotter.save('zjets-os-p1p2f3-nTruePU') - - - ## plotter.plot_mc_vs_data('os/p1p2f3', 'bCSVVeto', rebin=1, xaxis='bveto') - ## plotter.add_cms_blurb(sqrts) - ## plotter.save('mcdata-os-p1p2f3-bveto') - - plotter.plot_mc_vs_data('os/p1p2f3/w3', 'e_m_Mass', 10) - plotter.save('mcdata-os-p1p2f3-w3-emMass') - - plotter.plot_mc_vs_data('os/p1f2p3', 'e_m_Mass', 10) - plotter.save('mcdata-os-p1f2p3-emMass') - - plotter.plot_mc_vs_data('os/f1p2p3', 'e_m_Mass', 10) - plotter.save('mcdata-os-p1f2p3-emMass') - - # Check PU variables - #plotter.plot_mc_vs_data('os/p1p2f3', 'rho') - #plotter.save('mcdata-os-p1p2f3-rho') - - #plotter.plot_mc_vs_data('os/p1p2f3', 'nvtx') - #plotter.save('mcdata-os-p1p2f3-nvtx') - - # Lower stat but closer to signal region - #plotter.plot_mc_vs_data('os/p1p2p3', 'rho') - #plotter.save('mcdata-os-p1p2p3-rho') - - #plotter.plot_mc_vs_data('os/p1p2p3', 'nvtx') - #plotter.save('mcdata-os-p1p2p3-nvtx') - - # Make Z->mumu + tau jet control - - weighted = plotter.plot('data', 'os/p1p2f3/w3/e_m_Mass', 'hist', rebin=20, styler=make_styler(2, 'hist'), xaxis='m_{e#mu} (GeV)') - unweighted = plotter.plot('data', 'os/p1p2p3/e_m_Mass', 'same', rebin=20, styler=make_styler(1), xaxis='m_{e#mu} (GeV)') - weighted.SetTitle('e^{+}#mu^{-} + fake #tau_{h} est.') - weighted.legendstyle = 'l' - unweighted.SetTitle('e^{+}#mu^{-} + fake #tau_{h} obs.') - unweighted.legendstyle = 'pe' - plotter.add_legend([weighted, unweighted]) - plotter.add_cms_blurb(sqrts) - plotter.save('ztt-os-fr-control') - - #plotter.plot('data', 'os/p1p2p3/prescale', styler=make_styler(1)) - #plotter.save('ztt-os-prescale-check') - - #plotter.plot('Zjets_M50', 'os/p1p2f3/weight') - #plotter.save('ztt-mc-event-weights') - ## Check MC weights - #plotter.plot('Zjets_M50', 'os/p1p2f3/weight_nopu') - #plotter.save('ztt-mc-event-weight_nopu') - - - ########################################################################### - ## FR sideband MC-vs-Data ################################################ - ########################################################################### - - plotter.plot_mc_vs_data('ss/p1f2p3', 'mPt', 5, '#mu_{1} p_{T}', leftside=False) - plotter.save('mcdata-ss-p1f2p3-mPt') - - plotter.plot_mc_vs_data('ss/p1f2p3', 'subMass', 20, 'Subleading mass (GeV)', leftside=False) - plotter.save('mcdata-ss-p1f2p3-subMass') - - plotter.plot_mc_vs_data('ss/p1p2f3', 'subMass', 20, 'Subleading mass (GeV)', leftside=False) - plotter.save('mcdata-ss-p1p2f3-subMass') - - plotter.plot_mc_vs_data('ss/f1p2p3', 'subMass', 20, 'Subleading mass (GeV)', leftside=False) - plotter.save('mcdata-ss-f1p2p3-subMass') - - plotter.plot_mc_vs_data('ss/p1f2p3/w2', 'mPt', 5, '#mu_{1} p_{T}', leftside=False) - plotter.save('mcdata-ss-p1f2p3-w2-mPt') - - plotter.plot_mc_vs_data('ss/p1f2p3', 'ePt', 5, 'Electron p_{T}', leftside=False) - plotter.save('mcdata-ss-p1f2p3-ePt') - - plotter.plot_mc_vs_data('ss/p1f2p3/w2', 'ePt', 5, 'Electron p_{T}', leftside=False) - plotter.save('mcdata-ss-p1f2p3-w2-ePt') - - plotter.plot_mc_vs_data('ss/f1p2p3', 'ePt', 5, 'Electron p_{T}', leftside=False) - plotter.save('mcdata-ss-f1p2p3-ePt') - - plotter.plot_mc_vs_data('ss/f1p2p3/w1', 'ePt', 5, 'Electron p_{T}', leftside=False) - plotter.save('mcdata-ss-f1p2p3-w2-ePt') - - ########################################################################### - ## Signal region plots ################################################ - ########################################################################### - plotter.set_subdir('') - rebin_slim = [0,20]+range(30, 81, 10)+[100,130,200] - - plotter.plot_final('subMass#LT', rebin_slim, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, project=[0, 130], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTLow') - - plotter.plot_final('subMass#LT', rebin_slim, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, project=[130, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTHigh') - - plotter.plot_final('subMass#LT', rebin_slim, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, project=[80, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTCut') - - plotter.plot_final('subMass#LT', 20, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, project=[80, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTCut-flatbin') - - plotter.plot_final('LT', 5, xaxis='LT (GeV)', maxy=15) - plotter.add_cms_blurb(sqrts) - plotter.save('final-LT') - - plotter.plot_final('mPt', 10) - plotter.save('final-mPt') - - plotter.plot_final('ePt', 10) - plotter.save('final-ePt') - - plotter.plot_final('tPt', 10) - plotter.save('final-tPt') - - plotter.plot_final('mAbsEta', 10) - plotter.save('final-mAbsEta') - - plotter.plot_final('eAbsEta', 10) - plotter.save('final-eAbsEta') - - plotter.plot_final('tAbsEta', 10) - plotter.save('final-tAbsEta') - - plotter.plot_final('subMass', 20, xaxis='m_{l_{2}#tau} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass') - - plotter.plot_final('subMass', 20, xaxis='m_{l_{2}#tau} (GeV)', qcd_weight_fraction=0.5) - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-qweight05') - plotter.canvas.SetLogy(True) - plotter.save('final-subMass-qweight05-logscale') - - - plotter.plot_final('subMass', 20, xaxis='m_{l_{2}#tau} (GeV)', show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-werror') - - # Shape only - plotter.plot_final('subMass', 20, xaxis='m_{l_{2}#tau} (GeV)', show_error=True, - fake_error=0, wz_error=0, zz_error=0) - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-wshapeerror') - - ## plotter.plot_final('metSig', 5) - ## plotter.save('final-metSig') - plotter.plot_final('tLeadDR', 10) - plotter.save('final-tLeadDR') - plotter.plot_final('tSubDR', 10) - plotter.save('final-tSubDR') - - plotter.plot_final('e_t_Mass', 10) - plotter.save('final-etMass') - - plotter.plot_final('subMass#LT', 20, xaxis='subleading mass from projection', maxy=None, project=[0, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMassProj') - - plotter.plot_final('subMass#LT', 20, xaxis='M_{l_{2}#tau} (GeV)', maxy=None, project=[0, 90], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTLo') - - plotter.plot_final('subMass#LT', 20, xaxis='M_{l_{2}#tau} (GeV)', maxy=None, project=[90, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTHi') - - ########################################################################### - ## WZ enhanced region plots ########################################### - ########################################################################### - plotter.set_subdir('WZ_enhanced') - - plotter.plot_final_wz('e_t_Mass', 10, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-wz-etMass') - - plotter.plot_final_wz('mPt', 5, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', maxy=20) - plotter.add_cms_blurb(sqrts) - plotter.save('final-wz-mPt') - - #plotter.plot_final_wz('mJetPt', 5, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)') - #plotter.add_cms_blurb(sqrts) - #plotter.save('final-wz-mJetPt') - - ########################################################################### - ## F3 enhanced region plots ########################################### - ########################################################################### - plotter.set_subdir('f3') - - categories = { - 'LTCut' : [80, 650], - 'LTLow' : [0, 130], - 'LTHigh': [130, 650], - } - - for label, proj_range in categories.iteritems(): - plotter.set_subdir('f3/%s' % label) - plotter.plot_final_f3('subMass#LT', rebin_slim, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, project=proj_range, project_axis='X') + if not options.no_mc_data: + ########################################################################### + ## Ztt control plots ##################################################### + ########################################################################### + plotter.set_subdir('mc_data/em') + + # Control Z->tautau + jet region + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'e_m_Mass#LT', rebin=10, xaxis='m_{e#mu} (GeV)', + leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-%s' % label) + plotter.save('mcdata-em-emMass') - plotter.plot_final_f3('subMass#LT', 200, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-%s-counting' % label, dotc=True, dotroot=True) - #pt - plotter.plot_final_f3("mPt#LT" , 10, xaxis='p_{T#mu} (GeV)', maxy=None, project=proj_range, project_axis='X') + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'mPt#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-mPt-%s' % label) + plotter.save('mcdata-em-mPt') - plotter.plot_final_f3("ePt#LT" , 10, xaxis='p_{Te} (GeV)', maxy=None, project=proj_range, project_axis='X') + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'ePt#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-ePt-%s' % label) + plotter.save('mcdata-em-ePt') - plotter.plot_final_f3("mJetPt#LT" , 10, xaxis='p_{T Jet#mu} (GeV)', maxy=None, project=proj_range, project_axis='X') + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'mAbsEta#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-mJetPt-%s' % label) + plotter.save('mcdata-em-mAbsEta') - plotter.plot_final_f3("eJetPt#LT" , 10, xaxis='p_{T Jet e} (GeV)', maxy=None, project=proj_range, project_axis='X') + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'eAbsEta#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-eJetPt-%s' % label) + plotter.save('mcdata-em-eAbsEta') - plotter.plot_final_f3("tPt#LT" , 10, xaxis='p_{T#tau} (GeV)', maxy=None, project=proj_range, project_axis='X') + if not options.no_wz: + ########################################################################### + ## WZ control plots ##################################################### + ########################################################################### + plotter.set_subdir('mc_data/wz_enhanced') + + plotter.plot_mc_vs_data('ss/tau_os/p1p2p3_enhance_wz', 'e_t_Mass#LT', xaxis='m_{e#tau} (GeV)', + xrange=(0, 120), rebin=10, leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-tPt-%s' % label) - - #eta - plotter.plot_final_f3("mAbsEta#LT", 10, xaxis='|#eta_{#mu}|', maxy=None, project=proj_range, project_axis='X') + plotter.save('mcdata-ss-p1p2p3-enhance_wz-e_t_Mass') + + plotter.plot_mc_vs_data('ss/tau_os/p1p2p3_enhance_wz', 'm_t_Mass#LT', xaxis='m_{#mu#tau} (GeV)', + xrange=(0, 120), rebin=10, leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-mAbsEta-%s' % label) + plotter.save('mcdata-ss-p1p2p3-enhance_wz-leadMass') + + plotter.set_subdir('WZ_enhanced') - plotter.plot_final_f3("eAbsEta#LT", 10, xaxis='|#eta_{e}|' , maxy=None, project=proj_range, project_axis='X') + plotter.plot_final_wz('e_t_Mass#LT', 10, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', + project=[0,650], project_axis='X') plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-eAbsEta-%s' % label) + plotter.save('final-wz-e_t_Mass') - plotter.plot_final_f3("tAbsEta#LT", 10, xaxis='|#eta_{#tau}|' , maxy=None, project=proj_range, project_axis='X') + plotter.plot_final_wz('m_t_Mass#LT', 10, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', + project=[0,650], project_axis='X') plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-tAbsEta-%s' % label) + plotter.save('final-wz-m_t_Mass') - #DR - plotter.plot_final_f3("m_t_DR#LT", 10, xaxis='#DeltaR_{#mu#tau}', maxy=None, project=proj_range, project_axis='X') + plotter.plot_final_wz('ePt#LT', 5, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', + project=[0,650], project_axis='X') plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m_t_DR-%s' % label) - - plotter.plot_final_f3("e_t_DR#LT", 10, xaxis='#DeltaR_{e#tau}' , maxy=None, project=proj_range, project_axis='X') + plotter.save('final-wz-ePt') + + plotter.plot_final_wz('eJetPt#LT', 5, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', + project=[0,650], project_axis='X') plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-e_t_DR-%s' % label) - - #Jet BTag - plotter.plot_final_f3("eJetBtag#LT", 2, xaxis='e Jet Btag' , maxy=None, project=proj_range, project_axis='X') + plotter.save('final-wz-eJetPt') + + if not options.no_signal: + ########################################################################### + ## Signal region plots ################################################ + ########################################################################### + plotter.set_subdir('') + + for label, proj_range in categories.iteritems(): + for tau_charge in ['tau_os', 'tau_ss']: + if tau_charge == 'tau_os': + plotter.set_subdir('%s' % label) + else: + plotter.set_subdir('%s_charge3' % label) + + plotter.plot_final('subMass#LT', rebin_slim, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', differential=True, + yaxis='Events / GeV', show_error=True, tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-subMass-%s' % label) + + plotter.plot_final('subMass#LT', binning_7TeV, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', differential=True, + yaxis='Events / GeV', show_error=True, tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-subMass-7TeVBin-%s' % label) + + + #plotter.plot_final("e_m_Mass#LT", 20, xaxis='m_{e#mu} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-e_m_Mass-%s' % label) + # + #plotter.plot_final("m_t_Mass#LT", 20, xaxis='m_{#mu#tau} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m_t_Mass-%s' % label) + # + #plotter.plot_final("e_t_Mass#LT", 20, xaxis='m_{e#tau} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-e_t_Mass-%s' % label) + + plotter.plot_final('subMass#LT', 300, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge, + show_error=True) + plotter.add_cms_blurb(sqrts) + plotter.save('final-subMass-%s-counting' % label, dotc=True, dotroot=True) + + #pt + #plotter.plot_final("mPt#LT" , 10, xaxis='p_{T#mu} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-mPt-%s' % label) + # + #plotter.plot_final("ePt#LT" , 10, xaxis='p_{Te} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-ePt-%s' % label) + # + #plotter.plot_final("tPt#LT" , 10, xaxis='p_{T#tau} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-tPt-%s' % label) + + #plotter.plot_final("mJetPt#LT" , 10, xaxis='p_{T Jet#mu} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-mJetPt-%s' % label) + # + #plotter.plot_final("eJetPt#LT" , 10, xaxis='p_{T Jet e} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-eJetPt-%s' % label) + + #eta + #plotter.plot_final("mAbsEta#LT", 10, xaxis='|#eta_{#mu}|', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-mAbsEta-%s' % label) + # + #plotter.plot_final("eAbsEta#LT", 10, xaxis='|#eta_{e}|' , maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-eAbsEta-%s' % label) + # + #plotter.plot_final("tAbsEta#LT", 10, xaxis='|#eta_{#tau}|' , maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-tAbsEta-%s' % label) + + #DR + #plotter.plot_final("m_t_DR#LT", 10, xaxis='#DeltaR_{#mu#tau}', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m_t_DR-%s' % label) + # + #plotter.plot_final("e_t_DR#LT", 10, xaxis='#DeltaR_{e#tau}' , maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-e_t_DR-%s' % label) + + #Jet BTag + #plotter.plot_final("eJetBtag#LT", 2, xaxis='e Jet Btag' , maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-eJetBtag-%s' % label) + # + #plotter.plot_final("mJetBtag#LT", 2, xaxis='#mu Jet Btag' , maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-mJetBtag-%s' % label) + + + plotter.plot_final('LT', 5, xaxis='LT (GeV)', maxy=15) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-eJetBtag-%s' % label) - - plotter.plot_final_f3("mJetBtag#LT", 2, xaxis='#mu Jet Btag' , maxy=None, project=proj_range, project_axis='X') + plotter.save('final-LT') + + if not options.no_f3: + ########################################################################### + ## F3 enhanced region plots ########################################### + ########################################################################### + plotter.set_subdir('f3') + + for label, proj_range in categories.iteritems(): + for tau_charge in ['tau_os', 'tau_ss']: + if tau_charge == 'tau_os': + plotter.set_subdir('f3/%s' % label) + else: + plotter.set_subdir('f3/%s_charge3' % label) + + plotter.plot_final_f3('subMass#LT', rebin_slim, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', differential=True, + yaxis='Events / GeV', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-subMass-%s' % label) + + plotter.plot_final_f3('subMass#LT', binning_7TeV, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', differential=True, + yaxis='Events / GeV', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-subMass-7TeVBin-%s' % label) + + #plotter.plot_final_f3('subMass#LT', [20, 40, 120, 300], xaxis='m_{l_{2}#tau} (GeV)', maxy=None, + # project=proj_range, project_axis='X', differential=True, + # yaxis='Events / GeV', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-subMass-widebin-%s' % label) + # + #plotter.plot_final_f3('subMass#LT', rebin_slim, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-subMass-%s-notDifferential' % label) + + #plotter.plot_final_f3("e_m_Mass#LT", 20, xaxis='m_{e#mu} (GeV)', maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-e_m_Mass-%s' % label) + # + #plotter.plot_final_f3("m_t_Mass#LT", 20, xaxis='m_{#mu#tau} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m_t_Mass-%s' % label) + # + #plotter.plot_final_f3("e_t_Mass#LT", 20, xaxis='m_{e#tau} (GeV)' , maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-e_t_Mass-%s' % label) + + plotter.plot_final_f3('subMass#LT', 300, xaxis='m_{l_{2}#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-subMass-%s-counting' % label, dotc=True, dotroot=True) + + #pt + #plotter.plot_final_f3("mPt#LT" , 10, xaxis='p_{T#mu} (GeV)', maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-mPt-%s' % label) + # + #plotter.plot_final_f3("ePt#LT" , 10, xaxis='p_{Te} (GeV)', maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-ePt-%s' % label) + # + #plotter.plot_final_f3("tPt#LT" , 10, xaxis='p_{T#tau} (GeV)', maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-tPt-%s' % label) + + #plotter.plot_final_f3("mJetPt#LT" , 10, xaxis='p_{T Jet#mu} (GeV)', maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-mJetPt-%s' % label) + # + #plotter.plot_final_f3("eJetPt#LT" , 10, xaxis='p_{T Jet e} (GeV)', maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-eJetPt-%s' % label) + + #eta + #plotter.plot_final_f3("mAbsEta#LT", 10, xaxis='|#eta_{#mu}|', maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-mAbsEta-%s' % label) + # + #plotter.plot_final_f3("eAbsEta#LT", 10, xaxis='|#eta_{e}|' , maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-eAbsEta-%s' % label) + # + #plotter.plot_final_f3("tAbsEta#LT", 10, xaxis='|#eta_{#tau}|' , maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-tAbsEta-%s' % label) + + #DR + #plotter.plot_final_f3("m_t_DR#LT", 10, xaxis='#DeltaR_{#mu#tau}', maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-m_t_DR-%s' % label) + # + #plotter.plot_final_f3("e_t_DR#LT", 10, xaxis='#DeltaR_{e#tau}' , maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-e_t_DR-%s' % label) + + #Jet BTag + #plotter.plot_final_f3("eJetBtag#LT", 2, xaxis='e Jet Btag' , maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-eJetBtag-%s' % label) + # + #plotter.plot_final_f3("mJetBtag#LT", 2, xaxis='#mu Jet Btag' , maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-mJetBtag-%s' % label) + + plotter.set_subdir('f3') + plotter.plot_final_f3('LT', 5, xaxis='LT (GeV)', show_error=True) plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-mJetBtag-%s' % label) - - plotter.set_subdir('f3') - plotter.plot_final_f3('LT', 5, xaxis='LT (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-LT') - - plotter.plot_final_f3('subMass', 20, xaxis='m_{l_{2}#tau_{#mu}} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass') - - plotter.plot_final_f3('subMass', 200, xaxis='m_{l_{2}#tau_{#mu}} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-counting-like', dotc=True, dotroot=True) - - plotter.plot_final_f3('subMass', 20, xaxis='m_{l_{2}#tau_{#mu}} (GeV)', qcd_weight_fraction=1, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-qcdfake-subMass') - - plotter.plot_final_f3('subMass', 20, xaxis='m_{l_{2}#tau_{#mu}} (GeV)', qcd_weight_fraction=0, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-wjetfake-subMass') - - plotter.plot_final_f3("eJetBtag#LT", 2, xaxis='e Jet Btag' , maxy=None, project=[0, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-eJetBtag') - - plotter.plot_final_f3("mJetBtag#LT", 2, xaxis='#mu Jet Btag' , maxy=None, project=[0, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-mJetBtag') - - - plotter.plot_final_f3('e_t_Mass', 20, xaxis='m_{e#tau_{#mu}} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-etMass') - - plotter.plot_final_f3('e_m_Mass', 20, xaxis='m_{e#mu} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-emMass') - - #plotter.plot_final_f3('m_t_Mass', 20, xaxis='m_{#mu#tau} (GeV)', qcd_weight_fraction=0.5, show_error=True) - #plotter.add_cms_blurb(sqrts) - #plotter.save('final-f3-emMass') - - plotter.plot_final_f3('mPt', 10, xaxis='p_{T#mu} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-mPt') - - plotter.plot_final_f3('ePt', 10, xaxis='p_{Te} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-ePt') - - plotter.plot_final_f3('tPt', 10, xaxis='p_{T#tau} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-tPt') - - plotter.plot_final_f3('tSubDR', 10, xaxis='#DeltaR_{l_{2}#tau}', show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-tSubDR') - - plotter.plot_final_f3('tLeadDR', 10, xaxis='#DeltaR_{l_{1}#tau}', show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-tLeadDR') - - plotter.plot_final_f3('e_t_DR', 10, xaxis='#DeltaR_{e#tau}', show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-etDR') - - plotter.plot_final_f3('m_t_DR', 10, xaxis='#DeltaR_{#mu#tau}', show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-mtDR') - - plotter.plot_final_f3('subPt', 10, xaxis='p_{Tl_{2}} (GeV)', show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subPt') - - plotter.plot_final_f3('subJetPt', 10, xaxis='p_{TJetl_{2}} (GeV)', show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subJetPt') - - plotter.plot_final_f3('leadPt', 10, xaxis='p_{Tl_{1}} (GeV)', show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-leadPt') - - plotter.plot_final_f3('eChargeIdTight', 1, xaxis='Charge ID Tight', maxy=None) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-eChargeIdTight') - - plotter.plot_final_f3('subMass#LT', 20, xaxis='subleading mass from projection', maxy=None, project=[0, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMassProj') - - plotter.plot_final_f3('subMass#LT', 20, xaxis='M_{l_{2}#tau} (GeV)', maxy=None, project=[0, 90], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-LTLo') - - plotter.plot_final_f3('subMass#LT', 20, xaxis='M_{l_{2}#tau} (GeV)', maxy=None, project=[90, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-LTHi') + plotter.save('final-f3-LT') #END OF if not options.dry_run: - ########################################################################### - ## Making shape file ################################################# - ########################################################################### - plotter.set_subdir('') - prefixes = [options.prefix+'$'] if options.prefix else [''] - prefixes = [i+'$' for i in options.prefixes.split(',') if i] if options.prefixes else prefixes - for prefix in prefixes: - shape_prefix = prefix if len(prefixes) > 1 else '' - shape_prefix = shape_prefix.replace(':','_').replace('$','_') - - plotter.plot_final(prefix+'LT', 5, xaxis='LT (GeV)', qcd_weight_fraction=0.5) - plotter.add_cms_blurb(sqrts) - plotter.save('study-%s-LT-qweight05' % shape_prefix) - - plotter.plot_final(prefix+'subMass', 20, xaxis='m_{#l_{2}#tau} (GeV)', qcd_weight_fraction=0.5) - plotter.add_cms_blurb(sqrts) - plotter.save('study-%s-subMass-qweight05' % shape_prefix) - - plotter.plot_final_f3(prefix+'subMass', 20, xaxis='m_{l_{1}#tau_{#mu}} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('study-%s-f3-qweight05-werror-subMass' % shape_prefix) - - shape_file = ROOT.TFile( - os.path.join(plotter.outputdir, 'LTCut_emt_shapes_%s.root' % ( plotter.period) ), 'RECREATE') - shape_dir = shape_file.mkdir('emtCatHigh') - plotter.write_shapes(prefix+'subMass#LT', 20, shape_dir, qcd_fraction=0.5, project=[80, 650], project_axis='X') - shape_dir = shape_file.mkdir('emtCatHigh_w') - plotter.write_shapes(prefix+'subMass#LT', 20, shape_dir, qcd_fraction=0.0, project=[80, 650], project_axis='X') - shape_dir = shape_file.mkdir('emtCatHigh_q') - plotter.write_shapes(prefix+'subMass#LT', 20, shape_dir, qcd_fraction=1.0, project=[80, 650], project_axis='X') - shape_file.Close() - - shape_file = ROOT.TFile( - os.path.join(plotter.outputdir, '%semt_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') - - shape_dir = shape_file.mkdir('emtCatLow') - plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0, 130], project_axis='X') - shape_dir = shape_file.mkdir('emtCatLow_w') - plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0, 130], project_axis='X') - shape_dir = shape_file.mkdir('emtCatLow_q') - plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0, 130], project_axis='X') - - shape_dir = shape_file.mkdir('emtCatHigh') - plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[130, 650], project_axis='X') - shape_dir = shape_file.mkdir('emtCatHigh_w') - plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') - shape_dir = shape_file.mkdir('emtCatHigh_q') - plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') - - logging.warning('shape file %s created' % shape_file.GetName()) - shape_file.Close() - - shape_file = ROOT.TFile( - os.path.join(plotter.outputdir, '%semt_f3_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') - - shape_dir = shape_file.mkdir('emtCatLow') - plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0., 130], project_axis='X') - shape_dir = shape_file.mkdir('emtCatLow_w') - plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') - shape_dir = shape_file.mkdir('emtCatLow_q') - plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') - - shape_dir = shape_file.mkdir('emtCatHigh') - plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[130, 650], project_axis='X') - shape_dir = shape_file.mkdir('emtCatHigh_w') - plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') - shape_dir = shape_file.mkdir('emtCatHigh_q') - plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') - - logging.warning('shape file %s created' % shape_file.GetName()) - shape_file.Close() + if not options.no_shapes: + ########################################################################### + ## Making shape file ################################################# + ########################################################################### + plotter.set_subdir('') + prefixes = [options.prefix+'$'] if options.prefix else [''] + prefixes = [i+'$' for i in options.prefixes.split(',') if i] if options.prefixes else prefixes + for prefix in prefixes: + shape_prefix = prefix if len(prefixes) > 1 else '' + shape_prefix = shape_prefix.replace(':','_').replace('$','_') + binning_7TeV = [0,40,80,120,200] + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, 'LTAll_emt_shapes_%s.root' % ( plotter.period) ), 'RECREATE') + shape_dir = shape_file.mkdir('emt') + plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[0, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emt_w') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emt_q') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, 'LTCut_emt_shapes_%s.root' % ( plotter.period) ), 'RECREATE') + shape_dir = shape_file.mkdir('emt') + plotter.write_shapes(prefix+'subMass#LT', binning_7TeV, shape_dir, qcd_fraction=0., project=[80, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHigh_w') + #plotter.write_shapes(prefix+'subMass#LT', 20, shape_dir, qcd_fraction=0.0, project=[80, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHigh_q') + #plotter.write_shapes(prefix+'subMass#LT', 20, shape_dir, qcd_fraction=1.0, project=[80, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, 'LTCut_emt_shapes_%s_different_fakes.root' % ( plotter.period) ), 'RECREATE') + shape_dir = shape_file.mkdir('emt') + plotter.write_shapes(prefix+'subMass#LT', binning_7TeV, shape_dir, qcd_fraction=0., project=[80, 650], project_axis='X', different_fakes=True) + #shape_dir = shape_file.mkdir('emtCatHigh_w') + #plotter.write_shapes(prefix+'subMass#LT', 20, shape_dir, qcd_fraction=0.0, project=[80, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHigh_q') + #plotter.write_shapes(prefix+'subMass#LT', 20, shape_dir, qcd_fraction=1.0, project=[80, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, '%semt_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') + + shape_dir = shape_file.mkdir('emtCatLow') + plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[0, 130], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatLow_w') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0, 130], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatLow_q') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0, 130], project_axis='X') + + shape_dir = shape_file.mkdir('emtCatHigh') + plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHigh_w') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHigh_q') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, '%semt_shapes_%s_different_fakes.root' % (shape_prefix, plotter.period) ), 'RECREATE') + + shape_dir = shape_file.mkdir('emtCatLow') + plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[0, 130], project_axis='X', different_fakes=True) + #shape_dir = shape_file.mkdir('emtCatLow_w') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0, 130], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatLow_q') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0, 130], project_axis='X') + + shape_dir = shape_file.mkdir('emtCatHigh') + plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[130, 650], project_axis='X', different_fakes=True) + #shape_dir = shape_file.mkdir('emtCatHigh_w') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHigh_q') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + + #shape_file = ROOT.TFile( + # os.path.join(plotter.outputdir, '%semt_f3_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') + # + #shape_dir = shape_file.mkdir('emtCatLow') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatLow_w') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatLow_q') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') + # + #shape_dir = shape_file.mkdir('emtCatHigh') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHigh_w') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHigh_q') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + # + #logging.warning('shape file %s created' % shape_file.GetName()) + #shape_file.Close() + # + #shape_file = ROOT.TFile( + # os.path.join(plotter.outputdir, '%semt_f3_all_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') + # + #shape_dir = shape_file.mkdir('emtCatLow') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatLow_w') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatLow_q') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') + # + #shape_dir = shape_file.mkdir('emtCatHigh') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHigh_w') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHigh_q') + #plotter.write_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + # + #shape_dir = shape_file.mkdir('emtCatLowf3') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatLowf3_w') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatLowf3_q') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') + # + #shape_dir = shape_file.mkdir('emtCatHighf3') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHighf3_w') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('emtCatHighf3_q') + #plotter.write_f3_shapes(prefix+'subMass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + # + #logging.warning('shape file %s created' % shape_file.GetName()) + #shape_file.Close() diff --git a/wh/WHPlotterMMT.py b/wh/WHPlotterMMT.py index da48fe38..571a7d9d 100644 --- a/wh/WHPlotterMMT.py +++ b/wh/WHPlotterMMT.py @@ -12,575 +12,531 @@ import ROOT import sys import WHPlotterBase -from WHPlotterBase import make_styler, parser +from WHPlotterBase import make_styler, parser, project_x import rootpy.plotting.views as views from FinalStateAnalysis.MetaData.data_styles import data_styles, colors logging.basicConfig(stream=sys.stderr, level=logging.INFO) +ROOT.gStyle.SetOptTitle(0) +#ROOT.gROOT.SetStyle('Plain') class WHPlotterMMT(WHPlotterBase.WHPlotterBase): def __init__(self): super(WHPlotterMMT, self).__init__('MMT') +rebin_slim = range(20, 81, 10)+[100, 130, 200] +binning_7TeV = [0,40,80,120,200] +categories = { + 'LTCut' : [80, 650], + 'Full' : [0, 650], + 'LTLow' : [0, 130], + 'LTHigh': [130, 650], +} + + + if __name__ == "__main__": plotter = WHPlotterMMT() sqrts = plotter.sqrts options,NOTUSED = parser.parse_args() if not options.dry_run: - ########################################################################### - ## Zmm control plots ##################################################### - ########################################################################### - plotter.set_subdir('mc_data') - - # Control Z->mumu + jet region - plotter.plot_mc_vs_data('os/p1p2f3', 'm1_m2_Mass', xaxis='m_{#mu#mu} (GeV)', xrange=(60, 120)) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-os-p1p2f3-m1m2Mass') - - plotter.plot_mc_vs_data('os/p1p2p3', 'm1_m2_Mass', xaxis='m_{#mu#mu} (GeV)', xrange=(60, 120)) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-os-p1p2p3-m1m2Mass') - - plotter.plot_mc_vs_data('ss/p1p2p3_enhance_wz', 'm2_t_Mass', xaxis='m_{#mu#mu} (GeV)', xrange=(0, 120), rebin=10) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1p2p3-enhance_wz-subMass') - - plotter.plot_mc_vs_data('ss/p1p2p3_enhance_wz', 'm1_t_Mass', xaxis='m_{#mu#mu} (GeV)', xrange=(0, 120), rebin=10) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1p2p3-enhance_wz-leadMass') - - plotter.plot_mc_vs_data('ss/p1f2p3_enhance_wz', 'm1_t_Mass', xaxis='m_{#mu#mu} (GeV)', xrange=(0, 120), rebin=10) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1f2p3-enhance_wz-leadMass') - - plotter.plot_mc_vs_data('ss/p1f2p3_enhance_wz/w2', 'm1_t_Mass', xaxis='m_{#mu#mu} (GeV)', xrange=(0, 120), rebin=10) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1f2p3-w2-enhance_wz-leadMass') - - plotter.plot_mc_vs_data('ss/p1p2p3_enhance_wz', 'subMTMass', xaxis='m_{#mu#mu} (GeV)', xrange=(0, 120), rebin=10) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1p2p3-enhance_wz-subMTMass') - - plotter.plot_mc_vs_data('ss/p1p2p3_enhance_wz', 'm2Pt', xaxis='m_{#mu#mu} (GeV)', xrange=(0, 120), rebin=5) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1p2p3-enhance_wz-m2Pt') - - plotter.compare_shapes('Zjets_M50', 'data', 'os/p1p2f3/nvtx') - plotter.save('z-vs-data-nvtx-shape') - plotter.compare_shapes('Zjets_M50', 'data', 'os/p1p2f3/rho') - plotter.save('z-vs-data-rho-shape') - - plotter.plot_mc_vs_data('os/p1p2f3/w3', 'm1_m2_Mass') - plotter.save('mcdata-os-p1p2f3-w3-m1m2Mass') - - plotter.plot_mc_vs_data('os/p1f2p3', 'm1_m2_Mass', xaxis='m_{#mu#mu} (GeV)', xrange=(60, 120)) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-os-p1f2p3-m1m2Mass') - - # Check PU variables - plotter.plot_mc_vs_data('os/p1p2f3', 'rho') - plotter.save('mcdata-os-p1p2f3-rho') - - plotter.plot_mc_vs_data('os/p1p2f3', 'nvtx') - plotter.save('mcdata-os-p1p2f3-nvtx') - - # Lower stat but closer to signal region - plotter.plot_mc_vs_data('os/p1p2p3', 'rho') - plotter.save('mcdata-os-p1p2p3-rho') - - plotter.plot_mc_vs_data('os/p1p2p3', 'nvtx') - plotter.save('mcdata-os-p1p2p3-nvtx') - - # Make Z->mumu + tau jet control - - antiiso_m2JetPt = plotter.plot('data', 'ss/p1f2p3/m2JetPt', 'hist', styler=make_styler(2, 'hist'), xrange=(0, 120), rebin=10) - antiiso_m2JetPt.SetTitle("Anti-iso CR yield") - antiiso_m2JetPt.legendstyle='l' - antiiso_m2JetPt.GetXaxis().SetTitle("#mu_{2} Jet Pt") - plotter.save('data-p1f2p3-m2JetPt') - - antiiso_m1JetPt = plotter.plot('data', 'ss/f1p2p3/m1JetPt', 'hist', styler=make_styler(2, 'hist'), xrange=(0, 120), rebin=10) - antiiso_m1JetPt.SetTitle("Anti-iso CR yield") - antiiso_m1JetPt.legendstyle='l' - antiiso_m1JetPt.GetXaxis().SetTitle("#mu_{1} Jet Pt") - plotter.save('data-f1p2p3-m1JetPt') + if not options.no_mc_data: + ########################################################################### + ## Zmm control plots ##################################################### + ########################################################################### + plotter.set_subdir('mc_data/zmm') - zmm_weighted = plotter.plot('data', 'os/p1p2f3/w3/m1_m2_Mass', 'hist', styler=make_styler(2, 'hist'), xrange=(60, 120)) - zmm_weighted.SetTitle("Z#mu#mu + fake #tau_{h} est.") - zmm_weighted.legendstyle='l' - zmm_weighted.GetXaxis().SetTitle("m_{#mu#mu} (GeV)") - - zmm_unweighted = plotter.plot('data', 'os/p1p2p3/m1_m2_Mass', 'same', styler=make_styler(1), xrange=(60, 120)) - zmm_unweighted.SetTitle("Z#mu#mu observed") - zmm_unweighted.SetTitle("Z#mu#mu + fake #tau_{h} obs.") - zmm_unweighted.legendstyle='pe' - - plotter.add_legend([zmm_weighted, zmm_unweighted]) - plotter.add_cms_blurb(sqrts) - plotter.save('zmm-os-fr-control') - - ## plotter.plot('data', 'os/p1p2p3/prescale', styler=make_styler(1)) - ## plotter.save('zmm-os-prescale-check') - - plotter.plot('Zjets_M50', 'os/p1p2f3/weight') - plotter.save('zmm-mc-event-weights') - # Check MC weights - ## plotter.plot('Zjets_M50', 'os/p1p2f3/weight_nopu') - ## plotter.save('zmm-mc-event-weight_nopu') - - plotter.plot('Zjets_M50', 'os/p1p2f3/nTruePU', 'nTruePU', rebin=1, xaxis='True PU') - plotter.save('zjets-os-p1p2f3-nTruePU') - - - ########################################################################### - ## FR sideband MC-vs-Data ################################################ - ########################################################################### - - plotter.plot_mc_vs_data('ss/p1f2p3', 'm1Pt', rebin=10, xaxis='#mu_{1} p_{T} (GeV)', leftside=False) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1f2p3-m1Pt') - - plotter.plot_mc_vs_data('ss/p1f2p3', 'm2_t_Mass', rebin=10, xaxis='m_{#mu2#tau} (GeV)', leftside=False) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1f2p3-subMass') - - plotter.plot_mc_vs_data('ss/p1f2p3/w2', 'm1Pt', rebin=10, xaxis='#mu_{1} p_{T}', leftside=False) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1f2p3-w2-m1Pt') - - plotter.plot_mc_vs_data('ss/f1p2p3', 'm2_t_Mass', rebin=20, xaxis='m_{#mu2#tau} (GeV)', leftside=False) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-f1p2p3-subMass') - - plotter.plot_mc_vs_data('ss/f1p2p3/w1', 'm2_t_Mass', rebin=20, xaxis='m_{#mu2#tau} (GeV)', leftside=False) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-f1p2p3-w1-subMass') - - plotter.plot_mc_vs_data('ss/p1f2f3', 'm2AbsEta', rebin=10, xaxis='m_{#mu2#tau} (GeV)', leftside=False) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1f2f3-m2AbsEta') - - plotter.plot_mc_vs_data('ss/p1f2p3', 'm2AbsEta', rebin=10, xaxis='m_{#mu2#tau} (GeV)', leftside=False) - plotter.add_cms_blurb(sqrts) - plotter.save('mcdata-ss-p1f2p3-m2AbsEta') - - - - ########################################################################### - ## Signal region plots ################################################ - ########################################################################### - plotter.set_subdir('') - rebin_slim = [0,20]+range(30, 91, 10)+[110,200] - categories = { - 'LTCut' : [80, 650], - 'LTLow' : [0, 130], - 'LTHigh': [130, 650], - } - - for label, proj_range in categories.iteritems(): - plotter.set_subdir('%s' % label) - plotter.plot_final('m2_t_Mass#LT', rebin_slim, xaxis='m_{#mu_{2}#tau} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-%s' % label) - - plotter.plot_final('m2_t_Mass#LT', 200, xaxis='m_{#mu_{2}#tau} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-%s-counting' % label, dotc=True, dotroot=True) - - #pt - plotter.plot_final("m1Pt#LT" , 10, xaxis='p_{T#mu_{1}} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-m1Pt-%s' % label) - - plotter.plot_final("m2Pt#LT" , 10, xaxis='p_{T#mu_{2}} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-m2Pt-%s' % label) - - plotter.plot_final("m1JetPt#LT" , 10, xaxis='p_{T Jet #mu_{1}} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-m1JetPt-%s' % label) - - plotter.plot_final("m2JetPt#LT" , 10, xaxis='p_{T Jet #mu_{2}} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-m2JetPt-%s' % label) - - plotter.plot_final("tPt#LT" , 10, xaxis='p_{T#tau} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-tPt-%s' % label) - - #eta - plotter.plot_final("m1AbsEta#LT", 10, xaxis='|#eta_{#mu_{1}}|', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-m1AbsEta-%s' % label) - - plotter.plot_final("m2AbsEta#LT", 10, xaxis='|#eta_{#mu_{2}}|' , maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-m2AbsEta-%s' % label) - - plotter.plot_final("tAbsEta#LT", 10, xaxis='|#eta_{#tau}|' , maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-tAbsEta-%s' % label) - - #DR - plotter.plot_final("m1_t_DR#LT", 10, xaxis='#DeltaR_{#mu_{1}#tau}', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-m1_t_DR-%s' % label) - - plotter.plot_final("m2_t_DR#LT", 10, xaxis='#DeltaR_{#mu_{2}#tau}' , maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-m2_t_DR-%s' % label) - - #Jet BTag - - #from pdb import set_trace; set_trace() - plotter.plot_final("m2JetBtag#LT", [-100, -40, 20, 100], xaxis='#mu_{2} Jet Btag' , maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-m2JetBtag-%s' % label, dotroot=True) - - plotter.plot_final("m1JetBtag#LT", 1, xaxis='#mu_{1} Jet Btag' , maxy=None, project=proj_range, project_axis='X') + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'm1_m2_Mass', rebin=10, + xaxis='m_{#mu#mu} (GeV)', leftside=False) plotter.add_cms_blurb(sqrts) - plotter.save('final-m1JetBtag-%s' % label) + plotter.save('mcdata-Zmm-m1m2Mass') - plotter.plot_final("m2JetCSVBtag#LT", 1, xaxis='#mu_{2} Jet CSV Btag' , maxy=None, project=proj_range, project_axis='X', x_range=[0,1]) + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'm1Pt#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-m2JetCSVBtag-%s' % label, dotroot=True) + plotter.save('mcdata-Zmm-m1Pt') - plotter.plot_final("m1JetCSVBtag#LT", 1, xaxis='#mu_{1} Jet CSV Btag' , maxy=None, project=proj_range, project_axis='X', x_range=[0,1]) + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'm2Pt#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) plotter.add_cms_blurb(sqrts) - plotter.save('final-m1JetCSVBtag-%s' % label) - - - - #from pdb import set_trace; set_trace() - plotter.plot_final('m2_t_Mass#LT', rebin_slim, xaxis='m_{#mu_{2}#tau} (GeV)', maxy=None, project=[0, 130], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTLow') - - plotter.plot_final('m2_t_Mass#LT', rebin_slim, xaxis='m_{#mu_{2}#tau} (GeV)', maxy=None, project=[130, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTHigh') - - plotter.plot_final('m2_t_Mass#LT', rebin_slim, xaxis='m_{#mu_{2}#tau} (GeV)', maxy=None, project=[80, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTCut') - - plotter.plot_final('m2_t_Mass#LT', 20, xaxis='m_{#mu_{2}#tau} (GeV)', maxy=None, project=[80, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-LTCut-flatbin') - - plotter.plot_final('LT', 5, xaxis='LT (GeV)', maxy=15) - plotter.add_cms_blurb(sqrts) - plotter.save('final-LT') - - plotter.plot_final('m1Pt', 10) - plotter.add_cms_blurb(sqrts) - plotter.save('final-m1Pt') - - plotter.plot_final('m2Pt', 10) - plotter.add_cms_blurb(sqrts) - plotter.save('final-m2Pt') - - plotter.plot_final('m2Pt', 10) - plotter.add_cms_blurb(sqrts) - plotter.save('final-m2Pt') - - plotter.plot_final('m1AbsEta', 10) - plotter.add_cms_blurb(sqrts) - plotter.save('final-m1AbsEta') - - plotter.plot_final('m2AbsEta', 10) - plotter.add_cms_blurb(sqrts) - plotter.save('final-m2AbsEta') - - plotter.plot_final('m2AbsEta', 10) - plotter.add_cms_blurb(sqrts) - plotter.save('final-m2AbsEta') - - plotter.plot_final('m2_t_Mass', 20, xaxis='m_{#mu_{2}#tau} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass') - - plotter.plot_final('m2_t_Mass', 20, xaxis='m_{#mu_{2}#tau} (GeV)', qcd_weight_fraction=1) - plotter.add_cms_blurb(sqrts) - plotter.save('final-qweight-subMass') - - plotter.plot_final('m2_t_Mass', 20, xaxis='m_{#mu_{2}#tau} (GeV)', qcd_weight_fraction=0.5) - plotter.add_cms_blurb(sqrts) - plotter.save('final-qweight05-subMass') - - plotter.plot_final('m2_t_Mass', 20, xaxis='m_{#mu_{2}#tau} (GeV)', show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-werror') - - plotter.plot_final('m2_t_Mass', 20, xaxis='m_{#mu_{2}#tau} (GeV)', - show_error=True, fake_error=0, wz_error=0, zz_error=0) - plotter.add_cms_blurb(sqrts) - plotter.save('final-subMass-wshapeerror') - - plotter.plot_final('m2RelPFIsoDB', 10) - plotter.add_cms_blurb(sqrts) - plotter.save('final-m2Iso') - - ########################################################################### - ## WZ enhanced region plots ########################################### - ########################################################################### - plotter.set_subdir('WZ_enhanced') - - plotter.plot_final_wz('m1_t_Mass', 10, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-wz-leadMass') - - plotter.plot_final_wz('m2Pt', 5, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-wz-m2Pt') - - plotter.plot_final_wz('m2JetPt', 5, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-wz-m2JetPt') - - ########################################################################### - ## F3 enhanced region plots ########################################### - ########################################################################### - categories = { - 'LTCut' : [80, 650], - 'LTLow' : [0, 130], - 'LTHigh': [130, 650], - } + plotter.save('mcdata-Zmm-m2Pt') + + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'm1AbsEta#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) + plotter.add_cms_blurb(sqrts) + plotter.save('mcdata-Zmm-m1AbsEta') + + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'm2AbsEta#LT', rebin=10, + xaxis='p_{T}^{#mu1} (GeV)', leftside=False, preprocess=project_x) + plotter.add_cms_blurb(sqrts) + plotter.save('mcdata-Zmm-m2AbsEta') + + # Check PU variables + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'rho') + plotter.save('mcdata-Zmm-rho') + + plotter.plot_mc_vs_data('os/tau_?s/p1p2?3', 'nvtx') + plotter.save('mcdata-Zmm-nvtx') + + if not options.no_wz: + ########################################################################### + ## WZ control plots ##################################################### + ########################################################################### + plotter.set_subdir('mc_data/wz_enhanced') + + plotter.plot_mc_vs_data('ss/tau_os/p1p2p3_enhance_wz', 'm2_t_Mass#LT', xaxis='m_{#mu#mu} (GeV)', + xrange=(0, 120), rebin=10, leftside=False, preprocess=project_x) + plotter.add_cms_blurb(sqrts) + plotter.save('mcdata-ss-p1p2p3-enhance_wz-subMass') + + plotter.plot_mc_vs_data('ss/tau_os/p1p2p3_enhance_wz', 'm1_t_Mass', xaxis='m_{#mu#mu} (GeV)', + xrange=(0, 120), rebin=10, leftside=False, preprocess=project_x) + plotter.add_cms_blurb(sqrts) + plotter.save('mcdata-ss-p1p2p3-enhance_wz-leadMass') + + plotter.set_subdir('WZ_enhanced') + + plotter.plot_final_wz('m1_t_Mass', 10, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)') + plotter.add_cms_blurb(sqrts) + plotter.save('final-wz-leadMass') + + plotter.plot_final_wz('m2_t_Mass#LT', 10, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', + project=[0,650], project_axis='X') + plotter.add_cms_blurb(sqrts) + plotter.save('final-wz-subMass') + + plotter.plot_final_wz('m2Pt#LT', 5, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', + project=[0,650], project_axis='X') + plotter.add_cms_blurb(sqrts) + plotter.save('final-wz-m2Pt') + + plotter.plot_final_wz('m2JetPt#LT', 5, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', + project=[0,650], project_axis='X') + plotter.add_cms_blurb(sqrts) + plotter.save('final-wz-m2JetPt') + + if not options.no_signal: + ########################################################################### + ## Signal region plots ################################################ + ########################################################################### + plotter.set_subdir('') + #rebin_slim = range(20, 91, 10)+[110,200] + + for label, proj_range in categories.iteritems(): + for tau_charge in ['tau_os', 'tau_ss']: + if tau_charge == 'tau_os': + plotter.set_subdir('%s' % label) + else: + plotter.set_subdir('%s_charge3' % label) + + plotter.plot_final('m2_t_Mass#LT', rebin_slim, xaxis='m_{#mu_{2}#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', differential=True, + yaxis='Events / GeV', show_error=True, tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-subMass-%s' % label) + + plotter.plot_final('m2_t_Mass#LT', binning_7TeV, xaxis='m_{#mu_{2}#tau} (GeV)', + project=proj_range, project_axis='X', + yaxis='Events', show_error=True, x_range=[0,199], + maxy=15.6, tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-subMass-7TeVBin-%s' % label, verbose=True) + + plotter.plot_final('m2_t_Mass#LT', 300, xaxis='m_{#mu_{2}#tau} (GeV)', maxy=None, + project=proj_range, project_axis='X', show_error=True, tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-subMass-%s-counting' % label, json=True) #, dotc=True, dotroot=True) + + #pt + #plotter.plot_final("m1Pt#LT" , 10, xaxis='p_{T#mu_{1}} (GeV)', + # maxy=None, project=proj_range, project_axis='X', + # show_error=True, tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m1Pt-%s' % label) + # + #plotter.plot_final("m2Pt#LT" , 10, xaxis='p_{T#mu_{2}} (GeV)', maxy=None, + # project=proj_range, project_axis='X', + # show_error=True, tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m2Pt-%s' % label) + # + #plotter.plot_final("tPt#LT" , 10, xaxis='p_{T#tau} (GeV)', maxy=None, + # project=proj_range, project_axis='X', + # show_error=True, tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-tPt-%s' % label) + + #plotter.plot_final("m1JetPt#LT" , 10, xaxis='p_{T Jet #mu_{1}} (GeV)', + # maxy=None, project=proj_range, project_axis='X', + # show_error=True, tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m1JetPt-%s' % label) + # + #plotter.plot_final("m2JetPt#LT" , 10, xaxis='p_{T Jet #mu_{2}} (GeV)', + # maxy=None, project=proj_range, project_axis='X', + # show_error=True, tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m2JetPt-%s' % label) + # + #plotter.plot_final("tPt#LT" , 10, xaxis='p_{T#tau} (GeV)', maxy=None, + # project=proj_range, project_axis='X', show_error=True, + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-tPt-%s' % label) + + #eta + #plotter.plot_final("m1AbsEta#LT", 10, xaxis='|#eta_{#mu_{1}}|', maxy=None, + # project=proj_range, project_axis='X', show_error=True, + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m1AbsEta-%s' % label) + # + #plotter.plot_final("m2AbsEta#LT", 10, xaxis='|#eta_{#mu_{2}}|' , maxy=None, + # project=proj_range, project_axis='X', show_error=True, + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m2AbsEta-%s' % label) + # + #plotter.plot_final("tAbsEta#LT", 10, xaxis='|#eta_{#tau}|' , maxy=None, + # project=proj_range, project_axis='X', show_error=True, + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-tAbsEta-%s' % label) + + #DR + #plotter.plot_final("m1_t_DR#LT", 10, xaxis='#DeltaR_{#mu_{1}#tau}', maxy=None, + # project=proj_range, project_axis='X', show_error=True, + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m1_t_DR-%s' % label) + # + #plotter.plot_final("m2_t_DR#LT", 10, xaxis='#DeltaR_{#mu_{2}#tau}' , maxy=None, + # project=proj_range, project_axis='X', show_error=True, + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m2_t_DR-%s' % label) + + #Jet BTag + + #from pdb import set_trace; set_trace() + #plotter.plot_final("m2JetBtag#LT", [-100, -40, 20, 100], xaxis='#mu_{2} Jet Btag' , + # maxy=None, project=proj_range, project_axis='X', show_error=True, + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m2JetBtag-%s' % label, dotroot=True) + # + #plotter.plot_final("m1JetBtag#LT", 1, xaxis='#mu_{1} Jet Btag' , maxy=None, + # project=proj_range, project_axis='X', show_error=True, + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m1JetBtag-%s' % label) + # + #plotter.plot_final("m2JetCSVBtag#LT", 1, xaxis='#mu_{2} Jet CSV Btag' , maxy=None, + # project=proj_range, project_axis='X', x_range=[0,1], show_error=True, + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m2JetCSVBtag-%s' % label, dotroot=True) + # + #plotter.plot_final("m1JetCSVBtag#LT", 1, xaxis='#mu_{1} Jet CSV Btag' , maxy=None, + # project=proj_range, project_axis='X', x_range=[0,1], show_error=True, + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-m1JetCSVBtag-%s' % label) + + plotter.set_subdir('') + plotter.plot_final('LT', 10, xaxis='LT (GeV)', maxy=15) + plotter.add_cms_blurb(sqrts) + plotter.save('final-LT') + + if not options.no_f3: + ########################################################################### + ## F3 enhanced region plots ########################################### + ########################################################################### + plotter.set_subdir('f3') + #plotter.plot_final_f3('LT', [0, 50, 75, 100, 125, 150, 200, 300, 500], xaxis='LT (GeV)', maxy=None, + # show_ratio=True, fit = { 'model' : ("slope*x + offset", "offset[0, -1, 1], slope[0,-1,1]"), + # 'range' : [50, 500], 'options' : 'QIRMENS', + # 'stat position' : 'bottom-left'}) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-LT-tauOS-chi2', verbose=True) + + plotter.plot_final_f3('LT', [0, 50, 75, 100, 125, 150, 200, 300, 500], xaxis='LT (GeV)', maxy=None, + show_ratio=True, fit = { 'model' : ("slope*x + offset", "offset[0, -1, 1], slope[0,-1,1]"), + 'range' : [50, 500], 'options' : 'WLQIRMENS', + 'stat position' : 'bottom-left'}) + plotter.add_cms_blurb(sqrts) + plotter.save('final-LT-tauOS-likelihood') + + plotter.plot_final_f3('LT', [0, 50, 130, 500], xaxis='LT (GeV)', maxy=None, show_ratio=True) + plotter.add_cms_blurb(sqrts) + plotter.save('final-LT-tauOS-2categories') + + #plotter.plot_final_f3('LT', [0, 50, 75, 100, 125, 150, 200, 300, 500], xaxis='LT (GeV)', tau_charge='tau_ss', + # maxy=None, show_ratio=True, + # fit = {'range' : [50, 500], 'model' : ("slope*x + offset", "offset[0, -1, 1], slope[0,-1,1]")}) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-LT-tauSS') + + plotter.plot_final_f3('LT', [0, 50, 130, 500], xaxis='LT (GeV)', tau_charge='tau_ss', maxy=None, show_ratio=True) + plotter.add_cms_blurb(sqrts) + plotter.save('final-LT-tauSS-2categories') + + for label, proj_range in categories.iteritems(): + for tau_charge in ['tau_os', 'tau_ss']: + if tau_charge == 'tau_os': + plotter.set_subdir('f3/%s' % label) + else: + plotter.set_subdir('f3/%s_charge3' % label) + + plotter.plot_final_f3('m2_t_Mass#LT', rebin_slim, xaxis='m_{#mu_{2}#tau} (GeV)', + maxy=None, project=proj_range, project_axis='X', differential=True, + show_error=True, tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-subMass-%s' % label) + + plotter.plot_final_f3('m2_t_Mass#LT', binning_7TeV, xaxis='m_{#mu_{2}#tau} (GeV)', + maxy=None, project=proj_range, project_axis='X', differential=True, + show_error=True, tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-subMass-7TeVBin-%s' % label) + + plotter.plot_final_f3('m2_t_Mass#LT', 300, xaxis='m_{#mu_{2}#tau} (GeV)', + maxy=None, project=proj_range, project_axis='X', + tau_charge=tau_charge) + plotter.add_cms_blurb(sqrts) + plotter.save('final-f3-subMass-%s-counting' % label, json=True, verbose=True) + + #pt + #plotter.plot_final_f3("m1Pt#LT" , 10, xaxis='p_{T#mu_{1}} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-m1Pt-%s' % label) + # + #plotter.plot_final_f3("m2Pt#LT" , 10, xaxis='p_{T#mu_{2}} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-m2Pt-%s' % label) + # + #plotter.plot_final_f3("tPt#LT", 10, xaxis='p_{T#tau} (GeV)', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-tPt-%s' % label) + # + ##plotter.plot_final_f3("m1JetPt#LT" , 10, xaxis='p_{T Jet #mu_{1}} (GeV)', + ## maxy=None, project=proj_range, project_axis='X', + ## tau_charge=tau_charge) + ##plotter.add_cms_blurb(sqrts) + ##plotter.save('final-f3-m1JetPt-%s' % label) + ## + ##plotter.plot_final_f3("m2JetPt#LT" , 10, xaxis='p_{T Jet #mu_{2}} (GeV)', + ## maxy=None, project=proj_range, project_axis='X', + ## tau_charge=tau_charge) + ##plotter.add_cms_blurb(sqrts) + ##plotter.save('final-f3-m2JetPt-%s' % label) + # + ##eta + #plotter.plot_final_f3("m1AbsEta#LT", 10, xaxis='|#eta_{#mu_{1}}|', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-m1AbsEta-%s' % label) + # + #plotter.plot_final_f3("m2AbsEta#LT", 10, xaxis='|#eta_{#mu_{2}}|', maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-m2AbsEta-%s' % label) + # + #plotter.plot_final_f3("tAbsEta#LT", 10, xaxis='|#eta_{#tau}|' , maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-tAbsEta-%s' % label) + + #DR + #plotter.plot_final_f3("m1_t_DR#LT", 5, xaxis='#DeltaR_{#mu_{1}#tau}', + # maxy=None, project=proj_range, project_axis='X', + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-m1_t_DR-%s' % label) + # + #plotter.plot_final_f3("m2_t_DR#LT", 5, xaxis='#DeltaR_{#mu_{2}#tau}' , maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-m2_t_DR-%s' % label) + + #Jet BTag - for label, proj_range in categories.iteritems(): - plotter.set_subdir('f3/%s' % label) - plotter.plot_final_f3('m2_t_Mass#LT', rebin_slim, xaxis='m_{#mu_{2}#tau} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-%s' % label) - - plotter.plot_final_f3('m2_t_Mass#LT', 200, xaxis='m_{#mu_{2}#tau} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-%s-counting' % label, dotc=True, dotroot=True) - - #pt - plotter.plot_final_f3("m1Pt#LT" , 10, xaxis='p_{T#mu_{1}} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m1Pt-%s' % label) - - plotter.plot_final_f3("m2Pt#LT" , 10, xaxis='p_{T#mu_{2}} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m2Pt-%s' % label) - - plotter.plot_final_f3("m1JetPt#LT" , 10, xaxis='p_{T Jet #mu_{1}} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m1JetPt-%s' % label) - - plotter.plot_final_f3("m2JetPt#LT" , 10, xaxis='p_{T Jet #mu_{2}} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m2JetPt-%s' % label) - - plotter.plot_final_f3("tPt#LT" , 10, xaxis='p_{T#tau} (GeV)', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-tPt-%s' % label) - - #eta - plotter.plot_final_f3("m1AbsEta#LT", 10, xaxis='|#eta_{#mu_{1}}|', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m1AbsEta-%s' % label) - - plotter.plot_final_f3("m2AbsEta#LT", 10, xaxis='|#eta_{#mu_{2}}|' , maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m2AbsEta-%s' % label) - - plotter.plot_final_f3("tAbsEta#LT", 10, xaxis='|#eta_{#tau}|' , maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-tAbsEta-%s' % label) - - #DR - plotter.plot_final_f3("m1_t_DR#LT", 10, xaxis='#DeltaR_{#mu_{1}#tau}', maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m1_t_DR-%s' % label) - - plotter.plot_final_f3("m2_t_DR#LT", 10, xaxis='#DeltaR_{#mu_{2}#tau}' , maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m2_t_DR-%s' % label) - - #Jet BTag - - #from pdb import set_trace; set_trace() - plotter.plot_final_f3("m2JetBtag#LT", [-100, -40, 20, 100], xaxis='#mu_{2} Jet Btag' , maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m2JetBtag-%s' % label, dotroot=True) - - plotter.plot_final_f3("m1JetBtag#LT", 1, xaxis='#mu_{1} Jet Btag' , maxy=None, project=proj_range, project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m1JetBtag-%s' % label) - - plotter.plot_final_f3("m2JetCSVBtag#LT", 1, xaxis='#mu_{2} Jet CSV Btag' , maxy=None, project=proj_range, project_axis='X', x_range=[0,1]) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m2JetCSVBtag-%s' % label, dotroot=True) - - plotter.plot_final_f3("m1JetCSVBtag#LT", 1, xaxis='#mu_{1} Jet CSV Btag' , maxy=None, project=proj_range, project_axis='X', x_range=[0,1]) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m1JetCSVBtag-%s' % label) + #from pdb import set_trace; set_trace() + #plotter.plot_final_f3("m2JetBtag#LT", [-100, -40, 20, 100], xaxis='#mu_{2} Jet Btag', + # maxy=None, project=proj_range, project_axis='X', + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-m2JetBtag-%s' % label, dotroot=True) + # + #plotter.plot_final_f3("m1JetBtag#LT", 1, xaxis='#mu_{1} Jet Btag' , maxy=None, + # project=proj_range, project_axis='X', tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-m1JetBtag-%s' % label) + # + #plotter.plot_final_f3("m2JetCSVBtag#LT", 1, xaxis='#mu_{2} Jet CSV Btag', maxy=None, + # project=proj_range, project_axis='X', x_range=[0,1], + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-m2JetCSVBtag-%s' % label, dotroot=True) + # + #plotter.plot_final_f3("m1JetCSVBtag#LT", 1, xaxis='#mu_{1} Jet CSV Btag', maxy=None, + # project=proj_range, project_axis='X', x_range=[0,1], + # tau_charge=tau_charge) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-m1JetCSVBtag-%s' % label) + + #TAU ISO + #plotter.plot_final_f3("tRawIso3Hits#LT", [0,5,10,15,20,25,30,35,40,50,60,120,200], + # xaxis='#tau 3Hits Raw Iso', maxy=None, + # project=proj_range, project_axis='X', + # tau_charge=tau_charge, show_ratio=True, + # fit = { 'model' : ("slope*x + offset", "offset[0, -1, 1], slope[0,-1,1]"), + # 'options' : 'WLQIRMENS', + # 'stat position' : 'bottom-right'}) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-tRawIso3Hits-Likelihood-%s' % label) + # + #plotter.plot_final_f3("tRawIso3Hits#LT", [0,5,10,15,20,25,30,35,40,50,60,120,200], + # xaxis='#tau 3Hits Raw Iso', maxy=None, + # project=proj_range, project_axis='X', + # tau_charge=tau_charge, show_ratio=True, + # fit = { 'model' : ("slope*x + offset", "offset[0, -1, 1], slope[0,-1,1]"), + # 'options' : 'QIRMENS', + # 'stat position' : 'bottom-right'}) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-tRawIso3Hits-chi2-%s' % label) + # + #plotter.plot_final_f3("tRawIso3Hits#LT", [0,5,10,15,20,25,30,35,40,50,60,120,200], + # xaxis='#tau 3Hits Raw Iso', maxy=None, project=proj_range, + # project_axis='X', tau_charge=tau_charge, show_ratio=True, + # wz_error=0., zz_error =0.) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-f3-tRawIso3Hits-noDibosonErr-%s' % label) + #END OF if not options.dry_run: - plotter.set_subdir('f3') - plotter.plot_final_f3('LT', 5, xaxis='LT (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-LT') - - plotter.plot_final_f3('m1_t_Mass', 10, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-leadMass') - - plotter.plot_final_f3('m2_t_Mass', 20, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', qcd_correction=False, qcd_weight_fraction=0, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-wjetfake-subMass') - - plotter.plot_final_f3('m2_t_Mass', 20, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', qcd_correction=False, qcd_weight_fraction=1, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-qcdfake-subMass') - - plotter.plot_final_f3("m2JetBtag#LT", 4, xaxis='#mu_{2} Jet Btag' , maxy=None, project=[0, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m2JetBtag') - - plotter.plot_final_f3("m1JetBtag#LT", 4, xaxis='#mu_{1} Jet Btag' , maxy=None, project=[0, 650], project_axis='X') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m1JetBtag') - - plotter.plot_final_f3('m2_t_Mass', 20, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', qcd_correction=False, qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass') - - plotter.plot_final_f3('m2_t_Mass', 200, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', qcd_correction=False, qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-subMass-counting-like', dotc=True, dotroot=True) - - plotter.plot_final_f3_split('m2_t_Mass', 10, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-split-subMass') - - plotter.plot_final_f3('m1Pt', 5, xaxis='p_{T#mu_{1}} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m1Pt') - - plotter.plot_final_f3('m1JetPt', 5, xaxis='p_{TJet#mu_{1}} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m1JetPt') - - plotter.plot_final_f3('m1AbsEta', 10, xaxis='|#eta_{#mu_{1}}| (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m1AbsEta') - - plotter.plot_final_f3('m2Pt', 5, xaxis='p_{T#mu_{2}} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m2Pt') - - plotter.plot_final_f3('m2JetPt', 5, xaxis='p_{TJet#mu_{2}} (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m2JetPt') - - plotter.plot_final_f3('m2AbsEta', 10, xaxis='|#eta_{#mu_{2}}| (GeV)') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m2AbsEta') - - plotter.plot_final_f3('m1_t_DR', 5, xaxis='#DeltaR_{#mu_{1}#tau}') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m1_t_DR') - - plotter.plot_final_f3('m2_t_DR', 5, xaxis='#DeltaR_{#mu_{2}#tau}') - plotter.add_cms_blurb(sqrts) - plotter.save('final-f3-m2_t_DR') - + if not options.no_shapes: ########################################################################### - ## Check QCD contamination in control regions ############################ + ## Making shape file ################################################# ########################################################################### - plotter.set_subdir('qcd_contamination') - - plotter.plot_qcd_contamination('m2_t_Mass', 2, 10) - plotter.add_cms_blurb(sqrts) - plotter.save('final-qcd2-subMass') - - plotter.plot_qcd_contamination('m2_t_Mass', 1, 20) - plotter.add_cms_blurb(sqrts) - plotter.save('final-qcd1-subMass') - - plotter.plot_qcd_contamination('m2JetPt', 2, 10) - plotter.add_cms_blurb(sqrts) - plotter.save('final-qcd2-m2JetPt') - - plotter.plot_qcd_contamination('m1JetPt', 1, 20) - plotter.add_cms_blurb(sqrts) - plotter.save('final-qcd1-m1JetPt') - - #END OF if not options.dry_run: - ########################################################################### - ## Making shape file ################################################# - ########################################################################### - plotter.set_subdir('') - prefixes = [options.prefix+'$'] if options.prefix else [''] - prefixes = [i+'$' for i in options.prefixes.split(',') if i] if options.prefixes else prefixes - for prefix in prefixes: - plotter.plot_final(prefix+'m2_t_Mass', 20, xaxis='m_{#mu_{2}#tau} (GeV)', qcd_weight_fraction=0.5) - plotter.add_cms_blurb(sqrts) - plotter.save('final-%s-qweight05-subMass' % options.prefix) - - plotter.plot_final_f3(prefix+'m2_t_Mass', 20, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', qcd_correction=False, qcd_weight_fraction=0.5, show_error=True) - plotter.add_cms_blurb(sqrts) - plotter.save('final-%s-f3-qweight05-subMass' % options.prefix) - - shape_prefix = prefix if len(prefixes) > 1 else '' - shape_prefix = shape_prefix.replace(':','_').replace('$','_') - - shape_file = ROOT.TFile( - os.path.join(plotter.outputdir, 'LTCut_mmt_shapes_%s.root' % ( plotter.period) ), 'RECREATE') - shape_dir = shape_file.mkdir('mmtCatHigh') - plotter.write_shapes(prefix+'m2_t_Mass#LT', 20, shape_dir, qcd_fraction=0.5, project=[80, 650], project_axis='X') - shape_dir = shape_file.mkdir('mmtCatHigh_w') - plotter.write_shapes(prefix+'m2_t_Mass#LT', 20, shape_dir, qcd_fraction=0.0, project=[80, 650], project_axis='X') - shape_dir = shape_file.mkdir('mmtCatHigh_q') - plotter.write_shapes(prefix+'m2_t_Mass#LT', 20, shape_dir, qcd_fraction=1.0, project=[80, 650], project_axis='X') - shape_file.Close() - - - shape_file = ROOT.TFile( - os.path.join(plotter.outputdir, '%smmt_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') - - shape_dir = shape_file.mkdir('mmtCatLow') - plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0., 130], project_axis='X') - shape_dir = shape_file.mkdir('mmtCatLow_w') - plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') - shape_dir = shape_file.mkdir('mmtCatLow_q') - plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') - - shape_dir = shape_file.mkdir('mmtCatHigh') - plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[130, 650], project_axis='X') - shape_dir = shape_file.mkdir('mmtCatHigh_w') - plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') - shape_dir = shape_file.mkdir('mmtCatHigh_q') - plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') - - logging.warning('shape file %s created' % shape_file.GetName()) - shape_file.Close() - - - - shape_file = ROOT.TFile( - os.path.join(plotter.outputdir, '%smmt_f3_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') - - shape_dir = shape_file.mkdir('mmtCatLow') - plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[0., 130], project_axis='X') - shape_dir = shape_file.mkdir('mmtCatLow_w') - plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') - shape_dir = shape_file.mkdir('mmtCatLow_q') - plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') - - shape_dir = shape_file.mkdir('mmtCatHigh') - plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.5, project=[130, 650], project_axis='X') - shape_dir = shape_file.mkdir('mmtCatHigh_w') - plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') - shape_dir = shape_file.mkdir('mmtCatHigh_q') - plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') - - logging.warning('shape file %s created' % shape_file.GetName()) - shape_file.Close() + plotter.set_subdir('') + prefixes = [options.prefix+'$'] if options.prefix else [''] + prefixes = [i+'$' for i in options.prefixes.split(',') if i] if options.prefixes else prefixes + for prefix in prefixes: + #plotter.plot_final(prefix+'m2_t_Mass', 20, xaxis='m_{#mu_{2}#tau} (GeV)', qcd_weight_fraction=0.5) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-%s-qweight05-subMass' % options.prefix) + # + #plotter.plot_final_f3(prefix+'m2_t_Mass', 20, xaxis='m_{#mu_{1}#tau_{#mu}} (GeV)', qcd_correction=False, qcd_weight_fraction=0.5, show_error=True) + #plotter.add_cms_blurb(sqrts) + #plotter.save('final-%s-f3-qweight05-subMass' % options.prefix) + + shape_prefix = prefix if len(prefixes) > 1 else '' + shape_prefix = shape_prefix.replace(':','_').replace('$','_') + binning_7TeV = [0,40,80,120,200] + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, 'LTCut_mmt_shapes_%s.root' % ( plotter.period) ), 'RECREATE') + shape_dir = shape_file.mkdir('mmt') + plotter.write_shapes(prefix+'m2_t_Mass#LT', binning_7TeV, shape_dir, qcd_fraction=0., project=[80, 650], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatHigh_w') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', 20, shape_dir, qcd_fraction=-1., project=[80, 650], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatHigh_q') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', 20, shape_dir, qcd_fraction=1.0, project=[80, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, 'LTAll_mmt_shapes_%s.root' % ( plotter.period) ), 'RECREATE') + shape_dir = shape_file.mkdir('mmt') + plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[0, 650], project_axis='X') + #shape_dir = shape_file.mkdir('mmt_w') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=-1., project=[0, 650], project_axis='X') + #shape_dir = shape_file.mkdir('mmt_q') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + #rebin_slim = range(20, 91, 10)+[110,200] + shape_file = ROOT.TFile( + os.path.join(plotter.outputdir, '%smmt_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') + + shape_dir = shape_file.mkdir('mmtCatLow') + plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatLow_w') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=-1., project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatLow_q') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') + + shape_dir = shape_file.mkdir('mmtCatHigh') + plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0., project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatHigh_w') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=-1., project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatHigh_q') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + + logging.warning('shape file %s created' % shape_file.GetName()) + shape_file.Close() + + + #shape_file = ROOT.TFile( + # os.path.join(plotter.outputdir, '%smmt_f3_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') + # + #shape_dir = shape_file.mkdir('mmtCatLow') + #plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') + ##shape_dir = shape_file.mkdir('mmtCatLow_w') + ##plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=-1., project=[0., 130], project_axis='X') + ##shape_dir = shape_file.mkdir('mmtCatLow_q') + ##plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') + # + #shape_dir = shape_file.mkdir('mmtCatHigh') + #plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') + ##shape_dir = shape_file.mkdir('mmtCatHigh_w') + ##plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=-1., project=[130, 650], project_axis='X') + ##shape_dir = shape_file.mkdir('mmtCatHigh_q') + ##plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + # + #logging.warning('shape file %s created' % shape_file.GetName()) + #shape_file.Close() + # + #shape_file = ROOT.TFile( + # os.path.join(plotter.outputdir, '%smmt_f3_all_shapes_%s.root' % (shape_prefix, plotter.period) ), 'RECREATE') + # + #shape_dir = shape_file.mkdir('mmtCatLow') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatLow_w') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=-1., project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatLow_q') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') + # + #shape_dir = shape_file.mkdir('mmtCatHigh') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatHigh_w') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=-1., project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatHigh_q') + #plotter.write_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + # + #shape_dir = shape_file.mkdir('mmtCatLowf3') + #plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatLowf3_w') + #plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=-1., project=[0., 130], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatLowf3_q') + #plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[0., 130], project_axis='X') + # + #shape_dir = shape_file.mkdir('mmtCatHighf3') + #plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=0.0, project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatHighf3_w') + #plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=-1., project=[130, 650], project_axis='X') + #shape_dir = shape_file.mkdir('mmtCatHighf3_q') + #plotter.write_f3_shapes(prefix+'m2_t_Mass#LT', rebin_slim, shape_dir, qcd_fraction=1.0, project=[130, 650], project_axis='X') + # + #logging.warning('shape file %s created' % shape_file.GetName()) + #shape_file.Close() diff --git a/wh/baseSelections.py b/wh/baseSelections.py index 2e16cf93..fac2065f 100755 --- a/wh/baseSelections.py +++ b/wh/baseSelections.py @@ -11,23 +11,31 @@ def splitEid(label): return label.split('_')[-1], label.split('_')[0] #OBJECT SELECTION -def muSelection(row, name): +def muSelection(row, name, tracker=None): if getattr( row, getVar(name,'Pt')) < 10: return False + if tracker: tracker.Fill('muon Pt') + if getattr( row, getVar(name,'AbsEta')) > 2.4: return False + if tracker: tracker.Fill('muon AbsEta') + if not getattr( row, getVar(name,'PixHits')): return False + if tracker: tracker.Fill('muon PixHits') + if getattr( row, getVar(name,'JetCSVBtag')) > 0.8: return False - #if getattr( row, getVar(name,'JetBtag')) > 3.3: return False #was 3.3 + if tracker: tracker.Fill('muon JetCSVBtag') + if abs(getattr( row, getVar(name,'DZ'))) > 0.2: return False + if tracker: tracker.Fill('muon DZ') + return True def eSelection(row, name): if getattr( row, getVar(name,'Pt')) < 10: return False if getattr( row, getVar(name,'AbsEta')) > 2.5: return False if getattr( row, getVar(name,'MissingHits')): return False - if getattr( row, getVar(name,'HasConversion')): return False + if getattr( row, getVar(name,'HasMatchedConversion')): return False if not getattr( row, getVar(name,'ChargeIdTight')): return False if getattr( row, getVar(name,'JetCSVBtag')) > 0.8: return False - #if getattr( row, getVar(name,'JetBtag')) > 3.3: return False if abs(getattr( row, getVar(name,'DZ'))) > 0.2: return False return True @@ -35,16 +43,27 @@ def tauSelection(row, name): if getattr( row, getVar(name,'Pt')) < 20: return False if getattr( row, getVar(name,'AbsEta')) > 2.3: return False if abs(getattr( row, getVar(name,'DZ'))) > 0.2: return False + if getattr( row, + getVar( name, + 'ElectronPt10IdIsoVtxOverlap') + ): return False return True #VETOS -def vetos(row): +def vetos(row, tracker=None, bveto=True): if row.muVetoPt5IsoIdVtx: return False - if row.bjetCSVVetoZHLike: return False - #if row.bjetCSVVeto: return False + if tracker: tracker.Fill('muon veto') + + if bveto: + if row.bjetCSVVetoZHLike: return False + if tracker: tracker.Fill('bjet veto') + if row.eVetoMVAIsoVtx: return False + if tracker: tracker.Fill('electron veto') + if row.tauVetoPt20Loose3HitsVtx: return False + if tracker: tracker.Fill('tau veto') return True def lepton_id_iso(row, name, label): #label in the format eidtype_isotype @@ -68,8 +87,8 @@ def lepton_id_iso(row, name, label): #label in the format eidtype_isotype def control_region_ee(row): '''Figure out what control region we are in. Shared among two codes, to avoid mismatching copied here''' - if row.e1_e2_SS and lepton_id_iso(row, 'e1', 'eid12Medium_h2taucuts') and row.e1MtToMET > 30: - return 'wjets' + if row.e1_e2_SS and lepton_id_iso(row, 'e1', 'eid12Medium_h2taucuts') and row.e1MtToMET > 35: + return 'wjetsLtLow' elif row.e1_e2_SS and row.e1RelPFIsoDB > 0.3 and row.type1_pfMetEt < 25: #and row.metSignificance < 3: # return 'qcd' elif lepton_id_iso(row,'e1', 'eid12Medium_h2taucuts') and lepton_id_iso(row,'e2', 'eid12Medium_h2taucuts') \ diff --git a/wh/card_config/cgs.conf.eet.7TeV b/wh/card_config/cgs.conf.eet.7TeV new file mode 100644 index 00000000..32f2fff9 --- /dev/null +++ b/wh/card_config/cgs.conf.eet.7TeV @@ -0,0 +1,19 @@ +# cgs.config: Specification of groups, categories, and samples + +#$ GROUP signal Higgs_vtth_sm_120,Higgs_gf_sm_120,Higgs_vbf_sm_120 +#$ GROUP background Ztt,ttbar,EWK,Fakes +#categories: emu_vbf +#signals: signal +#backgrounds: background +#data: data_obs + +$ GROUP signal WH +#,WH_hww +$ GROUP background wz,zz,fakes,WH_hww125 +$ GROUP simulated WH,wz,zz,WH_hww125 +#,WH_hww + +categories: eetCatHigh,eetCatLow +signals: signal +backgrounds: background +data: data_obs diff --git a/wh/card_config/cgs.conf.eet.8TeV b/wh/card_config/cgs.conf.eet.8TeV index 317982bb..c70fa4f5 100644 --- a/wh/card_config/cgs.conf.eet.8TeV +++ b/wh/card_config/cgs.conf.eet.8TeV @@ -7,9 +7,9 @@ #backgrounds: background #data: data_obs -$ GROUP signal WH,WH_hww -$ GROUP all_background wz,zz,fakes,charge_fakes -$ GROUP simulated WH,wz,zz,WH_hww +$ GROUP signal WH +$ GROUP all_background wz,zz,fakes,charge_fakes,WH_hww125 +$ GROUP simulated WH,wz,zz,WH_hww125 categories: eetCatHigh,eetCatLow signals: signal diff --git a/wh/card_config/cgs.conf.emt.7TeV b/wh/card_config/cgs.conf.emt.7TeV new file mode 100644 index 00000000..381f2a15 --- /dev/null +++ b/wh/card_config/cgs.conf.emt.7TeV @@ -0,0 +1,19 @@ +# cgs.config: Specification of groups, categories, and samples + +#$ GROUP signal Higgs_vtth_sm_120,Higgs_gf_sm_120,Higgs_vbf_sm_120 +#$ GROUP background Ztt,ttbar,EWK,Fakes +#categories: emu_vbf +#signals: signal +#backgrounds: background +#data: data_obs + +$ GROUP signal WH +# +$ GROUP background wz,zz,fakes,WH_hww125 +$ GROUP simulated WH,wz,zz,WH_hww125 +#,WH_hww + +categories: emtCatHigh,emtCatLow +signals: signal +backgrounds: background +data: data_obs diff --git a/wh/card_config/cgs.conf.emt.8TeV b/wh/card_config/cgs.conf.emt.8TeV index 7a41f0a7..07e64400 100644 --- a/wh/card_config/cgs.conf.emt.8TeV +++ b/wh/card_config/cgs.conf.emt.8TeV @@ -7,9 +7,9 @@ #backgrounds: background #data: data_obs -$ GROUP signal WH,WH_hww -$ GROUP all_background wz,zz,fakes,charge_fakes -$ GROUP simulated WH,wz,zz,WH_hww +$ GROUP signal WH +$ GROUP all_background wz,zz,fakes,charge_fakes,WH_hww125 +$ GROUP simulated WH,wz,zz,WH_hww125 categories: emtCatHigh,emtCatLow signals: signal diff --git a/wh/card_config/cgs.conf.mmt.7TeV b/wh/card_config/cgs.conf.mmt.7TeV new file mode 100644 index 00000000..d64b07c8 --- /dev/null +++ b/wh/card_config/cgs.conf.mmt.7TeV @@ -0,0 +1,19 @@ +# cgs.config: Specification of groups, categories, and samples + +#$ GROUP signal Higgs_vtth_sm_120,Higgs_gf_sm_120,Higgs_vbf_sm_120 +#$ GROUP background Ztt,ttbar,EWK,Fakes +#categories: emu_vbf +#signals: signal +#backgrounds: background +#data: data_obs + +$ GROUP signal WH +#, +$ GROUP background wz,zz,fakes,WH_hww125 +$ GROUP simulated WH,wz,zz,WH_hww125 +#,WH_hww + +categories: mmtCatHigh,mmtCatLow +signals: signal +backgrounds: background +data: data_obs diff --git a/wh/card_config/cgs.conf.mmt.8TeV b/wh/card_config/cgs.conf.mmt.8TeV index bad4fb30..aa3d1bf9 100644 --- a/wh/card_config/cgs.conf.mmt.8TeV +++ b/wh/card_config/cgs.conf.mmt.8TeV @@ -7,9 +7,9 @@ #backgrounds: background #data: data_obs -$ GROUP signal WH,WH_hww -$ GROUP background wz,zz,fakes -$ GROUP simulated WH,wz,zz,WH_hww +$ GROUP signal WH +$ GROUP background wz,zz,fakes,WH_hww125 +$ GROUP simulated WH,wz,zz,WH_hww125 categories: mmtCatHigh,mmtCatLow signals: signal diff --git a/wh/card_config/unc.conf.eet.7TeV b/wh/card_config/unc.conf.eet.7TeV new file mode 100644 index 00000000..06257e08 --- /dev/null +++ b/wh/card_config/unc.conf.eet.7TeV @@ -0,0 +1,23 @@ +# unc.config: Specification of uncertainty parameter names and types +# Example: +# lumi lnN +# some_sb_parameter gmN 1000 +# some_shape_parameter shape + +lumi_7TeV lnN + +CMS_trigger_e lnN + +CMS_eff_e lnN +CMS_eff_t lnN + +CMS_fake_b_7TeV lnN + +CMS_scale_t lnN + +CMS_vhtt_eet_fakes_7TeV lnN +CMS_vhtt_eet_chargeFlip_7TeV shape + +pdf_qqbar lnN +QCDscale_VH lnN +QCDscale_VV lnN diff --git a/wh/card_config/unc.conf.eet.8TeV b/wh/card_config/unc.conf.eet.8TeV index 944955bc..7c64358b 100644 --- a/wh/card_config/unc.conf.eet.8TeV +++ b/wh/card_config/unc.conf.eet.8TeV @@ -6,6 +6,8 @@ lumi_8TeV lnN +CMS_trigger_e lnN + CMS_eff_e lnN CMS_eff_t lnN @@ -14,9 +16,6 @@ CMS_fake_b_8TeV lnN CMS_scale_t lnN CMS_vhtt_eet_fakes_8TeV lnN -#CMS_vhtt_eet_fakes_8TeV lnN -#CMS_vhtt_eetCatLow_fakes_8TeV lnN -#CMS_vhtt_eetCatHigh_fakes_8TeV lnN CMS_vhtt_eet_chargeFlip_8TeV shape @@ -24,5 +23,3 @@ pdf_qqbar lnN QCDscale_VH lnN QCDscale_VV lnN -# Only for testing. -#CMS_vhtt_llt_fakes lnN diff --git a/wh/card_config/unc.conf.emt.7TeV b/wh/card_config/unc.conf.emt.7TeV new file mode 100644 index 00000000..2809be5f --- /dev/null +++ b/wh/card_config/unc.conf.emt.7TeV @@ -0,0 +1,27 @@ +# unc.config: Specification of uncertainty parameter names and types +# Example: +# lumi lnN +# some_sb_parameter gmN 1000 +# some_shape_parameter shape + +lumi_7TeV lnN + +CMS_trigger_em lnN + +CMS_eff_e lnN +CMS_eff_m lnN +CMS_eff_t lnN + +CMS_fake_b_7TeV lnN + +CMS_scale_t lnN + +CMS_vhtt_emt_fakes_7TeV lnN +CMS_vhtt_emt_chargeFlip_7TeV shape + +pdf_qqbar lnN +QCDscale_VH lnN +QCDscale_VV lnN + +# Only for testing. +CMS_vhtt_llt_fakes lnN diff --git a/wh/card_config/unc.conf.emt.8TeV b/wh/card_config/unc.conf.emt.8TeV index e071e1a5..a448dd44 100644 --- a/wh/card_config/unc.conf.emt.8TeV +++ b/wh/card_config/unc.conf.emt.8TeV @@ -6,6 +6,8 @@ lumi_8TeV lnN +CMS_trigger_em lnN + CMS_eff_e lnN CMS_eff_m lnN CMS_eff_t lnN diff --git a/wh/card_config/unc.conf.mmt.7TeV b/wh/card_config/unc.conf.mmt.7TeV new file mode 100644 index 00000000..e9dc8834 --- /dev/null +++ b/wh/card_config/unc.conf.mmt.7TeV @@ -0,0 +1,23 @@ +# unc.config: Specification of uncertainty parameter names and types +# Example: +# lumi lnN +# some_sb_parameter gmN 1000 +# some_shape_parameter shape + +lumi_7TeV lnN + +CMS_trigger_m lnN + +CMS_eff_m lnN +CMS_eff_t lnN + +CMS_fake_b_7TeV lnN + +CMS_scale_t lnN + +CMS_vhtt_mmt_fakes_7TeV lnN + +pdf_qqbar lnN +QCDscale_VH lnN +QCDscale_VV lnN + diff --git a/wh/card_config/unc.conf.mmt.8TeV b/wh/card_config/unc.conf.mmt.8TeV index be49adf7..6f1268f3 100644 --- a/wh/card_config/unc.conf.mmt.8TeV +++ b/wh/card_config/unc.conf.mmt.8TeV @@ -6,6 +6,8 @@ lumi_8TeV lnN +CMS_trigger_m lnN + CMS_eff_m lnN CMS_eff_t lnN @@ -14,12 +16,8 @@ CMS_fake_b_8TeV lnN CMS_scale_t lnN CMS_vhtt_mmt_fakes_8TeV lnN -#CMS_vhtt_mmtCatHigh_fakes_8TeV lnN -#CMS_vhtt_mmtCatLow_fakes_8TeV lnN pdf_qqbar lnN QCDscale_VH lnN QCDscale_VV lnN -# Only for testing. -#CMS_vhtt_llt_fakes lnN diff --git a/wh/card_config/unc.vals.eet.7TeV b/wh/card_config/unc.vals.eet.7TeV new file mode 100644 index 00000000..9f6e0ec6 --- /dev/null +++ b/wh/card_config/unc.vals.eet.7TeV @@ -0,0 +1,32 @@ +########################################################################### +# unc.vals: specification of uncertainty values by category, sample, and uncertainty name +# Example: +# emu_vbf,emu_novbf,emu_boost signal,EWK lumi 1.045 + +eetCatLow,eetCatHigh simulated lumi_7TeV 1.022 + +# Trigger uncertainties +eetCatLow,eetCatHigh simulated CMS_trigger_e 1.01 + +# Efficiency uncertainties +eetCatLow,eetCatHigh simulated CMS_eff_e 1.04 +eetCatLow,eetCatHigh simulated CMS_eff_t 1.06 + +# Scale uncertainties +eetCatLow,eetCatHigh simulated CMS_scale_t 1.03 + +# Efficiency of b-jet veto +eetCatLow,eetCatHigh simulated CMS_fake_b_7TeV 1.01 + +# Background uncertainties - due to the theoretical unc. on the xsection +eetCatLow,eetCatHigh zz pdf_qqbar 1.033 +eetCatLow,eetCatHigh zz QCDscale_VV 1.023 + +eetCatLow,eetCatHigh wz pdf_qqbar 1.04 +eetCatLow,eetCatHigh wz QCDscale_VV 1.04 + +eetCatLow,eetCatHigh signal pdf_qqbar 1.026 +eetCatLow,eetCatHigh signal QCDscale_VH 1.009 + +#charge flip uncertainty +eetCatLow,eetCatHigh charge_fakes CMS_vhtt_eet_chargeFlip_7TeV 1.0 \ No newline at end of file diff --git a/wh/card_config/unc.vals.eet.8TeV b/wh/card_config/unc.vals.eet.8TeV index 79f1e01d..515bfc51 100644 --- a/wh/card_config/unc.vals.eet.8TeV +++ b/wh/card_config/unc.vals.eet.8TeV @@ -3,11 +3,14 @@ # Example: # emu_vbf,emu_novbf,emu_boost signal,EWK lumi 1.045 -eetCatLow,eetCatHigh simulated lumi_8TeV 1.042 +eetCatLow,eetCatHigh simulated lumi_8TeV 1.026 + +# Trigger uncertainties +eetCatLow,eetCatHigh simulated CMS_trigger_e 1.01 # Efficiency uncertainties -eetCatLow,eetCatHigh simulated CMS_eff_e 1.02 -eetCatLow,eetCatHigh simulated CMS_eff_t 1.08 +eetCatLow,eetCatHigh simulated CMS_eff_e 1.04 +eetCatLow,eetCatHigh simulated CMS_eff_t 1.06 # Scale uncertainties eetCatLow,eetCatHigh simulated CMS_scale_t 1.03 @@ -22,8 +25,8 @@ eetCatLow,eetCatHigh zz QCDscale_VV 1.023 eetCatLow,eetCatHigh wz pdf_qqbar 1.04 eetCatLow,eetCatHigh wz QCDscale_VV 1.04 -eetCatLow,eetCatHigh signal pdf_qqbar 1.034 -eetCatLow,eetCatHigh signal QCDscale_VH 1.004 +eetCatLow,eetCatHigh signal pdf_qqbar 1.023 +eetCatLow,eetCatHigh signal QCDscale_VH 1.01 #charge flip uncertainty eetCatLow,eetCatHigh charge_fakes CMS_vhtt_eet_chargeFlip_8TeV 1.0 \ No newline at end of file diff --git a/wh/card_config/unc.vals.emt.7TeV b/wh/card_config/unc.vals.emt.7TeV new file mode 100644 index 00000000..581895ff --- /dev/null +++ b/wh/card_config/unc.vals.emt.7TeV @@ -0,0 +1,33 @@ +########################################################################### +# unc.vals: specification of uncertainty values by category, sample, and uncertainty name +# Example: +# emu_vbf,emu_novbf,emu_boost signal,EWK lumi 1.045 + +emtCatHigh,emtCatLow simulated lumi_7TeV 1.022 + +# Trigger uncertainties +emtCatHigh,emtCatLow simulated CMS_trigger_em 1.01 + +# Efficiency uncertainties +emtCatHigh,emtCatLow simulated CMS_eff_e 1.02 +emtCatHigh,emtCatLow simulated CMS_eff_m 1.02 +emtCatHigh,emtCatLow simulated CMS_eff_t 1.08 + +# Scale uncertainties +emtCatHigh,emtCatLow simulated CMS_scale_t 1.03 + +# Efficiency of b-jet veto +emtCatHigh,emtCatLow simulated CMS_fake_b_7TeV 1.01 + +# Background uncertainties - due to the theoretical unc. on the xsection +emtCatHigh,emtCatLow zz pdf_qqbar 1.033 +emtCatHigh,emtCatLow zz QCDscale_VV 1.023 + +emtCatHigh,emtCatLow wz pdf_qqbar 1.04 +emtCatHigh,emtCatLow wz QCDscale_VV 1.04 + +emtCatHigh,emtCatLow signal pdf_qqbar 1.026 +emtCatHigh,emtCatLow signal QCDscale_VH 1.009 + +#charge flip uncertainty +emtCatHigh,emtCatLow charge_fakes CMS_vhtt_emt_chargeFlip_7TeV 1.0 diff --git a/wh/card_config/unc.vals.emt.8TeV b/wh/card_config/unc.vals.emt.8TeV index d42a8ab1..15590606 100644 --- a/wh/card_config/unc.vals.emt.8TeV +++ b/wh/card_config/unc.vals.emt.8TeV @@ -3,12 +3,15 @@ # Example: # emu_vbf,emu_novbf,emu_boost signal,EWK lumi 1.045 -emtCatHigh,emtCatLow simulated lumi_8TeV 1.042 +emtCatHigh,emtCatLow simulated lumi_8TeV 1.026 + +# Trigger uncertainties +emtCatHigh,emtCatLow simulated CMS_trigger_em 1.01 # Efficiency uncertainties emtCatHigh,emtCatLow simulated CMS_eff_e 1.02 -emtCatHigh,emtCatLow simulated CMS_eff_m 1.01 -emtCatHigh,emtCatLow simulated CMS_eff_t 1.08 +emtCatHigh,emtCatLow simulated CMS_eff_m 1.02 +emtCatHigh,emtCatLow simulated CMS_eff_t 1.06 # Scale uncertainties emtCatHigh,emtCatLow simulated CMS_scale_t 1.03 @@ -23,8 +26,8 @@ emtCatHigh,emtCatLow zz QCDscale_VV 1.023 emtCatHigh,emtCatLow wz pdf_qqbar 1.04 emtCatHigh,emtCatLow wz QCDscale_VV 1.04 -emtCatHigh,emtCatLow signal pdf_qqbar 1.034 -emtCatHigh,emtCatLow signal QCDscale_VH 1.004 +emtCatHigh,emtCatLow signal pdf_qqbar 1.023 +emtCatHigh,emtCatLow signal QCDscale_VH 1.01 #charge flip uncertainty emtCatHigh,emtCatLow charge_fakes CMS_vhtt_emt_chargeFlip_8TeV 1.0 diff --git a/wh/card_config/unc.vals.mmt.7TeV b/wh/card_config/unc.vals.mmt.7TeV new file mode 100644 index 00000000..b82af8cc --- /dev/null +++ b/wh/card_config/unc.vals.mmt.7TeV @@ -0,0 +1,29 @@ +########################################################################### +# unc.vals: specification of uncertainty values by category, sample, and uncertainty name +# Example: +# emu_vbf,emu_novbf,emu_boost signal,EWK lumi 1.045 + +mmtCatHigh,mmtCatLow simulated lumi_7TeV 1.022 + +# Trigger uncertainties +mmtCatHigh,mmtCatLow simulated CMS_trigger_m 1.01 + +# Efficiency uncertainties +mmtCatHigh,mmtCatLow simulated CMS_eff_m 1.04 +mmtCatHigh,mmtCatLow simulated CMS_eff_t 1.06 + +# Scale uncertainties +mmtCatHigh,mmtCatLow simulated CMS_scale_t 1.03 + +# Efficiency of b-jet veto +mmtCatHigh,mmtCatLow simulated CMS_fake_b_7TeV 1.01 + +# Background uncertainties - due to the theoretical unc. on the xsection +mmtCatHigh,mmtCatLow zz pdf_qqbar 1.033 +mmtCatHigh,mmtCatLow zz QCDscale_VV 1.023 + +mmtCatHigh,mmtCatLow wz pdf_qqbar 1.04 +mmtCatHigh,mmtCatLow wz QCDscale_VV 1.04 + +mmtCatHigh,mmtCatLow signal pdf_qqbar 1.026 +mmtCatHigh,mmtCatLow signal QCDscale_VH 1.009 diff --git a/wh/card_config/unc.vals.mmt.8TeV b/wh/card_config/unc.vals.mmt.8TeV index b3630604..915c37bb 100644 --- a/wh/card_config/unc.vals.mmt.8TeV +++ b/wh/card_config/unc.vals.mmt.8TeV @@ -3,11 +3,14 @@ # Example: # emu_vbf,emu_novbf,emu_boost signal,EWK lumi 1.045 -mmtCatHigh,mmtCatLow simulated lumi_8TeV 1.042 +mmtCatHigh,mmtCatLow simulated lumi_8TeV 1.026 + +# Trigger uncertainties +mmtCatHigh,mmtCatLow simulated CMS_trigger_m 1.01 # Efficiency uncertainties -mmtCatHigh,mmtCatLow simulated CMS_eff_m 1.02 -mmtCatHigh,mmtCatLow simulated CMS_eff_t 1.08 +mmtCatHigh,mmtCatLow simulated CMS_eff_m 1.04 +mmtCatHigh,mmtCatLow simulated CMS_eff_t 1.06 # Scale uncertainties mmtCatHigh,mmtCatLow simulated CMS_scale_t 1.03 @@ -22,6 +25,6 @@ mmtCatHigh,mmtCatLow zz QCDscale_VV 1.023 mmtCatHigh,mmtCatLow wz pdf_qqbar 1.04 mmtCatHigh,mmtCatLow wz QCDscale_VV 1.04 -mmtCatHigh,mmtCatLow signal pdf_qqbar 1.034 -mmtCatHigh,mmtCatLow signal QCDscale_VH 1.004 +mmtCatHigh,mmtCatLow signal pdf_qqbar 1.023 +mmtCatHigh,mmtCatLow signal QCDscale_VH 1.01 diff --git a/wh/check_shapes.py b/wh/check_shapes.py new file mode 100755 index 00000000..55537656 --- /dev/null +++ b/wh/check_shapes.py @@ -0,0 +1,214 @@ +#! /bin/env python + +import ROOT +import fnmatch +from optparse import OptionParser +import os +import re +import logging +import sys + +__author__ = "Mauro Verzetti (mauro.verzetti@cern.ch)" +__doc__ = """checks that the shapes for the limit are looking good""" + +parser = OptionParser(description=__doc__) +parser.add_option('--fakes', '-f', type=str, default = '*fakes*', + help='pattern to match to fakes',dest='fakes') +parser.add_option('--exclude', '-e', type=str, default = '*_?', + help='pattern of dirs to exclude',dest='exclude') +parser.add_option('--signal', '-s', type=str, default = 'WH*', + help='pattern of plots to be recognised as signals',dest='signal') +parser.add_option('--shape-unc-matcher', default='*CMS_*', + dest='shapematch', + help='Shell glob-style matcher for shape errors. These shapes arent shown.') +parser.add_option('--level', '-l', type=str, default = 'INFO', + help='pattern of dirs to excludelogging level', dest='level') + +args, shapes_filenames = parser.parse_args() + +logging.basicConfig(stream=sys.stderr, level=getattr(logging, args.level)) + +ROOT.gROOT.SetBatch(True) +ROOT.gStyle.SetOptStat('111111111') + +def walk(inputdir): + ''' Recursive function which generates (path, subdirs, histos) ''' + directories = [] + histos = [] + for key in inputdir.GetListOfKeys(): + # Keep track of stuff we find in this directory + name = key.GetName() + classname = key.GetClassName() + if classname.startswith('TDirectory'): + directories.append(name) + elif isinstance(inputdir.Get(name), ROOT.TH1): + histos.append(name) + path = inputdir.GetPath().split(':')[1] + # Yield the stuff in this directory + yield (path, tuple(directories), tuple(histos)) + # Now get the stuff in sub directories + for directory in directories: + for subresult in walk(inputdir.Get(directory)): + yield subresult + +def seperate_histos(histo_list, signal_pattern, err_pattern, fakes_pattern): + ''' Separate histogram lists into backgrounds, shape_uncs, and signals ''' + signals = [] + shapes = [] + bkgs = [] + fakes = [] + for x in histo_list: + if 'obs' in x: + continue + if fnmatch.fnmatch(x, signal_pattern): + signals.append(x) + elif fnmatch.fnmatch(x, err_pattern): + shapes.append(x) + elif fnmatch.fnmatch(x, fakes_pattern): + fakes.append(x) + else: + bkgs.append(x) + return (tuple(signals), tuple(shapes), tuple(fakes), tuple(bkgs)) + +def count_holes(histo): + nbins_x = histo.GetNbinsX() + last_filled = 0 + first_filled = nbins_x + for i in xrange(nbins_x, 1, -1): + if histo.GetBinContent(i): + last_filled = i + break + for i in xrange(1, nbins_x+1): + if histo.GetBinContent(i): + first_filled = i + break + + if last_filled < first_filled: + return -1 + #raise Exception("Error in count_holes") + + holes = sum( + 1 for i in xrange(first_filled, last_filled) + if not histo.GetBinContent(i) + ) + return holes + +def count_negatives(histo): + nbins_x = histo.GetNbinsX() + negatives = sum( + 1 for i in xrange(1, nbins_x+1) + if (histo.GetBinContent(i) + histo.GetBinError(i)) < 0 + ) + return negatives + +def check_empty(histo): + return histo.Integral() == 0 + +def check_sig_bkg(signals, backgrounds): + nbins_x = signals[0].GetNbinsX() #same as + bins = 0 + for ibin in xrange(1, nbins_x+1): + bkg_sum = sum( + h.GetBinContent(ibin) for h in backgrounds + if h.GetBinContent(ibin) >= 0 + ) + if not bkg_sum: #in background is empty + #check signals + bins += int( + any( + h.GetBinContent(ibin) > 0 + for h in signals + ) + ) + return bins + +def pair_signals(signals): + regex = re.compile("[a-zA-Z_]+(?P\d+)") + pairs = {} + for name in signals: + logging.debug(name) + mass = regex.match(name).group('mass') + pairs[mass] = pairs.get(mass,[]) + [name] + return pairs + + +good_shapes = [] +bad_shapes = [] +ugly_shapes = [] #not used + +for shape_filename in shapes_filenames: + logging.info( "Inspecting %s" % shape_filename) + shape_file = ROOT.TFile.Open(shape_filename) + + info = {} + for path, subdirs, histos in walk(shape_file): + if not histos or fnmatch.fnmatch(path, args.exclude): + continue + + h_dict = {} + for h in histos: + h_dict[h] = shape_file.Get(os.path.join(path, h)) + + logging.debug('%s, %s, %s' % (path.__repr__(), subdirs.__repr__(), histos.__repr__())) + signals, unc, fakes, bkgs = seperate_histos( + histos, args.signal, args.shapematch, args.fakes) + + paired_signals = pair_signals(signals) + logging.debug('paired signals: %s' % paired_signals) + signals = [] + for mass, names in paired_signals.iteritems(): + if len(names) == 1: + signals += names + if len(names) == 2: + h_dict[names[0]].Add(h_dict[names[1]]) + signals.append(names[0]) + + logging.debug('new signals: %s' % signals) + signals = tuple(signals) + #count holes + for h in fakes+bkgs: + hpath = os.path.join(path, h) + info[hpath] = {} + info[hpath]['holes'] = count_holes(h_dict[h]) + + for h in signals: + info[hpath]['empty'] = check_empty(h_dict[h]) + + #count negatives + for h in fakes: + hpath = os.path.join(path, h) + info[hpath]['negatives'] = count_negatives(h_dict[h]) + + h_sigs = [ h_dict[h] for h in signals ] + h_bkgs = [ h_dict[h] for h in fakes+bkgs ] + info[path] = { + 'signal_only' : check_sig_bkg(h_sigs, h_bkgs) + } + + has_errors = False + for path, values in info.iteritems(): + if values.get('holes') or values.get('negatives'): + has_errors = True + logging.info( '%s found with %i holes and %i negative bins!' % (path, values['holes'], values.get('negatives', 0)) ) + if values.get('empty'): + has_errors = True + logging.info( '%s found to be empty!' % path ) + if values.get('signal_only'): + has_errors = True + logging.info('category %s has %i bins with signal-only contribution!' % (path, values['signal_only'])) + if has_errors: + bad_shapes.append(shape_filename) + logging.warning('%s has errors!' % shape_filename) + else: + good_shapes.append(shape_filename) + logging.info( "\n\n\n") + shape_file.Close() + + + +print '\n\nBAD SHAPE FILES:\n\n' +print '\n'.join( bad_shapes ) + + +print '\n\nGOOD SHAPE FILES:\n\n' +print '\n'.join( good_shapes ) diff --git a/wh/cutflowtracker.py b/wh/cutflowtracker.py new file mode 100644 index 00000000..7963ac08 --- /dev/null +++ b/wh/cutflowtracker.py @@ -0,0 +1,44 @@ +import os + +class cut_flow_tracker(object): + def __init__(self, hist): + self.labels = [hist.GetXaxis().GetBinLabel(i+1) for i in range(hist.GetNbinsX())] + self.cut_flow = dict([ (i, False) for i in self.labels]) + self.hist = hist + self.evt_info = [-1, -1, -1] + self.disabled = 'CutFlow' not in os.environ + self.sync_mode = 'SYNC' in os.environ + + def fill(self, label): + self.cut_flow[label] = True + + def Fill(self, *labels): + if self.disabled: + return + for label in labels: + self.fill(label) + + def flush(self): + if self.disabled: + return + final_i = -1 + for i, label in enumerate(self.labels): + val = self.cut_flow[label] + if val: + self.hist.Fill(i+0.5) + final_i = i + if self.sync_mode: + fails = '' + try: + fails = 'fails %s' % (self.labels[final_i+1]) + except IndexError: + fails = 'passes the selection' #if len(self.labels) == final_i else 'passes the selection' + print 'Event %i:%i:%i ' % tuple(self.evt_info) + fails + + def new_row(self, *args): + if self.disabled: + return + if self.evt_info != list(args): + self.flush() + self.evt_info = list(args) + self.cut_flow = dict([ (i, False) for i in self.labels]) diff --git a/wh/dump_lumis.py b/wh/dump_lumis.py new file mode 100644 index 00000000..16e72136 --- /dev/null +++ b/wh/dump_lumis.py @@ -0,0 +1,43 @@ +import glob +import os +from FinalStateAnalysis.MetaData.data_views import extract_sample, read_lumi +import re +#from pudb import set_trace; set_trace() + +paths = { + #os.environ['current'], + 'current' : 'inputs/%s/' % os.environ['jobid'], + 'previous' : 'inputs/2013-Jun-30-8TeV/', +} + + +for dataset in ['DoubleMu', 'DoubleElectron', 'MuEG']: + lumi_dict = {} + for jobid, path in paths.iteritems(): + lumi_dict[jobid] = {} + for lumifile in glob.glob(path+'/data_'+dataset+'*.lumicalc.sum'): + lumi_dict[jobid][extract_sample(lumifile)] = read_lumi(lumifile) + + print dataset + print '%60s%20s%20s' % ('', 'current', 'previous') + keys = [] + for i in lumi_dict.values(): + keys += i.keys() + keys = list(set(keys)) + + total_lumis = dict([(i, {'current' : 0., 'previous' : 0.}) for i in ['2012A', '2012B', '2012C', '2012D', 'TOTAL',]]) + + for sample in keys: + curr_l = lumi_dict['current'][sample] if sample in lumi_dict['current'] else 0. + total_lumis['TOTAL']['current'] += curr_l + previous_l = lumi_dict['previous'][sample] if sample in lumi_dict['previous'] else 0. + total_lumis['TOTAL']['previous'] += previous_l + for key in total_lumis: + if key in sample: + total_lumis[key]['previous'] += previous_l + total_lumis[key]['current'] += curr_l + print '%60s%20s%20s' % (sample, '%.1f' % curr_l, '%.1f' % previous_l) + + for key, val in total_lumis.iteritems(): + print '%60s%20s%20s' % (key, '%.1f' % val['current'], '%.1f' % val['previous']) + print '\n\n' diff --git a/wh/dump_xsecs.py b/wh/dump_xsecs.py new file mode 100644 index 00000000..876a79c2 --- /dev/null +++ b/wh/dump_xsecs.py @@ -0,0 +1,29 @@ +import FinalStateAnalysis.Utilities.prettyjson as prettyjson +import os +import glob +from pdb import set_trace + +def dump_xsec(jobid): + json_files = [i for i in glob.glob('inputs/%s/*.json' % jobid) if 'data_' not in i] + lumi_files = [i for i in glob.glob('inputs/%s/*.sum' % jobid) if 'data_' not in i] + + datasets = {} + for json_file in json_files: + dname = json_file.split('/')[-1].split('.')[0] + datasets[dname] = {} + datasets[dname]['numevts'] = prettyjson.loads(open(json_file).read())['n_evts'] + + for lumi_file in lumi_files: + dname = lumi_file.split('/')[-1].split('.')[0] + datasets[dname]['lumi'] = float( open(lumi_file).read().strip() ) + + out_format = '%30s'+'%15s'*3 + print out_format % ('dataset', '# evts', 'lumi [/pb]', 'xsec [pb]') + for dataset, info in datasets.iteritems(): + print out_format % (dataset, '%.3f' % info['numevts'], '%.3f' % info['lumi'], '%.5f' % (info['numevts']/info['lumi']) ) + +print '\n\n%s\n' % os.environ['jobid7'] +dump_xsec(os.environ['jobid7']) + +print '\n\n%s\n' % os.environ['jobid8'] +dump_xsec(os.environ['jobid8']) diff --git a/wh/fakerate_functions.py b/wh/fakerate_functions.py index aa9db0e8..2728b37a 100755 --- a/wh/fakerate_functions.py +++ b/wh/fakerate_functions.py @@ -1,10 +1,14 @@ import os -from FinalStateAnalysis.StatTools.RooFunctorFromWS import build_roofunctor, make_corrector_from_th2, build_uncorr_2Droofunctor, FunctorFromMVA +from FinalStateAnalysis.StatTools.RooFunctorFromWS import build_roofunctor, make_corrector_from_th2,\ + build_uncorr_2Droofunctor, FunctorFromMVA, MultiFunctorFromMVA, MultiFunctorFromTF1 from FinalStateAnalysis.StatTools.VariableScaler import make_scaler from FinalStateAnalysis.Utilities.smartdict import SmartDict +from FinalStateAnalysis.MetaData.data_views import read_lumi import itertools import optimizer import re +import glob +from fnmatch import fnmatch #from optimizer import leading_lepton_iso_tag, subleading_lepton_iso_tag ################################################################################ @@ -12,7 +16,27 @@ ################################################################################ # Get fitted fake rate functions -frfit_dir = os.path.join('results', os.environ['jobid'], 'fakerate_fits') +frfit_dir = os.path.join('results', os.environ['jobid'], 'fakerate_fits')+'/' +is7TeV = '7TeV' in os.environ['jobid'] + +def get_lumis(pattern): + lumi_files = glob.glob( os.path.join('inputs', os.environ['jobid'], '*.lumicalc.sum') ) + ret = 0. + #print pattern + for lumi_file in lumi_files: + sample_name = os.path.basename(lumi_file).split('.')[0] + #print lumi_file, sample_name + if fnmatch(sample_name, pattern): + #print 'Matches!' + ret += read_lumi(lumi_file) + return ret + +double_e_lumi = get_lumis('data_DoubleElectron*') +double_mu_lumi = get_lumis('data_DoubleMu*') +mueg_lumi = get_lumis('data_MuEG*') +wz_lumi = get_lumis('WZ*ZToTauTau*') +zz_lumi = get_lumis('ZZ*') + def make_simple_mapper(tomap): def f_(string): @@ -77,10 +101,10 @@ def make_corrector_dict(filename, mapname, mapper=None): ret[i] = make_corrector_from_th2(filename % lepid, mapname) return ret -def make_scaler_dict(filename, mapname): +def make_scaler_dict(filename, mapname, **kwargs): ret = {} for i in optimizer.lep_id: - ret[i] = make_scaler(filename % i, 'mass_scale') + ret[i] = make_scaler(filename % i, 'mass_scale', **kwargs) return ret def make_mva_functor_dict(template, variables, mapper=None): @@ -96,64 +120,232 @@ def make_mva_functor_dict(template, variables, mapper=None): ) return ret +def null(*args, **kwargs): + return 0. -################## -## 1D Muon Func ## -################## +def make_null_dict(template, variables, mapper=None): + ret = {} + for i in optimizer.lep_id: + lepid = i + if mapper: + lepid = mapper(lepid) + ret[i] = null + return ret + +SKIP_MMT=eval( os.environ.get('SKIP_MMT','False') ) +SKIP_EMT=eval( os.environ.get('SKIP_EMT','False') ) +SKIP_EET=eval( os.environ.get('SKIP_EET','False') ) -#no changes in muonID in 2013 -mapper = {'eid1[0-9][A-Z][a-z]+_':'', 'idiso02' : 'pfidiso02'} -variables = ['muonJetPt', 'muonPt', 'muonJetCSVBtag'] #'muonPVDXY']#, 'muonJetBtag'] -highpt_mu_fr = make_mva_functor_dict(frfit_dir + '/m_wjets_pt20_%s_muonInfo.kNN.weights.xml', variables, mapper=make_regex_mapper(mapper)) -lowpt_mu_fr = make_mva_functor_dict(frfit_dir + '/m_wjets_pt10_%s_muonInfo.kNN.weights.xml', variables, mapper=make_regex_mapper(mapper)) +################### +## MMT FUNCTIONS ## +################### -highpt_mu_qcd_fr = make_mva_functor_dict(frfit_dir + '/m_qcd_pt20_%s_muonInfo.kNN.weights.xml', variables, mapper=make_regex_mapper(mapper)) -lowpt_mu_qcd_fr = make_mva_functor_dict(frfit_dir + '/m_qcd_pt10_%s_muonInfo.kNN.weights.xml', variables, mapper=make_regex_mapper(mapper)) +wz_sample = 'WZJetsTo3LNu_ZToTauTau_pythia' if '8TeV' in os.environ['jobid'] else 'WZJetsTo3LNu_ZToTauTau' +if not SKIP_MMT: + #no changes in muonID in 2013 + mapper = {'eid1[0-9][A-Z][a-z]+_':'', 'idiso02' : 'pfidiso02'} + variables = ['muonJetPt', 'muonPt', 'numJets20'] #, 'muonJetCSVBtag'] + lowpt_mu_fr = SmartDict() + lowpt_mu_fr.book( 'eid12Medium_h2taucuts020', + MultiFunctorFromMVA, + 'lowpt_mu_fr', + (frfit_dir + 'mm_wjets_pt10_h2taucuts020_muonInfo_k50.data.kNN.weights.xml', double_mu_lumi), + [ (frfit_dir + 'mm_wjets_pt10_h2taucuts020_muonInfo_k50.%s.kNN.weights.xml' % wz_sample, wz_lumi), + (frfit_dir + 'mm_wjets_pt10_h2taucuts020_muonInfo_k50.ZZJetsTo4L_pythia.kNN.weights.xml', zz_lumi)], + *variables) + + highpt_mu_fr = SmartDict() + highpt_mu_fr.book( 'eid12Medium_h2taucuts', + MultiFunctorFromMVA, + 'highpt_mu_fr', + (frfit_dir + 'mm_wjets_pt10_h2taucuts_muonInfo_k50.data.kNN.weights.xml', double_mu_lumi), + [ (frfit_dir + 'mm_wjets_pt10_h2taucuts_muonInfo_k50.%s.kNN.weights.xml' % wz_sample, wz_lumi), + (frfit_dir + 'mm_wjets_pt10_h2taucuts_muonInfo_k50.ZZJetsTo4L_pythia.kNN.weights.xml', zz_lumi)], + *variables, + phase_space='muonPt > 20' + ) + + lowpt_mu_qcd_fr = SmartDict() + lowpt_mu_qcd_fr.book( 'eid12Medium_h2taucuts020', + FunctorFromMVA, + 'lowpt_mu_qcd_fr', + frfit_dir + 'mm_qcd_pt10_h2taucuts020_muonInfo_k50.data.kNN.weights.xml', + *variables) -####################### -## 1D Electrons Func ## -####################### + highpt_mu_qcd_fr = SmartDict() + highpt_mu_qcd_fr.book( 'eid12Medium_h2taucuts', + FunctorFromMVA, + 'highpt_mu_qcd_fr', + frfit_dir + 'mm_qcd_pt10_h2taucuts_muonInfo_k50.data.kNN.weights.xml', + *variables) + + + + #lowpt_mu_fr = make_mva_functor_dict(frfit_dir + '/mm_wjets_pt10_%s_muonInfo_k100.kNN.weights.xml', variables, mapper=make_regex_mapper(mapper)) + #highpt_mu_fr = lowpt_mu_fr + # + #lowpt_mu_qcd_fr = make_mva_functor_dict(frfit_dir + '/mm_qcd_pt10_%s_muonInfo_k100.kNN.weights.xml', variables, mapper=make_regex_mapper(mapper)) + #highpt_mu_qcd_fr = lowpt_mu_qcd_fr + +################### +## EMT FUNCTIONS ## +################### -variables = ['electronJetPt', 'electronPt'] -#EMT -highpt_e_fr = make_mva_functor_dict(frfit_dir + '/e_wjets_pt20_%s_electronInfo.kNN.weights.xml', variables) -lowpt_e_fr = make_mva_functor_dict(frfit_dir + '/e_wjets_pt10_%s_electronInfo.kNN.weights.xml', variables) +if not SKIP_EMT: + variables = ['muonJetPt', 'muonPt', 'numJets20'] -highpt_e_qcd_fr = make_mva_functor_dict(frfit_dir + '/e_qcd_pt20_%s_electronInfo.kNN.weights.xml', variables) -lowpt_e_qcd_fr = make_mva_functor_dict(frfit_dir + '/e_qcd_pt10_%s_electronInfo.kNN.weights.xml', variables) + lowpt_mue_fr = SmartDict() + lowpt_mue_fr.book( 'eid12Medium_h2taucuts', + MultiFunctorFromMVA, + 'lowpt_mue_fr', + (frfit_dir + 'em_Mwjets_pt10_h2taucuts_muonInfo_k50.data.kNN.weights.xml', mueg_lumi), + [ (frfit_dir + 'em_Mwjets_pt10_h2taucuts_muonInfo_k50.%s.kNN.weights.xml' % wz_sample, wz_lumi), + (frfit_dir + 'em_Mwjets_pt10_h2taucuts_muonInfo_k50.ZZJetsTo4L_pythia.kNN.weights.xml', zz_lumi)], + *variables) -#EET -highpt_ee_fr = make_mva_functor_dict(frfit_dir + '/ee_wjetsNoZmass_pt20_%s_electronInfo.kNN.weights.xml', variables) -lowpt_ee_fr = make_mva_functor_dict(frfit_dir + '/ee_wjetsNoZmass_pt10_%s_electronInfo.kNN.weights.xml', variables) + highpt_mue_fr = SmartDict() + highpt_mue_fr.book( 'eid12Medium_h2taucuts', + MultiFunctorFromMVA, + 'lowpt_mue_fr', + (frfit_dir + 'em_Mwjets_pt10_h2taucuts_muonInfo_k50.data.kNN.weights.xml', mueg_lumi), + [ (frfit_dir + 'em_Mwjets_pt10_h2taucuts_muonInfo_k50.%s.kNN.weights.xml' % wz_sample, wz_lumi), + (frfit_dir + 'em_Mwjets_pt10_h2taucuts_muonInfo_k50.ZZJetsTo4L_pythia.kNN.weights.xml', zz_lumi)], + *variables, + phase_space='muonPt > 20' + ) -highpt_ee_qcd_fr = make_mva_functor_dict(frfit_dir + '/ee_qcd_pt20_%s_electronInfo.kNN.weights.xml', variables) -lowpt_ee_qcd_fr = make_mva_functor_dict(frfit_dir + '/ee_qcd_pt10_%s_electronInfo.kNN.weights.xml', variables) + + lowpt_mue_qcd_fr = SmartDict() + lowpt_mue_qcd_fr.book( 'eid12Medium_h2taucuts', + FunctorFromMVA, + 'lowpt_mue_qcd_fr', + frfit_dir + 'em_Mqcd_pt10_h2taucuts_muonInfo_k50.data.kNN.weights.xml', + *variables ) + highpt_mue_qcd_fr = lowpt_mue_qcd_fr + + + variables = ['electronJetPt', 'electronPt', 'numJets20'] + lowpt_e_fr = SmartDict() + lowpt_e_fr.book( 'eid12Medium_h2taucuts', + MultiFunctorFromMVA, + 'lowpt_e_fr', + (frfit_dir + 'em_wjets_pt10_eid12Medium_h2taucuts_electronInfo_k50.data.kNN.weights.xml', mueg_lumi), + [ (frfit_dir + 'em_wjets_pt10_eid12Medium_h2taucuts_electronInfo_k50.%s.kNN.weights.xml' % wz_sample, wz_lumi), + (frfit_dir + 'em_wjets_pt10_eid12Medium_h2taucuts_electronInfo_k50.ZZJetsTo4L_pythia.kNN.weights.xml', zz_lumi)], + *variables) + + highpt_e_fr = SmartDict() + highpt_e_fr.book( 'eid12Medium_h2taucuts', + MultiFunctorFromMVA, + 'lowpt_e_fr', + (frfit_dir + 'em_wjets_pt10_eid12Medium_h2taucuts_electronInfo_k50.data.kNN.weights.xml', mueg_lumi), + [ (frfit_dir + 'em_wjets_pt10_eid12Medium_h2taucuts_electronInfo_k50.%s.kNN.weights.xml' % wz_sample, wz_lumi), + (frfit_dir + 'em_wjets_pt10_eid12Medium_h2taucuts_electronInfo_k50.ZZJetsTo4L_pythia.kNN.weights.xml', zz_lumi)], + *variables, + phase_space='electronPt > 20' + ) + + lowpt_e_qcd_fr = SmartDict() + lowpt_e_qcd_fr.book( 'eid12Medium_h2taucuts', + FunctorFromMVA, + 'lowpt_e_qcd_fr', + frfit_dir + 'em_qcd_pt10_eid12Medium_h2taucuts_electronInfo_k50.data.kNN.weights.xml', + *variables ) + highpt_e_qcd_fr = lowpt_e_qcd_fr + + + #lowpt_mue_fr = make_mva_functor_dict(frfit_dir + '/em_Mwjets_pt10_%s_muonInfo_k100.kNN.weights.xml', variables, mapper=make_regex_mapper(mapper)) + #highpt_mue_fr = lowpt_mue_fr + # + #lowpt_mue_qcd_fr = make_mva_functor_dict(frfit_dir + '/em_Mqcd_pt10_%s_muonInfo_k100.kNN.weights.xml', variables, mapper=make_regex_mapper(mapper)) + #highpt_mue_qcd_fr = lowpt_mue_qcd_fr + # + #variables = ['electronJetPt', 'electronPt', 'numJets20'] + #lowpt_e_fr = make_mva_functor_dict(frfit_dir + '/em_wjets_pt10_%s_electronInfo_k100.kNN.weights.xml', variables) + #highpt_e_fr = lowpt_e_fr + # + #lowpt_e_qcd_fr = make_mva_functor_dict(frfit_dir + '/em_qcd_pt10_%s_electronInfo_k100.kNN.weights.xml', variables) + #highpt_e_qcd_fr = lowpt_e_qcd_fr + + +################### +## EET FUNCTIONS ## +################### + +if not SKIP_EET: + variables = ['electronJetPt', 'electronPt', 'numJets20'] + lowpt_ee_fr = SmartDict() + lowpt_ee_fr.book( 'eid12Medium_h2taucuts020', + MultiFunctorFromMVA, + 'lowpt_ee_fr', + (frfit_dir + 'ee_wjetsNoZmass_pt10_eid12Medium_h2taucuts020_electronInfo_k50.data.kNN.weights.xml', double_e_lumi), + [ (frfit_dir + 'ee_wjetsNoZmass_pt10_eid12Medium_h2taucuts020_electronInfo_k50.%s.kNN.weights.xml' % wz_sample, wz_lumi), + (frfit_dir + 'ee_wjetsNoZmass_pt10_eid12Medium_h2taucuts020_electronInfo_k50.ZZJetsTo4L_pythia.kNN.weights.xml', zz_lumi)], + *variables) + + highpt_ee_fr = SmartDict() + highpt_ee_fr.book( 'eid12Tight_h2taucuts', + MultiFunctorFromMVA, + 'highpt_ee_fr', + (frfit_dir + 'ee_wjetsNoZmass_pt10_eid12Tight_h2taucuts_electronInfo_k50.data.kNN.weights.xml', double_e_lumi), + [ (frfit_dir + 'ee_wjetsNoZmass_pt10_eid12Tight_h2taucuts_electronInfo_k50.%s.kNN.weights.xml' % wz_sample, wz_lumi), + (frfit_dir + 'ee_wjetsNoZmass_pt10_eid12Tight_h2taucuts_electronInfo_k50.ZZJetsTo4L_pythia.kNN.weights.xml', zz_lumi)], + *variables, + phase_space='electronPt > 20' + ) + + + lowpt_ee_qcd_fr = SmartDict() + lowpt_ee_qcd_fr.book( 'eid12Medium_h2taucuts020', + FunctorFromMVA, + 'lowpt_ee_qcd_fr', + frfit_dir + 'ee_qcd_pt10_eid12Medium_h2taucuts020_electronInfo_k50.data.kNN.weights.xml', + *variables) + + highpt_ee_qcd_fr = SmartDict() + highpt_ee_qcd_fr.book( 'eid12Tight_h2taucuts', + FunctorFromMVA, + 'highpt_ee_qcd_fr', + frfit_dir + 'ee_qcd_pt10_eid12Tight_h2taucuts_electronInfo_k50.data.kNN.weights.xml', + *variables) + + + + + #lowpt_ee_fr = make_mva_functor_dict(frfit_dir + '/ee_wjetsNoZmass_pt10_%s_electronInfo_k100.kNN.weights.xml', variables) + #highpt_ee_fr = lowpt_ee_fr + # + #lowpt_ee_qcd_fr = make_mva_functor_dict(frfit_dir + '/ee_qcd_pt10_%s_electronInfo_k100.kNN.weights.xml', variables) + #highpt_ee_qcd_fr = lowpt_ee_qcd_fr ################## ## 1D Taus Func ## ################## -tau_fr = build_roofunctor( - frfit_dir + '/t_ztt_pt20_mvaloose_tauPt.root', - 'fit_efficiency', # workspace name - 'efficiency' +filename = '2011.root' if is7TeV else '2012.root' +date = '2011' if is7TeV else '2012' +tau_fr = MultiFunctorFromTF1( + 'external/%s' % filename, + [('%s_wmuJetsAll_electronLooseMVA3_muonTight_lepTauOS_fakeable_combinedIsolationLoose3Hits_byEta_projX_barrel/landau' % date, [-1, 0.8]), + ('%s_wmuJetsAll_electronLooseMVA3_muonTight_lepTauOS_fakeable_combinedIsolationLoose3Hits_byEta_projX_transition/landau' % date, [0.8, 1.6]), + ('%s_wmuJetsAll_electronLooseMVA3_muonTight_lepTauOS_fakeable_combinedIsolationLoose3Hits_byEta_projX_forward/landau' % date, [1.6, 100])] ) tau_qcd_fr = tau_fr -e_charge_flip = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_%s.root", "efficiency_map") -e_charge_flip_up = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_%s.root", "efficiency_map_statUp") -e_charge_flip_down = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_%s.root", "efficiency_map_statDown") -mass_scaler = make_scaler_dict(frfit_dir+"/charge_flip_prob_map_%s.root", 'mass_scale') -default_scaler = mass_scaler[mass_scaler.keys()[0]] - - -e1_charge_flip = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_e1_%s.root", "efficiency_map") -e1_charge_flip_up = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_e1_%s.root", "efficiency_map_statUp") -e1_charge_flip_down = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_e1_%s.root", "efficiency_map_statDown") +############################ +## CHARGE FLIP FUNCTIONS ## +############################ -e2_charge_flip = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_e2_%s.root", "efficiency_map") -e2_charge_flip_up = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_e2_%s.root", "efficiency_map_statUp") -e2_charge_flip_down = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_e2_%s.root", "efficiency_map_statDown") +if not (SKIP_EMT or SKIP_EET): + e_charge_flip = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_%s.root", "efficiency_map") + e_charge_flip_up = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_%s.root", "efficiency_map_statUp") + e_charge_flip_down = make_corrector_dict(frfit_dir+"/charge_flip_prob_map_%s.root", "efficiency_map_statDown") + mass_scaler = make_scaler_dict(frfit_dir+"/charge_flip_prob_map_%s.root", 'mass_scale') + mass_scaler_up = make_scaler_dict(frfit_dir+"/charge_flip_prob_map_%s.root", 'mass_scale', error_scale=1 ) + mass_scaler_down = make_scaler_dict(frfit_dir+"/charge_flip_prob_map_%s.root", 'mass_scale', error_scale=-1) + default_scaler = mass_scaler[ mass_scaler.keys()[0]] + default_scaler_up = mass_scaler_up[ mass_scaler.keys()[0]] + default_scaler_down = mass_scaler_down[mass_scaler.keys()[0]] diff --git a/wh/get_fake_systematic.py b/wh/get_fake_systematic.py index 7b39c2fa..9b12d4bc 100755 --- a/wh/get_fake_systematic.py +++ b/wh/get_fake_systematic.py @@ -1,29 +1,31 @@ -#!/usr/bin/env python +#! /bin/env python -''' - -Get fake systematic. - -''' - -from rootpy import io -import rootpy.plotting.views as views import sys - -the_filename = sys.argv[1] -base_dirs = sys.argv[2].split(',') -syst_name = sys.argv[3] - -the_file = io.open(the_filename, 'READ') -nom_view = views.SumView( - *[ views.SubdirectoryView( the_file, dirname ) for dirname in base_dirs] -) - -qcd_view = views.SumView( - *[ views.SubdirectoryView( the_file, dirname+'_q' ) for dirname in base_dirs] -) - -nom = nom_view.Get('fakes').Integral() -qcd = qcd_view.Get('fakes').Integral() - -print "%s fakes %s %0.2f" % (','.join(base_dirs), syst_name, 1 + abs(nom - qcd) / nom) +import os +import re + +"Usage pull files get_unc_val_from_pull.py pulls.txt nuisance initial_value header" + +header = sys.argv[-1] +prefit = float(sys.argv[-2]) +nuisance = sys.argv[-3] +filename = sys.argv[-4] + +if not os.path.isfile(filename): + print "%s not found!" % filename + sys.exit(1) + +lines = open(filename).readlines() +parser = re.compile(r'(?P\w+)\s+(?P(?:[\*\!] )?[\-+]\d+\.\d+)\,\s(?P\d+\.\d+)') + +for line in lines: + match = parser.match(line) + if match: + if match.group('nuisance') != nuisance: + continue + pull = float(match.group('pull').replace('* ','').replace('! ','')) + constraint = float(match.group('constraint')) + max_dev = max(abs(pull), abs(constraint)) + new_dev = abs(1-prefit)*max_dev + 1 + print '%s %s %.2f' % (header, nuisance, new_dev) + diff --git a/wh/jobid.sh b/wh/jobid.sh index 9a98b4dc..fab0f77a 100644 --- a/wh/jobid.sh +++ b/wh/jobid.sh @@ -1,4 +1,5 @@ # Central definition of current job IDs -export jobid7='2013-Jul-04-7TeV' +export jobid7='2013-Sept-02-7TeV' +#export jobid7='2013-Sept-02' export jobid8='2013-Jul-03-8TeV' diff --git a/wh/limits.sh b/wh/limits.sh index 21c72f49..b36e2571 100755 --- a/wh/limits.sh +++ b/wh/limits.sh @@ -6,16 +6,10 @@ set -o nounset set -o errexit source jobid.sh -#export jobid=$jobid7 -#rake cards -#rake limits -#rake harvest_limits -#rake plot_limits -#rake compare_limits +export jobid=$jobid7 +rake f3_postfit_plots +rake compare_limits -export jobid=$jobid8 -rake cards -rake limits -rake harvest_limits -rake plot_limits -rake compare_limits \ No newline at end of file +export jobid=$jobid8 #'2013-Jun-30-8TeV' # +rake f3_postfit_plots +rake compare_limits diff --git a/wh/mcCorrectors.py b/wh/mcCorrectors.py index 9f29d16b..c49e5939 100644 --- a/wh/mcCorrectors.py +++ b/wh/mcCorrectors.py @@ -8,6 +8,13 @@ is7TeV = bool('7TeV' in os.environ['jobid']) print "Is 7TeV:", is7TeV + +######################################################################## +## +## PILE UP CORRECTIONS +## +######################################################################## + # Make PU corrector from expected data PU distribution # PU corrections .root files from pileupCalc.py pu_distributions = { @@ -20,24 +27,6 @@ print "Using S6_600bins PU weights for HWW3l" mc_pu_tag = 'S6_600bins' -#Makes appropriate correction function for electrons or muons according to run period -mu_pog_2011_id = MuonPOGCorrections.make_muon_pog_PFTight_2011() -mu_pog_2011_iso = MuonPOGCorrections.make_muon_pog_PFRelIsoDB02_2011() -muon_pog_IsoID = (lambda pt, eta: mu_pog_2011_id(pt,eta)*mu_pog_2011_iso(pt,eta)) if is7TeV else H2TauCorrections.correct_mu_idiso_2012 -electron_corrections = H2TauCorrections.correct_e_idiso_2011 if is7TeV else H2TauCorrections.correct_e_idiso_2012 - -#scale factors for double muon trigger, too messy to make only in one line, the function will take care of this -# takes etas of muons -muon_pog_Mu17Mu8_2011 = MuonPOGCorrections.muon_pog_Mu17Mu8_eta_eta_2011 -muon_pog_Mu17Mu8_Mu17_2012 = MuonPOGCorrections.make_muon_pog_Mu17Mu8_Mu17_2012() -muon_pog_Mu17Mu8_Mu8_2012 = MuonPOGCorrections.make_muon_pog_Mu17Mu8_Mu8_2012() - -#Scale factors for mueg triggers, that's better -correct_mueg_mu = H2TauCorrections.correct_mueg_mu_2011 if is7TeV else H2TauCorrections.correct_mu_idiso_2012 -correct_mueg_e = H2TauCorrections.correct_mueg_e_2011 if is7TeV else H2TauCorrections.correct_mueg_e_2012 - -#Double electrons does NOT need scale factors - def make_puCorrector(dataset, kind=None): 'makes PU reweighting according to the pu distribution of the reference data and the MC, MC distribution can be forced' if not kind: @@ -48,7 +37,55 @@ def make_puCorrector(dataset, kind=None): raise KeyError('dataset not present. Please check the spelling or add it to mcCorrectors.py') ## def force_pu_distribution(kind): ## pu_corrector = PileupWeight.PileupWeight( kind, *pu_distributions) - + + + +######################################################################## +## +## TRIGGER +## +######################################################################## + +#DOUBLE MU +muon_pog_Mu17Mu8_2011 = MuonPOGCorrections.muon_pog_Mu17Mu8_eta_eta_2011 +#muon_pog_Mu17Mu8_Mu17_2012 = MuonPOGCorrections.make_muon_pog_Mu17Mu8_Mu17_2012() +#muon_pog_Mu17Mu8_Mu8_2012 = MuonPOGCorrections.make_muon_pog_Mu17Mu8_Mu8_2012() +muon_h2tau_Mu17Mu8_2012 = H2TauCorrections.correct_double_muon_trg_2012 + +def double_muon_trigger(row,m1,m2): + 'makes scale factor for double mu trigger' + if is7TeV: + return muon_pog_Mu17Mu8_2011(getattr(row, '%sEta' % m1), getattr(row, '%sEta' % m2) ) + else: + return muon_h2tau_Mu17Mu8_2012(getattr(row, '%sPt' % m1), getattr(row, '%sEta' % m1), getattr(row, '%sPt' % m2), getattr(row, '%sEta' % m2)) + + +#MUEG +correct_mueg_mu = H2TauCorrections.correct_mueg_mu_2011 if is7TeV else H2TauCorrections.correct_mueg_mu_2012 +correct_mueg_e = H2TauCorrections.correct_mueg_e_2011 if is7TeV else H2TauCorrections.correct_mueg_e_2012 + +#Double electrons scale factors +correct_double_electron = H2TauCorrections.correct_double_electron_trg_2011 if is7TeV else H2TauCorrections.correct_double_electron_trg_2012 + +def double_electron_trigger(row): + return correct_double_electron( row.e1Pt, row.e1Eta, row.e2Pt, row.e2Eta ) + + + +######################################################################## +## +## ID/ISO +## +######################################################################## + + +#Makes appropriate correction function for electrons or muons according to run period +mu_pog_2011_id = MuonPOGCorrections.make_muon_pog_PFTight_2011() +mu_pog_2011_iso = MuonPOGCorrections.make_muon_pog_PFRelIsoDB02_2011() +muon_pog_IsoID = (lambda pt, eta: mu_pog_2011_id(pt,eta)*mu_pog_2011_iso(pt,eta)) if is7TeV else H2TauCorrections.correct_mu_idiso_2012 +electron_corrections = H2TauCorrections.correct_e_idiso_2011 if is7TeV else H2TauCorrections.correct_e_idiso_2012 +electron_tight_corrections = H2TauCorrections.correct_e_TIGHTidiso_2011 if is7TeV else H2TauCorrections.correct_e_TIGHTidiso_2012 + def get_muon_corrections(row,*args): 'makes corrections to iso and id of muons' @@ -69,12 +106,35 @@ def get_electron_corrections(row,*args): ret *= electron_corrections(pt,abseta) return ret -def double_muon_trigger(row,m1,m2): - 'makes scale factor for double mu trigger' - if is7TeV: - return muon_pog_Mu17Mu8_2011(getattr(row, '%sEta' % m1), getattr(row, '%sEta' % m2) ) - else: - f1 = muon_pog_Mu17Mu8_Mu17_2012(getattr(row, '%sPt' % m1), getattr(row, '%sEta' % m1)) - f2 = muon_pog_Mu17Mu8_Mu8_2012(getattr(row, '%sPt' % m2), getattr(row, '%sEta' % m2)) - return f1*f2 + + + +######################################################################## +## +## MC MATCHING +## +######################################################################## + + +from FinalStateAnalysis.PlotTools.decorators import memo + +@memo +def getVar(name, var): + return name+var + +def match_mc_object(row, obj_name): + '''check that muons are real muons, electrons are real electrons, + had taus are real had taus and little furry creatures from alpha + centauri are real little furry creatures form alpha centauri''' + if obj_name[0] == 'm': + return (abs( getattr(row, getVar(obj_name, 'GenPdgId')) ) == 13) + elif obj_name[0] == 'e': + return (abs( getattr(row, getVar(obj_name, 'GenPdgId')) ) == 11) + elif obj_name[0] == 't': + return ( getattr(row, getVar(obj_name, 'GenDecayMode')) > -1) + return False + +def mc_matching(row, *args): + 'matches object to the gen-level object' + return all( match_mc_object(row, i) for i in args ) diff --git a/wh/optimizer.py b/wh/optimizer.py index bb786e02..be16d9f1 100644 --- a/wh/optimizer.py +++ b/wh/optimizer.py @@ -6,23 +6,17 @@ RUN_OPTIMIZATION = ('RUN_OPTIMIZATION' in os.environ) and eval(os.environ['RUN_OPTIMIZATION']) lep_id = [ - 'eid12Tight_idiso02', 'eid12Tight_h2taucuts', 'eid12Tight_h2taucuts020', - 'eid12Loose_idiso02', - 'eid12Loose_h2taucuts', - 'eid12Loose_h2taucuts020', - 'eid12Medium_idiso02', + #'eid12Loose_h2taucuts', + #'eid12Loose_h2taucuts020', 'eid12Medium_h2taucuts', 'eid12Medium_h2taucuts020' ] \ if RUN_OPTIMIZATION else \ [ - 'eid12Medium_idiso02', - 'eid12Loose_idiso02', - 'eid12Tight_idiso02', - 'eid12Loose_h2taucuts', - 'eid12Loose_h2taucuts020', + #'eid12Loose_h2taucuts', + #'eid12Loose_h2taucuts020', 'eid12Tight_h2taucuts', 'eid12Medium_h2taucuts020', 'eid12Medium_h2taucuts' @@ -81,8 +75,8 @@ } else: grid_search['MMT'] = { - 'leading_iso' : 'eid12Loose_h2taucuts', - 'subleading_iso' : 'eid12Loose_h2taucuts020', + 'leading_iso' : 'eid12Medium_h2taucuts', + 'subleading_iso' : 'eid12Medium_h2taucuts020', 'LT' : 50, 'tauID' : None, 'tauPT' : 0, diff --git a/wh/plot.sh b/wh/plot.sh index 2ab705df..43a0b0ac 100755 --- a/wh/plot.sh +++ b/wh/plot.sh @@ -5,24 +5,27 @@ set -o nounset set -o errexit source jobid.sh -#export jobid=$jobid7 -#rake plot_zee -#rake plot_eet -# -#rake plot_em -#rake plot_emt -# -#rake plot_zmm -#rake plot_mmt -#python plots_for_prepp.py -# +export jobid=$jobid7 +echo $jobid +python WHPlotterMMT.py +python WHPlotterEMT.py +python WHPlotterEET.py + +python PlotControlZMM.py +python PlotControlEM.py +python PlotControlZEE.py + +python plots_for_prepp_kNN.py + export jobid=$jobid8 -#rake plot_zee -rake plot_eet -# -#rake plot_em -rake plot_emt -# -#rake plot_zmm -rake plot_mmt -python plots_for_prepp.py +echo $jobid +python WHPlotterMMT.py +python WHPlotterEMT.py +python WHPlotterEET.py + +python PlotControlZMM.py +python PlotControlEM.py +python PlotControlZEE.py + +python plots_for_prepp_kNN.py + diff --git a/wh/plots_for_prepp_kNN.py b/wh/plots_for_prepp_kNN.py new file mode 100644 index 00000000..b7d44d8f --- /dev/null +++ b/wh/plots_for_prepp_kNN.py @@ -0,0 +1,206 @@ +from rootpy.utils import asrootpy +from FinalStateAnalysis.Utilities.rootbindings import ROOT +import os +from FinalStateAnalysis.PlotTools.RebinView import RebinView +import rootpy.plotting as plotting +import logging +import sys + +logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) + +def round_to_ints(histo): + new = histo.Clone() + new.Reset() + for bin in range(histo.GetNbinsX()+1): + nentries = ROOT.TMath.Nint(histo.GetBinContent(bin)) \ + if histo.GetBinContent(bin) >= 0 else 0 + centerx = histo.GetXaxis().GetBinCenter(bin) + for _ in range(nentries): + new.Fill(centerx) + return new + + +ROOT.gROOT.SetBatch(True) +ROOT.gROOT.SetStyle("Plain") +ROOT.gStyle.SetOptTitle(0) +ROOT.gStyle.SetOptStat(0) + +jobid = os.environ['jobid'] + +rebinPt = ( + [10,12,15,20,25,30,35,40,45,50,60,70,100], #,150,200], + [10,12,15,20,25,30,35,40,45,50,60,70,100], #range(0,50,5)+range(50,110,10), + ) + +rebinJet = ( + [0,1,2,3,4,6,9,12], + [0,1,2,3,4,6,9,12], + #range(13), + ) + +axes = { + 'muonPt' : 'muon p_{T} (GeV)', + 'muonJetPt' : 'muon p_{T}^{Jet} (GeV)', + 'numJets20' : '# Jets', + 'electronPt' : 'electron p_{T} (GeV)', + 'electronJetPt' : 'electron p_{T}^{Jet} (GeV)', +} + +sources = { + ##MMT + 'm_mmt_subleading_kNN' : { + 'vars' : {'muonPt':rebinPt, 'muonJetPt':rebinPt, 'numJets20':rebinJet}, + 'wjets' : 'results/%s/fakerate_fits/mm_wjets_pt10_h2taucuts020_muonInfo_k50.data.kNN.root' % jobid, + 'qcd' : 'results/%s/fakerate_fits/mm_qcd_pt10_h2taucuts020_muonInfo_k50.data.kNN.root' % jobid, + }, + 'm_mmt_leading_kNN' : { + 'vars' : {'muonPt':rebinPt, 'muonJetPt':rebinPt, 'numJets20':rebinJet}, + 'wjets' : 'results/%s/fakerate_fits/mm_wjets_pt10_h2taucuts_muonInfo_k50.data.kNN.root' % jobid, + 'qcd' : 'results/%s/fakerate_fits/mm_qcd_pt10_h2taucuts_muonInfo_k50.data.kNN.root' % jobid, + }, + + #EMT + 'm_emt_kNN' : { + 'vars' : {'muonPt':rebinPt, 'muonJetPt':rebinPt, 'numJets20':rebinJet}, + 'wjets' : 'results/%s/fakerate_fits/em_Mwjets_pt10_h2taucuts_muonInfo_k50.data.kNN.root' % jobid, + 'qcd' : 'results/%s/fakerate_fits/em_Mqcd_pt10_h2taucuts_muonInfo_k50.data.kNN.root' % jobid, + }, + 'e_emt_kNN' : { + 'vars' : {'electronPt':rebinPt, 'electronJetPt':rebinPt, 'numJets20':rebinJet}, + 'wjets' : 'results/%s/fakerate_fits/em_wjets_pt10_eid12Medium_h2taucuts_electronInfo_k50.data.kNN.root' % jobid, + 'qcd' : 'results/%s/fakerate_fits/em_qcd_pt10_eid12Medium_h2taucuts_electronInfo_k50.data.kNN.root' % jobid, + }, + + #EET + #'e_eet_subleading_kNN' : { + # 'vars' : {'electronPt':rebinPt, 'electronJetPt':rebinPt, 'numJets20':rebinJet}, + # 'wjets' : 'results/%s/fakerate_fits/ee_wjetsNoZmass_pt10_eid12Medium_h2taucuts020_electronInfo_k50.kNN.root' % jobid, + # 'qcd' : 'results/%s/fakerate_fits/ee_qcd_pt10_eid12Medium_h2taucuts020_electronInfo_k50.kNN.root' % jobid, + # }, + #'e_eet_leading_kNN' : { + # 'vars' : {'electronPt':rebinPt, 'electronJetPt':rebinPt, 'numJets20':rebinJet}, + # 'wjets' : 'results/%s/fakerate_fits/ee_wjetsNoZmass_pt10_eid12Tight_h2taucuts_electronInfo_k50.kNN.root' % jobid, + # 'qcd' : 'results/%s/fakerate_fits/ee_qcd_pt10_eid12Tight_h2taucuts_electronInfo_k50.kNN.root' % jobid, + # }, +} + +canvas = plotting.Canvas(800, 800, name='adsf', title='asdf') +canvas.SetLogy(True) + +#from pdb import set_trace; set_trace() +for output, info in sources.iteritems(): + logging.info("analyzing %s" % output) + wjets_name = info['wjets'] + #qcd_name = info['qcd'] + variables = info['vars'] + + wjets_file = ROOT.TFile.Open(wjets_name) + logging.debug('filename: %s instance: %s' % (wjets_name, wjets_file.__repr__())) + #qcd_file = ROOT.TFile.Open(qcd_name) + #logging.debug('filename: %s instance: %s' % (qcd_name, qcd_file.__repr__())) + + for var, rebins in variables.iteritems(): + logging.debug('variable %s' % var) + ##### + #WJets fake rate + ##### + wjets_view = RebinView(wjets_file, rebins[0]) + wjets_eff = asrootpy( + ROOT.TGraphAsymmErrors( + round_to_ints( wjets_view.Get('%s_pass' % var)), + round_to_ints( wjets_view.Get('%s_all' % var)) + ) + ) + wjets_eff.SetTitle('WJets fake rate') + wjets_eff.markerstyle = 20 + wjets_eff.markercolor = ROOT.EColor.kBlue + + ##### + #QCD fake rate + ##### + #qcd_view = RebinView(qcd_file, rebins[0]) + #qcd_eff = asrootpy( + # ROOT.TGraphAsymmErrors( + # round_to_ints(qcd_view.Get('%s_pass' % var)), + # round_to_ints(qcd_view.Get('%s_all' % var)) + # ) + #) + #qcd_eff.SetTitle('QCD fake rate') + #qcd_eff.markerstyle = 20 + #qcd_eff.markercolor = ROOT.EColor.kRed + + + ##### + #WJets kNN output + ##### + wjets_view = RebinView(wjets_file, rebins[1]) + wjets_estimate = asrootpy( + wjets_view.Get('%s_estimate' % var) + ) + wjets_estimate_denom = asrootpy( + wjets_view.Get('%s_estimate_all' % var) + ) + wjets_estimate.Divide(wjets_estimate_denom) + wjets_estimate.SetTitle('WJets kNN output') + wjets_estimate.linecolor = ROOT.kBlue + wjets_estimate.linewidth = 2 + wjets_estimate.legendstyle = 'l' + wjets_estimate.fillstyle = 0 + wjets_estimate.drawstyle = 'hist' + + ##### + #QCD kNN output + ##### + #qcd_view = RebinView(qcd_file, rebins[1]) + #qcd_estimate = asrootpy( + # qcd_view.Get('%s_estimate' % var) + #) + #qcd_estimate_denom = asrootpy( + # qcd_view.Get('%s_estimate_all' % var) + #) + #qcd_estimate.Divide(qcd_estimate_denom) + #qcd_estimate.SetTitle('QCD kNN output') + #qcd_estimate.linecolor = ROOT.kRed + #qcd_estimate.linewidth = 2 + #qcd_estimate.fillstyle = 0 + #qcd_estimate.legendstyle = 'l' + #qcd_estimate.drawstyle = 'hist' + + canvas.SetGrid() + + wjets_estimate.GetXaxis().SetTitle(axes[var]) + wjets_estimate.GetYaxis().SetTitle('fake rate') + wjets_estimate.GetYaxis().SetRangeUser(10**-2,1.) + for axis in [wjets_estimate.GetXaxis(), wjets_estimate.GetYaxis()]: + axis.SetLabelSize( axis.GetLabelSize()*0.6 ) + axis.SetTitleSize( axis.GetTitleSize()*0.6 ) + #axis.SetTitleOffset( axis.GetTitleOffset()*0.6 ) + + wjets_estimate.Draw() + #qcd_estimate.Draw('same') + + wjets_eff.Draw('P SAME') + #qcd_eff.Draw('P SAME') + + ##### + #Legend + ##### + legend = plotting.Legend(2, rightmargin=0.02, topmargin=0.05, leftmargin=0.45) + legend.AddEntry(wjets_estimate) + #legend.AddEntry(qcd_estimate) + legend.AddEntry(wjets_eff) + #legend.AddEntry(qcd_eff) + legend.SetTextSize(0.5*legend.GetTextSize()) + legend.SetEntrySeparation(0.0) + legend.SetMargin(0.35) + legend.Draw() + + canvas.Update() + canvas.SetLogy(True) + print "saving results/%s/fakerate_fits/%s_%s.pdf(png)" % (jobid, output, var) + canvas.SaveAs('results/%s/fakerate_fits/%s_%s.pdf' % (jobid, output, var)) + canvas.SaveAs('results/%s/fakerate_fits/%s_%s.png' % (jobid, output, var)) + + + wjets_file.Close() + #qcd_file.Close() diff --git a/wh/print_evt_list.py b/wh/print_evt_list.py new file mode 100755 index 00000000..6fd69361 --- /dev/null +++ b/wh/print_evt_list.py @@ -0,0 +1,15 @@ +#! /bin/env python + +import ROOT +import sys + +tfile_path = sys.argv[-2] +tree_path = sys.argv[-1] + +tfile = ROOT.TFile.Open(tfile_path) +tree = tfile.Get(tree_path) + +for row in tree: + print '%i:%i:%i' % (row.run, row.lumi, row.evt) + + diff --git a/wh/run.sh b/wh/run.sh index 0b0ba5e8..7ef7c979 100755 --- a/wh/run.sh +++ b/wh/run.sh @@ -8,25 +8,27 @@ source jobid.sh export jobid=$jobid8 rake fakerates -rake fits - -#rake mmcontrol -#rake emcontrol -rake eecontrol +rake charge_fakes +rake kNN rake mmt rake emt rake eet -#export jobid=$jobid7 -#rake fakerates -#rake fits +rake mmcontrol +rake emcontrol +rake eecontrol -#rake mmcontrol -#rake emcontrol -#rake eecontrol +export jobid=$jobid7 +rake fakerates +rake charge_fakes +rake kNN +#rake fits -#rake mmt -#rake emt -#rake eet +rake mmt +rake emt +rake eet +rake mmcontrol +rake emcontrol +rake eecontrol diff --git a/wh/setup.sh b/wh/setup.sh index 3c60d84a..5aabb3e8 100755 --- a/wh/setup.sh +++ b/wh/setup.sh @@ -1,6 +1,9 @@ #!/bin/bash # Setup the cython proxies, find input ntuple files, and compute luminosity. +export OVERRIDE_META_TREE_data_DoubleElectron='ee/metaInfo' +export OVERRIDE_META_TREE_data_MuEG='em/metaInfo' +export IGNORE_LUMI_ERRORS=1 source jobid.sh export jobid=$jobid7 @@ -19,7 +22,7 @@ for file in $(ls inputs/$jobid/WZJetsTo3LNu*); do done export jobid=$jobid8 -export datasrc=$(ls -d /scratch/*/data/$jobid | awk -F$jobid '{print $1}') +export datasrc=/hdfs/store/user/$USER/$jobid #$(ls -d /scratch/*/data/$jobid | awk -F$jobid '{print $1}') #./make_proxies.sh for dir in $datasrc; do echo $dir @@ -33,6 +36,10 @@ for file in $(ls inputs/$jobid/WZJetsTo3LNu*); do cp -uv $file $newname done +unset OVERRIDE_META_TREE_data_DoubleElectron +unset OVERRIDE_META_TREE_data_MuEG +unset IGNORE_LUMI_ERRORS + # Use the 7TeV WH samples for 8TeV #pushd inputs/$jobid/ # Symlink the list of input files and the counts of the number of events.