CMS 3D CMS Logo

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