#!/usr/bin/python # # # (Note: you should run tt_fix_fdc_strip_no2.py after this. See comments # there for details 8/9/2014 DL ) # # 4/29/2014 David Lawrence # # # This script will modify a tt.db file to use the correct fADC channel numbers # for the FDC cathode strips. When the TT was originally created from the # spreadsheet, the channel numbers were assumed to be increasing with increasing # strip number. It turns out this is incorrect. The simplest fix seems to be to # modify the channel numbers in the Channel table. This has to be done on a # connector-by-connector basis. Each connector has 24 channels and it is that # group of 24 that must be reversed. # # 0 <= channel <= 23 channel -> (23 - channel) # # # 23 <= channel <= 47 channel -> (47 - channel + 24) # or (71 - channel) # # 48 <= channel <= 71 channel -> (71 - channel + 48) # or (119 - channel) # import sqlite3 as lite import sys import os import re import datetime if len(sys.argv) != 2: print "You must supply a SQLite DB file to modify!" sys.exit(0); # Connect to SQLite DB file db_filename = sys.argv[1] print "" print "opening SQLite DB file: %s" % db_filename print "---------------------------------------------" con = lite.connect(db_filename) with con: # 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 that this TT file has not already been updated sql = 'SELECT channel,strip FROM Channel,FDC_Cathodes WHERE chanid=adc_chanid AND channel=0 AND strip=1' cur.execute(sql) s_rows = cur.fetchall() if len(s_rows) != 48: print 'This file may have already been updated. Expected 48 rows from the following' print 'query, but got %d' % len(s_rows) print '\n%s\n' % sql sys.exit(-1) # Correct channels 0-23 print 'updating channels 0-23 ...' subselect = 'SELECT adc_chanid FROM Channel,FDC_Cathodes WHERE chanid=adc_chanid AND (channel BETWEEN 0 AND 23)' sql = 'UPDATE Channel SET channel=(23-channel) WHERE chanid IN (%s)' % subselect cur.execute(sql) # Correct channels 24-47 print 'updating channels 24-47 ...' subselect = 'SELECT adc_chanid FROM Channel,FDC_Cathodes WHERE chanid=adc_chanid AND (channel BETWEEN 24 AND 47)' sql = 'UPDATE Channel SET channel=(71-channel) WHERE chanid IN (%s)' % subselect cur.execute(sql) # Correct channels 48-71 print 'updating channels 48-71 ...' subselect = 'SELECT adc_chanid FROM Channel,FDC_Cathodes WHERE chanid=adc_chanid AND (channel BETWEEN 48 AND 71)' sql = 'UPDATE Channel SET channel=(119-channel) WHERE chanid IN (%s)' % subselect cur.execute(sql) print 'Done'