#!/usr/bin/env python # # 1/11/2019 JRS # # This script adds meaningful Channel.name values to the tt.db # file based on the convention Hovanes and Vanik defined, namely: # box:boardColumn(1-2):boardRow(1-18):boardPmtColumn(1-3),pmtPixelID(1-64) # # The Channel.name values are used by the EPICS system for scalers. # import os import sys import xml.etree.ElementTree as ET import sqlite3 as lite # Create cursor for tt.db con = lite.connect('tt.db') # Open DB con.row_factory = lite.Row # Specify that next cursor should be "dictionary" cur = con.cursor() # Create Cursor #--------------------------- # UpdateDIRC # # This is called to update the DIRC names, which do not have a default value in # any version of the Channel table, so are explicitly set here #--------------------------- def UpdateDIRC(): # Hovanes request sql = 'UPDATE Module SET type="SSPDIRC" where type="SSP"' cur.execute(sql) sql = 'UPDATE Crate SET function="SSPDIRC" where function="SSP"' cur.execute(sql) sql = 'UPDATE Channel SET col_name="sspdirc_chanid" where system="DIRC" and col_name="ssp_chanid"' cur.execute(sql) print 'DIRC: Selecting channels for update ...' sql = 'SELECT * FROM Channel,DIRC WHERE ssp_chanid=chanid' cur.execute(sql) rows = cur.fetchall() Ntot = len(rows) N = 0 boxes = ['s', 'n'] for row in rows: # use pixel geometry definition to give EPICS names pixel = row['pixel'] box = boxes[pixel/6912] pmtID = (pixel%6912)/64 pmtColumn = pmtID/18 pmtRow = pmtID%18 + 1 boardColumn = pmtColumn/3 + 1 boardPmtColumn = pmtColumn%3 + 1 pmtPixelID = pixel%64 + 1 new_name = 'DIRC-%s-c%d-r%d-%d-%d' % (box, boardColumn, pmtRow, boardPmtColumn, pmtPixelID) #if pmtColumn == 2 and pmtRow == 1: # print '%d: %s' % (pixel, name) sql = 'UPDATE Channel SET name="'+new_name+'" WHERE chanid="'+str(row['chanid'])+'" AND system="'+row['system']+'"' cur.execute(sql) N += 1 sys.stdout.write(' DIRC : %3.1f%%\r' % (100.0*N/Ntot)) sys.stdout.flush() #print sql print '' # change column name to match requirement for scalers in EPICs sql = 'ALTER TABLE DIRC RENAME TO DIRC_ORIG' cur.execute(sql) sql = 'CREATE TABLE DIRC(pixel INT, sspdirc_chanid INT, runrangeid INTEGER DEFAULT 1);' cur.execute(sql) sql = 'INSERT INTO DIRC(pixel, sspdirc_chanid, runrangeid) SELECT pixel, ssp_chanid, runrangeid from DIRC_ORIG;' cur.execute(sql) sql = 'DROP TABLE DIRC_ORIG' cur.execute(sql) #------- DIRC UpdateDIRC() # Commit all changes con.commit()