#!/usr/bin/python # # # 11/18/2019 Justin Stevens # # # This script will modify a tt.db file to add the # DIRC LED reference counters # # 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() ######################## ## Reference counters ## ######################## # get run range ID to add reference counters runrange_name = 'RunPeriod-2019-11+' 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] cur.execute("SELECT MAX(chanid) FROM Channel") row = cur.fetchone() chanid = row[0]+1 cur.execute("SELECT MAX(moduleid) FROM Module") row = cur.fetchone() moduleid = row[0]+1 # get channel and module ID to start counting from cratename = 'D2-1-TOP' rocid = 77 slot = 19 channel = 15 cur.execute("SELECT moduleid from Module,Crate where rocid==%d and slot==%d and module.crateid==crate.crateid" % (rocid,slot)) row = cur.fetchone() # create module if it doesn't exist if row == None: cur.execute("SELECT * from Crate where rocid==%d" % (rocid)) row = cur.fetchone() crateid = row['crateid'] cur.execute("INSERT INTO Module Values (%d, %d, %d, 'fADC250', '')" % (moduleid, crateid, slot)) name = 'DIRC_LED_REF_A' cur.execute("SELECT moduleid from Module,Crate where rocid==%d and slot==%d and module.crateid==crate.crateid" % (rocid,slot)) row = cur.fetchone() moduleid = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'DIRC_REF', 'adc_chanid', 1, %d)" % (chanid, moduleid, name, channel, runrangeid)) adc_chanid = chanid cur.execute("INSERT INTO Channel Values (41140, 1048, 'DIRC_REF-unused', 15, '', '', 0, %d)" % runrangeid) # finally insert into channel table tdc_chanid = 41141 cur.execute("INSERT INTO DIRC_REF VALUES (%d, %d, %d)" % (adc_chanid, tdc_chanid, runrangeid)) print 'Done'