#!/usr/bin/python # # Sasha Somov, Sep 28, 2024 # # This script adds ECAL crates to the RunPeriod-2023-01-default.xml # # This run period has already been created by Justin, # so you need to run this script after # # ./hd_tt_tool.py runrange create 120000 inf RunPeriod-2023-01-default # ./tt_add_dirc_ref3.py tt.db # --- ./tt_add_ecal.py tt.db --- # # Use ./tt_db2xml.py tt.db to create the xml file # # upload_to_ccdb.py - upload translation table to the CCDB # # Dec 29, 2024 # # corrected connection of 6 ECAL channels in the D1-1-TOP crate # # Added two channels for the ECAL reference PMTs (ECAL_REF) # # import sqlite3 as lite import sys import os import re import datetime from array import array import numpy as np list_of_slots111 = [3,4,5,6,7,8,9,10,13,14,15,16,17,18] list_of_slots112 = [3,4,5,6,7,8,9,10,13,14,15,16,17,18] list_of_slots113 = [3,4,5,6,7,8,9,10,13,14,15,16,17,18] list_of_slots114 = [3,4,5,6,7,8,9,10,13,14,15,16,17,18,20] list_of_slots115 = [3,4,5,6,7,8,9,10,13,14,15,16,17,18] list_of_slots116 = [3,4,5,6,7,8,9,10,13,14,15,16,17,18] list_of_slots117 = [3,4,5,6,7,8,9,10,13,14,15,16,17,18,19,20] # 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-2023-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) # tt_input: 0 - crate, 1 - slot, 2 - ch, 3 - column, 4 - row tt_input = np.zeros((1600,5)) ind = 0 # # Open file with input values (crate, slot, channel, column, row). This file is created from the excel files in GlueX-doc-6224-v1 # with open('ecal/ecal_all.dat') as file: for line in file: print line words = line.split(); print words[1] crate = int(words[0]) tt_input[ind][0] = int(words[0]) tt_input[ind][1] = int(words[1]) tt_input[ind][2] = int(words[2]) tt_input[ind][3] = int(words[3]) tt_input[ind][4] = int(words[4]) print 'Read from file: %d %d %d %d %d ' % (tt_input[ind][0], tt_input[ind][1], tt_input[ind][2], tt_input[ind][3], tt_input[ind][4]) ind += 1 print '============== ECAL tables =========================' # Get the maximum number of crates in the DB cur.execute("SELECT MAX(crateid) FROM Crate") row = cur.fetchone() crateid_min = row[0]+1 print 'ECAL Crate ID: %d' % crateid_min crateid = crateid_min # 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 = 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 ECAL channel: %d' % chanid_min chanid = chanid_min list_of_crateid = array('d') # Create ECAL table cur.execute("CREATE TABLE ECAL(col INT, row INT, adc_chanid INT, runrangeid INTEGER DEFAULT 1)") print 'ECAL Table Created' # Create ECAL_REF table cur.execute("CREATE TABLE ECAL_REF(id INT, adc_chanid INT, runrangeid INTEGER DEFAULT 1)") print 'ECAL_REF Table Created' # Add 7 ECAL crates (crateid|name|area|rack|location|SN|Function|rocid|host|IP) cur.execute("INSERT INTO Crate Values (%d, 'D1-1-TOP', 'D1', 1, 'TOP', '', 'fADC250', 111, '', '129.57.135.111')" %crateid) list_of_crateid.append(crateid) crateid += 1 cur.execute("INSERT INTO Crate Values (%d, 'D1-1-MID', 'D1', 1, 'MID', '', 'fADC250', 112, '', '129.57.135.112')" %crateid) list_of_crateid.append(crateid) crateid += 1 cur.execute("INSERT INTO Crate Values (%d, 'D1-1-BOT', 'D1', 1, 'BOT', '', 'fADC250', 118, '', '129.57.135.118')" %crateid) list_of_crateid.append(crateid) crateid += 1 cur.execute("INSERT INTO Crate Values (%d, 'D1-2-TOP', 'D1', 2, 'TOP', '', 'fADC250', 114, '', '129.57.135.114')" %crateid) list_of_crateid.append(crateid) crateid += 1 cur.execute("INSERT INTO Crate Values (%d, 'D1-2-MID', 'D1', 2, 'MID', '', 'fADC250', 115, '', '129.57.135.115')" %crateid) list_of_crateid.append(crateid) crateid += 1 cur.execute("INSERT INTO Crate Values (%d, 'D1-2-BOT', 'D1', 2, 'BOT', '', 'fADC250', 116, '', '129.57.135.116')" %crateid) list_of_crateid.append(crateid) crateid += 1 cur.execute("INSERT INTO Crate Values (%d, 'D1-3-TOP', 'D1', 3, 'TOP', '', 'fADC250', 117, '', '129.57.135.117')" %crateid) list_of_crateid.append(crateid) # Get the maximum number of crates in the DB cur.execute("SELECT MAX(crateid) FROM Crate") row = cur.fetchone() crateid_max = row[0]+1 print 'Test: Crate ID which can be added after the last ECAL crate %d %d:' % (crateid_min, crateid_max) crate_number = 111 for crateid in list_of_crateid: print crateid cur.execute("INSERT INTO Module Values (%d, %d, 1, 'CPU', '')" % (moduleid, crateid)) moduleid += 1 cur.execute("INSERT INTO Module Values (%d, %d, 11, 'CTP', '')" % (moduleid, crateid)) moduleid += 1 cur.execute("INSERT INTO Module Values (%d, %d, 12, 'SD', '')" % (moduleid, crateid)) moduleid += 1 cur.execute("INSERT INTO Module Values (%d, %d, 21, 'TI', '')" % (moduleid, crateid)) moduleid += 1 print 'Crate number %d' %crate_number list_of_slots = list_of_slots111 if crate_number == 111: list_of_slots = list_of_slots111 elif crate_number == 112: list_of_slots = list_of_slots112 elif crate_number == 113: list_of_slots = list_of_slots113 elif crate_number == 114: list_of_slots = list_of_slots114 elif crate_number == 115: list_of_slots = list_of_slots115 elif crate_number == 116: list_of_slots = list_of_slots116 elif crate_number == 117: list_of_slots = list_of_slots117 else: print 'Wrong crate number "%s"!' % crate_number sys.exit(-1) for slot in list_of_slots: cur.execute("INSERT INTO Module Values (%d, %d, %d, 'fADC250', '')" % (moduleid, crateid, slot)) for ii in range(ind): if tt_input[ii][0] == crate_number: if tt_input[ii][1] == slot: print 'Nashel %d %d' % (slot, ii) col = tt_input[ii][3] row = tt_input[ii][4] adc_ch = tt_input[ii][2] name = "ECAL:%d:%d" % (col, row) print 'chanid=%d adc_ch=%d name=%s' %(chanid, adc_ch, name) # # Add channels for TPOL (chanid|moduleid|name|channel|system|col_name|enable|runrangeid) cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'ECAL', 'adc_chanid', 1, %d)" % (chanid, moduleid, name, adc_ch , runrangeid)) cur.execute("INSERT INTO ECAL VALUES (%d, %d, %d, %d)" %(col, row, chanid, runrangeid)) chanid += 1 if crate_number == 114 and slot == 20: print 'Add reference PMT %d' %(slot) for ref_channel in range(0,2): name = 'CCAL_REF_%d' % (ref_channel + 1) cur.execute("INSERT INTO Channel Values (%d, %d, '%s', %d, 'ECAL_REF', 'adc_chanid', 1, %d)" % (chanid, moduleid, name, ref_channel, runrangeid)) cur.execute("INSERT INTO ECAL_REF VALUES (%d, %d, %d)" % (ref_channel + 1, chanid, runrangeid)) chanid += 1 moduleid += 1 crate_number += 1 print 'Done'