#!/usr/bin/python # # Sasha Somov, October 7, 2025 # # This script modifies FCAL channels after installing the ECAL (in 2024) # The changes took place for the new run period RunPeriod-2025-01-default # # see make_RunPeriod-2025-01.sh (the new run period is 38) # # # import sqlite3 as lite import sys import os import re import datetime from array import array import numpy as np # list_of_slots = [3] list_of_slots = [3,4,5,6,7,8,9,10,13,14,15,16,17,18,19] # 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() 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) # # Read imput values for rocid = 12 and rocid = 21 # # tt_input: 0 - crate, 1 - slot, 2 - ch, 3 - column, 4 - row tt_input_12 = np.zeros((1600,5)) ind_12 = 0 with open('fcal/D2-2-MID.txt') as file: for line in file: # print line words = line.split(); # print words[1] crate = int(words[0]) tt_input_12[ind_12][0] = int(words[0]) tt_input_12[ind_12][1] = int(words[1]) tt_input_12[ind_12][2] = int(words[2]) tt_input_12[ind_12][3] = int(words[3]) tt_input_12[ind_12][4] = int(words[4]) # print 'Sasha %d %d %d %d %d ' % (tt_input_12[ind_12][0], tt_input_12[ind_12][1], tt_input_12[ind_12][2], tt_input_12[ind_12][3], tt_input_12[ind_12][4]) ind_12 += 1 # tt_input: 0 - crate, 1 - slot, 2 - ch, 3 - column, 4 - row tt_input_21 = np.zeros((1600,5)) ind_21 = 0 with open('fcal/D2-7-MID.txt') as file: for line in file: # print line words = line.split(); # print words[1] crate = int(words[0]) tt_input_21[ind_21][0] = int(words[0]) tt_input_21[ind_21][1] = int(words[1]) tt_input_21[ind_21][2] = int(words[2]) tt_input_21[ind_21][3] = int(words[3]) tt_input_21[ind_21][4] = int(words[4]) # print 'Sasha %d %d %d %d %d ' % (tt_input_21[ind_21][0], tt_input_21[ind_21][1], tt_input_21[ind_21][2], tt_input_21[ind_21][3], tt_input_21[ind_21][4]) ind_21 += 1 # Get the maximum number of modules in the DB cur.execute("SELECT MAX(moduleid) FROM Module") row = cur.fetchone() moduleid_min = row[0]+1 print 'ID of the first ECAL module: %d' % moduleid_min moduleid_new = moduleid_min # Get the maximum number of channels in the DB cur.execute("SELECT MAX(chanid) FROM Channel") row = cur.fetchone() chanid_min = row[0]+1 # print 'chanid_min = ' + str(chanid_min) print 'ID of the first FCAL channel: %d' % chanid_min chanid_new = chanid_min runrangeid_old = 37 # # Step 1 # Clear entries for rocids 12, 15, 18, and 21 # nn = 0 # Loop over slots for slot in list_of_slots: cur.execute("SELECT moduleid from Module,Crate where (rocid==12 or rocid==15 or rocid==18 or rocid==21 ) and slot=%d and module.crateid==crate.crateid " %slot) # cur.execute("SELECT moduleid from Module,Crate where (rocid==12 ) and slot==%d and module.crateid==crate.crateid " %slot) rows = cur.fetchall() modid_tac = 0 for row in rows: modid = int(row[0]) print 'Module ID %d' %modid nn = nn + 1 # if slot == 3: # modid_tac = modid # print 'TAC MODULE ID = %d' %modid_tac cur.execute("SELECT * from Channel where moduleid==%d and channel.moduleid==%d and runrangeid=%d" %(modid, modid, runrangeid_old)) rows = cur.fetchall() for row in rows: chanid_old = int(row['chanid']) name = row['name'] enable = row['enable'] runrange = row['runrangeid'] chan = row['channel'] system = row['system'] print 'chanid = %d name = %s enable = %d slot = %d system %s runrange = %d' % (chanid_old, name, enable, slot, system, runrange) cur.execute("INSERT INTO Channel Values (%d, %d, 'FCAL-unused',%d, '', '', 0, %d)" %(chanid_old, modid, chan, runrangeid)) # Have to disable TAC in Ch 0, Slot 3, rocid 12 (otherwise the channel shows up twice in the XML generation modid_tac = 806 cur.execute("INSERT INTO Channel Values (24342, %d, 'TAC-unused',0, '', '', 0, %d)" %(modid_tac, runrangeid)) nn = nn + 1 print 'Number of modules removed = %d' % nn #---------------------------------------------------------------------------------------------------- # # ROCID 12 (D1-2-MID) # Add channels # #---------------------------------------------------------------------------------------------------- rocid = 12 cur.execute("SELECT name,crateid from Crate where rocid=12") row = cur.fetchone() cratename = row['name'] crateid = row['crateid'] print 'Rocid 12: Crate ID: %d' % crateid for slot in list_of_slots: cur.execute("SELECT moduleid from Module,Crate where rocid==%d and slot==%d and module.crateid==crate.crateid " %(rocid,slot)) rows = cur.fetchall() for row in rows: modid = int(row[0]) print 'Module ID %d Slot = %d' %(modid, slot) for ii in range(ind_12): if tt_input_12[ii][0] == rocid: if tt_input_12[ii][1] == slot: # print ' Found: %d %d' % (slot, ii) col = tt_input_12[ii][3] row = tt_input_12[ii][4] adc_ch = tt_input_12[ii][2] name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 # Add channels for TPOL (chanid|moduleid|name|channel|system|col_name|enable|runrangeid) cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, modid, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 print ' Inserted channels into ROCID 12: chanid_new = %d' % chanid_new #---------------------------------------------------------------------------------------------------- # # Add new module in Slot 20, ROCID 12 # #---------------------------------------------------------------------------------------------------- new_slot = 20 cur.execute("INSERT INTO Module Values (%d, %d, %d, 'fADC250', '')" % (moduleid_new, crateid, new_slot)) for ii in range(ind_12): if tt_input_12[ii][0] == rocid: if tt_input_12[ii][1] == new_slot: # print 'Fount %d %d' % (slot, ii) col = tt_input_12[ii][3] row = tt_input_12[ii][4] adc_ch = tt_input_12[ii][2] name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 # Add channels for TPOL (chanid|moduleid|name|channel|system|col_name|enable|runrangeid) cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, moduleid_new, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 print 'Inserted channels into Slot 20 of ROCID 12: chanid_new = %d' % chanid_new moduleid_new = moduleid_new + 1 #---------------------------------------------------------------------------------------------------- # # ROCID 21 (D2-7-MID) # Add channels # #---------------------------------------------------------------------------------------------------- rocid = 21 cur.execute("SELECT name,crateid from Crate where rocid=21") row = cur.fetchone() cratename = row['name'] crateid = row['crateid'] print 'Rocid 21: Crate ID: %d' % crateid for slot in list_of_slots: cur.execute("SELECT moduleid from Module,Crate where rocid==%d and slot==%d and module.crateid==crate.crateid " %(rocid,slot)) rows = cur.fetchall() for row in rows: modid = int(row[0]) print 'Module ID %d Slot = %d' %(modid, slot) for ii in range(ind_21): if tt_input_21[ii][0] == rocid: if tt_input_21[ii][1] == slot: # print ' Found %d %d' % (slot, ii) col = tt_input_21[ii][3] row = tt_input_21[ii][4] adc_ch = tt_input_21[ii][2] name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 # Add channels for TPOL (chanid|moduleid|name|channel|system|col_name|enable|runrangeid) cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, modid, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 print 'Inserted channels into ROCID 21: chanid_new = %d' % chanid_new #---------------------------------------------------------------------------------------------------- # # Add new module in Slot 20, ROCID 21 # #---------------------------------------------------------------------------------------------------- new_slot = 20 cur.execute("INSERT INTO Module Values (%d, %d, %d, 'fADC250', '')" % (moduleid_new, crateid, new_slot)) for ii in range(ind_21): if tt_input_21[ii][0] == rocid: if tt_input_21[ii][1] == new_slot: # print 'Found %d %d' % (slot, ii) col = tt_input_21[ii][3] row = tt_input_21[ii][4] adc_ch = tt_input_21[ii][2] name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 # Add channels for TPOL (chanid|moduleid|name|channel|system|col_name|enable|runrangeid) cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, moduleid_new, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 print 'Inserted channels into Slot 20 of ROCID 21: chanid_new = %d' % chanid_new moduleid_new = moduleid_new + 1 #---------------------------------------------------------------------------------------------------- # # Install a new flash ADC in Slot 10 of the rocfcal4 (ROCID 14, D2-3-TOP). Connnect 4 channels # #---------------------------------------------------------------------------------------------------- rocid = 14 cur.execute("SELECT name,crateid from Crate where rocid=14") row = cur.fetchone() cratename = row['name'] crateid = row['crateid'] print 'Rocid 14: Crate ID: %d' % crateid new_slot = 19 cur.execute("INSERT INTO Module Values (%d, %d, %d, 'fADC250', '')" % (moduleid_new, crateid, new_slot)) col = 9 row = -11 adc_ch = 0 name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, moduleid_new, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 col = 10 row = -11 adc_ch = 1 name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, moduleid_new, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 col = 10 row = 12 adc_ch = 14 name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, moduleid_new, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 col = 11 row = 12 adc_ch = 15 name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, moduleid_new, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 moduleid_new = moduleid_new + 1 #---------------------------------------------------------------------------------------------------- # # Install a new flash ADC in Slot 10 of the rocfcal7 (ROCID 17, D2-6-TOP). Connnect 4 channels # #---------------------------------------------------------------------------------------------------- rocid = 17 cur.execute("SELECT name,crateid from Crate where rocid=17") row = cur.fetchone() cratename = row['name'] crateid = row['crateid'] print 'Rocid 17: Crate ID: %d' % crateid new_slot = 19 cur.execute("INSERT INTO Module Values (%d, %d, %d, 'fADC250', '')" % (moduleid_new, crateid, new_slot)) col = -11 row = -4 adc_ch = 0 name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, moduleid_new, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 col = -11 row = -3 adc_ch = 1 name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, moduleid_new, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 col = -12 row = 8 adc_ch = 14 name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, moduleid_new, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 col = -11 row = 8 adc_ch = 15 name = "FCAL:%d:%d" % (col, row) col_xml = col + 29 row_xml = row + 29 cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'FCAL', 'adc_chanid', 1, %d)" % (chanid_new, moduleid_new, name, adc_ch , runrangeid)) cur.execute("INSERT INTO FCAL VALUES (%d, %d, %d, %d)" %(row_xml, col_xml, chanid_new, runrangeid)) chanid_new += 1 moduleid_new = moduleid_new + 1 #---------------------------------------------------------------------------------------------------- # # Remove modules which are not used in the FCAL2 anymore (8 channels in total) # #---------------------------------------------------------------------------------------------------- cur.execute("SELECT * from FCAL where row==2 and col==16 and runrangeid==27") rows = cur.fetchall() for row in rows: irow = row['row'] icol = row['col'] adc_chanid = row['adc_chanid'] print 'IROW %d %d %d' % (irow, icol, adc_chanid) cur.execute("SELECT * from Channel where chanid=%d and runrangeid==27" %(adc_chanid)) rows = cur.fetchall() for row in rows: chanid_old = int(row['chanid']) name = row['name'] enable = row['enable'] runrange = row['runrangeid'] chan = row['channel'] system = row['system'] mod_id = row['moduleid'] print 'IROW 1 = %d %d %d %s %d' % (mod_id, chan,adc_chanid, name, enable) cur.execute("INSERT INTO Channel Values (%d, %d, 'FCAL-unused',%d, '', '', 0, %d)" %(adc_chanid, mod_id, chan, runrangeid)) #---------------------------------------------------------------------------------------------------- cur.execute("SELECT * from FCAL where row==2 and col==42 and runrangeid==27") rows = cur.fetchall() for row in rows: adc_chanid = row['adc_chanid'] cur.execute("SELECT * from Channel where chanid=%d and runrangeid==27" %(adc_chanid)) rows = cur.fetchall() for row in rows: chan = row['channel'] mod_id = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, 'FCAL-unused',%d, '', '', 0, %d)" %(adc_chanid, mod_id, chan, runrangeid)) #---------------------------------------------------------------------------------------------------- cur.execute("SELECT * from FCAL where row==16 and col==2 and runrangeid==27") rows = cur.fetchall() for row in rows: adc_chanid = row['adc_chanid'] cur.execute("SELECT * from Channel where chanid=%d and runrangeid==27" %(adc_chanid)) rows = cur.fetchall() for row in rows: chan = row['channel'] mod_id = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, 'FCAL-unused',%d, '', '', 0, %d)" %(adc_chanid, mod_id, chan, runrangeid)) #---------------------------------------------------------------------------------------------------- cur.execute("SELECT * from FCAL where row==16 and col==56 and runrangeid==27") rows = cur.fetchall() for row in rows: adc_chanid = row['adc_chanid'] cur.execute("SELECT * from Channel where chanid=%d and runrangeid==27" %(adc_chanid)) rows = cur.fetchall() for row in rows: chan = row['channel'] mod_id = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, 'FCAL-unused',%d, '', '', 0, %d)" %(adc_chanid, mod_id, chan, runrangeid)) #---------------------------------------------------------------------------------------------------- cur.execute("SELECT * from FCAL where row==42 and col==2 and runrangeid==27") rows = cur.fetchall() for row in rows: adc_chanid = row['adc_chanid'] cur.execute("SELECT * from Channel where chanid=%d and runrangeid==27" %(adc_chanid)) rows = cur.fetchall() for row in rows: chan = row['channel'] mod_id = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, 'FCAL-unused',%d, '', '', 0, %d)" %(adc_chanid, mod_id, chan, runrangeid)) #---------------------------------------------------------------------------------------------------- cur.execute("SELECT * from FCAL where row==42 and col==56 and runrangeid==27") rows = cur.fetchall() for row in rows: adc_chanid = row['adc_chanid'] cur.execute("SELECT * from Channel where chanid=%d and runrangeid==27" %(adc_chanid)) rows = cur.fetchall() for row in rows: chan = row['channel'] mod_id = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, 'FCAL-unused',%d, '', '', 0, %d)" %(adc_chanid, mod_id, chan, runrangeid)) #---------------------------------------------------------------------------------------------------- cur.execute("SELECT * from FCAL where row==56 and col==16 and runrangeid==27") rows = cur.fetchall() for row in rows: adc_chanid = row['adc_chanid'] cur.execute("SELECT * from Channel where chanid=%d and runrangeid==27" %(adc_chanid)) rows = cur.fetchall() for row in rows: chan = row['channel'] mod_id = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, 'FCAL-unused',%d, '', '', 0, %d)" %(adc_chanid, mod_id, chan, runrangeid)) #---------------------------------------------------------------------------------------------------- cur.execute("SELECT * from FCAL where row==56 and col==42 and runrangeid==27") rows = cur.fetchall() for row in rows: adc_chanid = row['adc_chanid'] cur.execute("SELECT * from Channel where chanid=%d and runrangeid==27" %(adc_chanid)) rows = cur.fetchall() for row in rows: chan = row['channel'] mod_id = row['moduleid'] cur.execute("INSERT INTO Channel Values (%d, %d, 'FCAL-unused',%d, '', '', 0, %d)" %(adc_chanid, mod_id, chan, runrangeid)) print 'Done'