def plot(path, leg_title, ecms, xmin, xmax, ymin, ymax, bins, charm): try: f_data = TFile(path[0]) t_data = f_data.Get('save') entries_data = t_data.GetEntries() logging.info('data entries :' + str(entries_data)) except: logging.error(path[0] + ' is invalid!') sys.exit() try: f_side1 = TFile(path[1]) t_side1 = f_side1.Get('save') entries_side1 = t_side1.GetEntries() logging.info('data(side1) entries :' + str(entries_side1)) except: logging.error(path[1] + ' is invalid!') sys.exit() try: f_side2 = TFile(path[2]) t_side2 = f_side2.Get('save') entries_side2 = t_side2.GetEntries() logging.info('data(side2) entries :' + str(entries_side2)) except: logging.error(path[2] + ' is invalid!') sys.exit() try: f_side3 = TFile(path[3]) t_side3 = f_side3.Get('save') entries_side3 = t_side3.GetEntries() logging.info('data(side3) entries :' + str(entries_side3)) except: logging.error(path[3] + ' is invalid!') sys.exit() try: f_side4 = TFile(path[4]) t_side4 = f_side4.Get('save') entries_side4 = t_side4.GetEntries() logging.info('data(side4) entries :' + str(entries_side4)) except: logging.error(path[4] + ' is invalid!') sys.exit() set_pub_style() set_prelim_style() # from ROOT import gStyle # colors = array('i', 7*[0]) # for i in range(7): colors[i] = 18 - i # gStyle.SetPalette(7, colors) gStyle.SetPalette(112) mbc = TCanvas('mbc', 'mbc', 800, 600) if charm == 1: xtitle = 'RM(D_{miss}^{-}) (GeV/c^{2})' ytitle = 'RM(#pi_{d}^{+}#pi_{d}^{-}) (GeV/c^{2})' else: xtitle = 'RM(D_{miss}^{+}) (GeV/c^{2})' ytitle = 'RM(#pi_{d}^{+}#pi_{d}^{-}) (GeV/c^{2})' h_data = TH2F('scatter_data', 'scatter plot of M(Dpi) and M(Dmisspi)', bins, xmin, xmax, bins, ymin, ymax) format_data_hist(h_data) name_axis(h_data, xtitle, ytitle) fill(t_data, h_data, charm) h_side1 = TH2F('scatter_side1', 'scatter plot of M(Dpi) and M(Dmisspi)', bins, xmin, xmax, bins, ymin, ymax) fill(t_side1, h_side1, charm) h_side2 = TH2F('scatter_side2', 'scatter plot of M(Dpi) and M(Dmisspi)', bins, xmin, xmax, bins, ymin, ymax) fill(t_side2, h_side2, charm) h_side3 = TH2F('scatter_side3', 'scatter plot of M(Dpi) and M(Dmisspi)', bins, xmin, xmax, bins, ymin, ymax) fill(t_side3, h_side3, charm) h_side4 = TH2F('scatter_side4', 'scatter plot of M(Dpi) and M(Dmisspi)', bins, xmin, xmax, bins, ymin, ymax) fill(t_side4, h_side4, charm) h_side1.Add(h_side2) h_side1.Scale(0.5) h_side3.Add(h_side4) h_side3.Scale(0.25) h_side1.Add(h_side3, -1) h_data.Add(h_side1, -1) h_data.Draw('COLZ') # h_data.Draw('box') pt = TPaveText(0.55, 0.7, 0.75, 0.85, "BRNDC") set_pavetext(pt) pt.Draw() # pt.AddText(leg_title) if charm == 1: pt.AddText(leg_title + ' D_{tag}^{+}') if charm == -1: pt.AddText(leg_title + ' D_{tag}^{-}') if not os.path.exists('./figs/'): os.makedirs('./figs/') mbc.SaveAs('./figs/scatter_rm_Dmiss_rm_pipi_' + str(ecms) + '_' + str(charm) + '.pdf') raw_input('Press <Enter> to end...')
def fit(path, shape_path, ecms, mode, patch): try: f_data = TFile(path[0]) t_data = f_data.Get('save') entries_data = t_data.GetEntries() logging.info('Entries :'+str(entries_data)) except: logging.error('File paths are invalid!') set_pub_style() set_prelim_style() mbc = TCanvas('mbc', 'mbc', 1000, 700) pad = TPad("pad", "pad", 0.0, 0.0, 1.0, 1.0) pad.Draw() xmin = 1.75 xmax = 1.95 xbins = 100 if mode == 'data_after' and (ecms == 4245 or ecms == 4280 or ecms == 4310 or ecms == 4575): xbins = 50 if mode == 'data_before' and (ecms == 4245 or ecms == 4280 or ecms == 4310 or ecms == 4575): xbins = 50 rm_Dpipi = RooRealVar('rm_Dpipi', 'rm_Dpipi', xmin, xmax) data = RooDataSet('data', 'dataset', t_data, RooArgSet(rm_Dpipi)) is_OK = 2 chi2_ndf = 999. while True: # signal f_shape = TFile(shape_path, 'READ') h_shape = f_shape.Get('h_hist') h_signal = RooDataHist('h_shape', 'h_shape', RooArgList(rm_Dpipi), h_shape) pdf_signal = RooHistPdf('pdf_signal', 'pdf_signal', RooArgSet(rm_Dpipi), h_signal, 0) mean_low, mean_up, sigma_up = param_rm_Dpipi(ecms) if not (mode == 'data_before' or mode == 'data_after'): mean_low, mean_up, sigma_up = mean_low*(1 + random.uniform(-1, 1)), mean_up*(1 + random.uniform(-1, 1)), sigma_up*(1 + random.uniform(-1, 1)) if sigma_up > 0.001: sigma_up += 0.001 mean = RooRealVar('mean', 'mean of gaussian', 0., mean_low, mean_up) sigma = RooRealVar('sigma', 'sigma of gaussian', 0.001, 0, sigma_up) f_param = open('./txts/param_' + str(ecms) + '_' + patch + '.txt', 'r') lines_param = f_param.readlines() for line_param in lines_param: rs_param = line_param.rstrip('\n') rs_param = filter(None, rs_param.split(" ")) ndf = float(float(rs_param[0])) a_val = float(float(rs_param[1])) if ndf == 6: b_val = float(float(rs_param[2])) mean_val = float(float(rs_param[3])) sigma_val = float(float(rs_param[4])) else: mean_val = float(float(rs_param[2])) sigma_val = float(float(rs_param[3])) if mode == 'data_before' or mode == 'data_after': mean = RooRealVar('mean', 'mean of gaussian', mean_val) sigma = RooRealVar('sigma', 'sigma of gaussian', sigma_val) gauss = RooGaussian('gauss', 'gaussian', rm_Dpipi, mean, sigma) rm_Dpipi.setBins(xbins, 'cache') sigpdf = RooFFTConvPdf('sigpdf', 'sigpdf', rm_Dpipi, pdf_signal, gauss) # background if ndf == 6: a = RooRealVar('a', 'a', 0., -99., 99.) b = RooRealVar('b', 'b', 0., -99., 99.) if ecms == 4440: a = RooRealVar('a', 'a', 0., -1., 1.) b = RooRealVar('b', 'b', 0., -1., 1.) if ecms == 4470: a = RooRealVar('a', 'a', 0., -1., 1.) b = RooRealVar('b', 'b', 0., -1., 1.) if ecms == 4260: a = RooRealVar('a', 'a', 0., -1., 1.) b = RooRealVar('b', 'b', 0., -1., 1.) if ecms == 4190: a = RooRealVar('a', 'a', 0., -9., 9.) b = RooRealVar('b', 'b', 0., -9., 9.) bkgpdf = RooChebychev('bkgpdf', 'bkgpdf', rm_Dpipi, RooArgList(a, b)) if ndf == 5: a = RooRealVar('a', 'a', 0., -99., 99.) bkgpdf = RooChebychev('bkgpdf', 'bkgpdf', rm_Dpipi, RooArgList(a)) # event number nsig = RooRealVar('nsig', 'nsig', 100, -500000, 500000) nbkg = RooRealVar('nbkg', 'nbkg', 80, 0, 500000) # fit model model = RooAddPdf('model', 'sigpdf + bkgpdf', RooArgList(sigpdf, bkgpdf), RooArgList(nsig, nbkg)) results = model.fitTo(data, RooFit.Save()) is_OK = int(results.covQual()) status = int(results.status()) # plot results xframe = rm_Dpipi.frame(RooFit.Bins(xbins), RooFit.Range(xmin, xmax)) data.plotOn(xframe) model.plotOn(xframe, RooFit.LineWidth(5)) model.plotOn(xframe, RooFit.Components('sigpdf'), RooFit.LineColor(kRed), RooFit.LineWidth(5), RooFit.LineStyle(9)) model.plotOn(xframe, RooFit.Components('bkgpdf'), RooFit.LineColor(kGreen), RooFit.LineWidth(5), RooFit.LineStyle(6)) data.plotOn(xframe) xtitle = 'RM(D^{+}#pi^{+}_{d}#pi^{-}_{d}) (GeV/c^{2})' content = (xmax - xmin)/xbins * 1000 ytitle = 'Events/%.1f MeV/c^{2}'%content format_data_hist(xframe) name_axis(xframe, xtitle, ytitle) xframe.Draw() if mode == 'data_before' or mode == 'data_after' or mode == 'DDPIPI' or mode == 'psipp' or mode == 'D1_2420': fr = model.fitTo(data, RooFit.Extended(kTRUE), RooFit.Save(kTRUE)) curve = xframe.getObject(1) histo = xframe.getObject(0) chi2_tot, nbin, ytot, avg, eyl, eyh = 0, 0, 0, 0, 0, 0 x = array('d', 999*[0]) y = array('d', 999*[0]) for i in xrange(xbins): histo.GetPoint(i, x, y) exl = histo.GetEXlow()[i] exh = histo.GetEXhigh()[i] avg += curve.average(x[0] - exl, x[0] + exh) ytot += y[0] eyl += histo.GetEYlow()[i] * histo.GetEYlow()[i] eyh += histo.GetEYhigh()[i] * histo.GetEYhigh()[i] if ytot >= 7: if ytot > avg: pull = (ytot - avg)/sqrt(eyl) else: pull = (ytot - avg)/sqrt(eyh) chi2_tot += pull * pull nbin += 1 ytot, avg, eyl, eyh = 0, 0, 0, 0 if mode == 'data_before' or mode == 'data_after': if ecms == 4245 or ecms == 4310: pt = TPaveText(0.6, 0.7, 0.75, 0.85, "BRNDC") else: pt = TPaveText(0.17, 0.15, 0.3, 0.25, "BRNDC") else: pt = TPaveText(0.17, 0.7, 0.3, 0.85, "BRNDC") set_pavetext(pt) pt.Draw() if ecms == 4230: pt_title = '(b)' elif ecms == 4420: pt_title = '(d)' elif ecms == 4680: pt_title = '(f)' else: pt_title = str(ecms) + ' MeV: ' pt.AddText(pt_title) n_param = results.floatParsFinal().getSize() pt_title = '#chi^{2}/ndf = ' # pt.AddText(pt_title) pt_title = str(round(chi2_tot, 2)) + '/' + str(nbin - n_param -1) + '=' + str(round(chi2_tot/(nbin - n_param -1), 2)) chi2_ndf = chi2_tot/(nbin - n_param -1) # pt.AddText(pt_title) print 'chi2 vs ndf = ' + str(round(chi2_tot/(nbin - n_param -1), 2)) if not os.path.exists('./figs/'): os.makedirs('./figs/') mbc.SaveAs('./figs/fit_rm_Dpipi_' + str(ecms) + '_' + mode + '_sideband.pdf') if not os.path.exists('./txts/'): os.makedirs('./txts/') if mode == 'data_after': path_sig = './txts/data_sideband_events_'+ str(ecms) +'_' + patch + '.txt' if not mode == 'data_after': path_sig = './txts/' + mode + '_sideband_events_'+ str(ecms) +'_' + patch + '.txt' f_sig = open(path_sig, 'w') out = str(nsig.getVal()) + ' ' + str(nsig.getError()) + '\n' f_sig.write(out) f_sig.close() if mode == 'data_before': is_OK = -1 if mode == 'data_after': is_OK = -1 if mode == 'D1_2420': is_OK = -1 if mode == 'psipp': is_OK = -1 if mode == 'DDPIPI': is_OK = -1 if mode == 'DDPI': is_OK = -1 if is_OK == -1: break if (is_OK == 3 and status == 0 and chi2_ndf < 1.8 and ecms < 4221): break if (is_OK == 3 and status == 0 and chi2_ndf < 1.5 and ecms > 4221): break raw_input('enter anything to end...')
def fit(path, shape_path, ecms, mode, patch): try: f_data = TFile(path[0]) t_data = f_data.Get('save') entries_data = t_data.GetEntries() logging.info('Entries :' + str(entries_data)) except: logging.error('File paths are invalid!') set_pub_style() set_prelim_style() mbc = TCanvas('mbc', 'mbc', 1000, 700) pad = TPad("pad", "pad", 0.0, 0.0, 1.0, 1.0) # set_pad_style(pad) pad.Draw() xmin = 1.75 xmax = 1.95 xbins = 100 if mode == 'data' and (ecms == 4245 or ecms == 4280 or ecms == 4310 or ecms == 4575): xbins = 50 rm_Dpipi = RooRealVar('rm_Dpipi', 'rm_Dpipi', xmin, xmax) data = RooDataSet('data', 'dataset', t_data, RooArgSet(rm_Dpipi)) is_OK = 2 chi2_ndf = 999. while True: # signal f_shape = TFile(shape_path, 'READ') h_shape = f_shape.Get('h_hist') h_signal = RooDataHist('h_shape', 'h_shape', RooArgList(rm_Dpipi), h_shape) pdf_signal = RooHistPdf('pdf_signal', 'pdf_signal', RooArgSet(rm_Dpipi), h_signal, 0) mean_low, mean_up, sigma_up = param_rm_Dpipi(ecms) if not mode == 'data': mean_low, mean_up, sigma_up = mean_low * (1 + random.uniform( -1, 1)), mean_up * (1 + random.uniform(-1, 1)), sigma_up * ( 1 + random.uniform(-1, 1)) if sigma_up > 0.001: sigma_up += 0.001 mean = RooRealVar('mean', 'mean of gaussian', 0., mean_low, mean_up) sigma = RooRealVar('sigma', 'sigma of gaussian', 0.001, 0, sigma_up) if mode == 'data' and (ecms == 4245 or ecms == 4390 or ecms == 4575 or ecms == 4946 or ecms == 4237 or ecms == 4220 or ecms == 4210 or ecms == 4200 or ecms == 4190): if ecms == 4190: f_param = open('./txts/param_4230_' + patch + '.txt', 'r') if ecms == 4200: f_param = open('./txts/param_4230_' + patch + '.txt', 'r') if ecms == 4210: f_param = open('./txts/param_4230_' + patch + '.txt', 'r') if ecms == 4220: f_param = open('./txts/param_4230_' + patch + '.txt', 'r') if ecms == 4237: f_param = open('./txts/param_4230_' + patch + '.txt', 'r') if ecms == 4245: f_param = open('./txts/param_4246_' + patch + '.txt', 'r') if ecms == 4390: f_param = open('./txts/param_4380_' + patch + '.txt', 'r') if ecms == 4575 or ecms == 4530: f_param = open('./txts/param_4440_' + patch + '.txt', 'r') if ecms == 4946: f_param = open('./txts/param_4914_' + patch + '.txt', 'r') lines_param = f_param.readlines() for line_param in lines_param: rs_param = line_param.rstrip('\n') rs_param = filter(None, rs_param.split(" ")) ndf = float(float(rs_param[0])) a_val = float(float(rs_param[1])) if ndf == 6: b_val = float(float(rs_param[2])) mean_val = float(float(rs_param[3])) sigma_val = float(float(rs_param[4])) else: mean_val = float(float(rs_param[2])) sigma_val = float(float(rs_param[3])) mean = RooRealVar('mean', 'mean of gaussian', mean_val) sigma = RooRealVar('sigma', 'sigma of gaussian', sigma_val) gauss = RooGaussian('gauss', 'gaussian', rm_Dpipi, mean, sigma) rm_Dpipi.setBins(xbins, 'cache') sigpdf = RooFFTConvPdf('sigpdf', 'sigpdf', rm_Dpipi, pdf_signal, gauss) # background a = RooRealVar('a', 'a', 0, -99, 99) b = RooRealVar('b', 'b', 0, -99, 99) if ecms == 4780: a = RooRealVar('a', 'a', 0, -1, 1) b = RooRealVar('b', 'b', 0, -1, 1) if ecms == 4700: a = RooRealVar('a', 'a', 0, -1, 1) b = RooRealVar('b', 'b', 0, -1, 1) if ecms == 4640: a = RooRealVar('a', 'a', 0, -1, 1) b = RooRealVar('b', 'b', 0, -1, 1) if ecms == 4600: a = RooRealVar('a', 'a', 0, -1, 1) b = RooRealVar('b', 'b', 0, -1, 1) if ecms == 4440: a = RooRealVar('a', 'a', 0, -9, 9) b = RooRealVar('b', 'b', 0, -9, 9) if ecms == 4400: a = RooRealVar('a', 'a', 0, -1, 1) b = RooRealVar('b', 'b', 0, -1, 1) if ecms == 4380: a = RooRealVar('a', 'a', 0, -9, 9) b = RooRealVar('b', 'b', 0, -9, 9) c = RooRealVar('c', 'c', 0, -99, 99) d = RooRealVar('c', 'c', 0, -99, 99) bkgpdf = RooChebychev('bkgpdf', 'bkgpdf', rm_Dpipi, RooArgList(a, b)) n_free = 6 if ecms == 4237 or ecms == 4245 or ecms == 4270 or ecms == 4280 or ecms == 4310 or ecms == 4360 or ecms == 4390: bkgpdf = RooChebychev('bkgpdf', 'bkgpdf', rm_Dpipi, RooArgList(a)) n_free = 5 if ecms == 4290 or ecms == 4315 or ecms == 4340 or ecms == 4575 or ecms == 4620 or ecms == 4740 or ecms == 4750 or ecms == 4780 or ecms == 4840: bkgpdf = RooChebychev('bkgpdf', 'bkgpdf', rm_Dpipi, RooArgList(a)) n_free = 5 # event number nsig = RooRealVar('nsig', 'nsig', 100, -500000, 500000) nbkg = RooRealVar('nbkg', 'nbkg', 80, 0, 500000) # fit model model = RooAddPdf('model', 'sigpdf + bkgpdf', RooArgList(sigpdf, bkgpdf), RooArgList(nsig, nbkg)) if mode == 'none_sig': model = RooAddPdf('model', 'bkgpdf', RooArgList(bkgpdf), RooArgList(nbkg)) results = model.fitTo(data, RooFit.Save()) is_OK = int(results.covQual()) status = int(results.status()) # plot results xframe = rm_Dpipi.frame(RooFit.Bins(xbins), RooFit.Range(xmin, xmax)) data.plotOn(xframe) model.plotOn(xframe, RooFit.LineWidth(5)) if not mode == 'none_sig': model.plotOn(xframe, RooFit.Components('sigpdf'), RooFit.LineColor(kRed), RooFit.LineWidth(5), RooFit.LineStyle(9)) model.plotOn(xframe, RooFit.Components('bkgpdf'), RooFit.LineColor(kGreen), RooFit.LineWidth(5), RooFit.LineStyle(6)) data.plotOn(xframe) xtitle = 'RM(D^{+}#pi^{+}_{d}#pi^{-}_{d}) (GeV/c^{2})' content = (xmax - xmin) / xbins * 1000 ytitle = 'Events/%.1f MeV/c^{2}' % content format_data_hist(xframe) name_axis(xframe, xtitle, ytitle) xframe.Draw() if mode == 'data' or mode == 'DDPIPI' or mode == 'psipp' or mode == 'D1_2420' or mode == 'MC': fr = model.fitTo(data, RooFit.Extended(kTRUE), RooFit.Save(kTRUE)) curve = xframe.getObject(1) histo = xframe.getObject(0) chi2_tot, nbin, ytot, avg, eyl, eyh = 0, 0, 0, 0, 0, 0 x = array('d', 999 * [0]) y = array('d', 999 * [0]) for i in xrange(xbins): histo.GetPoint(i, x, y) exl = histo.GetEXlow()[i] exh = histo.GetEXhigh()[i] avg += curve.average(x[0] - exl, x[0] + exh) ytot += y[0] eyl += histo.GetEYlow()[i] * histo.GetEYlow()[i] eyh += histo.GetEYhigh()[i] * histo.GetEYhigh()[i] if ytot >= 7: if ytot > avg: pull = (ytot - avg) / sqrt(eyl) else: pull = (ytot - avg) / sqrt(eyh) chi2_tot += pull * pull nbin += 1 ytot, avg, eyl, eyh = 0, 0, 0, 0 if mode == 'data': if ecms == 4245 or ecms == 4310: pt = TPaveText(0.6, 0.7, 0.75, 0.85, "BRNDC") else: pt = TPaveText(0.17, 0.15, 0.3, 0.25, "BRNDC") else: pt = TPaveText(0.17, 0.7, 0.3, 0.85, "BRNDC") set_pavetext(pt) pt.Draw() if ecms == 4230: pt_title = '(a)' elif ecms == 4420: pt_title = '(c)' elif ecms == 4680: pt_title = '(e)' else: pt_title = str(ecms) + ' MeV: ' pt.AddText(pt_title) n_param = results.floatParsFinal().getSize() pt_title = '#chi^{2}/ndf = ' # pt.AddText(pt_title) pt_title = str(round( chi2_tot, 2)) + '/' + str(nbin - n_param - 1) + '=' + str( round(chi2_tot / (nbin - n_param - 1), 2)) chi2_ndf = chi2_tot / (nbin - n_param - 1) # pt.AddText(pt_title) print 'chi2 vs ndf = ' + str( round(chi2_tot / (nbin - n_param - 1), 2)) if not os.path.exists('./figs/'): os.makedirs('./figs/') mbc.SaveAs('./figs/fit_rm_Dpipi_' + str(ecms) + '_' + mode + '.pdf') if not os.path.exists('./txts/'): os.makedirs('./txts/') path_sig = './txts/' + mode + '_events_' + str( ecms) + '_' + patch + '.txt' f_sig = open(path_sig, 'w') out = str(nsig.getVal()) + ' ' + str(nsig.getError()) + '\n' f_sig.write(out) f_sig.close() if mode == 'data': path_param = './txts/param_' + str(ecms) + '_' + patch + '.txt' f_param = open(path_param, 'w') param = str(n_free) + ' ' param += str(a.getVal()) + ' ' if n_free == 6: param += str(b.getVal()) + ' ' param += str(mean.getVal()) + ' ' param += str(sigma.getVal()) + ' ' f_param.write(param) f_param.close() if mode == 'data' or mode == 'D1_2420' or mode == 'DDPIPI' or mode == 'psipp' or mode == 'MC': signal_low = 1.86965 - window(ecms) / 2. signal_up = 1.86965 + window(ecms) / 2. rm_Dpipi.setRange('srange', signal_low, signal_up) rm_Dpipi.setRange('allrange', xmin, xmax) nsrange = sigpdf.createIntegral( RooArgSet(rm_Dpipi), RooFit.NormSet(RooArgSet(rm_Dpipi)), RooFit.Range('srange')) nallrange = sigpdf.createIntegral( RooArgSet(rm_Dpipi), RooFit.NormSet(RooArgSet(rm_Dpipi)), RooFit.Range('allrange')) n_signal = nsrange.getVal() / nallrange.getVal() * (nsig.getVal()) n_all = nsig.getVal() n_signal_err = nsrange.getVal() / nallrange.getVal() * ( nsig.getError()) n_all_err = nsig.getError() factor = n_signal / n_all factor_err = sqrt(abs(factor * (1 - factor) / n_all)) print 'factor = n(signal) / n(all) = ' + str(round( factor, 4)) + '+/-' + str(round(factor_err, 4)) path_factor = './txts/factor_rm_Dpipi_' + str( ecms) + '_' + mode + '.txt' f_factor = open(path_factor, 'w') out = str(round(factor, 4)) + ' ' + str(round(factor_err, 4)) + '\n' f_factor.write(out) f_factor.close() if mode == 'data' or mode == 'none_sig': path_out = './txts/significance_likelihood_total_' + str( ecms) + '.txt' f_out = open(path_out, 'a') # -log(L) minimum sig_out = str(results.minNll()) + '\n' f_out.write(sig_out) f_out.close() if mode == 'data' or mode == 'none_sig': is_OK = -1 if is_OK == -1: break if (is_OK == 3 and status == 0 and chi2_ndf < 3. and ecms > 4221): break if (is_OK == 3 and status == 0 and chi2_ndf < 5. and ecms < 4221): break raw_input('enter anything to end...')
def fit(path, shape_path, ecms, mode, patch): try: f_data = TFile(path[0]) f_X_3842 = TFile(shape_path) t_data = f_data.Get('save') t_X_3842 = f_X_3842.Get('save') entries_data = t_data.GetEntries() entries_X_3842 = t_X_3842.GetEntries() logging.info('Entries(data) :' + str(entries_data)) logging.info('Entries(X_3842) :' + str(entries_X_3842)) except: logging.error('File paths are invalid!') set_pub_style() set_prelim_style() mbc = TCanvas('mbc', 'mbc', 1000, 700) pad = TPad("pad", "pad", 0.0, 0.0, 1.0, 1.0) pad.Draw() xmin = 3.79 xmax = 3.89 xbins = 50 rm_pipi = RooRealVar('rm_pipi', 'rm_pipi', xmin, xmax) data = RooDataSet('data', 'dataset', t_data, RooArgSet(rm_pipi)) chi2_ndf = 999. # signal cut = '' h_X_3842_rm_pipi = TH1F('h_X_3842_rm_pipi', '', xbins, xmin, xmax) t_X_3842.Project('h_X_3842_rm_pipi', 'rm_pipi', cut) hist_X_3842_rm_pipi = RooDataHist('hist_X_3842_rm_pipi', 'hist_X_3842_rm_pipi', RooArgList(rm_pipi), h_X_3842_rm_pipi) pdf_signal = RooHistPdf('pdf_X_3842_rm_pipi', 'pdf_X_3842_rm_pipi', RooArgSet(rm_pipi), hist_X_3842_rm_pipi, 1) mean_low, mean_up, sigma_up = param_rm_pipi(ecms) mean = RooRealVar('mean', 'mean of gaussian', 0., mean_low, mean_up) sigma = RooRealVar('sigma', 'sigma of gaussian', 0.001, 0, sigma_up) if (mode == 'data' and ecms != 4680) or mode == 'total': f_param = open('../../X_3842/txts/param_4680_' + patch + '.txt', 'r') lines_param = f_param.readlines() for line_param in lines_param: rs_param = line_param.rstrip('\n') rs_param = filter(None, rs_param.split(" ")) ndf = float(float(rs_param[0])) a_val = float(float(rs_param[1])) if ndf == 6: b_val = float(float(rs_param[2])) mean_val = float(float(rs_param[3])) sigma_val = float(float(rs_param[4])) else: mean_val = float(float(rs_param[2])) sigma_val = float(float(rs_param[3])) mean = RooRealVar('mean', 'mean of gaussian', mean_val) sigma = RooRealVar('sigma', 'sigma of gaussian', sigma_val) gauss = RooGaussian('gauss', 'gaussian', rm_pipi, mean, sigma) rm_pipi.setBins(xbins, 'cache') sigpdf = RooFFTConvPdf('sigpdf', 'sigpdf', rm_pipi, pdf_signal, gauss) # background a = RooRealVar('a', 'a', 0, -99, 99) b = RooRealVar('b', 'b', 0, -99, 99) c = RooRealVar('c', 'c', 0, -99, 99) d = RooRealVar('c', 'c', 0, -99, 99) bkgpdf = RooChebychev('bkgpdf', 'bkgpdf', rm_pipi, RooArgList(a, b)) if ecms == 4680: n_free = 6 else: n_free = 4 # event number nsig = RooRealVar('nsig', 'nsig', 100, -500000, 500000) nbkg = RooRealVar('nbkg', 'nbkg', 80, 0, 500000) # fit model model = RooAddPdf('model', 'sigpdf + bkgpdf', RooArgList(sigpdf, bkgpdf), RooArgList(nsig, nbkg)) if mode == 'none_sig': model = RooAddPdf('model', 'bkgpdf', RooArgList(bkgpdf), RooArgList(nbkg)) results = model.fitTo(data, RooFit.Save()) # plot results xframe = rm_pipi.frame(RooFit.Bins(xbins), RooFit.Range(xmin, xmax)) data.plotOn(xframe) model.plotOn(xframe, RooFit.LineWidth(5)) if not mode == 'none_sig': model.plotOn(xframe, RooFit.Components('sigpdf'), RooFit.LineColor(kRed), RooFit.LineWidth(5), RooFit.LineStyle(9)) model.plotOn(xframe, RooFit.Components('bkgpdf'), RooFit.LineColor(kGreen), RooFit.LineWidth(5), RooFit.LineStyle(6)) data.plotOn(xframe) xtitle = 'RM(#pi^{+}_{0}#pi^{-}_{0}) (GeV/c^{2})' content = (xmax - xmin) / xbins * 1000 ytitle = 'Events/%.1f MeV/c^{2}' % content format_data_hist(xframe) name_axis(xframe, xtitle, ytitle) xframe.Draw() if mode == 'data' or mode == 'total': fr = model.fitTo(data, RooFit.Extended(kTRUE), RooFit.Save(kTRUE)) curve = xframe.getObject(1) histo = xframe.getObject(0) chi2_tot, nbin, ytot, avg, eyl, eyh = 0, 0, 0, 0, 0, 0 x = array('d', 999 * [0]) y = array('d', 999 * [0]) for i in xrange(xbins): histo.GetPoint(i, x, y) exl = histo.GetEXlow()[i] exh = histo.GetEXhigh()[i] avg += curve.average(x[0] - exl, x[0] + exh) ytot += y[0] eyl += histo.GetEYlow()[i] * histo.GetEYlow()[i] eyh += histo.GetEYhigh()[i] * histo.GetEYhigh()[i] if ytot >= 7: if ytot > avg: pull = (ytot - avg) / sqrt(eyl) else: pull = (ytot - avg) / sqrt(eyh) chi2_tot += pull * pull nbin += 1 ytot, avg, eyl, eyh = 0, 0, 0, 0 if ecms == 4914 or ecms == 4946 or ecms == 4620: pt = TPaveText(0.17, 0.15, 0.3, 0.3, "BRNDC") elif ecms < 4600: pt = TPaveText(0.17, 0.15, 0.3, 0.2, "BRNDC") else: pt = TPaveText(0.17, 0.15, 0.3, 0.2, "BRNDC") set_pavetext(pt) pt.Draw() if ecms != 'all': if ecms == 4420: pt_title = '(a)' if ecms == 4680: pt_title = '(b)' else: pt_title = '(c)' pt.AddText(pt_title) n_param = results.floatParsFinal().getSize() pt_title = '#chi^{2}/ndf = ' # pt.AddText(pt_title) pt_title = str(round(chi2_tot, 2)) + '/' + str(nbin - n_param - 1) + '=' + str( round(chi2_tot / (nbin - n_param - 1), 2)) chi2_ndf = chi2_tot / (nbin - n_param - 1) # pt.AddText(pt_title) print 'chi2 vs ndf = ' + str(round(chi2_tot / (nbin - n_param - 1), 2)) if not os.path.exists('./figs/'): os.makedirs('./figs/') mbc.SaveAs('./figs/fit_rm_pipi_' + str(ecms) + '_' + mode + '.pdf') if not os.path.exists('./txts/'): os.makedirs('./txts/') path_sig = './txts/' + mode + '_events_' + str(ecms) + '_' + patch + '.txt' f_sig = open(path_sig, 'w') out = str(nsig.getVal()) + ' ' + str(nsig.getError()) + '\n' f_sig.write(out) f_sig.close() if mode == 'data': path_param = './txts/param_' + str(ecms) + '_' + patch + '.txt' f_param = open(path_param, 'w') param = str(n_free) + ' ' param += str(a.getVal()) + ' ' param += str(b.getVal()) + ' ' param += str(mean.getVal()) + ' ' param += str(sigma.getVal()) + ' ' f_param.write(param) f_param.close() if mode == 'data' or mode == 'none_sig' or mode == 'total': path_out = './txts/significance_likelihood_total_' + str(ecms) + '.txt' f_out = open(path_out, 'a') # -log(L) minimum sig_out = str(results.minNll()) + '\n' f_out.write(sig_out) f_out.close() raw_input('enter anything to end...')
def plot(path, leg_title, ecms, xmin, xmax, ymin, ymax, bins): try: f_data = TFile(path[0]) t_data = f_data.Get('save') entries_data = t_data.GetEntries() logging.info('data entries :'+str(entries_data)) except: logging.error(path[0] + ' is invalid!') sys.exit() set_pub_style() set_prelim_style() from ROOT import gStyle colors = array('i', 7*[0]) for i in range(7): colors[i] = 18 - i gStyle.SetPalette(7, colors) mbc = TCanvas('mbc', 'mbc', 800, 600) xtitle = 'M(K^{-}#pi^{+}#pi^{+}) (GeV)/c^{2}' ytitle = 'RM(D^{+}#pi_{d}^{+}#pi_{d}^{-}) (GeV)/c^{2}' h_data = TH2F('scatter_data', 'scatter plot of M(Kpipi) and Rm(Dpipi)', bins, xmin, xmax, bins, ymin, ymax) format_data_hist(h_data) name_axis(h_data, xtitle, ytitle) fill(t_data, h_data) h_data.SetMarkerSize(0.2) # h_data.Draw('box') h_data.Draw('COLZ') x_move = width(ecms) y_move = window(ecms) x_offset = width(ecms) y_offset = window(ecms) x0 = 1.86965 - 5/2. * x_move y0 = 1.86965 + 5/2. * y_move mats = make_matrix(3, 3, (x0, y0), x_move, x_offset, y_move, y_offset) VarsManager = locals() for i, mat in enumerate(mats): x_list = [] y_list = [] for point in mat: x, y = point x_list.append(x) y_list.append(y) x, y = array('d', x_list), array('d', y_list) VarsManager['box_' + str(i)] = TPolyLine(5, x, y) if i == 4: color = ROOT.kRed else: color = ROOT.kBlue VarsManager['box_' + str(i)].SetLineColor(color) VarsManager['box_' + str(i)].SetLineWidth(4) if i == 1 or i == 3 or i == 5 or i == 7: VarsManager['box_' + str(i)].SetLineStyle(9) if i == 0 or i == 2 or i == 6 or i == 8: VarsManager['box_' + str(i)].SetLineStyle(2) VarsManager['box_' + str(i)].Draw('same') # if i == 0: left, right, bottom, top = position_convert(min(x), xmin, xmax) + 0.0045, position_convert(max(x), xmin, xmax) + 0.025, position_convert(min(y), ymin, ymax) + 0.0045, position_convert(max(y), ymin, ymax) + 0.025 # elif i == 1 or i == 2: left, right, bottom, top = position_convert(min(x), xmin, xmax), position_convert(max(x), xmin, xmax) + 0.025, position_convert(min(y), ymin, ymax), position_convert(max(y), ymin, ymax) + 0.025 # elif i == 3: left, right, bottom, top = position_convert(min(x), xmin, xmax) + 0.0045, position_convert(max(x), xmin, xmax), position_convert(min(y), ymin, ymax) + 0.0045, position_convert(max(y), ymin, ymax) # elif i == 6: left, right, bottom, top = position_convert(min(x), xmin, xmax) + 0.0045, position_convert(max(x), xmin, xmax) - 0.025, position_convert(min(y), ymin, ymax) + 0.0045, position_convert(max(y), ymin, ymax) - 0.025 # elif i == 6 or i == 7 or i == 8: left, right, bottom, top = position_convert(min(x), xmin, xmax), position_convert(max(x), xmin, xmax) - 0.025, position_convert(min(y), ymin, ymax), position_convert(max(y), ymin, ymax) - 0.025 # else: left, right, bottom, top = position_convert(min(x), xmin, xmax), position_convert(max(x), xmin, xmax), position_convert(min(y), ymin, ymax), position_convert(max(y), ymin, ymax) # VarsManager['pt_' + str(i)] = TPaveText(left, bottom, right, top, "BRNDC") # set_pavetext(VarsManager['pt_' + str(i)]) # VarsManager['pt_' + str(i)].SetTextSize(0.06) # VarsManager['pt_' + str(i)].Draw() # if i == 0: VarsManager['pt_' + str(i)].AddText('(-1, 1)') # if i == 1: VarsManager['pt_' + str(i)].AddText(' (0, 1) ') # if i == 2: VarsManager['pt_' + str(i)].AddText(' (1, 1) ') # if i == 3: VarsManager['pt_' + str(i)].AddText('(-1, 0)') # if i == 4: VarsManager['pt_' + str(i)].AddText(' (0, 0) ') # if i == 5: VarsManager['pt_' + str(i)].AddText(' (1, 0) ') # if i == 6: VarsManager['pt_' + str(i)].AddText('(-1, -1)') # if i == 7: VarsManager['pt_' + str(i)].AddText(' (0, -1)') # if i == 8: VarsManager['pt_' + str(i)].AddText(' (1, -1)') # VarsManager['pt_' + str(i)].SetTextColor(color) pt = TPaveText(0.75, 0.77, 0.85, 0.87, "BRNDC") set_pavetext(pt) pt.Draw() pt.AddText(leg_title) if not os.path.exists('./figs/'): os.makedirs('./figs/') mbc.SaveAs('./figs/scatter_m_Kpipi_rm_Dpipi_'+str(ecms)+'.pdf') raw_input('Press <Enter> to end...')