forked from mflechl/ProductionFromNano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeSamples.py
277 lines (206 loc) · 10.5 KB
/
mergeSamples.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import ROOT as R
import json
import os
import glob
import shutil
import subprocess as sp
import multiprocessing as mp
import shlex
import sys
import numpy as np
import argparse
from datetime import datetime
from runUtils import checkProxy, checkTokens, getSystem, getHeplxPublicFolder
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-v', dest='version', help='Version of the merged samples')
parser.add_argument('-c', dest='channel', help='Dataset channel',choices = ['mt','et','tt'], default = 'mt')
parser.add_argument('-s', dest='stitch', help='Stitch samples',action="store_true")
args = parser.parse_args()
if not checkTokens(): sys.exit()
M = Merger(version=args.version, channel = args.channel)
print "1"
M.createSamples()
if args.stitch: M.mergeSamples()
class Merger():
def __init__(self, force=False, version="v1", channel = "mt"):
self.force = force
self.version = version
self.channel = channel
if "cern" in getSystem():
print "Merging from lxplus - You should switch to heplx to run faster."
self.logpath = "/".join([ getHeplxPublicFolder(),"submit_log.log" ])
with open(self.logpath,"r") as FSO:
self.log = json.load(FSO)
self.outdir = "/afs/hephy.at/data/higgs02/"
self.hephy_redirectory = "hephyse.oeaw.ac.at/"
self.pathToCondorProductionNtuples = "/dpm/oeaw.ac.at/home/cms/store/user/jaandrej/condor_production"
self.indir = "srm://hephyse.oeaw.ac.at//dpm/oeaw.ac.at/home/cms/store/user/jaandrej/condor_production"
self.samples = self.collectFilesDPM()
#self.samples = self.collectFiles()
self.mergekeys = self.mapCompletedJobs()
#print self.mergekeys
def __del__(self):
with open(self.logpath,"w") as FSO:
json.dump(self.log, FSO, indent=2)
# not needed since it is in bookkeeping.py and was just needed needed to adapt collectFromDPM where we do not need the timestamp info
# def lsDPM(self, sample, shift):
# print "gfal-ls -l --time-style long-iso {0}/{1}".format(self.indir, sample)
# exit(0)
# proc = sp.Popen( shlex.split( "gfal-ls -l --time-style long-iso {0}/{1}".format(self.indir, sample) ), stdout=sp.PIPE )
# (out, err) = proc.communicate()
# files = []
# for file in out.splitlines():
# #print file
# if ".root" in file and "{0}-{1}".format(self.channel, shift) in file:
# file = [i for i in file.split(" ") if i]
# # Need to get UTC under control ....
# timestamp = int( datetime.strptime(" ".join( [file[5], file[6]] ),"%Y-%m-%d %H:%M").strftime("%s") ) + 7200
# #print (timestamp, "{0}/{1}/{2}".format(self.indir, sample, file[-1] ) )
# files.append( (timestamp, "{0}/{1}/{2}".format(self.indir, sample, file[-1] ) ) )
# return files
def createSamples(self):
for m in self.mergekeys:
mergedir = "/".join([self.outdir,m[0],self.version])
#print mergedir
if not os.path.exists(mergedir):
os.makedirs(mergedir)
outfile = "/".join([mergedir, "{1}-{2}_{0}.root".format( *m ) ])
#print outfile
if os.path.exists(outfile):
shutil.rmtree(outfile, ignore_errors=True)
print "\033[1m{0}\033[0m: {1}-{2}".format(*m)
# Fallback
# os.system("hadd -f {0} {1}".format(outfile, " ".join(self.samples[m[0]][m[1]][m[2]]["files"]) ) )
self.combineFiles(outfile, mergekey=m)
def combineFiles(self, outfile, mergekey):
m = mergekey
FM = R.TFileMerger()
FM.OutputFile(outfile)
success = True
print "Adding files for merging... "
for f in self.samples[m[0]][m[1]][m[2]]["files"] :
#f = f.replace("srm:","root:") # when gfal-ls was still in use
f = "root://{redir}/{file}".format(redir=self.hephy_redirectory,file=f)
if not FM.AddFile(f,False):
success = False
break
if success:
if not FM.Merge():
print "\033[0;31mProblem during merge...\033[0m\n"
else:
print "\033[0;32mMerge successful!\033[0m\n"
self.log[m[0]][m[1]][m[2]]["status"] = "DONE"
def mergeSamples(self):
print "hi"
with open("stitchConfig.json","r") as FSO:
stitch_config = json.load(FSO)
cmd_list = {}
mergedir = "/".join([self.outdir,self.version])
print "mergedir: {}".format(mergedir)
if not os.path.exists(mergedir):
os.makedirs(mergedir)
for mergename in stitch_config:
# if mergename == "BASIS_ntuple_VBF":
tomerge = []
for sample in stitch_config[mergename]["samples"]:
for ext in glob.glob( "/".join([ self.outdir, sample + "*", self.version, self.channel + "-*" ]) ):
tomerge.append(ext)
mergecmds = self.getMergeCmds(mergename, tomerge)
if mergecmds:
for mergecmd in mergecmds:
os.system(mergecmd)
# cmd_list[mergename] = mergecmd
# self.applyCmdMulti(cmd_list)
def getMergeCmds(self, name, parts):
mergedir = "/".join([self.outdir,self.version])
shifts = ["NOMINAL"]
for es in ["TES","MES","EES"]:
for dm in ["1p0p0","1p1p0","3p0p0"]:
for sh in ["Up","Down"]:
shifts.append( es + dm + sh )
mergeCmds = []
for shift in shifts:
filename = "-".join([ self.channel, name.replace("BASIS",shift) ]) + ".root"
if "_SingleElectron" in filename: filename = filename.replace("SingleElectron","Data")
if "_SingleMuon" in filename: filename = filename.replace("SingleMuon","Data")
if "_Tau" in filename: filename = filename.replace("Tau","Data")
outfile = "/".join([mergedir, filename ])
addfiles = []
for i,file in enumerate(parts):
if "{0}-{1}".format( self.channel, shift ) in file:
addfiles.append(file)
if addfiles:
mergeCmds.append("hadd -f {0} {1}".format(outfile, " ".join( addfiles ) ) )
return mergeCmds
def applyCmdMulti(self, cmd_list, max_proc=8):
done_queue = mp.Queue()
for i, mergename in enumerate(cmd_list):
print "Stitchhing: " + mergename
if i >= max_proc:
done_queue.get(block=True)
proc = mp.Process(target=self.exec_cmd, args=(cmd_list[mergename], done_queue ))
proc.start()
def exec_cmd(self, cmd, q):
shlCmd = shlex.split(cmd)
# print shlCmd
p = sp.Popen(shlCmd,stdout = sp.PIPE, stderr = sys.__stderr__, shell=False)
p.communicate()
q.put(object)
def mapCompletedJobs(self):
mergekeys = []
samples = self.log.keys()
samples.sort()
for sample in samples:
for channel in self.log[sample]:
for shift in self.log[sample][channel]:
if self.log[sample][channel][shift]["status"] == "MERGE" or (self.force and self.log[sample][channel][shift]["status"] == "DONE"):
mergekeys.append((sample,channel,shift))
return np.array(mergekeys)
def collectFilesDPM (self):
samples = {}
# xrdfs srm://hephyse.oeaw.ac.at/ ls /dpm/oeaw.ac.at/home/cms/store/user/jaandrej/
#print "gfal-ls {0}".format(self.indir) # does not work on heplx anymore
#print shlex.split( "xrdfs {redir} ls {path}".format(redir=self.hephy_redirectory,path=self.pathToCondorProductionNtuples) )
proc = sp.Popen( shlex.split( "xrdfs {redir} ls {path}".format(redir=self.hephy_redirectory,path=self.pathToCondorProductionNtuples) ) , stdout=sp.PIPE )
(out, err) = proc.communicate()
#files = []
for samplename in out.splitlines():
if ".root" in samplename : continue # This is a root file not a sample directory
split_samplename = samplename.split("/")[-1] # changes because xrdfs does list the whole path to the directory not just from the parent one
samples[split_samplename] = {}
# proc = sp.Popen( shlex.split( "gfal-ls {0}/{1}".format(self.indir,samplename) ), stdout=sp.PIPE ) # does not work on heplx anymore
proc = sp.Popen( shlex.split( "xrdfs {redir} ls {sample}".format(redir=self.hephy_redirectory,path=self.pathToCondorProductionNtuples,sample=samplename) ), stdout=sp.PIPE )
(out2, err2) = proc.communicate()
for file in out2.splitlines() :
root_file = file.split("/")[-1]
channel = root_file.split("-")[0]
shift = root_file.split("_")[0].replace(channel+"-","")
if not samples[split_samplename].get(channel,False): samples[split_samplename][channel] = {}
if not samples[split_samplename][channel].get(shift,False): samples[split_samplename][channel][shift] = {"files":[]}
samples[split_samplename][channel][shift]["files"].append( "{0}/{1}".format(samplename,root_file))
# print samplename
# print root_file
# print channel
# print shift
# print "-"*80
return samples
def collectFiles(self):
samples = {}
for sample in glob.glob(self.outdir + "*"):
samplename = sample.replace(self.outdir,"")
samples[samplename] = {}
for file in glob.glob( "/".join([ sample, "*"]) ):
root_file = file.split("/")[-1]
channel = root_file.split("-")[0]
shift = root_file.split("_")[0].replace(channel+"-","")
if not samples[samplename].get(channel,False): samples[samplename][channel] = {}
if not samples[samplename][channel].get(shift,False): samples[samplename][channel][shift] = {"files":[]}
samples[samplename][channel][shift]["files"].append( "/".join([sample,root_file]))
print samplename
print channel
print shift
print "/".join([sample,root_file])
return samples
if __name__ == '__main__':
main()