Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:26:11

0001 import das_client, rrClient
0002 import json, datetime
0003 
0004 def today() :
0005     return datetime.datetime.now().date()
0006 
0007 def daysAgo(n) :
0008     date = today()
0009     return date - datetime.timedelta(days=n)
0010 
0011 def day(string) :
0012     return datetime.datetime.strptime(string, "%Y%m%d").date()
0013 
0014 def getRunsForDate(date, minlumis=10) :
0015     '''
0016         date: expected to be datetime.datetime object
0017     '''
0018     datestring = date.strftime('%Y%m%d')
0019     querystring = "run date={date} | grep run.nlumis>{minlumis}".format(date=datestring, minlumis=minlumis)
0020     return dasRunQueryToDict(querystring)
0021 
0022 def getRunsNewer(run, minlumis=10, source='RunRegistry') :
0023     '''
0024         run: run number int
0025         source: Either RunRegistry or DAS
0026     '''
0027     if source == 'RunRegistry' :
0028         return rrClient.getRunsNewer(run, minlumis)
0029     elif source == 'DAS' :
0030         querystring = "run | grep run.nlumis>{minlumis}, run.runnumber>{run}".format(run=run, minlumis=minlumis)
0031         return dasRunQueryToDict(querystring)
0032 
0033 def runGetDatasetsAvailable(run) :
0034     '''
0035         run : int
0036         Will return a list of datasets in DAS that have the run in them
0037     '''
0038     querystring = "dataset run={run}".format(run=run)
0039 
0040     datasets = []
0041     for datasetDict in dasQuery(querystring, 'dataset') :
0042         # cmsRun will not like unicode
0043         datasets.append(datasetDict['name'].encode('ascii','replace'))
0044 
0045     return datasets
0046 
0047 def getFilesForRun(run, dataset) :
0048     '''
0049         run : int
0050         dataset : string dataset to find files in, e.g.
0051             /ExpressCosmics/Run2015A-Express-v1/FEVT
0052             /ExpressPhysics/Run2015A-Express-v1/FEVT
0053     '''
0054     querystring = "file dataset={dataset} run={run}".format(dataset=dataset, run=run)
0055 
0056     files = []
0057     for fileDict in dasQuery(querystring, 'file') :
0058         # cmsRun will not like unicode
0059         files.append(fileDict['name'].encode('ascii','replace'))
0060 
0061     return files
0062 
0063 
0064 # -------------------- DAS convert tools
0065 
0066 def dasRunQueryToDict(querystring) :
0067     runs = {}
0068     for runDict in dasQuery(querystring, 'run') :
0069         runNo = int(runDict['run_number'])
0070         runDict['date'] = datestring
0071         runs[runNo] = runDict
0072     return runs
0073 
0074 def dasQuery(queryString, entryTitle) :
0075     dasinfo = das_client.get_data('https://cmsweb.cern.ch', queryString, 0, 0, False)
0076     if dasinfo['status'] != 'ok' :
0077         raise Exception('DAS query failed.\nQuery: %s\nDAS Status returned: %s' % (queryString, dasinfo['status']))
0078 
0079     if len(dasinfo['data']) > 0 :
0080         for entry in dasinfo['data'] :
0081             if entryTitle in entry and len(entry[entryTitle]) > 0 :
0082                 yield entry[entryTitle][0]