ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/DataDiscovery.py
(Generate patch)

Comparing COMP/CRAB/python/DataDiscovery.py (file contents):
Revision 1.26 by spiga, Wed Feb 4 14:26:09 2009 UTC vs.
Revision 1.42 by ewv, Mon Mar 22 21:17:15 2010 UTC

# Line 1 | Line 1
1   #!/usr/bin/env python
2 +
3 + __revision__ = "$Id$"
4 + __version__ = "$Revision$"
5 +
6   import exceptions
7   import DBSAPI.dbsApi
8 < from DBSAPI.dbsApiException import *
8 > from DBSAPI.dbsApiException import *
9   import common
10   from crab_util import *
11 < import os
11 > from LumiList import LumiList
12 > import os
13 >
14  
15  
10 # #######################################
16   class DBSError(exceptions.Exception):
17      def __init__(self, errorName, errorMessage):
18          args='\nERROR DBS %s : %s \n'%(errorName,errorMessage)
19          exceptions.Exception.__init__(self, args)
20          pass
21 <    
21 >
22      def getErrorMessage(self):
23          """ Return error message """
24          return "%s" % (self.args)
25  
26 < # #######################################
26 >
27 >
28   class DBSInvalidDataTierError(exceptions.Exception):
29      def __init__(self, errorName, errorMessage):
30          args='\nERROR DBS %s : %s \n'%(errorName,errorMessage)
31          exceptions.Exception.__init__(self, args)
32          pass
33 <    
33 >
34      def getErrorMessage(self):
35          """ Return error message """
36          return "%s" % (self.args)
37  
38 < # #######################################
38 >
39 >
40   class DBSInfoError:
41      def __init__(self, url):
42          print '\nERROR accessing DBS url : '+url+'\n'
43          pass
44  
45 < # ####################################
45 >
46 >
47   class DataDiscoveryError(exceptions.Exception):
48      def __init__(self, errorMessage):
49          self.args=errorMessage
# Line 46 | Line 54 | class DataDiscoveryError(exceptions.Exce
54          """ Return exception error """
55          return "%s" % (self.args)
56  
57 < # ####################################
57 >
58 >
59   class NotExistingDatasetError(exceptions.Exception):
60      def __init__(self, errorMessage):
61          self.args=errorMessage
# Line 57 | Line 66 | class NotExistingDatasetError(exceptions
66          """ Return exception error """
67          return "%s" % (self.args)
68  
69 < # ####################################
69 >
70 >
71   class NoDataTierinProvenanceError(exceptions.Exception):
72      def __init__(self, errorMessage):
73          self.args=errorMessage
# Line 68 | Line 78 | class NoDataTierinProvenanceError(except
78          """ Return exception error """
79          return "%s" % (self.args)
80  
81 < # ####################################
82 < # class to find and extact info from published data
81 >
82 >
83   class DataDiscovery:
84 +    """
85 +    Class to find and extact info from published data
86 +    """
87      def __init__(self, datasetPath, cfg_params, skipAnBlocks):
88  
89          #       Attributes
90          self.datasetPath = datasetPath
91 +        # Analysis dataset is primary/processed/tier/definition
92 +        self.ads = len(self.datasetPath.split("/")) > 4
93          self.cfg_params = cfg_params
94          self.skipBlocks = skipAnBlocks
95  
96          self.eventsPerBlock = {}  # DBS output: map fileblocks-events for collection
97          self.eventsPerFile = {}   # DBS output: map files-events
98 <        self.blocksinfo = {}      # DBS output: map fileblocks-files
98 > #         self.lumisPerBlock = {}   # DBS output: number of lumis in each block
99 > #         self.lumisPerFile = {}    # DBS output: number of lumis in each file
100 >        self.blocksinfo = {}      # DBS output: map fileblocks-files
101          self.maxEvents = 0        # DBS output: max events
102 <        self.parent = {}       # DBS output: max events
102 >        self.maxLumis = 0         # DBS output: total number of lumis
103 >        self.parent = {}          # DBS output: parents of each file
104 >        self.lumis = {}           # DBS output: lumis in each file
105 >        self.lumiMask = None
106  
87 # ####################################
107      def fetchDBSInfo(self):
108          """
109          Contact DBS
# Line 93 | Line 112 | class DataDiscovery:
112          global_url="http://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet"
113          caf_url = "http://cmsdbsprod.cern.ch/cms_dbs_caf_analysis_01/servlet/DBSServlet"
114          dbs_url_map  =   {'glite':    global_url,
115 +                          'glite_slc5':global_url,\
116                            'glitecoll':global_url,\
117                            'condor':   global_url,\
118                            'condor_g': global_url,\
119                            'glidein':  global_url,\
120                            'lsf':      global_url,\
121                            'caf':      caf_url,\
122 <                          'sge':      global_url
122 >                          'sge':      global_url,\
123 >                          'arc':      global_url,\
124 >                          'pbs':      global_url
125                            }
126  
127          dbs_url_default = dbs_url_map[(common.scheduler.name()).lower()]
128          dbs_url=  self.cfg_params.get('CMSSW.dbs_url', dbs_url_default)
129 <        common.logger.debug(3,"Accessing DBS at: "+dbs_url)
129 >        common.logger.info("Accessing DBS at: "+dbs_url)
130  
131          ## check if runs are selected
132          runselection = []
133          if (self.cfg_params.has_key('CMSSW.runselection')):
134              runselection = parseRange2(self.cfg_params['CMSSW.runselection'])
135  
136 +        ## check if various lumi parameters are set
137 +        self.lumiMask = self.cfg_params.get('CMSSW.lumi_mask',None)
138 +        self.lumiParams = self.cfg_params.get('CMSSW.total_number_of_lumis',None) or \
139 +                          self.cfg_params.get('CMSSW.lumis_per_job',None)
140 +
141 +        lumiList = None
142 +        if self.lumiMask:
143 +            lumiList = LumiList(filename=self.lumiMask)
144  
145          self.splitByRun = int(self.cfg_params.get('CMSSW.split_by_run', 0))
116          
117        self.ads = int(self.cfg_params.get('CMSSW.ads', 0))
146  
147 <        common.logger.debug(6,"runselection is: %s"%runselection)
147 >        common.logger.log(10-1,"runselection is: %s"%runselection)
148          ## service API
149          args = {}
150          args['url']     = dbs_url
# Line 125 | Line 153 | class DataDiscovery:
153          ## check if has been requested to use the parent info
154          useparent = int(self.cfg_params.get('CMSSW.use_parent',0))
155  
156 <        ## check if has been asked for a non default file to store/read analyzed fileBlocks  
157 <        defaultName = common.work_space.shareDir()+'AnalyzedBlocks.txt'  
156 >        ## check if has been asked for a non default file to store/read analyzed fileBlocks
157 >        defaultName = common.work_space.shareDir()+'AnalyzedBlocks.txt'
158          fileBlocks_FileName = os.path.abspath(self.cfg_params.get('CMSSW.fileblocks_file',defaultName))
131
132        api = DBSAPI.dbsApi.DbsApi(args)
159  
160 <        self.files = self.queryDbs(api,path=self.datasetPath,runNumber=runselection,useParent=useparent)
160 >        api = DBSAPI.dbsApi.DbsApi(args)
161 >        self.files = self.queryDbs(api,path=self.datasetPath,runselection=runselection,useParent=useparent)
162  
163          anFileBlocks = []
164 <        if self.skipBlocks: anFileBlocks = readTXTfile(self, fileBlocks_FileName)
164 >        if self.skipBlocks: anFileBlocks = readTXTfile(self, fileBlocks_FileName)
165  
166          # parse files and fill arrays
167          for file in self.files :
168 <            parList = []
168 >            parList  = []
169 >            fileLumis = [] # List of tuples
170              # skip already analyzed blocks
171              fileblock = file['Block']['Name']
172              if fileblock not in anFileBlocks :
173                  filename = file['LogicalFileName']
174 <                # asked retry the list of parent for the given child
175 <                if useparent==1: parList = [x['LogicalFileName'] for x in file['ParentList']]
176 <                self.parent[filename] = parList
174 >                # asked retry the list of parent for the given child
175 >                if useparent==1:
176 >                    parList = [x['LogicalFileName'] for x in file['ParentList']]
177 >                if self.ads or self.lumiMask or self.lumiParams:
178 >                    fileLumis = [ (x['RunNumber'], x['LumiSectionNumber'])
179 >                                 for x in file['LumiList'] ]
180 >                self.parent[filename] = parList
181 >                # For LumiMask, intersection of two lists.
182 >                if self.lumiMask:
183 >                    self.lumis[filename] = lumiList.filterLumis(fileLumis)
184 >                else:
185 >                    self.lumis[filename] = fileLumis
186                  if filename.find('.dat') < 0 :
187                      events    = file['NumberOfEvents']
188 <                    # number of events per block
188 >                    # Count number of events and lumis per block
189                      if fileblock in self.eventsPerBlock.keys() :
190                          self.eventsPerBlock[fileblock] += events
191                      else :
192                          self.eventsPerBlock[fileblock] = events
193 <                    # number of events per file
193 >                    # Number of events per file
194                      self.eventsPerFile[filename] = events
195 <            
196 <                    # number of events per block
195 >
196 >                    # List of files per block
197                      if fileblock in self.blocksinfo.keys() :
198                          self.blocksinfo[fileblock].append(filename)
199                      else :
200                          self.blocksinfo[fileblock] = [filename]
201 <            
201 >
202                      # total number of events
203                      self.maxEvents += events
204 +                    self.maxLumis  += len(self.lumis[filename])
205 +
206          if  self.skipBlocks and len(self.eventsPerBlock.keys()) == 0:
207              msg = "No new fileblocks available for dataset: "+str(self.datasetPath)
208 <            raise  CrabException(msg)    
208 >            raise  CrabException(msg)
209  
210 <        saveFblocks=''
210 >        saveFblocks=''
211          for block in self.eventsPerBlock.keys() :
212 <            saveFblocks += str(block)+'\n'
213 <            common.logger.debug(6,"DBSInfo: total nevts %i in block %s "%(self.eventsPerBlock[block],block))
214 <        writeTXTfile(self, fileBlocks_FileName , saveFblocks)
215 <                      
212 >            saveFblocks += str(block)+'\n'
213 >            common.logger.log(10-1,"DBSInfo: total nevts %i in block %s "%(self.eventsPerBlock[block],block))
214 >        writeTXTfile(self, fileBlocks_FileName , saveFblocks)
215 >
216          if len(self.eventsPerBlock) <= 0:
217              raise NotExistingDatasetError(("\nNo data for %s in DBS\nPlease check"
218                                              + " dataset path variables in crab.cfg")
219                                              % self.datasetPath)
220  
221  
222 < ###########################
222 >    def queryDbs(self,api,path=None,runselection=None,useParent=None):
223  
224 <    def queryDbs(self,api,path=None,runNumber=None,useParent=None):
225 <
226 <        allowedRetriveValue = [#'retrive_parent',
227 <                               'retrive_block',
228 <                               #'retrive_lumi',
190 <                               'retrive_run'
191 <                               ]
224 >        allowedRetriveValue = ['retrive_block', 'retrive_run']
225 >        if self.ads or self.lumiMask or self.lumiParams:
226 >            allowedRetriveValue.append('retrive_lumi')
227 >        if useParent == 1: allowedRetriveValue.append('retrive_parent')
228 >        common.logger.debug("Set of input parameters used for DBS query: %s" % allowedRetriveValue)
229          try:
230 <            if not runNumber :
231 <                if useParent==1 or self.splitByRun==1 :
232 <                    if self.ads==1 :          
233 <                        files = api.listFiles(analysisDataset=path, retriveList=allowedRetriveValue)
230 >            if len(runselection) <=0 :
231 >                if useParent==1 or self.splitByRun==1 or self.ads or self.lumiMask or self.lumiParams:
232 >                    if self.ads:
233 >                        files = api.listFiles(analysisDataset=path, retriveList=allowedRetriveValue)
234                      else :
235 <                        files = api.listFiles(path=path, retriveList=allowedRetriveValue)
199 <                    common.logger.debug(5,"Set of input parameters used for DBS query : \n"+str(allowedRetriveValue))
200 <                    common.logger.write("Set of input parameters used for DBS query : \n"+str(allowedRetriveValue))
235 >                        files = api.listFiles(path=path, retriveList=allowedRetriveValue)
236                  else:
202                    print 'MALE2'
237                      files = api.listDatasetFiles(self.datasetPath)
238              else :
239                  files=[]
240                  for arun in runselection:
241                      try:
242 <                        if self.ads==1 : filesinrun = api.listFiles(analysisDataset=path,retriveList=allowedRetriveValue,runNumber=arun)
243 <                        else: filesinrun = api.listFiles(path=path,retriveList=allowedRetriveValue,runNumber=arun)
242 >                        if self.ads:
243 >                            filesinrun = api.listFiles(analysisDataset=path,retriveList=allowedRetriveValue,runNumber=arun)
244 >                        else:
245 >                            filesinrun = api.listFiles(path=path,retriveList=allowedRetriveValue,runNumber=arun)
246                          files.extend(filesinrun)
247                      except:
248                          msg="WARNING: problem extracting info from DBS for run %s "%arun
249 <                        common.logger.message(msg)
249 >                        common.logger.info(msg)
250                          pass
251  
252          except DbsBadRequest, msg:
# Line 220 | Line 256 | class DataDiscovery:
256  
257          return files
258  
259 < # #################################################
259 >
260      def getMaxEvents(self):
261          """
262 <        max events
262 >        max events
263          """
264          return self.maxEvents
265  
266 < # #################################################
266 >
267 >    def getMaxLumis(self):
268 >        """
269 >        Return the number of lumis in the dataset
270 >        """
271 >        return self.maxLumis
272 >
273 >
274      def getEventsPerBlock(self):
275          """
276 <        list the event collections structure by fileblock
276 >        list the event collections structure by fileblock
277          """
278          return self.eventsPerBlock
279  
280 < # #################################################
280 >
281      def getEventsPerFile(self):
282          """
283 <        list the event collections structure by file
283 >        list the event collections structure by file
284          """
285          return self.eventsPerFile
286  
287 < # #################################################
287 >
288      def getFiles(self):
289          """
290 <        return files grouped by fileblock
290 >        return files grouped by fileblock
291          """
292 <        return self.blocksinfo        
292 >        return self.blocksinfo
293 >
294  
251 # #################################################
295      def getParent(self):
296          """
297 <        return parent grouped by file
297 >        return parent grouped by file
298          """
299 <        return self.parent        
299 >        return self.parent
300  
301 < # #################################################
302 <    def getListFiles(self):
301 >
302 >    def getLumis(self):
303          """
304 <        return parent grouped by file
304 >        return lumi sections grouped by file
305          """
306 <        return self.files        
306 >        return self.lumis
307  
308 < ########################################################################
308 >
309 >    def getListFiles(self):
310 >        """
311 >        return parent grouped by file
312 >        """
313 >        return self.files

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines