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.7 by slacapra, Thu Jul 6 10:22:47 2006 UTC vs.
Revision 1.20 by afanfani, Fri Jan 11 22:11:55 2008 UTC

# Line 1 | Line 1
1   #!/usr/bin/env python
2 < import sys, os, string, re
3 < from DBSInfo import *
2 > import exceptions
3 > import DBSAPI.dbsApi
4 > from DBSAPI.dbsApiException import *
5 > import common
6 > from crab_util import *
7  
8  
9 + # #######################################
10 + class DBSError(exceptions.Exception):
11 +    def __init__(self, errorName, errorMessage):
12 +        args='\nERROR DBS %s : %s \n'%(errorName,errorMessage)
13 +        exceptions.Exception.__init__(self, args)
14 +        pass
15 +    
16 +    def getErrorMessage(self):
17 +        """ Return error message """
18 +        return "%s" % (self.args)
19 +
20 + # #######################################
21 + class DBSInvalidDataTierError(exceptions.Exception):
22 +    def __init__(self, errorName, errorMessage):
23 +        args='\nERROR DBS %s : %s \n'%(errorName,errorMessage)
24 +        exceptions.Exception.__init__(self, args)
25 +        pass
26 +    
27 +    def getErrorMessage(self):
28 +        """ Return error message """
29 +        return "%s" % (self.args)
30 +
31 + # #######################################
32 + class DBSInfoError:
33 +    def __init__(self, url):
34 +        print '\nERROR accessing DBS url : '+url+'\n'
35 +        pass
36 +
37   # ####################################
38   class DataDiscoveryError(exceptions.Exception):
39      def __init__(self, errorMessage):
40 <        args=errorMessage
41 <        exceptions.Exception.__init__(self, args)
40 >        self.args=errorMessage
41 >        exceptions.Exception.__init__(self, self.args)
42          pass
43  
44      def getErrorMessage(self):
# Line 17 | Line 48 | class DataDiscoveryError(exceptions.Exce
48   # ####################################
49   class NotExistingDatasetError(exceptions.Exception):
50      def __init__(self, errorMessage):
51 <        args=errorMessage
52 <        exceptions.Exception.__init__(self, args)
51 >        self.args=errorMessage
52 >        exceptions.Exception.__init__(self, self.args)
53          pass
54  
55      def getErrorMessage(self):
# Line 28 | Line 59 | class NotExistingDatasetError(exceptions
59   # ####################################
60   class NoDataTierinProvenanceError(exceptions.Exception):
61      def __init__(self, errorMessage):
62 <        args=errorMessage
63 <        exceptions.Exception.__init__(self, args)
62 >        self.args=errorMessage
63 >        exceptions.Exception.__init__(self, self.args)
64          pass
65  
66      def getErrorMessage(self):
# Line 39 | Line 70 | class NoDataTierinProvenanceError(except
70   # ####################################
71   # class to find and extact info from published data
72   class DataDiscovery:
73 <    def __init__(self, owner, dataset, dataTiers, cfg_params):
73 >    def __init__(self, datasetPath, cfg_params):
74  
75 < #       Attributes
76 <        self.owner = owner
46 <        self.dataset = dataset
47 <        self.dataTiers = dataTiers
75 >        #       Attributes
76 >        self.datasetPath = datasetPath
77          self.cfg_params = cfg_params
78  
79 <        self.dbspaths= []     # DBS output: list of dbspaths for all data
80 <        self.allblocks = []   # DBS output: list of map fileblocks-totevts for all dataset-owners
81 <        self.blocksinfo = {}  # DBS output: map fileblocks-totevts for the primary block, used internally to this class
82 < #DBS output: max events computed by method getMaxEvents
79 >        self.eventsPerBlock = {}  # DBS output: map fileblocks-events for collection
80 >        self.eventsPerFile = {}   # DBS output: map files-events
81 >        self.blocksinfo = {}      # DBS output: map fileblocks-files
82 >        self.maxEvents = 0        # DBS output: max events
83  
84   # ####################################
85      def fetchDBSInfo(self):
# Line 58 | Line 87 | class DataDiscovery:
87          Contact DBS
88          """
89  
90 <        ## add the PU among the required data tiers if the Digi are requested
91 <        if (self.dataTiers.count('Digi')>0) & (self.dataTiers.count('PU')<=0) :
92 <            self.dataTiers.append('PU')
90 >        ## get DBS URL
91 >        dbs_url="http://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet"
92 >        if (self.cfg_params.has_key('CMSSW.dbs_url')):
93 >            dbs_url=self.cfg_params['CMSSW.dbs_url']
94 >
95 >        common.logger.debug(3,"Accessing DBS at: "+dbs_url)
96 >
97 >        ## check if runs are selected
98 >        runselection = []
99 >        if (self.cfg_params.has_key('CMSSW.runselection')):
100 >            runselection = parseRange2(self.cfg_params['CMSSW.runselection'])
101 >
102 >        common.logger.debug(6,"runselection is: %s"%runselection)
103 >        ## service API
104 >        args = {}
105 >        args['url']     = dbs_url
106 >        args['level']   = 'CRITICAL'
107  
108 <        ## get info about the requested dataset
66 <        dbs=DBSInfo()
67 <        try:
68 <            self.datasets = dbs.getMatchingDatasets(self.owner, self.dataset)
69 <        except DBSError, ex:
70 <            raise DataDiscoveryError(ex.getErrorMessage())
71 <        if len(self.datasets) == 0:
72 <            raise DataDiscoveryError("Owner=%s, Dataset=%s unknown to DBS" % (self.owner, self.dataset))
73 <        if len(self.datasets) > 1:
74 <            raise DataDiscoveryError("Owner=%s, Dataset=%s is ambiguous" % (self.owner, self.dataset))
108 >        api = DBSAPI.dbsApi.DbsApi(args)
109          try:
110 <            self.dbsdataset = self.datasets[0].get('datasetPathName')
111 <            self.blocksinfo = dbs.getDatasetContents(self.dbsdataset)
112 <            self.allblocks.append (self.blocksinfo.keys ()) # add also the current fileblocksinfo
113 <            self.dbspaths.append(self.dbsdataset)
114 <        except DBSError, ex:
115 <            raise DataDiscoveryError(ex.getErrorMessage())
116 <        
117 <        if len(self.blocksinfo)<=0:
118 <            msg="\nERROR Data for %s do not exist in DBS! \n Check the dataset/owner variables in crab.cfg !"%self.dbsdataset
119 <            raise NotExistingDatasetError(msg)
110 >            if len(runselection) <= 0 :
111 >                files = api.listDatasetFiles(self.datasetPath)
112 >            else :
113 >                files=[]
114 >                for arun in runselection:
115 >                    try:
116 >                        filesinrun = api.listFiles(path=self.datasetPath, details=True,runNumber=arun)
117 >                        files.extend(filesinrun)
118 >                    except:
119 >                        msg="WARNING: problem extracting info from DBS for run %s "%arun
120 >                        common.logger.message(msg)
121 >                        pass
122  
123 <
124 <        ## get info about the parents
125 <        try:
90 <            parents=dbs.getDatasetProvenance(self.dbsdataset, self.dataTiers)
91 <        except DBSInvalidDataTierError, ex:
92 <            msg=ex.getErrorMessage()+' \n Check the data_tier variable in crab.cfg !\n'
123 >        except DbsBadRequest, msg:
124 >            raise DataDiscoveryError(msg)
125 >        except DBSError, msg:
126              raise DataDiscoveryError(msg)
94        except DBSError, ex:
95            raise DataDiscoveryError(ex.getErrorMessage())
96
97        ## check that the user asks for parent Data Tier really existing in the DBS provenance
98        self.checkParentDataTier(parents, self.dataTiers)
99
100        ## for each parent get the corresponding fileblocks
101        try:
102            for p in parents:
103                ## fill a list of dbspaths
104                parentPath = p.get('parent').get('datasetPathName')
105                self.dbspaths.append (parentPath)
106                parentBlocks = dbs.getDatasetContents (parentPath)
107                self.allblocks.append (parentBlocks.keys ())  # add parent fileblocksinfo
108            except DBSError, ex:
109                raise DataDiscoveryError(ex.getErrorMessage())
127  
128 < # #################################################
129 <    def checkParentDataTier(self, parents, dataTiers):
130 <        """
131 <        check that the data tiers requested by the user really exists in the provenance of the given dataset
132 <        """
133 <        startType = string.split(self.dbsdataset,'/')[2]
134 <        # for example 'type' is PU and 'dataTier' is Hit
135 <        parentTypes = map(lambda p: p.get('type'), parents)
136 <        for tier in dataTiers:
137 <            if parentTypes.count(tier) <= 0 and tier != startType:
138 <                msg="\nERROR Data %s not published in DBS with asked data tiers : the data tier not found is %s !\n  Check the data_tier variable in crab.cfg !"%(self.dbsdataset,tier)
139 <                raise  NoDataTierinProvenanceError(msg)
128 >        # parse files and fill arrays
129 >        for file in files :
130 >            filename = file['LogicalFileName']
131 >            if filename.find('.dat') < 0 :
132 >                fileblock = file['Block']['Name']
133 >                events    = file['NumberOfEvents']
134 >                # number of events per block
135 >                if fileblock in self.eventsPerBlock.keys() :
136 >                    self.eventsPerBlock[fileblock] += events
137 >                else :
138 >                    self.eventsPerBlock[fileblock] = events
139 >                # number of events per file
140 >                self.eventsPerFile[filename] = events
141 >
142 >                # number of events per block
143 >                if fileblock in self.blocksinfo.keys() :
144 >                    self.blocksinfo[fileblock].append(filename)
145 >                else :
146 >                    self.blocksinfo[fileblock] = [filename]
147 >
148 >                # total number of events
149 >                self.maxEvents += events
150 >
151 >        for block in self.eventsPerBlock.keys() :
152 >            common.logger.debug(6,"DBSInfo: total nevts %i in block %s "%(self.eventsPerBlock[block],block))
153 >
154 >        if len(self.eventsPerBlock) <= 0:
155 >            raise NotExistingDatasetError(("\nNo data for %s in DBS\nPlease check"
156 >                                            + " dataset path variables in crab.cfg")
157 >                                            % self.datasetPath)
158  
159  
160   # #################################################
161      def getMaxEvents(self):
162          """
163 <        max events of the primary dataset-owner
163 >        max events
164          """
165 <        ## loop over the fileblocks of the primary dataset-owner
131 <        nevts=0      
132 <        for blockevts in self.blocksinfo.values():
133 <            nevts=nevts+blockevts
134 <
135 <        return nevts
165 >        return self.maxEvents
166  
167   # #################################################
168 <    def getDBSPaths(self):
168 >    def getEventsPerBlock(self):
169          """
170 <        list the DBSpaths for all required data
170 >        list the event collections structure by fileblock
171          """
172 <        return self.dbspaths
172 >        return self.eventsPerBlock
173  
174   # #################################################
175 <    def getEVC(self):
175 >    def getEventsPerFile(self):
176          """
177 <        list the event collections structure by fileblock
177 >        list the event collections structure by file
178          """
179 <        print "To be used by a more complex job splitting... TODO later... "
150 <        print "it requires changes in what's returned by DBSInfo.getDatasetContents and then fetchDBSInfo"
179 >        return self.eventsPerFile
180  
181   # #################################################
182 <    def getFileBlocks(self):
182 >    def getFiles(self):
183          """
184 <        fileblocks for all required dataset-owners
184 >        return files grouped by fileblock
185          """
186 <        return self.allblocks        
186 >        return self.blocksinfo        
187  
188   ########################################################################

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines