Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:31

0001 import FWCore.ParameterSet.Config as cms
0002 import sys
0003 
0004 def checkPrefix(mainList, inputGTParams):
0005     """ Compares two input GTs to see if they have the same prefix. Returns the index in the internal list of GTs of the match
0006     or -1 in case of no match. """
0007     if inputGTParams.find("_") == -1:
0008         raise Exception("Invalid GT name. It does not contain an _, it cannot be used for replacements.")
0009     prefix = inputGTParams.split("_")[0]
0010     for i in range(0, len(mainList)):
0011         if mainList[i].split("_")[0] == prefix:
0012             return i
0013     return -1
0014 
0015 
0016 from Configuration.AlCa.autoCond import aliases
0017 def combineGTs(globaltag):
0018     gtList = globaltag.split("+")
0019     main = gtList[0]
0020     # Alias expansion
0021     if main in aliases:
0022         main = aliases[main]
0023     mainList = main.split("|")
0024     # Do any requested replacement
0025     if len(gtList) > 1:
0026         for gt in gtList[1:]:
0027             index = checkPrefix(mainList, gt)
0028             if index != -1:
0029                 mainList[index] = gt
0030             else:
0031                 exceptionString = "Error: replacement of GT "+gt+" not allowed. No matching prefix found in existing GT components. Available components are:\n"
0032                 for comp in mainList:
0033                     exceptionString += comp + "\n"
0034                 raise Exception(exceptionString)
0035         mainGT = mainList[0]
0036         if len(mainList) > 1:
0037             for mainPart in mainList[1:]:
0038                 mainGT += mainPart
0039         return mainGT
0040     else:
0041         # Nothing to do, return the input globaltag
0042         return main
0043     
0044 
0045 def GlobalTag(essource = None, globaltag = None, conditions = None):
0046     """Modify the GlobalTag module configuration, allowing to use the extended
0047     Configuration/AlCa/python/autoCond.py functionality of specifying symbolic
0048     globaltags (i.e., actual global tags, plus additional payloads to get from
0049     the DB as customisations).
0050     The actual code is taken from cmsDriver's ConfigBuilder.py, adapted here to
0051     apply the changes directly to the process object and its module GlobalTag."""
0052 
0053     # custom conditions: 
0054     #  - start from an empty map
0055     #  - add any conditions coming from autoCond.py
0056     #  - then add and override them with any conditions explicitly requested
0057     custom_conditions = {}
0058 
0059     # if no GlobalTag ESSource module is given, load a "default" configuration
0060     if essource is None:
0061         from Configuration.StandardSequences.CondDBESSource_cff import GlobalTag as essource
0062 
0063     # if a Global Tag is given, check for an "auto" tag, and look it up in autoCond.py
0064     # if a match is found, load the actual Global Tag and optional conditions
0065     if globaltag is not None:
0066         if globaltag.startswith('auto:'):
0067             from Configuration.AlCa.autoCond import autoCond
0068             globaltag = globaltag[5:]
0069             if globaltag not in autoCond:
0070                 raise Exception('no correspondence for '+globaltag+'\navailable keys are\n'+','.join(autoCond.keys()))
0071             if 'phase1_2017_design' == globaltag:
0072                 sys.stderr.write('Warning: %s now points to %s. This has reco-Beamspot centered to (0,0,0)\n'%(globaltag,autoCond[globaltag]))
0073             if 'phase2_realistic' == globaltag:
0074                 sys.stderr.write('*'*120+'\n\t\t\t\t\t\t WARNING!\n The key %s now points to %s.'\
0075                                  '\n Usage of this key is discretionary and users are cautioned to use it at their own risk!'\
0076                                  '\n This Global Tag contains tags suited for an Inner Tracker layout with rectangular (25x100um2) pixel cells (T15,T21).'\
0077                                  '\n If this is not the geometry you are expecting to use, please consult:' \
0078                                  '\n https://github.com/cms-sw/cmssw/blob/master/Configuration/AlCa/python/autoCondPhase2.py' \
0079                                  ' to retrieve the right key.\n'%(globaltag,autoCond[globaltag])+'*'*120+'\n')
0080             autoKey = autoCond[globaltag]
0081             if isinstance(autoKey, tuple) or isinstance(autoKey, list):
0082                 globaltag = autoKey[0]
0083                 # TODO backward compatible code: to be removed after migrating autoCond.py to use a map for custom conditions
0084                 map = {}
0085                 for entry in autoKey[1:]:
0086                   entry = entry.split(',')
0087                   record     = entry[1]
0088                   label      = len(entry) > 3 and entry[3] or ""
0089                   tag        = entry[0]
0090                   connection = len(entry) > 2 and entry[2] or None
0091                   snapshotTime = len(entry) > 4 and entry[4] or None
0092                   map[ (record, label) ] = (tag, connection, snapshotTime)
0093                 custom_conditions.update( map )
0094             else:
0095                 globaltag = autoKey
0096 
0097         
0098         # if a GlobalTag globaltag is given or loaded from autoCond.py, check for optional connection string and pfn prefix
0099         globaltag = globaltag.split(',')
0100         if len(globaltag) > 0 :
0101             # Perform any alias expansion and consistency check.
0102             # We are assuming the same connection and pfnPrefix/Postfix will be used for all GTs.
0103             globaltag[0] = combineGTs(globaltag[0])
0104             essource.globaltag = cms.string( str(globaltag[0]) )
0105         if len(globaltag) > 1:
0106             essource.connect   = cms.string( str(globaltag[1]) )
0107         if len(globaltag) > 2:
0108             essource.pfnPrefix = cms.untracked.string( str(globaltag[2]) )
0109 
0110 
0111     # add any explicitly requested conditions, possibly overriding those from autoCond.py
0112     if conditions is not None:
0113         # TODO backward compatible code: to be removed after migrating ConfigBuilder.py and confdb.py to use a map for custom conditions
0114         if isinstance(conditions, str): 
0115           if conditions:
0116             map = {}
0117             for entry in conditions.split('+'):
0118                 entry = entry.split(',')
0119                 record     = entry[1]
0120                 label      = len(entry) > 3 and entry[3] or ""
0121                 tag        = entry[0]
0122                 connection = len(entry) > 2 and entry[2] or None
0123                 snapshotTime = len(entry) > 4 and entry[4] or None
0124                 map[ (record, label) ] = (tag, connection, snapshotTime)
0125             custom_conditions.update( map )
0126         elif isinstance(conditions, dict):
0127           custom_conditions.update( conditions )
0128         else:
0129           raise TypeError("the 'conditions' argument should be either a string or a dictionary")
0130 
0131     # explicit payloads toGet from DB
0132     if custom_conditions:
0133         for ( (record, label), (tag, connection, snapshotTime) ) in sorted(custom_conditions.items()):
0134             payload = cms.PSet()
0135             payload.record = cms.string( record )
0136             if label:
0137                 payload.label = cms.untracked.string( label )
0138             payload.tag = cms.string( tag )
0139             if connection:
0140                 payload.connect = cms.string( connection )
0141             if snapshotTime:
0142                 payload.snapshotTime = cms.string( snapshotTime )
0143             essource.toGet.append( payload )
0144 
0145     return essource