#!/usr/bin/python # # This script adds the DIRC pixels to the translation table. # # 1/3/2019 D. Lawrence # This script was originally written by Justin S. and then modified # slightly to remove some hardcoded numbers that could change due to # the tt.db having been updated from the CCDB. # # This is intended as a one time script that will add the info. to the # tt.db file where it will be maintained. Here are the steps to running # this: # # > co https://halldsvn.jlab.org/repos/trunk/online/packages/TranslationTable # > cd TranslationTable # > setenv PYTHONPATH $PWD # > $PYTHONPATH/hd_tt_tool.py runrange create 60000 inf RunPeriod-2019-01-default # > ./tt_add_dirc.py tt.db # # To generate an XML file suitable for the CCDB, use the tt_db2xml.py script. # Modify the add_custom_data.py file to actually write the resulting XML # to the CCDB. import sqlite3 as lite import sys import os import re import datetime f = open('checkDIRCtt.txt','w') CHANNEL_PER_PMT = 64 PMT_ROWS = 18 PMT_COLUMNS = 6 MAX_BOX_CHANNEL = CHANNEL_PER_PMT * PMT_ROWS * PMT_COLUMNS fullrows = [[35, 33, 34, 32, 28, 30, 29, 31, 99, 97, 98, 96, 92, 94, 93, 95, 163, 161, 162, 160, 156, 158, 157, 159, 131, 129, 130, 128, 188, 190, 189, 191, 67, 65, 66, 64, 124, 126, 125, 127, 3, 1, 2, 0, 60, 62, 61, 63], [39, 37, 38, 36, 24, 26, 25, 27, 103, 101, 102, 100, 88, 90, 89, 91, 167, 165, 166, 164, 152, 154, 153, 155, 135, 133, 134, 132, 184, 186, 185, 187, 71, 69, 70, 68, 120, 122, 121, 123, 7, 5, 6, 4, 56, 58, 57, 59], [43, 41, 42, 40, 20, 22, 21, 23, 107, 105, 106, 104, 84, 86, 85, 87, 171, 169, 170, 168, 148, 150, 149, 151, 139, 137, 138, 136, 180, 182, 181, 183, 75, 73, 74, 72, 116, 118, 117, 119, 11, 9, 10, 8, 52, 54, 53, 55], [47, 45, 46, 44, 16, 18, 17, 19, 111, 109, 110, 108, 80, 82, 81, 83, 175, 173, 174, 172, 144, 146, 145, 147, 143, 141, 142, 140, 176, 178, 177, 179, 79, 77, 78, 76, 112, 114, 113, 115, 15, 13, 14, 12, 48, 50, 49, 51], [51, 49, 50, 48, 12, 14, 13, 15, 115, 113, 114, 112, 76, 78, 77, 79, 179, 177, 178, 176, 140, 142, 141, 143, 147, 145, 146, 144, 172, 174, 173, 175, 83, 81, 82, 80, 108, 110, 109, 111, 19, 17, 18, 16, 44, 46, 45, 47], [55, 53, 54, 52, 8, 10, 9, 11, 119, 117, 118, 116, 72, 74, 73, 75, 183, 181, 182, 180, 136, 138, 137, 139, 151, 149, 150, 148, 168, 170, 169, 171, 87, 85, 86, 84, 104, 106, 105, 107, 23, 21, 22, 20, 40, 42, 41, 43], [59, 57, 58, 56, 4, 6, 5, 7, 123, 121, 122, 120, 68, 70, 69, 71, 187, 185, 186, 184, 132, 134, 133, 135, 155, 153, 154, 152, 164, 166, 165, 167, 91, 89, 90, 88, 100, 102, 101, 103, 27, 25, 26, 24, 36, 38, 37, 39], [63, 61, 62, 60, 0, 2, 1, 3, 127, 125, 126, 124, 64, 66, 65, 67, 191, 189, 190, 188, 128, 130, 129, 131, 159, 157, 158, 156, 160, 162, 161, 163, 95, 93, 94, 92, 96, 98, 97, 99, 31, 29, 30, 28, 32, 34, 33, 35]] def GetPmtID(channel): if channel < MAX_BOX_CHANNEL: pmt = channel/CHANNEL_PER_PMT else: pmt = (channel-MAX_BOX_CHANNEL)/CHANNEL_PER_PMT return pmt def GetPixelRow(channel): pmt = GetPmtID(channel); pmt_row = pmt%PMT_ROWS; pix = channel%CHANNEL_PER_PMT pixel_row = pix%8 return 8*pmt_row + pixel_row def GetPixelColumn(channel): pmt = GetPmtID(channel) pmt_column = pmt/PMT_ROWS pix = channel%CHANNEL_PER_PMT pixel_column = pix/8 return 8*pmt_column + pixel_column def GetIDs( pixel, pmtrow, pmtcolumn ): moduleid = 0 devid = 0 if pixel >= 108*64: # North rows 0-11 if pmtrow < 12: moduleid = moduleid_SSP1 if pmtcolumn < 3: if pmtrow == 0: devid = 0; if pmtrow == 1: devid = 1; if pmtrow == 2: devid = 2; if pmtrow == 3: devid = 3; if pmtrow == 4: devid = 8; if pmtrow == 5: devid = 9; if pmtrow == 6: devid = 10; if pmtrow == 7: devid = 11; if pmtrow == 8: devid = 16; if pmtrow == 9: devid = 17; if pmtrow == 10: devid = 18; if pmtrow == 11: devid = 19; else: if pmtrow == 0: devid = 4; if pmtrow == 1: devid = 5; if pmtrow == 2: devid = 6; if pmtrow == 3: devid = 7; if pmtrow == 4: devid = 12; if pmtrow == 5: devid = 13; if pmtrow == 6: devid = 14; if pmtrow == 7: devid = 15; if pmtrow == 8: devid = 20; if pmtrow == 9: devid = 21; if pmtrow == 10: devid = 22; if pmtrow == 11: devid = 23; # North rows 12-17 else: moduleid = moduleid_SSP2 if pmtcolumn < 3: if pmtrow == 12: devid = 0; if pmtrow == 13: devid = 1; if pmtrow == 14: devid = 2; if pmtrow == 15: devid = 3; if pmtrow == 16: devid = 8; if pmtrow == 17: devid = 9; else: if pmtrow == 12: devid = 4; if pmtrow == 13: devid = 5; if pmtrow == 14: devid = 6; if pmtrow == 15: devid = 7; if pmtrow == 16: devid = 10; if pmtrow == 17: devid = 11; else: # South (lower) box if pmtrow < 4 or (pmtrow < 8 and pmtcolumn < 3): # South rows 0-7 fibers 12-19 moduleid = moduleid_SSP2 if pmtcolumn < 3: if pmtrow == 0: devid = 12; if pmtrow == 1: devid = 13; if pmtrow == 2: devid = 14; if pmtrow == 3: devid = 15; if pmtrow == 4: devid = 20; if pmtrow == 5: devid = 21; if pmtrow == 6: devid = 22; if pmtrow == 7: devid = 23; else: if pmtrow == 0: devid = 16; if pmtrow == 1: devid = 17; if pmtrow == 2: devid = 18; if pmtrow == 3: devid = 19; else: # South rows 4-17 moduleid = moduleid_SSP3 if pmtcolumn < 3: if pmtrow == 8: devid = 4; if pmtrow == 9: devid = 5; if pmtrow == 10: devid = 6; if pmtrow == 11: devid = 7; if pmtrow == 12: devid = 12; if pmtrow == 13: devid = 13; if pmtrow == 14: devid = 14; if pmtrow == 15: devid = 15; if pmtrow == 16: devid = 20; if pmtrow == 17: devid = 21; else: if pmtrow == 4: devid = 0; if pmtrow == 5: devid = 1; if pmtrow == 6: devid = 2; if pmtrow == 7: devid = 3; if pmtrow == 8: devid = 8; if pmtrow == 9: devid = 9; if pmtrow == 10: devid = 10; if pmtrow == 11: devid = 11; if pmtrow == 12: devid = 16; if pmtrow == 13: devid = 17; if pmtrow == 14: devid = 18; if pmtrow == 15: devid = 19; if pmtrow == 16: devid = 22; if pmtrow == 17: devid = 23; return (moduleid,devid) 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 = 'DIRC'") rf_rows = cur.fetchall() if len(rf_rows) != 0: print 'DIRC table was already created' sys.exit(-1) # 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 moduleid already in DB and set our values for our SSP modules based on that cur.execute("SELECT MAX(moduleid) FROM Module") row = cur.fetchone() moduleid_SSP1 = row[0]+1 moduleid_SSP2 = row[0]+2 moduleid_SSP3 = row[0]+3 print 'SSP module ids: %d, %d, and %d' % (moduleid_SSP1, moduleid_SSP2, moduleid_SSP3) # Get runrangeid based on name. This should be created ahead of time via: # hd_tt_tool.py runrange create 50000 inf RunPeriod-2019-01-default runrange_name = 'RunPeriod-2019-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) # Add crates (crateid|name|area|rack|location|SN|Function|rocid|host|IP) cur.execute("INSERT INTO Crate Values (83, 'D2-8-BOT', 'D2', 8, 'BOT', '', 'SSP', 92, '', '129.57.153.92')") # Add modules for DIRC SSP (moduleid|crateid|slot|type|SN) cur.execute("INSERT INTO Module Values (%d, 83, 3, 'SSP', '')" % moduleid_SSP1) cur.execute("INSERT INTO Module Values (%d, 83, 4, 'SSP', '')" % moduleid_SSP2) cur.execute("INSERT INTO Module Values (%d, 83, 5, 'SSP', '')" % moduleid_SSP3) # make DIRC table cur.execute("CREATE TABLE DIRC(pixel INT, ssp_chanid INT, runrangeid INTEGER DEFAULT 1)") ################################################################### DIRC ################################################################### moduleid = 0 # SSP slot (moduleid_SSP1-moduleid_SSP3 as obtained above) devid = 0 # fiber number in SSP (0-31) channel_fpga = 0 # channel in FPGA (0-191) # loop over all channels and insert into both DIRC and CHANNEL tables max_ichanid = 108*64*2 for ichanid in range(0,max_ichanid): # chanid is unique integer in all of GlueX # moduleid must match SSP module index (1040-1042) # channel from 0-31 for DEVID (fiber) in SSP pixel = ichanid # Geant geometry and reco LUT index (0-13823) chanid = min_chanid + ichanid row = GetPixelRow(pixel) # 0-143 column = GetPixelColumn(pixel) # 0-47 # Get devid, moduleid, and channel to insert (based on offline map) pixelrow = row%8 # 0-7 within PMT pmtrow = row/8 # 0-17 pmtcolumn = column/8 # 0-5 channel_fpga = fullrows[pixelrow][column] # FPGA channel number moduleid, devid = GetIDs(pixel, pmtrow, pmtcolumn) channel = (devid<<8) + channel_fpga; f.write("%d, %d, %d, %d, %d, %d, %d\n" % (pixel, row, column, moduleid, devid, channel, chanid)) # Add channels for DIRC (chanid|moduleid|name|channel|system|col_name|enable) #print "insert chanid %d, moduleid %d, channel %d" % (chanid, moduleid, channel) cur.execute("INSERT INTO Channel Values (%d, %d, 'D2-8-BOT', %d, 'DIRC', 'ssp_chanid', 1, 1)" % (chanid, moduleid, channel)) # Add matching entry to DIRC table (pixel|ssp_chanid) #print "insert chanid %d, pixel %d" % (chanid, pixel) cur.execute("INSERT INTO DIRC VALUES (%d, %d, %d)" % (pixel, chanid, runrangeid)) # Add LED monitor channel from other crates (see Fernando's e-mails...) # Update channels for TAC (TOF-unused) (chanid|moduleid|name|channel|system|col_name|enable) #cur.execute("INSERT INTO Channel Values (41138, ????, 'DIRC-LED-MONITOR', 4, 'DIRC', 'disc_chanid?', 1)") print 'Done' f.close()