Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 '''
0002 Update the bookkeeping tables for G1 O2O.
0003 
0004 @author: hqu
0005 '''
0006 
0007 from CondTools.SiStrip.o2o_db_manager import make_dbtype, DbManager
0008 import os
0009 import sys
0010 import datetime
0011 import logging
0012 from importlib import import_module
0013 import sqlalchemy
0014 from sqlalchemy.ext.declarative import declared_attr
0015 
0016 class GainO2OPartitionDef(object):
0017     __tablename__ = 'STRIP_GAIN_O2O_PARTITION'
0018     o2oid = sqlalchemy.Column(sqlalchemy.BigInteger, primary_key=True)
0019     iovstart = sqlalchemy.Column(sqlalchemy.BigInteger, nullable=False)
0020     o2otimestamp = sqlalchemy.Column(sqlalchemy.TIMESTAMP, nullable=False)
0021     subDetector = sqlalchemy.Column(sqlalchemy.String(256), primary_key=True)
0022     partitionname = sqlalchemy.Column(sqlalchemy.String(256), nullable=False)
0023     fecVersionMajorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0024     fecVersionMinorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0025     fedVersionMajorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0026     fedVersionMinorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0027     connectionVersionMajorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0028     connectionVersionMinorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0029     dcuInfoVersionMajorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0030     dcuInfoVersionMinorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0031     dcuPsuMapVersionMajorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0032     dcuPsuMapVersionMinorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0033     maskVersionMajorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0034     maskVersionMinorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0035     apvTimingVersionMajorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0036     apvTimingVersionMinorId = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
0037     timingRunNumber = sqlalchemy.Column(sqlalchemy.BigInteger, nullable=False)
0038 
0039 class GainO2OSkippedDevicesDef(object):
0040     __tablename__ = 'STRIP_GAIN_O2O_SKIPPED'
0041     # o2o identifier
0042     o2oid = sqlalchemy.Column(sqlalchemy.BigInteger, primary_key=True)
0043     itemid = sqlalchemy.Column(sqlalchemy.BigInteger, primary_key=True)
0044     # FEC coordinates
0045     fecCrate = sqlalchemy.Column(sqlalchemy.Integer, nullable=True)
0046     fecSlot = sqlalchemy.Column(sqlalchemy.Integer, nullable=True)
0047     fecRing = sqlalchemy.Column(sqlalchemy.Integer, nullable=True)
0048     ccuAddr = sqlalchemy.Column(sqlalchemy.Integer, nullable=True)
0049     ccuChan = sqlalchemy.Column(sqlalchemy.Integer, nullable=True)
0050     lldChan = sqlalchemy.Column(sqlalchemy.Integer, nullable=True)
0051     i2cAddr = sqlalchemy.Column(sqlalchemy.Integer, nullable=True)
0052     # FED coordinates
0053     fedId = sqlalchemy.Column(sqlalchemy.Integer, nullable=True)
0054     feUnit = sqlalchemy.Column(sqlalchemy.Integer, nullable=True)
0055     feChan = sqlalchemy.Column(sqlalchemy.Integer, nullable=True)
0056     fedApv = sqlalchemy.Column(sqlalchemy.Integer, nullable=True)
0057     # detid
0058     detid = sqlalchemy.Column(sqlalchemy.BigInteger, nullable=True)
0059     
0060 class GainO2OWhitelistedDevicesDef(GainO2OSkippedDevicesDef):
0061     __tablename__ = 'STRIP_GAIN_O2O_WHITELISTED'
0062 
0063 
0064 class DbManagerGain(DbManager):
0065     def __init__(self, db, authFile=None):
0066         DbManager.__init__(self, db, authFile)
0067         self.GainO2OPartition = make_dbtype(GainO2OPartitionDef, self.schema)
0068         self.GainO2OSkippedDevices = make_dbtype(GainO2OSkippedDevicesDef, self.schema)
0069         self.GainO2OWhitelistedDevices = make_dbtype(GainO2OWhitelistedDevicesDef, self.schema)
0070 
0071     def _readPartitions(self, p):
0072         self.o2o_partitions = []
0073         partitionDict = {'PartTECM':'TEC-','PartTECP':'TEC+','PartTIBD':'TIB/TID','PartTOB':'TOB'}
0074         o2otimestamp = datetime.datetime.utcnow()
0075         for part in partitionDict:
0076             psetPart = p.SiStripConfigDb.Partitions.getParameter(part)
0077             if not psetPart: continue
0078             entry = self.GainO2OPartition(
0079                 o2oid = self.o2oid,
0080                 iovstart = self.iovstart,
0081                 o2otimestamp = o2otimestamp,
0082                 subDetector = partitionDict[part],
0083                 partitionname = psetPart.getParameter('PartitionName').value(),
0084                 fecVersionMajorId = psetPart.getParameter('FecVersion')[0],
0085                 fecVersionMinorId = psetPart.getParameter('FecVersion')[1],
0086                 fedVersionMajorId = psetPart.getParameter('FedVersion')[0],
0087                 fedVersionMinorId = psetPart.getParameter('FedVersion')[1],
0088                 connectionVersionMajorId = psetPart.getParameter('CablingVersion')[0],
0089                 connectionVersionMinorId = psetPart.getParameter('CablingVersion')[1],
0090                 dcuInfoVersionMajorId = psetPart.getParameter('DcuDetIdsVersion')[0],
0091                 dcuInfoVersionMinorId = psetPart.getParameter('DcuDetIdsVersion')[1],
0092                 dcuPsuMapVersionMajorId = psetPart.getParameter('DcuPsuMapVersion')[0],
0093                 dcuPsuMapVersionMinorId = psetPart.getParameter('DcuPsuMapVersion')[1],
0094                 maskVersionMajorId = psetPart.getParameter('MaskVersion')[0],
0095                 maskVersionMinorId = psetPart.getParameter('MaskVersion')[1],
0096                 apvTimingVersionMajorId = psetPart.getParameter('ApvTimingVersion')[0],
0097                 apvTimingVersionMinorId = psetPart.getParameter('ApvTimingVersion')[1],
0098                 timingRunNumber = psetPart.getParameter('RunNumber').value()
0099                 )
0100             self.o2o_partitions.append(entry)
0101 
0102     def _readSkippedDevices(self, p, whitelist=False):
0103         dev_type = self.GainO2OSkippedDevices
0104         attr_name = 'SkippedDevices'
0105         if whitelist:
0106             dev_type = self.GainO2OWhitelistedDevices
0107             attr_name = 'WhitelistedDevices'
0108 
0109         dev_list = []
0110         value = lambda p: None if p is None else p.value()
0111         for itemid, pset in enumerate(getattr(p.SiStripCondObjBuilderFromDb, attr_name)):
0112             entry = dev_type(
0113                 o2oid = self.o2oid,
0114                 itemid = itemid,
0115                 fecCrate = value(pset.getParameter('fecCrate')),
0116                 fecSlot = value(pset.getParameter('fecSlot')),
0117                 fecRing = value(pset.getParameter('fecRing')),
0118                 ccuAddr = value(pset.getParameter('ccuAddr')),
0119                 ccuChan = value(pset.getParameter('ccuChan')),
0120                 lldChan = value(pset.getParameter('lldChan')),
0121                 i2cAddr = value(pset.getParameter('i2cAddr')),
0122                 fedId = value(pset.getParameter('fedId')),
0123                 feUnit = value(pset.getParameter('feUnit')),
0124                 feChan = value(pset.getParameter('feChan')),
0125                 fedApv = value(pset.getParameter('fedApv')),
0126                 detid = value(pset.getParameter('detid'))
0127                 )
0128             dev_list.append(entry)
0129 
0130         return dev_list
0131 
0132     def update_gain_logs(self, iov, cfgname):
0133         """Insert bookkeeping info to the tables.
0134         Args:
0135             iov (int or str): IOV number
0136             cfgname (str): name of the CMSSW cfg file.
0137                 The cfg file need to be placed in the current directory.
0138         """
0139 
0140         self.iovstart = int(iov)
0141         sys.path.append(os.getcwd())
0142         if cfgname.endswith('.py'):
0143             cfgname = cfgname.replace('.py', '')
0144         cfg = import_module(cfgname)
0145 
0146         self.check_table(GainO2OPartitionDef, self.GainO2OPartition)
0147         self.check_table(GainO2OSkippedDevicesDef, self.GainO2OSkippedDevices)
0148         self.check_table(GainO2OWhitelistedDevicesDef, self.GainO2OWhitelistedDevices)
0149         destSession = self.connect()
0150         o2oid = destSession.query(self.GainO2OPartition.o2oid).order_by(self.GainO2OPartition.o2oid.desc()).first()
0151         if o2oid:
0152             self.o2oid = o2oid[0] + 1
0153         else:
0154             self.o2oid = 0
0155 
0156         self._readPartitions(cfg.process)
0157         for entry in self.o2o_partitions:
0158             destSession.add(entry)
0159 
0160         o2o_skipped = self._readSkippedDevices(cfg.process)
0161         for entry in o2o_skipped:
0162             destSession.add(entry)
0163 
0164         o2o_whitelisted = self._readSkippedDevices(cfg.process, whitelist=True)
0165         for entry in o2o_whitelisted:
0166             destSession.add(entry)
0167 
0168         destSession.commit()
0169         logging.info('Inserted Gain O2O info to DB!')
0170 
0171 
0172 if __name__ == '__main__':
0173     # for testing
0174     dbmgr = DbManagerGain('private')
0175     dbmgr.update_gain_logs(1, 'test')