forked from rootpy/rootpy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroot-friend
More file actions
executable file
·78 lines (69 loc) · 2.44 KB
/
Copy pathroot-friend
File metadata and controls
executable file
·78 lines (69 loc) · 2.44 KB
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
#!/usr/bin/env python
import sys
from optparse import OptionParser
import os
import re
def fatal():
print "Incorrect arguments"
sys.exit(1)
parser = OptionParser()
options, args = parser.parse_args()
import ROOT
from rootpy.common import getTrees
if len(args) == 1:
file = ROOT.TFile.Open(args[0])
trees = getTrees(file)
for tree in trees:
print "%s (%i entries)"%(tree.GetName(),tree.GetEntries())
friends = tree.GetListOfFriends()
if len(friends)>0:
print "\tfriends:"
for friend in friends:
path = friend.GetTitle()
if os.path.exists(path):
file2 = ROOT.TFile.Open(path)
friendTree = file2.Get(friend.GetTreeName())
print "\t%s (%i entries) in %s"%(friend.GetTreeName(),friendTree.GetEntries(),path)
else:
print "\t%s in %s %s"%(friend.GetTreeName(),path,"(file does not exist!)")
file.Close()
elif len(args) == 2:
pattern = re.compile('^[\S]+ unfriend$')
if not re.match(pattern," ".join(args)):
fatal()
file = ROOT.TFile.Open(args[0],"update")
trees = getTrees(file)
for tree in trees:
for friend in tree.GetListOfFriends():
friendFile = ROOT.TFile.Open(friend.GetTitle())
friendTree = friendFile.Get(friend.GetTreeName())
print "removing friend"
tree.RemoveFriend(friendTree)
friendFile.Close()
file.cd()
tree.Write("",ROOT.TObject.kOverwrite)
file.Close()
elif len(args) == 3:
pattern = re.compile('^[\S]+ with [\S]+$')
if not re.match(pattern," ".join(args)):
fatal()
file1 = ROOT.TFile.Open(args[0],"update")
file2 = ROOT.TFile.Open(args[2])
trees = getTrees(file1)
for tree in trees:
friends = [(friend.GetTreeName(),friend.GetTitle()) for friend in tree.GetListOfFriends()]
if (tree.GetName(),args[2]) in friends:
print "tree %s in %s is already a friend, skipping..."%(tree.GetName(),args[2])
continue
refTree = file2.Get(tree.GetName())
if refTree:
tree.AddFriend(tree.GetName(),args[2])
print "adding friend tree %s in %s"%(tree.GetName(),args[2])
file1.cd()
tree.Write("",ROOT.TObject.kOverwrite)
else:
print "Warning: tree %s not found in %s"%(tree.GetName(),args[2])
file1.Close()
file2.Close()
else:
fatal()