#!/usr/bin/python ''' Created on Mar 2, 2014 @author: Hovanes Egiyan ''' from subsystem import subsystem as subsystem from caen import CaenCrate as CaenCrate, A1535SNBoard as A1535SNBoard def initCrates(): retDict = dict() # crateInst = mpod.crate( "svt", "hvsvt3.jlab.org" ) crateInst = CaenCrate( "D2", "8", "TOP", "D2-8-TOP", "d2-8-top", "500" ) crateInst.AddA1535SNBoard( 14, 1515 ) crateInst.rocid = 910 retDict["D2-8-TOP"] = crateInst crateInst = CaenCrate( "U1", "9", "TOP", "U1-9-TOP", "u1-9-top", "501" ) crateInst.AddA1535SNBoard( 15, 1515 ) crateInst.rocid = 911 retDict["U1-9-TOP"] = crateInst crateInst = CaenCrate( "T2", "8", "BOT", "T1-8-BOT", "t1-8-bot", "503" ) crateInst.AddA1535SNBoard( 8, 1515 ) crateInst.rocid = 912 retDict["T1-8-BOT"] = crateInst return retDict def openSQLFile( fileName="beam.sql" ): try : outFile = open( fileName, "w") except IOError as err: raise err sql_string = 'CREATE TABLE Crate(crateid INTEGER PRIMARY KEY, name TEXT, area TEXT, rack INT, location TEXT, SN TEXT, Function TEXT, rocid INT, host TEXT, IP TEXT);\n' outFile.write(sql_string) sql_string = 'CREATE TABLE Module(moduleid INTEGER PRIMARY KEY, crateid INTEGER, slot INT, type TEXT, SN TEXT);\n' outFile.write(sql_string) sql_string = 'CREATE TABLE Channel(chanid INTEGER PRIMARY KEY, moduleid INTEGER, name TEXT, channel INT, system TEXT, col_name TEXT, enable INTEGER DEFAULT 1);\n' outFile.write(sql_string) sql_string = 'CREATE TABLE Detector_Hierarchy( id INTEGER PRIMARY KEY, parent_id INTEGER, name TEXT, type TEXT, chanid INTEGER, mtime DATETIME DEFAULT CURRENT_TIMESTAMP);\n' outFile.write(sql_string) outFile.write("\n\n\n") return outFile if __name__ == '__main__': print "Opening file" outFile = openSQLFile( "beam.sql" ) crates = initCrates() # print len(beamCrate.boardDict) print "Initializing detector" print "Making SQL" # Make the SVT detector entry detName = "BEAM" beam = subsystem( None, "Detector", detName ) beam.makeSQL(outFile) # Make TAC subdetector subDetName = "TAC" tac = subsystem( beam, "SubDetector", subDetName ) tac.makeSQL(outFile) # Make the TAC-HV subsystem entry beamTAC_HV = subsystem( tac, "Voltage type", "hv" ) beamTAC_HV.makeSQL(outFile) tacSlot = 14 tacChannel = 20 tacCrate = crates["D2-8-TOP"] channid = tacCrate.boardDict[tacSlot].channelMap[tacChannel].chanid tacCrate.boardDict[tacSlot].channelMap[tacChannel].system = detName tacCrate.boardDict[tacSlot].channelMap[tacChannel].name = "0" tacTAC_HV_chan = subsystem( beamTAC_HV, "HV Channel", "0", channid ) tacTAC_HV_chan.makeSQL(outFile) # Make LUMI subdetector subDetName = "LUMI" lumi = subsystem( beam, "SubDetector", subDetName ) lumi.makeSQL(outFile) # Make the LUMI-HV subsystem entry beamLUMI_HV = subsystem( lumi, "Voltage type", "hv" ) beamLUMI_HV.makeSQL(outFile) lumiSlot = 14 lumiChannel = 21 lumiCrate = crates["D2-8-TOP"] channid = lumiCrate.boardDict[lumiSlot].channelMap[lumiChannel].chanid lumiCrate.boardDict[lumiSlot].channelMap[lumiChannel].system = detName lumiCrate.boardDict[lumiSlot].channelMap[lumiChannel].name = "0" beamLUMI_HV_chan = subsystem( beamLUMI_HV, "HV Channel", "0", channid ) beamLUMI_HV_chan.makeSQL(outFile) # Make HALO subdetector ''' First describe the channel mapping using dictonaries Dictionary of dictionaries where the key is the halo assembly location ''' haloSlotChannel = dict() ''' For each location define a map of lists where the key of the map is the location on the PMT in the assembly. The list for each location is the cratename-slot-channel triplet (0th-element = crate, 1st-element = slot#, 2nd element = channel#). ''' haloSlotChannel["g:tag"] = {"l" :["T1-8-BOT",8,17],"t" :["T1-8-BOT",8,18],"r" :["T1-8-BOT",8,19],"b" :["T1-8-BOT",8,20] } haloSlotChannel["e:tag"] = {"t" :["T1-8-BOT",8,21],"b" :["T1-8-BOT",8,22],"l" :["T1-8-BOT",8,23] } haloSlotChannel["g:tgt"] = {"l" :["U1-9-TOP",15,0],"t" :["U1-9-TOP",15,1],"r" :["U1-9-TOP",15,2],"b" :["U1-9-TOP",15,3] } haloSlotChannel["g:col"] = {"l" :["U1-9-TOP",15,4],"t" :["U1-9-TOP",15,5],"r" :["U1-9-TOP",15,6],"b" :["U1-9-TOP",15,7] } # create the subsystem for HALO subdetector subDetName = "HALO" halo = subsystem( beam, "SubDetector", subDetName ) halo.makeSQL(outFile) # Make the HALO-HV subsystem entry beamHALO_HV = subsystem( halo, "Voltage type", "hv" ) beamHALO_HV.makeSQL(outFile) for assemblyLocation in haloSlotChannel.keys(): haloLocSys = subsystem( beamHALO_HV, "PMT Assembly", assemblyLocation ) haloLocSys.makeSQL(outFile) for pmtLocation in haloSlotChannel[assemblyLocation].keys() : # haloPMTSys = subsystem( haloLocSys, "Halo PMT", subDetName ) haloCrate = crates[haloSlotChannel[assemblyLocation][pmtLocation][0]] haloSlot = haloSlotChannel[assemblyLocation][pmtLocation][1] haloChannel = haloSlotChannel[assemblyLocation][pmtLocation][2] channid = haloCrate.boardDict[haloSlot].channelMap[haloChannel].chanid haloCrate.boardDict[haloSlot].channelMap[haloChannel].system = detName haloCrate.boardDict[haloSlot].channelMap[haloChannel].name = pmtLocation haloPMT_chan = subsystem( haloLocSys, "HV Channel", pmtLocation, channid ) haloPMT_chan.makeSQL(outFile) # Make SQL entries for the crates themselves for crateName in crates.keys(): crates[crateName].makeSQL(outFile)