#!/usr/bin/python # # Summer 2022 (beginning with run 100942) # - Move rocfdc11 module from slot 3 -> 19 # 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() # get run range ID to add reference counters runrange_name = 'RunPeriod-2022-05++' cur.execute("SELECT runrangeid FROM RunRange WHERE name='%s'" % runrange_name) rows = cur.fetchall() if len(rows) == 0: print 'Unable to find id for run range with name "%s"!' % runrange_name sys.exit(-1) runrangeid = rows[0][0] # Get max chanid and moduleid already in DB and set our values for the new module cur.execute("SELECT MAX(chanid) FROM Channel") row = cur.fetchone() chanid_min = row[0]+1 cur.execute("SELECT MAX(moduleid) FROM Module") row = cur.fetchone() moduleid_min = row[0]+1 print 'First fADC125 module ID: %d' % moduleid_min # Insert new module for FDC channels moduleid = moduleid_min crateid = 53 # rocfdc11 -> rocid = 61 slotid = 19 cur.execute("INSERT INTO Module Values (%d, %d, %d, 'fADC125', '')" % (moduleid, crateid, slotid)) # Find previous moduleid to replace previous_slotid = 3 cur.execute("SELECT * from Module WHERE crateid=%d AND slot=%d" % (crateid, previous_slotid)) rows = cur.fetchall() previous_moduleid = rows[0][0] print("Previous module = ", previous_moduleid) # Update channels with new moduleid sql = 'SELECT * from Channel WHERE moduleid=%d AND runrangeid=27' % (previous_moduleid) cur.execute(sql) rows = cur.fetchall() print(rows) ichan = 0 for i, row in enumerate(rows): chanid = chanid_min+i cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, '%s', '%s', %d, %d)" % (chanid,moduleid,row[2],row[3],row[4],row[5],row[6],runrangeid)) cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, '%s', '%s', %d, %d)" % (row[0],row[1],'FDC-unused',0,'','',0,runrangeid)) cathode_sql = 'SELECT * from FDC_Cathodes WHERE adc_chanid=%d AND runrangeid=27' % (row[0]) cur.execute(cathode_sql) cathode_row = cur.fetchall()[0] cur.execute("INSERT INTO FDC_Cathodes Values (%d, %d, '%s', %d, '%s', %d, %d)" % (cathode_row[0],cathode_row[1],cathode_row[2],cathode_row[3],cathode_row[4],chanid,runrangeid)) #cur.execute("UPDATE Channel SET name='FDC-unused', system='', col_name='', enable=0 WHERE chanid=%d AND runrangeid=%d" % (row[0],27)) #sql = 'UPDATE Channel SET moduleid=%d,runrangeid=%d WHERE moduleid=%d AND runrangeid=27' % (moduleid, runrangeid, previous_moduleid) #cur.execute(sql) print 'Done'