4 Miscellaneous utilities related to COOL. 
    6 __author__  = 
'Juerg Beringer' 
    7 __version__ = 
'COOLUtils.py atlas/athena' 
   11 from PyCool 
import cool
 
   12 from CoolConvUtilities 
import AtlCoolLib
 
   16 sys.path.append(
'/afs/cern.ch/user/a/atlcond/utils22/')
 
   22 def openBeamSpotDbFile(fileName, forceNew=False, folderName='/Indet/Beampos', dbName='BEAMSPOT'):
 
   23     """Open a beam spot SQLite COOL file and get a `folderHandle` to the beam spot folder. If the folder 
   24        doesn't exist yet, it is created. If the SQLite file doesn't exist, it is created. If forceNew=True, 
   25        any previously existing output file with the specified name is overwritten.""" 
   26     connString =  
'sqlite://;schema=%s;dbname=%s' % (fileName,dbName)
 
   27     dbSvc = cool.DatabaseSvcFactory.databaseService()
 
   28     if forceNew 
and os.path.exists(fileName):
 
   30     if os.path.exists(fileName):
 
   31         db = dbSvc.openDatabase(connString,
False)
 
   33         db = dbSvc.createDatabase(connString)
 
   36     spec=cool.RecordSpecification()
 
   37     spec.extend(
"status",cool.StorageType.Int32)
 
   38     spec.extend(
"posX",cool.StorageType.Float)
 
   39     spec.extend(
"posY",cool.StorageType.Float)
 
   40     spec.extend(
"posZ",cool.StorageType.Float)
 
   41     spec.extend(
"sigmaX",cool.StorageType.Float)
 
   42     spec.extend(
"sigmaY",cool.StorageType.Float)
 
   43     spec.extend(
"sigmaZ",cool.StorageType.Float)
 
   44     spec.extend(
"tiltX",cool.StorageType.Float)
 
   45     spec.extend(
"tiltY",cool.StorageType.Float)
 
   46     spec.extend(
"sigmaXY",cool.StorageType.Float)
 
   47     spec.extend(
"posXErr",cool.StorageType.Float)
 
   48     spec.extend(
"posYErr",cool.StorageType.Float)
 
   49     spec.extend(
"posZErr",cool.StorageType.Float)
 
   50     spec.extend(
"sigmaXErr",cool.StorageType.Float)
 
   51     spec.extend(
"sigmaYErr",cool.StorageType.Float)
 
   52     spec.extend(
"sigmaZErr",cool.StorageType.Float)
 
   53     spec.extend(
"tiltXErr",cool.StorageType.Float)
 
   54     spec.extend(
"tiltYErr",cool.StorageType.Float)
 
   55     spec.extend(
"sigmaXYErr",cool.StorageType.Float)
 
   57     folder = AtlCoolLib.ensureFolder(db,folderName,spec,AtlCoolLib.athenaDesc(
True,
'AthenaAttributeList'),cool.FolderVersioning.MULTI_VERSION)
 
   59     folderHandle = (db,folder,spec)
 
   64                        runMin=0, runMax=(1 << 31)-1, lbMin=0, lbMax=(1 << 32)-2,
 
   66                        posX=0., posY=0., posZ=0.,
 
   67                        sigmaX=30., sigmaY=30., sigmaZ=500.,
 
   70                        posXErr=0., posYErr=0., posZErr=0.,
 
   71                        sigmaXErr=0., sigmaYErr=0., sigmaZErr=0.,
 
   72                        tiltXErr=0., tiltYErr=0.,
 
   74     """Write a beam spot entry for a given IOV into a beam spot folder whose 'folderHandle' is passsed. 
   75        The IOV is specified in terms of run and LB range. Note that lbMax is inclusive. 
   76        The default parameters for the position and tilt are zero, the ones for the widths are large 
   77        non-constraining widths of 30mm (500mm) transverse (longitudinal).""" 
   78     since = (runMin << 32)+lbMin
 
   79     until = (runMax << 32)+lbMax+1
 
   80     payload=cool.Record(folderHandle[2])
 
   81     payload[
'status'] = 
int(status)
 
   82     payload[
'posX'] = 
float(posX)
 
   83     payload[
'posY'] = 
float(posY)
 
   84     payload[
'posZ'] = 
float(posZ)
 
   85     payload[
'sigmaX'] = 
float(sigmaX)
 
   86     payload[
'sigmaY'] = 
float(sigmaY)
 
   87     payload[
'sigmaZ'] = 
float(sigmaZ)
 
   88     payload[
'tiltX'] = 
float(tiltX)
 
   89     payload[
'tiltY'] = 
float(tiltY)
 
   90     payload[
'sigmaXY'] = 
float(sigmaXY)
 
   91     payload[
'posXErr'] = 
float(posXErr)
 
   92     payload[
'posYErr'] = 
float(posYErr)
 
   93     payload[
'posZErr'] = 
float(posZErr)
 
   94     payload[
'sigmaXErr'] = 
float(sigmaXErr)
 
   95     payload[
'sigmaYErr'] = 
float(sigmaYErr)
 
   96     payload[
'sigmaZErr'] = 
float(sigmaZErr)
 
   97     payload[
'tiltXErr'] = 
float(tiltXErr)
 
   98     payload[
'tiltYErr'] = 
float(tiltYErr)
 
   99     payload[
'sigmaXYErr'] = 
float(sigmaXYErr)
 
  102         folderHandle[1].storeObject(since,until,payload,0)
 
  104         folderHandle[1].storeObject(since,until,payload,0,tag)
 
  112     if coolTime 
is not None:
 
  113         return int(coolTime/1000000000)
 
  123     """Utility to query COOL to retrieve start and end time of run and LBs.""" 
  138         print (
'open cool db' )
 
  140         print (
'open cooltrig db')
 
  142         print (
'open cooldcs db')
 
  150           self.
cooldb.closeDatabase()
 
  154           print (
"DB time out -- ignore")
 
  157         """Get start time of run in Unix time (seconds since epoch).""" 
  159         if (iov>cool.ValidityKeyMax): iov=cool.ValidityKeyMax
 
  161         itr = folderSOR_Params.browseObjects(iov, iov, cool.ChannelSelection.all())
 
  164             obj = itr.currentRef()
 
  165             sorTime = obj.payload()[
'SORTime']
 
  172         """Get LHC fill and other info from COOL. The relevant COOL folder, 
  173            /LHC/DCS/FILLSTATE is time-based, so the iov must be specified in 
  174            ns since the epoch, but the argument to getLHCInfo is s since the 
  175            epoch for convenience.""" 
  176         t = timeSinceEpochInSec*1000000000
 
  178         itr = lhcfolder.browseObjects(t,t,cool.ChannelSelection.all())
 
  180             info = { 
'FillNumber': 0,
 
  181                      'StableBeams': 
False,
 
  186             obj = itr.currentRef()
 
  187             for k 
in info.keys():
 
  189                     info[k] = obj.payload()[k]
 
  191                     print (
'WARNING: Cannot find value for',k)
 
  197         """Get end time of run in Unix time (seconds since epoch).""" 
  199         if (iov>cool.ValidityKeyMax): iov=cool.ValidityKeyMax
 
  201         itr = folderEOR_Params.browseObjects(iov, iov, cool.ChannelSelection.all())
 
  204             obj = itr.currentRef()
 
  205             eorTime = obj.payload()[
'EORTime']
 
  211         """Get dict of LB start and end times in Unix time (seconds since epoch).""" 
  214         if (iov2>cool.ValidityKeyMax): iov2=cool.ValidityKeyMax
 
  216         itr = folderLB_Params.browseObjects(iov1, iov2, cool.ChannelSelection.all())
 
  218         while itr.goToNext():
 
  219             obj = itr.currentRef()
 
  222             lb = since & 0xFFFFFFFF
 
  223             if (run != runnr): 
continue 
  225             tlo = obj.payload()[
'StartTime']
 
  226             thi = obj.payload()[
'EndTime']
 
  231         """Get (startTime,endTime) for a given LB. The LB information is cached 
  232            for the last run, in order make this efficient for querying for the 
  233            times of individual LBs.""" 
  242         """Get dict of scan info""" 
  245         if (iov2>cool.ValidityKeyMax): iov2=cool.ValidityKeyMax
 
  247         itr = folderScan_Params.browseObjects(iov1, iov2, cool.ChannelSelection.all())
 
  249         while itr.goToNext():
 
  250             obj = itr.currentRef()
 
  251             runLB = obj.payload()[
'RunLB']
 
  253             if (run != runnr): 
continue 
  254             lb = runLB & 0xFFFFFFFF
 
  255             channelId = obj.channelId()
 
  256             scanningIP = obj.payload()[
'ScanningIP']
 
  258             mask = 1 << channelId
 
  259             if(scanningIP & mask == 0): 
continue 
  260             acquisitionFlag = obj.payload()[
'AcquisitionFlag']
 
  261             nominalSeparation = obj.payload()[
'NominalSeparation']
 
  262             nominalSeparationPlane = obj.payload()[
'NominalSeparationPlane']
 
  263             B1DeltaXSet = obj.payload()[
'B1DeltaXSet']
 
  264             B2DeltaXSet = obj.payload()[
'B2DeltaXSet']
 
  265             B1DeltaYSet = obj.payload()[
'B1DeltaYSet']
 
  266             B2DeltaYSet = obj.payload()[
'B2DeltaYSet']
 
  267             scanDict[lb] = (scanningIP,acquisitionFlag,nominalSeparation,nominalSeparationPlane,B1DeltaXSet,B2DeltaXSet,B1DeltaYSet,B2DeltaYSet)
 
  271         """Get scan information for a given LB. The LB information is cached 
  272            for the last run, in order make this efficient for querying for the 
  273            times of individual LBs.""" 
  281     "Resolve the current BLK tag alias" 
  283     from CondUtilsLib.AtlCoolBKLib 
import resolveAlias
 
  286     alias = resolveAlias.getCurrent()
 
  287     return alias.replace(
'*', tagtype)
 
  290     "Resolve the next BLK tag alias" 
  292     from CondUtilsLib.AtlCoolBKLib 
import resolveAlias
 
  298         alias = resolveAlias.getNext()
 
  302     return alias.replace(
'*', tagtype)
 
  307     Resolve the beamspot folder tag for the current BLK tag alisa 
  314     Resolve the beamspot folder tag for the next BLK tag alias 
  318     if tag == 
'': 
return '' 
  322 def resolveBLKTag(blktag, db = 'COOLOFL_INDET/CONDBR2', folder = '/Indet/Beampos'):
 
  324     Resolve a global tag into the corresponding tag for given folder in the database specified 
  327     dbSvc = cool.DatabaseSvcFactory.databaseService()
 
  328     dbconn = dbSvc.openDatabase(db)
 
  329     folder = dbconn.getFolder(folder)
 
  331         tag = folder.resolveTag(blktag)
 
  338 if __name__ == 
'__main__':
 
  340     print (time.strftime(
'%c', time.gmtime(c.getRunStartTime(142191)))) 
 
  341     print (time.strftime(
'%c', time.gmtime(c.getRunStartTime(142193)))) 
 
  342     print (time.strftime(
'%c', time.gmtime(c.getRunEndTime(142193))))
 
  343     lbDict = c.getLbTimes(142193)
 
  344     for l 
in lbDict.keys():
 
  345         print (l,time.ctime(lbDict[l][0]),
'-',time.ctime(lbDict[l][1]))