-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale-datasets.py
executable file
·154 lines (129 loc) · 4.81 KB
/
scale-datasets.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python
import sys
import os
import glob
import re
import subprocess
import string
import argparse
parser = argparse.ArgumentParser(description='Combine ROOT files according to the process cross section.')
parser.add_argument('--dbfile', required=True)
#targetLumi = 30010.6 # inb
parser.add_argument('--targetLumi', type=float, required=True, help='Target lumi for scaling, in the units corresponding to the dbfile')
parser.add_argument('--refh', default='rc20_45/exmu30000/cuts_p', help='A cuts_p histogram')
parser.add_argument('--refbin', default=1, type=int, help='N before cuts bin in the cuts_p histogram')
parser.add_argument('datasets', nargs='+', help='List of *output* dataset dirs - input components are looked for underneath each of the dirs')
opt = parser.parse_args()
#print 'got: ',opt
comment = r"^\s*#"
#================================================================
def getDSNum(fn):
pat = r"^.*user\.andr\.(\d{6})\..*$"
m = re.match(pat, fn)
if m:
return int(m.group(1))
raise Exception('ERROR parsing file name for (project,dsnum,gentag): "'+fn+'"')
#================================================================
print "Reading dbfile ",opt.dbfile
xsec = {}
geneff = {}
f = open(opt.dbfile, 'r')
for line in f:
m = re.match(comment, line)
if not m:
line = line.rstrip()
if line != "":
(dsnum, xs, eff, name) = string.split(line, None, 4)
dsnum = int(dsnum)
xsec[dsnum] = float(xs)
geneff[dsnum] = float(eff)
#print "Got: dsnum = ",dsnum," xsec = ", xsec[dsnum], ", geneff = ",geneff[dsnum]
pass
pass
f.close()
del(f)
#print "xsec = ",xsec
for ds in opt.datasets:
outfile = ds + "/" + os.path.basename(ds).replace("_AGRS","") + ".lumi"+str(opt.targetLumi).replace('.', '_')+".ms.root"
print "Producing ",outfile
infiles = glob.glob(ds + "/*/*.mh.root")
xsfilepairs = []
for fn in infiles:
dsnum = getDSNum(fn)
effxs = xsec[dsnum] * geneff[dsnum]
#print "dsnum = ",dsnum,", effxs = ", effxs, ", for ",fn
xsfilepairs += [ str(effxs), fn ]
pass
print "Got xsfilepairs = ",xsfilepairs
try:
os.unlink(outfile)
except OSError as (errno, strerror):
pass
try:
retcode = subprocess.call([#'/bin/echo',
'/home/f14/andr/scripts/agxmerge',
'--targetLumi', str(opt.targetLumi),
'--refh', opt.refh,
'--refbin', str(opt.refbin),
'--out', outfile
] + xsfilepairs)
if retcode != 0:
print >>sys.stderr, "Child return code ", retcode
sys.exit(1)
pass
except OSError, e:
print >>sys.stderr, "Execution failed:", e
pass # try-subprocess
pass # ds in datasets
#for ds in list:
#
#
# m = re.match(comment, ds)
# if not m:
# ds = ds.rstrip()
# if ds != "":
# outfile = ds + "/" + ds.rstrip("/").replace("_AGRS","") + ".mh.root"
# #print "Merging dataset: ",ds," out = ",outfile
# infiles = glob.glob(ds + "/*/*.root*")
#
# if len(infiles) > 0:
# print "Preparing ", outfile
# try:
# #print "Removing old merged file"
# os.unlink(outfile)
# # Requiring os.access(outfile, os.F_OK)
# # would leave hanging symlinks not removed.
# # Use the try/catch to do the "rm -f"
# except OSError as (errno, strerror):
# #print "OS error({0}): {1}".format(errno, strerror)
# pass
#
# if len(infiles) == 1:
# #print "Linking"
# os.link(infiles[0], outfile)
# else:
# #print "merging out=",outfile,", in = ",infiles
# try:
# retcode = subprocess.call([ # '/bin/echo',
# '/home/f14/andr/scripts/agmerge',
# outfile ] + infiles)
# if retcode != 0:
# print >>sys.stderr, "Child return code ", retcode
# sys.exit(1)
# pass
#
# except OSError, e:
# print >>sys.stderr, "Execution failed:", e
# pass # try-subprocess
#
# pass # len(infiles)
# else:
# print "WARNING: no input files for ",ds
# pass
#
# pass
# pass
# pass
#
#print "Done"
##EOF