def NEvtsCalc( uPars, uEPars): Pars = {} Pars[0] = RooRealVar('c1', 'Exponential constant', -1, -10, 0) Pars[1] = RooRealVar('ExpY', 'Background Yield', 100, 0, 10000000) Pars[2] = RooRealVar('Mean', 'Voigtian Mean' , 90.0, 20, 180.0) Pars[3] = RooRealVar('Width', 'Voigtian Width' , 5.0, 0.5, 40.0) Pars[4] = RooRealVar('Sigma', 'Voigtian Sigma' , 5.0, 0.5, 40.0) Pars[5] = RooRealVar('VoY', 'Signal Yield', 100, 0, 10000000) if len(Pars) != len(uPars): print 'The input array has a weird number of entries...' return 0 if len(uPars) != len(uEPars): print 'The input arrays are of different sizes...' return 0 for x in xrange(0, 6): Pars[x].setVal(uPars[x]) Pars[x].setError(uEPars[x]) v = RooRealVar('v', 'Invariant Mass (GeV)', 60, 120) Voigt = RooVoigtian('Voigt', 'Voigtian - Signal', v, Pars[2], Pars[3], Pars[4]) ## Calculate integral from -2sigma to 2sigma VStDev_r = Voigt.sigma(v) VStDev = VStDev_r.getVal() v.setRange("sobRange", Pars[2].getVal() - 2.*VStDev, Pars[2].getVal() + 2.*VStDev) integralSig = Voigt.createIntegral(RooArgSet(v), RooFit.NormSet(RooArgSet(v)), RooFit.Range("sobRange")) FinalNumber = integralSig.getVal()*Pars[5].getVal() return FinalNumber
a1 = RooRealVar("p_1", "p_1", 0.001, -10., 10.) a2 = RooRealVar("p_2", "p_2", -0.00001, -10., 10.) a3 = RooRealVar("p_3", "p_3", -0.000001, -10., 10.) a4 = RooRealVar("p_4", "p_4", -0.000001, -10., 10.) a5 = RooRealVar("p_5", "p_5", -0.000001, -10., 10.) poliset = RooArgList(a0, a1, a2, a3, a4) # gaussFrac = RooRealVar("s","fraction of component 1 in kkSig",0.3,0.0,1.0) nSigKK = RooRealVar("nSig", "nSig", theData.numEntries() * 0.3, 0.0, theData.numEntries() * 1.5) nBkgKK = RooRealVar("nBkg", "nBkg", theData.numEntries() * 0.7, 0.0, theData.numEntries() * 1.5) kkSig = RooVoigtian("kkSig", "kkSig", tt_mass, kkMean, kkGamma, kkSigma) #kkSig = RooGaussian("kkSig","kkSig",tt_mass,kkMean,kkGamma)#,kkSigma) #kkBkg = RooBernstein("kkBkg" , "kkBkg", tt_mass, RooArgList(B_1, B_2,B_3,B_4))#,B_5) )#,B_6)) kkBkg = RooChebychev("kkBkg", "Background", tt_mass, poliset) kkTot = RooAddPdf("kkTot", "kkTot", RooArgList(kkSig, kkBkg), RooArgList(nSigKK, nBkgKK)) nfit = 0 #kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005),RooFit.PrintLevel(-1), RooFit.NumCPU(7),RooFit.Save()) #nfit +=1 if debugging: kkGamma.setConstant(kTRUE) kkMean.setConstant(kTRUE) kkSigma.setConstant(kTRUE)
def RooFitHist(inputhist, title='title', path='.'): # RooFit.gErrorIgnoreLevel = RooFit.kInfo # <6.02 # RooFit.SetSilentMode()# >6.02 # RooMsgService().instance().SetSilentMode(kTRUE) fitbinning = array('d') binwidth = 200 #NBins=(14000/binwidth) - ( (1040/binwidth) + 1 ) NBins = (14000 / binwidth) - ((1040 / binwidth) + 1) for i in range(NBins + 1): fitbinning.append(1050 + i * binwidth) # print(fitbinning) hist = inputhist.Rebin(NBins, "fit parameter", fitbinning) meanstart = hist.GetBinCenter(hist.GetMaximumBin()) sigmastart = hist.GetRMS() print('meanstart:', meanstart, 'sigmastart:', sigmastart) # inputhist.Draw() # hist.Draw() # hold=raw_input('press enter to exit.') gStyle.SetOptFit(1100) gStyle.SetOptTitle(0) RooFit.SumW2Error(kTRUE) mjj = RooRealVar('mjj', 'M_{jj-AK8}', fitbinning[0], fitbinning[len(fitbinning) - 1], 'GeV') mjjral = RooArgList(mjj) dh = RooDataHist('dh', 'dh', mjjral, RooFit.Import(hist)) shapes = {} #Gaussian gaussmean = RooRealVar('#mu_{gauss}', 'mass mean value', meanstart, 0, 2 * meanstart) gausssigma = RooRealVar('#sigma_{gauss}', 'mass resolution', sigmastart, 0, 2 * sigmastart) gauss = RooGaussian('gauss', 'gauss', mjj, gaussmean, gausssigma) shapes.update({'Gauss': gauss}) #CrystalBall mean = RooRealVar('#mu', 'mean', meanstart, 0, 2 * meanstart) sigma = RooRealVar('#sigma', 'sigma', sigmastart, 0, 2 * sigmastart) alpha = RooRealVar('#alpha', 'Gaussian tail', -1000, 0) n = RooRealVar('n', 'Normalization', -1000, 1000) cbshape = RooCBShape('cbshape', 'crystalball PDF', mjj, mean, sigma, alpha, n) shapes.update({'CrystalBall': cbshape}) #Voigt voigtmean = RooRealVar('#mu', 'mass mean value', meanstart, 0, 2 * meanstart) voigtwidth = RooRealVar('#gamma', 'width of voigt', 0, 5000) voigtsigma = RooRealVar('#sigma', 'mass resolution', sigmastart, 0, 2 * sigmastart) voigt = RooVoigtian('voigt', 'voigt', mjj, voigtmean, voigtwidth, voigtsigma) shapes.update({'Voigt': voigt}) #BreitWigner bwmean = RooRealVar('#mu', 'mass mean value', meanstart, 0, 2 * meanstart) bwwidth = RooRealVar('#sigma', 'width of bw', sigmastart, 0, 2 * sigmastart) bw = RooBreitWigner('bw', 'bw', mjj, bwmean, bwwidth) shapes.update({'BreitWigner': bw}) #Landau landaumean = RooRealVar('#mu_{landau}', 'mean landau', meanstart, 0, 2 * meanstart) landausigma = RooRealVar('#sigma_{landau}', 'mass resolution', sigmastart, 0, 2 * sigmastart) landau = RooLandau('landau', 'landau', mjj, landaumean, landausigma) shapes.update({'Landau': landau}) #LandauGauss Convolution landaugauss = RooFFTConvPdf('landaugauss', 'landau x gauss', mjj, landau, gauss) shapes.update({'LandauGauss': landaugauss}) #Logistics # logisticsmean=RooRealVar('#mu_{logistics}','mean logistics',meanstart,0,2*meanstart) # logisticssigma= RooRealVar('#sigma_{logistics}','mass resolution',sigmastart,0,2*sigmastart) # logistics=RooLogistics('logistics','logistics',mjj,logisticsmean,logisticssigma) # shapes.update({'Logistics':logistics}) #ExpAndGauss # expgaussmean=RooRealVar('#mu_{expgauss}','mean expgauss',meanstart,0,2*meanstart) # expgausssigma= RooRealVar('#sigma_{expgauss}','mass resolution',sigmastart,0,2*sigmastart) # expgausstrans= RooRealVar('trans','trans',0,100) # expgauss=RooExpAndGauss('expgauss','expgauss',mjj,expgaussmean,expgausssigma,expgausstrans) # shapes.update({'ExpAndGauss':expgauss}) #BifurGauss BifurGaussmean = RooRealVar('#mu_{BifurGauss}', 'mean BifurGauss', meanstart, 0, 2 * meanstart) BifurGausslsigma = RooRealVar('#sigma_{left}', 'mass resolution', sigmastart, 0, 2 * sigmastart) BifurGaussrsigma = RooRealVar('#sigma_{right}', 'mass resolution', sigmastart, 0, 2 * sigmastart) BifurGauss = RooBifurGauss('BifurGauss', 'BifurGauss', mjj, BifurGaussmean, BifurGausslsigma, BifurGaussrsigma) shapes.update({'BifurGauss': BifurGauss}) #Chebychev Chebychev1 = RooRealVar('c0', 'Chebychev0', -1000, 1000) Chebychev2 = RooRealVar('c1', 'Chebychev1', -1000, 1000) Chebychev3 = RooRealVar('c2', 'Chebychev2', 2, -1000, 1000) Chebychev = RooChebychev('Chebychev', 'Chebychev', mjj, RooArgList(Chebychev1, Chebychev2, Chebychev3)) shapes.update({'Chebychev': Chebychev}) #Polynomial Polynomial1 = RooRealVar('Polynomial1', 'Polynomial1', 100, 0, 1000) Polynomial2 = RooRealVar('Polynomial2', 'Polynomial2', 100, 0, 1000) Polynomial = RooPolynomial('Polynomial', 'Polynomial', mjj, RooArgList(Polynomial1, Polynomial2)) shapes.update({'Polynomial': Polynomial}) # mjj.setRange("FitRange",1050.,14000.) # for fname in ['Gauss','Logistics','BifurGauss']: for fname in ['BifurGauss']: plottitle = '%s Fit of %s' % (fname, title) shape = shapes[fname] # shape.fitTo(dh,RooFit.Range("FitRange"),RooFit.SumW2Error(True)) shape.fitTo(dh, RooFit.SumW2Error(True)) frame = mjj.frame(RooFit.Title(plottitle)) frame.GetYaxis().SetTitleOffset(2) dh.plotOn(frame, RooFit.MarkerStyle(4)) shape.plotOn(frame, RooFit.LineColor(2)) ndof = dh.numEntries() - 3 #chiSquare legend chi2 = frame.chiSquare() probChi2 = TMath.Prob(chi2 * ndof, ndof) chi2 = round(chi2, 2) probChi2 = round(probChi2, 2) leg = TLegend(0.5, 0.5, 0.9, 0.65) leg.SetBorderSize(0) leg.SetFillStyle(0) shape.paramOn(frame, RooFit.Layout(0.5, 0.9, 0.9)) leg.AddEntry(0, '#chi^{2} =' + str(chi2), '') leg.AddEntry(0, 'Prob #chi^{2} = ' + str(probChi2), '') leg.SetTextSize(0.04) frame.addObject(leg) canv = TCanvas(plottitle, plottitle, 700, 700) canv.SetLogy() canv.SetLeftMargin(0.20) canv.cd() frame.SetMinimum(10**(-3)) frame.Draw() canv.Print(path + '/%s__%s.eps' % (title, fname)) return chi2
def voigtian(mean, width_, sigma_, tagged_mass, w): sigmaV = RooRealVar ("#sigma_{V}" , "sigmaV" , sigma_ , 0, 10 ) widthV = RooRealVar ("widthV" , "widthV" , width_ , 0, 5 ) vgshape = RooVoigtian ("vgshape" , "vgshape" , tagged_mass, mean, widthV, sigmaV) _import(w,vgshape)
model.SetParameter(0, hist.Integral()) model.SetParameter(1, 0.0) model.SetParameter(2, 0.01) hist.Fit(model) """ x = RooRealVar("x", "x", -1000.0, 1000.0) roodata = RooDataHist("roodata", "equity dataset", RooArgList(x), hist) frame = x.frame() mass = RooRealVar("mass", "mass", 0.0, -10.0, 10.0) width = RooRealVar("width", "width", 0.01, -10.0, 10.0) s = RooRealVar("s", "s", 0.005, -10.0, 10.0) #model = RooGaussian("gaus", "gaus", x, mass, width) #model = RooBreitWigner("bw", "bw", x, mass, width) model = RooVoigtian("voigt", "voigt", x, mass, width, s) fit_result = model.fitTo(roodata, RooFit.Save()) roodata.plotOn(frame) #roodata.statOn(frame) model.plotOn(frame) model.paramOn(frame, roodata) frame.Draw() chisq = frame.chiSquare() ndf = n_bins print "------> Fit Quality:", TMath.Prob(chisq, ndf) canv.Update() canv.SaveAs("%s.png" % name[i])
c.SaveAs("testmass.png") # In[13]: phimean = 1.019 sigma = RooRealVar("sigma", "width of gaussian", 0.0013) gamma = RooRealVar("gamma", "gamma of bw", 0.004253, 0.001, 0.01) mean = RooRealVar("mean", "mean of gaussian", phimean, phimean - 0.005, phimean + 0.005) nSig = RooRealVar("nSig", "nSig", 1E6, 0., 5.0E6) nBkg = RooRealVar("nBkg", "nBkg", 5E5, 0., 5.0E6) #cheb = RooChebychev("cheb","Background",masskk,aset) #gauss = RooGaussian("gauss","gaussian PDF ",mass,mean,sigma) signal = RooVoigtian("signal", "signal", masskk, mean, gamma, sigma) B_1 = RooRealVar("B_1", "B_1 ", 0.3, -20, 100) B_2 = RooRealVar("B_2", "B_2", 0.3, -20, 100) B_3 = RooRealVar("B_3", "B_3", 0.3, -20, 100) B_4 = RooRealVar("B_4", "B_4", 0.3, -20, 100) bkg = RooBernstein("pdfB", "pdfB", masskk, RooArgList(B_1, B_2, B_3, B_4)) tot = RooAddPdf("tot", "g+cheb", RooArgList(signal, bkg), RooArgList(nSig, nBkg)) mean.setVal(phimean) gamma.setConstant(ROOT.kTRUE) mean.setConstant(ROOT.kTRUE)
7 a_{3} -2.42999e-02 6.27522e-04 3.86717e-04 -4.86190e-02 8 n_{Bkg} 1.87466e+07 1.36843e+04 3.48814e-04 8.52813e-01 9 n_{Sig} 1.92959e+06 1.30579e+04 3.90748e-04 3.83492e+00 ''' sigma_1 = RooRealVar("#sigma_{1}","width of gaussian",1.6002e-03,0.001,0.004) sigma_2 = RooRealVar("#sigma_{2}","width of gaussian",1.6002e-03,0.001,0.004) rFrac = RooRealVar("f_{1}","f1",0.5,0.0,1.0) gamma = RooRealVar("#Gamma","gamma of bw",4.41369e-03,0.004,0.005) mean = RooRealVar("m","mean of gaussian",1.01959e+00,phimean-0.005,phimean+0.005); nSig = RooRealVar("n_{Sig}","nSig",float(nentries)*0.01,0.,float(nentries)*0.5) nBkg = RooRealVar("n_{Bkg}","nBkg",float(nentries)*0.9,0.,float(nentries)) cheb = RooChebychev("cheb","Background",masskk,aset) res = RooGaussian("gauss","gaussian PDF ",masskk,mean,sigma_1) peak = RooVoigtian("peak","peak",masskk,mean,gamma,sigma_2) signal = RooAddPdf("signal","signal",res,peak,rFrac) B_1 = RooRealVar ( "B_{1}" , "B_1 " , 0.3 , -20 , 100 ) B_2 = RooRealVar ( "B_{2}" , "B_2" , 0.3 , -20 , 100 ) B_3 = RooRealVar ( "B_{3}" , "B_3" , 0.3 , -20 , 100 ) B_4 = RooRealVar ( "B_{4}" , "B_4" , 0.3 , -20 , 100 ) bkg = RooChebychev("pdfB" , "pdfB" , masskk , RooArgList(aset)) tot = RooAddPdf("tot","g+cheb",RooArgList(signal,bkg),RooArgList(nSig,nBkg)) nfits = 0 mean.setConstant(True) gamma.setConstant(True)
def fitChicSpectrum(dataset, binname): """ Fit chic spectrum""" x = RooRealVar('s', 's', -2, 2) x.setBins(200) #signal model q_chi1 = RooRealVar('qchi1', 'q_{#chi 1}', 0.414, 0.2, 0.6) q_chi2 = RooRealVar('qchi2', 'q_{#chi 2}', 0.430, 0.2, 0.6) delta_chi10 = RooRealVar('delta_chi10', 'delta_chi10', 0.09591) q_chi0 = RooFormulaVar('q_chi0', '@0 - @1', RooArgList(q_chi1, delta_chi10)) alphacb_chi1 = RooRealVar('alphacb_chi1', '#alpha^{CB}_{#chi 1}', 0.6, 0, 2) alphacb_chi2 = RooRealVar('alphacb_chi2', '#alpha^{CB}_{#chi 2}', 0.4, 0, 2) sigmacb_chi1 = RooRealVar('sigmacb_chi1', '#sigma^{CB}_{#chi 1}', 0.005, 0, 1) sigmacb_chi2 = RooRealVar('sigmacb_chi2', '#sigma^{CB}_{#chi 2}', 0.005, 0, 1) n_cb = RooRealVar('ncb', 'n^{CB}', 3.0, 0., 5.) gamma_chi0 = RooRealVar('gamma_chi0', 'gamma_chi0', 0.0104) sigmacb_chi0 = RooRealVar('sigmacb_chi0', '#sigma^{CB}_{#chi 0}', 0.005) chi0_sig = RooVoigtian('chi0sig', 'chi0sig,', x, q_chi0, sigmacb_chi0, gamma_chi0) chi1_sig = RooCBShape('chi1sig', 'chi1sig', x, q_chi1, sigmacb_chi1, alphacb_chi1, n_cb) chi2_sig = RooCBShape('chi2sig', 'chi2sig', x, q_chi2, sigmacb_chi2, alphacb_chi2, n_cb) fchi0 = RooRealVar('fchi0', 'f_{#chi 0}', 0.01, 0, 1) fchi1 = RooRealVar('fchi1', 'f_{#chi 1}', 0.5, 0, 1) fchi2 = RooFormulaVar('fchi2', '1-@0-@1', RooArgList(fchi0, fchi1)) fbck = RooRealVar('fbck', 'f_{bck}', 0.2, 0, 1) sigmodel = RooAddPdf('sigm', 'sigm', RooArgList(chi0_sig, chi1_sig, chi2_sig), RooArgList(fchi0, fchi1, fchi2)) #background model q0Start = 0.0 a_bck = RooRealVar('a_bck', 'a_{bck}', 0.5, -5, 5) b_bck = RooRealVar('b_bck', 'b_{bck}', -2.5, -7., 0.) q0 = RooRealVar('q0', 'q0', q0Start) delta = RooFormulaVar('delta', 'TMath::Abs(@0-@1)', RooArgList(x, q0)) bfun = RooFormulaVar('bfun', '@0*(@1-@2)', RooArgList(b_bck, x, q0)) signum = RooFormulaVar('signum', '( TMath::Sign( -1.,@0-@1 )+1 )/2.', RooArgList(x, q0)) background = RooGenericPdf('background', 'Background', 'signum*pow(delta,a_bck)*exp(bfun)', RooArgList(signum, delta, a_bck, bfun)) modelPdf = RooAddPdf('chicmodel', 'chicmodel', RooArgList(sigmodel, background), RooArgList(fbck)) frame = x.frame(RooFit.Title('Q')) range = x.setRange('range', 0, 2) # result = modelPdf.fitTo(dataset,RooFit.Save(),RooFit.Range('range')) dataset.plotOn(frame, RooFit.MarkerSize(0.7)) modelPdf.plotOn(frame, RooFit.LineWidth(2)) #plotting canvas = TCanvas('fit', "", 1400, 700) canvas.Divide(1) canvas.cd(1) gPad.SetRightMargin(0.3) gPad.SetFillColor(10) modelPdf.paramOn(frame, RooFit.Layout(0.725, 0.9875, 0.9)) frame.Draw() canvas.SaveAs('out-' + binname + '.png') canvas.SaveAs('out-' + binname + '.root')
def RooFitHist(inputhist,title='title',path='.'): # RooFit.gErrorIgnoreLevel = RooFit.kInfo # <6.02 # RooFit.SetSilentMode()# >6.02 # RooMsgService().instance().SetSilentMode(kTRUE) fitbinning=array('d') binwidth=200 #NBins=(14000/binwidth) - ( (1040/binwidth) + 1 ) Mjj NBins=(8000/binwidth) - ( (200/binwidth)+1 )#pT for i in range(NBins+1): fitbinning.append(210+i*binwidth)# Mjj 1050 # print(fitbinning) #import numpy as np #fitbinning=np.linspace(0,8000,81) hist=inputhist.Rebin(NBins,"fit parameter",fitbinning) #hist=inputhist.Rebin(80,"fit parameter", fitbinning) #meanstart=hist.GetBinCenter(2000) meanstart=hist.GetBinCenter(hist.GetMaximumBin())#maximum sigmastart=hist.GetRMS() #sigmastart=3000 print('meanstart:',meanstart,'sigmastart:',sigmastart) # inputhist.Draw() # hist.Draw() # hold=raw_input('press enter to exit.') gStyle.SetOptFit(1111) gStyle.SetOptTitle(0) RooFit.SumW2Error(kTRUE) RooFit.Extended(kTRUE) # RooDataHist::adjustBinning(dh): fit range of variable mjj expanded to nearest bin boundaries: [1050,13850] --> [1050,13850] mjj=RooRealVar('mjj','P_{T-AK8}',fitbinning[0],fitbinning[len(fitbinning)-1],'GeV') mjjral=RooArgList(mjj) dh=RooDataHist('dh','dh',mjjral,RooFit.Import(hist)) #shape.fitTo(dh,RooFit.Range("FitRange"),RooFit.Extended(True),RooFit.SumW2Error(False)) shapes={} #Gaussian not really # 3rd, 4th and 5th arguments are: (starting value, minimum possible value, maximum possible value) -- not goot gaussmean = RooRealVar('#mu_{gauss}','mass mean value',meanstart,0,2*meanstart) gausssigma= RooRealVar('#sigma_{gauss}','mass resolution',sigmastart,0,2*meanstart) gauss=RooGaussian('gauss','gauss',mjj,gaussmean,gausssigma) shapes.update({'Gauss':gauss}) #poisson poissonmean = RooRealVar('#mu_{poisson}', 'pT mean value', meanstart,0,2*meanstart) poissonsigma = RooRealVar('#sigma_{poisson]', 'pT resolution', sigmastart, 0, 2*sigmastart) poisson = RooPoisson('poisson', 'poisson', mjj, poissonmean, False) shapes.update({'Poisson': poisson}) #Landau -- not good landaumean=RooRealVar('#mu_{landau}','mean landau',meanstart,0,2*meanstart) landausigma= RooRealVar('#sigma_{landau}','mass resolution',sigmastart,0,2*sigmastart)#bzw8 landau=RooLandau('landau','landau',mjj,landaumean,landausigma) shapes.update({'Landau':landau}) #CrystalBall -> this is close to be good but matrix error :( mean = RooRealVar('#mu','mean',meanstart,0,2*meanstart) sigma= RooRealVar('#sigma','sigma',sigmastart,0,2*sigmastart) alpha=RooRealVar('#alpha','Gaussian tail',-1000,0) n=RooRealVar('n','Normalization',-1000,1000) cbshape=RooCBShape('cbshape','crystalball PDF',mjj,mean,sigma,alpha,n) shapes.update({'CrystalBall':cbshape}) #Voigt ---pff voigtmean = RooRealVar('#mu','mass mean value',meanstart,0,2*meanstart) voigtwidth = RooRealVar('#gamma','width of voigt',0,100) voigtsigma= RooRealVar('#sigma','mass resolution',sigmastart,0,150) voigt=RooVoigtian('voigt','voigt',mjj,voigtmean,voigtwidth,voigtsigma) shapes.update({'Voigt':voigt}) #BreitWigner--worst bwmean = RooRealVar('#mu','mass mean value',meanstart,0,2*meanstart) bwwidth = RooRealVar('#sigma','width of bw',sigmastart,100, 150) bw=RooBreitWigner('bw','bw',mjj,bwmean,bwwidth) shapes.update({'BreitWigner':bw}) #Logistics #logisticsmean=RooRealVar('#mu_{logistics}','mean logistics',meanstart,0,2*meanstart) #logisticssigma= RooRealVar('#sigma_{logistics}','mass resolution',sigmastart,0,2*sigmastart) #logistics=RooLogistics('logistics','logistics',mjj,logisticsmean,logisticssigma) #shapes.update({'Logistics':logistics}) #ExpAndGauss expgaussmean=RooRealVar('#mu_{expgauss}','mean expgauss',meanstart,490,900) expgausssigma= RooRealVar('#sigma_{expgauss}','mass resolution',sigmastart,20,3*sigmastart) expgausstrans= RooRealVar('trans','trans',0,1000) ExpAndGauss=RooExpAndGauss('expgauss','expgauss',mjj,expgaussmean,expgausssigma,expgausstrans) shapes.update({'ExpAndGauss':ExpAndGauss}) #Exp #expmean=RooRealVar('') #BifurGauss -bad BifurGaussmean=RooRealVar('#mu_{BifurGauss}','mean BifurGauss',meanstart,0,2*meanstart) BifurGausslsigma= RooRealVar('#sigma_{left}','mass resolution',sigmastart,200,2*sigmastart)#2*sigmastart BifurGaussrsigma= RooRealVar('#sigma_{right}','mass resolution',sigmastart,200,2*sigmastart) BifurGauss=RooBifurGauss('BifurGauss','BifurGauss',mjj,BifurGaussmean,BifurGausslsigma,BifurGaussrsigma) shapes.update({'BifurGauss':BifurGauss}) #Chebychev -nope Chebychev1=RooRealVar('c0','Chebychev0',-1000,1000) Chebychev2= RooRealVar('c1','Chebychev1',-1000,1000) Chebychev3= RooRealVar('c2','Chebychev2',2,-1000,1000) Chebychev=RooChebychev('Chebychev','Chebychev',mjj,RooArgList(Chebychev1,Chebychev2,Chebychev3)) shapes.update({'Chebychev':Chebychev}) #Polynomial -nope Polynomial1=RooRealVar('Polynomial1','Polynomial1',100,0,1000) Polynomial2= RooRealVar('Polynomial2','Polynomial2',100,0,1000) Polynomial=RooPolynomial('Polynomial','Polynomial',mjj,RooArgList(Polynomial1,Polynomial2)) shapes.update({'Polynomial':Polynomial}) #pareto #___________________________________________________________We will see #Convolutions #-> use bin > 1000 for better accuracy -----cyclic trouble #-> Convolution depends on order!!! RooFFTConvPdf the first p.d.f. is the theory model and that the second p.d.f. is the resolution model # #LandauGauss Convolution - really bad landaugauss=RooFFTConvPdf('landaugauss','landau x gauss',mjj,landau, gauss) #landaugauss.setBufferFraction(126) shapes.update({'LandauGauss':landaugauss}) #GaussLandau Convolution -> Better but NOT posdef. ...but for 100 it is gausslandau=RooFFTConvPdf('gausslandau','gauss x landau ',mjj,gauss,landau) #gausslandau.setBufferFraction(126) shapes.update({'GaussLandau':gausslandau}) #CrystalBallLandau Convolution cbshape x landau looks better -> status failed crystlandau=RooFFTConvPdf('crystallandau','cbshape x landau', mjj, landau, cbshape) crystlandau.setBufferFraction(39) shapes.update({'CrystLandau': crystlandau}) #BifurGaussLandau Convolution -> Better in look, NO matrix error for Binwidth 200 BifurGaussLandau=RooFFTConvPdf('bifurgausslandau','landau x bifurgauss',mjj,landau, BifurGauss) BifurGaussLandau.setBufferFraction(39) #against over cycling shapes.update({'BifurGaussLandau':BifurGaussLandau}) #CrystalGauss Convolution looks better -> status failed crystgauss=RooFFTConvPdf('crystalgauss','cbshape x gauss', mjj, cbshape, gauss) #crystgauss.setBufferFraction(39) shapes.update({'CrystGauss': crystgauss}) #BreitWignerLandau Convolution (Breitwigner = Lorentz)-> status OK.... BreitWignerLandau=RooFFTConvPdf('breitwignerlandau','breitwigner x landau',mjj,landau,bw,3) #BreitWignerLandau.setBufferFraction(48) #setBufferFraction(fraction of the sampling array size) ->cyclic behaviour fix #crystgauss.setShift(0,0) #s1 and s2 are the amounts by which the sampling ranges for pdf's are shifted respectively #(0,-(xmin+xmax)/2) replicates the default behavior #(0,0) disables the shifting feature altogether shapes.update({'BreitWignerLandau':BreitWignerLandau}) for fname in ['ExpAndGauss']: plottitle='%s Fit of %s'%(fname,title) shape=shapes[fname] # shape.fitTo(dh,RooFit.Range("FitRange"),RooFit.SumW2Error(True)) #shape.fitTo(dh,RooFit.Extended(True),RooFit.SumW2Error(True)) mjj.setRange("FitRange",500,4000)#tried shape.fitTo(dh,RooFit.Range("FitRange"),RooFit.Extended(False),RooFit.SumW2Error(True)) frame=mjj.frame(RooFit.Title(plottitle)) #frame.GetYaxis().SetTitleOffset(2) dh.plotOn(frame,RooFit.MarkerStyle(4)) shape.plotOn(frame,RooFit.LineColor(2)) ndof=dh.numEntries()-3 print ('ndof', ndof) #chiSquare legend chi2 = frame.chiSquare()#there are 2 chiSquare. the 2cond returns chi2/ndf /// \return \f$ \chi^2 / \mathrm{ndf} \f$ print ('chi2', chi2) probChi2 = TMath.Prob(chi2*ndof, ndof)# why chi2*ndof ?! makes no sense to me #Double_t Prob(Double_t chi2, Int_t ndf) #Computation of the probability for a certain Chi-squared (chi2) #and number of degrees of freedom (ndf). #P(a,x) represents the probability that the observed Chi-squared #for a correct model should be less than the value chi2. #The returned probability corresponds to 1-P(a,x), !!!! #which denotes the probability that an observed Chi-squared exceeds #the value chi2 by chance, even for a correct model. #--- NvE 14-nov-1998 UU-SAP Utrecht #probChi2=TMath.Prob(chi2, ndof) chi2 = round(chi2,2) #probChi2 = round(probChi2,2) leg = TLegend(0.5,0.5,0.5,0.65)#0.9 leg.SetBorderSize(0) leg.SetFillStyle(0) shape.paramOn(frame, RooFit.Layout(0.5,0.9,0.9)) leg.AddEntry(0,'#chi^{2} ='+str(chi2),'') leg.AddEntry(0,'Prob #chi^{2} = '+str(probChi2),'') leg.SetTextSize(0.04) frame.addObject(leg) canv=TCanvas(plottitle,plottitle,700,700) canv.SetLogy() canv.SetLeftMargin(0.20) canv.cd() frame.SetMaximum(10**(1)) frame.SetMinimum(10**(-11))#from -3 -> -6 frame.Draw() #canv.Print(path+'/%s__%sS2MoreLessY.pdf'%(title,fname)) raw_input('press enter to continue') return chi2
def buildPdf(ws, p): mass = RooRealVar("mass", "mass", p.minMass, p.maxMass) # ws.import(mass) getattr(ws,'import')(mass) # Construct signal pdf mean = RooRealVar("mean", "mean", 60, 0, 130) width = RooRealVar("width", "width", 10, 1, 40) # alpha = RooRealVar("alpha", "#alpha", -0.1, -10.0, -0.1) # npow = RooRealVar("npow", "n_{CB}", 2.3, 0.1, 10.) # width.setConstant(ROOT.kTRUE) sigma = RooRealVar("sigma", "sigma", 1.2, 0.2, 40) # minusMass = RooFormulaVar("minusMass", "@0*-1", RooArgList(mass)) # signalAll = RooCBShape("signalAll", "signalAll", mass, mean, width, alpha, npow); signalAll = RooVoigtian("signalAll", "signalAll", mass, mean, width, sigma) # Construct background pdf # a0_all = RooRealVar("a0_all","a0_all",-0.1,-1,1) # a1_all = RooRealVar("a1_all","a1_all",0.004,-1,1) # backgroundAll = RooChebychev("backgroundAll","backgroundAll",mass,RooArgList(a0_all,a1_all)) turnOnAll = RooRealVar("turnOnAll","turnOnAll", 80., 40., 150.) widthAll_bkg = RooRealVar("widthAll","widthAll", 2., 0., 50.) decayAll_bkg = RooRealVar("decayAll","decayAll", 80., 20., 150.) meanB = RooRealVar("meanB", "meanB", 90, 60, 130) sigmaB = RooRealVar("sigmaB", "sigmaB", 10, 1, 20) bkg_a1 = RooRealVar("bkg_a1", "bkg_a1", 0., -2., 2.) bkg_a2 = RooRealVar("bkg_a2", "bkg_a2", 0., -2., 2.) #backgroundAll = RooChebychev("backgroundAll", "backgroundAll", mass, RooArgList(bkg_a1, bkg_a2)) #backgroundAll = RooGenericPdf("backgroundAll","backgroundAll", "exp(-@0/@3)*(TMath::Erf((@0-@1)/@2)+1)", RooArgList(mass, turnOnAll, widthAll_bkg, decayAll_bkg)) backgroundAll = RooGenericPdf("backgroundAll","backgroundAll","exp(-@0/@1)",RooArgList(mass, decayAll_bkg)) #backgroundAll = RooGaussian("backgroundAll", "backgroundAll", mass, meanB, sigmaB) # Construct composite pdf sigAll = RooRealVar("sigAll", "sigAll", 2000, 0, 10000000) bkgAll = RooRealVar("bkgAll", "bkgAll", 100, 0, 1000000) modelAll = RooAddPdf("modelAll", "modelAll", RooArgList(signalAll, backgroundAll), RooArgList(sigAll, bkgAll)) if p.NoBkgd: modelAll = RooAddPdf("modelAll", "modelAll", RooArgList(signalAll), RooArgList(sigAll)) # Define pdf for all probes # Construct signal pdf. # NOTE that sigma is shared with the signal sample model signalPass = RooVoigtian("signalPass","signalPass",mass,mean,width,sigma) # signalPass = RooCBShape("signalPass", "signalPass", mass, mean, width, alpha, npow); # Construct the background pdf # a0_pass = RooRealVar("a0_pass","a0_pass",-0.1,-1,1) # a1_pass = RooRealVar("a1_pass","a1_pass",0.5,-0.1,1) # backgroundPass = RooChebychev("backgroundPass","backgroundPass",mass,RooArgList(a0_pass,a1_pass)) # turnOnPass = RooRealVar("turnOnPass","turnOnPass", 80., 50., 150.) # widthPass_bkg = RooRealVar("widthPass","widthPass", 2., 0., 50.) decayPass_bkg = RooRealVar("decayPass","decayPass", 80., 20., 150.) #backgroundPass = RooChebychev("backgroundPass", "backgroundPass", mass, RooArgList(bkg_a1, bkg_a2)) #backgroundPass = RooGenericPdf("backgroundPass","backgroundPass", "exp(-@0/@3)*(TMath::Erf((@0-@1)/@2)+1)", RooArgList(mass, turnOnPass, widthPass_bkg, decayPass_bkg)); #backgroundPass = RooGenericPdf("backgroundPass","backgroundPass", "exp(-@0/@3)*(TMath::Erf((@0-@1)/@2)+1)", RooArgList(mass, turnOnAll, widthAll_bkg, decayAll_bkg)); backgroundPass = RooGenericPdf("backgroundPass","backgroundPass","exp(-@0/@1)",RooArgList(mass, decayPass_bkg)) #backgroundPass = RooGaussian("backgroundPass", "backgroundPass", mass, meanB, sigmaB) # Construct the composite model efficiency = RooRealVar("efficiency","efficiency",0.9,0.1,1.) # Use it only analyzing the data prescaling value set to be 20 sigPass = RooFormulaVar("sigPass", "@0*@1*"+str(p.scaleFactor), RooArgList(sigAll, efficiency)) bkgPass = RooRealVar("bkgPass", "bkgPass", 100, 0, 1000000) # bkgPass = RooFormulaVar("bkgPass", "@0*@1", RooArgList(bkgAll, efficiency)) modelPass = RooAddPdf("modelPass", "modelPass", RooArgList(signalPass, backgroundPass), RooArgList(sigPass, bkgPass)) if p.NoBkgd: modelPass = RooAddPdf("modelPass", "modelPass", RooArgList(signalPass), RooArgList(sigPass)) frac = RooRealVar("frac", "frac", 0.8, 0., 1.) # Define combined pdf for simultaneous fit # Define category to distinguish physics and control samples events sample = RooCategory("sample","sample") sample.defineType("all") sample.defineType("pass") simPdf = RooSimultaneous("simPdf","simultaneous pdf",sample) # Associate model with the physics state and model_ctl with the control state simPdf.addPdf(modelAll,"all") simPdf.addPdf(modelPass,"pass") # ws.import(simPdf) getattr(ws,'import')(simPdf)
# B_5 = RooRealVar ( "B_5" , "B_5" , 0.3 , -20 , 100 ) # B_6 = RooRealVar ( "B_6" , "B_6" , 0.3 , -20 , 100 ) a0 = RooRealVar("p0", "p0", 0.001, -2., 2.) a1 = RooRealVar("p1", "p1", 0.001, -2., 2.) a2 = RooRealVar("p2", "p2", -0.00001, -2., 2.) a3 = RooRealVar("p3", "p3", -0.000001, -2, 2.) a4 = RooRealVar("p4", "p4", -0.000001, -2., 2.) # a5 = RooRealVar("a5","a5",-0.000001,-2.,2.) poliset = RooArgList(a0, a1, a2, a3, a4) # gaussFrac = RooRealVar("s","fraction of component 1 in kkSig",0.3,0.0,1.0) nSigKK = RooRealVar("nSig", "nSig", 1E5, 0., 10E6) nBkgKK = RooRealVar("nBkg", "nBkg", 5E5, 0., 10E6) kkSig = RooVoigtian("kkSig", "kkSig", tt_mass, kkMean, kkGamma, kkSigma) #kkBkg = RooBernstein("kkBkg" , "kkBkg", tt_mass, RooArgList(B_1, B_2,B_3,B_4))#,B_5) )#,B_6)) kkBkg = RooChebychev("kkBkg", "Background", tt_mass, poliset) kkTot = RooAddPdf("kkTot", "kkTot", RooArgList(kkSig, kkBkg), RooArgList(nSigKK, nBkgKK)) kkGamma.setConstant(ROOT.kTRUE) kkMean.setConstant(ROOT.kTRUE) nfit = 0 # kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005),RooFit.PrintLevel(-1), RooFit.NumCPU(7),RooFit.Save()) # nfit +=1 if not debugging:
def buildPdf(ws, p): mass = RooRealVar("mass", "mass", p.minMass, p.maxMass) getattr(ws, 'import')(mass) # Construct signal pdf mean = RooRealVar("mean", "mean", 90, 85, 95) width = RooRealVar("width", "width", 2.4952, 1, 3) width.setConstant(ROOT.kTRUE) sigma = RooRealVar("sigma", "sigma", 1.2, 0.2, 10) signalAll = RooVoigtian("signalAll", "signalAll", mass, mean, width, sigma) turnOnAll = RooRealVar("turnOnAll", "turnOnAll", 80., 40., 150.) widthAll_bkg = RooRealVar("widthAll", "widthAll", 2., 0., 50.) decayAll_bkg = RooRealVar("decayAll", "decayAll", 80., 20., 150.) meanB = RooRealVar("meanB", "meanB", 90, 60, 130) sigmaB = RooRealVar("sigmaB", "sigmaB", 10, 1, 20) bkg_a1 = RooRealVar("bkg_a1", "bkg_a1", 0., -2., 2.) bkg_a2 = RooRealVar("bkg_a2", "bkg_a2", 0., -2., 2.) backgroundAll = RooGaussian("backgroundAll", "backgroundAll", mass, meanB, sigmaB) # Construct composite pdf sigAll = RooRealVar("sigAll", "sigAll", 2000, 0, 100000) bkgAll = RooRealVar("bkgAll", "bkgAll", 100, 0, 10000) modelAll = RooAddPdf("modelAll", "modelAll", RooArgList(signalAll, backgroundAll), RooArgList(sigAll, bkgAll)) if p.NoBkgd: modelAll = RooAddPdf("modelAll", "modelAll", RooArgList(signalAll), RooArgList(sigAll)) # Define pdf for all probes # Construct signal pdf. # NOTE that sigma is shared with the signal sample model signalPass = RooVoigtian("signalPass", "signalPass", mass, mean, width, sigma) # Construct the background pdf backgroundPass = RooGaussian("backgroundPass", "backgroundPass", mass, meanB, sigmaB) # Construct the composite model efficiency = RooRealVar("efficiency", "efficiency", 0.9, 0.3, 1.) sigPass = RooFormulaVar("sigPass", "@0*@1", RooArgList(sigAll, efficiency)) bkgPass = RooRealVar("bkgPass", "bkgPass", 100, 0, 10000) modelPass = RooAddPdf("modelPass", "modelPass", RooArgList(signalPass, backgroundPass), RooArgList(sigPass, bkgPass)) if p.NoBkgd: modelPass = RooAddPdf("modelPass", "modelPass", RooArgList(signalPass), RooArgList(sigPass)) frac = RooRealVar("frac", "frac", 0.8, 0., 1.) # Define combined pdf for simultaneous fit # Define category to distinguish physics and control samples events sample = RooCategory("sample", "sample") sample.defineType("all") sample.defineType("pass") simPdf = RooSimultaneous("simPdf", "simultaneous pdf", sample) # Associate model with the physics state and model_ctl with the control state simPdf.addPdf(modelAll, "all") simPdf.addPdf(modelPass, "pass") # ws.import(simPdf) getattr(ws, 'import')(simPdf)