import sys
import ROOT
from progressbar import Bar, Percentage, ProgressBar
from time import time
from tools import duration, check_outfile_path
ECMS = 3.686
p4shw = ROOT.vector('double')()
def main ():
args = sys.argv[1:]
if (len(args) < 2):
print 'input error'
infile = args[0]
outfile = args[1]
check_outfile_path(outfile)
fin = ROOT.TFile(infile)
t = fin.Get('ana')
t.SetBranchAddress("p4shw", p4shw)
entries = t.GetEntriesFast()
fout = ROOT.TFile(outfile, "RECREATE")
t_out = ROOT.TTree("ana","ana")
rec_mass_gam1 = ROOT.vector('double')()
rec_mass_gam2 = ROOT.vector('double')()
t_out.Branch("rec_mass_gam1", rec_mass_gam1, "rec_mass_gam1/D")
t_out.Branch("rec_mass_gam2", rec_mass_gam2, "rec_mass_gam2/D")
pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=entries).start()
time_start = time()
print("checking error 2")
cms_p4 = ROOT.TLorentzVector(0.011*ECMS, 0, 0, ECMS)
print 'entries=', entries
print("checking error 3")
for k in range(entries):
pbar.update(k+1)
#t.GetEntry(k)
print("indentent error checking")
#exit()
p4shw_gam1 = ROOT.TLorentzVector(t.p4shw[0],t.p4shw[1],t.p4shw[2],t.p4shw[3])
p4shw_gam2 = ROOT.TLorentzVector(t.p4shw[4],t.p4shw[5],t.p4shw[6],t.p4shw[7])
print("checking error 4")
p4_shw_gam1 = cms_p4 - p4shw_gam1
p4_shw_gam2 = cms_p4 - p4shw_gam2
rec_mass_gam1 = p4_shw_gam1.M()
rec_mass_gam2 = p4_shw_gam2.M()
print("rec_mass_gam1", rec_mass_gam1)
#exit()
t_out.Fill()
print("checking error 5")
t_out.Write()
fout.Close()
pbar.finish()
dur = duration(time()-time_start)
sys.stdout.write(' \nDone in %s. \n' % dur)
print("checking error 6")
if __name__ =='__main__':
main()
问问题
1185 次
1 回答
0
当我将您的代码与此示例进行比较时,您使用的是ROOT.vector
而不是array
. 当我执行此更改时,分支会按预期填充
#!/bin/python
import ROOT
from array import array
# doesn't work
def test1():
t_out = ROOT.TTree("ana", "ana")
rec_mass_gam1 = ROOT.vector('double')()
t_out.Branch("rec_mass_gam1", rec_mass_gam1, "rec_mass_gam1/D")
rec_mass_gam1 = 1337.
t_out.Fill()
t_out.Draw("rec_mass_gam1")
# works
def test2():
t_out = ROOT.TTree("ana", "ana")
rec_mass_gam1 = array('f', [0.])
t_out.Branch("rec_mass_gam1", rec_mass_gam1, "rec_mass_gam1/F")
rec_mass_gam1[0] = 1337.
t_out.Fill()
t_out.Draw("rec_mass_gam1")
当我运行 test1 时,我看到树和分支被填满了,只是没有达到我想要的值。在第二个示例中,所需的值被填充。
现在仔细看看会发生什么,无论如何你的脚本中有一个错误:
python不会将rec_mass_gam1 = p4_shw_gam1.M()
向量变量的值视为“将向量变量的值设置为rec_mass_gam1
退出M()
方法的数字。而是创建一个具有名称的新浮点变量,rec_mass_gam1
并且保留原始向量变量(由分支使用)不变。
我不得不承认我不知道是否也有办法填充分支vector
。
于 2019-03-21T11:17:33.940 回答