forked from TreeMaker/TreeMaker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlumiSummary.py
171 lines (143 loc) · 6.65 KB
/
lumiSummary.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
import os
import subprocess
from FWCore.PythonUtilities.LumiList import LumiList
from itertools import izip
import array
from multiprocessing import Pool
from optparse import OptionParser
from XRootD import client
import imp
# ------------------------------------------------------------
# ------------------------------------------------------------
# ------------------------------------------------------------
class sampleInfo :
'''
Example Use:
from lumiSummary import sampleInfo
si = sampleInfo("data2016Lumi/","/store/user/lpcsusyhad/SusyRA2Analysis2015/Run2ProductionV15/",["Run2016B-17Jul2018_ver1-v1.HTMHT_*"])
'''
def __init__( self , outName, baseDir, fileNames ) :
self.fileNames = fileNames
self.outName = outName
self.fileList = []
if baseDir[-1]!="/": baseDir = baseDir+"/"
for f in fileNames:
self.addToList(baseDir,f)
def addToList( self, baseDir, fileName ):
redirector = "root://cmseos.fnal.gov/"
xrdfs = client.FileSystem(redirector)
dirToList = baseDir
patternToMatch = fileName
# detect folderized mode
if "/" in fileName:
dirToList = baseDir+os.path.dirname(fileName)
patternToMatch = os.path.basename(fileName)
if dirToList[-1]!="/": dirToList = dirToList+"/"
patternToMatch = patternToMatch.strip('*')
status, listing = xrdfs.dirlist(dirToList)
if status.status != 0:
raise Exception("XRootD failed to stat %s%s" % (str(xrdfs.url),baseDir))
files = [redirector+dirToList+entry.name for entry in listing if patternToMatch in entry.name]
self.fileList.extend(files)
def __repr__(self):
return "%s(%r, %r)" % (self.__class__.__name__, self.outName,self.fileNames)
def __str__(self):
dict_of_members = self.__dict__
list_of_keys = dict_of_members.keys()
list_of_values = dict_of_members.values()
max_len_keys = max(len(str(x)) for x in list_of_keys)
min_len_values = min(len(str(x)) for x in list_of_values)
key_format="{:<"+str(max_len_keys)+"}"
value_format="{:<"+str(min_len_values)+"}"
rep_format = "sampleInfo("
for ikey, key in enumerate(list_of_keys):
if ikey != len(list_of_keys)-1:
rep_format += (key_format+": "+value_format)
rep_format += ("\n{:<11}")
else:
rep_format += (key_format+": {:<"+str(len(str(self.fileNames)))+"}")
rep_format += ")"
return rep_format.format('outName',self.outName,' ','fileNames',self.fileNames,' ','fileList',self.fileList)
# ------------------------------------------------------------
# ------------------------------------------------------------
# ------------------------------------------------------------
#stolen unashamedly from:
#https://github.com/dmwm/CRABClient/blob/master/src/python/CRABClient/Commands/report.py
#https://github.com/dmwm/CRABClient/blob/master/src/python/CRABClient/JobType/BasicJobType.py
def makeJSON(optlist):
outdir = optlist[0]
basedir = optlist[1]
verbose = optlist[2]
name = optlist[3]
files = optlist[4:]
s = sampleInfo(name,basedir,files)
#lumi set for this sample
mergedLumis = set()
for f in s.fileList:
# skip empty paths
if f == '': continue
# open the file or skip the file if it can't be opened
if verbose: print "Trying to open file \"" + f +"\""
file = TFile.Open(f)
if file == None:
if verbose: print "\tWARNING: Can't open file \"" + f + "\" from sample" + s
continue
# only keep necessary branches
t = file.Get("TreeMaker2/PreSelection")
if t == None: continue
t.SetBranchStatus("*",0)
t.SetBranchStatus("RunNum",1)
t.SetBranchStatus("LumiBlockNum",1)
#get tree entries
nentries = t.GetEntries()
if nentries==0: continue
t.SetEstimate(nentries)
t.Draw("RunNum:LumiBlockNum","","goff")
v1 = t.GetV1(); v1.SetSize(t.GetSelectedRows()); a1 = array.array('d',v1); v1 = None;
v2 = t.GetV2(); v2.SetSize(t.GetSelectedRows()); a2 = array.array('d',v2); v2 = None;
#loop over tree entries
for run,ls in izip(a1,a2):
irun = int(run)
ils = int(ls)
if not (irun,ils) in mergedLumis:
mergedLumis.add((irun,ils))
file.Close()
### end loop over files in sample
#convert the runlumis from list of pairs to dict: [(123,3), (123,4), (123,5), (123,7), (234,6)] => {123 : [3,4,5,7], 234 : [6]}
mLumisDict = {}
for k, v in mergedLumis:
mLumisDict.setdefault(k, []).append(int(v))
#make lumi list from dict
mergedLumiList = LumiList(runsAndLumis=mLumisDict)
if mergedLumiList:
outfile = outdir+'/lumiSummary_'+s.outName+'.json'
mergedLumiList.writeJSON(outfile)
print "wrote "+outfile
# ------------------------------------------------------------
# ------------------------------------------------------------
# ------------------------------------------------------------
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-o", "--outdir", dest="outdir", default="json", help="output directory for JSON files (default = %default)")
parser.add_option("-b", "--basedir", dest="basedir", default="/store/user/lpcsusyhad/SusyRA2Analysis2015/Run2ProductionV4", help="location of data ntuples (default = %default)")
parser.add_option("-d", "--dict", dest="dictfile", default="dataSamples.py", help="file containing a list of data sample names and files (default = %default)")
parser.add_option("-n", "--npool", dest="npool", default=4, help="number of processes to run (0: don't use multiprocessing, for debugging) (default = %default)")
parser.add_option("-v", "--verbose", dest="verbose", default=False, action="store_true", help="print extra error messages (default = %default)")
(options, args) = parser.parse_args()
from ROOT import *
#check for output directory
if not os.path.isdir(options.outdir):
os.mkdir(options.outdir)
module_name = os.path.basename(options.dictfile).replace(".py","")
dict = imp.load_source(module_name,options.dictfile)
#common list of options
optlist = [options.outdir, options.basedir, options.verbose]
#prepend common list to dict
for i,d in enumerate(dict.dataSamples):
dict.dataSamples[i] = optlist + dict.dataSamples[i]
if options.npool==0:
for d in dict.dataSamples:
makeJSON(d)
else:
p = Pool(int(options.npool))
p.map(makeJSON,dict.dataSamples)