Public Member Functions | |
def | __add__ |
def | __and__ |
def | __contains__ |
def | __init__ |
def | __len__ |
def | __or__ |
def | __str__ |
def | __sub__ |
def | contains |
def | filterLumis |
def | getCMSSWString |
def | getCompactList |
def | getLumis |
def | getRuns |
def | getVLuminosityBlockRange |
def | removeRuns |
def | writeJSON |
Public Attributes | |
compactList | |
filename | |
Private Member Functions | |
def | _getLumiParts |
Deal with lists of lumis in several different forms: Compact list: { '1': [[1, 33], [35, 35], [37, 47], [49, 75], [77, 130], [133, 136]], '2':[[1,45],[50,80]] } where the first key is the run number, subsequent pairs are ranges of lumis within that run that are desired Runs and lumis: { '1': [1,2,3,4,6,7,8,9,10], '2': [1,4,5,20] } where the first key is the run number and the list is a list of individual lumi sections Run lumi pairs: [[1,1], [1,2],[1,4], [2,1], [2,5], [1,10]] where each pair in the list is an individual run&lumi CMSSW representation: '1:1-1:33,1:35,1:37-1:47,2:1-2:45,2:50-2:80' The string used by CMSSW in lumisToProcess or lumisToSkip is a subset of the compactList example above
Definition at line 18 of file LumiList.py.
def LumiList::LumiList::__init__ | ( | self, | |
filename = None , |
|||
lumis = None , |
|||
runsAndLumis = None , |
|||
runs = None , |
|||
compactList = None |
|||
) |
Constructor takes filename (JSON), a list of run/lumi pairs, or a dict with run #'s as the keys and a list of lumis as the values, or just a list of runs
Definition at line 45 of file LumiList.py.
00046 : 00047 """ 00048 Constructor takes filename (JSON), a list of run/lumi pairs, 00049 or a dict with run #'s as the keys and a list of lumis as the values, or just a list of runs 00050 """ 00051 self.compactList = {} 00052 if filename: 00053 self.filename = filename 00054 jsonFile = open(self.filename,'r') 00055 self.compactList = json.load(jsonFile) 00056 elif lumis: 00057 runsAndLumis = {} 00058 for (run, lumi) in lumis: 00059 run = str(run) 00060 if not runsAndLumis.has_key(run): 00061 runsAndLumis[run] = [] 00062 runsAndLumis[run].append(lumi) 00063 00064 if runsAndLumis: 00065 for run in runsAndLumis.keys(): 00066 runString = str(run) 00067 lastLumi = -1000 00068 lumiList = runsAndLumis[run] 00069 if lumiList: 00070 self.compactList[runString] = [] 00071 for lumi in sorted(lumiList): 00072 if lumi == lastLumi: 00073 pass # Skip duplicates 00074 elif lumi != lastLumi + 1: # Break in lumi sequence 00075 self.compactList[runString].append([lumi, lumi]) 00076 else: 00077 nRange = len(self.compactList[runString]) 00078 self.compactList[runString][nRange-1][1] = lumi 00079 lastLumi = lumi 00080 if runs: 00081 for run in runs: 00082 runString = str(run) 00083 self.compactList[runString] = [[1, 0xFFFFFFF]] 00084 00085 if compactList: 00086 for run in compactList.keys(): 00087 runString = str(run) 00088 if compactList[run]: 00089 self.compactList[runString] = compactList[run] 00090
def LumiList::LumiList::__add__ | ( | self, | |
other | |||
) |
Definition at line 167 of file LumiList.py.
def LumiList::LumiList::__and__ | ( | self, | |
other | |||
) |
Definition at line 119 of file LumiList.py.
00119 : # Things in both 00120 result = {} 00121 aruns = set(self.compactList.keys()) 00122 bruns = set(other.compactList.keys()) 00123 for run in aruns & bruns: 00124 lumiList = [] # List for this run 00125 unique = [] # List for this run 00126 for alumi in self.compactList[run]: 00127 for blumi in other.compactList[run]: 00128 if blumi[0] <= alumi[0] and blumi[1] >= alumi[1]: # blumi has all of alumi 00129 lumiList.append(alumi) 00130 if blumi[0] > alumi[0] and blumi[1] < alumi[1]: # blumi is part of alumi 00131 lumiList.append(blumi) 00132 elif blumi[0] <= alumi[0] and blumi[1] < alumi[1] and blumi[1] >= alumi[0]: # overlaps start 00133 lumiList.append([alumi[0], blumi[1]]) 00134 elif blumi[0] > alumi[0] and blumi[1] >= alumi[1] and blumi[0] <= alumi[1]: # overlaps end 00135 lumiList.append([blumi[0], alumi[1]]) 00136 00137 00138 if lumiList: 00139 unique = [lumiList[0]] 00140 for pair in lumiList[1:]: 00141 if pair[0] == unique[-1][1]+1: 00142 unique[-1][1] = pair[1] 00143 else: 00144 unique.append(pair) 00145 00146 result[run] = unique 00147 return LumiList(compactList = result) 00148 00149
def LumiList::LumiList::__contains__ | ( | self, | |
runTuple | |||
) |
Definition at line 325 of file LumiList.py.
00326 : 00327 return self.contains (runTuple) 00328 00329 00330 00331 ''' 00332 # Unit test code 00333 import unittest 00334 import FWCore.ParameterSet.Config as cms 00335 00336 class LumiListTest(unittest.TestCase): 00337 """ 00338 _LumiListTest_ 00339 00340 """ 00341 00342 def testRead(self): 00343 """ 00344 Test reading from JSON 00345 """ 00346 exString = "1:1-1:33,1:35,1:37-1:47,2:49-2:75,2:77-2:130,2:133-2:136" 00347 exDict = {'1': [[1, 33], [35, 35], [37, 47]], 00348 '2': [[49, 75], [77, 130], [133, 136]]} 00349 exVLBR = cms.VLuminosityBlockRange('1:1-1:33', '1:35', '1:37-1:47', '2:49-2:75', '2:77-2:130', '2:133-2:136') 00350 00351 jsonList = LumiList(filename = 'lumiTest.json') 00352 lumiString = jsonList.getCMSSWString() 00353 lumiList = jsonList.getCompactList() 00354 lumiVLBR = jsonList.getVLuminosityBlockRange(True) 00355 00356 self.assertTrue(lumiString == exString) 00357 self.assertTrue(lumiList == exDict) 00358 self.assertTrue(lumiVLBR == exVLBR) 00359 00360 def testList(self): 00361 """ 00362 Test constucting from list of pairs 00363 """ 00364 00365 listLs1 = range(1, 34) + [35] + range(37, 48) 00366 listLs2 = range(49, 76) + range(77, 131) + range(133, 137) 00367 lumis = zip([1]*100, listLs1) + zip([2]*100, listLs2) 00368 00369 jsonLister = LumiList(filename = 'lumiTest.json') 00370 jsonString = jsonLister.getCMSSWString() 00371 jsonList = jsonLister.getCompactList() 00372 00373 pairLister = LumiList(lumis = lumis) 00374 pairString = pairLister.getCMSSWString() 00375 pairList = pairLister.getCompactList() 00376 00377 self.assertTrue(jsonString == pairString) 00378 self.assertTrue(jsonList == pairList) 00379 00380 00381 def testRuns(self): 00382 """ 00383 Test constucting from run and list of lumis 00384 """ 00385 runsAndLumis = { 00386 1: range(1, 34) + [35] + range(37, 48), 00387 2: range(49, 76) + range(77, 131) + range(133, 137) 00388 } 00389 runsAndLumis2 = { 00390 '1': range(1, 34) + [35] + range(37, 48), 00391 '2': range(49, 76) + range(77, 131) + range(133, 137) 00392 } 00393 blank = { 00394 '1': [], 00395 '2': [] 00396 } 00397 00398 jsonLister = LumiList(filename = 'lumiTest.json') 00399 jsonString = jsonLister.getCMSSWString() 00400 jsonList = jsonLister.getCompactList() 00401 00402 runLister = LumiList(runsAndLumis = runsAndLumis) 00403 runString = runLister.getCMSSWString() 00404 runList = runLister.getCompactList() 00405 00406 runLister2 = LumiList(runsAndLumis = runsAndLumis2) 00407 runList2 = runLister2.getCompactList() 00408 00409 runLister3 = LumiList(runsAndLumis = blank) 00410 00411 00412 self.assertTrue(jsonString == runString) 00413 self.assertTrue(jsonList == runList) 00414 self.assertTrue(runList2 == runList) 00415 self.assertTrue(len(runLister3) == 0) 00416 00417 def testFilter(self): 00418 """ 00419 Test filtering of a list of lumis 00420 """ 00421 runsAndLumis = { 00422 1: range(1, 34) + [35] + range(37, 48), 00423 2: range(49, 76) + range(77, 131) + range(133, 137) 00424 } 00425 00426 completeList = zip([1]*150, range(1, 150)) + \ 00427 zip([2]*150, range(1, 150)) + \ 00428 zip([3]*150, range(1, 150)) 00429 00430 smallList = zip([1]*50, range(1, 10)) + zip([2]*50, range(50, 70)) 00431 overlapList = zip([1]*150, range(30, 40)) + \ 00432 zip([2]*150, range(60, 80)) 00433 overlapRes = zip([1]*9, range(30, 34)) + [(1, 35)] + \ 00434 zip([1]*9, range(37, 40)) + \ 00435 zip([2]*30, range(60, 76)) + \ 00436 zip([2]*9, range(77, 80)) 00437 00438 runLister = LumiList(runsAndLumis = runsAndLumis) 00439 00440 # Test a list to be filtered which is a superset of constructed list 00441 filterComplete = runLister.filterLumis(completeList) 00442 # Test a list to be filtered which is a subset of constructed list 00443 filterSmall = runLister.filterLumis(smallList) 00444 # Test a list to be filtered which is neither 00445 filterOverlap = runLister.filterLumis(overlapList) 00446 00447 self.assertTrue(filterComplete == runLister.getLumis()) 00448 self.assertTrue(filterSmall == smallList) 00449 self.assertTrue(filterOverlap == overlapRes) 00450 00451 def testDuplicates(self): 00452 """ 00453 Test a list with lots of duplicates 00454 """ 00455 result = zip([1]*100, range(1, 34) + range(37, 48)) 00456 lumis = zip([1]*100, range(1, 34) + range(37, 48) + range(5, 25)) 00457 00458 lister = LumiList(lumis = lumis) 00459 self.assertTrue(lister.getLumis() == result) 00460 00461 def testNull(self): 00462 """ 00463 Test a null list 00464 """ 00465 00466 runLister = LumiList(lumis = None) 00467 00468 self.assertTrue(runLister.getCMSSWString() == '') 00469 self.assertTrue(runLister.getLumis() == []) 00470 self.assertTrue(runLister.getCompactList() == {}) 00471 00472 def testSubtract(self): 00473 """ 00474 a-b for lots of cases 00475 """ 00476 00477 alumis = {'1' : range(2,20) + range(31,39) + range(45,49), 00478 '2' : range(6,20) + range (30,40), 00479 '3' : range(10,20) + range (30,40) + range(50,60), 00480 } 00481 blumis = {'1' : range(1,6) + range(12,13) + range(16,30) + range(40,50) + range(33,36), 00482 '2' : range(10,35), 00483 '3' : range(10,15) + range(35,40) + range(45,51) + range(59,70), 00484 } 00485 clumis = {'1' : range(1,6) + range(12,13) + range(16,30) + range(40,50) + range(33,36), 00486 '2' : range(10,35), 00487 } 00488 result = {'1' : range(6,12) + range(13,16) + range(31,33) + range(36,39), 00489 '2' : range(6,10) + range(35,40), 00490 '3' : range(15,20) + range(30,35) + range(51,59), 00491 } 00492 result2 = {'1' : range(6,12) + range(13,16) + range(31,33) + range(36,39), 00493 '2' : range(6,10) + range(35,40), 00494 '3' : range(10,20) + range (30,40) + range(50,60), 00495 } 00496 a = LumiList(runsAndLumis = alumis) 00497 b = LumiList(runsAndLumis = blumis) 00498 c = LumiList(runsAndLumis = clumis) 00499 r = LumiList(runsAndLumis = result) 00500 r2 = LumiList(runsAndLumis = result2) 00501 00502 self.assertTrue((a-b).getCMSSWString() == r.getCMSSWString()) 00503 self.assertTrue((a-b).getCMSSWString() != (b-a).getCMSSWString()) 00504 # Test where c is missing runs from a 00505 self.assertTrue((a-c).getCMSSWString() == r2.getCMSSWString()) 00506 self.assertTrue((a-c).getCMSSWString() != (c-a).getCMSSWString()) 00507 # Test empty lists 00508 self.assertTrue(str(a-a) == '{}') 00509 self.assertTrue(len(a-a) == 0) 00510 00511 def testOr(self): 00512 """ 00513 a|b for lots of cases 00514 """ 00515 00516 alumis = {'1' : range(2,20) + range(31,39) + range(45,49), 00517 '2' : range(6,20) + range (30,40), 00518 '3' : range(10,20) + range (30,40) + range(50,60), 00519 } 00520 blumis = {'1' : range(1,6) + range(12,13) + range(16,30) + range(40,50) + range(39,80), 00521 '2' : range(10,35), 00522 '3' : range(10,15) + range(35,40) + range(45,51) + range(59,70), 00523 } 00524 clumis = {'1' : range(1,6) + range(12,13) + range(16,30) + range(40,50) + range(39,80), 00525 '2' : range(10,35), 00526 } 00527 result = {'1' : range(2,20) + range(31,39) + range(45,49) + range(1,6) + range(12,13) + range(16,30) + range(40,50) + range(39,80), 00528 '2' : range(6,20) + range (30,40) + range(10,35), 00529 '3' : range(10,20) + range (30,40) + range(50,60) + range(10,15) + range(35,40) + range(45,51) + range(59,70), 00530 } 00531 a = LumiList(runsAndLumis = alumis) 00532 b = LumiList(runsAndLumis = blumis) 00533 c = LumiList(runsAndLumis = blumis) 00534 r = LumiList(runsAndLumis = result) 00535 self.assertTrue((a|b).getCMSSWString() == r.getCMSSWString()) 00536 self.assertTrue((a|b).getCMSSWString() == (b|a).getCMSSWString()) 00537 self.assertTrue((a|b).getCMSSWString() == (a+b).getCMSSWString()) 00538 00539 00540 def testAnd(self): 00541 """ 00542 a&b for lots of cases 00543 """ 00544 00545 alumis = {'1' : range(2,20) + range(31,39) + range(45,49), 00546 '2' : range(6,20) + range (30,40), 00547 '3' : range(10,20) + range (30,40) + range(50,60), 00548 '4' : range(1,100), 00549 } 00550 blumis = {'1' : range(1,6) + range(12,13) + range(16,25) + range(25,40) + range(40,50) + range(33,36), 00551 '2' : range(10,35), 00552 '3' : range(10,15) + range(35,40) + range(45,51) + range(59,70), 00553 '5' : range(1,100), 00554 } 00555 result = {'1' : range(2,6) + range(12,13) + range(16,20) + range(31,39) + range(45,49), 00556 '2' : range(10,20) + range(30,35), 00557 '3' : range(10,15) + range(35,40) + range(50,51)+ range(59,60), 00558 } 00559 a = LumiList(runsAndLumis = alumis) 00560 b = LumiList(runsAndLumis = blumis) 00561 r = LumiList(runsAndLumis = result) 00562 self.assertTrue((a&b).getCMSSWString() == r.getCMSSWString()) 00563 self.assertTrue((a&b).getCMSSWString() == (b&a).getCMSSWString()) 00564 self.assertTrue((a|b).getCMSSWString() != r.getCMSSWString()) 00565 00566 def testWrite(self): 00567 alumis = {'1' : range(2,20) + range(31,39) + range(45,49), 00568 '2' : range(6,20) + range (30,40), 00569 '3' : range(10,20) + range (30,40) + range(50,60), 00570 '4' : range(1,100), 00571 } 00572 a = LumiList(runsAndLumis = alumis) 00573 a.writeJSON('newFile.json') 00574 00575 00576 if __name__ == '__main__': 00577 jsonFile = open('lumiTest.json','w') 00578 jsonFile.write('{"1": [[1, 33], [35, 35], [37, 47]], "2": [[49, 75], [77, 130], [133, 136]]}') 00579 jsonFile.close() 00580 unittest.main() 00581 ''' 00582 # Test JSON file 00583 00584 #{"1": [[1, 33], [35, 35], [37, 47]], "2": [[49, 75], [77, 130], [133, 136]]}
def LumiList::LumiList::__len__ | ( | self | ) |
Returns number of runs in list
Definition at line 171 of file LumiList.py.
def LumiList::LumiList::__or__ | ( | self, | |
other | |||
) |
Definition at line 150 of file LumiList.py.
00151 : 00152 result = {} 00153 aruns = self.compactList.keys() 00154 bruns = other.compactList.keys() 00155 runs = set(aruns + bruns) 00156 for run in runs: 00157 overlap = sorted(self.compactList.get(run, []) + other.compactList.get(run, [])) 00158 unique = [overlap[0]] 00159 for pair in overlap[1:]: 00160 if pair[0] >= unique[-1][0] and pair[0] <= unique[-1][1]+1 and pair[1] > unique[-1][1]: 00161 unique[-1][1] = pair[1] 00162 elif pair[0] > unique[-1][1]: 00163 unique.append(pair) 00164 result[run] = unique 00165 return LumiList(compactList = result) 00166
def LumiList::LumiList::__str__ | ( | self | ) |
Definition at line 191 of file LumiList.py.
def LumiList::LumiList::__sub__ | ( | self, | |
other | |||
) |
Definition at line 91 of file LumiList.py.
00091 : # Things from self not in other 00092 result = {} 00093 for run in sorted(self.compactList.keys()): 00094 alumis = sorted(self.compactList[run]) 00095 blumis = sorted(other.compactList.get(run, [])) 00096 alist = [] # verified part 00097 for alumi in alumis: 00098 tmplist = [alumi[0], alumi[1]] # may be part 00099 for blumi in blumis: 00100 if blumi[0] <= tmplist[0] and blumi[1] >= tmplist[1]: # blumi has all of alumi 00101 tmplist = [] 00102 break # blumi is has all of alumi 00103 if blumi[0] > tmplist[0] and blumi[1] < tmplist[1]: # blumi is part of alumi 00104 alist.append([tmplist[0], blumi[0]-1]) 00105 tmplist = [blumi[1]+1, tmplist[1]] 00106 elif blumi[0] <= tmplist[0] and blumi[1] < tmplist[1] and blumi[1]>=tmplist[0]: # overlaps start 00107 tmplist = [blumi[1]+1, tmplist[1]] 00108 elif blumi[0] > tmplist[0] and blumi[1] >= tmplist[1] and blumi[0]<=tmplist[1]: # overlaps end 00109 alist.append([tmplist[0], blumi[0]-1]) 00110 tmplist = [] 00111 break 00112 if tmplist: 00113 alist.append(tmplist) 00114 result[run] = alist 00115 00116 return LumiList(compactList = result) 00117 00118
def LumiList::LumiList::_getLumiParts | ( | self | ) | [private] |
Turn compactList into a list of the format [ 'R1:L1', 'R2:L2-R2:L3' ] which is used by getCMSSWString and getVLuminosityBlockRange
Definition at line 227 of file LumiList.py.
00228 : 00229 """ 00230 Turn compactList into a list of the format 00231 [ 'R1:L1', 'R2:L2-R2:L3' ] which is used by getCMSSWString and getVLuminosityBlockRange 00232 """ 00233 00234 parts = [] 00235 runs = self.compactList.keys() 00236 runs.sort(key=int) 00237 for run in runs: 00238 lumis = self.compactList[run] 00239 for lumiPair in sorted(lumis): 00240 if lumiPair[0] == lumiPair[1]: 00241 parts.append("%s:%s" % (run, lumiPair[0])) 00242 else: 00243 parts.append("%s:%s-%s:%s" % 00244 (run, lumiPair[0], run, lumiPair[1])) 00245 return parts 00246
def LumiList::LumiList::contains | ( | self, | |
run, | |||
lumiSection = None |
|||
) |
returns true if the run, lumi section passed in is contained in this lumiList. Input can be either: - a single tuple of (run, lumi), - separate run and lumi numbers - a single run number (returns true if any lumi sections exist)
Definition at line 290 of file LumiList.py.
00291 : 00292 ''' 00293 returns true if the run, lumi section passed in is contained 00294 in this lumiList. Input can be either: 00295 - a single tuple of (run, lumi), 00296 - separate run and lumi numbers 00297 - a single run number (returns true if any lumi sections exist) 00298 ''' 00299 if lumiSection is None: 00300 # if this is an integer or a string, see if the run exists 00301 if isinstance (run, int) or isinstance (run, str): 00302 return self.compactList.has_key( str(run) ) 00303 # if we're here, then run better be a tuple or list 00304 try: 00305 lumiSection = run[1] 00306 run = run[0] 00307 except: 00308 raise RuntimeError, "Improper format for run '%s'" % run 00309 lumiRangeList = self.compactList.get( str(run) ) 00310 if not lumiRangeList: 00311 # the run isn't there, so no need to look any further 00312 return False 00313 for lumiRange in lumiRangeList: 00314 # we want to make this as found if either the lumiSection 00315 # is inside the range OR if the lumi section is greater 00316 # than or equal to the lower bound of the lumi range and 00317 # the upper bound is 0 (which means extends to the end of 00318 # the run) 00319 if lumiRange[0] <= lumiSection and \ 00320 (0 == lumiRange[1] or lumiSection <= lumiRange[1]): 00321 # got it 00322 return True 00323 return False 00324
def LumiList::LumiList::filterLumis | ( | self, | |
lumiList | |||
) |
Return a list of lumis that are in compactList. lumilist is of the simple form [(run1,lumi1),(run1,lumi2),(run2,lumi1)]
Definition at line 175 of file LumiList.py.
00176 : 00177 """ 00178 Return a list of lumis that are in compactList. 00179 lumilist is of the simple form 00180 [(run1,lumi1),(run1,lumi2),(run2,lumi1)] 00181 """ 00182 filteredList = [] 00183 for (run, lumi) in lumiList: 00184 runsInLumi = self.compactList.get(str(run), [[0, -1]]) 00185 for (first, last) in runsInLumi: 00186 if lumi >= first and lumi <= last: 00187 filteredList.append((run, lumi)) 00188 break 00189 return filteredList 00190
def LumiList::LumiList::getCMSSWString | ( | self | ) |
Turn compactList into a list of the format R1:L1,R2:L2-R2:L3 which is acceptable to CMSSW LumiBlockRange variable
Definition at line 247 of file LumiList.py.
def LumiList::LumiList::getCompactList | ( | self | ) |
Return the compact list representation
Definition at line 197 of file LumiList.py.
def LumiList::LumiList::getLumis | ( | self | ) |
Return the list of pairs representation
Definition at line 204 of file LumiList.py.
00205 : 00206 """ 00207 Return the list of pairs representation 00208 """ 00209 theList = [] 00210 runs = self.compactList.keys() 00211 runs.sort(key=int) 00212 for run in runs: 00213 lumis = self.compactList[run] 00214 for lumiPair in sorted(lumis): 00215 for lumi in range(lumiPair[0], lumiPair[1]+1): 00216 theList.append((int(run), lumi)) 00217 00218 return theList 00219
def LumiList::LumiList::getRuns | ( | self | ) |
return the sorted list of runs contained
Definition at line 220 of file LumiList.py.
def LumiList::LumiList::getVLuminosityBlockRange | ( | self, | |
tracked = False |
|||
) |
Turn compactList into an (optionally tracked) VLuminosityBlockRange
Definition at line 258 of file LumiList.py.
00259 : 00260 """ 00261 Turn compactList into an (optionally tracked) VLuminosityBlockRange 00262 """ 00263 00264 import FWCore.ParameterSet.Config as cms 00265 parts = self._getLumiParts() 00266 if tracked: 00267 return cms.VLuminosityBlockRange(parts) 00268 else: 00269 return cms.untracked.VLuminosityBlockRange(parts) 00270
def LumiList::LumiList::removeRuns | ( | self, | |
runList | |||
) |
removes runs from runList from collection
Definition at line 280 of file LumiList.py.
def LumiList::LumiList::writeJSON | ( | self, | |
fileName | |||
) |
Write out a JSON file representation of the object
Definition at line 271 of file LumiList.py.
Definition at line 48 of file LumiList.py.
Definition at line 48 of file LumiList.py.