1 from __future__
import print_function
2 from HTMLParser
import HTMLParser
3 from urllib2
import urlopen
4 import cPickle
as pickle
7 locatestarttagend = re.compile(
r"""
8 <[a-zA-Z][-.a-zA-Z0-9:_]* # tag name
9 (?:\s+ # whitespace before attribute name
10 (?:[a-zA-Z_][-.:a-zA-Z0-9_]* # attribute name
11 (?:\s*=\s* # value indicator
12 (?:'[^']*' # LITA-enclosed value
13 |\"[^\"]*\" # LIT-enclosed value
14 |this.src='[^']*' # hack
15 |[^'\">\s]+ # bare value
20 \s* # trailing whitespace
23 tagfind = re.compile(
'[a-zA-Z][-.a-zA-Z0-9:_]*')
24 attrfind = re.compile(
25 r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
26 r'(\'[^\']*\'|"[^"]*"|[-a-zA-Z0-9./,:;+*%?!&$\(\)_#=~@]*))?')
32 HTMLParser.__init__(self)
40 self.TriggerRates = []
45 self.PrescaleColumn=[]
46 self.L1PrescaleTable=[]
47 self.HLTPrescaleTable=[]
48 self.TotalPrescaleTable=[]
58 def parse_starttag(self, i):
59 self.__starttag_text =
None
60 endpos = self.check_for_whole_start_tag(i)
63 rawdata = self.rawdata
64 self.__starttag_text = rawdata[i:endpos]
68 match = tagfind.match(rawdata, i+1)
69 assert match,
'unexpected call to parse_starttag()'
71 self.lasttag = tag = rawdata[i+1:k].lower()
77 m = attrfind.match(rawdata, k)
80 attrname, rest, attrvalue = m.group(1, 2, 3)
83 elif attrvalue[:1] ==
'\'' == attrvalue[-1:]
or \
84 attrvalue[:1] ==
'"' == attrvalue[-1:]:
85 attrvalue = attrvalue[1:-1]
86 attrvalue = self.unescape(attrvalue)
87 attrs.append((attrname.lower(), attrvalue))
90 end = rawdata[k:endpos].
strip()
91 if end
not in (
">",
"/>"):
92 lineno, offset = self.getpos()
93 if "\n" in self.__starttag_text:
94 lineno = lineno + self.__starttag_text.count(
"\n")
95 offset = len(self.__starttag_text) \
96 - self.__starttag_text.rfind(
"\n")
98 offset = offset + len(self.__starttag_text)
99 self.error(
"junk characters in start tag: %r"
100 % (rawdata[k:endpos][:20],))
101 if end.endswith(
'/>'):
103 self.handle_startendtag(tag, attrs)
105 self.handle_starttag(tag, attrs)
106 if tag
in self.CDATA_CONTENT_ELEMENTS:
107 self.set_cdata_mode()
110 def check_for_whole_start_tag(self, i):
111 rawdata = self.rawdata
112 m = locatestarttagend.match(rawdata, i)
115 next = rawdata[j:j+1]
124 if rawdata.startswith(
"/>", j):
126 if rawdata.startswith(
"/", j):
130 self.updatepos(i, j + 1)
131 self.error(
"malformed empty start tag")
135 if next
in (
"abcdefghijklmnopqrstuvwxyz=/"
136 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
141 self.error(
"malformed start tag")
142 raise AssertionError(
"we should not get here!")
144 def _Parse(self,url):
149 self.feed(req.read())
150 except Exception
as inst:
153 def handle_starttag(self,tag,attrs):
154 ValidTags = [
'a',
'tr',
'td']
156 if not tag
in ValidTags:
160 if tag ==
'a' and attrs:
161 self.hyperlinks.append(attrs[0][1])
170 def handle_endtag(self,tag):
174 self.table.append(self.tmpRow)
179 def handle_startendtag(self,tag, attrs):
182 def handle_data(self,data):
184 self.tmpRow.append(data)
187 def ParsePage1(self):
189 MostRecent = self.table[0]
190 for line
in self.table:
195 TriggerMode = MostRecent[3]
196 isCollisions =
not (TriggerMode.find(
'l1_hlt_collisions') == -1)
199 self.RunNumber = MostRecent[0]
200 for link
in self.hyperlinks:
201 if not link.find(
'RUN='+self.RunNumber)==-1:
206 def ParseRunPage(self):
207 for entry
in self.hyperlinks:
208 entry = entry.replace(
'../../',
'http://cmswbm/')
209 if not entry.find(
'HLTSummary') == -1:
210 self.RatePage = entry
211 if not entry.find(
'L1Summary') == -1:
213 if not entry.find(
'LumiSections') == -1:
214 self.LumiPage =
"http://cmswbm/cmsdb/servlet/"+entry
215 if not entry.find(
'TriggerMode') == -1:
216 if not entry.startswith(
"http://cmswbm/cmsdb/servlet/"):
217 entry =
"http://cmswbm/cmsdb/servlet/"+entry
218 self.TrigModePage = entry
219 return [self.RatePage,self.LumiPage,self.L1Page,self.TrigModePage]
221 def ParseRunSummaryPage(self):
222 for line
in self.table:
225 if line[1].startswith(
'HLT_'):
226 TriggerName = line[1][:line[1].
find(
' ')]
227 TriggerRate = float(line[6].
replace(
',',
''))
228 self.Nevts.append([TriggerName,int(line[3]),int(line[4]),int(line[5]),line[9]])
231 PS = float(line[3])/float(line[4])
232 self.TriggerRates.append([TriggerName,TriggerRate,PS,line[9]])
234 def ParseLumiPage(self):
235 for line
in self.table[1:]:
236 if len(line)<4
or len(line)>12:
238 self.PrescaleColumn.append(int(line[2]))
239 self.LumiByLS.append(float(line[4]))
240 if self.FirstLS == -1
and float(line[6]) > 0:
241 self.FirstLS = int(line[0])
242 self.RatePage = self.RatePage.replace(
'HLTSummary?',
'HLTSummary?fromLS='+line[0]+
'&toLS=&')
244 self.AvLumi = sum(self.LumiByLS[self.FirstLS:])/len(self.LumiByLS[self.FirstLS:])
245 except ZeroDivisionError:
246 print(
"Cannot calculate average lumi -- something is wrong!")
247 print(self.table[:10])
250 def ParseL1Page(self):
251 for line
in self.table:
255 if line[1].startswith(
'L1_'):
258 def ParseTrigModePage(self):
260 for line
in self.table:
264 if line[0].isdigit()
and len(line)>=3:
265 if int(line[0])==ColIndex:
267 StrLumiSplit = line[2].
split(
'E')
268 if len(StrLumiSplit)!=2:
271 lumi = float(StrLumiSplit[0])
272 lumi*=
pow(10,int(StrLumiSplit[1])-30)
273 self.ColumnLumi.append(round(lumi,1))
277 if line[1].startswith(
'L1_')
or line[1].startswith(
'HLT_'):
281 seedtmp.append(line[1])
282 for entry
in line[2:]:
285 if entry.startswith(
'L1_'):
286 seedtmp.append(entry)
288 del tmp[len(self.ColumnLumi)+1:]
290 if line[1].startswith(
'L1_'):
291 self.L1PrescaleTable.append(tmp)
293 self.HLTPrescaleTable.append(tmp)
295 self.SeedMap.append(seedtmp)
296 if len(self.PrescaleColumn)==0:
298 for L1Row
in self.L1PrescaleTable:
301 for prescaleThisLS
in self.PrescaleColumn[self.FirstLS:]:
302 thisAvPS+=float(L1Row[prescaleThisLS+1])
305 self.L1Prescales.append([L1Row[0],thisAvPS])
307 def ComputeTotalPrescales(self):
308 if len(self.L1PrescaleTable)==0
or len(self.HLTPrescaleTable)==0
or len(self.SeedMap)==0:
311 for hltLine
in self.HLTPrescaleTable:
316 for hlt,l1
in self.SeedMap:
322 totalLine = [hltName]+[l1Name]+[-3]*(len(hltLine)-1)
326 if not l1Name.find(
' OR ')==-1:
327 l1Line = [l1Name]+[1]*(len(hltLine)-1)
329 for thisl1Line
in self.L1PrescaleTable:
330 if thisl1Line[0] == l1Name:
334 totalLine = [hltName]+[l1Name]+[-4]*(len(hltLine)-1)
336 totalLine = [hltName,l1Name]
337 for hltPS,l1PS
in zip(hltLine[1:],l1Line[1:]):
339 totalLine.append( int(hltPS)*int(l1PS) )
344 self.TotalPrescaleTable.append(totalLine)
347 def Save(self, fileName):
348 pickle.dump( self, open( fileName,
'w' ) )
350 def Load(self, fileName):
351 self = pickle.load( open( fileName ) )
353 def ComputePU(nBunches):
354 ScaleFactor = 71e-27/11.2e3/nBunches
356 for l
in self.LumiByLS:
357 out.append(l*ScaleFactor)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Power< A, B >::type pow(const A &a, const B &b)