def draw_DR(DR_tt, DR_qcd, DR_wjet): #set line colors DR_tt.SetLineColor(4) DR_qcd.SetLineColor(8) DR_wjet.SetLineColor(2) DR_tt.legendstyle = 'L' DR_qcd.legendstyle = 'L' DR_wjet.legendstyle = 'L' #begin drawing stuff c1 = Canvas() DR_qcd.SetStats(0) DR_qcd.Draw('HIST') DR_tt.Draw('HIST SAME') DR_wjet.Draw('HIST SAME') #make legend l1 = Legend([DR_tt, DR_qcd, DR_wjet], textfont=42, textsize=.03) l1.Draw() #save as pdf c1.SaveAs("DeltaR.pdf") #make the plots wait on screen wait(True)
def extract_MET(f_tt, f_qcd, f_wjet): # get trees from files t_tt = f_tt.Get("Delphes") t_qcd = f_qcd.Get("Delphes") t_wjet = f_wjet.Get("Delphes") # get number of entries tt_n_entries = t_tt.GetEntries() qcd_n_entries = t_qcd.GetEntries() wjet_n_entries = t_wjet.GetEntries() # define leaves var_tt = "MissingET.MET" var_qcd = "MissingET.MET" var_wjet = "MissingET.MET" leaf_tt = t_tt.GetLeaf(var_tt) leaf_qcd = t_qcd.GetLeaf(var_qcd) leaf_wjet = t_wjet.GetLeaf(var_wjet) # create the histograms MET_tt = Hist(MET_NBINS, MET_NLO, MET_NHI, title='MET_tt', legendstyle='L') MET_qcd = Hist(MET_NBINS, MET_NLO, MET_NHI, title='MET_qcd', legendstyle='L') MET_wjet = Hist(MET_NBINS, MET_NLO, MET_NHI, title='MET_wjet', legendstyle='L') # FILLING THE TREE fill_MET_tree(tt_n_entries, t_tt, leaf_tt, MET_tt) fill_MET_tree(qcd_n_entries, t_qcd, leaf_qcd, MET_qcd) fill_MET_tree(wjet_n_entries, t_wjet, leaf_wjet, MET_wjet) #set line colors MET_tt.SetLineColor('blue') MET_qcd.SetLineColor('green') MET_wjet.SetLineColor('red') #begin drawing stuff c1 = Canvas() MET_qcd.SetStats(0) MET_qcd.Draw('HIST') MET_tt.Draw('HIST SAME') MET_wjet.Draw('HIST SAME') #make legend l1 = Legend([MET_tt, MET_qcd, MET_wjet], textfont=42, textsize=.03) l1.Draw() #save as pdf c1.SaveAs("../plots/MET_plots/MET.pdf") #make the plots wait on screen wait(True)
def checkOnMC(unfolding, method): global bins, nbins RooUnfold.SVD_n_toy = 1000 pulls = [] for sub in range(1,9): inputFile2 = File('../data/unfolding_merged_sub%d.root' % sub, 'read') h_data = asrootpy(inputFile2.unfoldingAnalyserElectronChannel.measured.Rebin(nbins, 'measured', bins)) nEvents = inputFile2.EventFilter.EventCounter.GetBinContent(1) lumiweight = 164.5 * 5050 / nEvents # print sub, nEvents h_data.Scale(lumiweight) doUnfoldingSequence(unfolding, h_data, method, '_sub%d' %sub) pull = unfolding.pull_inputErrorOnly() # unfolding.printTable() pulls.append(pull) unfolding.Reset() allpulls = [] for pull in pulls: allpulls.extend(pull) h_allpulls = Hist(100,-30,30) filling = h_allpulls.Fill for entry in allpulls: filling(entry) fit = h_allpulls.Fit('gaus', 'WWS') h_fit = asrootpy(h_allpulls.GetFunction("gaus").GetHistogram()) canvas = Canvas(width=1600, height=1000) canvas.SetLeftMargin(0.15) canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.10) canvas.SetRightMargin(0.05) h_allpulls.Draw() fit.Draw('same') canvas.SaveAs('plots/Pull_allBins_withFit.png') plt.figure(figsize=(16, 10), dpi=100) rplt.errorbar(h_allpulls, label=r'Pull distribution for all bins', emptybins=False) rplt.hist(h_fit, label=r'fit') plt.xlabel('(unfolded-true)/error', CMS.x_axis_title) plt.ylabel('entries', CMS.y_axis_title) plt.title('Pull distribution for all bins', CMS.title) plt.tick_params(**CMS.axis_label_major) plt.tick_params(**CMS.axis_label_minor) plt.legend(numpoints=1) plt.savefig('plots/Pull_allBins.png') #individual bins for bin_i in range(nbins): h_pull = Hist(100,-30,30) for pull in pulls: h_pull.Fill(pull[bin_i]) plt.figure(figsize=(16, 10), dpi=100) rplt.errorbar(h_pull, label=r'Pull distribution for bin %d' % (bin_i + 1), emptybins=False) plt.xlabel('(unfolded-true)/error', CMS.x_axis_title) plt.ylabel('entries', CMS.y_axis_title) plt.title('Pull distribution for bin %d' % (bin_i + 1), CMS.title) plt.savefig('Pull_bin_%d.png' % (bin_i + 1))
def draw_curve_7(_func, name, title, ylow, yhigh): """ Draw 7TeV trigger efficiency curves """ graphs = [] for (trigger, color) in triggers: tool = ROOT.TauTriggerCorrections( os.path.join(base, 'triggerSF_%s.root' % trigger)) func = getattr(tool, _func) eff = map(lambda x: func(x, 0), pt) eff_low = map(lambda x: func(x, -1), pt) eff_high = map(lambda x: func(x, 1), pt) graph = Graph(len(pt), name=trigger) for i, (p, e, e_low, e_high) in enumerate(zip(pt, eff, eff_low, eff_high)): graph.SetPoint(i, p / 1000, e) graph.SetPointError(i, 0.4, 0.4, e - e_low, e_high - e) graph.linecolor = color graph.linewidth = 2 graph.fillstyle = '/' graph.fillcolor = color graphs.append(graph) c = Canvas() leg = Legend(len(graphs), pad=c, topmargin=0.4, leftmargin=0.3, textsize=25, margin=0.2) for i, g in enumerate(graphs): if i == 0: g.Draw('3AC') g.xaxis.title = '#font[52]{p}_{T} [GeV]' g.xaxis.SetLimits(20, 100) g.yaxis.SetLimits(ylow, yhigh) g.yaxis.SetRangeUser(ylow, yhigh) g.yaxis.title = title else: g.Draw('3C SAME') leg.AddEntry(g, g.name, 'L') leg.Draw() lines = [] for thresh in (25, 35): line = Line(thresh, ylow, thresh, yhigh) line.linestyle = 'dashed' line.linewidth = 2 line.Draw() lines.append(line) c.SaveAs('trigger_{0}.png'.format(name)) c.SaveAs('trigger_{0}.eps'.format(name))
def plot_distribution(self, output_file): f = r.TFile.Open(os.path.join(self.input_directory, output_file), 'read') intuple = f.Get('pythia8-Geant4') h = Hist2D(100, 0, 350, 100, 0, 6) for muon in intuple: px = muon.px py = muon.py pz = muon.pz p = np.sqrt(px**2 + py**2 + pz**2) pt = np.sqrt(px**2 + py**2) h.Fill(p, pt) c = Canvas() r.gStyle.SetOptStat(11111111) h.Draw("colz") c.Draw() c.SaveAs(os.path.join(self.input_directory, "p_dist.png"))
def draw_stuff(): ROOT.TH1.AddDirectory(ROOT.kFALSE) #TH1.SetDirectory(0) f = root_open( "/eos/cms/store/group/dpg_dt/comm_dt/dtRootple2015/DTTree_Run259685.root", "r") f2 = root_open("tmp.root", "RECREATE") for path, dirs, objects in f.walk(): print(objects) tree = f.DTTree #for branch in f.DTTree.branchnames: # print branch canvas = Canvas() hist = Hist(100, 0.0, 1E3, name='h' + '_' + '_IPchi2', type='D') #tree.Draw('Ndigis', '', '', hist) selection = "digi_wheel==-1 & digi_station==4 & digi_sector==4 & digi_sl==1 & digi_layer==1 & (digi_time < 300 | digi_time > 700)" # & digi_wire=='wire0'" h = tree.Draw('timestamp', selection=selection, create_hist=True) #h.SetDirectory(0) canvas.SaveAs("test.png")
def draw(event, vects, radii=None, colors=None, etamin=-5, etamax=5, phimin=-math.pi, phimax=math.pi): """ This function creates a 2D view of an event over eta and phi """ c = Canvas() display = Hist2D(100, etamin, etamax, 100, phimin, phimax) display.SetXTitle('#eta') display.SetYTitle('#phi') display.Draw() things = [] for i, vect in enumerate(vects): radius = .4 if radii: radius = radii[i] thing = Ellipse(vect.Eta(), vect.Phi(), radius, radius) thing.SetLineWidth(3) if colors: thing.SetLineColor(colors[i]) thing.SetFillStyle(0) thing.Draw() things.append(thing) c.SaveAs('eventviews/event_%i.png' % event.EventNumber)
def draw_DPHImj(DPHI_tt, DPHI_qcd, DPHI_wjet): #set line colors DPHI_tt.SetLineColor(4) DPHI_qcd.SetLineColor(8) DPHI_wjet.SetLineColor(2) DPHI_tt.legendstyle = 'L' DPHI_qcd.legendstyle = 'L' DPHI_wjet.legendstyle = 'L' #begin drawing stuff c1 = Canvas() DPHI_qcd.SetStats(0) DPHI_qcd.Draw('HIST') DPHI_tt.Draw('HIST SAME') DPHI_wjet.Draw('HIST SAME') #make legend l1 = Legend([DPHI_tt, DPHI_qcd, DPHI_wjet], textfont=42, textsize=.03) l1.Draw() #save as pdf c1.SaveAs("DPHI_metjet.pdf")
def draw_MT(MT_tt, MT_qcd, MT_wjet): #set line colors MT_tt.SetLineColor(4) MT_qcd.SetLineColor(8) MT_wjet.SetLineColor(2) MT_tt.legendstyle = 'L' MT_qcd.legendstyle = 'L' MT_wjet.legendstyle = 'L' #begin drawing stuff c1 = Canvas() MT_qcd.SetStats(0) MT_qcd.Draw('HIST') MT_tt.Draw('HIST SAME') MT_wjet.Draw('HIST SAME') #make legend l1 = Legend([MT_tt, MT_qcd, MT_wjet], textfont=42, textsize=.03) l1.Draw() #save as pdf c1.SaveAs("MT.pdf")
def draw_HT(HT_tt, HT_qcd, HT_wjet): #set line colors HT_tt.SetLineColor(4) HT_qcd.SetLineColor(8) HT_wjet.SetLineColor(2) HT_tt.legendstyle = 'L' HT_qcd.legendstyle = 'L' HT_wjet.legendstyle = 'L' #begin drawing stuff c1 = Canvas() HT_wjet.SetStats(0) HT_wjet.Draw('HIST') HT_tt.Draw('HIST SAME') HT_qcd.Draw('HIST SAME') #make legend l1 = Legend([HT_tt, HT_qcd, HT_wjet], textfont=42, textsize=.03) l1.Draw() #save as pdf c1.SaveAs("HT.pdf")
def fit_pmt_channel(self, channelNumber, bypassApproval=False): print '\n\n---------- Automated fit of channel ' + str( channelNumber) + ' ----------\n\n' assert channelNumber >= 1 and channelNumber <= 17 and type( channelNumber) == type( 1), 'Improper channel number given - please check input' failStatus = -1 # grab required histograms bkgFile = self.lProcessedRootFiles[0] signalFile = self.lProcessedRootFiles[self.dLightLevels[channelNumber]] hBkg = bkgFile.Get('spe_' + str(channelNumber)) hSignal = signalFile.Get('spe_' + str(channelNumber)) hSignal.DrawStyle = 'hist' lowBkgTrial = -1e5 highBkgTrial = 8e5 lowBkgSubtractTrial = 1e6 highBkgSubtractTrial = 3.5e6 # used in setting limits of actual fits after trials trialWidthFactor = 2 individualFitSensitivity = 0.6 # step 1: find approximate location of peak via bkg subtraction fBkgGausTrial = root.TF1('fBkgGausTrial', 'gaus', self.lHistParameters[1], self.lHistParameters[2]) fitResult = hBkg.Fit(fBkgGausTrial, 'NQMELS+', '', lowBkgTrial, highBkgTrial) if fitResult.Status() % 10 != 0 or fitResult.Status( ) % 100 != 0 or fitResult.Status() % 1000 != 0: print 'Fit of bkg trial failed!!!' return failStatus bkgTrialMean = fBkgGausTrial.GetParameter(1) bkgTrialWidth = fBkgGausTrial.GetParameter(2) fBkgGaus = root.TF1('fBkgGaus', 'gaus', self.lHistParameters[1], self.lHistParameters[2]) fitResult = hBkg.Fit(fBkgGaus, 'NQSMEL+', '', bkgTrialMean - trialWidthFactor * bkgTrialWidth, bkgTrialMean + trialWidthFactor * bkgTrialWidth) if fitResult.Status() % 10 != 0 or fitResult.Status( ) % 100 != 0 or fitResult.Status() % 1000 != 0: print 'Fit of bkg failed!!!' return failStatus bkgMean = fBkgGaus.GetParameter(1) bkgWidth = fBkgGaus.GetParameter(2) bkgMeanErr = fBkgGaus.GetParError(1) bkgWidthErr = fBkgGaus.GetParError(2) # step 2: find approximate location of bkg peak in bkg file hBkgSubtract = hSignal.Clone('hBkgSubtract') hBkgSubtract.Add(hBkg, -1.) fBkgSubtractGausTrial = root.TF1('fBkgSubtractGausTrial', 'gaus', self.lHistParameters[1], self.lHistParameters[2]) fitResult = hBkgSubtract.Fit(fBkgSubtractGausTrial, 'NQSMEL+', '', lowBkgSubtractTrial, highBkgSubtractTrial) if fitResult.Status() % 10 != 0 or fitResult.Status( ) % 100 != 0 or fitResult.Status() % 1000 != 0: print 'Fit of bkg subtract trial failed!!!' return failStatus bkgSubtractTrialMean = fBkgSubtractGausTrial.GetParameter(1) bkgSubtractTrialWidth = fBkgSubtractGausTrial.GetParameter(2) lowEndFit = bkgSubtractTrialMean - trialWidthFactor * bkgSubtractTrialWidth highEndFit = bkgSubtractTrialMean + trialWidthFactor * bkgSubtractTrialWidth fBkgSubtractGaus = root.TF1('fBkgSubtractGaus', 'gaus', self.lHistParameters[1], self.lHistParameters[2]) fitResult = hBkgSubtract.Fit(fBkgSubtractGaus, 'NSMEL+', '', lowEndFit, highEndFit) if fitResult.Status() % 10 != 0 or fitResult.Status( ) % 100 != 0 or fitResult.Status() % 1000 != 0: print 'Fit of bkg subtract failed!!!' return failStatus # check bkg subtracted fit #hBkgSubtract.drawstyle = 'hist' #hBkgSubtract.Sumw2() hBkgSubtract.SetStats(0) c1 = Canvas() bkgSubtractMean = fBkgSubtractGaus.GetParameter(1) bkgSubtractWidth = fBkgSubtractGaus.GetParameter(2) bkgSubtractMeanErr = fBkgSubtractGaus.GetParError(1) bkgSubtractWidthErr = fBkgSubtractGaus.GetParError(2) bkgSubtractChi2 = fBkgSubtractGaus.GetChisquare() bkgSubtractNDF = fBkgSubtractGaus.GetNDF() hBkgSubtract.Draw('E1') hBkgSubtract.GetXaxis().SetRangeUser(0, 1.1 * highEndFit) fBkgSubtractGaus.Draw('same') meanNumPE = hBkgSubtract.Integral( hBkgSubtract.FindBin(0), hBkgSubtract.FindBin( self.lHistParameters[2])) / self.numEventsBeforeCuts fitInfo = 'Mean = %.3e +/- %.2e, #mu_{spe} = %.3f' % ( bkgSubtractMean, bkgSubtractMeanErr, meanNumPE) pt1 = root.TPaveText(.5, .85, .9, .9, 'blNDC"') text1 = pt1.AddText(fitInfo) pt1.SetTextColor(root.kAzure + 1) pt1.SetFillStyle(0) pt1.SetBorderSize(0) pt1.Draw('SAME') #c1.SetLogy() c1.Update() if not bypassApproval: print 'Please examine fit and if you approve please enter "y" otherwise press any other key.' approvalResponse = raw_input('Please enter response: ') if bypassApproval or approvalResponse == 'y': self.dFitParameters[channelNumber]['bkg_mean'] = bkgMean self.dFitParameters[channelNumber]['bkg_width'] = bkgWidth self.dFitParameters[channelNumber]['spe_mean'] = bkgSubtractMean self.dFitParameters[channelNumber]['spe_width'] = bkgSubtractWidth self.dFitParameters[channelNumber]['bkg_mean_err'] = bkgMeanErr self.dFitParameters[channelNumber]['bkg_width_err'] = bkgWidthErr self.dFitParameters[channelNumber][ 'spe_mean_err'] = bkgSubtractMeanErr self.dFitParameters[channelNumber][ 'spe_width_err'] = bkgSubtractWidthErr self.dFitParameters[channelNumber]['chi2'] = bkgSubtractChi2 self.dFitParameters[channelNumber]['ndf'] = bkgSubtractNDF self.dFitParameters[channelNumber]['mean_pe'] = meanNumPE if not os.path.isdir(self.sPathToHists + self.sNameOfSingleFile): os.mkdir(self.sPathToHists + self.sNameOfSingleFile) c1.SaveAs(self.sPathToHists + self.sNameOfSingleFile + '/gain_pmt_' + str(channelNumber) + '.png') return 1 else: return failStatus # ---------- step 3 (optional and unfinished - needs optimization) """
tl.Draw() # draw and update all legend.Draw() canvas.Modified() canvas.Update() # make file_name and directories if needed file_name = "{0:s}/{1:s}".format(args.output_path, h.path) print("Saving {0:s}... \r".format(file_name), end='\r') plot_tag = plots_path.get('plot tag', plots_config.get('plot tag', None)) if plot_tag is not None: file_name += '_'+plot_tag ensure_dir(file_name) for file_ext in args.file_ext: canvas.SaveAs("{0:s}.{1:s}".format(file_name, file_ext)) sys.stdout.flush() print("Saved {0:s} successfully.".format(file_name)) canvas.Close() del canvas if not args.debug: ROOT.gROOT.ProcessLine("gSystem->RedirectOutput(0);") except Exception, e: # stop redirecting if we crash as well if not args.debug: ROOT.gROOT.ProcessLine("gSystem->RedirectOutput(0);") logger.exception("{0}\nAn exception was caught!".format("-"*20))
def extract_Electron(f_tt, f_qcd, f_wjet): # get trees from files t_tt = f_tt.Get("Delphes") t_qcd = f_qcd.Get("Delphes") t_wjet = f_wjet.Get("Delphes") # get number of entries tt_n_entries = t_tt.GetEntries() qcd_n_entries = t_qcd.GetEntries() wjet_n_entries = t_wjet.GetEntries() # define leaves var_tt = "Electron.PT" var_qcd = "Electron.PT" var_wjet = "Electron.PT" leaf_tt = t_tt.GetLeaf(var_tt) leaf_qcd = t_qcd.GetLeaf(var_qcd) leaf_wjet = t_wjet.GetLeaf(var_wjet) # create the histograms numElectrons_tt = Hist(NBINS,NLO,NHI, title = 'numElectrons_tt', legendstyle = 'L') numElectrons_qcd = Hist(NBINS,NLO,NHI, title = 'numElectrons_qcd', legendstyle = 'L') numElectrons_wjet = Hist(NBINS,NLO,NHI, title = 'numElectrons_wjet', legendstyle = 'L') # interesting values to plot max_ept_per_event_tt = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Max ElectronPT/Event tt', legendstyle = 'L') min_ept_per_event_tt = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Min ElectronPT/Event tt', legendstyle = 'L') max_ept_per_event_qcd = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Max ElectronPT/Event qcd', legendstyle = 'L') min_ept_per_event_qcd = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Min ElectronPT/Event qcd', legendstyle = 'L') max_ept_per_event_wjet = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Max ElectronPT/Event wjet', legendstyle = 'L') min_ept_per_event_wjet = Hist(PT_NBINS,PT_NLO,PT_NHI, title = 'Min ElectronPT/Event wjet', legendstyle = 'L') # FILLING THE TREE fill_Electron_tree(tt_n_entries, t_tt, leaf_tt, numElectrons_tt, min_ept_per_event_tt, max_ept_per_event_tt) fill_Electron_tree(qcd_n_entries, t_qcd, leaf_qcd, numElectrons_qcd, min_ept_per_event_qcd, max_ept_per_event_qcd) fill_Electron_tree(wjet_n_entries, t_wjet, leaf_wjet, numElectrons_wjet, min_ept_per_event_wjet, max_ept_per_event_wjet) #set line colors numElectrons_tt.SetLineColor('blue') numElectrons_qcd.SetLineColor('green') numElectrons_wjet.SetLineColor('red') #begin drawing stuff c1 = Canvas() numElectrons_wjet.SetStats(0) numElectrons_wjet.Draw('HIST') numElectrons_tt.Draw('HIST SAME') numElectrons_qcd.Draw('HIST SAME') #make legend l1 = Legend([numElectrons_tt, numElectrons_qcd, numElectrons_wjet], textfont = 42, textsize = .03) l1.Draw() #save as pdf c1.SaveAs("../plots/ElectronPT_plots/numElectrons.pdf"); ################ MIN MAX STUFF # TT #set line colors max_ept_per_event_tt.SetLineColor('blue') min_ept_per_event_tt.SetLineColor('green') #begin drawing stuff c2 = Canvas() min_ept_per_event_tt.SetStats(0) min_ept_per_event_tt.Draw('HIST') max_ept_per_event_tt.Draw('HIST SAME') #make legend l2 = Legend([min_ept_per_event_tt, max_ept_per_event_tt], textfont = 42, textsize = .03) l2.Draw() #save as pdf c2.SaveAs("../plots/ElectronPT_plots/e_maxminpt_tt.pdf") # QCD #set line colors max_ept_per_event_qcd.SetLineColor('blue') min_ept_per_event_qcd.SetLineColor('green') #begin drawing stuff c3 = Canvas() max_ept_per_event_qcd.SetStats(0) max_ept_per_event_qcd.Draw('HIST') min_ept_per_event_qcd.Draw('HIST SAME') #make legend l3 = Legend([min_ept_per_event_qcd, max_ept_per_event_qcd], textfont = 42, textsize = .03) l3.Draw() #save as pdf c3.SaveAs("../plots/ElectronPT_plots/e_maxminpt_qcd.pdf") #WJET #set line colors max_ept_per_event_wjet.SetLineColor('blue') min_ept_per_event_wjet.SetLineColor('green') #begin drawing stuff c4 = Canvas() min_ept_per_event_wjet.SetStats(0) min_ept_per_event_wjet.Draw('HIST') max_ept_per_event_wjet.Draw('HIST SAME') #make legend l4 = Legend([min_ept_per_event_wjet, max_ept_per_event_wjet], textfont = 42, textsize = .03) l4.Draw() #save as pdf c4.SaveAs("../plots/ElectronPT_plots/e_maxminpt_wjet.pdf") #make the plots wait on screen wait(True)
plots_written = [] with io.open(layout_filename, 'r') as layout_file: log.info("Plotting all histograms") for path, subdirs, histos in layout_file.walk(class_pattern='TH1F'): for histo in histos: log.info("Plotting %s in %s", histo, path) data_histo = data_view.Get(os.path.join(path, histo)) mc_histo = mc_view.Get(os.path.join(path, histo)) log.info("Data histo has %f entries", data_histo.Integral()) mc_histo.Draw() data_histo.Draw('same') legend = Legend(7, leftmargin=0.5) legend.AddEntry(mc_histo) legend.AddEntry(data_histo) legend.SetBorderSize(0) legend.Draw() mc_histo.SetMaximum( 1.2 * max(mc_histo.GetMaximum(), data_histo.GetMaximum())) plot_filename = os.path.join( args.output, path.replace('/', '_') + '_' + histo + '.pdf') plots_written.append(plot_filename) canvas.SaveAs(plot_filename) with open(os.path.join(args.output, 'plot_list.txt'), 'w') as plot_list: for plot_filename in plots_written: plot_list.write(plot_filename + '\n')
_mcerr.SetMarkerSize(0) _mcerr.SetFillColor(ROOT.kGray + 2) _mcerr.SetFillStyle(3254) axis, limits = draw( [ _ratio, ], pad=subPad, xdivisions=args.xdiv, ylimits=(0, 2), ydivisions=5, ) _mcerr.Draw('same e2') if htitle and 'phi' in htitle: decorate_axis_pi(_ratio.xaxis) ratio_line = Line(_ratio.xaxis.GetXmin(), 1, _ratio.xaxis.GetXmax(), 1) ratio_line.color = 'red' ratio_line.linewidth = 2 ratio_line.Draw() #print (outdir) #c.SaveAs('{}/{}_{}_{}.pdf'.format(outdir, args.dataset, chan, hname)) c.SaveAs('{}/{}_{}_{}.png'.format(outdir, args.dataset, chan, hname)) #c.SaveAs('{}/{}_{}_{}_{}.pdf'.format(outdir, args.dataset, chan, hname,datetime.now().strftime('%y%m%d'))) #c.SaveAs('{}/{}_{}_{}_{}.png'.format(outdir, args.dataset, chan, hname,datetime.now().strftime('%y%m%d'))) #c.SaveAs('{}/{}_{}_{}_{}.png'.format(outdir, datetime.now().strftime('%y%m%d'), args.dataset, chan, hname,)) c.Clear() f.close()
ymax = g.get('ymax', None) if isinstance(ymax, dict): ymax = ymax[chan] for s in samples: histName = '{}__{}__{}'.format(s, chan, g['name']) h = getattr(f, histName).Clone() h.title = s h.drawstyle = 'ap' h.markersize = 0.4 draw(h, pad=c) if ymax is not None: h.GetHistogram().SetMaximum(ymax) legend = Legend([h], pad=c, leftmargin=0.05, margin=0.1, entryheight=0.02, textsize=12) legend.Draw() title = TitleAsLatex("[{}] {}".format(channelLatex, g['title'])) title.Draw() draw_labels('59.74 fb^{-1} (13 TeV)', cms_position='right', extra_text='work-in-progress') c.SaveAs('{}/{}.pdf'.format(outdir, histName)) c.Clear() f.Close()
class Plotter(object): def __init__(self, channel, year, plot_dir, base_dir, post_fix, selection_data, selection_mc, selection_tight, pandas_selection, lumi, model, transformation, features, process_signals, plot_signals, blinded, datacards=[], mini_signals=False, do_ratio=True, mc_subtraction=True, dir_suffix='', relaxed_mc_scaling=1., data_driven=True): self.channel = channel.split('_')[0] self.year = year self.full_channel = channel self.plt_dir = '/'.join( [plot_dir, channel, '_'.join([dir_suffix, get_time_str()])]) self.base_dir = base_dir self.post_fix = post_fix self.selection_data = ' & '.join(selection_data) self.selection_mc = ' & '.join(selection_mc) self.selection_tight = selection_tight self.pandas_selection = pandas_selection self.lumi = lumi self.model = model self.transformation = transformation self.features = features self.process_signals = process_signals self.plot_signals = plot_signals if self.process_signals else [] self.blinded = blinded self.selection_lnt = 'not (%s)' % self.selection_tight self.do_ratio = do_ratio self.mini_signals = mini_signals self.datacards = datacards self.mc_subtraction = mc_subtraction self.relaxed_mc_scaling = relaxed_mc_scaling self.data_driven = data_driven if self.year == 2018: from plotter.samples.samples_2018 import get_data_samples, get_mc_samples, get_signal_samples if self.year == 2017: from plotter.samples.samples_2017 import get_data_samples, get_mc_samples, get_signal_samples if self.year == 2016: from plotter.samples.samples_2016 import get_data_samples, get_mc_samples, get_signal_samples self.get_data_samples = get_data_samples self.get_mc_samples = get_mc_samples self.get_signal_samples = get_signal_samples def total_weight_calculator(self, df, weight_list, scalar_weights=[]): total_weight = df[weight_list[0]].to_numpy().astype(np.float) for iw in weight_list[1:]: total_weight *= df[iw].to_numpy().astype(np.float) for iw in scalar_weights: total_weight *= iw return total_weight def create_canvas(self, ratio=True): if ratio: self.canvas = Canvas(width=700, height=700) self.canvas.Draw() self.canvas.cd() self.main_pad = Pad(0., 0.25, 1., 1.) self.main_pad.Draw() self.canvas.cd() self.ratio_pad = Pad(0., 0., 1., 0.25) self.ratio_pad.Draw() self.main_pad.SetTicks(True) self.main_pad.SetBottomMargin(0.) self.main_pad.SetLeftMargin(0.15) self.main_pad.SetRightMargin(0.15) self.ratio_pad.SetLeftMargin(0.15) self.ratio_pad.SetRightMargin(0.15) self.ratio_pad.SetTopMargin(0.) self.ratio_pad.SetGridy() self.ratio_pad.SetBottomMargin(0.3) else: self.canvas = Canvas(width=700, height=700) self.canvas.Draw() self.canvas.cd() self.main_pad = Pad(0., 0., 1., 1.) self.main_pad.Draw() self.canvas.cd() self.ratio_pad = Pad(-1., -1., -.9, -.9) self.ratio_pad.Draw() # put it outside the canvas self.main_pad.SetTicks(True) self.main_pad.SetTopMargin(0.15) self.main_pad.SetBottomMargin(0.15) self.main_pad.SetLeftMargin(0.15) self.main_pad.SetRightMargin(0.15) def create_datacards(self, data, bkgs, signals, label, protect_empty_bins=['nonprompt']): ''' FIXME! For now this is specific to the data-driven case ''' # save a ROOT file with histograms, aka datacard datacard_dir = '/'.join([self.plt_dir, 'datacards']) makedirs(datacard_dir, exist_ok=True) outfile = ROOT.TFile.Open( '/'.join([datacard_dir, 'datacard_%s.root' % label]), 'recreate') outfile.cd() # data in tight data.name = 'data_obs' data.Write() # reads off a dictionary for bkg_name, bkg in bkgs.items(): bkg.name = bkg_name.split('#')[0] bkg.drawstyle = 'HIST E' bkg.color = 'black' bkg.linewidth = 2 # manual protection against empty bins, that would make combine crash if bkg_name in protect_empty_bins: for ibin in bkg.bins_range(): if bkg.GetBinContent(ibin) <= 0.: bkg.SetBinContent(ibin, 1e-2) bkg.SetBinError(ibin, np.sqrt(1e-2)) bkg.Write() # signals for isig in signals: isig.name = isig.name.split('#')[0] isig.drawstyle = 'HIST E' isig.color = 'black' isig.Write() # print out the txt datacard with open( '/'.join([ datacard_dir, 'datacard_%s_%s.txt' % (label, isig.name) ]), 'w') as card: card.write(''' imax 1 number of bins jmax * number of processes minus 1 kmax * number of nuisance parameters -------------------------------------------------------------------------------------------------------------------------------------------- shapes * {cat} datacard_{cat}.root $PROCESS $PROCESS_$SYSTEMATIC -------------------------------------------------------------------------------------------------------------------------------------------- bin {cat} observation {obs:d} -------------------------------------------------------------------------------------------------------------------------------------------- bin {cat} {cat} {cat} process {signal_name} nonprompt prompt process 0 1 2 rate {signal:.4f} {nonprompt:.4f} {prompt:.4f} -------------------------------------------------------------------------------------------------------------------------------------------- lumi lnN 1.025 - - norm_prompt_{ch}_{y}_{cat} lnN - - 1.15 norm_nonprompt_{ch}_{y}_{cat} lnN - 1.20 - norm_sig_{ch}_{y}_{cat} lnN 1.2 - - -------------------------------------------------------------------------------------------------------------------------------------------- {cat} autoMCStats 0 0 1 '''.format( cat=label, obs=int(data.integral()) if self.blinded == False else -1, signal_name=isig.name, signal=isig.integral(), ch=self.full_channel, y=self.year, prompt=bkgs['prompt'].integral(), nonprompt=bkgs['nonprompt'].integral(), )) outfile.Close() def plot(self): evaluator = Evaluator(self.model, self.transformation, self.features) makedirs(self.plt_dir, exist_ok=True) makedirs('/'.join([self.plt_dir, 'lin']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'log']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lin', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lin', 'root']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'log', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'log', 'root']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'lin']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'log']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'lin', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'lin', 'root']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'log', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'lnt_region', 'log', 'root']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'lin']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'log']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'lin', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'lin', 'root']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'log', 'png']), exist_ok=True) makedirs('/'.join([self.plt_dir, 'shapes', 'log', 'root']), exist_ok=True) # NN evaluator print('============> starting reading the trees') print('Plots will be stored in: ', self.plt_dir) now = time() signal = [] if self.process_signals: signal = self.get_signal_samples(self.channel, self.base_dir, self.post_fix, self.selection_data, mini=self.mini_signals) else: signal = [] data = self.get_data_samples(self.channel, self.base_dir, self.post_fix, self.selection_data) mc = self.get_mc_samples(self.channel, self.base_dir, self.post_fix, self.selection_mc) print('============> it took %.2f seconds' % (time() - now)) # evaluate FR for isample in (mc + data): #+signal): isample.df['fr'] = evaluator.evaluate(isample.df) # already corrected, ready to be applied in lnt-not-tight isample.df['fr_corr'] = isample.df['fr'] / (1. - isample.df['fr']) # apply an extra selection to the pandas dataframes if len(self.pandas_selection): for isample in (mc + data + signal): isample.df = isample.df.query(self.pandas_selection) # split the dataframe in tight and lnt-not-tight (called simply lnt for short) print('============> splitting dataframe in tight and loose not tight') for isample in (mc + data + signal): isample.df_tight = isample.df.query(self.selection_tight) if isample not in signal: isample.df_lnt = isample.df.query(self.selection_lnt) # free some mem del isample.df gc.collect() print('============> ... done') # sort depending on their position in the stack mc.sort(key=lambda x: x.position_in_stack) # now we plot self.create_canvas(self.do_ratio) for ivar in variables: variable, bins, label, xlabel, ylabel, extra_sel = ivar.var, ivar.bins, ivar.label, ivar.xlabel, ivar.ylabel, ivar.extra_selection print('plotting', label) ###################################################################################### # plot MC stacks, in tight and lnt ###################################################################################### stack_prompt = [] stack_nonprompt = [] stack_nonprompt_control = [] for imc in mc: if extra_sel: mc_df_tight = imc.df_tight.query(extra_sel) mc_df_lnt = imc.df_lnt.query(extra_sel) else: mc_df_tight = imc.df_tight mc_df_lnt = imc.df_lnt histo_tight = Hist(bins, title=imc.label, markersize=0, legendstyle='F', name=imc.datacard_name + '#' + label + '#t') weights = self.total_weight_calculator( mc_df_tight, ['weight'] + imc.extra_signal_weights, [self.lumi, imc.lumi_scaling]) histo_tight.fill_array(mc_df_tight[variable], weights=weights * self.relaxed_mc_scaling) histo_tight.fillstyle = 'solid' histo_tight.fillcolor = 'steelblue' if self.data_driven else imc.colour histo_tight.linewidth = 0 stack_prompt.append(histo_tight) # optionally remove the MC subtraction in loose-not-tight # may help if MC stats is terrible (and it often is) if self.data_driven: if self.mc_subtraction: histo_lnt = Hist(bins, title=imc.label, markersize=0, legendstyle='F', name=imc.datacard_name + '#' + label + '#lnt') weights = self.total_weight_calculator( mc_df_lnt, ['weight', 'fr_corr'] + imc.extra_signal_weights, [-1., self.lumi, imc.lumi_scaling]) histo_lnt.fill_array(mc_df_lnt[variable], weights=weights * self.relaxed_mc_scaling) histo_lnt.fillstyle = 'solid' histo_lnt.fillcolor = 'skyblue' if self.data_driven else imc.colour histo_lnt.linewidth = 0 stack_nonprompt.append(histo_lnt) histo_lnt_control = Hist(bins, title=imc.label, markersize=0, legendstyle='F', name=imc.datacard_name + '#' + label + '#lntcontrol') weights_control = self.total_weight_calculator( mc_df_lnt, ['weight'] + imc.extra_signal_weights, [self.lumi, imc.lumi_scaling]) histo_lnt_control.fill_array(mc_df_lnt[variable], weights=weights_control * self.relaxed_mc_scaling) histo_lnt_control.fillstyle = 'solid' histo_lnt_control.fillcolor = imc.colour histo_lnt_control.linewidth = 0 # print(histo_lnt_control) # print(histo_lnt_control.fillcolor) # print(imc.name, imc.colour) # print(histo_lnt_control.integral()) stack_nonprompt_control.append(histo_lnt_control) # merge different samples together (add the histograms) # prepare two temporary containers for the post-grouping histograms stack_prompt_tmp = [] stack_nonprompt_tmp = [] stack_nonprompt_control_tmp = [] for ini, fin in [(stack_prompt, stack_prompt_tmp), (stack_nonprompt, stack_nonprompt_tmp), (stack_nonprompt_control, stack_nonprompt_control_tmp)]: for k, v in groups.items(): grouped = [] for ihist in ini: if ihist.name.split('#')[0] in v: grouped.append(ihist) elif ihist.name.split('#')[0] not in togroup: fin.append(ihist) if len(grouped): group = sum(grouped) group.title = k group.name = '#'.join([k] + ihist.name.split('#')[1:]) group.fillstyle = grouped[0].fillstyle group.fillcolor = grouped[0].fillcolor group.linewidth = grouped[0].linewidth fin.append(group) stack_prompt = stack_prompt_tmp stack_nonprompt = stack_nonprompt_tmp stack_nonprompt_control = stack_nonprompt_control_tmp ###################################################################################### # plot the signals ###################################################################################### all_signals = [] signals_to_plot = [] for isig in signal: if variable not in self.datacards: if not isig.toplot: continue if variable == 'fr' or variable == 'fr_corr': continue if extra_sel: isig_df_tight = isig.df_tight.query(extra_sel) else: isig_df_tight = isig.df_tight histo_tight = Hist( bins, title=isig.label, markersize=0, legendstyle='L', name=isig.datacard_name + '#' + label ) # the "#" thing is a trick to give hists unique name, else ROOT complains weights = self.total_weight_calculator( isig_df_tight, ['weight'] + isig.extra_signal_weights, [self.lumi, isig.lumi_scaling]) histo_tight.fill_array(isig_df_tight[variable], weights=weights) histo_tight.color = isig.colour histo_tight.fillstyle = 'hollow' histo_tight.linewidth = 2 histo_tight.linestyle = 'dashed' histo_tight.drawstyle = 'HIST' all_signals.append(histo_tight) if isig.toplot: signals_to_plot.append(histo_tight) ###################################################################################### # plot the data ###################################################################################### data_prompt = [] data_nonprompt = [] data_nonprompt_control = [] for idata in data: if extra_sel: idata_df_tight = idata.df_tight.query(extra_sel) idata_df_lnt = idata.df_lnt.query(extra_sel) else: idata_df_tight = idata.df_tight idata_df_lnt = idata.df_lnt histo_tight = Hist(bins, title=idata.label, markersize=1, legendstyle='LEP') histo_tight.fill_array(idata_df_tight[variable]) data_prompt.append(histo_tight) if self.data_driven: histo_lnt = Hist(bins, title=idata.label, markersize=0, legendstyle='F') histo_lnt.fill_array(idata_df_lnt[variable], weights=idata_df_lnt.fr_corr) histo_lnt.fillstyle = 'solid' histo_lnt.fillcolor = 'skyblue' histo_lnt.linewidth = 0 histo_lnt_control = Hist(bins, title=idata.label, markersize=1, legendstyle='LEP') histo_lnt_control.fill_array(idata_df_lnt[variable]) data_nonprompt.append(histo_lnt) data_nonprompt_control.append(histo_lnt_control) if self.data_driven: # put the prompt backgrounds together all_exp_prompt = sum(stack_prompt) all_exp_prompt.title = 'prompt' # put the nonprompt backgrounds together all_exp_nonprompt = sum(stack_nonprompt + data_nonprompt) all_exp_nonprompt.fillstyle = 'solid' all_exp_nonprompt.fillcolor = 'skyblue' all_exp_nonprompt.linewidth = 0 all_exp_nonprompt.title = 'nonprompt' # create the stacks stack = HistStack([all_exp_prompt, all_exp_nonprompt], drawstyle='HIST', title='') stack_control = HistStack(stack_nonprompt_control, drawstyle='HIST', title='') else: stack = HistStack(stack_prompt, drawstyle='HIST', title='') # stat uncertainty hist_error = stack.sum #sum([all_exp_prompt, all_exp_nonprompt]) hist_error.drawstyle = 'E2' hist_error.fillstyle = '/' hist_error.color = 'gray' hist_error.title = 'stat. unc.' hist_error.legendstyle = 'F' if self.data_driven: hist_error_control = stack_control.sum hist_error_control.drawstyle = 'E2' hist_error_control.fillstyle = '/' hist_error_control.color = 'gray' hist_error_control.title = 'stat. unc.' hist_error_control.legendstyle = 'F' # put the data together all_obs_prompt = sum(data_prompt) all_obs_prompt.title = 'observed' if self.data_driven: all_obs_nonprompt_control = sum(data_nonprompt_control) all_obs_nonprompt_control.title = 'observed' all_obs_nonprompt_control.drawstyle = 'EP' # prepare the legend print(signals_to_plot) for jj in signals_to_plot: print(jj.name, jj.integral()) if len(signals_to_plot): legend = Legend([all_obs_prompt, stack, hist_error], pad=self.main_pad, leftmargin=0., rightmargin=0., topmargin=0., textfont=42, textsize=0.025, entrysep=0.01, entryheight=0.04) legend_signals = Legend(signals_to_plot, pad=self.main_pad, leftmargin=0., rightmargin=0., topmargin=0., textfont=42, textsize=0.025, entrysep=0.01, entryheight=0.04) legend_signals.SetBorderSize(0) legend_signals.x1 = 0.42 legend_signals.y1 = 0.74 legend_signals.x2 = 0.88 legend_signals.y2 = 0.90 legend_signals.SetFillColor(0) legend.SetBorderSize(0) legend.x1 = 0.2 legend.y1 = 0.74 legend.x2 = 0.45 legend.y2 = 0.90 legend.SetFillColor(0) else: legend = Legend([all_obs_prompt, stack, hist_error], pad=self.main_pad, leftmargin=0., rightmargin=0., topmargin=0., textfont=42, textsize=0.03, entrysep=0.01, entryheight=0.04) legend.SetBorderSize(0) legend.x1 = 0.55 legend.y1 = 0.74 legend.x2 = 0.88 legend.y2 = 0.90 legend.SetFillColor(0) # plot with ROOT, linear and log scale for islogy in [False, True]: things_to_plot = [stack, hist_error] if not self.blinded: things_to_plot.append(all_obs_prompt) # plot signals, as an option if self.plot_signals: things_to_plot += signals_to_plot # set the y axis range # FIXME! setting it by hand to each object as it doesn't work if passed to draw if islogy: yaxis_max = 40. * max( [ithing.max() for ithing in things_to_plot]) else: yaxis_max = 1.65 * max( [ithing.max() for ithing in things_to_plot]) if islogy: yaxis_min = 0.01 else: yaxis_min = 0. for ithing in things_to_plot: ithing.SetMaximum(yaxis_max) draw(things_to_plot, xtitle=xlabel, ytitle=ylabel, pad=self.main_pad, logy=islogy) # expectation uncertainty in the ratio pad ratio_exp_error = Hist(bins) ratio_data = Hist(bins) for ibin in hist_error.bins_range(): ratio_exp_error.set_bin_content(ibin, 1.) ratio_exp_error.set_bin_error( ibin, hist_error.get_bin_error(ibin) / hist_error.get_bin_content(ibin) if hist_error.get_bin_content(ibin) != 0. else 0.) ratio_data.set_bin_content( ibin, all_obs_prompt.get_bin_content(ibin) / hist_error.get_bin_content(ibin) if hist_error.get_bin_content(ibin) != 0. else 0.) ratio_data.set_bin_error( ibin, all_obs_prompt.get_bin_error(ibin) / hist_error.get_bin_content(ibin) if hist_error.get_bin_content(ibin) != 0. else 0.) ratio_data.drawstyle = 'EP' ratio_data.title = '' ratio_exp_error.drawstyle = 'E2' ratio_exp_error.markersize = 0 ratio_exp_error.title = '' ratio_exp_error.fillstyle = '/' ratio_exp_error.color = 'gray' for ithing in [ratio_data, ratio_exp_error]: ithing.xaxis.set_label_size( ithing.xaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_label_size( ithing.yaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.xaxis.set_title_size( ithing.xaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_title_size( ithing.yaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_ndivisions(405) ithing.yaxis.set_title_offset(0.4) things_to_plot = [ratio_exp_error] if not self.blinded: things_to_plot.append(ratio_data) draw(things_to_plot, xtitle=xlabel, ytitle='obs/exp', pad=self.ratio_pad, logy=False, ylimits=(0.5, 1.5)) line = ROOT.TLine(min(bins), 1., max(bins), 1.) line.SetLineColor(ROOT.kBlack) line.SetLineWidth(1) self.ratio_pad.cd() line.Draw('same') # chi2_score_text = '\chi^{2}/NDF = %.1f' %(all_obs_prompt.Chi2Test(hist_error, 'UW CHI2/NDF')) chi2_score_text = 'p-value = %.2f' % (all_obs_prompt.Chi2Test( hist_error, 'UW')) chi2_score = ROOT.TLatex(0.7, 0.81, chi2_score_text) chi2_score.SetTextFont(43) chi2_score.SetTextSize(15) chi2_score.SetNDC() chi2_score.Draw('same') self.canvas.cd() # FIXME! add SS and OS channels if self.full_channel == 'mmm': channel = '\mu\mu\mu' elif self.full_channel == 'eee': channel = 'eee' elif self.full_channel == 'mem_os': channel = '\mu^{\pm}\mu^{\mp}e' elif self.full_channel == 'mem_ss': channel = '\mu^{\pm}\mu^{\pm}e' elif self.full_channel == 'eem_os': channel = 'e^{\pm}e^{\mp}\mu' elif self.full_channel == 'eem_ss': channel = 'e^{\pm}e^{\pm}\mu' else: assert False, 'ERROR: Channel not valid.' finalstate = ROOT.TLatex(0.68, 0.68, channel) finalstate.SetTextFont(43) finalstate.SetTextSize(25) finalstate.SetNDC() finalstate.Draw('same') self.canvas.cd() # remove old legend for iprim in self.canvas.primitives: if isinstance(iprim, Legend): self.canvas.primitives.remove(iprim) legend.Draw('same') if self.plot_signals: legend_signals.Draw('same') CMS_lumi(self.main_pad, 4, 0, lumi_13TeV="%d, L = %.1f fb^{-1}" % (self.year, self.lumi / 1000.)) self.canvas.Modified() self.canvas.Update() for iformat in ['pdf', 'png', 'root']: self.canvas.SaveAs('/'.join([ self.plt_dir, 'log' if islogy else 'lin', iformat if iformat != 'pdf' else '', '%s%s.%s' % (label, '_log' if islogy else '_lin', iformat) ])) # plot distributions in loose not tight # check MC contamination there if self.data_driven and variable not in ['fr', 'fr_corr']: things_to_plot = [ stack_control, hist_error_control, all_obs_nonprompt_control ] # set the y axis range # FIXME! setting it by hand to each object as it doesn't work if passed to draw if islogy: yaxis_max = 40. * max( [ithing.max() for ithing in things_to_plot]) else: yaxis_max = 1.65 * max( [ithing.max() for ithing in things_to_plot]) if islogy: yaxis_min = 0.01 else: yaxis_min = 0. for ithing in things_to_plot: ithing.SetMaximum(yaxis_max) ithing.SetMinimum(yaxis_min) draw(things_to_plot, xtitle=xlabel, ytitle=ylabel, pad=self.main_pad, logy=islogy, ylimits=(yaxis_min, yaxis_max)) new_legend = Legend( stack_control.hists + [hist_error_control, all_obs_nonprompt_control], pad=self.main_pad, leftmargin=0., rightmargin=0., topmargin=0., textfont=42, textsize=0.03, entrysep=0.01, entryheight=0.04) new_legend.SetBorderSize(0) new_legend.x1 = 0.55 new_legend.y1 = 0.71 new_legend.x2 = 0.88 new_legend.y2 = 0.90 new_legend.SetFillColor(0) # divide MC to subtract by data stack_nonprompt_control_scaled_list = [] for ihist in stack_control.hists: new_hist = copy(ihist) for ibin in new_hist.bins_range(): new_hist.SetBinContent( ibin, np.nan_to_num( np.divide( new_hist.GetBinContent(ibin), all_obs_nonprompt_control. GetBinContent(ibin)))) new_hist.SetBinError( ibin, np.nan_to_num( np.divide( new_hist.GetBinError(ibin), all_obs_nonprompt_control. GetBinContent(ibin)))) stack_nonprompt_control_scaled_list.append(new_hist) stack_control_scaled = HistStack( stack_nonprompt_control_scaled_list, drawstyle='HIST', title='') stack_control_scaled_err = stack_control_scaled.sum stack_control_scaled_err.drawstyle = 'E2' stack_control_scaled_err.fillstyle = '/' stack_control_scaled_err.color = 'gray' stack_control_scaled_err.title = 'stat. unc.' stack_control_scaled_err.legendstyle = 'F' draw([stack_control_scaled, stack_control_scaled_err], xtitle=xlabel, ytitle='MC/data', pad=self.ratio_pad, logy=False) stack_control_scaled.xaxis.set_label_size( stack_control_scaled.xaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio stack_control_scaled.yaxis.set_label_size( stack_control_scaled.yaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio stack_control_scaled.xaxis.set_title_size( stack_control_scaled.xaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio stack_control_scaled.yaxis.set_title_size( stack_control_scaled.yaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio stack_control_scaled.yaxis.set_ndivisions(405) stack_control_scaled.yaxis.set_title_offset(0.4) stack_control_scaled.SetMinimum(0.) stack_control_scaled.SetMaximum(1.5) CMS_lumi(self.main_pad, 4, 0, lumi_13TeV="%d, L = %.1f fb^{-1}" % (self.year, self.lumi / 1000.)) self.canvas.cd() # remove old legend for iprim in self.canvas.primitives: if isinstance(iprim, Legend): self.canvas.primitives.remove(iprim) # draw new legend new_legend.Draw('same') self.canvas.Modified() self.canvas.Update() for iformat in ['pdf', 'png', 'root']: self.canvas.SaveAs('/'.join([ self.plt_dir, 'lnt_region', 'log' if islogy else 'lin', iformat if iformat != 'pdf' else '', '%s%s.%s' % (label, '_log' if islogy else '_lin', iformat) ])) # compare shapes in tight and loose not tight # data in tight all_obs_prompt_norm = copy(all_obs_prompt) if all_obs_prompt_norm.integral() != 0: all_obs_prompt_norm.Scale( np.nan_to_num( np.divide(1., all_obs_prompt_norm.integral()))) #import pdb; pdb.set_trace() all_obs_prompt_norm.drawstyle = 'hist e' all_obs_prompt_norm.linecolor = 'black' all_obs_prompt_norm.markersize = 0 all_obs_prompt_norm.legendstyle = 'LE' all_obs_prompt_norm.title = '' all_obs_prompt_norm.label = 'data - tight' # data MC subtracted in loose all_obs_prompt_mc_sub_norm = copy(all_obs_prompt) all_obs_prompt_mc_sub_norm.add(all_exp_prompt, -1) all_obs_prompt_mc_sub_norm.Scale( np.nan_to_num( np.divide(1., all_obs_prompt_mc_sub_norm.integral()))) all_obs_prompt_mc_sub_norm.drawstyle = 'hist e' all_obs_prompt_mc_sub_norm.linecolor = 'green' all_obs_prompt_mc_sub_norm.markersize = 0 all_obs_prompt_mc_sub_norm.legendstyle = 'LE' all_obs_prompt_mc_sub_norm.title = '' all_obs_prompt_mc_sub_norm.label = '(data-MC) - tight' # data in loose all_obs_nonprompt_control_norm = copy( all_obs_nonprompt_control) all_obs_nonprompt_control_norm.Scale( np.nan_to_num( np.divide( 1., all_obs_nonprompt_control_norm.integral()))) all_obs_nonprompt_control_norm.drawstyle = 'hist e' all_obs_nonprompt_control_norm.linecolor = 'red' all_obs_nonprompt_control_norm.markersize = 0 all_obs_nonprompt_control_norm.legendstyle = 'LE' all_obs_nonprompt_control_norm.title = '' all_obs_nonprompt_control_norm.label = 'data - l-n-t' # data MC subtracted in loose all_obs_nonprompt_control_mc_sub_norm = copy( all_obs_nonprompt_control) all_obs_nonprompt_control_mc_sub_norm.add( stack_control.sum, -1) all_obs_nonprompt_control_mc_sub_norm.Scale( np.nan_to_num( np.divide( 1., all_obs_nonprompt_control_mc_sub_norm.integral( )))) all_obs_nonprompt_control_mc_sub_norm.drawstyle = 'hist e' all_obs_nonprompt_control_mc_sub_norm.linecolor = 'blue' all_obs_nonprompt_control_mc_sub_norm.markersize = 0 all_obs_nonprompt_control_mc_sub_norm.legendstyle = 'LE' all_obs_nonprompt_control_mc_sub_norm.title = '' all_obs_nonprompt_control_mc_sub_norm.label = '(data-MC) - l-n-t' things_to_plot = [ all_obs_prompt_norm, all_obs_prompt_mc_sub_norm, all_obs_nonprompt_control_norm, all_obs_nonprompt_control_mc_sub_norm, ] yaxis_max = max([ii.GetMaximum() for ii in things_to_plot]) draw(things_to_plot, xtitle=xlabel, ytitle=ylabel, pad=self.main_pad, logy=islogy, ylimits=(yaxis_min, 1.55 * yaxis_max)) self.canvas.cd() # remove old legend for iprim in self.canvas.primitives: if isinstance(iprim, Legend): self.canvas.primitives.remove(iprim) shape_legend = Legend([], pad=self.main_pad, leftmargin=0., rightmargin=0., topmargin=0., textfont=42, textsize=0.03, entrysep=0.01, entryheight=0.04) shape_legend.AddEntry(all_obs_prompt_norm, all_obs_prompt_norm.label, all_obs_prompt_norm.legendstyle) shape_legend.AddEntry( all_obs_prompt_mc_sub_norm, all_obs_prompt_mc_sub_norm.label, all_obs_prompt_mc_sub_norm.legendstyle) shape_legend.AddEntry( all_obs_nonprompt_control_norm, all_obs_nonprompt_control_norm.label, all_obs_nonprompt_control_norm.legendstyle) shape_legend.AddEntry( all_obs_nonprompt_control_mc_sub_norm, all_obs_nonprompt_control_mc_sub_norm.label, all_obs_nonprompt_control_mc_sub_norm.legendstyle) shape_legend.SetBorderSize(0) shape_legend.x1 = 0.50 shape_legend.y1 = 0.71 shape_legend.x2 = 0.88 shape_legend.y2 = 0.90 shape_legend.SetFillColor(0) shape_legend.Draw('same') # plot ratios all_obs_prompt_norm_ratio = copy(all_obs_prompt_norm) all_obs_prompt_mc_sub_norm_ratio = copy( all_obs_prompt_mc_sub_norm) all_obs_nonprompt_control_norm_ratio = copy( all_obs_nonprompt_control_norm) all_obs_nonprompt_control_mc_sub_norm_ratio = copy( all_obs_nonprompt_control_mc_sub_norm) all_obs_prompt_norm_ratio.Divide( all_obs_prompt_mc_sub_norm_ratio) all_obs_nonprompt_control_norm_ratio.Divide( all_obs_prompt_mc_sub_norm_ratio) all_obs_nonprompt_control_mc_sub_norm_ratio.Divide( all_obs_prompt_mc_sub_norm_ratio) things_to_plot_ratio = [ all_obs_prompt_norm_ratio, all_obs_nonprompt_control_norm_ratio, all_obs_nonprompt_control_mc_sub_norm_ratio, ] for ithing in things_to_plot_ratio: ithing.xaxis.set_label_size( ithing.xaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_label_size( ithing.yaxis.get_label_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.xaxis.set_title_size( ithing.xaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_title_size( ithing.yaxis.get_title_size() * 3. ) # the scale should match that of the main/ratio pad size ratio ithing.yaxis.set_ndivisions(405) ithing.yaxis.set_title_offset(0.4) ithing.SetMinimum(0.) ithing.SetMaximum(2.) draw(things_to_plot_ratio, xtitle=xlabel, ytitle='1/(data-MC)_{tight}', pad=self.ratio_pad, logy=False, ylimits=(0., 2.)) self.ratio_pad.cd() line.Draw('same') CMS_lumi(self.main_pad, 4, 0, lumi_13TeV="%d, L = %.1f fb^{-1}" % (self.year, self.lumi / 1000.)) self.canvas.Modified() self.canvas.Update() for iformat in ['pdf', 'png', 'root']: self.canvas.SaveAs('/'.join([ self.plt_dir, 'shapes', 'log' if islogy else 'lin', iformat if iformat != 'pdf' else '', '%s%s.%s' % (label, '_log' if islogy else '_lin', iformat) ])) # save only the datacards you want, don't flood everything if len(self.datacards) and label not in self.datacards: continue # FIXME! allow it to save datacards even for non data driven bkgs if self.data_driven: self.create_datacards(data=all_obs_prompt, bkgs={ 'prompt': all_exp_prompt, 'nonprompt': all_exp_nonprompt }, signals=all_signals, label=label)
def manual_fit(self, channelNumber): print '\n\n---------- Manual fit of channel ' + str( channelNumber) + ' ----------\n\n' assert channelNumber >= 1 and channelNumber <= 17 and type( channelNumber) == type( 1), 'Improper channel number given - please check input' failStatus = -1 # grab required histograms bkgFile = self.lProcessedRootFiles[0] signalFile = self.lProcessedRootFiles[self.dLightLevels[channelNumber]] hBkg = bkgFile.Get('spe_' + str(channelNumber)) hSignal = signalFile.Get('spe_' + str(channelNumber)) hSignal.DrawStyle = 'hist' lowBkgTrial = -10e5 highBkgTrial = 10e5 lowBkgSubtractTrial = 1e6 highBkgSubtractTrial = 3.5e6 # used in setting limits of actual fits after trials trialWidthFactor = 1 individualFitSensitivity = 0.6 # repeat procedure from before # background should never fail fBkgGausTrial = root.TF1('fBkgGausTrial', 'gaus', self.lHistParameters[1], self.lHistParameters[2]) fitResult = hBkg.Fit(fBkgGausTrial, 'NQMELS+', '', lowBkgTrial, highBkgTrial) if fitResult.Status() % 10 != 0 or fitResult.Status( ) % 100 != 0 or fitResult.Status() % 1000 != 0: print 'Fit of bkg trial failed!!!' return failStatus bkgTrialMean = fBkgGausTrial.GetParameter(1) bkgTrialWidth = fBkgGausTrial.GetParameter(2) fBkgGaus = root.TF1('fBkgGaus', 'gaus', self.lHistParameters[1], self.lHistParameters[2]) fitResult = hBkg.Fit(fBkgGaus, 'NSQMEL+', '', bkgTrialMean - trialWidthFactor * bkgTrialWidth, bkgTrialMean + trialWidthFactor * bkgTrialWidth) if fitResult.Status() % 10 != 0 or fitResult.Status( ) % 100 != 0 or fitResult.Status() % 1000 != 0: print 'Fit of bkg failed!!!' return failStatus bkgMean = fBkgGaus.GetParameter(1) bkgWidth = fBkgGaus.GetParameter(2) bkgMeanErr = fBkgGaus.GetParError(1) bkgWidthErr = fBkgGaus.GetParError(2) # move to bkg subtract default hBkgSubtract = hSignal.Clone('hBkgSubtract') hBkgSubtract.Add(hBkg, -1.) fBkgSubtractGausTrial = root.TF1('fBkgSubtractGausTrial', 'gaus', self.lHistParameters[1], self.lHistParameters[2]) fitResult = hBkgSubtract.Fit(fBkgSubtractGausTrial, 'NQSMEL+', '', lowBkgSubtractTrial, highBkgSubtractTrial) if fitResult.Status() % 10 != 0 or fitResult.Status( ) % 100 != 0 or fitResult.Status() % 1000 != 0: print 'Fit of bkg subtract trial failed!!!' bkgSubtractTrialMean = fBkgSubtractGausTrial.GetParameter(1) bkgSubtractTrialWidth = fBkgSubtractGausTrial.GetParameter(2) lowEndFit = bkgSubtractTrialMean - trialWidthFactor * bkgSubtractTrialWidth highEndFit = bkgSubtractTrialMean + trialWidthFactor * bkgSubtractTrialWidth fBkgSubtractGaus = root.TF1('fBkgSubtractGaus', 'gaus', self.lHistParameters[1], self.lHistParameters[2]) c1 = Canvas() dFitParametersToChange = { 'lower_limit_fit': lowEndFit, 'upper_limit_fit': highEndFit } while 1: fitResult = hBkgSubtract.Fit( fBkgSubtractGaus, 'NSMEL+', '', float(dFitParametersToChange['lower_limit_fit']), float(dFitParametersToChange['upper_limit_fit'])) hBkgSubtract.SetStats(0) hBkgSubtract.Draw('E1') hBkgSubtract.GetXaxis().SetRangeUser(0, 1.1 * highEndFit) fBkgSubtractGaus.Draw('same') bkgSubtractMean = fBkgSubtractGaus.GetParameter(1) bkgSubtractWidth = fBkgSubtractGaus.GetParameter(2) bkgSubtractMeanErr = fBkgSubtractGaus.GetParError(1) bkgSubtractWidthErr = fBkgSubtractGaus.GetParError(2) bkgSubtractChi2 = fBkgSubtractGaus.GetChisquare() bkgSubtractNDF = fBkgSubtractGaus.GetNDF() meanNumPE = hBkgSubtract.Integral( hBkgSubtract.FindBin(0), hBkgSubtract.FindBin( self.lHistParameters[2])) / self.numEventsBeforeCuts fitInfo = 'Mean = %.3e +/- %.2e, #mu_{spe} = %.3f' % ( bkgSubtractMean, bkgSubtractMeanErr, meanNumPE) pt1 = root.TPaveText(.5, .85, .9, .9, 'blNDC"') text1 = pt1.AddText(fitInfo) pt1.SetTextColor(root.kAzure + 1) pt1.SetFillStyle(0) pt1.SetBorderSize(0) pt1.Draw('SAME') #c1.SetLogy() c1.Update() acceptFit = False if fitResult.Status() % 10 != 0 or fitResult.Status( ) % 100 != 0 or fitResult.Status() % 1000 != 0: print 'Fit failed so must try new values or quit.' dFitParametersToChange = self.change_parameter_value_subroutine( dFitParametersToChange) else: print 'Would you like to keep this fit, quit out (if spectrum is unlikely to ever give converging fit), or try to fit with new values?' print 'Type "y" to accept the fit.' print 'Type "q" to quit this channel\'s manual fit' print 'Press enter to try new parameters' response = raw_input('Please enter response: ') if response == 'y': self.dFitParameters[channelNumber]['bkg_mean'] = bkgMean self.dFitParameters[channelNumber]['bkg_width'] = bkgWidth self.dFitParameters[channelNumber][ 'spe_mean'] = bkgSubtractMean self.dFitParameters[channelNumber][ 'spe_width'] = bkgSubtractWidth self.dFitParameters[channelNumber][ 'bkg_mean_err'] = bkgMeanErr self.dFitParameters[channelNumber][ 'bkg_width_err'] = bkgWidthErr self.dFitParameters[channelNumber][ 'spe_mean_err'] = bkgSubtractMeanErr self.dFitParameters[channelNumber][ 'spe_width_err'] = bkgSubtractWidthErr self.dFitParameters[channelNumber][ 'chi2'] = bkgSubtractChi2 self.dFitParameters[channelNumber]['ndf'] = bkgSubtractNDF self.dFitParameters[channelNumber]['mean_pe'] = meanNumPE if not os.path.isdir(self.sPathToHists + self.sNameOfSingleFile): os.mkdir(self.sPathToHists + self.sNameOfSingleFile) c1.SaveAs(self.sPathToHists + self.sNameOfSingleFile + '/gain_pmt_' + str(channelNumber) + '.png') return 1 elif response == 'q': del self.dFitParameters[channelNumber] return failStatus else: dFitParametersToChange = self.change_parameter_value_subroutine( dFitParametersToChange)
def plot_gain_vs_time(self, channelNumber): # take graph parameters from led_datasets lGraphRangeY = neriX_led_datasets.lGraphRangeY # grab list of parameters from pickle dictionary # and sort them for graph aUnsortedTimes = array.array('d') aUnsortedTimesErr = array.array('d') aUnsortedGains = array.array('d') aUnsortedGainsErr = array.array('d') aTimes = array.array('d') aTimesErr = array.array('d') aGains = array.array('d') aGainsErr = array.array('d') numElements = 0 for time in self.dCompletedFitParameters[self.runNumber]: try: aUnsortedGains.append(self.dCompletedFitParameters[ self.runNumber][time][channelNumber]['spe_mean']) aUnsortedGainsErr.append(self.dCompletedFitParameters[ self.runNumber][time][channelNumber]['spe_mean_err']) except KeyError: continue aUnsortedTimes.append(time) aUnsortedTimesErr.append(0) numElements += 1 if len(aUnsortedTimes) == 0: return 0 points = zip(aUnsortedTimes, aUnsortedGains, aUnsortedTimesErr, aUnsortedGainsErr) sorted_points = sorted(points) for point in sorted_points: aTimes.append(point[0]) aGains.append(point[1]) aTimesErr.append(point[2]) aGainsErr.append(point[3]) # now that points are sorted create graph c1 = Canvas() gGainVsTime = root.TGraphErrors(numElements, aTimes, aGains, aTimesErr, aGainsErr) gGainVsTime.SetMarkerColor(root.kBlue) gGainVsTime.SetLineColor(root.kBlue) gGainVsTime.SetTitle('Channel ' + str(channelNumber) + ' - Gain run_' + str(self.runNumber)) gGainVsTime.GetXaxis().SetTimeDisplay(1) gGainVsTime.GetXaxis().SetTimeFormat('%m-%d') gGainVsTime.GetXaxis().SetTimeOffset(0, 'gmt') gGainVsTime.GetYaxis().SetRangeUser(lGraphRangeY[0], lGraphRangeY[1]) gGainVsTime.GetYaxis().SetTitle('SPE gain [electrons]') gGainVsTime.Draw('AP') c1.SaveAs(self.sPathToGraphs + '/gain_pmt_' + str(channelNumber) + '.png') c1.SaveAs(self.sPathToGraphs + '/gain_pmt_' + str(channelNumber) + '.C')
def make_plots_ROOT(histograms, category, save_path, histname, channel): global variable, translateOptions, k_value, b_tag_bin, maximum ROOT.TH1.SetDefaultSumw2(False) ROOT.gROOT.SetBatch(True) ROOT.gROOT.ProcessLine('gErrorIgnoreLevel = 1001;') plotting.setStyle() gStyle.SetTitleYOffset(2.) ROOT.gROOT.ForceStyle() canvas = Canvas(width=700, height=500) canvas.SetLeftMargin(0.18) canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.05) canvas.SetRightMargin(0.05) legend = plotting.create_legend(x0=0.6, y1=0.5) hist_data = histograms['unfolded'] hist_data.GetXaxis().SetTitle(translate_options[variable] + ' [GeV]') hist_data.GetYaxis().SetTitle('#frac{1}{#sigma} #frac{d#sigma}{d' + translate_options[variable] + '} [GeV^{-1}]') hist_data.GetXaxis().SetTitleSize(0.05) hist_data.GetYaxis().SetTitleSize(0.05) hist_data.SetMinimum(0) hist_data.SetMaximum(maximum[variable]) hist_data.SetMarkerSize(1) hist_data.SetMarkerStyle(8) plotAsym = TGraphAsymmErrors(hist_data) plotStatErr = TGraphAsymmErrors(hist_data) xsections = read_unfolded_xsections(channel) bins = variable_bins_ROOT[variable] assert (len(bins) == len(xsections['central'])) for bin_i in range(len(bins)): scale = 1 # / width centralresult = xsections['central'][bin_i] fit_error = centralresult[1] uncertainty = calculateTotalUncertainty(xsections, bin_i) uncertainty_total_plus = uncertainty['Total+'][0] uncertainty_total_minus = uncertainty['Total-'][0] uncertainty_total_plus, uncertainty_total_minus = symmetriseErrors( uncertainty_total_plus, uncertainty_total_minus) error_up = sqrt(fit_error**2 + uncertainty_total_plus**2) * scale error_down = sqrt(fit_error**2 + uncertainty_total_minus**2) * scale plotStatErr.SetPointEYhigh(bin_i, fit_error * scale) plotStatErr.SetPointEYlow(bin_i, fit_error * scale) plotAsym.SetPointEYhigh(bin_i, error_up) plotAsym.SetPointEYlow(bin_i, error_down) gStyle.SetEndErrorSize(20) plotAsym.SetLineWidth(2) plotStatErr.SetLineWidth(2) hist_data.Draw('P') plotStatErr.Draw('same P') plotAsym.Draw('same P Z') legend.AddEntry(hist_data, 'unfolded', 'P') hist_measured = histograms['measured'] hist_measured.SetMarkerSize(1) hist_measured.SetMarkerStyle(20) hist_measured.SetMarkerColor(2) #hist_measured.Draw('same P') #legend.AddEntry(hist_measured, 'measured', 'P') for key, hist in sorted(histograms.iteritems()): if not 'unfolded' in key and not 'measured' in key: hist.SetLineStyle(7) hist.SetLineWidth(2) # setting colours if 'POWHEG' in key or 'matchingdown' in key: hist.SetLineColor(kBlue) elif 'MADGRAPH' in key or 'matchingup' in key: hist.SetLineColor(kRed + 1) elif 'MCATNLO' in key or 'scaleup' in key: hist.SetLineColor(kGreen - 3) elif 'scaledown' in key: hist.SetLineColor(kMagenta + 3) hist.Draw('hist same') legend.AddEntry(hist, translate_options[key], 'l') legend.Draw() mytext = TPaveText(0.5, 0.97, 1, 1.01, "NDC") channelLabel = TPaveText(0.18, 0.97, 0.5, 1.01, "NDC") if 'electron' in histname: channelLabel.AddText( "e, %s, %s, k = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) elif 'muon' in histname: channelLabel.AddText( "#mu, %s, %s, k = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) else: channelLabel.AddText( "combined, %s, %s, k = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) mytext.AddText("CMS Preliminary, L = %.1f fb^{-1} at #sqrt{s} = 8 TeV" % (5.8)) mytext.SetFillStyle(0) mytext.SetBorderSize(0) mytext.SetTextFont(42) mytext.SetTextAlign(13) channelLabel.SetFillStyle(0) channelLabel.SetBorderSize(0) channelLabel.SetTextFont(42) channelLabel.SetTextAlign(13) mytext.Draw() channelLabel.Draw() canvas.Modified() canvas.Update() path = save_path + '/' + variable + '/' + category make_folder_if_not_exists(path) canvas.SaveAs(path + '/' + histname + '_kv' + str(k_value) + '.png') canvas.SaveAs(path + '/' + histname + '_kv' + str(k_value) + '.pdf')
sighs.append(h) draw([bkgh] + sighs, ylimits=(1e-4, 1e4), logy=True) leg = Legend([bkgh] + sighs, pad=canvas, margin=0.25, leftmargin=0.45, topmargin=0.02, entrysep=0.01, entryheight=0.02, textsize=10) leg.Draw() title = TitleAsLatex('[4#mu] lepton-jet |d_{0}|') title.Draw() canvas.SaveAs('{}/ch4mu_muljd0.pdf'.format(outdir)) canvas.clear() signalsigs = [] for h in sighs: h_ = h.clone() for i in range(binnum, h.nbins() + 1): bkg_tot = bkgh.integral(xbin1=i, overflow=True) bkg_abcd = bkgf.ch4mu.data.dphiIso2D.clone() bkg_abcd.scale(bkg_tot / bkg_abcd.integral()) b, sigma_b = extract_background(bkg_abcd, yboundary=0.2) sig_tot = h.integral(xbin1=i, overflow=True) sigtag = h.title.split(' ')[0] sig_abcd = getattr(sigabcdf.ch4mu.sig, sigtag).dphiIso2Dinc.clone() sig_abcd.scale(sig_tot / sig_abcd.integral())
def plot_central_and_systematics(channel): global variable, translate_options, k_value, b_tag_bin, maximum, categories ROOT.TH1.SetDefaultSumw2(False) ROOT.gROOT.SetBatch(True) ROOT.gROOT.ProcessLine('gErrorIgnoreLevel = 1001;') plotting.setStyle() gStyle.SetTitleYOffset(1.4) ROOT.gROOT.ForceStyle() canvas = Canvas(width=700, height=500) canvas.SetLeftMargin(0.15) canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.05) canvas.SetRightMargin(0.05) legend = plotting.create_legend(x0=0.6, y1=0.5) hist_data_central = read_xsection_measurement_results( 'central', channel)[0]['unfolded'] hist_data_central.GetXaxis().SetTitle(translate_options[variable] + ' [GeV]') hist_data_central.GetYaxis().SetTitle('#frac{1}{#sigma} #frac{d#sigma}{d' + translate_options[variable] + '} [GeV^{-1}]') hist_data_central.GetXaxis().SetTitleSize(0.05) hist_data_central.GetYaxis().SetTitleSize(0.05) hist_data_central.SetMinimum(0) hist_data_central.SetMaximum(maximum[variable]) hist_data_central.SetMarkerSize(1) hist_data_central.SetMarkerStyle(20) # plotAsym = TGraphAsymmErrors(hist_data) # plotStatErr = TGraphAsymmErrors(hist_data) gStyle.SetEndErrorSize(20) hist_data_central.Draw('P') # plotStatErr.Draw('same P') # plotAsym.Draw('same P Z') legend.AddEntry(hist_data_central, 'measured (unfolded)', 'P') for systematic in categories: if systematic != 'central': hist_data_systematic = read_xsection_measurement_results( systematic, channel)[0]['unfolded'] hist_data_systematic.SetMarkerSize(0.5) hist_data_systematic.SetMarkerStyle(20) colour_number = categories.index(systematic) + 1 if colour_number == 10: colour_number = 42 hist_data_systematic.SetMarkerColor(colour_number) hist_data_systematic.Draw('same P') legend.AddEntry(hist_data_systematic, systematic, 'P') # for central_generator in ['MADGRAPH', 'POWHEG', 'MCATNLO']: # hist_MC = read_xsection_measurement_results('central', channel)[0][central_generator] # hist_MC.SetLineStyle(7) # hist_MC.SetLineWidth(2) # #setting colours # if central_generator == 'POWHEG': # hist_MC.SetLineColor(kBlue) # elif central_generator == 'MADGRAPH': # hist_MC.SetLineColor(kRed + 1) # elif central_generator == 'MCATNLO': # hist_MC.SetLineColor(kMagenta + 3) # hist_MC.Draw('hist same') #legend.AddEntry(hist_MC, translate_options[central_generator], 'l') legend.Draw() mytext = TPaveText(0.5, 0.97, 1, 1.01, "NDC") channelLabel = TPaveText(0.18, 0.97, 0.5, 1.01, "NDC") if channel == 'electron': channelLabel.AddText( "e, %s, %s, k_v = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) elif channel == 'muon': channelLabel.AddText( "#mu, %s, %s, k_v = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) else: channelLabel.AddText( "combined, %s, %s, k_v = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) mytext.AddText("CMS Preliminary, L = %.1f fb^{-1} at #sqrt{s} = 8 TeV" % (5.8)) mytext.SetFillStyle(0) mytext.SetBorderSize(0) mytext.SetTextFont(42) mytext.SetTextAlign(13) channelLabel.SetFillStyle(0) channelLabel.SetBorderSize(0) channelLabel.SetTextFont(42) channelLabel.SetTextAlign(13) mytext.Draw() if not channel == 'combination': channelLabel.Draw() canvas.Modified() canvas.Update() path = save_path + '/' + variable make_folder_if_not_exists(path) canvas.SaveAs(path + '/normalised_xsection_' + channel + '_altogether_kv' + str(k_value) + '.png') canvas.SaveAs(path + '/normalised_xsection_' + channel + '_altogether_kv' + str(k_value) + '.pdf')
h.linewidth=2 h.title=sigtag+' (norm. 30fb)' h.legendstyle='L' sighs.append(h) axes, limits = draw([bkgh]+sighs, ylimits=(1e-5,1e4), logy=True) decorate_xaxis_pi(axes[0]) leg = Legend([bkgh]+sighs, pad=canvas, margin=0.25, leftmargin=0.45, topmargin=0.02, entrysep=0.01, entryheight=0.02, textsize=10) leg.Draw() title = TitleAsLatex('[4#mu] lepton-jet |#Delta#phi|') title.Draw() canvas.SaveAs('{}/ch4mu_dphi.pdf'.format(outdir)) canvas.clear() signalsigs = [] for h in sighs: h_ = h.clone() for i in range(1, h.nbins()+1): b = math.sqrt(bkgh.integral(xbin1=i, overflow=True)) s = h.integral(xbin1=i, overflow=True) h_[i] = calculate_za(s, b) h_[i].error = 0 h_.scale(1/h_.integral()) signalsigs.append(h_) ROOT.gPad.SetLogy(0)
skip = 1 if skip: # print "skipping" continue try: legend.AddEntry(hists[signalsample], style='L') except: continue legend.SetBorderSize(1) legend.SetTextSize(0.03) legend.Draw() c.SaveAs( "N-1Plots/root/%s.pdf" % (kindOfRegion + "_" + histogramName.split(">")[0].split("<")[0])) # break if plotWithMPL: xmin, xmax = axes.get_xlim() plt.plot([xmin, xmax], [1, 1], 'k--', lw=1) yticks(arange(0, 2.0, 0.2)) ylim([0, 2]) axes_ratio.set_ylabel('Data/MC') axes.set_ylabel('Events') axes_ratio.set_xlabel(axislabel)
ratio = hi / lo ratio_err = ratio * math.sqrt(1 / lo + 1 / hi) h_hori.Fill(h.yedges(i), ratio) h_hori.SetBinError(int(i / nbins_per_slice) + 1, ratio_err) return h_vert, h_hori, ybound_val, xbound_val h = f.ch4mu.data.dphiIso2Dinit print('[4mu VR] dphiIsoinit - correlation factor: ', h.get_correlation_factor()) h.Draw('colz') h.GetListOfFunctions().FindObject("palette").SetX2NDC(0.92) title = TitleAsLatex('[4#mu VR] ' + h.title) title.Draw() canvas.SaveAs('{}/ch4mu_vr_dphiisopre.pdf'.format(outdir)) canvas.Clear() h.Draw('colz') h.GetListOfFunctions().FindObject("palette").SetX2NDC(0.92) title.Draw() dphi_bounds = [0.7 * math.pi, 0.8 * math.pi, 0.9 * math.pi] vlines = [] for i, b in enumerate(dphi_bounds): vline = Line(b, h.yaxis.GetXmin(), b, h.yaxis.GetXmax()) vline.color = COLORS[i] vline.linewidth = 2 vlines.append(vline) for l in vlines: l.Draw() canvas.SaveAs('{}/ch4mu_vr_dphiisopre_vline.pdf'.format(outdir))
canvas = Canvas() nominal.SetMaximum(max(dn) * 1.1) nominal.Draw() nominal.xaxis.SetTitle('BDT Score') nominal.yaxis.SetTitle('Events') up.Draw('same hist') dn.Draw('same hist') leg = Legend(3, pad=canvas, leftmargin=.5) leg.AddEntry(nominal, style='LEP') leg.AddEntry(up, style='L') leg.AddEntry(dn, style='L') leg.Draw() canvas.SaveAs('canvas_original.png') # Take the ratio of systematic / nominal ratio_up = up / nominal ratio_dn = dn / nominal ratio_canvas = Canvas() ratio_canvas.SetLogy() ratio_up.SetMinimum(0.001) ratio_up.Draw('hist') ratio_dn.Draw('same hist') ratio_up.xaxis.SetTitle('BDT Score') ratio_up.yaxis.SetTitle('Systematic / Nominal') ratio_canvas.SaveAs('canvas_ratio.png')
h.color=sigCOLORS[it] h.linewidth=2 h.title=sigtag+' (norm. 30fb)' h.legendstyle='L' sighs.append(h) draw([bkgh]+sighs, ylimits=(1e-4,1e3), logy=True) leg = Legend([bkgh]+sighs, pad=canvas, margin=0.25, leftmargin=0.45, topmargin=0.02, entrysep=0.01, entryheight=0.02, textsize=10) leg.Draw() title = TitleAsLatex('[4#mu] muon-type lepton-jet isolation') title.Draw() canvas.SaveAs('{}/ch4mu_muljiso.pdf'.format(outdir)) canvas.clear() signalsigs = [] for h in sighs: h_ = h.clone() for i in range(1, h.nbins()+1): b = math.sqrt(bkgh.integral(xbin1=i, overflow=True)) s = h.integral(xbin1=i, overflow=True) if s==0 or b==0: h_[i]=h_[i-1] else: h_[i] = calculate_simpsig(s, b) h_[i].error = 0 h_.scale(1/h_.integral()) signalsigs.append(h_)
l1_efficiency.Draw('pe same') uct_efficiency.Draw('pe same') uct_iso_efficiency.Draw('pe same') legend = Legend(3, topmargin=0.25, leftmargin=0.25) legend.AddEntry(l1_efficiency, 'lp', 'Current Tau44') legend.AddEntry(uct_efficiency, 'lp', 'Upgrade RlxTau25 Rel. Rate %0.2f' % (9488. / 9355)) legend.AddEntry(uct_iso_efficiency, 'lp', 'Upgrade IsoTau25 Rel. Rate %0.2f' % (6224. / 9355)) legend.SetBorderSize(0) legend.SetTextSize(0.03) legend.Draw() canvas.SaveAs("eff_vs_pu.png") canvas.SaveAs("eff_vs_pu.pdf") frame.Draw() l1_efficiency.Draw('pe same') uct_efficiency.Draw('pe same') legend = Legend(2, topmargin=0.25, leftmargin=0.25) legend.AddEntry(l1_efficiency, 'lp', 'Current Tau44') legend.AddEntry(uct_efficiency, 'lp', 'Upgrade RlxTau25 Rel. Rate %0.2f' % (9488. / 9355)) legend.SetBorderSize(0) legend.SetTextSize(0.04) legend.Draw()
h.legendstyle = 'L' h.title = 'lxy: {}cm c#tau: {}mm'.format(lxy, ctau) hsd[float(lxy)] = h hs = [hsd[k] for k in sorted(hsd)] leg = Legend(hs, pad=c, leftmargin=0.5, margin=0.1, entryheight=0.02, textsize=12) draw(hs, pad=c, xlimits=(1e-1, 500), logy=True, logx=True) leg.Draw() t = LuminosityLabel('XX#rightarrow2A#rightarrow4#mu ({}, {}) GeV'.format( mxx, ma)) t.draw() c.SaveAs(os.path.join(outdir, 'ch4mu_dplxy.pdf'.format(outdir))) c.clear() massdir = getattr(f.ch2mu2e, masstag) lxytags = [x.name for x in massdir.keys()] # lxy-0p3_ctau-0p0025 hsd = {} for tag in lxytags: lxy_, ctau_ = tag.split('_') lxy = lxy_.split('-')[1].replace('p', '.') ctau = ctau_.split('-')[1].replace('p', '.') h = getattr(massdir, tag).dplxy h.linewidth = 2 h.drawstyle = 'PLC hist' h.scale(1. / h.integral())
p.legendstyle = 'LEP' draw([pljiso, pproxyiso, psljiso, ljisotot], ) # ylimits=(3e-3,1), logy=True) leg = Legend(4, pad=canvas, topmargin=0.02, margin=0.2, entryheight=0.015, entrysep=0.01, textsize=10) leg.AddEntry(pproxyiso, label='proxy muon iso') leg.AddEntry(pljiso, label='lepton-jet iso (proxy events)') leg.AddEntry(psljiso, label='lepton-jet iso (signal events)') leg.AddEntry(ljisotot, label='lepton-jet iso (signal+proxy events)') leg.Draw() canvas.SaveAs('{}/ch4mu_isolationprofiles.pdf'.format(outdir)) canvas.clear() proxymusf = pljiso.Clone() proxymusf.Divide(pproxyiso) draw([proxymusf], ylimits=(0, 2)) leg = Legend([proxymusf], pad=canvas, topmargin=0.02, margin=0.2, entryheight=0.015, entrysep=0.01, textsize=10) leg.Draw() canvas.SaveAs('{}/ch4mu_proxymuisosf.pdf'.format(outdir)) canvas.clear()