def fitNBkg(ibdt, fullbkg): slope = RooRealVar("slope", "slope", 0.5, -10, 10) bkg_exp = RooExponential("bkg_exp", "exponential", slope, theBMass) cut = cut_base + '&& bdt_prob > %s' % (ibdt) theBMass.setRange('sigRangeMC', B0Mass_ - 3 * dict_sigma[ibdt], B0Mass_ + 3 * dict_sigma[ibdt]) databkg = fullbkg.reduce(RooArgSet(theBMass, mumuMass, mumuMassE), cut) r = bkg_exp.fitTo(databkg, RooFit.Save(), ROOT.RooFit.Range('left,right')) frame = theBMass.frame() databkg.plotOn(frame, RooFit.Binning(70), RooFit.MarkerSize(.7)) bkg_exp.plotOn(frame, ) # bkg_exp.fixCoefRange('left,right') nbkg = RooRealVar('nbkg', 'bkg n', 1000, 0, 550000) ebkg = RooExtendPdf('ebkg', 'ebkg', bkg_exp, nbkg, 'sigRangeMC') ebkg.fitTo(databkg, ROOT.RooFit.Range('left,right')) ebkg.plotOn(frame, RooFit.LineStyle(ROOT.kDashed), RooFit.LineColor(ROOT.kGreen + 1), RooFit.Range(4.9, 5.6)) frame.Draw() dict_b_v1[ibdt] = [nbkg.getVal(), nbkg.getError()]
def fitNBkg(ibdt, fullbkg, isample): slope = RooRealVar("slope", "slope", 0.5, -10, 10) bkg_exp = RooExponential("bkg_exp", "exponential", slope, theBMass) cut = cut_base + '&& bdt_prob > %s' % (ibdt) theBMass.setRange('sigRangeMC', B0Mass_ - 3 * dict_sigma[ibdt], B0Mass_ + 3 * dict_sigma[ibdt]) databkg = fullbkg.reduce(RooArgSet(theBMass, mumuMass, mumuMassE), cut) r = bkg_exp.fitTo(databkg, RooFit.Save(), ROOT.RooFit.Range('left,right'), RooFit.PrintLevel(-1)) frame = theBMass.frame() databkg.plotOn(frame, RooFit.Binning(70), RooFit.MarkerSize(.7)) bkg_exp.plotOn(frame, ) canv = ROOT.TCanvas() frame.Draw() nbkg = RooRealVar('nbkg', 'bkg n', 1000, 0, 550000) ebkg = RooExtendPdf( 'ebkg', 'ebkg', bkg_exp, nbkg, 'sigRangeMC') ## here imposing the range to calculate bkg yield ebkg.fitTo(databkg, ROOT.RooFit.Range('left,right'), RooFit.PrintLevel(-1)) ebkg.plotOn(frame, RooFit.LineStyle(ROOT.kDashed), RooFit.LineColor(ROOT.kGreen + 1), RooFit.Range(4.9, 5.6)) frame.Draw() # canv.SaveAs('bkg_fit_bdt%f_sample%i.pdf'%(ibdt,isample)) dict_b_v1[ibdt] = [nbkg.getVal(), nbkg.getError()]
def RooFitSig(mbbarray, bdtarray, weightarray, TC_mass, binstart, binend): fitstart = 40 fitend = 150 mbbarray = range(200) bdtarray = range(200) weightarray = range(200) mass = RooRealVar("X", "m(bb)[GeV]", fitstart, fitend) BDT = RooRealVar("BDT", "BDT", -1, 100) weight = RooRealVar("weight", "weight", -100, 200) branchnames = ["X", "BDT", "weight"] dtype = np.dtype([(branchnames[idx], np.float64) for idx in range(len(branchnames))]) treearray = np.array([(mbbarray[idx], bdtarray[idx], weightarray[idx]) for idx in range(len(mbbarray))], dtype) tree = rnp.array2tree(treearray) m0 = RooRealVar("m0", "m0", TC_mass * 1., TC_mass * 1. - 60., TC_mass * 1. + 60.) m02 = RooRealVar("m02", "m02", TC_mass * 1., TC_mass * 1. - 60., TC_mass * 1. + 60.) alpha = RooRealVar("alpha", "alpha", 1.295, 1.0, 1.6) sigma2 = RooRealVar("sigma2", "sigma2", 35, 8., 100) n = RooRealVar("n", "n", 5, 1, 35) mean = RooRealVar("mean", "mean of gaussian", 750, 0, 6000) sigma = RooRealVar("sigma", "width of gaussian", 90, 38, 300) gauss = RooGaussian("gauss", "gaussian PDF", mass, m0, sigma) gauss2 = RooGaussian("gauss2", "gaussian PDF", mass, m02, sigma2) CBshape = RooCBShape("CBshape", "Crystal Ball PDF", mass, m0, sigma2, alpha, n) ##PDF normalization num1 = RooRealVar("num1", "number of events", 400, 0, 5000) ##relative weight of 2 PDFs f = RooRealVar("f", "f", 0.95, 0.6, 1) sigPdf = RooAddPdf("sigPdf", "Signal PDF", RooArgList(CBshape, gauss), RooArgList(f)) extPdf = RooExtendPdf("extPdf", "extPdf", sigPdf, num1) data = RooDataSet("data", "data", tree, RooArgSet(mass, BDT, weight), "BDT>0", "weight") xframe = mass.frame() mass.setBins(20) data.plotOn(xframe) extPdf.plotOn( xframe) #,Normalization(1.0,RooAbsReal.RelativeExpected),LineColor(1)) hist = extPdf.createHistogram("X", fitend - fitstart) hist.SetAxisRange(binstart, binend) return deepcopy(hist)
xArg = RooArgSet(x) N_times_experiments = 1000 N_number_of_events = 500 # gauss 16 mean16 = RooRealVar("mean16","mean of gaussian",-1,-10,10) sigma16 = RooRealVar("sigma16","width of gaussian",2,0.1,10) gauss16 = RooGaussian("gauss16","gaussian PDF",x,mean16,sigma16) nGauss16 = RooRealVar("nGauss16", "nGauss16", N_number_of_events , 0. ,1000 ) gauss16_ext = RooExtendPdf("gauss16_ext", "extended p.d.f", gauss16, nGauss16) gauss16_ext.plotOn( xframe10 ) # RooMCStudy mgr1 = RooMCStudy(gauss16_ext , gauss16_ext , xArg ) mgr1.generateAndFit( N_times_experiments , N_number_of_events ) # plot list_par = [mean16, sigma16, nGauss16] list_frame1 = RooMCStudy_box( mgr1 , list_par ) # ------------------------------------------- # plot and save
print "for gauss3 before fit" print "mean3: ", mean3.Print()," sigma3: ",sigma3.Print() print "nGauss3: ", nGauss3.Print() # fit gauss3_ext.fitTo(data1) # plot norm3 = nGauss3.getVal() #gauss3_ext.plotOn(xframe2, RooFit.LineColor(RooFit.kRed)) #gauss3_ext.plotOn(xframe2, RooFit.Normalization(norm3, RooAbsReal.NumEvent), RooFit.LineColor(RooFit.kRed)) gauss3_ext.plotOn(xframe2, RooFit.Normalization(1.0, RooAbsReal.RelativeExpected), RooFit.LineColor(RooFit.kRed)) #gauss3.plotOn(xframe2, RooFit.Normalization(norm3, RooAbsReal.NumEvent), RooFit.LineColor(RooFit.kRed)) data1.plotOn(xframe2) gauss1.plotOn(xframe2,RooFit.LineColor(RooFit.kBlue)) print "for data1" print "number of events: ", data1.sumEntries() print "for gauss3 after fit" print "mean3: ", mean3.Print()," sigma3: ",sigma3.Print() print "nGauss3: ", nGauss3.Print() # ------------------------------------------- # 3. Add PDF = gauss4 + gauss5
print "iLeft: ", iLeft.getVal() print "iTotal: ", iTotal.getVal() #iLeft = gauss11_ext.createIntegral(xArg, RooFit.Range("left_gaussian_range")) #iTotal = gauss11_ext.createIntegral(xArg, RooFit.Range("whole_range")) #print "without NormSet" #print "iLeft: ", iLeft.getVal() #print "iTotal: ", iTotal.getVal() n_total_evaluate = nGauss11.getVal() / iLeft.getVal() print "n_total_evaluate: ", n_total_evaluate #gauss11_ext.plotOn(xframe7,RooFit.Normalization( n_total_evaluate , RooAbsReal.NumEvent), RooFit.Range("whole_range"), RooFit.LineColor(RooFit.kRed)) #gauss11_ext.plotOn(xframe7,RooFit.Normalization( nGauss11.getVal() , RooAbsReal.NumEvent), RooFit.Range("whole_range"), RooFit.LineColor(RooFit.kRed)) gauss11_ext.plotOn(xframe7,RooFit.Normalization( nGauss11.getVal() , RooAbsReal.NumEvent), RooFit.Range("left_gaussian_range"), RooFit.LineColor(RooFit.kRed)) #gauss11_ext.plotOn(xframe7,RooFit.Normalization( nGauss11.getVal() , RooAbsReal.NumEvent), RooFit.Range("whole_range"), RooFit.LineColor(RooFit.kRed),RooFit.DrawOption("F"), RooFit.FillColor(RooFit.kRed), RooFit.FillStyle(1001) ) #data3.plotOn(xframe7) data3_half.plotOn(xframe7) #gauss11.plotOn(xframe7,RooFit.LineColor(RooFit.kRed)) # ------------------------------------------- # 8. use combined PDF to fit SB only and test fixAddCoefRange xframe8 = x.frame(RooFit.Title("8. use combined PDF to fit SB only and test fixAddCoefRange")) # gauss 12
norm.Print() mass_var = multipdf.getVariables().first() mass_var.setRange("HiggsMassWindow5",120,130) mass_var.setRange("HiggsMassWindow2",123,127) mass_var.setRange("HiggsMassWindow10",115,135) mass_var.setRange("WholeRange",100,180) frame = mass_var.frame() option = "AL" pdf_information = {} multipdf.plotOn( frame ) for pdf_index in range(0,multipdf.getNumPdfs()): pdf_ = multipdf.getPdf( pdf_index ) print pdf_.GetName() + "_Normalized" , pdf_.GetTitle() pdf = RooExtendPdf( pdf_.GetName() + "_Normalized" , pdf_.GetTitle() , pdf_ , norm ) pdf.plotOn( frame , RooFit.DrawOption(option) , RooFit.LineColor( colors[pdf_index] ) , RooFit.LineStyle(0) , RooFit.LineWidth(3) , RooFit.Range("WholeRange") ) totalInt = pdf.createIntegral( RooArgSet(mass_var) ,RooFit.Range("WholeRange")) #totalInt.Print() , RooFit.NormSet( RooArgSet(mass_var) ), fracInt5 = pdf.createIntegral( RooArgSet(mass_var) ,RooFit.Range("HiggsMassWindow5")) fracInt2 = pdf.createIntegral( RooArgSet(mass_var) ,RooFit.Range("HiggsMassWindow2")) fracInt10 = pdf.createIntegral( RooArgSet(mass_var) ,RooFit.Range("HiggsMassWindow10")) #fracInt.Print() if "A" in option : option = "L" pdf_information[ pdf_index ] = { "name":pdf.GetName() , "pdf":pdf , "ratio5":fracInt5.getVal()/totalInt.getVal(), "ratio2":fracInt2.getVal()/totalInt.getVal(), "ratio10":fracInt10.getVal()/totalInt.getVal() } doSig = False if len(sys.argv) > 3 : fsigName = sys.argv[2] fsig = TFile.Open( fsigName ) sigPdf = fsig.Get( sys.argv[3] )