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 ConfigParser
3 
4 def replaceTemplate(template,**opts):
5  result = open(template).read()
6  for item in opts:
7  old = '@@%s@@'%item
8  new = str(opts[item])
9  print "Replacing",old,"to",new
10  result = result.replace(old,new)
11 
12  return result
13 
14 def listFilesInCastor(castor_dir,type = 'root',prefix = 'rfio:'):
15  if not castor_dir: raise ValueError,'Please specify valid castor dir'
16 
17  from subprocess import Popen,PIPE
18  p1 = Popen(['nsls',castor_dir],stdout=PIPE)
19  p2 = Popen(['grep',type],stdin=p1.stdout,stdout=PIPE)
20  files = [prefix + castor_dir + "/" + item[:-1] for item in p2.stdout]
21  p2.stdout.close()
22  return files
23 
24 def listFilesLocal(dir,type = 'root'):
25  if not dir: raise ValueError,'Please specify valid dir'
26 
27  from subprocess import Popen,PIPE
28  p1 = Popen(['ls',dir],stdout=PIPE)
29  p2 = Popen(['grep',type],stdin=p1.stdout,stdout=PIPE)
30  files = [dir + "/" + item[:-1] for item in p2.stdout]
31  p2.stdout.close()
32  return files
33 
34 def haddInCastor(castor_dir,result_file,type = 'root',prefix = 'rfio:',suffix = None):
35  if not castor_dir: raise ValueError,'Please specify valid castor dir'
36  if not result_file: raise ValueError,'Please specify valid output file name'
37 
38  #cmd = 'hadd %s `./listfilesCastor %s | grep %s`'%(result_file,castor_dir,type)
39  #print "Running",cmd
40  #os.system(cmd)
41  from subprocess import call
42  files = listFilesInCastor(castor_dir,type,prefix)
43  if suffix: files = [item + suffix for item in files]
44 
45  cmd = ['hadd',result_file]
46  cmd.extend(files)
47  #print cmd
48  retcode = call(cmd)
49  return retcode
50 
51 def haddLocal(dir,result_file,type = 'root'):
52  if not dir: raise ValueError,'Please specify valid dir'
53  if not result_file: raise ValueError,'Please specify valid output file name'
54 
55  from subprocess import call
56  files = listFilesLocal(dir,type)
57  cmd = ['hadd',result_file]
58  cmd.extend(files)
59  #print cmd
60  retcode = call(cmd)
61  return retcode
62 
63 def setGridEnv(cmssw_dir):
64  cwd = os.getcwd()
65  os.chdir(cmssw_dir)
66 
67  os.system('source /afs/cern.ch/cms/LCG/LCG-2/UI/cms_ui_env.sh')
68  os.system('cmsenv')
69  os.system('source /afs/cern.ch/cms/ccs/wm/scripts/Crab/crab.sh')
70 
71  os.chdir(cwd)
72 
73  return
74 
75 def parseInput(inputFields,requiredFields = ()):
76 
77  class options: pass
78  for item in sys.argv:
79  option = item.split('=')[0]
80  if option in inputFields:
81  value = item.split('=')[1]
82  if value in ('true','True','yes','Yes'): value = True
83  elif value in ('false','False','no','No'): value = False
84 
85  setattr(options,option,value)
86 
87  for item in requiredFields:
88  if not hasattr(options,item):
89  raise RuntimeError,'Need to set "%s"' % item
90 
91  return options
92 
93 def loadCmsProcess(pset_name):
94  pset = imp.load_source("psetmodule",pset_name)
95  return pset.process
96 
97 def prependPaths(process,seqname):
98  for path in process.paths:
99  getattr(process,path)._seq = getattr(process,seqname)*getattr(process,path)._seq
100 
101 def writeCfg(process,dir,pset_name):
102  if not os.path.exists(dir): os.makedirs(dir)
103  open(dir + '/' + pset_name,'w').write(process.dumpPython())
104 
105 def loadCrabCfg(cfg_name):
106  config = ConfigParser.ConfigParser()
107  config.read(cfg_name)
108  return config
109 
110 def loadCrabDefault(crab_cfg,config):
111  # CRAB section
112  if not crab_cfg.has_section('CRAB'): crab_cfg.add_section('CRAB')
113  crab_cfg.set('CRAB','jobtype','cmssw')
114  crab_cfg.set('CRAB','scheduler',config.scheduler)
115  if config.useserver: crab_cfg.set('CRAB','use_server',1)
116 
117  # CMSSW section
118  if not crab_cfg.has_section('CMSSW'): crab_cfg.add_section('CMSSW')
119  crab_cfg.set('CMSSW','datasetpath',config.datasetpath)
120  crab_cfg.set('CMSSW','pset','pset.py')
121 
122  # Splitting config
123  crab_cfg.remove_option('CMSSW','total_number_of_events')
124  crab_cfg.remove_option('CMSSW','events_per_job')
125  crab_cfg.remove_option('CMSSW','number_of_jobs')
126  crab_cfg.remove_option('CMSSW','total_number_of_lumis')
127  crab_cfg.remove_option('CMSSW','lumis_per_job')
128  crab_cfg.remove_option('CMSSW','lumi_mask')
129  crab_cfg.remove_option('CMSSW','split_by_run')
130 
131  crab_cfg.set('CMSSW','runselection',config.runselection)
132  #if hasattr(config,'totalnumberevents'): crab_cfg.set('CMSSW','total_number_of_events',config.totalnumberevents)
133  #if hasattr(config,'eventsperjob'): crab_cfg.set('CMSSW','events_per_job',config.eventsperjob)
134  if hasattr(config,'splitByLumi') and config.splitByLumi:
135  crab_cfg.set('CMSSW','total_number_of_lumis',config.totalnumberlumis)
136  crab_cfg.set('CMSSW','lumis_per_job',config.lumisperjob)
137  if hasattr(config,'lumimask') and config.lumimask: crab_cfg.set('CMSSW','lumi_mask',config.lumimask)
138  else:
139  crab_cfg.set('CMSSW','split_by_run',1)
140 
141  # USER section
142  if not crab_cfg.has_section('USER'): crab_cfg.add_section('USER')
143 
144  # Stageout config
145  if hasattr(config,'stageOutCAF') and config.stageOutCAF:
146  crab_cfg.set('USER','return_data',0)
147  crab_cfg.set('USER','copy_data',1)
148  crab_cfg.set('USER','storage_element','T2_CH_CAF')
149  crab_cfg.set('USER','user_remote_dir',config.userdircaf)
150  crab_cfg.set('USER','check_user_remote_dir',0)
151  elif hasattr(config,'stageOutLocal') and config.stageOutLocal:
152  crab_cfg.set('USER','return_data',1)
153  crab_cfg.set('USER','copy_data',0)
154  crab_cfg.remove_option('USER','storage_element')
155  crab_cfg.remove_option('USER','user_remote_dir')
156  crab_cfg.remove_option('USER','check_user_remote_dir')
157 
158  if hasattr(config,'email') and config.email: crab_cfg.set('USER','eMail',config.email)
159  crab_cfg.set('USER','xml_report','crabReport.xml')
160 
161  if hasattr(config,'runOnGrid') and config.runOnGrid:
162  crab_cfg.remove_section('CAF')
def parseInput
Definition: tools.py:75
def listFilesInCastor
Definition: tools.py:14
def loadCrabDefault
Definition: tools.py:110
def listFilesLocal
Definition: tools.py:24
def loadCmsProcess
Definition: tools.py:93
def haddLocal
Definition: tools.py:51
def replaceTemplate
Definition: tools.py:4
def haddInCastor
Definition: tools.py:34
def loadCrabCfg
Definition: tools.py:105
def setGridEnv
Definition: tools.py:63
def prependPaths
Definition: tools.py:97
def writeCfg
Definition: tools.py:101