def Direct_Estimator(var, cut, year): from root_numpy import root2array, fill_hist, array2root import numpy.lib.recfunctions as rfn ### Preliminary Operations ### treeRead = not cut in [ "nnqq", "en", "enqq", "mn", "mnqq", "ee", "eeqq", "mm", "mmqq", "em", "emqq", "qqqq" ] # Read from tree channel = cut unit = '' if "GeV" in variable[var]['title']: unit = ' GeV' isBlind = BLIND and 'SR' in channel isAH = False #'qqqq' in channel or 'hp' in channel or 'lp' in channel showSignal = False if 'SB' in cut or 'TR' in cut else True #'SR' in channel or channel=='qqqq'#or len(channel)==5 stype = "HVT model B" if len(sign) > 0 and 'AZh' in sign[0]: stype = "2HDM" elif len(sign) > 0 and 'monoH' in sign[0]: stype = "Z'-2HDM m_{A}=300 GeV" if treeRead: for k in sorted(alias.keys(), key=len, reverse=True): if BTAGGING == 'semimedium': if k in cut: cut = cut.replace(k, aliasSM[k]) else: if k in cut: cut = cut.replace( k, alias[k].format(WP=working_points[BTAGGING])) print "Plotting from", ("tree" if treeRead else "file"), var, "in", channel, "channel with:" print " cut :", cut if var == 'jj_deltaEta_widejet': if "jj_deltaEta_widejet<1.1 && " in cut: print print "omitting jj_deltaEta_widejet<1.1 cut to draw the deltaEta distribution" print cut = cut.replace("jj_deltaEta_widejet<1.1 && ", "") else: print print "no 'jj_deltaEta_widejet<1.1 && ' in the cut string detected, so it cannot be ommited explicitly" print ### Create and fill MC histograms ### # Create dict file = {} tree = {} hist = {} ### Create and fill MC histograms ### for i, s in enumerate(back + sign): if True: #FIXME if variable[var]['nbins'] > 0: hist[s] = TH1F( s, ";" + variable[var]['title'] + ";Events / ( " + str( (variable[var]['max'] - variable[var]['min']) / variable[var]['nbins']) + unit + " );" + ('log' if variable[var]['log'] else ''), variable[var]['nbins'], variable[var]['min'], variable[var]['max']) else: hist[s] = TH1F( s, ";" + variable[var]['title'] + ";Events" + ('log' if variable[var]['log'] else ''), len(variable[var]['bins']) - 1, array('f', variable[var]['bins'])) hist[s].Sumw2() for j, ss in enumerate(sample[s]['files']): if not 'data' in s: if year == "run2" or year in ss: arr = root2array( NTUPLEDIR + ss + ".root", branches=[ var, "jpt_1", "jpt_2", "eventWeightLumi", "TMath::Abs(jflavour_1)==5 && TMath::Abs(jflavour_2)==5", "TMath::Abs(jflavour_1)==5 && TMath::Abs(jflavour_2)!=5", "TMath::Abs(jflavour_1)!=5 && TMath::Abs(jflavour_2)==5", "TMath::Abs(jflavour_1)!=5 && TMath::Abs(jflavour_2)!=5" ], selection=cut if len(cut) > 0 else "") print "imported " + NTUPLEDIR + ss + ".root" arr.dtype.names = [ var, "jpt_1", "jpt_2", "eventWeightLumi", "bb", "bq", "qb", "qq" ] MANtag_eff1 = np.array(map(MANtag_eff, arr["jpt_1"])) MANtag_eff2 = np.array(map(MANtag_eff, arr["jpt_2"])) MANtag_mis1 = np.array(map(MANtag_mis, arr["jpt_1"])) MANtag_mis2 = np.array(map(MANtag_mis, arr["jpt_2"])) MANtag_weight = np.multiply( arr["eventWeightLumi"], np.multiply(arr['bb'], np.multiply(MANtag_eff1, MANtag_eff2)) + np.multiply( arr['bq'], np.multiply(MANtag_eff1, MANtag_mis2)) + np.multiply(arr['qb'], np.multiply(MANtag_mis1, MANtag_eff2)) + np.multiply(arr['qq'], np.multiply(MANtag_mis1, MANtag_mis2))) fill_hist(hist[s], arr[var], weights=MANtag_weight) deepCSV_eff1 = np.array(map(deepCSV_eff, arr["jpt_1"])) deepCSV_eff2 = np.array(map(deepCSV_eff, arr["jpt_2"])) deepCSV_mis1 = np.array(map(deepCSV_mis, arr["jpt_1"])) deepCSV_mis2 = np.array(map(deepCSV_mis, arr["jpt_2"])) deepCSV_weight = np.multiply( arr["eventWeightLumi"], np.multiply( arr['bb'], np.multiply(deepCSV_eff1, deepCSV_eff2)) + np.multiply( arr['bq'], np.multiply(deepCSV_eff1, deepCSV_mis2)) + np.multiply( arr['qb'], np.multiply(deepCSV_mis1, deepCSV_eff2)) + np.multiply( arr['qq'], np.multiply(deepCSV_mis1, deepCSV_mis2))) if var == "jj_mass_widejet" and options.save and not "data" in ss: arr = rfn.append_fields(arr, "MANtag_weight", MANtag_weight, usemask=False) arr = rfn.append_fields(arr, "deepCSV_weight", deepCSV_weight, usemask=False) array2root(arr, NTUPLEDIR + "MANtag/" + ss + "_" + BTAGGING + ".root", treename="tree", mode='recreate') print "saved as", NTUPLEDIR + "MANtag/" + ss + "_" + BTAGGING + ".root" arr = None hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0) hist[s].SetFillColor(sample[s]['fillcolor']) hist[s].SetFillStyle(sample[s]['fillstyle']) hist[s].SetLineColor(sample[s]['linecolor']) hist[s].SetLineStyle(sample[s]['linestyle']) if channel.endswith('TR') and channel.replace('TR', '') in topSF: hist['TTbarSL'].Scale(topSF[channel.replace('TR', '')][0]) hist['ST'].Scale(topSF[channel.replace('TR', '')][0]) hist['BkgSum'] = hist['data_obs'].Clone( "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum") hist['BkgSum'].Reset("MICES") hist['BkgSum'].SetFillStyle(3003) hist['BkgSum'].SetFillColor(1) for i, s in enumerate(back): hist['BkgSum'].Add(hist[s]) # Create data and Bkg sum histograms if options.blind or 'SR' in channel: hist['data_obs'] = hist['BkgSum'].Clone("data_obs") hist['data_obs'].Reset("MICES") # Set histogram style hist['data_obs'].SetMarkerStyle(20) hist['data_obs'].SetMarkerSize(1.25) for i, s in enumerate(back + sign + ['BkgSum']): addOverflow(hist[s], False) # Add overflow for i, s in enumerate(sign): hist[s].SetLineWidth(3) for i, s in enumerate(sign): sample[s][ 'plot'] = True #sample[s]['plot'] and s.startswith(channel[:2]) if isAH: for i, s in enumerate(back): hist[s].SetFillStyle(3005) hist[s].SetLineWidth(2) #for i, s in enumerate(sign): # hist[s].SetFillStyle(0) if not var == "Events": sfnorm = hist[data[0]].Integral() / hist['BkgSum'].Integral() print "Applying SF:", sfnorm for i, s in enumerate(back + ['BkgSum']): hist[s].Scale(sfnorm) if BLIND and var.endswith("Mass"): for i, s in enumerate(data + back + ['BkgSum']): first, last = hist[s].FindBin(65), hist[s].FindBin(135) for j in range(first, last): hist[s].SetBinContent(j, -1.e-4) if BLIND and var.endswith("Tau21"): for i, s in enumerate(data): first, last = hist[s].FindBin(0), hist[s].FindBin(0.6) for j in range(first, last): hist[s].SetBinContent(j, -1.e-4) # Create stack if variable[var]['nbins'] > 0: bkg = THStack( "Bkg", ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events / ( " + str( (variable[var]['max'] - variable[var]['min']) / variable[var]['nbins']) + unit + " )") else: bkg = THStack("Bkg", ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events; ") for i, s in enumerate(back): bkg.Add(hist[s]) # Legend leg = TLegend(0.65, 0.6, 0.95, 0.9) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) if len(data) > 0: leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe") for i, s in reversed(list(enumerate(['BkgSum'] + back))): leg.AddEntry(hist[s], sample[s]['label'], "f") if showSignal: for i, s in enumerate(sign): if sample[s]['plot']: leg.AddEntry(hist[s], sample[s]['label'], "fl") leg.SetY1(0.9 - leg.GetNRows() * 0.05) # --- Display --- c1 = TCanvas("c1", hist.values()[0].GetXaxis().GetTitle(), 800, 800 if RATIO else 600) if RATIO: c1.Divide(1, 2) setTopPad(c1.GetPad(1), RATIO) setBotPad(c1.GetPad(2), RATIO) c1.cd(1) c1.GetPad(bool(RATIO)).SetTopMargin(0.06) c1.GetPad(bool(RATIO)).SetRightMargin(0.05) c1.GetPad(bool(RATIO)).SetTicks(1, 1) log = variable[var]['log'] #"log" in hist['BkgSum'].GetZaxis().GetTitle() if log: c1.GetPad(bool(RATIO)).SetLogy() # Draw bkg.Draw("HIST") # stack hist['BkgSum'].Draw("SAME, E2") # sum of bkg if not isBlind and len(data) > 0: hist['data_obs'].Draw("SAME, PE") # data if 'sync' in hist: hist['sync'].Draw("SAME, PE") #data_graph.Draw("SAME, PE") if showSignal: smagn = 1. #if treeRead else 1.e2 #if log else 1.e2 for i, s in enumerate(sign): # if sample[s]['plot']: hist[s].Scale(smagn) hist[s].Draw( "SAME, HIST" ) # signals Normalized, hist[s].Integral()*sample[s]['weight'] textS = drawText(0.80, 0.9 - leg.GetNRows() * 0.05 - 0.02, stype + " (x%d)" % smagn, True) #bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset()*1.075) bkg.GetYaxis().SetTitleOffset(0.9) #bkg.GetYaxis().SetTitleOffset(2.) bkg.SetMaximum((5. if log else 1.25) * max( bkg.GetMaximum(), hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) + hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin()))) #if bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25) bkg.SetMinimum( max( min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin( )), hist['data_obs'].GetMinimum()), 5.e-1) if log else 0.) if log: bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4) #bkg.GetYaxis().SetMoreLogLabels(True) bkg.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max']) #if log: bkg.SetMinimum(1) leg.Draw() #drawCMS(LUMI[year], "Preliminary") drawCMS(LUMI[year], "Work in Progress", suppressCMS=True) drawRegion('XVH' + channel, True) drawAnalysis(channel) setHistStyle(bkg, 1.2 if RATIO else 1.1) setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1) if RATIO: c1.cd(2) err = hist['BkgSum'].Clone("BkgErr;") err.SetTitle("") err.GetYaxis().SetTitle("Data / MC") err.GetYaxis().SetTitleOffset(0.9) err.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max']) for i in range(1, err.GetNbinsX() + 1): err.SetBinContent(i, 1) if hist['BkgSum'].GetBinContent(i) > 0: err.SetBinError( i, hist['BkgSum'].GetBinError(i) / hist['BkgSum'].GetBinContent(i)) setBotStyle(err) errLine = err.Clone("errLine") errLine.SetLineWidth(1) errLine.SetFillStyle(0) res = hist['data_obs'].Clone("Residues") for i in range(0, res.GetNbinsX() + 1): if hist['BkgSum'].GetBinContent(i) > 0: res.SetBinContent( i, res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i)) res.SetBinError( i, res.GetBinError(i) / hist['BkgSum'].GetBinContent(i)) if 'sync' in hist: res.SetMarkerColor(2) res.SetMarkerStyle(31) res.Reset() for i in range(0, res.GetNbinsX() + 1): x = hist['data_obs'].GetXaxis().GetBinCenter(i) if hist['sync'].GetBinContent(hist['sync'].FindBin(x)) > 0: res.SetBinContent( i, hist['data_obs'].GetBinContent( hist['data_obs'].FindBin(x)) / hist['sync'].GetBinContent(hist['sync'].FindBin(x))) res.SetBinError( i, hist['data_obs'].GetBinError( hist['data_obs'].FindBin(x)) / hist['sync'].GetBinContent(hist['sync'].FindBin(x))) setBotStyle(res) #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5) #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2) err.Draw("E2") errLine.Draw("SAME, HIST") if not isBlind and len(data) > 0: res.Draw("SAME, PE0") #res_graph.Draw("SAME, PE0") if len(err.GetXaxis().GetBinLabel( 1)) == 0: # Bin labels: not a ordinary plot drawRatio(hist['data_obs'], hist['BkgSum']) drawStat(hist['data_obs'], hist['BkgSum']) c1.Update() if gROOT.IsBatch(): if channel == "": channel = "nocut" varname = var.replace('.', '_').replace('()', '') if not os.path.exists("plots/" + channel): os.makedirs("plots/" + channel) suffix = '' if "b" in channel or 'mu' in channel: suffix += "_" + BTAGGING c1.Print("plots/MANtag_study/" + channel + "/" + varname + "_" + year + suffix + ".png") c1.Print("plots/MANtag_study/" + channel + "/" + varname + "_" + year + suffix + ".pdf") # Print table printTable(hist, sign)
def DeepCSV_pt_distribution( year): ## everything below is jsut copy&past from above from root_numpy import root2array, fill_hist, array2root import numpy.lib.recfunctions as rfn from aliases import alias_deepCSV, WP_deepCSV ### Preliminary Operations ### treeRead = True var = 'jpt_1' channel = 'preselection' cut = alias_deepCSV['preselection'] unit = '' if "GeV" in variable[var]['title']: unit = ' GeV' isBlind = BLIND and 'SR' in channel isAH = False showSignal = True stype = "HVT model B" if len(sign) > 0 and 'AZh' in sign[0]: stype = "2HDM" elif len(sign) > 0 and 'monoH' in sign[0]: stype = "Z'-2HDM m_{A}=300 GeV" if treeRead: for k in sorted(alias_deepCSV.keys(), key=len, reverse=True): if k in cut: cut = cut.replace( k, alias_deepCSV[k].format(WP=WP_deepCSV[BTAGGING][year])) print "Plotting from", ("tree" if treeRead else "file"), var, "in", channel, "channel with:" print " cut :", cut ### Create and fill MC histograms ### # Create dict file = {} tree = {} hist = {} N_signal_tot = 0. N_signal_tag = 0. ### Create and fill MC histograms ### for i, s in enumerate(back + sign): if variable[var]['nbins'] > 0: hist[s] = TH1F( s, ";jet p_{T};Events / ( " + str( (variable[var]['max'] - variable[var]['min']) / variable[var]['nbins']) + unit + " );" + ('log' if variable[var]['log'] else ''), variable[var]['nbins'], variable[var]['min'], variable[var]['max']) else: hist[s] = TH1F( s, ";jet p_{T};Events" + ('log' if variable[var]['log'] else ''), len(variable[var]['bins']) - 1, array('f', variable[var]['bins'])) hist[s].Sumw2() for j, ss in enumerate(sample[s]['files']): if not 'data' in s: if year == "run2" or year in ss: arr = root2array(NTUPLEDIR + ss + ".root", branches=["jpt_1", "eventWeightLumi"], selection=cut + " && jdeepCSV_1>" + str(WP_deepCSV[BTAGGING][year])) if 'signal' in ss.lower(): N_signal_tag += len(arr['jpt_1'][arr['jpt_1'] > 3500]) print "imported " + NTUPLEDIR + ss + ".root" fill_hist(hist[s], arr["jpt_1"], weights=arr["eventWeightLumi"]) arr = None arr = root2array(NTUPLEDIR + ss + ".root", branches=["jpt_2", "eventWeightLumi"], selection=cut + " && jdeepCSV_2>" + str(WP_deepCSV[BTAGGING][year])) print "imported " + NTUPLEDIR + ss + ".root" if 'signal' in ss.lower(): N_signal_tag += len(arr['jpt_2'][arr['jpt_2'] > 3500]) fill_hist(hist[s], arr["jpt_2"], weights=arr["eventWeightLumi"]) arr = None if 'signal' in ss.lower(): arr = root2array(NTUPLEDIR + ss + ".root", branches=["jpt_1", "eventWeightLumi"], selection=cut) N_signal_tot += len(arr['jpt_1'][arr['jpt_1'] > 3500]) arr = None arr = root2array(NTUPLEDIR + ss + ".root", branches=["jpt_2", "eventWeightLumi"], selection=cut) N_signal_tot += len(arr['jpt_2'][arr['jpt_2'] > 3500]) arr = None hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0) hist[s].SetFillColor(sample[s]['fillcolor']) hist[s].SetFillStyle(sample[s]['fillstyle']) hist[s].SetLineColor(sample[s]['linecolor']) hist[s].SetLineStyle(sample[s]['linestyle']) if channel.endswith('TR') and channel.replace('TR', '') in topSF: hist['TTbarSL'].Scale(topSF[channel.replace('TR', '')][0]) hist['ST'].Scale(topSF[channel.replace('TR', '')][0]) hist['BkgSum'] = hist['data_obs'].Clone( "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum") hist['BkgSum'].Reset("MICES") hist['BkgSum'].SetFillStyle(3003) hist['BkgSum'].SetFillColor(1) for i, s in enumerate(back): hist['BkgSum'].Add(hist[s]) # Create data and Bkg sum histograms if options.blind or 'SR' in channel: hist['data_obs'] = hist['BkgSum'].Clone("data_obs") hist['data_obs'].Reset("MICES") # Set histogram style hist['data_obs'].SetMarkerStyle(20) hist['data_obs'].SetMarkerSize(1.25) for i, s in enumerate(back + sign + ['BkgSum']): addOverflow(hist[s], False) # Add overflow for i, s in enumerate(sign): hist[s].SetLineWidth(3) for i, s in enumerate(sign): sample[s][ 'plot'] = True #sample[s]['plot'] and s.startswith(channel[:2]) if isAH: for i, s in enumerate(back): hist[s].SetFillStyle(3005) hist[s].SetLineWidth(2) #for i, s in enumerate(sign): # hist[s].SetFillStyle(0) if not var == "Events": sfnorm = hist[data[0]].Integral() / hist['BkgSum'].Integral() print "Applying SF:", sfnorm for i, s in enumerate(back + ['BkgSum']): hist[s].Scale(sfnorm) if BLIND and var.endswith("Mass"): for i, s in enumerate(data + back + ['BkgSum']): first, last = hist[s].FindBin(65), hist[s].FindBin(135) for j in range(first, last): hist[s].SetBinContent(j, -1.e-4) if BLIND and var.endswith("Tau21"): for i, s in enumerate(data): first, last = hist[s].FindBin(0), hist[s].FindBin(0.6) for j in range(first, last): hist[s].SetBinContent(j, -1.e-4) # Create stack if variable[var]['nbins'] > 0: bkg = THStack( "Bkg", ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events / ( " + str( (variable[var]['max'] - variable[var]['min']) / variable[var]['nbins']) + unit + " )") else: bkg = THStack("Bkg", ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events; ") for i, s in enumerate(back): bkg.Add(hist[s]) # Legend leg = TLegend(0.65, 0.6, 0.95, 0.9) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) if len(data) > 0: leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe") for i, s in reversed(list(enumerate(['BkgSum'] + back))): leg.AddEntry(hist[s], sample[s]['label'], "f") if showSignal: for i, s in enumerate(sign): if sample[s]['plot']: leg.AddEntry(hist[s], sample[s]['label'], "fl") leg.SetY1(0.9 - leg.GetNRows() * 0.05) # --- Display --- c1 = TCanvas("c1", hist.values()[0].GetXaxis().GetTitle(), 800, 800 if RATIO else 600) if RATIO: c1.Divide(1, 2) setTopPad(c1.GetPad(1), RATIO) setBotPad(c1.GetPad(2), RATIO) c1.cd(1) c1.GetPad(bool(RATIO)).SetTopMargin(0.06) c1.GetPad(bool(RATIO)).SetRightMargin(0.05) c1.GetPad(bool(RATIO)).SetTicks(1, 1) log = variable[var]['log'] #"log" in hist['BkgSum'].GetZaxis().GetTitle() if log: c1.GetPad(bool(RATIO)).SetLogy() # Draw bkg.Draw("HIST") # stack hist['BkgSum'].Draw("SAME, E2") # sum of bkg if not isBlind and len(data) > 0: hist['data_obs'].Draw("SAME, PE") # data if 'sync' in hist: hist['sync'].Draw("SAME, PE") #data_graph.Draw("SAME, PE") if showSignal: smagn = 1. #if treeRead else 1.e2 #if log else 1.e2 for i, s in enumerate(sign): # if sample[s]['plot']: hist[s].Scale(smagn) hist[s].Draw( "SAME, HIST" ) # signals Normalized, hist[s].Integral()*sample[s]['weight'] textS = drawText(0.80, 0.9 - leg.GetNRows() * 0.05 - 0.02, stype + " (x%d)" % smagn, True) #bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset()*1.075) bkg.GetYaxis().SetTitleOffset(0.9) #bkg.GetYaxis().SetTitleOffset(2.) bkg.SetMaximum((5. if log else 1.25) * max( bkg.GetMaximum(), hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) + hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin()))) #if bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25) bkg.SetMinimum( max( min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin( )), hist['data_obs'].GetMinimum()), 5.e-1) if log else 0.) if log: bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4) #bkg.GetYaxis().SetMoreLogLabels(True) bkg.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max']) #if log: bkg.SetMinimum(1) leg.Draw() #drawCMS(LUMI[year], "Preliminary") drawCMS(LUMI[year], "", suppressCMS=True) drawRegion('XVH' + channel, True) drawAnalysis(channel) setHistStyle(bkg, 1.2 if RATIO else 1.1) setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1) if RATIO: c1.cd(2) err = hist['BkgSum'].Clone("BkgErr;") err.SetTitle("") err.GetYaxis().SetTitle("Data / MC") err.GetYaxis().SetTitleOffset(0.9) err.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max']) for i in range(1, err.GetNbinsX() + 1): err.SetBinContent(i, 1) if hist['BkgSum'].GetBinContent(i) > 0: err.SetBinError( i, hist['BkgSum'].GetBinError(i) / hist['BkgSum'].GetBinContent(i)) setBotStyle(err) errLine = err.Clone("errLine") errLine.SetLineWidth(1) errLine.SetFillStyle(0) res = hist['data_obs'].Clone("Residues") for i in range(0, res.GetNbinsX() + 1): if hist['BkgSum'].GetBinContent(i) > 0: res.SetBinContent( i, res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i)) res.SetBinError( i, res.GetBinError(i) / hist['BkgSum'].GetBinContent(i)) if 'sync' in hist: res.SetMarkerColor(2) res.SetMarkerStyle(31) res.Reset() for i in range(0, res.GetNbinsX() + 1): x = hist['data_obs'].GetXaxis().GetBinCenter(i) if hist['sync'].GetBinContent(hist['sync'].FindBin(x)) > 0: res.SetBinContent( i, hist['data_obs'].GetBinContent( hist['data_obs'].FindBin(x)) / hist['sync'].GetBinContent(hist['sync'].FindBin(x))) res.SetBinError( i, hist['data_obs'].GetBinError( hist['data_obs'].FindBin(x)) / hist['sync'].GetBinContent(hist['sync'].FindBin(x))) setBotStyle(res) #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5) #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2) err.Draw("E2") errLine.Draw("SAME, HIST") if not isBlind and len(data) > 0: res.Draw("SAME, PE0") #res_graph.Draw("SAME, PE0") if len(err.GetXaxis().GetBinLabel( 1)) == 0: # Bin labels: not a ordinary plot drawRatio(hist['data_obs'], hist['BkgSum']) drawStat(hist['data_obs'], hist['BkgSum']) c1.Update() if gROOT.IsBatch(): if channel == "": channel = "nocut" varname = var.replace('.', '_').replace('()', '') if not os.path.exists("plots/" + channel): os.makedirs("plots/" + channel) suffix = '' if "b" in channel or 'mu' in channel: suffix += "_" + BTAGGING c1.Print("plots/MANtag_study/deepCSV_plots/pt_" + year + suffix + ".png") c1.Print("plots/MANtag_study/deepCSV_plots/pt_" + year + suffix + ".pdf") # Print table printTable(hist, sign) print 'deepCSV efficiency:', N_signal_tag / N_signal_tot
def limit2HDM(): global signals signals = range(800, 2000 + 1, 50) multF = HTOBB THEORY = ['T1', 'T2'] mass, val = fillValues("./combine/AZh/AZh_M%d.txt") Obs0s = TGraph() Exp0s = TGraph() Exp1s = TGraphAsymmErrors() Exp2s = TGraphAsymmErrors() massB, valB = fillValues("./combine/BBAZh/BBAZh_M%d.txt") Obs0sB = TGraph() Exp0sB = TGraph() Exp1sB = TGraphAsymmErrors() Exp2sB = TGraphAsymmErrors() for i, m in enumerate(mass): if not m in val: print "Key Error:", m, "not in value map" continue n = Exp0s.GetN() Obs0s.SetPoint(n, m, val[m][0] * multF) Exp0s.SetPoint(n, m, val[m][3] * multF) Exp1s.SetPoint(n, m, val[m][3] * multF) Exp1s.SetPointError(n, 0., 0., val[m][3] * multF - val[m][2] * multF, val[m][4] * multF - val[m][3] * multF) Exp2s.SetPoint(n, m, val[m][3] * multF) Exp2s.SetPointError(n, 0., 0., val[m][3] * multF - val[m][1] * multF, val[m][5] * multF - val[m][3] * multF) Obs0sB.SetPoint(n, m, valB[m][0] * multF) Exp0sB.SetPoint(n, m, valB[m][3] * multF) Exp1sB.SetPoint(n, m, valB[m][3] * multF) Exp1sB.SetPointError(n, 0., 0., valB[m][3] * multF - valB[m][2] * multF, valB[m][4] * multF - valB[m][3] * multF) Exp2sB.SetPoint(n, m, valB[m][3] * multF) Exp2sB.SetPointError(n, 0., 0., valB[m][3] * multF - valB[m][1] * multF, valB[m][5] * multF - valB[m][3] * multF) col = 629 Exp2s.SetLineWidth(2) Exp2s.SetLineStyle(1) Obs0s.SetLineWidth(3) Obs0s.SetMarkerStyle(0) Obs0s.SetLineColor(1) Exp0s.SetLineStyle(2) Exp0s.SetLineWidth(3) Exp0s.SetLineColor(1) # Exp1s.SetFillColorAlpha(col, 0.4) #kGreen+1 # Exp1s.SetLineColorAlpha(col, 0.4) # Exp2s.SetFillColorAlpha(col, 0.2) #kOrange # Exp2s.SetLineColorAlpha(col, 0.2) Exp1s.SetFillColor(417) Exp1s.SetLineColor(417) Exp2s.SetFillColor(800) Exp2s.SetLineColor(800) colB = 922 Exp2sB.SetLineWidth(2) Obs0sB.SetLineStyle(9) Obs0sB.SetLineWidth(3) Obs0sB.SetMarkerStyle(0) Obs0sB.SetLineColor(colB) Exp0sB.SetLineStyle(8) Exp0sB.SetLineWidth(3) Exp0sB.SetLineColor(colB) Exp1sB.SetFillColorAlpha(colB, 0.4) #kGreen+1 Exp1sB.SetLineColorAlpha(colB, 0.4) Exp2sB.SetFillColorAlpha(colB, 0.2) #kOrange Exp2sB.SetLineColorAlpha(colB, 0.2) Exp2s.GetXaxis().SetTitle("m_{A} (GeV)") Exp2s.GetXaxis().SetTitleSize(Exp2s.GetXaxis().GetTitleSize() * 1.25) Exp2s.GetXaxis().SetNoExponent(True) Exp2s.GetXaxis().SetMoreLogLabels(True) Exp2s.GetYaxis().SetTitle( "#sigma(A) #bf{#it{#Beta}}(A #rightarrow Zh) #bf{#it{#Beta}}(h #rightarrow bb) (fb)" ) Exp2s.GetYaxis().SetTitleOffset(1.5) Exp2s.GetYaxis().SetNoExponent(True) Exp2s.GetYaxis().SetMoreLogLabels() Theory = {} #for t in THEORY: # Theory[t] = TGraphAsymmErrors() # for m in sorted(THDM[t]['ggA'].keys()): # if m < mass[0] or m > mass[-1]: continue # Xs, Xs_Up, Xs_Down = 0., 0., 0. # Xs = THDM[t]['ggA'][m] # Xs_Up = Xs*(1.+math.sqrt((THDM['PDF']['ggA'][m][0]-1.)**2 + (THDM['QCD']['ggA'][m][0]-1.)**2)) # Xs_Down = Xs*(1.-math.sqrt((1.-THDM['PDF']['ggA'][m][1])**2 + (1.-THDM['QCD']['ggA'][m][1])**2)) # n = Theory[t].GetN() # Theory[t].SetPoint(n, m, Xs) # Theory[t].SetPointError(n, 0., 0., (Xs-Xs_Down), (Xs_Up-Xs)) # Theory[t].SetLineColor(theoryLineColor[t]) # Theory[t].SetFillColor(theoryFillColor[t]) # Theory[t].SetFillStyle(theoryFillStyle[t]) # Theory[t].SetLineWidth(2) # #Theory[t].SetLineStyle(7) c1 = TCanvas("c1", "Exclusion Limits", 800, 600) c1.cd() #SetPad(c1.GetPad(0)) c1.GetPad(0).SetTopMargin(0.06) c1.GetPad(0).SetRightMargin(0.05) c1.GetPad(0).SetLeftMargin(0.12) c1.GetPad(0).SetTicks(1, 1) c1.GetPad(0).SetLogy() Exp2s.Draw("A3") Exp1s.Draw("SAME, 3") Exp0s.Draw("SAME, L") # Exp2sB.Draw("SAME, 3") # Exp1sB.Draw("SAME, 3") Exp0sB.Draw("SAME, L") if not options.blind: Obs0s.Draw("SAME, L") Obs0sB.Draw("SAME, L") for t in THEORY: Theory[t].Draw("SAME, L3") Theory[t].Draw("SAME, L3X0Y0") #setHistStyle(Exp2s) # Exp2s.GetXaxis().SetTitleSize(0.045) # Exp2s.GetYaxis().SetTitleSize(0.04) # Exp2s.GetXaxis().SetLabelSize(0.04) # Exp2s.GetYaxis().SetLabelSize(0.04) # Exp2s.GetXaxis().SetTitleOffset(1) # Exp2s.GetYaxis().SetTitleOffset(1.25) Exp2s.GetXaxis().SetTitleSize(0.050) Exp2s.GetYaxis().SetTitleSize(0.050) Exp2s.GetXaxis().SetLabelSize(0.045) Exp2s.GetYaxis().SetLabelSize(0.045) Exp2s.GetXaxis().SetTitleOffset(0.90) Exp2s.GetYaxis().SetTitleOffset(1.25) Exp2s.GetYaxis().SetMoreLogLabels(True) Exp2s.GetYaxis().SetNoExponent(True) Exp2s.GetYaxis().SetRangeUser(0.5, 1.e3) Exp2s.GetXaxis().SetRangeUser(mass[0], mass[-1]) drawAnalysis('AZh') drawRegion('AZHsl', True) drawCMS(LUMI, "") #Preliminary #drawCMS(LUMI, "Work in Progress", suppressCMS=True) # legend leg = TLegend(0.6, 0.90, 0.99, 0.90) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) leg.SetHeader("95% CL upper limits") leg.AddEntry(None, "gg #rightarrow A #rightarrow Zh", "") #"95% CL upper limits" leg.AddEntry(Obs0s, "Observed", "l") leg.AddEntry(Exp0s, "Expected", "l") leg.AddEntry(Exp1s, "#pm 1 std. deviation", "f") leg.AddEntry(Exp2s, "#pm 2 std. deviation", "f") leg.AddEntry(None, "", "") leg.AddEntry(None, "bbA #rightarrow Zh", "") leg.AddEntry(Obs0sB, "Observed", "l") leg.AddEntry(Exp0sB, "Expected", "l") leg.SetY1(leg.GetY2() - leg.GetNRows() * 0.045) leg.Draw() # latex = TLatex() # latex.SetNDC() # latex.SetTextSize(0.040) # latex.SetTextFont(42) # latex.DrawLatex(0.65, leg.GetY1()-0.045, "cos(#beta-#alpha)=0.25, tan(#beta)=1") # legB = TLegend(0.12, 0.4-4*0.3/5., 0.65, 0.4) legB = TLegend(0.15, 0.27, 0.68, 0.27) legB.SetBorderSize(0) legB.SetFillStyle(0) #1001 legB.SetFillColor(0) for t in THEORY: legB.AddEntry(Theory[t], theoryLabel[t], "fl") legB.AddEntry(None, "cos(#beta-#alpha)=0.25, tan(#beta)=1", "") legB.SetY1(legB.GetY2() - legB.GetNRows() * 0.045) legB.Draw() c1.GetPad(0).RedrawAxis() leg.Draw() c1.Update() if not gROOT.IsBatch(): raw_input("Press Enter to continue...") c1.Print("plotsLimit/Exclusion/THDM.png") c1.Print("plotsLimit/Exclusion/THDM.pdf")
def make_plot_all_rods(error_dict, rod_dict, name): leg = TLegend(0,0,0,0) if 'lock' in name: leg = TLegend(0.18,0.85,0.50,0.55) else: leg = TLegend(0.18,0.85,0.40,0.55) leg.SetLineColor(0) leg.SetFillStyle(0) leg.SetShadowColor(0) leg.SetBorderSize(0) leg.SetNColumns(3) gStyle.SetLegendTextSize(0.045) v_hists = [] #for e,c in zip(error_bits, error_colors): for e in error_bits: h = TH1F('h'+e,'h'+e, len(rod_dict), 0, len(rod_dict)) h.SetFillStyle(1001) h.SetLineWidth(0) v_hists.append(h) v_hists[-1].SetDirectory(0) if bin(int('0x'+e, 16))[2:].zfill(4) == '0111': leg.AddEntry(v_hists[-1],'GOL 3',"f"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1011': leg.AddEntry(v_hists[-1],'GOL 2',"f"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1101': leg.AddEntry(v_hists[-1],'GOL 1',"f"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1110': leg.AddEntry(v_hists[-1],'GOL 0',"f"); else: leg.AddEntry(v_hists[-1],bin(int('0x'+e, 16))[2:].zfill(4),"f"); h = leg.GetY2()-leg.GetY1(); w = leg.GetX2()-leg.GetX1()*.6; leg.SetMargin(leg.GetNColumns()*h/(leg.GetNRows()*w)) for key,val in error_dict.items(): idx_rod = 0 for i, key2 in enumerate(rod_dict): if key2 == key[:8]: idx_rod = i v_hists[int(key[11:12], 16)].Fill(idx_rod, val) stack = THStack("stack","stack") for hist in v_hists: stack.Add(hist) if 'buff' in name: c1 = TCanvas( 'c1', 'c1', 2000, 500) else: c1 = TCanvas( 'c1', 'c1', 1000, 500) h1 = TH1F('h_1','h_1', len(rod_dict), 0, len(rod_dict)) for i, key in enumerate(rod_dict): h1.GetXaxis().SetBinLabel(i+1,key) h1.SetBinContent(i+1,rod_dict[key]) h1.GetXaxis().LabelsOption("v") h1.GetXaxis().SetTitle("ROD") h1.GetXaxis().SetTitleOffset(2.2) h1.GetYaxis().SetTitle("# of rocketio errors") h1.SetLineColor(kRed) h1.SetLineWidth(1) leg.AddEntry(h1,'total',"l"); c1.SetBottomMargin(0.23) h1.GetXaxis().SetTitle("ROD") h1.Draw("HIST") stack.Draw("PFC PLC SAME HIST") h1.Draw("SAMEHIST") AtlasStyle.ATLAS_LABEL(0.19,.88, 1, "Internal") leg.Draw() c1.Update() c1.Print("plots/"+name +".pdf") c1.Clear()
def addSys(var, cut, sys): binLow = "" binHigh = "" binName = "" if "binned" in cut: binLow = cut[cut.find("LowVal") + 6:cut.find("HighVal") - 1] binHigh = cut[cut.find("HighVal") + 7:] binName = "bin_" + binLow + "_" + binHigh cut = cut[:cut.find("binned")] channel = cut weight = "eventWeightLumi" #+ ("*stitchWeight" if any([x for x in back if x.endswith('b')]) else "") cut = selection[cut] if not binLow == "": cut = cut + " && " + var + " > " + binLow + " && " + var + " < " + binHigh weightUp = weightDown = weight varUp = varDown = var cutUp = cutDown = cut # Systematics if sys == 'CMS_scale_j': if var != "MET_sign": varUp = var.replace('pt', 'ptScaleUp') else: varUp = var.replace('sign', 'signScaleUp') if var != "MET_sign": varDown = var.replace('pt', 'ptScaleDown') else: varDown = var.replace('sign', 'signScaleDown') cutUp = cut.replace('MET_pt', 'MET_ptScaleUp') cutUp = cutUp.replace('Jets', 'JetsScaleUp') cutUp = cutUp.replace('12', '12ScaleUp') cutUp = cutUp.replace('mT>', 'mTScaleUp>') cutUp = cutUp.replace('mT2', 'mT2ScaleUp') cutDown = cut.replace('MET_pt', 'MET_ptScaleDown') cutDown = cutDown.replace('Jets', 'JetsScaleDown') cutDown = cutDown.replace('12', '12ScaleDown') cutDown = cutDown.replace('mT>', 'mTScaleDown>') cutDown = cutDown.replace('mT2', 'mT2ScaleDown') elif sys == 'CMS_res_j': if var != "MET_sign": varUp = var.replace('pt', 'ptResUp') else: varUp = var.replace('sign', 'signResUp') if var != "MET_sign": varDown = var.replace('pt', 'ptResDown') else: varDown = var.replace('sign', 'signResDown') cutUp = cut.replace('MET_pt', 'MET_ptResUp') cutUp = cutUp.replace('Jets', 'JetsResUp') cutUp = cutUp.replace('12', '12ResUp') cutUp = cutUp.replace('mT>', 'mTResUp>') cutUp = cutUp.replace('mT2', 'mT2ResUp') cutDown = cut.replace('MET_pt', 'MET_ptResDown') cutDown = cutDown.replace('Jets', 'JetsResDown') cutDown = cutDown.replace('12', '12ResDown') cutDown = cutDown.replace('mT>', 'mTResDown>') cutDown = cutDown.replace('mT2', 'mT2ResDown') elif sys == 'CMS_WqcdWeightRen': weightUp += "*WqcdWeightRenUp/WqcdWeight" weightDown += "*WqcdWeightRenDown/WqcdWeight" elif sys == 'CMS_WqcdWeightFac': weightUp += "*WqcdWeightFacUp/WqcdWeight" weightDown += "*WqcdWeightFacDown/WqcdWeight" elif sys == 'CMS_ZqcdWeightRen': weightUp += "*ZqcdWeightRenUp/ZqcdWeight" weightDown += "*ZqcdWeightRenDown/ZqcdWeight" elif sys == 'CMS_ZqcdWeightFac': weightUp += "*ZqcdWeightFacUp/ZqcdWeight" weightDown += "*ZqcdWeightFacDown/ZqcdWeight" elif sys == 'CMS_WewkWeight': weightUp += "/WewkWeight" weightDown += "" elif sys == 'CMS_ZewkWeight': weightUp += "/ZewkWeight" weightDown += "" elif sys == 'CMS_pdf': weightUp += "*PDFWeightUp/eventWeight" weightDown += "*PDFWeightDown/eventWeight" elif sys == 'CMS_HF': weightUp += "*1.20" weightDown += "*0.8" elif sys == 'CMS_eff_b': weightUp += "*bTagWeightUp/bTagWeight" weightDown += "*bTagWeightDown/bTagWeight" elif sys == 'CMS_scale_pu': weightUp += "*puWeightUp/puWeight" weightDown += "*puWeightDown/puWeight" elif sys == 'CMS_scale_top': weightUp += "/TopWeight" weightDown += "" elif sys == 'CMS_eff_trigger': weightUp += "*triggerWeightUp/triggerWeight" weightDown += "*triggerWeightDown/triggerWeight" elif sys == 'CMS_eff_e' and '2e' in cut or '1e' in channel: weightUp += "*leptonWeightUp/leptonWeight" weightDown += "*leptonWeightDown/leptonW\ eight" elif sys == 'CMS_eff_m' and '2m' in cut or '1m' in channel: weightUp += "*leptonWeightUp/leptonWeight" weightDown += "*leptonWeightDown/leptonW\ eight" elif sys == 'QCDscale_ren': weightUp += "*QCDRenWeightUp" weightDown += "*QCDRenWeightDown" elif sys == 'QCDscale_fac': weightUp += "*QCDFacWeightUp" weightDown += "*QCDFacWeightDown" # elif sys=='EWKscale_Z': weightDown += "/ZewkWeight" # elif sys=='EWKscale_W': weightDown += "/WewkWeight" else: print "Systematic", sys, "not applicable or not recognized." ### Create and fill MC histograms ### file = {} tree = {} hist = {} histUp = {} histDown = {} isBlind = BLIND and 'SR' in channel for i, s in enumerate(back + sign): tree[s] = TChain("tree") for j, ss in enumerate(sample[s]['files']): tree[s].Add(NTUPLEDIR + ss + ".root") if not binLow == "": hist[s] = TH1F( s, ";" + variable[var]['title'] + ";Events;" + ('log' if variable[var]['log'] else ''), 1, float(binLow), float(binHigh)) elif binLow == "" and variable[var]['nbins'] > 0: hist[s] = TH1F( s, ";" + variable[var]['title'] + ";Events;" + ('log' if variable[var]['log'] else ''), variable[var]['nbins'], variable[var]['min'], variable[var]['max']) else: hist[s] = TH1F(s, ";" + variable[var]['title'], len(variable[var]['bins']) - 1, array('f', variable[var]['bins'])) hist[s].Sumw2() histUp[s] = hist[s].Clone(s + 'Up') histDown[s] = hist[s].Clone(s + 'Down') redFactorString = "" redFactorValue = "" #if isBlind and 'data' not in s and options.limit: if isBlind and 'data' not in s: redFactorValue = " / 15" cutstring = ("*(" + cut + ")" if len(cut) > 0 else "") cutstringUp = ("*(" + cutUp + ")" if len(cut) > 0 else "") cutstringDown = ("*(" + cutDown + ")" if len(cut) > 0 else "") if '-' in s: cutstring = cutstring.replace( cut, cut + "&& nBQuarks==" + s.split('-')[1][0]) tree[s].Project(s, var, "(" + weight + redFactorValue + ")" + cutstring) if 'HF' not in sys or 'QCDscale' not in sys: tree[s].Project(s + 'Up', varUp, "(" + weightUp + redFactorValue + ")" + cutstring) tree[s].Project( s + 'Down', varDown, "(" + weightDown + redFactorValue + ")" + cutstring) if 'HF' in sys: if s.startswith('WJ') or s.startswith('ZJ') or s.startswith( 'DYJets'): tree[s].Project( s + 'Up', varUp, "(" + weightUp + redFactorValue + ")" + cutstringUp) tree[s].Project( s + 'Down', varDown, "(" + weightDown + redFactorValue + ")" + cutstringDown) else: tree[s].Project( s + 'Up', varUp, "(" + weight + redFactorValue + ")" + cutstringUp) tree[s].Project( s + 'Down', varDown, "(" + weight + redFactorValue + ")" + cutstringDown) if 'QCDscale' in sys: if s.startswith('WJ') or s.startswith('ZJ') or s.startswith( 'DYJets'): tree[s].Project( s + 'Up', varUp, "(" + weight + redFactorValue + ")" + cutstringUp) tree[s].Project( s + 'Down', varDown, "(" + weight + redFactorValue + ")" + cutstringDown) else: tree[s].Project( s + 'Up', varUp, "(" + weightUp + redFactorValue + ")" + cutstringUp) tree[s].Project( s + 'Down', varDown, "(" + weightDown + redFactorValue + ")" + cutstringDown) hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0) hist[s].SetLineWidth(2) histUp[s].SetLineWidth(2) histDown[s].SetLineWidth(2) hist[s].SetLineColor(1) histUp[s].SetLineColor(629) histDown[s].SetLineColor(602) # Rescale normalization for QCD scales FIXME if 'QCDscale' in sys: for s in back + sign: #['TTbar', 'TTbarSL', 'ST']: if s in hist and histUp[s].Integral( ) > 0. and histDown[s].Integral() > 0.: histUp[s].Scale(hist[s].Integral() / histUp[s].Integral()) histDown[s].Scale(hist[s].Integral() / histDown[s].Integral()) hist['BkgSum'] = hist[back[0]].Clone("BkgSum") hist['BkgSum'].Reset() histUp['BkgSum'] = hist['BkgSum'].Clone("BkgSumUp") histUp['BkgSum'].SetLineColor(629) histUp['BkgSum'].Reset() histDown['BkgSum'] = hist['BkgSum'].Clone("BkgSumDown") histDown['BkgSum'].SetLineColor(602) histDown['BkgSum'].Reset() for i, s in enumerate(back): hist['BkgSum'].Add(hist[s], 1) histUp['BkgSum'].Add(histUp[s], 1) histDown['BkgSum'].Add(histDown[s], 1) for i, s in enumerate(back + sign + ['BkgSum']): addOverflow(hist[s], False) addOverflow(histUp[s], False) addOverflow(histDown[s], False) c1 = TCanvas("c1", "Signals", 800, 600) c1.cd() gStyle.SetOptStat(0) gStyle.SetOptTitle(0) if RATIO: c1.Divide(1, 2) setTopPad(c1.GetPad(1), RATIO) setBotPad(c1.GetPad(2), RATIO) c1.cd(1) c1.GetPad(bool(RATIO)).SetTopMargin(0.06) c1.GetPad(bool(RATIO)).SetRightMargin(0.06) c1.GetPad(bool(RATIO)).SetTicks(1, 1) c1.GetPad(bool(RATIO)).SetLogy() histUp['BkgSum'].SetMaximum(histUp['BkgSum'].GetMaximum() * 5) histUp['BkgSum'].Draw("HIST") histDown['BkgSum'].Draw("SAME, HIST") hist['BkgSum'].Draw("SAME, HIST") drawCMS(-1, "Simulation", False) setHistStyle(histUp['BkgSum'], 1.2 if RATIO else 1.1) if RATIO: c1.cd(2) errUp = histUp['BkgSum'].Clone("BkgUp;") errUp.Add(hist['BkgSum'], -1) errUp.Divide(hist['BkgSum']) errUp.SetTitle("") errUp.GetYaxis().SetTitle("#frac{shifted-central}{central}") errUp.GetYaxis().SetNdivisions(503) setBotStyle(errUp) errUp.GetYaxis().SetRangeUser(-0.3, 0.3) errUp.Draw("HIST") errDown = histDown['BkgSum'].Clone("BkgDown;") errDown.Add(hist['BkgSum'], -1) errDown.Divide(hist['BkgSum']) errDown.Draw("SAME, HIST") f1 = TF1("myfunc", "[0]", -100000, 10000) f1.SetLineColor(1) f1.SetLineStyle(7) f1.SetLineWidth(1) f1.SetParameter(0, 0) f1.Draw("same") leg = TLegend(0.65, 0.80, 0.95, 0.80) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetHeader(sys.replace('CMS', '').replace('_', ' ')) leg.AddEntry(histUp['BkgSum'], "Up", "l") leg.AddEntry(hist['BkgSum'], "Central", "l") leg.AddEntry(histDown['BkgSum'], "Down", "l") leg.SetY1(0.75 - leg.GetNRows() * 0.045) c1.cd(1) leg.Draw() if options.saveplots: if not os.path.exists("plotsSys_" + options.name + "/" + channel + binName): os.makedirs("plotsSys_" + options.name + "/" + channel + binName) c1.Print("plotsSys_" + options.name + "/" + channel + binName + "/" + sys + ".png") c1.Print("plotsSys_" + options.name + "/" + channel + binName + "/" + sys + ".pdf") for i, s in enumerate(back + sign): c2 = TCanvas(s + "canvas", "Signals", 800, 600) c2.cd() gStyle.SetOptStat(0) gStyle.SetOptTitle(0) c2.GetPad(0).SetTopMargin(0.06) c2.GetPad(0).SetRightMargin(0.06) c2.GetPad(0).SetTicky(2) c2.GetPad(0).SetLogy() histUp[s].SetMaximum(histUp[s].GetMaximum() * 5) histUp[s].Draw("HIST") histDown[s].Draw("SAME, HIST") hist[s].Draw("SAME, HIST") drawCMS(-1, "Simulation", False) if options.saveplots: c2.Print("plotsSys_" + options.name + "/" + channel + binName + "/" + sys + "_" + s + ".png") c2.Print("plotsSys_" + options.name + "/" + channel + binName + "/" + sys + "_" + s + ".pdf") saveHist(histUp, channel + binName, sys + 'Up') saveHist(histDown, channel + binName, sys + 'Down') print "Added systematic", sys, "to channel", channel
def MakeLegend(can, x1=.8, y1=.8, x2=.9, y2=.9, textsize=18, ncolumns=1, totalentries=0, option='f', skip=[]): from ROOT import TLegend, TH1, gStyle, TGraph if can.GetPrimitive('pad_top'): MakeLegend(can.GetPrimitive('pad_top'), x1, y1, x2, y2, textsize, ncolumns, totalentries, skip=skip) return if CanvasEmpty(can): print 'Error: trying to make legend from canvas with 0 plots. Will do nothing.' return # # if a previous version exists from this function, delete it # if can.GetPrimitive('legend'): can.GetPrimitive('legend').Delete() leg = TLegend(x1, y1, x2, y2) leg.SetName('legend') tobject_collector.append(leg) leg.SetTextFont(43) leg.SetTextSize(textsize) leg.SetTextFont(43) leg.SetBorderSize(0) leg.SetFillStyle(0) leg.SetNColumns(ncolumns) # # Add by TH1 GetTitle() # the_primitives = can.GetListOfPrimitives() if can.GetPrimitive('pad_top'): the_primitives = can.GetPrimitive('pad_top').GetListOfPrimitives() if can.GetPrimitive('stack'): the_stack = list(reversed(list(can.GetPrimitive('stack').GetHists()))) the_primitives = the_stack + list(the_primitives) if type(option) == type(''): option = [option] * 100 total = 0 for i in the_primitives: if i.GetName() == 'stack': continue drawopt = i.GetDrawOption() if issubclass(type(i), TH1) or issubclass(type(i), TGraph): if i.GetTitle() in skip: continue leg.AddEntry(i, i.GetTitle(), option[total]) # plef total += 1 # # Add empty entries to ensure a standard layout # for i in range(100): if totalentries == 0: break if total >= totalentries: break leg.AddEntry(None, '', '') total += 1 # recipe for making roughly square boxes h = leg.GetY2() - leg.GetY1() w = leg.GetX2() - leg.GetX1() leg.SetMargin(leg.GetNColumns() * h / float(leg.GetNRows() * w)) can.cd() if can.GetPrimitive('pad_top'): can.GetPrimitive('pad_top').cd() leg.Draw() can.Modified() can.Update() return
def plot(var, cut, nm1=False): ### Preliminary Operations ### treeRead = True if not FILE else False # Read from tree channel = cut isBlind = BLIND showSignal = False if 'SB' in cut or 'TR' in cut else True # Determine explicit cut if treeRead: for k in sorted(alias.keys(), key=len, reverse=True): if k in cut: cut = cut.replace(k, alias[k]) # Determine Primary Dataset pd = [] if "isSingleMuonPhotonTrigger" in cut: pd = [x for x in sample['data_obs']['files'] if "MuonEG" in x] elif "isJPsiTrigger" in cut: pd = [x for x in sample['data_obs']['files'] if "Charmonium" in x] else: print "Cannot determine Primary Dataset." exit() print "Plotting from", ("tree" if treeRead else "file"), var, "in", channel, "channel with:" print " dataset:", pd print " cut :", cut if isBlind and "SR" in channel and var in ["H_mass"]: cut += " && ( isMC ? 1 : !(H_mass > 86 && H_mass < 96) && !(H_mass > 120 && H_mass < 130) )" ### Create and fill MC histograms ### # Create dict file = {} tree = {} hist = {} cutstring = "(eventWeightLumi)" + ("*(" + cut + ")" if len(cut) > 0 else "") ### Create and fill MC histograms ### for i, s in enumerate(data + back + sign): hist[s] = TH1F( s, ";" + variable[var]['title'] + ";Events;" + ('logx' if variable[var]['logx'] else '') + ('logy' if variable[var]['logy'] else ''), variable[var]['nbins'], variable[var]['min'], variable[var]['max']) hist[s].Sumw2() tree[s] = TChain("Events") for j, ss in enumerate(sample[s]['files']): if s in data and not ss in pd: continue if YEAR == 2016 and not ('Run2016' in ss or 'Summer16' in ss): continue if YEAR == 2017 and not ('Run2017' in ss or 'Fall17' in ss): continue if YEAR == 2018 and not ('Run2018' in ss or 'Autumn18' in ss): continue for f in os.listdir(NTUPLEDIR + '/' + ss): tree[s].Add(NTUPLEDIR + '/' + ss + '/' + f) tree[s].Project(s, var, cutstring) if not tree[s].GetTree() == None: hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast()) # jobs = [] # queue = multiprocessing.Queue() # for i, s in enumerate(data+back+sign): # for j, ss in enumerate(sample[s]['files']): # if s in data and not ss in pd: continue # if YEAR == 2016 and not ('Run2016' in ss or 'Summer16' in ss): continue # if YEAR == 2017 and not ('Run2017' in ss or 'Fall17' in ss): continue # if YEAR == 2018 and not ('Run2018' in ss or 'Autumn18' in ss): continue # if treeRead: # Project from tree ## hist[s] = loopProject(s, ss, variable[var], cutstring, True) # p = multiprocessing.Process(target=parallelProject, args=(queue, s, ss, variable[var], cutstring, )) # jobs.append(p) # p.start() # else: # Histogram written to file # hist[s] = readhist(FILE, s, var, cut) # # # Wait for all jobs to finish # for job in jobs: # h = queue.get() # if not h.GetOption() in hist: hist[h.GetOption()] = h # else: hist[h.GetOption()].Add(h) # for job in jobs: # job.join() # Histogram style for i, s in enumerate(data + back + sign): hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0) hist[s].SetFillColor(sample[s]['fillcolor']) hist[s].SetFillStyle(sample[s]['fillstyle'] if not options.norm else 0) hist[s].SetLineColor(sample[s]['linecolor']) hist[s].SetLineStyle(sample[s]['linestyle']) hist[s].SetLineWidth(sample[s]['linewidth']) ### Create Bkg Sum histogram ### hist['BkgSum'] = hist['data_obs'].Clone( "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum") hist['BkgSum'].Reset("MICES") hist['BkgSum'].SetFillStyle(3003) hist['BkgSum'].SetFillColor(1) for i, s in enumerate(back): hist['BkgSum'].Add(hist[s]) if options.norm: for i, s in enumerate(back + ['BkgSum']): hist[s].Scale(hist[data[0]].Integral() / hist['BkgSum'].Integral()) for i, s in enumerate(sign): hist[s].Scale(hist[data[0]].Integral() / hist[s].Integral()) # Create data and Bkg sum histograms # if BLIND: # or 'SR' in channel: # hist['data_obs'] = hist['BkgSum'].Clone("data_obs") # hist['data_obs'].Reset("MICES") # Set histogram style hist['data_obs'].SetMarkerStyle(20) hist['data_obs'].SetMarkerSize(1.25) # for i, s in enumerate(data+back+sign+['BkgSum']): addOverflow(hist[s], False) # Add overflow for i, s in enumerate(sign): hist[s].SetLineWidth(3) for i, s in enumerate(sign): sample[s]['plot'] = True # Create stack bkg = THStack("Bkg", ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events") for i, s in enumerate(back): bkg.Add(hist[s]) # Legend leg = TLegend(0.65, 0.6, 0.95, 0.9) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) if len(data) > 0: leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe") for i, s in reversed(list(enumerate(['BkgSum'] + back))): leg.AddEntry(hist[s], sample[s]['label'], "f") if showSignal: for i, s in enumerate(sign): if sample[s]['plot']: leg.AddEntry(hist[s], sample[s]['label'], "fl") leg.SetY1(0.9 - leg.GetNRows() * 0.04) # --- Display --- c1 = TCanvas("c1", hist.values()[0].GetXaxis().GetTitle(), 800, 800 if RATIO else 600) if RATIO: c1.Divide(1, 2) setTopPad(c1.GetPad(1), RATIO) setBotPad(c1.GetPad(2), RATIO) c1.cd(1) c1.GetPad(bool(RATIO)).SetTopMargin(0.06) c1.GetPad(bool(RATIO)).SetRightMargin(0.05) c1.GetPad(bool(RATIO)).SetTicks(1, 1) logX, logY = "logx" in hist['BkgSum'].GetZaxis().GetTitle( ), "logy" in hist['BkgSum'].GetZaxis().GetTitle() if logY: c1.GetPad(bool(RATIO)).SetLogy() if logX: c1.GetPad(bool(RATIO)).SetLogx() # Draw bkg.Draw("HIST") # stack hist['BkgSum'].Draw("SAME, E2") # sum of bkg if len(data) > 0: hist['data_obs'].Draw("SAME, PE") # data #data_graph.Draw("SAME, PE") # if showSignal: # smagn = 1. #if treeRead else 1.e2 #if logY else 1.e2 for i, s in enumerate(sign): if sample[s]['plot']: hist[s].Draw("SAME, HIST") # hist[s].Scale(smagn) # hist[s].Draw("SAME, HIST") # signals Normalized, hist[s].Integral()*sample[s]['weight'] # #textS = drawText(0.80, 0.9-leg.GetNRows()*0.05 - 0.02, stype+" (x%d)" % smagn, True) bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset() * 1.075) bkg.SetMaximum((5. if logY else 1.25) * max( bkg.GetMaximum(), hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) + hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin()))) #if bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25) bkg.SetMinimum( max( min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin( )), hist['data_obs'].GetMinimum()), 5.e-1) if logY else 0.) if logY: bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4) bkg.GetYaxis().SetMoreLogLabels(True) #if logY: bkg.SetMinimum(1) leg.Draw() drawCMS(LUMI[YEAR], "Preliminary") if channel in aliasNames: drawRegion(aliasNames[channel], True) #drawAnalysis(channel) #if nm1 and not cutValue is None: drawCut(cutValue, bkg.GetMinimum(), bkg.GetMaximum()) #FIXME #if len(sign) > 0: # if channel.startswith('X') and len(sign)>0: drawNorm(0.9-0.05*(leg.GetNRows()+1), "#sigma(X) = %.1f pb" % 1.) setHistStyle(bkg, 1.2 if RATIO else 1.1) setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1) if RATIO: c1.cd(2) if logX: c1.GetPad(2).SetLogx() err = hist['BkgSum'].Clone("BkgErr;") err.SetTitle("") err.GetYaxis().SetTitle("Data / Bkg") for i in range(1, err.GetNbinsX() + 1): err.SetBinContent(i, 1) if hist['BkgSum'].GetBinContent(i) > 0: err.SetBinError( i, hist['BkgSum'].GetBinError(i) / hist['BkgSum'].GetBinContent(i)) setBotStyle(err) errLine = err.Clone("errLine") errLine.SetLineWidth(1) errLine.SetFillStyle(0) res = hist['data_obs'].Clone("Residues") for i in range(0, res.GetNbinsX() + 1): if hist['BkgSum'].GetBinContent(i) > 0: res.SetBinContent( i, res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i)) res.SetBinError( i, res.GetBinError(i) / hist['BkgSum'].GetBinContent(i)) setBotStyle(res) #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5) #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2) err.Draw("E2") errLine.Draw("SAME, HIST") if len(data) > 0: res.Draw("SAME, PE0") #res_graph.Draw("SAME, PE0") if len(err.GetXaxis().GetBinLabel( 1)) == 0: # Bin labels: not a ordinary plot drawRatio(hist['data_obs'], hist['BkgSum']) drawStat(hist['data_obs'], hist['BkgSum']) if var in ["H_mass"]: c1.cd(bool(RATIO)) boxZ = drawBox(XZMIN, hist['data_obs'].GetMinimum(), XZMAX, hist['data_obs'].GetMaximum() / 1.30, "Z") boxH = drawBox(XHMIN, hist['data_obs'].GetMinimum(), XHMAX, hist['data_obs'].GetMaximum() / 1.30, "H") c1.Update() if True: #gROOT.IsBatch(): varname = var.replace('.', '_').replace('()', '') if not os.path.exists("plots/" + channel): os.makedirs("plots/" + channel) c1.Print("plots/" + channel + "/" + varname + ".png") c1.Print("plots/" + channel + "/" + varname + ".pdf") # Print table printTable(hist, sign) if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
def plot(var, cut, norm=False, nm1=False): ### Preliminary Operations ### doBinned = False if options.mode == "binned": doBinned = True fileRead = os.path.exists("combinedCards_" + options.name + "/fitDiagnostics_" + options.file + ".root") treeRead = not any( x == cut for x in ['0l', '1e', '1m', '2e', '2m', '1e1m', 'Gen', 'Trigger' ]) #(var in variable.keys()) # Read from tree #signal definition if fileRead: sign = ['ttDM_MChi1_MPhi200_scalar', 'tDM_MChi1_MPhi200_scalar'] #for postfit plot if not fileRead and not options.limit: sign = ['ttDM_MChi1_MPhi100_scalar', 'tDM_MChi1_MPhi100_scalar'] #for normal plotting #bkg definition if fileRead or options.limit: back = [ "QCD", "DYJetsToNuNu_HT", "DYJetsToLL_HT", "VV", "ST", "WJetsToLNu_HT", "TTbarSL" ] #for postfit or limit if (cut).find('>250') or (cut).startswith('AH'): #for hadronic selections back = [ "QCD", "DYJetsToLL_HT", "VV", "ST", "WJetsToLNu_HT", "TTbarV", "TTbar2L", "TTbar1L", "DYJetsToNuNu_HT" ] if fileRead or options.limit: back = [ "QCD", "DYJetsToLL_HT", "VV", "ST", "WJetsToLNu_HT", "TTbarSL", "DYJetsToNuNu_HT" ] #for postfit or limit binLow = "" binHigh = "" binName = "" if "binned" in cut: binLow = cut[cut.find("LowVal") + 6:cut.find("HighVal") - 1] binHigh = cut[cut.find("HighVal") + 7:] binName = "bin_" + binLow + "_" + binHigh cut = cut[:cut.find("binned")] useformula = False if 'formula' in variable[var]: useformula = True channel = cut plotdir = cut plotname = var weight = "eventWeightLumi" #*(2.2/35.9) isBlind = BLIND and ('SR' in channel or 'ps' in channel) if fileRead: isBlind = False options.saveplots = True RESIDUAL = True elif isBlind: RATIO = 0 SIGNAL = 20 else: RATIO = 4 SIGNAL = 1 RESIDUAL = False showSignal = True #('SR' in channel) cutSplit = cut.split() for s in cutSplit: if s in selection.keys(): plotdir = s cut = cut.replace(s, selection[s]) if not binLow == "": cut = cut + " && " + var + " > " + binLow + " && " + var + " < " + binHigh #if treeRead and cut in selection: cut = cut.replace(cut, selection[cut]) # Determine Primary Dataset pd = [] if any(w in cut for w in ['1l', '1m', '2m', 'isWtoMN', 'isZtoMM', 'isTtoEM']): pd += [x for x in sample['data_obs']['files'] if 'SingleMuon' in x] if any(w in cut for w in ['1l', '1e', '2e', 'isWtoEN', 'isZtoEE']): pd += [x for x in sample['data_obs']['files'] if 'SingleElectron' in x] if any(w in cut for w in ['0l', 'isZtoNN']): pd += [x for x in sample['data_obs']['files'] if 'MET' in x] if len(pd) == 0: raw_input("Warning: Primary Dataset not recognized, continue?") print "Plotting from", ("tree" if treeRead else "file"), var, "in", channel, "channel with:" print " dataset:", pd print " cut :", cut print " cut :", weight ### Create and fill MC histograms ### # Create dict file = {} tree = {} hist = {} ### Create and fill MC histograms ### for i, s in enumerate(data + back + sign): if fileRead: var = 'MET_pt' if channel.startswith('SL'): var = 'MET_sign' if channel.endswith('ZR'): var = 'FakeMET_pt' plotname = var hist[s] = TH1F( s, ";" + variable[var]['title'] + ";Events;" + ('log' if variable[var]['log'] else ''), variable[var]['nbins'], variable[var]['min'], variable[var]['max']) if doBinned: bins = np.array([]) if 'bins' in variable[var].keys(): bins = np.array(variable[var]['bins']) else: binsize = (variable[var]['max'] - variable[var]['min']) / variable[var]['nbins'] bins = np.arange(variable[var]['min'], variable[var]['max'] + binsize, binsize) bins = np.append(bins, 10000) for i in range(0, len(bins) - 1): rbin = str(bins[i]) + "_" + str(bins[i + 1]) fileName = "combinedCards_" + options.name + "/fitDiagnostics_" + options.file + ".root" if not any( t in s for t in ['data', 'tDM'] ) else "rootfiles_" + options.name + "/" + channel + "bin_" + rbin + ".root" histName = "shapes_fit_b/" + channel + "bin_" + rbin + "/" + s if not any( t in s for t in ['data', 'tDM']) else s file[s] = TFile(fileName, "READ") tmphist = file[s].Get(histName) if 'data' not in s: hist[s].SetMarkerSize(0) if tmphist: hist[s].SetBinContent(i + 1, tmphist.GetBinContent(1)) hist[s].SetBinError(i + 1, tmphist.GetBinError(1)) else: hist[s].SetBinContent(i + 1, 0.) hist[s].SetBinError(i + 1, 0.) else: fileName = "combinedCards_" + options.name + "/fitDiagnostics_" + options.file + ".root" if not s == 'data_obs' else "rootfiles_" + options.name + "/" + channel + binName + ".root" histName = "shapes_fit_b/" + channel + "/" + s if not s == 'data_obs' else s file[s] = TFile(fileName, "READ") tmphist = file[s].Get(histName) if tmphist == None: tmphist = hist[back[0]].Clone(s) tmphist.Reset("MICES") print "Histogram", histName, "not found in file", fileName if s == 'data_obs': hist[s] = tmphist else: hist[s] = hist['data_obs'].Clone(s) hist[s].SetMarkerSize(0) for i in range(tmphist.GetNbinsX() + 1): hist[s].SetBinContent(i + 1, tmphist.GetBinContent(i + 1)) elif treeRead: # Project from tree tree[s] = TChain("tree") for j, ss in enumerate(sample[s]['files']): if not 'data' in s or ('data' in s and ss in pd): tree[s].Add(NTUPLEDIR + ss + ".root") if not binLow == "": hist[s] = TH1F( s, ";" + variable[var]['title'] + ";Events;" + ('log' if variable[var]['log'] else ''), 1, float(binLow), float(binHigh)) elif binLow == "" and variable[var]['nbins'] > 0: hist[s] = TH1F( s, ";" + variable[var]['title'] + ";Events;" + ('log' if variable[var]['log'] else ''), variable[var]['nbins'], variable[var]['min'], variable[var]['max']) else: hist[s] = TH1F( s, ";" + variable[var]['title'] + ";Events;" + ('log' if variable[var]['log'] else ''), len(variable[var]['bins']) - 1, array('f', variable[var]['bins'])) hist[s].Sumw2() redFactorString = "" redFactorValue = "" #if isBlind and 'data' in s: if isBlind and 'data' in s and options.limit: redFactorString = " && Entry$ % 15 == 1" #if isBlind and 'data' not in s: if isBlind and 'data' not in s and options.limit: redFactorValue = " / 15" cutstring = "(" + weight + redFactorValue + ")" + ( "*(" + cut + redFactorString + ")" if len(cut) > 0 else "") if '-' in s: cutstring = cutstring.replace( cut, cut + "&& nBQuarks==" + s.split('-')[1][0]) if useformula == True: tree[s].Project(s, variable[var]['formula'], cutstring) else: tree[s].Project(s, var, cutstring) if not tree[s].GetTree() == None: hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast()) else: # Histogram written to file for j, ss in enumerate(sample[s]['files']): if not 'data' in s or ('data' in s and ss in pd): file[ss] = TFile(NTUPLEDIR + ss + ".root", "R") if file[ss].IsZombie(): print "WARNING: file", NTUPLEDIR + ss + ".root", "does not exist" continue tmphist = file[ss].Get(cut + "/" + var) if tmphist == None: continue if not s in hist.keys(): hist[s] = tmphist else: hist[s].Add(tmphist) if hist[s].Integral() < 0: hist[s].Scale(0) hist[s].SetFillColor(sample[s]['fillcolor']) hist[s].SetFillStyle(sample[s]['fillstyle']) hist[s].SetLineColor(sample[s]['linecolor']) hist[s].SetLineStyle(sample[s]['linestyle']) #if 'WJetsToLNu' in s and 'SL' in channel and 'WR' in channel: hist[s].Scale(1.30) #if 'TTbar' in s and 'SL' in channel and 'TR' in channel: hist[s].Scale(0.91) hist['BkgSum'] = hist[back[0]].Clone("BkgSum") hist['BkgSum'].Reset("MICES") for i, s in enumerate(back): hist['BkgSum'].Add(hist[s], 1) if fileRead: hist['PreFit'] = hist['BkgSum'].Clone("PreFit") if doBinned: for i in range(0, len(bins) - 1): rbin = str(bins[i]) + "_" + str(bins[i + 1]) tmphist = file[back[0]].Get("shapes_prefit/" + channel + "bin_" + rbin + "/" + "total_background") if tmphist: hist['PreFit'].SetBinContent(i + 1, tmphist.GetBinContent(1)) else: hist['PreFit'].SetBinContent(i + 1, 0.) else: tmphist = file[back[0]].Get("shapes_prefit/" + channel + "/" + "total_background") for i in range(tmphist.GetNbinsX() + 1): hist['PreFit'].SetBinContent(i + 1, tmphist.GetBinContent(i + 1)) addOverflow(hist['PreFit'], False) hist['PreFit'].SetLineStyle(2) hist['PreFit'].SetLineColor(617) #923 hist['PreFit'].SetLineWidth(3) hist['PreFit'].SetFillStyle(0) hist['BkgSum'].SetFillStyle(3002) hist['BkgSum'].SetFillColor(1) # Create data and Bkg sum histograms # if options.blind or 'SR' in channel: # hist['data_obs'] = hist['BkgSum'].Clone("data_obs") # hist['data_obs'].Reset("MICES") # Set histogram style hist[data[0]].SetMarkerStyle(20) hist[data[0]].SetMarkerSize(1.25) for i, s in enumerate(data + back + sign + ['BkgSum']): addOverflow(hist[s], False) # Add overflow for i, s in enumerate(sign): hist[s].SetLineWidth(3) #for i, s in enumerate(sign): sample[s]['plot'] = True#sample[s]['plot'] and s.startswith(channel[:2]) if norm: for i, s in enumerate(sign): hist[s].Scale(hist['BkgSum'].Integral() / hist[s].Integral()) # for i, s in enumerate(back): # hist[s].SetFillStyle(3005) # hist[s].SetLineWidth(2) # #for i, s in enumerate(sign): # # hist[s].SetFillStyle(0) # if not var=="Events": # sfnorm = hist[data[0]].Integral()/hist['BkgSum'].Integral() # print "Applying SF:", sfnorm # for i, s in enumerate(back+['BkgSum']): hist[s].Scale(sfnorm) if SIGNAL > 1: if not var == "Events": for i, s in enumerate(sign): hist[s].Scale(SIGNAL) # Create stack bkg = THStack("Bkg", ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events") for i, s in enumerate(back): bkg.Add(hist[s]) # Legend #leg = TLegend(0.65, 0.6, 0.95, 0.9) leg = TLegend(0.45, 0.63, 0.93, 0.92) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) leg.SetNColumns(3) leg.SetTextFont(42) if len(data) > 0: leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe") for i, s in reversed(list(enumerate(back))): leg.AddEntry(hist[s], sample[s]['label'], "f") if 'PreFit' not in hist: leg.AddEntry(hist['BkgSum'], sample['BkgSum']['label'], "f") else: leg.AddEntry(hist['BkgSum'], 'MC unc.', "l") leg.AddEntry(hist['PreFit'], sample['PreFit']['label'], "l") if showSignal: for i, s in enumerate(sign): if SIGNAL > 1: if sample[s]['plot']: leg.AddEntry(hist[s], '%s (x%d)' % (sample[s]['label'], SIGNAL), "l") else: if sample[s]['plot']: leg.AddEntry(hist[s], sample[s]['label'], "l") leg.SetY1(0.9 - leg.GetNRows() * 0.05) # --- Display --- c1 = TCanvas("c1", hist.values()[0].GetXaxis().GetTitle(), 800, 800 if RATIO else 600) if RATIO: if RESIDUAL: c1.Divide(1, 3) setFitTopPad(c1.GetPad(1), RATIO) setFitBotPad(c1.GetPad(2), RATIO) setFitResPad(c1.GetPad(3), RATIO) else: c1.Divide(1, 2) setTopPad(c1.GetPad(1), RATIO) setBotPad(c1.GetPad(2), RATIO) c1.cd(1) c1.GetPad(bool(RATIO)).SetTopMargin(0.06) c1.GetPad(bool(RATIO)).SetRightMargin(0.05) c1.GetPad(bool(RATIO)).SetTicks(1, 1) log = ("log" in hist['BkgSum'].GetZaxis().GetTitle()) if log: c1.GetPad(bool(RATIO)).SetLogy() # Draw bkg.Draw("HIST") # stack hist['BkgSum'].Draw("SAME, E2") # sum of bkg if not isBlind and len(data) > 0: graph = fixData(hist[data[0]], USEGARWOOD) graph.Draw("SAME, PE") #data_graph.Draw("SAME, PE") if 'PreFit' in hist: hist['PreFit'].Draw("SAME, HIST") if showSignal: for i, s in enumerate(sign): if sample[s]['plot']: hist[s].Draw( "SAME, HIST" ) # signals Normalized, hist[s].Integral()*sample[s]['weight'] bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset() * 1.075) bkg.SetMaximum((5. if log else 1.25) * max( bkg.GetMaximum(), hist[data[0]].GetBinContent(hist[data[0]].GetMaximumBin()) + hist[data[0]].GetBinError(hist[data[0]].GetMaximumBin()))) if len(sign) > 0 and bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum( max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()) * 1.25) bkg.SetMinimum( max( min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin( )), hist[data[0]].GetMinimum()), 5.e-1) if log else 0.) if log: #bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4) bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e3) bkg.GetYaxis().SetMoreLogLabels(True) else: bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e3) leg.Draw() if fileRead and 'SR' in channel: drawCMS(LUMI / 15., "Preliminary") else: drawCMS(LUMI, "Preliminary") drawRegion(channel, True) drawAnalysis("DM" + channel[:2]) drawOverflow() setHistStyle(bkg, 1.2 if RATIO else 1.1) setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1) if RATIO: c1.cd(2) err = hist['BkgSum'].Clone("BkgErr;") err.SetTitle("") err.GetYaxis().SetTitle("Data / Bkg") for i in range(1, err.GetNbinsX() + 1): err.SetBinContent(i, 1) if hist['BkgSum'].GetBinContent(i) > 0: err.SetBinError( i, hist['BkgSum'].GetBinError(i) / hist['BkgSum'].GetBinContent(i)) if RESIDUAL: setFitBotStyle(err) else: setBotStyle(err) errLine = err.Clone("errLine") errLine.SetLineWidth(1) errLine.SetFillStyle(0) res = hist[data[0]].Clone("Residues") for i in range(0, res.GetNbinsX() + 1): if hist['BkgSum'].GetBinContent(i) > 0: res.SetBinContent( i, res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i)) res.SetBinError( i, res.GetBinError(i) / hist['BkgSum'].GetBinContent(i)) if RESIDUAL: setFitBotStyle(res) else: setBotStyle(res) #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5) #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2) err.Draw("E2") if 'PreFit' in hist: respre = hist[data[0]].Clone("ResiduesPreFit") respre.Divide(hist['PreFit']) respre.SetLineStyle(2) respre.SetLineColor(617) #923 respre.SetLineWidth(3) respre.SetFillStyle(0) respre.Draw("SAME, HIST") errLine.Draw("SAME, HIST") if not isBlind and len(data) > 0: res.Draw("SAME, PE0") #res_graph.Draw("SAME, PE0") if len(err.GetXaxis().GetBinLabel( 1)) == 0: # Bin labels: not a ordinary plot drawRatio(hist['data_obs'], hist['BkgSum']) drawStat(hist['data_obs'], hist['BkgSum']) c1.Update() if RATIO and RESIDUAL: c1.cd(3) c1.SetGrid(1, 0) resFit = hist[data[0]].Clone("Residues") resFit.Reset("MICES") resFit.SetTitle("") #resFit.GetYaxis().SetTitle("Residuals") resFit.GetYaxis().SetTitle( "#frac{Data - Bkg}{#sqrt{#sigma_{Data}^{2}+#sigma_{Bkg}^{2}}}") for i in range(0, res.GetNbinsX() + 1): if hist['BkgSum'].GetBinContent(i) > 0: resFit.SetBinContent( i, (hist[data[0]].GetBinContent(i) - hist['BkgSum'].GetBinContent(i)) / (math.sqrt( math.pow(hist['BkgSum'].GetBinError(i), 2) + math.pow(hist[data[0]].GetBinError(i), 2)))) setFitResStyle(resFit) resFit.SetLineColor(15) resFit.SetFillColor(15) resFit.SetFillStyle(1001) resFit.Draw("HIST") resFitLine = resFit.Clone("resFitLine") resFitLine.SetLineWidth(1) resFitLine.SetFillStyle(0) resFitLine.Draw("SAME, HIST") c1.Update() if gROOT.IsBatch( ) and options.saveplots: # and (treeRead and channel in selection.keys()): AddString = "" if not os.path.exists("plots_" + options.name + "/" + plotdir): os.makedirs("plots_" + options.name + "/" + plotdir) if fileRead: if RESIDUAL: AddString = "_PostFit_Residual" else: AddString = "_PostFit" #c1.Print("plots_"+options.name+"/"+plotdir+"/"+plotname+binName+AddString+".png") c1.Print("plots_" + options.name + "/" + plotdir + "/" + plotname + binName + AddString + ".pdf") # Print table printTable(hist, sign) if not gROOT.IsBatch(): raw_input("Press Enter to continue...") if gROOT.IsBatch() and not fileRead and ( var == 'MET_pt' or (channel.startswith('SL') and var == 'MET_sign') or (channel.endswith('ZR') and var == 'FakeMET_pt')): saveHist(hist, channel + binName)
def plot(var, cut, norm=False, nm1=False): ### Preliminary Operations ### fileRead = os.path.exists(options.file) treeRead = not any(x==cut for x in ['0l', '1e', '1m', '2e', '2m', '1e1m', 'Gen', 'Trigger'])#(var in variable.keys()) # Read from tree binLow = "" binHigh = "" binName = "" if "binned" in cut: binLow = cut[cut.find("LowVal")+6:cut.find("HighVal")-1] binHigh = cut[cut.find("HighVal")+7:] binName = "bin_"+binLow+"_"+binHigh cut = cut[:cut.find("binned")] useformula = True if 'formula' in variable[var]: print variable[var]['formula'] useformula = True channel = cut plotdir = cut plotname = var weight = "eventWeightLumi" #*(2.2/35.9) isBlind = BLIND and 'SR' in channel showSignal = True#('SR' in channel) cutSplit = cut.split() for s in cutSplit: if s in selection.keys(): plotdir = s cut = cut.replace(s, selection[s]) if not binLow == "": cut = cut + " && " + var + " > " + binLow + " && " + var + " < " + binHigh #if treeRead and cut in selection: cut = cut.replace(cut, selection[cut]) # Determine Primary Dataset pd = [] print cut if any(w in cut for w in ['1l', '1m', '2m', 'isWtoMN', 'isZtoMM', 'isTtoEM']): pd += [x for x in sample['data_obs']['files'] if 'SingleMuon' in x] if any(w in cut for w in ['1l', '1e', '2e', 'isWtoEN', 'isZtoEE']): pd += [x for x in sample['data_obs']['files'] if 'SingleElectron' in x] if any(w in cut for w in ['0l', 'isZtoNN']): pd += [x for x in sample['data_obs']['files'] if 'MET' in x] if len(pd)==0: raw_input("Warning: Primary Dataset not recognized, continue?") print "Plotting from", ("tree" if treeRead else "file"), var, "in", channel, "channel with:" print " dataset:", pd print " cut :", cut print " cut :", weight ### Create and fill MC histograms ### # Create dict file = {} tree = {} hist = {} ### Create and fill MC histograms ### for i, s in enumerate(data+back+sign): if fileRead: fileName = options.file if not s=='data_obs' else "rootfiles_"+options.name+"/"+channel+binName+".root" histName = "shapes_fit_b/"+channel+"/"+s if not s=='data_obs' else s file[s] = TFile(fileName, "READ") tmphist = file[s].Get(histName) if tmphist==None: tmphist = hist[back[0]].Clone(s) tmphist.Reset("MICES") print "Histogram", histName, "not found in file", fileName if s=='data_obs': hist[s] = tmphist else: hist[s] = hist['data_obs'].Clone(s) #hist[s].Reset("MICES") hist[s].SetMarkerSize(0) for i in range(tmphist.GetNbinsX()+1): hist[s].SetBinContent(i+1, tmphist.GetBinContent(i+1)) elif treeRead: # Project from tree tree[s] = TChain("tree") for j, ss in enumerate(sample[s]['files']): if not 'data' in s or ('data' in s and ss in pd): tree[s].Add(NTUPLEDIR + ss + ".root") if not binLow == "": hist[s] = TH1F(s, ";"+variable[var]['title']+";Events;"+('log' if variable[var]['log'] else ''), 1, float(binLow), float(binHigh)) elif binLow == "" and variable[var]['nbins']>0: hist[s] = TH1F(s, ";"+variable[var]['title']+";Events;"+('log' if variable[var]['log'] else ''), variable[var]['nbins'], variable[var]['min'], variable[var]['max']) else: hist[s] = TH1F(s, ";"+variable[var]['title']+";Events;"+('log' if variable[var]['log'] else ''), len(variable[var]['bins'])-1, array('f', variable[var]['bins'])) hist[s].Sumw2() redFactorString = "" redFactorValue = "" if isBlind and 'data' in s: redFactorString = " && Entry$ % 15 == 0" if isBlind and 'data' not in s: redFactorValue = " / 15" cutstring = "("+weight+redFactorValue+")" + ("*("+cut+redFactorString+")" if len(cut)>0 else "") if '-' in s: cutstring = cutstring.replace(cut, cut + "&& nBQuarks==" + s.split('-')[1][0]) if useformula == True: tree[s].Project(s, variable[var]['formula'], cutstring) else: tree[s].Project(s, var, cutstring) if not tree[s].GetTree()==None: hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast()) else: # Histogram written to file for j, ss in enumerate(sample[s]['files']): if not 'data' in s or ('data' in s and ss in pd): file[ss] = TFile(NTUPLEDIR + ss + ".root", "R") if file[ss].IsZombie(): print "WARNING: file", NTUPLEDIR + ss + ".root", "does not exist" continue tmphist = file[ss].Get(cut+"/"+var) if tmphist==None: continue if not s in hist.keys(): hist[s] = tmphist else: hist[s].Add(tmphist) if hist[s].Integral() < 0: hist[s].Scale(0) hist[s].SetFillColor(sample[s]['fillcolor']) hist[s].SetFillStyle(sample[s]['fillstyle']) hist[s].SetLineColor(sample[s]['linecolor']) hist[s].SetLineStyle(sample[s]['linestyle']) #if 'WJetsToLNu' in s and 'SL' in channel and 'WR' in channel: hist[s].Scale(1.30) #if 'TTbar' in s and 'SL' in channel and 'TR' in channel: hist[s].Scale(0.91) hist['BkgSum'] = hist[back[0]].Clone("BkgSum") hist['BkgSum'].Reset("MICES") for i, s in enumerate(back): hist['BkgSum'].Add(hist[s], 1) if fileRead: #hist['BkgSum'] = file[back[0]].Get("shapes_fit_b/"+channel+"/"+"total_background") tmphist = file[back[0]].Get("shapes_prefit/"+channel+"/"+"total_background") hist['PreFit'] = hist['BkgSum'].Clone("PreFit") for i in range(tmphist.GetNbinsX()+1): hist['PreFit'].SetBinContent(i+1, tmphist.GetBinContent(i+1)) hist['PreFit'].SetLineStyle(2) hist['PreFit'].SetLineColor(923) hist['PreFit'].SetLineWidth(3) hist['PreFit'].SetFillStyle(0) hist['BkgSum'].SetFillStyle(3003) hist['BkgSum'].SetFillColor(1) # Create data and Bkg sum histograms # if options.blind or 'SR' in channel: # hist['data_obs'] = hist['BkgSum'].Clone("data_obs") # hist['data_obs'].Reset("MICES") # Set histogram style hist[data[0]].SetMarkerStyle(20) hist[data[0]].SetMarkerSize(1.25) for i, s in enumerate(data+back+sign+['BkgSum']): addOverflow(hist[s], False) # Add overflow for i, s in enumerate(sign): hist[s].SetLineWidth(3) #for i, s in enumerate(sign): sample[s]['plot'] = True#sample[s]['plot'] and s.startswith(channel[:2]) if norm: for i, s in enumerate(sign): hist[s].Scale(hist['BkgSum'].Integral()/hist[s].Integral()) # for i, s in enumerate(back): # hist[s].SetFillStyle(3005) # hist[s].SetLineWidth(2) # #for i, s in enumerate(sign): # # hist[s].SetFillStyle(0) # if not var=="Events": # sfnorm = hist[data[0]].Integral()/hist['BkgSum'].Integral() # print "Applying SF:", sfnorm # for i, s in enumerate(back+['BkgSum']): hist[s].Scale(sfnorm) # Create stack bkg = THStack("Bkg", ";"+hist['BkgSum'].GetXaxis().GetTitle()+";Events") for i, s in enumerate(back): bkg.Add(hist[s]) # Legend leg = TLegend(0.65, 0.6, 0.95, 0.9) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) if len(data) > 0: leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe") for i, s in reversed(list(enumerate(['BkgSum']+back))): leg.AddEntry(hist[s], sample[s]['label'], "f") if 'PreFit' in hist: leg.AddEntry(hist['PreFit'], sample['PreFit']['label'], "l") if showSignal: for i, s in enumerate(sign): if sample[s]['plot']: leg.AddEntry(hist[s], sample[s]['label'], "fl") leg.SetY1(0.9-leg.GetNRows()*0.05) # --- Display --- c1 = TCanvas("c1", hist.values()[0].GetXaxis().GetTitle(), 800, 800 if RATIO else 600) if RATIO: c1.Divide(1, 2) setTopPad(c1.GetPad(1), RATIO) setBotPad(c1.GetPad(2), RATIO) c1.cd(1) c1.GetPad(bool(RATIO)).SetTopMargin(0.06) c1.GetPad(bool(RATIO)).SetRightMargin(0.05) c1.GetPad(bool(RATIO)).SetTicks(1, 1) log = ("log" in hist['BkgSum'].GetZaxis().GetTitle()) if log: c1.GetPad(bool(RATIO)).SetLogy() # Draw bkg.Draw("HIST") # stack hist['BkgSum'].Draw("SAME, E2") # sum of bkg if not isBlind and len(data) > 0: hist[data[0]].Draw("SAME, PE") # data #data_graph.Draw("SAME, PE") if 'PreFit' in hist: hist['PreFit'].Draw("SAME, HIST") if showSignal: for i, s in enumerate(sign): if sample[s]['plot']: hist[s].Draw("SAME, HIST") # signals Normalized, hist[s].Integral()*sample[s]['weight'] bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset()*1.075) bkg.SetMaximum((5. if log else 1.25)*max(bkg.GetMaximum(), hist[data[0]].GetBinContent(hist[data[0]].GetMaximumBin())+hist[data[0]].GetBinError(hist[data[0]].GetMaximumBin()))) if len(sign) > 0 and bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25) bkg.SetMinimum(max(min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin()), hist[data[0]].GetMinimum()), 5.e-1) if log else 0.) if log: bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4) bkg.GetYaxis().SetMoreLogLabels(True) leg.Draw() drawCMS(LUMI, "Preliminary") drawRegion(channel, True) drawAnalysis("DM"+channel[:2]) drawOverflow() setHistStyle(bkg, 1.2 if RATIO else 1.1) setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1) if RATIO: c1.cd(2) err = hist['BkgSum'].Clone("BkgErr;") err.SetTitle("") err.GetYaxis().SetTitle("Data / Bkg") for i in range(1, err.GetNbinsX()+1): err.SetBinContent(i, 1) if hist['BkgSum'].GetBinContent(i) > 0: err.SetBinError(i, hist['BkgSum'].GetBinError(i)/hist['BkgSum'].GetBinContent(i)) setBotStyle(err) errLine = err.Clone("errLine") errLine.SetLineWidth(1) errLine.SetFillStyle(0) res = hist[data[0]].Clone("Residues") for i in range(0, res.GetNbinsX()+1): if hist['BkgSum'].GetBinContent(i) > 0: res.SetBinContent(i, res.GetBinContent(i)/hist['BkgSum'].GetBinContent(i)) res.SetBinError(i, res.GetBinError(i)/hist['BkgSum'].GetBinContent(i)) setBotStyle(res) #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5) #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2) err.Draw("E2") if 'PreFit' in hist: respre = hist['PreFit'].Clone("ResiduesPreFit") respre.Divide(hist['BkgSum']) respre.Draw("SAME, HIST") errLine.Draw("SAME, HIST") if not isBlind and len(data) > 0: res.Draw("SAME, PE0") #res_graph.Draw("SAME, PE0") if len(err.GetXaxis().GetBinLabel(1))==0: # Bin labels: not a ordinary plot drawRatio(hist['data_obs'], hist['BkgSum']) drawStat(hist['data_obs'], hist['BkgSum']) c1.Update() if gROOT.IsBatch() and options.saveplots: # and (treeRead and channel in selection.keys()): if not os.path.exists("plots_"+options.name+"/"+plotdir): os.makedirs("plots_"+options.name+"/"+plotdir) c1.Print("plots_"+options.name+"/"+plotdir+"/"+plotname+binName+".png") c1.Print("plots_"+options.name+"/"+plotdir+"/"+plotname+binName+".pdf") # Print table printTable(hist, sign) if not gROOT.IsBatch(): raw_input("Press Enter to continue...") if gROOT.IsBatch() and not fileRead and (var == 'MET_pt' or (channel.startswith('SL') and var == 'MET_sign') or (channel.endswith('ZR') and var == 'FakeMET_pt')): saveHist(hist, channel+binName)
def dijet(category): channel = 'bb' stype = channel isSB = True # relict from using Alberto's more complex script isData = not ISMC nTupleDir = NTUPLEDIR samples = data if isData else back pd = [] for sample_name in samples: if YEAR == 'run2': pd += sample[sample_name]['files'] else: pd += [x for x in sample[sample_name]['files'] if YEAR in x] print "datasets:", pd if not os.path.exists(PLOTDIR): os.makedirs(PLOTDIR) if BIAS: print "Running in BIAS mode" order = 0 RSS = {} X_mass = RooRealVar("jj_mass_widejet", "m_{jj}", X_min, X_max, "GeV") weight = RooRealVar("MANtag_weight", "", -1.e9, 1.e9) variables = RooArgSet(X_mass) variables.add(RooArgSet(weight)) if VARBINS: binsXmass = RooBinning(len(abins) - 1, abins) X_mass.setBinning(RooBinning(len(abins_narrow) - 1, abins_narrow)) plot_binning = RooBinning( int((X_mass.getMax() - X_mass.getMin()) / 100), X_mass.getMin(), X_mass.getMax()) else: X_mass.setBins(int((X_mass.getMax() - X_mass.getMin()) / 10)) binsXmass = RooBinning(int((X_mass.getMax() - X_mass.getMin()) / 100), X_mass.getMin(), X_mass.getMax()) plot_binning = binsXmass baseCut = "" print stype, "|", baseCut print " - Reading from Tree" treeBkg = TChain("tree") for ss in pd: if os.path.exists(nTupleDir + ss + "_" + BTAGGING + ".root"): treeBkg.Add(nTupleDir + ss + "_" + BTAGGING + ".root") else: print "found no file for sample:", ss setData = RooDataSet("setData", "Data (QCD+TTbar MC)", variables, RooFit.Cut(baseCut), RooFit.WeightVar(weight), RooFit.Import(treeBkg)) nevents = setData.sumEntries() dataMin, dataMax = array('d', [0.]), array('d', [0.]) setData.getRange(X_mass, dataMin, dataMax) xmin, xmax = dataMin[0], dataMax[0] lastBin = X_mass.getMax() if VARBINS: for b in narrow_bins: if b > xmax: lastBin = b break print "Imported", ( "data" if isData else "MC" ), "RooDataSet with", nevents, "events between [%.1f, %.1f]" % (xmin, xmax) #xmax = xmax+binsXmass.averageBinWidth() # start form next bin # 1 parameter print "fitting 1 parameter model" p1_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p1_1", "p1", 7.0, 0., 2000.) modelBkg1 = RooGenericPdf("Bkg1", "Bkg. fit (2 par.)", "1./pow(@0/13000, @1)", RooArgList(X_mass, p1_1)) normzBkg1 = RooRealVar( modelBkg1.GetName() + "_norm", "Number of background events", nevents, 0., 5. * nevents) #range dependent of actual number of events! modelExt1 = RooExtendPdf(modelBkg1.GetName() + "_ext", modelBkg1.GetTitle(), modelBkg1, normzBkg1) fitRes1 = modelExt1.fitTo(setData, RooFit.Extended(True), RooFit.Save(1), RooFit.SumW2Error(not isData), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.PrintLevel(1 if VERBOSE else -1)) fitRes1.Print() RSS[1] = drawFit("Bkg1", category, X_mass, modelBkg1, setData, binsXmass, [fitRes1], normzBkg1.getVal()) # 2 parameters print "fitting 2 parameter model" p2_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p2_1", "p1", 0., -100., 1000.) p2_2 = RooRealVar("CMS" + YEAR + "_" + category + "_p2_2", "p2", p1_1.getVal(), -100., 600.) modelBkg2 = RooGenericPdf("Bkg2", "Bkg. fit (3 par.)", "pow(1-@0/13000, @1) / pow(@0/13000, @2)", RooArgList(X_mass, p2_1, p2_2)) normzBkg2 = RooRealVar(modelBkg2.GetName() + "_norm", "Number of background events", nevents, 0., 5. * nevents) modelExt2 = RooExtendPdf(modelBkg2.GetName() + "_ext", modelBkg2.GetTitle(), modelBkg2, normzBkg2) fitRes2 = modelExt2.fitTo(setData, RooFit.Extended(True), RooFit.Save(1), RooFit.SumW2Error(not isData), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.PrintLevel(1 if VERBOSE else -1)) fitRes2.Print() RSS[2] = drawFit("Bkg2", category, X_mass, modelBkg2, setData, binsXmass, [fitRes2], normzBkg2.getVal()) # 3 parameters print "fitting 3 parameter model" p3_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p3_1", "p1", p2_1.getVal(), -2000., 2000.) p3_2 = RooRealVar("CMS" + YEAR + "_" + category + "_p3_2", "p2", p2_2.getVal(), -400., 2000.) p3_3 = RooRealVar("CMS" + YEAR + "_" + category + "_p3_3", "p3", -2.5, -500., 500.) modelBkg3 = RooGenericPdf( "Bkg3", "Bkg. fit (4 par.)", "pow(1-@0/13000, @1) / pow(@0/13000, @2+@3*log(@0/13000))", RooArgList(X_mass, p3_1, p3_2, p3_3)) normzBkg3 = RooRealVar(modelBkg3.GetName() + "_norm", "Number of background events", nevents, 0., 5. * nevents) modelExt3 = RooExtendPdf(modelBkg3.GetName() + "_ext", modelBkg3.GetTitle(), modelBkg3, normzBkg3) fitRes3 = modelExt3.fitTo(setData, RooFit.Extended(True), RooFit.Save(1), RooFit.SumW2Error(not isData), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.PrintLevel(1 if VERBOSE else -1)) fitRes3.Print() RSS[3] = drawFit("Bkg3", category, X_mass, modelBkg3, setData, binsXmass, [fitRes3], normzBkg3.getVal()) # 4 parameters print "fitting 4 parameter model" p4_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_1", "p1", p3_1.getVal(), -2000., 2000.) p4_2 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_2", "p2", p3_2.getVal(), -2000., 2000.) p4_3 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_3", "p3", p3_3.getVal(), -50., 50.) p4_4 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_4", "p4", 0.1, -50., 50.) modelBkg4 = RooGenericPdf( "Bkg4", "Bkg. fit (5 par.)", "pow(1 - @0/13000, @1) / pow(@0/13000, @2+@3*log(@0/13000)+@4*pow(log(@0/13000), 2))", RooArgList(X_mass, p4_1, p4_2, p4_3, p4_4)) normzBkg4 = RooRealVar(modelBkg4.GetName() + "_norm", "Number of background events", nevents, 0., 5. * nevents) modelExt4 = RooExtendPdf(modelBkg4.GetName() + "_ext", modelBkg4.GetTitle(), modelBkg4, normzBkg4) fitRes4 = modelExt4.fitTo(setData, RooFit.Extended(True), RooFit.Save(1), RooFit.SumW2Error(not isData), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.PrintLevel(1 if VERBOSE else -1)) fitRes4.Print() RSS[4] = drawFit("Bkg4", category, X_mass, modelBkg4, setData, binsXmass, [fitRes4], normzBkg4.getVal()) # Normalization parameters are should be set constant, but shape ones should not # if BIAS: # p1_1.setConstant(True) # p2_1.setConstant(True) # p2_2.setConstant(True) # p3_1.setConstant(True) # p3_2.setConstant(True) # p3_3.setConstant(True) # p4_1.setConstant(True) # p4_2.setConstant(True) # p4_3.setConstant(True) # p4_4.setConstant(True) normzBkg1.setConstant(True) normzBkg2.setConstant(True) normzBkg3.setConstant(True) normzBkg4.setConstant(True) #*******************************************************# # # # Fisher # # # #*******************************************************# # Fisher test with open(PLOTDIR + "/Fisher_" + category + ".tex", 'w') as fout: fout.write(r"\begin{tabular}{c|c|c|c|c}") fout.write("\n") fout.write(r"function & $\chi^2$ & RSS & ndof & F-test \\") fout.write("\n") fout.write("\hline") fout.write("\n") CL_high = False for o1 in range(1, 5): o2 = min(o1 + 1, 5) fout.write("%d par & %.2f & %.2f & %d & " % (o1 + 1, RSS[o1]["chi2"], RSS[o1]["rss"], RSS[o1]["nbins"] - RSS[o1]["npar"])) if o2 > len(RSS): fout.write(r"\\") fout.write("\n") continue #order==0 and CL = fisherTest(RSS[o1]['rss'], RSS[o2]['rss'], o1 + 1., o2 + 1., RSS[o1]["nbins"]) fout.write("CL=%.3f " % (CL)) if CL > 0.10: # The function with less parameters is enough if not CL_high: order = o1 #fout.write( "%d par are sufficient " % (o1+1)) CL_high = True else: #fout.write( "%d par are needed " % (o2+1)) if not CL_high: order = o2 fout.write(r"\\") fout.write("\n") fout.write("\hline") fout.write("\n") fout.write(r"\end{tabular}") print "saved F-test table as", PLOTDIR + "/Fisher_" + category + ".tex" #print "-"*25 #print "function & $\\chi^2$ & RSS & ndof & F-test & result \\\\" #print "\\multicolumn{6}{c}{", "Zprime_to_bb", "} \\\\" #print "\\hline" #CL_high = False #for o1 in range(1, 5): # o2 = min(o1 + 1, 5) # print "%d par & %.2f & %.2f & %d & " % (o1+1, RSS[o1]["chi2"], RSS[o1]["rss"], RSS[o1]["nbins"]-RSS[o1]["npar"]), # if o2 > len(RSS): # print "\\\\" # continue #order==0 and # CL = fisherTest(RSS[o1]['rss'], RSS[o2]['rss'], o1+1., o2+1., RSS[o1]["nbins"]) # print "%d par vs %d par CL=%f & " % (o1+1, o2+1, CL), # if CL > 0.10: # The function with less parameters is enough # if not CL_high: # order = o1 # print "%d par are sufficient" % (o1+1), # CL_high=True # else: # print "%d par are needed" % (o2+1), # if not CL_high: # order = o2 # print "\\\\" #print "\\hline" #print "-"*25 #print "@ Order is", order, "("+category+")" #order = min(3, order) #order = 2 if order == 1: modelBkg = modelBkg1 #.Clone("Bkg") modelAlt = modelBkg2 #.Clone("BkgAlt") normzBkg = normzBkg1 #.Clone("Bkg_norm") fitRes = fitRes1 elif order == 2: modelBkg = modelBkg2 #.Clone("Bkg") modelAlt = modelBkg3 #.Clone("BkgAlt") normzBkg = normzBkg2 #.Clone("Bkg_norm") fitRes = fitRes2 elif order == 3: modelBkg = modelBkg3 #.Clone("Bkg") modelAlt = modelBkg4 #.Clone("BkgAlt") normzBkg = normzBkg3 #.Clone("Bkg_norm") fitRes = fitRes3 elif order == 4: modelBkg = modelBkg4 #.Clone("Bkg") modelAlt = modelBkg3 #.Clone("BkgAlt") normzBkg = normzBkg4 #.Clone("Bkg_norm") fitRes = fitRes4 else: print "Functions with", order + 1, "or more parameters are needed to fit the background" exit() modelBkg.SetName("Bkg_" + YEAR + "_" + category) modelAlt.SetName("Alt_" + YEAR + "_" + category) normzBkg.SetName("Bkg_" + YEAR + "_" + category + "_norm") print "-" * 25 # Generate pseudo data setToys = RooDataSet() setToys.SetName("data_toys") setToys.SetTitle("Data (toys)") if not isData: print " - Generating", nevents, "events for toy data" setToys = modelBkg.generate(RooArgSet(X_mass), nevents) #setToys = modelAlt.generate(RooArgSet(X_mass), nevents) print "toy data generated" if VERBOSE: raw_input("Press Enter to continue...") #*******************************************************# # # # Plot # # # #*******************************************************# print "starting to plot" c = TCanvas("c_" + category, category, 800, 800) c.Divide(1, 2) setTopPad(c.GetPad(1), RATIO) setBotPad(c.GetPad(2), RATIO) c.cd(1) frame = X_mass.frame() setPadStyle(frame, 1.25, True) if VARBINS: frame.GetXaxis().SetRangeUser(X_mass.getMin(), lastBin) signal = getSignal( category, stype, 2000) #replacing Alberto's getSignal by own dummy function graphData = setData.plotOn(frame, RooFit.Binning(plot_binning), RooFit.Scaling(False), RooFit.Invisible()) modelBkg.plotOn(frame, RooFit.VisualizeError(fitRes, 1, False), RooFit.LineColor(602), RooFit.FillColor(590), RooFit.FillStyle(1001), RooFit.DrawOption("FL"), RooFit.Name("1sigma")) modelBkg.plotOn(frame, RooFit.LineColor(602), RooFit.FillColor(590), RooFit.FillStyle(1001), RooFit.DrawOption("L"), RooFit.Name(modelBkg.GetName())) modelAlt.plotOn(frame, RooFit.LineStyle(7), RooFit.LineColor(613), RooFit.FillColor(609), RooFit.FillStyle(1001), RooFit.DrawOption("L"), RooFit.Name(modelAlt.GetName())) if not isSB and signal[0] is not None: # FIXME remove /(2./3.) signal[0].plotOn( frame, RooFit.Normalization(signal[1] * signal[2], RooAbsReal.NumEvent), RooFit.LineStyle(3), RooFit.LineWidth(6), RooFit.LineColor(629), RooFit.DrawOption("L"), RooFit.Name("Signal")) graphData = setData.plotOn( frame, RooFit.Binning(plot_binning), RooFit.Scaling(False), RooFit.XErrorSize(0 if not VARBINS else 1), RooFit.DataError(RooAbsData.Poisson if isData else RooAbsData.SumW2), RooFit.DrawOption("PE0"), RooFit.Name(setData.GetName())) fixData(graphData.getHist(), True, True, not isData) pulls = frame.pullHist(setData.GetName(), modelBkg.GetName(), True) chi = frame.chiSquare(setData.GetName(), modelBkg.GetName(), True) #setToys.plotOn(frame, RooFit.DataError(RooAbsData.Poisson), RooFit.DrawOption("PE0"), RooFit.MarkerColor(2)) frame.GetYaxis().SetTitle("Events / ( 100 GeV )") frame.GetYaxis().SetTitleOffset(1.05) frame.Draw() #print "frame drawn" # Get Chi2 # chi2[1] = frame.chiSquare(modelBkg1.GetName(), setData.GetName()) # chi2[2] = frame.chiSquare(modelBkg2.GetName(), setData.GetName()) # chi2[3] = frame.chiSquare(modelBkg3.GetName(), setData.GetName()) # chi2[4] = frame.chiSquare(modelBkg4.GetName(), setData.GetName()) frame.SetMaximum(frame.GetMaximum() * 10) frame.SetMinimum(max(frame.GetMinimum(), 1.e-1)) c.GetPad(1).SetLogy() drawAnalysis(category) drawRegion(category, True) #drawCMS(LUMI, "Simulation Preliminary") drawCMS(LUMI, "Work in Progress", suppressCMS=True) leg = TLegend(0.575, 0.6, 0.95, 0.9) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) leg.AddEntry(setData.GetName(), setData.GetTitle() + " (%d events)" % nevents, "PEL") leg.AddEntry(modelBkg.GetName(), modelBkg.GetTitle(), "FL") #.SetTextColor(629) leg.AddEntry(modelAlt.GetName(), modelAlt.GetTitle(), "L") if not isSB and signal[0] is not None: leg.AddEntry("Signal", signal[0].GetTitle(), "L") leg.SetY1(0.9 - leg.GetNRows() * 0.05) leg.Draw() latex = TLatex() latex.SetNDC() latex.SetTextSize(0.04) latex.SetTextFont(42) if not isSB: latex.DrawLatex(leg.GetX1() * 1.16, leg.GetY1() - 0.04, "HVT model B (g_{V}=3)") # latex.DrawLatex(0.67, leg.GetY1()-0.045, "#sigma_{X} = 1.0 pb") c.cd(2) frame_res = X_mass.frame() setPadStyle(frame_res, 1.25) frame_res.addPlotable(pulls, "P") setBotStyle(frame_res, RATIO, False) if VARBINS: frame_res.GetXaxis().SetRangeUser(X_mass.getMin(), lastBin) frame_res.GetYaxis().SetRangeUser(-5, 5) frame_res.GetYaxis().SetTitle("pulls(#sigma)") frame_res.GetYaxis().SetTitleOffset(0.3) frame_res.Draw() fixData(pulls, False, True, False) drawChi2(RSS[order]["chi2"], RSS[order]["nbins"] - (order + 1), True) line = drawLine(X_mass.getMin(), 0, lastBin, 0) if VARBINS: c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".pdf") c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".png") else: c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".pdf") c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".png") #*******************************************************# # # # Generate workspace # # # #*******************************************************# if BIAS: gSystem.Load("libHiggsAnalysisCombinedLimit.so") from ROOT import RooMultiPdf cat = RooCategory("pdf_index", "Index of Pdf which is active") pdfs = RooArgList(modelBkg, modelAlt) roomultipdf = RooMultiPdf("roomultipdf", "All Pdfs", cat, pdfs) normulti = RooRealVar("roomultipdf_norm", "Number of background events", nevents, 0., 1.e6) normzBkg.setConstant( False ) ## newly put here to ensure it's freely floating in the combine fit # create workspace w = RooWorkspace("Zprime_" + YEAR, "workspace") # Dataset if isData: getattr(w, "import")(setData, RooFit.Rename("data_obs")) else: getattr(w, "import")(setToys, RooFit.Rename("data_obs")) #getattr(w, "import")(setData, RooFit.Rename("data_obs")) if BIAS: getattr(w, "import")(cat, RooFit.Rename(cat.GetName())) getattr(w, "import")(normulti, RooFit.Rename(normulti.GetName())) getattr(w, "import")(roomultipdf, RooFit.Rename(roomultipdf.GetName())) getattr(w, "import")(modelBkg, RooFit.Rename(modelBkg.GetName())) getattr(w, "import")(modelAlt, RooFit.Rename(modelAlt.GetName())) getattr(w, "import")(normzBkg, RooFit.Rename(normzBkg.GetName())) w.writeToFile(WORKDIR + "%s_%s.root" % (DATA_TYPE + "_" + YEAR, category), True) print "Workspace", WORKDIR + "%s_%s.root" % ( DATA_TYPE + "_" + YEAR, category), "saved successfully" if VERBOSE: raw_input("Press Enter to continue...")
def plot(var, cut, year, norm=False, nm1=False): ### Preliminary Operations ### treeRead = not cut in [ "nnqq", "en", "enqq", "mn", "mnqq", "ee", "eeqq", "mm", "mmqq", "em", "emqq", "qqqq" ] # Read from tree channel = cut unit = '' if "GeV" in variable[var]['title']: unit = ' GeV' isBlind = BLIND and 'SR' in channel isAH = False #'qqqq' in channel or 'hp' in channel or 'lp' in channel showSignal = False if 'SB' in cut or 'TR' in cut else True #'SR' in channel or channel=='qqqq'#or len(channel)==5 stype = "HVT model B" if len(sign) > 0 and 'AZh' in sign[0]: stype = "2HDM" elif len(sign) > 0 and 'monoH' in sign[0]: stype = "Z'-2HDM m_{A}=300 GeV" if treeRead: for k in sorted(alias.keys(), key=len, reverse=True): if BTAGGING == 'semimedium': if k in cut: if ADDSELECTION: cut = cut.replace( k, aliasSM[k] + SELECTIONS[options.selection]) else: cut = cut.replace(k, aliasSM[k]) else: if k in cut: if ADDSELECTION: cut = cut.replace( k, alias[k].format(WP=working_points[BTAGGING]) + SELECTIONS[options.selection]) else: cut = cut.replace( k, alias[k].format(WP=working_points[BTAGGING])) # Determine Primary Dataset pd = sample['data_obs']['files'] print "Plotting from", ("tree" if treeRead else "file"), var, "in", channel, "channel with:" print " dataset:", pd print " cut :", cut if var == 'jj_deltaEta_widejet': if "jj_deltaEta_widejet<1.1 && " in cut: print print "omitting jj_deltaEta_widejet<1.1 cut to draw the deltaEta distribution" print cut = cut.replace("jj_deltaEta_widejet<1.1 && ", "") else: print print "no 'jj_deltaEta_widejet<1.1 && ' in the cut string detected, so it cannot be ommited explicitly" print ### Create and fill MC histograms ### # Create dict file = {} tree = {} hist = {} ### Create and fill MC histograms ### for i, s in enumerate(data + back + sign): if treeRead: # Project from tree tree[s] = TChain("tree") for j, ss in enumerate(sample[s]['files']): if not 'data' in s or ('data' in s and ss in pd): if year == "run2" or year in ss: tree[s].Add(NTUPLEDIR + ss + ".root") if variable[var]['nbins'] > 0: hist[s] = TH1F( s, ";" + variable[var]['title'] + ";Events / ( " + str( (variable[var]['max'] - variable[var]['min']) / variable[var]['nbins']) + unit + " );" + ('log' if variable[var]['log'] else ''), variable[var]['nbins'], variable[var]['min'], variable[var]['max']) else: hist[s] = TH1F( s, ";" + variable[var]['title'] + ";Events" + ('log' if variable[var]['log'] else ''), len(variable[var]['bins']) - 1, array('f', variable[var]['bins'])) hist[s].Sumw2() cutstring = "(eventWeightLumi)" + ("*(" + cut + ")" if len(cut) > 0 else "") tree[s].Project(s, var, cutstring) if not tree[s].GetTree() == None: hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast()) else: # Histogram written to file for j, ss in enumerate(sample[s]['files']): if not 'data' in s or ('data' in s and ss in pd): file[ss] = TFile(NTUPLEDIR + ss + ".root", "R") if file[ss].IsZombie(): print "WARNING: file", NTUPLEDIR + ss + ".root", "does not exist" continue tmphist = file[ss].Get(cut + "/" + var) if tmphist == None: continue if not s in hist.keys(): hist[s] = tmphist else: hist[s].Add(tmphist) hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0) hist[s].SetFillColor(sample[s]['fillcolor']) hist[s].SetFillStyle(sample[s]['fillstyle']) hist[s].SetLineColor(sample[s]['linecolor']) hist[s].SetLineStyle(sample[s]['linestyle']) if channel.endswith('TR') and channel.replace('TR', '') in topSF: hist['TTbarSL'].Scale(topSF[channel.replace('TR', '')][0]) hist['ST'].Scale(topSF[channel.replace('TR', '')][0]) hist['BkgSum'] = hist['data_obs'].Clone( "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum") hist['BkgSum'].Reset("MICES") hist['BkgSum'].SetFillStyle(3003) hist['BkgSum'].SetFillColor(1) for i, s in enumerate(back): hist['BkgSum'].Add(hist[s]) if options.norm: for i, s in enumerate(back + ['BkgSum']): hist[s].Scale(hist[data[0]].Integral() / hist['BkgSum'].Integral()) # Create data and Bkg sum histograms if options.blind or 'SR' in channel: hist['data_obs'] = hist['BkgSum'].Clone("data_obs") hist['data_obs'].Reset("MICES") # Set histogram style hist['data_obs'].SetMarkerStyle(20) hist['data_obs'].SetMarkerSize(1.25) for i, s in enumerate(data + back + sign + ['BkgSum']): addOverflow(hist[s], False) # Add overflow for i, s in enumerate(sign): hist[s].SetLineWidth(3) for i, s in enumerate(sign): sample[s][ 'plot'] = True #sample[s]['plot'] and s.startswith(channel[:2]) if isAH: for i, s in enumerate(back): hist[s].SetFillStyle(3005) hist[s].SetLineWidth(2) #for i, s in enumerate(sign): # hist[s].SetFillStyle(0) if not var == "Events": sfnorm = hist[data[0]].Integral() / hist['BkgSum'].Integral() print "Applying SF:", sfnorm for i, s in enumerate(back + ['BkgSum']): hist[s].Scale(sfnorm) if BLIND and var.endswith("Mass"): for i, s in enumerate(data + back + ['BkgSum']): first, last = hist[s].FindBin(65), hist[s].FindBin(135) for j in range(first, last): hist[s].SetBinContent(j, -1.e-4) if BLIND and var.endswith("Tau21"): for i, s in enumerate(data): first, last = hist[s].FindBin(0), hist[s].FindBin(0.6) for j in range(first, last): hist[s].SetBinContent(j, -1.e-4) if SYNC and var == "jj_mass_widejet" and year in ["2016", "2017", "2018"]: #iFile = TFile("sync/JetHT_run" + year + "_red_cert_scan.root", "READ") #hist['sync'] = iFile.Get("Mjj") if year == '2016': iFile = TFile("sync/2016/2016_07Aug2017_1246_1p1.root", "READ") hist['sync'] = iFile.Get("h_mjj_data") elif year == '2017': iFile = TFile( "sync/2017/histos_Run2017BCDEF_17Nov2017_JEC2017_mjj1530_cemf_lt_0p8_deltaETA_lt_1p1.root", "READ") hist['sync'] = iFile.Get("h_mjj_data") elif year == '2018': iFile = TFile( "sync/2018/Double_sideband_inputs_18v10_preliminary_v2.root", "READ") hist['sync'] = iFile.Get("h_mjj") # hist['sync'] = tmp.Rebin(len(dijet_bins)-1, "sync", array('d', dijet_bins)) # hist['sync'] = tmp.Rebin(100, "sync") hist['sync'].SetMarkerStyle(31) hist['sync'].SetMarkerSize(1.25) hist['sync'].SetMarkerColor(2) print "Imported and drawing sync file" # Create stack if variable[var]['nbins'] > 0: bkg = THStack( "Bkg", ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events / ( " + str( (variable[var]['max'] - variable[var]['min']) / variable[var]['nbins']) + unit + " )") else: bkg = THStack("Bkg", ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events; ") for i, s in enumerate(back): bkg.Add(hist[s]) # Legend leg = TLegend(0.65, 0.6, 0.95, 0.9) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) if len(data) > 0: leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe") for i, s in reversed(list(enumerate(['BkgSum'] + back))): leg.AddEntry(hist[s], sample[s]['label'], "f") if showSignal: for i, s in enumerate(sign): if sample[s]['plot']: leg.AddEntry(hist[s], sample[s]['label'], "fl") leg.SetY1(0.9 - leg.GetNRows() * 0.05) # --- Display --- c1 = TCanvas("c1", hist.values()[0].GetXaxis().GetTitle(), 800, 800 if RATIO else 600) if RATIO: c1.Divide(1, 2) setTopPad(c1.GetPad(1), RATIO) setBotPad(c1.GetPad(2), RATIO) c1.cd(1) c1.GetPad(bool(RATIO)).SetTopMargin(0.06) c1.GetPad(bool(RATIO)).SetRightMargin(0.05) c1.GetPad(bool(RATIO)).SetTicks(1, 1) log = variable[var]['log'] #"log" in hist['BkgSum'].GetZaxis().GetTitle() if log: c1.GetPad(bool(RATIO)).SetLogy() # Draw bkg.Draw("HIST") # stack hist['BkgSum'].Draw("SAME, E2") # sum of bkg if not isBlind and len(data) > 0: hist['data_obs'].Draw("SAME, PE") # data if 'sync' in hist: hist['sync'].Draw("SAME, PE") #data_graph.Draw("SAME, PE") if showSignal: smagn = 1. #if treeRead else 1.e2 #if log else 1.e2 for i, s in enumerate(sign): # if sample[s]['plot']: hist[s].Scale(smagn) hist[s].Draw( "SAME, HIST" ) # signals Normalized, hist[s].Integral()*sample[s]['weight'] textS = drawText(0.80, 0.9 - leg.GetNRows() * 0.05 - 0.02, stype + " (x%d)" % smagn, True) #bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset()*1.075) bkg.GetYaxis().SetTitleOffset(0.9) #bkg.GetYaxis().SetTitleOffset(2.) bkg.SetMaximum((5. if log else 1.25) * max( bkg.GetMaximum(), hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) + hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin()))) #if bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25) bkg.SetMinimum( max( min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin( )), hist['data_obs'].GetMinimum()), 5.e-1) if log else 0.) if log: bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4) #bkg.GetYaxis().SetMoreLogLabels(True) bkg.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max']) #if log: bkg.SetMinimum(1) leg.Draw() #drawCMS(LUMI[year], "Preliminary") #drawCMS(LUMI[year], "Work in Progress", suppressCMS=True) drawCMS(LUMI[year], "", suppressCMS=True) drawRegion('XVH' + channel, True) drawAnalysis(channel) setHistStyle(bkg, 1.2 if RATIO else 1.1) setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1) if RATIO: c1.cd(2) err = hist['BkgSum'].Clone("BkgErr;") err.SetTitle("") if SYNC: err.GetYaxis().SetTitle("Nano/Mini") else: err.GetYaxis().SetTitle("Data / MC") err.GetYaxis().SetTitleOffset(0.9) err.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max']) for i in range(1, err.GetNbinsX() + 1): err.SetBinContent(i, 1) if hist['BkgSum'].GetBinContent(i) > 0: err.SetBinError( i, hist['BkgSum'].GetBinError(i) / hist['BkgSum'].GetBinContent(i)) setBotStyle(err) errLine = err.Clone("errLine") errLine.SetLineWidth(1) errLine.SetFillStyle(0) res = hist['data_obs'].Clone("Residues") for i in range(0, res.GetNbinsX() + 1): if hist['BkgSum'].GetBinContent(i) > 0: res.SetBinContent( i, res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i)) res.SetBinError( i, res.GetBinError(i) / hist['BkgSum'].GetBinContent(i)) if 'sync' in hist: res.SetMarkerColor(1) res.SetMarkerStyle(20) res.Reset() for i in range(0, res.GetNbinsX() + 1): x = hist['data_obs'].GetXaxis().GetBinCenter(i) if hist['sync'].GetBinContent(hist['sync'].FindBin(x)) > 0: res.SetBinContent( i, hist['data_obs'].GetBinContent( hist['data_obs'].FindBin(x)) / hist['sync'].GetBinContent(hist['sync'].FindBin(x))) res.SetBinError( i, hist['data_obs'].GetBinError( hist['data_obs'].FindBin(x)) / hist['sync'].GetBinContent(hist['sync'].FindBin(x))) setBotStyle(res) #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5) #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2) err.Draw("E2") errLine.Draw("SAME, HIST") if not isBlind and len(data) > 0: res.Draw("SAME, PE0") #res_graph.Draw("SAME, PE0") if len(err.GetXaxis().GetBinLabel( 1)) == 0: # Bin labels: not a ordinary plot drawRatio(hist['data_obs'], hist['BkgSum']) drawStat(hist['data_obs'], hist['BkgSum']) if SYNC: err.GetYaxis().SetRangeUser(0.9, 1.1) c1.Update() if gROOT.IsBatch(): if channel == "": channel = "nocut" varname = var.replace('.', '_').replace('()', '') if not os.path.exists("plots/" + channel): os.makedirs("plots/" + channel) suffix = '' if "b" in channel or 'mu' in channel: suffix += "_" + BTAGGING if ADDSELECTION: suffix += "_" + options.selection c1.Print("plots/" + channel + "/" + varname + "_" + year + suffix + ".png") c1.Print("plots/" + channel + "/" + varname + "_" + year + suffix + ".pdf") # Print table printTable(hist, sign) # if True: # sFile = TFile("sync/data_2016.root", "RECREATE") # sFile.cd() # hist['data_obs']. if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
def plotPrePost(category, category2): if len(category) == 0: print "Please select a category with the -c option" exit() is2 = len(category2) > 0 if 'l' in category: category, category2, is2 = category.replace('l', 'e'), category.replace( 'l', 'm'), True isAH = len(category) >= 5 X_name = "JJ_mass" if isAH else "VH_mass" signalName = signal + category + "_M%d" % mass data = ["data_obs"] back = ["Bkg_" + category] if isAH else [ "VV_" + category, "Top_" + category, "Vjets_" + category ] sign = [signalName] if len(signal) > 0 else [] fitr = ["total", "total_signal", "total_background"] labels = { "data_obs": "Data", "VV_" + category: "VV, Vh", "Top_" + category: "t#bar{t}, t+X", "Vjets_" + category: "V+jets", "Bkg_" + category: "Bkg fit", signalName: "Signal" } if 'ee' in category or 'mm' in category: labels["Vjets_" + category] = "Z(ll)+jets" elif 'en' in category or 'mn' in category: labels["Vjets_" + category] = "W(l#nu)+jets" elif 'nn' in category: labels["Vjets_" + category] = "Z(#nu#nu),W(l#nu)+jets" lastBin = 3500. if 'ee' in category or 'mm' in category or 'nn' in category else 4500. # xs = 0. # if 'XWH' in signal or 'XVH' in signal: xs += HVT['B3']['W']['XS'][mass]*HVT['B3']['W']['BR'][mass] # if 'XZH' in signal or 'XVH' in signal: xs += HVT['B3']['Z']['XS'][mass]*HVT['B3']['Z']['BR'][mass] xs = getCrossSection(signal, category, mass) #if len(signal2)>0: xs[signal2] = getCrossSection(signal2, category, 0) histData, histPre, histPost, graphPre, graphPost = None, {}, {}, {}, {} if readData: dataFile = TFile("workspace/" + category + ".root", "READ") workspace = dataFile.Get("VH_2016") variable = workspace.var(X_name) dataset = workspace.data(data[0]) if is2: dataFile2 = TFile("workspace/" + category2 + ".root", "READ") workspace2 = dataFile2.Get("VH_2016") dataset2 = workspace2.data(data[0]) dataset.append(dataset2) data2 = dataset.createHistogram(variable, variable, variable.getBinning().numBins() / 10, 1) data1 = data2.ProjectionX() data1.SetMarkerStyle(20) data1.SetMarkerSize(1.25) data1.SetLineColor(1) histData = data1 graphData = convertHistToGraph(histData, True) width = histData.GetXaxis().GetBinWidth(1) inFile = TFile(options.fileName, "READ") if inFile == None: print "File", options.fileName, "not found" return if not inFile.GetDirectory("shapes_prefit/" + category): print "Category", category, "not recognized" return for i, h in enumerate(back + sign + fitr): histPre[h] = inFile.Get("shapes_prefit/" + category + "/" + h) if is2: histPre[h].Add( inFile.Get("shapes_prefit/" + category2 + "/" + h.replace(category, category2))) histPre[h].SetName(h + '_pre') histPre[h].SetLineColor(getColor(h, category)) histPre[h].SetFillColor(getColor(h, category)) histPre[h].SetLineWidth(3) if h in back: histPre[h].SetFillStyle(1001) elif h in fitr: histPre[h].SetFillStyle(3002) elif h in sign: histPre[h].SetTitle("m_{%s'} = %d GeV" % (signal[1], mass)) histPre[h].SetOption("HVT model B g_{V}=3") histPre[h].SetFillStyle(1) histPre[h].SetLineStyle(3) histPre[h].SetLineWidth(6) if xs > 0.: histPre[h].Scale(xs * 1000.) # histPre[h].Rebin(10) if readData: histPre[h].Scale(width) histPre[h].GetYaxis().SetTitle("Events / ( %d GeV )" % width) if 'nn' in category: histPre[h].GetXaxis().SetTitle( histPre[h].GetXaxis().GetTitle().replace( 'm_{VH}', 'm^{T}_{VH}')) for i, h in enumerate(back + sign + fitr): histPost[h] = inFile.Get("shapes_fit_b/" + category + "/" + h) if is2: histPost[h].Add( inFile.Get("shapes_fit_b/" + category2 + "/" + h.replace(category, category2))) histPost[h].SetName(h + '_post') histPost[h].SetLineColor(getColor(h, category)) histPost[h].SetFillColor(getColor(h, category)) histPost[h].SetLineWidth(3) if h in back: histPost[h].SetFillStyle(1001) elif h in fitr: histPost[h].SetFillStyle(3002) elif h in sign: histPost[h].SetTitle("m_{%s'} = %d GeV" % (signal[1], mass)) histPost[h].SetOption("HVT model B g_{V}=3") histPost[h].SetFillStyle(1) histPost[h].SetLineStyle(3) histPost[h].SetLineWidth(6) if xs > 0.: histPost[h].Scale(xs * 1000.) # histPost[h].Rebin(10) if readData: histPost[h].Scale(width) histPost[h].GetYaxis().SetTitle("Events / ( %d GeV )" % width) if 'nn' in category: histPost[h].GetXaxis().SetTitle( histPost[h].GetXaxis().GetTitle().replace( 'm_{VH}', 'm^{T}_{VH}')) # Set errors ot zero to have smooth curves for i, h in enumerate(back + sign): for i in range(histPre[h].GetNbinsX()): histPre[h].SetBinError(i + 1, 0.) for i in range(histPost[h].GetNbinsX()): histPost[h].SetBinError(i + 1, 0.) stackPre = THStack( "Pre", ";" + histPre['total'].GetXaxis().GetTitle() + ";" + histPre['total'].GetYaxis().GetTitle()) for i, s in enumerate(back): stackPre.Add(histPre[s]) stackPost = THStack( "Post", ";" + histPost['total'].GetXaxis().GetTitle() + ";" + histPost['total'].GetYaxis().GetTitle()) for i, s in enumerate(back): stackPost.Add(histPost[s]) for i, h in enumerate(back): tmpPre = histPre[back[i]].Clone(back[i] + "_stack_pre") for j in range(i): tmpPre.Add(histPre[back[j]]) graphPre[back[i]] = convertHistToGraph(tmpPre) tmpPost = histPost[back[i]].Clone(back[i] + "_stack_post") for j in range(i): tmpPost.Add(histPost[back[j]]) graphPost[back[i]] = convertHistToGraph(tmpPost) # Additional signal, if present inFiles = {} for i, f in enumerate( signals): #combine/test/mlfit_monoHnn_MZ3000_MA300.root signalName2 = f.replace('combine/test/mlfit_', '').replace('.root', '') if 'AZh' in signalName2: signalName2 = signalName2.replace('AZh', 'AZh' + category) signalSmpl2 = signalName2.replace(category, '') signal2 = signalName2.split('_M')[0].replace(category, '') try: mass2, mass2A = int(signalName2.split('_M')[1]), 0 except: mass2, mass2A = int( signalName2.split('_MZ')[1].split('_MA')[0]), int( signalName2.split('_MA')[1]) # inFiles[signalName2] = TFile(f, "READ") histSign2 = inFiles[signalName2].Get("shapes_prefit/" + category + "/" + signalName2) if is2: histSign2.Add(inFiles[signalName2].Get( "shapes_prefit/" + category2 + "/" + signalName2.replace(category, category2))) histSign2.SetName(signalName2 + '_pre') if signal2.startswith('X'): histSign2.SetTitle("m_{V'} = %d GeV" % mass2) elif signal2.startswith('A'): histSign2.SetTitle("m_{A} = %d GeV" % mass2) elif not mass2 == 0: histSign2.SetTitle("m_{Z'} = %d GeV" % mass2) if i == 0: histSign2.SetOption( "Z'-2HDM\nm_{A}=300 GeV" if 'monoH' in signalName2 else "Type-II 2HDM\ncos(#beta-#alpha) = 0.25\ntan#beta = 1") histSign2.SetLineColor(getColor(signalName2, category)) histSign2.SetFillColor(getColor(signalName2, category)) histSign2.SetFillStyle(1) histSign2.SetLineStyle(5 + i) histSign2.SetLineWidth(5) if readData: histSign2.Scale(width) xs2 = getCrossSection(signalName2, category, 0) if xs2 > 0.: histSign2.Scale(xs2 * 1000.) sign += [signalName2] histPre[signalName2] = histSign2 histPost[signalName2] = histSign2 leg = TLegend(0.6 - 0.005, 0.6, 0.925, 0.9) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) if readData: leg.AddEntry(graphData, labels[data[0]], "pe") for i, s in reversed(list(enumerate(back))): leg.AddEntry(histPre[s], labels[s], "f") #for i, s in enumerate(sign): # leg.AddEntry(histPre[s], labels[s], "fl") ### c1 = TCanvas("c1", "Pre-Post Fit", 800, 800) c1.Divide(1, 2) c1.cd(1) setTopPad(c1.GetPad(1), RATIO) setBotPad(c1.GetPad(2), RATIO) c1.GetPad(1).SetTopMargin(0.06) c1.GetPad(1).SetRightMargin(0.05) c1.GetPad(1).SetBottomMargin(0.01) c1.GetPad(1).SetTicks(1, 1) #histData.Draw("APE" if d==0 else "SAME, PE") #for i, h in enumerate(back): graphPost[h].Draw("ACL" if i==0 else "C") stackPost.Draw("C") setHistStyle(stackPost, 1.2) stackPost.SetMaximum(stackPost.GetMaximum() * 5.) stackPost.SetMinimum(max(stackPost.GetMinimum(), 0.2)) stackPost.GetXaxis().SetRangeUser(stackPost.GetXaxis().GetXmin(), lastBin) histPost['total'].SetLineWidth(1) histPost['total'].Draw("SAME, E3") histPre['total_background'].SetLineColor(921) histPre['total_background'].SetLineStyle(2) histPre['total_background'].SetLineWidth(3) histPre['total_background'].SetFillColor(1) histPre['total_background'].SetFillStyle(0) histPre['total_background'].Draw("SAME, HIST") for i, s in enumerate(sign): histPre[s].Draw("SAME, L") if readData: graphData.Draw("SAME, PE0") leg.AddEntry(histPost['total'], "Bkg. unc.", "f") leg.AddEntry(histPre['total_background'], "Pre-fit", "l") for i, s in enumerate(sign): for o in histPre[s].GetOption().split('\n'): if len(o) > 0.: leg.AddEntry(None, o, "") leg.AddEntry(histPre[s], histPre[s].GetTitle(), "l") leg.SetY1(0.9 - leg.GetNRows() * 0.060) leg.Draw() # if len(sign)>0: # latex = TLatex() # latex.SetNDC() # latex.SetTextSize(0.045) # latex.SetTextFont(42) # latex.DrawLatex(0.67, leg.GetY1()-0.045, "HVT model B g_{V}=3") drawCMS(LUMI, "") #Preliminary drawRegion('XVH' + options.category, True) drawAnalysis(category) c1.GetPad(1).SetLogy() c1.cd(2) err = histPost['total'].Clone("BkgErr;") err.SetTitle("") err.Reset("MICES") err.GetYaxis().SetTitle("(N^{data}-N^{bkg})/#sigma") setBotStyle(err, 4 + 1) err.GetXaxis().SetTitleSize(0.16) #err.GetXaxis().SetTitleOffset(1.25); err.GetYaxis().SetTitleOffset(0.33) err.GetXaxis().SetRangeUser(err.GetXaxis().GetXmin(), lastBin) err.GetYaxis().SetRangeUser(-5., 5.) err.SetLineWidth(2) err.SetLineStyle(2) err.SetFillStyle(0) err.Draw("L") #"E2" if readData: pulls = makeResidHist(graphData, histPost['total']) #setBotStyle(pulls, RATIO, False) pulls.Draw("SAME, PE0") #drawRatio(hist['data_obs'], hist['BkgSum']) #drawStat(hist['data_obs'], hist['BkgSum']) chi2, nbins, npar = 0., 0, 0 for i in range(0, pulls.GetN()): if graphData.GetY()[i] > 1.e-3: nbins = nbins + 1 chi2 += pulls.GetY()[i]**2 #drawChi2(chi2, nbins-npar, True) c1.Update() if not gROOT.IsBatch(): raw_input("Press Enter to continue...") c1.Print("plotsPrePost/BkgSR_" + options.category + ".png") c1.Print("plotsPrePost/BkgSR_" + options.category + ".pdf") ### if VERBOSE: c2 = TCanvas("c2", "Pre-Post Fit", 1200, 600) c2.Divide(2, 1) c2.cd(1) c2.GetPad(1).SetTopMargin(0.06) c2.GetPad(1).SetRightMargin(0.05) c2.GetPad(1).SetBottomMargin(0.10) c2.GetPad(1).SetTicks(1, 1) #histData.Draw("APE" if d==0 else "SAME, PE") #for i, h in enumerate(back): graphPre[h].Draw("FL" if i==0 else "SAME, FL") stackPre.Draw("HIST") histPre['total'].Draw("SAME, E2") stackPre.SetMaximum(stackPre.GetMaximum() * 5.) stackPre.SetMinimum(max(stackPre.GetMinimum(), 0.01)) histData.Draw("SAME, PE") leg.Draw() drawCMS(LUMI, "Preliminary") drawRegion('XVH' + category, True) drawAnalysis(category) c2.cd(2) c2.GetPad(2).SetTopMargin(0.06) c2.GetPad(2).SetRightMargin(0.05) c2.GetPad(2).SetBottomMargin(0.10) c2.GetPad(2).SetTicks(1, 1) #histData.Draw("APE" if d==0 else "SAME, PE") #for i, h in enumerate(back): graphPost[h].Draw("ACL" if i==0 else "C") stackPost.Draw("HIST") histPost['total'].Draw("SAME, E2") stackPost.SetMaximum(stackPost.GetMaximum() * 5.) stackPost.SetMinimum(max(stackPost.GetMinimum(), 0.01)) histData.Draw("SAME, PE") leg.Draw() drawCMS(LUMI, "Preliminary") drawRegion('XVH' + category, True) drawAnalysis(category) c2.GetPad(1).SetLogy() c2.GetPad(2).SetLogy() c2.Print("combine/test/" + signalName + "_prepost.png") c2.Print("combine/test/" + signalName + "_prepost.pdf") c2.Close()
for cand in cands[cand_type]: ceff[cand], ccand_perevent[cand] = ({} for _ in range(2)) for iVar, var in enumerate(vars_to_plot[cand_type]): ceff[cand][var] = TCanvas(f"c{var}{cand}", "", 1200, 400) ccand_perevent[cand][var] = TCanvas(f"c{var}{cand}_perEvent", "", 1200, 400) nCutsTested = hvar[cand][var][origins[0]][-1].GetNbinsX() varTitle = hvar[cand][var][origins[0]][-1].GetXaxis().GetTitle() ceff[cand][var].Divide(nPtBins, 1) ccand_perevent[cand][var].Divide(nPtBins, 1) for iPtBin in range(nPtBins): # fill legend for orig in origins: if iPtBin == 1 and leg_orig.GetNRows() < len(origins): leg_orig.AddEntry( hvar[cand][var][orig][iPtBin - 1], leg_orig_names[orig], "pl", ) ptMin = ( hvar_vs_pt[cand][var][origins[0]] .GetXaxis() .GetBinLowEdge(iPtBin + 1) ) ptMax = ( hvar_vs_pt[cand][var][origins[0]] .GetXaxis() .GetBinUpEdge(iPtBin + 1)
def hvt(benchmark=['B3', 'A1']): hxs = {} hw = {} gxs = {} gw = {} mg = TMultiGraph() for m in massPoints: hxs[m] = TH2F("hxs_M%d" % m, ";;", 50, -0.04, 3.96, 100, 0., 2.) hw[m] = TH2F("hw_M%d" % m, ";;", 50, -0.04, 3.96, 50, 0., 2.) for m in massPoints: file = TFile.Open("HVT/scanHVT_M%s.root" % m, "READ") tree = file.Get("tree") for entry in range( tree.GetEntries()): # Fill mass points only if NOT excluded tree.GetEntry(entry) gH, gF = tree.gv * tree.ch, tree.g * tree.g * tree.cq / tree.gv XsBr = tree.CX0 * tree.BRbb * 1000. # in fb if XsBr < observed[m]: hxs[m].Fill(gH, gF) if tree.total_widthV0 / float(m) < width: hw[m].Fill(gH, gF) for b in range(hxs[m].GetNbinsX() * hxs[m].GetNbinsY()): hxs[m].SetBinContent(b, 1. if hxs[m].GetBinContent(b) > 0. else 0.) hw[m].SetBinContent(b, 1. if hw[m].GetBinContent(b) > 0. else 0.) #hxs[m].Smooth(20) #hw[m].Smooth(20) gxs[m] = getCurve(hxs[m]) for i, g in enumerate(gxs[m]): g.SetLineColor(massColors[m]) g.SetFillColor(massColors[m]) g.SetFillStyle(massFill[m]) #(3345 if i>1 else 3354) g.SetLineWidth(503 * (1 if i < 2 else -1)) mg.Add(g) #if m==3000: if m == massPoints[-1]: gw[m] = getCurve(hw[m]) for i, g in enumerate(gw[m]): g.SetPoint(0, 0., g.GetY()[0]) g.SetLineWidth(501 * (1 if i < 2 else -1)) g.SetLineColor(920 + 2) g.SetFillColor(920 + 1) g.SetFillStyle(3003) mg.Add(g) if options.root: outFile = TFile("plotsLimit/Model.root", "RECREATE") outFile.cd() for m in massPoints: mg[m].Write("X_M%d" % m) mgW.Write("width") outFile.Close() print "Saved histogram in file plotsLimit/Model.root, exiting..." exit() ### plot ### c1 = TCanvas("c1", "HVT Exclusion Limits", 800, 600) c1.cd() c1.GetPad(0).SetTopMargin(0.06) c1.GetPad(0).SetRightMargin(0.05) c1.GetPad(0).SetTicks(1, 1) mg.Draw("AC") #mg.GetXaxis().SetTitle("g_{V} c_{H}") mg.GetXaxis().SetTitle("Higgs and vector boson coupling g_{H}") mg.GetXaxis().SetRangeUser(-3., 3.) mg.GetXaxis().SetLabelSize(0.045) mg.GetXaxis().SetTitleSize(0.045) mg.GetXaxis().SetTitleOffset(1.) #mg.GetYaxis().SetTitle("g^{2} c_{F} / g_{V}") mg.GetYaxis().SetTitle("Fermion coupling g_{F}") mg.GetYaxis().SetLabelSize(0.045) mg.GetYaxis().SetTitleSize(0.045) mg.GetYaxis().SetTitleOffset(1.) mg.GetYaxis().SetRangeUser(-1.2, 1.2) mg.GetYaxis().SetNdivisions(505) # hxs[3500].Draw("CONTZ") drawCMS(LUMI, "Preliminary", False) # drawAnalysis("XVH"+category, False) # latex = TLatex() # latex.SetNDC() # latex.SetTextFont(62) # latex.SetTextSize(0.06) # latex.DrawLatex(0.10, 0.925, "CMS") # model B g_model = {} for i, b in enumerate(benchmark): g_model[i] = TGraph(1) g_model[i].SetTitle(models_name[b]) g_model[i].SetPoint(0, models_point[b][0], models_point[b][1]) g_model[i].SetMarkerStyle(models_style[b]) g_model[i].SetMarkerColor(models_color[b]) g_model[i].SetMarkerSize(1.5) g_model[i].Draw("PSAME") # text latex = TLatex() latex.SetTextSize(0.045) latex.SetTextFont(42) latex.SetTextColor(630) # for b in benchmark: latex.DrawLatex(models_point[b][0]+0.02, models_point[b][1]+0.02, models_name[b]) latex.SetTextColor(920 + 2) latex.DrawLatex(-2.8, -0.875, "#frac{#Gamma_{Z'}}{m_{Z'}} > %.0f%%" % (width * 100, )) #leg = TLegend(0.68, 0.60, 0.95, 0.94) leg = TLegend(0.68, 0.34, 0.95, 0.66) leg.SetBorderSize(1) leg.SetFillStyle(1001) leg.SetFillColor(0) for m in massPoints: leg.AddEntry(gxs[m][0], "m_{Z'} = %.1f TeV" % (m / 1000.), "fl") for i, b in enumerate(benchmark): leg.AddEntry(g_model[i], g_model[i].GetTitle(), "P") leg.SetY1(leg.GetY2() - leg.GetNRows() * 0.050) leg.SetMargin(0.35) leg.Draw() gxs_ = gxs[massPoints[0]][0].Clone("gxs_") gxs_.SetLineColor(1) # gxs_.SetFillColor(1) latex.SetNDC() latex.SetTextColor(1) latex.SetTextSize(0.04) latex.SetTextFont(52) latex.DrawLatex(0.15, 0.95, "q#bar{q} #rightarrow Z' #rightarrow b#bar{b}") c1.Print("plots/model/HVT.png") c1.Print("plots/model/HVT.pdf") c1.Print("plots/model/HVT.root") c1.Print("plots/model/HVT.C") #g = 0.646879, cH = 0.976246, cF = 1.02433 print "model B = [", 3 * 0.976246, ",", 0.646879 * 0.646879 * 1.02433 / 3, "]" if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
def plot(var, cut, signal): sign = signal ### Preliminary Operations ### treeRead = not 'cutflow' in var #treeRead = False channel = cut if 'inc' in cut: eventWeightLuminame = 'eventWeightLumi_nobtag' else: eventWeightLuminame = 'eventWeightLumi' if "SB" in cut or "SR" in cut: channel_name = cut[:-2] else: channel_name = cut if year in ['2016', '2017', '2018']: NTUPLEDIR = "/work/pbaertsc/heavy_resonance/Ntuples%s/" % (year) else: NTUPLEDIR = "/work/pbaertsc/heavy_resonance/" #showSignal = False if 'SB' in cut or 'TR' in cut else True #'SR' in channel or channel=='qqqq'#or len(channel)==5 if var in [ 'dijet_VBF_mass', 'deltaR_VBF', 'deltaR_HVBFjet1', 'deltaR_HVBFjet2' ]: showSignal = False else: showSignal = True if treeRead: for k in sorted(selection.keys(), key=len, reverse=True): if k in cut: cut = cut.replace(k, selection[k]) print "Plotting from", ("tree" if treeRead else "file"), var, "in", channel, "channel with:" print " cut :", cut ### Create and fill MC histograms ### # Create dict file = {} tree = {} hist = {} ### Create and fill MC histograms ### tree[sign] = TChain("tree") for j, ss in enumerate(sample[sign]['files']): tree[sign].Add(NTUPLEDIR + ss + ".root") if variable[var]['nbins'] > 0: min_value = variable[var]['min'] max_value = variable[var]['max'] title = variable[var]['title'] hist[sign] = TH1F( sign, ";" + title + ";Events;" + ('log' if variable[var]['log'] else ''), variable[var]['nbins'], min_value, max_value) else: hist[s] = TH1F(sign, ";" + variable[var]['title'], len(variable[var]['bins']) - 1, array('f', variable[var]['bins'])) hist[sign].Sumw2() cutstring = "%s" % eventWeightLuminame + ("*(" + cut + ")") tree[sign].Project(sign, var, cutstring) if not tree[sign].GetTree() == None: hist[sign].SetOption("%s" % tree[sign].GetTree().GetEntriesFast()) hist[sign].SetFillColor(sample[sign]['fillcolor']) hist[sign].SetFillStyle(sample[sign]['fillstyle']) hist[sign].SetLineColor(sample[sign]['linecolor']) hist[sign].SetLineStyle(sample[sign]['linestyle']) hist[sign].SetLineWidth(3) sample[sign]['plot'] = True # Legend leg = TLegend(0.6, 0.6, 0.9, 0.9) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) leg.AddEntry(hist[sign], sample[sign]['label'], "fl") leg.SetY1(0.9 - leg.GetNRows() * 0.05) # --- Display --- c1 = TCanvas("c1", hist.values()[0].GetXaxis().GetTitle(), 800, 800) c1.SetTopMargin(0.06) c1.SetRightMargin(0.05) c1.SetTicks(1, 1) hist[sign].Draw("HIST") leg.Draw() drawCMS(LUMI, year, "Preliminary") drawRegion('XVH' + channel, True) drawAnalysis(channel) c1.Update() if gROOT.IsBatch(): varname = var.replace('.', '_').replace('()', '') if not os.path.exists(OUTPUTDIR + channel_name): os.makedirs(OUTPUTDIR + channel_name) c1.Print("%s" % OUTPUTDIR + channel_name + "/" + varname + sign + ".png") c1.Print("%s" % OUTPUTDIR + channel_name + "/" + varname + sign + ".pdf")