[英]How to fill the branches of a tree?
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()
當我將您的代碼與此示例進行比較時,您使用的是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
填充分支。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.