forked from rootpy/rootpy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroot-cp
More file actions
executable file
·99 lines (85 loc) · 2.78 KB
/
Copy pathroot-cp
File metadata and controls
executable file
·99 lines (85 loc) · 2.78 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
from optparse import OptionParser
import sys
import os
import re
parser = OptionParser()
options,args = parser.parse_args()
from rootpy.io.file import VALIDPATH
validpath = re.compile(VALIDPATH)
if len(args) == 1:
sys.exit("too few arguments")
target = args[-1]
sources = args[:-1]
match = re.match(validpath,target)
if match:
targetfilename = match.group('file')
targetpath = match.group('path')
if not targetpath:
targetpath = ''
else:
sys.exit("target %s is not a valid path"% target)
for source in sources:
match = re.match(validpath,source)
if not match:
sys.exit("source %s is not a valid path"% source)
import ROOT
from ROOT import TFile
targetFile = TFile.Open(targetfilename,'update')
if not targetFile:
sys.exit("cannot open target file %s"% targetfilename)
targetpathname = os.path.dirname(targetpath)
targettreename = os.path.basename(targetpath)
if targetFile.GetDirectory(targetpath):
targetpathname = targetpath
targettreename = ''
elif not targetFile.GetDirectory(targetpathname):
targetFile.Close()
sys.exit("path %s not found in target %s"%(targetpathname,targetfilename))
if targettreename and len(sources)>1:
targetFile.Close()
sys.exit("you specified a target tree while listing multiple sources")
for source in sources:
match = re.match(validpath,source)
if match:
sourcefilename = match.group('file')
sourcepath = match.group('path')
if not sourcepath:
sourcepath = ''
else:
targetFile.Close()
sys.exit("source %s is not a valid path"% source)
sourcepathname = os.path.dirname(sourcepath)
sourcetreename = os.path.basename(sourcepath)
if not sourcetreename:
targetFile.Close()
sys.exit("no tree specified in source %s"%source)
f = TFile.Open(sourcefilename)
if not f:
targetFile.Close()
f.Close()
sys.exit("file %s will not open"%filename)
if not f.GetDirectory(sourcepathname):
targetFile.Close()
f.Close()
sys.exit("path %s not found in source %s"%(sourcepathname,sourcefilename))
f.cd(sourcepathname)
tree = f.Get(sourcepath)
targetFile.cd(targetpathname)
if tree:
if not isinstance(tree,ROOT.TTree):
targetFile.Close()
sys.exit("object at %s in source %s is not a TTree"%(sourcepath,sourcefilename))
newTree = tree.CloneTree(-1,'fast')
if targettreename:
targetName = targettreename
else:
targetName = sourcetreename
newTree.SetName(targetName)
newTree.Write('',ROOT.TObject.kOverwrite)
else:
targetFile.Close()
f.Close()
sys.exit("%s not found in %s"%(sourcetreename,sourcefilename))
f.Close()
targetFile.Close()