CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
tools.py
Go to the documentation of this file.
1 import os,sys,imp
2 import pickle
3 import ConfigParser
4 
5 def replaceTemplate(template,**opts):
6  result = open(template).read()
7  for item in opts:
8  old = '@@%s@@'%item
9  new = str(opts[item])
10  print "Replacing",old,"to",new
11  result = result.replace(old,new)
12 
13  return result
14 
15 def getDatasetStr(datasetpath):
16  datasetstr = datasetpath
17  datasetstr.strip()
18  if datasetstr[0] == '/': datasetstr = datasetstr[1:]
19  datasetstr = datasetstr.replace('/','_')
20 
21  return datasetstr
22 
23 def dqmWorkflowName(datasetpath,type,rev=1):
24  workflowName = datasetpath
25  sections = workflowName.split('/')[1:]
26  workflowName = '/%s/%s-%s-rev%d/%s' % (sections[0],sections[1],type,rev,sections[2])
27 
28  return workflowName
29 
30 def listFilesInCastor(castor_dir,type = 'root',prefix = 'rfio:'):
31  if not castor_dir: raise ValueError,'Please specify valid castor dir'
32 
33  from subprocess import Popen,PIPE
34  p1 = Popen(['nsls',castor_dir],stdout=PIPE)
35  #p2 = Popen(['grep',type],stdin=p1.stdout,stdout=PIPE)
36  #files = [prefix + castor_dir + "/" + item[:-1] for item in p2.stdout]
37  #p2.stdout.close()
38  files = [ "%s%s/%s" % (prefix,castor_dir,item.rstrip()) for item in p1.stdout if item.find(type) != -1 ]
39  p1.stdout.close()
40  return files
41 
42 def listFilesLocal(dir,type = 'root'):
43  if not dir: raise ValueError,'Please specify valid dir'
44 
45  #from subprocess import Popen,PIPE
46  #p1 = Popen(['ls',dir],stdout=PIPE)
47  #p2 = Popen(['grep',type],stdin=p1.stdout,stdout=PIPE)
48  #files = [dir + "/" + item[:-1] for item in p2.stdout]
49  #p2.stdout.close()
50  files = os.listdir(dir)
51  files = [ "%s/%s" % (dir,item) for item in files if item.find(type) != -1 ]
52 
53  return files
54 
55 def copyFilesFromCastor(castor_dir,output_dir,type='root'):
56  from subprocess import call
57  files = listFilesInCastor(castor_dir,type,'')
58 
59  print "Copying files from %s to %s" % (castor_dir,output_dir)
60  for item in files:
61  cmd = ['rfcp',item,output_dir]
62  print "..." + item
63  retcode = call(cmd)
64  if retcode != 0: raise RuntimeError,'Error in copying file %s to directory %s' % (item,output_dir)
65 
66  return 0
67 
68 def copyFilesLocal(dir,output_dir,type='root'):
69  if not dir: raise ValueError,'Please specify valid dir'
70  if not output_dir: raise ValueError,'Please specify valid output dir'
71 
72  from subprocess import call
73  files = listFilesLocal(dir,type)
74  cmd = ['cp']
75  cmd.extend(files)
76  cmd.append(output_dir)
77  print cmd
78  retcode = call(cmd)
79  return retcode
80 
81 def haddInCastor(castor_dir,result_file,type = 'root',prefix = 'rfio:',suffix = None):
82  if not castor_dir: raise ValueError,'Please specify valid castor dir'
83  if not result_file: raise ValueError,'Please specify valid output file name'
84 
85  #cmd = 'hadd %s `./listfilesCastor %s | grep %s`'%(result_file,castor_dir,type)
86  #print "Running",cmd
87  #os.system(cmd)
88  from subprocess import call
89  files = listFilesInCastor(castor_dir,type,prefix)
90  if suffix: files = [item + suffix for item in files]
91 
92  cmd = ['hadd',result_file]
93  cmd.extend(files)
94  #print cmd
95  retcode = call(cmd)
96  return retcode
97 
98 def haddLocal(dir,result_file,type = 'root'):
99  if not dir: raise ValueError,'Please specify valid dir'
100  if not result_file: raise ValueError,'Please specify valid output file name'
101 
102  from subprocess import call
103  files = listFilesLocal(dir,type)
104  cmd = ['hadd',result_file]
105  cmd.extend(files)
106  #print cmd
107  retcode = call(cmd)
108  return retcode
109 
110 def setGridEnv(cmssw_dir):
111  cwd = os.getcwd()
112  os.chdir(cmssw_dir)
113 
114  os.system('source /afs/cern.ch/cms/LCG/LCG-2/UI/cms_ui_env.sh')
115  os.system('cmsenv')
116  os.system('source /afs/cern.ch/cms/ccs/wm/scripts/Crab/crab.sh')
117 
118  os.chdir(cwd)
119 
120  return
121 
122 def parseInput(inputFields,requiredFields = ()):
123 
124  class options: pass
125  for item in sys.argv:
126  option = item.split('=')[0]
127  if option in inputFields:
128  value = item.split('=')[1]
129  if value in ('true','True','yes','Yes'): value = True
130  elif value in ('false','False','no','No'): value = False
131 
132  setattr(options,option,value)
133 
134  for item in requiredFields:
135  if not hasattr(options,item):
136  raise RuntimeError,'Need to set "%s"' % item
137 
138  return options
139 
140 def loadCmsProcessFile(psetName):
141  pset = imp.load_source("psetmodule",psetName)
142  return pset.process
143 
144 def loadCmsProcess(psetPath):
145  module = __import__(psetPath)
146  process = sys.modules[psetPath].process
147 
148  import copy
149  #FIXME: clone process
150  #processNew = copy.deepcopy(process)
151  processNew = copy.copy(process)
152  return processNew
153 
154 def prependPaths(process,seqname):
155  for path in process.paths:
156  getattr(process,path)._seq = getattr(process,seqname)*getattr(process,path)._seq
157 
158 def writeCfg(process,dir,psetName):
159  if not os.path.exists(dir): os.makedirs(dir)
160  open(dir + '/' + psetName,'w').write(process.dumpPython())
161 
162 def writeCfgPkl(process,dir,psetName):
163  if not os.path.exists(dir): os.makedirs(dir)
164 
165  pklFileName = psetName.split('.')[0] + '.pkl'
166  pklFile = open(dir + '/' + pklFileName,"wb")
167  myPickle = pickle.Pickler(pklFile)
168  myPickle.dump(process)
169  pklFile.close()
170 
171  outFile = open(dir + '/' + psetName,"w")
172  outFile.write("import FWCore.ParameterSet.Config as cms\n")
173  outFile.write("import pickle\n")
174  outFile.write("process = pickle.load(open('%s', 'rb'))\n" % pklFileName)
175  outFile.close()
176 
177 
178 def loadCrabCfg(cfgName=None):
179  config = ConfigParser.ConfigParser()
180  if cfgName: config.read(cfgName)
181  return config
182 
183 def addCrabInputFile(crabCfg,inputFile):
184  additionalInputFiles = ''
185  if crabCfg.has_option('USER','additional_input_files'):
186  additionalInputFiles = crabCfg.get('USER','additional_input_files')
187 
188  if additionalInputFiles: additionalInputFiles += ',%s' % inputFile
189  else: additionalInputFiles = inputFile
190 
191  crabCfg.set('USER','additional_input_files',additionalInputFiles)
192 
193  return crabCfg
194 
195 def loadCrabDefault(crabCfg,config):
196  # CRAB section
197  if not crabCfg.has_section('CRAB'): crabCfg.add_section('CRAB')
198  crabCfg.set('CRAB','jobtype','cmssw')
199 
200  if hasattr(config,'scheduler') and config.scheduler: crabCfg.set('CRAB','scheduler',config.scheduler)
201  else: crabCfg.set('CRAB','scheduler','CAF')
202 
203  if hasattr(config,'useserver') and config.useserver: crabCfg.set('CRAB','use_server',1)
204 
205  # CMSSW section
206  if not crabCfg.has_section('CMSSW'): crabCfg.add_section('CMSSW')
207  if hasattr(config,'datasetpath') and config.datasetpath: crabCfg.set('CMSSW','datasetpath',config.datasetpath)
208  else: crabCfg.set('CMSSW','datasetpath','/XXX/YYY/ZZZ')
209  crabCfg.set('CMSSW','pset','pset.py')
210 
211  # Splitting config
212  crabCfg.remove_option('CMSSW','total_number_of_events')
213  crabCfg.remove_option('CMSSW','events_per_job')
214  crabCfg.remove_option('CMSSW','number_of_jobs')
215  crabCfg.remove_option('CMSSW','total_number_of_lumis')
216  crabCfg.remove_option('CMSSW','lumis_per_job')
217  crabCfg.remove_option('CMSSW','lumi_mask')
218  crabCfg.remove_option('CMSSW','split_by_run')
219 
220  """
221  if hasattr(config,'totalnumberevents'): crabCfg.set('CMSSW','total_number_of_events',config.totalnumberevents)
222  if hasattr(config,'eventsperjob'): crabCfg.set('CMSSW','events_per_job',config.eventsperjob)
223  """
224  if hasattr(config,'splitByLumi') and config.splitByLumi:
225  crabCfg.set('CMSSW','total_number_of_lumis',config.totalnumberlumis)
226  crabCfg.set('CMSSW','lumis_per_job',config.lumisperjob)
227  if hasattr(config,'lumimask') and config.lumimask: crabCfg.set('CMSSW','lumi_mask',config.lumimask)
228  elif hasattr(config,'splitByEvent') and config.splitByEvent:
229  crabCfg.set('CMSSW','total_number_of_events',config.totalnumberevents)
230  crabCfg.set('CMSSW','events_per_job',config.eventsperjob)
231  else:
232  crabCfg.set('CMSSW','split_by_run',1)
233 
234  if hasattr(config,'splitByEvent') and config.splitByEvent:
235  crabCfg.remove_option('CMSSW','runselection')
236  else:
237  if hasattr(config,'runselection') and config.runselection:
238  crabCfg.set('CMSSW','runselection',config.runselection)
239 
240  # USER section
241  if not crabCfg.has_section('USER'): crabCfg.add_section('USER')
242 
243  # Stageout config
244  if hasattr(config,'stageOutCAF') and config.stageOutCAF:
245  crabCfg.set('USER','return_data',0)
246  crabCfg.set('USER','copy_data',1)
247  crabCfg.set('USER','storage_element','T2_CH_CAF')
248  crabCfg.set('USER','user_remote_dir',config.userdircaf)
249  crabCfg.set('USER','check_user_remote_dir',0)
250  elif hasattr(config,'stageOutLocal') and config.stageOutLocal:
251  crabCfg.set('USER','return_data',1)
252  crabCfg.set('USER','copy_data',0)
253  crabCfg.remove_option('USER','storage_element')
254  crabCfg.remove_option('USER','user_remote_dir')
255  crabCfg.remove_option('USER','check_user_remote_dir')
256 
257  if hasattr(config,'email') and config.email: crabCfg.set('USER','eMail',config.email)
258  crabCfg.set('USER','xml_report','crabReport.xml')
259 
260  if hasattr(config,'runOnGrid') and config.runOnGrid:
261  crabCfg.remove_section('CAF')
262  if hasattr(config,'ce_black_list'):
263  if not crabCfg.has_section('GRID'): crabCfg.add_section('GRID')
264  crabCfg.set('GRID','ce_black_list', config.ce_black_list)
265  if hasattr(config,'ce_white_list'):
266  if not crabCfg.has_section('GRID'): crabCfg.add_section('GRID')
267  crabCfg.set('GRID','ce_white_list', config.ce_white_list)
268  else:
269  if not crabCfg.has_section('CAF'): crabCfg.add_section('CAF')
270  crabCfg.set('CAF','queue',config.queueAtCAF)
271 
272  return crabCfg
def parseInput
Definition: tools.py:122
def addCrabInputFile
Definition: tools.py:183
def listFilesInCastor
Definition: tools.py:30
def loadCmsProcessFile
Definition: tools.py:140
def getDatasetStr
Definition: tools.py:15
def loadCrabDefault
Definition: tools.py:195
def writeCfgPkl
Definition: tools.py:162
def listFilesLocal
Definition: tools.py:42
def loadCmsProcess
Definition: tools.py:144
def haddLocal
Definition: tools.py:98
def copyFilesFromCastor
Definition: tools.py:55
def replaceTemplate
Definition: tools.py:5
def haddInCastor
Definition: tools.py:81
def loadCrabCfg
Definition: tools.py:178
def setGridEnv
Definition: tools.py:110
def copyFilesLocal
Definition: tools.py:68
def prependPaths
Definition: tools.py:154
def writeCfg
Definition: tools.py:158
def dqmWorkflowName
Definition: tools.py:23