#!/usr/bin/python import sys,os from optparse import OptionParser from array import array #from __future__ import print_function valid_subdetectors = [ "BCAL", "CDC", "FCAL", "TOF" ] output_filename = "table.out" VERBOSE = False MAX_CHANNEL = 0 ###################################### 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 == "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() return vals[0] if __name__ == "__main__": parser = OptionParser(usage = "build_calib_table.py [options] subdetector input_file") 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) inverse_channel_map = dict((v,k) for k, v in channel_map.iteritems()) # read in new values and arrange them properly #new_table = ["" for x in range(int(MAX_CHANNEL)+1)] new_table = [] channel = 0 for line in input_file: value = GetEntry(line.strip(), subdetector) if VERBOSE: print "adding channel #"+str(channel)+" = " +str(value)+" to detector index "+str(inverse_channel_map[channel]) new_table.append(" ".join(map(str,inverse_channel_map[channel]))+" "+str(value)) channel += 1 input_file.close() # dump the table for the CCDB for entry in new_table: output_file.write(entry+"\n") output_file.close()