def DoRooFit(histo, title): can = makeCMSCanvas(str(random.random()),"Fit result ",900,700) #Varible if "ele" in title: x1 = RooRealVar("x1","m_{e^{+}e^{-}}",80,100) if "mu" in title: x1 = RooRealVar("x1","m_{#mu^{+}#mu^{-}}",80,100) #Define CB function m = RooRealVar("mean_{CB}","mean of gaussian",60,120) s = RooRealVar("#sigma_{CB}","width of gaussian",0,3) a = RooRealVar("#alpha_{CB}","mean of gaussian",0,100) n = RooRealVar("n_{CB}","width of gaussian",0,5) CB = RooCBShape("CB","CB PDF",x1, m, s, a, n) m.setConstant(kFALSE) s.setConstant(kFALSE) a.setConstant(kFALSE) n.setConstant(kFALSE) #Define Gaussian function mean1 = RooRealVar("mean_{G}","mean of gaussian",-60,60) sigma1 = RooRealVar("#sigma_{G}","width of gaussian",0,10) gauss1 = RooGaussian("gauss1","gaussian PDF",x1,mean1,sigma1) mean1.setConstant(kFALSE) sigma1.setConstant(kFALSE) #Starting values of the parameters mean1.setVal(1.0) sigma1.setVal(1.0) m.setVal(90.0) s.setVal(1.0) a.setVal(10.0) n.setVal(2.0) # Construct CB (x) gauss x1.setBins(10000, "cache") CBxG = RooFFTConvPdf("CBxG", "CB (X) gauss", x1, CB, gauss1) can.cd() d = RooDataHist("d","d",RooArgList(x1),RooFit.Import(histo)) CBxG.fitTo(d, RooLinkedList()) # Plot PDF and toy data overlaid xframe2 = x1.frame(RooFit.Name("xframe"),RooFit.Title("")) # RooPlot d.plotOn(xframe2, RooLinkedList() ) CBxG.paramOn(xframe2, RooFit.Layout(0.65,0.99,0.9)) xframe2.getAttText().SetTextSize(0.03) CBxG.plotOn(xframe2) xframe2.Draw() can.SaveAs("DataVsMC/FitResults/"+title+"_Roofit.pdf") can.SaveAs("DataVsMC/FitResults/"+title+"_Roofit.png") return;
def fit(self): sim_pdf = RooSimultaneous("simPdf", "simultaneous pdf", self.sample) self.individual_results = {} for name, model in self.models.iteritems(): fit_input = self.fit_data[name] model.fitTo(fit_input.real_data_roofit_histogram()) self.individual_results[name] = fit_input.get_results() sim_pdf.addPdf(model, name) argument_list = RooLinkedList() argument_list.Add(RooFit.Minimizer("Minuit2", "Migrad")) argument_list.Add(RooFit.NumCPU(1)) argument_list.Add(RooFit.Extended()) argument_list.Add(RooFit.Save()) sim_pdf.fitTo(self.comb_data, # argument_list ) # sim_pdf.fitTo( self.combined_data, # RooFit.Minimizer( "Minuit2", "Migrad" ) ) # sim_pdf.fitTo( data = self.combined_data, # arg1 = RooFit.Minimizer( "Minuit2", "Migrad" ), # arg2 = RooFit.NumCPU( 1 ), # arg3 = RooFit.Extended(), # arg4 = RooFit.Save() ) # sim_pdf.fitTo( self.combined_data, # argument_list ) # get fit results results = {} for variable, fit_input in self.fit_data.iteritems(): results[variable] = fit_input.get_results() self.results = results return results
RooFit.Timer(), RooFit.Save(), RooFit.Strategy(config['FitConfig']['Strategy']), RooFit.Optimize(config['FitConfig']['Optimize']), RooFit.Offset(config['FitConfig']['Offset']), RooFit.NumCPU(config['FitConfig']['NumCPU']) ] # set up blinding for data fitopts.append( RooFit.Verbose(not (config['IsData'] and config['Blinding']))) if config['IsData'] and config['Blinding']: from ROOT import RooMsgService RooMsgService.instance().setGlobalKillBelow(RooFit.WARNING) fitopts.append(RooFit.PrintLevel(-1)) fitOpts = RooLinkedList() for o in fitopts: fitOpts.Add(o) # fit rawfitresult = fitpdf['pdf'].fitTo(tupleDataSet, fitOpts) # pretty-print the result from B2DXFitters.FitResult import getDsHBlindFitResult result = getDsHBlindFitResult(config['IsData'], config['Blinding'], rawfitresult) print result ''' doubleCanvas = TCanvas(); frameList = []
setConstantIfSoConfigured(fitconfig, fitpdf['pdf']) # set up fitting options fitopts = [ RooFit.Timer(), RooFit.Save(), RooFit.Strategy(fitconfig['FitConfig']['Strategy']), RooFit.Optimize(fitconfig['FitConfig']['Optimize']), RooFit.Offset(fitconfig['FitConfig']['Offset']), RooFit.NumCPU(fitconfig['FitConfig']['NumCPU']) ] # set up blinding for data fitopts.append(RooFit.Verbose(not (fitconfig['IsData'] and fitconfig['Blinding']))) if fitconfig['IsData'] and fitconfig['Blinding']: from ROOT import RooMsgService RooMsgService.instance().setGlobalKillBelow(RooFit.WARNING) fitopts.append(RooFit.PrintLevel(-1)) fitOpts = RooLinkedList() for o in fitopts: fitOpts.Add(o) # fit rawfitresult = fitpdf['pdf'].fitTo(ds1, fitOpts) #p0End = rawfitresult.floatParsFinal().find('Bs2DsPi_mistagcalib_p0'); #p1End = rawfitresult.floatParsFinal().find('Bs2DsPi_mistagcalib_p1'); genEtaList.AddLast(rawfitresult.floatParsFinal().find('eta')); '''print "\n\n\nbleah\n",rawfitresult.floatParsFinal().Print(),"\n\nboh\n\n\n\n"; ''' # pretty-print the result from B2DXFitters.FitResult import getDsHBlindFitResult result = getDsHBlindFitResult(fitconfig['IsData'], fitconfig['Blinding'], rawfitresult) print 'RESULT',result rawfitresult.floatParsFinal().Print()
def plot(pad, w, pdfname, dsetname, resid=False, cat=None, popts=None, dopts=None): if cat: if cat.startswith('b0_'): cat = cat.replace('b0_', '') if cat.startswith('bs_'): cat = cat.replace('bs_', '') xtitle = { 'b0g': '#it{m}(' + rt.Dzb + rt.g + rt.Kp + rt.pim + ') [MeV/c^{2}]', 'b0pi0': '#it{m}(' + rt.Dzb + rt.piz + rt.Kp + rt.pim + ') [MeV/c^{2}]', 'bsg': '#it{m}(' + rt.Dzb + rt.g + rt.Km + rt.pip + ') [MeV/c^{2}]', 'bspi0': '#it{m}(' + rt.Dzb + rt.piz + rt.Km + rt.pip + ') [MeV/c^{2}]' } leg = { 'b0g': '#it{B}^{0} #rightarrow #bar{#it{D}}^{0}#it{#gamma}#it{K}^{+}#it{#pi}^{-}', 'b0pi0': '#it{B}^{0} #rightarrow #bar{#it{D}}^{0}#it{#pi}^{0}#it{K}^{+}#it{#pi}^{-}', 'bsg': '#it{B_{s}}^{#kern[-0.2]{0}} #rightarrow #bar{#it{D}}^{0}#it{#gamma}#it{K}^{-}#it{#pi}^{+}', 'bspi0': '#it{B_{s}}^{#kern[-0.2]{0}} #rightarrow #bar{#it{D}}^{0}#it{#pi}^{0}#it{K}^{-}#it{#pi}^{+}' } if not popts: popts = RooLinkedList() if not dopts: dopts = RooLinkedList() pl = w.var('B_DTFDict_D0_B_M').frame() if cat: pl.GetXaxis().SetTitle(xtitle[cat]) if w.data(dsetname): w.data(dsetname).plotOn(pl, dopts) if w.pdf(pdfname): w.pdf(pdfname).plotOn(pl, popts) if resid and w.data(dsetname) and w.pdf(pdfname): pu = TPad(pad.GetName() + '_pu', '', 0., 0.33, 1., 1.) pd = TPad(pad.GetName() + '_pd', '', 0., 0., 1., 0.33) pu.SetBottomMargin(0.03) pd.SetTopMargin(0.03) pd.SetBottomMargin(0.35) pad.cd() pu.Draw() pd.Draw() pu.cd() pl.GetYaxis().SetTitleOffset(1.1) pl.GetXaxis().SetLabelSize(0.) pl.Draw() pd.cd() pull = pl.pullHist() # roofit does something funny with pull hist range so need a dummy hist for it pull.Draw() ymax = max(abs(pull.GetYaxis().GetXmin()), pull.GetYaxis().GetXmax()) pulld = TH1F(pull.GetName() + '_pd', '', pl.GetNbinsX(), pl.GetXaxis().GetXmin(), pl.GetXaxis().GetXmax()) pulld.GetYaxis().SetRangeUser(-ymax, ymax) pulld.GetXaxis().SetTitle(pl.GetXaxis().GetTitle()) pulld.GetYaxis().SetTitle("Pull") pl.GetXaxis().SetTitle("") pulld.GetXaxis().SetTitleSize(0.13) pulld.GetYaxis().SetTitleSize(0.13) pulld.GetXaxis().SetLabelSize(0.13) pulld.GetYaxis().SetLabelSize(0.13) pulld.GetXaxis().SetTitleOffset(1.1) pulld.GetYaxis().SetTitleOffset(0.6) pulld.GetYaxis().SetNdivisions(505) pulld.GetXaxis().SetTickLength(0.06) ll = TLine() ll.SetLineWidth(4) ll.SetLineColor(4) pd.cd() pd.Clear() pulld.Draw("HIST") pull.Draw("EPsame") ll.DrawLine(pulld.GetXaxis().GetXmin(), 0., pulld.GetXaxis().GetXmax(), 0.) pulld.Draw("AXISsame") gROOT.Append(pulld) else: pad.cd() pl.Draw() pad.cd() if cat: lat = TLatex() lat.SetTextSize(0.05) lat.SetNDC() lat.DrawLatex(0.6, 0.8, leg[cat]) gROOT.Append(lat) pad.Update() pad.Modified()
def fit(self): fit_variable = RooRealVar("fit_variable", "fit_variable", self.fit_boundaries[0], self.fit_boundaries[1]) fit_variable.setBins(self.histograms[self.data_label].nbins()) variables = RooArgList() variables.add(fit_variable) variable_set = RooArgSet() variable_set.add(fit_variable) roofit_histograms = {} roofit_pdfs = {} roofit_variables = {} N_min = 0. N_max = self.normalisation[self.data_label] * 2. pdf_arglist = RooArgList() variable_arglist = RooArgList() roofit_histograms[self.data_label] = RooDataHist( self.data_label, self.data_label, variables, self.histograms[self.data_label]) for sample in self.samples: roofit_histogram = RooDataHist(sample, sample, variables, self.histograms[sample]) roofit_histograms[sample] = roofit_histogram roofit_pdf = RooHistPdf('pdf' + sample, 'pdf' + sample, variable_set, roofit_histogram) roofit_pdfs[sample] = roofit_pdf roofit_variable = RooRealVar(sample, sample + " events", self.normalisation[sample], N_min, N_max) roofit_variables[sample] = roofit_variable pdf_arglist.add(roofit_pdf) variable_arglist.add(roofit_variable) model = RooAddPdf('model', 'sum of all known', pdf_arglist, variable_arglist) use_model = model if self.constraints: arg_set = RooArgSet(model) constraints = self.get_fit_normalisation_constraints( model, roofit_variables) for constraint in constraints: arg_set.add(constraint) model_with_constraints = RooProdPdf( "model_with_constraints", "model with gaussian constraints", arg_set, RooLinkedList()) use_model = model_with_constraints if self.method == 'TMinuit': #WARNING: number of cores changes the results!!! self.saved_result = use_model.fitTo( roofit_histograms[self.data_label], RooFit.Minimizer("Minuit2", "Migrad"), RooFit.NumCPU(1), RooFit.Extended(), RooFit.Save(), ) results = {} for sample in self.samples: results[sample] = (roofit_variables[sample].getVal(), roofit_variables[sample].getError()) self.results = results
tot = RooAddPdf("tot", "g+cheb", RooArgList(signal, bkg), RooArgList(nSig, nBkg)) mean.setVal(phimean) gamma.setConstant(ROOT.kTRUE) mean.setConstant(ROOT.kTRUE) rfit = tot.fitTo(b0dataNonPrompt, Range(massmin, massmax), RooFit.NumCPU(8)) mean.setConstant(ROOT.kFALSE) rfit = tot.fitTo(b0dataNonPrompt, Range(massmin, massmax), RooFit.NumCPU(8)) gamma.setConstant(ROOT.kFALSE) rfit = tot.fitTo(b0dataNonPrompt, Range(phimean - 0.025, phimean + 0.025), RooFit.NumCPU(8)) masskkFrame = masskk.frame(Range(phimean - 0.025, phimean + 0.025)) b0dataNonPrompt.plotOn(massFrame, RooLinkedList()) tot.plotOn(masskkFrame) massFrame.Draw() c.SaveAs("testmassFit.png") cD = TCanvas("cD", "cD", 750, 600) cD.cd() splot = RooStats.SPlot("sPlot", "sPlot", b0dataNonPrompt, tot, RooArgList(nSig, nBkg)) # In[18]: dstree = b0dataNonPrompt.store().tree() # In[19]:
# In[10]: alldata.numEntries() c = TCanvas("canvas","canvas",1200,800) massFrame = mass.frame() alldata.plotOn(massFrame) massFrame.Draw() c.SaveAs("plots/testmass.png") massKKFrame = masskk.frame(Range(phimin,phimax)) alldata.plotOn(massKKFrame,RooLinkedList()) massKKFrame.Draw() c.SaveAs("plots/testmasskk.png") # In[11]: #b0dataNonPromptMass = b0dataNonPrompt.reduce(SelectVars(RooArgSet(mass))) # In[12]: sigma1 = RooRealVar("sigma1","width of gaussian1",0.002,0.0005,0.05);
def get_fitted_normalisation_from_ROOT(channel, input_files, variable, met_type, b_tag_bin): results = {} initial_values = {} templates = {} for variable_bin in variable_bins_ROOT[variable]: histograms = get_histograms(channel, input_files, variable=variable, met_type=met_type, variable_bin=variable_bin, b_tag_bin=b_tag_bin, rebin=measurement_config.rebin) # create signal histograms h_eta_signal = histograms['TTJet'] + histograms['SingleTop'] N_ttbar_before_fit = histograms['TTJet'].Integral() N_SingleTop_before_fit = histograms['SingleTop'].Integral() N_vjets_before_fit = histograms['V+Jets'].Integral() N_qcd_before_fit = histograms['QCD'].Integral() N_signal_before_fit = N_ttbar_before_fit + N_SingleTop_before_fit N_ttbar_error_before_fit = sum(histograms['TTJet'].errors()) N_SingleTop_error_before_fit = sum(histograms['SingleTop'].errors()) N_vjets_error_before_fit = sum(histograms['V+Jets'].errors()) N_QCD_error_before_fit = sum(histograms['QCD'].errors()) if (N_SingleTop_before_fit != 0): TTJet_SingleTop_ratio = N_ttbar_before_fit / N_SingleTop_before_fit else: print 'Bin ', variable_bin, ': ttbar/singleTop ratio undefined for %s channel! Setting to 0.' % channel TTJet_SingleTop_ratio = 0 leptonAbsEta = RooRealVar("leptonAbsEta", "leptonAbsEta", 0., 2.4) # this has to move to dps.utils.Fitting.py vars = RooArgList() vars.add(leptonAbsEta) vars_set = RooArgSet() vars_set.add(leptonAbsEta) n_event_obs = histograms['data'].Integral() lowerBound = 0. upperBound = n_event_obs + 10 * sqrt(n_event_obs) n_init = n_event_obs / 2. data = RooDataHist("data", "dataset with leptonAbsEta", vars, histograms['data']) rh_vj = RooDataHist("rh_vj", "vj", vars, histograms['V+Jets']) rh_qcd = RooDataHist("rh_qcd", "qcd", vars, histograms['QCD']) rh_signal = RooDataHist("rh_signal", "signal", vars, h_eta_signal) pdf_vj = RooHistPdf("pdf_vj", "V+Jets pdf", vars_set, rh_vj, 0) pdf_qcd = RooHistPdf("pdf_qcd", "QCD pdf ", vars_set, rh_qcd, 0) pdf_signal = RooHistPdf("pdf_signal", "single top pdf", vars_set, rh_signal, 0) # RooRealVar(const char *name, const char *title, Double_t value, Double_t minValue, Double_t maxValue, const char *unit) : nSignal = RooRealVar("nSignal", "number of single top + ttbar events", N_signal_before_fit, lowerBound, upperBound, "event") nvj = RooRealVar("nvj", "number of V+Jets bgnd events", N_vjets_before_fit, lowerBound, upperBound, "event") nqcd = RooRealVar("nqcd", "number of QCD bgnd events", N_QCD_error_before_fit, lowerBound, upperBound, "event") model = RooAddPdf("model", "sig+vj+qcd", RooArgList(pdf_signal, pdf_vj, pdf_qcd), RooArgList(nSignal, nvj, nqcd)) vj_constraint = RooGaussian("nvj_constraint", "nvj_constraint", nvj, RooFit.RooConst(N_vjets_before_fit), RooFit.RooConst(0.5 * N_vjets_before_fit)) qcd_constraint = RooGaussian("nqcd_constraint", "nqcd_constraint", nqcd, RooFit.RooConst(N_qcd_before_fit), RooFit.RooConst(2 * N_qcd_before_fit)) model_with_constraints = RooProdPdf( "model_with_constraints", "model with gaussian constraints", RooArgSet(model, vj_constraint, qcd_constraint), RooLinkedList()) model_with_constraints.fitTo(data, RooFit.Minimizer( "Minuit2", "Migrad")) #WARNING: number of cores changes the results!!! # nll = model.createNLL(data, RooFit.NumCPU(2)) # RooMinuit(nll).migrad() # frame1 = nSignal.frame(RooFit.Bins(100), RooFit.Range(lowerBound, n_event_obs), RooFit.Title("LL and profileLL in nSignal")) # nll.plotOn(frame1, RooFit.ShiftToZero()) # frame2 = nvj.frame(RooFit.Bins(100), RooFit.Range(lowerBound, n_event_obs), RooFit.Title("LL and profileLL in nvj")) # nll.plotOn(frame2, RooFit.ShiftToZero()) # frame3 = nqcd.frame(RooFit.Bins(100), RooFit.Range(lowerBound, n_event_obs), RooFit.Title("LL and profileLL in nqcd")) # nll.plotOn(frame3, RooFit.ShiftToZero()) # # pll_nSignal = nll.createProfile(nSignal) # pll_nSignal.plotOn(frame1, RooFit.LineColor(2)) # frame1.SetMinimum(0) # frame1.SetMaximum(3) # # pll_nvj = nll.createProfile(nvj) # pll_nvj.plotOn(frame2, RooFit.LineColor(2)) # frame2.SetMinimum(0) # frame2.SetMaximum(3) # # pll_nqcd = nll.createProfile(nqcd) # pll_nqcd.plotOn(frame3, RooFit.LineColor(2)) # frame3.SetMinimum(0) # frame3.SetMaximum(3) # c = TCanvas("profilell","profilell",1200, 400) # c.Divide(3) # c.cd(1) # frame1.Draw() # c.cd(2) # frame2.Draw() # c.cd(3) # frame3.Draw() # c.SaveAs('profileLL.png') # model.fitTo(data, RooFit.Minimizer("Minuit2", "Migrad"), RooFit.NumCPU(1))#WARNING: number of cores changes the results!!! fit_results = {} fit_results['signal'] = (nSignal.getVal(), nSignal.getError()) fit_results['QCD'] = ufloat(nqcd.getVal(), nqcd.getError()) fit_results['V+Jets'] = ufloat(nvj.getVal(), nvj.getError()) N_ttbar, N_SingleTop = decombine_result(fit_results['signal'], TTJet_SingleTop_ratio) fit_results['signal'] = ufloat(nSignal.getVal(), nSignal.getError()) fit_results['TTJet'] = ufloat(N_ttbar) fit_results['SingleTop'] = ufloat(N_SingleTop) if results == {}: # empty for sample in fit_results.keys(): results[sample] = [fit_results[sample]] else: for sample in fit_results.keys(): results[sample].append(fit_results[sample]) return results, None, None
#mean.setValV(phimean) gamma.setConstant(ROOT.kTRUE) mean.setConstant(ROOT.kTRUE) # In[ ]: rfit = tot.fitTo(alldata, Range(phimin, phimax), RooFit.NumCPU(8)) mean.setConstant(ROOT.kFALSE) rfit = tot.fitTo(alldata, Range(phimin, phimax), RooFit.NumCPU(8)) gamma.setConstant(ROOT.kFALSE) rfit = tot.fitTo(alldata, Range(phimin, phimax), RooFit.NumCPU(8)) # In[ ]: kkFrame = masskk.frame(Range(phimin, phimax)) alldata.plotOn(kkFrame, RooLinkedList()) tot.plotOn(kkFrame, RooFit.Normalization((nSig.getValV() + nBkg.getValV()))) kkFrame.Draw() c.SaveAs("testmassPhiFit.png") cD = TCanvas("cD", "cD", 750, 600) cD.cd() splot = RooStats.SPlot("sPlot", "sPlot", alldata, tot, RooArgList(nSig, nBkg)) # In[ ]: dstree = alldata.store().tree() dstree.GetEntryNumber(88) # In[ ]:
event = 2 xTuple.SetBranchAddress("event", AddressOf(milk, 'price5')) newfile = TFile("small.root", "recreate") newtree = xTuple.CloneTree() newtree.CopyEntries(xTuple) newtree.Write() sys.exit() # In[16]: file = TFile("newFile.root", "RECREATE") canvas = TCanvas("canvas", "canvas", 1200, 1000) mass = RooRealVar("xM", "M(#mu#mu#mu#mu)[GeV]", 5.15, 5.55) trigger = RooRealVar("trigger", "trigger", 0.0, 10000) vProb = RooRealVar("vProb", "vProb", -1.0, 1.0) alldata = RooDataSet("alldata", "alldata", xTuple, RooArgSet(mass), RooFormulaVar("vProb", "vProb", "vProb>0.01", RooArgList(vProb))) #,cutFormula) frame = mass.frame(Range(5.15, 5.55)) alldata.plotOn(frame, RooLinkedList()) alldata.Write() frame.Draw() # In[ ]: canvas.SaveAs("testCanvas.eps")