def get_value(self): fig = figure() xname = "invariantMass" xmin,xmax,xbins = -5.,15.,50 index = "run*" x,counts = vis_bokeh.get_1d_hist(xname,xmin,xmax,xbins,es,index=index) deltas = np.sqrt(counts) fig=vis_bokeh.whiskered_histogram(xmin,xmax,xbins,counts,deltas,-deltas) #fit params model = mix model.fit(sample_weight=counts)#,values_init={'sig_weightlog':np.log(0.4),'bck_weightlog':np.log(0.6)}) parameters = model.parameters w_sig,w_bkg =np.exp(parameters['sig_weightlog']),np.exp(parameters['bck_weightlog']) w_sum = w_sig+w_bkg w_sig,w_bkg = w_sig/w_sum,w_bkg/w_sum n_events = np.sum(counts) norm = n_events*(xmax-xmin)/xbins #plot lines expo = lambda x_arr:st.expon(0,1./parameters['slope']).pdf(x_arr)*w_bkg*norm gauss = lambda x_arr:st.norm(parameters['mean'],parameters['sigma']).pdf(x_arr)*w_sig*norm pdf_x = np.arange(1000,dtype='float')/1000.*(xmax-xmin) + xmin fig.line(pdf_x, expo(pdf_x), legend="Background", line_width=2,color = 'red') fig.line(pdf_x, gauss(pdf_x), legend="Signal", line_width=2,color='blue') fig.line(pdf_x, gauss(pdf_x)+expo(pdf_x), legend="Sum", line_width=2,color='green') fig.xaxis.axis_label = time.strftime("%H:%M:%S") return vis_bokeh.fig_to_html(fig)
def make_figure(self,hist,model,es,index="run*"): xname,xmin,xmax,xbins = hist.name,hist.xmin,hist.xmax,hist.xbins x,counts = vis_bokeh.get_1d_hist(xname,xmin,xmax,xbins,es,index=index) deltas = np.sqrt(counts) fig=vis_bokeh.whiskered_histogram(xmin,xmax,xbins,counts,deltas,-deltas) #fit params model.fit(sample_weight=counts,values_init={'sigma':1.}) parameters = model.parameters w_sig,w_bkg =np.exp(parameters['sig_weightlog']),np.exp(parameters['bck_weightlog']) w_sum = w_sig+w_bkg w_sig,w_bkg = w_sig/w_sum,w_bkg/w_sum n_events = np.sum(counts) norm = n_events*(xmax-xmin)/xbins #plot lines #residual = np.exp(-parameters['slope']*(xmax-xmin)) #expo = lambda x_arr:st.expon(xmin,1./parameters['slope']).pdf(x_arr)*w_bkg*norm /(1.-residual) uni = lambda x_arr: np.repeat(1./(xmax-xmin),len(x_arr))*w_bkg*norm gauss = lambda x_arr:st.norm(parameters['mean'],parameters['sigma']).pdf(x_arr)*w_sig*norm pdf_x = np.arange(1000,dtype='float')/1000.*(xmax-xmin) + xmin sig = gauss(pdf_x) bkg = uni(pdf_x) upperLimit = np.max(counts)*1.1 # for exponential distribution peak bkg[bkg > upperLimit] = upperLimit fig.line(pdf_x, sig, legend="Signal", line_width=2,color='blue') fig.line(pdf_x, bkg, legend="Background", line_width=2,color = 'red') fig.line(pdf_x, sig+bkg, legend="Sum", line_width=2,color='green') return fig
def get_hist(self, fig): x, c = self.get_hist_data() deltas = np.sqrt(c) return vis_bokeh.whiskered_histogram(self.xmin, self.xmax, self.xbins, c, deltas, -deltas, fig=fig)