00001 import os,sys,imp
00002 import pickle
00003 import ConfigParser
00004
00005 def replaceTemplate(template,**opts):
00006 result = open(template).read()
00007 for item in opts:
00008 old = '@@%s@@'%item
00009 new = str(opts[item])
00010 print "Replacing",old,"to",new
00011 result = result.replace(old,new)
00012
00013 return result
00014
00015 def getDatasetStr(datasetpath):
00016 datasetstr = datasetpath
00017 datasetstr.strip()
00018 if datasetstr[0] == '/': datasetstr = datasetstr[1:]
00019 datasetstr = datasetstr.replace('/','_')
00020
00021 return datasetstr
00022
00023 def dqmWorkflowName(datasetpath,type,rev=1):
00024 workflowName = datasetpath
00025 sections = workflowName.split('/')[1:]
00026 workflowName = '/%s/%s-%s-rev%d/%s' % (sections[0],sections[1],type,rev,sections[2])
00027
00028 return workflowName
00029
00030 def listFilesInCastor(castor_dir,type = 'root',prefix = 'rfio:'):
00031 if not castor_dir: raise ValueError,'Please specify valid castor dir'
00032
00033 from subprocess import Popen,PIPE
00034 p1 = Popen(['nsls',castor_dir],stdout=PIPE)
00035
00036
00037
00038 files = [ "%s%s/%s" % (prefix,castor_dir,item.rstrip()) for item in p1.stdout if item.find(type) != -1 ]
00039 p1.stdout.close()
00040 return files
00041
00042 def listFilesLocal(dir,type = 'root'):
00043 if not dir: raise ValueError,'Please specify valid dir'
00044
00045
00046
00047
00048
00049
00050 files = os.listdir(dir)
00051 files = [ "%s/%s" % (dir,item) for item in files if item.find(type) != -1 ]
00052
00053 return files
00054
00055 def copyFilesFromCastor(castor_dir,output_dir,type='root'):
00056 from subprocess import call
00057 files = listFilesInCastor(castor_dir,type,'')
00058
00059 print "Copying files from %s to %s" % (castor_dir,output_dir)
00060 for item in files:
00061 cmd = ['rfcp',item,output_dir]
00062 print "..." + item
00063 retcode = call(cmd)
00064 if retcode != 0: raise RuntimeError,'Error in copying file %s to directory %s' % (item,output_dir)
00065
00066 return 0
00067
00068 def copyFilesLocal(dir,output_dir,type='root'):
00069 if not dir: raise ValueError,'Please specify valid dir'
00070 if not output_dir: raise ValueError,'Please specify valid output dir'
00071
00072 from subprocess import call
00073 files = listFilesLocal(dir,type)
00074 cmd = ['cp']
00075 cmd.extend(files)
00076 cmd.append(output_dir)
00077 print cmd
00078 retcode = call(cmd)
00079 return retcode
00080
00081 def haddInCastor(castor_dir,result_file,type = 'root',prefix = 'rfio:',suffix = None):
00082 if not castor_dir: raise ValueError,'Please specify valid castor dir'
00083 if not result_file: raise ValueError,'Please specify valid output file name'
00084
00085
00086
00087
00088 from subprocess import call
00089 files = listFilesInCastor(castor_dir,type,prefix)
00090 if suffix: files = [item + suffix for item in files]
00091
00092 cmd = ['hadd',result_file]
00093 cmd.extend(files)
00094
00095 retcode = call(cmd)
00096 return retcode
00097
00098 def haddLocal(dir,result_file,type = 'root'):
00099 if not dir: raise ValueError,'Please specify valid dir'
00100 if not result_file: raise ValueError,'Please specify valid output file name'
00101
00102 from subprocess import call
00103 files = listFilesLocal(dir,type)
00104 cmd = ['hadd',result_file]
00105 cmd.extend(files)
00106
00107 retcode = call(cmd)
00108 return retcode
00109
00110 def setGridEnv(cmssw_dir):
00111 cwd = os.getcwd()
00112 os.chdir(cmssw_dir)
00113
00114 os.system('source /afs/cern.ch/cms/LCG/LCG-2/UI/cms_ui_env.sh')
00115 os.system('cmsenv')
00116 os.system('source /afs/cern.ch/cms/ccs/wm/scripts/Crab/crab.sh')
00117
00118 os.chdir(cwd)
00119
00120 return
00121
00122 def parseInput(inputFields,requiredFields = ()):
00123
00124 class options: pass
00125 for item in sys.argv:
00126 option = item.split('=')[0]
00127 if option in inputFields:
00128 value = item.split('=')[1]
00129 if value in ('true','True','yes','Yes'): value = True
00130 elif value in ('false','False','no','No'): value = False
00131
00132 setattr(options,option,value)
00133
00134 for item in requiredFields:
00135 if not hasattr(options,item):
00136 raise RuntimeError,'Need to set "%s"' % item
00137
00138 return options
00139
00140 def loadCmsProcessFile(psetName):
00141 pset = imp.load_source("psetmodule",psetName)
00142 return pset.process
00143
00144 def loadCmsProcess(psetPath):
00145 module = __import__(psetPath)
00146 process = sys.modules[psetPath].process
00147
00148 import copy
00149
00150
00151 processNew = copy.copy(process)
00152 return processNew
00153
00154 def prependPaths(process,seqname):
00155 for path in process.paths:
00156 getattr(process,path)._seq = getattr(process,seqname)*getattr(process,path)._seq
00157
00158 def writeCfg(process,dir,psetName):
00159 if not os.path.exists(dir): os.makedirs(dir)
00160 open(dir + '/' + psetName,'w').write(process.dumpPython())
00161
00162 def writeCfgPkl(process,dir,psetName):
00163 if not os.path.exists(dir): os.makedirs(dir)
00164
00165 pklFileName = psetName.split('.')[0] + '.pkl'
00166 pklFile = open(dir + '/' + pklFileName,"wb")
00167 myPickle = pickle.Pickler(pklFile)
00168 myPickle.dump(process)
00169 pklFile.close()
00170
00171 outFile = open(dir + '/' + psetName,"w")
00172 outFile.write("import FWCore.ParameterSet.Config as cms\n")
00173 outFile.write("import pickle\n")
00174 outFile.write("process = pickle.load(open('%s', 'rb'))\n" % pklFileName)
00175 outFile.close()
00176
00177
00178 def loadCrabCfg(cfgName=None):
00179 config = ConfigParser.ConfigParser()
00180 if cfgName: config.read(cfgName)
00181 return config
00182
00183 def addCrabInputFile(crabCfg,inputFile):
00184 additionalInputFiles = ''
00185 if crabCfg.has_option('USER','additional_input_files'):
00186 additionalInputFiles = crabCfg.get('USER','additional_input_files')
00187
00188 if additionalInputFiles: additionalInputFiles += ',%s' % inputFile
00189 else: additionalInputFiles = inputFile
00190
00191 crabCfg.set('USER','additional_input_files',additionalInputFiles)
00192
00193 return crabCfg
00194
00195 def loadCrabDefault(crabCfg,config):
00196
00197 if not crabCfg.has_section('CRAB'): crabCfg.add_section('CRAB')
00198 crabCfg.set('CRAB','jobtype','cmssw')
00199
00200 if hasattr(config,'scheduler') and config.scheduler: crabCfg.set('CRAB','scheduler',config.scheduler)
00201 else: crabCfg.set('CRAB','scheduler','CAF')
00202
00203 if hasattr(config,'useserver') and config.useserver: crabCfg.set('CRAB','use_server',1)
00204
00205
00206 if not crabCfg.has_section('CMSSW'): crabCfg.add_section('CMSSW')
00207 if hasattr(config,'datasetpath') and config.datasetpath: crabCfg.set('CMSSW','datasetpath',config.datasetpath)
00208 else: crabCfg.set('CMSSW','datasetpath','/XXX/YYY/ZZZ')
00209 crabCfg.set('CMSSW','pset','pset.py')
00210
00211
00212 crabCfg.remove_option('CMSSW','total_number_of_events')
00213 crabCfg.remove_option('CMSSW','events_per_job')
00214 crabCfg.remove_option('CMSSW','number_of_jobs')
00215 crabCfg.remove_option('CMSSW','total_number_of_lumis')
00216 crabCfg.remove_option('CMSSW','lumis_per_job')
00217 crabCfg.remove_option('CMSSW','lumi_mask')
00218 crabCfg.remove_option('CMSSW','split_by_run')
00219
00220 """
00221 if hasattr(config,'totalnumberevents'): crabCfg.set('CMSSW','total_number_of_events',config.totalnumberevents)
00222 if hasattr(config,'eventsperjob'): crabCfg.set('CMSSW','events_per_job',config.eventsperjob)
00223 """
00224 if hasattr(config,'splitByLumi') and config.splitByLumi:
00225 crabCfg.set('CMSSW','total_number_of_lumis',config.totalnumberlumis)
00226 crabCfg.set('CMSSW','lumis_per_job',config.lumisperjob)
00227 if hasattr(config,'lumimask') and config.lumimask: crabCfg.set('CMSSW','lumi_mask',config.lumimask)
00228 elif hasattr(config,'splitByEvent') and config.splitByEvent:
00229 crabCfg.set('CMSSW','total_number_of_events',config.totalnumberevents)
00230 crabCfg.set('CMSSW','events_per_job',config.eventsperjob)
00231 else:
00232 crabCfg.set('CMSSW','split_by_run',1)
00233
00234 if hasattr(config,'splitByEvent') and config.splitByEvent:
00235 crabCfg.remove_option('CMSSW','runselection')
00236 else:
00237 if hasattr(config,'runselection') and config.runselection:
00238 crabCfg.set('CMSSW','runselection',config.runselection)
00239
00240
00241 if not crabCfg.has_section('USER'): crabCfg.add_section('USER')
00242
00243
00244 if hasattr(config,'stageOutCAF') and config.stageOutCAF:
00245 crabCfg.set('USER','return_data',0)
00246 crabCfg.set('USER','copy_data',1)
00247 crabCfg.set('USER','storage_element','T2_CH_CAF')
00248 crabCfg.set('USER','user_remote_dir',config.userdircaf)
00249 crabCfg.set('USER','check_user_remote_dir',0)
00250 elif hasattr(config,'stageOutLocal') and config.stageOutLocal:
00251 crabCfg.set('USER','return_data',1)
00252 crabCfg.set('USER','copy_data',0)
00253 crabCfg.remove_option('USER','storage_element')
00254 crabCfg.remove_option('USER','user_remote_dir')
00255 crabCfg.remove_option('USER','check_user_remote_dir')
00256
00257 if hasattr(config,'email') and config.email: crabCfg.set('USER','eMail',config.email)
00258 crabCfg.set('USER','xml_report','crabReport.xml')
00259
00260 if hasattr(config,'runOnGrid') and config.runOnGrid:
00261 crabCfg.remove_section('CAF')
00262 else:
00263 if not crabCfg.has_section('CAF'): crabCfg.add_section('CAF')
00264 crabCfg.set('CAF','queue',config.queueAtCAF)
00265
00266 return crabCfg