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 29 of file heppy_loop.py.

References edm.print(), and str.

29 def callBack( result ):
30  pass
31  print('production done:', str(result))
32 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
#define str(s)
def callBack(result)
Definition: heppy_loop.py:29
def heppy_loop.chunks (   l,
  n 
)

Definition at line 94 of file heppy_loop.py.

Referenced by split().

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

Definition at line 67 of file heppy_loop.py.

References join(), and edm.print().

Referenced by main().

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

Definition at line 128 of file heppy_loop.py.

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

Definition at line 135 of file heppy_loop.py.

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

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

Definition at line 45 of file heppy_loop.py.

References createfilelist.int, and join().

Referenced by main(), and runLoopAsync().

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

Definition at line 33 of file heppy_loop.py.

References edm.print(), and runLoop().

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

Definition at line 131 of file heppy_loop.py.

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

Definition at line 97 of file heppy_loop.py.

References chunks().

Referenced by main().

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

Variable Documentation

heppy_loop._heppyGlobalOptions
private

Definition at line 126 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 18 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 17 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.