Index: trunk/FACT++/src/root2csv.cc
===================================================================
--- trunk/FACT++/src/root2csv.cc	(revision 19792)
+++ trunk/FACT++/src/root2csv.cc	(revision 19794)
@@ -51,9 +51,9 @@
         ("force,f",        po_switch(),               "Force overwrite if output file already exists.")
         ("append,a",       po_switch(),               "Append to an existing file (not check for the format is done!)")
-        ("create",         po_switch(),               "Create the database if not existing")
         ("tree,t",         var<string>("Events"),     "Name of the root tree to convert")
         ("ignore",         vars<string>(),            "Ignore the given leaf, if the given regular expression matches")
         ("alias.*",        var<string>(),             "Define an alias")
         ("auto-alias",     vars<Map>(),               "Regular expression to define aliases from the branch names automatically")
+        ("header",         var<uint16_t>(uint16_t(0)),"Type of header line (0: preceeding #, 1: without preceeding #, 2: none)")
         ("add.*",          var<string>(),             "Define an additional column")
         ("selector",       var<string>("1"),          "Define a selector for the columns (colums where this evaluates to a value <=0 are discarded)")
@@ -61,5 +61,5 @@
         ("first",          var<int64_t>(int64_t(0)),  "First event to start with (default: 0), mainly for test purpose")
         ("max",            var<int64_t>(int64_t(0)),  "Maximum number of events to process (0: all), mainly for test purpose")
-        ("const.*",        var<string>(),             "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/.../.../`")
+        //("const.*",        var<string>(),             "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/.../.../`")
         ("dry-run",        po_switch(),               "Do not create or manipulate any output file")
         ;
@@ -329,7 +329,12 @@
 
 // --------------------------- Write Header --------------------------------
-void WriteHeader(ostream &out, const vector<Container> &vec, const vector<TTreeFormula*> &form, bool skip)
-{
-    out << "#";
+void WriteHeader(ostream &out, const vector<Container> &vec, const vector<TTreeFormula*> &form, bool skip, uint16_t header)
+{
+    if (header>1)
+        return;
+    if (header==0)
+        out << "# ";
+
+    vector<string> join;
 
     if (!skip)
@@ -340,7 +345,8 @@
             for (size_t i=0; i<N; i++)
             {
-                out << " " << v->column;
+                string name = v->column;
                 if (N!=1)
-                    out << "["+to_string(i)+"]";
+                    name += "["+to_string(i)+"]";
+                join.emplace_back(name);
             }
         }
@@ -348,7 +354,7 @@
 
     for (auto v=form.cbegin(); v!=form.cend(); v++)
-        out << " " << (*v)->GetName();
-
-    out << "\n";
+        join.emplace_back((*v)->GetName());
+
+    out << boost::join(join, " ") << "\n";
 }
 
@@ -422,4 +428,5 @@
     const int64_t  first         = conf.Get<int64_t>("first");
     const int64_t  max           = conf.Get<int64_t>("max");
+    const uint16_t header        = conf.Get<uint16_t>("header");
 
     const bool print_ls          = conf.Get<bool>("print-ls");
@@ -482,5 +489,5 @@
     for (const auto &file : files)
     {
-        const auto add = c.Add(file.c_str());
+        const auto add = c.Add(file.c_str(), 0);
         if (verbose>0)
             cout << file << ": " << add << " file(s) added." << endl;
@@ -515,5 +522,5 @@
     }
 
-    const auto entries = c.GetEntries();
+    const auto entries = c.GetEntriesFast();
 
     if (verbose>0)
@@ -587,6 +594,6 @@
     */
 
-    if (autoalias.size())
-    {
+    // ------------------------- Setup all branches in tree -------------------
+
         TIter Next(leaves);
         TObject *o = 0;
@@ -594,120 +601,20 @@
         {
             TLeaf *L = c.GetLeaf(o->GetName());
-            if (L->GetLenStatic()!=L->GetLen())
-                continue;
 
             string name = o->GetName();
-/*
-            bool found = false;
-            for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++)
-            {
-                if (boost::regex_match(name, boost::regex(*b)))
-                {
-                    found = true;
-                    break;
-                }
-            }
-            if (found)
-                continue;
-*/
-            const string tn = L->GetTypeName();
-            auto it = ConvRoot.find(tn);
-            if (it==ConvRoot.end())
-                continue;
 
             for (auto m=autoalias.cbegin(); m!=autoalias.cend(); m++)
                 name = boost::regex_replace(name, boost::regex(m->first), m->second);
 
-            if (name==o->GetName())
+            if (name!=o->GetName())
+            {
+                if (verbose>0)
+                    cout << "Auto-alias: " << name << " = " << o->GetName() << endl;
+                if (!c.SetAlias(name.c_str(), o->GetName()))
+                    cout << "WARNING - Alias could not be established!" << endl;
+            }
+
+            if (skip)
                 continue;
-
-            if (verbose>0)
-                cout << "Auto-alias: " << name << " = " << o->GetName() << endl;
-
-            if (!c.SetAlias(name.c_str(), o->GetName()))
-                cout << "WARNING - Alias could not be established!" << endl;
-        }
-    }
-
-    // ------------------------ Configure Aliases -----------------------------
-
-    const auto valiases = conf.GetWildcardOptions("alias.*");
-    if (verbose>0 && valiases.size()>0)
-        cout << '\n';
-    for (auto it=valiases.cbegin(); it!=valiases.cend(); it++)
-    {
-        const string name = it->substr(6);
-        const string val  = conf.Get<string>(*it);
-
-        if (verbose>0)
-            cout << "Alias: " << name << " = " << val << endl;
-
-        if (!c.SetAlias(name.c_str(), val.c_str()))
-        {
-            cerr << "Alias could not be established!" << endl;
-            return 2;
-        }
-    }
-
-    // -------------------------- Configure Selector --------------------------
-
-    vector<string> leaflist;
-    c.SetBranchStatus("*", 1);
-
-    TTreeFormulaManager *manager = new TTreeFormulaManager;
-
-    if (verbose>0)
-        cout << "\nSelector: " <<  conf.Get<string>("selector") << endl;
-
-    TTreeFormula selector("Selector", conf.Get<string>("selector").c_str(), &c);
-    if (selector.GetNdim()==0)
-    {
-        cerr << "Compilation of Selector failed!" << endl;
-        return 3;
-    }
-    selector.SetQuickLoad(kTRUE);
-    manager->Add(&selector);
-    GetLeaves(leaflist, selector);
-
-    // -------------------- Configure additional columns ----------------------
-
-    vector<TTreeFormula*> formulas;
-
-    const auto vform = conf.GetWildcardOptions("add.*");
-    if (verbose>0 && vform.size()>0)
-        cout << '\n';
-    for (auto it=vform.cbegin(); it!=vform.cend(); it++)
-    {
-        const string name = it->substr(4);
-        const string val  = conf.Get<string>(*it);
-
-        if (verbose>0)
-            cout << "Column: " << name << " = " << val << endl;
-
-        TTreeFormula *form = new TTreeFormula(name.c_str(), val.c_str(), &c);
-        if (form->GetNdim()==0)
-        {
-            cerr << "Compilation of Column failed!" << endl;
-            return 4;
-        }
-        form->SetQuickLoad(kTRUE);
-        formulas.emplace_back(form);
-        manager->Add(form);
-        GetLeaves(leaflist, *form);
-    }
-    manager->Sync();
-
-    if (verbose>0)
-        cout << '\n' << formulas.size() << " additional columns setup for writing." << endl;
-
-    // ------------------------- Setup all branches in tree -------------------
-
-    if (!skip)
-    {
-        TIter Next(leaves);
-        TObject *o = 0;
-        while ((o=Next()))
-        {
-            TLeaf *L = c.GetLeaf(o->GetName());
 
             if (verbose>2)
@@ -721,10 +628,8 @@
             }
 
-            string name = o->GetName();
-
             bool found = false;
             for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++)
             {
-                if (boost::regex_match(name, boost::regex(*b)))
+                if (boost::regex_match(o->GetName(), boost::regex(*b)))
                 {
                     found = true;
@@ -734,4 +639,5 @@
                 }
             }
+
             if (found)
                 continue;
@@ -756,8 +662,83 @@
             c.SetBranchAddress(o->GetTitle(), vec.back().ptr);
         }
-    }
-
-    if (verbose>0)
-        cout << vec.size() << " default leaf/leaves setup for reading." << endl;
+
+        if (verbose>0)
+        {
+            if (skip)
+                cout << "Default columns skipped: ";
+            cout << vec.size() << " default leaf/leaves setup for reading." << endl;
+        }
+
+
+    // ------------------- Configure manual aliases ----------------------------
+
+    const auto valiases = conf.GetWildcardOptions("alias.*");
+    if (verbose>0 && valiases.size()>0)
+        cout << '\n';
+    for (auto it=valiases.cbegin(); it!=valiases.cend(); it++)
+    {
+        const string name = it->substr(6);
+        const string val  = conf.Get<string>(*it);
+
+        if (verbose>0)
+            cout << "Alias: " << name << " = " << val << endl;
+
+        if (!c.SetAlias(name.c_str(), val.c_str()))
+        {
+            cerr << "Alias could not be established!" << endl;
+            return 2;
+        }
+    }
+
+    // -------------------------- Configure Selector --------------------------
+
+    vector<string> leaflist;
+    c.SetBranchStatus("*", 1);
+
+    TTreeFormulaManager *manager = new TTreeFormulaManager;
+
+    if (verbose>0)
+        cout << "\nSelector: " <<  conf.Get<string>("selector") << endl;
+
+    TTreeFormula selector("Selector", conf.Get<string>("selector").c_str(), &c);
+    if (selector.GetNdim()==0)
+    {
+        cerr << "Compilation of Selector failed!" << endl;
+        return 3;
+    }
+    selector.SetQuickLoad(kTRUE);
+    manager->Add(&selector);
+    GetLeaves(leaflist, selector);
+
+    // -------------------- Configure additional columns ----------------------
+
+    vector<TTreeFormula*> formulas;
+
+    const auto vform = conf.GetWildcardOptions("add.*");
+    if (verbose>0 && vform.size()>0)
+        cout << '\n';
+    for (auto it=vform.cbegin(); it!=vform.cend(); it++)
+    {
+        const string name = it->substr(4);
+        const string val  = conf.Get<string>(*it);
+
+        if (verbose>0)
+            cout << "Adding column: " << name << " = " << val << endl;
+
+        TTreeFormula *form = new TTreeFormula(name.c_str(), val.c_str(), &c);
+        if (form->GetNdim()==0)
+        {
+            cerr << "Compilation of Column failed!" << endl;
+            return 4;
+        }
+        form->SetQuickLoad(kTRUE);
+        formulas.emplace_back(form);
+        manager->Add(form);
+        GetLeaves(leaflist, *form);
+    }
+    manager->Sync();
+
+    if (verbose>0)
+        cout << '\n' << formulas.size() << " additional columns setup for writing." << endl;
 
     // --------------------- Setup all branches in formulas -------------------
@@ -850,5 +831,5 @@
         outfiles.emplace_back(path.Data(), append ? ios::app : ios::trunc);
         if (rc==-1 || (force && rc==0 && !append))
-            WriteHeader(outfiles.back(), vec, formulas, skip);
+            WriteHeader(outfiles.back(), vec, formulas, skip, header);
     }
     else
@@ -865,5 +846,5 @@
             outfiles.emplace_back(path.Data(), append ? ios::app : ios::trunc);
             if (rc==-1 || (force && rc==0 && !append))
-                WriteHeader(outfiles.back(), vec, formulas, skip);
+                WriteHeader(outfiles.back(), vec, formulas, skip, header);
         }
     }
@@ -902,4 +883,6 @@
         }
 
+        vector<string> join;
+
         if (!skip)
         {
@@ -908,22 +891,12 @@
                 const size_t N = v->num;
                 for (size_t i=0; i<N; i++)
-                {
-                    if (v!=vec.cbegin() || i>0)
-                        outfiles[index] << " ";
-
-                    outfiles[index] << v->fmt(i);
-                }
+                    join.emplace_back(v->fmt(i));
             }
         }
 
         for (auto v=formulas.cbegin(); v!=formulas.cend(); v++)
-        {
-            if (v!=formulas.cbegin() || (vec.size()>0 && !skip))
-                outfiles[index] << " ";
-
-            outfiles[index] << setprecision(8) << (*v)->EvalInstance(0);
-        }
-
-        outfiles[index] << "\n";
+            join.emplace_back(to_string((*v)->EvalInstance(0)));
+
+        outfiles[index] << boost::join(join, " ") << "\n";
 
         count ++;
