def deltae(opts, args, figname): parsed = parse_args(args) datatype = parsed[0] tag = parsed[1] modes = parsed[2] label = parsed[3] alt_deltae = {'decutl': -1, 'decuth': 1} canvas = ROOT.TCanvas("aCanvas", "Canvas", 1000, 1000) canvas.Divide(3,3) deltae_hist = {} hist = ROOT.TH1F('deltae', 'DeltaE', 100, -0.1, 0.1) pad = 1 for mode in modes: frametitle = tools.get_mode_root_name(mode) canvas.cd(pad) pad += 1 modekey, sign = get_modekey_sign(mode) deltae_hist[modekey] = ROOT.TH1F('deltae-%s' % modekey, frametitle, 100, -0.1, 0.1) rootfile = get_rootfile(datatype, mode, label) pt = add_rootfile(rootfile, debug=opts.debug) for pte in pt: d =chooseD(modekey, pte, sign, alt_deltae=alt_deltae) if d != None: deltae_hist[modekey].Fill(pte.ddeltae[d]) deltae_hist[modekey].Draw() canvas_output(canvas, figname, opts.test)
def output_var_mass_kk(pt, mode, evtfile, label, test=False): nselected = 0 ntotal = 0 modekey, sign = tools.get_modekey_sign(mode) alt_deltae = None opt = '' modedict = {} fo = open(evtfile, 'w') for pte in pt: if test and nselected > 10: break ntotal += 1 if sign == None: raise ValueError(sign) d = chooseD(modekey, pte, sign, alt_deltae=alt_deltae, opt=opt) if d != None: nselected += 1 kaons = [pte.ddau1[d], pte.ddau2[d]] fourvecs = [] for index in kaons: fourvecs.append([ pte.trke[index], pte.trkpy[index], pte.trkpy[index], pte.trkpz[index] ]) kkmass = invmass(fourvecs[0], fourvecs[1]) fo.write('%s\n' % kkmass) fo.close() pt.Delete() return nselected, ntotal
def output_var_pi0mass(pt, mode, evtfile, label, test=False): nselected = 0 ntotal = 0 modekey, sign = tools.get_modekey_sign(mode) alt_deltae = None opt = '' if '/desideband' in label: side = label.split('/desideband_')[1] alt_deltae = attr.desideband(modekey, side) fo = open(evtfile, 'w') for pte in pt: if test and nselected > 10: break ntotal += 1 if sign != None: # single tag d = chooseD(modekey, pte, sign, alt_deltae=alt_deltae, opt=opt) if d != None: nselected += 1 if modekey == 1 or modekey == 203: npi0 = pte.ddau3[d] if modekey == 201: npi0 = pte.ddau4[d] pi0mass = pte.pi0mass[npi0] fo.write('%s\n' % pi0mass) else: # double tag raise NameError fo.close() return nselected, ntotal
def momentum_pi(opts, args, figname): parsed = parse_args(args) datatype = parsed[0] tag = parsed[1] modes = parsed[2] label = parsed[3] if len(modes) > 1: raise ValueError(modes, 'Only handle one mode at this time!') mode = modes[0] modekey, sign = get_modekey_sign(mode) modename = attr.modes[modekey]['uname'] rootfile = get_rootfile(datatype, mode, label) pt = add_rootfile(rootfile, debug=opts.debug) canvas = ROOT.TCanvas("aCanvas", "Canvas", 600, 600) hist = ROOT.TH1F('pip', '#pi momentum in %s' %modename, 100, 0, 1) num = 0 for pte in pt: if opts.test and num >1000: break d =chooseD(modekey, pte, sign) if d != None: num += 1 npi = pte.ddau2[d] pipx = pte.trpipx[npi] pipy = pte.trpipy[npi] pipz = pte.trpipz[npi] pip = sqrt(pipx**2 + pipy**2 + pipz**2) hist.Fill(pip) hist.Draw() canvas_output(canvas, figname, opts.test) hist.Delete()
def output_var_mctruth(pt, mode, evtfile, label, test=False): nselected = 0 ntotal = 0 modekey, sign = tools.get_modekey_sign(mode) alt_deltae = None opt = '' if '/desideband' in label: side = label.split('/desideband_')[1] alt_deltae = attr.desideband(modekey, side) if '/kssideband' in label: opt = 'kssideband' modedict = {} for pte in pt: if test and nselected > 10: break ntotal += 1 if sign != None: # single tag d = chooseD(modekey, pte, sign, alt_deltae=alt_deltae, opt=opt) if d != None: nselected += 1 count_mcdmodes(modedict, pte.mcdmode, pte.mcdbmode) else: # double tag raise NameError pt.Delete() output_modedict(modedict, evtfile) return nselected, ntotal
def mbc(opts, args, figname): parsed = parse_args(args) datatype = parsed[0] tag = parsed[1] modes = parsed[2] label = parsed[3] if len(modes) > 1: raise ValueError(modes) labels = [label] if ',' in label: labels = label.split(',') figname = shorten_figname(figname) mode = modes[0] modekey, sign = get_modekey_sign(mode) modename = attr.modes[modekey]['uname'] frametitle = modename hnames = parse_opts_set(opts.set, 'legend') if hnames: hist_names = hnames.split(',') else: hist_names = labels nBins = 100 Xmin = 1.83 Xmax = 1.89 hist_list = [] for label, hist_name in zip(labels, hist_names): hist_title = frametitle hist = ROOT.TH1D(hist_name, hist_title, nBins, Xmin, Xmax) rootfile = get_rootfile(datatype, mode, label) pt = add_rootfile(rootfile, debug=opts.debug) ntotal = 0 nselected = 0 for pte in pt: ntotal += 1 d =chooseD(modekey, pte, sign, opt=label) if d != None: hist.Fill(pte.dmbc[d]) nselected += 1 sys.stdout.write('Selected %s out of %s. \n' %(nselected, ntotal)) hist_list.append(hist) xtitle = 'mBC (GeV)' legend = ROOT.TLegend(0.66, 0.8, 0.9, 0.9) canvas = draw_hist(hist_list, xtitle, legend) Logy = parse_opts_set(opts.set, 'SetLogy') if Logy != None: canvas.SetLogy(Logy) canvas_output(canvas, figname, opts.test) hist.Delete()
def events(args): parsed = yld.parse_args(args) datatype = parsed[0] tag = parsed[1] modes = parsed[2] label = parsed[3] if _set and 'events' in _set: unique_evt_label = _set.split('=')[1] for mode in modes: events = get_events_list(datatype, mode, unique_evt_label, debug=_debug) if _debug: sys.stdout.write('\ndebug: total events %s\n' % len(events)) pt = chain_rootfile(datatype, mode, label) mode_key, sign = get_modekey_sign(mode) nselected = 0 ntotal = len(events) for pte in pt: if found_this_event(events, pte.run, pte.event): d = chooseD(mode_key, pte, sign, opt=label) if d != None: if _debug: sys.stdout.write('\ndebug: selected %s %s \n' % (pte.run, pte.event)) nselected += 1 sys.stdout.write(' selected %s out of %s.\n' % (nselected, ntotal)) sys.exit() evtname = datatype + '_' + mode + '.evt' unique_A, unique_B = get_unique_list(evtname, label_A, label_B) for evt in unique_B: evt = evt.strip().split(' ') run = int(evt[0]) event = int(evt[1]) print run, event for pte in pt_A: if pte.run == run and pte.event == event: print pte.ecm sys.exit() sys.exit() events_A, unique_file_A = get_events_set(datpath, evtname, label_A) events_B, unique_file_B = get_events_set(datpath, evtname, label_B) events_inter = events_A & events_B unique_A = events_A - events_inter unique_B = events_B - events_inter sys.stdout.write('Writing the unique events for mode %s ...' % mode) output_set_to_file(unique_A, unique_file_A) output_set_to_file(unique_B, unique_file_B) sys.stdout.write(' done.\n')
def events(args): parsed = yld.parse_args(args) datatype = parsed[0] tag = parsed[1] modes = parsed[2] label = parsed[3] if _set and 'events' in _set: unique_evt_label = _set.split('=')[1] for mode in modes: events = get_events_list( datatype, mode, unique_evt_label, debug=_debug) if _debug: sys.stdout.write('\ndebug: total events %s\n' %len(events)) pt = chain_rootfile(datatype, mode, label) mode_key, sign = get_modekey_sign(mode) nselected = 0 ntotal = len(events) for pte in pt: if found_this_event(events, pte.run, pte.event): d = chooseD(mode_key, pte, sign, opt = label) if d != None: if _debug: sys.stdout.write('\ndebug: selected %s %s \n' % (pte.run, pte.event)) nselected += 1 sys.stdout.write(' selected %s out of %s.\n' % (nselected, ntotal)) sys.exit() evtname = datatype + '_' + mode + '.evt' unique_A, unique_B = get_unique_list(evtname, label_A, label_B) for evt in unique_B: evt = evt.strip().split(' ') run = int(evt[0]) event = int(evt[1]) print run, event for pte in pt_A: if pte.run == run and pte.event == event: print pte.ecm sys.exit() sys.exit() events_A, unique_file_A = get_events_set(datpath, evtname, label_A) events_B, unique_file_B = get_events_set(datpath, evtname, label_B) events_inter = events_A & events_B unique_A = events_A - events_inter unique_B = events_B - events_inter sys.stdout.write('Writing the unique events for mode %s ...' %mode) output_set_to_file(unique_A, unique_file_A) output_set_to_file(unique_B, unique_file_B) sys.stdout.write(' done.\n')
def momentum_pi0(opts, args, figname): parsed = parse_args(args) datatype = parsed[0] tag = parsed[1] modes = parsed[2] label = parsed[3] if len(modes) > 1: raise ValueError(modes, 'Only handle one mode at this time!') mode = modes[0] modekey, sign = get_modekey_sign(mode) modename = attr.modes[modekey]['uname'] rootfile = get_rootfile(datatype, mode, label) pt = add_rootfile(rootfile, debug=opts.debug) #canvas = ROOT.TCanvas("aCanvas", "Canvas", 900, 900) var = 'pi0p' title = modename unit = 'GeV' nBins = 100 Xmin = 0. Xmax = 1. xtitle = '#pi^{0} momentum (GeV)' ytitle = 'Events / (%s %s)' % ((Xmax-Xmin)/nBins, unit) hist = ROOT.TH1F(var, title, nBins, Xmin, Xmax) num = 0 for pte in pt: if opts.test and num >1000: break d =chooseD(modekey, pte, sign) if d != None: num += 1 if modekey == 1 or modekey == 203: npi0 = pte.ddau3[d] if modekey == 201: npi0 = pte.ddau4[d] pi0px = pte.pi0px[npi0] pi0py = pte.pi0py[npi0] pi0pz = pte.pi0pz[npi0] pi0p = sqrt(pi0px**2 + pi0py**2 + pi0pz**2) hist.Fill(pi0p) canvas = draw_hist(hist, xtitle, ytitle) canvas_output(canvas, figname, label, test=opts.test)
def ksmass(opts, args, figname): parsed = parse_args(args) datatype = parsed[0] tag = parsed[1] modes = parsed[2] label = parsed[3] var = 'ksmass' varname = 'K_{S} mass' unit = 'GeV' nBins = 100 Xmin = 0.45 Xmax = 0.55 if len(modes) > 1: raise ValueError(modes, 'Only handle one mode at this time!') mode = modes[0] modekey, sign = get_modekey_sign(mode) modename = attr.modes[modekey]['uname'] histname = '%s in %s' %(varname, modename) rootfile = get_rootfile(datatype, mode, label) pt = add_rootfile(rootfile, debug=opts.debug) canvas = ROOT.TCanvas("aCanvas", "Canvas", 600, 600) hist = ROOT.TH1F(var, histname, nBins, Xmin, Xmax) ntotal = 0 nselected = 0 def alt_checkVetoes(mode, pte, index, opt='', sign=None): return True if modekey not in (202, 203, 204): raise ValueError(modekey) for pte in pt: ntotal += 1 if opts.test and ntotal >1000: break d =chooseD(modekey, pte, sign, checkVetoes=alt_checkVetoes) if d != None: nselected += 1 ksmass = pte.ksmass[pte.ddau1[d]] hist.Fill(ksmass) hist.Draw() canvas_output(canvas, figname, opts.test) hist.Delete() sys.stdout.write(' selected %s out of %s.\n' % (nselected, ntotal))
def single_tag_mode(datatype, mode, label, test): rootfile = tools.get_rootfile(datatype, mode, label) if test: sys.stdout.write('rootfile: %s \n' % rootfile) pt = tools.add_rootfile(rootfile) evtfile = get_evtfile(datatype, mode, label, 'kpimass', test=test) nselected = 0 ntotal = 0 modekey, sign = tools.get_modekey_sign(mode) if sign == None: raise ValueError(sign) sys.stdout.write('Saving %s ...' % evtfile) sys.stdout.flush() fo = open(evtfile, 'w') for pte in pt: ntotal += 1 if test and nselected > 100: break d = chooseD(modekey, pte, sign) if d != None: dcand = ptobj(pte, d) mbc = dcand.dmbc if mbc > 1.83 and mbc < 1.89: k1 = trkFourTuple(ptobj(pte, dcand.ddau1), 'k') k2 = trkFourTuple(ptobj(pte, dcand.ddau2), 'k') pi = trkFourTuple(ptobj(pte, dcand.ddau3), 'pi') kpimass = invmass(k1, pi) fo.write('%.5f %.5f %.5f %d\n' % (mbc, pte.ecm / 2, kpimass, pte.dcsign[d])) nselected += 1 fo.close() sys.stdout.write(' selected %s out of %s.\n' % (nselected, ntotal))
def single_tag_mode(datatype, mode, label, test): rootfile = tools.get_rootfile(datatype, mode, label) if test: sys.stdout.write('rootfile: %s \n' %rootfile) pt = tools.add_rootfile(rootfile) evtfile = get_evtfile(datatype, mode, label, 'kkmass', test=test) nselected = 0 ntotal = 0 modekey, sign = tools.get_modekey_sign(mode) if sign == None: raise ValueError(sign) sys.stdout.write('Saving %s ...' %evtfile) sys.stdout.flush() fo = open(evtfile , 'w') for pte in pt: ntotal += 1 if test and nselected > 100: break d = chooseD(modekey, pte, sign) if d != None: dcand = ptobj(pte, d) mbc = dcand.dmbc if mbc > 1.83 and mbc < 1.89: k1 = trkFourTuple(ptobj(pte, dcand.ddau1), 'k') k2 = trkFourTuple(ptobj(pte, dcand.ddau2), 'k') pi = trkFourTuple(ptobj(pte, dcand.ddau3), 'pi') kkmass = invmass(k1,k2) fo.write('%.5f %.5f %.5f %d\n' % (mbc, pte.ecm/2, kkmass, pte.dcsign[d])) nselected += 1 fo.close() sys.stdout.write(' selected %s out of %s.\n' % (nselected, ntotal))
def mass_kk(opts, args, figname): parsed = parse_args(args) datatype = parsed[0] tag = parsed[1] modes = parsed[2] label = parsed[3] if len(modes) > 1: raise ValueError(modes, 'Only handle one mode at this time!') mode = modes[0] modekey, sign = get_modekey_sign(mode) modename = attr.modes[modekey]['uname'] rootfile = get_rootfile(datatype, mode, label) pt = add_rootfile(rootfile, debug=opts.debug) canvas = ROOT.TCanvas("aCanvas", "Canvas", 600, 600) hist = ROOT.TH1F('kkmass', 'KK mass in %s' %modename, 100, 0, 2) hist.SetXTitle('M(K^{+}K^{-}) (GeV/c^{2})') num = 0 for pte in pt: if opts.test and num >10000: break d =chooseD(modekey, pte, sign) if modekey != 205: raise NameError(modekey) if d != None: num += 1 kaons = [pte.ddau1[d], pte.ddau2[d]] fourvecs = [] for index in kaons: fourvecs.append([pte.trke[index], pte.trkpy[index], pte.trkpy[index], pte.trkpz[index]]) kkmass = invmass(fourvecs[0], fourvecs[1]) hist.Fill(kkmass) hist.Draw() canvas_output(canvas, figname, label, opts.test, outputroot=True) hist.Delete()
def momentum_K(opts, args, figname): parsed = parse_args(args) datatype = parsed[0] tag = parsed[1] modes = parsed[2] label = parsed[3] if len(modes) > 1: raise ValueError(modes, 'Only handle one mode at this time!') mode = modes[0] modekey, sign = get_modekey_sign(mode) modename = attr.modes[modekey]['uname'] rootfile = get_rootfile(datatype, mode, label) pt = add_rootfile(rootfile, debug=opts.debug) canvas = ROOT.TCanvas("aCanvas", "Canvas") pmax = 1 if modekey == 0: pmax = 1.5 hist = ROOT.TH1F('K', 'K momentum in %s' %modename, 100, 0, pmax) num = 0 for pte in pt: if opts.test and num >1000: break d =chooseD(modekey, pte, sign) if d != None: num += 1 nK = pte.ddau1[d] Kpx = pte.trkpx[nK] Kpy = pte.trkpy[nK] Kpz = pte.trkpz[nK] Kp = sqrt(Kpx**2 + Kpy**2 + Kpz**2) hist.Fill(Kp) hist.Draw() canvas_output(canvas, figname, opts.test) hist.Delete()
def mass_pi0(opts, args, figname): parsed = parse_args(args) datatype = parsed[0] tag = parsed[1] modes = parsed[2] label = parsed[3] if len(modes) > 1: raise ValueError(modes, 'Only handle one mode at this time!') mode = modes[0] modekey, sign = get_modekey_sign(mode) modename = attr.modes[modekey]['uname'] rootfile = get_rootfile(datatype, mode, label) print rootfile, label sys.exit() pt = add_rootfile(rootfile, debug=opts.debug) canvas = ROOT.TCanvas("aCanvas", "Canvas") hist = ROOT.TH1F('pi0mass', '#pi^{0} mass in %s' %modename, 100, 0.1, 0.2) num = 0 for pte in pt: if opts.test and num >1000: break d =chooseD(modekey, pte, sign) if d != None: num += 1 if modekey == 1 or modekey == 203: npi0 = pte.ddau3[d] if modekey == 201: npi0 = pte.ddau4[d] pi0mass = pte.pi0mass[npi0] hist.Fill(pi0mass) hist.Draw() canvas_output(canvas, figname, opts.test) hist.Delete()
def momentum_pim_KS(opts, args, figname): parsed = parse_args(args) datatype = parsed[0] tag = parsed[1] modes = parsed[2] label = parsed[3] if len(modes) > 1: raise ValueError(modes, 'Only handle one mode at this time!') labels = [label] if ',' in label: labels = label.split(',') figname = shorten_figname(figname) mode = modes[0] modekey, sign = get_modekey_sign(mode) modename = attr.modes[modekey]['uname'] frametitle = modename if modekey not in (202, 203, 204): raise ValueError(modekey) hnames = parse_opts_set(opts.set, 'legend') if hnames: hist_names = hnames.split(',') else: hist_names = labels nBins = 100 Xmin = 0 Xmax = 1 xtitle = '#pi^{-} (K_{S}) momentum (GeV)' legend = ROOT.TLegend(0.75, 0.8, 0.95, 0.9) hist_list = [] for label, hist_name in zip(labels, hist_names): hist_title = frametitle hist = ROOT.TH1D(hist_name, hist_title, nBins, Xmin, Xmax) rootfile = get_rootfile(datatype, mode, label) pt = add_rootfile(rootfile, debug=opts.debug) ntotal = 0 nselected = 0 for pte in pt: ntotal += 1 if opts.test and ntotal >80000: break d =chooseD(modekey, pte, sign, opt=label) if d != None: nks = pte.ddau1[d] npi = pte.ksdau2[nks] pipx = pte.trpipx[npi] pipy = pte.trpipy[npi] pipz = pte.trpipz[npi] pip = sqrt(pipx**2 + pipy**2 + pipz**2) hist.Fill(pip) nselected += 1 sys.stdout.write('%s: Selected %s out of %s. \n' %( label,nselected, ntotal)) hist_list.append(hist) canvas = draw_hist(hist_list, xtitle, legend) canvas_output(canvas, figname, opts.test) hist.Delete()
def output_trkmtm_ks3pi(pt, mode, selfile, label, test=False): nselected = 0 ntotal = 0 modekey, signs = tools.get_modekey_sign(mode) f = TFile(selfile, 'recreate') efffile = selfile.replace('.root', '.db') effs = shelve.open(efffile) if signs != None: # single tag if not isinstance(signs, list): signs = [signs] mbc = {}; kpi1 = {} ; kpi2 = {}; pipih = {}; pipil = {}; pipipi = {} kpim = {}; kpipih = {}; kpipil = {} h_pks = {}; h_ppi1 = {}; h_ppi2 = {}; h_ppim = {} h_angks = {}; h_angpi1 = {}; h_angpi2 = {}; h_angpim = {} real_ks_pmag = 0 ; real_p1_pmag = 0 ; real_p2_pmag = 0 ; real_pm_pmag = 0 h_ks_dp = TH1F('h_ks_dp', 'K_{S} delta p', 100, -0.05, 0.05) h_p1_dp = TH1F('h_p1_dp', 'pi1 delta p', 100, -0.05, 0.05) h_p2_dp = TH1F('h_p2_dp', 'pi2 delta p', 100, -0.05, 0.05) h_pm_dp = TH1F('h_pm_dp', 'pi- delta p', 100, -0.05, 0.05) for type in ('mc', 'mctruth'): mbc[type] = TH1F('mbc_' + type, 'mbc', 100, 1.83, 1.89) h_pks[type] = TH1F('h_pk_' + type, 'K_{S} momentum', 20, 0, 1) h_ppi1[type] = TH1F('h_ppi1_' + type, '#pi^{+}_{1} momentum', 20, 0, 1) h_ppi2[type] = TH1F('h_ppi2_' + type, '#pi^{+}_{2} momentum', 20, 0, 1) h_ppim[type] = TH1F('h_ppim_' + type, '#pi^{-} momentum', 20, 0, 1) args_p = (12, 0.1, 0.95, 4, 0.05, 0.9, 8, 0.05, 0.75, 4, 0.05, 0.9) args_a = (6, 0, 1, 6, 0, 1, 6, 0, 1, 6, 0, 1) boxes_num_p = histBox(*args_p) boxes_denom_p = histBox(*args_p) boxes_num_a = histBox(*args_a) boxes_denom_a = histBox(*args_a) for pte in pt: if test and nselected > 10: break ntotal += 1 for sign in signs: loctree = makeDDecaySubTree(pte, sign) if sign == 1 and mcDmodeFixRad( loctree[0].mcDmode()) != modes[204]['mcdmode']: continue if sign == -1 and mcDmodeFixRad( loctree[0].mcDmode()) != modes[204]['mcdbmode']: continue mc_ks = None; mc_pi1 = None; mc_pi2 = None; mc_pim = None; mc_kl = None reald = loctree[0] nodes_of_interest = reald.interestingDescendants() for node in nodes_of_interest: if node.pdgid == sign*pdgid_KS: mc_ks = node elif node.pdgid == sign*pdgid_KL: mc_kl = node elif node.pdgid == sign*pdgid_pip: if not mc_pi1: mc_pi1 = node else: if mc_pi2: print 'ERROR' print len(nodes_of_interest) print mc_pi1 print mc_pi2 print node print ' ---- ' for nnode in nodes_of_interest: print nnode print '-' print '=================' mc_pi2 = node elif node.pdgid == sign*pdgid_pim: mc_pim = node if not mc_ks or not mc_pi1 or not mc_pi2 or not mc_pim: if mc_kl: continue else: print 'ERROR2', mc_ks, mc_pi1, mc_pi2, mc_pim else: pass ks = mc_ks.index ; pip1 = mc_pi1.index ; pip2 = mc_pi2.index pim = mc_pim.index pks = (pte.mce[ks], pte.mcpx[ks], pte.mcpy[ks], pte.mcpz[ks]) ppip1 = (pte.mce[pip1], pte.mcpx[pip1], pte.mcpy[pip1], pte.mcpz[pip1]) ppip2 = (pte.mce[pip2], pte.mcpx[pip2], pte.mcpy[pip2], pte.mcpz[pip2]) ppim = (pte.mce[pim], pte.mcpx[pim], pte.mcpy[pim], pte.mcpz[pim]) real_ks_pmag = pmag(pks) ; real_pm_pmag = pmag(ppim) zhat = (0,1,0,0) ang_ks = abs(cosangle(pks,zhat)) ang_pim = abs(cosangle(ppim,zhat)) h_pks['mctruth'].Fill(real_ks_pmag) h_ppim['mctruth'].Fill(real_pm_pmag) mag_ppi1 = pmag(ppip1); mag_ppi2 = pmag(ppip2) if max(mag_ppi1, mag_ppi2) == mag_ppi1: h_ppi1['mctruth'].Fill(mag_ppi1) h_ppi2['mctruth'].Fill(mag_ppi2) real_p1_pmag = mag_ppi1 ; real_p2_pmag = mag_ppi2 ang_pi1 = abs(cosangle(ppip1,zhat)); ang_pi2 = abs(cosangle(ppip2,zhat)) else: h_ppi1['mctruth'].Fill(mag_ppi2) h_ppi2['mctruth'].Fill(mag_ppi1) real_p1_pmag = mag_ppi2 ; real_p2_pmag = mag_ppi1 ang_pi1 = abs(cosangle(ppip2,zhat)); ang_pi2 = abs(cosangle(ppip1,zhat)) boxes_denom_p.fill(real_ks_pmag, real_p1_pmag, real_p2_pmag, real_pm_pmag) boxes_denom_a.fill(ang_ks, ang_pi1, ang_pi2, ang_pim) choice = chooseD(204, pte, sign) if choice != None: # and passDE(choice, pte): nselected += 1 mbc['mc'].Fill(pte.dmbc[choice]) if not 1.865 < pte.dmbc[choice] < 1.875: continue ks = pte.ddau1[choice]; pip1 = pte.ddau2[choice]; pip2 = pte.ddau3[choice]; pim = pte.ddau4[choice] pks = (pte.kse[ks], pte.kspx[ks], pte.kspy[ks], pte.kspz[ks]) ppip1 = (pte.trpie[pip1], pte.trpipx[pip1], pte.trpipy[pip1], pte.trpipz[pip1]) ppip2 = (pte.trpie[pip2], pte.trpipx[pip2], pte.trpipy[pip2], pte.trpipz[pip2]) ppim = (pte.trpie[pim], pte.trpipx[pim], pte.trpipy[pim], pte.trpipz[pim]) h_ks_dp.Fill(pmag(pks)-real_ks_pmag) h_pm_dp.Fill(pmag(ppim)-real_pm_pmag) h_pks['mc'].Fill(pmag(pks)) h_ppim['mc'].Fill(pmag(ppim)) try: ang_ks = abs(pte.trcosth[ks]) ang_pim = abs(pte.trcosth[pim]) except IndexError: pass mag_ppi1 = pmag(ppip1); mag_ppi2 = pmag(ppip2) if max(mag_ppi1, mag_ppi2) == mag_ppi1: h_ppi1['mc'].Fill(mag_ppi1) h_ppi2['mc'].Fill(mag_ppi2) h_p1_dp.Fill(mag_ppi1 - real_p1_pmag) h_p2_dp.Fill(mag_ppi2 - real_p2_pmag) ang_pi1 = abs(pte.trcosth[pip1]) ang_pi2 = abs(pte.trcosth[pip2]) else: h_ppi1['mc'].Fill(mag_ppi2) h_ppi2['mc'].Fill(mag_ppi1) h_p1_dp.Fill(mag_ppi2 - real_p1_pmag) h_p2_dp.Fill(mag_ppi1 - real_p2_pmag) ang_pi1 = abs(pte.trcosth[pip2]) ang_pi2 = abs(pte.trcosth[pip1]) boxes_num_p.fill(pmag(pks), min(mag_ppi2, mag_ppi1), max(mag_ppi2, mag_ppi1), pmag(ppim)) boxes_num_a.fill(ang_ks, ang_pi1, ang_pi2, ang_pim) effs['boxes_num_p'] = boxes_num_p effs['boxes_denom_p'] = boxes_denom_p effs['boxes_num_a'] = boxes_num_a effs['boxes_denom_a'] = boxes_denom_a effs.close() f.Write() f.Close() pt.Delete() return nselected, ntotal
def output_trkmtm_kspipi0(pt, mode, selfile, label, test=False): nselected = 0 ntotal = 0 modekey, signs = tools.get_modekey_sign(mode) f = TFile(selfile, 'recreate') efffile = selfile.replace('.root', '.db') effs = shelve.open(efffile) if signs != None: # single tag if not isinstance(signs, list): signs = [signs] mbc = {}; kpi1 = {} ; kpi2 = {}; pipih = {}; pipil = {}; pipipi = {} kpiz = {}; kpipih = {}; kpipil = {} h_pks = {}; h_ppi1 = {}; h_ppiz = {} h_angks = {}; h_angpi1 = {}; h_angpi2 = {}; h_angpiz = {} real_ks_pmag = 0 ; real_p1_pmag = 0 ; real_pz_pmag = 0 h_ks_dp = TH1F('h_ks_dp', 'K_{S} delta p', 100, -0.05, 0.05) h_p1_dp = TH1F('h_p1_dp', '#pi^{+} delta p', 100, -0.05, 0.05) h_pz_dp = TH1F('h_pz_dp', '#pi^{0} delta p', 100, -0.05, 0.05) h_dalitz = TH2F('h_dalitz', 'K_{S} #pi^{+} #pi^{0} Dalitz plot', 100, 0, 2, 100, 0, 3) for type in ('mc', 'mctruth'): mbc[type] = TH1F('mbc_' + type, 'mbc', 100, 1.83, 1.89) h_pks[type] = TH1F('h_pks_' + type, 'K_{S} momentum', 20, 0, 1) h_ppi1[type] = TH1F('h_ppi1_' + type, '#pi^{+} momentum', 20, 0, 1) h_ppiz[type] = TH1F('h_ppiz_' + type, '#pi^{0} momentum', 20, 0, 1) args = (11, 0, 1.1, 8, 0, 1.0, 8, 0, 1.0) boxes_num = histBox(*args) boxes_denom = histBox(*args) for pte in pt: if test and nselected > 10: break ntotal += 1 for sign in signs: loctree = makeDDecaySubTree(pte, sign) if sign == 1 and mcDmodeFixRad( loctree[0].mcDmode()) != modes[203]['mcdmode']: continue if sign == -1 and mcDmodeFixRad( loctree[0].mcDmode()) != modes[203]['mcdbmode']: continue mc_ks = None; mc_pi1 = None; mc_piz = None reald = loctree[0] nodes_of_interest = reald.interestingDescendants() for node in nodes_of_interest: if node.pdgid == pdgid_KS or node.pdgid == pdgid_KL: mc_ks = node elif node.pdgid == sign*pdgid_pip: if not mc_pi1: mc_pi1 = node else: print 'ERROR' elif node.pdgid == pdgid_piz: mc_piz = node if mc_ks and mc_ks.pdgid == pdgid_KL: continue if not mc_ks or not mc_pi1 or not mc_piz: print 'ERROR2', mc_ks, mc_pi1, mc_piz for node in nodes_of_interest: print node else: pass ks = mc_ks.index ; pip1 = mc_pi1.index ; piz = mc_piz.index pks = (pte.mce[ks], pte.mcpx[ks], pte.mcpy[ks], pte.mcpz[ks]) ppip1 = (pte.mce[pip1], pte.mcpx[pip1], pte.mcpy[pip1], pte.mcpz[pip1]) ppiz = (pte.mce[piz], pte.mcpx[piz], pte.mcpy[piz], pte.mcpz[piz]) real_ks_pmag = pmag(pks) ; real_pz_pmag = pmag(ppiz) if pmag(ppiz) >0: # print 'K0+piz give', invmass(pks, ppiz) # print 'piz+pip give', invmass(ppiz, ppip1) h_dalitz.Fill(invmasssq(ppiz, ppip1), invmasssq(pks, ppiz)) h_pks['mctruth'].Fill(real_ks_pmag) h_ppiz['mctruth'].Fill(real_pz_pmag) mag_ppi1 = pmag(ppip1) h_ppi1['mctruth'].Fill(mag_ppi1) real_p1_pmag = mag_ppi1 boxes_denom.fill(real_ks_pmag, real_p1_pmag, real_pz_pmag) choice = chooseD(203, pte, sign) if choice != None: # and passDE(choice, pte): nselected += 1 mbc['mc'].Fill(pte.dmbc[choice]) if not 1.86 < pte.dmbc[choice] < 1.87: continue ks = pte.ddau1[choice]; pip1 = pte.ddau2[choice]; piz = pte.ddau3[choice] pks = (pte.kse[ks], pte.kspx[ks], pte.kspy[ks], pte.kspz[ks]) ppip1 = (pte.trpie[pip1], pte.trpipx[pip1], pte.trpipy[pip1], pte.trpipz[pip1]) ppiz = (pte.pi0e[piz], pte.pi0px[piz], pte.pi0py[piz], pte.pi0pz[piz]) h_ks_dp.Fill(pmag(pks)-real_ks_pmag) h_pz_dp.Fill(pmag(ppiz)-real_pz_pmag) h_pks['mc'].Fill(pmag(pks)) h_ppiz['mc'].Fill(pmag(ppiz)) mag_ppi1 = pmag(ppip1); h_ppi1['mc'].Fill(mag_ppi1) h_p1_dp.Fill(mag_ppi1 - real_p1_pmag) boxes_num.fill(pmag(pks), mag_ppi1, pmag(ppiz)) effs['boxes_num'] = boxes_num effs['boxes_denom'] = boxes_denom effs.close() f.Write() f.Close() pt.Delete() return nselected, ntotal
def output_trkmtm_kkpi(pt, mode, selfile, label, test=False): nselected = 0 ntotal = 0 modekey, signs = tools.get_modekey_sign(mode) f = TFile(selfile, 'recreate') efffile = selfile.replace('.root', '.db') effs = shelve.open(efffile) if signs != None: # single tag if not isinstance(signs, list): signs = [signs] mbc = {}; kpi1 = {} ; kpi2 = {}; pipih = {}; pipil = {}; pipipi = {} kpiz = {}; kpipih = {}; kpipil = {} h_pkp = {}; h_pkm = {}; h_ppi = {} h_angkp = {}; h_angkm = {}; h_angpi = {}; real_kp_pmag = 0 ; real_km_pmag = 0 ; real_pi_pmag = 0 h_kp_dp = TH1F('h_kp_dp', 'K^{+} delta p', 100, -0.05, 0.05) h_km_dp = TH1F('h_km_dp', 'K^{-} delta p', 100, -0.05, 0.05) h_pi_dp = TH1F('h_pi_dp', '#pi^{+} delta p', 100, -0.05, 0.05) for type in ('mc', 'mctruth'): mbc[type] = TH1F('mbc_' + type, 'mbc', 100, 1.83, 1.89) h_pkp[type] = TH1F('h_pkp_' + type, 'K^{+} momentum', 20, 0, 1) h_pkm[type] = TH1F('h_pkm_' + type, 'K^{-} momentum', 20, 0, 1) h_ppi[type] = TH1F('h_ppi_' + type, '#pi^{+} momentum', 20, 0, 1) args = (11, 0, 1.1, 8, 0, 1.0, 8, 0, 1.0) boxes_num = histBox(*args) boxes_denom = histBox(*args) for pte in pt: if test and nselected > 10: break ntotal += 1 for sign in signs: loctree = makeDDecaySubTree(pte, sign) if sign == 1 and mcDmodeFixRad( loctree[0].mcDmode()) != modes[205]['mcdmode']: continue if sign == -1 and mcDmodeFixRad( loctree[0].mcDmode()) != modes[205]['mcdbmode']: continue mc_kp = None; mc_km = None; mc_pi = None reald = loctree[0] nodes_of_interest = reald.interestingDescendants() for node in nodes_of_interest: if node.pdgid == sign*pdgid_Kp: mc_kp = node elif node.pdgid == sign*pdgid_Km: mc_km = node elif node.pdgid == sign*pdgid_pip: mc_pi = node if not mc_kp or not mc_km or not mc_pi: print 'ERROR2', mc_kp, mc_km, mc_pi else: pass kp = mc_kp.index ; km = mc_km.index ; pi = mc_pi.index pkp = (pte.mce[kp], pte.mcpx[kp], pte.mcpy[kp], pte.mcpz[kp]) pkm = (pte.mce[km], pte.mcpx[km], pte.mcpy[km], pte.mcpz[km]) ppi = (pte.mce[pi], pte.mcpx[pi], pte.mcpy[pi], pte.mcpz[pi]) real_kp_pmag = pmag(pkp) ; real_km_pmag = pmag(pkm) real_pi_pmag = pmag(ppi) h_pkp['mctruth'].Fill(real_kp_pmag) h_pkm['mctruth'].Fill(real_km_pmag) h_ppi['mctruth'].Fill(real_pi_pmag) boxes_denom.fill(real_kp_pmag, real_km_pmag, real_pi_pmag) choice = chooseD(205, pte, sign) if choice != None: # and passDE(choice, pte): nselected += 1 mbc['mc'].Fill(pte.dmbc[choice]) if not 1.865 < pte.dmbc[choice] < 1.875: continue km = pte.ddau1[choice]; kp = pte.ddau2[choice]; pi = pte.ddau3[choice] pkp = (pte.trke[kp], pte.trkpx[kp], pte.trkpy[kp], pte.trkpz[kp]) pkm = (pte.trke[km], pte.trkpx[km], pte.trkpy[km], pte.trkpz[km]) ppi = (pte.trpie[pi], pte.trpipx[pi], pte.trpipy[pi], pte.trpipz[pi]) h_kp_dp.Fill(pmag(pkp)-real_kp_pmag) h_km_dp.Fill(pmag(pkm)-real_km_pmag) h_pi_dp.Fill(pmag(ppi) - real_pi_pmag) h_pkm['mc'].Fill(pmag(pkm)) h_pkp['mc'].Fill(pmag(pkp)) h_ppi['mc'].Fill(pmag(ppi)) boxes_num.fill(pmag(pkp), pmag(pkm), pmag(ppi)) effs['boxes_num'] = boxes_num effs['boxes_denom'] = boxes_denom effs.close() f.Write() f.Close() pt.Delete() return nselected, ntotal
def output_trkmtm_kpipi0(pt, mode, selfile, label, test=False): nselected = 0 ntotal = 0 modekey, signs = tools.get_modekey_sign(mode) if signs != None: # single tag if not isinstance(signs, list): signs = [signs] real_k_pmag = 0 ; real_p1_pmag = 0 ; real_pz_pmag = 0 mbc = {}; kpi1 = {} ; kpi2 = {}; pipih = {}; pipil = {}; pipipi = {} kpiz = {}; kpipih = {}; kpipil = {} h_pk = {}; h_ppi1 = {}; h_ppiz = {} h_angk = {}; h_angpi1 = {}; h_angpi2 = {}; h_angpiz = {} f = TFile(selfile, 'recreate') efffile = selfile.replace('.root', '.db') effs = shelve.open(efffile) h_k_dp = TH1F('h_k_dp', 'K delta p', 100, -0.05, 0.05) h_p1_dp = TH1F('h_p1_dp', 'pi1 delta p', 100, -0.05, 0.05) h_pz_dp = TH1F('h_pz_dp', 'pi^{0} delta p', 100, -0.05, 0.05) for type in ('mc', 'mctruth'): mbc[type] = TH1F('mbc_' + type, 'mbc', 100, 1.83, 1.89) h_pk[type] = TH1F('h_pk_' + type, 'K^{-} momentum', 20, 0, 1) h_ppi1[type] = TH1F('h_ppi1_' + type, '#pi^{+} momentum', 20, 0, 1) h_ppiz[type] = TH1F('h_ppiz_' + type, '#pi^{0} momentum', 20, 0, 1) args = (11, 0, 1.1, 8, 0, 1.0, 8, 0, 1.0) boxes_num = histBox(*args) boxes_denom = histBox(*args) for pte in pt: if test and nselected > 10: break ntotal += 1 for sign in signs: loctree = makeDDecaySubTree(pte, sign) if sign == 1 and mcDmodeFixRad( loctree[0].mcDmode()) != modes[1]['mcdmode']: continue if sign == -1 and mcDmodeFixRad( loctree[0].mcDmode()) != modes[1]['mcdbmode']: continue # do MC truth stuff first mc_k = None mc_pi1 = None mc_piz = None reald = loctree[0] nodes_of_interest = reald.interestingDescendants() for node in nodes_of_interest: if node.pdgid == sign*pdgid_Km: mc_k = node elif node.pdgid == sign*pdgid_pip: if not mc_pi1: mc_pi1 = node else: print 'ERROR' elif node.pdgid == pdgid_piz: mc_piz = node if not mc_k or not mc_pi1 or not mc_piz: print 'ERROR2', mc_k, mc_pi1, mc_piz else: pass k = mc_k.index ; pip1 = mc_pi1.index ; piz = mc_piz.index pk = (pte.mce[k], pte.mcpx[k], pte.mcpy[k], pte.mcpz[k]) ppip1 = (pte.mce[pip1], pte.mcpx[pip1], pte.mcpy[pip1], pte.mcpz[pip1]) ppiz = (pte.mce[piz], pte.mcpx[piz], pte.mcpy[piz], pte.mcpz[piz]) real_k_pmag = pmag(pk) ; real_pz_pmag = pmag(ppiz) h_pk['mctruth'].Fill(real_k_pmag) h_ppiz['mctruth'].Fill(real_pz_pmag) mag_ppi1 = pmag(ppip1) h_ppi1['mctruth'].Fill(mag_ppi1) real_p1_pmag = mag_ppi1 boxes_denom.fill(real_k_pmag, real_p1_pmag, real_pz_pmag) choice = chooseD(modekey, pte, sign) if choice != None : #and passDE(choice, pte): included in chooseD nselected += 1 mbc['mc'].Fill(pte.dmbc[choice]) if not 1.86 < pte.dmbc[choice] < 1.87: continue k = pte.ddau1[choice]; pip1 = pte.ddau2[choice]; piz = pte.ddau3[choice] pk = (pte.trke[k], pte.trkpx[k], pte.trkpy[k], pte.trkpz[k]) ppip1 = (pte.trpie[pip1], pte.trpipx[pip1], pte.trpipy[pip1], pte.trpipz[pip1]) ppiz = (pte.pi0e[piz], pte.pi0px[piz], pte.pi0py[piz], pte.pi0pz[piz]) h_k_dp.Fill(pmag(pk)-real_k_pmag) h_pz_dp.Fill(pmag(ppiz)-real_pz_pmag) h_pk['mc'].Fill(pmag(pk)) h_ppiz['mc'].Fill(pmag(ppiz)) mag_ppi1 = pmag(ppip1); h_ppi1['mc'].Fill(mag_ppi1) h_p1_dp.Fill(mag_ppi1 - real_p1_pmag) boxes_num.fill(pmag(pk), mag_ppi1, pmag(ppiz)) effs['boxes_num'] = boxes_num effs['boxes_denom'] = boxes_denom effs.close() f.Write() f.Close() pt.Delete() return nselected, ntotal
def output_multspec(reweight, pt, mode, selfile, label, test=False): mode, signs = tools.get_modekey_sign(mode) f = TFile(selfile, 'recreate') load_roofit_lib('signal', label) from ROOT import RooRealVar, RooDataSet, RooArgList, RooArgSet, \ RooGaussian, RooArgusBG, RooCBShape, RooAddPdf, RooPolynomial, \ gSystem, TCanvas efffile = selfile.replace('.root', '.db') effs = shelve.open(efffile) mbc = RooRealVar('mbc', 'Beam constrained mass', 1.83, 1.89, 'GeV') weight2 = RooRealVar('weight2', 'weight', 1) weight3 = RooRealVar('weight3', 'weight', 1) mbc_aset = RooArgSet(mbc) ntrack_d0 = TH1F('ntrack_d0', 'Number of other side tracks for D^{0} #rightarrow K#pi', 10, 0, 9) ntrack_dp = TH1F( 'ntrack_dp', 'Number of other side tracks for D^{+} #rightarrow K#pi#pi', 10, 0, 9) unweight_sum = 0 reweight_ntr_sum = 0 reweight_npi0_sum = 0 reweight_ntr_mcand_cnt = 0 reweight_npi0_mcand_cnt = 0 unweight_dmbc = TH1F('unweight_dmbc', 'm_{BC}, direct MC', 100, 1.83, 1.89) reweight_ntr_dmbc = TH1F('reweight_ntr_dmbc', 'm_{BC}, track reweighted MC', 100, 1.83, 1.89) reweight_npi0_dmbc = TH1F('reweight_npi0_dmbc', 'm_{BC}, #pi^{0} reweighted MC', 100, 1.83, 1.89) chgpart = (pdgid_pip, pdgid_pim, pdgid_Kp, pdgid_Km, pdgid_mup, pdgid_mum, pdgid_ep, pdgid_em) neupart = (pdgid_KL, pdgid_piz, pdgid_gamma) intpart = chgpart + neupart ntr_denoms = {} gen_dmbc_ntr = {} npi0_denoms = {} gen_dmbc_npi0 = {} for i in range(7 + 1): ntr_denoms[i] = 0 gen_dmbc_ntr[i] = TH1F('gen_dmbc_ntr_' + ` i `, 'm_{BC} for ' + ` i ` + ' tracks', 100, 1.83, 1.89) for i in range(5 + 1): npi0_denoms[i] = 0 gen_dmbc_npi0[i] = TH1F('gen_dmbc_npi0_' + ` i `, 'm_{BC} for ' + ` i ` + ' #pi^{0}', 100, 1.83, 1.89) ntotal = 0 nselected = 0 for pte in pt: ntotal += 1 if test and nselected > 10: break if test and ntotal > 1000: break if pte.ecm < 3.7: continue othsidetrk = 0 othsidepi0 = 0 dtree = makeDDecaySubTree(pte, -1) ddesc = dtree[0].interestingDescendants(intpart, False, False) for i in ddesc: if i.pdgid in chgpart: othsidetrk += 1 if (i.pdgid in chgpart and i.parent.pdgid == pdgid_KS and len(i.parent.daughters) == 1): # This absurd special case is due to GEANT's "K_S -> pi" decay othsidetrk += 1 if i.pdgid == pdgid_piz: othsidepi0 += 1 if (i.pdgid == pdgid_KS) and len(i.daughters) != 2: print i if mode >= 200: mod2 = 1 else: mod2 = 0 if (othsidetrk % 2) != mod2: print '-----------------------------------', othsidetrk print chgpart # for node in makeDecayTree(pte): # pass # if len(node.daughters) == 0 and node.pdgid in chgpart: # print node choice = chooseD(mode, pte, 1) if choice != None and not passDE(choice, pte): choice = None unweight_sum += 1 index_tr = getindex(othsidetrk, ntr_denoms) ntr_denoms[index_tr] += 1 index_pi0 = getpi0index(othsidepi0, npi0_denoms) npi0_denoms[index_pi0] += 1 final_weight_vector_ntr = attr.multiplicity_weight_ntr[reweight] initial_weight_vector_ntr = attr.multiplicity_weight_ntr['init'] final_weight_vector_npi0 = attr.multiplicity_weight_npi0[reweight] initial_weight_vector_npi0 = attr.multiplicity_weight_npi0['init'] reweight_ntr_sum += (final_weight_vector_ntr[index_tr] / initial_weight_vector_ntr[index_tr]) reweight_npi0_sum += (final_weight_vector_npi0[index_pi0] / initial_weight_vector_npi0[index_pi0]) if choice != None: nselected += 1 unweight_dmbc.Fill(pte.dmbc[choice]) reweight_ntr_dmbc.Fill(pte.dmbc[choice], (final_weight_vector_ntr[index_tr] / initial_weight_vector_ntr[index_tr])) reweight_npi0_dmbc.Fill(pte.dmbc[choice], (final_weight_vector_npi0[index_pi0] / initial_weight_vector_npi0[index_pi0])) gen_dmbc_ntr[index_tr].Fill(pte.dmbc[choice]) gen_dmbc_npi0[index_pi0].Fill(pte.dmbc[choice]) mbc.setVal(pte.dmbc[choice]) effs['npi0_denom'] = npi0_denoms effs['ntr_denom'] = ntr_denoms effs['unweight_sum'] = unweight_sum effs['reweight_ntr_sum'] = reweight_ntr_sum effs['reweight_npi0_sum'] = reweight_npi0_sum effs.close() #sys.stdout.write('Saved %s \n' %efffile) f.Write() f.Close() pt.Delete() return nselected, ntotal
def output_multcand(pt, mode, selname, label, test=False): selpath_single = os.path.join(attr.datpath, 'evt', label, 'single_candidate') selfile_single = tools.check_and_join(selpath_single, selname) selpath_multiple = os.path.join(attr.datpath, 'evt', label, 'multiple_candidate') selfile_multiple = tools.check_and_join(selpath_multiple, selname) modekey, sign = tools.get_modekey_sign(mode) if sign == None: if not isinstance(modekey, tuple): raise ValueError(modekey) if modekey[0] in [0, 200]: signs = [-1] if modekey[1] in [0, 200]: signs = [1, -1] modekey = modekey[1] elif modekey[1] in [0, 200]: signs = [1] modekey = modekey[0] else: raise ValueError(modekey) else: signs = [sign] outf_s = open(selfile_single, 'w') outf_m = open(selfile_multiple, 'w') ntotal = 0 nselected = 0 for pte in pt: ntotal += 1 if test and nselected >= 30: break if pte.ecm < 3.7: continue for sign in signs: choice = chooseD(modekey, pte, sign) if choice != None and passDE(choice, pte): if pte.dmbc[choice] < 1.83: continue dictchoice = outf_s if countDcand(modekey, pte, sign) >= 2: dictchoice = outf_m dictchoice.write('%s %s %s\n' % (pte.dmbc[choice], pte.ecm/2, sign)) nselected += 1 outf_s.close() outf_m.close() sys.stdout.write('Saved as %s \n' %selfile_single) sys.stdout.write('Saved as %s \n' %selfile_multiple) return nselected, ntotal
def main(opts, args): label = args[0] test = opts.test sys.stdout.write('dhad.sel.extlbkgs ...') sys.stdout.flush() datpath = attr.datpath datatype = 'generic' rootfile = tools.get_rootfile(datatype, label=label, test=test) pt = tools.add_rootfile(rootfile) bkg_mcdmodes = { 'kpi' : mcstringtodmode('K+ pi-'), 'kpipi0' : mcstringtodmode('K+ pi- pi0'), 'k3pi' : mcstringtodmode('K+ pi- pi+ pi-'), 'kkspi' : mcstringtodmode('K- K0 pi+'), 'kkspibar' : mcstringtodmode('K+ K0 pi-'), '3pi' : mcstringtodmode('pi+ pi- pi+'), '3pipi0' : mcstringtodmode('pi+ pi- pi+ pi0'), '5pi' : mcstringtodmode('pi+ pi- pi+ pi- pi+'), 'kskspi' : mcstringtodmode('K0 K0 pi+') } bkg_mcdmodes_rev = {} counts = {} files = {} selpath = os.path.join(attr.datpath, 'sel', label, 'extlbkgs') for i in bkg_mcdmodes: bkg_mcdmodes_rev[bkg_mcdmodes[i]] = i counts[i] = 0 selname = 'singtag_bkg_%s.evt' % i if test: selname += '.test' selfile = tools.check_and_join(selpath, selname) files[i] = open(selfile, 'w') # First number is the sign of the D we should look at for the fake signal; # second number is the mode. bkg_dmodes = { 'kpi' : (-1, 0), 'kpipi0' : (-1, 1), 'k3pi' : (-1, 3), 'kkspi' : (1, 3), 'kkspibar' : (-1, 3), '3pi' : (1, 202), '3pipi0' : (1, 203), '5pi' : (1, 204), 'kskspi' : (1, 204) } ensure_other_side_is_not = { 'kpi' : (mcstringtodmode('K+ pi-'),), 'kpipi0' : (mcstringtodmode('K+ pi- pi0'),), 'k3pi' : (mcstringtodmode('K+ pi- pi+ pi-'), mcstringtodmode('K+ K0 pi-')), 'kkspi' : (mcstringtodmode('K- K0 pi+'), mcstringtodmode('K- pi+ pi- pi+')), 'kkspibar' : (mcstringtodmode('K+ K0 pi-'), mcstringtodmode('K+ pi- pi- pi+')), '3pi' : (), '3pipi0' : (), '5pi' : (), 'kskspi' : () } ntotal = 0 nselected = 0 for pte in pt: ntotal += 1 if test and ntotal > 1000: break mcd = makeDDecaySubTree(pte, 1) mcdb = makeDDecaySubTree(pte, -1) mcdmode = mcDmodeFixRad(mcd[0].mcDmode()) mcdbmode = mcDmodeFixRad(mcdb[0].mcDmode()) if (mcdmode in bkg_mcdmodes_rev): mcdmode_string = bkg_mcdmodes_rev[mcdmode] if mcdbmode not in ensure_other_side_is_not[mcdmode_string]: counts[mcdmode_string] += 1 choice = chooseD(bkg_dmodes[mcdmode_string][1], pte, bkg_dmodes[mcdmode_string][0]) if choice != None and passDE(choice, pte): nselected += 1 files[mcdmode_string].write('%g\n' % pte.dmbc[choice]) sys.stdout.write(' selected %s out of %s.\n' % (nselected, ntotal)) sys.stdout.write('Number of mcdmode: %s. \n' % counts) sys.stdout.flush()
def output_multcand(pt, mode, selname, label, test=False): selpath_single = os.path.join(attr.datpath, 'evt', label, 'single_candidate') selfile_single = tools.check_and_join(selpath_single, selname) selpath_multiple = os.path.join(attr.datpath, 'evt', label, 'multiple_candidate') selfile_multiple = tools.check_and_join(selpath_multiple, selname) modekey, sign = tools.get_modekey_sign(mode) if sign == None: if not isinstance(modekey, tuple): raise ValueError(modekey) if modekey[0] in [0, 200]: signs = [-1] if modekey[1] in [0, 200]: signs = [1, -1] modekey = modekey[1] elif modekey[1] in [0, 200]: signs = [1] modekey = modekey[0] else: raise ValueError(modekey) else: signs = [sign] outf_s = open(selfile_single, 'w') outf_m = open(selfile_multiple, 'w') ntotal = 0 nselected = 0 for pte in pt: ntotal += 1 if test and nselected >= 30: break if pte.ecm < 3.7: continue for sign in signs: choice = chooseD(modekey, pte, sign) if choice != None and passDE(choice, pte): if pte.dmbc[choice] < 1.83: continue dictchoice = outf_s if countDcand(modekey, pte, sign) >= 2: dictchoice = outf_m dictchoice.write('%s %s %s\n' % (pte.dmbc[choice], pte.ecm / 2, sign)) nselected += 1 outf_s.close() outf_m.close() sys.stdout.write('Saved as %s \n' % selfile_single) sys.stdout.write('Saved as %s \n' % selfile_multiple) return nselected, ntotal
def output_multspec(reweight, pt, mode, selfile, label, test=False): mode, signs = tools.get_modekey_sign(mode) f = TFile(selfile, 'recreate') load_roofit_lib('signal', label) from ROOT import RooRealVar, RooDataSet, RooArgList, RooArgSet, \ RooGaussian, RooArgusBG, RooCBShape, RooAddPdf, RooPolynomial, \ gSystem, TCanvas efffile = selfile.replace('.root', '.db') effs = shelve.open(efffile) mbc = RooRealVar('mbc', 'Beam constrained mass', 1.83, 1.89, 'GeV') weight2 = RooRealVar('weight2', 'weight', 1) weight3 = RooRealVar('weight3', 'weight', 1) mbc_aset = RooArgSet(mbc) ntrack_d0 = TH1F('ntrack_d0', 'Number of other side tracks for D^{0} #rightarrow K#pi', 10, 0, 9) ntrack_dp = TH1F('ntrack_dp', 'Number of other side tracks for D^{+} #rightarrow K#pi#pi', 10, 0, 9) unweight_sum = 0 reweight_ntr_sum = 0; reweight_npi0_sum = 0 reweight_ntr_mcand_cnt = 0; reweight_npi0_mcand_cnt = 0 unweight_dmbc = TH1F('unweight_dmbc', 'm_{BC}, direct MC', 100, 1.83, 1.89) reweight_ntr_dmbc = TH1F('reweight_ntr_dmbc', 'm_{BC}, track reweighted MC', 100, 1.83, 1.89) reweight_npi0_dmbc = TH1F('reweight_npi0_dmbc', 'm_{BC}, #pi^{0} reweighted MC', 100, 1.83, 1.89) chgpart = (pdgid_pip, pdgid_pim, pdgid_Kp, pdgid_Km, pdgid_mup, pdgid_mum, pdgid_ep, pdgid_em) neupart = (pdgid_KL, pdgid_piz, pdgid_gamma) intpart = chgpart + neupart ntr_denoms = {}; gen_dmbc_ntr = {}; npi0_denoms = {}; gen_dmbc_npi0 = {} for i in range(7+1): ntr_denoms[i] = 0 gen_dmbc_ntr[i] = TH1F('gen_dmbc_ntr_'+`i`, 'm_{BC} for '+`i`+' tracks', 100, 1.83, 1.89) for i in range(5+1): npi0_denoms[i] = 0 gen_dmbc_npi0[i] = TH1F('gen_dmbc_npi0_'+`i`, 'm_{BC} for '+`i`+' #pi^{0}', 100, 1.83, 1.89) ntotal = 0 nselected = 0 for pte in pt: ntotal += 1 if test and nselected > 10: break if test and ntotal > 1000: break if pte.ecm < 3.7: continue othsidetrk = 0; othsidepi0 = 0 dtree = makeDDecaySubTree(pte, -1) ddesc = dtree[0].interestingDescendants(intpart, False, False) for i in ddesc: if i.pdgid in chgpart: othsidetrk += 1 if (i.pdgid in chgpart and i.parent.pdgid == pdgid_KS and len(i.parent.daughters) == 1): # This absurd special case is due to GEANT's "K_S -> pi" decay othsidetrk += 1 if i.pdgid == pdgid_piz: othsidepi0 += 1 if (i.pdgid == pdgid_KS) and len(i.daughters) != 2: print i if mode >= 200: mod2 = 1 else: mod2 = 0 if (othsidetrk % 2) != mod2: print '-----------------------------------', othsidetrk print chgpart # for node in makeDecayTree(pte): # pass # if len(node.daughters) == 0 and node.pdgid in chgpart: # print node choice = chooseD(mode, pte, 1) if choice != None and not passDE(choice, pte): choice = None unweight_sum += 1 index_tr = getindex(othsidetrk, ntr_denoms) ntr_denoms[index_tr] += 1 index_pi0 = getpi0index(othsidepi0, npi0_denoms) npi0_denoms[index_pi0] += 1 final_weight_vector_ntr = attr.multiplicity_weight_ntr[reweight] initial_weight_vector_ntr = attr.multiplicity_weight_ntr['init'] final_weight_vector_npi0 = attr.multiplicity_weight_npi0[reweight] initial_weight_vector_npi0 = attr.multiplicity_weight_npi0['init'] reweight_ntr_sum += (final_weight_vector_ntr[index_tr]/ initial_weight_vector_ntr[index_tr]) reweight_npi0_sum += (final_weight_vector_npi0[index_pi0]/ initial_weight_vector_npi0[index_pi0]) if choice != None: nselected += 1 unweight_dmbc.Fill(pte.dmbc[choice]) reweight_ntr_dmbc.Fill(pte.dmbc[choice], (final_weight_vector_ntr[index_tr]/ initial_weight_vector_ntr[index_tr])) reweight_npi0_dmbc.Fill(pte.dmbc[choice], (final_weight_vector_npi0[index_pi0]/ initial_weight_vector_npi0[index_pi0])) gen_dmbc_ntr[index_tr].Fill(pte.dmbc[choice]) gen_dmbc_npi0[index_pi0].Fill(pte.dmbc[choice]) mbc.setVal(pte.dmbc[choice]) effs['npi0_denom'] = npi0_denoms effs['ntr_denom'] = ntr_denoms effs['unweight_sum'] = unweight_sum effs['reweight_ntr_sum'] = reweight_ntr_sum effs['reweight_npi0_sum'] = reweight_npi0_sum effs.close() #sys.stdout.write('Saved %s \n' %efffile) f.Write() f.Close() pt.Delete() return nselected, ntotal