Index: trunk/FACT++/src/rootifysql.cc
===================================================================
--- trunk/FACT++/src/rootifysql.cc	(revision 19073)
+++ trunk/FACT++/src/rootifysql.cc	(revision 19074)
@@ -3,4 +3,7 @@
 #include <regex>
 
+#include <boost/algorithm/string/join.hpp>
+
+#include "tools.h"
 #include "Time.h"
 #include "Configuration.h"
@@ -33,4 +36,5 @@
         ("no-fill",       po_switch(),                 "Do not fill events into the root file (mainly for debugging purposes, e.g. performance studies)")
         ("delimiter",     var<string>(""),             "The delimiter used if contents are displayed with --display (default=\\t)")
+        ("var.*",         var<string>(),               "Predefined user variables")
         ("verbose,v",     var<uint16_t>(1),            "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
         ;
@@ -70,4 +74,7 @@
         "As a trick, the rootify.sql file can be made excutable (chmod u+x rootify.sql). "
         "If the first line contains '#!rootifysql', the script can be executed directly.\n"
+        "\n"
+        "You can use variables in your sql query like @MyVar and define them on the "
+        "command line. In this example with --var.MyVar=5\n"
         "\n"
         "Comments in the query-file can be placed according to the SQL standard inline "
@@ -113,4 +120,10 @@
     // -------------------------------------------------------------------------
 
+    const auto vars = conf.GetWildcardOptions("var.*");
+
+    vector<string> variables;
+    for (const auto &var : vars)
+        variables.emplace_back('@'+var.substr(4)+":="+Tools::Trim(conf.Get<string>(var)));
+
     if (verbose>0)
         cout << "\n--------------------- Rootify SQL ----------------------" << endl;
@@ -165,22 +178,52 @@
         }
     }
-    // -------------------------------------------------------------------------
+
+    Time start2;
+
+    // --------------------------- Connect to database -------------------------------------------------
 
     if (query.back()!='\n')
         query += '\n';
 
+    if (verbose>0)
+        cout << "Connecting to database..." << endl;
+
+
+    Database connection(uri); // Keep alive while fetching rows
+
+    // -------------------------- Set user defined variables -------------------
+    if (variables.size()>0)
+    {
+        if (verbose>0)
+            cout << "Setting user defined variables..." << endl;
+
+        const string varset =
+            "SET\n   "+boost::algorithm::join(variables, ",\n   ");
+
+        try
+        {
+            connection.query(varset).execute();
+        }
+        catch (const exception &e)
+        {
+            cerr << varset << "\n\n";
+            cerr << "SQL query failed:\n" << e.what() << endl;
+            return 5;
+        }
+
+        if (verbose>2)
+            cout << '\n' << varset << '\n' << endl;
+    }
+
+    // -------------------------- Request data from database -------------------
+    if (verbose>0)
+        cout << "Requesting data..." << endl;
+
     if (verbose>2)
         cout << '\n' << query << endl;
 
-    if (verbose>0)
-        cout << "Requesting data..." << endl;
-
-    Time start2;
-
-    // -------------------------- Request data from database -------------------
-    Database connection(uri); // Keep alive while fetching rows
-
     const mysqlpp::UseQueryResult res =
         connection.query(query).use();
+
     // -------------------------------------------------------------------------
 
@@ -191,5 +234,5 @@
     TFile tfile(path, update?"UPDATE":(force?"RECREATE":"CREATE"), "Rootify SQL", compression);
     if (tfile.IsZombie())
-        return 5;
+        return 6;
 
     // -------------------------------------------------------------------------
@@ -200,5 +243,5 @@
     {
         cerr << "Empty set returned... nothing to write." << endl;
-        return 6;
+        return 7;
     }
 
