#!/usr/bin/python # # # 10/8/2014 David Lawrence # # # The order in which the 2 connectors going into the F1TDC module for # the FDC wires was changed from the orginal spreadsheet (P0 <-> P1). # This means that each group of 24 needs to be shifted either 24 channels # up or 24 channels down. Wire numbers should be shifted as follows. # # 1-24 -> 25-48 # 25-48 -> 1-24 # 49-72 -> 73-96 # 73-96 -> 49-72 # 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 wire FROM Channel,Module,Crate,FDC_wires WHERE Crate.crateid=Module.crateid AND Module.moduleid=Channel.moduleid AND system="FDC_Wires" AND channel=0 AND tdc_chanid=chanid GROUP BY wire ORDER BY wire' cur.execute(sql) s_rows = cur.fetchall() if len(s_rows) != 2: print '%d wires found with F1TDC channel 0 when there should be exactly 2' % len(s_rows) print 'Exiting without changing file.' sys.exit(-1) if s_rows[0][0]!=1 or s_rows[1][0]!=49: print 'Channel 0 not connected to wires 1 and 49. This file may have already' print 'been updated. (%d, %d)' % (s_rows[0][0], s_rows[1][0]) print 'Exiting without changing file.' sys.exit(-1) # Get all entries from FDC_Wires table and update them one at a time print 'updating ...' sql = 'SELECT wire,tdc_chanid FROM FDC_Wires' cur.execute(sql) s_rows = cur.fetchall() N = 0 for row in s_rows: wire = row['wire'] tdc_chanid = row['tdc_chanid'] if wire<=24 : wire += 24 elif wire<=48 : wire -= 24 elif wire<=72 : wire += 24 elif wire<=96 : wire -= 24 else: print 'wire number out of range! (%d)' % wire sys.exit(-1) sql = 'UPDATE FDC_Wires SET wire=%d WHERE tdc_chanid=%d' % (wire,tdc_chanid) #print sql cur.execute(sql) N += 1 print 'Updated %d FDC wires' % N print 'Done'