-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreefriend.py
118 lines (91 loc) · 2.09 KB
/
treefriend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import ROOT
import os,sys
if len(sys.argv)!=6:
print "Example: python treefriend.py mt2baby.root mt2 stbaby.root myTree mt2stbaby.root"
exit(1)
ffn = sys.argv[1]
ftn = sys.argv[2]
sfn = sys.argv[3]
stn = sys.argv[4]
ofn = sys.argv[5]
otn = "mt2st"
ott = "A mt2 baby-tree edition"
otstn = "st"
otstt = "A short-track baby-tree edition"
ff = ROOT.TFile(ffn, "READ")
ft = ff.Get(ftn).Clone()
Nf = ft.GetEntries()
sf = ROOT.TFile(sfn, "READ")
st = sf.Get(stn).Clone()
Ns = ft.GetEntries()
fr=[]
fl=[]
fe=[]
fi=[]
for index in range(0,Nf):
ft.GetEntry(index)
fr.append(ft.run)
fl.append(ft.lumi)
fe.append(ft.evt)
fi.append(index)
fzip = zip(fr,fl,fe,fi)
sr=[]
sl=[]
se=[]
si=[]
for index in range(0,Ns):
st.GetEntry(index)
sr.append(st.run)
sl.append(st.lumi)
se.append(st.evt)
si.append(index)
szip = zip(sr,sl,se,si)
fii = []
sii = []
lastY = 0
for x in xrange(len(fzip)):
for y in xrange(len(szip)):
if y<=lastY and lastY>0:
continue
if fzip[x][0]==szip[y][0] and fzip[x][1]==szip[y][1] and fzip[x][2]==szip[y][2]:
fii.append(fzip[x][3])
sii.append(szip[y][3])
lastY=y
break
else:
if fzip[x][0]<szip[y][0]:
break
elif fzip[x][0]==szip[y][0] and fzip[x][1]<szip[y][1]:
break
elif fzip[x][0]==szip[y][0]and fzip[x][1]==szip[y][1] and fzip[x][2]<szip[y][2]:
break
else:
continue
felist = ROOT.TEventList()
selist = ROOT.TEventList()
if len(fii) != len(sii):
print "Nope, something went wrong :("
exit(1)
for x in xrange(len(fii)):
felist.Enter(fii[x])
selist.Enter(sii[x])
ft.SetEventList(felist)
st.SetEventList(selist)
of = ROOT.TFile(ofn, "RECREATE")
ot = ft.CopyTree("")
del ft
ff.Close()
st.SetBranchStatus("run", False)
st.SetBranchStatus("lumi", False)
st.SetBranchStatus("evt", False)
otst = st.CopyTree("")
otst.SetName(otstn)
otst.SetTitle(otstt)
del st
sf.Close()
ot.SetName(otn)
ot.SetTitle(ott)
of.cd()
ot.Write()
otst.Write()
of.Close()