#!/usr/bin/python # # This fixes a cable swap described in an e-mail from Mark D. On Jan. 6, 2015: # # # Hi David, # # There is a BCAL cable swap for the Fall running. Please can you correct this in the translation # table. # # On the Upstream end: Module 40, Layer 3, Sector 2 is swapped with Module 40, Layer 3, Sector 3. I # looked this up and it corresponds to Roc 32, Slot 10, channel 1 and 2 that are swapped. # # We will correct this swap in hardware so that the translation table for the Spring run will be # as-designed. # # Thanks. # # Best, # Mark Macrae Dalton # import sqlite3 as lite import sys import os import getpass import socket import itertools import csv import re from time import gmtime, localtime, strftime if len(sys.argv) != 2: print "You must supply a sqlite file (e.g. tt.db)" sys.exit(0); con = lite.connect(sys.argv[1]) #------------------------ # main #------------------------ with con: global cur # Specify that next cursor should be "dictionary" # (i.e. python's hash map) so columns can be indexed # by name con.row_factory = lite.Row # Create Cursor cur = con.cursor() # Check if swap is already done subselect = 'SELECT chanid FROM Crate,Module,Channel WHERE Crate.crateid=Module.crateid AND Module.moduleid=Channel.moduleid AND rocid=32 AND slot=10 AND channel=1' query = 'SELECT * FROM BCAL WHERE adc_chanid=(%s)' % subselect cur.execute(query) row1 = cur.fetchall()[0] print 'module=%d layer=%d sector=%d end=%s adc_chanid=%d' % (row1['module'], row1['layer'], row1['sector'], row1['end'], row1['adc_chanid']) subselect = 'SELECT chanid FROM Crate,Module,Channel WHERE Crate.crateid=Module.crateid AND Module.moduleid=Channel.moduleid AND rocid=32 AND slot=10 AND channel=2' query = 'SELECT * FROM BCAL WHERE adc_chanid=(%s)' % subselect cur.execute(query) row2 = cur.fetchall()[0] print 'module=%d layer=%d sector=%d end=%s adc_chanid=%d' % (row2['module'], row2['layer'], row2['sector'], row2['end'], row2['adc_chanid']) if row1['module']!=40 or row1['layer']!=3 or row1['sector']!=2 or row1['end']!='U' or row2['module']!=40 or row2['layer']!=3 or row2['sector']!=3 or row2['end']!='U': print 'Existing DB does not have pre-swapped cable definitions. Assuming this file' print 'was already updated. Quitting, leaving the file untouched.' sys.exit(0) # swap cables by swapping adc_chanid adc_chanid1 = row1['adc_chanid'] adc_chanid2 = row2['adc_chanid'] print 'Swapping channels: %d <--> %d' % (adc_chanid1, adc_chanid2) query = 'UPDATE BCAL SET adc_chanid=-1000 WHERE adc_chanid=%d' % (adc_chanid1) cur.execute(query) query = 'UPDATE BCAL SET adc_chanid=%d WHERE adc_chanid=%d' % (adc_chanid1, adc_chanid2) cur.execute(query) query = 'UPDATE BCAL SET adc_chanid=%d WHERE adc_chanid=-1000' % (adc_chanid2) cur.execute(query) print ' Done.'