CMS 3D CMS Logo

cmsswVersionTools.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import FWCore.ParameterSet.Config as cms
3 
7 from Configuration.AlCa.autoCond import autoCond
8 import Utilities.General.cmssw_das_client as das_client
9 import os
10 import socket
11 
12 
13 ## ------------------------------------------------------
14 ## Automatic pick-up of RelVal input files
15 ## ------------------------------------------------------
16 
18  """ Picks up RelVal input files automatically and
19  returns a vector of strings with the paths to be used in [PoolSource].fileNames
20  PickRelValInputFiles( cmsswVersion, relVal, dataTier, condition, globalTag, maxVersions, skipFiles, numberOfFiles, debug )
21  - useDAS : switch to perform query in DAS rather than in DBS
22  optional; default: False
23  - cmsswVersion : CMSSW release to pick up the RelVal files from
24  optional; default: the current release (determined automatically from environment)
25  - formerVersion: use the last before the last valid CMSSW release to pick up the RelVal files from
26  applies also, if 'cmsswVersion' is set explicitly
27  optional; default: False
28  - relVal : RelVal sample to be used
29  optional; default: 'RelValTTbar'
30  - dataTier : data tier to be used
31  optional; default: 'GEN-SIM-RECO'
32  - condition : identifier of GlobalTag as defined in Configurations/PyReleaseValidation/python/autoCond.py
33  possibly overwritten, if 'globalTag' is set explicitly
34  optional; default: 'startup'
35  - globalTag : name of GlobalTag as it is used in the data path of the RelVals
36  optional; default: determined automatically as defined by 'condition' in Configurations/PyReleaseValidation/python/autoCond.py
37  !!! Determination is done for the release one runs in, not for the release the RelVals have been produced in.
38  !!! Example of deviation: data RelVals (CMSSW_4_1_X) might not only have the pure name of the GlobalTag 'GR_R_311_V2' in the full path,
39  but also an extension identifying the data: 'GR_R_311_V2_RelVal_wzMu2010B'
40  - maxVersions : max. versioning number of RelVal to check
41  optional; default: 9
42  - skipFiles : number of files to skip for a found RelVal sample
43  optional; default: 0
44  - numberOfFiles: number of files to pick up
45  setting it to negative values, returns all found ('skipFiles' remains active though)
46  optional; default: -1
47  - debug : switch to enable enhanced messages in 'stdout'
48  optional; default: False
49  """
50 
51  _label = 'pickRelValInputFiles'
52  _defaultParameters = dicttypes.SortedKeysDict()
53 
54  def getDefaultParameters( self ):
55  return self._defaultParameters
56 
57  def __init__( self ):
58  ConfigToolBase.__init__( self )
59  self.addParameter( self._defaultParameters, 'useDAS' , False , '' )
60  self.addParameter( self._defaultParameters, 'cmsswVersion' , os.getenv( "CMSSW_VERSION" ) , 'auto from environment' )
61  self.addParameter( self._defaultParameters, 'formerVersion', False , '' )
62  self.addParameter( self._defaultParameters, 'relVal' , 'RelValTTbar' , '' )
63  self.addParameter( self._defaultParameters, 'dataTier' , 'GEN-SIM-RECO' , '' )
64  self.addParameter( self._defaultParameters, 'condition' , 'startup' , '' )
65  gt = autoCond[ self.getDefaultParameters()[ 'condition' ].value ]
66  if isinstance(gt,tuple) or isinstance(gt,list):
67  gt = gt[0]
68  self.addParameter( self._defaultParameters, 'globalTag' , gt[ : -5 ] , 'auto from \'condition\'' )
69  self.addParameter( self._defaultParameters, 'maxVersions' , 3 , '' )
70  self.addParameter( self._defaultParameters, 'skipFiles' , 0 , '' )
71  self.addParameter( self._defaultParameters, 'numberOfFiles', -1 , 'all' )
72  self.addParameter( self._defaultParameters, 'debug' , False , '' )
73  self._parameters = copy.deepcopy( self._defaultParameters )
74  self._comment = ""
75 
76  def __call__( self
77  , useDAS = None
78  , cmsswVersion = None
79  , formerVersion = None
80  , relVal = None
81  , dataTier = None
82  , condition = None
83  , globalTag = None
84  , maxVersions = None
85  , skipFiles = None
86  , numberOfFiles = None
87  , debug = None
88  ):
89  if useDAS is None:
90  useDAS = self.getDefaultParameters()[ 'useDAS' ].value
91  if cmsswVersion is None:
92  cmsswVersion = self.getDefaultParameters()[ 'cmsswVersion' ].value
93  if formerVersion is None:
94  formerVersion = self.getDefaultParameters()[ 'formerVersion' ].value
95  if relVal is None:
96  relVal = self.getDefaultParameters()[ 'relVal' ].value
97  if dataTier is None:
98  dataTier = self.getDefaultParameters()[ 'dataTier' ].value
99  if condition is None:
100  condition = self.getDefaultParameters()[ 'condition' ].value
101  if globalTag is None:
102  globalTag = autoCond[ condition ][ : -5 ] # auto from 'condition'
103  if maxVersions is None:
104  maxVersions = self.getDefaultParameters()[ 'maxVersions' ].value
105  if skipFiles is None:
106  skipFiles = self.getDefaultParameters()[ 'skipFiles' ].value
107  if numberOfFiles is None:
108  numberOfFiles = self.getDefaultParameters()[ 'numberOfFiles' ].value
109  if debug is None:
110  debug = self.getDefaultParameters()[ 'debug' ].value
111  self.setParameter( 'useDAS' , useDAS )
112  self.setParameter( 'cmsswVersion' , cmsswVersion )
113  self.setParameter( 'formerVersion', formerVersion )
114  self.setParameter( 'relVal' , relVal )
115  self.setParameter( 'dataTier' , dataTier )
116  self.setParameter( 'condition' , condition )
117  self.setParameter( 'globalTag' , globalTag )
118  self.setParameter( 'maxVersions' , maxVersions )
119  self.setParameter( 'skipFiles' , skipFiles )
120  self.setParameter( 'numberOfFiles', numberOfFiles )
121  self.setParameter( 'debug' , debug )
122  return self.apply()
123 
124  def messageEmptyList( self ):
125  print('%s DEBUG: Empty file list returned'%( self._label ))
126  print(' This might be overwritten by providing input files explicitly to the source module in the main configuration file.')
127 
128  def apply( self ):
129  useDAS = self._parameters[ 'useDAS' ].value
130  cmsswVersion = self._parameters[ 'cmsswVersion' ].value
131  formerVersion = self._parameters[ 'formerVersion' ].value
132  relVal = self._parameters[ 'relVal' ].value
133  dataTier = self._parameters[ 'dataTier' ].value
134  condition = self._parameters[ 'condition' ].value # only used for GT determination in initialization, if GT not explicitly given
135  globalTag = self._parameters[ 'globalTag' ].value
136  maxVersions = self._parameters[ 'maxVersions' ].value
137  skipFiles = self._parameters[ 'skipFiles' ].value
138  numberOfFiles = self._parameters[ 'numberOfFiles' ].value
139  debug = self._parameters[ 'debug' ].value
140 
141  filePaths = []
142 
143  # Determine corresponding CMSSW version for RelVals
144  preId = '_pre'
145  patchId = '_patch' # patch releases
146  hltPatchId = '_hltpatch' # HLT patch releases
147  dqmPatchId = '_dqmpatch' # DQM patch releases
148  slhcId = '_SLHC' # SLHC releases
149  rootId = '_root' # ROOT test releases
150  ibId = '_X_' # IBs
151  if patchId in cmsswVersion:
152  cmsswVersion = cmsswVersion.split( patchId )[ 0 ]
153  elif hltPatchId in cmsswVersion:
154  cmsswVersion = cmsswVersion.split( hltPatchId )[ 0 ]
155  elif dqmPatchId in cmsswVersion:
156  cmsswVersion = cmsswVersion.split( dqmPatchId )[ 0 ]
157  elif rootId in cmsswVersion:
158  cmsswVersion = cmsswVersion.split( rootId )[ 0 ]
159  elif slhcId in cmsswVersion:
160  cmsswVersion = cmsswVersion.split( slhcId )[ 0 ]
161  elif ibId in cmsswVersion or formerVersion:
162  outputTuple = Popen( [ 'scram', 'l -c CMSSW' ], stdout = PIPE, stderr = PIPE ).communicate()
163  if len( outputTuple[ 1 ] ) != 0:
164  print('%s INFO : SCRAM error'%( self._label ))
165  if debug:
166  print(' from trying to determine last valid releases before \'%s\''%( cmsswVersion ))
167  print()
168  print(outputTuple[ 1 ])
169  print()
170  self.messageEmptyList()
171  return filePaths
172  versions = { 'last' :''
173  , 'lastToLast':''
174  }
175  for line in outputTuple[ 0 ].splitlines():
176  version = line.split()[ 1 ]
177  if cmsswVersion.split( ibId )[ 0 ] in version or cmsswVersion.rpartition( '_' )[ 0 ] in version:
178  if not ( patchId in version or hltPatchId in version or dqmPatchId in version or slhcId in version or ibId in version or rootId in version ):
179  versions[ 'lastToLast' ] = versions[ 'last' ]
180  versions[ 'last' ] = version
181  if version == cmsswVersion:
182  break
183  # FIXME: ordering of output problematic ('XYZ_pre10' before 'XYZ_pre2', no "formerVersion" for 'XYZ_pre1')
184  if formerVersion:
185  # Don't use pre-releases as "former version" for other releases than CMSSW_X_Y_0
186  if preId in versions[ 'lastToLast' ] and not preId in versions[ 'last' ] and not versions[ 'last' ].endswith( '_0' ):
187  versions[ 'lastToLast' ] = versions[ 'lastToLast' ].split( preId )[ 0 ] # works only, if 'CMSSW_X_Y_0' esists ;-)
188  # Use pre-release as "former version" for CMSSW_X_Y_0
189  elif versions[ 'last' ].endswith( '_0' ) and not ( preId in versions[ 'lastToLast' ] and versions[ 'lastToLast' ].startswith( versions[ 'last' ] ) ):
190  versions[ 'lastToLast' ] = ''
191  for line in outputTuple[ 0 ].splitlines():
192  version = line.split()[ 1 ]
193  versionParts = version.partition( preId )
194  if versionParts[ 0 ] == versions[ 'last' ] and versionParts[ 1 ] == preId:
195  versions[ 'lastToLast' ] = version
196  elif versions[ 'lastToLast' ] != '':
197  break
198  # Don't use CMSSW_X_Y_0 as "former version" for pre-releases
199  elif preId in versions[ 'last' ] and not preId in versions[ 'lastToLast' ] and versions[ 'lastToLast' ].endswith( '_0' ):
200  versions[ 'lastToLast' ] = '' # no alternative :-(
201  cmsswVersion = versions[ 'lastToLast' ]
202  else:
203  cmsswVersion = versions[ 'last' ]
204 
205  # Debugging output
206  if debug:
207  print('%s DEBUG: Called with...'%( self._label ))
208  for key in self._parameters.keys():
209  print(' %s:\t'%( key ), end=' ')
210  print(self._parameters[ key ].value, end=' ')
211  if self._parameters[ key ].value is self.getDefaultParameters()[ key ].value:
212  print(' (default)')
213  else:
214  print()
215  if key == 'cmsswVersion' and cmsswVersion != self._parameters[ key ].value:
216  if formerVersion:
217  print(' ==> modified to last to last valid release %s (s. \'formerVersion\' parameter)'%( cmsswVersion ))
218  else:
219  print(' ==> modified to last valid release %s'%( cmsswVersion ))
220 
221  # Check domain
222  domain = socket.getfqdn().split( '.' )
223  domainSE = ''
224  if len( domain ) == 0:
225  print('%s INFO : Cannot determine domain of this computer'%( self._label ))
226  if debug:
227  self.messageEmptyList()
228  return filePaths
229  elif os.uname()[0] == "Darwin":
230  print('%s INFO : Running on MacOSX without direct access to RelVal files.'%( self._label ))
231  if debug:
232  self.messageEmptyList()
233  return filePaths
234  elif len( domain ) == 1:
235  print('%s INFO : Running on local host \'%s\' without direct access to RelVal files'%( self._label, domain[ 0 ] ))
236  if debug:
237  self.messageEmptyList()
238  return filePaths
239  if not ( ( domain[ -2 ] == 'cern' and domain[ -1 ] == 'ch' ) or ( domain[ -2 ] == 'fnal' and domain[ -1 ] == 'gov' ) ):
240  print('%s INFO : Running on site \'%s.%s\' without direct access to RelVal files'%( self._label, domain[ -2 ], domain[ -1 ] ))
241  if debug:
242  self.messageEmptyList()
243  return filePaths
244  if domain[ -2 ] == 'cern':
245  domainSE = 'T2_CH_CERN'
246  elif domain[ -2 ] == 'fnal':
247  domainSE = 'T1_US_FNAL_MSS'
248  if debug:
249  print('%s DEBUG: Running at site \'%s.%s\''%( self._label, domain[ -2 ], domain[ -1 ] ))
250  print('%s DEBUG: Looking for SE \'%s\''%( self._label, domainSE ))
251 
252  # Find files
253  validVersion = 0
254  dataset = ''
255  datasetAll = '/%s/%s-%s-v*/%s'%( relVal, cmsswVersion, globalTag, dataTier )
256  if useDAS:
257  if debug:
258  print('%s DEBUG: Using DAS query'%( self._label ))
259  dasLimit = numberOfFiles
260  if dasLimit <= 0:
261  dasLimit = 1
262  for version in range( maxVersions, 0, -1 ):
263  filePaths = []
264  filePathsTmp = []
265  fileCount = 0
266  dataset = '/%s/%s-%s-v%i/%s'%( relVal, cmsswVersion, globalTag, version, dataTier )
267  dasQuery = 'file dataset=%s | grep file.name'%( dataset )
268  if debug:
269  print('%s DEBUG: Querying dataset \'%s\' with'%( self._label, dataset ))
270  print(' \'%s\''%( dasQuery ))
271  jsondict = das_client.get_data(dasQuery,dasLimit)
272  if debug:
273  print('%s DEBUG: Received DAS JSON dictionary:'%( self._label ))
274  print(' \'%s\''%( jsondict ))
275  if jsondict[ 'status' ] != 'ok':
276  print('There was a problem while querying DAS with query \'%s\'. Server reply was:\n %s' % (dasQuery, jsondict))
277  exit( 1 )
278  mongo_query = jsondict[ 'mongo_query' ]
279  filters = mongo_query[ 'filters' ]
280  data = jsondict[ 'data' ]
281  if debug:
282  print('%s DEBUG: Query in JSON dictionary:'%( self._label ))
283  print(' \'%s\''%( mongo_query ))
284  print('%s DEBUG: Filters in query:'%( self._label ))
285  print(' \'%s\''%( filters ))
286  print('%s DEBUG: Data in JSON dictionary:'%( self._label ))
287  print(' \'%s\''%( data ))
288  for row in data:
289  filePath = [ r for r in das_client.get_value( row, filters[ 'grep' ] ) ][ 0 ]
290  if debug:
291  print('%s DEBUG: Testing file entry \'%s\''%( self._label, filePath ))
292  if len( filePath ) > 0:
293  if validVersion != version:
294  jsontestdict = das_client.get_data('site dataset=%s | grep site.name' % ( dataset ), 999)
295  mongo_testquery = jsontestdict[ 'mongo_query' ]
296  testfilters = mongo_testquery[ 'filters' ]
297  testdata = jsontestdict[ 'data' ]
298  if debug:
299  print('%s DEBUG: Received DAS JSON dictionary (site test):'%( self._label ))
300  print(' \'%s\''%( jsontestdict ))
301  print('%s DEBUG: Query in JSON dictionary (site test):'%( self._label ))
302  print(' \'%s\''%( mongo_testquery ))
303  print('%s DEBUG: Filters in query (site test):'%( self._label ))
304  print(' \'%s\''%( testfilters ))
305  print('%s DEBUG: Data in JSON dictionary (site test):'%( self._label ))
306  print(' \'%s\''%( testdata ))
307  foundSE = False
308  for testrow in testdata:
309  siteName = [ tr for tr in das_client.get_value( testrow, testfilters[ 'grep' ] ) ][ 0 ]
310  if siteName == domainSE:
311  foundSE = True
312  break
313  if not foundSE:
314  if debug:
315  print('%s DEBUG: Possible version \'v%s\' not available on SE \'%s\''%( self._label, version, domainSE ))
316  break
317  validVersion = version
318  if debug:
319  print('%s DEBUG: Valid version set to \'v%i\''%( self._label, validVersion ))
320  if numberOfFiles == 0:
321  break
322  # protect from double entries ( 'unique' flag in query does not work here)
323  if not filePath in filePathsTmp:
324  filePathsTmp.append( filePath )
325  if debug:
326  print('%s DEBUG: File \'%s\' found'%( self._label, filePath ))
327  fileCount += 1
328  # needed, since and "limit" overrides "idx" in 'get_data' (==> "idx" set to '0' rather than "skipFiles")
329  if fileCount > skipFiles:
330  filePaths.append( filePath )
331  elif debug:
332  print('%s DEBUG: File \'%s\' found again'%( self._label, filePath ))
333  if validVersion > 0:
334  if numberOfFiles == 0 and debug:
335  print('%s DEBUG: No files requested'%( self._label ))
336  break
337  else:
338  if debug:
339  print('%s DEBUG: Using DBS query'%( self._label ))
340  print('%s WARNING: DBS query disabled for DBS3 transition to new API'%( self._label ))
341  #for version in range( maxVersions, 0, -1 ):
342  #filePaths = []
343  #fileCount = 0
344  #dataset = '/%s/%s-%s-v%i/%s'%( relVal, cmsswVersion, globalTag, version, dataTier )
345  #dbsQuery = 'find file where dataset = %s'%( dataset )
346  #if debug:
347  #print '%s DEBUG: Querying dataset \'%s\' with'%( self._label, dataset )
348  #print ' \'%s\''%( dbsQuery )
349  #foundSE = False
350  #for line in os.popen( 'dbs search --query="%s"'%( dbsQuery ) ).readlines():
351  #if line.find( '.root' ) != -1:
352  #if validVersion != version:
353  #if not foundSE:
354  #dbsSiteQuery = 'find dataset where dataset = %s and site = %s'%( dataset, domainSE )
355  #if debug:
356  #print '%s DEBUG: Querying site \'%s\' with'%( self._label, domainSE )
357  #print ' \'%s\''%( dbsSiteQuery )
358  #for lineSite in os.popen( 'dbs search --query="%s"'%( dbsSiteQuery ) ).readlines():
359  #if lineSite.find( dataset ) != -1:
360  #foundSE = True
361  #break
362  #if not foundSE:
363  #if debug:
364  #print '%s DEBUG: Possible version \'v%s\' not available on SE \'%s\''%( self._label, version, domainSE )
365  #break
366  #validVersion = version
367  #if debug:
368  #print '%s DEBUG: Valid version set to \'v%i\''%( self._label, validVersion )
369  #if numberOfFiles == 0:
370  #break
371  #filePath = line.replace( '\n', '' )
372  #if debug:
373  #print '%s DEBUG: File \'%s\' found'%( self._label, filePath )
374  #fileCount += 1
375  #if fileCount > skipFiles:
376  #filePaths.append( filePath )
377  #if not numberOfFiles < 0:
378  #if numberOfFiles <= len( filePaths ):
379  #break
380  #if validVersion > 0:
381  #if numberOfFiles == 0 and debug:
382  #print '%s DEBUG: No files requested'%( self._label )
383  #break
384 
385  # Check output and return
386  if validVersion == 0:
387  print('%s WARNING : No RelVal file(s) found at all in datasets \'%s*\' on SE \'%s\''%( self._label, datasetAll, domainSE ))
388  if debug:
389  self.messageEmptyList()
390  elif len( filePaths ) == 0:
391  print('%s WARNING : No RelVal file(s) picked up in dataset \'%s\''%( self._label, dataset ))
392  if debug:
393  self.messageEmptyList()
394  elif len( filePaths ) < numberOfFiles:
395  print('%s INFO : Only %i RelVal file(s) instead of %i picked up in dataset \'%s\''%( self._label, len( filePaths ), numberOfFiles, dataset ))
396 
397  if debug:
398  print('%s DEBUG: returning %i file(s):\n%s'%( self._label, len( filePaths ), filePaths ))
399  return filePaths
400 
401 pickRelValInputFiles = PickRelValInputFiles()
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
static void * communicate(void *obj)
Definition: DQMNet.cc:1251
def __call__(self, useDAS=None, cmsswVersion=None, formerVersion=None, relVal=None, dataTier=None, condition=None, globalTag=None, maxVersions=None, skipFiles=None, numberOfFiles=None, debug=None)
Automatic pick-up of RelVal input files
double split
Definition: MVATrainer.cc:139