CMS 3D CMS Logo

pyRunSummaryBaseClass.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 '''
00004 Class access the RunSummary web page for a given list of runs, and stores a list of valid runs (i.e., runs meeting user-specified criteria) in that range.  For valid HCAL runs, we simply require that the HCAL was present in the readout of the runs.
00005 '''
00006 
00007 import sys
00008 import string
00009 # Url Libraries needed for accessing/parsing html results
00010 import urllib
00011 import urllib2
00012 
00013 
00014 # class parseResults stores web info for each run, and parses it to search for all run-related info (run #, start, end time, components, etc.)
00015 
00016 class parseResult:
00017     '''
00018     Takes output from html search, and tries to identify whether HCAL was in the run.
00019     '''
00020 
00021     def __init__(self,text,debug=False):
00022         '''
00023         This is all stuff for parsing the html results
00024         For Hcal, all we care about is that:
00025         a) self.run is a valid integer
00026         b) self.events is a non-zero integer
00027         c) self.components contains "HCAL"
00028         However, we fill the class with all the information provided with the expectation that we may want to use more info in our sorting later (such as requiring the B field to be above a certain value, or requiring a specific trigger).
00029          
00030         A valid list of results (text) will contain 12 lines:  <blank line>, Run, Sequence, Booking Time, Key, Start Time, End Time, Triggers, Events, B Field, Components, and a </TR> row end.
00031         '''
00032 
00033         if (debug):
00034             print self.__init__.__doc__
00035             
00036         self.isvalid=False # determines whether run is valid or not (just tests whether text info provided can be properly parsed)
00037         self.isvalidHcal=False # Boolean describing whether run is valid for Hcal
00038         self.debug=debug
00039         
00040         self.events=None
00041         self.components=None
00042         self.run=None
00043         # values below aren't used yet for determining Hcal validity, but they could be
00044         self.sequence=None
00045         self.bookingtime=None
00046         self.key=None
00047         self.starttime=None
00048         self.endtime=None
00049         self.triggers=None
00050         self.bfield=None
00051         
00052         text=string.replace(text,"&nbsp;","") # get rid of html space format
00053         self.text=string.split(text,"\n")[1:] # ignore initial blank line
00054 
00055         # Should really use regular expressions for parsing at some point
00056         try:
00057             self.run=self.text[0]
00058             self.run=string.split(self.run,"</A>")[0]
00059             self.run=string.split(self.run,">")[2]
00060             self.run=string.atoi(self.run)
00061         except:
00062             if (self.debug):
00063                 print "Could not determine run info from %s"%self.text[0]
00064             return
00065 
00066         try:
00067             self.sequence = self.text[1]
00068             self.sequence = string.split(self.sequence,"</TD>")[0]
00069             self.sequence = string.split(self.sequence,"<TD>")[1]
00070         except:
00071             if (self.debug):
00072                 print "Could not determine sequence from %s"%self.text[1]
00073             return
00074 
00075         try:
00076             self.bookingtime = self.text[2]
00077             self.bookingtime = string.split(self.bookingtime,"</TD>")[0]
00078             self.bookingtime = string.split(self.bookingtime,"<TD>")[1]
00079         except:
00080             if (self.debug):
00081                 print "Could not determine booking time from %s"%self.text[2]
00082             return
00083         
00084         try:
00085             self.key = self.text[3]
00086             self.key = string.split(self.key,"</TD>")[0]
00087             self.key = string.split(self.key,"<TD>")[1]
00088         except:
00089             if (self.debug):
00090                 print "Could not determine key from %s"%self.text[3]
00091             return
00092         
00093         try:
00094             self.starttime = self.text[4]
00095             self.starttime = string.split(self.starttime,"</TD>")[0]
00096             self.starttime = string.split(self.starttime,"<TD>")[1]
00097         except:
00098             if (self.debug):
00099                 print "Could not determine start time from %s"%self.text[4]
00100             return
00101         
00102         try:
00103             self.endtime = self.text[5]
00104             self.endtime = string.split(self.endtime,"</TD>")[0]
00105             self.endtime = string.split(self.endtime,"<TD>")[1]
00106         except:
00107             if (self.debug):
00108                 print "Could not determine end time from %s"%self.text[5]
00109             return
00110         
00111         try:
00112             self.triggers = self.text[6]
00113             self.triggers = string.split(self.triggers,"</TD>")[0]
00114             self.triggers = string.split(self.triggers,">")[1]
00115             if (self.triggers<>"null"):
00116                 self.triggers=string.atoi(self.triggers)
00117         except:
00118             if (self.debug):
00119                 print "Could not determine triggers from %s"%self.text[6]
00120             return
00121         
00122         try:
00123             self.events = self.text[7]
00124             self.events = string.split(self.events,"</TD>")[0]
00125             self.events = string.split(self.events,">")[1]
00126             # Make sure read integer value 
00127             if (self.events<>"null"):
00128                 self.events=string.atoi(self.events)
00129             # If events ="null", try to estimate num. of events from triggers
00130             if (self.events=="null" and self.triggers<>"null"):
00131                 self.events=self.triggers
00132         except:
00133             if (self.debug):
00134                 print "Could not determine # of events from %s"%self.text[7]
00135             return
00136         
00137         try:
00138             self.bfield = self.text[8]
00139             self.bfield = string.split(self.bfield,"</TD>")[0]
00140             self.bfield = string.split(self.bfield,">")[1]
00141             if (self.bfield<>"null"):
00142                 self.bfield=string.atof(self.bfield)
00143         except:
00144             if (self.debug):
00145                 print "Could not determine B field from %s"%self.text[8]
00146             return
00147         
00148         try:
00149             self.components = self.text[9]
00150             self.components = string.split(self.components,"</TD>")[0]
00151             self.components = string.split(self.components,">")[1]
00152         except:
00153             if (self.debug):
00154                 print "Could not determine components from %s"%self.text[9]
00155             return
00156 
00157         self.isvalid=True # able to read all event info
00158 
00159         # Some good runs have 'events' listed as null -- don't use events as a test of goodness?
00160         if (self.components<>None and string.find(self.components,"HCAL")>-1):
00161             self.isvalidHcal=True
00162         return
00163 
00164     def printParse(self):
00165         ''' printParse method prints result of parsing input string.\n\n '''
00166         if (self.debug):
00167             print self.printParse.__doc__
00168             
00169         print "Run # = ", self. run
00170         print "\tsequence = ",self.sequence
00171         print "\tbooking time = ",self.bookingtime
00172         print "\tkey = ",self.key
00173         print "\tstart time = ",self.starttime
00174         print "\tend time = ",self.endtime
00175         print "\ttriggers = ",self.triggers
00176         print "\tevents = ",self.events
00177         print "\tB field = ",self.bfield
00178         print "\tcomponents = ",self.components
00179         print "Is info valid? ",self.isvalid
00180         print "\n\n"
00181         return
00182 
00183 
00184 class goodHCALRun:
00185     '''
00186     stores info about a single run number'''
00187     def __init__(self, run=43293, debug=False):
00188         self.beginRun=run
00189         self.endRun=run
00190         self.debug=debug
00191         self.values={"RUN_BEGIN":self.beginRun,
00192                      "RUN_END":self.endRun}
00193         # Run Summary Page URL
00194         myurl="https://cmsmon.cern.ch/cmsdb/servlet/RunSummary"
00195         
00196         # encode values as URL data, and post them to run summary page
00197         data=urllib.urlencode(self.values)
00198         req=urllib2.Request(myurl,data)
00199         # Get result of sending data to URL
00200         response=urllib2.urlopen(req)
00201         self.thepage=response.read()
00202         # Split result by <TR> (since result is a table of runs, if runs found)
00203         self.thepage=string.split(self.thepage,"<TR>")
00204         #for i in thepage:
00205         #    print i
00206         self.runinfo = parseResult(self.thepage[-1],self.debug)
00207         print "\n\n\n"
00208         self.runinfo.printParse()
00209         return
00210 
00211 class goodHCALList:    
00212     '''
00213     goodHCALList class stores list of runs in range, and list of runs containing HCAL.
00214     '''
00215 
00216     def __init__(self, beginRun=43293, endRun=43493,debug=False):
00217         self.beginRun=beginRun
00218         self.endRun=endRun
00219         self.debug=debug
00220         if (self.debug):
00221             print "Initializing goodHCALList class"
00222             print "Looking for runs between %i - %i"%(self.beginRun, self.endRun)
00223         self.values={"RUN_BEGIN":self.beginRun,
00224                      "RUN_END":self.endRun}
00225         self.allruns=[]
00226         self.hcalruns=[]
00227         # Check for HCAL runs in range (beginRun-endRun)
00228         self.CheckHtml()
00229         return
00230 
00231     def CheckHtml(self):
00232         '''CheckHtml method searches https://cmsmon.cern.ch/cmsdb/servlet/RunSummary for runs in specified range, and store runs containing HCAL in a separate list.'''
00233         if (self.debug):
00234             print self.CheckHtml.__doc__
00235 
00236         # Run Summary Page URL
00237         myurl="https://cmsmon.cern.ch/cmsdb/servlet/RunSummary"
00238 
00239         # encode values as URL data, and post them to run summary page
00240         data=urllib.urlencode(self.values)
00241         req=urllib2.Request(myurl,data)
00242 
00243 
00244         # Get result of sending data to URL
00245         response=urllib2.urlopen(req)
00246         thepage=response.read()
00247         
00248         # Split result by <TR> (since result is a table of runs, if runs found)
00249         thepage=string.split(thepage,"<TR>")
00250 
00251         for i in thepage:
00252             temp = parseResult(i,self.debug)
00253             if (self.debug):
00254                 temp.printParse()
00255             if temp.isvalid:
00256                 self.allruns.append(temp.run)
00257                 if temp.isvalidHcal:
00258                     self.hcalruns.append(temp.run)
00259 
00260         self.allruns.sort()
00261         self.hcalruns.sort()
00262         return
00263 
00264     def printRuns(self):
00265         ''' prints all runs found in user-specified range, and indicates if HCAL was present in each run. '''
00266         if (self.debug):
00267             print self.printRuns.__doc__
00268 
00269         print "\n%20s%20s"%("Run found","Includes HCAL")
00270         for i in self.allruns:
00271             if i in self.hcalruns:
00272                 text="%20s%20s"%(`i`,"YES")
00273             else:
00274                 text="%20s"%(`i`)
00275             print text
00276         return
00277 
00278 
00279 ######################################################
00280 
00281 if __name__=="__main__":
00282     
00283     values={"RUN_BEGIN": 43293,
00284             "RUN_END": 43493}
00285 
00286     debug=False
00287     
00288     # allow user to input start, end run values on command line
00289     if len(sys.argv)>1:
00290         try:
00291             values["RUN_BEGIN"]=string.atoi(sys.argv[1])
00292         except:
00293             print "Could not recognize starting run number '%s' as an integer"%sys.argv[1]
00294             print "Continuing using default starting run number %i"%values["RUN_BEGIN"]
00295 
00296     if len(sys.argv)>2:
00297         try:
00298             values["RUN_END"]=string.atoi(sys.argv[2])
00299         except:
00300             print "Could not recognize ending run number '%s' as an integer"%sys.argv[2]
00301             print "Continuing using default ending run number %i"%values["RUN_END"]
00302 
00303     if len(sys.argv)>3:
00304         debug=True
00305 
00306     # Get list of good HCAL runs
00307     x=goodHCALList(values["RUN_BEGIN"],values["RUN_END"],debug)
00308     x.printRuns() # print out list

Generated on Tue Jun 9 17:32:56 2009 for CMSSW by  doxygen 1.5.4