Index: trunk/FACT++/src/spectrum.cc
===================================================================
--- trunk/FACT++/src/spectrum.cc	(revision 19986)
+++ trunk/FACT++/src/spectrum.cc	(revision 19987)
@@ -133,30 +133,5 @@
          , "Database link as in\n\tuser:password@server[:port]/database[?compress=0|1].")
         ("out,o", var<string>(conf.GetName()), "Defines the prefix (with path) of the output files.")
-        ("confidence-level,c", var<double>(0.99), "Confidence level for the calculation of the upper limits.")
-        ("feldman-cousins", po_bool(), "Calculate Feldman-Cousins ULs (slow and only minor difference to Rolke).")
-        ;
-
-    po::options_description binnings("Binnings");
-    binnings.add_options()
-        ("theta",             var<Binning>(Binning(90, 0, 90)),  "Add equidistant bins in theta (degrees). Syntax: N,lo,hi (Number N of equidistant bins between lo and hi)")
-        ("theta-bin",         vars<double>(),                    "Add a bin-edge to the theta binning (degree)")
-        ("energy-dense",      var<Binning>(Binning(30, 2, 5)),   "Add equidistant bins in log10 simulated energy. Syntax: N,lo,hi (Number N of equidistant bins between lo and hi)")
-        ("energy-dense-bin",  vars<double>(),                    "Add a bin-edge to the binnnig in log10 simulated enegry")
-        ("energy-sparse",     var<Binning>(Binning(15, 2, 5)),   "Add equidistant bins in log10 estimated energy. Syntax: N,lo,hi (Number N of equidistant bins between lo and hi)")
-        ("energy-sparse-bin", vars<double>(),                    "Add a bin-edge to the binning in log10 estimated enegry")
-        ("impact",            var<Binning>(Binning(28, 0, 280)), "Add equidistant bins in impact in meter. Syntax: N,lo,hi (Number N of equidistant bins between lo and hi)")
-        ("impact-bin",        vars<double>(),                    "Add a bin-edge to the binning in impact in meter")
-        ;
-
-    po::options_description analysis("Analysis Setup");
-    analysis.add_options()
-        ("analysis",    var<string>("analysis.sql"),   "File with the analysis query. A default file is created automatically in the <prefix> directory it does not exist.")
-        ("source-key", var<uint16_t>(5),          "Source key to be used in data file selection.")
-        ("selector",   vars<string>()->required(),"WHERE clause to be used in data file selection.")
-        ("selsim",     vars<string>(),            "WHERE clause to be used in monte carlo file selection.")
-        ("estimator",  var<string>()->required(), "Energy estimator to be used.")
-        ("spectrum",   var<string>()->required(), "Spectral shape for re-weighting of simulated 'Energy'")
-        ("env.*",      var<string>(),             "Define a variable that is replaced in all queries automatically.")
-        ;
+        ("force,f", po_bool(), "Force overwriting output files.");
 
     po::options_description debug("Debug options");
@@ -173,11 +148,42 @@
         ;
 
+
+    po::options_description binnings("Binnings");
+    binnings.add_options()
+        ("theta",             var<Binning>(Binning(90, 0, 90)),  "Add equidistant bins in theta (degrees). Syntax: N,lo,hi (Number N of equidistant bins between lo and hi)")
+        ("theta-bin",         vars<double>(),                    "Add a bin-edge to the theta binning (degree)")
+        ("energy-dense",      var<Binning>(Binning(30, 2, 5)),   "Add equidistant bins in log10 simulated energy. Syntax: N,lo,hi (Number N of equidistant bins between lo and hi)")
+        ("energy-dense-bin",  vars<double>(),                    "Add a bin-edge to the binnnig in log10 simulated enegry")
+        ("energy-sparse",     var<Binning>(Binning(15, 2, 5)),   "Add equidistant bins in log10 estimated energy. Syntax: N,lo,hi (Number N of equidistant bins between lo and hi)")
+        ("energy-sparse-bin", vars<double>(),                    "Add a bin-edge to the binning in log10 estimated enegry")
+        ("impact",            var<Binning>(Binning(28, 0, 280)), "Add equidistant bins in impact in meter. Syntax: N,lo,hi (Number N of equidistant bins between lo and hi)")
+        ("impact-bin",        vars<double>(),                    "Add a bin-edge to the binning in impact in meter")
+        ;
+
+    po::options_description analysis("Analysis Setup");
+    analysis.add_options()
+        ("analysis",    var<string>("analysis.sql"),   "File with the analysis query. A default file is created automatically in the <prefix> directory it does not exist.")
+        ("source-key", var<uint16_t>(5),          "Source key to be used in data file selection (Default=Crab).")
+        ("selector",   vars<string>()->required(),"WHERE clause to be used in data file selection, selectors are concatenated by AND.")
+        ("selsim",     vars<string>(),            "WHERE clause to be used in monte carlo file selection, selsims are concatenated by AND.")
+        ("estimator",  var<string>()->required(), "Energy estimator to be used (units must be consistent with energy binnings).")
+        ("spectrum",   var<string>()->required(), "Spectral shape for re-weighting of simulated 'Energy'")
+//        ("env.*",      var<string>(),             "Define a variable that is replaced in all queries automatically.")
+        ;
+
+    po::options_description physics("Physics");
+    physics.add_options()
+        ("confidence-level,c", var<double>(0.99), "Confidence level for the calculation of the upper limits.")
+        ("feldman-cousins", po_bool(), "Calculate Feldman-Cousins ULs (slow and only minor difference to Rolke).")
+        ;
+
     //po::positional_options_description p;
     //p.add("file", 1); // The 1st positional options (n=1)
 
     conf.AddOptions(control);
+    conf.AddOptions(debug);
     conf.AddOptions(binnings);
     conf.AddOptions(analysis);
-    conf.AddOptions(debug);
+    conf.AddOptions(physics);
     //conf.SetArgumentPositions(p);
 }
@@ -627,4 +633,5 @@
     const string   uri        = conf.Get<bool>("dry-run") ? "" : conf.Get<string>("uri");
     const string   out        = conf.Get<string>("out");
+    const bool     force      = conf.Get<bool>("force");
     const uint16_t verbose    = conf.Get<uint16_t>("verbose");
     const double   confidence = conf.Get<double>("confidence-level");
@@ -652,5 +659,5 @@
     const string   estimator  = conf.Get<string>("estimator");
     const string   spectrum   = conf.Get<string>("spectrum");
-    const auto     env        = conf.GetOptions<string>("env.");
+//    const auto     env        = conf.GetOptions<string>("env.");
 
     cout << "\n";
@@ -719,29 +726,51 @@
     // Create log streams
 
+    cout << "\n";
+    cout << "Queries    will be logged  to " << out << ".query.sql\n";
+    if (connection.connected())
+    {
+        cout << "Tables     will be dumped  to " << out << ".dump.sql\n";
+        cout << "ROOT macro will be written to " << out << ".C\n";
+    }
+
+#ifdef HAVE_ROOT
+    TFile root(connection.connected() ? (out+".hist.root").c_str() : "", force ? "RECREATE" : "CREATE");
+    if (connection.connected())
+    {
+        if (root.IsZombie())
+            return 10;
+        cout << "Histograms will be written to " << out << ".hist.root\n";
+    }
+    if (verbose>0)
+        cout << "\nCalculating upper limits for a confidence interval of " << confidence << endl;
+#endif
+
+    cout << endl;
+
+    if (!force)
+    {
+        if (fs::exists(out+".query.sql"))
+        {
+            cerr << "File '" << out <<  ".query.sql' already exists!" << endl;
+            return 11;
+        }
+        if (connection.connected())
+        {
+            if (fs::exists(out+".dump.sql"))
+            {
+                cerr << "File '" << out <<  ".dump.sql' already exists!" << endl;
+                return 12;
+            }
+            if (fs::exists(out+".C"))
+            {
+                cerr << "File '" << out <<  ".C' already exists!" << endl;
+                return 13;
+            }
+        }
+    }
+
     ofstream qlog(out+".query.sql");
     ofstream flog(connection.connected() ? out+".dump.sql" : "");
     ofstream mlog(connection.connected() ? out+".C" : "");
-
-    cout << "\n";
-    cout << "Queries    will be logged  to " << out << ".query.sql\n";
-    if (connection.connected())
-    {
-        cout << "Tables     will be dumped  to " << out << ".dump.sql\n";
-        cout << "ROOT macro will be written to " << out << ".C\n";
-    }
-
-#ifdef HAVE_ROOT
-    TFile root(connection.connected() ? (out+".hist.root").c_str() : "", "RECREATE");
-    if (connection.connected())
-    {
-        if (root.IsZombie())
-            return 10;
-        cout << "Histograms will be written to " << out << ".hist.root\n";
-    }
-    if (verbose>0)
-        cout << "\nCalculating upper limits for a confidence interval of " << confidence << endl;
-#endif
-
-    cout << endl;
 
     // FIMXE: Implement SYNTAX check on spectrum, estimator and selector
@@ -1330,6 +1359,6 @@
 
     query8.parse();
-    for (auto it=env.cbegin(); it!=env.cend(); it++)
-        query8.template_defaults[it->first.c_str()] = it->second.c_str();
+//    for (auto it=env.cbegin(); it!=env.cend(); it++)
+//        query8.template_defaults[it->first.c_str()] = it->second.c_str();
 
     //query6.template_defaults["columns"]   = "FileId, EvtNumber, CorsikaNumReuse,";
@@ -1998,6 +2027,6 @@
 
     query12.parse();
-    for (auto it=env.cbegin(); it!=env.cend(); it++)
-        query12.template_defaults[it->first.c_str()] = it->second.c_str();
+//    for (auto it=env.cbegin(); it!=env.cend(); it++)
+//        query12.template_defaults[it->first.c_str()] = it->second.c_str();
 
     //query5.template_defaults["columns"]   = "FileId, EvtNumber,";
