#!/usr/bin/python import sys,os from optparse import OptionParser from array import array #from __future__ import print_function valid_subdetectors = [ "BCAL", "BCAL2", "FCAL", "CDC", "TOF" ] output_filename = "table.out" VERBOSE = False MAX_CHANNEL = 0 subdetector_help_text = { "BCAL" : "module layer sector end", "BCAL2" : "module layer sector", "CDC" : "ring straw", "FCAL" : "row column", "TOF" : "plane bar end", } ###################################### def LoadChannelMap(channel_map, subdetector): global MAX_CHANNEL mapfile = open(subdetector+"ChannelMap") for line in mapfile: vals = line.strip().split() if(subdetector == "BCAL"): index = (int(vals[0]),int(vals[1]),int(vals[2]),int(vals[3])) channel = int(vals[4]) elif(subdetector == "BCAL2"): index = (int(vals[0]),int(vals[1]),int(vals[2])) channel = int(vals[3]) elif(subdetector == "CDC"): index = (int(vals[0]),int(vals[1])) channel = int(vals[2]) elif(subdetector == "FCAL"): index = (int(vals[0]),int(vals[1])) channel = int(vals[2]) elif(subdetector == "TOF"): index = (int(vals[0]),int(vals[1]),int(vals[2])) channel = int(vals[3]) channel_map[index] = channel if(VERBOSE): print "channel map["+str(index)+"] = " +str(channel) if(channel > MAX_CHANNEL): MAX_CHANNEL = channel mapfile.close() def GetEntry(line, subdetector): vals = line.strip().split() if(subdetector == "BCAL"): index = (int(vals[0]),int(vals[1]),int(vals[2]),int(vals[3])) #calib_val = float(vals[4]) calib_val = " ".join(vals[4:]) elif(subdetector == "BCAL2"): index = (int(vals[0]),int(vals[1]),int(vals[2])) #calib_val = float(vals[3]) calib_val = " ".join(vals[3:]) elif(subdetector == "CDC"): index = (int(vals[0]),int(vals[1])) #calib_val = float(vals[2]) calib_val = " ".join(vals[2:]) elif(subdetector == "FCAL"): index = (int(vals[0]),int(vals[1])) #calib_val = float(vals[2]) calib_val = " ".join(vals[2:]) elif(subdetector == "TOF"): index = (int(vals[0]),int(vals[1]),int(vals[2])) #calib_val = float(vals[3]) calib_val = " ".join(vals[3:]) return (index,calib_val) if __name__ == "__main__": # build the useful help string help_string = "build_calib_table.py [options] subdetector input_file" help_string = help_string + "\n\n" help_string = help_string + " supported detectors:\n" #for det in valid_subdetectors: # help_string for (key,value) in subdetector_help_text.items(): help_string = help_string + " " + key + ": " + value + "\n" #parser = OptionParser(usage = "build_calib_table.py [options] subdetector input_file") parser = OptionParser(usage = help_string) parser.add_option("-o","--output", dest="output_filename", help="Save the table with the output filename.") parser.add_option("-v","--version", action="store_true", dest="verbose", help="Enable verbose output.") ## handle commandline options (options, args) = parser.parse_args() if( (len(args) < 2) ): parser.print_help() exit(0) if( options.verbose ): VERBOSE = True subdetector = args[0] if subdetector not in valid_subdetectors: print "Invalid subdetector %s! Supported subdetectors are %s" % (subdetector, ", ".join(valid_subdetectors)) input_filename = args[1] input_file = open(input_filename, "r") if(options.output_filename is not None): output_filename = options.output_filename output_file = open(output_filename, "w") # initialize mapping channel_map = {} LoadChannelMap(channel_map, subdetector) # read in new values and arrange them properly #new_table = array('d', [0 for x in range(int(MAX_CHANNEL)+1)]) new_table = ["0" for x in range(int(MAX_CHANNEL)+1)] for line in input_file: (index, value) = GetEntry(line.strip(), subdetector) if VERBOSE: print "adding index ["+str(index)+"] = " +str(value)+" to channel "+str(channel_map[index]) new_table[channel_map[index]] = value input_file.close() # dump the table for the CCDB for value in new_table: output_file.write(str(value)+"\n") output_file.close()