#!/usr/bin/env python2 """This is a very crude script for generating control samples. The only real redeeming feature of this script is that it should create control samples with the right file names. """ import optparse import sys import ConfigParser import random import sha import time import base64 import os # Little method to generate hashs... def generateHash(intext,hashSize): """Generate hash""" intext += str(time.time()) chars = list('abcdefghijklmnopqrstuvwxyz0123456789') hash = "" sh=sha.new(intext) longhash = base64.b32encode(sh.digest()) hash = longhash[0:int(hashSize)] hash = hash.lower() return hash # Use optparse to define commmand line options. parser = optparse.OptionParser() parser.add_option("-c","--configfile",action="store",dest="configfile", type="string",default = "comet.cfg", help="Specify the name of the cards file for the comet job") (options,args) = parser.parse_args() ################################################################# # Start the real Script. # Read the config file (using ConfigParser) print "Reading Configuration" parser = ConfigParser.ConfigParser() parser.read(options.configfile) ################################################################# # Let's parse the list of input files. # Figure out what is the minimum and maximum run-subrun pair; # Also figure out if the input is spill or cosmic, the 'detector' field, min_run = 99999999 min_subrun = 9999 max_run = -1 max_subrun = -1 files = None is_beam = True overall = "" trigger = "" detector = "" if parser.has_option("configuration","inputfiles"): filelist = parser.get("configuration","inputfiles") files = filelist.split(" ") for inputfile in files: print inputfile fields = inputfile.split('/')[len(inputfile.split('/'))-1].split("_") if len(fields)>= 7: # We have a potential input file. # Grab the run and subrun number runnumber = fields[3] (run,subrun) = runnumber.split("-") print "Run number = " + runnumber + " " + run + " " + subrun if int(run) > int(max_run): max_run = run if run == max_run and int(subrun) > int(max_subrun): max_subrun = subrun if int(run) < int(min_run): min_run = run if run == min_run and int(subrun) < int(min_subrun): min_subrun = subrun # Check if it is cosmic (data or MC) if fields[2] == "cos" or (fields[1] == "cs" and fields[1] == "mu"): is_beam = False overall = fields[0] detector = fields[1] trigger = fields[2] else: print " No input files specified. Set the option [configuration] / inputfiles" sys.exit() print "Minimum run/subrun pair: " + str(min_run) + "-" + str(min_subrun) print "Maximum run/subrun pair: " + str(max_run) + "-" + str(max_subrun) if is_beam: print "This is spill input." else: print "This is cosmic input." ################################################################# # Let's build the command to execute jobhash = generateHash("overall job hash",4) command = "CreateControlSample.exe " # Figure out if we are storing reco data or raw data ('mdas'); # default is to store reco info. stage = "mdas" if parser.has_option("controlSample","strip_cali_reco"): if not parser.getboolean("controlSample","strip_cali_reco"): stage = "reco" command += " -k " else: stage = "reco" command += " -k " if parser.has_option("controlSample","num_events"): command += " -n " + parser.get("controlSample","num_events") comment = "controlsample" if(parser.has_option("filenaming","comment")): comment = parser.get("filenaming","comment") # Loop over different samples; Figure out filename for each sample. samples = [] if is_beam: samples = ["bsksmu","tpcsmu","fgdsmu","p0dsmu","empspl","presca","dsecel","brecel"] else : samples = ["fgdstp","fgdcol","presca"] for sample in samples: filename = overall + "_" + detector + "_" + "ctl" + "-" + trigger filename += "-" + sample filename += "_" + str(min_run) + "-" + str(min_subrun) + "-" + str(max_run) + "-" + str(max_subrun) filename += "_" + jobhash + generateHash(sample,8) # job hash + file hash filename += "_" + stage filename += "_000" filename += "_" + comment filename += ".root " command += " -O " + sample + "=" + filename command += parser.get("configuration","inputfiles") # Finally, execute command print "Executing command = " + command os.system(command)