-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampleInfo.py
executable file
·202 lines (163 loc) · 6.8 KB
/
sampleInfo.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
#!/usr/bin/env python3
from os import listdir, makedirs, path, system
#import numpy as np
import sys
import pickle as pkl
import subprocess
import uproot
xs_150_250 = {
"2016_DYnJ": 88,
"2017_DY1J": 9.7,
"2017_DY2J": 16.0,
}
xs_250_400 = {
#"2016_DYnJ": 3.13, # TuneCUET8M1
"2016_DYnJ": 3.67, # TuneCP5
"2017_DY1J": 1.13,
"2017_DY2J": 2.71,
}
def getRootFilesFromPath(d, lim=None):
import subprocess
if "xrootd" in d:
sp = d.split("/")
siteIP = "/".join(sp[0:4])
pathToFiles = "/".join(sp[3:-1])+"/"
allfiles = str(subprocess.check_output(["xrdfs", siteIP, "ls", pathToFiles]), 'utf-8').split("\n")
#rootfiles = [siteIP+'/'+f for i,f in enumerate(allfiles) if f.endswith(".root") and (lim==None or i<lim)]
else:
siteIP = ""
pathToFiles = d
allfiles = [path.join(d, f) for i,f in enumerate(listdir(d)) if f.endswith(".root")]
rootfiles = []
for file_or_dir in allfiles:
# print(file_or_dir)
if (file_or_dir == "" or file_or_dir == pathToFiles): continue
file_or_dir = siteIP + file_or_dir
if file_or_dir.endswith(".root"):
if lim==None or len(rootfiles)<lim:
rootfiles.append(file_or_dir)
elif not "log" in file_or_dir and not file_or_dir[-1]=='/' and len(rootfiles)<lim:
file_or_dir=file_or_dir+'/'
rootfiles.extend(getRootFilesFromPath(file_or_dir, lim-len(rootfiles)))
# print("Input path:", d)
# print("List of root files to be processed:\n",rootfiles)
return rootfiles
def getFilesFromDas(ds, instance):
allfiles = str(subprocess.check_output(str('/cvmfs/cms.cern.ch/common/dasgoclient -query="instance=%s file dataset=%s"'%(instance, ds.strip())),
shell=True), 'utf-8').split("\n")
#print(ds, allfiles)
return allfiles
def makeDasQueryAndPickle(inpfile, pklFileName="./FilesOnDas.pkl"):
allFiles = {}
with open(inpfile, mode='r') as in_file:
for line in in_file:
if line.startswith('#') or line.strip()=="":
#print("we skip this line:", line)
continue
sample = line.strip()
dsname = sample.split("/")[1]
Tier = sample.split("/")[3] # NANOAODSIM for regular samples, USER for provate
instance="prod/global"
if Tier=="USER":
instance="prod/phys03"
print("Getting file list for sample=%s, ds=%s, tier=%s, instance=%s"%(sample,dsname,Tier,instance))
if dsname in allFiles.keys():
allFiles[dsname].extend(getFilesFromDas(sample,instance))
else:
allFiles[dsname] = getFilesFromDas(sample,instance)
print("Found %i files"%len(allFiles[dsname]))
#
# print(allFiles)
pkl.dump( allFiles, open(pklFileName, 'wb') )
print("The files are written to ", pklFileName)
return allFiles
def ReadSampleInfoFile(inpfile):
info = {}
with open(inpfile, mode='r') as in_file:
for line in in_file:
sampleInfo = {}
name = None
if not line.startswith('name'): continue
for item in line.split():
# print("Item: ", item)
if "=" not in item: continue
key,value=item.split("=")
if key=="name":
name=str(value)
if key=="dir":
sampleInfo['dataset'] = str(value).strip("/")
if key=="type":
sampleInfo["type"]=int(value)
if key=="xsec":
sampleInfo["xsec"]=float(value)
if key=="kfac":
sampleInfo["kfac"]=float(value)
info[name] = sampleInfo
#
# print(info)
return info
def makeListOfInputRootFilesForProcess(proc_name, sampleInfo, listOfFilesPkl, xroot, lim=None, checkOpen=False):
if proc_name not in sampleInfo.keys():
print("This process does not exist on the books:", proc_name)
sys.exit(1)
dsname = sampleInfo[proc_name]["dataset"]
print("%s process has the dataset name %s"%(proc_name, dsname) )
file_list = pkl.load(open(listOfFilesPkl,'rb'))
if dsname not in file_list.keys():
print("This dataset name does not exist on the books:", dsname)
return []
allfiles = file_list[dsname]
# print("all files for sample %s: \n"%dsname, allfiles)
rootfiles = []
for f in allfiles:
if f.strip()=='': continue
fname = xroot+f
if checkOpen:
try:
with uproot.open(fname) as up:
#print("Uproot can open file, ", fname)
rootfiles.append(fname)
except:
print("No, we can't open the file: ", fname)
pass
else:
rootfiles.append(fname)
if lim!=None and len(rootfiles)==lim:
#print("It's enough files")
break
#print(rootfiles)
return rootfiles
if __name__ == "__main__":
print("This is the __main__ part")
import argparse
parser = argparse.ArgumentParser(description='Run das quiries etc')
parser.add_argument("inputfile")
parser.add_argument('-o','--output', type=str, default="./FilesOnDas.pkl", help="Directory to output the plots.")
opt = parser.parse_args()
print(opt)
if ".pkl" in opt.inputfile:
full_file_list = pkl.load(open(opt.inputfile,'rb'))
else:
full_file_list = makeDasQueryAndPickle(opt.inputfile, opt.output)
print("All samples with query:", full_file_list.keys())
#sampleInfo = ReadSampleInfoFile('2L_samples_2017_vhcc.txt')
#sampleInfo = ReadSampleInfoFile('mc_2016_vhcc.conf')
sampleInfo = ReadSampleInfoFile('mc_vjets_samples.info')
print("all processes:", sampleInfo.keys())
#xroot = 'root://xrootd-cms.infn.it/'
#xroot = 'root://cms-xrd-global.cern.ch/'
xroot = 'root://grid-cms-xrootd.physik.rwth-aachen.de/'
pkl_file = "./VJetsPickle.pkl"
#file_list = makeListOfInputRootFilesForProcess("DYJets_inc_FXFX", sampleInfo, pkl_file, xroot, lim=2, checkOpen=True)
#file_list = makeListOfInputRootFilesForProcess("DYJets_inc_MLM", sampleInfo, pkl_file, xroot, lim=2, checkOpen=True)
file_list = makeListOfInputRootFilesForProcess("DYJets_inc_FXFX", sampleInfo, pkl_file, xroot, lim=2, checkOpen=True)
print(file_list)
"""
pkl = "./FilesOnDas_2016.pkl"
file_list = makeListOfInputRootFilesForProcess("W1Jets-Pt50To150", sampleInfo, pkl, xroot, 2)
print(file_list)
file_list = makeListOfInputRootFilesForProcess("TT_DiLep", sampleInfo, pkl, xroot, 2)
print(file_list)
file_list = makeListOfInputRootFilesForProcess("WZTo3L1Nu", sampleInfo, pkl, xroot, 2)
print(file_list)
"""