#!/usr/bin/python # # This script adds the TRD fADC125 to the translation table (mapping from Lubomir). # # This is intended as a one time script that will add the info. to the # tt.db file where it will be maintained. import sqlite3 as lite import sys import os import re import datetime # diagnostic text file for checking translation f = open('checkTRDtt.txt','w') first_slot = 3 wire_slot = 0 wire_y_ch0 = 24 gem_x_slot = 1 gem_y_slot = 4 gem_y_ch0 = 24 def GetXSlot(gch): return gem_x_slot+int(gch/72)+first_slot def GetXChan(gch): fch=int(gch%72) #inversed fADC chan card=int(fch/24) cch=int(fch%24) #inversed card chan return 23-cch+card*24 def GetYSlot(gch): return gem_y_slot+int((gch+gem_y_ch0)/72)+first_slot def GetYChan(gch): fch=int((gch+gem_y_ch0)%72) #inversed fADC chan card=int(fch/24) cch=int(fch%24) #inversed card chan return 23-cch+card*24 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 cur.execute("SELECT * FROM sqlite_master WHERE type = 'table' AND name = 'TRD'") rf_rows = cur.fetchall() if len(rf_rows) != 0: print 'TRD table was already created' sys.exit(-1) print 'Write TRD table' # Get max chanid already in DB and set our min_chanid to be one more than that cur.execute("SELECT MAX(chanid) FROM Channel") row = cur.fetchone() min_chanid = row[0]+1 print 'min_chanid = ' + str(min_chanid) # Get max crateid already in DB and set our values for our TRD modules based on that cur.execute("SELECT MAX(crateid) FROM Crate") row = cur.fetchone() crateid_min = row[0]+1 print 'TRD Crate ID: %d' % crateid_min # Get max moduleid already in DB and set our values for our TRD modules based on that cur.execute("SELECT MAX(moduleid) FROM Module") row = cur.fetchone() moduleid_min = row[0]+1 print 'First fADC125 module ID: %d' % moduleid_min # Get runrangeid based on name. This should be created ahead of time via: # hd_tt_tool.py runrange create 70000 inf RunPeriod-2019-11-default runrange_name = 'RunPeriod-2019-01+' 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] print 'runrangeid = ' + str(runrangeid) # Add crates (crateid|name|area|rack|location|SN|Function|rocid|host|IP) cratename = 'S-1-TRD' cur.execute("INSERT INTO Crate Values (%d, '%s', 'S', 1, 'TRD', '', 'fADC125', 76, '', '129.57.153.76')" % (crateid_min, cratename)) # Add modules for TRD fADC modules (moduleid|crateid|slot|type|SN) for module in range(0,8): moduleid = moduleid_min + module slotid = 3 + module cur.execute("INSERT INTO Module Values (%d, %d, %d, 'fADC125', '')" % (moduleid, crateid_min, slotid)) # make TRD table for inserting cur.execute("CREATE TABLE TRD(plane INT, strip INT, adc_chanid INT, runrangeid INTEGER DEFAULT 1)") ################################################################### TRD ################################################################### # global channel ID that should be incremented for all channels chanid = min_chanid f.write(" plane, strip, slot, ch\n") plane=0 f.write(" W-TRD X plane\n") for strip in range(0,24): slot = wire_slot+first_slot channel = strip name = "TRD-%d-%d" % (plane, strip) f.write(" %d %d %d %d\n" % (plane, strip, slot, channel)) cur.execute("SELECT moduleid from Module where crateid=%d and slot=%d" % (crateid_min,slot)) row = cur.fetchone() moduleid = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'TRD', 'adc_chanid', 1, %d)" % (chanid, moduleid, name, channel, runrangeid)) cur.execute("INSERT INTO TRD VALUES (%d, %d, %d, %d)" % (plane, strip, chanid, runrangeid)) chanid += 1 plane=1 f.write(" W-TRD Y plane\n") for strip in range(0,24): slot = wire_slot+first_slot channel = strip+wire_y_ch0 name = "TRD-%d-%d" % (plane, strip) f.write(" %d %d %d %d\n" % (plane, strip, slot, channel)) cur.execute("SELECT moduleid from Module where crateid=%d and slot=%d" % (crateid_min,slot)) row = cur.fetchone() moduleid = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'TRD', 'adc_chanid', 1, %d)" % (chanid, moduleid, name, channel, runrangeid)) cur.execute("INSERT INTO TRD VALUES (%d, %d, %d, %d)" % (plane, strip, chanid, runrangeid)) chanid += 1 plane=2 f.write(" GEM-TRD X plane\n") for strip in range(0,240): slot = GetXSlot(strip) channel = GetXChan(strip) name = "TRD-%d-%d" % (plane, strip) f.write(" %d %d %d %d\n" % (plane, strip, slot, channel)) cur.execute("SELECT moduleid from Module where crateid=%d and slot=%d" % (crateid_min,slot)) row = cur.fetchone() moduleid = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'TRD', 'adc_chanid', 1, %d)" % (chanid, moduleid, name, channel, runrangeid)) cur.execute("INSERT INTO TRD VALUES (%d, %d, %d, %d)" % (plane, strip, chanid, runrangeid)) chanid += 1 plane=3 f.write(" GEM-TRD Y plane\n") for strip in range(0,240): slot = GetYSlot(strip) channel = GetYChan(strip) name = "TRD-%d-%d" % (plane, strip) f.write(" %d %d %d %d\n" % (plane, strip, slot, channel)) cur.execute("SELECT moduleid from Module where crateid=%d and slot=%d" % (crateid_min,slot)) row = cur.fetchone() moduleid = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'TRD', 'adc_chanid', 1, %d)" % (chanid, moduleid, name, channel, runrangeid)) cur.execute("INSERT INTO TRD VALUES (%d, %d, %d, %d)" % (plane, strip, chanid, runrangeid)) chanid += 1 # GEM SRS readout channels slot = 24 cur.execute("SELECT MAX(moduleid) FROM Module") row = cur.fetchone() moduleid_min = row[0]+1 cur.execute("INSERT INTO Module Values (%d, %d, %d, 'SRS', '')" % (moduleid_min, crateid_min, slot)) moduleid = moduleid_min planes = [5, 5, 4, 4, 7, 7, 6, 6] for apv in range(0,8): for ch in range(0,128): plane = planes[apv] # 4 + int(apv)/2 channel = (apv)*128 + ch strip = (32 * (ch%4)) + (8 * int(ch/4)) - (31 * int(ch/16)) name = "TRD-%d-%d" % (plane, strip) if apv%2 == 0: strip += 128 # second apv in plane f.write(" %d %d %d %d\n" % (plane, strip, slot, channel)) cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'TRD', 'adc_chanid', 1, %d)" % (chanid, moduleid, name, channel, runrangeid)) cur.execute("INSERT INTO TRD VALUES (%d, %d, %d, %d)" % (plane, strip, chanid, runrangeid)) chanid += 1 print 'Done' f.close()