+op.add_option("-v", "--debug", dest="DEBUG", action="store_true", default=False, help="Turn on some debug messages")
+op.add_option("-q", "--quiet", dest="QUIET", action="store_true", default=False, help="turn off messages")
+op.add_option("--wfile", dest="WFILE", default=None, help="Path to a weight file, used to restrict ipol building to a subset of bins (default: %default)")
+op.add_option("--limit-errs", dest="USE_RUNSDIR", action="store_true", default=False, help="Re-read the runsdir to regularise error ipols")
+op.add_option("--limits", dest="LIMITS", default=None, help="Simple text file with parameter limits and fixed parameters")
+op.add_option("--filter", dest="FILTER", action="store_true", default=False, help="Filter out data bins that have 0 error")
+op.add_option("--target", dest="TARGET", type=float, default=2, help="Target factor for delta chi2")
+op.add_option("--kernel", dest="KERNELCOV", action="store_true", default=False, help="Estimate covariance matrix using emcee")
+op.add_option("--kernel-np", dest="KERNELCOVNP", type=int, default=1000, help="Number of emcee points to generate (default: %default)")
+opts, args = op.parse_args()
+
+## Get mandatory arguments, same as prof2-tune
+if len(args) < 1:
+ print "Argument missing... exiting\n\n"
+ op.print_usage()
+ sys.exit(1)
+REFDIR = opts.DATADIR
+if REFDIR is None:
+ print "Error, no data directory specified (-d/--datadir), exiting\n\n"
+ op.print_usage()
+ sys.exit(1)
+IFILES = args
+RUNSDIR = opts.RUNSDIR
+
+# Sanity
+if not os.path.exists(REFDIR):
+ print "Error, specified data directory '%s' does not exist, exiting\n\n"%REFDIR
+ op.print_usage()
+ sys.exit(1)
+if not os.path.exists(opts.OUTDIR):
+ os.makedirs(opts.OUTDIR)
+
+if opts.RESULT is None:
+ print "Error, no result file given"
+ sys.exit(1)
+
+## Load Professor and show the standard banner
+import professor2 as prof
+if not opts.QUIET:
+ print prof.logo
+
+# Read data files
+DHISTOS = prof.read_all_histos(REFDIR)
+
+from collections import OrderedDict
+MASTERBOX=OrderedDict()
+MASTERCENTER=OrderedDict()
+
+## Weight file parsing --- by default read from results file
+matchers = prof.read_pointmatchers(opts.WFILE) if opts.WFILE else prof.read_pointmatchersfromresults(opts.RESULT)
+
+## Try to read run histos and extract maximum errors --- NOTE this bit might be broken with patches NOTE
+MAXERRDICT = None
+if opts.USE_RUNSDIR:
+ try:
+ _, RUNHISTOS = prof.read_all_rundata(RUNSDIR, None) #< don't care about reading params files
+ MAXERRDICT = prof.find_maxerrs(RUNHISTOS)
+ except:
+ print "Could not read run data for error regularisation -- chi2 may be unstable"