def METHOD_NAME(rootpath, trial, rungroup, run, task=None):
  import os
  try:
    p = os.path.join(rootpath, "r%04d"%int(run.run), "%03d_rg%03d"%(trial.trial, rungroup.id))
  except ValueError:
    p = os.path.join(rootpath, run.run, "%03d_rg%03d"%(trial.trial, rungroup.id))
  if task is not None:
    p = os.path.join(p, "task%03d"%task.id)
  return p