ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/DataDiscovery.py
Revision: 1.22
Committed: Mon Jun 9 15:53:05 2008 UTC (16 years, 10 months ago) by spiga
Content type: text/x-python
Branch: MAIN
Changes since 1.21: +54 -39 lines
Log Message:
improved support for two tiers input. Added support to skip already analyzed Bloks

File Contents

# User Rev Content
1 gutsche 1.6 #!/usr/bin/env python
2 slacapra 1.18 import exceptions
3     import DBSAPI.dbsApi
4     from DBSAPI.dbsApiException import *
5     import common
6     from crab_util import *
7 spiga 1.22 import os
8 afanfani 1.1
9 afanfani 1.3
10 slacapra 1.18 # #######################################
11     class DBSError(exceptions.Exception):
12     def __init__(self, errorName, errorMessage):
13     args='\nERROR DBS %s : %s \n'%(errorName,errorMessage)
14     exceptions.Exception.__init__(self, args)
15     pass
16    
17     def getErrorMessage(self):
18     """ Return error message """
19     return "%s" % (self.args)
20    
21     # #######################################
22     class DBSInvalidDataTierError(exceptions.Exception):
23     def __init__(self, errorName, errorMessage):
24     args='\nERROR DBS %s : %s \n'%(errorName,errorMessage)
25     exceptions.Exception.__init__(self, args)
26     pass
27    
28     def getErrorMessage(self):
29     """ Return error message """
30     return "%s" % (self.args)
31    
32     # #######################################
33     class DBSInfoError:
34     def __init__(self, url):
35     print '\nERROR accessing DBS url : '+url+'\n'
36     pass
37    
38 afanfani 1.1 # ####################################
39 afanfani 1.3 class DataDiscoveryError(exceptions.Exception):
40 slacapra 1.7 def __init__(self, errorMessage):
41 gutsche 1.15 self.args=errorMessage
42 slacapra 1.14 exceptions.Exception.__init__(self, self.args)
43 slacapra 1.7 pass
44    
45     def getErrorMessage(self):
46     """ Return exception error """
47     return "%s" % (self.args)
48 afanfani 1.3
49 afanfani 1.1 # ####################################
50 afanfani 1.3 class NotExistingDatasetError(exceptions.Exception):
51 slacapra 1.7 def __init__(self, errorMessage):
52 gutsche 1.15 self.args=errorMessage
53 slacapra 1.14 exceptions.Exception.__init__(self, self.args)
54 slacapra 1.7 pass
55    
56     def getErrorMessage(self):
57     """ Return exception error """
58     return "%s" % (self.args)
59 afanfani 1.1
60     # ####################################
61 afanfani 1.3 class NoDataTierinProvenanceError(exceptions.Exception):
62 slacapra 1.7 def __init__(self, errorMessage):
63 gutsche 1.15 self.args=errorMessage
64 slacapra 1.14 exceptions.Exception.__init__(self, self.args)
65 slacapra 1.7 pass
66    
67     def getErrorMessage(self):
68     """ Return exception error """
69     return "%s" % (self.args)
70 afanfani 1.1
71     # ####################################
72     # class to find and extact info from published data
73     class DataDiscovery:
74 spiga 1.22 def __init__(self, datasetPath, cfg_params, skipAnBlocks):
75 afanfani 1.1
76 slacapra 1.18 # Attributes
77 slacapra 1.11 self.datasetPath = datasetPath
78 afanfani 1.1 self.cfg_params = cfg_params
79 spiga 1.22 self.skipBlocks = skipAnBlocks
80 afanfani 1.1
81 slacapra 1.11 self.eventsPerBlock = {} # DBS output: map fileblocks-events for collection
82     self.eventsPerFile = {} # DBS output: map files-events
83 slacapra 1.18 self.blocksinfo = {} # DBS output: map fileblocks-files
84     self.maxEvents = 0 # DBS output: max events
85 spiga 1.21 self.parent = {} # DBS output: max events
86 afanfani 1.1
87     # ####################################
88     def fetchDBSInfo(self):
89     """
90     Contact DBS
91     """
92    
93 slacapra 1.11 ## get DBS URL
94 slacapra 1.19 dbs_url="http://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet"
95     if (self.cfg_params.has_key('CMSSW.dbs_url')):
96 slacapra 1.11 dbs_url=self.cfg_params['CMSSW.dbs_url']
97 afanfani 1.3
98 slacapra 1.18 common.logger.debug(3,"Accessing DBS at: "+dbs_url)
99    
100     ## check if runs are selected
101 slacapra 1.19 runselection = []
102     if (self.cfg_params.has_key('CMSSW.runselection')):
103 slacapra 1.18 runselection = parseRange2(self.cfg_params['CMSSW.runselection'])
104    
105 afanfani 1.20 common.logger.debug(6,"runselection is: %s"%runselection)
106 slacapra 1.18 ## service API
107     args = {}
108     args['url'] = dbs_url
109     args['level'] = 'CRITICAL'
110    
111 spiga 1.21 ## check if has been requested to use the parent info
112 spiga 1.22 useParent = self.cfg_params.get('CMSSW.use_parent',False)
113 spiga 1.21
114 spiga 1.22 ## check if has been asked for a non default file to store/read analyzed fileBlocks
115     defaultName = common.work_space.shareDir()+'AnalyzedBlocks.txt'
116     fileBlocks_FileName = os.path.abspath(self.cfg_params.get('CMSSW.fileblocks_file',defaultName))
117    
118 slacapra 1.18 api = DBSAPI.dbsApi.DbsApi(args)
119 afanfani 1.5 try:
120 slacapra 1.18 if len(runselection) <= 0 :
121 spiga 1.22 if useParent:
122     files = api.listFiles(path=self.datasetPath,retriveList= [
123     'retrive_parent',
124     'retrive_block',
125     'retrive_lumi',
126     'retrive_run'
127     ])
128     common.logger.debug(5,"Set of input parameters used for DBS query : \n"+str(allowedRetriveValue))
129     common.logger.write("Set of input parameters used for DBS query : \n"+str(allowedRetriveValue))
130     else:
131     files = api.listDatasetFiles(self.datasetPath)
132 slacapra 1.18 else :
133 afanfani 1.20 files=[]
134     for arun in runselection:
135     try:
136 spiga 1.21 filesinrun = api.listFiles(path=self.datasetPath,retriveList=allowedRetriveValue,runNumber=arun)
137 afanfani 1.20 files.extend(filesinrun)
138     except:
139     msg="WARNING: problem extracting info from DBS for run %s "%arun
140     common.logger.message(msg)
141     pass
142    
143 slacapra 1.18 except DbsBadRequest, msg:
144 slacapra 1.11 raise DataDiscoveryError(msg)
145 slacapra 1.13 except DBSError, msg:
146     raise DataDiscoveryError(msg)
147 slacapra 1.11
148 spiga 1.22 anFileBlocks = []
149     if self.skipBlocks: anFileBlocks = readTXTfile(self, fileBlocks_FileName)
150    
151 slacapra 1.18 # parse files and fill arrays
152     for file in files :
153 spiga 1.21 parList = []
154 spiga 1.22 # skip already analyzed blocks
155     fileblock = file['Block']['Name']
156     if fileblock not in anFileBlocks :
157     filename = file['LogicalFileName']
158     # asked retry the list of parent for the given child
159     if useParent: parList = [x['LogicalFileName'] for x in file['ParentList']]
160     self.parent[filename] = parList
161     if filename.find('.dat') < 0 :
162     events = file['NumberOfEvents']
163     # number of events per block
164     if fileblock in self.eventsPerBlock.keys() :
165     self.eventsPerBlock[fileblock] += events
166     else :
167     self.eventsPerBlock[fileblock] = events
168     # number of events per file
169     self.eventsPerFile[filename] = events
170    
171     # number of events per block
172     if fileblock in self.blocksinfo.keys() :
173     self.blocksinfo[fileblock].append(filename)
174     else :
175     self.blocksinfo[fileblock] = [filename]
176    
177     # total number of events
178     self.maxEvents += events
179     if self.skipBlocks and len(self.eventsPerBlock.keys()) == 0:
180     msg = "No new fileblocks available for dataset: "+str(self.datasetPath)
181     raise CrabException(msg)
182 slacapra 1.11
183 spiga 1.22 saveFblocks=''
184 slacapra 1.18 for block in self.eventsPerBlock.keys() :
185 spiga 1.22 saveFblocks += str(block)+'\n'
186 slacapra 1.18 common.logger.debug(6,"DBSInfo: total nevts %i in block %s "%(self.eventsPerBlock[block],block))
187 spiga 1.22 writeTXTfile(self, fileBlocks_FileName , saveFblocks)
188    
189 slacapra 1.11 if len(self.eventsPerBlock) <= 0:
190 slacapra 1.18 raise NotExistingDatasetError(("\nNo data for %s in DBS\nPlease check"
191 slacapra 1.11 + " dataset path variables in crab.cfg")
192 slacapra 1.18 % self.datasetPath)
193 afanfani 1.1
194    
195     # #################################################
196     def getMaxEvents(self):
197     """
198 slacapra 1.11 max events
199 afanfani 1.1 """
200 slacapra 1.18 return self.maxEvents
201 afanfani 1.1
202     # #################################################
203 slacapra 1.11 def getEventsPerBlock(self):
204 afanfani 1.1 """
205 slacapra 1.11 list the event collections structure by fileblock
206 afanfani 1.1 """
207 slacapra 1.11 return self.eventsPerBlock
208 afanfani 1.1
209     # #################################################
210 slacapra 1.11 def getEventsPerFile(self):
211 afanfani 1.1 """
212 slacapra 1.11 list the event collections structure by file
213 afanfani 1.1 """
214 slacapra 1.11 return self.eventsPerFile
215 afanfani 1.1
216     # #################################################
217 slacapra 1.11 def getFiles(self):
218 afanfani 1.1 """
219 slacapra 1.11 return files grouped by fileblock
220 afanfani 1.1 """
221 slacapra 1.11 return self.blocksinfo
222 afanfani 1.1
223 spiga 1.21 # #################################################
224     def getParent(self):
225     """
226     return parent grouped by file
227     """
228     return self.parent
229    
230 afanfani 1.1 ########################################################################