#!/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 = CaenCrate( "D2", "8", "BOT", "D2-8-BOT", "halldcaenhv6", "600" ) crateInst.AddA1535SNBoard( 0, 1515 ) crateInst.AddA1535SNBoard( 2, 1515 ) crateInst.AddA1535SNBoard( 4, 1515 ) crateInst.AddA1535SNBoard( 6, 1515 ) crateInst.rocid = 910 retDict["D2-8-BOT"] = crateInst return retDict def openSQLFile( fileName="comCal.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( "comCal.sql" ) crates = initCrates() # print len(beamCrate.boardDict) print "Initializing detector" print "Making SQL" # Make the SVT detector entry detName = "COMCAL" comcal = subsystem( None, "Detector", detName ) comcal.makeSQL(outFile) # Make the raw 1 column 1 subsystem entry comCal_HV = subsystem( comcal, "Voltage type", "hv" ) comCal_HV.makeSQL(outFile) columnDict = dict(); pmtNumber = 0 comCalCrate = crates["D2-8-BOT"] for columnNumber in range(-5,6) : # Make raw subdetector columnSubDetName = str(columnNumber) columnDict[columnSubDetName] = subsystem( comCal_HV, "column", columnSubDetName ) if( abs(columnNumber) > 0.5 ) : columnDict[columnSubDetName].makeSQL(outFile) for rowNumber in range (-5,6): if( ( abs(columnNumber) > 1 or abs(rowNumber) > 1 ) and ( abs(columnNumber) > 0.5 and abs(rowNumber) > 0.5 ) ) : # Make column subdetector rowSubDetName = str(rowNumber) slotNumber = 2 * int(pmtNumber / 24) channelNumber = pmtNumber % 24 print "slot is {0} , channel is {1} ".format( slotNumber, channelNumber ) channid = comCalCrate.boardDict[slotNumber].channelMap[channelNumber].chanid comCalCrate.boardDict[slotNumber].channelMap[channelNumber].system = detName comCalCrate.boardDict[slotNumber].channelMap[channelNumber].name = str(columnNumber) + ":" + str(rowNumber) rowCol_HV_chan = subsystem( columnDict[columnSubDetName], "HV Channel", rowSubDetName, channid ) rowCol_HV_chan.makeSQL(outFile) pmtNumber += 1 # Make SQL entries for the crates themselves for crateName in crates.keys(): crates[crateName].makeSQL(outFile)