def runLumiToTimeStamp(runnumber): run=long(runnumber) dbSvc = cool.DatabaseSvcFactory.databaseService() db=dbSvc.openDatabase("COOLONL_TRIGGER/"+dbname) f=db.getFolder("/TRIGGER/LUMI/LBLB") t1=run<<32 t2=(run<<32) + 0xFFFFFFFFL runstart=None runstop=None lbtimes=[] itr=f.browseObjects(cool.ValidityKey(t1),cool.ValidityKey(t2),cool.ChannelSelection(0)) while itr.goToNext(): obj=itr.currentRef() pl=obj.payload() lbstart=pl["StartTime"] if runstart is None: runstart=lbstart runstop=pl["EndTime"] lbtimes+=[(obj.since()&0xFFFFFFFF,runstop),] itr.close() db.closeDatabase() #print lbtimes return (runstart, runstop, lbtimes)
def printChannelInfo(db,folderName,chnl,t1,t2,lbtimes=None): if not db.existsFolder(folderName): print "ERROR: Folder",folderName,"does not exist" return None f=db.getFolder(folderName) #lastVoltage=dict() #lastStat=dict() itr=f.browseObjects(cool.ValidityKey(t1), cool.ValidityKey(t2), cool.ChannelSelection.all()) while itr.goToNext(): obj=itr.currentRef() chid=obj.channelId(); pl=obj.payload() stat=pl["R_STAT"] v=pl["R_VMEAS"] i=pl["R_IMEAS"] if chid==chnl: print "Status",eventInfo(obj,stat,v,i,lbtimes) itr.close() return
def getCoolValidityKey(pointInTime, isSince=True): """ The interpretation of pointInTime depends on their type: - tuple(int,int) : run and lbk number - integer : Values are interpreted as unix time stamps - string : time stamp of format 'yyyy-mm-dd hh:mm:ss' """ validityKey = None #=== string: convert to unix time and treat as latter if isinstance(pointInTime, str): pointInTime = decodeTimeString(pointInTime) #=== integer: unix time stamp if isinstance(pointInTime, int): if pointInTime >= 0: validityKey = pointInTime * UNIX2COOL else: if isSince: validityKey = int(time.time()) * UNIX2COOL else: validityKey = cool.ValidityKeyMax #=== run-lumi tuple elif isinstance(pointInTime, tuple): validityKey = iovFromRunLumi(pointInTime[0], pointInTime[1]) #=== catch other types else: raise Exception("Unrecognized pointInTime type=%s" % type(pointInTime)) return cool.ValidityKey(validityKey)
def update(self): # SOR if self.sor == None: db = CoolDB.get("COOLONL_TDAQ/COMP200") f = db.getFolder("/TDAQ/RunCtrl/SOR_Params") o = f.findObject(cool.ValidityKey(self.runno << 32), 0) self.sor = toSec(o.get().payload()['SORTime']) # SMK if self.smk == None: keys = TriggerCoolUtil.getHLTConfigKeys( CoolDB.get('COOLONL_TRIGGER/COMP200'), [[self.runno, self.runno]]) if not self.runno in keys: raise RuntimeError, ("Cannot find SMK for run %d" % self.runno) self.smk = int(keys[self.runno]['SMK']) # GlobalTag if self.globalTag == None: res = getPropertyFromDB('TRIGGERDB', self.smk, 'IOVDbSvc', 'GlobalTag') if len(res) == 0: raise RuntimeError, ('Cannot find GlobalTag for SMK %d' % self.smk) self.globalTag = res[0][3]
def bunchSpacingOfRun(runnumber,LB,verbose=False): if (runnumber<236107): print "WARNING BunchSpacingUtils don't work for run-1 data" return None tdaqDBName="COOLONL_TDAQ/CONDBR2" folder="/TDAQ/OLC/LHC/FILLPARAMS" iovtime=getTimeForLB(runnumber,LB,readOracle=True) if iovtime==0: print "ERROR, can't get start time of run %i, LB %i" % (runnumber,LB) return None obj=None db = None try: db=indirectOpen(tdaqDBName,oracle=True) f=db.getFolder(folder) obj=f.findObject(cool.ValidityKey(iovtime),0) except Exception,e: print e.args[0] if len(e.args)>1 and e.args[0].find("Object not found - 0"): print "WARNING No data found in folder %s for run/LB %i/%i" % (folder,runnumber,LB) else: print "BunchSpacingUtils: ERROR accesssing folder",folder,"on db",tdaqDBName print e if db is not None: db.closeDatabase() return None
def getEntry(self, drawer, variable, unixTime): """ """ #=== sort variables by directories folder, channel = self.info.get_folder_and_channel(variable, drawer) #NGO quick hack to fix ./checkCoolLatestUpdate.py --> need to introduce some varname mapping if '/HVSET' in folder: variable = variable.split(".")[1] elif '/AI' in folder: variable = "AI_" + variable + "_VALUE" else: variable = variable.upper() valKey = cool.ValidityKey(unixTime * self.unix2cool) coolFolder = self.db.getFolder(folder) obj = coolFolder.findObject(valKey, channel) iovSince = obj.since() iovUntil = obj.until() sval = obj.payloadValue(variable) if sval == "NULL": sval = "0" val = float(sval) inTime = obj.insertionTime() return (val, iovSince, iovUntil, inTime)
def check_db(self, iovStartUnix, iovEndUnix, ar): if self.dbCounter == 0: if iovStartUnix < self.R2minTime: self.dbCounter = 1 if iovEndUnix > self.R2minTime: ar[0] = cool.ValidityKey(iovStartUnix * self.unix2cool) ar[1] = cool.ValidityKey(self.R2minTime * self.unix2cool) else: self.dbCounter = 2 elif self.dbCounter == 1: if iovEndUnix <= self.R2minTime: self.dbCounter = 0 else: self.dbCounter = 2 if iovStartUnix < self.R2minTime: ar[0] = cool.ValidityKey(self.R2minTime * self.unix2cool) ar[1] = cool.ValidityKey(iovEndUnix * self.unix2cool) else: self.dbCounter = 0 self.db = self.dbV[self.dbCounter] return self.dbCounter
def get_partition_name(runnr): try: from PyCool import cool import time folder=l1ctpy.getCoolFolder(l1ctpy.get_dbid_TDAQ_COMP(),'/TDAQ/RunCtrl/EventCounters') ROV1=cool.ValidityKey((run<<32)+1) ROV2=ROV1 # objs=list(folder.browseObjects(ROV1,ROV2,cool.ChannelSelection.all())) return objs[0].payload()["PartitionName"] except IndexError: return "" # except BaseException,e: # print "Exception: ",e,type(e) return ""
def get_partition_ongoing(runnr): try: from PyCool import cool import time folder=l1ctpy.getCoolFolder(l1ctpy.get_dbid_TDAQ_COMP(),'/TDAQ/RunCtrl/EOR_Params') ROV1=cool.ValidityKey((run<<32)+1) ROV2=ROV1 # objs=list(folder.browseObjects(ROV1,ROV2,cool.ChannelSelection.all())) if objs[0].payload()["CleanStop"] : return "CleanStop" else: return "NotCleanStop" except IndexError: return "" # except BaseException,e: # print "Exception: ",e return ""
def get_current_runs(historical_days=5,historical_maxruns=1): final=[] try: from PyCool import cool import time SOR_fld=getCoolFolder(get_dbid_COMP(),'/TRIGGER/LUMI/LBTIME') ROV1=1e9*(time.time()-historical_days*24*3600) ROV1=cool.ValidityKey(ROV1) ROV2=cool.ValidityKeyMax # objs=list(SOR_fld.browseObjects(ROV1,ROV2,cool.ChannelSelection.all())) runs=map(lambda x:x.get().payload()["Run"] ,objs) runs=sorted(list(set(runs))) runs.reverse() i=0 for x in runs: i+=1 final.append(int(x)) if i >= historical_maxruns: break except BaseException,e: print "Exception trying to find Run numbers:",e
def getMissingFebs(run, tag=None): if tag is None: sys.path.append('/afs/cern.ch/user/a/atlcond/utils/python/') from AtlCoolBKLib import resolveAlias resolver = resolveAlias() tag = resolver.getCurrent().replace("*", "ST") retval = [] dbName = "COOLOFL_LAR/CONDBR2" fName = "/LAR/BadChannelsOfl/MissingFEBs" dbSvc = cool.DatabaseSvcFactory.databaseService() try: db = dbSvc.openDatabase(dbName) except Exception as e: print("Problems connecting to database:", e) return None print("getMissingFEBs: Checking tag", tag, "for run", run) if not db.existsFolder(fName): print("ERROR: Folder", fName, "not found in ", dbName) return None f = db.getFolder(fName) if not f.existsUserTag(tag): try: t = f.resolveTag(tag) except Exception as e: print("ERROR: Tag", tag, "not defined in folder", fName) print(e) return None else: t = tag cl_LArBadChannelDBTools = ROOT.LArBadChannelDBTools cl_AthenaAttributeList = ROOT.AthenaAttributeList ms = cl_LArBadChannelDBTools.getDefaultMsgStream() ts1 = cool.ValidityKey(1 + (run << 32)) ts2 = cool.ValidityKey(((run + 1) << 32) - 1) itr = f.browseObjects(ts1, ts2, cool.ChannelSelection(0), t) while itr.goToNext(): obj = itr.currentRef() pl = obj.payload() #print (obj.since(), obj.until(), pl) al = cl_AthenaAttributeList(pl.attributeList()) vect_BadFebEntry = cl_LArBadChannelDBTools.readBadFeb(al, ms) feblist = set() for (id, mf) in vect_BadFebEntry: if not mf.good(): feblist.add(id.get_identifier32().get_compact()) retval.append((obj.since(), obj.until(), feblist)) db.closeDatabase() return retval
def readFromCool(self, iovStartUnix, iovEndUnix, folderChannelDict, folderVariableDict): """ """ self.reconnect() #=== verify and translate times, maximum is current time #=== need iovStart/End as cool::ValidityKey if not (iovStartUnix > 0): iovStartUnix = self.R2minTime if not (iovEndUnix > iovStartUnix and iovEndUnix > 0): iovEndUnix = int(time.time()) iovS = cool.ValidityKey(iovStartUnix * self.unix2cool) iovE = cool.ValidityKey(iovEndUnix * self.unix2cool) print "IOV start: ", time.ctime( iovS / self.unix2cool), "\t COOL time stamp: ", iovS if iovStartUnix < self.R2minTime and iovEndUnix > self.R2minTime: print "R1->R2 tm: ", time.ctime( self.R2minTime ), "\t COOL time stamp: ", self.R2minTime * self.unix2cool print "IOV end : ", time.ctime( iovE / self.unix2cool), "\t COOL time stamp: ", iovE #=== initialize return list folderVarSetList = [] #=== loop over all different folders and build list of FolderVarSets for folder, channels in folderChannelDict.iteritems(): logging.debug("treating folder: %s" % folder) varsInFolder = folderVariableDict[folder] logging.debug("---> variables in this folder: %s" % varsInFolder) folderVarSet = FolderVarSet(varsInFolder, self.putDuplicates) ar = [iovS, iovE] #=== loop over different databases while self.check_db(iovStartUnix, iovEndUnix, ar): iovStart = ar[0] iovEnd = ar[1] coolFolder = self.dbV[self.dbCounter].getFolder(folder) coolFolder.setPrefetchAll(False) #=== save some RAM for channel in channels: drawer = self.info.get_drawer(folder, channel) logging.debug("---> treating channel %s, drawer %s" % (channel, drawer)) channelSelection = cool.ChannelSelection(channel) objs = coolFolder.browseObjects(iovStart, iovEnd, channelSelection) bar = progressBar(iovStart, iovEnd, 78, drawer) while objs.goToNext(): obj = objs.currentRef() iovSince = max(iovStart, obj.since()) for var in varsInFolder: #=== pre- and append stuff for AI variables if '/HVSET' in folder: coolVarName = var.split(".")[1] elif '/AI' in folder: coolVarName = "AI_" + var + "_VALUE" else: coolVarName = var.upper() sval = obj.payloadValue(coolVarName) if sval == "NULL": sval = "0" val = float(sval) logging.debug( "-------------> %s (%s) %s : %f" % (iovSince, time.ctime( iovSince / self.unix2cool), var, val)) folderVarSet.setVariable(var, val) folderVarSet.registerInIOVStore(drawer, iovSince) bar.update(iovSince) bar.done() #=== release server resources objs.close() folderVarSetList.append(folderVarSet) return folderVarSetList
def getSyncDCSTree(self, h2000, drawer, variables=[], timeVarName="EvTime"): """ This function returns a tree containing the DCS variables in <variables> for the drawer <drawer> synchronized with the input tree <h2000>. The name of the time stamp used for synchronization is <timeVarName>. Input: - h2000 : a TTree containing at least the variable <timeVarName> - drawer : The drawer of interst in string from, i.e. \"LBC02\" - variables : A python list containing all the variable names of interest. By default all variables are attached. - timeVarName: name of the time stamp in h2000 tree """ #=== avoid variable duplication if not len(variables): variables = self.info.get_all_variables() variables = list(set(variables)) #=== sort variables by directories folders = self.info.get_variables_by_folder(variables, drawer) #=== prepare input tree for fast reading evTime = array('i', [0]) h2000.SetBranchStatus("*", 0) h2000.SetBranchStatus(timeVarName, 1) h2000.SetBranchAddress(timeVarName, evTime) nEntries = h2000.GetEntries() #=== create the friend friendName = drawer t = ROOT.TTree(friendName, friendName) t.SetDirectory(0) buffer = [] nVars = len(variables) previousVal = {} for iVar in xrange(nVars): var = variables[iVar] arrayType, treeType = self.info.get_variable_type(var) previousVal[var] = array(arrayType, [0]) buffer.append(array(arrayType, [0])) t.Branch(var, buffer[iVar], var + treeType) #=== loop over input tree h2000 timeBeg = -1 timeEnd = -1 bar = progressBar(0, nEntries, 78) for jentry in xrange(nEntries): #=== load current entry ientry = h2000.LoadTree(jentry) if ientry < 0: break nb = h2000.GetEntry(jentry) if nb <= 0: continue bar.update(jentry) #=== access cool only if IOV changed if timeEnd == -1: timeBeg = evTime[0] if evTime[0] != timeEnd: timeEnd = evTime[0] valKey = cool.ValidityKey(evTime[0] * self.unix2cool) for folder, varlist in folders.iteritems(): coolFolder = self.db.getFolder(folder) channel = self.info.get_channel(folder, drawer) obj = coolFolder.findObject(valKey, channel) #payload = obj.payload() for var in varlist: if '/HVSET' in folder: coolVarName = var.split(".")[1] elif '/AI' in folder: coolVarName = "AI_" + var + "_VALUE" else: coolVarName = var.upper() #previousVal[var][0] = payload[coolVarName] sval = obj.payloadValue(coolVarName) if sval == "NULL": sval = "0" previousVal[var][0] = float(sval) #=== fill the buffers with current value for iVar in xrange(len(variables)): var = variables[iVar] buffer[iVar][0] = previousVal[var][0] #=== fill friend tree t.Fill() #=== reset trees h2000.SetBranchStatus("*", 1) t.ResetBranchAddresses() bar.done() #=== print out stats print "IOV start: ", time.ctime(timeBeg), "\t COOL time stamp: ", ( timeBeg * self.unix2cool) print "IOV end : ", time.ctime(timeEnd), "\t COOL time stamp: ", ( timeEnd * self.unix2cool) return t
def main(): parser = OptionParser( usage="%prog [options] iovfile", version=__version__, description= """This script produces SQLite data base from a text file with directory names and validity intervals. Each line in the input file should look like \n directoryname : begin-iov : end-iov \n for example \n NewAlignment : 2009-10-22_10:48:00 : 2009-10-22_10:53:15 \n Unless '-n' is specified, the script first makes a snapshot of the default databse using CondDBAdmin_MakeSnapshot.py. This is essential if you want to use the result as a layer.""" ) parser.add_option("-s", "--sqlitefilename", dest="sqlitefilename", type="string", help="name of the output sqlitefile [default: %default]", default="LocalDDDB.db") parser.add_option("-p", "--partition", dest="partition", type="string", help="partition [default: %default]", default="LHCBCOND") parser.add_option("-n", "--no-snapshot", action="store_true", dest="doNotMakeSnapShot", help="do not create a snapshot before adding the files", default=False) parser.add_option("-T", "--tag", action="store_true", dest="tag", help="tag for the snapshot [default: %default]", default="HEAD") (opts, args) = parser.parse_args() # check arguments if len(args) != 1: parser.error("not enough arguments. Try with --help.") # do we want to load the files on top of the snapshot? if so, make a snapshot # first make a snap shot #sys.exec('CondDBAdmin_MakeSnapshot.py -T head-20090928 LHCBCOND sqlite_file:VeloIOVTest.db/LHCBCOND') connectString = "sqlite_file:" + opts.sqlitefilename + "/" + opts.partition if not opts.doNotMakeSnapShot: #from CondDBAdmin_MakeSnapshot import main as CondDBAdmin_MakeSnapshot_main # remove database if it exists os.system('rm -f ' + opts.sqlitefilename) # create a new one os.system('CondDBAdmin_MakeSnapshot.py -T ' + opts.tag + ' ' + opts.partition + ' ' + connectString) # now read the input files and do something inputfilename = sys.argv[1] inputfilename = args[0] print 'Processing file: ', inputfilename db = CondDBUI.CondDB(connectString, create_new_db=True, readOnly=False) f = open(inputfilename, 'r') lines = f.read().splitlines() for line in lines: if len(line) < 5: continue if line[0] == '#': continue words = line.split(' : ') directory = words[0].strip(' ') since = words[1].strip(' ') until = words[2].strip(' ') print "Now inserting ", directory from PyCool import cool from GaudiPython.Bindings import gbl try: intsince = cool.ValidityKey(since) except ValueError: intsince = CondDBUI.Admin.timeToValKey(since, cool.ValidityKeyMin) try: intuntil = cool.ValidityKey(until) except ValueError: intuntil = CondDBUI.Admin.timeToValKey(until, cool.ValidityKeyMax) #intsince = CondDBUI.Admin.timeToValKey( since, cool.ValidityKeyMin) #intuntil = CondDBUI.Admin.timeToValKey( until, cool.ValidityKeyMax) print "From: ", since, " --> ", intsince, " --> ", gbl.Gaudi.Time( intsince).format(True, "%c") print "Until: ", until, " --> ", intuntil, " --> ", gbl.Gaudi.Time( intuntil).format(True, "%c") CondDBUI.Admin.MakeDBFromFiles(directory, db, includes=[], excludes=[], verbose=True, since=intsince, until=intuntil) # make sure to tag everything if opts.tag != 'HEAD': db.recursiveTag('/', opts.tag)
def findLArHVTrip(db, folderName, t1, t2, lbtimes=None): allTrips = [] if not db.existsFolder(folderName): print "ERROR: Folder", folderName, "does not exist" return None f = db.getFolder(folderName) #lastVoltage=dict() #lastStat=dict() offMap = dict() tripped = set() stablezero = set() itr = f.browseObjects(cool.ValidityKey(t1), cool.ValidityKey(t2), cool.ChannelSelection.all()) while itr.goToNext(): obj = itr.currentRef() chid = obj.channelId() pl = obj.payload() stat = pl["R_STAT"] v = pl["R_VMEAS"] i = pl["R_IMEAS"] #if chid==322000: # print "Status",eventInfo(obj,stat,v,i,lbtimes) if not offMap.has_key(chid): offMap[chid] = isOff(stat) if offMap[chid] == True: continue if chid in tripped: #Channel previously found to be tripped #print "Status: ",eventInfo(obj,stat,v,i,lbtimes) if isAllOk(stat) and v > 50: print "Recovered: ", eventInfo(obj, stat, v, i, lbtimes) tripped.remove(chid) if chid in stablezero: stablezero.remove(chid) if chid in stablezero: if not isStableZero(stat, v, i): print "Not any more stable at zero voltage", eventInfo( obj, stat, v, i, lbtimes) stablezero.remove(chid) else: # Not (yet) in stablezero: if isStableZero(stat, v, i): print "Stable at zero voltage", eventInfo( obj, stat, v, i, lbtimes) # LB+1!!! stablezero.add(chid) else: #Not (yet) found to be #if not isOff(stat) and not isAllOk(stat): if not isAllOk(stat): print "\nTrip: ", eventInfo(obj, stat, v, i, lbtimes) tripped.add(chid) pass itr.close() return
#pre-fill with zero for i in range(0,3564): mask.append(0x0) t8b4e=[0x3,0x3,0x3,0x3, 0x3,0x3,0x3,0x3, 0x0,0x0,0x0,0x0] for i in range(0,20): #create a train of 20 8be4 groups start mask[100+i*12:100+(i+1)*12]=t8b4e return mask if __name__=="__main__": db,folder=createSqlite("test.db") d1=createBCMask1() d2=createBCMask2() fillFolder(folder,d1,iovMin=cool.ValidityKey(1e6),iovMax=cool.ValidityKey(2e6)) fillFolder(folder,d2,iovMin=cool.ValidityKey(2e6),iovMax=cool.ValidityKey(3e6)) db.closeDatabase()
#$ print " TimeRange:",1e-9*b[0]-t0,1e-9*b[1]-t0," Duration",int(1e-9*(b[1]-b[0])) #+------------------------------------ # Modify the lables, some records have their lables in cool: #+------------------------------------ print "Selected Busy Counters are:", OPT["--countersnames"] busyCfg = {} if not OPT["--keepnames"]: #+------------------------------------------- # Read the busy configuration, names and enabled:on/off #+------------------------------------------- try: from PyCool import cool lbROV0 = cool.ValidityKey((OPT["--run"] << 32) + 2) # lb 2 -> # lbROV1=cool.ValidityKey((OPT["--run"]+1)<<32-1) # lb 3 Cobj = busyConffolder.browseObjects(lbROV0, lbROV0, cool.ChannelSelection.all()) for tCobj in Cobj: b = {} x = tCobj.payload() b['identifier'] = x["identifier"] b['busyEnabled'] = x["busyEnabled"] b['description'] = x["description"] busyCfg[x["identifier"]] = b # take info from anywhere in the run t0 = TimeRanges[0][0] #first time t1 = TimeRanges[-1][1] #last time
def intTime( time, default ): try: inttime = cool.ValidityKey(time) except ValueError: inttime = CondDBUI.Admin.timeToValKey( time, default) return inttime
def bunchSpacingOfRun(runnumber, LB, verbose=False): if (runnumber < 236107): print("WARNING BunchSpacingUtils don't work for run-1 data") return None tdaqDBName = "COOLONL_TDAQ/CONDBR2" folder = "/TDAQ/OLC/LHC/FILLPARAMS" iovtime = getTimeForLB(runnumber, LB, readOracle=True) if iovtime == 0: print("ERROR, can't get start time of run %i, LB %i" % (runnumber, LB)) return None obj = None db = None try: db = indirectOpen(tdaqDBName, oracle=True) f = db.getFolder(folder) obj = f.findObject(cool.ValidityKey(iovtime), 0) except Exception as e: print(e.args[0]) if len(e.args) > 1 and e.args[0].find("Object not found - 0"): print("WARNING No data found in folder %s for run/LB %i/%i" % (folder, runnumber, LB)) else: print("BunchSpacingUtils: ERROR accesssing folder", folder, "on db", tdaqDBName) print(e) if db is not None: db.closeDatabase() return None pl = obj.payload() buf = pl["BCIDmasks"] bucketDiff = 0 firstFilled = -1 lastFilled = -1 bucketDiffs = [] for iBucket, filled in enumerate(buf): if filled != 0: if (verbose): print("Bucket", iBucket, "filled") lastFilled = iBucket if firstFilled < 0: firstFilled = iBucket if (verbose): print("First filled bucket=", iBucket) else: if (verbose): print("Bucket #%i, bunch spacing=%i * 25ns" % (iBucket, bucketDiff)) bucketDiffs.append(bucketDiff) bucketDiff = 1 pass else: # not filled if (verbose): print("Bucket", iBucket, "not filled") bucketDiff += 1 pass #Handle wrap-around: if (firstFilled >= 0 and lastFilled > 0): bucketDiffs.append(len(buf) - lastFilled + firstFilled) if (verbose): print("Bunchdiff at wrap-around:", (len(buf) - lastFilled + firstFilled)) if db is not None: db.closeDatabase() if len(bucketDiffs) == 0: return None else: return min(bucketDiffs)