def execute(self): chanlist = self.getchans() if len(chanlist) == 0: sys.exit(1) # now do update - setup folder specification and create if needed spec = cool.RecordSpecification() spec.extend("NBuilt", cool.StorageType.Int32) print ">== Store object in folder", self.foldername cfolder = AtlCoolLib.ensureFolder( self.db, self.foldername, spec, AtlCoolLib.athenaDesc(True, 'CondAttrListCollection') + '<named/>', cool.FolderVersioning.SINGLE_VERSION) if (cfolder is None): sys.exit(1) # if we do not have data to add - finish here if (not self.isdata): return # now write data payload = cool.Record(spec) payload['NBuilt'] = self.nbuilt for channel in chanlist: print '>== Store object with IOV [', self.since, ',', self.until, '] channel', channel, 'NBuilt=', self.nbuilt try: cfolder.storeObject(self.since, self.until, payload, channel) except Exception, e: print e print 'Exception thrown when storing for channel', channel print '>== Storing COOL object FAILED' sys.exit(1)
def createDB(): """Create sqlite file with DCS currents""" from PyCool import cool from CoolConvUtilities import AtlCoolLib, AtlCoolTool # Cleanup previous file import os if os.path.isfile("magfield.db"): os.remove("magfield.db") db = cool.DatabaseSvcFactory.databaseService().createDatabase( 'sqlite://;schema=magfield.db;dbname=CONDBR2') spec = cool.RecordSpecification() spec.extend("value", cool.StorageType.Float) spec.extend("quality_invalid", cool.StorageType.Bool) f = AtlCoolLib.ensureFolder( db, folder, spec, AtlCoolLib.athenaDesc(True, 'CondAttrListCollection')) for v in currents: sol = cool.Record(spec) sol['value'] = v[1] sol['quality_invalid'] = False tor = cool.Record(spec) tor['value'] = v[2] tor['quality_invalid'] = False f.storeObject(v[0], cool.ValidityKeyMax, sol, 1) # channel 1 f.storeObject(v[0], cool.ValidityKeyMax, tor, 3) # channel 3 # print database content act = AtlCoolTool.AtlCoolTool(db) print(act.more(folder))
def execute(self): folder = '/DMTest/TestAttrList' # do update - setup folder specification and create if needed spec = cool.RecordSpecification() spec.extend("xint", cool.StorageType.Int32) print(">== Store object in folder", folder) cfolder = AtlCoolLib.ensureFolder( self.db, folder, spec, AtlCoolLib.athenaDesc(self.runLumi, 'AthenaAttributeList'), cool.FolderVersioning.MULTI_VERSION) if (cfolder is None): sys.exit(1) # now write data payload = cool.Record(spec) payload['xint'] = self.xint print('>== Store object with IOV [', self.since, ',', self.until, '] and tag', self.tag, 'xint', self.xint) try: if (self.tag == "HEAD"): cfolder.storeObject(self.since, self.until, payload, 0) else: cfolder.storeObject(self.since, self.until, payload, 0, self.tag) print(">== Storing COOL object succeeded. Current content:") except Exception: import traceback traceback.print_exc() print('>== Storing COOL object FAILED') sys.exit(1) # print full content act = AtlCoolTool.AtlCoolTool(self.db) print(act.more(folder))
def execute(self): # do update - setup folder specification and create if needed spec = cool.RecordSpecification() spec.extend("status", cool.StorageType.Int32) spec.extend("posX", cool.StorageType.Float) spec.extend("posY", cool.StorageType.Float) spec.extend("posZ", cool.StorageType.Float) spec.extend("sigmaX", cool.StorageType.Float) spec.extend("sigmaY", cool.StorageType.Float) spec.extend("sigmaZ", cool.StorageType.Float) spec.extend("tiltX", cool.StorageType.Float) spec.extend("tiltY", cool.StorageType.Float) spec.extend("sigmaXY", cool.StorageType.Float) folder = '/Indet/Beampos' print ">== Store object in folder", folder cfolder = AtlCoolLib.ensureFolder( self.db, folder, spec, AtlCoolLib.athenaDesc(self.runLumi, 'AthenaAttributeList'), cool.FolderVersioning.MULTI_VERSION) if (cfolder is None): sys.exit(1) # now write data payload = cool.Record(spec) payload['status'] = self.status payload['posX'] = self.posx payload['posY'] = self.posy payload['posZ'] = self.posz payload['sigmaX'] = self.sigmax payload['sigmaY'] = self.sigmay payload['sigmaZ'] = self.sigmaz payload['tiltX'] = self.tiltx payload['tiltY'] = self.tilty payload['sigmaXY'] = self.sigmaxy print '>== Store object with IOV [', self.since, ',', self.until, '] and tag', self.tag, 'status', self.status print '>== Beamspot position (mm):', self.posx, self.posy, self.posz print '>== Beamspot sigma (mm):', self.sigmax, self.sigmay, self.sigmaz print '>== Beamspot tilt (rad):', self.tiltx, self.tilty try: if (self.tag == "HEAD"): cfolder.storeObject(self.since, self.until, payload, 0) else: cfolder.storeObject(self.since, self.until, payload, 0, self.tag) print ">== Storing COOL object succeeded" except Exception, e: print e print '>== Storing COOL object FAILED' sys.exit(1)
def openBeamSpotDbFile(fileName, forceNew=False, folderName='/Indet/Beampos', dbName='BEAMSPOT'): """Open a beam spot SQLite COOL file and get a `folderHandle` to the beam spot folder. If the folder doesn't exist yet, it is created. If the SQLite file doesn't exist, it is created. If forceNew=True, any previously existing output file with the specified name is overwritten.""" connString = 'sqlite://;schema=%s;dbname=%s' % (fileName, dbName) dbSvc = cool.DatabaseSvcFactory.databaseService() if forceNew and os.path.exists(fileName): os.remove(fileName) if os.path.exists(fileName): db = dbSvc.openDatabase(connString, False) else: db = dbSvc.createDatabase(connString) # Make sure we have /Indet/Beampos with the correct schema spec = cool.RecordSpecification() spec.extend("status", cool.StorageType.Int32) spec.extend("posX", cool.StorageType.Float) spec.extend("posY", cool.StorageType.Float) spec.extend("posZ", cool.StorageType.Float) spec.extend("sigmaX", cool.StorageType.Float) spec.extend("sigmaY", cool.StorageType.Float) spec.extend("sigmaZ", cool.StorageType.Float) spec.extend("tiltX", cool.StorageType.Float) spec.extend("tiltY", cool.StorageType.Float) spec.extend("sigmaXY", cool.StorageType.Float) spec.extend("posXErr", cool.StorageType.Float) spec.extend("posYErr", cool.StorageType.Float) spec.extend("posZErr", cool.StorageType.Float) spec.extend("sigmaXErr", cool.StorageType.Float) spec.extend("sigmaYErr", cool.StorageType.Float) spec.extend("sigmaZErr", cool.StorageType.Float) spec.extend("tiltXErr", cool.StorageType.Float) spec.extend("tiltYErr", cool.StorageType.Float) spec.extend("sigmaXYErr", cool.StorageType.Float) folder = AtlCoolLib.ensureFolder( db, folderName, spec, AtlCoolLib.athenaDesc(True, 'AthenaAttributeList'), cool.FolderVersioning.MULTI_VERSION) folderHandle = (db, folder, spec) return folderHandle
def execute(self): print '>== Inserting reference to file:', self.file, ' - find GUID' os.system('coolHist_extractFileIdentifier.sh %s' % self.file) guid = open('coolhist_guid.tmp', 'r').readline()[:-1] if (guid == 'none'): print '>== File has no GUID - aborting' sys.exit(1) # setup folder specification and create if needed spec = cool.RecordSpecification() spec.extend("fileGUID", cool.StorageType.String4k) cfolder = AtlCoolLib.ensureFolder( self.db, self.folder, spec, AtlCoolLib.athenaDesc(self.runLumi, 'CondAttrListCollection') + '<named/>', cool.FolderVersioning.MULTI_VERSION) print if (cfolder is None): sys.exit(1) # check if channel number needs to be looked up if (self.channelstr != ""): try: self.channel = cfolder.channelId(self.channelstr) print '>== Channel name', self.channelstr, 'is channelId', self.channel except Exception: print 'Non-existant or invalid channel name:', self.channelstr sys.exit(1) print '>== Write data on COOL connection:', self.conn print '>== To folder:', self.folder, 'channel:', self.channel print '>== COOL tag:', self.tag # now write data payload = cool.Record(spec) payload['fileGUID'] = guid print '>== Store object with IOV [', self.since, ',', self.until, '] channel', self.channel, 'and tag', self.tag try: cfolder.storeObject(self.since, self.until, payload, self.channel, self.tag) return except Exception, e: print e print '>== Storing COOL object FAILED' sys.exit(1)
class mergeStatus(AtlCoolLib.coolTool): def setup(self, args): # set values of non-optional parameters self.destdbstr = str(args[0]) # access to name encoding self.namelookup = DetStatusLib.DetStatusNames() self.folderinfo = [] self.destfolder = '/GLOBAL/DETSTATUS/LBSUMM' self.tag = '' self.truncate = False self.numbers = False def usage(self): "Define additional syntax for options" self._usage1() print 'destdbname' print '--and=<folder> : set folder name to be Anded' print '--override=<folder>:<tag> : set folder and tag name for override' print '--destfolder=<folder> : set destinaton folder (default /GLOBAL/DETSTATUS/LBSUMM' print '--desttag=<tag> : set destination tag' print '--numbers : Transfer NConfig and NWorking from source folders if given' print '--truncate : truncate IOVs which exceed given interval' self._usage2() def procopts(self, opts): "Process additional options" for o, a in opts: if (o == '--and' or o == '--override'): fields = str(a).split(':') folder = fields[0] if len(fields) > 1: tag = fields[1] else: tag = 'HEAD' self.folderinfo += [(folder, tag, (o == '--override'))] if (o == '--destfolder'): self.destfolder = str(a) if (o == '--numbers'): self.numbers = True if (o == '--desttag'): self.tag = str(a) if (o == '--truncate'): self.truncate = True def execute(self): # print "Name is %s" AtlCoolLib.getHostname() # prepare an empty StatusList for each channel statuslists = {} for chan in self.namelookup.allNums(): statuslists[chan] = DetStatusLib.StatusList() # deduce the source database instance name, looking for xyzP200 idx = self.conn.find('P200') if (idx >= 3): dbinst = self.conn[idx - 3:idx + 4] print "Working with database instance %s" % dbinst else: print "Cannot deduce database instance name, assume COMP200" dbinst = 'COMP200' # Extract timestamp information for mapping to run/LB tsconvdb = AtlCoolLib.indirectOpen('COOLONL_TRIGGER/' + dbinst, oracle=self.oracle) # initialize the converter tsToRLB = AtlCoolLib.TimeStampToRLB(tsconvdb, self.since, self.until) tsconvdb.closeDatabase() StartTime = tsToRLB.StartTime EndTime = tsToRLB.EndTime # open auxillary databases for online and DCS info onlinedb = AtlCoolLib.indirectOpen('COOLONL_GLOBAL/' + dbinst, oracle=self.oracle) dcsdb = AtlCoolLib.indirectOpen('COOLOFL_GLOBAL/' + dbinst, oracle=self.oracle) # loop over all 'AND' folders and merge information ngood = 0 nbad = 0 for (afolder, atag, override) in self.folderinfo: print "Process folder %s tag %s override %d" % (afolder, atag, override) try: atsindexed = 0 isonline = 0 if (afolder.find("ONL") != -1): isonline = 1 isdcs = 0 if (afolder.find("DCS") != -1): isdcs = 1 tostart = self.since toend = self.until ifolder = None if (isonline): ifolder = onlinedb.getFolder(afolder) elif (isdcs): ifolder = dcsdb.getFolder(afolder) else: ifolder = self.db.getFolder(afolder) descr = ifolder.description() if (descr.find("<timeStamp>time") != -1): atsindexed = 1 tostart = StartTime toend = EndTime objs = None if (isonline | isdcs): objs = ifolder.browseObjects(tostart, toend, cool.ChannelSelection.all()) else: objs = ifolder.browseObjects(tostart, toend, cool.ChannelSelection.all(), atag) while objs.goToNext(): obj = objs.currentRef() chan = obj.channelId() if (chan in statuslists.keys()): start = obj.since() stop = obj.until() payload = obj.payload() code = payload['Code'] deadfrac = payload['deadFrac'] thrust = payload['Thrust'] if ('NConfig' in payload.keys()): nconfig = payload['NConfig'] nworking = payload['NWorking'] else: nconfig = -1 nworking = -1 if ('Comment' in payload.keys()): comment = payload['Comment'] else: comment = '' #override=False if (atsindexed): start = tsToRLB.getRLB(start, True) stop = tsToRLB.getRLB(stop, False) + 1 statuslists[chan].merge( DetStatusLib.StatusObj(start, stop, code, deadfrac, thrust, nconfig, nworking, comment), override) ngood += 1 else: print "Data found for unexpected channel %i" % chan nbad += 1 except Exception, e: print e print "Problem accessing folder %s" % afolder nbad += 1 print "All folders processed with %i good and %i items of bad data" % ( ngood, nbad) # now compose COOL update # establish write connection writedb = AtlCoolLib.forceOpen(self.destdbstr) if (writedb is None): return try: cfolder = writedb.getFolder(self.destfolder) print "Write data to existing folder %s" % self.destfolder spec = cfolder.payloadSpecification() except Exception, e: print "Creating destination folder %s" % self.destfolder spec = cool.RecordSpecification() spec.extend("Code", cool.StorageType.Int32) spec.extend("deadFrac", cool.StorageType.Float) spec.extend("Thrust", cool.StorageType.Float) if self.numbers: spec.extend("NConfig", cool.StorageType.Int32) spec.extend("NWorking", cool.StorageType.Int32) spec.extend("Comment", cool.StorageType.String255) cfolder = AtlCoolLib.ensureFolder( writedb, self.destfolder, spec, AtlCoolLib.athenaDesc(self.runLumi, 'CondAttrListCollection'), cool.FolderVersioning.MULTI_VERSION)
def execute(self): # check if flag given on command line, if not will get from each file # input line if (self.flag != ''): status = DetStatusLib.colourVal(self.flag) if (status is None): sys.exit(1) chanlist = self.getchans() if len(chanlist) == 0: sys.exit(1) # folder name depends on choice of run-lumi or timestamp, or explicit if (self.foldername == ''): folder = DetStatusLib.folderName(self.runLumi) else: folder = self.foldername print ">== Store object in folder", folder hascomment = (folder == '/GLOBAL/DETSTATUS/SHIFTOFL' or folder == '/GLOBAL/DETSTATUS/SHIFTONL') # now do update - setup folder specification and create if needed spec = cool.RecordSpecification() spec.extend("Code", cool.StorageType.Int32) spec.extend("deadFrac", cool.StorageType.Float) spec.extend("Thrust", cool.StorageType.Float) if (hascomment): spec.extend("Comment", cool.StorageType.String255) cfolder = AtlCoolLib.ensureFolder( self.db, folder, spec, AtlCoolLib.athenaDesc(self.runLumi, 'CondAttrListCollection') + '<named/>', cool.FolderVersioning.MULTI_VERSION) if (cfolder is None): sys.exit(1) # check if tag exists if self.tag != 'HEAD': taglist = cfolder.listTags() if self.tag not in taglist: if self.newtag: print ">== Tag %s will be created" % self.tag else: print ">== ERROR: Tag %s does not exist (use --newtag if you really want to create it)" % self.tag sys.exit(1) # now write data according to list in file datafile = open(self.filename, 'r') # setup storage buffer ready for input cfolder.setupStorageBuffer() nobj = 0 print "Reading data from file %s to insert in tag %s" % (self.filename, self.tag) for line in datafile.readlines(): tokens = line[:-1].split() if (len(tokens) > 0 and tokens[0][0] != '#'): # if no flag value specified on command line, take from first # argument on line if (self.flag == ''): status = DetStatusLib.colourVal(tokens[0]) if (status is None): print "Cannot define status from line %s" % line sys.exit(-1) del tokens[0] run1 = int(tokens[0]) run2 = run1 if len(tokens) == 2: run2 = int(tokens[1]) if len(tokens) < 3: lb1 = 0 lb2 = (1 << 32) - 1 else: lb1 = int(tokens[1]) lb2 = int(tokens[2]) if len(tokens) > 3: # take the rest of the line from where the comment starts comment = line[line.find(tokens[3]):-1] else: comment = "" if (comment != "" and not hascomment): print "Comment %s specified for folder type without comments" % comment sys.exit(-1) if comment == "": commstr = "" else: commstr = ' comment "%s"' % comment print "Update status for [%i,%i] to [%i,%i] to %s%s" % ( run1, lb1, run2, lb2, DetStatusLib.colour(status), commstr) payload = cool.Record(spec) payload['Code'] = status payload['deadFrac'] = 0. payload['Thrust'] = 0. if (hascomment): payload['Comment'] = comment since = (run1 << 32) + lb1 until = (run2 << 32) + lb2 + 1 for chan in chanlist: if (self.tag == "HEAD"): cfolder.storeObject(since, until, payload, chan) else: cfolder.storeObject(since, until, payload, chan, self.tag, True) nobj += 1 datafile.close() if (nobj > 0): chk = raw_input("Execute upload of %i objects (y/n)" % nobj) if (chk.upper() == "Y"): try: cfolder.flushStorageBuffer() print "Data written to COOL" except Exception, e: print "Error during bulk upload", e else: print "Upload ABORTED - not done"
def execute(self): "Execute the database creation/update" # setup specification spec = cool.RecordSpecification() spec.extend("stave", cool.StorageType.Int32) spec.extend("eta", cool.StorageType.Int32) spec.extend("mag", cool.StorageType.Float) spec.extend("base", cool.StorageType.Float) spec.extend("free", cool.StorageType.Float) # create folder if it does not exist folder = '/Indet/IBLDist' cfolder = AtlCoolLib.ensureFolder( self.db, folder, spec, AtlCoolLib.athenaDesc(self.runLumi, 'CondAttrListCollection'), cool.FolderVersioning.MULTI_VERSION) if (cfolder is None): sys.exit(1) # read data from the text file print "Reading input data from %s" % self.txtfile bowdata = [] staves = [] for iline in open(self.txtfile).readlines(): # split line into space-separated tokens, ignoring newline tokens = iline[:-1].split() # skip lines starting with # if tokens[0] == '#': continue # expect format '<stave> <eta> <mag> <base> <free>' if len(tokens) != 5: print "Skipping malformed line with %i tokens: " % len( tokens), iline else: stave = int(tokens[0]) eta = int(tokens[1]) mag = float(tokens[2]) base = float(tokens[3]) free = float(tokens[4]) if stave not in staves: bowdata += [(stave, eta, mag, base, free)] staves += [stave] else: print "ERROR: read multiple lines for stave %i" % stave print "Ready to add data for %i staves" % len(bowdata) # now print data and write to COOL folder print "Channel Stave eta mag base free" for idata in bowdata: (stave, eta, mag, base, free) = idata # determine channel number from stave and eta # have 14 staves 0-13, and eta -10 to +9 channel = 100 * (1 + stave) + eta print "%7i" % channel, "%3i %6i %6.3f %6.3f %6.3f" % idata payload = cool.Record(spec) payload['stave'] = stave payload['eta'] = eta payload['mag'] = mag #*.001 payload['base'] = base #*.001 payload['free'] = free try: cfolder.storeObject(self.since, self.until, payload, channel, self.tag) except Exception, e: print e print "Store to COOL failed for channel %i" % channel sys.exit(-1)
def execute(self): chanlist = self.getchans() if len(chanlist) == 0: sys.exit(1) # now do update - setup folder specification and create if needed spec = cool.RecordSpecification() spec.extend("Code", cool.StorageType.Int32) spec.extend("deadFrac", cool.StorageType.Float) spec.extend("Thrust", cool.StorageType.Float) if (self.numbers): spec.extend("NConfig", cool.StorageType.Int32) spec.extend("NWorking", cool.StorageType.Int32) if (self.comments): spec.extend("Comment", cool.StorageType.String255) # folder name depends on choice of run-lumi or timestamp if (self.foldername == ''): folder = DetStatusLib.folderName(self.runLumi) else: folder = self.foldername print ">== Store object in folder", folder cfolder = AtlCoolLib.ensureFolder( self.db, folder, spec, AtlCoolLib.athenaDesc(self.runLumi, 'CondAttrListCollection') + '<named/>', self.fversion) if (cfolder is None): sys.exit(1) # if we do not have data to add - finish here if (not self.isdata): return # translate detector to COOL channel status = DetStatusLib.colourVal(self.light) if (status is None): print "Cannot define status from value", self.light sys.exit(1) # check if tag exists if self.tag != 'HEAD': taglist = cfolder.listTags() if self.tag not in taglist: if self.newtag: print ">== Tag %s will be created" % self.tag else: print ">== ERROR: Tag %s does not exist (use --newtag if you really want to create it)" % self.tag sys.exit(1) # now write data payload = cool.Record(spec) payload['Code'] = status payload['deadFrac'] = self.deadfrac payload['Thrust'] = self.deadthrust if (self.comments): payload['Comment'] = self.commstr if (self.numbers): payload['NConfig'] = self.nconfig payload['NWorking'] = self.nworking for channel in chanlist: print '>== Store object with IOV [', self.since, ',', self.until, '] channel', channel, 'and tag', self.tag, 'status info', status, self.deadfrac, self.deadthrust try: if (self.tag == "HEAD"): cfolder.storeObject(self.since, self.until, payload, channel) else: cfolder.storeObject(self.since, self.until, payload, channel, self.tag, True) except Exception, e: print e print 'Exception thrown when storing for channel', channel print '>== Storing COOL object FAILED' sys.exit(1)