Back to home page

Project CMSSW displayed by LXR

 
 

    


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