def buildSmoothBkgCombA(self): """Build with RooWorkspace.factory. See also RooFactoryWSTool.factory""" wspace = self.getWspace() f_bkgCombAAltA = wspace.pdf("f_bkgCombAAltA") if f_bkgCombAAltA == None: f_bkgCombAAltKUp = RooKeysPdf( "f_bkgCombAAltKUp", "f_bkgCombAAltKUp", CosThetaK, self.process.sourcemanager.get('dataReader.USB'), RooKeysPdf.MirrorBoth, 1.0) f_bkgCombAAltKLo = RooKeysPdf( "f_bkgCombAAltKLo", "f_bkgCombAAltKLo", CosThetaK, self.process.sourcemanager.get('dataReader.LSB'), RooKeysPdf.MirrorBoth, 1.0) f_bkgCombAAltLUp = RooKeysPdf( "f_bkgCombAAltLUp", "f_bkgCombAAltLUp", CosThetaL, self.process.sourcemanager.get('dataReader.USB'), RooKeysPdf.MirrorBoth, 1.0) f_bkgCombAAltLLo = RooKeysPdf( "f_bkgCombAAltLLo", "f_bkgCombAAltLLo", CosThetaL, self.process.sourcemanager.get('dataReader.LSB'), RooKeysPdf.MirrorBoth, 1.0) for f in f_bkgCombAAltKLo, f_bkgCombAAltKUp, f_bkgCombAAltLLo, f_bkgCombAAltLUp: getattr(wspace, 'import')(f) wspace.factory( "PROD::f_bkgCombAAltAUp(f_bkgCombAAltKUp,f_bkgCombAAltLUp)") wspace.factory( "PROD::f_bkgCombAAltALo(f_bkgCombAAltKLo,f_bkgCombAAltLLo)") wspace.factory( "SUM::f_bkgCombAAltA(frac_bkgCombAAltA[0.5,0,1]*f_bkgCombAAltALo,f_bkgCombAAltAUp)" ) f_bkgCombAAltA = wspace.pdf("f_bkgCombAAltA") frac_bkgCombAAltA = wspace.var("frac_bkgCombAAltA") frac_bkgCombAAltA.setVal( self.process.sourcemanager.get('dataReader.LSB').sumEntries() / (self.process.sourcemanager.get('dataReader.LSB').sumEntries() + self.process.sourcemanager.get('dataReader.USB').sumEntries())) frac_bkgCombAAltA.setConstant(True) self.cfg['source']['frac_bkgCombAAltA'] = frac_bkgCombAAltA self.cfg['source']['f_bkgCombAAltA'] = f_bkgCombAAltA
dataset_COMB_CORR = RooDataSet("dataset_COMB_CORR","dataset_COMB_CORR",newtree,varset_small) key_COMB = [] hist_COMB = [] for i, bin in enumerate(decaytime_binnning): if i == 0: continue start = datetime.now() dataset_COMB_CORR_dtb_init = RooDataSet("dataset_COMB_CORR_dtb_init","Decaytime bin"+str(i),dataset_COMB_CORR,varset_small,"Dst_DTF_D0_CTAU>"+str(bin[0]*ctau)+"&&Dst_DTF_D0_CTAU<"+str(bin[1]*ctau)) dataset_COMB_CORR_dtb = Subtract_Distribution(dataset_COMB_CORR_dtb_init, DTF_D0sPi_M, LOG_D0_IPCHI2_OWNPV, str(i)+"_comb", True) dataset_COMB_CORR_dtb.SetName("dataset_COMB_CORR_dtb") print "Background substraction from combinatorial tool "+str(datetime.now()-start)+" \n" hist_COMB.append(RooDataHist("hist_COMB"+str(i),"hist_COMB", RooArgSet(LOG_D0_IPCHI2_OWNPV), dataset_COMB_CORR_dtb)) key_COMB.append(RooKeysPdf("key_COMB_"+str(i), "key_COMB", LOG_D0_IPCHI2_OWNPV, dataset_COMB_CORR_dtb)) #We store shapes of Log(IPCHI2) for mathced candidates in for each decay time bins here. wspace_2 = RooWorkspace("wspace_key_shapes") wspace_2.Print("t") wsfile = TFile("/afs/cern.ch/user/"+prefix+"/"+user+"/eos/lhcb/user/"+prefix+"/"+user+"/WrongSign/2015/Secondary_Key_Shapes.root", "recreate") for k in key_COMB: wspace_2.rfimport(k) wspace_2.Write("wspace") wspace_3 = RooWorkspace("wspace_hist_shapes") wspace_3.Print("t") wsfile = TFile("/afs/cern.ch/user/"+prefix+"/"+user+"/eos/lhcb/user/"+prefix+"/"+user+"/WrongSign/2015/Secondary_Hist_Shapes.root", "recreate") for s in hist_COMB: wspace_3.rfimport(s) wspace_3.Write("wspace")
def main(options, args): from ROOT import gSystem, gROOT, gStyle gROOT.SetBatch() gSystem.Load("libRooFitCore") if options.doWebPage: from lip.Tools.rootutils import loadToolsLib, apply_modifs loadToolsLib() from ROOT import TFile, RooFit, RooArgSet, RooDataHist, RooKeysPdf, RooHistPdf, TCanvas, TLegend, TLatex, TArrow, TPaveText, RooAddPdf, RooArgList from ROOT import kWhite, kBlue, kOpenSquare if options.doWebPage: from ROOT import HtmlHelper, HtmlTag, HtmlTable, HtmlPlot rootglobestyle.setTDRStyle() gStyle.SetMarkerSize(1.5) gStyle.SetTitleYOffset(1.5) gStyle.SetPadLeftMargin(0.16) gStyle.SetPadRightMargin(0.05) gStyle.SetPadTopMargin(0.05) gStyle.SetPadBottomMargin(0.13) gStyle.SetLabelFont(42, "XYZ") gStyle.SetLabelOffset(0.007, "XYZ") gStyle.SetLabelSize(0.05, "XYZ") gStyle.SetTitleSize(0.06, "XYZ") gStyle.SetTitleXOffset(0.9) gStyle.SetTitleYOffset(1.24) gStyle.SetTitleFont(42, "XYZ") ## ## Read files ## options.outdir = "%s_m%1.0f" % (options.outdir, options.mH) if options.fp: options.outdir += "_fp" ncat = options.ncat cats = options.cats if cats is "": categories = ["_cat%d" % i for i in range(0, ncat)] else: categories = ["_cat%s" % i for i in cats.split(",")] if options.mva: clables = { "_cat0": ("MVA > 0.89", ""), "_cat1": ("0.74 #leq MVA", "MVA < 0.89"), "_cat2": ("0.545 #leq MVA", "MVA < 0.74"), "_cat3": ("0.05 #leq MVA", "MVA < 0.545"), "_cat4": ("Di-jet", "Tagged"), "_cat5": ("Di-jet", "Tagged"), "_combcat": ("All Classes", "Combined") } else: clables = { "_cat0": ("max(|#eta|<1.5", "min(R_{9})>0.94"), "_cat1": ("max(|#eta|<1.5", "min(R_{9})<0.94"), "_cat2": ("max(|#eta|>1.5", "min(R_{9})>0.94"), "_cat3": ("max(|#eta|>1.5", "min(R_{9})<0.94"), "_cat4": ("Di-jet", "Tagged"), "_cat5": ("Di-jet", "Tagged"), "_combcat": ("All Classes", "Combined") } helper = Helper() fin = TFile.Open(options.infile) helper.files.append(fin) ws = fin.Get("cms_hgg_workspace") mass = ws.var("CMS_hgg_mass") mass.SetTitle("m_{#gamma#gamma}") mass.setUnit("GeV") mass.setRange(100., 150.) mass.setBins(100, "plot") mass.setBins(5000) print ws aset = RooArgSet(mass) helper.objs.append(mass) helper.objs.append(aset) fitopt = (RooFit.Minimizer("Minuit2", ""), RooFit.Minos(False), RooFit.SumW2Error(False), RooFit.NumCPU(8)) if not options.binned and not options.refit: finpdf = TFile.Open(options.infilepdf) helper.files.append(finpdf) wspdf = finpdf.Get("wsig") else: wspdf = ws for c in categories: processes = ["ggh", "vbf", "wzh"] if options.fp: processes = ["vbf", "wzh"] ### elif clables[c][0] == "Di-jet": ### processes = [ "vbf", "ggh" ] dsname = "sig_mass_m%1.0f%s" % (options.mH, c) print dsname print ws ds = ws.data("sig_%s_mass_m%1.0f%s" % (processes[0], options.mH, c)).Clone(dsname) for proc in processes[1:]: ds.append(ws.data("sig_%s_mass_m%1.0f%s" % (proc, options.mH, c))) helper.dsets.append(ds) if options.binned: binned_ds = RooDataHist("binned_%s" % dsname, "binned_%s" % dsname, aset, ds) pdf = RooKeysPdf("pdf_%s_%s" % (dsname, f), "pdf_%s" % dsname, mass, ds) plot_pdf = RooHistPdf("pdf_%s" % dsname, "pdf_%s" % dsname, aset, plot_ds) helper.add(binned_ds, binned_ds.GetName()) else: if options.refit: if options.refitall and clables[c][0] != "Di-jet": rpdfs = [] for proc in processes: for ngaus in range(1, 4): pp = build_pdf(ws, "%s_%s" % (c, proc), ngaus, ngaus == 3) pp.fitTo( ws.data("sig_%s_mass_m%1.0f%s" % (proc, options.mH, c)), RooFit.Strategy(0), *fitopt) rpdfs.append(pp) pdf = RooAddPdf("hggpdfrel%s" % c, "hggpdfrel%s" % c, RooArgList(*tuple(rpdfs))) else: if options.refitall and clables[c][0] == "Di-jet": for ngaus in range(1, 5): pdf = build_pdf(ws, c, ngaus, ngaus == 5) pdf.fitTo(ds, RooFit.Strategy(0), *fitopt) else: for ngaus in range(1, 4): pdf = build_pdf(ws, c, ngaus, ngaus == 3) pdf.fitTo(ds, RooFit.Strategy(0), *fitopt) else: pdfs = (wspdf.pdf("hggpdfrel%s_%s" % (c, p)) for p in processes) pdf = RooAddPdf("hggpdfrel%s" % c, "hggpdfrel%s" % c, RooArgList(*pdfs)) helper.add(pdf, pdf.GetName()) plot_pdf = pdf.Clone("pdf_%s" % dsname) plot_ds = RooDataHist("plot_%s" % dsname, "plot_%s" % dsname, aset, "plot") plot_ds.add(ds) cdf = pdf.createCdf(aset) hmin, hmax, hm = get_FWHM(mass, pdf, cdf, options.mH - 10., options.mH + 10.) wmin, wmax = get_eff_sigma(mass, pdf, cdf, options.mH - 10., options.mH + 10.) ### hmin, hmax, hm = get_FWHM( points ) helper.add(plot_ds, plot_ds.GetName()) helper.add(plot_pdf, plot_pdf.GetName()) helper.add((wmin, wmax), "eff_sigma%s" % c) helper.add((hmin, hmax, hm), "FWHM%s" % c) helper.add(ds.sumEntries(), "sumEntries%s" % c) # signal model integral # data integral for PAS tables data = ws.data("data_mass%s" % c) helper.add( data.sumEntries("CMS_hgg_mass>=%1.4f && CMS_hgg_mass<=%1.4f" % (options.mH - 10., options.mH + 10.)), "data_sumEntries%s" % c) del cdf del pdf dsname = "sig_mass_m%1.0f_combcat" % options.mH print dsname combined_ds = helper.dsets[0].Clone(dsname) for d in helper.dsets[1:]: combined_ds.append(d) if options.binned: binned_ds = RooDataHist("binned_%s" % dsname, "binned_%s" % dsname, aset, combined_ds) pdf = RooKeysPdf("pdf_%s" % (dsname), "pdf_%s" % dsname, mass, combined_ds) plot_pdf = RooHistPdf("pdf_%s" % dsname, "pdf_%s" % dsname, aset, plot_ds) helper.add(binned_ds, binned_ds.GetName()) else: #### pdf = build_pdf(ws,"_combcat") #### pdf.fitTo(combined_ds, RooFit.Strategy(0), *fitopt ) #### plot_pdf = pdf.Clone( "pdf_%s" % dsname ) pdf = RooAddPdf( "pdf_%s" % dsname, "pdf_%s" % dsname, RooArgList(*(helper.histos["hggpdfrel%s" % c] for c in categories))) plot_pdf = pdf cdf = pdf.createCdf(aset) plot_ds = RooDataHist("plot_%s" % dsname, "plot_%s" % dsname, aset, "plot") plot_ds.add(combined_ds) wmin, wmax = get_eff_sigma(mass, pdf, cdf, options.mH - 10., options.mH + 10.) hmin, hmax, hm = get_FWHM(mass, pdf, cdf, options.mH - 10., options.mH + 10.) helper.add(plot_ds, plot_ds.GetName()) helper.add(plot_pdf, plot_pdf.GetName()) helper.add((wmin, wmax), "eff_sigma_combcat") helper.add((hmin, hmax, hm), "FWHM_combcat") helper.add(plot_ds.sumEntries(), "sumEntries_combcat") mass.setRange("higgsrange", options.mH - 25., options.mH + 15.) del cdf del pdf del helper.dsets ### label = TLatex(0.1812081,0.8618881,"#scale[0.8]{#splitline{CMS preliminary}{Simulation}}") label = TLatex(0.7, 0.86, "#scale[0.65]{#splitline{CMS preliminary}{Simulation}}") label.SetNDC(1) ## ## Make web page with plots ## if options.doWebPage: hth = HtmlHelper(options.outdir) hth.navbar().cell(HtmlTag("a")).firstChild().txt("..").set( "href", "../?C=M;O=D") hth.navbar().cell(HtmlTag("a")).firstChild().txt("home").set( "href", "./") tab = hth.body().add(HtmlTable()) ip = 0 for c in ["_combcat"] + categories: ### for c in categories: if options.doWebPage and ip % 4 == 0: row = tab.row() ip = ip + 1 dsname = "sig_mass_m%1.0f%s" % (options.mH, c) canv = TCanvas(dsname, dsname, 600, 600) helper.objs.append(canv) ### leg = TLegend(0.4345638,0.6835664,0.9362416,0.9178322) leg = TLegend(0.2, 0.96, 0.5, 0.55) #apply_modifs( leg, [("SetLineColor",kWhite),("SetFillColor",kWhite),("SetFillStyle",0),("SetLineStyle",0)] ) hplotcompint = mass.frame(RooFit.Bins(250), RooFit.Range("higgsrange")) helper.objs.append(hplotcompint) helper.objs.append(leg) plot_ds = helper.histos["plot_%s" % dsname] plot_pdf = helper.histos["pdf_%s" % dsname] wmin, wmax = helper.histos["eff_sigma%s" % c] hmin, hmax, hm = helper.histos["FWHM%s" % c] print hmin, hmax, hm style = (RooFit.LineColor(kBlue), RooFit.LineWidth(2), RooFit.FillStyle(0)) style_seff = ( RooFit.LineWidth(2), RooFit.FillStyle(1001), RooFit.VLines(), RooFit.LineColor(15), ) style_ds = (RooFit.MarkerStyle(kOpenSquare), ) plot_ds.plotOn(hplotcompint, RooFit.Invisible()) plot_pdf.plotOn(hplotcompint, RooFit.NormRange("higgsrange"), RooFit.Range(wmin, wmax), RooFit.FillColor(19), RooFit.DrawOption("F"), *style_seff) seffleg = hplotcompint.getObject(int(hplotcompint.numItems() - 1)) plot_pdf.plotOn(hplotcompint, RooFit.NormRange("higgsrange"), RooFit.Range(wmin, wmax), RooFit.LineColor(15), *style_seff) plot_pdf.plotOn(hplotcompint, RooFit.NormRange("higgsrange"), RooFit.Range("higgsrange"), *style) pdfleg = hplotcompint.getObject(int(hplotcompint.numItems() - 1)) plot_ds.plotOn(hplotcompint, *style_ds) pointsleg = hplotcompint.getObject(int(hplotcompint.numItems() - 1)) iob = int(hplotcompint.numItems() - 1) leg.AddEntry(pointsleg, "Simulation", "pe") leg.AddEntry(pdfleg, "Parametric model", "l") leg.AddEntry(seffleg, "#sigma_{eff} = %1.2f GeV " % (0.5 * (wmax - wmin)), "fl") clabel = TLatex(0.74, 0.65, "#scale[0.65]{#splitline{%s}{%s}}" % clables[c]) clabel.SetNDC(1) helper.objs.append(clabel) hm = hplotcompint.GetMaximum() * 0.5 * 0.9 ### hm = pdfleg.GetMaximum()*0.5 fwhmarrow = TArrow(hmin, hm, hmax, hm) fwhmarrow.SetArrowSize(0.03) helper.objs.append(fwhmarrow) fwhmlabel = TPaveText(0.20, 0.58, 0.56, 0.48, "brNDC") fwhmlabel.SetFillStyle(0) fwhmlabel.SetLineColor(kWhite) reducedFWHM = (hmax - hmin) / 2.3548200 fwhmlabel.AddText("FWHM/2.35 = %1.2f GeV" % reducedFWHM) helper.objs.append(fwhmlabel) hplotcompint.SetTitle("") hplotcompint.GetXaxis().SetNoExponent(True) hplotcompint.GetXaxis().SetTitle("m_{#gamma#gamma} (GeV)") hplotcompint.GetXaxis().SetNdivisions(509) ## hplotcompint.GetYaxis().SetTitle("A.U."); ## hplotcompint.GetYaxis().SetRangeUser(0.,hplotcompint.GetMaximum()*1.4); hplotcompint.Draw() leg.Draw("same") label.Draw("same") clabel.Draw("same") fwhmarrow.Draw("<>") fwhmlabel.Draw("same") plot_ds.sumEntries() if options.doWebPage: hpl = HtmlPlot(canv, False, "", True, True, True) hpl.caption("<i>%s</i>" % canv.GetTitle()) row.cell(hpl) else: if os.path.isdir(options.outdir) is False: os.mkdir(options.outdir) for ext in "C", "png", "pdf": canv.SaveAs( os.path.join(options.outdir, "%s.%s" % (canv.GetName(), ext))) if "comb" in c: ip = 0 if options.doWebPage: print "Creating pages..." hth.dump() for f in helper.files: f.Close() gROOT.Reset() from pprint import pprint pprint(helper) print 'Summary statistics per event class' print 'Cat\tSignal\t\tData/GeV (in %3.1f+/-10)\tsigEff\tFWHM/2.35' % options.mH sigTotal = 0. dataTotal = 0. for c in categories: sigVal = helper.histos["sumEntries%s" % c] datVal = helper.histos["data_sumEntries%s" % c] sigTotal += sigVal dataTotal += datVal for c in categories: sigVal = helper.histos["sumEntries%s" % c] datVal = helper.histos["data_sumEntries%s" % c] effSig = 0.5 * (helper.histos["eff_sigma%s" % c][1] - helper.histos["eff_sigma%s" % c][0]) fwhm = (helper.histos["FWHM%s" % c][1] - helper.histos["FWHM%s" % c][0]) / 2.3548200 print c, '\t%3.1f (%3.1f%%)\t%3.1f (%3.1f%%)\t\t\t%2.2f\t%2.2f' % ( sigVal, 100. * sigVal / sigTotal, datVal / (10. + 10.), 100. * datVal / dataTotal, effSig, fwhm) print "Done."
loadDatasetLb = RooDataSet( 'loadData16', 'loadData16', inN16, RooArgSet(massLb, massBd, massbD, massBs, massKK, massKp, masskP, tk1Pt, tk2Pt)) frame = "lbtkMass>5.3&&lbtkMass<6." ptsel = "tk1Pt>2. && tk2Pt>2." Bdbkg = "((bdMass>5.25&&bdMass<5.31)&&(kpiMass>0.86&&kpiMass<0.93))" bDbkg = "(bdbarMass>5.25&&bdbarMass<5.31&&kpibarMass<0.93)" bsbkg = "(bsMass>5.25&&bsMass<5.4&&kkMass>1.01&&kkMass<1.03)" cutData = loadDatasetLb.reduce('&&'.join([frame, ptsel])) bkgComb = loadDatasetLb.reduce('&&'.join([frame, ptsel]) + '&&' + '||'.join([Bdbkg, bDbkg, bsbkg])) space.factory('Polynomial::pPDF(lbtkMass,{c1[0.01,-5.,5.]})') keyPDF = RooKeysPdf('kPDF', 'kPDF', massLb, bkgComb, RooKeysPdf.NoMirror, 0.1) getattr(space, 'import')(keyPDF) space.factory( 'SUM::totPDF(numBKG[{initNum},0.,{maxNum}]*pPDF,{bkgNum}*kPDF)'.format( initNum=cutData.sumEntries() - bkgComb.sumEntries(), maxNum=cutData.sumEntries() * 1.05, bkgNum=bkgComb.sumEntries())) fitModel = space.factory('totPDF') massLb.setRange('leftSideband', 5.3, 5.619 - 0.03) massLb.setRange('rightSideband', 5.619 + 0.03, 6.) fitRes = fitModel.fitTo(cutData, RooFit.Minos(True), RooFit.Range('leftSideband,rightSideband'), RooFit.Save())
ws_file0 = TFile("/afs/cern.ch/user/"+prefix+"/"+user+"/eos/lhcb/user/"+prefix+"/"+user+"/WrongSign/2015/WorkSpaces/Merged_WS_Bin_"+str(i)+".root", "read") wsp = ws_file0.Get("wspace") ws_file0.Close() dataset_RS_dtb = wsp.data("dataset_RS") dataset_RS_dtb.SetName("dataset_RS_dtb") #Open secondary shape for given in start = datetime.now() dataset_COMB_CORR_dtb_init = RooDataSet("dataset_COMB_CORR_dtb_init","Decaytime bin"+str(i),dataset_COMB_CORR,varset_small,"Dst_DTF_D0_CTAU>"+str(bin[0]*ctau)+"&&Dst_DTF_D0_CTAU<"+str(bin[1]*ctau)) dataset_COMB_CORR_dtb = Subtract_Distribution(dataset_COMB_CORR_dtb_init, DTF_D0sPi_M, LOG_D0_IPCHI2_OWNPV, str(i)+"_comb", True) dataset_COMB_CORR_dtb.SetName("dataset_COMB_CORR_dtb") print "Background substraction from combinatorial tool "+str(datetime.now()-start)+" \n" hist_COMB = RooDataHist("hist_COMB","hist_COMB", RooArgSet(LOG_D0_IPCHI2_OWNPV), dataset_COMB_CORR_dtb) shape_COMB = RooHistPdf("shape_COMB", "shape_COMB", RooArgSet(LOG_D0_IPCHI2_OWNPV), hist_COMB) key_COMB = RooKeysPdf("key_COMB", "key_COMB", LOG_D0_IPCHI2_OWNPV, dataset_COMB_CORR_dtb) Prim_Yield = RooRealVar("Prim_Yield", "Prim_Yield", 1000000,0,1e7); Sec_Yield = RooRealVar("Sec_Yield", "Sec_Yield", 0,0,1e7); #Choise of fit model used to fit primary decays: fage (s_fage_ or histogram of 0 bin (shape_RS_0) #Two_hists = RooAddPdf("Two_hists", "Two_hists", RooArgList(shape_RS_0, key_COMB), RooArgList(Prim_Yield, Sec_Yield)) Two_hists = RooAddPdf("Two_hists", "Two_hists", RooArgList(s_fage, key_COMB), RooArgList(Prim_Yield, Sec_Yield)) LOG_D0_IPCHI2_OWNPV.setRange("All", -10, 10) LOG_D0_IPCHI2_OWNPV.setRange("Signal", -10, Log(9)) LOG_D0_IPCHI2_OWNPV.setRange("Background", -10, 10) #N.B. Here, Signal region is one that we use for analysis, bagkround - is one with secondaries. #We can play with fit limits if we fail to describe signal shape properly
# fit lbtkMass in antiLb MC {{{ if fitToLbMass_AntiLbTkMC: fitLabel='lbtkDist_antilbtkMC' mass=space.var('lbtkMass') inN=inFF.Get('pLbTk/AntiLbTk') #dataset=RooDataSet('dataset','dataset',inN,RooArgSet(mass,tk1Pt,tk2Pt)).reduce('tk1Pt>3.&&tk2Pt>2.') dataset=RooDataSet('dataset','dataset',inN,RooArgSet(mass)) fitBkg=dataset.reduce(RooFit.Name('fitData_{0}'.format(fitLabel))) #getattr(space,'import')(fitBkg) # create target PDF. #space.factory('KeysPdf::kPDF_{0}(lbtkMass,fitData_{0},NoMirror,2.0)'.format(fitLabel)) #myPDF=space.pdf('kPDF_{0}'.format(fitLabel)) keyPDF=RooKeysPdf('kPDF_{0}'.format(fitLabel),'kPDF_{0}'.format(fitLabel),mass,fitBkg,RooKeysPdf.NoMirror,2.0) getattr(space,'import')(keyPDF) myPDF=keyPDF myFrame=mass.frame(RooFit.Title(fitLabel),RooFit.Title(fitLabel)) if setAxis: myFrame=mass.frame(xaxisMin,xaxisMax) myFrame.GetXaxis().SetTitle('J/#psi p K^{-} Mass(GeV)') dataset.plotOn(myFrame) myPDF.plotOn(myFrame,RooFit.LineColor(2)) myFrame.Draw() figDir.cd() myFrame.Write(fitLabel) canv.SaveAs(outFig)
def createKDE(self, data, htype): starttime = time.time() keys = RooKeysPdf('keys%s' % htype, 'keys%s' % htype, self.roopred, data) self.pdfs[htype] = keys print 'Creating KDE pdf for %s took %s' % (htype, time.strftime("%H:%M:%S", time.gmtime(time.time()-starttime)))
def create_pdfs(self, paths_to_data, save=True): """ Create KDE pdfs for network output distribution """ if not isinstance(paths_to_data, list): paths_to_data = [paths_to_data] files_H = [] files_A = [] for path in paths_to_data: # These are merged, so only one of each if not path.endswith('/'): path += '/' files_H.append(path+'model_H_merged.h5') files_A.append(path+'model_A_merged.h5') # Find background samples files_bgnd = [] for path in paths_to_data: for sample in self.samples: if sample not in ['H', 'A']: files_bgnd.append(path+'/model_%s_merged.h5' % sample) # Read in the data dataH_X, _, feats = self.read_array_from_files(files_H) dataH_X, _ = select_features(dataH_X, feats, include_mass_variables=False) dataH_X = self.scaler.transform(dataH_X) rho = 0.8 #ntemplateevents = 1000000 ntemplateevents = 500000 #ntemplateevents = 50000 print 'Limiting events for pdf creation to %d' % ntemplateevents # 700k -> 4h 42m # 500k -> 2h 30m # 200k -> 0h 27m # 100k -> 0h 7m # 50k -> 0h 2m dataH_X = dataH_X[:ntemplateevents] # Predict, fill a RooDataHist starttime = time.time() predsH = self.model.predict(dataH_X)[:,1] self.roopred = RooRealVar('roopred', 'roopred', 0, 1) roopreddataH = RooDataSet('roopreddataH', 'roopreddataH', RooArgSet(self.roopred)) for pred in predsH: self.roopred.setVal(pred) roopreddataH.add(RooArgSet(self.roopred)) # Create the KDE pdfs def createKDE(self, data, htype): starttime = time.time() keys = RooKeysPdf('keys%s' % htype, 'keys%s' % htype, self.roopred, data) self.pdfs[htype] = keys print 'Creating KDE pdf for %s took %s' % (htype, time.strftime("%H:%M:%S", time.gmtime(time.time()-starttime))) from ROOT.RooKeysPdf import NoMirror keysH = RooKeysPdf('keysH', 'keysH', self.roopred, roopreddataH, NoMirror, rho) self.pdfs['H'] = keysH # Do the same for A dataA_X, _, feats = self.read_array_from_files(files_A) dataA_X, _ = select_features(dataA_X, feats, include_mass_variables=False) dataA_X = self.scaler.transform(dataA_X) dataA_X = dataA_X[:ntemplateevents] starttime = time.time() predsA = self.model.predict(dataA_X)[:,1] roopreddataA = RooDataSet('roopreddataA', 'roopreddataA', RooArgSet(self.roopred)) for pred in predsA: self.roopred.setVal(pred) roopreddataA.add(RooArgSet(self.roopred)) keysA = RooKeysPdf('keysA', 'keysA', self.roopred, roopreddataA, NoMirror, rho) self.pdfs['A'] = keysA if save: ws = RooWorkspace('ws', 'ws') getattr(ws, 'import')(self.roopred) getattr(ws, 'import')(self.pdfs['H']) getattr(ws, 'import')(self.pdfs['A']) ws.writeToFile('%s/pdfs_%s.root' % (self.outdir, self.title))