1 from HTMLParser
import HTMLParser
2 from urllib2
import urlopen
3 import cPickle
as pickle
6 locatestarttagend = re.compile(
r"""
7 <[a-zA-Z][-.a-zA-Z0-9:_]* # tag name
8 (?:\s+ # whitespace before attribute name
9 (?:[a-zA-Z_][-.:a-zA-Z0-9_]* # attribute name
10 (?:\s*=\s* # value indicator
11 (?:'[^']*' # LITA-enclosed value
12 |\"[^\"]*\" # LIT-enclosed value
13 |this.src='[^']*' # hack
14 |[^'\">\s]+ # bare value
19 \s* # trailing whitespace
22 tagfind = re.compile(
'[a-zA-Z][-.a-zA-Z0-9:_]*')
23 attrfind = re.compile(
24 r'\s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(\s*=\s*'
25 r'(\'[^\']*\'|"[^"]*"|[-a-zA-Z0-9./,:;+*%?!&$\(\)_#=~@]*))?')
31 HTMLParser.__init__(self)
39 self.TriggerRates = []
44 self.PrescaleColumn=[]
45 self.L1PrescaleTable=[]
46 self.HLTPrescaleTable=[]
47 self.TotalPrescaleTable=[]
57 def parse_starttag(self, i):
58 self.__starttag_text =
None
59 endpos = self.check_for_whole_start_tag(i)
62 rawdata = self.rawdata
63 self.__starttag_text = rawdata[i:endpos]
67 match = tagfind.match(rawdata, i+1)
68 assert match,
'unexpected call to parse_starttag()'
70 self.lasttag = tag = rawdata[i+1:k].lower()
76 m = attrfind.match(rawdata, k)
79 attrname, rest, attrvalue = m.group(1, 2, 3)
82 elif attrvalue[:1] ==
'\'' == attrvalue[-1:]
or \
83 attrvalue[:1] ==
'"' == attrvalue[-1:]:
84 attrvalue = attrvalue[1:-1]
85 attrvalue = self.unescape(attrvalue)
86 attrs.append((attrname.lower(), attrvalue))
89 end = rawdata[k:endpos].
strip()
90 if end
not in (
">",
"/>"):
91 lineno, offset = self.getpos()
92 if "\n" in self.__starttag_text:
93 lineno = lineno + self.__starttag_text.count(
"\n")
94 offset = len(self.__starttag_text) \
95 - self.__starttag_text.rfind(
"\n")
97 offset = offset + len(self.__starttag_text)
98 self.error(
"junk characters in start tag: %r"
99 % (rawdata[k:endpos][:20],))
100 if end.endswith(
'/>'):
102 self.handle_startendtag(tag, attrs)
104 self.handle_starttag(tag, attrs)
105 if tag
in self.CDATA_CONTENT_ELEMENTS:
106 self.set_cdata_mode()
109 def check_for_whole_start_tag(self, i):
110 rawdata = self.rawdata
111 m = locatestarttagend.match(rawdata, i)
114 next = rawdata[j:j+1]
123 if rawdata.startswith(
"/>", j):
125 if rawdata.startswith(
"/", j):
129 self.updatepos(i, j + 1)
130 self.error(
"malformed empty start tag")
134 if next
in (
"abcdefghijklmnopqrstuvwxyz=/"
135 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
140 self.error(
"malformed start tag")
141 raise AssertionError(
"we should not get here!")
143 def _Parse(self,url):
148 self.feed(req.read())
149 except Exception, inst:
152 def handle_starttag(self,tag,attrs):
153 ValidTags = [
'a',
'tr',
'td']
155 if not tag
in ValidTags:
159 if tag ==
'a' and attrs:
160 self.hyperlinks.append(attrs[0][1])
169 def handle_endtag(self,tag):
173 self.table.append(self.tmpRow)
178 def handle_startendtag(self,tag, attrs):
181 def handle_data(self,data):
183 self.tmpRow.append(data)
186 def ParsePage1(self):
188 MostRecent = self.table[0]
189 for line
in self.table:
194 TriggerMode = MostRecent[3]
195 isCollisions =
not (TriggerMode.find(
'l1_hlt_collisions') == -1)
198 self.RunNumber = MostRecent[0]
199 for link
in self.hyperlinks:
200 if not link.find(
'RUN='+self.RunNumber)==-1:
205 def ParseRunPage(self):
206 for entry
in self.hyperlinks:
207 entry = entry.replace(
'../../',
'http://cmswbm/')
208 if not entry.find(
'HLTSummary') == -1:
209 self.RatePage = entry
210 if not entry.find(
'L1Summary') == -1:
212 if not entry.find(
'LumiSections') == -1:
213 self.LumiPage =
"http://cmswbm/cmsdb/servlet/"+entry
214 if not entry.find(
'TriggerMode') == -1:
215 if not entry.startswith(
"http://cmswbm/cmsdb/servlet/"):
216 entry =
"http://cmswbm/cmsdb/servlet/"+entry
217 self.TrigModePage = entry
218 return [self.RatePage,self.LumiPage,self.L1Page,self.TrigModePage]
220 def ParseRunSummaryPage(self):
221 for line
in self.table:
224 if line[1].startswith(
'HLT_'):
225 TriggerName = line[1][:line[1].
find(
' ')]
226 TriggerRate = float(line[6].
replace(
',',
''))
227 self.Nevts.append([TriggerName,int(line[3]),int(line[4]),int(line[5]),line[9]])
230 PS = float(line[3])/float(line[4])
231 self.TriggerRates.append([TriggerName,TriggerRate,PS,line[9]])
233 def ParseLumiPage(self):
234 for line
in self.table[1:]:
235 if len(line)<4
or len(line)>12:
237 self.PrescaleColumn.append(int(line[2]))
238 self.LumiByLS.append(float(line[4]))
239 if self.FirstLS == -1
and float(line[6]) > 0:
240 self.FirstLS = int(line[0])
241 self.RatePage = self.RatePage.replace(
'HLTSummary?',
'HLTSummary?fromLS='+line[0]+
'&toLS=&')
243 self.AvLumi = sum(self.LumiByLS[self.FirstLS:])/len(self.LumiByLS[self.FirstLS:])
244 except ZeroDivisionError:
245 print "Cannot calculate average lumi -- something is wrong!"
246 print self.table[:10]
249 def ParseL1Page(self):
250 for line
in self.table:
254 if line[1].startswith(
'L1_'):
257 def ParseTrigModePage(self):
259 for line
in self.table:
263 if line[0].isdigit()
and len(line)>=3:
264 if int(line[0])==ColIndex:
266 StrLumiSplit = line[2].
split(
'E')
267 if len(StrLumiSplit)!=2:
270 lumi = float(StrLumiSplit[0])
271 lumi*=
pow(10,int(StrLumiSplit[1])-30)
272 self.ColumnLumi.append(round(lumi,1))
276 if line[1].startswith(
'L1_')
or line[1].startswith(
'HLT_'):
280 seedtmp.append(line[1])
281 for entry
in line[2:]:
284 if entry.startswith(
'L1_'):
285 seedtmp.append(entry)
287 del tmp[len(self.ColumnLumi)+1:]
289 if line[1].startswith(
'L1_'):
290 self.L1PrescaleTable.append(tmp)
292 self.HLTPrescaleTable.append(tmp)
294 self.SeedMap.append(seedtmp)
295 if len(self.PrescaleColumn)==0:
297 for L1Row
in self.L1PrescaleTable:
300 for prescaleThisLS
in self.PrescaleColumn[self.FirstLS:]:
301 thisAvPS+=float(L1Row[prescaleThisLS+1])
304 self.L1Prescales.append([L1Row[0],thisAvPS])
306 def ComputeTotalPrescales(self):
307 if len(self.L1PrescaleTable)==0
or len(self.HLTPrescaleTable)==0
or len(self.SeedMap)==0:
310 for hltLine
in self.HLTPrescaleTable:
315 for hlt,l1
in self.SeedMap:
321 totalLine = [hltName]+[l1Name]+[-3]*(len(hltLine)-1)
325 if not l1Name.find(
' OR ')==-1:
326 l1Line = [l1Name]+[1]*(len(hltLine)-1)
328 for thisl1Line
in self.L1PrescaleTable:
329 if thisl1Line[0] == l1Name:
333 totalLine = [hltName]+[l1Name]+[-4]*(len(hltLine)-1)
335 totalLine = [hltName,l1Name]
336 for hltPS,l1PS
in zip(hltLine[1:],l1Line[1:]):
338 totalLine.append( int(hltPS)*int(l1PS) )
343 self.TotalPrescaleTable.append(totalLine)
346 def Save(self, fileName):
347 pickle.dump( self, open( fileName,
'w' ) )
349 def Load(self, fileName):
350 self = pickle.load( open( fileName ) )
352 def ComputePU(nBunches):
353 ScaleFactor = 71e-27/11.2e3/nBunches
355 for l
in self.LumiByLS:
356 out.append(l*ScaleFactor)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Power< A, B >::type pow(const A &a, const B &b)