Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:25

0001 #! /usr/bin/env python
0002 """
0003 runPixelPopConCalib.py
0004 
0005 Python script to run PixelPopConCalibAnalyzer application
0006 
0007 Initial version: M. Eads, Sep 2008
0008 """
0009 
0010 import os, sys, getopt, shutil
0011 from socket import getfqdn
0012 
0013 def usage():
0014     print("""
0015 runPixelPopConCalib.py usage:
0016     
0017 This script runs the pixel popcon application for the calibration
0018 configuration object. It accepts the following arguments
0019 
0020 -h (or --help)  prints this usage message
0021 -d (or --debug) sets to debug mode, printing extra information
0022 -f XXX (or --filename=XXX) filename (and path to) calib.dat file (REQUIRED)
0023 -r XXX (or --runnumber=XXX) run number, used to set IOV (REQUIRED)
0024 -t XXX (or --tagname=XXX) tag name to use (by default, will use default based on calibration type)
0025 -c XXX (or --cfgtemplate=XXX) template cfg.py file to use (defaults to CondTools/SiPixel/test/test_PixelPopConCalibAnalyzer_cfg.py)
0026 -D XXX (or --database=XXX) connect string for database. Defaults to ORCON if at P5, sqlite file otherwise
0027 -l XXX (or --logdatabase=XXX) connect string for logging database. Defaults to official logging db if at P5, sqlite file otherwise
0028 -a XXX (or --authPath=XXX) path to database authentication files
0029 -p (or --point5) Force point5 behavior (this gets set by default if running from a machine in the .cms network)
0030 -o XXX (or --outputFilename=XXX) Filename for output cfg.py file
0031 -w (or --writeOnly) Only write the cfg.py file, don't run it
0032 -q (or --writeChecker) Also write a PixelPopConCalibChecker cfg.py file
0033 -Q XXX (or --writeCheckerTemplate=XXX) Template cfg.py for PixelPopConCalibChecker 
0034 """)
0035 
0036 def main(argv):
0037     # check that CMSSW environment has been set up
0038     if 'CMSSW_BASE' not in os.environ:
0039         print('CMMSW is not set up! Please run "cmsenv"')
0040         sys.exit(2)
0041     
0042     # get the options from the command line
0043     try:
0044         opts, args = getopt.getopt(argv, 'hdf:t:r:c:D:l:a:po:wqQ:', 
0045                                    ['help', 'debug', 'filename=', 'tagname=', 'runnumber=', 
0046                                     'cfgtemplate=', 'database=', 'logdatabase=', 'authPath=', 
0047                                     'point5', 'outputFilename=', 'writeOnly', 'writeChecker',
0048                                     'writeCheckerTemplate='])
0049     except getopt.GetoptError:
0050         usage()
0051         sys.exit(2)
0052         
0053     # if no options given, print usage and exit
0054     if not opts:
0055         print('runPixelPopConCalib.py: No options given')
0056         usage()
0057         sys.exit(2)
0058     
0059     #print 'opts:', opts
0060     
0061     # figure out if we are at point 5
0062     atPoint5 = False
0063     hostname = getfqdn()
0064     if hostname.split('.')[-1] == 'cms':
0065         atPoint5 = True
0066         
0067     debugMode = False
0068     calibFilename = False
0069     tagName = False
0070     runNumber = False
0071     cfgTemplate = False
0072     databaseConnect = False
0073     logdbConnect = False
0074     authenticationPath = False
0075     writeOnly = False
0076     writeFilename = False
0077     writeChecker = False
0078     writeCheckerTemplate = False
0079     writeCheckerFilename = False
0080     # loop over command-line arguments
0081     for opt, value in opts:
0082         if opt in ('-h', '--help'):
0083             usage()
0084             sys.exit()
0085         elif opt in ('-d', '--debug'):
0086             debugMode = True
0087         elif opt in ('-f', '--filename'):
0088             calibFilename = value
0089         elif opt in ('-t', '--tagname'):
0090             tagName = value
0091         elif opt in ['-r', '--runnumber']:
0092             # check that it's an integer
0093             if not value.isdigit():
0094                 print('Run number given was', value, ', which is not an integer')
0095                 sys.exit(2)
0096             runNumber = value
0097         elif opt in ['-c', '--cfgtemplate']:
0098             cfgTemplate = value
0099         elif opt in ['-D', '--database']:
0100             databaseConnect = value
0101         elif opt in ['-l', '--logdatabase']:
0102             logdbConnect = value 
0103         elif opt in ['-a', '--authPath']:
0104             authenticationPath = value
0105         elif opt in ['-p', '--point5']:
0106             atPoint5 = True
0107             if debugMode:
0108                 print('** forcing point5 mode')
0109         elif opt in ['-o', '--outputFilename']:
0110             writeFilename = value
0111         elif opt in ['-w', '--writeOnly']:
0112             writeOnly = True
0113         elif opt in ['-q', '--writeChecker']:
0114             writeChecker = True
0115         elif opt in ['-Q', '--writeCheckerTemplate']:
0116             writeCheckerTemplate = value
0117             
0118     
0119     if debugMode:
0120         print('** debugMode activated')
0121         
0122     # check that calib filename was provided
0123     if not calibFilename:
0124         print('You must provide a path to the calib.dat file with the -f (or ---filename) option')
0125         sys.exit(2)
0126     if debugMode:
0127         print('** calib.dat filename set to', calibFilename)
0128     
0129     # set the tagname if not provided
0130     if not tagName:
0131         tagName = getTagNameFromFile(calibFilename, debugMode)
0132         if not tagName:
0133             print('Unknown calibration type from calib.dat file!')
0134             sys.exit(2)
0135     if debugMode:
0136         print('** tag name set to', tagName)
0137         
0138     # check that the run number was provided
0139     if not runNumber:
0140         print('You must provide a run number to set the IOV')
0141         sys.exit(2)
0142     if debugMode:
0143         print('** run number for IOV set to', runNumber)
0144         
0145     # set cfg template to default if not given
0146     if not cfgTemplate:
0147         cfgTemplate = os.environ['CMSSW_BASE'] + '/src/CondTools/SiPixel/test/testPixelPopConCalibAnalyzer_cfg.py'
0148     if debugMode:
0149         print('** Using cfg file template:', cfgTemplate)
0150         
0151     if atPoint5:
0152         print('** point 5 mode is set')
0153         
0154     # set database connect string if not given
0155     if not databaseConnect:
0156         if atPoint5:
0157             databaseConnect = 'oracle://cms_orcon_prod/CMS_COND_31X_PIXEL'
0158         else:
0159             databaseConnect = 'sqlite_file:testExample.db'
0160     if debugMode:
0161         print('** database connect string:', databaseConnect)
0162         
0163     # set the logging database connect string if not given
0164     if not logdbConnect:
0165         if atPoint5:
0166             logdbConnect = 'oracle://cms_orcon_prod/CMS_COND_31X_POPCONLOG'
0167         else:
0168             logdbConnect = 'sqlite_file:logtestExample.db'
0169     if debugMode:
0170         print('** logging db connect string:', logdbConnect)
0171         
0172     if not authenticationPath:
0173         if atPoint5:
0174             authenticationPath = '/nfshome0/xiezhen/conddb'
0175         else:
0176             authenticationPath = '/afs/cern.ch/cms/DB/conddb'
0177         
0178     if writeOnly and debugMode:
0179         print('** PixelPopConCalib cfg file will only be written, not run')
0180         
0181     if not writeFilename:
0182         writeFilename = 'PixelPopConCalib_' + tagName + '_' + runNumber + '_cfg.py'
0183     if debugMode:
0184         print('** PixelPopConCalib cfg file will be named ', writeFilename)
0185         
0186     if writeChecker:
0187         if not writeCheckerFilename:
0188             writeCheckerFilename = 'PixelPopConCalibChecker_' + tagName + '_' + runNumber + '_cfg.py'
0189         if not writeCheckerTemplate:
0190             writeCheckerTemplate = os.environ['CMSSW_BASE'] + '/src/CondTools/SiPixel/test/PixelPopConCalibChecker_cfg.py'
0191         if debugMode:
0192             print('** PixelPopConCalibChecker cfg file will be written from template', writeCheckerTemplate)
0193             print('   with filename', writeCheckerFilename)
0194     
0195             
0196     # write the cfg.py file
0197     writePixelPopConCalibCfg(filename = writeFilename,
0198                              calibFilename = calibFilename,
0199                              cfgTemplate = cfgTemplate, 
0200                              runNumber = runNumber,
0201                              tagName = tagName,
0202                              databaseConnect = databaseConnect,
0203                              logdbConnect = logdbConnect,
0204                              authenticationPath = authenticationPath,
0205                              debugMode = debugMode)
0206     
0207     # write the checker
0208     if writeChecker: 
0209         writePixelPopConCalibCheckerCfg(filename = writeCheckerFilename, 
0210                                         cfgTemplate = writeCheckerTemplate,
0211                                         calibFilename = calibFilename,
0212                                         runNumber = runNumber,
0213                                         tagName = tagName,
0214                                         databaseConnect = databaseConnect,
0215                                         authenticationPath = authenticationPath,
0216                                         debugMode = debugMode)
0217     
0218     # run the popcon calib job
0219     if not writeOnly:
0220         if debugMode:
0221             print('** running the cfg file ', writeFilename)
0222         os.system('cmsRun ' + writeFilename)
0223     else:
0224         print('PixelPopConCalib cfg.py written as', writeFilename)
0225         
0226     if writeChecker:
0227         print('PixelPopConCalibChecker cfg written as', writeCheckerFilename)
0228         print('To check if the popcon transfer was successful, run "cmsRun "' + writeCheckerFilename)
0229             
0230 def getTagNameFromFile(filename, debugMode = False):
0231     """
0232     getTagNameFromFile() reads a calib.dat text file and sets the database calib.dat tag based on the "Mode:" setting
0233     """
0234     # open the calib.dat file and find the Mode: line
0235     if debugMode:
0236         print('** getting tag name from calib.dat file')
0237         print('   calib.dat filename:', filename) 
0238     f = open(filename)
0239     for line in f:
0240         if line.find('Mode:') == 0:
0241             if debugMode:
0242                 print('   using line:', line)
0243             if line.find('GainCalibration') != -1:
0244                 return 'GainCalibration_default'
0245             elif line.find('SCurve') != -1:
0246                 return 'SCurve_default'
0247             elif line.find('PixelAlive') != -1:
0248                 return 'PixelAlive_default'
0249             # otherwise, it is an unknown calibration type, return False
0250             return False
0251         
0252 def writePixelPopConCalibCfg(filename, cfgTemplate, calibFilename = '',
0253                              runNumber = '', tagName = '', 
0254                              databaseConnect = '', logdbConnect = '', 
0255                              authenticationPath = '', debugMode = False):
0256     """
0257     writePixelPopConCalibCfg() writes a cfg.py file to run the PixelPopConCalibAnalyzer job
0258     """
0259     # copy the template file to the new cfg.py file
0260     shutil.copyfile(cfgTemplate, filename)
0261     
0262     # open the new cfg file and add the necessary lines
0263     f = open(filename, 'a')
0264     f.write('\n')
0265 
0266     if calibFilename:
0267         f.write('process.PixelPopConCalibAnalyzer.Source.connectString = "file://' + calibFilename + '"\n')                
0268     if runNumber:
0269         f.write('process.PixelPopConCalibAnalyzer.Source.sinceIOV = ' + runNumber + '\n')
0270     if logdbConnect:
0271         f.write('process.PoolDBOutputService.logconnect = "' + logdbConnect + '"\n')
0272     if tagName:
0273         f.write('process.PoolDBOutputService.toPut[0].tag = "' + tagName + '"\n')
0274     if databaseConnect:
0275         f.write('process.PoolDBOutputService.connect = "' + databaseConnect + '"\n')
0276     if authenticationPath:
0277         f.write('process.PoolDBOutputService.DBParameters.authenticationPath = "' + authenticationPath + '"\n')
0278         
0279     
0280 
0281 def writePixelPopConCalibCheckerCfg(filename, cfgTemplate, calibFilename = '', runNumber = '', 
0282                                     tagName = '', databaseConnect = '', 
0283                                     authenticationPath = '', debugMode = False):
0284     """
0285     writePixelPopConCalibCheckerCfg() writes a cfg.py file to run a PixelPopConCalibChecker job
0286     """
0287     # copy the template file to the new cfg.py file
0288     shutil.copyfile(cfgTemplate, filename)
0289     
0290     # open the new cfg file and add the necessary lines
0291     f = open(filename, 'a')
0292     f.write('\n')
0293     
0294     if calibFilename:
0295         f.write('process.demo.filename = "' + calibFilename + '"\n')
0296     if runNumber:
0297         f.write('process.source.firstValue = ' + runNumber + '\n')
0298         f.write('process.source.lastValue = ' + runNumber + '\n')
0299     if tagName:
0300         f.write('process.sipixelcalib_essource.toGet[0].tag = "' + tagName + '"\n')
0301     if databaseConnect:
0302         f.write('process.sipixelcalib_essource.connect = "' + databaseConnect + '"\n')
0303     if authenticationPath:
0304         f.write('process.sipixelcalib_essource.DBParameters.authenticationPath = "' + authenticationPath + '"\n')
0305         
0306 
0307 if __name__ == '__main__':
0308     main(sys.argv[1:])