#!/usr/bin/python # # This script adds the RunPeriod-2019-11 GEM/TRD 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('checkTRD2tt.txt','w') first_slot = 3 wire_slot = 0 wire_y_ch0 = 24 gem_x_slot = 0 gem_x_ch0 = 48 gem_y_slot = 4 gem_y_ch0 = 0 def GetXSlot(gch): return gem_x_slot+int((gch+gem_x_ch0)/72)+first_slot def GetXChan(gch): fch=int((gch+gem_x_ch0)%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() 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 name,crateid from Crate where rocid=76") row = cur.fetchone() cratename = row['name'] crateid = row['crateid'] print 'TRD Crate ID: %d' % crateid # 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-11-default' 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) ################################################################### TRD ################################################################### # global channel ID that should be incremented for all channels chanid = min_chanid f.write(" plane, strip, slot, ch\n") plane=4 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,slot)) row = cur.fetchone() moduleid = row['moduleid'] cur.execute("SELECT chanid from Channel where moduleid=%d and channel=%d" % (moduleid,channel)) row = cur.fetchone() if row != None: # if channel exists already then insert new run period and mapping loc_chanid = row['chanid'] cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'TRD', 'adc_chanid', 1, %d)" % (loc_chanid, moduleid, name, channel, runrangeid)) cur.execute("INSERT INTO TRD VALUES (%d, %d, %d, %d)" % (plane, strip, loc_chanid, runrangeid)) else: # if channel doesn't exist make new entry 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=5 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,slot)) row = cur.fetchone() moduleid = row['moduleid'] cur.execute("SELECT chanid from Channel where moduleid=%d and channel=%d" % (moduleid,channel)) row = cur.fetchone() if row != None: # if channel exists already then insert new run period and mapping loc_chanid = row['chanid'] cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'TRD', 'adc_chanid', 1, %d)" % (loc_chanid, moduleid, name, channel, runrangeid)) cur.execute("INSERT INTO TRD VALUES (%d, %d, %d, %d)" % (plane, strip, loc_chanid, runrangeid)) else: # if channel doesn't exist make new entry 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=6 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,slot)) row = cur.fetchone() moduleid = row['moduleid'] cur.execute("SELECT chanid from Channel where moduleid=%d and channel=%d" % (moduleid,channel)) row = cur.fetchone() if row != None: # if channel exists already then insert new run period and mapping loc_chanid = row['chanid'] cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'TRD', 'adc_chanid', 1, %d)" % (loc_chanid, moduleid, name, channel, runrangeid)) cur.execute("INSERT INTO TRD VALUES (%d, %d, %d, %d)" % (plane, strip, loc_chanid, runrangeid)) else: # if channel doesn't exist make new entry 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=7 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,slot)) row = cur.fetchone() moduleid = row['moduleid'] cur.execute("SELECT chanid from Channel where moduleid=%d and channel=%d" % (moduleid,channel)) row = cur.fetchone() if row != None: # if channel exists already then insert new run period and mapping loc_chanid = row['chanid'] cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'TRD', 'adc_chanid', 1, %d)" % (loc_chanid, moduleid, name, channel, runrangeid)) cur.execute("INSERT INTO TRD VALUES (%d, %d, %d, %d)" % (plane, strip, loc_chanid, runrangeid)) else: # if channel doesn't exist make new entry 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 moduleid FROM Module where type='SRS' and slot=%d" % slot) row = cur.fetchone() moduleid = row['moduleid'] print 'SRS MODULE ID: %d' % moduleid for apv in range(0,16): for ch in range(0,128): plane = int(apv)/2 if apv > 7 and apv < 12: # GEM2 is the last two planes plane += 4 #if apv > 11 and apv < 16: # GEM TRD is the second to last plane # plane -= 4 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("SELECT chanid from Channel where moduleid=%d and channel=%d" % (moduleid,channel)) row = cur.fetchone() if row != None: # if channel exists already then insert new run period and mapping loc_chanid = row['chanid'] cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'TRD', 'adc_chanid', 1, %d)" % (loc_chanid, moduleid, name, channel, runrangeid)) cur.execute("INSERT INTO TRD VALUES (%d, %d, %d, %d)" % (plane, strip, loc_chanid, runrangeid)) else: # if channel doesn't exist make new entry 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()