def createConfigFile(datafilename, sigfilename, bkgfilename, hname, gvfilename=None, ntrials=100): data, edata = getYield(datafilename, hname) ndata = sum(data) nbins = len(data) if gvfilename: gvariates = map(atof, open(gvfilename).readlines()) nsample = min(len(gvariates), ntrials) else: gvariates = [1.0] nsample = 1 os.system('mkdir -p configs') cfgfilename = 'configs/%s_%s_%4.4d.cfg' % \ (nameonly(sigfilename), hname, nsample) print '-> config file: %s' % cfgfilename cfg = open(cfgfilename, 'w') record = '#' + '-' * 78 cfg.write('%s\n' % record) record = '# file: %s' % cfgfilename cfg.write('%s\n' % record) record = '# created: %s' % ctime() cfg.write('%s\n' % record) record = '#' + '-' * 78 cfg.write('%s\n' % record) # get counts and uncertainty in counts sig, dsig = getYield(sigfilename, hname) bkg, dbkg = getYield(bkgfilename, hname) nsig = sum(sig) esig = sqrt(sum(map(lambda x: x * x, dsig))) nbkg = sum(bkg) ebkg = sqrt(sum(map(lambda x: x * x, dbkg))) record = '#nbins:\t%9d' % nbins cfg.write('%s\n' % record) record = '#data:\t%9d' % ndata cfg.write('%s\n' % record) record = '#sig:\t%9.2e' % nsig cfg.write('%s\n' % record) record = '#esig:\t%9.2e' % esig cfg.write('%s\n' % record) record = '#bkg:\t%9.2e' % nbkg cfg.write('%s\n' % record) record = '#ebkg:\t%9.2e' % ebkg cfg.write('%s\n' % record) record = '#' + '-' * 78 cfg.write('%s\n' % record) record = '# number of bins' cfg.write('%s\n' % record) record = '%10d' % nbins cfg.write('%s\n' % record) record = '# observed counts' cfg.write('%s\n' % record) record = '' for ii in xrange(nbins): record += ' %9d' % int(data[ii]) cfg.write('%s\n' % record) record = '# number of sampled points' cfg.write('%s\n' % record) cfg.write('\t%d\n' % nsample) x1 = 0.0 x2 = 0.0 for jj in xrange(nsample): s = [0] * nbins es = [0] * nbins b = [0] * nbins eb = [0] * nbins x = gvariates[jj] x1 += x x2 += x * x for ii in xrange(nbins): s[ii] = x * sig[ii] es[ii] = x * dsig[ii] b[ii] = x * bkg[ii] eb[ii] = x * dbkg[ii] nn = jj + 1 record = '# signal counts\t%d' % nn cfg.write('%s\n' % record) record = '' for ii in xrange(nbins): record += ' %9.2e' % s[ii] cfg.write('%s\n' % record) record = '# signal uncertainties\t%d' % nn cfg.write('%s\n' % record) record = '' for ii in xrange(nbins): record += ' %9.2e' % es[ii] cfg.write('%s\n' % record) record = '# background counts\t%d' % nn cfg.write('%s\n' % record) record = '' for ii in xrange(nbins): record += ' %9.2e' % b[ii] cfg.write('%s\n' % record) record = '# background uncertainties\t%d' % nn cfg.write('%s\n' % record) record = '' for ii in xrange(nbins): record += ' %9.2e' % eb[ii] cfg.write('%s\n' % record) cfg.close() x1 /= nsample x2 /= nsample x2 = sqrt(x2 - x1 * x1) print "-> gamma(mean): %5.3f\tgamma(stdev): %5.3f" % (x1, x2)
def main(): # --------------------------------------- # set up some standard graphics style # --------------------------------------- tdrstyle.setTDRStyle() gStyle.SetPadRightMargin(0.12) gStyle.SetOptStat('ei') gStyle.SetStatFont(42) gStyle.SetStatFontSize(0.03) gStyle.SetStatBorderSize(1) gStyle.SetStatH(0.2) gStyle.SetStatW(0.3) gStyle.SetStatX(0.83) gStyle.SetStatY(0.93) bnn = m4lmela bnn3 = m4lmelamet #change the CMS_lumi variables (see CMS_lumi.py) iPeriod = 4 iPos = 0 CMS_lumi.relPosX = 0.12 CMS_lumi.lumi_13TeV = "36 fb^{-1}" CMS_lumi.writeExtraText = 1 CMS_lumi.extraText = "Simulation" if len(sys.argv) > 1: filenames = sys.argv[1:] else: sys.exit(''' Usage: makeHists.py ntuple-file ... ''') # name of graphics file name = nameonly(filenames[0]) plotname = replace(name, 'ntuple_', 'fig_') plotnameSRCR = replace(name, 'ntuple_', 'fig_regions_') histfname = replace(name, 'ntuple_', 'histos_') if len(filenames) > 1: plotname = stripit.sub('', plotname) histfname= stripit.sub('', histfname) outfilename = 'histos/%s.root' % histfname # change marker size and color depending on filename isData = False isHiggs= False if find(plotname, 'data') > -1: msize = 0.8 mcolor = kBlack isData = True CMS_lumi.extraText = "Preliminary" elif find(plotname, 'bkg') > -1: msize = 0.01 mcolor = kMagenta+1 elif find(plotname, 'higgs') > -1: msize = 0.01 mcolor = kCyan+1 isHiggs = True else: msize = 0.01 mcolor = kCyan+2 # --------------------------------------- # open ntuple file # --------------------------------------- ntuple = Ntuple(filenames, treename='HZZ4LeptonsAnalysisReduced') nevents = ntuple.size() print '='*80 print 'number of entries: %d' % nevents print 'output file: %s' % outfilename print '='*80 # open output root file for histograms hfile = TFile(outfilename, 'recreate') hfile.cd() # --------------------------------------- # book histograms # --------------------------------------- # 2-D plot in (f_mass4l, f_D_bkg_kin) space cname = 'histos/%s' % plotname cm4lD = TCanvas(cname, cname, 10, 10, 500, 500) hm4lD = TH2F('hm4lD', '', MASSBINS, MASSMIN, MASSMAX, DBINS, 0, 1) hm4lD.SetMarkerSize(msize) hm4lD.SetMarkerColor(mcolor) hm4lD.GetXaxis().SetTitle('#font[12]{m}_{4l} (GeV)') hm4lD.GetYaxis().SetTitle('#font[12]{D}_{bkg}^{kin}') hm4lD.Sumw2() # needed to handle weights correctly hm4lD.SetMinimum(0) hm4lD.GetXaxis().SetNdivisions(505) hm4lD.GetYaxis().SetNdivisions(505) # 1-D plot in D(f_mass4l, f_D_bkg_kin) space cname = 'histos/%s_bnn' % plotname cbnn = TCanvas(cname, cname, 520, 10, 500, 500) hbnn = TH1F('hbnn', '', DBINS, 0, 1) hbnn.SetLineWidth(1) hbnn.SetFillColor(mcolor) hbnn.SetFillStyle(3001) hbnn.GetXaxis().SetTitle('D(#font[12]{m}_{4l}, '\ '#font[12]{D}_{bkg}^{kin})') hbnn.GetXaxis().SetNdivisions(505) hbnn.Sumw2() hbnn.SetMinimum(0) # 1-D plot in f_mass4l cname = 'histos/%s_m4l' % plotname cm4l = TCanvas(cname, cname, 1040, 10, 500, 500) hm4l = TH1F('hm4l', '', MASSBINS, MASSMIN, MASSMAX) hm4l.SetLineWidth(1) hm4l.SetFillColor(mcolor) hm4l.SetFillStyle(3001) hm4l.GetXaxis().SetTitle('#font[12]{m}_{4l} (GeV)') hm4l.GetXaxis().SetNdivisions(505) hm4l.Sumw2() hm4l.SetMinimum(0) # 1-D plot in f_pfmet cname = 'histos/%s_met' % plotname cmet = TCanvas(cname, cname, 10, 510, 500, 500) hmet = TH1F('hmet', '', METBINS, METMIN, METMAX) hmet.SetLineWidth(1) hmet.SetFillColor(mcolor) hmet.SetFillStyle(3001) hmet.GetXaxis().SetTitle('#font[12]{E}_{T}^{miss} (GeV)') hmet.GetXaxis().SetNdivisions(505) hmet.Sumw2() hmet.SetMinimum(0) # 1-D plot in D(f_mass4l, f_D_bkg_kin, f_pfmet) cname = 'histos/%s_bnn3' % plotname cbnn3 = TCanvas(cname, cname, 510, 510, 500, 500) hbnn3 = TH1F('hbnn3', '', DBINS, 0, 1) hbnn3.SetLineWidth(1) hbnn3.SetFillColor(mcolor) hbnn3.SetFillStyle(3001) hbnn3.GetXaxis().SetTitle('D(#font[12]{m}_{4l}, '\ '#font[12]{D}_{bkg}^{kin}, '\ '#font[12]{E}_{T}^{miss})') hbnn3.GetXaxis().SetNdivisions(505) hbnn3.Sumw2() hbnn3.SetMinimum(0) hbag = [] for ii, (name, xtitle, xoff, yoff, xbins, xmin, xmax) in enumerate(PLOTS): hbag.append(HistBag(ntuple, plotnameSRCR, name, xtitle, xoff, yoff, xbins, xmin, xmax)) for ii,(name,xtitle, xoff, yoff, xbins, xmin, xmax) in enumerate(PLOTSZOOM): hbag.append(HistBag(ntuple, plotnameSRCR, name, xtitle, xoff, yoff, xbins, xmin, xmax, ymin=1.e-9, postfix='_zoom')) # --------------------------------------- # Loop over events # --------------------------------------- t1 = 0.0 t2 = 0.0 w1 = 0.0 w2 = 0.0 passed = 0 for index, event in enumerate(ntuple): m4l = event.f_mass4l if m4l < MASSMIN: continue if m4l > MASSMAX: continue #if (m4l >= 100) and (m4l <=150): continue w = event.f_weight t1 += w t2 += w*w if event.f_outlier: continue w1 += w w2 += w*w Dbkg = event.f_D_bkg_kin met = event.f_pfmet Dbnn = bnn(m4l, Dbkg) Dbnn3= bnn3(m4l, Dbkg, met) hm4l.Fill(m4l, w) hm4lD.Fill(m4l, Dbkg, w) hbnn.Fill(Dbnn, w) hbnn3.Fill(Dbnn3, w) hmet.Fill(met, w) if Dbnn > BNNCUT: for h in hbag: h.fillSR() else: for h in hbag: h.fillCR() if passed % SKIP == 0: print '%10d %10d' % (passed, index) cm4lD.cd() hm4lD.Draw('p') cm4lD.Update() gSystem.ProcessEvents() cbnn.cd() if isData: hbnn.Draw('ep') else: hbnn.Draw('hist') cbnn.Update() gSystem.ProcessEvents() for h in hbag: h.draw() passed += 1 # now re-scale histograms to compensate for removal of # outlier events. scaleFactor = t1 / w1 t2 = sqrt(t2) w2 = sqrt(w2) hm4l.Scale(scaleFactor) hm4lD.Scale(scaleFactor) hbnn.Scale(scaleFactor) hbnn3.Scale(scaleFactor) hmet.Scale(scaleFactor) for h in hbag: h.scale(scaleFactor) s1, s2, c1, c2 = hintegral(hbnn, BNNCUT) print '='*80 txtfilename = '%s.txt' % plotname print txtfilename out = open(txtfilename, 'w') record = 'number of entries: %d' % nevents print record out.write('%s\n' % record) record = "number of events (with outliers): %10.2e +/- %-10.1e" % \ (t1, t2) print record out.write('%s\n' % record) record = "number of events (without outliers): %10.2e +/- %-10.1e" % \ (w1, w2) print record out.write('%s\n' % record) record = ' ' print record out.write('%s\n' % record) record = 'histograms scaled by factor %10.3f' % scaleFactor print record out.write('%s\n' % record) record = "number of events (BNN > %3.1f): %10.2e +/- %-10.1e" % \ (BNNCUT, s1, s2) print record out.write('%s\n' % record) record = "number of events (BNN <= %3.1f): %10.2e +/- %-10.1e" % \ (BNNCUT, c1, c2) print record out.write('%s\n' % record) out.close() print '='*80 cm4lD.cd() hm4lD.Draw('p') CMS_lumi.CMS_lumi(cm4lD, iPeriod, iPos) cm4lD.Update() gSystem.ProcessEvents() cm4lD.SaveAs('.png') cbnn.cd() if isData: hbnn.Draw('ep') else: hbnn.Draw('hist') CMS_lumi.CMS_lumi(cbnn, iPeriod, iPos) cbnn.Update() gSystem.ProcessEvents() cbnn.SaveAs('.png') cbnn3.cd() if isData: hbnn3.Draw('ep') else: hbnn3.Draw('hist') CMS_lumi.CMS_lumi(cbnn3, iPeriod, iPos) cbnn3.Update() gSystem.ProcessEvents() cbnn3.SaveAs('.png') cmet.cd() if isData: hmet.Draw('ep') else: hmet.Draw('hist') CMS_lumi.CMS_lumi(cmet, iPeriod, iPos) cmet.Update() gSystem.ProcessEvents() cmet.SaveAs('.png') cm4l.cd() if isData: hm4l.Draw('ep') else: hm4l.Draw('hist') CMS_lumi.CMS_lumi(cm4l, iPeriod, iPos) cm4l.Update() gSystem.ProcessEvents() cm4l.SaveAs('.png') for h in hbag: h.draw(True) hfile.cd() cm4lD.Write() cbnn.Write() cbnn3.Write() cmet.Write() cm4l.Write() for h in hbag: h.write() hfile.Write() hfile.Close() sleep(2)
def main(): argv = sys.argv[1:] argc = len(argv) if argc < 1: print ''' Usage: plot.py <sig-filename1> [SR] ''' sys.exit() print "=" * 80 os.system("mkdir -p figures") SR = argv[-1] == 'SR' if SR: argv = argv[:-1] postfix = '_SR' else: postfix = '' sigfilenames = argv sigfilenames.sort() bkgfilename = '../../ntuple_SM.root' BNNname = BNNNAME print 'BNN: %s' % BNNname print print 'signal file: %s' % sigfilenames print print 'background file: %s' % bkgfilename print "=" * 80 # compile bnn function #gSystem.Load("libmvd") gROOT.ProcessLine('.L metd.cpp') gROOT.ProcessLine('.L m4lmela.cpp') bnn = eval(BNNname) # --------------------------------------- # set up some standard graphics style # --------------------------------------- tdrstyle.setTDRStyle() gStyle.SetPadRightMargin(0.12) gStyle.SetOptStat('ei') gStyle.SetStatFont(42) gStyle.SetStatFontSize(0.03) gStyle.SetStatBorderSize(1) gStyle.SetStatH(0.2) gStyle.SetStatW(0.3) gStyle.SetStatX(0.83) gStyle.SetStatY(0.93) #change the CMS_lumi variables (see CMS_lumi.py) iPeriod = 4 iPos = 0 CMS_lumi.relPosX = 0.12 CMS_lumi.lumi_13TeV = "36.0 fb^{-1}" CMS_lumi.writeExtraText = 1 CMS_lumi.extraText = "Simulation" hfile = TFile("figures/h_%s.root" % BNNname, "RECREATE") hfile.cd() nbins = 100 kolors = [ kRed, kOrange, kYellow + 2, kGreen + 1, kBlue, kMagenta + 1, kCyan + 1 ] icolor = 0 h = [] # SIGNAL for ii, sigfilename in enumerate(sigfilenames): color = kolors[icolor] icolor += 1 if icolor >= len(kolors): icolor = 0 name = replace(nameonly(sigfilename), 'ntuple_', '') hs1 = TH1F(name, '', nbins, 0, 1) hs1.GetXaxis().SetTitle('D_{%s}' % name) hs1.Sumw2() # needed to handle weights correctly hs1.SetMinimum(1.e-6) hs1.GetXaxis().SetNdivisions(505) hs1.GetYaxis().SetNdivisions(505) hs1.SetFillColor(color) hs1.SetFillStyle(3001) h.append(hs1) # BACKGROUND mcolor = kMagenta + 1 hb1 = TH1F('hb1', '', nbins, 0, 1) hb1.GetXaxis().SetTitle('D_{bkg}') hb1.Sumw2() # needed to handle weights correctly hb1.SetMinimum(1.e-6) hb1.GetXaxis().SetNdivisions(505) hb1.GetYaxis().SetNdivisions(505) hb1.SetFillColor(mcolor) hb1.SetFillStyle(3003) h.append(hb1) # --------------------------------------------------------- # Fill # --------------------------------------------------------- cbnn1 = TCanvas("figures/f_%s_lin%s" % (BNNname, postfix), BNNname, 10, 10, 500, 500) # plot for ii, sigfilename in enumerate(sigfilenames): name = h[ii].GetName() cbnn1.SetTitle(name) print name readAndFillHist(sigfilename, bnn, cbnn1, h[ii], m4lmela, 500, SR) readAndFillHist(bkgfilename, bnn, cbnn1, hb1, m4lmela, 10000, SR) icolor = 0 hroc = [] for ii, sigfilename in enumerate(sigfilenames): name = '%s_roc' % h[ii].GetName() color = kolors[icolor] icolor += 1 if icolor >= len(kolors): icolor = 0 hroc.append(mkroc(name, h[ii], h[-1], lcolor=color)) # --------------------------------------------------------- # Plot # --------------------------------------------------------- cbnn1.cd() h[0].SetMaximum(1.0) h[-1].SetMaximum(1.0) h[-1].Draw('hist') for hist in h: hist.Draw("histsame") cbnn1.Update() gSystem.ProcessEvents() cbnn1.SaveAs('.png') cbnn1log = TCanvas("figures/f_%s_log%s" % (BNNname, postfix), BNNname, 520, 10, 500, 500) cbnn1log.cd() cbnn1log.SetLogy() h[-1].Draw('hist') for hist in h: hist.Draw("histsame") cbnn1log.Update() gSystem.ProcessEvents() cbnn1log.SaveAs('.png') croc = TCanvas("figures/f_%s_roc%s" % (BNNname, postfix), BNNname, 1040, 10, 500, 500) croc.cd() hroc[0].Draw('al') for hist in hroc: hist.Draw("lsame") croc.Update() gSystem.ProcessEvents() croc.SaveAs('.png') sleep(5)
def main(): # --------------------------------------- # set up some standard graphics style # --------------------------------------- tdrstyle.setTDRStyle() gStyle.SetPadRightMargin(0.12) gStyle.SetOptStat('ei') gStyle.SetStatFont(42) gStyle.SetStatFontSize(0.03) gStyle.SetStatBorderSize(1) gStyle.SetStatH(0.2) gStyle.SetStatW(0.3) gStyle.SetStatX(0.83) gStyle.SetStatY(0.93) #change the CMS_lumi variables (see CMS_lumi.py) iPeriod = 4 iPos = 0 CMS_lumi.relPosX = 0.12 CMS_lumi.lumi_13TeV = "36 fb^{-1}" CMS_lumi.writeExtraText = 1 CMS_lumi.extraText = "Simulation" filename, options = decodeCommandLine() # name of graphics file name = nameonly(filename) plotname = replace(name, 'ntuple_', '') histfname = replace(name, 'ntuple_', 'histos_') os.system('mkdir -p histos') outfilename = 'histos/%s.root' % histfname # --------------------------------------- # open ntuple file # --------------------------------------- ntuple = getNtuple(filename) nevents = ntuple.nevents SKIP = nevents / 10 print '=' * 80 print 'number of entries: %d' % nevents print 'output file: %s' % outfilename print '=' * 80 # open output root file for histograms hfile = TFile(outfilename, 'recreate') hfile.cd() CRbag = [] for ii, (field, hname, xtitle, xoff, yoff, xbins, xmin, xmax) in enumerate(CRPLOTS): CRbag.append( HistBag(CMS_lumi, iPeriod, iPos, 'CR', plotname, ntuple, field, hname, xtitle, xoff, yoff, xbins, xmin, xmax)) SRbag = [] for ii, (field, hname, xtitle, xoff, yoff, xbins, xmin, xmax) in enumerate(SRPLOTS): SRbag.append( HistBag(CMS_lumi, iPeriod, iPos, 'SR', plotname, ntuple, field, hname, xtitle, xoff, yoff, xbins, xmin, xmax)) # --------------------------------------- # Loop over events # --------------------------------------- t1 = 0.0 t2 = 0.0 w1 = 0.0 w2 = 0.0 swatch = TStopwatch() swatch.Start() passed = 0 for index in xrange(nevents): ntuple.read(index) if index % SKIP == 0: print index m4l = ntuple.f_mass4l if m4l < CRMASSMIN: continue if m4l > CRMASSMAX: continue w = ntuple.f_weight t1 += w t2 += w * w if ntuple.f_outlier: continue w1 += w w2 += w * w SR = (m4l >= SRMASSMIN) and (m4l <= SRMASSMAX) if SR: for h in SRbag: h.fill() else: for h in CRbag: h.fill() if passed % SKIP == 0: for h in SRbag: h.draw() passed += 1 t = swatch.RealTime() t /= nevents t *= 1e6 print 'execution time: %10.2f microsecond/event' % t # now re-scale histograms to compensate for removal of # outlier events. scaleFactor = t1 / w1 t2 = sqrt(t2) w2 = sqrt(w2) for h in SRbag: h.scale(scaleFactor) for h in CRbag: h.scale(scaleFactor) print '=' * 80 txtfilename = '%s.txt' % plotname print txtfilename out = open(txtfilename, 'w') record = 'number of entries: %d' % nevents print record out.write('%s\n' % record) record = "number of events (with outliers): %10.2e +/- %-10.1e" % \ (t1, t2) print record out.write('%s\n' % record) record = "number of events (without outliers): %10.2e +/- %-10.1e" % \ (w1, w2) print record out.write('%s\n' % record) record = ' ' print record out.write('%s\n' % record) record = 'histograms scaled by factor %10.3f' % scaleFactor print record out.write('%s\n' % record) out.close() print '=' * 80 for h in CRbag: h.draw() h.save() h.write() for h in SRbag: h.draw() h.save() h.write() hfile.Write() hfile.Close() sleep(2)