CMS 3D CMS Logo

MatrixUtil.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import os
3 class Matrix(dict):
4  def __setitem__(self,key,value):
5  if key in self:
6  print("ERROR in Matrix")
7  print("overwriting",key,"not allowed")
8  else:
9  self.update({float(key):WF(float(key),value)})
10 
11  def addOverride(self,key,override):
12  self[key].addOverride(override)
13 
14 #the class to collect all possible steps
15 class Steps(dict):
16  def __setitem__(self,key,value):
17  if key in self:
18  print("ERROR in Step")
19  print("overwriting",key,"not allowed")
20  import sys
21  sys.exit(-9)
22  else:
23  self.update({key:value})
24  # make the python file named <step>.py
25  #if not '--python' in value: self[key].update({'--python':'%s.py'%(key,)})
26 
27  def overwrite(self,keypair):
28  value=self[keypair[1]]
29  self.update({keypair[0]:value})
30 
31 class WF(list):
32  def __init__(self,n,l):
33  self.extend(l)
34  self.num=n
35  #the actual steps of this WF
36  self.steps=[]
37  self.overrides={}
38  def addOverride(self,overrides):
39  self.overrides=overrides
40 
41  def interpret(self,stepsDict):
42  for s in self:
43  print('steps',s,stepsDict[s])
44  steps.append(stepsDict[s])
45 
46 
47 
48 def expandLsInterval(lumis):
49  return range(lumis[0],(lumis[1]+1))
50 
52 jsonFile2015 = findFileInPath("DPGAnalysis/Skims/data/Cert_13TeV_16Dec2015ReReco_Collisions15_25ns_50ns_JSON.txt")
53 jsonFile2016 = findFileInPath("DPGAnalysis/Skims/data/Cert_271036-274240_13TeV_PromptReco_Collisions16_JSON.txt")
54 
55 import json
56 with open(jsonFile2015) as data_file:
57  data_json2015 = json.load(data_file)
58 
59 with open(jsonFile2016) as data_file:
60  data_json2016 = json.load(data_file)
61 
62 # return a portion of the 2015 golden json
63 # LS for a full run by default; otherwise a subset of which you determined the size
64 def selectedLS(list_runs=[],maxNum=-1,l_json=data_json2015):
65  # print "maxNum is %s"%(maxNum)
66  if not isinstance(list_runs[0], int):
67  print("ERROR: list_runs must be a list of integers")
68  return None
69  local_dict = {}
70  ls_count = 0
71 
72  for run in list_runs:
73  if str(run) in l_json.keys():
74  # print "run %s is there"%(run)
75  runNumber = run
76  # print "Doing lumi-section selection for run %s: "%(run)
77  for LSsegment in l_json[str(run)] :
78  # print LSsegment
79  ls_count += (LSsegment[-1] - LSsegment[0] + 1)
80  if (ls_count > maxNum) & (maxNum != -1):
81  break
82  # return local_dict
83  if runNumber in local_dict.keys():
84  local_dict[runNumber].append(LSsegment)
85  else:
86  local_dict[runNumber] = [LSsegment]
87  # print "total LS so far %s - grow %s"%(ls_count,local_dict)
88  #local_dict[runNumber] = [1,2,3]
89  else:
90  print("run %s is NOT present in json %s\n\n"%(run, l_json))
91  # print "++ %s"%(local_dict)
92 
93  if ( len(local_dict) > 0 ) :
94  return local_dict
95  else :
96  print("No luminosity section interval passed the json and your selection; returning None")
97  return None
98 
99 # print "\n\n\n THIS IS WHAT I RETURN: %s \n\n"%( selectedLS([251244,251251]) )
100 
101 
102 
103 
104 InputInfoNDefault=2000000
106  def __init__(self,dataSet,dataSetParent='',label='',run=[],ls={},files=1000,events=InputInfoNDefault,split=10,location='CAF',ib_blacklist=None,ib_block=None) :
107  self.run = run
108  self.ls = ls
109  self.files = files
110  self.events = events
111  self.location = location
112  self.label = label
113  self.dataSet = dataSet
114  self.split = split
115  self.ib_blacklist = ib_blacklist
116  self.ib_block = ib_block
117  self.dataSetParent = dataSetParent
118 
119  def das(self, das_options, dataset):
120  if len(self.run) is not 0 or self.ls:
121  queries = self.queries(dataset)[:3]
122  if len(self.run) != 0:
123  command = ";".join(["dasgoclient %s --query '%s'" % (das_options, query) for query in queries])
124  else:
125  lumis = self.lumis()
126  commands = []
127  while queries:
128  commands.append("dasgoclient %s --query 'lumi,%s' --format json | das-selected-lumis.py %s " % (das_options, queries.pop(), lumis.pop()))
129  command = ";".join(commands)
130  command = "({0})".format(command)
131  else:
132  command = "dasgoclient %s --query '%s'" % (das_options, self.queries(dataset)[0])
133 
134  # Run filter on DAS output
135  if self.ib_blacklist:
136  command += " | grep -E -v "
137  command += " ".join(["-e '{0}'".format(pattern) for pattern in self.ib_blacklist])
138  from os import getenv
139  if getenv("CMSSW_USE_IBEOS","false")=="true": return command + " | ibeos-lfn-sort"
140  return command + " | sort -u"
141 
142  def lumiRanges(self):
143  if len(self.run) != 0:
144  return "echo '{\n"+",".join(('"%d":[[1,268435455]]\n'%(x,) for x in self.run))+"}'"
145  if self.ls :
146  return "echo '{\n"+",".join(('"%d" : %s\n'%( int(x),self.ls[x]) for x in self.ls.keys()))+"}'"
147  return None
148 
149  def lumis(self):
150  query_lumis = []
151  if self.ls:
152  for run in self.ls.keys():
153  run_lumis = []
154  for rng in self.ls[run]: run_lumis.append(str(rng[0])+","+str(rng[1]))
155  query_lumis.append(":".join(run_lumis))
156  return query_lumis
157 
158  def queries(self, dataset):
159  query_by = "block" if self.ib_block else "dataset"
160  query_source = "{0}#{1}".format(dataset, self.ib_block) if self.ib_block else dataset
161 
162  if self.ls :
163  the_queries = []
164  #for query_run in self.ls.keys():
165  # print "run is %s"%(query_run)
166  # if you have a LS list specified, still query das for the full run (multiple ls queries take forever)
167  # and use step1_lumiRanges.log to run only on LS which respect your selection
168 
169  # DO WE WANT T2_CERN ?
170  return ["file {0}={1} run={2}".format(query_by, query_source, query_run) for query_run in self.ls.keys()]
171  #return ["file {0}={1} run={2} site=T2_CH_CERN".format(query_by, query_source, query_run) for query_run in self.ls.keys()]
172 
173 
174  #
175  #for a_range in self.ls[query_run]:
176  # # print "a_range is %s"%(a_range)
177  # the_queries += ["file {0}={1} run={2} lumi={3} ".format(query_by, query_source, query_run, query_ls) for query_ls in expandLsInterval(a_range) ]
178  #print the_queries
179  return the_queries
180 
181  site = " site=T2_CH_CERN"
182  if "CMSSW_DAS_QUERY_SITES" in os.environ:
183  if os.environ["CMSSW_DAS_QUERY_SITES"]:
184  site = " site=%s" % os.environ["CMSSW_DAS_QUERY_SITES"]
185  else:
186  site = ""
187  if len(self.run) is not 0:
188  return ["file {0}={1} run={2}{3}".format(query_by, query_source, query_run, site) for query_run in self.run]
189  #return ["file {0}={1} run={2} ".format(query_by, query_source, query_run) for query_run in self.run]
190  else:
191  return ["file {0}={1}{2}".format(query_by, query_source, site)]
192  #return ["file {0}={1} ".format(query_by, query_source)]
193 
194  def __str__(self):
195  if self.ib_block:
196  return "input from: {0} with run {1}#{2}".format(self.dataSet, self.ib_block, self.run)
197  return "input from: {0} with run {1}".format(self.dataSet, self.run)
198 
199 
200 # merge dictionaries, with prioty on the [0] index
201 def merge(dictlist,TELL=False):
202  import copy
203  last=len(dictlist)-1
204  if TELL: print(last,dictlist)
205  if last==0:
206  # ONLY ONE ITEM LEFT
207  return copy.copy(dictlist[0])
208  else:
209  reducedlist=dictlist[0:max(0,last-1)]
210  if TELL: print(reducedlist)
211  # make a copy of the last item
212  d=copy.copy(dictlist[last])
213  # update with the last but one item
214  d.update(dictlist[last-1])
215  # and recursively do the rest
216  reducedlist.append(d)
217  return merge(reducedlist,TELL)
218 
219 def remove(d,key,TELL=False):
220  import copy
221  e = copy.deepcopy(d)
222  if TELL: print("original dict, BEF: %s"%d)
223  del e[key]
224  if TELL: print("copy-removed dict, AFT: %s"%e)
225  return e
226 
227 
228 
229 
230 stCond={'--conditions':'auto:run1_mc'}
231 def Kby(N,s):
232  return {'--relval':'%s000,%s'%(N,s)}
233 def Mby(N,s):
234  return {'--relval':'%s000000,%s'%(N,s)}
235 
236 def changeRefRelease(steps,listOfPairs):
237  for s in steps:
238  if ('INPUT' in steps[s]):
239  oldD=steps[s]['INPUT'].dataSet
240  for (ref,newRef) in listOfPairs:
241  if ref in oldD:
242  steps[s]['INPUT'].dataSet=oldD.replace(ref,newRef)
243  if '--pileup_input' in steps[s]:
244  for (ref,newRef) in listOfPairs:
245  if ref in steps[s]['--pileup_input']:
246  steps[s]['--pileup_input']=steps[s]['--pileup_input'].replace(ref,newRef)
247 
248 def addForAll(steps,d):
249  for s in steps:
250  steps[s].update(d)
251 
252 
253 def genvalid(fragment,d,suffix='all',fi='',dataSet=''):
254  import copy
255  c=copy.copy(d)
256  if suffix:
257  c['-s']=c['-s'].replace('genvalid','genvalid_'+suffix)
258  if fi:
259  c['--filein']='lhe:%d'%(fi,)
260  if dataSet:
261  c['--filein']='das:%s'%(dataSet,)
262  c['cfg']=fragment
263  return c
264 
265 
MatrixUtil.InputInfo.ls
ls
Definition: MatrixUtil.py:108
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
MatrixUtil.InputInfo.lumis
def lumis(self)
Definition: MatrixUtil.py:149
MatrixUtil.InputInfo.lumiRanges
def lumiRanges(self)
Definition: MatrixUtil.py:142
MatrixUtil.InputInfo.__str__
def __str__(self)
Definition: MatrixUtil.py:194
MatrixUtil.expandLsInterval
def expandLsInterval(lumis)
Definition: MatrixUtil.py:48
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
MatrixUtil.WF.num
num
Definition: MatrixUtil.py:34
relativeConstraints.keys
keys
Definition: relativeConstraints.py:89
MatrixUtil.InputInfo.files
files
Definition: MatrixUtil.py:109
MatrixUtil.InputInfo.split
split
Definition: MatrixUtil.py:114
MatrixUtil.Steps
Definition: MatrixUtil.py:15
MatrixUtil.WF.interpret
def interpret(self, stepsDict)
Definition: MatrixUtil.py:41
str
#define str(s)
Definition: TestProcessor.cc:51
MatrixUtil.addForAll
def addForAll(steps, d)
Definition: MatrixUtil.py:248
MatrixUtil.WF.addOverride
def addOverride(self, overrides)
Definition: MatrixUtil.py:38
MatrixUtil.WF
Definition: MatrixUtil.py:31
MatrixUtil.InputInfo.dataSet
dataSet
Definition: MatrixUtil.py:113
MatrixUtil.InputInfo.dataSetParent
dataSetParent
Definition: MatrixUtil.py:117
MatrixUtil.WF.steps
steps
Definition: MatrixUtil.py:36
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
MatrixUtil.changeRefRelease
def changeRefRelease(steps, listOfPairs)
Definition: MatrixUtil.py:236
golden_json_2015
mps_setup.append
append
Definition: mps_setup.py:85
MatrixUtil.InputInfo
Definition: MatrixUtil.py:105
MatrixUtil.InputInfo.__init__
def __init__(self, dataSet, dataSetParent='', label='', run=[], ls={}, files=1000, events=InputInfoNDefault, split=10, location='CAF', ib_blacklist=None, ib_block=None)
Definition: MatrixUtil.py:106
MatrixUtil.WF.overrides
overrides
Definition: MatrixUtil.py:37
createfilelist.int
int
Definition: createfilelist.py:10
dcsonly_json_2012.findFileInPath
def findFileInPath(theFile)
Definition: dcsonly_json_2012.py:6
MatrixUtil.InputInfo.das
def das(self, das_options, dataset)
Definition: MatrixUtil.py:119
MatrixUtil.Steps.overwrite
def overwrite(self, keypair)
Definition: MatrixUtil.py:27
MatrixUtil.selectedLS
def selectedLS(list_runs=[], maxNum=-1, l_json=data_json2015)
Definition: MatrixUtil.py:64
MatrixUtil.InputInfo.run
run
Definition: MatrixUtil.py:107
MatrixUtil.InputInfo.ib_blacklist
ib_blacklist
Definition: MatrixUtil.py:115
MatrixUtil.Steps.__setitem__
def __setitem__(self, key, value)
Definition: MatrixUtil.py:16
MatrixUtil.Mby
def Mby(N, s)
Definition: MatrixUtil.py:233
MatrixUtil.remove
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:219
format
MatrixUtil.InputInfo.queries
def queries(self, dataset)
Definition: MatrixUtil.py:158
MatrixUtil.Matrix
Definition: MatrixUtil.py:3
MatrixUtil.Matrix.__setitem__
def __setitem__(self, key, value)
Definition: MatrixUtil.py:4
MatrixUtil.InputInfo.location
location
Definition: MatrixUtil.py:111
MatrixUtil.merge
def merge(dictlist, TELL=False)
Definition: MatrixUtil.py:201
MatrixUtil.WF.__init__
def __init__(self, n, l)
Definition: MatrixUtil.py:32
MatrixUtil.Matrix.addOverride
def addOverride(self, key, override)
Definition: MatrixUtil.py:11
MatrixUtil.InputInfo.events
events
Definition: MatrixUtil.py:110
MatrixUtil.InputInfo.ib_block
ib_block
Definition: MatrixUtil.py:116
MatrixUtil.genvalid
def genvalid(fragment, d, suffix='all', fi='', dataSet='')
Definition: MatrixUtil.py:253
MatrixUtil.InputInfo.label
label
Definition: MatrixUtil.py:112
MatrixUtil.Kby
def Kby(N, s)
Standard release validation samples ####.
Definition: MatrixUtil.py:231
merge
Definition: merge.py:1
update
#define update(a, b)
Definition: TrackClassifier.cc:10
python.rootplot.root2matplotlib.replace
def replace(string, replacements)
Definition: root2matplotlib.py:444