CMS 3D CMS Logo

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