CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CalibMuon/DTCalibration/python/Workflow/CrabTask.py

Go to the documentation of this file.
00001 from crabWrap import crabCreate,crabSubmit,crabWatch,getOutput
00002 from tools import replaceTemplate
00003 import os
00004 #from threading import Thread
00005 
00006 #class CrabTask(Thread):
00007 class CrabTask:
00008     def __init__(self, dir, crab_cfg, pset=None, pset_name='mypset.py'):
00009         #Thread.__init__(self)
00010         self.dir = dir
00011   
00012         self.crabCfg_name = 'crab.cfg'
00013         self.crab_cfg = crab_cfg
00014   
00015         self.pset_name = pset_name
00016         self.pset = pset
00017         self.initializeTask(dir=self.dir)
00018 
00019     def initializeTask(self, dir):
00020         if not os.path.exists(dir): os.makedirs(dir)
00021 
00022         # Write pset 
00023         if self.pset:
00024             self.crab_cfg.set('CMSSW','pset',self.pset_name)
00025             open(dir + '/' + self.pset_name,'w').write(self.pset) 
00026 
00027         # Write CRAB cfg
00028         self.crab_cfg.write(open(dir + '/' + self.crabCfg_name,'w'))
00029          
00030     def create(self,dir):
00031         self.project = crabCreate(dir,self.crabCfg_name)
00032         return self.project
00033 
00034     def submit(self):
00035         if not self.project: raise RuntimeError
00036         crabSubmit(self.project)
00037 
00038     def getoutput(self):
00039         if not self.project: raise RuntimeError
00040         getOutput(self.project)
00041 
00042     #def watch(self):
00043     #    if not self.project: raise RuntimeError
00044     #    crabWatch(getOutput,self.project) 
00045         
00046     def run(self):
00047         #self.initializeTask(dir=self.dir)
00048         proj = self.create(self.dir) 
00049         self.submit()
00050         return proj
00051 
00052 if __name__ == '__main__':
00053 
00054     run = None
00055     trial = None
00056     import sys
00057     for opt in sys.argv:
00058         if opt[:4] == 'run=':
00059             run = opt[4:] 
00060         if opt[:6] == 'trial=':
00061             trial = opt[6:] 
00062  
00063     if not run: raise ValueError,'Need to set run number' 
00064     if not trial: raise ValueError,'Need to set trial number'
00065 
00066     #run = 100850
00067     #ntrial = 1 
00068     pset_name = 'DTTTrigCalibration_cfg.py'
00069 
00070     crab_template = 'templates/crab/crab_ttrig_prod_TEMPL.cfg'
00071     crab_opts = {'DATASETPATH':'/Cosmics/Commissioning09-v3/RAW',
00072                  'RUNNUMBER':run,
00073                  'PSET':pset_name, 
00074                  'USERDIRCAF':'TTRIGCalibration/Run' + str(run) + '/v' + str(trial),
00075                  'EMAIL':'vilela@to.infn.it'}
00076 
00077     pset_template = 'templates/config/DTTTrigCalibration_TEMPL_cfg.py' 
00078     pset_opts = {'GLOBALTAG':'CRAFT_31X::All',
00079                  'DIGITEMPLATE':'muonDTDigis'}
00080 
00081     dir = 'Run%s'%run
00082     dir += '/Ttrig/Production'
00083 
00084     crab_cfg = replaceTemplate(crab_template,**crab_opts)
00085     pset = replaceTemplate(pset_template,**pset_opts)
00086 
00087     prod = CrabTask(dir,crab_cfg,pset,pset_name)
00088     project = prod.run()
00089 
00090     print "Sent jobs with project",project