#!/usr/bin/python # # This script adds the RunPeriod-2025-01 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('checkTRD3tt.txt','w') first_slot = 3 gem_x_slot = 0 gem_x_ch0 = 0 gem_y_slot = 10 gem_y_ch0 = 0 def GetXSlot(gch): slot = gem_x_slot+int((gch+gem_x_ch0)/72)+first_slot if slot>10: slot=slot+2 return 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 cch+card*24 def GetYSlot(gch): slot = gem_y_slot+int((gch+gem_y_ch0)/72)+first_slot if slot>10: slot=slot+2 return 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 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) # Add modules for TRD fADC modules (moduleid|crateid|slot|type|SN) cur.execute("SELECT MAX(moduleid) FROM Module") row = cur.fetchone() moduleid_min = row[0]+1 for module in range(8,16): moduleid = moduleid_min + module slotid = 3 + module if slotid>10: slotid=slotid+2 cur.execute("INSERT INTO Module Values (%d, %d, %d, 'fADC125', '')" % (moduleid, crateid, slotid)) # Get runrangeid based on name. This should be created ahead of time via: # hd_tt_tool.py runrange create 130000 inf RunPeriod-2025-01-default runrange_name = 'RunPeriod-2025-01-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=1 f.write(" GEM-TRD X plane\n") for strip in range(0,720): slot = GetXSlot(strip) channel = GetXChan(strip) name = "TRD-%d-%d" % (plane, strip+1) f.write(" %d %d %d %d\n" % (plane, strip+1, 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+1, 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+1, chanid, runrangeid)) chanid += 1 plane=2 f.write(" GEM-TRD Y plane\n") for strip in range(0,432): slot = GetYSlot(strip) channel = GetYChan(strip) name = "TRD-%d-%d" % (plane, strip+1) f.write(" %d %d %d %d\n" % (plane, strip+1, 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+1, 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+1, chanid, runrangeid)) chanid += 1 # remove old SRS module moduleid = 1057 for channel in range(0,2048): 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', '', 0, %d)" % (loc_chanid, moduleid, "TRD-unused", channel, runrangeid)) print('Done') f.close()