CMS 3D CMS Logo

Functions | Variables
heppy_loop Namespace Reference

Functions

def callBack (result)
 
def chunks (l, n)
 
def createOutputDir (dir, components, force)
 
def getHeppyOption (name, default=None)
 
def main (options, args, parser)
 
def runLoop (comp, outDir, config, options)
 
def runLoopAsync (comp, outDir, configName, options)
 
def setHeppyOption (name, value=True)
 
def split (comps)
 

Variables

 _heppyGlobalOptions
 
 action
 
 args
 
 argv
 
 default
 
 dest
 
 help
 
 loop
 
 oldv
 
 options
 
 parser
 
 type
 
 usage
 

Function Documentation

def heppy_loop.callBack (   result)

Definition at line 28 of file heppy_loop.py.

References harvestTrackValidationPlots.str.

28 def callBack( result ):
29  pass
30  print 'production done:', str(result)
31 
def callBack(result)
Definition: heppy_loop.py:28
def heppy_loop.chunks (   l,
  n 
)

Definition at line 93 of file heppy_loop.py.

Referenced by split().

93 def chunks(l, n):
94  return [l[i:i+n] for i in range(0, len(l), n)]
95 
def chunks(l, n)
Definition: heppy_loop.py:93
def heppy_loop.createOutputDir (   dir,
  components,
  force 
)
Creates the output dir, dealing with the case where dir exists.

Definition at line 66 of file heppy_loop.py.

References join().

Referenced by main().

66 def createOutputDir(dir, components, force):
67  '''Creates the output dir, dealing with the case where dir exists.'''
68  answer = None
69  try:
70  os.mkdir(dir)
71  return True
72  except OSError:
73  print 'directory %s already exists' % dir
74  print 'contents: '
75  dirlist = [path for path in os.listdir(dir) if os.path.isdir( '/'.join([dir, path]) )]
76  pprint( dirlist )
77  print 'component list: '
78  print [comp.name for comp in components]
79  if force is True:
80  print 'force mode, continue.'
81  return True
82  else:
83  while answer not in ['Y','y','yes','N','n','no']:
84  answer = raw_input('Continue? [y/n]')
85  if answer.lower().startswith('n'):
86  return False
87  elif answer.lower().startswith('y'):
88  return True
89  else:
90  raise ValueError( ' '.join(['answer can not have this value!',
91  answer]) )
92 
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def createOutputDir(dir, components, force)
Definition: heppy_loop.py:66
def heppy_loop.getHeppyOption (   name,
  default = None 
)

Definition at line 127 of file heppy_loop.py.

127 def getHeppyOption(name,default=None):
128  global _heppyGlobalOptions
129  return _heppyGlobalOptions[name] if name in _heppyGlobalOptions else default
def getHeppyOption(name, default=None)
Definition: heppy_loop.py:127
def heppy_loop.main (   options,
  args,
  parser 
)

Definition at line 134 of file heppy_loop.py.

References createOutputDir(), min(), runLoop(), and split().

134 def main( options, args, parser ):
135 
136  if len(args) != 2:
137  parser.print_help()
138  print 'ERROR: please provide the processing name and the component list'
139  sys.exit(1)
140 
141  outDir = args[0]
142  if os.path.exists(outDir) and not os.path.isdir( outDir ):
143  parser.print_help()
144  print 'ERROR: when it exists, first argument must be a directory.'
145  sys.exit(2)
146  cfgFileName = args[1]
147  if not os.path.isfile( cfgFileName ):
148  parser.print_help()
149  print 'ERROR: second argument must be an existing file (your input cfg).'
150  sys.exit(3)
151 
152  if options.verbose:
153  import logging
154  logging.basicConfig(level=logging.INFO)
155 
156  # Propagate global options to _heppyGlobalOptions within this module
157  # I have to import it explicitly, 'global' does not work since the
158  # module is not set when executing the main
159  from PhysicsTools.HeppyCore.framework.heppy_loop import _heppyGlobalOptions
160  for opt in options.extraOptions:
161  if "=" in opt:
162  (key,val) = opt.split("=",1)
163  _heppyGlobalOptions[key] = val
164  else:
165  _heppyGlobalOptions[opt] = True
166 
167  file = open( cfgFileName, 'r' )
168  cfg = imp.load_source( 'PhysicsTools.HeppyCore.__cfg_to_run__', cfgFileName, file)
169 
170  selComps = [comp for comp in cfg.config.components if len(comp.files)>0]
171  selComps = split(selComps)
172  # for comp in selComps:
173  # print comp
174  if len(selComps)>options.ntasks:
175  print "WARNING: too many threads {tnum}, will just use a maximum of {jnum}.".format(tnum=len(selComps),jnum=options.ntasks)
176  if not createOutputDir(outDir, selComps, options.force):
177  print 'exiting'
178  sys.exit(0)
179  if len(selComps)>1:
180  shutil.copy( cfgFileName, outDir )
181  pool = Pool(processes=min(len(selComps),options.ntasks))
182  ## workaround for a scoping problem in ipython+multiprocessing
183  import PhysicsTools.HeppyCore.framework.heppy_loop as ML
184  for comp in selComps:
185  print 'submitting', comp.name
186  pool.apply_async( ML.runLoopAsync, [comp, outDir, 'PhysicsTools.HeppyCore.__cfg_to_run__', options],
187  callback=ML.callBack)
188  pool.close()
189  pool.join()
190  else:
191  # when running only one loop, do not use multiprocessor module.
192  # then, the exceptions are visible -> use only one sample for testing
193  global loop
194  loop = runLoop( comp, outDir, cfg.config, options )
195  return loop
196 
def split(comps)
Definition: heppy_loop.py:96
def runLoop(comp, outDir, config, options)
Definition: heppy_loop.py:44
T min(T a, T b)
Definition: MathUtil.h:58
def main(options, args, parser)
Definition: heppy_loop.py:134
def createOutputDir(dir, components, force)
Definition: heppy_loop.py:66
def heppy_loop.runLoop (   comp,
  outDir,
  config,
  options 
)

Definition at line 44 of file heppy_loop.py.

References createfilelist.int, and join().

Referenced by main(), and runLoopAsync().

44 def runLoop( comp, outDir, config, options):
45  fullName = '/'.join( [outDir, comp.name ] )
46  # import pdb; pdb.set_trace()
47  config.components = [comp]
48  loop = Looper( fullName,
49  config,
50  options.nevents, 0,
51  nPrint = options.nprint,
52  timeReport = options.timeReport,
53  quiet = options.quiet)
54  # print loop
55  if options.iEvent is None:
56  loop.loop()
57  loop.write()
58  # print loop
59  else:
60  # loop.InitOutput()
61  iEvent = int(options.iEvent)
62  loop.process( iEvent )
63  return loop
64 
65 
def runLoop(comp, outDir, config, options)
Definition: heppy_loop.py:44
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def heppy_loop.runLoopAsync (   comp,
  outDir,
  configName,
  options 
)

Definition at line 32 of file heppy_loop.py.

References runLoop().

32 def runLoopAsync(comp, outDir, configName, options):
33  try:
34  loop = runLoop( comp, outDir, copy.copy(sys.modules[configName].config), options)
35  return loop.name
36  except Exception:
37  import traceback
38  print "ERROR processing component %s" % comp.name
39  print comp
40  print "STACK TRACE: "
41  print traceback.format_exc()
42  raise
43 
def runLoopAsync(comp, outDir, configName, options)
Definition: heppy_loop.py:32
def runLoop(comp, outDir, config, options)
Definition: heppy_loop.py:44
def heppy_loop.setHeppyOption (   name,
  value = True 
)

Definition at line 130 of file heppy_loop.py.

130 def setHeppyOption(name,value=True):
131  global _heppyGlobalOptions
132  _heppyGlobalOptions[name] = value
133 
def setHeppyOption(name, value=True)
Definition: heppy_loop.py:130
def heppy_loop.split (   comps)

Definition at line 96 of file heppy_loop.py.

References chunks().

Referenced by main().

96 def split(comps):
97  # import pdb; pdb.set_trace()
98  splitComps = []
99  for comp in comps:
100  if hasattr( comp, 'fineSplitFactor') and comp.fineSplitFactor>1:
101  subchunks = range(comp.fineSplitFactor)
102  for ichunk, chunk in enumerate([(f,i) for f in comp.files for i in subchunks]):
103  newComp = copy.deepcopy(comp)
104  newComp.files = [chunk[0]]
105  newComp.fineSplit = ( chunk[1], comp.fineSplitFactor )
106  newComp.name = '{name}_Chunk{index}'.format(name=newComp.name,
107  index=ichunk)
108  splitComps.append( newComp )
109  elif hasattr( comp, 'splitFactor') and comp.splitFactor>1:
110  chunkSize = len(comp.files) / comp.splitFactor
111  if len(comp.files) % comp.splitFactor:
112  chunkSize += 1
113  # print 'chunk size',chunkSize, len(comp.files), comp.splitFactor
114  for ichunk, chunk in enumerate( chunks( comp.files, chunkSize)):
115  newComp = copy.deepcopy(comp)
116  newComp.files = chunk
117  newComp.name = '{name}_Chunk{index}'.format(name=newComp.name,
118  index=ichunk)
119  splitComps.append( newComp )
120  else:
121  splitComps.append( comp )
122  return splitComps
123 
124 
def split(comps)
Definition: heppy_loop.py:96
def chunks(l, n)
Definition: heppy_loop.py:93

Variable Documentation

heppy_loop._heppyGlobalOptions
private

Definition at line 125 of file heppy_loop.py.

heppy_loop.action

Definition at line 27 of file heppy_loop.py.

heppy_loop.args

Definition at line 62 of file heppy_loop.py.

heppy_loop.argv

Definition at line 17 of file heppy_loop.py.

heppy_loop.default

Definition at line 16 of file heppy_loop.py.

heppy_loop.dest

Definition at line 13 of file heppy_loop.py.

heppy_loop.help

Definition at line 15 of file heppy_loop.py.

heppy_loop.oldv

Definition at line 16 of file heppy_loop.py.

heppy_loop.options

Definition at line 62 of file heppy_loop.py.

heppy_loop.parser

Definition at line 5 of file heppy_loop.py.

heppy_loop.type

Definition at line 14 of file heppy_loop.py.

heppy_loop.usage

Definition at line 6 of file heppy_loop.py.