def testPhysical(): with gl.name_scope('massFit'): with gl.name_scope("expBkg"): yBkg = gl.Parameter(500, name = 'yieldExp', minVal = -1000) minParam = gl.Parameter(0.0, name = 'minVal', fixed = True) maxParam = gl.Parameter(10.0, name = 'maxVal', fixed = True) aExp = gl.Parameter(-0.2, name = 'aExp') # expBkg = gl.Exponential({'a' : aExp, 'min' : gl.Parameter(0.0, fixed = True), 'max' : gl.Parameter(10.0, fixed = True)}) expBkg = gl.Exponential({'a' : aExp, 'min' : minParam, 'max' : maxParam}) with gl.name_scope("gaussSignal"): ySig = gl.Parameter(500, name = 'yieldSig', minVal = -1000) m = gl.Parameter(5.0, name = 'mean') s = gl.Parameter(1.0, name = 'sigma') g = gl.Gaussian({'mean' : m, 'sigma' : s}) initialFitYields = {g.name : ySig, expBkg.name : yBkg} initialFitComponents = {g.name : g, expBkg.name : expBkg} model = gl.Model(initialFitYields = initialFitYields, initialFitComponents = initialFitComponents, minVal = 0., maxVal = 10.) data = model.sample(0, 10) # plt.hist(data, bins = 50, histtype = 'stepfilled') # plt.savefig('textExp.pdf') fitter = gl.Fitter(model, backend = 'minuit') res = fitter.fit(data, verbose = True)#, nIterations = 5000) # samples = res.chain[:, 500:, :].reshape((-1, model.getNFloatingParameters())) plotter = gl.Plotter(model, data) plotter.plotDataModel(nDataBins = 30) plt.savefig('testPlot.pdf') plt.clf()
def crossfeedModel(signalYield, crossfeedYield, cbYield): with gl.name_scope('pidToy'): minParam = gl.Parameter(4800., name = 'minVal', fixed = True) maxParam = gl.Parameter(6000., name = 'maxVal', fixed = True) with gl.name_scope('signal'): with gl.name_scope('g1'): m1 = gl.Parameter(5265., name = 'mean', fixed = True) s1 = gl.Parameter(67.86, name = 'sigma', fixed = True) signalGauss1 = gl.Gaussian({'mean' : m1, 'sigma' : s1}) with gl.name_scope('g2'): m2 = gl.Parameter(5257., name = 'mean', fixed = True) s2 = gl.Parameter(185.9, name = 'sigma', fixed = True) signalGauss2 = gl.Gaussian({'mean' : m2, 'sigma' : s2}) y1 = gl.Parameter(signalYield // 2, name = 'g1Yield', minVal = -10., maxVal = signalYield) y2 = gl.Parameter(signalYield // 2, name = 'g2Yield', minVal = -10., maxVal = signalYield) # # # Would be good to have these represented using fractions, i.e., y1/y2 # # fitYields = {signalGauss1.name : y1, signalGauss2.name : y2} # fitComponents = {signalGauss1.name : signalGauss1, signalGauss2.name : signalGauss2} # # # signalModel = gl.Model(initialFitYields = fitYields, initialFitComponents = fitComponents, minVal = 4800., maxVal = 6000.) f1 = gl.Parameter(0.5665, name = 'frac1', fixed = True) fitFracs = {signalGauss1.name : f1} fitComponents = {signalGauss1.name : signalGauss1, signalGauss2.name : signalGauss2} signalModel = gl.Model(initialFitFracs = fitFracs, initialFitComponents = fitComponents, minVal = 4800., maxVal = 6000.) with gl.name_scope('crossfeedKpipi'): with gl.name_scope('g1'): m1_cf = gl.Parameter(5021., name = 'mean', fixed = True) s1_cf = gl.Parameter(300., name = 'sigma', fixed = True) crossfeedKpipiGauss1 = gl.Gaussian({'mean' : m1_cf, 'sigma' : s1_cf}) with gl.name_scope('g2'): m2_cf = gl.Parameter(5174., name = 'mean', fixed = True) s2_cf = gl.Parameter(97.77, name = 'sigma', fixed = True) crossfeedKpipiGauss2 = gl.Gaussian({'mean' : m2_cf, 'sigma' : s2_cf}) y1 = gl.Parameter(crossfeedYield * 0.5, name = 'g1Yield', minVal = -10.) y2 = gl.Parameter(crossfeedYield * 0.5, name = 'g2Yield', minVal = -10.) # Would be good to have these represented using fractions, i.e., y1/y2 # fitYields = {crossfeedKpipiGauss1.name : y1, crossfeedKpipiGauss2.name : y2} # fitComponents = {crossfeedKpipiGauss1.name : crossfeedKpipiGauss1, crossfeedKpipiGauss2.name : crossfeedKpipiGauss2} # # cfModel = gl.Model(initialFitYields = fitYields, initialFitComponents = fitComponents, minVal = 4800., maxVal = 6000.) f1_cf = gl.Parameter(0.3373, name = 'frac1', fixed = True) fitFracs = {crossfeedKpipiGauss1.name : f1_cf} fitComponents = {crossfeedKpipiGauss1.name : crossfeedKpipiGauss1, crossfeedKpipiGauss2.name : crossfeedKpipiGauss2} cfModel = gl.Model(initialFitFracs = fitFracs, initialFitComponents = fitComponents, minVal = 4800., maxVal = 6000.) with gl.name_scope('combinatorial'): a = gl.Parameter(-0.003, name = 'aExp', fixed = True) # ?? combinatorial = gl.Exponential({'a' : a, 'min' : minParam, 'max' : maxParam}) # ySig1 = gl.Parameter(signalYield // 2, name = 'sigYield1', minVal = -10.) # ySig2 = gl.Parameter(signalYield // 2, name = 'sigYield2', minVal = -10.) # yCF1 = gl.Parameter(crossfeedYield // 2, name = 'cfYield1', minVal = -10.) # yCF2 = gl.Parameter(crossfeedYield // 2, name = 'cfYield2', minVal = -10.) # yBkg = gl.Parameter(cbYield, name = 'cbYield', minVal = -10.) # # # Have sub-models defined using fractions of components, then report yields for models in addition to yields for individual components? # # AND FIX NORMALISATION! # # fitYields = {signalGauss1.name : ySig1, signalGauss2.name : ySig2, crossfeedKpipiGauss1.name : yCF1, crossfeedKpipiGauss2.name : yCF2, combinatorial.name : yBkg} # fitComponents = {signalGauss1.name : signalGauss1, signalGauss2.name : signalGauss2, crossfeedKpipiGauss1.name : crossfeedKpipiGauss1, crossfeedKpipiGauss2.name : crossfeedKpipiGauss2, combinatorial.name : combinatorial} # # model = gl.Model(initialFitYields = fitYields, initialFitComponents = fitComponents, minVal = 4800., maxVal = 6000.) ySig1 = gl.Parameter(signalYield, name = 'sigYield', minVal = -10.) yCF1 = gl.Parameter(crossfeedYield, name = 'cfYield', minVal = -10.) yBkg = gl.Parameter(cbYield, name = 'cbYield', minVal = -10.) # Have sub-models defined using fractions of components, then report yields for models in addition to yields for individual components? # AND FIX NORMALISATION! fitYields = {signalModel.name : ySig1, cfModel.name : yCF1, combinatorial.name : yBkg} fitComponents = {signalModel.name : signalModel, cfModel.name : cfModel, combinatorial.name : combinatorial} model = gl.Model(initialFitYields = fitYields, initialFitComponents = fitComponents, minVal = 4800., maxVal = 6000.) return model
def testPhysicalSimBF(): with gl.name_scope('massFit'): minParam = gl.Parameter(0.0, name = 'minVal', fixed = True) maxParam = gl.Parameter(10.0, name = 'maxVal', fixed = True) m = gl.Parameter(5.0, name = 'mean') s = gl.Parameter(1.0, name = 'sigma') with gl.name_scope('model1'): with gl.name_scope("expBkg"): yBkg1 = gl.Parameter(500, name = 'yieldExp', minVal = -1000) aExp1 = gl.Parameter(-0.2, name = 'aExp') expBkg1 = gl.Exponential({'a' : aExp1, 'min' : minParam, 'max' : maxParam}) with gl.name_scope("gaussSignal"): ySig1 = gl.Parameter(500, name = 'yieldSig', minVal = -1000) g1 = gl.Gaussian({'mean' : m, 'sigma' : s}) with gl.name_scope('model2'): with gl.name_scope("expBkg"): yBkg2 = gl.Parameter(1500, name = 'yieldExp', minVal = -1000) aExp2 = gl.Parameter(-0.1, name = 'aExp') expBkg2 = gl.Exponential({'a' : aExp2, 'min' : minParam, 'max' : maxParam}) with gl.name_scope("gaussSignal"): ySig2 = gl.Parameter(1000, name = 'yieldSig', minVal = -1000) g2 = gl.Gaussian({'mean' : m, 'sigma' : s}) initialFitYields1 = {g1.name : ySig1, expBkg1.name : yBkg1} initialFitComponents1 = {g1.name : g1, expBkg1.name : expBkg1} initialFitYields2= {g2.name : ySig2, expBkg2.name : yBkg2} initialFitComponents2 = {g2.name : g2, expBkg2.name : expBkg2} model1 = gl.Model(initialFitYields = initialFitYields1, initialFitComponents = initialFitComponents1) model2 = gl.Model(initialFitYields = initialFitYields2, initialFitComponents = initialFitComponents2) data1 = model1.sample(0, 10) data2 = model2.sample(0, 10) model = gl.SimultaneousModel(initialFitComponents = [model1, model2]) # plt.hist(data, bins = 50, histtype = 'stepfilled') # plt.savefig('textExp.pdf') fitter = gl.Fitter(model, backend = 'emcee') res = fitter.fit([data1, data2], verbose = True, nIterations = 2000) samples = res.chain[:, 100:, :].reshape((-1, model.getNFloatingParameters())) plotter = gl.Plotter(model1, data1) plotter.plotDataModel(nDataBins = 50) plt.savefig('testPlot1.pdf') plt.clf() plotter = gl.Plotter(model2, data2) plotter.plotDataModel(nDataBins = 50) plt.savefig('testPlot2.pdf') plt.clf() c = corner.corner(samples)#, truths = [-0.2, 500., 5.0, 1.0, 500.]) c.savefig('corner.pdf') plt.clf()
# fitter = gl.Fitter(model, backend = 'minuit') # res = fitter.fit(data, verbose = True) # # plotter = gl.Plotter(model, data) # plotter.plotDataModel(nDataBins = 50) # plt.savefig('testData.pdf') # plt.clf() minParam = gl.Parameter(4800., name = 'minVal', fixed = True) maxParam = gl.Parameter(6000., name = 'maxVal', fixed = True) with gl.name_scope('combinatorial'): a = gl.Parameter(-0.003, name = 'aExp', fixed = True) # ?? combinatorial = gl.Exponential({'a' : a, 'min' : minParam, 'max' : maxParam}) yBkg = gl.Parameter(5000, name = 'cbYield', minVal = -10.) with gl.name_scope('signal'): with gl.name_scope('g1'): m1 = gl.Parameter(5265., name = 'mean', fixed = True) s1 = gl.Parameter(67.86, name = 'sigma', fixed = True) signalGauss1 = gl.Gaussian({'mean' : m1, 'sigma' : s1}) with gl.name_scope('g2'): m2 = gl.Parameter(5257., name = 'mean', fixed = True)